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

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

MCS Webmaster
ViewVC Help
Powered by ViewVC 1.0.3