Grep/Backend/BackendI.pm

filter: Statementstmt 100% 66 / 66 Branchbran n/a Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 22 / 22 Podpod n/a totaltotal 100% 88 / 88 CC 1 SCAR 0.0
CC
1
Cov
100%
BEGIN
1.0
BEGIN
1.0
BEGIN
1.0
CRAP 1.0
« Grep/Backend/Vmatch.pm ↑ Summary Grep/Filter/FilterI.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::BackendI;
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 Carp::Assert;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 2 ▶use version; our $VERSION = qv('0.10.6');
Subroutine BEGIN: called CC=1 SCAR=0.0
14  
15 2 ▶use English qw( -no_match_vars );
Subroutine BEGIN: called CC=1 SCAR=0.0
16 2 ▶use autodie qw (open close opendir closedir);
Subroutine BEGIN: called CC=1 SCAR=0.0
17  
18 2 ▶use File::Spec;
Subroutine BEGIN: called CC=1 SCAR=0.0
19 2 ▶use File::Copy;
Subroutine BEGIN: called CC=1 SCAR=0.0
20 2 ▶use File::Temp qw/ tempfile tempdir /;
Subroutine BEGIN: called CC=1 SCAR=0.0
21 2 ▶use File::Basename;
Subroutine BEGIN: called CC=1 SCAR=0.0
22  
23 2 ▶use Scalar::Util qw(reftype);
Subroutine BEGIN: called CC=1 SCAR=0.0
24 2 ▶use Cwd 'abs_path';
Subroutine BEGIN: called CC=1 SCAR=0.0
25 2 ▶use IPC::Open3 'open3';
Subroutine BEGIN: called CC=1 SCAR=0.0
26 2 ▶use Readonly;
Subroutine BEGIN: called CC=1 SCAR=0.0
27  
28 2 ▶use Bio::AlignIO;
Subroutine BEGIN: called CC=1 SCAR=0.0
29 2 ▶use Bio::Factory::EMBOSS;
Subroutine BEGIN: called CC=1 SCAR=0.0
30 2 ▶use Bio::Index::Fasta;
Subroutine BEGIN: called CC=1 SCAR=0.0
31 2 ▶use Bio::Seq;
Subroutine BEGIN: called CC=1 SCAR=0.0
32 2 ▶use Bio::SeqIO;
Subroutine BEGIN: called CC=1 SCAR=0.0
33  
34 2 ▶use Bio::Grep::SearchSettings;
Subroutine BEGIN: called CC=1 SCAR=0.0
35 2 ▶use Bio::Grep::Root;
Subroutine BEGIN: called CC=1 SCAR=0.0
36  
37 2 ▶use UNIVERSAL qw(isa);
Subroutine BEGIN: called CC=1 SCAR=0.0
38  
39 use base 'Bio::Grep::Root';
40  
41 use Class::MethodMaker [
42 new => 'new2',
43 scalar => [qw / settings _output_fh _output_fn _current_res_id _tmp_var/],
44 array => [qw / _query_seqs _results/],
45 hash => [qw / features/],
46 abstract => [
47 qw / search get_sequences get_databases
48 generate_database _parse_next_res/
49 ],
50 ];
51  
52 Readonly my $DNA_ALPHABET_SIZE => 5;
53  
54 sub new {
55 my $self = shift->new2;
56  
57 # initialize standard settings
58 my $settings = Bio::Grep::SearchSettings->new();
59  
60 $self->settings($settings);
61  
62 # assume back-end binary is in path
63 $self->settings->execpath(q{});
64  
65 $self->features( %{ $self->_get_all_possible_features() } );
66 $self->import;
67 return $self;
68 }
69  
70 sub _get_all_possible_features {
71 my ($self) = @_;
72 return {
73 MISMATCHES => 1,
74 GUMISMATCHES => 1,
75 EDITDISTANCE => 1,
76 INSERTIONS => 1,
77 DELETIONS => 1,
78 FILTERS => 1,
79 NATIVE_ALIGNMENTS => 1,
80 EVALUE => 1,
81 PERCENT_IDENTITY => 1,
82 PROTEINS => 1,
83 ONLINE => 1,
84 UPSTREAM => 1,
85 DOWNSTREAM => 1,
86 SORT => 1,
87 MAXHITS => 1,
88 COMPLETE => 1,
89 QUERY => 1,
90 QUERY_FILE => 1,
91 QUERY_LENGTH => 1,
92 SHOWDESC => 1,
93 QSPEEDUP => 1,
94 HXDROP => 1,
95 EXDROP => 1,
96 REGEX => 1,
97 REVCOM_DEFAULT => 1,
98 DIRECT_AND_REV_COM => 1,
99 NATIVE_D_A_REV_COM => 1, # backend supports searching for
100 # direct and revcom matches or
101 # do we have to emulate it?
102 };
103 }
104  
105 sub _get_databases {
106 my ( $self, $suffix ) = @_;
107 my %result = ();
108 opendir my $DIR, $self->settings->datapath;
109 my @files = grep {/${suffix} \z/xms} readdir $DIR;
110 closedir $DIR;
111 FILE:
112 foreach my $file (@files) {
113 my $prefix = $file;
114 $prefix =~ s/$suffix//xms;
115 $result{$prefix} = $prefix;
116 $file =~ s/$suffix/\.nfo/xms;
117 $file = $self->_cat_path_filename( $self->settings->datapath, $file );
118 next FILE if !-e $file;
119 open my $FILE, '<', $file;
120 my $desc = q{};
121  
122 while ( my $line = <$FILE> ) {
123 chomp $line;
124 $desc .= $line;
125 }
126 $result{$prefix} = $desc;
127 close $FILE;
128 }
129 return %result;
130 }
131  
132 sub _filter_result {
133 my ( $self, $res ) = @_;
134 if ( $self->settings->filters_isset ) {
135 foreach my $filter ( @{ $self->settings->filters } ) {
136 $filter->message_reset;
137  
138 #$res->_real_query( $self->settings->_real_query );
139 $filter->search_result($res);
140 if ( !$filter->filter ) {
141 if ( $filter->delete ) {
142 return 0;
143 }
144 }
145 if ( $filter->message_isset ) {
146 $res->remark( $filter->message );
147 }
148 }
149 }
150 return $res;
151 }
152  
153 sub results {
154 my ($self) = @_;
155 $self->deprecated(q{Use 'next_res()' instead.});
156 my @results;
157 while ( my $res = $self->next_res ) {
158 push @results, $res;
159 }
160 return wantarray ? @results : \@results;
161 }
162  
163 sub _prepare_results {
164 my ($self) = @_;
165  
166 # sorting by dG is a Bio::Grep sortmode, not a back-end sortmode. so we
167 # have to load the results in memory and sort them.
168 # next_res() will then shift the next result in _results() (see
169 # next_res())
170 if ( $self->settings->sort_isset
171 && substr( $self->settings->sort, 0, 1 ) eq 'g' )
172 {
173 my @results;
174 while ( my $res = $self->next_res ) {
175 push @results, $res;
176 }
177 @results = $self->_sort_by_dg(@results);
178 $self->_results(@results);
179  
180 # reset for next_res iterator
181 $self->_current_res_id(0);
182 }
183 else {
184 $self->_results_reset;
185 }
186 return;
187 }
188  
189 sub next_res {
190 my ($self) = @_;
191 my $id = $self->_current_res_id;
192 $self->_current_res_id( $id + 1 );
193 if ( $self->_results_isset ) {
194 if ( $id >= scalar @{ $self->_results } ) {
195 return 0;
196 }
197 else {
198 return $self->_results->[$id];
199 }
200 }
201 else {
202 return $self->_parse_next_res();
203 }
204 }
205  
206 sub _sort_by_dg {
207 my ( $self, @results ) = @_;
208 foreach my $result (@results) {
209 if ( !$result->dG_isset ) {
210 $self->warn(
211 'Not sorting results by dG because some results have no '
212 . 'dG calculated. Use Bio::Grep::RNA.' );
213 return @results;
214 }
215 }
216 if ( $self->settings->sort eq 'gd' ) {
217 @results = reverse sort { $a->dG <=> $b->dG } @results;
218 }
219 else {
220 @results = sort { $a->dG <=> $b->dG } @results;
221 }
222 return @results;
223 }
224  
225 sub _results_may_have_gaps {
226 my ($self) = @_;
227 my $s = $self->settings;
228 return 1 if ( defined $s->editdistance && $s->editdistance > 0 );
229 return 1 if ( defined $s->insertions && $s->insertions > 0 );
230 return 1 if ( defined $s->deletions && $s->deletions > 0 );
231 return 0;
232 }
233  
234 # calculates needleman-wunsch global alignment with the EMBOSS
235 # implementation
236 sub _get_alignment {
237 my ( $self, $seq_a, $seq_b ) = @_;
238  
239 # aligments are easy when settings don't allow gaps
240 # back-ends must return hit coordinates
241 if ( !$self->_results_may_have_gaps
242 && length $seq_a->seq == length $seq_b->seq )
243 {
244 my $alignment = Bio::SimpleAlign->new( -source => 'Bio::Grep' );
245 $alignment->add_seq($seq_a);
246 $alignment->add_seq($seq_b);
247 return $alignment;
248 }
249  
250 my $factory = Bio::Factory::EMBOSS->new();
251 my $prog = $factory->program('needle');
252 my $outfile = $self->_cat_path_filename( $self->settings->tmppath,
253 $seq_a->id . '.out' );
254 my $gapopen = '5.0';
255 if ( !$self->settings->editdistance_isset ) {
256 $gapopen = '50.0';
257 }
258  
259 $prog->run(
260 { -asequence => $seq_a,
261 -bsequence => $seq_b,
262 -gapopen => $gapopen,
263 -gapextend => '5.0',
264 -outfile => $outfile
265 }
266 );
267  
268 if ( -e $outfile ) {
269 my $alignio_fmt = 'emboss';
270 my $align_io = Bio::AlignIO->new(
271 -format => $alignio_fmt,
272 -file => $outfile
273 );
274 unlink $outfile;
275 my $alignment = $align_io->next_aln();
276 my $s1 = $alignment->get_seq_by_pos(1);
277 my $s2 = $alignment->get_seq_by_pos(2);
278 $s1->start( $seq_a->start );
279 $s1->end( $seq_a->end );
280 $alignment = Bio::SimpleAlign->new( -source => 'Bio::Grep' );
281 $alignment->add_seq($s1);
282 $alignment->add_seq($s2);
283 return $alignment;
284 }
285  
286 }
287  
288 # concatenates a path and a filename platform-independently
289 sub _cat_path_filename {
290 my ( $self, $path, $filename ) = @_;
291 if ( $path eq q{} ) {
292 return $filename;
293 }
294 else {
295 return File::Spec->catfile( $path, $filename );
296 }
297 }
298  
299 sub _check_search_settings {
300 my ( $self, $arg_ref ) = @_;
301  
302 if ( defined $arg_ref ) {
303 $self->settings->set_attributes($arg_ref);
304 }
305 if ( !defined $self->settings->upstream ) {
306 $self->settings->upstream(0);
307 }
308 if ( !defined $self->settings->downstream ) {
309 $self->settings->downstream(0);
310 }
311  
312 $self->_check_int_features();
313 $self->_check_sort_mode();
314 $self->_check_database();
315 $self->_check_for_unsupported_features();
316  
317 if ( defined $self->settings->editdistance
318 && defined $self->settings->mismatches
319 && $self->settings->editdistance > 0
320 && $self->settings->mismatches > 0 )
321 {
322 $self->throw(
323 -class => 'Bio::Root::BadParameter',
324 -text => q{Can't combine editdistance and mismatches.},
325 -value => $self->settings->editdistance . ' and '
326 . $self->settings->mismatches,
327 );
328 }
329 if ( $ENV{BIOGREPDEBUG} ) {
330 carp $self->settings->to_string();
331 }
332  
333 # reset all filters
334 if ( $self->settings->filters_isset ) {
335 foreach my $filter ( @{ $self->settings->filters } ) {
336 $filter->reset_filter;
337 }
338 }
339 $self->_results_reset;
340 $self->_current_res_id(0);
341 return;
342 }
343  
344 ### SOME IMPORTANT SETTINGS TESTS
345 ### called by _check_search_settings()
346  
347 sub _check_int_features {
348 my ($self) = @_;
349  
350 my @int_features
351 = map { lc $_ } keys %{ $self->_get_all_possible_features() },
352 'reverse_complement', 'no_alignments';
353  
354 my %skip = (
355 FILTERS => 1, # checked later
356 NATIVE_ALIGNMENTS => 1,
357 NATIVE_D_A_REV_COM => 1,
358 PERCENT_IDENTITY => 1, # no user setting
359 PROTEINS => 1, # no user setting
360 SORT => 1, # checked later
361 QUERY_FILE => 1, # checked later
362 QUERY => 1, # checked later
363 EVALUE => 1, # no user setting
364 REVCOM_DEFAULT => 1, # no user setting
365 REGEX => 1, # no user setting
366 );
367  
368 INT_FEATURE:
369 for my $option (@int_features) {
370 next INT_FEATURE if defined $skip{ uc $option };
371  
372 # check if values are integers and untaint them
373 $self->settings->$option(
374 $self->is_integer( $self->settings->$option, $option ) );
375 if ( !defined $self->settings->$option ) {
376 my $reset = $option . '_reset';
377 $self->settings->$reset;
378 }
379 }
380 return 1;
381 }
382  
383 sub _check_for_unsupported_features {
384 my ($self) = @_;
385  
386 # some warnings if user requests features that are not available
387 my %skip = (
388 NATIVE_ALIGNMENTS => 1,
389 NATIVE_D_A_REV_COM => 1,
390 PERCENT_IDENTITY => 1,
391 REVCOM_DEFAULT => 1,
392 PROTEINS => 1,
393 EVALUE => 1,
394 REGEX => 1, # no user setting
395 );
396  
397 FEATURE:
398 for my $feature ( keys %{ $self->_get_all_possible_features() } ) {
399 next FEATURE if defined $skip{$feature};
400 my $value_ok = 0;
401 if ( $feature eq 'GUMISMATCHES' ) {
402 $value_ok = 1;
403 }
404 if ( !defined $self->features->{$feature} ) {
405 my $lc_f = lc $feature;
406 my $lc_i = $lc_f . '_isset';
407 if ( $self->settings->$lc_i
408 && $self->settings->$lc_f ne $value_ok )
409 {
410 $self->warn( $lc_f . ' not available in this back-end.' );
411 }
412 }
413 }
414 return;
415 }
416  
417 sub _check_database {
418 my ($self) = @_;
419  
420 if ( !$self->settings->datapath_isset ) {
421 $self->settings->datapath(q{./});
422 }
423  
424 # check if database is set and valid
425 my $found_database = 0;
426 if ( defined $self->settings->database ) {
427 $self->settings->database(
428 $self->is_word( $self->settings->database ) );
429 my %available_dbs = $self->get_databases;
430 if ( !defined $available_dbs{ $self->settings->database } ) {
431 $self->throw(
432 -class => 'Bio::Root::BadParameter',
433 -text => 'Database not found.',
434 -value => $self->settings->database,
435 );
436 }
437 }
438 else {
439 $self->throw(
440 -class => 'Bio::Root::BadParameter',
441 -text => 'Database not defined.',
442 );
443 }
444 return 1;
445 }
446  
447 sub _check_sort_mode {
448 my ($self) = @_;
449  
450 if ( $self->settings->sort_isset ) {
451 my %sort_modes = $self->available_sort_modes();
452 if ( defined $sort_modes{ $self->settings->sort } ) {
453 my ($sort_mode) = $self->settings->sort =~ /(\w+)/xms;
454 $self->settings->sort($sort_mode); #make taint happy
455 }
456 else {
457 $self->throw(
458 -class => 'Bio::Root::BadParameter',
459 -text => 'Sort mode not valid.',
460 -value => 'sort mode'
461 );
462 }
463 }
464 return 1;
465 }
466  
467 ### END OF SETTINGS TESTS
468  
469 # copies the specified fasta file in the data directory and cerate a file
470 # <databasename>.nfo with the description, specified in the optional 2nd
471 # argument
472 # changes directory! so please save the oldpath before calling this method
473 sub _copy_fasta_file_and_create_nfo {
474 my ( $self, $args ) = @_;
475  
476 # throw exception if filename looks wrong
477 $self->is_word( $args->{basefilename}, 'Fasta filename' );
478  
479 my $newfile = $self->_cat_path_filename( $self->settings->datapath,
480 $args->{basefilename} );
481  
482 $args->{filename} = $newfile;
483  
484 my %dbs = $self->get_databases;
485  
486 if ( defined $dbs{ $args->{basefilename} } ) {
487 $self->warn( "Database with that name already exists.\n"
488 . 'Skipping database generation.' );
489 $args->{skip} = 1;
490 return;
491 }
492  
493 if ( defined $args->{copy} && $args->{copy} ) {
494 copy( $args->{file}, $newfile )
495 or $self->throw(
496 -class => 'Bio::Root::IOException',
497 -text => q{Can't copy } . $args->{file} . " to $newfile",
498 -value => $OS_ERROR,
499 );
500 }
501 else {
502 my $abs_path = $self->is_path( abs_path( $args->{file} ) );
503 symlink $abs_path, $newfile || $self->throw(
504 -class => 'Bio::Root::IOException',
505 -text => q{Can't symlink } . $abs_path . " to $newfile",
506 -value => $OS_ERROR,
507 );
508 }
509 if ( defined $args->{description} ) {
510 open my $NFOFILE, '>', $newfile . '.nfo';
511 print ${NFOFILE} $args->{description}
512 or $self->_cannot_print("$newfile.nfo");
513 close $NFOFILE;
514 }
515 return;
516 }
517  
518 sub _guess_alphabet_of_file {
519 my ( $self, $filename ) = @_;
520 my $in = Bio::SeqIO->new( -file => $filename );
521 return $in->next_seq->alphabet;
522 }
523  
524 sub _bioseq_query {
525 my ($self) = @_;
526 my $query_obj = $self->settings->query;
527 my $query;
528  
529 if ( !defined $query_obj ) {
530 $self->throw(
531 -class => 'Bio::Root::BadParameter',
532 -text => 'Query not defined.',
533 );
534 }
535 my $db_alphabet
536 = $self->get_alphabet_of_database( $self->settings->database );
537  
538 if ( eval { $query_obj->isa('Bio::Seq') } ) {
539 $query = $query_obj->seq;
540 }
541 else {
542 $query = $query_obj;
543 if ( $query =~ m{\A \w+ \z}xms ) {
544 if ( $db_alphabet eq 'dna' ) {
545 $query =~ tr/uU/tT/;
546 }
547 $query_obj = Bio::Seq->new(
548 -id => '1',
549 -desc => 'Query',
550 -seq => $query
551 );
552 }
553 else {
554 $query_obj = Bio::Seq->new( -id => '1', -desc => 'Query' );
555 }
556 }
557 return ( $query, $query_obj, $db_alphabet );
558 }
559  
560 # prepares the query, for example calculating the reverse complement
561 # if necessary
562 # returns the prepared query. settings->query is unchanged!
563 sub _prepare_query {
564 my $self = shift;
565 my ( $query, $seq, $db_alphabet ) = $self->_bioseq_query();
566  
567 if ( $seq->alphabet ne $db_alphabet ) {
568 $self->throw(
569 -class => 'Bio::Root::BadParameter',
570 -text => 'Alphabet of query and database not equal',
571 -value => 'Seq: ' . $seq->alphabet . ", DB: $db_alphabet"
572 );
573 }
574 if ( $self->settings->reverse_complement
575 || $self->settings->direct_and_rev_com )
576 {
577 if ( $db_alphabet eq 'dna' ) {
578 if ( defined $self->features->{REVCOM_DEFAULT} ) {
579 $seq->desc( $seq->desc . ' (reverse complement)' );
580 }
581 elsif ( defined $self->features->{NATIVE_D_A_REV_COM}
582 && $self->settings->direct_and_rev_com )
583 {
584 }
585 else {
586 $query = $seq->revcom->seq;
587 $seq->desc( $seq->desc . ' (reverse complement)' );
588 $seq->seq($query);
589 }
590 }
591 else {
592 $self->throw(
593 -class => 'Bio::Root::BadParameter',
594 -text =>
595 'Reverse complement only available for DNA databases.',
596 );
597 }
598 }
599 elsif ( defined $self->features->{REVCOM_DEFAULT}
600 && $db_alphabet eq 'dna' )
601 {
602 $query = $seq->revcom->seq;
603 $seq->seq($query);
604 }
605  
606 # $self->settings->_real_query( uc $query );
607 $self->{_query_obj} = $seq;
608 return uc $query; # $self->settings->_real_query();
609 }
610  
611 sub generate_database_out_of_fastafile {
612 my ( $self, @args ) = @_;
613 $self->deprecated( "generate_database_out_of_fastafile() is deprecated.\n"
614 . q{Use generate_database() instead.} );
615 return $self->generate_database(@args);
616 }
617  
618 sub available_sort_modes {
619 my ($self) = @_;
620 return (
621 ga => 'ascending order of dG',
622 gd => 'descending order of dG',
623 );
624 }
625  
626 sub _get_sequences_from_bio_index {
627 my ( $self, $seqid ) = @_;
628  
629 $self->is_arrayref_of_size( $seqid, 1 );
630 my $indexfile = $self->settings->datapath . q{/}
631 . $self->settings->database . '.idx';
632 my $idx = Bio::Index::Fasta->new($indexfile);
633 my $string;
634 my $stringio = IO::String->new($string);
635 my $out = Bio::SeqIO->new( -fh => $stringio, -format => 'Fasta' );
636 foreach my $seqid ( @{$seqid} ) {
637 $out->write_seq( $idx->fetch($seqid) );
638 }
639 $stringio = IO::String->new($string);
640 return Bio::SeqIO->new( -fh => $stringio, -format => 'Fasta' );
641 }
642  
643 sub get_alphabet_of_database {
644 my ( $self, $db ) = @_;
645 my %dbs = $self->get_databases();
646 if ( !defined $dbs{$db} ) {
647 $self->throw(
648 -class => 'Bio::Root::BadParameter',
649 -text => 'Database not found',
650 -value => $db,
651 );
652 }
653 $db = $self->is_word($db);
654  
655 open my $ALFILE, '<',
656 $self->_cat_path_filename( $self->settings->datapath, $db . '.al1' );
657 my $lines = 0;
658 while ( my $line = <$ALFILE> ) {
659 $lines++;
660 }
661 close $ALFILE;
662 return $lines <= $DNA_ALPHABET_SIZE ? return 'dna' : return 'protein';
663 }
664  
665 sub _delete_output {
666 my ($self) = @_;
667 return 0 if !defined $self->_output_fn;
668 return 0 if !-e $self->_output_fn;
669 unlink $self->_output_fn
670 or $self->throw(
671 -class => 'Bio::Root::IOException',
672 -text => q{Can't remove } . $self->_output_fn,
673 -value => $OS_ERROR,
674 );
675 return 1;
676 }
677  
678 sub _execute_command {
679 my ( $self, $cmd ) = @_;
680 $self->_delete_output();
681 my ( $tmp_fh, $tmp_fn )
682 = tempfile( 'parser_XXXXXXXXXXXX', DIR => $self->settings->tmppath );
683 $self->_output_fh($tmp_fh);
684 $self->_output_fn($tmp_fn);
685 system "$cmd > $tmp_fn";
686  
687 return !$CHILD_ERROR;
688 }
689  
690 sub _execute_command_and_return_output {
691 my ( $self, $cmd ) = @_;
692 my ( $writer, $reader, $err );
693 my $pid = open3( $writer, $reader, $err, $cmd );
694 waitpid $pid, 0;
695 my $output = do { local $INPUT_RECORD_SEPARATOR = undef; <$reader> };
696  
697 # my $error = join q{}, <$err>;
698 return $output;
699 }
700  
701 sub _create_index_and_alphabet_file {
702 my ( $self, $filename ) = @_;
703 my $in = Bio::SeqIO->new( -file => $filename, -format => 'Fasta' );
704 my $alphabet = $in->next_seq()->alphabet();
705 if ( !defined $self->features->{PROTEINS} && $alphabet eq 'protein' ) {
706 $self->throw(
707 -class => 'Bio::Root::BadParameter',
708 -text => 'Back-end does not support protein data',
709 -value => $alphabet,
710 );
711 }
712  
713 # create a vmatch alphabet file
714 open my $ALFILE, '>', "$filename.al1";
715 if ( $alphabet eq 'dna' ) {
716 print ${ALFILE} "aA\ncC\ngG\ntTuU\nnsywrkvbdhmNSYWRKVBDHM\n"
717 or $self->_cannot_print("$filename.al1");
718 }
719 else {
720 print ${ALFILE}
721 "L\nV\nI\nF\nK\nR\nE\nD\nA\nG\nS\nT\nN\nQ\nY\nW\nP\nH\nM\nC\nXUBZ*\n"
722 or $self->_cannot_print("$filename.al1");
723 }
724 close $ALFILE;
725  
726 # create database from directory of fasta files
727 my $idx = Bio::Index::Fasta->new(
728 -filename => $filename . '.idx',
729 -write_flag => 1
730 );
731 $idx->make_index( ($filename) );
732  
733 return;
734 }
735  
736 sub _create_tmp_query_file {
737 my ($self) = @_;
738 my $s = $self->settings;
739 my $query_file = 0;
740 my ( $tmp_fh, $tmp_query_file );
741 if ( defined $s->query_file ) {
742 $query_file = $self->is_path( $s->query_file );
743 }
744 if ( $s->query_isset && $s->query_file_isset ) {
745 $self->throw(
746 -class => 'Bio::Root::BadParameter',
747 -text => 'Query and query_file are set. I am confused...',
748 -value => $s->query . ' and ' . $s->query_file,
749 );
750 }
751 my @query_seqs = ();
752  
753 my $query = q{};
754 if ( !$query_file ) {
755 $query = $self->_prepare_query();
756  
757 ( $tmp_fh, $tmp_query_file ) = tempfile(
758 'spatter_XXXXXXXXXXXX',
759 DIR => $s->tmppath,
760 UNLINK => !$ENV{BIOGREPDEBUG}
761 ); # don't delete when in debug mode, so
762 # we can reproduce it.
763  
764 # construct a temporary fasta file with the query for vmatch
765 my $seqobj = $self->{_query_obj};
766  
767 my $outseqio = Bio::SeqIO->new(
768 -fh => $tmp_fh,
769 -format => 'fasta'
770 );
771 $outseqio->write_seq($seqobj);
772  
773 push @query_seqs, $seqobj;
774 if ( $s->direct_and_rev_com
775 && !defined $self->features->{NATIVE_D_A_REV_COM} )
776 {
777 my $seqobj2 = $seqobj->revcom;
778 if ( defined $self->features->{REVCOM_DEFAULT} ) {
779 my ($desc)
780 = $seqobj->desc
781 =~ m{\A(.*) \s\(reverse\scomplement\) \z}xms;
782 $seqobj2->desc($desc);
783 }
784 else {
785 $seqobj2->desc( $seqobj->desc . ' (reverse complement)' );
786 }
787 $outseqio->write_seq($seqobj2);
788 push @query_seqs, $seqobj2;
789 }
790  
791 }
792 else {
793 $tmp_query_file = $query_file;
794 my $query_in = Bio::SeqIO->new( -file => $tmp_query_file );
795 while ( my $query_seq = $query_in->next_seq ) {
796 push @query_seqs, $query_seq;
797 }
798 }
799 $self->_query_seqs(@query_seqs);
800 return ( $query, $query_file, $tmp_query_file );
801 }
802  
803 sub _prepare_generate_database {
804 my ( $self, @args ) = @_;
805 my $reftype = reftype $args[0];
806 my %args;
807 if ( !defined $reftype ) {
808 $self->deprecated(
809 'List argument for generate_database() is deprecated. '
810 . 'Use hashref instead.' );
811 %args = (
812 format => 'Fasta',
813 file => $args[0],
814 description => $args[1],
815 copy => 0,
816 );
817 }
818 elsif ( $reftype eq 'HASH' ) {
819 %args = %{ $args[0] };
820 }
821 else {
822 $self->throw(
823 -class => 'Bio::Root::BadParameter',
824 -text => 'Invalid arguments. Hashref assumed.',
825 );
826 }
827  
828 if ( !-e $args{file} ) {
829 $self->throw(
830 -class => 'Bio::Root::BadParameter',
831 -text => 'No such file.',
832 -value => $args{file},
833 );
834 }
835 my ($filename) = fileparse( $args{file} );
836 $args{basefilename} = $filename;
837  
838 if ( !defined $args{format} ) {
839 $args{format} = 'Fasta';
840 }
841 if ( defined $args{prefix_length} ) {
842  
843 # untaint pl
844 $args{prefix_length} = $self->is_integer( $args{prefix_length} );
845 }
846 if ( defined $args{datapath} ) {
847 $self->settings->datapath( $args{datapath} );
848 }
849 if ( !defined $self->settings->datapath ) {
850 $self->settings->datapath(q{./});
851 }
852 $self->_copy_fasta_file_and_create_nfo( \%args );
853  
854 return %args;
855 }
856  
857 sub _parse_regions {
858 my ( $self, $args ) = @_;
859 my $upstream_seq = q{};
860 my $subject_seq = substr $args->{complete_seq}, $args->{subject_begin},
861 $args->{subject_end} - $args->{subject_begin};
862 my $downstream_seq = q{};
863  
864 # initialized for assert below
865 my $upstream_begin = $args->{subject_begin};
866  
867 if ( $self->settings->upstream > 0 || $self->settings->downstream > 0 ) {
868  
869 # coordinates of upstream region, check if available region is
870 # as large as requested
871 $upstream_begin = $args->{subject_begin} - $self->settings->upstream;
872 if ( $upstream_begin < 0 ) {
873 $upstream_begin = 0;
874 }
875  
876 $upstream_seq = substr $args->{complete_seq}, $upstream_begin,
877 $args->{subject_begin} - $upstream_begin;
878  
879 # and same for the downstream region
880 my $downstream_end
881 = $args->{subject_end} + $self->settings->downstream;
882  
883 if ( $downstream_end > length $args->{complete_seq} ) {
884 $downstream_end = length $args->{complete_seq};
885 }
886  
887 $downstream_seq = substr $args->{complete_seq}, $args->{subject_end},
888 $downstream_end - $args->{subject_end};
889  
890 }
891 my @ret = ( $upstream_seq, $subject_seq, $downstream_seq );
892 assert(
893 index( $args->{complete_seq}, join( q{}, @ret ), $upstream_begin )
894 == $upstream_begin )
895 if DEBUG;
896 return @ret;
897 }
898  
899 1; # Magic true value required at end of module
900 __END__
« Grep/Backend/Vmatch.pm ↑ Summary Grep/Filter/FilterI.pm »