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

Diff of /FigKernelPackages/gjoseqlib.pm

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 1.1, Mon Dec 1 16:54:26 2003 UTC revision 1.6, Sun Jun 10 17:24:38 2007 UTC
# Line 1  Line 1 
1  package gjoseqlib;  package gjoseqlib;
2    
3  #  read_fasta_seqs( *FILEHANDLE )  #  A sequence entry is ( $id, $def, $seq )
4  #  read_next_fasta_seq( *FILEHANDLE )  #  A list of entries is a list of references
5  #  parse_fasta_title( $title )  #
6  #  split_fasta_title( $title )  #  @seq_entry   = read_next_fasta_seq( \*FILEHANDLE )
7  #  print_seq_list_as_fasta( *FILEHANDLE, @seq_entry_list )  #  @seq_entries = read_fasta_seqs( \*FILEHANDLE )   # Original form
8  #  print_seq_as_fasta( *FILEHANDLE, $id, $desc, $seq )  #  @seq_entries = read_fasta( )                     # STDIN
9  #  print_gb_locus( *FILEHANDLE, $locus, $def, $accession, $seq )  #  @seq_entries = read_fasta( \*FILEHANDLE )
10    #  @seq_entries = read_fasta(  $filename )
11  #  index_seq_list( @seq_entry_list )  #  @seq_entries = read_clustal( )                   # STDIN
12  #  seq_entry_by_id( \%seq_index, $seq_id )  #  @seq_entries = read_clustal( \*FILEHANDLE )
13  #  seq_desc_by_id( \%seq_index, $seq_id )  #  @seq_entries = read_clustal(  $filename )
14  #  seq_data_by_id( \%seq_index, $seq_id )  #  @seq_entries = read_clustal_file(  $filename )
15    #
16  #  subseq_DNA_entry( @seq_entry, $from, $to [, $fix_id] )  #  $seq_ind   = index_seq_list( @seq_entries );   # hash from ids to entries
17  #  subseq_RNA_entry( @seq_entry, $from, $to [, $fix_id] )  #  @seq_entry = seq_entry_by_id( \%seq_index, $seq_id );
18  #  complement_DNA_entry( @seq_entry [, $fix_id] )  #  $seq_desc  = seq_desc_by_id(  \%seq_index, $seq_id );
19  #  complement_RNA_entry( @seq_entry [, $fix_id] )  #  $seq       = seq_data_by_id(  \%seq_index, $seq_id );
20  #  complement_DNA_seq( $NA_seq )  #
21  #  complement_RNA_seq( $NA_seq )  #  ( $id, $def ) = parse_fasta_title( $title )
22  #  to_DNA_seq( $NA_seq )  #  ( $id, $def ) = split_fasta_title( $title )
23  #  to_RNA_seq( $NA_seq )  #
24  #  clean_ae_sequence( $seq )  #  print_seq_list_as_fasta( \*FILEHANDLE, @seq_entry_list );  # Original form
25    #  print_alignment_as_fasta(                @seq_entry_list ); # STDOUT
26  #  translate_seq( $seq [, $met_start] )  #  print_alignment_as_fasta(               \@seq_entry_list ); # STDOUT
27  #  translate_codon( $triplet )  #  print_alignment_as_fasta( \*FILEHANDLE,  @seq_entry_list );
28  #  translate_seq_with_user_code( $seq, \%gen_code [, $met_start] )  #  print_alignment_as_fasta( \*FILEHANDLE, \@seq_entry_list );
29    #  print_alignment_as_fasta(  $filename,    @seq_entry_list );
30    #  print_alignment_as_fasta(  $filename,   \@seq_entry_list );
31    #  print_alignment_as_phylip(                @seq_entry_list ); # STDOUT
32    #  print_alignment_as_phylip(               \@seq_entry_list ); # STDOUT
33    #  print_alignment_as_phylip( \*FILEHANDLE,  @seq_entry_list );
34    #  print_alignment_as_phylip( \*FILEHANDLE, \@seq_entry_list );
35    #  print_alignment_as_phylip(  $filename,    @seq_entry_list );
36    #  print_alignment_as_phylip(  $filename,   \@seq_entry_list );
37    #  print_alignment_as_nexus(               [ \%label_hash, ]  @seq_entry_list );
38    #  print_alignment_as_nexus(               [ \%label_hash, ] \@seq_entry_list );
39    #  print_alignment_as_nexus( \*FILEHANDLE, [ \%label_hash, ]  @seq_entry_list );
40    #  print_alignment_as_nexus( \*FILEHANDLE, [ \%label_hash, ] \@seq_entry_list );
41    #  print_alignment_as_nexus(  $filename,   [ \%label_hash, ]  @seq_entry_list );
42    #  print_alignment_as_nexus(  $filename,   [ \%label_hash, ] \@seq_entry_list );
43    #  print_seq_as_fasta( \*FILEHANDLE, $id, $desc, $seq) ;
44    #  print_seq_as_fasta( \*FILEHANDLE, @seq_entry );
45    #  print_gb_locus( \*FILEHANDLE, $locus, $def, $accession, $seq );
46    #
47    #   @packed_seqs = pack_alignment(  @seqs )
48    #   @packed_seqs = pack_alignment( \@seqs )
49    #  \@packed_seqs = pack_alignment(  @seqs )
50    #  \@packed_seqs = pack_alignment( \@seqs )
51    #
52    #  @entry  = subseq_DNA_entry( @seq_entry, $from, $to [, $fix_id] );
53    #  @entry  = subseq_RNA_entry( @seq_entry, $from, $to [, $fix_id] );
54    #  @entry  = complement_DNA_entry( @seq_entry [, $fix_id] );
55    #  @entry  = complement_RNA_entry( @seq_entry [, $fix_id] );
56    #  $DNAseq = complement_DNA_seq( $NA_seq );
57    #  $RNAseq = complement_RNA_seq( $NA_seq );
58    #  $DNAseq = to_DNA_seq( $NA_seq );
59    #  $RNAseq = to_RNA_seq( $NA_seq );
60    #  $seq    = pack_seq( $sequence )
61    #  $seq    = clean_ae_sequence( $seq )
62    #
63    #  $seq = translate_seq( $seq [, $met_start] )
64    #  $aa  = translate_codon( $triplet );
65    #  $aa  = translate_uc_DNA_codon( $upcase_DNA_triplet );
66    #
67    #  User-supplied genetic code must be upper case index and match the
68    #  DNA versus RNA type of sequence
69    #
70    #  Locations (= oriented intervals) are ( id, start, end )
71    #  Intervals are ( id, left, right )
72    #
73    #  @intervals = read_intervals( \*FILEHANDLE )
74    #  @intervals = read_oriented_intervals( \*FILEHANDLE )
75    #  @intervals = standardize_intervals( @interval_refs ) # (id, left, right)
76    #  @joined    = join_intervals( @interval_refs )
77    #  @intervals = locations_2_intervals( @locations )
78    #  $interval  = locations_2_intervals( $location  )
79    #  @reversed  = reverse_intervals( @interval_refs )      # (id, end, start)
80    #
81    #  Convert GenBank locations to SEED locations
82    #
83    #  @seed_locs = gb_location_2_seed( $contig, @gb_locs )
84    
85    
86  #  read_intervals( *FILEHANDLE )  use strict;
 #  join_intervals( @interval_refs )  
87    
88  use gjolib qw(wrap_text);  use gjolib qw(wrap_text);
89    
90    #  Exported global variables:
91    
92    our @aa_1_letter_order;  # Alpha by 1 letter
93    our @aa_3_letter_order;  # PAM matrix order
94    our @aa_n_codon_order;
95    our %genetic_code;
96    our %genetic_code_with_U;
97    our %amino_acid_codons_DNA;
98    our %amino_acid_codons_RNA;
99    our %n_codon_for_aa;
100    our %reverse_genetic_code_DNA;
101    our %reverse_genetic_code_RNA;
102    our %DNA_letter_can_be;
103    our %RNA_letter_can_be;
104    our %one_letter_to_three_letter_aa;
105    our %three_letter_to_one_letter_aa;
106    
107  require Exporter;  require Exporter;
108    
109  our @ISA = qw(Exporter);  our @ISA = qw(Exporter);
110  our @EXPORT = qw(  our @EXPORT = qw(
111          read_fasta_seqs          read_fasta_seqs
112            read_fasta
113          read_next_fasta_seq          read_next_fasta_seq
114            read_clustal_file
115            read_clustal
116          parse_fasta_title          parse_fasta_title
117          split_fasta_title          split_fasta_title
118          print_seq_list_as_fasta          print_seq_list_as_fasta
119            print_alignment_as_fasta
120            print_alignment_as_phylip
121            print_alignment_as_nexus
122          print_seq_as_fasta          print_seq_as_fasta
123          print_gb_locus          print_gb_locus
124    
# Line 49  Line 127 
127          seq_desc_by_id          seq_desc_by_id
128          seq_data_by_id          seq_data_by_id
129    
130            pack_alignment
131    
132          subseq_DNA_entry          subseq_DNA_entry
133          subseq_RNA_entry          subseq_RNA_entry
134          complement_DNA_entry          complement_DNA_entry
# Line 57  Line 137 
137          complement_RNA_seq          complement_RNA_seq
138          to_DNA_seq          to_DNA_seq
139          to_RNA_seq          to_RNA_seq
140            pack_seq
141          clean_ae_sequence          clean_ae_sequence
142    
143          translate_seq          translate_seq
# Line 64  Line 145 
145          translate_seq_with_user_code          translate_seq_with_user_code
146    
147          read_intervals          read_intervals
148            standardize_intervals
149          join_intervals          join_intervals
150            locations_2_intervals
151            read_oriented_intervals
152            reverse_intervals
153    
154            gb_location_2_seed
155          );          );
156    
157  use strict;  our @EXPORT_OK = qw(
158            @aa_1_letter_order
159            @aa_3_letter_order
160            @aa_n_codon_order
161            %genetic_code
162            %genetic_code_with_U
163            %amino_acid_codons_DNA
164            %amino_acid_codons_RNA
165            %n_codon_for_aa
166            %reverse_genetic_code_DNA
167            %reverse_genetic_code_RNA
168            %DNA_letter_can_be
169            %RNA_letter_can_be
170            %one_letter_to_three_letter_aa
171            %three_letter_to_one_letter_aa
172            );
173    
174    
175    #-----------------------------------------------------------------------------
176    #  Helper function for defining an input filehandle:
177    #     filehandle is passed through
178    #     string is taken as file name to be openend
179    #     undef or "" defaults to STDOUT
180    #
181    #    ( \*FH, $name, $close [, $file] ) = input_filehandle( $file );
182    #
183    #-----------------------------------------------------------------------------
184    sub input_filehandle
185    {
186        my $file = shift;
187    
188        #  FILEHANDLE
189    
190        return ( $file, $file, 0 ) if ( ref( $file ) eq "GLOB" );
191    
192        #  Null string or undef
193    
194        return ( \*STDIN, "", 0 ) if ( ! defined( $file ) || ( $file eq "" ) );
195    
196        #  File name
197    
198        if ( ! ref( $file ) )
199        {
200            my $fh;
201            -f $file or die "Could not find input file \"$file\"\n";
202            open( $fh, "<$file" ) || die "Could not open \"$file\" for input\n";
203            return ( $fh, $file, 1 );
204        }
205    
206        #  Some other kind of reference; return the unused value
207    
208        return ( \*STDIN, undef, 0, $file );
209    }
210    
211    
212  #-----------------------------------------------------------------------------  #-----------------------------------------------------------------------------
213  #  Read fasta sequences from a file.  #  Read fasta sequences from a filehandle (legacy interface; use read_fasta)
214  #  Save the contents in a list of refs to arrays:  (id, description, seq)  #  Save the contents in a list of refs to arrays:  (id, description, seq)
215  #  #
216  #     @seqs = read_fasta_seqs(*FILEHANDLE)  #     @seq_entries = read_fasta_seqs( \*FILEHANDLE )
217  #-----------------------------------------------------------------------------  #-----------------------------------------------------------------------------
218  sub read_fasta_seqs {  sub read_fasta_seqs { read_fasta( @_ ) }
219      my $fh = shift;  
220      wantarray || die "read_fasta_seqs requires list context\n";  
221    #-----------------------------------------------------------------------------
222    #  Read fasta sequences.
223    #  Save the contents in a list of refs to arrays:  (id, description, seq)
224    #
225    #     @seq_entries = read_fasta( )               #  STDIN
226    #    \@seq_entries = read_fasta( )               #  STDIN
227    #     @seq_entries = read_fasta( \*FILEHANDLE )
228    #    \@seq_entries = read_fasta( \*FILEHANDLE )
229    #     @seq_entries = read_fasta(  $filename )
230    #    \@seq_entries = read_fasta(  $filename )
231    #-----------------------------------------------------------------------------
232    sub read_fasta {
233        my ( $fh, $name, $close, $unused ) = input_filehandle( $_[0] );
234        $unused && die "Bad reference type (" . ref( $unused ) . ") passed to read_fasta\n";
235    
236      my @seqs = ();      my @seqs = ();
237      my ($id, $desc, $seq) = ("", "", "");      my ($id, $desc, $seq) = ("", "", "");
# Line 90  Line 243 
243              ($id, $desc, $seq) = ($1, $3 ? $3 : "", "");              ($id, $desc, $seq) = ($1, $3 ? $3 : "", "");
244          }          }
245          else {          else {
246              tr/\t 0-9//d;              tr/     0-9//d;
247              $seq .= $_ ;              $seq .= $_ ;
248          }          }
249      }      }
250        close( $fh ) if $close;
251    
252      if ($id && $seq) { push @seqs, [ $id, $desc, $seq ] }      if ($id && $seq) { push @seqs, [ $id, $desc, $seq ] }
253      return @seqs;      return wantarray ? @seqs : \@seqs;
254  }  }
255    
256    
# Line 104  Line 258 
258  #  Read one fasta sequence at a time from a file.  #  Read one fasta sequence at a time from a file.
259  #  Return the contents as an array:  (id, description, seq)  #  Return the contents as an array:  (id, description, seq)
260  #  #
261  #     @seq_entry = read_next_fasta_seq(*FILEHANDLE)  #     @seq_entry = read_next_fasta_seq( \*FILEHANDLE )
262  #-----------------------------------------------------------------------------  #-----------------------------------------------------------------------------
263  #  Reading always overshoots, so save next id and description  #  Reading always overshoots, so save next id and description
264  #  (these should really be hashes indexed by file handle):  
265  #  {   #  Use bare block to scope the header hash
266  my $next_fasta_seq_header = "";  
267        my %next_header;
268    
269  sub read_next_fasta_seq {  sub read_next_fasta_seq {
270      my $fh = shift;      my $fh = shift;
271      wantarray || die "read_next_fasta_seq requires list context\n";          my ( $id, $desc );
272    
273      my ( $id, $desc ) = parse_fasta_title( $next_fasta_seq_header );          if ( defined( $next_header{$fh} ) ) {
274                ( $id, $desc ) = parse_fasta_title( $next_header{$fh} );
275            }
276            else {
277                $next_header{$fh} = "";
278                ( $id, $desc ) = ( undef, "" );
279            }
280      my $seq = "";      my $seq = "";
281    
282      while ( <$fh> ) {      while ( <$fh> ) {
283          chomp;          chomp;
284          if ( /^>/ ) {        #  new id          if ( /^>/ ) {        #  new id
285              $next_fasta_seq_header = $_;                  $next_header{$fh} = $_;
286              if ( $id && $seq ) { return ($id, $desc, $seq) }                  if ( defined($id) && $seq )
287              ( $id, $desc ) = parse_fasta_title( $next_fasta_seq_header );                  {
288                        return wantarray ? ($id, $desc, $seq) : [$id, $desc, $seq]
289                    }
290                    ( $id, $desc ) = parse_fasta_title( $next_header{$fh} );
291              $seq = "";              $seq = "";
292          }          }
293          else {          else {
294              tr/\t 0-9//d;                  tr/     0-9//d;
295              $seq .= $_ ;              $seq .= $_ ;
296          }          }
297      }      }
298    
299      #  Done with file, clear "next header"          #  Done with file, delete "next header"
300    
301      $next_fasta_seq_header = "";          delete $next_header{$fh};
302      return ($id && $seq) ? ($id, $desc, $seq) : () ;          return ( defined($id) && $seq ) ? ( wantarray ? ($id, $desc, $seq)
303                                                          : [$id, $desc, $seq]
304                                              )
305                                            : () ;
306        }
307    }
308    
309    
310    #-----------------------------------------------------------------------------
311    #  Read a clustal alignment from a file.
312    #  Save the contents in a list of refs to arrays:  (id, description, seq)
313    #
314    #     @seq_entries = read_clustal_file( $filename )
315    #-----------------------------------------------------------------------------
316    sub read_clustal_file { read_clustal( @_ ) }
317    
318    
319    #-----------------------------------------------------------------------------
320    #  Read a clustal alignment.
321    #  Save the contents in a list of refs to arrays:  (id, description, seq)
322    #
323    #     @seq_entries = read_clustal( )              # STDIN
324    #     @seq_entries = read_clustal( \*FILEHANDLE )
325    #     @seq_entries = read_clustal(  $filename )
326    #-----------------------------------------------------------------------------
327    sub read_clustal {
328        my ( $fh, undef, $close, $unused ) = input_filehandle( shift );
329        $unused && die "Bad reference type (" . ref( $unused ) . ") passed to read_clustal_file\n";
330    
331        my ( %seq, @ids, $line );
332        while ( defined( $line = <$fh> ) )
333        {
334            ( $line =~ /^[A-Za-z0-9]/ ) or next;
335            chomp $line;
336            my @flds = split /\s+/, $line;
337            if ( @flds == 2 )
338            {
339                $seq{ $flds[0] } or push @ids, $flds[0];
340                push @{ $seq{ $flds[0] } }, $flds[1];
341            }
342        }
343        close( $fh ) if $close;
344    
345        map { [ $_, "", join( "", @{$seq{$_}} ) ] } @ids;
346  }  }
347    
348    
# Line 151  Line 358 
358      if ($title =~ /^>?\s*(\S+)(:?\s+(.*\S)\s*)?$/) {      if ($title =~ /^>?\s*(\S+)(:?\s+(.*\S)\s*)?$/) {
359          return ($1, $3 ? $3 : "");          return ($1, $3 ? $3 : "");
360      }      }
361      else {      elsif ($title =~ /^>/) {
362          return ("", "");          return ("", "");
363      }      }
364        else {
365            return (undef, "");
366        }
367  }  }
368    
369  sub split_fasta_title {  sub split_fasta_title {
# Line 162  Line 372 
372    
373    
374  #-----------------------------------------------------------------------------  #-----------------------------------------------------------------------------
375  #  Print list of sequence entries in fasta format  #  Helper function for defining an output filehandle:
376    #     filehandle is passed through
377    #     string is taken as file name to be openend
378    #     undef or "" defaults to STDOUT
379    #
380    #    ( \*FH, $name, $close [, $file] ) = output_filehandle( $file );
381  #  #
 #     print_seq_list_as_fasta(*FILEHANDLE, @seq_entry_list);  
382  #-----------------------------------------------------------------------------  #-----------------------------------------------------------------------------
383  sub print_seq_list_as_fasta {  sub output_filehandle
384      my $fh = shift;  {
385      my @seq_list = @_;      my $file = shift;
386    
387        #  FILEHANDLE
388    
389        return ( $file, $file, 0 ) if ( ref( $file ) eq "GLOB" );
390    
391        #  Null string or undef
392    
393        return ( \*STDOUT, "", 0 ) if ( ! defined( $file ) || ( $file eq "" ) );
394    
395        #  File name
396    
397        if ( ! ref( $file ) )
398        {
399            my $fh;
400            open( $fh, ">$file" ) || die "Could not open output $file\n";
401            return ( $fh, $file, 1 );
402        }
403    
404        #  Some other kind of reference; return the unused value
405    
406        return ( \*STDOUT, undef, 0, $file );
407    }
408    
409    
410    #-----------------------------------------------------------------------------
411    #  Legacy function for printing fasta sequence set:
412    #
413    #     print_seq_list_as_fasta( \*FILEHANDLE, @seq_entry_list );
414    #-----------------------------------------------------------------------------
415    sub print_seq_list_as_fasta { print_alignment_as_fasta( @_ ) }
416    
417    
418    #-----------------------------------------------------------------------------
419    #  Print list of sequence entries in fasta format.
420    #  Missing, undef or "" filename defaults to STDOUT.
421    #
422    #     print_alignment_as_fasta(                @seq_entry_list ); # STDOUT
423    #     print_alignment_as_fasta(               \@seq_entry_list ); # STDOUT
424    #     print_alignment_as_fasta( \*FILEHANDLE,  @seq_entry_list );
425    #     print_alignment_as_fasta( \*FILEHANDLE, \@seq_entry_list );
426    #     print_alignment_as_fasta(  $filename,    @seq_entry_list );
427    #     print_alignment_as_fasta(  $filename,   \@seq_entry_list );
428    #-----------------------------------------------------------------------------
429    sub print_alignment_as_fasta {
430        my ( $fh, undef, $close, $unused ) = output_filehandle( shift );
431        ( unshift @_, $unused ) if $unused;
432    
433        ( ref( $_[0] ) eq "ARRAY" ) or die "Bad sequence entry passed to print_alignment_as_fasta\n";
434    
435        #  Expand the sequence entry list if necessary:
436    
437        if ( ref( $_[0]->[0] ) eq "ARRAY" ) { @_ = @{ $_[0] } }
438    
439        foreach my $seq_ptr ( @_ ) { print_seq_as_fasta( $fh, @$seq_ptr ) }
440    
441        close( $fh ) if $close;
442    }
443    
444    
445    #-----------------------------------------------------------------------------
446    #  Print list of sequence entries in phylip format.
447    #  Missing, undef or "" filename defaults to STDOUT.
448    #
449    #     print_alignment_as_phylip(                @seq_entry_list ); # STDOUT
450    #     print_alignment_as_phylip(               \@seq_entry_list ); # STDOUT
451    #     print_alignment_as_phylip( \*FILEHANDLE,  @seq_entry_list );
452    #     print_alignment_as_phylip( \*FILEHANDLE, \@seq_entry_list );
453    #     print_alignment_as_phylip(  $filename,    @seq_entry_list );
454    #     print_alignment_as_phylip(  $filename,   \@seq_entry_list );
455    #-----------------------------------------------------------------------------
456    sub print_alignment_as_phylip {
457        my ( $fh, undef, $close, $unused ) = output_filehandle( shift );
458        ( unshift @_, $unused ) if $unused;
459    
460        ( ref( $_[0] ) eq "ARRAY" ) or die die "Bad sequence entry passed to print_alignment_as_phylip\n";
461    
462        my @seq_list = ( ref( $_[0]->[0] ) eq "ARRAY" ) ? @{ $_[0] } : @_;
463    
464        my ( %id2, %used );
465        my $maxlen = 0;
466        foreach ( @seq_list )
467        {
468            my ( $id, undef, $seq ) = @$_;
469    
470            #  Need a name that is unique within 10 characters
471    
472            my $id2 = substr( $id, 0, 10 );
473            $id2 =~ s/_/ /g;  # PHYLIP sequence files accept spaces
474            my $n = "0";
475            while ( $used{ $id2 } )
476            {
477                $n++;
478                $id2 = substr( $id, 0, 10 - length( $n ) ) . $n;
479            }
480            $used{ $id2 } = 1;
481            $id2{ $id } = $id2;
482    
483                    #  Prepare to pad sequences (should not be necessary, but ...)
484    
485            my $len = length( $seq );
486            $maxlen = $len if ( $len > $maxlen );
487        }
488    
489        my $nseq = @seq_list;
490        print $fh "$nseq  $maxlen\n";
491        foreach ( @seq_list )
492        {
493            my ( $id, undef, $seq ) = @$_;
494            my $len = length( $seq );
495            printf $fh "%-10s  %s%s\n", $id2{ $id },
496                                        $seq,
497                                        $len<$maxlen ? ("?" x ($maxlen-$len)) : "";
498        }
499    
500        close( $fh ) if $close;
501    }
502    
503    
504    #-----------------------------------------------------------------------------
505    #  Print list of sequence entries in nexus format.
506    #  Missing, undef or "" filename defaults to STDOUT.
507    #
508    #     print_alignment_as_nexus(               [ \%label_hash, ]  @seq_entry_list );
509    #     print_alignment_as_nexus(               [ \%label_hash, ] \@seq_entry_list );
510    #     print_alignment_as_nexus( \*FILEHANDLE, [ \%label_hash, ]  @seq_entry_list );
511    #     print_alignment_as_nexus( \*FILEHANDLE, [ \%label_hash, ] \@seq_entry_list );
512    #     print_alignment_as_nexus(  $filename,   [ \%label_hash, ]  @seq_entry_list );
513    #     print_alignment_as_nexus(  $filename,   [ \%label_hash, ] \@seq_entry_list );
514    #-----------------------------------------------------------------------------
515    sub print_alignment_as_nexus {
516        my ( $fh, undef, $close, $unused ) = output_filehandle( shift );
517        ( unshift @_, $unused ) if $unused;
518    
519        my $lbls = ( ref( $_[0] ) eq "HASH" ) ? shift : undef;
520    
521        ( ref( $_[0] ) eq "ARRAY" ) or die "Bad sequence entry passed to print_alignment_as_nexus\n";
522    
523        my @seq_list = ( ref( $_[0]->[0] ) eq "ARRAY" ) ? @{ $_[0] } : @_;
524    
525      foreach my $seq_ptr (@seq_list) {      my %id2;
526          print_seq_as_fasta($fh, @$seq_ptr);      my ( $maxidlen, $maxseqlen ) = ( 0, 0 );
527        my ( $n1, $n2, $nt, $nu ) = ( 0, 0, 0, 0 );
528        foreach ( @seq_list )
529        {
530            my ( $id, undef, $seq ) = @$_;
531            my $id2 = $lbls ? ( $lbls->{ $id } || $id ) : $id;
532            if ( $id2 !~ /^[-+.0-9A-Za-z~_|]+$/ )
533            {
534                    $id2 =~ s/'/''/g;
535                    $id2 = qq('$id2');
536      }      }
537            $id2{ $id } = $id2;
538            my $idlen = length( $id2 );
539            $maxidlen = $idlen if ( $idlen > $maxidlen );
540    
541            my $seqlen = length( $seq );
542            $maxseqlen = $seqlen if ( $seqlen > $maxseqlen );
543    
544            $nt += $seq =~ tr/Tt//d;
545            $nu += $seq =~ tr/Uu//d;
546            $n1 += $seq =~ tr/ACGNacgn//d;
547            $n2 += $seq =~ tr/A-Za-z//d;
548        }
549    
550        my $nseq = @seq_list;
551        my $type = ( $n1 < 2 * $n2 ) ?  'protein' : ($nt>$nu) ? 'DNA' : 'RNA';
552    
553        print $fh <<"END_HEAD";
554    #NEXUS
555    
556    BEGIN Data;
557        Dimensions
558            NTax=$nseq
559            NChar=$maxseqlen
560            ;
561        Format
562            DataType=$type
563            Gap=-
564            Missing=?
565            ;
566        Matrix
567    
568    END_HEAD
569    
570        foreach ( @seq_list )
571        {
572            my ( $id, undef, $seq ) = @$_;
573            my $len = length( $seq );
574            printf  $fh  "%-${maxidlen}s  %s%s\n",
575                         $id2{ $id },
576                         $seq,
577                         $len<$maxseqlen ? ("?" x ($maxseqlen-$len)) : "";
578        }
579    
580        print $fh <<"END_TAIL";
581    ;
582    END;
583    END_TAIL
584    
585        close( $fh ) if $close;
586  }  }
587    
588    
589  #-----------------------------------------------------------------------------  #-----------------------------------------------------------------------------
590  #  Print one sequence in fasta format to an open file  #  Print one sequence in fasta format to an open file
591  #  #
592  #     print_seq_as_fasta(*FILEHANDLE, $id, $desc, $seq);  #     print_seq_as_fasta( \*FILEHANDLE, $id, $desc, $seq );
593  #     print_seq_as_fasta(*FILEHANDLE, @seq_entry);  #     print_seq_as_fasta( \*FILEHANDLE, @seq_entry );
594  #-----------------------------------------------------------------------------  #-----------------------------------------------------------------------------
595  sub print_seq_as_fasta {  sub print_seq_as_fasta {
596      my $fh = shift;      my $fh = shift;
# Line 197  Line 607 
607  #-----------------------------------------------------------------------------  #-----------------------------------------------------------------------------
608  #  Print one sequence in GenBank flat file format:  #  Print one sequence in GenBank flat file format:
609  #  #
610  #     print_gb_locus( *FILEHANDLE, $locus, $def, $accession, $seq )  #     print_gb_locus( \*FILEHANDLE, $locus, $def, $accession, $seq )
611  #-----------------------------------------------------------------------------  #-----------------------------------------------------------------------------
612  sub print_gb_locus {  sub print_gb_locus {
613      my ($fh, $loc, $def, $acc, $seq) = @_;      my ($fh, $loc, $def, $acc, $seq) = @_;
# Line 225  Line 635 
635  #-----------------------------------------------------------------------------  #-----------------------------------------------------------------------------
636  #  Build an index from seq_id to pointer to sequence entry: (id, desc, seq)  #  Build an index from seq_id to pointer to sequence entry: (id, desc, seq)
637  #  #
638  #     my %seq_ind  = index_seq_list(@seq_list);  #     my \%seq_ind  = index_seq_list(  @seq_list );
639    #     my \%seq_ind  = index_seq_list( \@seq_list );
640  #  #
641  #  Usage example:  #  Usage example:
642  #  #
643  #  my @seq_list   = read_fasta_seqs(*STDIN);   # list of pointers to entries  #  my  @seq_list   = read_fasta_seqs(\*STDIN);  # list of pointers to entries
644  #  my %seq_ind    = index_seq_list(@seq_list); # hash from names to pointers  #  my \%seq_ind    = index_seq_list(@seq_list); # hash from names to pointers
645  #  my @chosen_seq = @{%seq_ind{"contig1"}};    # extract one entry  #  my @chosen_seq = @{%seq_ind{"contig1"}};    # extract one entry
646  #  #
647  #-----------------------------------------------------------------------------  #-----------------------------------------------------------------------------
648  sub index_seq_list {  sub index_seq_list {
649      my %seq_index = map { @{$_}[0] => $_ } @_;      ( ref( $_[0] )      ne 'ARRAY' ) ? {}
650      return \%seq_index;    : ( ref( $_[0]->[0] ) ne 'ARRAY' ) ? { map { $_->[0] => $_ } @_ }
651      :                                    { map { $_->[0] => $_ } @{ $_[0] } }
652  }  }
653    
654    
655  #-----------------------------------------------------------------------------  #-----------------------------------------------------------------------------
656  #  Three routines to access all or part of sequence entry by id:  #  Three routines to access all or part of sequence entry by id:
657  #  #
658  #     my @seq_entry  = seq_entry_by_id( \%seq_index, $seq_id );  #     @seq_entry = seq_entry_by_id( \%seq_index, $seq_id );
659  #     my $seq_desc   = seq_desc_by_id(  \%seq_index, $seq_id );  #    \@seq_entry = seq_entry_by_id( \%seq_index, $seq_id );
660  #     my $seq        = seq_data_by_id(  \%seq_index, $seq_id );  #     $seq_desc  = seq_desc_by_id(  \%seq_index, $seq_id );
661    #     $seq       = seq_data_by_id(  \%seq_index, $seq_id );
662  #  #
663  #-----------------------------------------------------------------------------  #-----------------------------------------------------------------------------
664  sub seq_entry_by_id {  sub seq_entry_by_id {
665      (my $ind_ref = shift)  || die "No index supplied to seq_entry_by_id\n";      (my $ind_ref = shift)  || die "No index supplied to seq_entry_by_id\n";
666      (my $id      = shift)  || die "No id supplied to seq_entry_by_id\n";      (my $id      = shift)  || die "No id supplied to seq_entry_by_id\n";
667      wantarray || die "entry_by_id requires list context\n";      return wantarray ? @{ $ind_ref->{$id} } : $ind_ref->{$id};
     return @{ $ind_ref->{$id} };  
668  }  }
669    
670    
# Line 269  Line 681 
681      return ${ $ind_ref->{$id} }[2];      return ${ $ind_ref->{$id} }[2];
682  }  }
683    
684    #-----------------------------------------------------------------------------
685    #  Remove columns of alignment gaps from sequences:
686    #
687    #   @packed_seqs = pack_alignment(  @seqs )
688    #   @packed_seqs = pack_alignment( \@seqs )
689    #  \@packed_seqs = pack_alignment(  @seqs )
690    #  \@packed_seqs = pack_alignment( \@seqs )
691    #
692    #-----------------------------------------------------------------------------
693    
694    sub pack_alignment
695    {
696        my @seqs = ( ref( $_[0] ) eq 'ARRAY' and ref( $_[0]->[0] ) eq 'ARRAY' ) ? @{$_[0] } : @_;
697        @seqs or return wantarray ? () : [];
698    
699        my $mask  = pack_mask( $seqs[0]->[2] );
700        foreach ( @seqs[ 1 .. (@seqs-1) ] )
701        {
702            $mask |= pack_mask( $_->[2] );
703        }
704    
705        my $seq;
706        my @seqs2 = map { $seq = $_->[2] & $mask;
707                          $seq =~ tr/\000//d;
708                          [ $_->[0], $_->[1], $seq ]
709                        }
710                    @seqs;
711    
712        return wantarray ? @seqs2 : \@seqs2;
713    }
714    
715    sub pack_mask
716    {
717        my $mask = shift;
718        $mask =~ tr/-/\000/;
719        $mask =~ tr/\000/\377/c;
720        return $mask;
721    }
722    
723  #-----------------------------------------------------------------------------  #-----------------------------------------------------------------------------
724  #  Some simple sequence manipulations:  #  Some simple sequence manipulations:
# Line 332  Line 782 
782      }      }
783    
784      if ( $fix_id ) {      if ( $fix_id ) {
785          if ( ( $id =~ s/_([0-9]+)_([0-9]+)$// )          if ( ( $id =~ s/_(\d+)_(\d+)$// )
786            && ( abs($2-$1)+1 == $len ) ) {            && ( abs($2-$1)+1 == $len ) ) {
787              if ( $1 <= $2 ) { $from += $1 - 1;         $to += $1 - 1 }              if ( $1 <= $2 ) { $from += $1 - 1;         $to += $1 - 1 }
788              else            { $from  = $1 + 1 - $from; $to  = $1 + 1 - $to }              else            { $from  = $1 + 1 - $from; $to  = $1 + 1 - $to }
# Line 353  Line 803 
803      $seq =~ tr[ACGTUKMRSWYBDHVNacgtukmrswybdhvn]      $seq =~ tr[ACGTUKMRSWYBDHVNacgtukmrswybdhvn]
804                [TGCAAMKYSWRVHDBNtgcaamkyswrvhdbn];                [TGCAAMKYSWRVHDBNtgcaamkyswrvhdbn];
805      if ($fix_id) {      if ($fix_id) {
806          if ($id =~ s/_([0-9]+)_([0-9]+)$//) {          if ($id =~ s/_(\d+)_(\d+)$//) {
807              $id .= "_" . $2 . "_" . $1;              $id .= "_" . $2 . "_" . $1;
808          }          }
809          else {          else {
# Line 374  Line 824 
824      $seq =~ tr[ACGTUKMRSWYBDHVNacgtukmrswybdhvn]      $seq =~ tr[ACGTUKMRSWYBDHVNacgtukmrswybdhvn]
825                [UGCAAMKYSWRVHDBNugcaamkyswrvhdbn];                [UGCAAMKYSWRVHDBNugcaamkyswrvhdbn];
826      if ($fix_id) {      if ($fix_id) {
827          if ($id =~ s/_([0-9]+)_([0-9]+)$//) {          if ($id =~ s/_(\d+)_(\d+)$//) {
828              $id .= "_" . $2 . "_" . $1;              $id .= "_" . $2 . "_" . $1;
829          }          }
830          else {          else {
# Line 416  Line 866 
866  }  }
867    
868    
869    sub pack_seq {
870        my $seq = shift;
871        $seq =~ tr/A-Za-z//cd;
872        return $seq;
873    }
874    
875    
876  sub clean_ae_sequence {  sub clean_ae_sequence {
877      $_ = shift;      $_ = shift;
878      $_ = to7bit($_);      $_ = to7bit($_);
# Line 464  Line 921 
921  #  #
922  #-----------------------------------------------------------------------------  #-----------------------------------------------------------------------------
923    
924  our %genetic_code = (   # work as DNA  @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
925    @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
926    @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 );
927    
928    %genetic_code = (
929    
930        # DNA version
931    
932      TTT => "F",  TCT => "S",  TAT => "Y",  TGT => "C",      TTT => "F",  TCT => "S",  TAT => "Y",  TGT => "C",
933      TTC => "F",  TCC => "S",  TAC => "Y",  TGC => "C",      TTC => "F",  TCC => "S",  TAC => "Y",  TGC => "C",
934      TTA => "L",  TCA => "S",  TAA => "*",  TGA => "*",      TTA => "L",  TCA => "S",  TAA => "*",  TGA => "*",
# Line 482  Line 946 
946      GTA => "V",  GCA => "A",  GAA => "E",  GGA => "G",      GTA => "V",  GCA => "A",  GAA => "E",  GGA => "G",
947      GTG => "V",  GCG => "A",  GAG => "E",  GGG => "G",      GTG => "V",  GCG => "A",  GAG => "E",  GGG => "G",
948    
949        # RNA suppliment
950    
951        UUU => "F",  UCU => "S",  UAU => "Y",  UGU => "C",
952        UUC => "F",  UCC => "S",  UAC => "Y",  UGC => "C",
953        UUA => "L",  UCA => "S",  UAA => "*",  UGA => "*",
954        UUG => "L",  UCG => "S",  UAG => "*",  UGG => "W",
955        CUU => "L",  CCU => "P",  CAU => "H",  CGU => "R",
956        CUC => "L",
957        CUA => "L",
958        CUG => "L",
959        AUU => "I",  ACU => "T",  AAU => "N",  AGU => "S",
960        AUC => "I",
961        AUA => "I",
962        AUG => "M",
963        GUU => "V",  GCU => "A",  GAU => "D",  GGU => "G",
964        GUC => "V",
965        GUA => "V",
966        GUG => "V",
967    
968      #  The following ambiguous encodings are not necessary,  but      #  The following ambiguous encodings are not necessary,  but
969      #  speed up the processing of some common ambiguous triplets:      #  speed up the processing of some ambiguous triplets:
970    
971      TTY => "F",  TCY => "S",  TAY => "Y",  TGY => "C",      TTY => "F",  TCY => "S",  TAY => "Y",  TGY => "C",
972      TTR => "L",  TCR => "S",  TAR => "*",      TTR => "L",  TCR => "S",  TAR => "*",
# Line 496  Line 979 
979                   ACN => "T",                   ACN => "T",
980      GTY => "V",  GCY => "A",  GAY => "D",  GGY => "G",      GTY => "V",  GCY => "A",  GAY => "D",  GGY => "G",
981      GTR => "V",  GCR => "A",  GAR => "E",  GGR => "G",      GTR => "V",  GCR => "A",  GAR => "E",  GGR => "G",
982      GTN => "V",  GCN => "A",               GGN => "G"      GTN => "V",  GCN => "A",               GGN => "G",
983    
984        UUY => "F",  UCY => "S",  UAY => "Y",  UGY => "C",
985        UUR => "L",  UCR => "S",  UAR => "*",
986                     UCN => "S",
987        CUY => "L",
988        CUR => "L",
989        CUN => "L",
990        AUY => "I",
991        GUY => "V",
992        GUR => "V",
993        GUN => "V"
994    );
995    
996    
997    #  Add lower case by construction:
998    
999    foreach ( keys %genetic_code ) {
1000        $genetic_code{ lc $_ } = lc $genetic_code{ $_ }
1001    }
1002    
1003    
1004    #  Construct the genetic code with selanocysteine by difference:
1005    
1006    %genetic_code_with_U = map { $_ => $genetic_code{ $_ } } keys %genetic_code;
1007    $genetic_code_with_U{ TGA } = "U";
1008    $genetic_code_with_U{ tga } = "u";
1009    $genetic_code_with_U{ UGA } = "U";
1010    $genetic_code_with_U{ uga } = "u";
1011    
1012    
1013    %amino_acid_codons_DNA = (
1014             L  => [ qw( TTA TTG CTA CTG CTT CTC ) ],
1015             R  => [ qw( AGA AGG CGA CGG CGT CGC ) ],
1016             S  => [ qw( AGT AGC TCA TCG TCT TCC ) ],
1017             A  => [ qw( GCA GCG GCT GCC ) ],
1018             G  => [ qw( GGA GGG GGT GGC ) ],
1019             P  => [ qw( CCA CCG CCT CCC ) ],
1020             T  => [ qw( ACA ACG ACT ACC ) ],
1021             V  => [ qw( GTA GTG GTT GTC ) ],
1022             I  => [ qw( ATA ATT ATC ) ],
1023             C  => [ qw( TGT TGC ) ],
1024             D  => [ qw( GAT GAC ) ],
1025             E  => [ qw( GAA GAG ) ],
1026             F  => [ qw( TTT TTC ) ],
1027             H  => [ qw( CAT CAC ) ],
1028             K  => [ qw( AAA AAG ) ],
1029             N  => [ qw( AAT AAC ) ],
1030             Q  => [ qw( CAA CAG ) ],
1031             Y  => [ qw( TAT TAC ) ],
1032             M  => [ qw( ATG ) ],
1033             U  => [ qw( TGA ) ],
1034             W  => [ qw( TGG ) ],
1035    
1036             l  => [ qw( tta ttg cta ctg ctt ctc ) ],
1037             r  => [ qw( aga agg cga cgg cgt cgc ) ],
1038             s  => [ qw( agt agc tca tcg tct tcc ) ],
1039             a  => [ qw( gca gcg gct gcc ) ],
1040             g  => [ qw( gga ggg ggt ggc ) ],
1041             p  => [ qw( cca ccg cct ccc ) ],
1042             t  => [ qw( aca acg act acc ) ],
1043             v  => [ qw( gta gtg gtt gtc ) ],
1044             i  => [ qw( ata att atc ) ],
1045             c  => [ qw( tgt tgc ) ],
1046             d  => [ qw( gat gac ) ],
1047             e  => [ qw( gaa gag ) ],
1048             f  => [ qw( ttt ttc ) ],
1049             h  => [ qw( cat cac ) ],
1050             k  => [ qw( aaa aag ) ],
1051             n  => [ qw( aat aac ) ],
1052             q  => [ qw( caa cag ) ],
1053             y  => [ qw( tat tac ) ],
1054             m  => [ qw( atg ) ],
1055             u  => [ qw( tga ) ],
1056             w  => [ qw( tgg ) ],
1057    
1058            '*' => [ qw( TAA TAG TGA ) ]
1059    );
1060    
1061    
1062    
1063    %amino_acid_codons_RNA = (
1064             L  => [ qw( UUA UUG CUA CUG CUU CUC ) ],
1065             R  => [ qw( AGA AGG CGA CGG CGU CGC ) ],
1066             S  => [ qw( AGU AGC UCA UCG UCU UCC ) ],
1067             A  => [ qw( GCA GCG GCU GCC ) ],
1068             G  => [ qw( GGA GGG GGU GGC ) ],
1069             P  => [ qw( CCA CCG CCU CCC ) ],
1070             T  => [ qw( ACA ACG ACU ACC ) ],
1071             V  => [ qw( GUA GUG GUU GUC ) ],
1072             B  => [ qw( GAU GAC AAU AAC ) ],
1073             Z  => [ qw( GAA GAG CAA CAG ) ],
1074             I  => [ qw( AUA AUU AUC ) ],
1075             C  => [ qw( UGU UGC ) ],
1076             D  => [ qw( GAU GAC ) ],
1077             E  => [ qw( GAA GAG ) ],
1078             F  => [ qw( UUU UUC ) ],
1079             H  => [ qw( CAU CAC ) ],
1080             K  => [ qw( AAA AAG ) ],
1081             N  => [ qw( AAU AAC ) ],
1082             Q  => [ qw( CAA CAG ) ],
1083             Y  => [ qw( UAU UAC ) ],
1084             M  => [ qw( AUG ) ],
1085             U  => [ qw( UGA ) ],
1086             W  => [ qw( UGG ) ],
1087    
1088             l  => [ qw( uua uug cua cug cuu cuc ) ],
1089             r  => [ qw( aga agg cga cgg cgu cgc ) ],
1090             s  => [ qw( agu agc uca ucg ucu ucc ) ],
1091             a  => [ qw( gca gcg gcu gcc ) ],
1092             g  => [ qw( gga ggg ggu ggc ) ],
1093             p  => [ qw( cca ccg ccu ccc ) ],
1094             t  => [ qw( aca acg acu acc ) ],
1095             v  => [ qw( gua gug guu guc ) ],
1096             b  => [ qw( gau gac aau aac ) ],
1097             z  => [ qw( gaa gag caa cag ) ],
1098             i  => [ qw( aua auu auc ) ],
1099             c  => [ qw( ugu ugc ) ],
1100             d  => [ qw( gau gac ) ],
1101             e  => [ qw( gaa gag ) ],
1102             f  => [ qw( uuu uuc ) ],
1103             h  => [ qw( cau cac ) ],
1104             k  => [ qw( aaa aag ) ],
1105             n  => [ qw( aau aac ) ],
1106             q  => [ qw( caa cag ) ],
1107             y  => [ qw( uau uac ) ],
1108             m  => [ qw( aug ) ],
1109             u  => [ qw( uga ) ],
1110             w  => [ qw( ugg ) ],
1111    
1112            '*' => [ qw( UAA UAG UGA ) ]
1113    );
1114    
1115    
1116    %n_codon_for_aa = map {
1117        $_ => scalar @{ $amino_acid_codons_DNA{ $_ } }
1118        } keys %amino_acid_codons_DNA;
1119    
1120    
1121    %reverse_genetic_code_DNA = (
1122             A  => "GCN",  a  => "gcn",
1123             C  => "TGY",  c  => "tgy",
1124             D  => "GAY",  d  => "gay",
1125             E  => "GAR",  e  => "gar",
1126             F  => "TTY",  f  => "tty",
1127             G  => "GGN",  g  => "ggn",
1128             H  => "CAY",  h  => "cay",
1129             I  => "ATH",  i  => "ath",
1130             K  => "AAR",  k  => "aar",
1131             L  => "YTN",  l  => "ytn",
1132             M  => "ATG",  m  => "atg",
1133             N  => "AAY",  n  => "aay",
1134             P  => "CCN",  p  => "ccn",
1135             Q  => "CAR",  q  => "car",
1136             R  => "MGN",  r  => "mgn",
1137             S  => "WSN",  s  => "wsn",
1138             T  => "ACN",  t  => "acn",
1139             U  => "TGA",  u  => "tga",
1140             V  => "GTN",  v  => "gtn",
1141             W  => "TGG",  w  => "tgg",
1142             X  => "NNN",  x  => "nnn",
1143             Y  => "TAY",  y  => "tay",
1144            '*' => "TRR"
1145    );
1146    
1147    %reverse_genetic_code_RNA = (
1148             A  => "GCN",  a  => "gcn",
1149             C  => "UGY",  c  => "ugy",
1150             D  => "GAY",  d  => "gay",
1151             E  => "GAR",  e  => "gar",
1152             F  => "UUY",  f  => "uuy",
1153             G  => "GGN",  g  => "ggn",
1154             H  => "CAY",  h  => "cay",
1155             I  => "AUH",  i  => "auh",
1156             K  => "AAR",  k  => "aar",
1157             L  => "YUN",  l  => "yun",
1158             M  => "AUG",  m  => "aug",
1159             N  => "AAY",  n  => "aay",
1160             P  => "CCN",  p  => "ccn",
1161             Q  => "CAR",  q  => "car",
1162             R  => "MGN",  r  => "mgn",
1163             S  => "WSN",  s  => "wsn",
1164             T  => "ACN",  t  => "acn",
1165             U  => "UGA",  u  => "uga",
1166             V  => "GUN",  v  => "gun",
1167             W  => "UGG",  w  => "ugg",
1168             X  => "NNN",  x  => "nnn",
1169             Y  => "UAY",  y  => "uay",
1170            '*' => "URR"
1171  );  );
1172    
1173    
1174  my %DNA_letter_can_be = (  %DNA_letter_can_be = (
1175      A => ["A"],                 a => ["a"],      A => ["A"],                 a => ["a"],
1176      B => ["C", "G", "T"],       b => ["c", "g", "t"],      B => ["C", "G", "T"],       b => ["c", "g", "t"],
1177      C => ["C"],                 c => ["c"],      C => ["C"],                 c => ["c"],
# Line 520  Line 1191 
1191  );  );
1192    
1193    
1194  my %RNA_letter_can_be = (  %RNA_letter_can_be = (
1195      A => ["A"],                 a => ["a"],      A => ["A"],                 a => ["a"],
1196      B => ["C", "G", "U"],       b => ["c", "g", "u"],      B => ["C", "G", "U"],       b => ["c", "g", "u"],
1197      C => ["C"],                 c => ["c"],      C => ["C"],                 c => ["c"],
# Line 540  Line 1211 
1211  );  );
1212    
1213    
1214  my %one_letter_to_three_letter_aa = (  %one_letter_to_three_letter_aa = (
1215       A  => "Ala",           A  => "Ala", a  => "Ala",
1216       B  => "Asx",           B  => "Asx", b  => "Asx",
1217       C  => "Cys",           C  => "Cys", c  => "Cys",
1218       D  => "Asp",           D  => "Asp", d  => "Asp",
1219       E  => "Glu",           E  => "Glu", e  => "Glu",
1220       F  => "Phe",           F  => "Phe", f  => "Phe",
1221       G  => "Gly",           G  => "Gly", g  => "Gly",
1222       H  => "His",           H  => "His", h  => "His",
1223       I  => "Ile",           I  => "Ile", i  => "Ile",
1224       K  => "Lys",           K  => "Lys", k  => "Lys",
1225       L  => "Leu",           L  => "Leu", l  => "Leu",
1226       M  => "Met",           M  => "Met", m  => "Met",
1227       N  => "Asn",           N  => "Asn", n  => "Asn",
1228       P  => "Pro",           P  => "Pro", p  => "Pro",
1229       Q  => "Gln",           Q  => "Gln", q  => "Gln",
1230       R  => "Arg",           R  => "Arg", r  => "Arg",
1231       S  => "Ser",           S  => "Ser", s  => "Ser",
1232       T  => "Thr",           T  => "Thr", t  => "Thr",
1233       U  => "Sec",           U  => "Sec", u  => "Sec",
1234       V  => "Val",           V  => "Val", v  => "Val",
1235       W  => "Trp",           W  => "Trp", w  => "Trp",
1236       X  => "Xxx",           X  => "Xxx", x  => "Xxx",
1237       Y  => "Tyr",           Y  => "Tyr", y  => "Tyr",
1238       Z  => "Glx",           Z  => "Glx", z  => "Glx",
1239      "*" => "***"          '*' => "***"
1240            );
1241    
1242    
1243    %three_letter_to_one_letter_aa = (
1244         ALA  => "A",   Ala  => "A",   ala  => "a",
1245         ARG  => "R",   Arg  => "R",   arg  => "r",
1246         ASN  => "N",   Asn  => "N",   asn  => "n",
1247         ASP  => "D",   Asp  => "D",   asp  => "d",
1248         ASX  => "B",   Asx  => "B",   asx  => "b",
1249         CYS  => "C",   Cys  => "C",   cys  => "c",
1250         GLN  => "Q",   Gln  => "Q",   gln  => "q",
1251         GLU  => "E",   Glu  => "E",   glu  => "e",
1252         GLX  => "Z",   Glx  => "Z",   glx  => "z",
1253         GLY  => "G",   Gly  => "G",   gly  => "g",
1254         HIS  => "H",   His  => "H",   his  => "h",
1255         ILE  => "I",   Ile  => "I",   ile  => "i",
1256         LEU  => "L",   Leu  => "L",   leu  => "l",
1257         LYS  => "K",   Lys  => "K",   lys  => "k",
1258         MET  => "M",   Met  => "M",   met  => "m",
1259         PHE  => "F",   Phe  => "F",   phe  => "f",
1260         PRO  => "P",   Pro  => "P",   pro  => "p",
1261         SEC  => "U",   Sec  => "U",   sec  => "u",
1262         SER  => "S",   Ser  => "S",   ser  => "s",
1263         THR  => "T",   Thr  => "T",   thr  => "t",
1264         TRP  => "W",   Trp  => "W",   trp  => "w",
1265         TYR  => "Y",   Tyr  => "Y",   tyr  => "y",
1266         VAL  => "V",   Val  => "V",   val  => "v",
1267         XAA  => "X",   Xaa  => "X",   xaa  => "x",
1268         XXX  => "X",   Xxx  => "X",   xxx  => "x",
1269        '***' => "*"
1270  );  );
1271    
1272    
# Line 586  Line 1287 
1287                              #  (note: undef is false)                              #  (note: undef is false)
1288    
1289      my $imax = length($seq) - 2;  # will try to translate 2 nucleotides!      my $imax = length($seq) - 2;  # will try to translate 2 nucleotides!
1290      my $pep  = ($met && ($imax >= 0)) ? "M" : "";      my $pep = ( ($met && ($imax >= 0)) ? "M" : "" );
     my $aa;  
1291      for (my $i = $met ? 3 : 0; $i <= $imax; $i += 3) {      for (my $i = $met ? 3 : 0; $i <= $imax; $i += 3) {
1292          $pep .= translate_uc_DNA_codon( substr($seq,$i,3) );          $pep .= translate_uc_DNA_codon( substr($seq,$i,3) );
1293      }      }
# Line 796  Line 1496 
1496  #  Read a list of intervals from a file.  #  Read a list of intervals from a file.
1497  #  Allow id_start_end, or id \s start \s end formats  #  Allow id_start_end, or id \s start \s end formats
1498  #  #
1499  #     @intervals = read_intervals(*FILEHANDLE)  #     @intervals = read_intervals( \*FILEHANDLE )
1500  #-----------------------------------------------------------------------------  #-----------------------------------------------------------------------------
1501  sub read_intervals {  sub read_intervals {
1502      my $fh = shift;      my $fh = shift;
# Line 819  Line 1519 
1519    
1520    
1521  #-----------------------------------------------------------------------------  #-----------------------------------------------------------------------------
1522    #  Convert a list of intervals to read [ id, left_end, right_end ].
1523    #
1524    #     @intervals = standardize_intervals( @interval_refs )
1525    #-----------------------------------------------------------------------------
1526    sub standardize_intervals {
1527        map { ( $_->[1] < $_->[2] ) ? $_ : [ $_->[0], $_->[2], $_->[1] ] } @_;
1528    }
1529    
1530    
1531    #-----------------------------------------------------------------------------
1532  #  Take the union of a list of intervals  #  Take the union of a list of intervals
1533  #  #
1534  #     @joined = join_intervals( @interval_refs )  #     @joined = join_intervals( @interval_refs )
# Line 858  Line 1568 
1568      return @joined;      return @joined;
1569  }  }
1570    
1571    
1572    #-----------------------------------------------------------------------------
1573    #  Split location strings to oriented intervals.
1574    #
1575    #     @intervals = locations_2_intervals( @locations )
1576    #     $interval  = locations_2_intervals( $location  )
1577    #-----------------------------------------------------------------------------
1578    sub locations_2_intervals {
1579        my @intervals = map { /^(\S+)_(\d+)_(\d+)(\s.*)?$/
1580                           || /^(\S+)_(\d+)-(\d+)(\s.*)?$/
1581                           || /^(\S+)=(\d+)=(\d+)(\s.*)?$/
1582                           || /^(\S+)\s+(\d+)\s+(\d+)(\s.*)?$/
1583                            ? [ $1, $2+0, $3+0 ]
1584                            : ()
1585                            } @_;
1586    
1587        return wantarray ? @intervals : $intervals[0];
1588    }
1589    
1590    
1591    #-----------------------------------------------------------------------------
1592    #  Read a list of oriented intervals from a file.
1593    #  Allow id_start_end, or id \s start \s end formats
1594    #
1595    #     @intervals = read_oriented_intervals( \*FILEHANDLE )
1596    #-----------------------------------------------------------------------------
1597    sub read_oriented_intervals {
1598        my $fh = shift;
1599        my @intervals = ();
1600    
1601        while (<$fh>) {
1602            chomp;
1603               /^(\S+)_(\d+)_(\d+)(\s.*)?$/        #  id_start_end       WIT2
1604            || /^(\S+)_(\d+)-(\d+)(\s.*)?$/        #  id_start-end       ???
1605            || /^(\S+)=(\d+)=(\d+)(\s.*)?$/        #  id=start=end       Badger
1606            || /^(\S+)\s+(\d+)\s+(\d+)(\s.*)?$/    #  id \s start \s end
1607            || next;
1608    
1609            #  Matched a pattern.  Store reference to (id, start, end):
1610            push @intervals, [ $1, $2+0, $3+0 ];
1611        }
1612        return @intervals;
1613    }
1614    
1615    
1616    #-----------------------------------------------------------------------------
1617    #  Reverse the orientation of a list of intervals
1618    #
1619    #     @reversed = reverse_intervals( @interval_refs )
1620    #-----------------------------------------------------------------------------
1621    sub reverse_intervals {
1622        map { [ $_->[0], $_->[2], $_->[1] ] } @_;
1623    }
1624    
1625    
1626    #-----------------------------------------------------------------------------
1627    #  Convert GenBank locations to SEED locations
1628    #
1629    #     @seed_locs = gb_location_2_seed( $contig, @gb_locs )
1630    #-----------------------------------------------------------------------------
1631    sub gb_location_2_seed
1632    {
1633        my $contig = shift @_;
1634        $contig or die "First arg of gb_location_2_seed must be contig_id\n";
1635    
1636        map { join( ',', gb_loc_2_seed_2( $contig, $_ ) ) || undef } @_
1637    }
1638    
1639    sub gb_loc_2_seed_2
1640    {
1641        my ( $contig, $loc ) = @_;
1642    
1643        if ( $loc =~ /^(\d+)\.\.(\d+)$/ )
1644        {
1645            join( '_', $contig, $1, $2 )
1646        }
1647    
1648        elsif ( $loc =~ /^join\((.*)\)$/ )
1649        {
1650            $loc = $1;
1651            my $lvl = 0;
1652            for ( my $i = length( $loc )-1; $i >= 0; $i-- )
1653            {
1654                for ( substr( $loc, $i, 1 ) )
1655                {
1656                    /,/ && ! $lvl and substr( $loc, $i, 1 ) = "\t";
1657                    /\(/          and $lvl--;
1658                    /\)/          and $lvl++;
1659                }
1660            }
1661            $lvl == 0 or print STDERR "Paren matching error: $loc\n" and die;
1662            map { gb_loc_2_seed_2( $contig, $_ ) } split /\t/, $loc
1663        }
1664    
1665        elsif ( $loc =~ /^complement\((.*)\)$/ )
1666        {
1667            map { s/_(\d+)_(\d+)$/_$2_$1/; $_ }
1668            reverse
1669            gb_loc_2_seed_2( $contig, $1 )
1670        }
1671    
1672        else
1673        {
1674            ()
1675        }
1676    }
1677    
1678    
1679  1;  1;

Legend:
Removed from v.1.1  
changed lines
  Added in v.1.6

MCS Webmaster
ViewVC Help
Powered by ViewVC 1.0.3