lib/Bio/MUST/Drivers/Exonerate/Aligned.pm

filter: Statementstmt 33.9% 57 / 168 Branchbran 0.0% 0 / 44 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 72.2% 13 / 18 Podpod 0.0% 0 / 1 totaltotal 30.3% 70 / 231 CC 26 SCAR 39.0
CC
26
Cov
27%
_parse_outfile
132.0
_exonerate
56.0
BUILD
30.0
CRAP 49.4
↑ Summary lib/Bio/MUST/Drivers/Roles/Blastable.pm »
1 package Bio::MUST::Drivers::Exonerate::Aligned;
2 # ABSTRACT: Bio::MUST driver for running the Exonerate alignment program
3 5 $Bio::MUST::Drivers::Exonerate::Aligned::VERSION = '0.252830';
4 5 ▶use Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 5 ▶use namespace::autoclean;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 5 ▶use autodie;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 5 ▶use feature qw(say);
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 # use Smart::Comments '###';
11  
12 5 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 5 ▶use Const::Fast;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 5 ▶use IPC::System::Simple qw(system);
Subroutine BEGIN: called CC=1 SCAR=0.0
15 5 ▶use Module::Runtime qw(use_module);
Subroutine BEGIN: called CC=1 SCAR=0.0
16 5 ▶use Path::Class qw(file);
Subroutine BEGIN: called CC=1 SCAR=0.0
17  
18 5 ▶use Bio::MUST::Core;
Subroutine BEGIN: called CC=1 SCAR=0.0
19 5 ▶use Bio::MUST::Core::Constants qw(:gaps);
Subroutine BEGIN: called CC=1 SCAR=0.0
20 5 ▶use aliased 'Bio::MUST::Core::Ali';
Subroutine BEGIN: called CC=1 SCAR=0.0
21 5 ▶use aliased 'Bio::MUST::Core::Seq';
Subroutine BEGIN: called CC=1 SCAR=0.0
22  
23  
24 5 has 'dna_seq' => (
25 is => 'ro',
26 isa => 'Bio::MUST::Core::Seq',
27 required => 1,
28 );
29  
30 5 has 'pep_seq' => (
31 is => 'ro',
32 isa => 'Bio::MUST::Core::Seq',
33 required => 1,
34 );
35  
36 # TODO: homogeneize with main class
37 5 has 'code' => (
38 is => 'ro',
39 isa => 'Str',
40 default => '1',
41 );
42  
43 5 has 'model' => (
44 is => 'ro',
45 isa => 'Str',
46 init_arg => undef,
47 writer => '_set_model',
48 );
49  
50 has $_ => (
51 is => 'ro',
52 isa => 'Num',
53 init_arg => undef,
54 writer => '_set_' . $_,
55 5 ) for qw(
56 query_start query_end
57 target_start target_end
58 score
59 );
60  
61 has $_ . '_seq' => (
62 is => 'ro',
63 isa => 'Bio::MUST::Core::Seq',
64 init_arg => undef,
65 writer => '_set_' . $_,
66 5 ) for qw(query target spliced);
67  
68  
69 # TODO: subclass this to avoid code repetition with main class
70 # TODO: handle reverse complement
71  
72 0 ▶sub BUILD {
Subroutine BUILD: not called CC=5 SCAR=34.0
Pod undocumented
73 0 my $self = shift;
74  
75 # provision executable
76 0 my $app = use_module('Bio::MUST::Provision::Exonerate')->new;
77 0 $app->meet();
78  
79 # build temp Ali file for input DNA seq
80 0 my $dna = Ali->new(
81 seqs => [ $self->dna_seq ],
82 guessing => 0,
83 );
84 0 my $dnafile = $dna->temp_fasta;
85  
86 # build temp Ali file for input PEP seq
87 0 my $pep = Ali->new(
88 seqs => [ $self->pep_seq ],
89 guessing => 0,
90 );
91 0 my $pepfile = $pep->temp_fasta;
92  
93 # execute exonerate
94 0 my $outfile = $self->_exonerate($dnafile, $pepfile);
95  
96 # parse outfile and populate seqs
97 0 $self->_parse_outfile($outfile);
98  
99 # check that everything ran fine
100 0 ▶ unless ( $self->query_seq->seq_len ) {
Branch: 3 branches
BranchTrueFalseTotal
if ($self->query_seq->seq_len) { } 0 0 0
if (do { $self->query_seq->seq_len != $self->target_seq->seq_len }) { } 0 0 0
elsif ($self->spliced_seq->seq_len != 3 * $self->target_seq->seq_len) { } 0 0 0
101 0 carp '[BMD] Warning: exonerate could not align seqs;'
102 . ' returning empty seqs!';
103 ### dnafile: join q{}, "\n", file($dnafile)->slurp
104 ### pepfile: join q{}, "\n", file($pepfile)->slurp
105 }
106 elsif ( $self->query_seq->seq_len != $self->target_seq->seq_len ) {
107 0 carp '[BMD] Warning: query and target seqs not of same length!';
108 ### dnafile: join q{}, "\n", file($dnafile)->slurp
109 ### pepfile: join q{}, "\n", file($pepfile)->slurp
110 }
111 elsif ( $self->spliced_seq->seq_len != 3 * $self->target_seq->seq_len ) {
112 0 carp '[BMD] Warning: DNA and protein target seqs not of same length!';
113 ### dnafile: join q{}, "\n", file($dnafile)->slurp
114 ### pepfile: join q{}, "\n", file($pepfile)->slurp
115 }
116  
117 # unlink temp files
118 0 file($_)->remove for ($dnafile, $pepfile, $outfile);
119  
120 0 return;
121 }
122  
123  
124 5 const my %ARGS_FOR => (
125 'lc' => 'protein2genome',
126 'bf' => 'protein2genome:bestfit --exhaustive',
127 );
128  
129 5 const my $NEW_HSP => qr{C4 \s Alignment:}xms;
130  
131 0 ▶sub _exonerate {
Subroutine _exonerate: not called CC=7 SCAR=40.3
132 0 my $self = shift;
133 0 my $dnafile = shift;
134 0 my $pepfile = shift;
135  
136 # first try with heuristic 'protein2genome' model
137 # if it yields > 1 HSPs try with exhaustive 'protein2genome:bestfit' model
138 # it this better model fails then return first HSP from lesser model
139  
140 0 my $pgm = 'exonerate';
141 0 my $code = $self->code;
142  
143 0 my $return = q{};
144 0 my $remove = q{};
145  
146 0 my %outfile_for;
147 my $hsp_n;
148  
149 MODEL:
150 0 for my $model ( qw(lc bf) ) {
151  
152 0 $remove = $return; # lesser model failed (if any)
153 0 $return = $model; # try better model
154  
155 # create exonerate command
156 0 my $outfile = $dnafile . ".exo.$model.out";
157 0 $outfile_for{$model} = $outfile;
158 my $cmd = qq{$pgm --showvulgar no --showalignment yes}
159 . ' --alignmentwidth 100000' # needed for robust splicing
160 0 . " --verbose 0 --geneticcode $code --model " . $ARGS_FOR{$model}
161 . " --target $dnafile --query $pepfile > $outfile 2> /dev/null"
162 ;
163 #### $cmd
164  
165 # Note: we ask for a single-line alignment to avoid the difficult
166 # parsing of linebreak-interrupted runs of spaces within introns
167  
168 # try to robustly execute exonerate
169 0 my $ret_code = system( [ 0, 1, 127, 139 ], $cmd);
170 0 ▶ if ($ret_code == 127) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ret_code == 127) 0 0 0
171 0 carp "[BMD] Warning: cannot execute $pgm command;"
172 . ' returning nothing!';
173 0 return; # This will likely crash calling code but that's OK.
174 }
175 0 ▶ if ($ret_code == 139) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ret_code == 139) 0 0 0
176 0 carp "[BMD] Warning: $pgm crashed; skipping model: $model!";
177 # do nothing more to leave loop with accurate $hsp_n
178 }
179 0 ▶ if ($ret_code == 1) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ret_code == 1) 0 0 0
180 0 carp "[BMD] Warning: $pgm crashed because of a bad nt seq!";
181 0 return $outfile_for{$model};
182 }
183 # TODO: try to bypass shell (need for absolute path to executable then)
184  
185 # check number of HSPs in outfile
186 0 my @lines = file($outfile)->slurp;
187 0 $hsp_n = grep { m/$NEW_HSP/xms } @lines;
188  
189 # stop here if only one; otherwise possibly try better model
190 0 ▶ last MODEL if $hsp_n == 1;
Branch: 1 branch
BranchTrueFalseTotal
if $hsp_n == 1 0 0 0
191 }
192  
193 # if no HSP then better model was tried and finally failed
194 # thus switch back to lesser model (with > 1 HSPs)
195 0 ▶ if ($hsp_n == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($hsp_n == 0) 0 0 0
196 0 carp "[BMD] Warning: cannot get only one HSP from $pgm;"
197 . ' returning first one!';
198 0 ($return, $remove) = ($remove, $return);
199 }
200  
201 #### $remove
202 #### $return
203  
204 # delete unused file (if any) and return outfile
205 0 file( $outfile_for{ $remove} )->remove;
206 0 $self->_set_model( $return);
207  
208 0 return $outfile_for{$return};
209 }
210  
211  
212 # Raw score: 1215
213 # Query range: 0 -> 247
214 # Target range: 1429 -> 2184
215  
216 # 43 : ysGlyIleValLysGluIleIle<->AspProGlyArgGlyAlaProLeuAlaArgValVal : 61
217 # ||||||||||||||||||||||| ||||||||||||||||||||||||||||||||||||
218 # ysGlyIleValLysGluIleIleHisAspProGlyArgGlyAlaProLeuAlaArgValVal
219 # 1554 : AGGGCATAGTGAAAGAAATCATACACGACCCAGGAAGAGGCGCTCCCTTAGCCAGAGTTGTT : 1613
220 #
221 # ...
222 #
223 # 145 : sThrArgValArgLeuProSerGlySerLysLysValLeuSerSerThrAsnArg-AlaVal : 164
224 # |||||||||||||||||||||||||||||||||||||||||||||||||||||||#.!!|||
225 # sThrArgValArgLeuProSerGlySerLysLysValLeuSerSerThrAsnArg#UnkVal
226 # 1863 : AACGCGCGTGCGTCTTCCGTCAGGATCGAAAAAGGTGCTATCGTCGACGAATCGAGNCCGTG : 1923
227 #
228 # ...
229 #
230 # 177 : gGlnArgLysAlaHisLeuIleGluIleGlnValAsnGlyGlyThrValAlaGlnLysValAsp : 197
231 # ||||!:!||||||||||||!!:|||||||||||||||||||||:!!|||||| !!|||||||||
232 # gGlnLysLysAlaHisLeuMetGluIleGlnValAsnGlyGlySerValAla***LysValAsp
233 # 321 : GCAAAAGAAGGCCCACCTTATGGAAATCCAAGTCAATGGTGGATCCGTTGCGTAAAAGGTCGAC : 383
234 #
235 # ...
236 #
237 # 91 : rgLysHisProTrpHisValIleArgIleAsnLysMetLeuSerCysAlaGlyAlaAspArg{L : 111
238 # ||:!!|||||||||||||||:!!|||||||||||||||||||||||||||||||||||||||{|
239 # rgGlnHisProTrpHisValLeuArgIleAsnLysMetLeuSerCysAlaGlyAlaAspArg{L
240 # 257 : GACAACATCCATGGCACGTTCTTAGAATCAACAAGATGCTTTCCTGCGCAGGTGCCGATAGA{C : 319
241 #
242 # 112 : e} >>>> Target Intron 1 >>>> {u}GlnGlnGlyMetArgGlyAlaPheGlyLys : 121
243 # |} 86 bp {|} ||||||||| !||||||||||||
244 # e}++ --{u}UnkUnkGlyMetArgHisAlaPheGlyLys
245 # 320 : T}gt.........................nn{N}NNNNNCGGTATGAGACATGCTTTCGGAAAG : 435
246 #
247 # ...
248 #
249 # 126 : SerGlnAlaLeuAspValIleHisGluTyrPheLys : 137
250 # !!!! !||||||!!: !!:!! ! !|||
251 # ThrProAlaLeuGluPheValUnkUnkValHisLys
252 # 689 : ACGCCAGCTCTGGAGTTCGTAAKGRGAGTACATAAG : 652
253  
254 # regexes for parsing outfile
255 5 const my $LEFT_NUM => qr{-?\d+\s+:}xms;
256 5 const my $RIGHT_NUM => qr{:\s+-?\d+}xms;
257 5 const my $SEQ_STR => qr{[A-Za-z0-9\.\#\*\+\-\<\>\{\}\ ]+}xms;
258  
259 0 ▶sub _parse_outfile {
Subroutine _parse_outfile: not called CC=11 SCAR=48.8
260 0 my $self = shift;
261 0 my $outfile = shift;
262  
263 # read output file (human-readable alignment)
264 0 open my $out, '<', $outfile;
265 #### $outfile
266  
267 0 my $query_seq = q{};
268 0 my $target_seq = q{};
269 0 my $spliced_seq = q{};
270  
271 0 my $hsp_n = 0;
272 0 my $in_block = 0;
273  
274 LINE:
275 0 ▶ while (my $line = <$out>) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $line = readline $out)) 0 0 0
276 0 chomp $line;
277 ##### $line
278  
279 # header lines
280 0 ▶ if ($line =~ m/$NEW_HSP/xms) {
Branch: 6 branches
BranchTrueFalseTotal
if ($line =~ /$NEW_HSP/msx) { } 0 0 0
elsif ($line =~ /^\s*Raw \s score:\s (\d+)/msx) { } 0 0 0
elsif ($line =~ /^\s*Query \s range:\s (\d+) \s->\s (\d+)/msx) { } 0 0 0
elsif ($line =~ /^\s*Target \s range:\s (\d+) \s->\s (\d+)/msx) { } 0 0 0
elsif ($line =~ /^\s* $LEFT_NUM \s ($SEQ_STR) \s $RIGHT_NUM \s*$/msx) { } 0 0 0
elsif ($line =~ /^\s+ ($SEQ_STR) \s*$/msx) { } 0 0 0
281 0 $hsp_n++;
282 0 ▶ last LINE if $hsp_n > 1; # process only first HSP
Branch: 1 branch
BranchTrueFalseTotal
if $hsp_n > 1 0 0 0
283 }
284 elsif ($line =~ m/^\s*Raw \s score:\s (\d+)/xms) {
285 0 $self->_set_score( $1 );
286 }
287 elsif ($line =~ m/^\s*Query \s range:\s (\d+) \s->\s (\d+)/xms) {
288 0 $self->_set_query_start( $1 + 1 );
289 0 $self->_set_query_end( $2);
290 } # Note: coordinates are converted to BLAST/GFF standard
291 elsif ($line =~ m/^\s*Target \s range:\s (\d+) \s->\s (\d+)/xms) {
292 0 $self->_set_target_start( $1 + 1 );
293 0 $self->_set_target_end( $2);
294 } # Note: coordinates are converted to BLAST/GFF standard
295  
296 # alignment lines
297 elsif ($line =~ m/^\s* $LEFT_NUM \s ($SEQ_STR) \s $RIGHT_NUM \s*$/xms) {
298 0 ▶ unless ($in_block) {
Branch: 1 branch
BranchTrueFalseTotal
if ($in_block) { } 0 0 0
299 # this is query line
300 0 $query_seq .= $1;
301 0 $in_block = 1;
302 0 <$out>; # skip midline (may be hard to parse)
303 }
304 else {
305 # this is DNA
306 0 $spliced_seq .= $1;
307 0 $in_block = 0;
308 }
309 }
310 elsif ($line =~ m/^\s+ ($SEQ_STR) \s*$/xms) {
311 # this is likely target line
312 0 ▶ $target_seq .= $1 if $in_block;
Branch: 1 branch
BranchTrueFalseTotal
if $in_block 0 0 0
313 }
314 }
315  
316 # remove introns and frameshifts in all seqs simultaneously
317 0 ($query_seq, $target_seq, $spliced_seq)
318 = _splice_seqs($query_seq, $target_seq, $spliced_seq);
319  
320 # store aligned pep seqs
321 0 $self->_set_query(
322 Seq->new(
323 seq_id => $self->pep_seq->full_id,
324 seq => _canonize_seq(1, $query_seq)
325 )
326 );
327 0 $self->_set_target(
328 Seq->new(
329 seq_id => $self->dna_seq->full_id,
330 seq => _canonize_seq(1, $target_seq)
331 )
332 );
333 0 $self->_set_spliced(
334 Seq->new(
335 seq_id => $self->dna_seq->full_id,
336 seq => _canonize_seq(0, $spliced_seq)
337 )
338 );
339  
340 0 return;
341 }
342  
343  
344 # regexes for splicing seqs
345 5 const my $ANGLES => qr{(?:<|>){4}}xms;
346 5 const my $TARGET => qr{Target \s Intron \s \d+}xms;
347 5 const my $INTRON => qr{\s{2} $ANGLES \s $TARGET \s $ANGLES \s{2}}xms;
348  
349 0 ▶sub _splice_seqs {
Subroutine _splice_seqs: not called CC=4 SCAR=30.0
350 0 my $query_seq = shift;
351 0 my $target_seq = shift;
352 0 my $spliced_seq = shift;
353 #### $query_seq
354 #### $target_seq
355 #### $spliced_seq
356  
357 # preserve framshifts in target_seq
358 # Note: this is needed to avoid splicing out real frameshifts below
359 0 $target_seq =~ s/\#{2}\-{3}/##Unk/xmsg; # 1-nt deletion frameshifts
360 0 $target_seq =~ s/\#{3} /Unk/xmsg; # ???
361  
362 # store intron pos and 'lengths' in query_seq
363 0 my @introns;
364 0 ▶ while ($query_seq =~ m/($INTRON)/xmsg) {
Branch: 1 branch
BranchTrueFalseTotal
if ($query_seq =~ /($INTRON)/gmsx) 0 0 0
365 0 my $len = length $1;
366 0 my $pos = pos($query_seq) - $len;
367 0 push @introns, [ $pos, $len ];
368 }
369  
370 # store frameshift pos and 'lengths' in target_seq
371 0 ▶ while ($target_seq =~ m/(\#+)/xmsg) {
Branch: 1 branch
BranchTrueFalseTotal
if ($target_seq =~ /(\#+)/gmsx) 0 0 0
372 0 my $len = length $1;
373 0 my $pos = pos($target_seq) - $len;
374 0 push @introns, [ $pos, $len ];
375 }
376  
377 # splice out introns starting from seq ends
378 # @introns
379 0 for my $intron (sort { $b->[0] <=> $a->[0] } @introns) {
380 0 my ($pos, $len) = @{$intron};
381 0 my $query_in = substr $query_seq, $pos, $len, q{};
382 0 my $target_in = substr $target_seq, $pos, $len, q{};
383 0 my $spliced_in = substr $spliced_seq, $pos, $len, q{};
384  
385 #### $query_in
386 #### $target_in
387 #### $spliced_in
388 }
389  
390 #### $query_seq
391 #### $target_seq
392 #### $spliced_seq
393  
394 0 return ($query_seq, $target_seq, $spliced_seq);
395 }
396  
397  
398 5 const my %SHORT_FOR => (
399 Ala => 'A',
400 Asx => 'B',
401 Cys => 'C',
402 Asp => 'D',
403 Glu => 'E',
404 Phe => 'F',
405 Gly => 'G',
406 His => 'H',
407 Ile => 'I',
408 Lys => 'K',
409 Leu => 'L',
410 Met => 'M',
411 Asn => 'N',
412 Pro => 'P',
413 Gln => 'Q',
414 Arg => 'R',
415 Ser => 'S',
416 Thr => 'T',
417 Val => 'V',
418 Trp => 'W',
419 Xaa => 'X',
420 Tyr => 'Y',
421 Glx => 'Z',
422 Unk => $FRAMESHIFT,
423 '---' => '-',
424 );
425  
426 0 ▶sub _canonize_seq {
Subroutine _canonize_seq: not called CC=3 SCAR=24.8
427 0 my $prot = shift;
428 0 my $seq3 = shift;
429 #### $seq3
430  
431 # example of 1-nt deletion
432 # ArgGlyAsnAla--GlyGlyGlnHisHisHisArgIle...LysPhePheThrArgArgAlaGlu--GluLysIleLys
433 #
434 # ArgGlyAsnAla##---GlyGlnHisHisHisArgIle...LysPhePheThrArgArgAlaGlu##---LysIleLys
435 # CGTGGTAATGCTGT---GGTCAGCACCACCACAGAATT...AAGTTCTTCACACGCCGTGCTGAGGA---AAGATCAAG
436  
437 # ensure no residual frameshift
438 0 $seq3 =~ tr/\{\}//d; # spliced codons
439 0 $seq3 =~ s/\<\-\>/---/xmsg; # insertion
440 0 $seq3 =~ s/\*{3} /Unk/xmsg; # STOP
441 #### $seq3
442  
443 0 ▶ return $seq3 unless $prot;
Branch: 1 branch
BranchTrueFalseTotal
unless $prot 0 0 0
444  
445 # 'translate' 3-letter AAs to 1-letter AAs
446 0 my $seq1 = q{};
447 0 ▶ for (my $i = 0; $i < length $seq3; $i += 3) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < length $seq3) 0 0 0
448 0 my $triplet = substr $seq3, $i, 3;
449 0 $seq1 .= $SHORT_FOR{ $triplet };
450 }
451 #### $seq1
452  
453 0 return $seq1;
454 }
455  
456 5 __PACKAGE__->meta->make_immutable;
457 5 1;
458  
459 __END__
↑ Summary lib/Bio/MUST/Drivers/Roles/Blastable.pm »