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

Annotation of /FigKernelPackages/gjoseqlib.pm

Parent Directory Parent Directory | Revision Log Revision Log


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

1 : efrank 1.1 package gjoseqlib;
2 :    
3 : golsen 1.2 # A sequence entry is ( $id, $def, $seq )
4 :     # A list of entries is a list of references
5 :     #
6 : overbeek 1.4 # @seq_entry = read_next_fasta_seq( \*FILEHANDLE )
7 :     # @seq_entries = read_fasta_seqs( \*FILEHANDLE ) # Original form
8 :     # @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 : golsen 1.2 #
16 : overbeek 1.4 # $seq_ind = index_seq_list( @seq_entries ); # hash from ids to entries
17 : golsen 1.2 # @seq_entry = seq_entry_by_id( \%seq_index, $seq_id );
18 :     # $seq_desc = seq_desc_by_id( \%seq_index, $seq_id );
19 :     # $seq = seq_data_by_id( \%seq_index, $seq_id );
20 :     #
21 :     # ( $id, $def ) = parse_fasta_title( $title )
22 :     # ( $id, $def ) = split_fasta_title( $title )
23 :     #
24 : overbeek 1.4 # print_seq_list_as_fasta( \*FILEHANDLE, @seq_entry_list ); # Original form
25 :     # print_alignment_as_fasta( @seq_entry_list ); # STDOUT
26 :     # print_alignment_as_fasta( \@seq_entry_list ); # STDOUT
27 :     # 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 : golsen 1.2 #
47 : golsen 1.5 # @seqs = pack_alignment( @seqs )
48 :     #
49 : golsen 1.2 # @entry = subseq_DNA_entry( @seq_entry, $from, $to [, $fix_id] );
50 :     # @entry = subseq_RNA_entry( @seq_entry, $from, $to [, $fix_id] );
51 :     # @entry = complement_DNA_entry( @seq_entry [, $fix_id] );
52 :     # @entry = complement_RNA_entry( @seq_entry [, $fix_id] );
53 :     # $DNAseq = complement_DNA_seq( $NA_seq );
54 :     # $RNAseq = complement_RNA_seq( $NA_seq );
55 :     # $DNAseq = to_DNA_seq( $NA_seq );
56 :     # $RNAseq = to_RNA_seq( $NA_seq );
57 :     # $seq = pack_seq( $sequence )
58 :     # $seq = clean_ae_sequence( $seq )
59 :     #
60 :     # $seq = translate_seq( $seq [, $met_start] )
61 :     # $aa = translate_codon( $triplet );
62 :     # $aa = translate_uc_DNA_codon( $upcase_DNA_triplet );
63 :     #
64 :     # User-supplied genetic code must be upper case index and match the
65 :     # DNA versus RNA type of sequence
66 :     #
67 :     # Locations (= oriented intervals) are ( id, start, end )
68 :     # Intervals are ( id, left, right )
69 :     #
70 : overbeek 1.4 # @intervals = read_intervals( \*FILEHANDLE )
71 :     # @intervals = read_oriented_intervals( \*FILEHANDLE )
72 : golsen 1.2 # @intervals = standardize_intervals( @interval_refs ) # (id, left, right)
73 :     # @joined = join_intervals( @interval_refs )
74 :     # @intervals = locations_2_intervals( @locations )
75 :     # $interval = locations_2_intervals( $location )
76 :     # @reversed = reverse_intervals( @interval_refs ) # (id, end, start)
77 : overbeek 1.4 #
78 :     # Convert GenBank locations to SEED locations
79 :     #
80 :     # @seed_locs = gb_location_2_seed( $contig, @gb_locs )
81 : golsen 1.2
82 :    
83 :     use strict;
84 : efrank 1.1
85 : golsen 1.2 use gjolib qw( wrap_text );
86 : efrank 1.1
87 : golsen 1.2 # Exported global variables:
88 : efrank 1.1
89 : golsen 1.2 our @aa_1_letter_order; # Alpha by 1 letter
90 :     our @aa_3_letter_order; # PAM matrix order
91 :     our @aa_n_codon_order;
92 :     our %genetic_code;
93 :     our %genetic_code_with_U;
94 :     our %amino_acid_codons_DNA;
95 :     our %amino_acid_codons_RNA;
96 :     our %n_codon_for_aa;
97 :     our %reverse_genetic_code_DNA;
98 :     our %reverse_genetic_code_RNA;
99 :     our %DNA_letter_can_be;
100 :     our %RNA_letter_can_be;
101 :     our %one_letter_to_three_letter_aa;
102 :     our %three_letter_to_one_letter_aa;
103 : efrank 1.1
104 :     require Exporter;
105 :    
106 :     our @ISA = qw(Exporter);
107 :     our @EXPORT = qw(
108 :     read_fasta_seqs
109 : overbeek 1.4 read_fasta
110 : efrank 1.1 read_next_fasta_seq
111 : overbeek 1.4 read_clustal_file
112 :     read_clustal
113 : efrank 1.1 parse_fasta_title
114 :     split_fasta_title
115 :     print_seq_list_as_fasta
116 : overbeek 1.4 print_alignment_as_fasta
117 :     print_alignment_as_phylip
118 :     print_alignment_as_nexus
119 : efrank 1.1 print_seq_as_fasta
120 :     print_gb_locus
121 :    
122 :     index_seq_list
123 :     seq_entry_by_id
124 :     seq_desc_by_id
125 :     seq_data_by_id
126 :    
127 : golsen 1.5 pack_alignment
128 :    
129 : efrank 1.1 subseq_DNA_entry
130 :     subseq_RNA_entry
131 :     complement_DNA_entry
132 :     complement_RNA_entry
133 :     complement_DNA_seq
134 :     complement_RNA_seq
135 :     to_DNA_seq
136 :     to_RNA_seq
137 : golsen 1.2 pack_seq
138 : efrank 1.1 clean_ae_sequence
139 :    
140 :     translate_seq
141 :     translate_codon
142 :     translate_seq_with_user_code
143 :    
144 :     read_intervals
145 : golsen 1.2 standardize_intervals
146 : efrank 1.1 join_intervals
147 : golsen 1.2 locations_2_intervals
148 :     read_oriented_intervals
149 :     reverse_intervals
150 : overbeek 1.4
151 :     gb_location_2_seed
152 : efrank 1.1 );
153 :    
154 : golsen 1.2 our @EXPORT_OK = qw(
155 :     @aa_1_letter_order
156 :     @aa_3_letter_order
157 :     @aa_n_codon_order
158 :     %genetic_code
159 :     %genetic_code_with_U
160 :     %amino_acid_codons_DNA
161 :     %amino_acid_codons_RNA
162 :     %n_codon_for_aa
163 :     %reverse_genetic_code_DNA
164 :     %reverse_genetic_code_RNA
165 :     %DNA_letter_can_be
166 :     %RNA_letter_can_be
167 :     %one_letter_to_three_letter_aa
168 :     %three_letter_to_one_letter_aa
169 :     );
170 : efrank 1.1
171 :    
172 :     #-----------------------------------------------------------------------------
173 : overbeek 1.4 # Helper function for defining an input filehandle:
174 :     # filehandle is passed through
175 :     # string is taken as file name to be openend
176 :     # undef or "" defaults to STDOUT
177 :     #
178 :     # ( \*FH, $name, $close [, $file] ) = input_filehandle( $file );
179 :     #
180 :     #-----------------------------------------------------------------------------
181 :     sub input_filehandle
182 :     {
183 :     my $file = shift;
184 :    
185 :     # FILEHANDLE
186 :    
187 :     return ( $file, $file, 0 ) if ( ref( $file ) eq "GLOB" );
188 :    
189 :     # Null string or undef
190 :    
191 :     return ( \*STDIN, "", 0 ) if ( ! defined( $file ) || ( $file eq "" ) );
192 :    
193 :     # File name
194 :    
195 :     if ( ! ref( $file ) )
196 :     {
197 :     my $fh;
198 :     -f $file or die "Could not find input file \"$file\"\n";
199 :     open( $fh, "<$file" ) || die "Could not open \"$file\" for input\n";
200 :     return ( $fh, $file, 1 );
201 :     }
202 :    
203 :     # Some other kind of reference; return the unused value
204 :    
205 :     return ( \*STDIN, undef, 0, $file );
206 :     }
207 :    
208 :    
209 :     #-----------------------------------------------------------------------------
210 :     # Read fasta sequences from a filehandle (legacy interface; use read_fasta)
211 : efrank 1.1 # Save the contents in a list of refs to arrays: (id, description, seq)
212 :     #
213 : overbeek 1.4 # @seq_entries = read_fasta_seqs( \*FILEHANDLE )
214 :     #-----------------------------------------------------------------------------
215 :     sub read_fasta_seqs { read_fasta( @_ ) }
216 :    
217 :    
218 : efrank 1.1 #-----------------------------------------------------------------------------
219 : overbeek 1.4 # Read fasta sequences.
220 :     # Save the contents in a list of refs to arrays: (id, description, seq)
221 :     #
222 :     # @seq_entries = read_fasta( ) # STDIN
223 :     # \@seq_entries = read_fasta( ) # STDIN
224 :     # @seq_entries = read_fasta( \*FILEHANDLE )
225 :     # \@seq_entries = read_fasta( \*FILEHANDLE )
226 :     # @seq_entries = read_fasta( $filename )
227 :     # \@seq_entries = read_fasta( $filename )
228 :     #-----------------------------------------------------------------------------
229 :     sub read_fasta {
230 :     my ( $fh, $name, $close, $unused ) = input_filehandle( $_[0] );
231 :     $unused && die "Bad reference type (" . ref( $unused ) . ") passed to read_fasta\n";
232 : efrank 1.1
233 :     my @seqs = ();
234 :     my ($id, $desc, $seq) = ("", "", "");
235 :    
236 : overbeek 1.4 while ( <$fh> ) {
237 : efrank 1.1 chomp;
238 :     if (/^>\s*(\S+)(\s+(.*))?$/) { # new id
239 :     if ($id && $seq) { push @seqs, [ $id, $desc, $seq ] }
240 :     ($id, $desc, $seq) = ($1, $3 ? $3 : "", "");
241 :     }
242 :     else {
243 : golsen 1.2 tr/ 0-9//d;
244 : efrank 1.1 $seq .= $_ ;
245 :     }
246 :     }
247 : overbeek 1.4 close( $fh ) if $close;
248 : efrank 1.1
249 : overbeek 1.4 if ( $id && $seq ) { push @seqs, [ $id, $desc, $seq ] }
250 :     return wantarray ? @seqs : \@seqs;
251 : efrank 1.1 }
252 :    
253 :    
254 :     #-----------------------------------------------------------------------------
255 :     # Read one fasta sequence at a time from a file.
256 :     # Return the contents as an array: (id, description, seq)
257 :     #
258 : overbeek 1.4 # @seq_entry = read_next_fasta_seq( \*FILEHANDLE )
259 : efrank 1.1 #-----------------------------------------------------------------------------
260 :     # Reading always overshoots, so save next id and description
261 :    
262 : golsen 1.2 { # Use bare block to scope the header hash
263 :    
264 :     my %next_header;
265 :    
266 :     sub read_next_fasta_seq {
267 :     my $fh = shift;
268 :     my ( $id, $desc );
269 : efrank 1.1
270 : golsen 1.2 if ( defined( $next_header{$fh} ) ) {
271 :     ( $id, $desc ) = parse_fasta_title( $next_header{$fh} );
272 : efrank 1.1 }
273 :     else {
274 : golsen 1.2 $next_header{$fh} = "";
275 :     ( $id, $desc ) = ( undef, "" );
276 :     }
277 :     my $seq = "";
278 :    
279 :     while ( <$fh> ) {
280 :     chomp;
281 :     if ( /^>/ ) { # new id
282 :     $next_header{$fh} = $_;
283 : overbeek 1.4 if ( defined($id) && $seq )
284 :     {
285 :     return wantarray ? ($id, $desc, $seq) : [$id, $desc, $seq]
286 :     }
287 : golsen 1.2 ( $id, $desc ) = parse_fasta_title( $next_header{$fh} );
288 :     $seq = "";
289 :     }
290 :     else {
291 :     tr/ 0-9//d;
292 :     $seq .= $_ ;
293 :     }
294 : efrank 1.1 }
295 :    
296 : golsen 1.2 # Done with file, delete "next header"
297 : efrank 1.1
298 : golsen 1.2 delete $next_header{$fh};
299 : overbeek 1.4 return ( defined($id) && $seq ) ? ( wantarray ? ($id, $desc, $seq)
300 :     : [$id, $desc, $seq]
301 :     )
302 :     : () ;
303 : golsen 1.2 }
304 : efrank 1.1 }
305 :    
306 :    
307 :     #-----------------------------------------------------------------------------
308 : overbeek 1.4 # Read a clustal alignment from a file.
309 :     # Save the contents in a list of refs to arrays: (id, description, seq)
310 :     #
311 :     # @seq_entries = read_clustal_file( $filename )
312 :     #-----------------------------------------------------------------------------
313 :     sub read_clustal_file { read_clustal( @_ ) }
314 :    
315 :    
316 :     #-----------------------------------------------------------------------------
317 :     # Read a clustal alignment.
318 :     # Save the contents in a list of refs to arrays: (id, description, seq)
319 :     #
320 :     # @seq_entries = read_clustal( ) # STDIN
321 :     # @seq_entries = read_clustal( \*FILEHANDLE )
322 :     # @seq_entries = read_clustal( $filename )
323 :     #-----------------------------------------------------------------------------
324 :     sub read_clustal {
325 :     my ( $fh, undef, $close, $unused ) = input_filehandle( shift );
326 :     $unused && die "Bad reference type (" . ref( $unused ) . ") passed to read_clustal_file\n";
327 :    
328 :     my ( %seq, @ids, $line );
329 :     while ( defined( $line = <$fh> ) )
330 :     {
331 :     ( $line =~ /^[A-Za-z0-9]/ ) or next;
332 :     chomp $line;
333 :     my @flds = split /\s+/, $line;
334 :     if ( @flds == 2 )
335 :     {
336 :     $seq{ $flds[0] } or push @ids, $flds[0];
337 :     push @{ $seq{ $flds[0] } }, $flds[1];
338 :     }
339 :     }
340 :     close( $fh ) if $close;
341 :    
342 :     map { [ $_, "", join( "", @{$seq{$_}} ) ] } @ids;
343 :     }
344 :    
345 :    
346 :     #-----------------------------------------------------------------------------
347 : efrank 1.1 # Parse a fasta file header to id and definition parts
348 :     #
349 :     # ($id, $def) = parse_fasta_title( $title )
350 :     # ($id, $def) = split_fasta_title( $title )
351 :     #-----------------------------------------------------------------------------
352 :     sub parse_fasta_title {
353 :     my $title = shift;
354 :     chomp;
355 :     if ($title =~ /^>?\s*(\S+)(:?\s+(.*\S)\s*)?$/) {
356 :     return ($1, $3 ? $3 : "");
357 :     }
358 : golsen 1.2 elsif ($title =~ /^>/) {
359 :     return ("", "");
360 :     }
361 : efrank 1.1 else {
362 : golsen 1.2 return (undef, "");
363 : efrank 1.1 }
364 :     }
365 :    
366 :     sub split_fasta_title {
367 :     parse_fasta_title ( shift );
368 :     }
369 :    
370 :    
371 :     #-----------------------------------------------------------------------------
372 : overbeek 1.4 # Helper function for defining an output filehandle:
373 :     # filehandle is passed through
374 :     # string is taken as file name to be openend
375 :     # undef or "" defaults to STDOUT
376 :     #
377 :     # ( \*FH, $name, $close [, $file] ) = output_filehandle( $file );
378 : efrank 1.1 #
379 :     #-----------------------------------------------------------------------------
380 : overbeek 1.4 sub output_filehandle
381 :     {
382 :     my $file = shift;
383 :    
384 :     # FILEHANDLE
385 :    
386 :     return ( $file, $file, 0 ) if ( ref( $file ) eq "GLOB" );
387 :    
388 :     # Null string or undef
389 :    
390 :     return ( \*STDOUT, "", 0 ) if ( ! defined( $file ) || ( $file eq "" ) );
391 :    
392 :     # File name
393 :    
394 :     if ( ! ref( $file ) )
395 :     {
396 :     my $fh;
397 :     open( $fh, ">$file" ) || die "Could not open output $file\n";
398 :     return ( $fh, $file, 1 );
399 :     }
400 :    
401 :     # Some other kind of reference; return the unused value
402 :    
403 :     return ( \*STDOUT, undef, 0, $file );
404 :     }
405 :    
406 :    
407 :     #-----------------------------------------------------------------------------
408 :     # Legacy function for printing fasta sequence set:
409 :     #
410 :     # print_seq_list_as_fasta( \*FILEHANDLE, @seq_entry_list );
411 :     #-----------------------------------------------------------------------------
412 :     sub print_seq_list_as_fasta { print_alignment_as_fasta( @_ ) }
413 :    
414 :    
415 :     #-----------------------------------------------------------------------------
416 :     # Print list of sequence entries in fasta format.
417 :     # Missing, undef or "" filename defaults to STDOUT.
418 :     #
419 :     # print_alignment_as_fasta( @seq_entry_list ); # STDOUT
420 :     # print_alignment_as_fasta( \@seq_entry_list ); # STDOUT
421 :     # print_alignment_as_fasta( \*FILEHANDLE, @seq_entry_list );
422 :     # print_alignment_as_fasta( \*FILEHANDLE, \@seq_entry_list );
423 :     # print_alignment_as_fasta( $filename, @seq_entry_list );
424 :     # print_alignment_as_fasta( $filename, \@seq_entry_list );
425 :     #-----------------------------------------------------------------------------
426 :     sub print_alignment_as_fasta {
427 :     my ( $fh, undef, $close, $unused ) = output_filehandle( shift );
428 :     ( unshift @_, $unused ) if $unused;
429 :    
430 :     ( ref( $_[0] ) eq "ARRAY" ) or die "Bad sequence entry passed to print_alignment_as_fasta\n";
431 :    
432 :     # Expand the sequence entry list if necessary:
433 :    
434 :     if ( ref( $_[0]->[0] ) eq "ARRAY" ) { @_ = @{ $_[0] } }
435 :    
436 :     foreach my $seq_ptr ( @_ ) { print_seq_as_fasta( $fh, @$seq_ptr ) }
437 :    
438 :     close( $fh ) if $close;
439 :     }
440 :    
441 :    
442 :     #-----------------------------------------------------------------------------
443 :     # Print list of sequence entries in phylip format.
444 :     # Missing, undef or "" filename defaults to STDOUT.
445 :     #
446 :     # print_alignment_as_phylip( @seq_entry_list ); # STDOUT
447 :     # print_alignment_as_phylip( \@seq_entry_list ); # STDOUT
448 :     # print_alignment_as_phylip( \*FILEHANDLE, @seq_entry_list );
449 :     # print_alignment_as_phylip( \*FILEHANDLE, \@seq_entry_list );
450 :     # print_alignment_as_phylip( $filename, @seq_entry_list );
451 :     # print_alignment_as_phylip( $filename, \@seq_entry_list );
452 :     #-----------------------------------------------------------------------------
453 :     sub print_alignment_as_phylip {
454 :     my ( $fh, undef, $close, $unused ) = output_filehandle( shift );
455 :     ( unshift @_, $unused ) if $unused;
456 :    
457 :     ( ref( $_[0] ) eq "ARRAY" ) or die die "Bad sequence entry passed to print_alignment_as_phylip\n";
458 :    
459 :     my @seq_list = ( ref( $_[0]->[0] ) eq "ARRAY" ) ? @{ $_[0] } : @_;
460 :    
461 :     my ( %id2, %used );
462 :     my $maxlen = 0;
463 :     foreach ( @seq_list )
464 :     {
465 :     my ( $id, undef, $seq ) = @$_;
466 :    
467 :     # Need a name that is unique within 10 characters
468 :    
469 :     my $id2 = substr( $id, 0, 10 );
470 :     $id2 =~ s/_/ /g; # PHYLIP sequence files accept spaces
471 :     my $n = "0";
472 :     while ( $used{ $id2 } )
473 :     {
474 :     $n++;
475 :     $id2 = substr( $id, 0, 10 - length( $n ) ) . $n;
476 :     }
477 :     $used{ $id2 } = 1;
478 :     $id2{ $id } = $id2;
479 :    
480 :     # Prepare to pad sequences (should not be necessary, but ...)
481 :    
482 :     my $len = length( $seq );
483 :     $maxlen = $len if ( $len > $maxlen );
484 :     }
485 : efrank 1.1
486 : overbeek 1.4 my $nseq = @seq_list;
487 :     print $fh "$nseq $maxlen\n";
488 :     foreach ( @seq_list )
489 :     {
490 :     my ( $id, undef, $seq ) = @$_;
491 :     my $len = length( $seq );
492 :     printf $fh "%-10s %s%s\n", $id2{ $id },
493 :     $seq,
494 :     $len<$maxlen ? ("?" x ($maxlen-$len)) : "";
495 : efrank 1.1 }
496 : overbeek 1.4
497 :     close( $fh ) if $close;
498 :     }
499 :    
500 :    
501 :     #-----------------------------------------------------------------------------
502 :     # Print list of sequence entries in nexus format.
503 :     # Missing, undef or "" filename defaults to STDOUT.
504 :     #
505 :     # print_alignment_as_nexus( [ \%label_hash, ] @seq_entry_list );
506 :     # print_alignment_as_nexus( [ \%label_hash, ] \@seq_entry_list );
507 :     # print_alignment_as_nexus( \*FILEHANDLE, [ \%label_hash, ] @seq_entry_list );
508 :     # print_alignment_as_nexus( \*FILEHANDLE, [ \%label_hash, ] \@seq_entry_list );
509 :     # print_alignment_as_nexus( $filename, [ \%label_hash, ] @seq_entry_list );
510 :     # print_alignment_as_nexus( $filename, [ \%label_hash, ] \@seq_entry_list );
511 :     #-----------------------------------------------------------------------------
512 :     sub print_alignment_as_nexus {
513 :     my ( $fh, undef, $close, $unused ) = output_filehandle( shift );
514 :     ( unshift @_, $unused ) if $unused;
515 :    
516 :     my $lbls = ( ref( $_[0] ) eq "HASH" ) ? shift : undef;
517 :    
518 :     ( ref( $_[0] ) eq "ARRAY" ) or die "Bad sequence entry passed to print_alignment_as_nexus\n";
519 :    
520 :     my @seq_list = ( ref( $_[0]->[0] ) eq "ARRAY" ) ? @{ $_[0] } : @_;
521 :    
522 :     my %id2;
523 :     my ( $maxidlen, $maxseqlen ) = ( 0, 0 );
524 :     my ( $n1, $n2, $nt, $nu ) = ( 0, 0, 0, 0 );
525 :     foreach ( @seq_list )
526 :     {
527 :     my ( $id, undef, $seq ) = @$_;
528 :     my $id2 = $lbls ? ( $lbls->{ $id } || $id ) : $id;
529 :     if ( $id2 !~ /^[-+.0-9A-Za-z~_|]+$/ )
530 :     {
531 :     $id2 =~ s/'/''/g;
532 :     $id2 = qq('$id2');
533 :     }
534 :     $id2{ $id } = $id2;
535 :     my $idlen = length( $id2 );
536 :     $maxidlen = $idlen if ( $idlen > $maxidlen );
537 :    
538 :     my $seqlen = length( $seq );
539 :     $maxseqlen = $seqlen if ( $seqlen > $maxseqlen );
540 :    
541 :     $nt += $seq =~ tr/Tt//d;
542 :     $nu += $seq =~ tr/Uu//d;
543 :     $n1 += $seq =~ tr/ACGNacgn//d;
544 :     $n2 += $seq =~ tr/A-Za-z//d;
545 :     }
546 :    
547 :     my $nseq = @seq_list;
548 :     my $type = ( $n1 < 2 * $n2 ) ? 'protein' : ($nt>$nu) ? 'DNA' : 'RNA';
549 :    
550 :     print $fh <<"END_HEAD";
551 :     #NEXUS
552 :    
553 :     BEGIN Data;
554 :     Dimensions
555 :     NTax=$nseq
556 :     NChar=$maxseqlen
557 :     ;
558 :     Format
559 :     DataType=$type
560 :     Gap=-
561 :     Missing=?
562 :     ;
563 :     Matrix
564 :    
565 :     END_HEAD
566 :    
567 :     foreach ( @seq_list )
568 :     {
569 :     my ( $id, undef, $seq ) = @$_;
570 :     my $len = length( $seq );
571 :     printf $fh "%-${maxidlen}s %s%s\n",
572 :     $id2{ $id },
573 :     $seq,
574 :     $len<$maxseqlen ? ("?" x ($maxseqlen-$len)) : "";
575 :     }
576 :    
577 :     print $fh <<"END_TAIL";
578 :     ;
579 :     END;
580 :     END_TAIL
581 :    
582 :     close( $fh ) if $close;
583 : efrank 1.1 }
584 :    
585 :    
586 :     #-----------------------------------------------------------------------------
587 :     # Print one sequence in fasta format to an open file
588 :     #
589 : overbeek 1.4 # print_seq_as_fasta( \*FILEHANDLE, $id, $desc, $seq );
590 :     # print_seq_as_fasta( \*FILEHANDLE, @seq_entry );
591 : efrank 1.1 #-----------------------------------------------------------------------------
592 :     sub print_seq_as_fasta {
593 :     my $fh = shift;
594 :     my ($id, $desc, $seq) = @_;
595 :    
596 :     printf $fh ($desc) ? ">$id $desc\n" : ">$id\n";
597 :     my $len = length($seq);
598 :     for (my $i = 0; $i < $len; $i += 60) {
599 :     print $fh substr($seq, $i, 60) . "\n";
600 :     }
601 :     }
602 :    
603 :    
604 :     #-----------------------------------------------------------------------------
605 :     # Print one sequence in GenBank flat file format:
606 :     #
607 : overbeek 1.4 # print_gb_locus( \*FILEHANDLE, $locus, $def, $accession, $seq )
608 : efrank 1.1 #-----------------------------------------------------------------------------
609 :     sub print_gb_locus {
610 :     my ($fh, $loc, $def, $acc, $seq) = @_;
611 :     my ($len, $i, $imax);
612 :     my $istep = 10;
613 :    
614 :     $len = length($seq);
615 :     printf $fh "LOCUS %-10s%7d bp\n", substr($loc,0,10), $len;
616 :     print $fh "DEFINITION " . substr(wrap_text($def,80,12), 12) . "\n";
617 :     if ($acc) { print $fh "ACCESSION $acc\n" }
618 :     print $fh "ORIGIN\n";
619 :    
620 :     for ($i = 1; $i <= $len; ) {
621 :     printf $fh "%9d", $i;
622 :     $imax = $i + 59; if ($imax > $len) { $imax = $len }
623 :     for ( ; $i <= $imax; $i += $istep) {
624 :     print $fh " " . substr($seq, $i-1, $istep);
625 :     }
626 :     print $fh "\n";
627 :     }
628 :     print $fh "//\n";
629 :     }
630 :    
631 :    
632 :     #-----------------------------------------------------------------------------
633 :     # Build an index from seq_id to pointer to sequence entry: (id, desc, seq)
634 :     #
635 : overbeek 1.4 # my \%seq_ind = index_seq_list( @seq_list );
636 :     # my \%seq_ind = index_seq_list( \@seq_list );
637 : efrank 1.1 #
638 :     # Usage example:
639 :     #
640 : overbeek 1.4 # my @seq_list = read_fasta_seqs(\*STDIN); # list of pointers to entries
641 :     # my \%seq_ind = index_seq_list(@seq_list); # hash from names to pointers
642 :     # my @chosen_seq = @{%seq_ind{"contig1"}}; # extract one entry
643 : efrank 1.1 #
644 :     #-----------------------------------------------------------------------------
645 :     sub index_seq_list {
646 : overbeek 1.4 ( ref( $_[0] ) ne 'ARRAY' ) ? {}
647 :     : ( ref( $_[0]->[0] ) ne 'ARRAY' ) ? { map { $_->[0] => $_ } @_ }
648 :     : { map { $_->[0] => $_ } @{ $_[0] } }
649 : efrank 1.1 }
650 :    
651 :    
652 :     #-----------------------------------------------------------------------------
653 :     # Three routines to access all or part of sequence entry by id:
654 :     #
655 : overbeek 1.4 # @seq_entry = seq_entry_by_id( \%seq_index, $seq_id );
656 :     # \@seq_entry = seq_entry_by_id( \%seq_index, $seq_id );
657 :     # $seq_desc = seq_desc_by_id( \%seq_index, $seq_id );
658 :     # $seq = seq_data_by_id( \%seq_index, $seq_id );
659 : efrank 1.1 #
660 :     #-----------------------------------------------------------------------------
661 :     sub seq_entry_by_id {
662 :     (my $ind_ref = shift) || die "No index supplied to seq_entry_by_id\n";
663 :     (my $id = shift) || die "No id supplied to seq_entry_by_id\n";
664 : overbeek 1.4 return wantarray ? @{ $ind_ref->{$id} } : $ind_ref->{$id};
665 : efrank 1.1 }
666 :    
667 :    
668 :     sub seq_desc_by_id {
669 :     (my $ind_ref = shift) || die "No index supplied to seq_desc_by_id\n";
670 :     (my $id = shift) || die "No id supplied to seq_desc_by_id\n";
671 :     return ${ $ind_ref->{$id} }[1];
672 :     }
673 :    
674 :    
675 :     sub seq_data_by_id {
676 :     (my $ind_ref = shift) || die "No index supplied to seq_data_by_id\n";
677 :     (my $id = shift) || die "No id supplied to seq_data_by_id\n";
678 :     return ${ $ind_ref->{$id} }[2];
679 :     }
680 :    
681 : golsen 1.5 #-----------------------------------------------------------------------------
682 :     # Remove columns of alignment gaps from sequences:
683 :     #
684 :     # @packed_seqs = pack_alignment( @seqs )
685 :     #
686 :     #-----------------------------------------------------------------------------
687 :    
688 :     sub pack_alignment
689 :     {
690 :     my @seqs = ( ref( $_[0] ) eq 'ARRAY' and ref( $_[0]->[0] ) eq 'ARRAY' ) ? @{$_[0] } : @_;
691 :     @seqs or return wantarray ? () : [];
692 :    
693 :     my $mask = pack_mask( $seqs[0]->[2] );
694 :     foreach ( @seqs[ 1 .. (@seqs-1) ] )
695 :     {
696 :     $mask |= pack_mask( $_->[2] );
697 :     }
698 :    
699 :     my $seq;
700 :     my @seqs2 = map { $seq = $_->[2] & $mask;
701 :     $seq =~ tr/\000//d;
702 :     [ $_->[0], $_->[1], $seq ]
703 :     }
704 :     @seqs;
705 :    
706 :     return wantarray ? @seqs2 : \@seqs2;
707 :     }
708 :    
709 :     sub pack_mask
710 :     {
711 :     my $mask = shift;
712 :     $mask =~ tr/-/\000/;
713 :     $mask =~ tr/\000/\377/c;
714 :     return $mask;
715 :     }
716 : efrank 1.1
717 :     #-----------------------------------------------------------------------------
718 :     # Some simple sequence manipulations:
719 :     #
720 :     # @entry = subseq_DNA_entry( @seq_entry, $from, $to [, $fix_id] );
721 :     # @entry = subseq_RNA_entry( @seq_entry, $from, $to [, $fix_id] );
722 :     # @entry = complement_DNA_entry( @seq_entry [, $fix_id] );
723 :     # @entry = complement_RNA_entry( @seq_entry [, $fix_id] );
724 :     # $DNAseq = complement_DNA_seq( $NA_seq );
725 :     # $RNAseq = complement_RNA_seq( $NA_seq );
726 :     # $DNAseq = to_DNA_seq( $NA_seq );
727 :     # $RNAseq = to_RNA_seq( $NA_seq );
728 :     #
729 :     #-----------------------------------------------------------------------------
730 :    
731 :     sub subseq_DNA_entry {
732 :     my ($id, $desc, @rest) = @_;
733 :     wantarray || die "subseq_DNA_entry requires array context\n";
734 :    
735 :     my $seq;
736 :     ($id, $seq) = subseq_nt(1, $id, @rest); # 1 is for DNA, not RNA
737 :     return ($id, $desc, $seq);
738 :     }
739 :    
740 :    
741 :     sub subseq_RNA_entry {
742 :     my ($id, $desc, @rest) = @_;
743 :     wantarray || die "subseq_RNA_entry requires array context\n";
744 :    
745 :     my $seq;
746 :     ($id, $seq) = subseq_nt(0, $id, @rest); # 0 is for not DNA, i.e., RNA
747 :     return ($id, $desc, $seq);
748 :     }
749 :    
750 :    
751 :     sub subseq_nt {
752 :     my ($DNA, $id, $seq, $from, $to, $fix_id) = @_;
753 :     $fix_id ||= 0; # fix undef value
754 :    
755 :     my $len = length($seq);
756 :     if ( ( $from eq '$' ) || ( $from eq "" ) ) { $from = $len }
757 :     if (! $to || ( $to eq '$' ) || ( $to eq "" ) ) { $to = $len }
758 :    
759 :     my $left = ( $from < $to ) ? $from : $to;
760 :     my $right = ( $from < $to ) ? $to : $from;
761 :     if ( ( $right < 1 ) || ( $left > $len ) ) { return ($id, "") }
762 :     if ( $right > $len ) { $right = $len }
763 :     if ( $left < 1 ) { $left = 1 }
764 :    
765 :     $seq = substr($seq, $left-1, $right-$left+1);
766 :     if ( $from > $to ) {
767 :     $seq = reverse $seq;
768 :     if ( $DNA ) {
769 :     $seq =~ tr[ACGTUKMRSWYBDHVNacgtukmrswybdhvn]
770 :     [TGCAAMKYSWRVHDBNtgcaamkyswrvhdbn];
771 :     }
772 :     else {
773 :     $seq =~ tr[ACGTUKMRSWYBDHVNacgtukmrswybdhvn]
774 :     [UGCAAMKYSWRVHDBNugcaamkyswrvhdbn];
775 :     }
776 :     }
777 :    
778 :     if ( $fix_id ) {
779 : golsen 1.2 if ( ( $id =~ s/_(\d+)_(\d+)$// )
780 : efrank 1.1 && ( abs($2-$1)+1 == $len ) ) {
781 :     if ( $1 <= $2 ) { $from += $1 - 1; $to += $1 - 1 }
782 :     else { $from = $1 + 1 - $from; $to = $1 + 1 - $to }
783 :     }
784 :     $id .= "_" . $from . "_" . $to;
785 :     }
786 :    
787 :     return ($id, $seq);
788 :     }
789 :    
790 :    
791 :     sub complement_DNA_entry {
792 :     my ($id, $desc, $seq, $fix_id) = @_;
793 :     $fix_id ||= 0; # fix undef values
794 :    
795 :     wantarray || die "complement_DNA_entry requires array context\n";
796 :     $seq = reverse $seq;
797 :     $seq =~ tr[ACGTUKMRSWYBDHVNacgtukmrswybdhvn]
798 :     [TGCAAMKYSWRVHDBNtgcaamkyswrvhdbn];
799 :     if ($fix_id) {
800 : golsen 1.2 if ($id =~ s/_(\d+)_(\d+)$//) {
801 : efrank 1.1 $id .= "_" . $2 . "_" . $1;
802 :     }
803 :     else {
804 :     $id .= "_" . length($seq) . "_1";
805 :     }
806 :     }
807 :    
808 :     return ($id, $desc, $seq);
809 :     }
810 :    
811 :    
812 :     sub complement_RNA_entry {
813 :     my ($id, $desc, $seq, $fix_id) = @_;
814 :     $fix_id ||= 0; # fix undef values
815 :    
816 :     wantarray || die "complement_DNA_entry requires array context\n";
817 :     $seq = reverse $seq;
818 :     $seq =~ tr[ACGTUKMRSWYBDHVNacgtukmrswybdhvn]
819 :     [UGCAAMKYSWRVHDBNugcaamkyswrvhdbn];
820 :     if ($fix_id) {
821 : golsen 1.2 if ($id =~ s/_(\d+)_(\d+)$//) {
822 : efrank 1.1 $id .= "_" . $2 . "_" . $1;
823 :     }
824 :     else {
825 :     $id .= "_" . length($seq) . "_1";
826 :     }
827 :     }
828 :    
829 :     return ($id, $desc, $seq);
830 :     }
831 :    
832 :    
833 :     sub complement_DNA_seq {
834 :     my $seq = reverse shift;
835 :     $seq =~ tr[ACGTUKMRSWYBDHVNacgtukmrswybdhvn]
836 :     [TGCAAMKYSWRVHDBNtgcaamkyswrvhdbn];
837 :     return $seq;
838 :     }
839 :    
840 :    
841 :     sub complement_RNA_seq {
842 :     my $seq = reverse shift;
843 :     $seq =~ tr[ACGTUKMRSWYBDHVNacgtukmrswybdhvn]
844 :     [UGCAAMKYSWRVHDBNugcaamkyswrvhdbn];
845 :     return $seq;
846 :     }
847 :    
848 :    
849 :     sub to_DNA_seq {
850 :     my $seq = shift;
851 :     $seq =~ tr/Uu/Tt/;
852 :     return $seq;
853 :     }
854 :    
855 :    
856 :     sub to_RNA_seq {
857 :     my $seq = shift;
858 :     $seq =~ tr/Tt/Uu/;
859 :     return $seq;
860 :     }
861 :    
862 :    
863 : golsen 1.2 sub pack_seq {
864 :     my $seq = shift;
865 :     $seq =~ tr/A-Za-z//cd;
866 :     return $seq;
867 :     }
868 :    
869 :    
870 : efrank 1.1 sub clean_ae_sequence {
871 :     $_ = shift;
872 :     $_ = to7bit($_);
873 :     s/[+]/1/g;
874 :     s/[^0-9A-IK-NP-Za-ik-np-z~.-]/-/g;
875 :     return $_;
876 :     }
877 :    
878 :    
879 :     sub to7bit {
880 :     $_ = shift;
881 :     my ($o, $c);
882 :     while (/\\([0-3][0-7][0-7])/) {
883 :     $o = oct($1) % 128;
884 :     $c = sprintf("%c", $o);
885 :     s/\\$1/$c/g;
886 :     }
887 :     return $_;
888 :     }
889 :    
890 :    
891 :     sub to8bit {
892 :     $_ = shift;
893 :     my ($o, $c);
894 :     while (/\\([0-3][0-7][0-7])/) {
895 :     $o = oct($1);
896 :     $c = sprintf("%c", $o);
897 :     s/\\$1/$c/g;
898 :     }
899 :     return $_;
900 :     }
901 :    
902 :    
903 :    
904 :     #-----------------------------------------------------------------------------
905 :     # Translate nucleotides to one letter protein:
906 :     #
907 :     # $seq = translate_seq( $seq [, $met_start] )
908 :     # $aa = translate_codon( $triplet );
909 :     # $aa = translate_uc_DNA_codon( $upcase_DNA_triplet );
910 :     #
911 :     # User-supplied genetic code must be upper case index and match the
912 :     # DNA versus RNA type of sequence
913 :     #
914 :     # $seq = translate_seq_with_user_code( $seq, $gen_code_hash [, $met_start] )
915 :     #
916 :     #-----------------------------------------------------------------------------
917 :    
918 : golsen 1.2 @aa_1_letter_order = qw( A C D E F G H I K L M N P Q R S T V W Y ); # Alpha by 1 letter
919 :     @aa_3_letter_order = qw( A R N D C Q E G H I L K M F P S T W Y V ); # PAM matrix order
920 :     @aa_n_codon_order = qw( L R S A G P T V I C D E F H K N Q Y M W );
921 :    
922 :     %genetic_code = (
923 :    
924 :     # DNA version
925 :    
926 : efrank 1.1 TTT => "F", TCT => "S", TAT => "Y", TGT => "C",
927 :     TTC => "F", TCC => "S", TAC => "Y", TGC => "C",
928 :     TTA => "L", TCA => "S", TAA => "*", TGA => "*",
929 :     TTG => "L", TCG => "S", TAG => "*", TGG => "W",
930 :     CTT => "L", CCT => "P", CAT => "H", CGT => "R",
931 :     CTC => "L", CCC => "P", CAC => "H", CGC => "R",
932 :     CTA => "L", CCA => "P", CAA => "Q", CGA => "R",
933 :     CTG => "L", CCG => "P", CAG => "Q", CGG => "R",
934 :     ATT => "I", ACT => "T", AAT => "N", AGT => "S",
935 :     ATC => "I", ACC => "T", AAC => "N", AGC => "S",
936 :     ATA => "I", ACA => "T", AAA => "K", AGA => "R",
937 :     ATG => "M", ACG => "T", AAG => "K", AGG => "R",
938 :     GTT => "V", GCT => "A", GAT => "D", GGT => "G",
939 :     GTC => "V", GCC => "A", GAC => "D", GGC => "G",
940 :     GTA => "V", GCA => "A", GAA => "E", GGA => "G",
941 :     GTG => "V", GCG => "A", GAG => "E", GGG => "G",
942 :    
943 : golsen 1.2 # RNA suppliment
944 :    
945 :     UUU => "F", UCU => "S", UAU => "Y", UGU => "C",
946 :     UUC => "F", UCC => "S", UAC => "Y", UGC => "C",
947 :     UUA => "L", UCA => "S", UAA => "*", UGA => "*",
948 :     UUG => "L", UCG => "S", UAG => "*", UGG => "W",
949 :     CUU => "L", CCU => "P", CAU => "H", CGU => "R",
950 :     CUC => "L",
951 :     CUA => "L",
952 :     CUG => "L",
953 :     AUU => "I", ACU => "T", AAU => "N", AGU => "S",
954 :     AUC => "I",
955 :     AUA => "I",
956 :     AUG => "M",
957 :     GUU => "V", GCU => "A", GAU => "D", GGU => "G",
958 :     GUC => "V",
959 :     GUA => "V",
960 :     GUG => "V",
961 :    
962 : efrank 1.1 # The following ambiguous encodings are not necessary, but
963 : golsen 1.2 # speed up the processing of some ambiguous triplets:
964 : efrank 1.1
965 :     TTY => "F", TCY => "S", TAY => "Y", TGY => "C",
966 :     TTR => "L", TCR => "S", TAR => "*",
967 :     TCN => "S",
968 :     CTY => "L", CCY => "P", CAY => "H", CGY => "R",
969 :     CTR => "L", CCR => "P", CAR => "Q", CGR => "R",
970 :     CTN => "L", CCN => "P", CGN => "R",
971 :     ATY => "I", ACY => "T", AAY => "N", AGY => "S",
972 :     ACR => "T", AAR => "K", AGR => "R",
973 :     ACN => "T",
974 :     GTY => "V", GCY => "A", GAY => "D", GGY => "G",
975 :     GTR => "V", GCR => "A", GAR => "E", GGR => "G",
976 : golsen 1.2 GTN => "V", GCN => "A", GGN => "G",
977 :    
978 :     UUY => "F", UCY => "S", UAY => "Y", UGY => "C",
979 :     UUR => "L", UCR => "S", UAR => "*",
980 :     UCN => "S",
981 :     CUY => "L",
982 :     CUR => "L",
983 :     CUN => "L",
984 :     AUY => "I",
985 :     GUY => "V",
986 :     GUR => "V",
987 :     GUN => "V"
988 :     );
989 :    
990 :    
991 :     # Add lower case by construction:
992 :    
993 :     foreach ( keys %genetic_code ) {
994 :     $genetic_code{ lc $_ } = lc $genetic_code{ $_ }
995 :     }
996 :    
997 :    
998 :     # Construct the genetic code with selanocysteine by difference:
999 :    
1000 :     %genetic_code_with_U = map { $_ => $genetic_code{ $_ } } keys %genetic_code;
1001 :     $genetic_code_with_U{ TGA } = "U";
1002 :     $genetic_code_with_U{ tga } = "u";
1003 :     $genetic_code_with_U{ UGA } = "U";
1004 :     $genetic_code_with_U{ uga } = "u";
1005 :    
1006 :    
1007 :     %amino_acid_codons_DNA = (
1008 : overbeek 1.4 L => [ qw( TTA TTG CTA CTG CTT CTC ) ],
1009 :     R => [ qw( AGA AGG CGA CGG CGT CGC ) ],
1010 :     S => [ qw( AGT AGC TCA TCG TCT TCC ) ],
1011 :     A => [ qw( GCA GCG GCT GCC ) ],
1012 :     G => [ qw( GGA GGG GGT GGC ) ],
1013 :     P => [ qw( CCA CCG CCT CCC ) ],
1014 :     T => [ qw( ACA ACG ACT ACC ) ],
1015 :     V => [ qw( GTA GTG GTT GTC ) ],
1016 :     I => [ qw( ATA ATT ATC ) ],
1017 :     C => [ qw( TGT TGC ) ],
1018 :     D => [ qw( GAT GAC ) ],
1019 :     E => [ qw( GAA GAG ) ],
1020 :     F => [ qw( TTT TTC ) ],
1021 :     H => [ qw( CAT CAC ) ],
1022 :     K => [ qw( AAA AAG ) ],
1023 :     N => [ qw( AAT AAC ) ],
1024 :     Q => [ qw( CAA CAG ) ],
1025 :     Y => [ qw( TAT TAC ) ],
1026 :     M => [ qw( ATG ) ],
1027 :     U => [ qw( TGA ) ],
1028 :     W => [ qw( TGG ) ],
1029 :    
1030 :     l => [ qw( tta ttg cta ctg ctt ctc ) ],
1031 :     r => [ qw( aga agg cga cgg cgt cgc ) ],
1032 :     s => [ qw( agt agc tca tcg tct tcc ) ],
1033 :     a => [ qw( gca gcg gct gcc ) ],
1034 :     g => [ qw( gga ggg ggt ggc ) ],
1035 :     p => [ qw( cca ccg cct ccc ) ],
1036 :     t => [ qw( aca acg act acc ) ],
1037 :     v => [ qw( gta gtg gtt gtc ) ],
1038 :     i => [ qw( ata att atc ) ],
1039 :     c => [ qw( tgt tgc ) ],
1040 :     d => [ qw( gat gac ) ],
1041 :     e => [ qw( gaa gag ) ],
1042 :     f => [ qw( ttt ttc ) ],
1043 :     h => [ qw( cat cac ) ],
1044 :     k => [ qw( aaa aag ) ],
1045 :     n => [ qw( aat aac ) ],
1046 :     q => [ qw( caa cag ) ],
1047 :     y => [ qw( tat tac ) ],
1048 :     m => [ qw( atg ) ],
1049 :     u => [ qw( tga ) ],
1050 :     w => [ qw( tgg ) ],
1051 : golsen 1.2
1052 : overbeek 1.4 '*' => [ qw( TAA TAG TGA ) ]
1053 : efrank 1.1 );
1054 :    
1055 :    
1056 : golsen 1.2
1057 :     %amino_acid_codons_RNA = (
1058 : overbeek 1.4 L => [ qw( UUA UUG CUA CUG CUU CUC ) ],
1059 :     R => [ qw( AGA AGG CGA CGG CGU CGC ) ],
1060 :     S => [ qw( AGU AGC UCA UCG UCU UCC ) ],
1061 :     A => [ qw( GCA GCG GCU GCC ) ],
1062 :     G => [ qw( GGA GGG GGU GGC ) ],
1063 :     P => [ qw( CCA CCG CCU CCC ) ],
1064 :     T => [ qw( ACA ACG ACU ACC ) ],
1065 :     V => [ qw( GUA GUG GUU GUC ) ],
1066 :     B => [ qw( GAU GAC AAU AAC ) ],
1067 :     Z => [ qw( GAA GAG CAA CAG ) ],
1068 :     I => [ qw( AUA AUU AUC ) ],
1069 :     C => [ qw( UGU UGC ) ],
1070 :     D => [ qw( GAU GAC ) ],
1071 :     E => [ qw( GAA GAG ) ],
1072 :     F => [ qw( UUU UUC ) ],
1073 :     H => [ qw( CAU CAC ) ],
1074 :     K => [ qw( AAA AAG ) ],
1075 :     N => [ qw( AAU AAC ) ],
1076 :     Q => [ qw( CAA CAG ) ],
1077 :     Y => [ qw( UAU UAC ) ],
1078 :     M => [ qw( AUG ) ],
1079 :     U => [ qw( UGA ) ],
1080 :     W => [ qw( UGG ) ],
1081 :    
1082 :     l => [ qw( uua uug cua cug cuu cuc ) ],
1083 :     r => [ qw( aga agg cga cgg cgu cgc ) ],
1084 :     s => [ qw( agu agc uca ucg ucu ucc ) ],
1085 :     a => [ qw( gca gcg gcu gcc ) ],
1086 :     g => [ qw( gga ggg ggu ggc ) ],
1087 :     p => [ qw( cca ccg ccu ccc ) ],
1088 :     t => [ qw( aca acg acu acc ) ],
1089 :     v => [ qw( gua gug guu guc ) ],
1090 :     b => [ qw( gau gac aau aac ) ],
1091 :     z => [ qw( gaa gag caa cag ) ],
1092 :     i => [ qw( aua auu auc ) ],
1093 :     c => [ qw( ugu ugc ) ],
1094 :     d => [ qw( gau gac ) ],
1095 :     e => [ qw( gaa gag ) ],
1096 :     f => [ qw( uuu uuc ) ],
1097 :     h => [ qw( cau cac ) ],
1098 :     k => [ qw( aaa aag ) ],
1099 :     n => [ qw( aau aac ) ],
1100 :     q => [ qw( caa cag ) ],
1101 :     y => [ qw( uau uac ) ],
1102 :     m => [ qw( aug ) ],
1103 :     u => [ qw( uga ) ],
1104 :     w => [ qw( ugg ) ],
1105 : golsen 1.2
1106 : overbeek 1.4 '*' => [ qw( UAA UAG UGA ) ]
1107 : golsen 1.2 );
1108 :    
1109 :    
1110 :     %n_codon_for_aa = map {
1111 :     $_ => scalar @{ $amino_acid_codons_DNA{ $_ } }
1112 :     } keys %amino_acid_codons_DNA;
1113 :    
1114 :    
1115 :     %reverse_genetic_code_DNA = (
1116 : overbeek 1.4 A => "GCN", a => "gcn",
1117 :     C => "TGY", c => "tgy",
1118 :     D => "GAY", d => "gay",
1119 :     E => "GAR", e => "gar",
1120 :     F => "TTY", f => "tty",
1121 :     G => "GGN", g => "ggn",
1122 :     H => "CAY", h => "cay",
1123 :     I => "ATH", i => "ath",
1124 :     K => "AAR", k => "aar",
1125 :     L => "YTN", l => "ytn",
1126 :     M => "ATG", m => "atg",
1127 :     N => "AAY", n => "aay",
1128 :     P => "CCN", p => "ccn",
1129 :     Q => "CAR", q => "car",
1130 :     R => "MGN", r => "mgn",
1131 :     S => "WSN", s => "wsn",
1132 :     T => "ACN", t => "acn",
1133 :     U => "TGA", u => "tga",
1134 :     V => "GTN", v => "gtn",
1135 :     W => "TGG", w => "tgg",
1136 :     X => "NNN", x => "nnn",
1137 :     Y => "TAY", y => "tay",
1138 :     '*' => "TRR"
1139 : golsen 1.2 );
1140 :    
1141 :     %reverse_genetic_code_RNA = (
1142 : overbeek 1.4 A => "GCN", a => "gcn",
1143 :     C => "UGY", c => "ugy",
1144 :     D => "GAY", d => "gay",
1145 :     E => "GAR", e => "gar",
1146 :     F => "UUY", f => "uuy",
1147 :     G => "GGN", g => "ggn",
1148 :     H => "CAY", h => "cay",
1149 :     I => "AUH", i => "auh",
1150 :     K => "AAR", k => "aar",
1151 :     L => "YUN", l => "yun",
1152 :     M => "AUG", m => "aug",
1153 :     N => "AAY", n => "aay",
1154 :     P => "CCN", p => "ccn",
1155 :     Q => "CAR", q => "car",
1156 :     R => "MGN", r => "mgn",
1157 :     S => "WSN", s => "wsn",
1158 :     T => "ACN", t => "acn",
1159 :     U => "UGA", u => "uga",
1160 :     V => "GUN", v => "gun",
1161 :     W => "UGG", w => "ugg",
1162 :     X => "NNN", x => "nnn",
1163 :     Y => "UAY", y => "uay",
1164 :     '*' => "URR"
1165 : golsen 1.2 );
1166 :    
1167 :    
1168 :     %DNA_letter_can_be = (
1169 : efrank 1.1 A => ["A"], a => ["a"],
1170 :     B => ["C", "G", "T"], b => ["c", "g", "t"],
1171 :     C => ["C"], c => ["c"],
1172 :     D => ["A", "G", "T"], d => ["a", "g", "t"],
1173 :     G => ["G"], g => ["g"],
1174 :     H => ["A", "C", "T"], h => ["a", "c", "t"],
1175 :     K => ["G", "T"], k => ["g", "t"],
1176 :     M => ["A", "C"], m => ["a", "c"],
1177 :     N => ["A", "C", "G", "T"], n => ["a", "c", "g", "t"],
1178 :     R => ["A", "G"], r => ["a", "g"],
1179 :     S => ["C", "G"], s => ["c", "g"],
1180 :     T => ["T"], t => ["t"],
1181 :     U => ["T"], u => ["t"],
1182 :     V => ["A", "C", "G"], v => ["a", "c", "g"],
1183 :     W => ["A", "T"], w => ["a", "t"],
1184 :     Y => ["C", "T"], y => ["c", "t"]
1185 :     );
1186 :    
1187 :    
1188 : golsen 1.2 %RNA_letter_can_be = (
1189 : efrank 1.1 A => ["A"], a => ["a"],
1190 :     B => ["C", "G", "U"], b => ["c", "g", "u"],
1191 :     C => ["C"], c => ["c"],
1192 :     D => ["A", "G", "U"], d => ["a", "g", "u"],
1193 :     G => ["G"], g => ["g"],
1194 :     H => ["A", "C", "U"], h => ["a", "c", "u"],
1195 :     K => ["G", "U"], k => ["g", "u"],
1196 :     M => ["A", "C"], m => ["a", "c"],
1197 :     N => ["A", "C", "G", "U"], n => ["a", "c", "g", "u"],
1198 :     R => ["A", "G"], r => ["a", "g"],
1199 :     S => ["C", "G"], s => ["c", "g"],
1200 :     T => ["U"], t => ["u"],
1201 :     U => ["U"], u => ["u"],
1202 :     V => ["A", "C", "G"], v => ["a", "c", "g"],
1203 :     W => ["A", "U"], w => ["a", "u"],
1204 :     Y => ["C", "U"], y => ["c", "u"]
1205 :     );
1206 :    
1207 :    
1208 : overbeek 1.4 %one_letter_to_three_letter_aa = (
1209 :     A => "Ala", a => "Ala",
1210 :     B => "Asx", b => "Asx",
1211 :     C => "Cys", c => "Cys",
1212 :     D => "Asp", d => "Asp",
1213 :     E => "Glu", e => "Glu",
1214 :     F => "Phe", f => "Phe",
1215 :     G => "Gly", g => "Gly",
1216 :     H => "His", h => "His",
1217 :     I => "Ile", i => "Ile",
1218 :     K => "Lys", k => "Lys",
1219 :     L => "Leu", l => "Leu",
1220 :     M => "Met", m => "Met",
1221 :     N => "Asn", n => "Asn",
1222 :     P => "Pro", p => "Pro",
1223 :     Q => "Gln", q => "Gln",
1224 :     R => "Arg", r => "Arg",
1225 :     S => "Ser", s => "Ser",
1226 :     T => "Thr", t => "Thr",
1227 :     U => "Sec", u => "Sec",
1228 :     V => "Val", v => "Val",
1229 :     W => "Trp", w => "Trp",
1230 :     X => "Xxx", x => "Xxx",
1231 :     Y => "Tyr", y => "Tyr",
1232 :     Z => "Glx", z => "Glx",
1233 :     '*' => "***"
1234 :     );
1235 : golsen 1.2
1236 :    
1237 :     %three_letter_to_one_letter_aa = (
1238 :     ALA => "A", Ala => "A", ala => "a",
1239 :     ARG => "R", Arg => "R", arg => "r",
1240 :     ASN => "N", Asn => "N", asn => "n",
1241 :     ASP => "D", Asp => "D", asp => "d",
1242 :     ASX => "B", Asx => "B", asx => "b",
1243 :     CYS => "C", Cys => "C", cys => "c",
1244 :     GLN => "Q", Gln => "Q", gln => "q",
1245 :     GLU => "E", Glu => "E", glu => "e",
1246 :     GLX => "Z", Glx => "Z", glx => "z",
1247 :     GLY => "G", Gly => "G", gly => "g",
1248 :     HIS => "H", His => "H", his => "h",
1249 :     ILE => "I", Ile => "I", ile => "i",
1250 :     LEU => "L", Leu => "L", leu => "l",
1251 :     LYS => "K", Lys => "K", lys => "k",
1252 :     MET => "M", Met => "M", met => "m",
1253 :     PHE => "F", Phe => "F", phe => "f",
1254 :     PRO => "P", Pro => "P", pro => "p",
1255 :     SEC => "U", Sec => "U", sec => "u",
1256 :     SER => "S", Ser => "S", ser => "s",
1257 :     THR => "T", Thr => "T", thr => "t",
1258 :     TRP => "W", Trp => "W", trp => "w",
1259 :     TYR => "Y", Tyr => "Y", tyr => "y",
1260 :     VAL => "V", Val => "V", val => "v",
1261 :     XAA => "X", Xaa => "X", xaa => "x",
1262 :     XXX => "X", Xxx => "X", xxx => "x",
1263 :     '***' => "*"
1264 : efrank 1.1 );
1265 :    
1266 :    
1267 :     #-----------------------------------------------------------------------------
1268 :     # Translate nucleotides to one letter protein:
1269 :     #
1270 :     # $seq = translate_seq( $seq [, $met_start] )
1271 :     #
1272 :     #-----------------------------------------------------------------------------
1273 :    
1274 :     sub translate_seq {
1275 :     my $seq = uc shift;
1276 :     $seq =~ tr/UX/TN/; # make it DNA, and allow X
1277 :     $seq =~ tr/-//d; # remove gaps
1278 :    
1279 :     my $met = shift || 0; # a second argument that is true
1280 :     # forces first amino acid to be Met
1281 :     # (note: undef is false)
1282 :    
1283 :     my $imax = length($seq) - 2; # will try to translate 2 nucleotides!
1284 : golsen 1.2 my $pep = ( ($met && ($imax >= 0)) ? "M" : "" );
1285 : efrank 1.1 for (my $i = $met ? 3 : 0; $i <= $imax; $i += 3) {
1286 :     $pep .= translate_uc_DNA_codon( substr($seq,$i,3) );
1287 :     }
1288 :    
1289 :     return $pep;
1290 :     }
1291 :    
1292 :    
1293 :     #-----------------------------------------------------------------------------
1294 :     # Translate a single triplet with "universal" genetic code
1295 :     # Uppercase and DNA are performed, then translate_uc_DNA_codon
1296 :     # is called.
1297 :     #
1298 :     # $aa = translate_codon( $triplet )
1299 :     #
1300 :     #-----------------------------------------------------------------------------
1301 :    
1302 :     sub translate_codon {
1303 :     my $codon = uc shift; # Make it uppercase
1304 :     $codon =~ tr/UX/TN/; # Make it DNA, and allow X
1305 :     return translate_uc_DNA_codon($codon);
1306 :     }
1307 :    
1308 :    
1309 :     #-----------------------------------------------------------------------------
1310 :     # Translate a single triplet with "universal" genetic code
1311 :     # Uppercase and DNA assumed
1312 :     # Intended for private use by translate_codon and translate_seq
1313 :     #
1314 :     # $aa = translate_uc_DNA_codon( $triplet )
1315 :     #
1316 :     #-----------------------------------------------------------------------------
1317 :    
1318 :     sub translate_uc_DNA_codon {
1319 :     my $codon = shift;
1320 :     my $aa;
1321 :    
1322 :     # Try a simple lookup:
1323 :    
1324 :     if ( $aa = $genetic_code{ $codon } ) { return $aa }
1325 :    
1326 :     # With the expanded code defined above, this catches simple N, R
1327 :     # and Y ambiguities in the third position. Other codes like
1328 :     # GG[KMSWBDHV], or even GG, might be unambiguously translated by
1329 :     # converting the last position to N and seeing if this is in the
1330 :     # (expanded) code table:
1331 :    
1332 :     if ( $aa = $genetic_code{ substr($codon,0,2) . "N" } ) { return $aa }
1333 :    
1334 :     # Test that codon is valid and might have unambiguous aa:
1335 :    
1336 :     if ( $codon !~ m/^[ACGTMY][ACGT][ACGTKMRSWYBDHVN]$/ ) { return "X" }
1337 :     # ^^
1338 :     # |+- for leucine YTR
1339 :     # +-- for arginine MGR
1340 :    
1341 :     # Expand all ambiguous nucleotides to see if they all yield same aa.
1342 :     # Loop order tries to fail quickly with first position change.
1343 :    
1344 :     $aa = "";
1345 :     for my $n2 ( @{ $DNA_letter_can_be{ substr($codon,1,1) } } ) {
1346 :     for my $n3 ( @{ $DNA_letter_can_be{ substr($codon,2,1) } } ) {
1347 :     for my $n1 ( @{ $DNA_letter_can_be{ substr($codon,0,1) } } ) {
1348 :     # set the first value of $aa
1349 :     if ($aa eq "") { $aa = $genetic_code{ $n1 . $n2 . $n3 } }
1350 :     # or break out if any other amino acid is detected
1351 :     elsif ($aa ne $genetic_code{ $n1 . $n2 . $n3 } ) { return "X" }
1352 :     }
1353 :     }
1354 :     }
1355 :    
1356 :     return $aa || "X";
1357 :     }
1358 :    
1359 :    
1360 :     #-----------------------------------------------------------------------------
1361 :     # Translate with a user-supplied genetic code to translate a sequence.
1362 :     # Diagnose the use of upper versus lower, and T versus U in the supplied
1363 :     # code, and transform the supplied nucleotide sequence to match.
1364 :     #
1365 :     # translate_seq_with_user_code($seq, \%gen_code [, $start_with_met] )
1366 :     #
1367 :     #-----------------------------------------------------------------------------
1368 :    
1369 :     sub translate_seq_with_user_code {
1370 :     my $seq = shift;
1371 :     $seq =~ tr/-//d; # remove gaps *** Why?
1372 :     $seq =~ tr/Xx/Nn/; # allow X
1373 :    
1374 :     my $gc = shift; # Reference to hash of DNA alphabet code
1375 :     if (! $gc || ref($gc) ne "HASH") {
1376 :     die "translate_seq_with_user_code needs genetic code hash as secondargument.";
1377 :     }
1378 :    
1379 :     # Test the type of code supplied: uppercase versus lowercase
1380 :    
1381 :     my ($RNA_F, $DNA_F, $M, $N, $X);
1382 :    
1383 :     if ($gc->{ "AAA" }) { # Looks like uppercase code table
1384 :     $seq = uc $seq; # Uppercase sequence
1385 :     $RNA_F = "UUU"; # Uppercase RNA Phe codon
1386 :     $DNA_F = "TTT"; # Uppercase DNA Phe codon
1387 :     $M = "M"; # Uppercase initiator
1388 :     $N = "N"; # Uppercase ambiguous nuc
1389 :     $X = "X"; # Uppercase ambiguous aa
1390 :     }
1391 :     elsif ($gc->{ "aaa" }) { # Looks like lowercase code table
1392 :     $seq = lc $seq; # Lowercase sequence
1393 :     $RNA_F = "uuu"; # Lowercase RNA Phe codon
1394 :     $DNA_F = "ttt"; # Lowercase DNA Phe codon
1395 :     $M = "m"; # Lowercase initiator
1396 :     $N = "n"; # Lowercase ambiguous nuc
1397 :     $X = "x"; # Lowercase ambiguous aa
1398 :     }
1399 :     else {
1400 :     die "User-supplied genetic code does not have aaa or AAA\n";
1401 :     }
1402 :    
1403 :     # Test the type of code supplied: UUU versus TTT
1404 :    
1405 :     my ($ambigs);
1406 :    
1407 :     if ($gc->{ $RNA_F }) { # Looks like RNA code table
1408 :     $seq =~ tr/Tt/Uu/;
1409 :     $ambigs = \%RNA_letter_can_be;
1410 :     }
1411 :     elsif ($gc->{ $DNA_F }) { # Looks like DNA code table
1412 :     $seq =~ tr/Uu/Tt/;
1413 :     $ambigs = \%DNA_letter_can_be;
1414 :     }
1415 :     else {
1416 :     die "User-supplied genetic code does not have $RNA_F or $DNA_F\n";
1417 :     }
1418 :    
1419 :     my $imax = length($seq) - 2; # will try to translate 2 nucleotides!
1420 :    
1421 :     my $met = shift; # a third argument that is true
1422 :     # forces first amino acid to be Met
1423 :     # (note: undef is false)
1424 :     my $pep = ($met && ($imax >= 0)) ? $M : "";
1425 :     my $aa;
1426 :    
1427 :     for (my $i = $met ? 3 : 0; $i <= $imax; $i += 3) {
1428 :     $pep .= translate_codon_with_user_code( substr($seq,$i,3), $gc, $N, $X, $ambigs );
1429 :     }
1430 :    
1431 :     return $pep;
1432 :     }
1433 :    
1434 :    
1435 :     #-----------------------------------------------------------------------------
1436 :     # Translate with user-supplied genetic code hash. For speed, no error
1437 :     # check on the hash. Calling programs should check for the hash at a
1438 :     # higher level.
1439 :     #
1440 :     # Should only be called through translate_seq_with_user_code
1441 :     #
1442 :     # translate_codon_with_user_code( $triplet, \%code, $N, $X, $ambig_table )
1443 :     #
1444 :     # $triplet speaks for itself
1445 :     # $code ref to the hash with the codon translations
1446 :     # $N character to use for ambiguous nucleotide
1447 :     # $X character to use for ambiguous amino acid
1448 :     # $ambig_table ref to hash with lists of nucleotides for each ambig code
1449 :     #-----------------------------------------------------------------------------
1450 :    
1451 :    
1452 :     sub translate_codon_with_user_code {
1453 :     my $codon = shift;
1454 :     my $gc = shift;
1455 :     my $aa;
1456 :    
1457 :     # Try a simple lookup:
1458 :    
1459 :     if ( $aa = $gc->{ $codon } ) { return $aa }
1460 :    
1461 :     # Test that codon is valid and might have unambiguous aa:
1462 :    
1463 :     my ($N, $X, $ambigs) = @_;
1464 :     if ( $codon =~ m/^[ACGTUMY][ACGTU]$/i ) { $codon .= $N }
1465 :     if ( $codon !~ m/^[ACGTUMY][ACGTU][ACGTUKMRSWYBDHVN]$/i ) { return $X }
1466 :     # ^^
1467 :     # |+- for leucine YTR
1468 :     # +-- for arginine MGR
1469 :    
1470 :     # Expand all ambiguous nucleotides to see if they all yield same aa.
1471 :     # Loop order tries to fail quickly with first position change.
1472 :    
1473 :     $aa = "";
1474 :     for my $n2 ( @{ $ambigs->{ substr($codon,1,1) } } ) {
1475 :     for my $n3 ( @{ $ambigs->{ substr($codon,2,1) } } ) {
1476 :     for my $n1 ( @{ $ambigs->{ substr($codon,0,1) } } ) {
1477 :     # set the first value of $aa
1478 :     if ($aa eq "") { $aa = $gc->{ $n1 . $n2 . $n3 } }
1479 :     # break out if any other amino acid is detected
1480 :     elsif ($aa ne $gc->{ $n1 . $n2 . $n3 } ) { return "X" }
1481 :     }
1482 :     }
1483 :     }
1484 :    
1485 :     return $aa || $X;
1486 :     }
1487 :    
1488 :    
1489 :     #-----------------------------------------------------------------------------
1490 :     # Read a list of intervals from a file.
1491 :     # Allow id_start_end, or id \s start \s end formats
1492 :     #
1493 : overbeek 1.4 # @intervals = read_intervals( \*FILEHANDLE )
1494 : efrank 1.1 #-----------------------------------------------------------------------------
1495 :     sub read_intervals {
1496 :     my $fh = shift;
1497 :     my @intervals = ();
1498 :    
1499 :     while (<$fh>) {
1500 :     chomp;
1501 :     /^(\S+)_(\d+)_(\d+)(\s.*)?$/ # id_start_end WIT2
1502 :     || /^(\S+)_(\d+)-(\d+)(\s.*)?$/ # id_start-end ???
1503 :     || /^(\S+)=(\d+)=(\d+)(\s.*)?$/ # id=start=end Badger
1504 :     || /^(\S+)\s+(\d+)\s+(\d+)(\s.*)?$/ # id \s start \s end
1505 :     || next;
1506 :    
1507 :     # Matched a pattern. Store reference to (id, left, right):
1508 :     push @intervals, ($2 < $3) ? [ $1, $2+0, $3+0 ]
1509 :     : [ $1, $3+0, $2+0 ];
1510 :     }
1511 :     return @intervals;
1512 :     }
1513 :    
1514 :    
1515 :     #-----------------------------------------------------------------------------
1516 : golsen 1.2 # Convert a list of intervals to read [ id, left_end, right_end ].
1517 :     #
1518 :     # @intervals = standardize_intervals( @interval_refs )
1519 :     #-----------------------------------------------------------------------------
1520 :     sub standardize_intervals {
1521 :     map { ( $_->[1] < $_->[2] ) ? $_ : [ $_->[0], $_->[2], $_->[1] ] } @_;
1522 :     }
1523 :    
1524 :    
1525 :     #-----------------------------------------------------------------------------
1526 : efrank 1.1 # Take the union of a list of intervals
1527 :     #
1528 :     # @joined = join_intervals( @interval_refs )
1529 :     #-----------------------------------------------------------------------------
1530 :     sub join_intervals {
1531 :     my @ordered = sort { $a->[0] cmp $b->[0] # first by id
1532 :     || $a->[1] <=> $b->[1] # next by left end
1533 :     || $b->[2] <=> $a->[2] # finally longest first
1534 :     } @_;
1535 :    
1536 :     my @joined = ();
1537 :     my $n_int = @ordered;
1538 :    
1539 :     my ($cur_id) = "";
1540 :     my ($cur_left) = -1;
1541 :     my ($cur_right) = -1;
1542 :     my ($new_id, $new_left, $new_right);
1543 :    
1544 :     for (my $i = 0; $i < $n_int; $i++) {
1545 :     ($new_id, $new_left, $new_right) = @{$ordered[$i]}; # get the new data
1546 :    
1547 :     if ( ( $new_id ne $cur_id) # if new contig
1548 :     || ( $new_left > $cur_right + 1) # or not touching previous
1549 :     ) { # push the previous interval
1550 :     if ($cur_id) { push (@joined, [ $cur_id, $cur_left, $cur_right ]) }
1551 :     $cur_id = $new_id; # update the current interval
1552 :     $cur_left = $new_left;
1553 :     $cur_right = $new_right;
1554 :     }
1555 :    
1556 :     elsif ($new_right > $cur_right) { # extend the right end if necessary
1557 :     $cur_right = $new_right;
1558 :     }
1559 :     }
1560 :    
1561 :     if ($cur_id) { push (@joined, [$cur_id, $cur_left, $cur_right]) }
1562 :     return @joined;
1563 :     }
1564 :    
1565 : golsen 1.2
1566 :     #-----------------------------------------------------------------------------
1567 :     # Split location strings to oriented intervals.
1568 :     #
1569 :     # @intervals = locations_2_intervals( @locations )
1570 :     # $interval = locations_2_intervals( $location )
1571 :     #-----------------------------------------------------------------------------
1572 :     sub locations_2_intervals {
1573 :     my @intervals = map { /^(\S+)_(\d+)_(\d+)(\s.*)?$/
1574 :     || /^(\S+)_(\d+)-(\d+)(\s.*)?$/
1575 :     || /^(\S+)=(\d+)=(\d+)(\s.*)?$/
1576 :     || /^(\S+)\s+(\d+)\s+(\d+)(\s.*)?$/
1577 :     ? [ $1, $2+0, $3+0 ]
1578 :     : ()
1579 :     } @_;
1580 :    
1581 :     return wantarray ? @intervals : $intervals[0];
1582 :     }
1583 :    
1584 :    
1585 :     #-----------------------------------------------------------------------------
1586 :     # Read a list of oriented intervals from a file.
1587 :     # Allow id_start_end, or id \s start \s end formats
1588 :     #
1589 : overbeek 1.4 # @intervals = read_oriented_intervals( \*FILEHANDLE )
1590 : golsen 1.2 #-----------------------------------------------------------------------------
1591 :     sub read_oriented_intervals {
1592 :     my $fh = shift;
1593 :     my @intervals = ();
1594 :    
1595 :     while (<$fh>) {
1596 :     chomp;
1597 :     /^(\S+)_(\d+)_(\d+)(\s.*)?$/ # id_start_end WIT2
1598 :     || /^(\S+)_(\d+)-(\d+)(\s.*)?$/ # id_start-end ???
1599 :     || /^(\S+)=(\d+)=(\d+)(\s.*)?$/ # id=start=end Badger
1600 :     || /^(\S+)\s+(\d+)\s+(\d+)(\s.*)?$/ # id \s start \s end
1601 :     || next;
1602 :    
1603 :     # Matched a pattern. Store reference to (id, start, end):
1604 :     push @intervals, [ $1, $2+0, $3+0 ];
1605 :     }
1606 :     return @intervals;
1607 :     }
1608 :    
1609 :    
1610 :     #-----------------------------------------------------------------------------
1611 :     # Reverse the orientation of a list of intervals
1612 :     #
1613 :     # @reversed = reverse_intervals( @interval_refs )
1614 :     #-----------------------------------------------------------------------------
1615 :     sub reverse_intervals {
1616 :     map { [ $_->[0], $_->[2], $_->[1] ] } @_;
1617 :     }
1618 :    
1619 :    
1620 : overbeek 1.4 #-----------------------------------------------------------------------------
1621 :     # Convert GenBank locations to SEED locations
1622 :     #
1623 :     # @seed_locs = gb_location_2_seed( $contig, @gb_locs )
1624 :     #-----------------------------------------------------------------------------
1625 :     sub gb_location_2_seed
1626 :     {
1627 :     my $contig = shift @_;
1628 :     $contig or die "First arg of gb_location_2_seed must be contig_id\n";
1629 :    
1630 :     map { join( ',', gb_loc_2_seed_2( $contig, $_ ) ) || undef } @_
1631 :     }
1632 :    
1633 :     sub gb_loc_2_seed_2
1634 :     {
1635 :     my ( $contig, $loc ) = @_;
1636 :    
1637 :     if ( $loc =~ /^(\d+)\.\.(\d+)$/ )
1638 :     {
1639 :     join( '_', $contig, $1, $2 )
1640 :     }
1641 :    
1642 :     elsif ( $loc =~ /^join\((.*)\)$/ )
1643 :     {
1644 :     $loc = $1;
1645 :     my $lvl = 0;
1646 :     for ( my $i = length( $loc )-1; $i >= 0; $i-- )
1647 :     {
1648 :     for ( substr( $loc, $i, 1 ) )
1649 :     {
1650 :     /,/ && ! $lvl and substr( $loc, $i, 1 ) = "\t";
1651 :     /\(/ and $lvl--;
1652 :     /\)/ and $lvl++;
1653 :     }
1654 :     }
1655 :     $lvl == 0 or print STDERR "Paren matching error: $loc\n" and die;
1656 :     map { gb_loc_2_seed_2( $contig, $_ ) } split /\t/, $loc
1657 :     }
1658 :    
1659 :     elsif ( $loc =~ /^complement\((.*)\)$/ )
1660 :     {
1661 :     map { s/_(\d+)_(\d+)$/_$2_$1/; $_ }
1662 :     reverse
1663 :     gb_loc_2_seed_2( $contig, $1 )
1664 :     }
1665 :    
1666 :     else
1667 :     {
1668 :     ()
1669 :     }
1670 :     }
1671 :    
1672 :    
1673 : efrank 1.1 1;

MCS Webmaster
ViewVC Help
Powered by ViewVC 1.0.3