Bio/Tools/SeqPattern/Backtranslate.pm

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