1 # -*-Perl-*- Test Harness script for Bioperl
12 test_begin(-tests => 275);
14 use_ok('Bio::DB::GFF');
17 my $fasta_files = test_input_file('dbfa');
18 my $gff_file1 = test_input_file('biodbgff', 'test.gff');
19 my $gff_file2 = test_input_file('biodbgff', 'test.gff3');
21 my $build = Module::Build->current;
22 my $test_dsn = $build->notes('test_dsn');
24 my $adaptor = $test_dsn ? $test_dsn : 'memory';
25 $adaptor = shift if @ARGV;
27 if ($adaptor =~ /sqlite/i) {
32 if ($adaptor =~ /^dbi/) {
34 $cfg->{dbd_driver} = $build->notes('dbd_driver');
35 $cfg->{test_db} = $build->notes('test_db');
36 $cfg->{test_host} = $build->notes('test_host');
37 $cfg->{test_user} = $build->notes('test_user');
38 $cfg->{test_pass} = $build->notes('test_pass');
39 $cfg->{test_dsn} = $build->notes('test_dsn');
41 $adaptor = "dbi::$cfg->{dbd_driver}" if $cfg->{dbd_driver};
42 @args = ( '-adaptor' => $adaptor,
43 '-dsn' => $cfg->{test_dsn},
45 push @args,('-user' => $cfg->{test_user}) if $cfg->{test_user};
46 push @args,('-pass' => $cfg->{test_pass}) if $cfg->{test_pass};
48 @args = ('-adaptor' => $adaptor,
52 push @args,('-aggregators' => ['transcript','processed_transcript']);
55 for my $FILE ($gff_file1,$gff_file2) {
57 my $db = eval { Bio::DB::GFF->new(@args) };
58 skip "DB load failed? Skipping all! $@", 278 if $@;
62 $db->gff3_name_munging(1);
64 # set the preferred groups
65 $db->preferred_groups( [ 'transcript', 'gene', 'mRNA' ] );
66 my @pg = $db->preferred_groups;
71 ok($db->initialize(1));
72 ok($db->load_gff($FILE));
73 ok($db->load_fasta($fasta_files));
76 my @types = sort $db->types;
78 is($types[0],'CDS:confirmed');
79 is($types[-1],'transposon:tc1');
80 my %types = $db->types('-enumerate'=>1);
81 is($types{'transposon:tc1'},2);
84 my $segment1 = $db->segment('Contig1');
87 is($segment1->length,37450);
88 is($segment1->start,1);
89 is($segment1->end,37450);
90 is($segment1->strand,1);
92 my $segment2 = $db->segment('Contig1',1=>1000);
93 is($segment2->length,1000);
94 is($segment2->start,1);
95 is($segment2->end,1000);
96 is($segment2->strand,1);
98 my $segment3 = $db->segment('Contig1',10=>1);
99 is($segment3->start,10);
100 is($segment3->end,1);
101 is($segment3->strand,-1);
103 # exercise attribute fetching
104 my @t = $db->fetch_feature_by_name(Transcript => 'trans-1');
105 my ($t) = grep {$_->type eq 'transcript:confirmed'} @t;
106 is($t->attributes('Note'),'function unknown');
107 is(join(' ',sort $t->attributes('Gene')),'abc-1 xyz-2');
108 my $att = $t->attributes;
109 is(scalar @{$att->{Gene}},2);
110 @t = sort {$a->display_name cmp $b->display_name} $db->fetch_feature_by_attribute('Gene'=>'abc-1');
113 my $seg = $db->segment('Contig1');
114 @t = $seg->features(-attributes=>{'Gene'=>'abc-1'});
116 is($seg->feature_count, 17);
117 @t = $seg->features(-attributes=>{'Gene'=>'xyz-2',Note=>'Terribly interesting'});
120 # exercise dna() a bit
121 my $dna = $segment2->dna;
122 is(length $dna,1000);
123 is(substr($dna,0,10),'gcctaagcct');
124 is($segment3->dna,'aggcttaggc');
125 is($segment1->dna, $db->dna($segment1->ref));
128 my $segment4 = $db->segment('-name'=>'c128.1','-class'=>'Transposon');
129 is($segment4->length,1000);
130 is($segment4->start,1);
131 is($segment4->end,1000);
132 is($segment4->ref,'c128.1');
133 is($segment4->strand,1);
134 ok(!$segment4->absolute);
136 $segment4->absolute(1);
137 ok($segment4->absolute);
138 is($segment4->ref,'Contig1');
139 is($segment4->start,5001);
140 $segment4->absolute(0);
141 my $tmp = $db->segment('Contig1',5001=>6000);
142 is($segment4->dna,$tmp->dna);
144 $segment4->ref('Contig1');
145 is($segment4->ref,'Contig1');
146 is($segment4->start,5001);
147 is($segment4->end,6000);
149 my $segment5 = $db->segment('-name'=>'c128.2','-class'=>'Transposon');
150 is($segment5->length,1000);
151 is($segment5->start,1);
152 is($segment5->end,1000);
153 is($segment5->ref,'c128.2');
154 is($segment5->strand,1);
156 $tmp = $db->segment('Contig1',9000,8001);
157 is($segment5->dna,$tmp->dna);
158 $segment5->absolute(1);
159 is($segment5->strand,-1);
162 # first two positive strand features
163 $segment4 = $db->segment('-name'=>'c128.1','-class'=>'Transposon');
164 my $start4 = $segment4->abs_start;
165 $segment5 = $db->segment('Transcript' => 'trans-1');
166 my $start5 = $segment5->abs_start;
167 $segment4->ref($segment5);
168 is($segment4->strand,1);
169 is($segment4->start,$start4-$start5+1);
170 is($segment4->stop,$start4-$start5+$segment4->length);
172 $segment4->ref('Transposon' => 'c128.1');
173 $segment5->ref('Transcript' => 'trans-1');
174 $segment5->ref($segment4);
175 is($segment5->start,$start5-$start4+1);
177 # now a positive on a negative strand feature
178 my $segment6 = $db->segment('Transcript'=>'trans-2');
179 my $start6 = $segment6->abs_start;
180 is($segment6->strand,1);
181 is($segment6->abs_strand,-1);
182 $segment6->ref($segment4);
183 is($segment6->start,$start6-$start4+1);
184 is($segment6->strand,-1);
186 $segment4->ref($segment6);
187 is($segment4->start,$start6-$start4+1);
188 is($segment4->strand,-1);
189 is($segment4->ref,$segment6);
191 # the reference sequence shouldn't affect the dna
192 $segment6 = $db->segment('Transcript'=>'trans-2');
193 $dna = $segment6->dna;
194 $segment6->ref($segment4);
195 is($segment6->dna,$dna);
197 # segments should refuse to accept a reference sequence on a foreign segment
199 my $result = eval { $segment6->ref('Contig2') };
201 like($@, qr/are on different sequence segments/);
203 # types across a segment
204 $segment1 = $db->segment('Contig1');
205 @types = sort $segment1->types;
207 is($types[0],'CDS:confirmed');
208 is($types[-1],'transposon:tc1');
209 %types = $segment1->types('-enumerate'=>1);
210 is($types{'similarity:est'},3);
212 # features across a segment
213 my @features = $segment1->features('-automerge'=>0);
214 is(scalar @features,17);
216 foreach (@features) {
217 $types_seen{$_->type}++;
219 my $inconsistency = 0;
220 foreach (keys %types,keys %types_seen) {
221 $inconsistency++ unless $types_seen{$_} == $types{$_};
225 @features = sort {$a->start<=>$b->start} @features;
227 # make sure that we can use features to get at dna
228 is($features[0]->dna,$db->segment('Contig1',$features[0]->start,$features[0]->end)->dna);
230 # check three forward features and three reverse features
231 # (This depends on the test.gff data)
233 $segment2 = $db->segment($features[$_],50,100);
234 if ($features[$_]->strand >= 0) {
235 is($segment2->dna,$db->segment('Contig1',
236 $features[$_]->start+50-1,
237 $features[$_]->start+100-1)->dna)
239 is($segment2->dna,$db->segment('Contig1',
240 $features[$_]->start-50+1,
241 $features[$_]->start-100+1)->dna)
245 # exercise the aggregator
246 my $aggregator = Bio::DB::GFF::Aggregator->new('-method' => 'aggregated_transcript',
247 '-main_method' => 'transcript',
248 '-sub_parts' => ['exon','CDS']);
249 $db->add_aggregator($aggregator);
250 $segment1 = $db->segment('Contig1');
251 @features = sort $segment1->features('aggregated_transcript'); # sort so that trans-1 comes first
252 is(scalar @features,2);
253 cmp_ok($features[0]->Exon, '>', 0);
254 cmp_ok($features[0]->Cds,'>', 0);
256 # Test that sorting is correct. The way that test.gff is set up, the lower one is
257 # on the + strand and the higher is on the -.
258 @features = sort {$a->start <=> $b->start} @features;
259 is($features[0]->strand,1);
260 is($features[1]->strand,-1);
264 foreach ($features[0]->Exon) {
265 $inconsistency++ if $_->start > $_->end;
266 $inconsistency++ if $last && $_->start < $last;
271 $inconsistency = $last = 0;
272 foreach ($features[1]->Exon) {
273 $inconsistency++ if $_->start < $_->end;
274 $inconsistency++ if $last && $_->start > $last;
279 # relative addressing in aggregated features
280 my $transcript1 = $db->segment($features[0]);
281 $transcript1->ref($features[0]);
282 my @overlap = sort {$a->start <=> $b->start } $transcript1->features;
283 is(scalar(@overlap),5);
284 is($overlap[0]->start,-999);
286 $transcript1 = $db->segment('Transcript' => 'trans-1');
287 @overlap = sort {$a->start <=> $b->start } $transcript1->features;
288 is($overlap[0]->start,-999);
290 # test strandedness of features
291 $segment1 = $db->segment('-class' => 'Transcript',
292 '-name' => 'trans-3',
295 is($segment1->strand,1);
296 @overlap = sort {$a->start <=> $b->start} $segment1->features('transcript');
297 is(scalar(@overlap),2);
298 is($overlap[0]->name,'trans-3');
299 is($overlap[1]->name,'trans-4');
300 is($overlap[0]->strand,1);
301 is($overlap[1]->strand,-1);
303 # testing feature id and group_id
304 my $tf = $overlap[0];
306 my $t1 = $db->fetch_feature_by_id($tf->id);
310 if (defined $tf->group_id) {
311 my $t2 = $db->fetch_feature_by_gid($tf->group_id);
312 is($t2->group_id,$tf->group_id);
313 is($t2->group_id,$t1->group_id);
315 skip("fetch_feature_by_gid() not implemented by this adaptor",2);
319 $segment1 = $db->segment('-class' => 'Transcript',
320 '-name' => 'trans-4',
323 is($segment1->strand,1);
324 @overlap = sort {$a->start <=> $b->start} $segment1->features('transcript');
325 is($overlap[0]->name,'trans-4');
326 is($overlap[1]->name,'trans-3');
327 is($overlap[0]->strand,1);
328 is($overlap[1]->strand,-1);
330 @overlap = sort {$a->start <=> $b->start} $segment1->features('Component');
331 is($overlap[0]->strand,0);
334 # test preferred group assignments
335 if ($FILE =~ /\.gff$/) {
336 my @gene = $db->get_feature_by_name( gene => 'gene-9' );
337 my @mrna = $db->get_feature_by_name( mRNA => 'trans-9' );
338 is($gene[0]->ref, 'Contig4');
339 is(scalar(@gene), 2);
340 is(scalar(@mrna), 1);
342 skip('preferred groups are not supported by gff3',3);
346 # test iterator across a segment
347 $segment1 = $db->segment('Contig1');
348 my $i = $segment1->features('-automerge'=>0,'-iterator'=>1);
350 while (my $s = $i->next_feature) {
351 $strand{$s->strand}++;
355 # test iterator across entire database
356 $i = $db->features('-automerge'=>0,'-iterator'=>1);
358 while (my $s = $i->next_feature) {
359 $strand{$s->strand}++;
363 # test iterator across a segment, limited by an attribute
364 $i = $seg->get_feature_stream(-attributes=>{'Gene'=>'abc-1',Note=>'function unknown'});
366 while ($i->next_seq) {
371 # test that aliases work
372 my $st1 = $db->segment(Transcript => 'trans-3');
374 my $st2 = $db->segment(Transcript => 'trans-18'); # this is an alias!
377 my @transcripts = $st1->features('transcript');
378 is(($transcripts[0]->aliases)[0],'trans-18');
381 $db->strict_bounds_checking(1);
382 my $tseg = $db->segment(-name=>'trans-1',-class=>'Transcript',-start=>1,-stop=>500);
383 ok(!$tseg->truncated);
384 $tseg = $db->segment(-name=>'trans-1',-class=>'Transcript',-start=>1,-stop=>50000);
385 ok($tseg->truncated);
386 $db->strict_bounds_checking(0);
387 $tseg = $db->segment(-name=>'trans-1',-class=>'Transcript',-start=>1,-stop=>50000);
388 ok(!$tseg->truncated);
390 # test the processed_transcript aggregator
391 $db->clear_aggregators;
392 $db->add_aggregator('processed_transcript');
393 my @f = $db->fetch_feature_by_name(mRNA => 'trans-8');
395 is($f[0]->length,35000-32000+1);
396 is(scalar $f[0]->CDS,3);
397 is(scalar $f[0]->UTR,2);
400 # segment delete() method
401 my $clone = $db->segment(Clone=>'M7.3');
402 my $overlapping_feature_count = $clone->features(-range_type =>'overlaps');
403 my $contained_feature_count = $clone->features(-range_type =>'contains');
404 is(scalar $clone->delete(-range_type=>'contains'),$contained_feature_count);
405 is(scalar $clone->features,$overlapping_feature_count - $contained_feature_count);
407 # database delete() method
408 is($db->delete(-type=>['mRNA:confirmed','transposon:tc1']),4);
409 is($db->delete(-type=>'UTR',-ref=>'Contig29'),undef);
410 is($db->delete(-type=>'CDS',-ref=>'AL12345.2',-class=>'Clone'),3);
411 is($db->delete_features(1,2,3),3);
415 is($db->delete_groups(1,2,3,4,5),5);
416 my @features = $db->get_feature_by_name(Sequence => 'Contig2');
417 is($db->delete_groups(@features),1);
420 if (!$result && $@ =~ /not implemented/i) {
421 skip("delete_groups() not implemented by this adaptor",2);
426 test_skip(-tests => 1, -excludes_os => 'mswin');
428 # test ability to pass adaptors across a fork
429 if (my $child = open(F,"-|")) { # parent reads from child
435 my @f = $db->features();
441 ok(!defined eval{$db->delete()});
442 ok($db->delete(-force=>1));
443 is(scalar $db->features,0);
444 ok(!$db->segment('Contig1'));
453 unlink $fasta_files."/directory.index";