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

Annotation of /FigKernelPackages/gjoseqlib.pm

Parent Directory Parent Directory | Revision Log Revision Log


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

MCS Webmaster
ViewVC Help
Powered by ViewVC 1.0.3