[Bio] / FigKernelPackages / representative_sequences.pm Repository:
ViewVC logotype

Annotation of /FigKernelPackages/representative_sequences.pm

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.2 - (view) (download) (as text)

1 : golsen 1.1 package representative_sequences;
2 :    
3 :     use strict;
4 :     use gjoparseblast;
5 :    
6 :     # These four variables are used as globals below. They would be changed
7 :     # for the SEED environment. formatdb is run as a system command, and might
8 :     # be encapsulated in a run() construct for the SEED. blastall is in an
9 :     # input pipe.
10 :    
11 :     my $tmp_dir = ".";
12 :     my $formatdb = "formatdb";
13 :     my $blastall = "blastall";
14 :    
15 :     require Exporter;
16 :    
17 :     our @ISA = qw(Exporter);
18 :     our @EXPORT = qw( representative_sequences
19 :     rep_seq_2
20 :     );
21 :    
22 :     #===============================================================================
23 :     # Construct a representative set of related sequences:
24 :     #
25 :     # \@repseqs = representative_sequences( $ref, \@seqs, $max_sim, \%options );
26 :     #
27 :     # or
28 :     #
29 :     # ( \@repseqs, \%representing, \@low_sim ) = representative_sequences( $ref,
30 :     # \@seqs, $max_sim, \%options );
31 :     #
32 : golsen 1.2 # Build or add to a set of representative sequences (if you to not want an
33 :     # enrichment of sequences around a focus sequence (called the reference), this
34 :     # is probably the subroutine that you want).
35 :     #
36 :     # \@reps = rep_seq_2( \@reps, \@new, \%options );
37 :     # \@reps = rep_seq_2( \@new, \%options );
38 :     #
39 :     # or
40 :     #
41 :     # ( \@reps, \%representing ) = rep_seq_2( \@reps, \@new, \%options );
42 :     # ( \@reps, \%representing ) = rep_seq_2( \@new, \%options );
43 : golsen 1.1 #
44 :     # Output:
45 :     #
46 :     # \@repseqs Reference to the list of retained (representative subset)
47 :     # of sequence entries. Sequence entries have the form
48 :     # [ $id, $def, $seq ]
49 :     #
50 :     # \%representing
51 :     # Reference to a hash in which the keys are the ids of the
52 :     # representative sequences, for which the corresponding value
53 :     # is a list of ids of other sequences that are represented by
54 :     # that representive.
55 :     #
56 :     #
57 :     # Arguments (only \@seqs is required):
58 :     #
59 :     # $ref A reference sequence as [ $id, $def, $seq ]. If present, the
60 :     # reference sequence defines a focal point for the analysis. A
61 :     # representative sequence from each lineage in its vicinity will
62 :     # be retained, even though they are more similar than max_sim to
63 :     # the reference, or to each other. The reference will always be
64 :     # included in the representative set. A limit is put on the
65 :     # similarity of lineages retained by the reference sequence with
66 :     # the max_ref_sim option (default = 0.99). The reference sequence
67 : golsen 1.2 # should not be repeated in the set of other sequences. (Only
68 :     # applies to representative_sequences; there is no equivalent for
69 :     # rep_seq_2.)
70 :     #
71 :     # \@reps In rep_seq_2, these sequences will each be placed in their own
72 :     # cluster, regardless of their similarity to one another. Each
73 :     # remaining sequence is added to the cluster to which it is
74 :     # most similar, unless it is less simililar than max_sim, in
75 :     # which case it represents a new cluster.
76 : golsen 1.1 #
77 :     # \@seqs Set of sequences to be pruned. If there is no reference
78 :     # sequence, the fist sequence in this list will be the starting
79 :     # point for the analysis and will be retained, but all sequences
80 :     # more similar than max_sim to it will be removed (in contrast to
81 :     # a reference sequence, which retains a representative of each
82 :     # lineage in its vicinity). Sequences that fail the E-value test
83 :     # relative to the reference (or the fist sequence if there is no
84 :     # reference) are dropped.
85 :     #
86 : golsen 1.2 # $max_sim (representative_sequences only; an option for rep_seq_2)
87 :     # Sequences with a higher similarity than max_sim to an existing
88 :     # representative sequence will not be included in the @reps
89 :     # output. Their ids are associated with the identifier of the
90 :     # sequence representing them in \%representing. The details of
91 :     # the behaviour are modified by other options. (default = 0.80)
92 : golsen 1.1 #
93 :     # \%options Key=>Value pairs that modify the behaviour:
94 :     #
95 :     # logfile Filehandle for a logfile of the progress. As each
96 : golsen 1.2 # sequence is analyzed, its disposition in recorded.
97 :     # In representative_sequences(), the id of each new
98 :     # representative is followed by a tab separated list of the
99 :     # ids that it represents. In rep_seq_2(), as each sequence
100 :     # is analyzed, it is recorded, followed by the id of the
101 :     # sequence representing it, if it is not the first member
102 :     # of a new cluster. Autoflush is set for the logfile.
103 : golsen 1.1 # If the value supplied is not a reference to a GLOB, then
104 :     # the log is sent to STDOUT (which is probably not what you
105 :     # want in most cases). The behavior is intended to aid in
106 :     # following prgress, and in recovery of interupted runs.
107 :     #
108 : golsen 1.2 # max_ref_sim (representative_sequences only)
109 : golsen 1.1 # Maximum similarity of any sequence to the reference. If
110 :     # max_ref_sim is less than max_sim, it is silently reset to
111 :     # max_sim. (default = 0.99, because 1.0 can be annoying)
112 :     #
113 : golsen 1.2 # max_e_val Maximum E-value for blastall. Probably moot, but will help
114 :     # with performance. (default = 0.01)
115 :     #
116 :     # max_sim Sequences with a higher similarity than max_sim to a
117 :     # retained sequence will be deleted. The details of the
118 :     # behaviour is modified by other options. (default = 0.80)
119 :     # (a parameter for representative_sequences, but an option
120 :     # for rep_seq_2).
121 : golsen 1.1 #
122 :     # sim_meas Measure similarity for inclusion or exclusion by
123 :     # 'identity_fraction' (default), 'positive_fraction', or
124 :     # 'score_per_position'
125 :     #
126 : golsen 1.2 # save_exp (representative_sequences only)
127 :     # When there is a reference sequence, lineages more similar
128 : golsen 1.1 # than max_sim will be retained near the reference. The
129 :     # default goal is to save one member of each lineage. If
130 :     # the initial representative of the lineage is seq1, we
131 :     # pose the question, "Are there sufficiently deep divisions
132 :     # within the lineage to seq1 that it they might be viewed
133 :     # as independent? That is, might there be another sequence,
134 :     # seq2 that so different from seq1 that we might want to see
135 :     # it also?
136 :     #
137 :     # +---------------------- ref
138 :     # |
139 :     # ---+ +-------------------- seq1
140 :     # +-+
141 :     # +-------------------- seq2
142 :     #
143 :     # Without any special treatment, if the similarity of seq1
144 :     # to ref ( S(seq1,ref) ) is greater than max_sim, seq1 would
145 :     # be the sole representative of thelineage containing both
146 :     # seq1 and seq2, because the similarity of seq1 to seq2
147 :     # ( S(seq1,seq2) ) is greater than S(seq1,ref). This can
148 :     # be altered by the value of save_exp. In terms of
149 :     # similarity, seq2 will be discarded if:
150 :     #
151 :     # S(seq1,seq2) > S(seq1,ref) ** save_exp, and
152 :     # S(seq1,seq2) > S(seq2,ref) ** save_exp
153 :     #
154 :     # The default behavior described above occurs when save_exp
155 :     # is 1. If save_exp < 1, then greater similarities between
156 :     # seq1 and seq2 are allowed. Reasonable values of save_exp
157 :     # are roughly 0.7 to 1.0. (At save_exp = 0, any similarity
158 :     # would be allowed; yuck.)
159 :     #
160 : golsen 1.2 # stable (representative_sequences only; always true for rep_seq_2)
161 :     # If true (not undef, '', or 0), then the representatives
162 : golsen 1.1 # will be chosen from as early in the list as possible (this
163 : golsen 1.2 # facilitates augmentation of an existing list).
164 : golsen 1.1 #
165 :     #-------------------------------------------------------------------------------
166 :     #
167 : golsen 1.2 # Diagram of the pruning behavior of representative_sequences():
168 : golsen 1.1 #
169 :     # 0.5 0.6 0.7 0.8 0.9 1.0 Similarity
170 :     # |---------|---------|---------|---------|---------|
171 :     # .
172 :     # . + A
173 :     # . +---+
174 :     # . | + B
175 :     # . +---+
176 :     # . | +---- C
177 :     # . +----------+
178 :     # . | +-------- D
179 :     # . |
180 :     # +-----------+ +----------------- E
181 :     # | . +-+
182 :     # | . +----------------- F
183 :     # +----------------+ .
184 :     # | | . +--------------------------- G
185 :     # | +---+
186 :     # | . | +--------------------- H
187 :     # --+ . +-----+
188 :     # | . +--------------------- I
189 :     # | .
190 :     # | +------------------------------- J
191 :     # +----------------+ .
192 :     # | . +--------------------------- K
193 :     # +---+
194 :     # . +--------------------------- L
195 :     # .
196 :     # |---------|---------|---------|---------|---------|
197 :     # 0.5 0.6 0.7 0.8 0.9 1.0 Similarity
198 :     #
199 :     # In the above tree and max_sim = 0.70 and max_ref_sim = 0.99:
200 :     #
201 :     # With no reference sequence, and A first in the list, the representative
202 :     # sequences will be A, G, J and K.
203 :     #
204 :     # With A as the reference sequence and save_exp left at its default, the
205 :     # representative sequences will be A, C, D, E, G, J and K. B is excluded
206 :     # because it is more similar than max_ref_sim to A.
207 :     #
208 :     # With A as the reference sequence and save_exp = 0.8, the representative
209 :     # sequences will be A, C, D, E, F (comparably similar to A and E), G,
210 :     # H (comparably similar to A and G), J and K. The sequence L will be
211 :     # represented by K because L is much closer to K than to A.
212 :     #
213 :     # This oversimplifies the choice of representative of a cluster of related
214 :     # sequences. For example, whether G, H or I would represent the group of
215 :     # three actually depends on relative clock speed (slower is better) and
216 :     # sequence coverage (more complete is better). The actual order is by BLAST
217 :     # bit score (possibly combining independent segments).
218 :     #
219 :     # In addition, this discussion is in terms of a tree, but the calculations
220 :     # are based on a (partial) set of pairwise sequence similarities. Thus, the
221 :     # precise behavior is hard to predict, but should be similar to that described
222 :     # above.
223 :     #
224 :     #-------------------------------------------------------------------------------
225 :     #
226 :     # To construct a representative set of sequences relative to a reference
227 :     # sequence:
228 :     #
229 :     # 1. Prioritize sequences for keeping, from highest to lowest scoring
230 :     # relative to reference, as measured by blast score (bits).
231 :     # When stable is set, priority is from first to last in input file
232 :     # (a reference sequence should not be supplied).
233 :     #
234 :     # 2. Based on the similarity of each sequence to the reference and save_exp,
235 :     # compute sequence-specific values of max_sim:
236 :     #
237 :     # max_sim( seq_i ) = exp( save_exp * ln( seq_i_ref_sim ) )
238 :     #
239 :     # 3. Examine the next prioritized sequence (seq1).
240 :     #
241 :     # 4. If seq1 has been vetoed, go to 7.
242 :     #
243 :     # 5. Mark seq1 to keep.
244 :     #
245 :     # 6. Use blast to find similarities of seq1 to other sequences.
246 :     #
247 :     # 7. For each similar sequence (seq2):
248 :     #
249 :     # 7a. Skip if seq2 is marked to keep, or marked for veto
250 :     #
251 :     # 7b. Compute the maximum simiarity of seq1 and seq2 for retaining seq2:
252 :     #
253 :     # max_sim_1_2 = max( max_sim, max_sim( seq1 ), max_sim( seq2 ) )
254 :     #
255 :     # 7c. If similarity of seq1 and seq2 > max_sim, veto seq2
256 :     #
257 :     # 7d. Next seq2
258 :     #
259 :     # 8. If there are more sequences to examine, go to 3.
260 :     #
261 :     # 9. Collect the sequences marked for keeping.
262 :     #
263 :     #===============================================================================
264 :    
265 :     sub representative_sequences {
266 :     my $seqs = ( shift @_ || shift @_ ); # If $ref is undef, shift again
267 :     ref( $seqs ) eq "ARRAY"
268 :     or die "representative_sequences called with bad first argument\n";
269 :    
270 :     my ( $ref, $use_ref );
271 :     if ( ! ref( $seqs->[0] ) ) # First item was sequence entry, not list of entries
272 :     {
273 :     $ref = $seqs;
274 :     $seqs = shift @_;
275 :     ref( $seqs ) eq "ARRAY"
276 :     and ref( $seqs->[0] ) eq "ARRAY"
277 :     or die "representative_sequences called with bad sequences list\n";
278 :     $use_ref = 1;
279 :     }
280 :     else # First item was list of entries, split off first
281 :     {
282 :     ref( $seqs->[0] ) eq "ARRAY"
283 :     or die "representative_sequences called with bad sequences list\n";
284 :     $ref = shift @$seqs;
285 :     $use_ref = 0;
286 :     }
287 :    
288 :     my $max_sim = shift @_;
289 :     my $options;
290 :    
291 :     # Undocumented feature: skip max_sim (D = 0.8)
292 :    
293 :     if ( ref( $max_sim ) eq "HASH" )
294 :     {
295 :     $options = $max_sim;
296 :     $max_sim = undef;
297 :     }
298 :    
299 :     # If the above did not give us options, get them now:
300 :    
301 :     $options ||= ( shift @_ ) || {};
302 :    
303 :     # ---------------------------------------# Default values for options
304 :    
305 :     $max_sim ||= 0.80; # Retain 80% identity of less
306 :     my $logfile = undef; # Log file of sequences processed
307 :     my $max_ref_sim = 0.99; # Get rid of identical sequences
308 :     my $max_e_val = 0.01; # Blast E-value to decrease output
309 :     my $sim_meas = 'identity_fraction'; # Use sequence identity as measure
310 :     my $save_exp = 1.0; # Don't retain near equivalents
311 :     my $stable = 0; # Pick reps input order
312 :    
313 :     # Two questionable decisions:
314 :     # 1. Be painfully flexible on option names.
315 :     # 2. Silently fix bad parameter values.
316 :    
317 :     foreach ( keys %$options )
318 :     {
319 :     my $value = $options->{ $_ };
320 :     if ( m/^log/i ) # logfile
321 :     {
322 :     next if ! $value;
323 :     $logfile = ( ref $value eq "GLOB" ? $value : \*STDOUT );
324 :     select( ( select( $logfile ), $| = 1 )[0] ); # autoflush on
325 :     }
326 :     elsif ( m/ref/i ) # max_ref_sim
327 :     {
328 :     $value += 0;
329 :     $value = 0 if $value < 0;
330 :     $value = 1 if $value > 1;
331 :     $max_ref_sim = $value;
332 :     }
333 :     elsif ( m/max/i && m/sim/i ) # max(imum)_sim(ilarity)
334 :     {
335 :     $value += 0;
336 :     $value = 0 if $value < 0;
337 :     $value = 1 if $value > 1;
338 :     $max_sim = $value;
339 :     }
340 :     elsif ( m/max/i || m/[ep]_?val/i ) # Other "max" tests must come first
341 :     {
342 :     $value += 0;
343 :     $value = 0 if $value < 0;
344 :     $max_e_val = $value;
345 :     }
346 :     elsif ( m/sim/i || m/meas/i ) # sim(ilarity)_meas(ure)
347 :     {
348 :     $sim_meas = standardize_similarity_measure( $value );
349 :     }
350 :     elsif ( m/sav/i || m/exp/i ) # save_exp(onent)
351 :     {
352 :     $value += 0;
353 :     $value = 0 if $value < 0;
354 :     $value = 1 if $value > 1;
355 :     $save_exp = $value;
356 :     }
357 :     elsif ( m/stab/i ) # stable order
358 :     {
359 :     $stable = $value ? 1 : 0;
360 :     }
361 :     else
362 :     {
363 :     print STDERR "WARNING: representative_sequences bad option ignored: '$_' => '$value'\n";
364 :     }
365 :     }
366 :    
367 :     # Silent sanity check. This should not happen, as it is almost equivalent
368 :     # to making no reference sequence.
369 :    
370 :     $max_ref_sim = $max_sim if ( $max_ref_sim < $max_sim );
371 :    
372 :     # Do the analysis
373 :    
374 :     my $ref_id = $ref->[0];
375 :     my $ref_seq = $ref->[2];
376 :    
377 :     # Build a list of the ids (without ref) and an index for the sequence entries:
378 :    
379 :     my @seq_id = map { $_->[0] } @$seqs;
380 :     my $seq_ind = { map { @{$_}[0] => $_ } ( $ref, @$seqs ) };
381 :    
382 :     # Make a lookup table of the sequence number, for use in reording
383 :     # sequences later:
384 :    
385 :     my $n = 0;
386 :     my %ord = ( map { @$_[0] => ++$n } @$seqs );
387 :    
388 :     # Build blast database (it includes the reference):
389 :    
390 :     my $protein = are_protein( $seqs );
391 :     my $db = ( $tmp_dir ? "$tmp_dir/" : "" ) . "tmp_blast_db_$$";
392 :     make_blast_db( $db, [ $ref, @$seqs ], $protein );
393 :    
394 :     # Search query against new database
395 :    
396 :     my $max = 3 * @$seqs; # Alignments to keep
397 :     my $self = 1; # Keep self match (for its bit score)
398 :    
399 :     my $blast_opt = "-e $max_e_val -v $max -b $max -F F -a 2";
400 :    
401 :     # Do the blast analysis. Returned records are of the form:
402 :     #
403 :     # 0 1 2 3 4 5 6 7 8 9 10 11
404 :     # [ qid, qdef, qlen, sid, sdef, slen, scr, e_val, n_mat, n_id, n_pos, n_gap ]
405 :    
406 :     my $prog = $protein ? 'blastp' : 'blastn';
407 :     $blast_opt .= " -r 1 -q -1" if ! $protein;
408 :     my @ref_hits = top_blast_per_subject( $prog, $db, $ref_id, $ref_seq, $self, $blast_opt );
409 :    
410 :     # First hit is always a perfect match, so we get bits per position:
411 :     # This is only used if the measure is bits per position
412 :    
413 :     my $ref_bpp = $ref_hits[0]->[6] / $ref_hits[0]->[8];
414 :    
415 :     # Remove self match (might not be first if there are identical sequences):
416 :    
417 :     my %hit = ();
418 :     @ref_hits = grep { my $sid = $_->[3]; $hit{ $sid } = 1; ( $sid ne $ref_id ) } @ref_hits;
419 :    
420 :     my %keep = ();
421 :     $keep{ $ref_id } = [];
422 :     my %veto = ();
423 :     my $n_to_do = @ref_hits;
424 :     my $rebuild_d_n = 40;
425 :     my $last_rebuild = 1.5 * $rebuild_d_n;
426 :     my $rebuild = ( $n_to_do > $last_rebuild ) ? $n_to_do - $rebuild_d_n : 0;
427 :    
428 :     # Sequence-specific maximum similarities:
429 :    
430 :     my %max_sim = map { ( $_ => $max_sim ) } @seq_id;
431 :    
432 :     foreach ( @ref_hits )
433 :     {
434 :     my $id = $_->[3];
435 :     my $sim = seq_similarity( $_, $sim_meas, $ref_bpp );
436 :    
437 :     if ( $sim > ( $use_ref ? $max_ref_sim : $max_sim ) )
438 :     {
439 :     $veto{ $id } = 1;
440 :     push @{ $keep{ $ref_id } }, $id; # Log the sequences represented
441 :     $n_to_do--;
442 :     }
443 :     else
444 :     {
445 :     my $max_sim_i = exp( $save_exp * log( $sim ) );
446 :     $max_sim{ $id } = $max_sim_i if ( $max_sim_i > $max_sim );
447 :     }
448 :     }
449 :    
450 :    
451 :     if ( $logfile )
452 :     {
453 :     print $logfile join( "\t", $ref_id, @{ $keep{ $ref_id } } ), "\n";
454 :     }
455 :    
456 :     # Search each sequence against the database.
457 :     # If the order is to be stable, reorder hits to match input order.
458 :    
459 :     my ( $id1, $seq1, $max_sim_1, $id2, $max_sim_2, $bpp_max );
460 :     my @ids_to_do = map { $_->[3] } @ref_hits;
461 :     @ids_to_do = sort { $ord{ $a } <=> $ord{ $b } } @ids_to_do if $stable;
462 :    
463 :     while ( $id1 = shift @ids_to_do )
464 :     {
465 :     next if $veto{ $id1 };
466 :    
467 :     # Is it time to rebuild a smaller BLAST database? This helps
468 :     # significantly in the overall performance.
469 :    
470 :     if ( $n_to_do <= $rebuild )
471 :     {
472 :     if ( $protein ) { unlink $db, "$db.psq", "$db.pin", "$db.phr" }
473 :     else { unlink $db, "$db.nsq", "$db.nin", "$db.nhr" }
474 :     make_blast_db( $db, [ map { $seq_ind->{ $_ } } # id to sequence entry
475 :     grep { ! $veto{ $_ } } # id not vetoed
476 :     ( $id1, @ids_to_do ) # remaining ids
477 :     ],
478 :     $protein
479 :     );
480 :     $rebuild = ( $n_to_do > $last_rebuild ) ? $n_to_do - $rebuild_d_n : 0;
481 :     }
482 :    
483 :     $n_to_do--;
484 :     $keep{ $id1 } = [];
485 :    
486 :     $max_sim_1 = $max_sim{ $id1 };
487 :     $bpp_max = undef;
488 :     $seq1 = $seq_ind->{$id1}->[2];
489 :     foreach ( top_blast_per_subject( $prog, $db, $id1, $seq1, $self, $blast_opt ) )
490 :     {
491 :     $bpp_max ||= $_->[6] / $_->[8];
492 :     $id2 = $_->[3];
493 :     next if ( $veto{ $id2 } || $keep{ $id2 } );
494 :     $max_sim_2 = $max_sim{ $id2 };
495 :     $max_sim_2 = $max_sim_1 if ( $max_sim_1 > $max_sim_2 );
496 :     if ( seq_similarity( $_, $sim_meas, $bpp_max ) > $max_sim_2 )
497 :     {
498 :     $veto{ $id2 } = 1;
499 :     push @{ $keep{ $id1 } }, $id2; # Log the sequences represented
500 :     $n_to_do--;
501 :     }
502 :     }
503 :    
504 :     if ( $logfile )
505 :     {
506 :     print $logfile join( "\t", $id1, @{ $keep{ $id1 } } ), "\n";
507 :     }
508 :     }
509 :    
510 :     if ( $protein ) { unlink $db, "$db.psq", "$db.pin", "$db.phr" }
511 :     else { unlink $db, "$db.nsq", "$db.nin", "$db.nhr" }
512 :    
513 :     # Return the surviving sequence entries, and optionally the hash of
514 :     # ids represented by each survivor:
515 :    
516 :     my $kept = [ $ref, grep { $keep{ $_->[0] } } @$seqs ];
517 :    
518 :     wantarray ? ( $kept, \%keep, [ grep { ! $hit{ $_->[0] } } @$seqs ] ) : $kept;
519 :     }
520 :    
521 :    
522 :     #===============================================================================
523 :     # Build or add to a set of representative sequences.
524 :     #
525 :     # \@reps = rep_seq_2( \@reps, \@new, \%options );
526 :     # \@reps = rep_seq_2( \@new, \%options );
527 :     #
528 :     # or
529 :     #
530 :     # ( \@reps, \%representing ) = rep_seq_2( \@reps, \@new, \%options );
531 :     # ( \@reps, \%representing ) = rep_seq_2( \@new, \%options );
532 :     #
533 :     #===============================================================================
534 :    
535 :     sub rep_seq_2
536 :     {
537 :     ref $_[0] eq 'ARRAY'
538 :     or print STDERR "First parameter of rep_seq_2 must be an ARRAY reference\n"
539 :     and return undef;
540 :    
541 :     my ( $reps, $seqs, $options ) = ( [], undef, {} );
542 :    
543 :     if ( @_ == 1 )
544 :     {
545 :     $seqs = shift;
546 :     }
547 :    
548 :     elsif ( @_ == 2 )
549 :     {
550 :     if ( ref $_[1] eq 'ARRAY' ) { ( $reps, $seqs ) = @_ }
551 :     elsif ( ref $_[1] eq 'HASH' ) { ( $seqs, $options ) = @_ }
552 :     else
553 :     {
554 :     print STDERR "Second parameter of rep_seq_2 must be an ARRAY or HASH reference\n";
555 :     return undef;
556 :     }
557 :     }
558 :    
559 :     elsif ( @_ == 3 )
560 :     {
561 :     if ( ref $_[1] ne 'ARRAY' )
562 :     {
563 :     print STDERR "Second parameter of 3 parameter rep_seq_2 must be an ARRAY reference\n";
564 :     return undef;
565 :     }
566 :     if ( ref $_[2] ne 'HASH' )
567 :     {
568 :     print STDERR "Third parameter of 3 parameter rep_seq_2 must be a HASH reference\n";
569 :     return undef;
570 :     }
571 :    
572 :     ( $reps, $seqs, $options ) = @_;
573 :     }
574 :     else
575 :     {
576 :     print STDERR "rep_seq_2 called with @{[scalar @_]} parameters\n";
577 :     return undef;
578 :     }
579 :    
580 :     # ---------------------------------------# Default values for options
581 :    
582 :     my $max_sim = 0.80; # Retain 80% identity of less
583 :     my $logfile = undef; # Log file of sequences processed
584 :     my $max_e_val = 0.01; # Blast E-value to decrease output
585 :     my $sim_meas = 'identity_fraction'; # Use sequence identity as measure
586 :    
587 :     # Two questionable decisions:
588 :     # 1. Be painfully flexible on option names.
589 :     # 2. Silently fix bad parameter values.
590 :    
591 :     foreach ( keys %$options )
592 :     {
593 :     my $value = $options->{ $_ };
594 :     if ( m/^log/i ) # logfile
595 :     {
596 :     next if ! $value;
597 :     $logfile = ( ref $value eq "GLOB" ? $value : \*STDOUT );
598 :     select( ( select( $logfile ), $| = 1 )[0] ); # autoflush on
599 :     }
600 :     elsif ( m/max/i && m/sim/i ) # max(imum)_sim(ilarity)
601 :     {
602 :     $value += 0;
603 :     $value = 0 if $value < 0;
604 :     $value = 1 if $value > 1;
605 :     $max_sim = $value;
606 :     }
607 :     elsif ( m/max/i || m/[ep]_?val/i ) # Other "max" tests must come first
608 :     {
609 :     $value += 0;
610 :     $value = 0 if $value < 0;
611 :     $max_e_val = $value;
612 :     }
613 :     elsif ( m/sim/i || m/meas/i ) # sim(ilarity)_meas(ure)
614 :     {
615 :     $sim_meas = standardize_similarity_measure( $value );
616 :     }
617 :     else
618 :     {
619 :     print STDERR "WARNING: rep_seq_2 bad option ignored: '$_' => '$value'\n";
620 :     }
621 :     }
622 :    
623 :     # Check sequence ids for duplicates:
624 :    
625 :     my $reps2 = [];
626 :     my $seen = {};
627 :    
628 :     my $id;
629 :     foreach ( @$reps )
630 :     {
631 :     $id = $_->[0];
632 :     if ( $seen->{ $id }++ )
633 :     {
634 :     print STDERR "Duplicate sequence id '$id' skipped by rep_seq_2\n";
635 :     }
636 :     else
637 :     {
638 :     push @$reps2, $_;
639 :     }
640 :     }
641 :    
642 :     my $seqs2 = [];
643 :     foreach ( @$seqs )
644 :     {
645 :     $id = $_->[0];
646 :     if ( $seen->{ $id }++ )
647 :     {
648 :     print STDERR "Duplicate sequence id '$_[0]' skipped by rep_seq_2\n";
649 :     }
650 :     else
651 :     {
652 :     push @$seqs2, $_;
653 :     }
654 :     }
655 :    
656 :     # If no preexisting representatives, then take first sequence:
657 :    
658 :     @$reps2 or @$reps2 = ( shift @$seqs2 );
659 :    
660 :     if ( $logfile ) { foreach ( @$reps2 ) { print $logfile "$_->[0]\n" } }
661 :    
662 :     # Search each rep sequence against itself for max_bpp
663 :    
664 :     my $db = ( $tmp_dir ? "$tmp_dir/" : "" ) . "tmp_blast_db_$$";
665 :     my $protein = are_protein( $reps2 );
666 :     my %max_bpp;
667 :     my $entry;
668 :     foreach $entry ( @$reps2 )
669 :     {
670 :     $max_bpp{ $entry->[0] } = self_bpp( $db, $entry, $protein );
671 :     }
672 :    
673 :     my $naln = 10; # Alignments to make
674 :     my $self = 1; # Self match will never occur
675 :     my $prog = $protein ? 'blastp' : 'blastn';
676 :     my $blast_opt = "-e $max_e_val -v $naln -b $naln -F F -a 2";
677 :     $blast_opt .= " -r 1 -q -1" if ! $protein;
678 :    
679 :     # List of who is represented by a sequence:
680 :    
681 :     my %keep = map { $_->[0] => [] } @$reps2;
682 :    
683 :     # Search each sequence against the database.
684 :    
685 :     my ( $seq, $bpp_max );
686 :     my $newdb = 1;
687 :    
688 :     foreach $entry ( @$seqs2 )
689 :     {
690 :     # Is it time to rebuild a BLAST database?
691 :    
692 :     if ( $newdb )
693 :     {
694 :     make_blast_db( $db, $reps2, $protein );
695 :     $newdb = 0;
696 :     }
697 :    
698 :     # Do the blast analysis. Returned records are of the form:
699 :     #
700 :     # 0 1 2 3 4 5 6 7 8 9 10 11
701 :     # [ qid, qdef, qlen, sid, sdef, slen, scr, e_val, n_mat, n_id, n_pos, n_gap ]
702 :    
703 :     $id = $entry->[0];
704 :     $seq = $entry->[2];
705 :     my ( $tophit ) = sort { $b->[0] <=> $a->[0] }
706 :     grep { $_->[0] >= $max_sim }
707 :     map { [ seq_similarity( $_, $sim_meas, $max_bpp{ $_->[3] } ), $_ ] }
708 :     top_blast_per_subject( $prog, $db, $id, $seq, $self, $blast_opt );
709 :    
710 :    
711 :     # It matches an existing representative
712 :    
713 :     if ( $tophit )
714 :     {
715 :     # print STDERR join(", ", $tophit->[0], @{$tophit->[1]} ), "\n";
716 :     push @{ $keep{ $tophit->[1]->[3] } }, $id;
717 :     print $logfile "$id\t$tophit->[1]->[3]\n" if $logfile;
718 :     }
719 :    
720 :     # It is a new representative
721 :    
722 :     else
723 :     {
724 :     push @$reps2, $entry;
725 :     $keep{ $id } = [];
726 :     $max_bpp{ $id } = self_bpp( $db, $entry, $protein );
727 :     $newdb = 1;
728 :     print $logfile "$id\n" if $logfile;
729 :     }
730 :     }
731 :    
732 :     if ( $protein ) { unlink $db, "$db.psq", "$db.pin", "$db.phr" }
733 :     else { unlink $db, "$db.nsq", "$db.nin", "$db.nhr" }
734 :    
735 :     # Return the surviving sequence entries, and optionally the hash of
736 :     # ids represented by each survivor:
737 :    
738 :     wantarray ? ( $reps2, \%keep ) : $reps2;
739 :     }
740 :    
741 :    
742 :     #===============================================================================
743 :     # Try to figure out the sequence similarity measure that is being requested:
744 :     #
745 :     # $type = standardize_similarity_measure( $requested_type )
746 :     #
747 :     #===============================================================================
748 :    
749 :     sub standardize_similarity_measure
750 :     { my ( $req_meas ) = @_;
751 :     return ( ! $req_meas ) ? 'identity_fraction'
752 :     : ( $req_meas =~ /id/i ) ? 'identity_fraction'
753 :     : ( $req_meas =~ /sc/i ) ? 'score_per_position'
754 :     : ( $req_meas =~ /spp/i ) ? 'score_per_position'
755 :     : ( $req_meas =~ /bit/i ) ? 'score_per_position'
756 :     : ( $req_meas =~ /bpp/i ) ? 'score_per_position'
757 :     : ( $req_meas =~ /tiv/i ) ? 'positive_fraction'
758 :     : ( $req_meas =~ /pos_/i ) ? 'positive_fraction'
759 :     : ( $req_meas =~ /ppp/i ) ? 'positive_fraction'
760 :     : 'identity_fraction';
761 :     }
762 :    
763 :    
764 :     #===============================================================================
765 :     # Caluculate sequence similarity according to the requested measure:
766 :     #
767 :     # $similarity = seq_similarity( $hit, $measure, $bpp_max )
768 :     #
769 :     # $hit is a structure with blast information:
770 :     #
771 :     # [ qid, qdef, qlen, sid, sdef, slen, scr, e_val, n_mat, n_id, n_pos, n_gap ]
772 :     #===============================================================================
773 :    
774 :     sub seq_similarity
775 :     { my ( $hit, $measure, $bpp_max ) = @_;
776 :     return ( @$hit < 11 ) ? undef
777 :     : ( $measure =~ /^sc/ ) ? $hit->[ 6] / ( $hit->[8] * ( $bpp_max || 2 ) )
778 :     : ( $measure =~ /^po/ ) ? $hit->[10] / $hit->[8]
779 :     : $hit->[ 9] / $hit->[8]
780 :     }
781 :    
782 :    
783 :     #===============================================================================
784 :     # Caluculate self similarity of a sequence in bits per position:
785 :     #
786 :     # $max_bpp = self_bpp( $db_name, $entry, $protein )
787 :     #
788 :     #===============================================================================
789 :    
790 :     sub self_bpp
791 :     {
792 :     my ( $db, $entry, $protein ) = @_;
793 :    
794 :     # Build blast database:
795 :    
796 :     make_blast_db( $db, [ $entry ], $protein );
797 :    
798 :     # Search sequence against the database
799 :    
800 :     my $id = $entry->[0];
801 :     my $seq = $entry->[2];
802 :     my $self = 1; # Self match will never occur
803 :    
804 :     my $prog = $protein ? 'blastp' : 'blastn';
805 :     my $blast_opt = "-v 1 -b 1 -F F -a 2";
806 :     $blast_opt .= " -r 1 -q -1" if ! $protein;
807 :    
808 :     # Do the blast analysis. Returned records are of the form:
809 :     #
810 :     # 0 1 2 3 4 5 6 7 8 9 10 11
811 :     # [ qid, qdef, qlen, sid, sdef, slen, scr, e_val, n_mat, n_id, n_pos, n_gap ]
812 :    
813 :     my ( $hit ) = top_blast_per_subject( $prog, $db, $id, $seq, $self, $blast_opt );
814 :     # print STDERR join( ", ", @$hit ), "\n";
815 :    
816 :     # First hit is always a perfect match, so we get bits per position:
817 :     # This is only used if the measure is bits per position
818 :    
819 :     $hit->[6] / $hit->[8];
820 :     }
821 :    
822 :    
823 :     #===============================================================================
824 :     # Make a blast databse from a set of sequence entries. The type of database
825 :     # (protein or nucleic acid) is quessed from the sequence data.
826 :     #
827 :     # make_blast_db( $db_filename, \@seq_entries, $protein )
828 :     #
829 :     # Sequence entries have the form: [ $id, $def, $seq ]
830 :     #===============================================================================
831 :    
832 :     sub make_blast_db
833 :     { my ( $db, $seqs, $protein ) = @_;
834 :    
835 :     open FH, ">$db" or die "Could not create blast database file \"$db\"";
836 :     foreach ( @$seqs )
837 :     {
838 :     print FH ">$_->[0]", ( $_->[1] ? " $_->[1]" : () ), "\n$_->[2]\n";
839 :     }
840 :     close FH;
841 :    
842 :     my $is_prot = $protein ? 'T' : 'F';
843 :     system "$formatdb -p $is_prot -i '$db'";
844 :     }
845 :    
846 :    
847 :     #===============================================================================
848 :     # The type of data (protein or nucleic acid) is quessed from the sequences.
849 :     #
850 :     # are_protein( \@seq_entries )
851 :     #
852 :     # Sequence entries have the form: [ $id, $def, $seq ]
853 :     #===============================================================================
854 :    
855 :     sub are_protein
856 :     { my ( $seqs ) = @_;
857 :     my ( $nt, $aa ) = ( 0, 0 );
858 :     foreach ( @$seqs )
859 :     {
860 :     my $s = $_->[2];
861 :     $nt += $s =~ tr/ACGTacgt//d;
862 :     $aa += $s =~ tr/A-Za-z//d;
863 :     }
864 :     ( $nt < 3 * $aa ) ? 1 : 0;
865 :     }
866 :    
867 :    
868 :     #===============================================================================
869 :     # Blast a subject against a datbase, saving only top hit per subject
870 :     #
871 :     # Return:
872 :     #
873 :     # [ qid, qdef, qlen, sid, sdef, slen, scr, e_val, n_mat, n_id, n_pos, n_gap ]
874 :     #
875 :     #===============================================================================
876 :    
877 :     # Use a bare block to compartmentalize a counter:
878 :    
879 :     { my $cnt = 0;
880 :    
881 :     sub top_blast_per_subject
882 :     { my ( $prog, $db, $qid, $qseq, $self, $blast_opt, $sort, $no_merge ) = @_;
883 :    
884 :     $cnt++;
885 :     my $query_file = ( $tmp_dir ? "$tmp_dir/" : "" ) . "tmp_blast_seq_${$}_$cnt";
886 :     my $QFILE;
887 :     open $QFILE, ">$query_file" or die "Could not create sequence file \"$query_file\"";
888 :     print $QFILE ">$qid\n$qseq\n";
889 :     close $QFILE;
890 :    
891 :     my $blast_cmd = "$blastall -p $prog -d '$db' -i '$query_file' $blast_opt";
892 :    
893 :     my $BPIPE;
894 :     open $BPIPE, "$blast_cmd |" or die "Could open blast pipe\n";
895 :     my $sims = integrate_blast_segments( $BPIPE, $sort, $no_merge, $self );
896 :     close $BPIPE;
897 :     unlink $query_file;
898 :    
899 :     my $pq = ""; # Previous query id
900 :     my $ps = ""; # Previous subject id
901 :     my $keep;
902 :    
903 :     grep { $keep = ( $pq ne $_->[0] ) || ( $ps ne $_->[3] );
904 :     $pq = $_->[0];
905 :     $ps = $_->[3];
906 :     $keep && ( $self || ( $pq ne $ps ) );
907 :     } @$sims;
908 :     }
909 :     }
910 :    
911 :    
912 :     #===============================================================================
913 :     # Read output of rationalize blast and assemble minimally overlapping segments
914 :     # into a total score for each subject sequence. For each query, sort matches
915 :     # into user-chosen order (D = total score):
916 :     #
917 :     # @sims = integrate_blast_segments( \*FILEHANDLE, $sort_order, $no_merge )
918 :     # \@sims = integrate_blast_segments( \*FILEHANDLE, $sort_order, $no_merge )
919 :     #
920 :     # Allowed sort orders are 'score', 'score_per_position', 'identity_fraction',
921 :     # and 'positive_fraction' (matched very flexibly).
922 :     #
923 :     # Returned sims (e_val is only for best HSP, not any composite):
924 :     #
925 :     # [ qid, qdef, qlen, sid, sdef, slen, scr, e_val, n_mat, n_id, n_pos, n_gap ]
926 :     #
927 :     # There is a strategic decision to not read the blast output from memory;
928 :     # it could be enormous. This cuts the flexibility some.
929 :     #===============================================================================
930 :     #
931 :     # coverage fields:
932 :     #
933 :     # [ scr, e_val, n_mat, n_id, n_pos, n_gap, dir, [ intervals_covered ] ]
934 :     #
935 :     #===============================================================================
936 :    
937 :     sub integrate_blast_segments
938 :     { my ( $fh, $order, $no_merge, $self ) = @_;
939 :     $fh ||= \*STDIN;
940 :     ( ref( $fh ) eq "GLOB" ) || die "integrate_blast_segments called without a filehandle\n";
941 :    
942 :     $order = ( ! $order ) ? 'score'
943 :     : ( $order =~ /sc/i ) ? ( $order =~ /p/i ? 'score_per_position' : 'score' )
944 :     : ( $order =~ /bit/i ) ? ( $order =~ /p/i ? 'score_per_position' : 'score' )
945 :     : ( $order =~ /spp/i ) ? 'score_per_position'
946 :     : ( $order =~ /id/i ) ? 'identity_fraction'
947 :     : ( $order =~ /tiv/i ) ? 'positive_fraction'
948 :     : 'score';
949 :    
950 :     my $max_frac_overlap = 0.2;
951 :    
952 :     my ( $qid, $qdef, $qlen, $sid, $sdef, $slen );
953 :     my ( $scr, $e_val, $n_mat, $n_id, $n_pos, $n_gap );
954 :     my ( $ttl_scr, $ttl_mat, $ttl_id, $ttl_pos, $ttl_gap );
955 :     my @sims = ();
956 :     my @qsims = ();
957 :     my $coverage = undef;
958 :     my $record;
959 :    
960 :     while ( $_ = next_blast_record( $fh, $self ) )
961 :     {
962 :     chomp;
963 :     if ( $_->[0] eq 'Query=' )
964 :     {
965 :     if ( $coverage )
966 :     {
967 :     push @qsims, [ $sid, $sdef, $slen, @$coverage[ 0 .. 5 ] ];
968 :     $coverage = undef;
969 :     }
970 :     if ( @qsims ) { push @sims, order_query_sims( $qid, $qdef, $qlen, \@qsims, $order ) }
971 :     ( undef, $qid, $qdef, $qlen ) = @$_;
972 :     $sid = undef;
973 :     @qsims = ();
974 :     }
975 :     elsif ( $_->[0] eq '>' )
976 :     {
977 :     if ( $coverage )
978 :     {
979 :     push @qsims, [ $sid, $sdef, $slen, @$coverage[ 0 .. 5 ] ];
980 :     $coverage = undef;
981 :     }
982 :     next if ! $qid;
983 :     ( undef, $sid, $sdef, $slen ) = @$_;
984 :     }
985 :     elsif ( $_->[0] eq 'HSP' && $sid )
986 :     {
987 :     $coverage = integrate_HSP( $coverage, $_, $max_frac_overlap, $no_merge );
988 :     }
989 :     }
990 :    
991 :     if ( $coverage ) { push @qsims, [ $sid, $sdef, $slen, @$coverage[ 0 .. 5 ] ] }
992 :    
993 :     if ( @qsims ) { push @sims, order_query_sims( $qid, $qdef, $qlen, \@qsims, $order ) }
994 :    
995 :     wantarray ? @sims : \@sims;
996 :     }
997 :    
998 :    
999 :     #===============================================================================
1000 :     #
1001 :     # Try to integrate non-conflicting HSPs for the same subject sequence. The
1002 :     # conflicts are only assessed from the standpoint of the query, at least for
1003 :     # now. We could track the subject sequence coverage as well (to avoid a direct
1004 :     # repeat in the query from matching the same subject twice).
1005 :     #
1006 :     # $new_coverage = integrate_HSP( $coverage, $hsp, $max_frac_overlap, $no_merge )
1007 :     #
1008 :     # 0 1 2 3 4 5 6 7
1009 :     # $coverage = [ scr, e_val, n_mat, n_id, n_pos, n_gap, dir, [ intervals_covered ] ]
1010 :     #
1011 :     # $coverage should be undefined at the first call; the function intiallizes
1012 :     # all of the fields from the first HSP. scr, n_mat, n_id, n_pos, and n_gap
1013 :     # are sums over the combined HSPs. e_val is based only of the first HSP.
1014 :     #
1015 :     # 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
1016 :     # $hsp = [ 'HSP', scr, e_val, n_seg, e_val2, n_mat, n_id, n_pos, n_gap, dir, s1, e1, sq1, s2, e2, sq2 ]
1017 :     #
1018 :     # $max_frac_overlap Amount of the new HSP that is allowed to overlap already
1019 :     # incorporated HSPs
1020 :     #
1021 :     # $no_merge Disable the merging of multiple HSPs. The structure will
1022 :     # be filled in from the first HSP and left unchanged though
1023 :     # subsequence calls. This simplifies the program structure.
1024 :     #
1025 :     # Fitting a new HSP into covered intervals:
1026 :     #
1027 :     # 1 qlen
1028 :     # |---------------------------------------------------------------| query
1029 :     # ------------ --------------- covered
1030 :     # ------------- new match
1031 :     # l r
1032 :     #
1033 :     #===============================================================================
1034 :    
1035 :     sub integrate_HSP
1036 :     { my ( $coverage, $hsp, $max_frac_overlap, $no_merge ) = @_;
1037 :     my ( undef, $scr, $e_val, undef, undef, $n_mat, $n_id, $n_pos, $n_gap, $dir, $s1, $e1 ) = @$hsp;
1038 :    
1039 :     # Ignore frame; just use direction of match:
1040 :    
1041 :     $dir = substr( $dir, 0, 1 );
1042 :    
1043 :     # Orient by left and right ends:
1044 :    
1045 :     my ( $l, $r ) = ( $e1 > $s1 ) ? ( $s1, $e1 ) : ( $e1, $s1 );
1046 :    
1047 :     # First HSP for the subject sequence:
1048 :    
1049 :     if ( ! $coverage )
1050 :     {
1051 :     return [ $scr, $e_val, $n_mat, $n_id, $n_pos, $n_gap, $dir, [ [ $s1, $e1 ] ] ];
1052 :     }
1053 :    
1054 :     # Not first; must be same direction to combine (also test no_merge here):
1055 :    
1056 :     return $coverage if ( $no_merge || ( $dir ne $coverage->[6] ) );
1057 :    
1058 :     # Not first; must fall in a gap of query sequence coverage:
1059 :    
1060 :     my @intervals = @{ $coverage->[7] };
1061 :     my $max_overlap = $max_frac_overlap * ( $r - $l + 1 );
1062 :     my $prev_end = 0;
1063 :     my $next_beg = $intervals[0]->[0];
1064 :     my @used = ();
1065 :     while ( $next_beg <= $l ) # *** Sequential search could be made binary
1066 :     {
1067 :     $prev_end = $intervals[0]->[1];
1068 :     push @used, scalar shift @intervals;
1069 :     $next_beg = @intervals ? $intervals[0]->[0] : 1e10;
1070 :     }
1071 :    
1072 :     my $overlap = ( ( $l <= $prev_end ) ? ( $prev_end - $l + 1 ) : 0 )
1073 :     + ( ( $r >= $next_beg ) ? ( $r - $next_beg + 1 ) : 0 );
1074 :     return $coverage if ( $overlap > $max_overlap );
1075 :    
1076 :     # Okay, we have passed the overlap test. We need to integrate the
1077 :     # match into the coverage description. Yes, I know that this counts
1078 :     # the overlap region. We could pro rate it, but that is messy too:
1079 :    
1080 :     $coverage->[0] += $scr;
1081 :     $coverage->[2] += $n_mat;
1082 :     $coverage->[3] += $n_id;
1083 :     $coverage->[4] += $n_pos;
1084 :     $coverage->[5] += $n_gap;
1085 :    
1086 :     # Refigure the covered intervals, fusing intervals separated by a
1087 :     # gap of less than 10:
1088 :    
1089 :     my $min_gap = 10;
1090 :     if ( $l <= $prev_end + $min_gap )
1091 :     {
1092 :     if ( @used ) { $l = $used[-1]->[0]; pop @used }
1093 :     else { $l = 1 }
1094 :     }
1095 :     if ( $r >= $next_beg - $min_gap )
1096 :     {
1097 :     if ( @intervals ) { $r = $intervals[0]->[1]; shift @intervals }
1098 :     else { $r = 1e10 }
1099 :     }
1100 :    
1101 :     $coverage->[7] = [ @used, [ $l, $r ], @intervals ];
1102 :    
1103 :     return $coverage;
1104 :     }
1105 :    
1106 :    
1107 :     #===============================================================================
1108 :     # Sort the blast matches by the desired criterion:
1109 :     #
1110 :     # @sims = order_query_sims( $qid, $qdef, $qlen, \@qsims, $order )
1111 :     #
1112 :     # Allowed sort orders are 'score', 'score_per_position', 'identity_fraction',
1113 :     # and 'positive_fraction'
1114 :     #
1115 :     # @qsims fields:
1116 :     #
1117 :     # 0 1 2 3 4 5 6 7 8
1118 :     # [ sid, sdef, slen, scr, e_val, n_mat, n_id, n_pos, n_gap ]
1119 :     #
1120 :     #===============================================================================
1121 :    
1122 :     sub order_query_sims
1123 :     { my ( $qid, $qdef, $qlen, $qsims, $order ) = @_;
1124 :    
1125 :     my @sims;
1126 :     if ( $order eq 'score_per_position' )
1127 :     {
1128 :     @sims = map { [ $_->[5] ? $_->[3]/$_->[5] : 0, $_ ] } @$qsims;
1129 :     }
1130 :     elsif ( $order eq 'identity_fraction' )
1131 :     {
1132 :     @sims = map { [ $_->[5] ? $_->[6]/$_->[5] : 0, $_ ] } @$qsims;
1133 :     }
1134 :     elsif ( $order eq 'positive_fraction' )
1135 :     {
1136 :     @sims = map { [ $_->[5] ? $_->[7]/$_->[5] : 0, $_ ] } @$qsims;
1137 :     }
1138 :     else # Default is by 'score'
1139 :     {
1140 :     @sims = map { [ $_->[3], $_ ] } @$qsims;
1141 :     }
1142 :    
1143 :     map { [ $qid, $qdef, $qlen, @{$_->[1]} ] } sort { $b->[0] <=> $a->[0] } @sims;
1144 :     }
1145 :    
1146 :    
1147 :     1;

MCS Webmaster
ViewVC Help
Powered by ViewVC 1.0.3