FAST/Bio/Tools/SeqPattern/Backtranslate.pm untested

filter: Statementstmt 0.0% 0 / 145 Branchbran 0.0% 0 / 28 Conditioncond 0.0% 0 / 9 MC/DCmcdc n/a Subroutinesub 0.0% 0 / 16 Podpod 0.0% 0 / 8 totaltotal 0.0% 0 / 206 SCAR 54.8
CC
15
Cov
0%
_tokenize_motif
56.0
_negated_aas_to_codon
30.0
_convert
12.0
CRAP 240.0
1 package FAST::Bio::Tools::SeqPattern::Backtranslate;
2 use strict;
3 use warnings;
4  
5 use base qw(FAST::Bio::Root::Root);
6 use base qw(Exporter);
7  
8 =head1 NAME
9  
10 FAST::Bio::Tools::SeqPattern::Backtranslate
11  
12 =head1 DESCRIPTION
13  
14 This module should not be used directly. It provides helper methods to
15 FAST::Bio::Tools::SeqPattern to reverse translate protein patterns.
16  
17 =cut
18  
19 use FAST::Bio::Seq;
20 use FAST::Bio::Tools::CodonTable;
21  
22 use List::MoreUtils qw(uniq);
23 use Carp qw(croak);
24  
25 our @EXPORT_OK = qw(_reverse_translate_motif);
26  
27 our @EXPORT = @EXPORT_OK;
28  
29 sub _reverse_translate_motif {
30 # Main subroutine. It takes a Profam-like motif and returns its
31 # reverse translation using degenerate codons.
32  
33 # Steps:
34 # 1. Tokenize, then parse tokens.
35 # 2. Reverse translate each token type.
36 # 3. Join tokens in original order. Return the resulting string.
37  
38 my $motif = shift;
39  
40 $motif =~ s/\./X/g;
41 $motif = uc $motif;
42  
43 ### 1. Tokenize, parse the motif.
44 my ( $ordered, $classified ) = _parse_motif($motif);
45  
46 ### 2. Reverse translate each token type.
47 # Reverse translate the plain (unambiguous) tokens.
48 my $ct = FAST::Bio::Tools::CodonTable->new;
49 foreach my $seq ( @{ $classified->{plain} } ) {
50 my $seqO
51 = FAST::Bio::Seq->new( -seq => $$seq, -alphabet => 'protein' );
52 $$seq = $ct->reverse_translate_all($seqO);
53 }
54  
55 # Reverse translate the ambiguous tokens.
56 foreach my $token ( @{ $classified->{ambiguous} } ) {
57 my ($aas) = $$token =~ m(([A-Za-z\.]+));
58 my @codons_to_contract;
59  
60 foreach my $residue ( split '', $aas ) {
61 push @codons_to_contract, $ct->revtranslate($residue);
62 }
63  
64 my $ambiguous_codon = _contract_codons(@codons_to_contract);
65 $$token = $ambiguous_codon;
66 }
67  
68 # Reverse translate the negated residues.
69 foreach my $token ( @{ $classified->{negated} } ) {
70 my ($aas) = $$token =~ m(([A-Za-z\.]+));
71 my $ambiguous_codon = _negated_aas_to_codon($aas);
72 $$token = $ambiguous_codon;
73 }
74  
75 ### 3. Join the profile back from its tokens.
76 return join '', map {$$_} @{$ordered};
77  
78 }
79  
80 sub _parse_motif {
81 # Profam-like motif parser. It takes the pattern as a string, and
82 # returns two data structures that contain the tokens, organized
83 # by order of appearance in the pattern (first return value) and by
84 # category (second return value).
85  
86 my $motif = shift;
87 my $parser = _tokenize_motif($motif);
88 my ( %tokens, @tokens );
89  
90 while ( my $token = $parser->() ) {
91 croak ("Unknown syntax token: <", $token->[1], ">")
92 if ( $token->[0] eq 'UNKNOWN' );
93 push @{ $tokens{ $token->[0] } }, \$token->[1];
94 push @tokens, \$token->[1];
95 }
96 return ( \@tokens, \%tokens );
97 }
98  
99 sub _tokenize_motif {
100  
101 # Return a tokenizer iterator that sequentially recognizes and
102 # returns each token in the input pattern.
103 # Examples of each token type:
104  
105 # ambiguous: a position with more than one possible residue.
106 # eg. [ALEP]
107 # negated: a position in which some residues are excluded.
108 # eg. [^WY]
109 # plain: a common sequence of residues. One position, one residue.
110 # eg. MAAEIK
111 # open_par, close_par: tags surrounding a motif that is repeated
112 # a certain number of times.
113 # eg. (...){3}
114  
115 my $target = shift;
116 return sub {
117 return [ 'ambiguous', $1 ]
118 if $target =~ /\G (\[[A-Za-z\.]+\]) /gcx;
119 return [ 'negated', $1 ]
120 if $target =~ /\G (\[\^[A-Za-z\.]+\]) /gcx;
121 return [ 'plain', $1 ]
122 if $target =~ /\G ([A-Za-z\.]+) /gcx;
123 return [ 'open_par', $1 ]
124 if $target =~ /\G (\() /gcx;
125 return [ 'close_par', $1 ]
126 if $target =~ /\G (\)[\{\d+[,\d+]*\}]*) /gcx;
127 return [ 'UNKNOWN', $1 ]
128 if $target =~ /\G (.) /gcx;
129 return;
130 };
131 }
132  
133 sub _contract_codons {
134  
135 # Take a list of codons, return an ambiguous codon.
136 my @codons = map { uc $_ } @_;
137  
138 my @by_letter = ( [], [], [], );
139 my $ambiguous_codon;
140 foreach my $codon (@codons) {
141 my @letters = split '', $codon;
142 for my $i ( 0 .. 2 ) {
143 push @{ $by_letter[$i] }, $letters[$i];
144 }
145 }
146 for my $i ( 0 .. 2 ) {
147 $ambiguous_codon
148 .= _convert( 'dna', _uniq_string( @{ $by_letter[$i] } ) );
149 }
150 return $ambiguous_codon;
151 }
152  
153 sub _expand_codon {
154  
155 # Given a degenerate codon, return a list with all its
156 # constituents. Takes a three-letter string (codon) as
157 # input, returns a list with three-letter scalars.
158  
159 my $codon = shift;
160 die "Wrong codon length!\n" if length $codon != 3;
161  
162  
163 my ( @codons, @return_bases );
164 my @orig_bases = split '', $codon;
165  
166 for my $i ( 0 .. 2 ) {
167  
168 # from each redundant base, create a list with all their
169 # components (e.g., N -> (A, C, G, T) );
170 my @components = split '', _convert('dna', $orig_bases[$i] );
171 $orig_bases[$i] = [@components];
172 }
173  
174 # Combine all the bases of each of the three positions of the
175 # codons, and build the return list.
176 for my $i ( @{ $orig_bases[0] } ) {
177 for my $j ( @{ $orig_bases[1] } ) {
178 for my $k ( @{ $orig_bases[2] } ) {
179 push @return_bases, $i . $j . $k;
180 }
181 }
182 }
183 return @return_bases;
184 }
185  
186 {
187 my %convert;
188  
189 sub _convert {
190 # Interconvert between redundant and non-redundant protein and
191 # dna alphabets. Takes an alphabet (protein or dna) and a string
192 # with the letter, and returns its equivalent in
193 # redundant/non-redundant alphabet. Example ACTG -> N.
194  
195 my ($alphabet, $letter) = @_;
196 unless (
197 $alphabet and $alphabet =~ /^dna$|^protein$/i
198 and $letter and length $letter <= 4
199 ) { croak "Wrong arguments!\n"; }
200  
201 unless (%convert) {
202 %convert = (
203 'dna' => {
204 qw(N ACGT B CGT D AGT H ACT V ACG K GT
205 M AC R AG S CG W AT Y CT A A C C T T G G)
206 },
207 'protein' => {
208 '.' => 'ACDEFGHIJKLMNOPQRSTUVWY',
209 X => 'ACDEFGHIJKLMNOPQRSTUVWY',
210 Z => 'QE',
211 B => 'ND',
212 },
213 );
214  
215 # Make %convert hash key/value agnostic.
216 foreach my $alphabet ( keys %convert ) {
217 map { $convert{$alphabet}->{ $convert{$alphabet}{$_} } = $_ }
218 keys %{ $convert{$alphabet} };
219 }
220 }
221  
222 return $convert{$alphabet}{$letter};
223 }
224  
225 }
226  
227 sub _uniq_string {
228 # Takes a list of letters and returns an alphabetically sorted
229 # list with unique elements.
230  
231 my @letters = @_;
232 return join '', sort { $a cmp $b } uniq @letters;
233 }
234  
235 {
236 my ( @codon_library, $ct );
237  
238 sub _negated_aas_to_codon {
239  
240 # Given a string of residues, returns a degenerate codon that will
241 # not be translated into any of them, while maximizing degeneracy
242 # (ie, it tries to also translate into as many residues as possible).
243  
244 # This functionality is required for reverse translating profiles
245 # that contain negative patterns: [^X]. This means that the current
246 # position should not contain aminoacid X, but can have any of the
247 # others. The reverse translated nucleotide sequence should
248 # reflect this.
249  
250 # Approach: construct a list of all possible codons, incluiding all
251 # degenerate bases. This is an array of 15x15x15 = 3375 elements.
252 # Order them by descendent "degeneracy".
253 # Return the first one whose expansion in 4-lettered codons
254 # doesn't contain a codon that translates into any of the
255 # non-wanted residues.
256
257 # * Since this takes some time, I presorted them and saved them.
258 # Reading them from a file takes a fraction of the time that it taes
259 # to re-sort them every time the application is launched.
260  
261 my $aas_to_avoid = shift;
262  
263 # Initialize reusable variables if it's the first time the sub
264 # is called.
265 unless (@codon_library) {
266 while (<DATA>) { chomp; push @codon_library, split ' ', $_ }
267 }
268 unless ($ct) { $ct = FAST::Bio::Tools::CodonTable->new; }
269  
270 # Reverse translate the unwanted aminoacids to unwanted codons.
271 my @unwanted_codons;
272 foreach my $aa ( split '', $aas_to_avoid ) {
273 push @unwanted_codons, $ct->revtranslate($aa);
274 }
275  
276 foreach my $degenerate_codon (@codon_library) {
277 my @codons = _expand_codon($degenerate_codon);
278 my $success = 1;
279  
280 foreach my $unwanted (@unwanted_codons) {
281 if ( grep { uc $unwanted eq $_ } @codons ) {
282 $success = 0;
283 }
284 }
285  
286 if ($success) { return $degenerate_codon }
287 }
288 }
289  
290 }
291  
292 1;
293  
294 =head1 COPYRIGHT & LICENSE
295  
296 Copyright 2009 Bruno Vecchi, all rights reserved.
297  
298 This program is free software; you can redistribute it and/or modify it
299 under the same terms as Perl itself.
300  
301 =cut
302  
303 __DATA__