[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.3, Mon Dec 5 19:06:30 2005 UTC revision 1.9, Thu Nov 1 20:07:42 2007 UTC
# Line 1  Line 1 
 #  
 # Copyright (c) 2003-2006 University of Chicago and Fellowship  
 # for Interpretations of Genomes. All Rights Reserved.  
 #  
 # This file is part of the SEED Toolkit.  
 #  
 # The SEED Toolkit is free software. You can redistribute  
 # it and/or modify it under the terms of the SEED Toolkit  
 # Public License.  
 #  
 # You should have received a copy of the SEED Toolkit Public License  
 # along with this program; if not write to the University of Chicago  
 # at info@ci.uchicago.edu or the Fellowship for Interpretation of  
 # Genomes at veronika@thefig.info or download a copy from  
 # http://www.theseed.org/LICENSE.TXT.  
 #  
   
1  package gjoseqlib;  package gjoseqlib;
2    
3  #  A sequence entry is ( $id, $def, $seq )  #  A sequence entry is ( $id, $def, $seq )
4  #  A list of entries is a list of references  #  A list of entries is a list of references
5  #  #
6  #  @seq_entry = read_next_fasta_seq( *FILEHANDLE )  #  @seq_entry   = read_next_fasta_seq( \*FILEHANDLE )
7  #  @seq_entries = read_fasta_seqs( *FILEHANDLE )  #  @seq_entries = read_fasta_seqs( \*FILEHANDLE )   # Original form
8  #  $seq_ind = index_seq_list( @seq_entries ); # hash from ids to entry refs  #  @seq_entries = read_fasta( )                     # STDIN
9    #  @seq_entries = read_fasta( \*FILEHANDLE )
10    #  @seq_entries = read_fasta(  $filename )
11    #  @seq_entries = read_clustal( )                   # STDIN
12    #  @seq_entries = read_clustal( \*FILEHANDLE )
13    #  @seq_entries = read_clustal(  $filename )
14    #  @seq_entries = read_clustal_file(  $filename )
15  #  #
16    #  $seq_ind   = index_seq_list( @seq_entries );   # hash from ids to entries
17  #  @seq_entry = seq_entry_by_id( \%seq_index, $seq_id );  #  @seq_entry = seq_entry_by_id( \%seq_index, $seq_id );
18  #  $seq_desc  = seq_desc_by_id(  \%seq_index, $seq_id );  #  $seq_desc  = seq_desc_by_id(  \%seq_index, $seq_id );
19  #  $seq       = seq_data_by_id(  \%seq_index, $seq_id );  #  $seq       = seq_data_by_id(  \%seq_index, $seq_id );
# Line 31  Line 21 
21  #  ( $id, $def ) = parse_fasta_title( $title )  #  ( $id, $def ) = parse_fasta_title( $title )
22  #  ( $id, $def ) = split_fasta_title( $title )  #  ( $id, $def ) = split_fasta_title( $title )
23  #  #
24  #  print_seq_list_as_fasta( *FILEHANDLE, @seq_entry_list );  #  print_seq_list_as_fasta( \*FILEHANDLE, @seq_entry_list );  # Original form
25  #  print_seq_as_fasta( *FILEHANDLE, $id, $desc, $seq) ;  #  print_alignment_as_fasta(                @seq_entry_list ); # STDOUT
26  #  print_seq_as_fasta( *FILEHANDLE, @seq_entry );  #  print_alignment_as_fasta(               \@seq_entry_list ); # STDOUT
27  #  print_gb_locus( *FILEHANDLE, $locus, $def, $accession, $seq )  #  print_alignment_as_fasta( \*FILEHANDLE,  @seq_entry_list );
28    #  print_alignment_as_fasta( \*FILEHANDLE, \@seq_entry_list );
29    #  print_alignment_as_fasta(  $filename,    @seq_entry_list );
30    #  print_alignment_as_fasta(  $filename,   \@seq_entry_list );
31    #  print_alignment_as_phylip(                @seq_entry_list ); # STDOUT
32    #  print_alignment_as_phylip(               \@seq_entry_list ); # STDOUT
33    #  print_alignment_as_phylip( \*FILEHANDLE,  @seq_entry_list );
34    #  print_alignment_as_phylip( \*FILEHANDLE, \@seq_entry_list );
35    #  print_alignment_as_phylip(  $filename,    @seq_entry_list );
36    #  print_alignment_as_phylip(  $filename,   \@seq_entry_list );
37    #  print_alignment_as_nexus(               [ \%label_hash, ]  @seq_entry_list );
38    #  print_alignment_as_nexus(               [ \%label_hash, ] \@seq_entry_list );
39    #  print_alignment_as_nexus( \*FILEHANDLE, [ \%label_hash, ]  @seq_entry_list );
40    #  print_alignment_as_nexus( \*FILEHANDLE, [ \%label_hash, ] \@seq_entry_list );
41    #  print_alignment_as_nexus(  $filename,   [ \%label_hash, ]  @seq_entry_list );
42    #  print_alignment_as_nexus(  $filename,   [ \%label_hash, ] \@seq_entry_list );
43    #  print_seq_as_fasta( \*FILEHANDLE, $id, $desc, $seq) ;
44    #  print_seq_as_fasta( \*FILEHANDLE, @seq_entry );
45    #  print_gb_locus( \*FILEHANDLE, $locus, $def, $accession, $seq );
46    #
47    #   @packed_seqs = pack_alignment(  @seqs )
48    #   @packed_seqs = pack_alignment( \@seqs )
49    #  \@packed_seqs = pack_alignment(  @seqs )
50    #  \@packed_seqs = pack_alignment( \@seqs )
51  #  #
52  #  @entry  = subseq_DNA_entry( @seq_entry, $from, $to [, $fix_id] );  #  @entry  = subseq_DNA_entry( @seq_entry, $from, $to [, $fix_id] );
53  #  @entry  = subseq_RNA_entry( @seq_entry, $from, $to [, $fix_id] );  #  @entry  = subseq_RNA_entry( @seq_entry, $from, $to [, $fix_id] );
54    #  $DNAseq = DNA_subseq(  $seq, $from, $to );
55    #  $DNAseq = DNA_subseq( \$seq, $from, $to );
56    #  $RNAseq = RNA_subseq(  $seq, $from, $to );
57    #  $RNAseq = RNA_subseq( \$seq, $from, $to );
58  #  @entry  = complement_DNA_entry( @seq_entry [, $fix_id] );  #  @entry  = complement_DNA_entry( @seq_entry [, $fix_id] );
59  #  @entry  = complement_RNA_entry( @seq_entry [, $fix_id] );  #  @entry  = complement_RNA_entry( @seq_entry [, $fix_id] );
60  #  $DNAseq = complement_DNA_seq( $NA_seq );  #  $DNAseq = complement_DNA_seq( $NA_seq );
# Line 54  Line 71 
71  #  User-supplied genetic code must be upper case index and match the  #  User-supplied genetic code must be upper case index and match the
72  #  DNA versus RNA type of sequence  #  DNA versus RNA type of sequence
73  #  #
74    #  $seq = translate_seq_with_user_code( $seq, $gen_code_hash [, $met_start] )
75    #
76  #  Locations (= oriented intervals) are ( id, start, end )  #  Locations (= oriented intervals) are ( id, start, end )
77  #  Intervals are ( id, left, right )  #  Intervals are ( id, left, right )
78  #  #
79  #  @intervals = read_intervals( *FILEHANDLE )  #  @intervals = read_intervals( \*FILEHANDLE )
80  #  @intervals = read_oriented_intervals( *FILEHANDLE )  #  @intervals = read_oriented_intervals( \*FILEHANDLE )
81  #  @intervals = standardize_intervals( @interval_refs ) # (id, left, right)  #  @intervals = standardize_intervals( @interval_refs ) # (id, left, right)
82  #  @joined    = join_intervals( @interval_refs )  #  @joined    = join_intervals( @interval_refs )
83  #  @intervals = locations_2_intervals( @locations )  #  @intervals = locations_2_intervals( @locations )
84  #  $interval  = locations_2_intervals( $location  )  #  $interval  = locations_2_intervals( $location  )
85  #  @reversed  = reverse_intervals( @interval_refs )      # (id, end, start)  #  @reversed  = reverse_intervals( @interval_refs )      # (id, end, start)
86    #
87    #  Convert GenBank locations to SEED locations
88    #
89    #  @seed_locs = gb_location_2_seed( $contig, @gb_locs )
90    #
91    #  Read quality scores from a fasta-like file:
92    #
93    #  @seq_entries = read_qual( )               #  STDIN
94    # \@seq_entries = read_qual( )               #  STDIN
95    #  @seq_entries = read_qual( \*FILEHANDLE )
96    # \@seq_entries = read_qual( \*FILEHANDLE )
97    #  @seq_entries = read_qual(  $filename )
98    # \@seq_entries = read_qual(  $filename )
99    #
100    
101  use strict;  use strict;
102    use Carp;
 use gjolib qw( wrap_text );  
103    
104  #  Exported global variables:  #  Exported global variables:
105    
# Line 92  Line 123 
123  our @ISA = qw(Exporter);  our @ISA = qw(Exporter);
124  our @EXPORT = qw(  our @EXPORT = qw(
125          read_fasta_seqs          read_fasta_seqs
126            read_fasta
127          read_next_fasta_seq          read_next_fasta_seq
128            read_clustal_file
129            read_clustal
130          parse_fasta_title          parse_fasta_title
131          split_fasta_title          split_fasta_title
132          print_seq_list_as_fasta          print_seq_list_as_fasta
133            print_alignment_as_fasta
134            print_alignment_as_phylip
135            print_alignment_as_nexus
136          print_seq_as_fasta          print_seq_as_fasta
137          print_gb_locus          print_gb_locus
138    
# Line 104  Line 141 
141          seq_desc_by_id          seq_desc_by_id
142          seq_data_by_id          seq_data_by_id
143    
144            pack_alignment
145    
146          subseq_DNA_entry          subseq_DNA_entry
147          subseq_RNA_entry          subseq_RNA_entry
148            DNA_subseq
149            RNA_subseq
150          complement_DNA_entry          complement_DNA_entry
151          complement_RNA_entry          complement_RNA_entry
152          complement_DNA_seq          complement_DNA_seq
# Line 125  Line 166 
166          locations_2_intervals          locations_2_intervals
167          read_oriented_intervals          read_oriented_intervals
168          reverse_intervals          reverse_intervals
169    
170            gb_location_2_seed
171    
172            read_qual
173          );          );
174    
175  our @EXPORT_OK = qw(  our @EXPORT_OK = qw(
# Line 146  Line 191 
191    
192    
193  #-----------------------------------------------------------------------------  #-----------------------------------------------------------------------------
194  #  Read fasta sequences from a file.  #  Helper function for defining an input filehandle:
195    #     filehandle is passed through
196    #     string is taken as file name to be openend
197    #     undef or "" defaults to STDOUT
198    #
199    #    ( \*FH, $name, $close [, $file] ) = input_filehandle( $file );
200    #
201    #-----------------------------------------------------------------------------
202    sub input_filehandle
203    {
204        my $file = shift;
205    
206        #  FILEHANDLE
207    
208        return ( $file, $file, 0 ) if ( ref( $file ) eq "GLOB" );
209    
210        #  Null string or undef
211    
212        return ( \*STDIN, "", 0 ) if ( ! defined( $file ) || ( $file eq "" ) );
213    
214        #  File name
215    
216        if ( ! ref( $file ) )
217        {
218            my $fh;
219            -f $file or die "Could not find input file \"$file\"\n";
220            open( $fh, "<$file" ) || die "Could not open \"$file\" for input\n";
221            return ( $fh, $file, 1 );
222        }
223    
224        #  Some other kind of reference; return the unused value
225    
226        return ( \*STDIN, undef, 0, $file );
227    }
228    
229    
230    #-----------------------------------------------------------------------------
231    #  Read fasta sequences from a filehandle (legacy interface; use read_fasta)
232  #  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)
233  #  #
234  #     @seqs = read_fasta_seqs(*FILEHANDLE)  #     @seq_entries = read_fasta_seqs( \*FILEHANDLE )
235  #-----------------------------------------------------------------------------  #-----------------------------------------------------------------------------
236  sub read_fasta_seqs {  sub read_fasta_seqs { read_fasta( @_ ) }
237      my $fh = shift;  
238      wantarray || die "read_fasta_seqs requires list context\n";  
239    #-----------------------------------------------------------------------------
240    #  Read fasta sequences.
241    #  Save the contents in a list of refs to arrays:  (id, description, seq)
242    #
243    #     @seq_entries = read_fasta( )               #  STDIN
244    #    \@seq_entries = read_fasta( )               #  STDIN
245    #     @seq_entries = read_fasta( \*FILEHANDLE )
246    #    \@seq_entries = read_fasta( \*FILEHANDLE )
247    #     @seq_entries = read_fasta(  $filename )
248    #    \@seq_entries = read_fasta(  $filename )
249    #-----------------------------------------------------------------------------
250    sub read_fasta {
251        my ( $fh, $name, $close, $unused ) = input_filehandle( $_[0] );
252        $unused && die "Bad reference type (" . ref( $unused ) . ") passed to read_fasta\n";
253    
254      my @seqs = ();      my @seqs = ();
255      my ($id, $desc, $seq) = ("", "", "");      my ($id, $desc, $seq) = ("", "", "");
# Line 169  Line 265 
265              $seq .= $_ ;              $seq .= $_ ;
266          }          }
267      }      }
268        close( $fh ) if $close;
269    
270      if ($id && $seq) { push @seqs, [ $id, $desc, $seq ] }      if ($id && $seq) { push @seqs, [ $id, $desc, $seq ] }
271      return @seqs;      return wantarray ? @seqs : \@seqs;
272  }  }
273    
274    
# Line 179  Line 276 
276  #  Read one fasta sequence at a time from a file.  #  Read one fasta sequence at a time from a file.
277  #  Return the contents as an array:  (id, description, seq)  #  Return the contents as an array:  (id, description, seq)
278  #  #
279  #     @seq_entry = read_next_fasta_seq(*FILEHANDLE)  #     @seq_entry = read_next_fasta_seq( \*FILEHANDLE )
280  #-----------------------------------------------------------------------------  #-----------------------------------------------------------------------------
281  #  Reading always overshoots, so save next id and description  #  Reading always overshoots, so save next id and description
282    
# Line 188  Line 285 
285      my %next_header;      my %next_header;
286    
287      sub read_next_fasta_seq {      sub read_next_fasta_seq {
         wantarray || die "read_next_fasta_seq requires list context\n";  
   
288          my $fh = shift;          my $fh = shift;
289          my ( $id, $desc );          my ( $id, $desc );
290    
# Line 206  Line 301 
301              chomp;              chomp;
302              if ( /^>/ ) {        #  new id              if ( /^>/ ) {        #  new id
303                  $next_header{$fh} = $_;                  $next_header{$fh} = $_;
304                  if ( defined($id) && $seq ) { return ($id, $desc, $seq) }                  if ( defined($id) && $seq )
305                    {
306                        return wantarray ? ($id, $desc, $seq) : [$id, $desc, $seq]
307                    }
308                  ( $id, $desc ) = parse_fasta_title( $next_header{$fh} );                  ( $id, $desc ) = parse_fasta_title( $next_header{$fh} );
309                  $seq = "";                  $seq = "";
310              }              }
# Line 219  Line 317 
317          #  Done with file, delete "next header"          #  Done with file, delete "next header"
318    
319          delete $next_header{$fh};          delete $next_header{$fh};
320          return (defined($id) && $seq) ? ($id, $desc, $seq) : () ;          return ( defined($id) && $seq ) ? ( wantarray ? ($id, $desc, $seq)
321                                                          : [$id, $desc, $seq]
322                                              )
323                                            : () ;
324      }      }
325  }  }
326    
327    
328  #-----------------------------------------------------------------------------  #-----------------------------------------------------------------------------
329    #  Read a clustal alignment from a file.
330    #  Save the contents in a list of refs to arrays:  (id, description, seq)
331    #
332    #     @seq_entries = read_clustal_file( $filename )
333    #-----------------------------------------------------------------------------
334    sub read_clustal_file { read_clustal( @_ ) }
335    
336    
337    #-----------------------------------------------------------------------------
338    #  Read a clustal alignment.
339    #  Save the contents in a list of refs to arrays:  (id, description, seq)
340    #
341    #     @seq_entries = read_clustal( )              # STDIN
342    #     @seq_entries = read_clustal( \*FILEHANDLE )
343    #     @seq_entries = read_clustal(  $filename )
344    #-----------------------------------------------------------------------------
345    sub read_clustal {
346        my ( $fh, undef, $close, $unused ) = input_filehandle( shift );
347        $unused && die "Bad reference type (" . ref( $unused ) . ") passed to read_clustal_file\n";
348    
349        my ( %seq, @ids, $line );
350        while ( defined( $line = <$fh> ) )
351        {
352            ( $line =~ /^[A-Za-z0-9]/ ) or next;
353            chomp $line;
354            my @flds = split /\s+/, $line;
355            if ( @flds == 2 )
356            {
357                $seq{ $flds[0] } or push @ids, $flds[0];
358                push @{ $seq{ $flds[0] } }, $flds[1];
359            }
360        }
361        close( $fh ) if $close;
362    
363        map { [ $_, "", join( "", @{$seq{$_}} ) ] } @ids;
364    }
365    
366    
367    #-----------------------------------------------------------------------------
368  #  Parse a fasta file header to id and definition parts  #  Parse a fasta file header to id and definition parts
369  #  #
370  #     ($id, $def) = parse_fasta_title( $title )  #     ($id, $def) = parse_fasta_title( $title )
# Line 250  Line 390 
390    
391    
392  #-----------------------------------------------------------------------------  #-----------------------------------------------------------------------------
393  #  Print list of sequence entries in fasta format  #  Helper function for defining an output filehandle:
394    #     filehandle is passed through
395    #     string is taken as file name to be openend
396    #     undef or "" defaults to STDOUT
397    #
398    #    ( \*FH, $name, $close [, $file] ) = output_filehandle( $file );
399  #  #
 #     print_seq_list_as_fasta(*FILEHANDLE, @seq_entry_list);  
400  #-----------------------------------------------------------------------------  #-----------------------------------------------------------------------------
401  sub print_seq_list_as_fasta {  sub output_filehandle
402      my $fh = shift;  {
403      my @seq_list = @_;      my $file = shift;
404    
405        #  FILEHANDLE
406    
407        return ( $file, $file, 0 ) if ( ref( $file ) eq "GLOB" );
408    
409        #  Null string or undef
410    
411        return ( \*STDOUT, "", 0 ) if ( ! defined( $file ) || ( $file eq "" ) );
412    
413        #  File name
414    
415        if ( ! ref( $file ) )
416        {
417            my $fh;
418            open( $fh, ">$file" ) || die "Could not open output $file\n";
419            return ( $fh, $file, 1 );
420        }
421    
422        #  Some other kind of reference; return the unused value
423    
424        return ( \*STDOUT, undef, 0, $file );
425    }
426    
427    
428    #-----------------------------------------------------------------------------
429    #  Legacy function for printing fasta sequence set:
430    #
431    #     print_seq_list_as_fasta( \*FILEHANDLE, @seq_entry_list );
432    #-----------------------------------------------------------------------------
433    sub print_seq_list_as_fasta { print_alignment_as_fasta( @_ ) }
434    
435    
436    #-----------------------------------------------------------------------------
437    #  Print list of sequence entries in fasta format.
438    #  Missing, undef or "" filename defaults to STDOUT.
439    #
440    #     print_alignment_as_fasta(                @seq_entry_list ); # STDOUT
441    #     print_alignment_as_fasta(               \@seq_entry_list ); # STDOUT
442    #     print_alignment_as_fasta( \*FILEHANDLE,  @seq_entry_list );
443    #     print_alignment_as_fasta( \*FILEHANDLE, \@seq_entry_list );
444    #     print_alignment_as_fasta(  $filename,    @seq_entry_list );
445    #     print_alignment_as_fasta(  $filename,   \@seq_entry_list );
446    #-----------------------------------------------------------------------------
447    sub print_alignment_as_fasta {
448        my ( $fh, undef, $close, $unused ) = output_filehandle( shift );
449        ( unshift @_, $unused ) if $unused;
450    
451        ( ref( $_[0] ) eq "ARRAY" ) or confess "Bad sequence entry passed to print_alignment_as_fasta\n";
452    
453        #  Expand the sequence entry list if necessary:
454    
455        if ( ref( $_[0]->[0] ) eq "ARRAY" ) { @_ = @{ $_[0] } }
456    
457        foreach my $seq_ptr ( @_ ) { print_seq_as_fasta( $fh, @$seq_ptr ) }
458    
459        close( $fh ) if $close;
460    }
461    
462    
463    #-----------------------------------------------------------------------------
464    #  Print list of sequence entries in phylip format.
465    #  Missing, undef or "" filename defaults to STDOUT.
466    #
467    #     print_alignment_as_phylip(                @seq_entry_list ); # STDOUT
468    #     print_alignment_as_phylip(               \@seq_entry_list ); # STDOUT
469    #     print_alignment_as_phylip( \*FILEHANDLE,  @seq_entry_list );
470    #     print_alignment_as_phylip( \*FILEHANDLE, \@seq_entry_list );
471    #     print_alignment_as_phylip(  $filename,    @seq_entry_list );
472    #     print_alignment_as_phylip(  $filename,   \@seq_entry_list );
473    #-----------------------------------------------------------------------------
474    sub print_alignment_as_phylip {
475        my ( $fh, undef, $close, $unused ) = output_filehandle( shift );
476        ( unshift @_, $unused ) if $unused;
477    
478        ( ref( $_[0] ) eq "ARRAY" ) or die die "Bad sequence entry passed to print_alignment_as_phylip\n";
479    
480        my @seq_list = ( ref( $_[0]->[0] ) eq "ARRAY" ) ? @{ $_[0] } : @_;
481    
482        my ( %id2, %used );
483        my $maxlen = 0;
484        foreach ( @seq_list )
485        {
486            my ( $id, undef, $seq ) = @$_;
487    
488            #  Need a name that is unique within 10 characters
489    
490            my $id2 = substr( $id, 0, 10 );
491            $id2 =~ s/_/ /g;  # PHYLIP sequence files accept spaces
492            my $n = "0";
493            while ( $used{ $id2 } )
494            {
495                $n++;
496                $id2 = substr( $id, 0, 10 - length( $n ) ) . $n;
497            }
498            $used{ $id2 } = 1;
499            $id2{ $id } = $id2;
500    
501                    #  Prepare to pad sequences (should not be necessary, but ...)
502    
503            my $len = length( $seq );
504            $maxlen = $len if ( $len > $maxlen );
505        }
506    
507        my $nseq = @seq_list;
508        print $fh "$nseq  $maxlen\n";
509        foreach ( @seq_list )
510        {
511            my ( $id, undef, $seq ) = @$_;
512            my $len = length( $seq );
513            printf $fh "%-10s  %s%s\n", $id2{ $id },
514                                        $seq,
515                                        $len<$maxlen ? ("?" x ($maxlen-$len)) : "";
516        }
517    
518        close( $fh ) if $close;
519    }
520    
521    
522    #-----------------------------------------------------------------------------
523    #  Print list of sequence entries in nexus format.
524    #  Missing, undef or "" filename defaults to STDOUT.
525    #
526    #     print_alignment_as_nexus(               [ \%label_hash, ]  @seq_entry_list );
527    #     print_alignment_as_nexus(               [ \%label_hash, ] \@seq_entry_list );
528    #     print_alignment_as_nexus( \*FILEHANDLE, [ \%label_hash, ]  @seq_entry_list );
529    #     print_alignment_as_nexus( \*FILEHANDLE, [ \%label_hash, ] \@seq_entry_list );
530    #     print_alignment_as_nexus(  $filename,   [ \%label_hash, ]  @seq_entry_list );
531    #     print_alignment_as_nexus(  $filename,   [ \%label_hash, ] \@seq_entry_list );
532    #-----------------------------------------------------------------------------
533    sub print_alignment_as_nexus {
534        my ( $fh, undef, $close, $unused ) = output_filehandle( shift );
535        ( unshift @_, $unused ) if $unused;
536    
537      foreach my $seq_ptr (@seq_list) {      my $lbls = ( ref( $_[0] ) eq "HASH" ) ? shift : undef;
538          print_seq_as_fasta($fh, @$seq_ptr);  
539        ( ref( $_[0] ) eq "ARRAY" ) or die "Bad sequence entry passed to print_alignment_as_nexus\n";
540    
541        my @seq_list = ( ref( $_[0]->[0] ) eq "ARRAY" ) ? @{ $_[0] } : @_;
542    
543        my %id2;
544        my ( $maxidlen, $maxseqlen ) = ( 0, 0 );
545        my ( $n1, $n2, $nt, $nu ) = ( 0, 0, 0, 0 );
546        foreach ( @seq_list )
547        {
548            my ( $id, undef, $seq ) = @$_;
549            my $id2 = $lbls ? ( $lbls->{ $id } || $id ) : $id;
550            if ( $id2 !~ /^[-+.0-9A-Za-z~_|]+$/ )
551            {
552                    $id2 =~ s/'/''/g;
553                    $id2 = qq('$id2');
554                }
555            $id2{ $id } = $id2;
556            my $idlen = length( $id2 );
557            $maxidlen = $idlen if ( $idlen > $maxidlen );
558    
559            my $seqlen = length( $seq );
560            $maxseqlen = $seqlen if ( $seqlen > $maxseqlen );
561    
562            $nt += $seq =~ tr/Tt//d;
563            $nu += $seq =~ tr/Uu//d;
564            $n1 += $seq =~ tr/ACGNacgn//d;
565            $n2 += $seq =~ tr/A-Za-z//d;
566        }
567    
568        my $nseq = @seq_list;
569        my $type = ( $n1 < 2 * $n2 ) ?  'protein' : ($nt>$nu) ? 'DNA' : 'RNA';
570    
571        print $fh <<"END_HEAD";
572    #NEXUS
573    
574    BEGIN Data;
575        Dimensions
576            NTax=$nseq
577            NChar=$maxseqlen
578            ;
579        Format
580            DataType=$type
581            Gap=-
582            Missing=?
583            ;
584        Matrix
585    
586    END_HEAD
587    
588        foreach ( @seq_list )
589        {
590            my ( $id, undef, $seq ) = @$_;
591            my $len = length( $seq );
592            printf  $fh  "%-${maxidlen}s  %s%s\n",
593                         $id2{ $id },
594                         $seq,
595                         $len<$maxseqlen ? ("?" x ($maxseqlen-$len)) : "";
596      }      }
597    
598        print $fh <<"END_TAIL";
599    ;
600    END;
601    END_TAIL
602    
603        close( $fh ) if $close;
604  }  }
605    
606    
607  #-----------------------------------------------------------------------------  #-----------------------------------------------------------------------------
608  #  Print one sequence in fasta format to an open file  #  Print one sequence in fasta format to an open file
609  #  #
610  #     print_seq_as_fasta(*FILEHANDLE, $id, $desc, $seq);  #     print_seq_as_fasta( \*FILEHANDLE, $id, $desc, $seq );
611  #     print_seq_as_fasta(*FILEHANDLE, @seq_entry);  #     print_seq_as_fasta( \*FILEHANDLE, @seq_entry );
612  #-----------------------------------------------------------------------------  #-----------------------------------------------------------------------------
613  sub print_seq_as_fasta {  sub print_seq_as_fasta {
614      my $fh = shift;      my $fh = shift;
# Line 285  Line 625 
625  #-----------------------------------------------------------------------------  #-----------------------------------------------------------------------------
626  #  Print one sequence in GenBank flat file format:  #  Print one sequence in GenBank flat file format:
627  #  #
628  #     print_gb_locus( *FILEHANDLE, $locus, $def, $accession, $seq )  #     print_gb_locus( \*FILEHANDLE, $locus, $def, $accession, $seq )
629  #-----------------------------------------------------------------------------  #-----------------------------------------------------------------------------
630  sub print_gb_locus {  sub print_gb_locus {
631      my ($fh, $loc, $def, $acc, $seq) = @_;      my ($fh, $loc, $def, $acc, $seq) = @_;
# Line 311  Line 651 
651    
652    
653  #-----------------------------------------------------------------------------  #-----------------------------------------------------------------------------
654    #  Return a string with text wrapped to defined line lengths:
655    #
656    #     $wrapped_text = wrap_text( $str )                  # default len   =  80
657    #     $wrapped_text = wrap_text( $str, $len )            # default ind   =   0
658    #     $wrapped_text = wrap_text( $str, $len, $indent )   # default ind_n = ind
659    #     $wrapped_text = wrap_text( $str, $len, $indent_1, $indent_n )
660    #-----------------------------------------------------------------------------
661    sub wrap_text {
662        my ($str, $len, $ind, $indn) = @_;
663    
664        defined($str)  || die "wrap_text called without a string\n";
665        defined($len)  || ($len  =   80);
666        defined($ind)  || ($ind  =    0);
667        ($ind  < $len) || die "wrap error: indent greater than line length\n";
668        defined($indn) || ($indn = $ind);
669        ($indn < $len) || die "wrap error: indent_n greater than line length\n";
670    
671        $str =~ s/\s+$//;
672        $str =~ s/^\s+//;
673        my ($maxchr, $maxchr1);
674        my (@lines) = ();
675    
676        while ($str) {
677            $maxchr1 = ($maxchr = $len - $ind) - 1;
678            if ($maxchr >= length($str)) {
679                push @lines, (" " x $ind) . $str;
680                last;
681            }
682            elsif ($str =~ /^(.{0,$maxchr1}\S)\s+(\S.*)$/) { # no expr in {}
683                push @lines, (" " x $ind) . $1;
684                $str = $2;
685            }
686            elsif ($str =~ /^(.{0,$maxchr1}-)(.*)$/) {
687                push @lines, (" " x $ind) . $1;
688                $str = $2;
689            }
690            else {
691                push @lines, (" " x $ind) . substr($str, 0, $maxchr);
692                $str = substr($str, $maxchr);
693            }
694            $ind = $indn;
695        }
696    
697        return join("\n", @lines);
698    }
699    
700    
701    #-----------------------------------------------------------------------------
702  #  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)
703  #  #
704  #     my \%seq_ind  = index_seq_list(@seq_list);  #     my \%seq_ind  = index_seq_list(@seq_list);
705    #     my \%seq_ind  = index_seq_list( \@seq_list );
706  #  #
707  #  Usage example:  #  Usage example:
708  #  #
709  #  my @seq_list   = read_fasta_seqs(*STDIN);   # list of pointers to entries  #  my  @seq_list   = read_fasta_seqs(\*STDIN);  # list of pointers to entries
710  #  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
711  #  my @chosen_seq = @{%seq_ind{"contig1"}};    # extract one entry  #  my @chosen_seq = @{%seq_ind{"contig1"}};    # extract one entry
712  #  #
713  #-----------------------------------------------------------------------------  #-----------------------------------------------------------------------------
714  sub index_seq_list {  sub index_seq_list {
715      my %seq_index = map { @{$_}[0] => $_ } @_;      ( ref( $_[0] )      ne 'ARRAY' ) ? {}
716      return \%seq_index;    : ( ref( $_[0]->[0] ) ne 'ARRAY' ) ? { map { $_->[0] => $_ } @_ }
717      :                                    { map { $_->[0] => $_ } @{ $_[0] } }
718  }  }
719    
720    
721  #-----------------------------------------------------------------------------  #-----------------------------------------------------------------------------
722  #  Three routines to access all or part of sequence entry by id:  #  Three routines to access all or part of sequence entry by id:
723  #  #
724  #     my @seq_entry  = seq_entry_by_id( \%seq_index, $seq_id );  #     @seq_entry = seq_entry_by_id( \%seq_index, $seq_id );
725  #     my $seq_desc   = seq_desc_by_id(  \%seq_index, $seq_id );  #    \@seq_entry = seq_entry_by_id( \%seq_index, $seq_id );
726  #     my $seq        = seq_data_by_id(  \%seq_index, $seq_id );  #     $seq_desc  = seq_desc_by_id(  \%seq_index, $seq_id );
727    #     $seq       = seq_data_by_id(  \%seq_index, $seq_id );
728  #  #
729  #-----------------------------------------------------------------------------  #-----------------------------------------------------------------------------
730  sub seq_entry_by_id {  sub seq_entry_by_id {
731      (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";
732      (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";
733      wantarray || die "entry_by_id requires list context\n";      return wantarray ? @{ $ind_ref->{$id} } : $ind_ref->{$id};
     return @{ $ind_ref->{$id} };  
734  }  }
735    
736    
# Line 357  Line 747 
747      return ${ $ind_ref->{$id} }[2];      return ${ $ind_ref->{$id} }[2];
748  }  }
749    
750    #-----------------------------------------------------------------------------
751    #  Remove columns of alignment gaps from sequences:
752    #
753    #   @packed_seqs = pack_alignment(  @seqs )
754    #   @packed_seqs = pack_alignment( \@seqs )
755    #  \@packed_seqs = pack_alignment(  @seqs )
756    #  \@packed_seqs = pack_alignment( \@seqs )
757    #
758    #-----------------------------------------------------------------------------
759    
760    sub pack_alignment
761    {
762        my @seqs = ( ref( $_[0] ) eq 'ARRAY' and ref( $_[0]->[0] ) eq 'ARRAY' ) ? @{$_[0] } : @_;
763        @seqs or return wantarray ? () : [];
764    
765        my $mask  = pack_mask( $seqs[0]->[2] );
766        foreach ( @seqs[ 1 .. (@seqs-1) ] )
767        {
768            $mask |= pack_mask( $_->[2] );
769        }
770    
771        my $seq;
772        my @seqs2 = map { $seq = $_->[2] & $mask;
773                          $seq =~ tr/\000//d;
774                          [ $_->[0], $_->[1], $seq ]
775                        }
776                    @seqs;
777    
778        return wantarray ? @seqs2 : \@seqs2;
779    }
780    
781    sub pack_mask
782    {
783        my $mask = shift;
784        $mask =~ tr/-/\000/;
785        $mask =~ tr/\000/\377/c;
786        return $mask;
787    }
788    
789  #-----------------------------------------------------------------------------  #-----------------------------------------------------------------------------
790  #  Some simple sequence manipulations:  #  Some simple sequence manipulations:
# Line 432  Line 860 
860  }  }
861    
862    
863    sub DNA_subseq
864    {
865        my ( $seq, $from, $to ) = @_;
866    
867        my $len = ref( $seq ) eq 'SCALAR' ? length( $$seq )
868                                          : length(  $seq );
869        if ( ( $from eq '$' ) || ( $from eq "" ) ) { $from = $len }
870        if ( ( $to   eq '$' ) || ( ! $to       ) ) { $to   = $len }
871    
872        my $left  = ( $from < $to ) ? $from : $to;
873        my $right = ( $from < $to ) ? $to   : $from;
874        if ( ( $right < 1 ) || ( $left > $len ) ) { return "" }
875        if ( $right > $len ) { $right = $len }
876        if ( $left  < 1    ) { $left  =    1 }
877    
878        my $subseq = ref( $seq ) eq 'SCALAR' ? substr( $$seq, $left-1, $right-$left+1 )
879                                             : substr(  $seq, $left-1, $right-$left+1 );
880    
881        if ( $from > $to )
882        {
883            $subseq = reverse $subseq;
884            $subseq =~ tr[ACGTUKMRSWYBDHVNacgtukmrswybdhvn]
885                         [TGCAAMKYSWRVHDBNtgcaamkyswrvhdbn];
886        }
887    
888        $subseq
889    }
890    
891    
892    sub RNA_subseq
893    {
894        my ( $seq, $from, $to ) = @_;
895    
896        my $len = ref( $seq ) eq 'SCALAR' ? length( $$seq )
897                                          : length(  $seq );
898        if ( ( $from eq '$' ) || ( $from eq "" ) ) { $from = $len }
899        if ( ( $to   eq '$' ) || ( ! $to       ) ) { $to   = $len }
900    
901        my $left  = ( $from < $to ) ? $from : $to;
902        my $right = ( $from < $to ) ? $to   : $from;
903        if ( ( $right < 1 ) || ( $left > $len ) ) { return "" }
904        if ( $right > $len ) { $right = $len }
905        if ( $left  < 1    ) { $left  =    1 }
906    
907        my $subseq = ref( $seq ) eq 'SCALAR' ? substr( $$seq, $left-1, $right-$left+1 )
908                                             : substr(  $seq, $left-1, $right-$left+1 );
909    
910        if ( $from > $to )
911        {
912            $subseq = reverse $subseq;
913            $subseq =~ tr[ACGTUKMRSWYBDHVNacgtukmrswybdhvn]
914                         [UGCAAMKYSWRVHDBNugcaamkyswrvhdbn];
915        }
916    
917        $subseq
918    }
919    
920    
921  sub complement_DNA_entry {  sub complement_DNA_entry {
922      my ($id, $desc, $seq, $fix_id) = @_;      my ($id, $desc, $seq, $fix_id) = @_;
923      $fix_id ||= 0;     #  fix undef values      $fix_id ||= 0;     #  fix undef values
# Line 639  Line 1125 
1125  }  }
1126    
1127    
1128  #  Construct the genetic code with selanocysteine by difference:  #  Construct the genetic code with selenocysteine by difference:
1129    
1130  %genetic_code_with_U = map { $_ => $genetic_code{ $_ } } keys %genetic_code;  %genetic_code_with_U = map { $_ => $genetic_code{ $_ } } keys %genetic_code;
1131  $genetic_code_with_U{ TGA } = "U";  $genetic_code_with_U{ TGA } = "U";
# Line 849  Line 1335 
1335  );  );
1336    
1337    
1338  %one_letter_to_three_letter_aa = {  %one_letter_to_three_letter_aa = (
1339           A  => "Ala", a  => "Ala",           A  => "Ala", a  => "Ala",
1340           B  => "Asx", b  => "Asx",           B  => "Asx", b  => "Asx",
1341           C  => "Cys", c  => "Cys",           C  => "Cys", c  => "Cys",
# Line 875  Line 1361 
1361           Y  => "Tyr", y  => "Tyr",           Y  => "Tyr", y  => "Tyr",
1362           Z  => "Glx", z  => "Glx",           Z  => "Glx", z  => "Glx",
1363          '*' => "***"          '*' => "***"
1364          };          );
1365    
1366    
1367  %three_letter_to_one_letter_aa = (  %three_letter_to_one_letter_aa = (
# Line 1134  Line 1620 
1620  #  Read a list of intervals from a file.  #  Read a list of intervals from a file.
1621  #  Allow id_start_end, or id \s start \s end formats  #  Allow id_start_end, or id \s start \s end formats
1622  #  #
1623  #     @intervals = read_intervals( *FILEHANDLE )  #     @intervals = read_intervals( \*FILEHANDLE )
1624  #-----------------------------------------------------------------------------  #-----------------------------------------------------------------------------
1625  sub read_intervals {  sub read_intervals {
1626      my $fh = shift;      my $fh = shift;
# Line 1230  Line 1716 
1716  #  Read a list of oriented intervals from a file.  #  Read a list of oriented intervals from a file.
1717  #  Allow id_start_end, or id \s start \s end formats  #  Allow id_start_end, or id \s start \s end formats
1718  #  #
1719  #     @intervals = read_oriented_intervals( *FILEHANDLE )  #     @intervals = read_oriented_intervals( \*FILEHANDLE )
1720  #-----------------------------------------------------------------------------  #-----------------------------------------------------------------------------
1721  sub read_oriented_intervals {  sub read_oriented_intervals {
1722      my $fh = shift;      my $fh = shift;
# Line 1261  Line 1747 
1747  }  }
1748    
1749    
1750    #-----------------------------------------------------------------------------
1751    #  Convert GenBank locations to SEED locations
1752    #
1753    #     @seed_locs = gb_location_2_seed( $contig, @gb_locs )
1754    #-----------------------------------------------------------------------------
1755    sub gb_location_2_seed
1756    {
1757        my $contig = shift @_;
1758        $contig or die "First arg of gb_location_2_seed must be contig_id\n";
1759    
1760        map { join( ',', gb_loc_2_seed_2( $contig, $_ ) ) || undef } @_
1761    }
1762    
1763    sub gb_loc_2_seed_2
1764    {
1765        my ( $contig, $loc ) = @_;
1766    
1767        if ( $loc =~ /^(\d+)\.\.(\d+)$/ )
1768        {
1769            join( '_', $contig, $1, $2 )
1770        }
1771    
1772        elsif ( $loc =~ /^join\((.*)\)$/ )
1773        {
1774            $loc = $1;
1775            my $lvl = 0;
1776            for ( my $i = length( $loc )-1; $i >= 0; $i-- )
1777            {
1778                for ( substr( $loc, $i, 1 ) )
1779                {
1780                    /,/ && ! $lvl and substr( $loc, $i, 1 ) = "\t";
1781                    /\(/          and $lvl--;
1782                    /\)/          and $lvl++;
1783                }
1784            }
1785            $lvl == 0 or print STDERR "Paren matching error: $loc\n" and die;
1786            map { gb_loc_2_seed_2( $contig, $_ ) } split /\t/, $loc
1787        }
1788    
1789        elsif ( $loc =~ /^complement\((.*)\)$/ )
1790        {
1791            map { s/_(\d+)_(\d+)$/_$2_$1/; $_ }
1792            reverse
1793            gb_loc_2_seed_2( $contig, $1 )
1794        }
1795    
1796        else
1797        {
1798            ()
1799        }
1800    }
1801    
1802    
1803    #-----------------------------------------------------------------------------
1804    #  Read qual.
1805    #
1806    #  Save the contents in a list of refs to arrays: [ $id, $descript, \@qual ]
1807    #
1808    #     @seq_entries = read_qual( )               #  STDIN
1809    #    \@seq_entries = read_qual( )               #  STDIN
1810    #     @seq_entries = read_qual( \*FILEHANDLE )
1811    #    \@seq_entries = read_qual( \*FILEHANDLE )
1812    #     @seq_entries = read_qual(  $filename )
1813    #    \@seq_entries = read_qual(  $filename )
1814    #-----------------------------------------------------------------------------
1815    sub read_qual {
1816        my ( $fh, $name, $close, $unused ) = input_filehandle( $_[0] );
1817        $unused && die "Bad reference type (" . ref( $unused ) . ") passed to read_qual\n";
1818    
1819        my @quals = ();
1820        my ($id, $desc, $qual) = ("", "", []);
1821    
1822        while ( <$fh> ) {
1823            chomp;
1824            if (/^>\s*(\S+)(\s+(.*))?$/) {        #  new id
1825                if ($id && @$qual) { push @quals, [ $id, $desc, $qual ] }
1826                ($id, $desc, $qual) = ($1, $3 ? $3 : "", []);
1827            }
1828            else {
1829                push @$qual, split;
1830            }
1831        }
1832        close( $fh ) if $close;
1833    
1834        if ($id && @$qual) { push @quals, [ $id, $desc, $qual ] }
1835        return wantarray ? @quals : \@quals;
1836    }
1837    
1838    
1839  1;  1;

Legend:
Removed from v.1.3  
changed lines
  Added in v.1.9

MCS Webmaster
ViewVC Help
Powered by ViewVC 1.0.3