squash waffling test
[bioperl-live.git] / t / LocalDB / Index / Index.t
blobf63740d9a4d8b30e0d5f180307da7a2712b1b68a
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 => 73,
11               -requires_modules => [qw(DB_File
12                                        Storable
13                                        Fcntl)]);
14    
15    use_ok('Bio::Index::Fasta');
16    use_ok('Bio::Index::Qual');
17    use_ok('Bio::Index::SwissPfam');
18    use_ok('Bio::Index::EMBL');
19    use_ok('Bio::Index::GenBank');
20    use_ok('Bio::Index::Stockholm');
21    use_ok('Bio::Index::Swissprot');
22    use_ok('Bio::Index::Hmmer');
23    use_ok('Bio::DB::InMemoryCache');
24    use_ok('Bio::DB::InMemoryCache');
27 my $ind = Bio::Index::Fasta->new(-filename => 'Wibbl',
28                                  -write_flag => 1,
29                                  -verbose => 0);
30 $ind->make_index(test_input_file('multifa.seq'));
31 $ind->make_index(test_input_file('seqs.fas'));
33 ok ( -e "Wibbl" || -e "Wibbl.pag" );
34 my $seq = $ind->fetch('HSEARLOBE');
35 is($seq->length,321);
36 is($seq->primary_id(),'HSEARLOBE');
37 $seq = $ind->fetch('HSMETOO');
38 is($seq->length,134);
39 is($seq->primary_id(),'HSMETOO');
40 $seq = $ind->fetch('MMWHISK');
41 is($seq->length,62);
42 is($seq->primary_id(),'MMWHISK');
43 $seq = $ind->fetch('gi|238775|bbs|65126');
44 is($seq->length,70);
46 my $stream = $ind->get_PrimarySeq_stream();
47 $seq = $stream->next_seq;
48 isa_ok $seq, 'Bio::PrimarySeqI';
50 $ind = Bio::Index::Fasta->new(-filename => 'multifa_index',
51                               -write_flag => 1,
52                               -verbose => 0);
53 $ind->make_index(test_input_file('multifa.seq.qual'));
55 ok ( -e "multifa_index" );
57 $ind = Bio::Index::Qual->new(-filename => 'multifa_qual_index',
58                              -write_flag => 1,
59                              -verbose => 0);
60 $ind->make_index(test_input_file('multifa.seq.qual'));
62 ok ( -e "multifa_qual_index" );
64 ok ( defined($seq) );
65 isa_ok $seq, 'Bio::SeqI';
66 $seq = $ind->fetch('HSEARLOBE');
67 is($seq->length,321);
68 is($seq->primary_id(),'HSEARLOBE');
69 $seq = $ind->fetch('HSMETOO');
70 is($seq->length,134);
71 is($seq->primary_id(),'HSMETOO');
72 $seq = $ind->fetch('MMWHISK');
73 is($seq->length,62);
74 is($seq->primary_id(),'MMWHISK');
75 $seq = $ind->fetch('NONEXISTENT_SEQ');
76 ok(! defined $seq);
78 $ind = Bio::Index::SwissPfam->new(-filename => 'Wibbl2',
79                                   -write_flag =>1);
80 $ind->make_index(test_input_file('swisspfam.data'));
82 ok ( -e "Wibbl2" || -e "Wibbl2.pag" );
84 $ind = Bio::Index::EMBL->new(-filename   => 'Wibbl3',
85                              -write_flag =>1);
86 $ind->make_index(test_input_file('test.embl'));
87 ok ( -e "Wibbl3" || -e "Wibbl3.pag" );
88 is ($ind->fetch('AL031232')->length, 4870);
90 $ind = Bio::Index::Swissprot->new(-filename   => 'Wibbl4',
91                                   -write_flag => 1);
92 $ind->make_index(test_input_file('roa1.swiss'));
93 ok ( -e "Wibbl4" || -e "Wibbl4.pag" );
94 $seq = $ind->fetch('ROA1_HUMAN');
95 is ($seq->display_id(), 'ROA1_HUMAN');
96 $seq = $ind->fetch('P09651');
97 is ($seq->display_id(), 'ROA1_HUMAN');
99 # test id_parser
100 $ind = Bio::Index::Swissprot->new(-filename   => 'Wibbl4',
101                                   -write_flag => 1);
102 $ind->id_parser(\&get_id);
103 $ind->make_index(test_input_file('roa1.swiss'));
104 ok ( -e "Wibbl4" || -e "Wibbl4.pag" );
105 $seq = $ind->fetch('X12671');
106 is ($seq->length,371);
109 my $gb_ind = Bio::Index::GenBank->new(-filename => 'Wibbl5',
110                                       -write_flag =>1,
111                                       -verbose    => 0);
112 $gb_ind->make_index(test_input_file('roa1.genbank'));
113 ok ( -e "Wibbl5" || -e "Wibbl5.pag" );
114 $seq = $gb_ind->fetch('AI129902');
115 is ($seq->length, 37);
116 is ($seq->species->binomial, 'Homo sapiens');
117 $seq = $gb_ind->fetch(3598416);
118 is ($seq->seq,"CTCCGCGCCAACTCCCCCCACCCCCCCCCCACACCCC");
120 my $cache = Bio::DB::InMemoryCache->new( -seqdb => $gb_ind );
122 ok ( $cache->get_Seq_by_id('AI129902') );
124 SKIP: {
125    test_skip(-tests => 22, -requires_module => 'Bio::DB::FileCache');
127    $cache = Bio::DB::FileCache->new(-seqdb => $gb_ind,
128                                     -keep  => 1,
129                                     -file  => 'filecache.idx');
130    # problem:
131    my $seq = $cache->get_Seq_by_id('AI129902');
132    ok ( $seq);
133    is ( $seq->length, 37);
134    is ( lc($seq->seq()), 'ctccgcgccaactccccccaccccccccccacacccc');
136    my ( $f1 ) = $seq->get_SeqFeatures();
137    is ( ($f1->get_tag_values('sex'))[0], 'female');
138    is ( ($f1->get_tag_values('lab_host'))[0], 'DH10B');
139    my $species = $seq->species;
140    ok( $species );
141    is( $species->binomial, 'Homo sapiens');
142    is( $species->species(), 'sapiens');
143    is( $species->genus(), 'Homo');
144    # changes in GenBank file SOURCE line
145    # this is now the abbreviated name
146    ok defined($species->name('abbreviated'));
147    is ($species->name('abbreviated')->[0], 'human');
149    $cache = undef;
150    $cache = Bio::DB::FileCache->new(-seqdb => $gb_ind,
151                                     -keep  => 0,
152                                     -file  => 'filecache.idx');
153    $seq = $cache->get_Seq_by_id('AI129902');
154    ok ( $seq);
155    is ( $seq->length, 37);
156    is ( lc($seq->seq()), 'ctccgcgccaactccccccaccccccccccacacccc');
158    ( $f1 ) = $seq->get_SeqFeatures();
159    is ( ($f1->get_tag_values('sex'))[0], 'female');
160    is ( ($f1->get_tag_values('lab_host'))[0], 'DH10B');
161    $species = $seq->species;
162    ok( $species );
163    is( $species->binomial, 'Homo sapiens');
164    is( $species->species(), 'sapiens');
165    is( $species->genus(), 'Homo');
166    # changes in GenBank file SOURCE line
167    # this is now the abbreviated name
168    ok defined($species->name('abbreviated'));
169    is ($species->name('abbreviated')->[0], 'human');
172 # test id_parser
173 $gb_ind = Bio::Index::GenBank->new(-filename => 'Wibbl5',
174                                    -write_flag =>1,
175                                    -verbose    => 0);
176 $gb_ind->id_parser(\&get_id);
177 $gb_ind->make_index(test_input_file('roa1.genbank'));
178 ok ( -e "Wibbl5" || -e "Wibbl5.pag" );
179 $seq = $gb_ind->fetch('alpha D-globin');
180 is ($seq->length,141);
182 # test Stockholm
183 my $st_ind = Bio::Index::Stockholm->new(-filename => 'Wibbl6',
184                                         -write_flag => 1,
185                                         -verbose    => 0);
186 isa_ok $st_ind, 'Bio::Index::Stockholm';
187 $st_ind->make_index(test_input_file('testaln.stockholm'));
188 ok ( -e "Wibbl6" );
189 my $aln = $st_ind->fetch_aln('PF00244');
190 isa_ok($aln,'Bio::SimpleAlign');
192 # test Hmmer
193 my $hmmer_ind = Bio::Index::Hmmer->new(-filename => 'Wibbl7',
194                                        -write_flag => 1,
195                                        -verbose    => 0);
196 isa_ok $hmmer_ind, 'Bio::Index::Hmmer';
197 $hmmer_ind->make_index(test_input_file('hmmpfam_multiresult.out'));
198 ok ( -e "Wibbl7" );
199 my $hmm_result = $hmmer_ind->fetch_report('lcl|gi|340783625|Plus1');
200 is ($hmm_result->query_description, 'megaplasmid, complete sequence [UNKNOWN]');
205 sub get_id {
206    my $line = shift;
207    return $1 if ($line =~ /product="([^"]+)"/);
208    return $1 if ($line =~ /^DR\s+EMBL;\s+([^;]+)/);
211 END {
212    cleanup();
215 sub cleanup {
216    for my $root ( qw( Wibbl Wibbl2 Wibbl3 Wibbl4 Wibbl5 Wibbl6 Wibbl7
217                       multifa_index multifa_qual_index ) ) {
218       unlink $root if( -e $root );
219       unlink "$root.pag" if( -e "$root.pag");
220       unlink "$root.dir" if( -e "$root.dir");
221    }