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

Annotation of /FigKernelPackages/gjoseqlib.pm

Parent Directory Parent Directory | Revision Log Revision Log


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

MCS Webmaster
ViewVC Help
Powered by ViewVC 1.0.3