Grep/Backend/Vmatch.pm

filter: Statementstmt 90.4% 19 / 21 Branchbran n/a Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 7 / 7 Podpod n/a totaltotal 92.8% 26 / 28 CC 1 SCAR 0.4
CC
1
Cov
90%
BEGIN
1.3
BEGIN
1.0
BEGIN
1.0
CRAP 1.0
« Grep/Backend/RE.pm ↑ Summary Grep/Backend/BackendI.pm »
1 #############################################################################
2 # $Author: markus $
3 # $Date: 2009-11-12 19:54:03 +0100 (Thu, 12 Nov 2009) $
4 # $Revision: 1848 $
5 #############################################################################
6  
7 package Bio::Grep::Backend::Vmatch;
8  
9 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 2 ▶use version; our $VERSION = qv('0.10.6');
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14 2 ▶use autodie qw(open close);
Subroutine BEGIN: called CC=1 SCAR=0.0
15 2 ▶use English qw( -no_match_vars );
Subroutine BEGIN: called CC=1 SCAR=0.0
16  
17 2 ▶use Bio::Grep::SearchResult;
Subroutine BEGIN: called CC=1 SCAR=0.0
18 0 ▶use Bio::Grep::Backend::BackendI;
Subroutine BEGIN: called CC=1 SCAR=2.6
19  
20 use base 'Bio::Grep::Backend::BackendI';
21  
22 use File::Basename;
23 use File::Temp qw/ tempfile tempdir /;
24 use IO::String;
25 use Carp::Assert;
26  
27 use Readonly;
28  
29 # The VMATCH output columns
30 Readonly my $COL_LENGTH => 0;
31 Readonly my $COL_ID => 1;
32 Readonly my $COL_POS => 2;
33 Readonly my $COL_STRAND => 3;
34 Readonly my $COL_QUERY => 5;
35 Readonly my $COL_EVALUE => 8;
36 Readonly my $COL_IDENTITY => 10;
37  
38 sub new {
39 my $self = shift;
40 $self = $self->SUPER::new;
41 my %all_features = $self->features;
42 delete $all_features{GUMISMATCHES};
43 delete $all_features{DELETIONS};
44 delete $all_features{INSERTIONS};
45 delete $all_features{REVCOM_DEFAULT};
46 $self->features(%all_features);
47 return $self;
48 }
49  
50 sub search {
51 my ( $self, $arg_ref ) = @_;
52 my $s = $self->settings;
53 $self->_check_search_settings($arg_ref);
54  
55 my ( $query, $query_file, $tmp_query_file )
56 = $self->_create_tmp_query_file();
57 $self->_check_search_settings_vmatch($query_file);
58  
59 my $command = $self->_generate_vmatch_command( $query, $query_file,
60 $tmp_query_file );
61 if ( $ENV{BIOGREPDEBUG} ) {
62 warn $command . "\n";
63 }
64  
65 my $cmd_ok = $self->_execute_command($command);
66  
67 if ( !$cmd_ok ) {
68 $self->throw(
69 -class => 'Bio::Root::SystemException',
70 -text => "Vmatch call failed. Command was:\n\t$command"
71 );
72 }
73  
74 $self->_create_query_desc_lookup();
75  
76 $self->_prepare_results;
77 return 1;
78 }
79  
80 sub _generate_vmatch_command {
81 my ( $self, $query, $query_file, $tmp_query_file ) = @_;
82 my $s = $self->settings;
83  
84 my @params;
85  
86 my %flags = (
87 'online' => 'online',
88 'complete' => 'complete',
89 );
90  
91 for my $option ( keys %flags ) {
92 my $option_isset = $option . '_isset';
93 if ( $s->$option_isset && $s->$option ) {
94 push @params, " -$flags{$option} ";
95 }
96 }
97  
98 my $auto_query_length = 0;
99 if ( !defined $s->query_length && !$s->complete && !$query_file ) {
100 $s->query_length( length $query );
101 $auto_query_length = 1;
102 }
103  
104 # dg sorting done by BackendI, not vmatch
105 if ( $s->sort_isset && substr( $s->sort, 0, 1 ) ne 'g' ) {
106 push @params, ' -sort ' . $s->sort;
107 }
108  
109 if ( $s->qspeedup_isset ) {
110 if ( $s->complete_isset ) {
111 $self->throw(
112 -class => 'Bio::Root::BadParameter',
113 -text => q{You can't combine qspeedup and complete.},
114 );
115 }
116  
117 push @params, ' -qspeedup ' . $s->qspeedup . q{ };
118 }
119  
120 if ( $s->direct_and_rev_com ) {
121 push @params, ' -p -d ';
122 }
123 elsif ( $s->query_file && $s->reverse_complement ) {
124 push @params, ' -p ';
125 }
126  
127 my %arguments = (
128 'mismatches' => q{h},
129 'editdistance' => q{e},
130 'query_length' => q{l},
131 'maxhits' => 'best',
132 'showdesc' => 'showdesc',
133 'hxdrop' => 'hxdrop',
134 'exdrop' => 'exdrop',
135 );
136  
137 for my $option ( keys %arguments ) {
138 my $option_isset = $option . '_isset';
139 if ( $s->$option_isset && $s->$option ) {
140 push @params, " -$arguments{$option} " . $s->$option;
141 }
142 }
143  
144 if ($auto_query_length) {
145 $s->query_length_reset;
146 }
147 return
148 $self->_cat_path_filename( $s->execpath, 'vmatch' ) . ' -q '
149 . $tmp_query_file
150 . join( q{ }, @params ) . ' -s '
151 . $self->_cat_path_filename( $s->datapath, $s->database );
152 }
153  
154 ###########################################################################
155 # Usage : _create_query_desc_lookup();
156 # Purpose : create a lookup hash where the keys are query descriptions
157 # like the ones the Vmatch output. The values are the
158 # corresponding Bio::Seq objects
159 # Returns : nothing
160 # Parameters : none
161  
162 sub _create_query_desc_lookup {
163 my ($self) = @_;
164 if ( $self->settings->showdesc_isset ) {
165 my %query_desc_lookup;
166 foreach my $query_seq ( @{ $self->_query_seqs } ) {
167  
168 # simulate how this sequence would look in vmatch output
169 my $query_desc = $query_seq->id;
170  
171 if ( $query_seq->desc && length($query_desc) > 0 ) {
172 $query_desc .= q{ } . $query_seq->desc;
173 }
174 $query_desc = substr $query_desc, 0, $self->settings->showdesc;
175 $query_desc =~ s/\s/_/gxms;
176 $query_desc_lookup{$query_desc} = $query_seq;
177 }
178 $self->{_mapping} = \%query_desc_lookup;
179 }
180 return;
181 }
182  
183 ###########################################################################
184 # Usage : _check_search_settings_vmatch()
185 # Purpose : extends _check_search_settings() with some Vmatch specific
186 # tests.
187 # Returns : nothing
188 # Parameters : none
189 # Throws : Bio::Root::BadParameter
190  
191 sub _check_search_settings_vmatch {
192 my ( $self, $query_file ) = @_;
193 my $s = $self->settings;
194 if ( ( $s->upstream > 0 || $s->downstream > 0 ) && $s->showdesc_isset ) {
195 $self->throw(
196 -class => 'Bio::Root::BadParameter',
197 -text => q{You can't use showdesc() with upstream or downstream.},
198 );
199 }
200 if ( $query_file && !$s->complete && !$s->query_length_isset ) {
201 $self->throw(
202 -class => 'Bio::Root::BadParameter',
203 -text => 'You have to specify complete or querylength. See '
204 . 'the flags -complete and -l in the Vmatch documentation.',
205 );
206 }
207 return 1;
208 }
209  
210 sub get_databases {
211 my $self = shift;
212 return $self->_get_databases('.al1');
213 }
214  
215 ###########################################################################
216 # Usage : generate_database()
217 # Purpose : calling mkvtree to generate the suffix arrays
218 # Returns : 1
219 # Parameters : hashref with mandatory argument 'file'. optional parameters
220 # are format (not needed here), description, copy (instead of
221 # symlink), datapath and the mkvtree parameters prefix_length
222 # (-pl) and verbose (-v)
223 # Throws : Bio::Root::BadParameter,
224 # Bio::Root::SystemException
225  
226 sub generate_database {
227 my ( $self, @args ) = @_;
228 my %args = $self->_prepare_generate_database(@args);
229  
230 if ( defined $args{skip} ) {
231 return 0;
232 }
233  
234 my $alphabet = $self->_guess_alphabet_of_file( $args{filename} );
235 my $alphabet_specific_arguments = q{};
236  
237 my $verbose = q{};
238 if ( defined $args{verbose} ) {
239 $verbose = ' -v ';
240 }
241  
242 my $pl = ' -pl ';
243 if ( defined $args{prefix_length} ) {
244 $pl .= $args{prefix_length} . q{ };
245 }
246  
247 if ( $alphabet eq 'protein' ) {
248 $alphabet_specific_arguments = ' -protein ';
249 }
250 elsif ( $alphabet eq 'dna' || $alphabet eq 'rna' ) {
251 $alphabet_specific_arguments = ' -dna ';
252 }
253 else {
254 $self->throw(
255 -class => 'Bio::Root::BadParameter',
256 -text => 'Unsupported alphabet of file.',
257 -value => $alphabet,
258 );
259 }
260 my $command
261 = $self->_cat_path_filename( $self->settings->execpath, 'mkvtree' )
262 . ' -db '
263 . $args{basefilename}
264 . $alphabet_specific_arguments
265 . $pl
266 . ' -allout '
267 . $verbose;
268  
269 if ( $ENV{BIOGREPDEBUG} ) {
270 warn $command . "\n";
271 }
272 my $output_dir = $self->settings->datapath;
273 system qq{ cd $output_dir ; exec $command };
274 if ($CHILD_ERROR) {
275 $self->throw(
276 -class => 'Bio::Root::SystemException',
277 -text => 'mkvtree call failed. Cannot generate suffix array. '
278 . "Command was:\n\t$command",
279 );
280 }
281 return 1;
282 }
283  
284 ###########################################################################
285 # Usage : _parse_next_res()
286 # Purpose : Assumes that the output filehandle points to the beginning of
287 # a hit. This method then parses this hit.
288 # Returns : SearchResult object or 0 (end of file)
289 # Parameters : none
290  
291 sub _parse_next_res {
292 my $self = shift;
293 my @query_seqs = $self->_query_seqs;
294 my $s = $self->settings;
295 my @results = ();
296 my $alignment_in_output = 0;
297 my $skip_next_alignment = 0;
298 my $skipped_lines = 0;
299 my $subject;
300 my $tmp_aln;
301  
302 my ( $command, $output );
303  
304 my $FH = $self->_output_fh;
305 LINE:
306 while ( my $line = <$FH> ) {
307 chomp $line;
308 $line =~ s/\s+/ /gxms;
309 if ( $line !~ /\s/xms ) {
310 $skipped_lines++;
311 next LINE if $skipped_lines != 2;
312  
313 # we are here? this means the parser is at the end of one hit
314 # so store the alignment (we did not know the alignment when we
315 # had to create the search result object)
316 $results[-1]->alignment($tmp_aln);
317  
318 # without showdesc, we fetch the sequence with vsubseqselect
319 # with, we need to extract the sequence out of the vmatch output
320 # (the alignment)
321 if ( $s->showdesc_isset ) {
322 my $real_subject
323 = $results[-1]->alignment->get_seq_by_pos(1)->seq;
324  
325 # remove gaps out of alignment
326 $real_subject =~ s{-}{}gxms;
327 $results[-1]->sequence->seq($real_subject);
328 }
329  
330 my $res = $self->_filter_result( $results[-1] );
331 return $res if $res;
332 $alignment_in_output = 0;
333 next LINE;
334 }
335  
336 $skipped_lines = 0;
337  
338 my @fields = split q{ }, $line;
339 if ( $line =~ m{\A Sbjct: }xms && !$skip_next_alignment ) {
340 $alignment_in_output = 1;
341 }
342  
343 next
344 if !( $fields[$COL_LENGTH] =~ m{\A \d+ \z}xms
345 || $alignment_in_output
346 );
347 $skip_next_alignment = 0;
348  
349 if ( $line =~ m{\A Sbjct: \s (.*) \z}xms ) {
350 $subject = $1;
351 }
352 if ( $line =~ m{\A Query: \s (.*) \z}xms ) {
353  
354 # updates or creates the alignment
355 $self->_parser_create_alignment_obj(
356 { query => $1,
357 subject => $subject,
358 alignment => $tmp_aln,
359 results => \@results
360 }
361 );
362  
363 next LINE;
364 }
365 next LINE if $alignment_in_output;
366  
367 $tmp_aln = Bio::SimpleAlign->new( -source => 'VMATCH' );
368  
369 $self->_parser_untaint_data( \@fields );
370  
371 my ( $fasta, $upstream )
372 = $self->_parser_create_sequence_obj( \@fields );
373 my $internal_seq_id = $fields[$COL_ID];
374 if ( $s->showdesc_isset ) {
375 $internal_seq_id = $fasta->id;
376 }
377  
378 my $query;
379 if ( $s->showdesc_isset ) {
380 $query = $self->{_mapping}->{ $fields[$COL_QUERY] };
381 }
382 else {
383 $query = $query_seqs[ $fields[$COL_QUERY] ];
384 }
385  
386 my $rct = q{};
387 my $rcs = $query->seq;
388 if ( $s->direct_and_rev_com && $fields[$COL_STRAND] eq q{P} ) {
389 $rct = ' (reverse complement)';
390 $rcs = $query->revcom->seq;
391 }
392 my $result = Bio::Grep::SearchResult->new(
393 { sequence => $fasta,
394 begin => $upstream,
395 end => $upstream + $fields[$COL_LENGTH],
396 alignment => Bio::SimpleAlign->new(),
397 sequence_id => $internal_seq_id,
398 remark => q{},
399 evalue => $fields[$COL_EVALUE],
400 percent_identity => $fields[$COL_IDENTITY],
401 query => Bio::Seq->new(
402 -id => $query->id,
403 -desc => $query->desc . $rct,
404 -seq => $rcs,
405 ),
406 }
407 );
408 push @results, $result;
409 }
410 $self->_delete_output();
411 return 0;
412 }
413  
414 ###########################################################################
415 # Usage : _parser_create_sequence_obj()
416 # Purpose : creates a Bio::Seq object for $res->sequence, returns true
417 # upstream size (corrects upstream when available upstream
418 # region too small)
419 # Returns : Bio::Seq object and $upstream
420 # Parameters : ref to @fields array (containing the Vmatch output)
421  
422 sub _parser_create_sequence_obj {
423 my ( $self, $fields ) = @_;
424 my $upstream = $self->settings->upstream;
425 my $seq_obj;
426 if ( !$self->settings->showdesc_isset ) {
427 my $start = $fields->[$COL_POS] - $upstream;
428  
429 # maybe the defined upstream region is larger than available
430 # so check this and store in local variables
431 if ( $start < 0 ) {
432 $upstream = $upstream + $start;
433 $start = 0;
434 }
435  
436 my $length = $upstream + $fields->[$COL_LENGTH];
437 $length += $self->settings->downstream;
438  
439 $seq_obj
440 = $self->_get_subsequence( $length, $fields->[$COL_ID], $start );
441 }
442 else {
443 my ( $seq_id, $seq_desc )
444 = $fields->[$COL_ID] =~ m{\A (.+?) _ (.*) \z}xms;
445 if ( !defined $seq_id ) {
446 $seq_id = $fields->[$COL_ID];
447 }
448 $seq_desc =~ s{_}{ }gxms;
449 $seq_obj = Bio::Seq->new( -id => $seq_id, -desc => $seq_desc );
450 }
451 return ( $seq_obj, $upstream );
452 }
453  
454 sub _parser_create_alignment_obj {
455 my ( $self, $args ) = @_;
456  
457 my ( $query_pos, $subject_pos );
458  
459 if ( $args->{query} =~ s{\s+ (\d+) \s* \z}{}xms ) {
460 $query_pos = $1;
461 }
462 if ( $args->{subject} =~ s{\s + (\d+) \s* \z}{}xms ) {
463 $subject_pos = $1;
464 }
465 assert( defined $query_pos ) if DEBUG;
466 assert( defined $subject_pos ) if DEBUG;
467  
468 if ( !$args->{alignment}->no_sequences ) {
469 $args->{alignment}->add_seq(
470 Bio::LocatableSeq->new(
471 -id => 'Subject',
472 -seq => $args->{subject},
473 -start => ( $subject_pos - length $args->{subject} ) + 1,
474 -end => $subject_pos
475 )
476 );
477 $args->{alignment}->add_seq(
478 Bio::LocatableSeq->new(
479 -id => $args->{results}->[-1]->query->id,
480 -seq => $args->{query},
481 -start => ( $query_pos - length $args->{query} ) + 1,
482 -end => $query_pos
483 )
484 );
485 }
486 else {
487 my $s1 = $args->{alignment}->get_seq_by_pos(1);
488 my $s2 = $args->{alignment}->get_seq_by_pos(2);
489 $s1->seq( $s1->seq . $args->{subject} );
490 $s2->seq( $s2->seq . $args->{query} );
491 $args->{alignment} = Bio::SimpleAlign->new( -source => 'VMATCH' );
492 $args->{alignment}->add_seq($s1);
493 $args->{alignment}->add_seq($s2);
494 $s1->end($subject_pos);
495 $s2->end($query_pos);
496 }
497 return;
498 }
499  
500 sub _parser_untaint_data {
501 my ( $self, $fields ) = @_;
502  
503 ( $fields->[$COL_LENGTH] ) = $fields->[$COL_LENGTH] =~ m{ (\d+) }xms;
504 ( $fields->[$COL_POS] ) = $fields->[$COL_POS] =~ m{ (\d+) }xms;
505 ( $fields->[$COL_STRAND] ) = $fields->[$COL_STRAND] =~ m{ ([DP]) }xms;
506  
507 if ( $self->settings->showdesc_isset ) {
508  
509 # not numerical with showdesc on
510 # don't worry about this unclean untainting, we don't use that
511 # description in dangerous ways
512 ( $fields->[$COL_ID] ) = $fields->[$COL_ID] =~ m{ (.+) }xms;
513 ( $fields->[$COL_QUERY] ) = $fields->[$COL_QUERY] =~ m{ (.+) }xms;
514 }
515 else {
516 ( $fields->[$COL_ID] ) = $fields->[$COL_ID] =~ m{ (\d+) }xms;
517 ( $fields->[$COL_QUERY] ) = $fields->[$COL_QUERY] =~ m{ (\d+) }xms;
518 }
519 return;
520 }
521  
522 sub _get_subsequence {
523 my ( $self, $length, $id, $start ) = @_;
524 my $command
525 = $self->_cat_path_filename( $self->settings->execpath,
526 'vsubseqselect' )
527 . " -seq $length $id $start "
528 . $self->_cat_path_filename( $self->settings->datapath,
529 $self->settings->database );
530 my $output = $self->_execute_command_and_return_output($command);
531  
532 if ( $ENV{BIOGREPDEBUG} ) {
533 warn $command . "\n";
534 }
535 if ( $output =~ m{(\d+).*must\sbe\ssmaller\sthan\s (\d+)}xms ) {
536 return $self->_get_subsequence( $length - ( $1 - $2 + 1 ), $id,
537 $start );
538 }
539 my $stringio = IO::String->new($output);
540 my $in = Bio::SeqIO->new(
541 '-fh' => $stringio,
542 '-format' => 'fasta'
543 );
544 return $in->next_seq();
545 }
546  
547 sub get_sequences {
548 my ( $self, $seqid ) = @_;
549 my $s = $self->settings;
550 $self->is_arrayref_of_size( $seqid, 1 );
551 $self->_check_search_settings();
552 my ( $tmp_fh, $tmpfile );
553  
554 my $seq_query = q{};
555  
556 if ( @{$seqid}[0] =~ m{\A \d+ \z}xms ) {
557 ( $tmp_fh, $tmpfile )
558 = tempfile( 'vseqselect_XXXXXXXXXXXXX', DIR => $s->tmppath );
559  
560 for my $sid ( @{$seqid} ) {
561 print ${tmp_fh} $sid . " \n "
562 or $self->_cannot_print($tmpfile);
563 }
564 close $tmp_fh;
565 $seq_query = ' -seqnum ' . $tmpfile;
566 }
567 else {
568 my $seq_desc = $self->is_sentence( @{$seqid}[0] );
569 $seq_query = ' -matchdesc "' . $seq_desc . q{"};
570 }
571  
572 my $command
573 = $self->_cat_path_filename( $s->execpath, 'vseqselect' )
574 . $seq_query . q{ }
575 . $self->_cat_path_filename( $s->datapath, $s->database );
576  
577 if ( $ENV{BIOGREPDEBUG} ) {
578 warn $command . "\n";
579 }
580  
581 my $output = $self->_execute_command_and_return_output($command);
582 if ( $CHILD_ERROR && $output !~ m{\A \> }xms ) {
583 $self->throw(
584 -class => 'Bio::Root::SystemException',
585 -text => 'vseqselect call failed. Cannot fetch sequences. '
586 . "Command was:\n\t$command\n$output",
587 );
588 }
589 my $stringio = IO::String->new($output);
590 my $out = Bio::SeqIO->new(
591 '-fh' => $stringio,
592 '-format' => 'fasta'
593 );
594 return $out;
595 }
596  
597 sub available_sort_modes {
598 my ($self) = @_;
599 return (
600 $self->SUPER::available_sort_modes(),
601 la => 'ascending order of length',
602 ld => 'descending order of length',
603 ia => 'ascending order of first position',
604 id => 'descending order of first position',
605 ja => 'ascending order of second position',
606 jd => 'descending order of second position',
607 ea => 'ascending order of Evalue',
608 ed => 'descending order of Evalue',
609 sa => 'ascending order of score',
610 sd => 'descending order of score',
611 ida => 'ascending order of identity',
612 idd => 'descending order of identity'
613 );
614 }
615 1; # Magic true value required at end of module
616 __END__
« Grep/Backend/RE.pm ↑ Summary Grep/Backend/BackendI.pm »