[bug 2707]
[bioperl-live.git] / t / SeqIO.t
blob4f49ab77aa86d6ea2314a66ab973cb3dd4ef5e51
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 => 41);
11         
12         use_ok('Bio::SeqIO');
15 my $verbose = test_debug();
17 # Basic read and/or write tests for SeqIO. Specific tests for
18 # given module should go into their own file.
20 my @formats = qw(gcg fasta raw pir tab ace );
21 # The following files or formats are failing: swiss genbank interpro embl
23 foreach my $format (@formats) {
24         print "======== $format ========\n" if $verbose;
25         read_write($format);
28 sub read_write {
29         my $format = shift;
30         my $seq;
31         my $str = Bio::SeqIO->new(-file=> test_input_file("test.$format"),
32                                                           -format => $format);
33         ok $seq = $str->next_seq();
34         print "Sequence 1 of 2 from $format stream:\n", $seq->seq, "\n\n" if  $verbose;
35         unless ($format eq 'raw') {
36                 is $seq->id, 'roa1_drome',"ID for format $format";
37                 is $seq->length, 358;
38         }
39         
40         unless ($format eq 'gcg') { # GCG file can contain only one sequence
41                 ok $seq = $str->next_seq();
42                 print "Sequence 2 of 2 from $format stream:\n", $seq->seq, $seq->seq, "\n" if $verbose;
43         }
44         
45         my $outfile = test_output_file();
46         my $out = Bio::SeqIO->new(-file => ">$outfile",
47                                                           -format => $format);
48         ok $out->write_seq($seq);
49         if ($format eq 'fasta') {
50                 my $id_type;
51                 ok($id_type = $out->preferred_id_type('accession.version'),
52                         'accession.version');
53         }
54         
55         ok -s $outfile;
58 # from testformats.pl
59 SKIP: {
60     test_skip(-tests => 6, -requires_modules => [qw(Algorithm::Diff
61                                                     IO::ScalarArray
62                                                     IO::String)]);
63     use_ok('Algorithm::Diff');
64     eval "use Algorithm::Diff qw(diff LCS);";
65     use_ok('IO::ScalarArray');
66     use_ok('IO::String');
67     
68     my %files = ( 
69              #'test.embl'      => 'embl',
70              #'test.ace'       => 'ace',
71               'test.fasta'     => 'fasta',
72              #'test.game'      => 'game',
73               'test.gcg'       => 'gcg',
74              #'test.genbank'   => 'genbank',
75               'test.raw'       => 'raw',
76              #'test_badlf.gcg' => 'gcg'
77               );
78     
79     while( my ($file, $type) = each %files ) {
80         my $filename = test_input_file($file);
81         print "processing file $filename\n" if $verbose;
82         open(FILE, "< $filename") or die("cannot open $filename");
83         my @datain = <FILE>;
84         my $in = new IO::String(join('', @datain));
85         my $seqin = new Bio::SeqIO( -fh => $in,
86                     -format => $type);
87         my $out = new IO::String;
88         my $seqout = new Bio::SeqIO( -fh => $out,
89                      -format => $type);
90         my $seq;
91         while( defined($seq = $seqin->next_seq) ) {     
92         $seqout->write_seq($seq);
93         }
94         $seqout->close();
95         $seqin->close();
96         my $strref = $out->string_ref;
97         my @dataout = map { $_."\n"} split(/\n/, $$strref );
98         my @diffs = &diff( \@datain, \@dataout);
99         is @diffs, 0;
100         
101         if(@diffs && $verbose) {
102             foreach my $d ( @diffs ) {
103                 foreach my $diff ( @$d ) {
104                     chomp($diff->[2]);
105                     print $diff->[0], $diff->[1], "\n>", $diff->[2], "\n";
106                 }
107             }
108             print "in is \n", join('', @datain), "\n";
109             print "out is \n", join('',@dataout), "\n"; 
110         }
111     }