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

Annotation of /FigKernelPackages/gjoseqlib.pm

Parent Directory Parent Directory | Revision Log Revision Log


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

1 : efrank 1.1 package gjoseqlib;
2 :    
3 : golsen 1.2 # A sequence entry is ( $id, $def, $seq )
4 :     # A list of entries is a list of references
5 :     #
6 : overbeek 1.4 # @seq_entries = read_fasta( ) # STDIN
7 :     # @seq_entries = read_fasta( \*FILEHANDLE )
8 :     # @seq_entries = read_fasta( $filename )
9 : golsen 1.12 # @entry = read_next_fasta_seq( \*FILEHANDLE )
10 :     # \@entry = read_next_fasta_seq( \*FILEHANDLE )
11 :     # @entry = read_next_fasta_seq( $filename )
12 :     # \@entry = read_next_fasta_seq( $filename )
13 :     # @entry = read_next_fasta_seq() # STDIN
14 :     # \@entry = read_next_fasta_seq() # STDIN
15 :     # @seq_entries = read_fasta_seqs( \*FILEHANDLE ) # Original form
16 : overbeek 1.4 # @seq_entries = read_clustal( ) # STDIN
17 :     # @seq_entries = read_clustal( \*FILEHANDLE )
18 :     # @seq_entries = read_clustal( $filename )
19 :     # @seq_entries = read_clustal_file( $filename )
20 : golsen 1.2 #
21 : overbeek 1.4 # $seq_ind = index_seq_list( @seq_entries ); # hash from ids to entries
22 : golsen 1.2 # @seq_entry = seq_entry_by_id( \%seq_index, $seq_id );
23 :     # $seq_desc = seq_desc_by_id( \%seq_index, $seq_id );
24 :     # $seq = seq_data_by_id( \%seq_index, $seq_id );
25 :     #
26 :     # ( $id, $def ) = parse_fasta_title( $title )
27 :     # ( $id, $def ) = split_fasta_title( $title )
28 :     #
29 : overbeek 1.4 # print_seq_list_as_fasta( \*FILEHANDLE, @seq_entry_list ); # Original form
30 :     # print_alignment_as_fasta( @seq_entry_list ); # STDOUT
31 :     # print_alignment_as_fasta( \@seq_entry_list ); # STDOUT
32 :     # print_alignment_as_fasta( \*FILEHANDLE, @seq_entry_list );
33 :     # print_alignment_as_fasta( \*FILEHANDLE, \@seq_entry_list );
34 :     # print_alignment_as_fasta( $filename, @seq_entry_list );
35 :     # print_alignment_as_fasta( $filename, \@seq_entry_list );
36 :     # print_alignment_as_phylip( @seq_entry_list ); # STDOUT
37 :     # print_alignment_as_phylip( \@seq_entry_list ); # STDOUT
38 :     # print_alignment_as_phylip( \*FILEHANDLE, @seq_entry_list );
39 :     # print_alignment_as_phylip( \*FILEHANDLE, \@seq_entry_list );
40 :     # print_alignment_as_phylip( $filename, @seq_entry_list );
41 :     # print_alignment_as_phylip( $filename, \@seq_entry_list );
42 :     # print_alignment_as_nexus( [ \%label_hash, ] @seq_entry_list );
43 :     # print_alignment_as_nexus( [ \%label_hash, ] \@seq_entry_list );
44 :     # print_alignment_as_nexus( \*FILEHANDLE, [ \%label_hash, ] @seq_entry_list );
45 :     # print_alignment_as_nexus( \*FILEHANDLE, [ \%label_hash, ] \@seq_entry_list );
46 :     # print_alignment_as_nexus( $filename, [ \%label_hash, ] @seq_entry_list );
47 :     # print_alignment_as_nexus( $filename, [ \%label_hash, ] \@seq_entry_list );
48 : overbeek 1.14 # print_seq_as_fasta( \*FILEHANDLE, $id, $desc, $seq );
49 :     # print_seq_as_fasta( $id, $desc, $seq );
50 : overbeek 1.4 # print_gb_locus( \*FILEHANDLE, $locus, $def, $accession, $seq );
51 : golsen 1.2 #
52 : golsen 1.6 # @packed_seqs = pack_alignment( @seqs )
53 :     # @packed_seqs = pack_alignment( \@seqs )
54 :     # \@packed_seqs = pack_alignment( @seqs )
55 :     # \@packed_seqs = pack_alignment( \@seqs )
56 : golsen 1.5 #
57 : golsen 1.2 # @entry = subseq_DNA_entry( @seq_entry, $from, $to [, $fix_id] );
58 :     # @entry = subseq_RNA_entry( @seq_entry, $from, $to [, $fix_id] );
59 : golsen 1.9 # $DNAseq = DNA_subseq( $seq, $from, $to );
60 :     # $DNAseq = DNA_subseq( \$seq, $from, $to );
61 :     # $RNAseq = RNA_subseq( $seq, $from, $to );
62 :     # $RNAseq = RNA_subseq( \$seq, $from, $to );
63 : golsen 1.2 # @entry = complement_DNA_entry( @seq_entry [, $fix_id] );
64 :     # @entry = complement_RNA_entry( @seq_entry [, $fix_id] );
65 :     # $DNAseq = complement_DNA_seq( $NA_seq );
66 :     # $RNAseq = complement_RNA_seq( $NA_seq );
67 :     # $DNAseq = to_DNA_seq( $NA_seq );
68 :     # $RNAseq = to_RNA_seq( $NA_seq );
69 :     # $seq = pack_seq( $sequence )
70 :     # $seq = clean_ae_sequence( $seq )
71 :     #
72 : golsen 1.10 # $aa = translate_seq( $nt, $met_start )
73 :     # $aa = translate_seq( $nt )
74 :     # $aa = translate_codon( $triplet );
75 :     #
76 :     # User-supplied genetic code. The supplied code needs to be complete in
77 :     # RNA and/or DNA, and upper and/or lower case. The program guesses based
78 :     # on lysine and phenylalanine codons.
79 : golsen 1.2 #
80 : golsen 1.10 # $aa = translate_seq_with_user_code( $nt, $gen_code_hash, $met_start )
81 :     # $aa = translate_seq_with_user_code( $nt, $gen_code_hash )
82 : golsen 1.9 #
83 : golsen 1.10 # Locations (= oriented intervals) are ( id, start, end )
84 :     # Intervals are ( id, left, right )
85 : golsen 1.2 #
86 : overbeek 1.4 # @intervals = read_intervals( \*FILEHANDLE )
87 :     # @intervals = read_oriented_intervals( \*FILEHANDLE )
88 : golsen 1.2 # @intervals = standardize_intervals( @interval_refs ) # (id, left, right)
89 :     # @joined = join_intervals( @interval_refs )
90 :     # @intervals = locations_2_intervals( @locations )
91 :     # $interval = locations_2_intervals( $location )
92 :     # @reversed = reverse_intervals( @interval_refs ) # (id, end, start)
93 : overbeek 1.4 #
94 :     # Convert GenBank locations to SEED locations
95 :     #
96 :     # @seed_locs = gb_location_2_seed( $contig, @gb_locs )
97 : golsen 1.7 #
98 :     # Read quality scores from a fasta-like file:
99 :     #
100 :     # @seq_entries = read_qual( ) # STDIN
101 :     # \@seq_entries = read_qual( ) # STDIN
102 :     # @seq_entries = read_qual( \*FILEHANDLE )
103 :     # \@seq_entries = read_qual( \*FILEHANDLE )
104 :     # @seq_entries = read_qual( $filename )
105 :     # \@seq_entries = read_qual( $filename )
106 :     #
107 : golsen 1.11 # Evaluate nucleotide alignments:
108 :     #
109 :     # $fraction_diff = fraction_nt_diff( $seq1, $seq2, \%options )
110 :     # $fraction_diff = fraction_nt_diff( $seq1, $seq2 )
111 :     # $fraction_diff = fraction_nt_diff( $seq1, $seq2, $gap_weight )
112 :     # $fraction_diff = fraction_nt_diff( $seq1, $seq2, $gap_open, $gap_extend )
113 :     #
114 :     # ( $npos, $nid, $ndif, $ngap, $nopen, $tgap, $topen ) = interpret_nt_align( $seq1, $seq2 )
115 :     #
116 : golsen 1.2
117 :     use strict;
118 : golsen 1.9 use Carp;
119 : efrank 1.1
120 : golsen 1.2 # Exported global variables:
121 : efrank 1.1
122 : golsen 1.2 our @aa_1_letter_order; # Alpha by 1 letter
123 :     our @aa_3_letter_order; # PAM matrix order
124 :     our @aa_n_codon_order;
125 :     our %genetic_code;
126 :     our %genetic_code_with_U;
127 :     our %amino_acid_codons_DNA;
128 :     our %amino_acid_codons_RNA;
129 :     our %n_codon_for_aa;
130 :     our %reverse_genetic_code_DNA;
131 :     our %reverse_genetic_code_RNA;
132 :     our %DNA_letter_can_be;
133 :     our %RNA_letter_can_be;
134 :     our %one_letter_to_three_letter_aa;
135 :     our %three_letter_to_one_letter_aa;
136 : efrank 1.1
137 :     require Exporter;
138 :    
139 :     our @ISA = qw(Exporter);
140 :     our @EXPORT = qw(
141 :     read_fasta_seqs
142 : overbeek 1.4 read_fasta
143 : efrank 1.1 read_next_fasta_seq
144 : overbeek 1.4 read_clustal_file
145 :     read_clustal
146 : efrank 1.1 parse_fasta_title
147 :     split_fasta_title
148 :     print_seq_list_as_fasta
149 : overbeek 1.4 print_alignment_as_fasta
150 :     print_alignment_as_phylip
151 :     print_alignment_as_nexus
152 : efrank 1.1 print_seq_as_fasta
153 :     print_gb_locus
154 :    
155 :     index_seq_list
156 :     seq_entry_by_id
157 :     seq_desc_by_id
158 :     seq_data_by_id
159 :    
160 : golsen 1.5 pack_alignment
161 :    
162 : efrank 1.1 subseq_DNA_entry
163 :     subseq_RNA_entry
164 : golsen 1.9 DNA_subseq
165 :     RNA_subseq
166 : efrank 1.1 complement_DNA_entry
167 :     complement_RNA_entry
168 :     complement_DNA_seq
169 :     complement_RNA_seq
170 :     to_DNA_seq
171 :     to_RNA_seq
172 : golsen 1.2 pack_seq
173 : efrank 1.1 clean_ae_sequence
174 :    
175 :     translate_seq
176 :     translate_codon
177 :     translate_seq_with_user_code
178 :    
179 :     read_intervals
180 : golsen 1.2 standardize_intervals
181 : efrank 1.1 join_intervals
182 : golsen 1.2 locations_2_intervals
183 :     read_oriented_intervals
184 :     reverse_intervals
185 : overbeek 1.4
186 :     gb_location_2_seed
187 : golsen 1.7
188 :     read_qual
189 : golsen 1.11
190 :     fraction_nt_diff
191 :     interpret_nt_align
192 : efrank 1.1 );
193 :    
194 : golsen 1.2 our @EXPORT_OK = qw(
195 :     @aa_1_letter_order
196 :     @aa_3_letter_order
197 :     @aa_n_codon_order
198 :     %genetic_code
199 :     %genetic_code_with_U
200 :     %amino_acid_codons_DNA
201 :     %amino_acid_codons_RNA
202 :     %n_codon_for_aa
203 :     %reverse_genetic_code_DNA
204 :     %reverse_genetic_code_RNA
205 :     %DNA_letter_can_be
206 :     %RNA_letter_can_be
207 :     %one_letter_to_three_letter_aa
208 :     %three_letter_to_one_letter_aa
209 :     );
210 : efrank 1.1
211 :    
212 :     #-----------------------------------------------------------------------------
213 : overbeek 1.4 # Helper function for defining an input filehandle:
214 :     # filehandle is passed through
215 :     # string is taken as file name to be openend
216 :     # undef or "" defaults to STDOUT
217 :     #
218 :     # ( \*FH, $name, $close [, $file] ) = input_filehandle( $file );
219 :     #
220 :     #-----------------------------------------------------------------------------
221 :     sub input_filehandle
222 :     {
223 :     my $file = shift;
224 :    
225 :     # FILEHANDLE
226 :    
227 :     return ( $file, $file, 0 ) if ( ref( $file ) eq "GLOB" );
228 :    
229 :     # Null string or undef
230 :    
231 :     return ( \*STDIN, "", 0 ) if ( ! defined( $file ) || ( $file eq "" ) );
232 :    
233 :     # File name
234 :    
235 :     if ( ! ref( $file ) )
236 :     {
237 :     my $fh;
238 : golsen 1.12 if ( -f $file ) { }
239 :     elsif ( $file =~ /^>(.+)$/ && -f $1 ) { $file = $1 }
240 :     else { die "Could not find input file '$file'\n" }
241 :     open( $fh, "<$file" ) || die "Could not open '$file' for input\n";
242 : overbeek 1.4 return ( $fh, $file, 1 );
243 :     }
244 :    
245 :     # Some other kind of reference; return the unused value
246 :    
247 :     return ( \*STDIN, undef, 0, $file );
248 :     }
249 :    
250 :    
251 :     #-----------------------------------------------------------------------------
252 :     # Read fasta sequences from a filehandle (legacy interface; use read_fasta)
253 : efrank 1.1 # Save the contents in a list of refs to arrays: (id, description, seq)
254 :     #
255 : overbeek 1.4 # @seq_entries = read_fasta_seqs( \*FILEHANDLE )
256 :     #-----------------------------------------------------------------------------
257 :     sub read_fasta_seqs { read_fasta( @_ ) }
258 :    
259 :    
260 : efrank 1.1 #-----------------------------------------------------------------------------
261 : golsen 1.12 # Read fasta sequences. Save the contents in a list of refs to arrays:
262 :     #
263 :     # $seq_entry = [ id, description, seq ]
264 : overbeek 1.4 #
265 :     # @seq_entries = read_fasta( ) # STDIN
266 :     # \@seq_entries = read_fasta( ) # STDIN
267 :     # @seq_entries = read_fasta( \*FILEHANDLE )
268 :     # \@seq_entries = read_fasta( \*FILEHANDLE )
269 :     # @seq_entries = read_fasta( $filename )
270 :     # \@seq_entries = read_fasta( $filename )
271 : golsen 1.12 # # @seq_entries = read_fasta( "command |" ) # open and read from pipe
272 :     # # \@seq_entries = read_fasta( "command |" ) # open and read from pipe
273 :     #
274 : overbeek 1.4 #-----------------------------------------------------------------------------
275 : golsen 1.12 sub read_fasta
276 :     {
277 :     my @seqs = map { $_->[2] =~ tr/ \n\r\t//d; $_ }
278 : golsen 1.13 map { /^(\S+)([ \t]+([^\n]*\S)?\s*)?\n(.+)$/s ? [ $1, $3 || '', $4 ] : () }
279 : golsen 1.12 split /^>\s*/m, slurp( @_ );
280 :     wantarray() ? @seqs : \@seqs;
281 :     }
282 :    
283 :     #-----------------------------------------------------------------------------
284 :     # A fast file reader:
285 :     #
286 :     # $data = slurp( ) # \*STDIN
287 :     # $data = slurp( \*FILEHANDLE ) # an open file handle
288 :     # $data = slurp( $filename ) # a file name
289 :     # $data = slurp( "<$filename" ) # file with explicit direction
290 :     # # $data = slurp( "$command |" ) # open and read from pipe
291 :     #
292 :     # Note: It is faster to read lines by reading the file and splitting
293 :     # than by reading the lines sequentially. If space is not an
294 :     # issue, this is the way to go. If space is an issue, then lines
295 :     # or records should be processed one-by-one (rather than loading
296 :     # the whole input into a string or array).
297 :     #-----------------------------------------------------------------------------
298 :     sub slurp
299 :     {
300 :     my ( $fh, $close );
301 :     if ( ref $_[0] eq 'GLOB' )
302 :     {
303 :     $fh = shift;
304 :     }
305 :     elsif ( $_[0] && ! ref $_[0] )
306 :     {
307 :     my $file = shift;
308 :     if ( -f $file ) { $file = "<$file" }
309 :     elsif ( $file =~ /^<(.*)$/ && -f $1 ) { } # Explicit read
310 :     # elsif ( $file =~ /\S\s*\|$/ ) { } # Read from a pipe
311 :     else { return undef }
312 :     open $fh, $file or return undef;
313 :     $close = 1;
314 :     }
315 :     else
316 :     {
317 :     $fh = \*STDIN;
318 :     }
319 :    
320 :     my $out = '';
321 :     my $inc = 1048576;
322 :     my $end = 0;
323 :     my $read;
324 :     while ( $read = read( $fh, $out, $inc, $end ) ) { $end += $read }
325 :     close( $fh ) if $close;
326 :    
327 :     $out;
328 :     }
329 :    
330 :    
331 :     #-----------------------------------------------------------------------------
332 :     # Previous, 50% slower fasta reader:
333 :     #-----------------------------------------------------------------------------
334 :     sub read_fasta_0
335 :     {
336 : overbeek 1.4 my ( $fh, $name, $close, $unused ) = input_filehandle( $_[0] );
337 :     $unused && die "Bad reference type (" . ref( $unused ) . ") passed to read_fasta\n";
338 : efrank 1.1
339 :     my @seqs = ();
340 :     my ($id, $desc, $seq) = ("", "", "");
341 :    
342 : overbeek 1.4 while ( <$fh> ) {
343 : efrank 1.1 chomp;
344 :     if (/^>\s*(\S+)(\s+(.*))?$/) { # new id
345 :     if ($id && $seq) { push @seqs, [ $id, $desc, $seq ] }
346 :     ($id, $desc, $seq) = ($1, $3 ? $3 : "", "");
347 :     }
348 :     else {
349 : golsen 1.2 tr/ 0-9//d;
350 : efrank 1.1 $seq .= $_ ;
351 :     }
352 :     }
353 : overbeek 1.4 close( $fh ) if $close;
354 : efrank 1.1
355 : overbeek 1.4 if ( $id && $seq ) { push @seqs, [ $id, $desc, $seq ] }
356 :     return wantarray ? @seqs : \@seqs;
357 : efrank 1.1 }
358 :    
359 :    
360 :     #-----------------------------------------------------------------------------
361 : golsen 1.12 # Read one fasta sequence at a time from a file. This is half as fast a
362 :     # read_fasta(), but can handle an arbitrarily large file. State information
363 :     # is retained in hashes, so any number of streams can be interlaced.
364 :     #
365 :     # @entry = read_next_fasta_seq( \*FILEHANDLE )
366 :     # \@entry = read_next_fasta_seq( \*FILEHANDLE )
367 :     # @entry = read_next_fasta_seq( $filename )
368 :     # \@entry = read_next_fasta_seq( $filename )
369 :     # @entry = read_next_fasta_seq() # \*STDIN
370 :     # \@entry = read_next_fasta_seq() # \*STDIN
371 : efrank 1.1 #
372 : golsen 1.12 # @entry = ( $id, $description, $seq )
373 :     #
374 :     # When reading at the end of file, () is returned.
375 :     # With a filename, reading past this will reopen the file at the beginning.
376 : efrank 1.1 #-----------------------------------------------------------------------------
377 :     # Reading always overshoots, so save next id and description
378 :    
379 : golsen 1.2 { # Use bare block to scope the header hash
380 :    
381 :     my %next_header;
382 : golsen 1.12 my %file_handle;
383 :     my %close_file;
384 : golsen 1.2
385 : golsen 1.12 sub read_next_fasta_seq
386 :     {
387 : overbeek 1.14 $_[0] ||= \*STDIN; # Undefined $_[0] fails with use warn
388 : golsen 1.12 my $fh = $file_handle{ $_[0] };
389 :     if ( ! $fh )
390 :     {
391 :     if ( ref $_[0] )
392 :     {
393 :     return () if ref $_[0] ne 'GLOB';
394 :     $fh = $_[0];
395 :     }
396 : overbeek 1.14 else
397 : golsen 1.12 {
398 :     my $file = $_[0];
399 :     if ( -f $file ) { $file = "<$file" }
400 :     elsif ( $file =~ /^<(.*)$/ && -f $1 ) { } # Explicit read
401 :     # elsif ( $file =~ /\S\s*\|$/ ) { } # Read from a pipe
402 :     else { return () }
403 :     open $fh, $file or return ();
404 :     $close_file{ $fh } = 1;
405 :     }
406 :     $file_handle{ $_[0] } = $fh;
407 :     }
408 : efrank 1.1
409 : golsen 1.12 my ( $id, $desc, $seq ) = ( undef, '', '' );
410 :     if ( defined( $next_header{$fh} ) )
411 :     {
412 : golsen 1.2 ( $id, $desc ) = parse_fasta_title( $next_header{$fh} );
413 : efrank 1.1 }
414 : golsen 1.12 else
415 :     {
416 :     $next_header{$fh} = '';
417 : golsen 1.2 }
418 :    
419 : golsen 1.12 while ( <$fh> )
420 :     {
421 : golsen 1.2 chomp;
422 : golsen 1.12 if ( /^>/ ) # new id
423 :     {
424 : golsen 1.2 $next_header{$fh} = $_;
425 : overbeek 1.4 if ( defined($id) && $seq )
426 :     {
427 :     return wantarray ? ($id, $desc, $seq) : [$id, $desc, $seq]
428 :     }
429 : golsen 1.2 ( $id, $desc ) = parse_fasta_title( $next_header{$fh} );
430 : golsen 1.12 $seq = '';
431 : golsen 1.2 }
432 : golsen 1.12 else
433 :     {
434 :     tr/ \t\r//d;
435 :     $seq .= $_;
436 : golsen 1.2 }
437 : efrank 1.1 }
438 :    
439 : golsen 1.12 # Done with file; there is no next header:
440 :    
441 :     delete $next_header{ $fh };
442 :    
443 :     # Return last set of data:
444 :    
445 :     if ( defined($id) && $seq )
446 :     {
447 :     return wantarray ? ($id,$desc,$seq) : [$id,$desc,$seq]
448 :     }
449 :    
450 :     # Or close everything out (returning the empty list tells caller
451 :     # that we are done)
452 : efrank 1.1
453 : golsen 1.12 if ( $close_file{ $fh } ) { close $fh; delete $close_file{ $fh } }
454 :     delete $file_handle{ $_[0] };
455 :    
456 :     return ();
457 : golsen 1.2 }
458 : efrank 1.1 }
459 :    
460 :    
461 :     #-----------------------------------------------------------------------------
462 : overbeek 1.4 # Read a clustal alignment from a file.
463 :     # Save the contents in a list of refs to arrays: (id, description, seq)
464 :     #
465 :     # @seq_entries = read_clustal_file( $filename )
466 :     #-----------------------------------------------------------------------------
467 :     sub read_clustal_file { read_clustal( @_ ) }
468 :    
469 :    
470 :     #-----------------------------------------------------------------------------
471 :     # Read a clustal alignment.
472 :     # Save the contents in a list of refs to arrays: (id, description, seq)
473 :     #
474 :     # @seq_entries = read_clustal( ) # STDIN
475 :     # @seq_entries = read_clustal( \*FILEHANDLE )
476 :     # @seq_entries = read_clustal( $filename )
477 :     #-----------------------------------------------------------------------------
478 :     sub read_clustal {
479 :     my ( $fh, undef, $close, $unused ) = input_filehandle( shift );
480 :     $unused && die "Bad reference type (" . ref( $unused ) . ") passed to read_clustal_file\n";
481 :    
482 :     my ( %seq, @ids, $line );
483 :     while ( defined( $line = <$fh> ) )
484 :     {
485 :     ( $line =~ /^[A-Za-z0-9]/ ) or next;
486 :     chomp $line;
487 :     my @flds = split /\s+/, $line;
488 :     if ( @flds == 2 )
489 :     {
490 :     $seq{ $flds[0] } or push @ids, $flds[0];
491 :     push @{ $seq{ $flds[0] } }, $flds[1];
492 :     }
493 :     }
494 :     close( $fh ) if $close;
495 :    
496 :     map { [ $_, "", join( "", @{$seq{$_}} ) ] } @ids;
497 :     }
498 :    
499 :    
500 :     #-----------------------------------------------------------------------------
501 : efrank 1.1 # Parse a fasta file header to id and definition parts
502 :     #
503 :     # ($id, $def) = parse_fasta_title( $title )
504 :     # ($id, $def) = split_fasta_title( $title )
505 :     #-----------------------------------------------------------------------------
506 : golsen 1.12 sub parse_fasta_title
507 :     {
508 : efrank 1.1 my $title = shift;
509 : golsen 1.12 chomp $title;
510 :    
511 :     return $title =~ /^>?\s*(\S+)(\s+(.*\S)?\s*)?$/ ? ( $1, $3 || '' )
512 :     : $title =~ /^>/ ? ( '', '' )
513 :     : ( undef, undef )
514 : efrank 1.1 }
515 :    
516 : golsen 1.12 sub split_fasta_title { parse_fasta_title( @_ ) }
517 : efrank 1.1
518 :    
519 :     #-----------------------------------------------------------------------------
520 : overbeek 1.4 # Helper function for defining an output filehandle:
521 :     # filehandle is passed through
522 :     # string is taken as file name to be openend
523 :     # undef or "" defaults to STDOUT
524 :     #
525 :     # ( \*FH, $name, $close [, $file] ) = output_filehandle( $file );
526 : efrank 1.1 #
527 :     #-----------------------------------------------------------------------------
528 : overbeek 1.4 sub output_filehandle
529 :     {
530 :     my $file = shift;
531 :    
532 :     # FILEHANDLE
533 :    
534 :     return ( $file, $file, 0 ) if ( ref( $file ) eq "GLOB" );
535 :    
536 :     # Null string or undef
537 :    
538 :     return ( \*STDOUT, "", 0 ) if ( ! defined( $file ) || ( $file eq "" ) );
539 :    
540 :     # File name
541 :    
542 :     if ( ! ref( $file ) )
543 :     {
544 :     my $fh;
545 :     open( $fh, ">$file" ) || die "Could not open output $file\n";
546 :     return ( $fh, $file, 1 );
547 :     }
548 :    
549 :     # Some other kind of reference; return the unused value
550 :    
551 :     return ( \*STDOUT, undef, 0, $file );
552 :     }
553 :    
554 :    
555 :     #-----------------------------------------------------------------------------
556 :     # Legacy function for printing fasta sequence set:
557 :     #
558 :     # print_seq_list_as_fasta( \*FILEHANDLE, @seq_entry_list );
559 :     #-----------------------------------------------------------------------------
560 :     sub print_seq_list_as_fasta { print_alignment_as_fasta( @_ ) }
561 :    
562 :    
563 :     #-----------------------------------------------------------------------------
564 :     # Print list of sequence entries in fasta format.
565 :     # Missing, undef or "" filename defaults to STDOUT.
566 :     #
567 :     # print_alignment_as_fasta( @seq_entry_list ); # STDOUT
568 :     # print_alignment_as_fasta( \@seq_entry_list ); # STDOUT
569 :     # print_alignment_as_fasta( \*FILEHANDLE, @seq_entry_list );
570 :     # print_alignment_as_fasta( \*FILEHANDLE, \@seq_entry_list );
571 :     # print_alignment_as_fasta( $filename, @seq_entry_list );
572 :     # print_alignment_as_fasta( $filename, \@seq_entry_list );
573 :     #-----------------------------------------------------------------------------
574 :     sub print_alignment_as_fasta {
575 :     my ( $fh, undef, $close, $unused ) = output_filehandle( shift );
576 :     ( unshift @_, $unused ) if $unused;
577 :    
578 : overbeek 1.8 ( ref( $_[0] ) eq "ARRAY" ) or confess "Bad sequence entry passed to print_alignment_as_fasta\n";
579 : overbeek 1.4
580 :     # Expand the sequence entry list if necessary:
581 :    
582 :     if ( ref( $_[0]->[0] ) eq "ARRAY" ) { @_ = @{ $_[0] } }
583 :    
584 :     foreach my $seq_ptr ( @_ ) { print_seq_as_fasta( $fh, @$seq_ptr ) }
585 :    
586 :     close( $fh ) if $close;
587 :     }
588 :    
589 :    
590 :     #-----------------------------------------------------------------------------
591 :     # Print list of sequence entries in phylip format.
592 :     # Missing, undef or "" filename defaults to STDOUT.
593 :     #
594 :     # print_alignment_as_phylip( @seq_entry_list ); # STDOUT
595 :     # print_alignment_as_phylip( \@seq_entry_list ); # STDOUT
596 :     # print_alignment_as_phylip( \*FILEHANDLE, @seq_entry_list );
597 :     # print_alignment_as_phylip( \*FILEHANDLE, \@seq_entry_list );
598 :     # print_alignment_as_phylip( $filename, @seq_entry_list );
599 :     # print_alignment_as_phylip( $filename, \@seq_entry_list );
600 :     #-----------------------------------------------------------------------------
601 :     sub print_alignment_as_phylip {
602 :     my ( $fh, undef, $close, $unused ) = output_filehandle( shift );
603 :     ( unshift @_, $unused ) if $unused;
604 :    
605 :     ( ref( $_[0] ) eq "ARRAY" ) or die die "Bad sequence entry passed to print_alignment_as_phylip\n";
606 :    
607 :     my @seq_list = ( ref( $_[0]->[0] ) eq "ARRAY" ) ? @{ $_[0] } : @_;
608 :    
609 :     my ( %id2, %used );
610 :     my $maxlen = 0;
611 :     foreach ( @seq_list )
612 :     {
613 :     my ( $id, undef, $seq ) = @$_;
614 :    
615 :     # Need a name that is unique within 10 characters
616 :    
617 :     my $id2 = substr( $id, 0, 10 );
618 :     $id2 =~ s/_/ /g; # PHYLIP sequence files accept spaces
619 :     my $n = "0";
620 :     while ( $used{ $id2 } )
621 :     {
622 :     $n++;
623 :     $id2 = substr( $id, 0, 10 - length( $n ) ) . $n;
624 :     }
625 :     $used{ $id2 } = 1;
626 :     $id2{ $id } = $id2;
627 :    
628 :     # Prepare to pad sequences (should not be necessary, but ...)
629 :    
630 :     my $len = length( $seq );
631 :     $maxlen = $len if ( $len > $maxlen );
632 :     }
633 : efrank 1.1
634 : overbeek 1.4 my $nseq = @seq_list;
635 :     print $fh "$nseq $maxlen\n";
636 :     foreach ( @seq_list )
637 :     {
638 :     my ( $id, undef, $seq ) = @$_;
639 :     my $len = length( $seq );
640 :     printf $fh "%-10s %s%s\n", $id2{ $id },
641 :     $seq,
642 :     $len<$maxlen ? ("?" x ($maxlen-$len)) : "";
643 : efrank 1.1 }
644 : overbeek 1.4
645 :     close( $fh ) if $close;
646 :     }
647 :    
648 :    
649 :     #-----------------------------------------------------------------------------
650 :     # Print list of sequence entries in nexus format.
651 :     # Missing, undef or "" filename defaults to STDOUT.
652 :     #
653 :     # print_alignment_as_nexus( [ \%label_hash, ] @seq_entry_list );
654 :     # print_alignment_as_nexus( [ \%label_hash, ] \@seq_entry_list );
655 :     # print_alignment_as_nexus( \*FILEHANDLE, [ \%label_hash, ] @seq_entry_list );
656 :     # print_alignment_as_nexus( \*FILEHANDLE, [ \%label_hash, ] \@seq_entry_list );
657 :     # print_alignment_as_nexus( $filename, [ \%label_hash, ] @seq_entry_list );
658 :     # print_alignment_as_nexus( $filename, [ \%label_hash, ] \@seq_entry_list );
659 :     #-----------------------------------------------------------------------------
660 :     sub print_alignment_as_nexus {
661 :     my ( $fh, undef, $close, $unused ) = output_filehandle( shift );
662 :     ( unshift @_, $unused ) if $unused;
663 :    
664 :     my $lbls = ( ref( $_[0] ) eq "HASH" ) ? shift : undef;
665 :    
666 :     ( ref( $_[0] ) eq "ARRAY" ) or die "Bad sequence entry passed to print_alignment_as_nexus\n";
667 :    
668 :     my @seq_list = ( ref( $_[0]->[0] ) eq "ARRAY" ) ? @{ $_[0] } : @_;
669 :    
670 :     my %id2;
671 :     my ( $maxidlen, $maxseqlen ) = ( 0, 0 );
672 :     my ( $n1, $n2, $nt, $nu ) = ( 0, 0, 0, 0 );
673 :     foreach ( @seq_list )
674 :     {
675 :     my ( $id, undef, $seq ) = @$_;
676 :     my $id2 = $lbls ? ( $lbls->{ $id } || $id ) : $id;
677 :     if ( $id2 !~ /^[-+.0-9A-Za-z~_|]+$/ )
678 :     {
679 :     $id2 =~ s/'/''/g;
680 :     $id2 = qq('$id2');
681 :     }
682 :     $id2{ $id } = $id2;
683 :     my $idlen = length( $id2 );
684 :     $maxidlen = $idlen if ( $idlen > $maxidlen );
685 :    
686 :     my $seqlen = length( $seq );
687 :     $maxseqlen = $seqlen if ( $seqlen > $maxseqlen );
688 :    
689 :     $nt += $seq =~ tr/Tt//d;
690 :     $nu += $seq =~ tr/Uu//d;
691 :     $n1 += $seq =~ tr/ACGNacgn//d;
692 :     $n2 += $seq =~ tr/A-Za-z//d;
693 :     }
694 :    
695 :     my $nseq = @seq_list;
696 :     my $type = ( $n1 < 2 * $n2 ) ? 'protein' : ($nt>$nu) ? 'DNA' : 'RNA';
697 :    
698 :     print $fh <<"END_HEAD";
699 :     #NEXUS
700 :    
701 :     BEGIN Data;
702 :     Dimensions
703 :     NTax=$nseq
704 :     NChar=$maxseqlen
705 :     ;
706 :     Format
707 :     DataType=$type
708 :     Gap=-
709 :     Missing=?
710 :     ;
711 :     Matrix
712 :    
713 :     END_HEAD
714 :    
715 :     foreach ( @seq_list )
716 :     {
717 :     my ( $id, undef, $seq ) = @$_;
718 :     my $len = length( $seq );
719 :     printf $fh "%-${maxidlen}s %s%s\n",
720 :     $id2{ $id },
721 :     $seq,
722 :     $len<$maxseqlen ? ("?" x ($maxseqlen-$len)) : "";
723 :     }
724 :    
725 :     print $fh <<"END_TAIL";
726 :     ;
727 :     END;
728 :     END_TAIL
729 :    
730 :     close( $fh ) if $close;
731 : efrank 1.1 }
732 :    
733 :    
734 :     #-----------------------------------------------------------------------------
735 :     # Print one sequence in fasta format to an open file
736 :     #
737 : overbeek 1.4 # print_seq_as_fasta( \*FILEHANDLE, $id, $desc, $seq );
738 : overbeek 1.14 # print_seq_as_fasta( $id, $desc, $seq );
739 : efrank 1.1 #-----------------------------------------------------------------------------
740 : overbeek 1.14 sub print_seq_as_fasta
741 :     {
742 :     my $fh = ( ref $_[0] eq 'GLOB' ) ? shift : \*STDOUT;
743 :     print $fh $_[1] =~ /\S/ ? ">$_[0] $_[1]\n" : ">$_[0]\n";
744 :     print $fh join( "\n", $_[2] =~ m/.{1,60}/g ), "\n";
745 : efrank 1.1 }
746 :    
747 :    
748 :     #-----------------------------------------------------------------------------
749 :     # Print one sequence in GenBank flat file format:
750 :     #
751 : overbeek 1.4 # print_gb_locus( \*FILEHANDLE, $locus, $def, $accession, $seq )
752 : efrank 1.1 #-----------------------------------------------------------------------------
753 :     sub print_gb_locus {
754 :     my ($fh, $loc, $def, $acc, $seq) = @_;
755 :     my ($len, $i, $imax);
756 :     my $istep = 10;
757 :    
758 :     $len = length($seq);
759 :     printf $fh "LOCUS %-10s%7d bp\n", substr($loc,0,10), $len;
760 :     print $fh "DEFINITION " . substr(wrap_text($def,80,12), 12) . "\n";
761 :     if ($acc) { print $fh "ACCESSION $acc\n" }
762 :     print $fh "ORIGIN\n";
763 :    
764 :     for ($i = 1; $i <= $len; ) {
765 :     printf $fh "%9d", $i;
766 :     $imax = $i + 59; if ($imax > $len) { $imax = $len }
767 :     for ( ; $i <= $imax; $i += $istep) {
768 :     print $fh " " . substr($seq, $i-1, $istep);
769 :     }
770 :     print $fh "\n";
771 :     }
772 :     print $fh "//\n";
773 :     }
774 :    
775 :    
776 :     #-----------------------------------------------------------------------------
777 : golsen 1.7 # Return a string with text wrapped to defined line lengths:
778 :     #
779 :     # $wrapped_text = wrap_text( $str ) # default len = 80
780 :     # $wrapped_text = wrap_text( $str, $len ) # default ind = 0
781 :     # $wrapped_text = wrap_text( $str, $len, $indent ) # default ind_n = ind
782 :     # $wrapped_text = wrap_text( $str, $len, $indent_1, $indent_n )
783 :     #-----------------------------------------------------------------------------
784 :     sub wrap_text {
785 :     my ($str, $len, $ind, $indn) = @_;
786 :    
787 :     defined($str) || die "wrap_text called without a string\n";
788 :     defined($len) || ($len = 80);
789 :     defined($ind) || ($ind = 0);
790 :     ($ind < $len) || die "wrap error: indent greater than line length\n";
791 :     defined($indn) || ($indn = $ind);
792 :     ($indn < $len) || die "wrap error: indent_n greater than line length\n";
793 :    
794 :     $str =~ s/\s+$//;
795 :     $str =~ s/^\s+//;
796 :     my ($maxchr, $maxchr1);
797 :     my (@lines) = ();
798 :    
799 :     while ($str) {
800 :     $maxchr1 = ($maxchr = $len - $ind) - 1;
801 :     if ($maxchr >= length($str)) {
802 :     push @lines, (" " x $ind) . $str;
803 :     last;
804 :     }
805 :     elsif ($str =~ /^(.{0,$maxchr1}\S)\s+(\S.*)$/) { # no expr in {}
806 :     push @lines, (" " x $ind) . $1;
807 :     $str = $2;
808 :     }
809 :     elsif ($str =~ /^(.{0,$maxchr1}-)(.*)$/) {
810 :     push @lines, (" " x $ind) . $1;
811 :     $str = $2;
812 :     }
813 :     else {
814 :     push @lines, (" " x $ind) . substr($str, 0, $maxchr);
815 :     $str = substr($str, $maxchr);
816 :     }
817 :     $ind = $indn;
818 :     }
819 :    
820 :     return join("\n", @lines);
821 :     }
822 :    
823 :    
824 :     #-----------------------------------------------------------------------------
825 : efrank 1.1 # Build an index from seq_id to pointer to sequence entry: (id, desc, seq)
826 :     #
827 : overbeek 1.4 # my \%seq_ind = index_seq_list( @seq_list );
828 :     # my \%seq_ind = index_seq_list( \@seq_list );
829 : efrank 1.1 #
830 :     # Usage example:
831 :     #
832 : overbeek 1.4 # my @seq_list = read_fasta_seqs(\*STDIN); # list of pointers to entries
833 :     # my \%seq_ind = index_seq_list(@seq_list); # hash from names to pointers
834 :     # my @chosen_seq = @{%seq_ind{"contig1"}}; # extract one entry
835 : efrank 1.1 #
836 :     #-----------------------------------------------------------------------------
837 :     sub index_seq_list {
838 : overbeek 1.4 ( ref( $_[0] ) ne 'ARRAY' ) ? {}
839 :     : ( ref( $_[0]->[0] ) ne 'ARRAY' ) ? { map { $_->[0] => $_ } @_ }
840 :     : { map { $_->[0] => $_ } @{ $_[0] } }
841 : efrank 1.1 }
842 :    
843 :    
844 :     #-----------------------------------------------------------------------------
845 :     # Three routines to access all or part of sequence entry by id:
846 :     #
847 : overbeek 1.4 # @seq_entry = seq_entry_by_id( \%seq_index, $seq_id );
848 :     # \@seq_entry = seq_entry_by_id( \%seq_index, $seq_id );
849 :     # $seq_desc = seq_desc_by_id( \%seq_index, $seq_id );
850 :     # $seq = seq_data_by_id( \%seq_index, $seq_id );
851 : efrank 1.1 #
852 :     #-----------------------------------------------------------------------------
853 :     sub seq_entry_by_id {
854 :     (my $ind_ref = shift) || die "No index supplied to seq_entry_by_id\n";
855 :     (my $id = shift) || die "No id supplied to seq_entry_by_id\n";
856 : overbeek 1.4 return wantarray ? @{ $ind_ref->{$id} } : $ind_ref->{$id};
857 : efrank 1.1 }
858 :    
859 :    
860 :     sub seq_desc_by_id {
861 :     (my $ind_ref = shift) || die "No index supplied to seq_desc_by_id\n";
862 :     (my $id = shift) || die "No id supplied to seq_desc_by_id\n";
863 :     return ${ $ind_ref->{$id} }[1];
864 :     }
865 :    
866 :    
867 :     sub seq_data_by_id {
868 :     (my $ind_ref = shift) || die "No index supplied to seq_data_by_id\n";
869 :     (my $id = shift) || die "No id supplied to seq_data_by_id\n";
870 :     return ${ $ind_ref->{$id} }[2];
871 :     }
872 :    
873 : golsen 1.5 #-----------------------------------------------------------------------------
874 :     # Remove columns of alignment gaps from sequences:
875 :     #
876 : golsen 1.6 # @packed_seqs = pack_alignment( @seqs )
877 :     # @packed_seqs = pack_alignment( \@seqs )
878 :     # \@packed_seqs = pack_alignment( @seqs )
879 :     # \@packed_seqs = pack_alignment( \@seqs )
880 : golsen 1.5 #
881 :     #-----------------------------------------------------------------------------
882 :    
883 :     sub pack_alignment
884 :     {
885 :     my @seqs = ( ref( $_[0] ) eq 'ARRAY' and ref( $_[0]->[0] ) eq 'ARRAY' ) ? @{$_[0] } : @_;
886 :     @seqs or return wantarray ? () : [];
887 :    
888 :     my $mask = pack_mask( $seqs[0]->[2] );
889 :     foreach ( @seqs[ 1 .. (@seqs-1) ] )
890 :     {
891 :     $mask |= pack_mask( $_->[2] );
892 :     }
893 :    
894 :     my $seq;
895 :     my @seqs2 = map { $seq = $_->[2] & $mask;
896 :     $seq =~ tr/\000//d;
897 :     [ $_->[0], $_->[1], $seq ]
898 :     }
899 :     @seqs;
900 :    
901 :     return wantarray ? @seqs2 : \@seqs2;
902 :     }
903 :    
904 :     sub pack_mask
905 :     {
906 :     my $mask = shift;
907 :     $mask =~ tr/-/\000/;
908 :     $mask =~ tr/\000/\377/c;
909 :     return $mask;
910 :     }
911 : efrank 1.1
912 :     #-----------------------------------------------------------------------------
913 :     # Some simple sequence manipulations:
914 :     #
915 :     # @entry = subseq_DNA_entry( @seq_entry, $from, $to [, $fix_id] );
916 :     # @entry = subseq_RNA_entry( @seq_entry, $from, $to [, $fix_id] );
917 :     # @entry = complement_DNA_entry( @seq_entry [, $fix_id] );
918 :     # @entry = complement_RNA_entry( @seq_entry [, $fix_id] );
919 :     # $DNAseq = complement_DNA_seq( $NA_seq );
920 :     # $RNAseq = complement_RNA_seq( $NA_seq );
921 :     # $DNAseq = to_DNA_seq( $NA_seq );
922 :     # $RNAseq = to_RNA_seq( $NA_seq );
923 :     #
924 :     #-----------------------------------------------------------------------------
925 :    
926 :     sub subseq_DNA_entry {
927 :     my ($id, $desc, @rest) = @_;
928 :     wantarray || die "subseq_DNA_entry requires array context\n";
929 :    
930 :     my $seq;
931 :     ($id, $seq) = subseq_nt(1, $id, @rest); # 1 is for DNA, not RNA
932 :     return ($id, $desc, $seq);
933 :     }
934 :    
935 :    
936 :     sub subseq_RNA_entry {
937 :     my ($id, $desc, @rest) = @_;
938 :     wantarray || die "subseq_RNA_entry requires array context\n";
939 :    
940 :     my $seq;
941 :     ($id, $seq) = subseq_nt(0, $id, @rest); # 0 is for not DNA, i.e., RNA
942 :     return ($id, $desc, $seq);
943 :     }
944 :    
945 :    
946 :     sub subseq_nt {
947 :     my ($DNA, $id, $seq, $from, $to, $fix_id) = @_;
948 :     $fix_id ||= 0; # fix undef value
949 :    
950 :     my $len = length($seq);
951 :     if ( ( $from eq '$' ) || ( $from eq "" ) ) { $from = $len }
952 :     if (! $to || ( $to eq '$' ) || ( $to eq "" ) ) { $to = $len }
953 :    
954 :     my $left = ( $from < $to ) ? $from : $to;
955 :     my $right = ( $from < $to ) ? $to : $from;
956 :     if ( ( $right < 1 ) || ( $left > $len ) ) { return ($id, "") }
957 :     if ( $right > $len ) { $right = $len }
958 :     if ( $left < 1 ) { $left = 1 }
959 :    
960 :     $seq = substr($seq, $left-1, $right-$left+1);
961 :     if ( $from > $to ) {
962 :     $seq = reverse $seq;
963 :     if ( $DNA ) {
964 :     $seq =~ tr[ACGTUKMRSWYBDHVNacgtukmrswybdhvn]
965 :     [TGCAAMKYSWRVHDBNtgcaamkyswrvhdbn];
966 :     }
967 :     else {
968 :     $seq =~ tr[ACGTUKMRSWYBDHVNacgtukmrswybdhvn]
969 :     [UGCAAMKYSWRVHDBNugcaamkyswrvhdbn];
970 :     }
971 :     }
972 :    
973 :     if ( $fix_id ) {
974 : golsen 1.2 if ( ( $id =~ s/_(\d+)_(\d+)$// )
975 : efrank 1.1 && ( abs($2-$1)+1 == $len ) ) {
976 :     if ( $1 <= $2 ) { $from += $1 - 1; $to += $1 - 1 }
977 :     else { $from = $1 + 1 - $from; $to = $1 + 1 - $to }
978 :     }
979 :     $id .= "_" . $from . "_" . $to;
980 :     }
981 :    
982 :     return ($id, $seq);
983 :     }
984 :    
985 :    
986 : golsen 1.9 sub DNA_subseq
987 :     {
988 :     my ( $seq, $from, $to ) = @_;
989 :    
990 :     my $len = ref( $seq ) eq 'SCALAR' ? length( $$seq )
991 :     : length( $seq );
992 :     if ( ( $from eq '$' ) || ( $from eq "" ) ) { $from = $len }
993 :     if ( ( $to eq '$' ) || ( ! $to ) ) { $to = $len }
994 :    
995 :     my $left = ( $from < $to ) ? $from : $to;
996 :     my $right = ( $from < $to ) ? $to : $from;
997 :     if ( ( $right < 1 ) || ( $left > $len ) ) { return "" }
998 :     if ( $right > $len ) { $right = $len }
999 :     if ( $left < 1 ) { $left = 1 }
1000 :    
1001 :     my $subseq = ref( $seq ) eq 'SCALAR' ? substr( $$seq, $left-1, $right-$left+1 )
1002 :     : substr( $seq, $left-1, $right-$left+1 );
1003 :    
1004 :     if ( $from > $to )
1005 :     {
1006 :     $subseq = reverse $subseq;
1007 :     $subseq =~ tr[ACGTUKMRSWYBDHVNacgtukmrswybdhvn]
1008 :     [TGCAAMKYSWRVHDBNtgcaamkyswrvhdbn];
1009 :     }
1010 :    
1011 :     $subseq
1012 :     }
1013 :    
1014 :    
1015 :     sub RNA_subseq
1016 :     {
1017 :     my ( $seq, $from, $to ) = @_;
1018 :    
1019 :     my $len = ref( $seq ) eq 'SCALAR' ? length( $$seq )
1020 :     : length( $seq );
1021 :     if ( ( $from eq '$' ) || ( $from eq "" ) ) { $from = $len }
1022 :     if ( ( $to eq '$' ) || ( ! $to ) ) { $to = $len }
1023 :    
1024 :     my $left = ( $from < $to ) ? $from : $to;
1025 :     my $right = ( $from < $to ) ? $to : $from;
1026 :     if ( ( $right < 1 ) || ( $left > $len ) ) { return "" }
1027 :     if ( $right > $len ) { $right = $len }
1028 :     if ( $left < 1 ) { $left = 1 }
1029 :    
1030 :     my $subseq = ref( $seq ) eq 'SCALAR' ? substr( $$seq, $left-1, $right-$left+1 )
1031 :     : substr( $seq, $left-1, $right-$left+1 );
1032 :    
1033 :     if ( $from > $to )
1034 :     {
1035 :     $subseq = reverse $subseq;
1036 :     $subseq =~ tr[ACGTUKMRSWYBDHVNacgtukmrswybdhvn]
1037 :     [UGCAAMKYSWRVHDBNugcaamkyswrvhdbn];
1038 :     }
1039 :    
1040 :     $subseq
1041 :     }
1042 :    
1043 :    
1044 : efrank 1.1 sub complement_DNA_entry {
1045 :     my ($id, $desc, $seq, $fix_id) = @_;
1046 :     $fix_id ||= 0; # fix undef values
1047 :    
1048 :     wantarray || die "complement_DNA_entry requires array context\n";
1049 :     $seq = reverse $seq;
1050 :     $seq =~ tr[ACGTUKMRSWYBDHVNacgtukmrswybdhvn]
1051 :     [TGCAAMKYSWRVHDBNtgcaamkyswrvhdbn];
1052 :     if ($fix_id) {
1053 : golsen 1.2 if ($id =~ s/_(\d+)_(\d+)$//) {
1054 : efrank 1.1 $id .= "_" . $2 . "_" . $1;
1055 :     }
1056 :     else {
1057 :     $id .= "_" . length($seq) . "_1";
1058 :     }
1059 :     }
1060 :    
1061 :     return ($id, $desc, $seq);
1062 :     }
1063 :    
1064 :    
1065 :     sub complement_RNA_entry {
1066 :     my ($id, $desc, $seq, $fix_id) = @_;
1067 :     $fix_id ||= 0; # fix undef values
1068 :    
1069 :     wantarray || die "complement_DNA_entry requires array context\n";
1070 :     $seq = reverse $seq;
1071 :     $seq =~ tr[ACGTUKMRSWYBDHVNacgtukmrswybdhvn]
1072 :     [UGCAAMKYSWRVHDBNugcaamkyswrvhdbn];
1073 :     if ($fix_id) {
1074 : golsen 1.2 if ($id =~ s/_(\d+)_(\d+)$//) {
1075 : efrank 1.1 $id .= "_" . $2 . "_" . $1;
1076 :     }
1077 :     else {
1078 :     $id .= "_" . length($seq) . "_1";
1079 :     }
1080 :     }
1081 :    
1082 :     return ($id, $desc, $seq);
1083 :     }
1084 :    
1085 :    
1086 :     sub complement_DNA_seq {
1087 :     my $seq = reverse shift;
1088 :     $seq =~ tr[ACGTUKMRSWYBDHVNacgtukmrswybdhvn]
1089 :     [TGCAAMKYSWRVHDBNtgcaamkyswrvhdbn];
1090 :     return $seq;
1091 :     }
1092 :    
1093 :    
1094 :     sub complement_RNA_seq {
1095 :     my $seq = reverse shift;
1096 :     $seq =~ tr[ACGTUKMRSWYBDHVNacgtukmrswybdhvn]
1097 :     [UGCAAMKYSWRVHDBNugcaamkyswrvhdbn];
1098 :     return $seq;
1099 :     }
1100 :    
1101 :    
1102 :     sub to_DNA_seq {
1103 :     my $seq = shift;
1104 :     $seq =~ tr/Uu/Tt/;
1105 :     return $seq;
1106 :     }
1107 :    
1108 :    
1109 :     sub to_RNA_seq {
1110 :     my $seq = shift;
1111 :     $seq =~ tr/Tt/Uu/;
1112 :     return $seq;
1113 :     }
1114 :    
1115 :    
1116 : golsen 1.2 sub pack_seq {
1117 :     my $seq = shift;
1118 :     $seq =~ tr/A-Za-z//cd;
1119 :     return $seq;
1120 :     }
1121 :    
1122 :    
1123 : efrank 1.1 sub clean_ae_sequence {
1124 :     $_ = shift;
1125 :     $_ = to7bit($_);
1126 :     s/[+]/1/g;
1127 :     s/[^0-9A-IK-NP-Za-ik-np-z~.-]/-/g;
1128 :     return $_;
1129 :     }
1130 :    
1131 :    
1132 :     sub to7bit {
1133 :     $_ = shift;
1134 :     my ($o, $c);
1135 :     while (/\\([0-3][0-7][0-7])/) {
1136 :     $o = oct($1) % 128;
1137 :     $c = sprintf("%c", $o);
1138 :     s/\\$1/$c/g;
1139 :     }
1140 :     return $_;
1141 :     }
1142 :    
1143 :    
1144 :     sub to8bit {
1145 :     $_ = shift;
1146 :     my ($o, $c);
1147 :     while (/\\([0-3][0-7][0-7])/) {
1148 :     $o = oct($1);
1149 :     $c = sprintf("%c", $o);
1150 :     s/\\$1/$c/g;
1151 :     }
1152 :     return $_;
1153 :     }
1154 :    
1155 :    
1156 :    
1157 :     #-----------------------------------------------------------------------------
1158 :     # Translate nucleotides to one letter protein:
1159 :     #
1160 : golsen 1.10 # $seq = translate_seq( $seq [, $met_start] )
1161 :     # $aa = translate_codon( $triplet )
1162 :     # $aa = translate_DNA_codon( $triplet ) # Does not rely on DNA
1163 :     # $aa = translate_uc_DNA_codon( $triplet ) # Does not rely on uc or DNA
1164 : efrank 1.1 #
1165 :     # User-supplied genetic code must be upper case index and match the
1166 :     # DNA versus RNA type of sequence
1167 :     #
1168 : golsen 1.10 # $seq = translate_seq_with_user_code( $seq, $gen_code_hash [, $met_start] )
1169 : efrank 1.1 #
1170 :     #-----------------------------------------------------------------------------
1171 :    
1172 : golsen 1.2 @aa_1_letter_order = qw( A C D E F G H I K L M N P Q R S T V W Y ); # Alpha by 1 letter
1173 :     @aa_3_letter_order = qw( A R N D C Q E G H I L K M F P S T W Y V ); # PAM matrix order
1174 :     @aa_n_codon_order = qw( L R S A G P T V I C D E F H K N Q Y M W );
1175 :    
1176 :     %genetic_code = (
1177 :    
1178 :     # DNA version
1179 :    
1180 : golsen 1.10 TTT => 'F', TCT => 'S', TAT => 'Y', TGT => 'C',
1181 :     TTC => 'F', TCC => 'S', TAC => 'Y', TGC => 'C',
1182 :     TTA => 'L', TCA => 'S', TAA => '*', TGA => '*',
1183 :     TTG => 'L', TCG => 'S', TAG => '*', TGG => 'W',
1184 :     CTT => 'L', CCT => 'P', CAT => 'H', CGT => 'R',
1185 :     CTC => 'L', CCC => 'P', CAC => 'H', CGC => 'R',
1186 :     CTA => 'L', CCA => 'P', CAA => 'Q', CGA => 'R',
1187 :     CTG => 'L', CCG => 'P', CAG => 'Q', CGG => 'R',
1188 :     ATT => 'I', ACT => 'T', AAT => 'N', AGT => 'S',
1189 :     ATC => 'I', ACC => 'T', AAC => 'N', AGC => 'S',
1190 :     ATA => 'I', ACA => 'T', AAA => 'K', AGA => 'R',
1191 :     ATG => 'M', ACG => 'T', AAG => 'K', AGG => 'R',
1192 :     GTT => 'V', GCT => 'A', GAT => 'D', GGT => 'G',
1193 :     GTC => 'V', GCC => 'A', GAC => 'D', GGC => 'G',
1194 :     GTA => 'V', GCA => 'A', GAA => 'E', GGA => 'G',
1195 :     GTG => 'V', GCG => 'A', GAG => 'E', GGG => 'G',
1196 : golsen 1.2
1197 : efrank 1.1 # The following ambiguous encodings are not necessary, but
1198 : golsen 1.2 # speed up the processing of some ambiguous triplets:
1199 : efrank 1.1
1200 : golsen 1.10 TTY => 'F', TCY => 'S', TAY => 'Y', TGY => 'C',
1201 :     TTR => 'L', TCR => 'S', TAR => '*',
1202 :     TCN => 'S',
1203 :     CTY => 'L', CCY => 'P', CAY => 'H', CGY => 'R',
1204 :     CTR => 'L', CCR => 'P', CAR => 'Q', CGR => 'R',
1205 :     CTN => 'L', CCN => 'P', CGN => 'R',
1206 :     ATY => 'I', ACY => 'T', AAY => 'N', AGY => 'S',
1207 :     ACR => 'T', AAR => 'K', AGR => 'R',
1208 :     ACN => 'T',
1209 :     GTY => 'V', GCY => 'A', GAY => 'D', GGY => 'G',
1210 :     GTR => 'V', GCR => 'A', GAR => 'E', GGR => 'G',
1211 :     GTN => 'V', GCN => 'A', GGN => 'G'
1212 : golsen 1.2 );
1213 :    
1214 : golsen 1.10 # Add RNA by construction:
1215 :    
1216 :     foreach ( grep { /T/ } keys %genetic_code )
1217 :     {
1218 :     my $codon = $_;
1219 :     $codon =~ s/T/U/g;
1220 :     $genetic_code{ $codon } = lc $genetic_code{ $_ }
1221 :     }
1222 : golsen 1.2
1223 :     # Add lower case by construction:
1224 :    
1225 : golsen 1.10 foreach ( keys %genetic_code )
1226 :     {
1227 : golsen 1.2 $genetic_code{ lc $_ } = lc $genetic_code{ $_ }
1228 :     }
1229 :    
1230 :    
1231 : golsen 1.9 # Construct the genetic code with selenocysteine by difference:
1232 : golsen 1.2
1233 : golsen 1.10 %genetic_code_with_U = %genetic_code;
1234 :     $genetic_code_with_U{ TGA } = 'U';
1235 :     $genetic_code_with_U{ tga } = 'u';
1236 :     $genetic_code_with_U{ UGA } = 'U';
1237 :     $genetic_code_with_U{ uga } = 'u';
1238 : golsen 1.2
1239 :    
1240 :     %amino_acid_codons_DNA = (
1241 : overbeek 1.4 L => [ qw( TTA TTG CTA CTG CTT CTC ) ],
1242 :     R => [ qw( AGA AGG CGA CGG CGT CGC ) ],
1243 :     S => [ qw( AGT AGC TCA TCG TCT TCC ) ],
1244 :     A => [ qw( GCA GCG GCT GCC ) ],
1245 :     G => [ qw( GGA GGG GGT GGC ) ],
1246 :     P => [ qw( CCA CCG CCT CCC ) ],
1247 :     T => [ qw( ACA ACG ACT ACC ) ],
1248 :     V => [ qw( GTA GTG GTT GTC ) ],
1249 :     I => [ qw( ATA ATT ATC ) ],
1250 :     C => [ qw( TGT TGC ) ],
1251 :     D => [ qw( GAT GAC ) ],
1252 :     E => [ qw( GAA GAG ) ],
1253 :     F => [ qw( TTT TTC ) ],
1254 :     H => [ qw( CAT CAC ) ],
1255 :     K => [ qw( AAA AAG ) ],
1256 :     N => [ qw( AAT AAC ) ],
1257 :     Q => [ qw( CAA CAG ) ],
1258 :     Y => [ qw( TAT TAC ) ],
1259 :     M => [ qw( ATG ) ],
1260 :     U => [ qw( TGA ) ],
1261 :     W => [ qw( TGG ) ],
1262 :    
1263 :     l => [ qw( tta ttg cta ctg ctt ctc ) ],
1264 :     r => [ qw( aga agg cga cgg cgt cgc ) ],
1265 :     s => [ qw( agt agc tca tcg tct tcc ) ],
1266 :     a => [ qw( gca gcg gct gcc ) ],
1267 :     g => [ qw( gga ggg ggt ggc ) ],
1268 :     p => [ qw( cca ccg cct ccc ) ],
1269 :     t => [ qw( aca acg act acc ) ],
1270 :     v => [ qw( gta gtg gtt gtc ) ],
1271 :     i => [ qw( ata att atc ) ],
1272 :     c => [ qw( tgt tgc ) ],
1273 :     d => [ qw( gat gac ) ],
1274 :     e => [ qw( gaa gag ) ],
1275 :     f => [ qw( ttt ttc ) ],
1276 :     h => [ qw( cat cac ) ],
1277 :     k => [ qw( aaa aag ) ],
1278 :     n => [ qw( aat aac ) ],
1279 :     q => [ qw( caa cag ) ],
1280 :     y => [ qw( tat tac ) ],
1281 :     m => [ qw( atg ) ],
1282 :     u => [ qw( tga ) ],
1283 :     w => [ qw( tgg ) ],
1284 : golsen 1.2
1285 : overbeek 1.4 '*' => [ qw( TAA TAG TGA ) ]
1286 : efrank 1.1 );
1287 :    
1288 :    
1289 : golsen 1.2
1290 :     %amino_acid_codons_RNA = (
1291 : overbeek 1.4 L => [ qw( UUA UUG CUA CUG CUU CUC ) ],
1292 :     R => [ qw( AGA AGG CGA CGG CGU CGC ) ],
1293 :     S => [ qw( AGU AGC UCA UCG UCU UCC ) ],
1294 :     A => [ qw( GCA GCG GCU GCC ) ],
1295 :     G => [ qw( GGA GGG GGU GGC ) ],
1296 :     P => [ qw( CCA CCG CCU CCC ) ],
1297 :     T => [ qw( ACA ACG ACU ACC ) ],
1298 :     V => [ qw( GUA GUG GUU GUC ) ],
1299 :     B => [ qw( GAU GAC AAU AAC ) ],
1300 :     Z => [ qw( GAA GAG CAA CAG ) ],
1301 :     I => [ qw( AUA AUU AUC ) ],
1302 :     C => [ qw( UGU UGC ) ],
1303 :     D => [ qw( GAU GAC ) ],
1304 :     E => [ qw( GAA GAG ) ],
1305 :     F => [ qw( UUU UUC ) ],
1306 :     H => [ qw( CAU CAC ) ],
1307 :     K => [ qw( AAA AAG ) ],
1308 :     N => [ qw( AAU AAC ) ],
1309 :     Q => [ qw( CAA CAG ) ],
1310 :     Y => [ qw( UAU UAC ) ],
1311 :     M => [ qw( AUG ) ],
1312 :     U => [ qw( UGA ) ],
1313 :     W => [ qw( UGG ) ],
1314 :    
1315 :     l => [ qw( uua uug cua cug cuu cuc ) ],
1316 :     r => [ qw( aga agg cga cgg cgu cgc ) ],
1317 :     s => [ qw( agu agc uca ucg ucu ucc ) ],
1318 :     a => [ qw( gca gcg gcu gcc ) ],
1319 :     g => [ qw( gga ggg ggu ggc ) ],
1320 :     p => [ qw( cca ccg ccu ccc ) ],
1321 :     t => [ qw( aca acg acu acc ) ],
1322 :     v => [ qw( gua gug guu guc ) ],
1323 :     b => [ qw( gau gac aau aac ) ],
1324 :     z => [ qw( gaa gag caa cag ) ],
1325 :     i => [ qw( aua auu auc ) ],
1326 :     c => [ qw( ugu ugc ) ],
1327 :     d => [ qw( gau gac ) ],
1328 :     e => [ qw( gaa gag ) ],
1329 :     f => [ qw( uuu uuc ) ],
1330 :     h => [ qw( cau cac ) ],
1331 :     k => [ qw( aaa aag ) ],
1332 :     n => [ qw( aau aac ) ],
1333 :     q => [ qw( caa cag ) ],
1334 :     y => [ qw( uau uac ) ],
1335 :     m => [ qw( aug ) ],
1336 :     u => [ qw( uga ) ],
1337 :     w => [ qw( ugg ) ],
1338 : golsen 1.2
1339 : overbeek 1.4 '*' => [ qw( UAA UAG UGA ) ]
1340 : golsen 1.2 );
1341 :    
1342 :    
1343 :     %n_codon_for_aa = map {
1344 :     $_ => scalar @{ $amino_acid_codons_DNA{ $_ } }
1345 :     } keys %amino_acid_codons_DNA;
1346 :    
1347 :    
1348 :     %reverse_genetic_code_DNA = (
1349 : overbeek 1.4 A => "GCN", a => "gcn",
1350 :     C => "TGY", c => "tgy",
1351 :     D => "GAY", d => "gay",
1352 :     E => "GAR", e => "gar",
1353 :     F => "TTY", f => "tty",
1354 :     G => "GGN", g => "ggn",
1355 :     H => "CAY", h => "cay",
1356 :     I => "ATH", i => "ath",
1357 :     K => "AAR", k => "aar",
1358 :     L => "YTN", l => "ytn",
1359 :     M => "ATG", m => "atg",
1360 :     N => "AAY", n => "aay",
1361 :     P => "CCN", p => "ccn",
1362 :     Q => "CAR", q => "car",
1363 :     R => "MGN", r => "mgn",
1364 :     S => "WSN", s => "wsn",
1365 :     T => "ACN", t => "acn",
1366 :     U => "TGA", u => "tga",
1367 :     V => "GTN", v => "gtn",
1368 :     W => "TGG", w => "tgg",
1369 :     X => "NNN", x => "nnn",
1370 :     Y => "TAY", y => "tay",
1371 :     '*' => "TRR"
1372 : golsen 1.2 );
1373 :    
1374 :     %reverse_genetic_code_RNA = (
1375 : overbeek 1.4 A => "GCN", a => "gcn",
1376 :     C => "UGY", c => "ugy",
1377 :     D => "GAY", d => "gay",
1378 :     E => "GAR", e => "gar",
1379 :     F => "UUY", f => "uuy",
1380 :     G => "GGN", g => "ggn",
1381 :     H => "CAY", h => "cay",
1382 :     I => "AUH", i => "auh",
1383 :     K => "AAR", k => "aar",
1384 :     L => "YUN", l => "yun",
1385 :     M => "AUG", m => "aug",
1386 :     N => "AAY", n => "aay",
1387 :     P => "CCN", p => "ccn",
1388 :     Q => "CAR", q => "car",
1389 :     R => "MGN", r => "mgn",
1390 :     S => "WSN", s => "wsn",
1391 :     T => "ACN", t => "acn",
1392 :     U => "UGA", u => "uga",
1393 :     V => "GUN", v => "gun",
1394 :     W => "UGG", w => "ugg",
1395 :     X => "NNN", x => "nnn",
1396 :     Y => "UAY", y => "uay",
1397 :     '*' => "URR"
1398 : golsen 1.2 );
1399 :    
1400 :    
1401 :     %DNA_letter_can_be = (
1402 : efrank 1.1 A => ["A"], a => ["a"],
1403 :     B => ["C", "G", "T"], b => ["c", "g", "t"],
1404 :     C => ["C"], c => ["c"],
1405 :     D => ["A", "G", "T"], d => ["a", "g", "t"],
1406 :     G => ["G"], g => ["g"],
1407 :     H => ["A", "C", "T"], h => ["a", "c", "t"],
1408 :     K => ["G", "T"], k => ["g", "t"],
1409 :     M => ["A", "C"], m => ["a", "c"],
1410 :     N => ["A", "C", "G", "T"], n => ["a", "c", "g", "t"],
1411 :     R => ["A", "G"], r => ["a", "g"],
1412 :     S => ["C", "G"], s => ["c", "g"],
1413 :     T => ["T"], t => ["t"],
1414 :     U => ["T"], u => ["t"],
1415 :     V => ["A", "C", "G"], v => ["a", "c", "g"],
1416 :     W => ["A", "T"], w => ["a", "t"],
1417 :     Y => ["C", "T"], y => ["c", "t"]
1418 :     );
1419 :    
1420 :    
1421 : golsen 1.2 %RNA_letter_can_be = (
1422 : efrank 1.1 A => ["A"], a => ["a"],
1423 :     B => ["C", "G", "U"], b => ["c", "g", "u"],
1424 :     C => ["C"], c => ["c"],
1425 :     D => ["A", "G", "U"], d => ["a", "g", "u"],
1426 :     G => ["G"], g => ["g"],
1427 :     H => ["A", "C", "U"], h => ["a", "c", "u"],
1428 :     K => ["G", "U"], k => ["g", "u"],
1429 :     M => ["A", "C"], m => ["a", "c"],
1430 :     N => ["A", "C", "G", "U"], n => ["a", "c", "g", "u"],
1431 :     R => ["A", "G"], r => ["a", "g"],
1432 :     S => ["C", "G"], s => ["c", "g"],
1433 :     T => ["U"], t => ["u"],
1434 :     U => ["U"], u => ["u"],
1435 :     V => ["A", "C", "G"], v => ["a", "c", "g"],
1436 :     W => ["A", "U"], w => ["a", "u"],
1437 :     Y => ["C", "U"], y => ["c", "u"]
1438 :     );
1439 :    
1440 :    
1441 : overbeek 1.4 %one_letter_to_three_letter_aa = (
1442 :     A => "Ala", a => "Ala",
1443 :     B => "Asx", b => "Asx",
1444 :     C => "Cys", c => "Cys",
1445 :     D => "Asp", d => "Asp",
1446 :     E => "Glu", e => "Glu",
1447 :     F => "Phe", f => "Phe",
1448 :     G => "Gly", g => "Gly",
1449 :     H => "His", h => "His",
1450 :     I => "Ile", i => "Ile",
1451 :     K => "Lys", k => "Lys",
1452 :     L => "Leu", l => "Leu",
1453 :     M => "Met", m => "Met",
1454 :     N => "Asn", n => "Asn",
1455 :     P => "Pro", p => "Pro",
1456 :     Q => "Gln", q => "Gln",
1457 :     R => "Arg", r => "Arg",
1458 :     S => "Ser", s => "Ser",
1459 :     T => "Thr", t => "Thr",
1460 :     U => "Sec", u => "Sec",
1461 :     V => "Val", v => "Val",
1462 :     W => "Trp", w => "Trp",
1463 :     X => "Xxx", x => "Xxx",
1464 :     Y => "Tyr", y => "Tyr",
1465 :     Z => "Glx", z => "Glx",
1466 :     '*' => "***"
1467 :     );
1468 : golsen 1.2
1469 :    
1470 :     %three_letter_to_one_letter_aa = (
1471 :     ALA => "A", Ala => "A", ala => "a",
1472 :     ARG => "R", Arg => "R", arg => "r",
1473 :     ASN => "N", Asn => "N", asn => "n",
1474 :     ASP => "D", Asp => "D", asp => "d",
1475 :     ASX => "B", Asx => "B", asx => "b",
1476 :     CYS => "C", Cys => "C", cys => "c",
1477 :     GLN => "Q", Gln => "Q", gln => "q",
1478 :     GLU => "E", Glu => "E", glu => "e",
1479 :     GLX => "Z", Glx => "Z", glx => "z",
1480 :     GLY => "G", Gly => "G", gly => "g",
1481 :     HIS => "H", His => "H", his => "h",
1482 :     ILE => "I", Ile => "I", ile => "i",
1483 :     LEU => "L", Leu => "L", leu => "l",
1484 :     LYS => "K", Lys => "K", lys => "k",
1485 :     MET => "M", Met => "M", met => "m",
1486 :     PHE => "F", Phe => "F", phe => "f",
1487 :     PRO => "P", Pro => "P", pro => "p",
1488 :     SEC => "U", Sec => "U", sec => "u",
1489 :     SER => "S", Ser => "S", ser => "s",
1490 :     THR => "T", Thr => "T", thr => "t",
1491 :     TRP => "W", Trp => "W", trp => "w",
1492 :     TYR => "Y", Tyr => "Y", tyr => "y",
1493 :     VAL => "V", Val => "V", val => "v",
1494 :     XAA => "X", Xaa => "X", xaa => "x",
1495 :     XXX => "X", Xxx => "X", xxx => "x",
1496 :     '***' => "*"
1497 : efrank 1.1 );
1498 :    
1499 :    
1500 :     #-----------------------------------------------------------------------------
1501 : golsen 1.10 # Translate nucleotides to one letter protein. Respects case of the
1502 :     # nucleotide sequence.
1503 : efrank 1.1 #
1504 : golsen 1.10 # $aa = translate_seq( $nt, $met_start )
1505 :     # $aa = translate_seq( $nt )
1506 : efrank 1.1 #
1507 :     #-----------------------------------------------------------------------------
1508 :    
1509 : golsen 1.10 sub translate_seq
1510 :     {
1511 :     my $seq = shift;
1512 : efrank 1.1 $seq =~ tr/-//d; # remove gaps
1513 :    
1514 : golsen 1.10 my @codons = $seq =~ m/(...?)/g; # Will try to translate last 2 nt
1515 :    
1516 :     # A second argument that is true forces first amino acid to be Met
1517 : efrank 1.1
1518 : golsen 1.10 my @met;
1519 :     if ( ( shift @_ ) && ( my $codon1 = shift @codons ) )
1520 :     {
1521 :     push @met, ( $codon1 =~ /[a-z]/ ? 'm' : 'M' );
1522 : efrank 1.1 }
1523 :    
1524 : golsen 1.10 join( '', @met, map { translate_codon( $_ ) } @codons )
1525 : efrank 1.1 }
1526 :    
1527 :    
1528 :     #-----------------------------------------------------------------------------
1529 : golsen 1.10 # Translate a single triplet with "universal" genetic code.
1530 : efrank 1.1 #
1531 :     # $aa = translate_codon( $triplet )
1532 : golsen 1.10 # $aa = translate_DNA_codon( $triplet )
1533 :     # $aa = translate_uc_DNA_codon( $triplet )
1534 : efrank 1.1 #
1535 :     #-----------------------------------------------------------------------------
1536 :    
1537 : golsen 1.10 sub translate_DNA_codon { translate_codon( @_ ) }
1538 : efrank 1.1
1539 : golsen 1.10 sub translate_uc_DNA_codon { translate_codon( uc $_[0] ) }
1540 : efrank 1.1
1541 : golsen 1.10 sub translate_codon
1542 :     {
1543 :     my $codon = shift;
1544 :     $codon =~ tr/Uu/Tt/; # Make it DNA
1545 :    
1546 :     # Try a simple lookup:
1547 : efrank 1.1
1548 :     my $aa;
1549 : golsen 1.10 if ( $aa = $genetic_code{ $codon } ) { return $aa }
1550 : efrank 1.1
1551 : golsen 1.10 # Attempt to recover from mixed-case codons:
1552 : efrank 1.1
1553 : golsen 1.10 $codon = ( $codon =~ /[a-z]/ ) ? lc $codon : uc $codon;
1554 : efrank 1.1 if ( $aa = $genetic_code{ $codon } ) { return $aa }
1555 :    
1556 : golsen 1.10 # The code defined above catches simple N, R and Y ambiguities in the
1557 :     # third position. Other codons (e.g., GG[KMSWBDHV], or even GG) might
1558 :     # be unambiguously translated by converting the last position to N and
1559 :     # seeing if this is in the code table:
1560 :    
1561 :     my $N = ( $codon =~ /[a-z]/ ) ? 'n' : 'N';
1562 :     if ( $aa = $genetic_code{ substr($codon,0,2) . $N } ) { return $aa }
1563 :    
1564 :     # Test that codon is valid for an unambiguous aa:
1565 :    
1566 :     my $X = ( $codon =~ /[a-z]/ ) ? 'x' : 'X';
1567 :     if ( $codon !~ m/^[ACGTMY][ACGT][ACGTKMRSWYBDHVN]$/i
1568 :     && $codon !~ m/^YT[AGR]$/i # Leu YTR
1569 :     && $codon !~ m/^MG[AGR]$/i # Arg MGR
1570 :     )
1571 :     {
1572 :     return $X;
1573 :     }
1574 : efrank 1.1
1575 :     # Expand all ambiguous nucleotides to see if they all yield same aa.
1576 :    
1577 : golsen 1.10 my @n1 = @{ $DNA_letter_can_be{ substr( $codon, 0, 1 ) } };
1578 :     my $n2 = substr( $codon, 1, 1 );
1579 :     my @n3 = @{ $DNA_letter_can_be{ substr( $codon, 2, 1 ) } };
1580 :     my @triples = map { my $n12 = $_ . $n2; map { $n12 . $_ } @n3 } @n1;
1581 :    
1582 :     my $triple = shift @triples;
1583 :     $aa = $genetic_code{ $triple };
1584 :     $aa or return $X;
1585 :    
1586 :     foreach $triple ( @triples ) { return $X if $aa ne $genetic_code{$triple} }
1587 : efrank 1.1
1588 : golsen 1.10 return $aa;
1589 : efrank 1.1 }
1590 :    
1591 :    
1592 :     #-----------------------------------------------------------------------------
1593 :     # Translate with a user-supplied genetic code to translate a sequence.
1594 :     # Diagnose the use of upper versus lower, and T versus U in the supplied
1595 :     # code, and transform the supplied nucleotide sequence to match.
1596 :     #
1597 : golsen 1.10 # $aa = translate_seq_with_user_code( $nt, \%gen_code )
1598 :     # $aa = translate_seq_with_user_code( $nt, \%gen_code, $start_with_met )
1599 : efrank 1.1 #
1600 : golsen 1.10 # Modified 2007-11-22 to be less intrusive in these diagnoses by sensing
1601 :     # the presence of both versions in the user code.
1602 : efrank 1.1 #-----------------------------------------------------------------------------
1603 :    
1604 : golsen 1.10 sub translate_seq_with_user_code
1605 :     {
1606 : efrank 1.1 my $seq = shift;
1607 :     $seq =~ tr/-//d; # remove gaps *** Why?
1608 :    
1609 : golsen 1.10 my $gc = shift; # Reference to hash of code
1610 :     if (! $gc || ref($gc) ne "HASH")
1611 :     {
1612 :     print STDERR "translate_seq_with_user_code needs genetic code hash as second argument.";
1613 :     return undef;
1614 :     }
1615 :    
1616 :     # Test code support for upper vs lower case:
1617 :    
1618 :     my ( $TTT, $UUU );
1619 :     if ( $gc->{AAA} && ! $gc->{aaa} ) # Uppercase only code table
1620 :     {
1621 :     $seq = uc $seq; # Uppercase sequence
1622 :     ( $TTT, $UUU ) = ( 'TTT', 'UUU' );
1623 :     }
1624 :     elsif ( $gc->{aaa} && ! $gc->{AAA} ) # Lowercase only code table
1625 :     {
1626 :     $seq = lc $seq; # Lowercase sequence
1627 :     ( $TTT, $UUU ) = ( 'ttt', 'uuu' );
1628 :     }
1629 :     elsif ( $gc->{aaa} )
1630 :     {
1631 :     ( $TTT, $UUU ) = ( 'ttt', 'uuu' );
1632 : efrank 1.1 }
1633 : golsen 1.10 else
1634 :     {
1635 :     print STDERR "User-supplied genetic code does not have aaa or AAA\n";
1636 :     return undef;
1637 : efrank 1.1 }
1638 :    
1639 : golsen 1.10 # Test code support for U vs T:
1640 : efrank 1.1
1641 : golsen 1.10 my $ambigs;
1642 :     if ( $gc->{$UUU} && ! $gc->{$TTT} ) # RNA only code table
1643 :     {
1644 :     $seq = tr/Tt/Uu/;
1645 : efrank 1.1 $ambigs = \%RNA_letter_can_be;
1646 :     }
1647 : golsen 1.10 elsif ( $gc->{$TTT} && ! $gc->{$UUU} ) # DNA only code table
1648 :     {
1649 :     $seq = tr/Uu/Tt/;
1650 : efrank 1.1 $ambigs = \%DNA_letter_can_be;
1651 :     }
1652 : golsen 1.10 else
1653 :     {
1654 :     my $t = $seq =~ tr/Tt//;
1655 :     my $u = $seq =~ tr/Uu//;
1656 :     $ambigs = ( $t > $u ) ? \%DNA_letter_can_be : \%RNA_letter_can_be;
1657 : efrank 1.1 }
1658 :    
1659 : golsen 1.10 # We can now do the codon-by-codon translation:
1660 : efrank 1.1
1661 : golsen 1.10 my @codons = $seq =~ m/(...?)/g; # will try to translate last 2 nt
1662 :    
1663 :     # A third argument that is true forces first amino acid to be Met
1664 : efrank 1.1
1665 : golsen 1.10 my @met;
1666 :     if ( ( shift @_ ) && ( my $codon1 = shift @codons ) )
1667 :     {
1668 :     push @met, ( $codon1 =~ /[a-z]/ ? 'm' : 'M' );
1669 : efrank 1.1 }
1670 :    
1671 : golsen 1.10 join( '', @met, map { translate_codon_with_user_code( $_, $gc, $ambigs ) } @codons )
1672 : efrank 1.1 }
1673 :    
1674 :    
1675 :     #-----------------------------------------------------------------------------
1676 : golsen 1.10 # Translate with user-supplied genetic code hash. No error check on the code.
1677 :     # Should only be called through translate_seq_with_user_code.
1678 : efrank 1.1 #
1679 : golsen 1.12 # $aa = translate_codon_with_user_code( $triplet, \%code, \%ambig_table )
1680 : efrank 1.1 #
1681 : golsen 1.12 # $triplet speaks for itself
1682 :     # \%code ref to the hash with the codon translations
1683 :     # \%ambig_table ref to hash with lists of nucleotides for each ambig code
1684 : efrank 1.1 #-----------------------------------------------------------------------------
1685 :    
1686 : golsen 1.10 sub translate_codon_with_user_code
1687 :     {
1688 : golsen 1.12 my ( $codon, $gc, $ambigs ) = @_;
1689 : golsen 1.10
1690 :     # Try a simple lookup:
1691 : efrank 1.1
1692 :     my $aa;
1693 : golsen 1.10 if ( $aa = $gc->{ $codon } ) { return $aa }
1694 : efrank 1.1
1695 : golsen 1.10 # Attempt to recover from mixed-case codons:
1696 : efrank 1.1
1697 : golsen 1.10 $codon = ( $codon =~ /[a-z]/ ) ? lc $codon : uc $codon;
1698 :     if ( $aa = $genetic_code{ $codon } ) { return $aa }
1699 : efrank 1.1
1700 : golsen 1.10 # Test that codon is valid for an unambiguous aa:
1701 : efrank 1.1
1702 : golsen 1.10 my $X = ( $codon =~ /[a-z]/ ) ? 'x' : 'X';
1703 :    
1704 :     if ( $codon =~ m/^[ACGTU][ACGTU]$/i ) # Add N?
1705 :     {
1706 :     $codon .= ( $codon =~ /[a-z]/ ) ? 'n' : 'N';
1707 :     }
1708 : golsen 1.12 # This makes assumptions about the user code, but tranlating ambiguous
1709 :     # codons is really a bit off the wall to start with:
1710 : golsen 1.10 elsif ( $codon !~ m/^[ACGTUMY][ACGTU][ACGTUKMRSWYBDHVN]$/i ) # Valid?
1711 :     {
1712 :     return $X;
1713 :     }
1714 : efrank 1.1
1715 :     # Expand all ambiguous nucleotides to see if they all yield same aa.
1716 :    
1717 : golsen 1.10 my @n1 = @{ $ambigs->{ substr( $codon, 0, 1 ) } };
1718 :     my $n2 = substr( $codon, 1, 1 );
1719 :     my @n3 = @{ $ambigs->{ substr( $codon, 2, 1 ) } };
1720 :     my @triples = map { my $n12 = $_ . $n2; map { $n12 . $_ } @n3 } @n1;
1721 :    
1722 :     my $triple = shift @triples;
1723 :     $aa = $gc->{ $triple } || $gc->{ lc $triple } || $gc->{ uc $triple };
1724 :     $aa or return $X;
1725 :    
1726 :     foreach $triple ( @triples )
1727 :     {
1728 :     return $X if $aa ne ( $gc->{$triple} || $gc->{lc $triple} || $gc->{uc $triple} );
1729 : efrank 1.1 }
1730 :    
1731 : golsen 1.10 return $aa;
1732 : efrank 1.1 }
1733 :    
1734 :    
1735 :     #-----------------------------------------------------------------------------
1736 :     # Read a list of intervals from a file.
1737 :     # Allow id_start_end, or id \s start \s end formats
1738 :     #
1739 : overbeek 1.4 # @intervals = read_intervals( \*FILEHANDLE )
1740 : efrank 1.1 #-----------------------------------------------------------------------------
1741 :     sub read_intervals {
1742 :     my $fh = shift;
1743 :     my @intervals = ();
1744 :    
1745 :     while (<$fh>) {
1746 :     chomp;
1747 :     /^(\S+)_(\d+)_(\d+)(\s.*)?$/ # id_start_end WIT2
1748 :     || /^(\S+)_(\d+)-(\d+)(\s.*)?$/ # id_start-end ???
1749 :     || /^(\S+)=(\d+)=(\d+)(\s.*)?$/ # id=start=end Badger
1750 :     || /^(\S+)\s+(\d+)\s+(\d+)(\s.*)?$/ # id \s start \s end
1751 :     || next;
1752 :    
1753 :     # Matched a pattern. Store reference to (id, left, right):
1754 :     push @intervals, ($2 < $3) ? [ $1, $2+0, $3+0 ]
1755 :     : [ $1, $3+0, $2+0 ];
1756 :     }
1757 :     return @intervals;
1758 :     }
1759 :    
1760 :    
1761 :     #-----------------------------------------------------------------------------
1762 : golsen 1.2 # Convert a list of intervals to read [ id, left_end, right_end ].
1763 :     #
1764 :     # @intervals = standardize_intervals( @interval_refs )
1765 :     #-----------------------------------------------------------------------------
1766 :     sub standardize_intervals {
1767 :     map { ( $_->[1] < $_->[2] ) ? $_ : [ $_->[0], $_->[2], $_->[1] ] } @_;
1768 :     }
1769 :    
1770 :    
1771 :     #-----------------------------------------------------------------------------
1772 : efrank 1.1 # Take the union of a list of intervals
1773 :     #
1774 :     # @joined = join_intervals( @interval_refs )
1775 :     #-----------------------------------------------------------------------------
1776 :     sub join_intervals {
1777 :     my @ordered = sort { $a->[0] cmp $b->[0] # first by id
1778 :     || $a->[1] <=> $b->[1] # next by left end
1779 :     || $b->[2] <=> $a->[2] # finally longest first
1780 :     } @_;
1781 :    
1782 :     my @joined = ();
1783 :     my $n_int = @ordered;
1784 :    
1785 :     my ($cur_id) = "";
1786 :     my ($cur_left) = -1;
1787 :     my ($cur_right) = -1;
1788 :     my ($new_id, $new_left, $new_right);
1789 :    
1790 :     for (my $i = 0; $i < $n_int; $i++) {
1791 :     ($new_id, $new_left, $new_right) = @{$ordered[$i]}; # get the new data
1792 :    
1793 :     if ( ( $new_id ne $cur_id) # if new contig
1794 :     || ( $new_left > $cur_right + 1) # or not touching previous
1795 :     ) { # push the previous interval
1796 :     if ($cur_id) { push (@joined, [ $cur_id, $cur_left, $cur_right ]) }
1797 :     $cur_id = $new_id; # update the current interval
1798 :     $cur_left = $new_left;
1799 :     $cur_right = $new_right;
1800 :     }
1801 :    
1802 :     elsif ($new_right > $cur_right) { # extend the right end if necessary
1803 :     $cur_right = $new_right;
1804 :     }
1805 :     }
1806 :    
1807 :     if ($cur_id) { push (@joined, [$cur_id, $cur_left, $cur_right]) }
1808 :     return @joined;
1809 :     }
1810 :    
1811 : golsen 1.2
1812 :     #-----------------------------------------------------------------------------
1813 :     # Split location strings to oriented intervals.
1814 :     #
1815 :     # @intervals = locations_2_intervals( @locations )
1816 :     # $interval = locations_2_intervals( $location )
1817 :     #-----------------------------------------------------------------------------
1818 :     sub locations_2_intervals {
1819 :     my @intervals = map { /^(\S+)_(\d+)_(\d+)(\s.*)?$/
1820 :     || /^(\S+)_(\d+)-(\d+)(\s.*)?$/
1821 :     || /^(\S+)=(\d+)=(\d+)(\s.*)?$/
1822 :     || /^(\S+)\s+(\d+)\s+(\d+)(\s.*)?$/
1823 :     ? [ $1, $2+0, $3+0 ]
1824 :     : ()
1825 :     } @_;
1826 :    
1827 :     return wantarray ? @intervals : $intervals[0];
1828 :     }
1829 :    
1830 :    
1831 :     #-----------------------------------------------------------------------------
1832 :     # Read a list of oriented intervals from a file.
1833 :     # Allow id_start_end, or id \s start \s end formats
1834 :     #
1835 : overbeek 1.4 # @intervals = read_oriented_intervals( \*FILEHANDLE )
1836 : golsen 1.2 #-----------------------------------------------------------------------------
1837 :     sub read_oriented_intervals {
1838 :     my $fh = shift;
1839 :     my @intervals = ();
1840 :    
1841 :     while (<$fh>) {
1842 :     chomp;
1843 :     /^(\S+)_(\d+)_(\d+)(\s.*)?$/ # id_start_end WIT2
1844 :     || /^(\S+)_(\d+)-(\d+)(\s.*)?$/ # id_start-end ???
1845 :     || /^(\S+)=(\d+)=(\d+)(\s.*)?$/ # id=start=end Badger
1846 :     || /^(\S+)\s+(\d+)\s+(\d+)(\s.*)?$/ # id \s start \s end
1847 :     || next;
1848 :    
1849 :     # Matched a pattern. Store reference to (id, start, end):
1850 :     push @intervals, [ $1, $2+0, $3+0 ];
1851 :     }
1852 :     return @intervals;
1853 :     }
1854 :    
1855 :    
1856 :     #-----------------------------------------------------------------------------
1857 :     # Reverse the orientation of a list of intervals
1858 :     #
1859 :     # @reversed = reverse_intervals( @interval_refs )
1860 :     #-----------------------------------------------------------------------------
1861 :     sub reverse_intervals {
1862 :     map { [ $_->[0], $_->[2], $_->[1] ] } @_;
1863 :     }
1864 :    
1865 :    
1866 : overbeek 1.4 #-----------------------------------------------------------------------------
1867 :     # Convert GenBank locations to SEED locations
1868 :     #
1869 :     # @seed_locs = gb_location_2_seed( $contig, @gb_locs )
1870 :     #-----------------------------------------------------------------------------
1871 :     sub gb_location_2_seed
1872 :     {
1873 :     my $contig = shift @_;
1874 :     $contig or die "First arg of gb_location_2_seed must be contig_id\n";
1875 :    
1876 :     map { join( ',', gb_loc_2_seed_2( $contig, $_ ) ) || undef } @_
1877 :     }
1878 :    
1879 :     sub gb_loc_2_seed_2
1880 :     {
1881 :     my ( $contig, $loc ) = @_;
1882 :    
1883 :     if ( $loc =~ /^(\d+)\.\.(\d+)$/ )
1884 :     {
1885 :     join( '_', $contig, $1, $2 )
1886 :     }
1887 :    
1888 :     elsif ( $loc =~ /^join\((.*)\)$/ )
1889 :     {
1890 :     $loc = $1;
1891 :     my $lvl = 0;
1892 :     for ( my $i = length( $loc )-1; $i >= 0; $i-- )
1893 :     {
1894 :     for ( substr( $loc, $i, 1 ) )
1895 :     {
1896 :     /,/ && ! $lvl and substr( $loc, $i, 1 ) = "\t";
1897 :     /\(/ and $lvl--;
1898 :     /\)/ and $lvl++;
1899 :     }
1900 :     }
1901 :     $lvl == 0 or print STDERR "Paren matching error: $loc\n" and die;
1902 :     map { gb_loc_2_seed_2( $contig, $_ ) } split /\t/, $loc
1903 :     }
1904 :    
1905 :     elsif ( $loc =~ /^complement\((.*)\)$/ )
1906 :     {
1907 :     map { s/_(\d+)_(\d+)$/_$2_$1/; $_ }
1908 :     reverse
1909 :     gb_loc_2_seed_2( $contig, $1 )
1910 :     }
1911 :    
1912 :     else
1913 :     {
1914 :     ()
1915 :     }
1916 :     }
1917 :    
1918 :    
1919 : golsen 1.7 #-----------------------------------------------------------------------------
1920 :     # Read qual.
1921 :     #
1922 :     # Save the contents in a list of refs to arrays: [ $id, $descript, \@qual ]
1923 :     #
1924 :     # @seq_entries = read_qual( ) # STDIN
1925 :     # \@seq_entries = read_qual( ) # STDIN
1926 :     # @seq_entries = read_qual( \*FILEHANDLE )
1927 :     # \@seq_entries = read_qual( \*FILEHANDLE )
1928 :     # @seq_entries = read_qual( $filename )
1929 :     # \@seq_entries = read_qual( $filename )
1930 :     #-----------------------------------------------------------------------------
1931 :     sub read_qual {
1932 :     my ( $fh, $name, $close, $unused ) = input_filehandle( $_[0] );
1933 :     $unused && die "Bad reference type (" . ref( $unused ) . ") passed to read_qual\n";
1934 :    
1935 :     my @quals = ();
1936 :     my ($id, $desc, $qual) = ("", "", []);
1937 :    
1938 :     while ( <$fh> ) {
1939 :     chomp;
1940 :     if (/^>\s*(\S+)(\s+(.*))?$/) { # new id
1941 :     if ($id && @$qual) { push @quals, [ $id, $desc, $qual ] }
1942 :     ($id, $desc, $qual) = ($1, $3 ? $3 : "", []);
1943 :     }
1944 :     else {
1945 :     push @$qual, split;
1946 :     }
1947 :     }
1948 :     close( $fh ) if $close;
1949 :    
1950 :     if ($id && @$qual) { push @quals, [ $id, $desc, $qual ] }
1951 :     return wantarray ? @quals : \@quals;
1952 :     }
1953 :    
1954 :    
1955 : golsen 1.11 #-------------------------------------------------------------------------------
1956 :     # Fraction difference for an alignment of two nucleotide sequences in terms of
1957 :     # number of differing residues, number of gaps, and number of gap opennings.
1958 :     #
1959 :     # $fraction_diff = fraction_nt_diff( $seq1, $seq2, \%options )
1960 :     #
1961 :     # or
1962 :     #
1963 :     # $fraction_diff = fraction_nt_diff( $seq1, $seq2 )
1964 :     # $fraction_diff = fraction_nt_diff( $seq1, $seq2, $gap_wgt )
1965 :     # $fraction_diff = fraction_nt_diff( $seq1, $seq2, $open_wgt, $extend_wgt )
1966 :     #
1967 :     # Options:
1968 :     #
1969 :     # gap => $gap_wgt # Gap open and extend weight (D = 0.5)
1970 :     # open => $open_wgt # Gap openning weight (D = gap_wgt)
1971 :     # extend => $extend_wgt # Gap extension weight (D = open_wgt)
1972 :     # t_gap => $term_gap_wgt # Terminal open and extend weight
1973 :     # t_open => $term_open_wgt # Terminal gap open weight (D = open_wgt)
1974 :     # t_extend => $term_extend_wgt # Terminal gap extend weight (D = extend_wgt)
1975 :     #
1976 :     # Default gap open and gap extend weights are 1/2. Beware that
1977 :     #
1978 :     # $fraction_diff = fraction_nt_diff( $seq1, $seq2, 1 )
1979 :     #
1980 :     # and
1981 :     #
1982 :     # $fraction_diff = fraction_nt_diff( $seq1, $seq2, 1, 0 )
1983 :     #
1984 :     # are different. The first has equal openning and extension weights, whereas
1985 :     # the second has an openning weight of 1, and and extension weight of 0 (it
1986 :     # only penalizes the number of runs of gaps).
1987 :     #-------------------------------------------------------------------------------
1988 :     sub fraction_nt_diff
1989 :     {
1990 :     my ( $npos, $nid, $ndif, $ngap, $nopen, $tgap, $topen ) = interpret_nt_align( @_[0,1] );
1991 :    
1992 :     my $diff_scr;
1993 :     if ( ref( $_[2] ) eq 'HASH' )
1994 :     {
1995 :     my $opts = $_[2];
1996 :     my $gap_open = defined $opts->{ open } ? $opts->{ open }
1997 :     : defined $opts->{ gap } ? $opts->{ gap }
1998 :     : 0.5;
1999 :     my $gap_extend = defined $opts->{ extend } ? $opts->{ extend }
2000 :     : $gap_open;
2001 :     my $term_open = defined $opts->{ t_open } ? $opts->{ t_open }
2002 :     : defined $opts->{ t_gap } ? $opts->{ t_gap }
2003 :     : $gap_open;
2004 :     my $term_extend = defined $opts->{ t_extend } ? $opts->{ t_extend }
2005 :     : defined $opts->{ t_gap } ? $opts->{ t_gap }
2006 :     : $gap_extend;
2007 :    
2008 :     $nopen -= $topen;
2009 :     $ngap -= $tgap;
2010 :     $diff_scr = $ndif + $gap_open * $nopen + $gap_extend * ($ngap-$nopen)
2011 :     + $term_open * $topen + $term_extend * ($tgap-$topen);
2012 :     }
2013 :     else
2014 :     {
2015 :     my $gap_open = defined( $_[2] ) ? $_[2] : 0.5;
2016 :     my $gap_extend = defined( $_[3] ) ? $_[3] : $gap_open;
2017 :     $diff_scr = $ndif + $gap_open * $nopen + $gap_extend * ($ngap-$nopen);
2018 :     }
2019 :     my $ttl_scr = $nid + $diff_scr;
2020 :    
2021 :     $ttl_scr ? $diff_scr / $ttl_scr : undef
2022 :     }
2023 :    
2024 :    
2025 :     #-------------------------------------------------------------------------------
2026 :     # Interpret an alignment of two nucleotide sequences in terms of: useful
2027 :     # aligned positions (unambiguous, and not a common gap), number of identical
2028 :     # residues, number of differing residues, number of gaps, and number of gap
2029 :     # opennings.
2030 :     #
2031 :     # ( $npos, $nid, $ndif, $ngap, $nopen, $tgap, $topen ) = interpret_nt_align( $seq1, $seq2 )
2032 :     #
2033 :     # $npos = total aligned positons (= $nid + $ndif + $ngap)
2034 :     # $nid = number of positions with identical nucleotides (ignoring case)
2035 :     # $ndif = number of positions with differing nucleotides
2036 :     # $ngap = number of positions with gap in one sequence but not the other
2037 :     # $nopen = number of runs of gaps
2038 :     # $tgap = number of gaps in runs adjacent to a terminus
2039 :     # $topen = number of alignment ends with gaps
2040 :     #
2041 :     # Some of the methods might seem overly complex, but are necessary for cases
2042 :     # in which the gaps switch strands in the alignment:
2043 :     #
2044 :     # seq1 ---ACGTGAC--TTGCAGAG
2045 :     # seq2 TTT---TGACGG--GCAGGG
2046 :     # mask 00000011110000111111
2047 :     #
2048 :     # npos = 20
2049 :     # nid = 9
2050 :     # ndif = 1
2051 :     # ngap = 10
2052 :     # nopen = 4
2053 :     # tgap = 3
2054 :     # topen = 1
2055 :     #
2056 :     # Although there are 4 gap opennings, there are only 2 runs in the mask,
2057 :     # and the terminal run is length 6, not 3. (Why handle these? Because
2058 :     # pairs of sequences from a multiple sequence alignment can look like this.)
2059 :     #-------------------------------------------------------------------------------
2060 :     sub interpret_nt_align
2061 :     {
2062 :     # Remove alignment columns that are not informative:
2063 :     my ( $s1, $s2 ) = useful_nt_align( @_[0,1] );
2064 :     my $nmat = length( $s1 ); # Useful alignment length
2065 :    
2066 :     my $m1 = $s1;
2067 :     $m1 =~ tr/ACGT/\377/; # Nucleotides to all 1 bits
2068 :     $m1 =~ tr/\377/\000/c; # Others (gaps) to null byte
2069 :     my $m2 = $s2;
2070 :     $m2 =~ tr/ACGT/\377/; # Nucleotides to all 1 bits
2071 :     $m2 =~ tr/\377/\000/c; # Others (gaps) to null byte
2072 :     $m1 &= $m2; # Gap in either sequence becomes null
2073 :     $s1 &= $m1; # Apply mask to sequence 1
2074 :     $s2 &= $m1; # Apply mask to sequence 2
2075 :     my $nopen = @{[ $s1 =~ /\000+/g ]} # Gap opens in sequence 1
2076 :     + @{[ $s2 =~ /\000+/g ]}; # Gap opens in sequence 2
2077 :     my ( $tgap, $topen ) = ( 0, 0 );
2078 :     if ( $s1 =~ /^(\000+)/ || $s2 =~ /^(\000+)/ ) { $tgap += length( $1 ); $topen++ }
2079 :     if ( $s1 =~ /(\000+)$/ || $s2 =~ /(\000+)$/ ) { $tgap += length( $1 ); $topen++ }
2080 :     $s1 =~ tr/\000//d; # Remove nulls (former gaps)
2081 :     $s2 =~ tr/\000//d; # Remove nulls (former gaps)
2082 :     my $ngap = $nmat - length( $s1 ); # Total gaps
2083 :    
2084 :     my $xor = $s1 ^ $s2; # xor of identical residues is null byte
2085 :     my $nid = ( $xor =~ tr/\000//d ); # Count the nulls (identical residues)
2086 :     my $ndif = $nmat - $nid - $ngap;
2087 :    
2088 :     ( $nmat, $nid, $ndif, $ngap, $nopen, $tgap, $topen )
2089 :     }
2090 :    
2091 :    
2092 :     sub useful_nt_align
2093 :     {
2094 :     my ( $s1, $s2 ) = map { uc $_ } @_;
2095 :     $s1 =~ tr/U/T/; # Convert U to T
2096 :     my $m1 = $s1;
2097 :     $m1 =~ tr/ACGT-/\377/; # Allowed symbols to hex FF byte
2098 :     $m1 =~ tr/\377/\000/c; # All else to null byte
2099 :     $s2 =~ tr/U/T/; # Convert U to T
2100 :     my $m2 = $s2;
2101 :     $m2 =~ tr/ACGT-/\377/; # Allowed symbols to hex FF byte
2102 :     $m2 =~ tr/\377/\000/c; # All else to null byte
2103 :     $m1 &= $m2; # Invalid in either sequence becomes null
2104 :     $s1 &= $m1; # Apply mask to sequence 1
2105 :     $s1 =~ tr/\000//d; # Delete nulls in sequence 1
2106 :     $s2 &= $m1; # Apply mask to sequence 2
2107 :     $s2 =~ tr/\000//d; # Delete nulls in sequence 2
2108 :     ( $s1, $s2 )
2109 :     }
2110 :    
2111 :    
2112 : efrank 1.1 1;

MCS Webmaster
ViewVC Help
Powered by ViewVC 1.0.3