[BUG] bug 2598
[bioperl-live.git] / t / StandAloneBlast.t
blob7d044a41dd8cefd1c402a03a94d6ab314d835019
1 # -*-Perl-*- Test Harness script for Bioperl
2 # $Id$
4 use strict;
5 use File::Spec;
7 BEGIN { 
8         use lib 't/lib';
9     use BioperlTest;
10     
11     test_begin(-tests => 45);
12         
13         use_ok('Bio::Tools::Run::StandAloneBlast');
14         use_ok('Bio::SeqIO');
17 # Note: the swissprot and ecoli.nt data sets may be downloaded from
18 # ftp://ftp.ncbi.nih.gov/blast/db/FASTA
19 my $verbose = test_debug() || -1;
20 my $nt_database = 'ecoli.nt';
21 my $amino_database = 'swissprot';
22 my $evalue = 0.001;
23 my ($seq1,$seq2,$seq3,$seq4);
25 # Tests to check that "-attr" and "attr" and "a" all do the same thing
26 # http://bugzilla.open-bio.org/show_bug.cgi?id=1912
27 for my $p (qw(database db -d -database d)) {
28   my $f = Bio::Tools::Run::StandAloneBlast->new($p => $nt_database);
29   is $f->d(), $nt_database;
31 for my $p (qw(expect evalue -e -expect e)) {
32   my $f = Bio::Tools::Run::StandAloneBlast->new($p => $evalue);
33   is $f->e(), $evalue;
36 # NCBI blast params are case-sensitive, wublast aren't
37 my $ncbi_factory = Bio::Tools::Run::StandAloneBlast->new(-program => 'blastn');
38 isa_ok $ncbi_factory, 'Bio::Tools::Run::StandAloneBlast';
39 isa_ok $ncbi_factory, 'Bio::Tools::Run::StandAloneNCBIBlast';
40 my $wu_factory = Bio::Tools::Run::StandAloneBlast->new(-program => 'wublastn');
41 isa_ok $wu_factory, 'Bio::Tools::Run::StandAloneBlast';
42 isa_ok $wu_factory, 'Bio::Tools::Run::StandAloneWUBlast';
43 for my $p (qw(e E)) {
44   $ncbi_factory->$p($p);
45   $wu_factory->$p($p);
47 is $ncbi_factory->e, 'e';
48 is $ncbi_factory->E, 'E';
49 is $wu_factory->e, 'E';
50 is $wu_factory->E, 'E';
52 # blastall switches like -I should take boolean but return 'T' or 'F' once set
53 is $ncbi_factory->I, undef;
54 is $ncbi_factory->I(1), 'T';
55 is $ncbi_factory->I(0), 'F';
56 is $ncbi_factory->I('T'), 'T';
57 is $ncbi_factory->I('F'), 'F';
59 # We should be able to set -F "m D" in an intuitive way, and also by manually
60 # quoting the value ourselves
61 $ncbi_factory->F('m D');
62 my $param_string = $ncbi_factory->_setparams('blastall');
63 like $param_string, qr/-F "m D"/;
64 $ncbi_factory->F('"m S"');
65 $param_string = $ncbi_factory->_setparams('blastall');
66 like $param_string, qr/-F "m S"/;
67 $ncbi_factory->F("'m D'");
68 $param_string = $ncbi_factory->_setparams('blastall');
69 like $param_string, qr/-F 'm D'/;
71 # dashed parameters should work
72 my $outfile = test_output_file();
73 ok my $factory = Bio::Tools::Run::StandAloneBlast->new(-verbose     => $verbose,
74                                                                                                            -program     => 'blastn',
75                                                                                                            -database    => $nt_database , 
76                                                                                                            -_READMETHOD => 'SearchIO', 
77                                                                                                            -output      => $outfile,
78                                                                                                            -verbose     => 0);
79 is $factory->database, $nt_database;
81 # Setup and then do tests that actually run blast
83 my @params = ('program'     => 'blastn',
84                           'database'    => $nt_database , 
85                           '_READMETHOD' => 'SearchIO', 
86                           'output'      => $outfile,
87                           'verbose'     => 0 );
88 ok $factory = Bio::Tools::Run::StandAloneBlast->new('-verbose' => $verbose, @params);
90 my $inputfilename = test_input_file('test.txt');
92 is $factory->quiet(0), 0;
93 is $factory->q(-3), -3;
95 SKIP: {
96         skip 'blastall not installed, skipping tests', 12 unless $factory->executable('blastall');
97         skip 'must have BLASTDIR, BLASTDB or BLASTDATADIR env variable set, skipping tests', 12 unless defined $Bio::Tools::Run::StandAloneBlast::DATADIR;
98         
99         my @testresults = qw(37 182 182  253 167 2);
100         my $testcount = 0;
101         
102         # use ecoli.nt
103         my $nt_database_file = File::Spec->catfile($Bio::Tools::Run::StandAloneBlast::DATADIR, $nt_database);
104         like $nt_database_file, qr/$nt_database/;
105         SKIP: {
106                 skip "Database $nt_database not found, skipping tests on it", 8 unless -e $nt_database_file;
107                 
108                 my $parser = $factory->blastall($inputfilename);
109                 my $blast_report = $parser->next_result;
110                 is $blast_report->num_hits, $testresults[$testcount++];
111                 
112                 $factory->_READMETHOD('blast_pull');  # Note required leading underscore in _READMETHOD
113                 my $str = Bio::SeqIO->new('-file' => test_input_file('dna2.fa'),
114                                                                   '-format' => 'fasta');
115                 $seq1 = $str->next_seq();
116                 $seq2 = $str->next_seq();
117                 
118                 my $pull_report = $factory->blastall($seq1);
119                 my $sbjct = $pull_report->next_result->next_hit;
120                 my $hsp = $sbjct->next_hsp;
121                 is $hsp->score, $testresults[$testcount];
122                 
123                 $factory->_READMETHOD('Blast');
124                 my $searchio_report = $factory->blastall($seq1);
125                 $sbjct = $searchio_report->next_result->next_hit;
126                 $hsp = $sbjct->next_hsp;
127                 is $hsp->score, $testresults[$testcount++];
128                 
129                 my @seq_array =($seq1,$seq2);
130                 my $seq_array_ref = \@seq_array;
131                 $factory->_READMETHOD('blast_pull');
132                 $pull_report = $factory->blastall($seq_array_ref);
133                 $sbjct = $pull_report->next_result->next_hit;
134                 $hsp = $sbjct->next_hsp;
135                 is $hsp->score, $testresults[$testcount];
136                 
137                 $factory->_READMETHOD('Blast');
138                 $searchio_report = $factory->blastall($seq_array_ref);
139                 $sbjct = $searchio_report->next_result->next_hit;
140                 $hsp = $sbjct->next_hsp;
141                 is $hsp->score, $testresults[$testcount++];
142                 
143                 ok $sbjct = $searchio_report->next_result->next_hit;
144                 $hsp = $sbjct->next_hsp;
145                 is $hsp->score, $testresults[$testcount++];
146                 
147                 @params = ('-verbose' => $verbose, 'program'  => 'blastp'); 
148                 $factory = Bio::Tools::Run::StandAloneBlast->new(@params);
149                 
150                 $str = Bio::SeqIO->new(-file => test_input_file('amino.fa'),
151                                                            -format => 'Fasta' );
152                 $seq3 = $str->next_seq();
153                 $seq4 = $str->next_seq();
154                 
155                 #*** no longer testing BPlite since deprecated, blast_pull doesn't support bl2seq yet
156                 #$factory->_READMETHOD('BPlite');
157                 #my $bl2seq_report = $factory->bl2seq($seq3, $seq4);
158                 #$hsp = $bl2seq_report->next_feature;
159                 #is $hsp->hit->start, $testresults[$testcount], "creating/parsing BPlite bl2seq report object";
160                 
161                 $factory->_READMETHOD('Blast');
162                 my $bl2seq_report = $factory->bl2seq($seq3, $seq4);
163                 $hsp = $bl2seq_report->next_result->next_hit->next_hsp;
164                 is $hsp->hit->start, $testresults[$testcount++], "creating/parsing SearchIO bl2seq report object";
165         }
166         
167         # use swissprot
168         my $amino_database_file = File::Spec->catfile($Bio::Tools::Run::StandAloneBlast::DATADIR, $amino_database);
169         SKIP: {
170                 skip "Database $amino_database not found, skipping tests on it", 3 unless -e $amino_database_file;
171                 
172                 @params = ('database' => $amino_database, '-verbose' => $verbose);
173                 $factory = Bio::Tools::Run::StandAloneBlast->new(@params);
174                 
175                 my $iter = 2;
176                 $factory->j($iter);    # 'j' is blast parameter for # of iterations
177                 my $new_iter = $factory->j();
178                 is $new_iter, 2, "set blast parameter";
179                 
180                 my $blast_report = $factory->blastpgp($seq3)->next_result;
181                 is $blast_report->number_of_iterations, $testresults[$testcount];
182                 
183                 $factory->_READMETHOD('blast_pull');
184                 $iter = 2;
185                 $factory->j($iter);    # 'j' is blast parameter for # of iterations
186                 $new_iter = $factory->j();
187                 is $new_iter, $iter, "set blast parameter";
188                 
189                 #*** no longer testing BPlite since deprecated, blast_pull doesn't support iterations yet
190                 #$blast_report = $factory->blastpgp($seq3);
191                 #is $blast_report->number_of_iterations, $testresults[$testcount];
192         }