Supply TEMPLATE and SUFFIX for temporary query sequence files.
[bioperl-run.git] / t / Infernal.t
blob906353a82556f209cf2670ed69c3617aecac241a
1 # -*-Perl-*-
2 ## Bioperl Test Harness Script for Modules
4 use strict;
5 our $NUMTESTS;
6 our %INFERNAL_TESTS;
8 BEGIN {
9     $NUMTESTS = 3; # base number of tests (those not in blocks)
11     # I have set up eutils tests to run in sections for easier test maintenance
12     # and keeping track of problematic tests. The below hash is the list of
13     # tests, with test number and coderef.
14     
15     # these now run very simple tests for connectivity and data sampling
16     # main tests now with the parser
18     %INFERNAL_TESTS = (
19         'params'        => {'tests' => 13,
20                             'sub'   => \&simple_param_tests},
21         'cmalign'       => {'tests' => 6,
22                             'sub'   => \&cmalign_norm},
23         # need to add merge tests
24         'cmsearch'      => {'tests' => 7,
25                             'sub'   => \&cmsearch},
26         'cmbuild'       => {'tests' => 4,
27                             'sub'   => \&cmbuild},
28         'cmstat'        => {'tests' => 2,
29                             'sub'   => \&cmstat},
30         # leave this one commented (may run for quite a while dep. on CPU)
31         #'cmcalibrate'   => {'tests' => 2,
32         #                    'sub'   => \&cmcalibrate},
33         'cmscore'       => {'tests' => 2,
34                             'sub'   => \&cmscore},
35         'cmemit'        => {'tests' => 6,
36                             'sub'   => \&cmemit},
37         );
38     $NUMTESTS += $INFERNAL_TESTS{$_}->{'tests'} for (keys %INFERNAL_TESTS);
40     use Bio::Root::Test;
41     
42     test_begin(-tests   => $NUMTESTS );
43     use_ok('Bio::Tools::Run::Infernal');
44     use_ok('Bio::SeqIO');
45     use_ok('Bio::AlignIO');
48 my $factory = Bio::Tools::Run::Infernal->new(-program     => 'cmsearch');
50 SKIP: {
51     test_skip(-requires_executable => $factory,
52              -tests => $NUMTESTS - 3);
53     for my $test (keys %INFERNAL_TESTS) {
54         $INFERNAL_TESTS{$test}->{'sub'}->();
55     }
58 # test out parameters and command string building
59 sub simple_param_tests {
60     my %executable = (
61         'cmsearch'   => {
62             params => {
63                 outfile  => 'foo.txt', # this won't get set (not a valid param for cmsearch)
64                 o        => 'bar.infernal', # test multiple outfile designations
65                 dna      => 1,
66                 tabfile  => 'tab.txt', # param
67                 gcfile   => 'gc.txt',
68                 g        => 'glarg',
69             },
70             test1  => 'cmsearch --dna -g --gcfile gc.txt --tabfile tab.txt -o bar.infernal baz.cm seq1.txt',
71             test2  => 'cmsearch --dna -g --gcfile gc.txt --tabfile tab.txt -o bar.infernal arg.cm seq1.txt',
72             seq_files => ['seq1.txt'],
73             align_files => [],
74         },
75         'cmemit'     => {
76             params => {
77                 -n => 10,
78                 -a => 1,
79                 -l => 1,
80                 -rna => 1,
81                 -tfile => 'trees.txt',
82                 -outfile_name => 'seqs.stk'
83             },
84             test1  => 'cmemit -a -l --rna -n 10 --tfile trees.txt baz.cm seqs.stk',
85             test2  => 'cmemit -a -l --rna -n 10 --tfile trees.txt arg.cm seqs.stk',
86             seq_files => [],
87             align_files => [],
88         },
89         'cmscore'    => {
90             params => {
91                 n => 10,
92                 a => 1,
93                 l => 1,
94                 rna => 1,
95                 mxsize => 4096,
96                 outfile => 'seqs.fna', # note this is different from outfile_name
97                 -outfile_name => 'seqs.stk'                
98                        },
99             test1  => 'cmscore -a -l --mxsize 4096 -n 10 --outfile seqs.fna baz.cm > seqs.stk',
100             test2  => 'cmscore -a -l --mxsize 4096 -n 10 --outfile seqs.fna arg.cm > seqs.stk',
101             seq_files => [],
102             align_files => [],
103         },
104         'cmalign'    => {
105             params => {
106                 -tau => 1e-7,
107                 -l => 1,
108                 -cyk => 1,
109                 -o => 'aligns.stk'
110                        },
111             test1  => 'cmalign --cyk -l --tau 1e-07 -o aligns.stk baz.cm seq1.fas',
112             test2  => 'cmalign --cyk -l --tau 1e-07 -o aligns.stk arg.cm seq1.fas',
113             test3  => 'cmalign --cyk -l --merge --tau 1e-07 -o aligns.stk arg.cm alns1.stk alns2.stk',
114             seq_files => ['seq1.fas'],
115             align_files => ['alns1.stk', 'alns2.stk'],
116          },
117         'cmbuild'    => {
118             params => {
119                 -binary => 1,
120                 -ehmmre => 12,
121                        },
122             test1  => 'cmbuild --binary --ehmmre 12 baz.cm alns1.stk',
123             test2  => 'cmbuild --binary --ehmmre 12 arg.cm alns1.stk',
124             seq_files => ['seq1.fas'],
125             align_files => ['alns1.stk'],
126                          },
127         'cmcalibrate'=> {
128             params => {
129                 -s => 12,
130                 '-exp-no-qdb'  => 1,  # note use of quotes
131                        },
132             test1  => 'cmcalibrate --exp-no-qdb -s 12 baz.cm',
133             test2  => 'cmcalibrate --exp-no-qdb -s 12 arg.cm',
134                         },
135     );
136     for my $exe (sort keys %executable) {
137         my %p = %{$executable{$exe}{params}};
138         $factory = Bio::Tools::Run::Infernal->new(-program     => $exe,
139                                                   -model_file  => 'baz.cm',
140                                                 %p);
141         like($factory->to_exe_string(-seq_files => $executable{$exe}{seq_files},
142                                    -align_files => $executable{$exe}{align_files}),
143             qr/$executable{$exe}{test1}/,"$exe parameter setting");
144         $factory->model_file('arg.cm');
145         like($factory->to_exe_string(-seq_files => $executable{$exe}{seq_files},
146                                    -align_files => $executable{$exe}{align_files}),
147             qr/$executable{$exe}{test2}/,"$exe parameter setting");
148         if ($exe eq 'cmalign') {
149             $factory->set_parameters(merge => 1);
150             like($factory->to_exe_string(-seq_files => $executable{$exe}{seq_files},
151                                    -align_files => $executable{$exe}{align_files}),
152             qr/$executable{$exe}{test3}/,"$exe parameter setting");
153         }
154     }
157 sub cmsearch {
158     my ($model, $input) = (test_input_file('purine.c.cm'), test_input_file('xprt.gb'));
159     my $factory = Bio::Tools::Run::Infernal->new(-model_file  => $model,
160                                                  -program     => 'cmsearch',  
161                                                  -verbose     => test_debug());
162     
163     SKIP: {
164     test_skip(-requires_executable => $factory,
165              -tests => 7);
166     
167     # from a file
168     my $searchio = $factory->cmsearch($input);
169     isa_ok($searchio, 'Bio::SearchIO', 'cmsearch works for v1.0 with file name;');
170     my $result = $searchio->next_result();
171     isa_ok($result, 'Bio::Search::Result::ResultI');
172     is($result->hits, 1);
173     my $hit = $result->next_hit;
174     is($hit->hsps, 4);
175     
176     # from a Bio::PrimarySeqI
177     my $seq = Bio::SeqIO->new(-format => 'genbank',
178                               -file   => $input)->next_seq;
179     $searchio = $factory->cmsearch($seq);
180     isa_ok($searchio, 'Bio::SearchIO', 'cmsearch works for v1.0 with PrimarySeq;');
181     $result = $searchio->next_result();
182     is($result->hits, 1);
183     $hit = $result->next_hit;
184     is($hit->hsps, 4);
185     }
188 sub cmbuild {
189     my ($aln, $outfile) = (test_input_file('purine.1.sto'), test_output_file());
190     if (-e $outfile) {
191         unlink $outfile;
192     }
193     my $factory = Bio::Tools::Run::Infernal->new(-model_file    => $outfile,
194                                                  -quiet         => 1,
195                                                  -program       => 'cmbuild',  
196                                                  -verbose       => test_debug());
197     SKIP: {
198     test_skip(-requires_executable => $factory,
199              -tests => 4);
200     
201     # from a file
202     my $success = $factory->cmbuild($aln);
203     ok($success, 'cmbuild works using alignment file');
204     ok(-e $outfile, 'CM file created');
205     
206     my $outfile2 = test_output_file();
207     if (-e $outfile2) {
208         unlink $outfile2;
209     }
210     $factory->model_file($outfile2);    
211     
212     # from a Bio::Align::AlignI
213     my $alnobj = Bio::AlignIO->new(-format => 'stockholm',
214                               -file   => $aln)->next_aln;
215     $success = $factory->cmbuild($alnobj);
216     ok($success, 'cmbuild works using Bio::Align::AlignI');
217     ok(-e $outfile2, 'CM file created');
218     }
221 sub cmstat {
222     my ($cm, $outfile) = (test_input_file('purine.c.cm'), test_output_file());
223     my $factory = Bio::Tools::Run::Infernal->new(-model_file    => $cm,
224                                                  -outfile_name  => $outfile,
225                                                  -program       => 'cmstat',  
226                                                  -verbose       => test_debug());
227     SKIP: {
228     test_skip(-requires_executable => $factory,
229              -tests => 2);
230     
231     my $success = $factory->cmstat();
232     ok($success, 'cmstat works');
233     ok (-e $outfile, 'cmstat outfile created');
234     }    
237 sub cmscore {
238     my ($cm, $outfile) = (test_input_file('purine.c.cm'), test_output_file());
239     my $factory = Bio::Tools::Run::Infernal->new(-model_file    => $cm,
240                                                  -outfile_name  => $outfile,
241                                                  -program       => 'cmscore',  
242                                                  -verbose       => test_debug());
243     SKIP: {
244     test_skip(-requires_executable => $factory,
245              -tests => 2);
246     
247     my $success = $factory->cmscore();
248     ok($success, 'cmscore works');
249     ok (-e $outfile, 'cmscore outfile created');
250     }    
253 sub cmalign_norm {
254     my ($cm, $seqfile) = (test_input_file('purine.c.cm'),
255                           test_input_file('purine.added.fa'));
256     my $factory = Bio::Tools::Run::Infernal->new(-model_file    => $cm,
257                                                  -program       => 'cmalign');
258     SKIP: {
259     test_skip(-requires_executable => $factory,
260              -tests => 6);
261     
262     my @seqs;
263     
264     my $stream = $factory->cmalign($seqfile);
265     isa_ok($stream, 'Bio::AlignIO', 'cmalign works');
266     my $aln = $stream->next_aln;
267     isa_ok($aln, 'Bio::Align::AlignI');
268     is($aln->num_sequences, 2);
269     
270     my $seqio = Bio::SeqIO->new(-format => 'fasta', -file => $seqfile);
271     
272     while (my $seq = $seqio->next_seq) {
273         push @seqs, $seq;
274     }
275     
276     $stream = $factory->cmalign(@seqs);
277     isa_ok($stream, 'Bio::AlignIO', 'cmalign works');
278     $aln = $stream->next_aln;
279     isa_ok($aln, 'Bio::Align::AlignI');
280     is($aln->num_sequences, 2);
281     }        
284 sub cmemit {
285     my ($cm, $outfile) = (test_input_file('purine.c.cm'), test_output_file());
286     my $factory = Bio::Tools::Run::Infernal->new(-model_file    => $cm,
287                                                  -program       => 'cmemit',
288                                                  -outfile_name  => $outfile,
289                                                  -quiet         => 1
290                                                  );
291     SKIP: {
292     test_skip(-requires_executable => $factory,
293              -tests => 6);
294     
295     # seqs (default)
296     my $stream = $factory->cmemit();
297     isa_ok($stream, 'Bio::SeqIO', 'cmemit works');
298     my $seq = $stream->next_seq;
299     isa_ok($seq, 'Bio::PrimarySeqI');
300     is($seq->display_id, 'Purine-1');
302     # alignment (-a flag)
303     $factory->set_parameters(-a => 1);
304     $stream = $factory->cmemit();
305     isa_ok($stream, 'Bio::AlignIO', 'cmemit works');
306     my $aln = $stream->next_aln;
307     isa_ok($aln, 'Bio::Align::AlignI');
308     is($aln->num_sequences, 10);
309     }
312 sub cmcalibrate {
313     my $cm = test_input_file('purine.cm');
314     my $factory = Bio::Tools::Run::Infernal->new(-model_file    => $cm,
315                                                  -program       => 'cmcalibrate');
316     SKIP: {
317     test_skip(-requires_executable => $factory,
318              -tests => 2);
319     
320     # seqs (default)
321     my $success = $factory->cmcalibrate();
322     ok($success, 'cmcalibrate successful');
323     cmp_ok(-M $cm, '<=' ,1);
324     }