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

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

MCS Webmaster
ViewVC Help
Powered by ViewVC 1.0.3