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

Annotation of /FigKernelPackages/gjoseqlib.pm

Parent Directory Parent Directory | Revision Log Revision Log


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

MCS Webmaster
ViewVC Help
Powered by ViewVC 1.0.3