Sync'ed RichSeqI with the implementation. RichSeq provides backward
[bioperl-live.git] / t / Index.t
blob5918472a612dcfc9fa8cab1b262d61ce2c699172
1 # -*-Perl-*-
2 # $Id$
4 use strict;
5 BEGIN {     
6     eval { require Test; };
7     use vars qw($NUMTESTS);
8     $NUMTESTS = 32;
9     if( $@ ) {
10         use lib 't';
11     }
12     use Test;
13     plan tests => $NUMTESTS;
16 use Bio::Root::IO;
17 use Bio::Index::Fasta;
18 use Bio::Index::SwissPfam;
19 use Bio::Index::EMBL;
20 use Bio::Index::GenBank;
21 use Bio::Index::Swissprot;
22 use Bio::DB::InMemoryCache;
23 use Bio::DB::GenPept;
24 use Bio::DB::GenBank;
26 eval { require Bio::DB::FileCache };
29 use vars qw ($dir);
31 ($Bio::Root::IO::FILESPECLOADED && File::Spec->can('cwd') && ($dir = File::Spec->cwd) ) ||
32     ($dir = `pwd`) || ($dir = '.');
34 END { 
35     foreach ( $Test::ntest..$NUMTESTS) {
36         skip('Bio::DB::FileCache not loaded because one or more of Storable, DB_File or File::Temp not installed',1);
37     }
39     foreach my $root ( qw( Wibbl Wibbl2 Wibbl3 Wibbl4 Wibbl5 
40                            ) ) {
41         if( -e "$root" ) { unlink $root;}
42         if( -e "$root.pag") { unlink "$root.pag";}
43         if( -e "$root.dir") { unlink "$root.dir";}
44    }
45  }
47 chomp( $dir );
49     my $ind = Bio::Index::Fasta->new(-filename => 'Wibbl', 
50                                      -write_flag => 1,
51                                      -verbose => 0);
52     $ind->make_index(Bio::Root::IO->catfile($dir,"t","data","multifa.seq"));
53     $ind->make_index(Bio::Root::IO->catfile($dir,"t","data","seqs.fas"));
54     $ind->make_index(Bio::Root::IO->catfile($dir,"t","data","multi_1.fa"));
57 ok ( -e "Wibbl" || -e "Wibbl.pag" );
60     my %t_seq = (
61         HSEARLOBE               => 321,
62         HSMETOO                 => 134,
63         MMWHISK                 => 62,
64         'gi|238775|bbs|65126'   => 70,
65     );
67     my $ind = Bio::Index::Abstract->new(-FILENAME => 'Wibbl');
69     my $ok_3 = 1;
70     while (my($name, $length) = each %t_seq) {
71         my $seq = $ind->fetch($name);
72         if( defined($seq) and $seq->isa('Bio::SeqI') ) {
73             my $r_length = $seq->length;
74             unless ($r_length == $length) {
75                 warn "$name - retrieved length '$r_length' doesn't match known length '$length'\n";
76                 $ok_3 = 0;
77             }
78         } else {
79             warn "Didn't get sequence '$name' from index\n";
80             $ok_3 = 0;
81         }
82     }
83     ok $ok_3;
85     my $stream = $ind->get_PrimarySeq_stream();
86     my $ok_4 = 1;
87     while( my $seq2 = $stream->next_seq ) {
88         unless ($seq2->isa('Bio::PrimarySeqI')) {
89             $ok_4 = 0;
90             last; # no point continuing...
91         }
92     }
93     ok $ok_4;
97     my $ind = Bio::Index::SwissPfam->new(-filename=>'Wibbl2', 
98                                          -write_flag=>1);
99     $ind->make_index(Bio::Root::IO->catfile($dir,"t","data","swisspfam.data"));
100     ok ( -e "Wibbl2" || -e "Wibbl2.pag" );
104     my $ind = Bio::Index::EMBL->new(-filename=>'Wibbl3', 
105                                     -write_flag=>1);
106     $ind->make_index(Bio::Root::IO->catfile($dir,"t","data","test.embl"));
107     ok ( -e "Wibbl3" || -e "Wibbl3.pag" );
108     ok $ind->fetch('AL031232')->length, 4870;
112     my $ind = Bio::Index::Swissprot->new(-filename=>'Wibbl4', 
113                                     -write_flag=>1);
114     $ind->make_index(Bio::Root::IO->catfile($dir,"t","data","roa1.swiss"));
115     ok ( -e "Wibbl4" || -e "Wibbl4.pag" );
116     ok ($ind->fetch('P09651')->display_id(), 'ROA1_HUMAN');
119 my $gb_ind;
121     $gb_ind = Bio::Index::GenBank->new(-filename=>'Wibbl5', 
122                                        -write_flag=>1, 
123                                        -verbose => 0);
124     $gb_ind->make_index(Bio::Root::IO->catfile($dir,"t","data","roa1.genbank"));
125     ok ( -e "Wibbl5" || -e "Wibbl5.pag" );
126     my $seq =$gb_ind->fetch('AI129902'); 
127     ok ($seq->length, 37);
128     ok ($seq->species->binomial, 'Homo sapiens');
131 my $cache = Bio::DB::InMemoryCache->new( -seqdb => $gb_ind );
133 ok ( $cache->get_Seq_by_id('AI129902') );
135 if (Bio::DB::FileCache->can('new')) {
136   
137     $cache = Bio::DB::FileCache->new(-seqdb => $gb_ind,
138                                      -keep  => 1,
139                                      -file  => 'filecache.idx');
140     my $seq = $cache->get_Seq_by_id('AI129902');
141     ok ( $seq);
142     ok ( $seq->length, 37);
143     ok ( lc($seq->seq()), 'ctccgcgccaactccccccaccccccccccacacccc');
144     
145     my ( $f1 ) = $seq->get_SeqFeatures();
146     ok ( ($f1->each_tag_value('sex'))[0], 'female');
147     ok ( ($f1->each_tag_value('lab_host'))[0], 'DH10B');
148     my $species = $seq->species;
149     ok( $species );
150     ok( $species->binomial, 'Homo sapiens');
151     ok( $species->species(), 'sapiens');
152     ok( $species->genus(), 'Homo');
153     ok ($species->common_name(), 'human');
155     $cache = undef;
156     $cache = Bio::DB::FileCache->new(-seqdb => $gb_ind,
157                                      -keep  => 0,
158                                      -file  => 'filecache.idx');
159     $seq = $cache->get_Seq_by_id('AI129902');
160     ok ( $seq);
161     ok ( $seq->length, 37);
162     ok ( lc($seq->seq()), 'ctccgcgccaactccccccaccccccccccacacccc');
163     
164     ( $f1 ) = $seq->get_SeqFeatures();
165     ok ( ($f1->each_tag_value('sex'))[0], 'female');
166     ok ( ($f1->each_tag_value('lab_host'))[0], 'DH10B');
167     $species = $seq->species;
168     ok( $species );
169     ok( $species->binomial, 'Homo sapiens');
170     ok( $species->species(), 'sapiens');
171     ok( $species->genus(), 'Homo');
172     ok ($species->common_name(), 'human');    
173 } else {
174   skip('Bio::DB::FileCache not loaded because one or more of Storable, DB_File or File::Temp not installed',1);