fix skip miscounts
[bioperl-live.git] / t / Tools / Run / RemoteBlast.t
blob0f2a2636c8015b5d45df741b6487d062ee0a48f4
1 # -*-Perl-*- Test Harness script for Bioperl
2 # $Id$
4 use strict;
6 BEGIN {
7     use lib '.';
8     use Bio::Root::Test;
9     
10     test_begin(-tests => 21,
11                -requires_modules => [qw(IO::String LWP LWP::UserAgent)],
12                -requires_networking => 1);
13     
14     use_ok('Bio::Tools::Run::RemoteBlast');
17 my $prog = 'blastp';
18 my $db   = 'swissprot';
19 my $e_val= '1e-10';
20 my $v = test_debug();
22 SKIP: {
23     my $remote_blast = Bio::Tools::Run::RemoteBlast->new('-verbose' => $v,
24                                                     '-prog' => $prog,
25                                                     '-data' => $db,
26                                                     '-expect' => $e_val,
27                                     );
28     $remote_blast->submit_parameter('ENTREZ_QUERY', 'Escherichia coli[ORGN]');
29     my $inputfilename = test_input_file('ecolitst.fa');
30     ok( -e $inputfilename); 
31     
32     ok(1, 'Text BLAST');
33     
34     my $attempt = 1;
36     my $status;
37     eval{
38     $status = $remote_blast->submit_blast($inputfilename);
39     };
40     
41     ok($status,'BLAST text output submitted');
43     skip("Error accessing remote BLAST interface: $@", 3) if $@;
44     
45     cmp_ok($remote_blast->get_rtoe, '>', 0, "Estimated retrieval time");
46     
47     diag("Time remaining: ".$remote_blast->get_rtoe) if $v;
48     
49     my @rids = $remote_blast->each_rid;
50     is(@rids, 1, 'should only be one RID');
51     skip("Wrong number of RIDs: ".scalar(@rids), 2) if @rids != 1;
53     diag("Retrieving $rids[0]...\n") if $v;
54     my $rc;
55     while (defined($rc = $remote_blast->retrieve_blast($rids[0]))) {
56     if ( !ref($rc) ) {
57         if ( $rc < 0 ) {
58         skip("need a better solution for when 'Server failed to return any data'",2);
59         }
60         sleep 5;
61         diag("Retrieval attempt: $attempt\n") if $v;
62         $attempt++ < 10 ? next : last;
63     } else {
64         last
65     }
66     }
68     if ($rc) {
69     ok(1,'retrieve_blast succeeded');
70     $remote_blast->remove_rid($rids[0]);
71     my $count = 0;
72     isa_ok($rc, 'Bio::SearchIO');
73     while (my $result = $rc->next_result) {
74         while ( my $hit = $result->next_hit ) {
75         $count++;
76         next unless ( $v > 0);
77         print "sbjct name is ", $hit->name, "\n";
78         while ( my $hsp = $hit->next_hsp ) {
79             print "score is ", $hsp->bits, "\n";
80         } 
81         }
82     }
83     is($count, 3, 'HSPs returned');
84     } elsif ($attempt > 10) {
85     # have a test fail here (there should not be repeated failed attempts to
86     # get reports)
87     
88     ok(0,'Exceeded maximum attempts on server to retrieve report');
89     diag("Timeout, did not return report after ".($attempt - 1)." attempts");
90     } else {
91     # have a test fail here (whatever is returned should be eval as true and
92     # be a SearchIO)
93     
94     ok(0,"Other problem on remote server, no report returned: $rc");
95     skip('Possible remote server problems', 1);
96     }
99 SKIP: {
100     # test blasttable
102     my $remote_blast = Bio::Tools::Run::RemoteBlast->new
103       ('-verbose'    => $v,
104         '-prog'       => $prog,
105         '-data'       => $db,
106         '-readmethod' => 'blasttable',
107         '-expect'     => $e_val,
108       );
109     $remote_blast->submit_parameter('ENTREZ_QUERY', 'Escherichia coli[ORGN]');
110     
111     $remote_blast->retrieve_parameter('ALIGNMENT_VIEW', 'Tabular');
112     
113     my $inputfilename = test_input_file('ecolitst.fa');
114     my $attempt = 1;
116     my $status;
117     eval{
118     $status = $remote_blast->submit_blast($inputfilename);
119     };
120     
121     ok($status,'Tabular BLAST submitted');
123     skip("Error accessing remote BLAST interface: $@", 3) if $@;
124     
125     cmp_ok($remote_blast->get_rtoe, '>', 0, "Estimated retrieval time");
126     diag("Time remaining: ".$remote_blast->get_rtoe) if $v;
127     
128     my @rids = $remote_blast->each_rid;
129     is(@rids, 1, 'should only be one RID');
130     skip("Wrong number of RIDs: ".scalar(@rids), 2) if @rids != 1;
132     diag("waiting [$rids[0]]...") if $v;
133     my $rc;
134     while (defined($rc = $remote_blast->retrieve_blast($rids[0]))) {
135     if ( !ref($rc) ) {
136         if ( $rc < 0 ) {
137         skip("need a better solution for when 'Server failed to return any data'",2);
138         }
139         sleep 5;
140         diag("Retrieval attempt: $attempt") if $v;
141         $attempt++ < 10 ? next : last;
142     } else {
143         last
144     }
145     }
147     if ($rc) {
148     ok(1,'retrieve_blast succeeded');
149     $remote_blast->remove_rid($rids[0]);
150     my $count = 0;
151     isa_ok($rc, 'Bio::SearchIO');
152     while (my $result = $rc->next_result) {
153         while ( my $hit = $result->next_hit ) {
154         $count++;
155         next unless ( $v > 0);
156         print "sbjct name is ", $hit->name, "\n";
157         while ( my $hsp = $hit->next_hsp ) {
158             print "score is ", $hsp->bits, "\n";
159         } 
160         }
161     }
162     is($count, 3, 'HSPs returned');
163     } elsif ($attempt > 10) {
164     # have a test fail here (there should not be repeated failed attempts to
165     # get reports)
166     
167     ok(0,'Exceeded maximum attempts on server to retrieve report');
168     skip("Timeout, did not return report after ".($attempt - 1)." attempts", 1);
169     } else {
170     # have a test fail here (whatever is returned should be eval as true and
171     # be a SearchIO)
172     
173     ok(0,"Other problem on remote server, no report returned: $rc");
174     skip('Possible remote server problems', 1);
175     }
178 SKIP: {
179     test_skip(-tests => 5, -requires_module => 'Bio::SearchIO::blastxml');
180    
181     my $remote_blast = Bio::Tools::Run::RemoteBlast->new('-prog' => $prog,
182         '-data'       => $db,
183         '-readmethod' => 'xml',
184         '-expect'     => $e_val,
185     );
186     $remote_blast->submit_parameter('ENTREZ_QUERY', 
187                                     'Escherichia coli[ORGN]');
188     
189     $remote_blast->retrieve_parameter('FORMAT_TYPE', 'XML');
190     
191     my $inputfilename = test_input_file('ecolitst.fa');
192     my $attempt = 1;
194     my $status;
195     eval{
196     $status = $remote_blast->submit_blast($inputfilename);
197     };
198     
199     ok($status,'XML BLAST submitted');
201     skip("Error accessing remote BLAST interface: $@", 3) if $@;
202     
203     cmp_ok($remote_blast->get_rtoe, '>', 0, "Estimated retrieval time");
204     diag("Time remaining: ".$remote_blast->get_rtoe) if $v;
205     
206     my @rids = $remote_blast->each_rid;
207     is(@rids, 1, 'should only be one RID');
208     skip("Wrong number of RIDs: ".scalar(@rids), 2) if @rids != 1;
210     diag("waiting [$rids[0]]...") if $v;
211     my $rc;
212     while (defined($rc = $remote_blast->retrieve_blast($rids[0]))) {
213         if ( !ref($rc) ) {
214             if ( $rc < 0 ) {
215                 skip("need a better solution for when 'Server failed to return any data'",2);
216             }
217             sleep 5;
218             diag("Retrieval attempt: $attempt") if $v;
219             $attempt++ < 10 ? next : last;
220         } else {
221             last
222         }
223     }
225     if ($rc) {
226         ok(1,'retrieve_blast succeeded');
227         $remote_blast->remove_rid($rids[0]);
228         my $count = 0;
229         isa_ok($rc, 'Bio::SearchIO');
230         while (my $result = $rc->next_result) {
231             while ( my $hit = $result->next_hit ) {
232                 $count++;
233             }
234         }
235         is($count, 3, 'HSPs returned');
236     } elsif ($attempt > 10) {
237         # have a test fail here (there should not be repeated failed attempts to
238         # get reports)
240         ok(0,'Exceeded maximum attempts on server to retrieve report');
241         diag("Timeout, did not return report after ".($attempt - 1)." attempts");
242     } else {
243         # have a test fail here (whatever is returned should be eval as true and
244         # be a SearchIO)
246         ok(0,"Other problem on remote server, no report returned: $rc");
247         diag('Possible remote server problems');
248     }