fix deprecated usage warnings for perl 5.16
[bioperl-live.git] / t / SeqFeature / FeatureIO.t
blobc8583343f1991c816ffe310f741e11c23f063994
1 # -*-Perl-*- Test Harness script for Bioperl
2 # $Id$
4 use strict;
6 BEGIN {
7     use lib '.';
8     use Bio::Root::Test;
10     test_begin(-tests => 50,
11                -requires_module => 'Graph');
13     use_ok('Bio::FeatureIO');
16 my $io;
17 my $f;
18 my $s;
19 my $fcount;
20 my $scount;
22 ################################################################################
24 # use FeatureIO::gff to read a FASTA file.
26 $fcount = 0;
27 $scount = 0;
29 ok( $io = Bio::FeatureIO->new( -file => test_input_file('dna1.fa') ) );
31 #read features
32 while($f = $io->next_feature()){
33     warn $f;
34     $fcount++;
36 is($fcount, 0);
39 #then try to read sequences again.  should get seqs now
40 while($s = $io->next_seq()){
41     $scount++;
42     TODO: {
43         local $TODO = 'How did this ever work?!?';
44         if ($scount == 1) {
45             is($s->seq, 'Test1');
46         }
47     }
49 is($scount,  1);
51 ################################################################################
53 # use FeatureIO::gff to read a GFF3 file.
55 $fcount = 0;
56 $scount = 0;
58 ok( $io = Bio::FeatureIO->new( -file => test_input_file('knownGene.gff3') ) );
60 #try to read sequences first.  should be undef
61 while($s = $io->next_seq()){
62     $scount++;
64 is($scount,0);
66 #then read features
67 while($f = $io->next_feature()){
68     $fcount++;
70 is($fcount, 15);
72 #then try to read sequences again.  should still be undef
73 while($s = $io->next_seq()){
74     $scount++;
76 is($scount,0);
78 ################################################################################
80 # use FeatureIO::gff to read a GFF3 file w/ directivized FASTA tail
82 $fcount = 0;
83 $scount = 0;
85 ok( $io = Bio::FeatureIO->new( -file => test_input_file('hybrid1.gff3') ) );
87 #try to read sequences first.  should be undef
88 is $io->seqio, undef;
89 while($s = $io->next_seq()){
90     $scount++;
92 is($scount , 0);
94 #then read features
95 while($f = $io->next_feature()){
96     $fcount++;
98 is($fcount , 6);
100 #then try to read sequences again.
101 isa_ok $io->seqio, 'Bio::SeqIO';
102 while($s = $io->next_seq()){
103     $scount++;
104     isa_ok $s, 'Bio::Seq';
105     TODO: {
106         local $TODO = 'How did this ever work?!?';
107         if ($scount == 1) {
108             is($s->seq, 'Test1');
109         }
110     }
112 is($scount , 1);
114 ################################################################################
116 # use FeatureIO::gff to read a GFF3 file w/ non-directivized FASTA tail
118 $fcount = 0;
119 $scount = 0;
121 ok( $io = Bio::FeatureIO->new( -file => test_input_file('hybrid2.gff3') ) );
123 #try to read sequences first.  should be undef
124 while($s = $io->next_seq()){
125     $scount++;
127 is($scount , 0);
129 #then read features
130 while($f = $io->next_feature()){
131     $fcount++;
133 is($fcount , 6);
135 ################################################################################
137 # use FeatureIO::gff to read a GFF3 file of directives
139 $fcount = 0;
140 $scount = 0;
142 ok( $io = Bio::FeatureIO->new(-file => test_input_file('directives.gff3'),
143                               -verbose => test_debug() ? test_debug() : -1));
145 #read features
146 while($f = $io->next_feature()){
147     $fcount++;
149 is($fcount , 1); #sequence-region
151 ################################################################################
153 # use FeatureIO::gff to read a GFF3 file as aggregated feature groups
155 $fcount = 0;
156 $scount = 0;
158 ok( $io = Bio::FeatureIO->new( -file => test_input_file('hybrid1.gff3') ) );
160 #try to read sequences first.  should be undef
161 while($s = $io->next_seq()){
162     $scount++;
164 is($scount , 0);
166 #read feature groups
167 $f = $io->next_feature_group();
168 is($f , 1);
169 $f = $io->next_feature_group();
170 is($f , 0);
172 #then try to read sequences again.
173 while($s = $io->next_seq()){
174     $scount++;
175     TODO: {
176         local $TODO = 'How did this ever work?!?';
177         if ($scount == 1) {
178             is($s->seq, 'Test1');
179         }
180     }
182 is($scount , 1);
185 ################################################################################
187 # use FeatureIO::bed to read a bed file
189 ok($io = Bio::FeatureIO->new(-file => test_input_file('1.bed')));
191 ok($f = $io->next_feature);
192 # Check correct conversion of [0, feature-end+1) bed-coordinates into [1, feature-end]
193 # bioperl coordinates.  (here: bed [0, 10))
194 is($f->start, 1);
195 is($f->end, 10);
197 # Check field values.
198 my @tags = $f->get_tag_values("Name");
199 is(scalar(@tags), 1);
200 is($tags[0], "test-coordinates-1");
202 is($f->seq_id, "chr1");
208 ################################################################################
210 # use FeatureIO::ptt to read a PTT file.
212 $fcount = 0;
214 my $ptt_in = Bio::FeatureIO->new(
215     -file => test_input_file('test.ptt'),
216     -format => 'ptt',
217     );
218 ok($ptt_in);
220 while (my $f = $ptt_in->next_feature) {
221     $fcount++;
222     if ($fcount==2) {
223         # 2491..3423  + 310 24217063  metF  LB002 - COG0685E  5,10-methylenetetrahydrofolate reductase
224         is( $f->start , 2491 );
225         is( $f->end , 3423 );
226         cmp_ok( $f->strand, '>', 0 );
227         is( ($f->get_tag_values('PID'))[0],'24217063' );
228         is( ($f->get_tag_values('Gene'))[0], 'metF' );
229         is( ($f->get_tag_values('Synonym'))[0], 'LB002' );
230         ok( ! $f->has_tag('Code') );
231         is( ($f->get_tag_values('COG'))[0],'COG0685E' );
232         is( ($f->get_tag_values('Product'))[0], '5,10-methylenetetrahydrofolate reductase' );
233     }
235 is($fcount , 367);
237 ################################################################################
239 # use FeatureIO::vecscreen_simple to read a vecscreen file
244     my @expected_features =
245         (
246          {
247              'seq_id' => 'C02HBa0072A04.1',
248              'primary_tag' => 'moderate_match',
249              'end' => '60548',
250              'start' => '60522'
251          },
252          {
253              'seq_id' => 'SL_FOS91h17_SP6_0',
254              'primary_tag' => 'strong_match',
255              'end' => '122',
256              'start' => '60'
257          },
258          {
259              'seq_id' => 'SL_FOS91h18_T7_0',
260              'primary_tag' => 'strong_match',
261              'end' => '102',
262              'start' => '35'
263          },
264          {
265              'seq_id' => 'SL_FOS91h18_T7_0',
266              'primary_tag' => 'moderate_match',
267              'end' => '103',
268              'start' => '76'
269          },
270          {
271              'seq_id' => 'SL_FOS91h18_T7_0',
272              'primary_tag' => 'weak_match',
273              'end' => '104',
274              'start' => '82'
275          },
276          {
277              'seq_id' => 'SL_FOS91h18_T7_0',
278              'primary_tag' => 'suspect_origin',
279              'end' => '34',
280              'start' => '1'
281          },
282          {
283              'seq_id' => 'SL_FOS91i01_SP6_0',
284              'primary_tag' => 'strong_match',
285              'end' => '110',
286              'start' => '46'
287          },
288          {
289              'seq_id' => 'SL_FOS91i01_SP6_0',
290              'primary_tag' => 'suspect_origin',
291              'end' => '45',
292              'start' => '1'
293          },
294          {
295              'seq_id' => 'SL_FOS92b12_T7_0',
296              'primary_tag' => 'strong_match',
297              'end' => '108',
298              'start' => '41'
299          },
300          {
301              'seq_id' => 'SL_FOS92b12_T7_0',
302              'primary_tag' => 'moderate_match',
303              'end' => '109',
304              'start' => '82'
305          },
306          {
307              'seq_id' => 'SL_FOS92b12_T7_0',
308              'primary_tag' => 'weak_match',
309              'end' => '110',
310              'start' => '88'
311          },
312          {
313              'seq_id' => 'SL_FOS92b12_T7_0',
314              'primary_tag' => 'weak_match',
315              'end' => '1329',
316              'start' => '1313'
317          },
318          {
319              'seq_id' => 'SL_FOS92b12_T7_0',
320              'primary_tag' => 'suspect_origin',
321              'end' => '40',
322              'start' => '1'
323          },
324          {
325              'seq_id' => 'SL_FOS92b12_T7_0',
326              'primary_tag' => 'suspect_origin',
327              'end' => '1334',
328              'start' => '1330'
329          }
330         );
331     my @vs_features;
332     my $vs_in = Bio::FeatureIO->new( -file => test_input_file('vecscreen_simple.test_output'),
333                                      -format => 'vecscreen_simple',
334         );
335     ok( $vs_in );
336     while(my $feat = $vs_in->next_feature) {
337         push @vs_features,$feat;
338     }
340     #convert the array of feature objects to something that can more easily be checked with is_deeply
341     @vs_features = map {
342         my $f = $_;
343         my $rec = { map {$_ => $f->$_()} qw/start end primary_tag seq_id/ };
344     } @vs_features;
346     is_deeply(\@vs_features,\@expected_features,'vecscreen_simple gets the correct features');
349 ###############################################################################
351 # use FeatureIO to get some attributes from a GFF file
354 ok( $io = Bio::FeatureIO->new( -file => test_input_file('knownGene.gff3') ) );
356 while($f = $io->next_feature()){
357     if ($f->get_Annotations('Target')) {
358         # the last feature in the file should have a Target attribute
359         my $value_obj = $f->get_Annotations('Target');
360         is($value_obj->target_id, "BC046356.1%20space%20test"); 
361     }