URLify
[bioperl-live.git] / t / RemoteDB / GenBank.t
blob62fa9a4085e09261ee38b94ccdd284292ceb4452
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 => 44,
11                            -requires_modules => [qw(IO::String
12                                                                             LWP::UserAgent
13                                                                                 HTTP::Request::Common)],
14                            -requires_networking => 1);
16         use_ok('Bio::DB::GenBank');
19 my %expected_lengths = (
20     'MUSIGHBA1' => 408,
21     'AF303112'  => 1611,
22     'AF303112.1' => 1611,
23     'AF041456'  => 1156,
24     'CELRABGDI' => 1743,
25     'JH374761'  => 38055
28 my ($gb, $seq, $seqio, $seqin);
31 # Bio::DB::GenBank
34 ok $gb = Bio::DB::GenBank->new('-delay'=>0), 'Bio::DB::GenBank';
36 # get a single seq
37 SKIP: {
38     eval {$seq = $gb->get_Seq_by_id('J00522');1};
39     skip "Couldn't connect to Genbank with Bio::DB::GenBank.pm. Do you have network access? Skipping GenBank tests: $@", 4 if $@;
40     is $seq->length, $expected_lengths{$seq->display_id}, $seq->display_id;
41     eval {$seq = $gb->get_Seq_by_acc('AF303112');};
42     skip "Couldn't connect to Genbank with Bio::DB::GenBank.pm. Transient network problems? Skipping GenBank tests: $@", 3 if $@;
43     is $seq->length, $expected_lengths{$seq->display_id}, $seq->display_id;
44     eval {$seq = $gb->get_Seq_by_version('AF303112.1');};
45     skip "Couldn't connect to Genbank with Bio::DB::GenBank.pm. Transient network problems? Skipping GenBank tests: $@", 2 if $@;
46     is $seq->length, $expected_lengths{$seq->display_id}, $seq->display_id;
47     eval {$seq = $gb->get_Seq_by_gi('405830');};
48     skip "Couldn't connect to Genbank with Bio::DB::GenBank.pm. Transient network problems? Skipping GenBank tests: $@", 1 if $@;
49     is $seq->length, $expected_lengths{$seq->display_id}, $seq->display_id;
52 $seq = $seqio = undef;
54 # batch mode
55 SKIP: {
56     eval {$seqio = $gb->get_Stream_by_id([qw(J00522 AF303112 2981014)]);};
57     skip "Batch access test failed for Genbank. Skipping those tests", 4 if $@;
58     my $done = 0;
59     while (my $s = $seqio->next_seq) {
60         is $s->length, $expected_lengths{$s->display_id}, $s->display_id;
61         $done++;
62     }
63     skip('No seqs returned', 4) if !$done;
64     is $done, 3;
67 $seq = $seqio = undef;
69 # test the temporary file creation and fasta
70 ok $gb = Bio::DB::GenBank->new('-format' => 'fasta', '-retrievaltype' => 'tempfile', '-delay' => 0);
71 SKIP: {
72     eval {$seq = $gb->get_Seq_by_id('J00522');};
73     skip "Couldn't connect to complete GenBank tests with a tempfile with Bio::DB::GenBank.pm. Skipping those tests", 6 if $@;
74     # last part of id holds the key
75     is $seq->length, $expected_lengths{(split(/\|/,$seq->display_id))[-1]}, $seq->display_id;
76     eval {$seq = $gb->get_Seq_by_acc('AF303112');};
77     skip "Couldn't connect to complete GenBank tests with a tempfile with Bio::DB::GenBank.pm. Skipping those tests", 5 if $@;
78     # last part of id holds the key
79     is $seq->length, $expected_lengths{(split(/\|/,$seq->display_id))[-1]}, $seq->display_id;
80     # batch mode requires genbank format
81     $gb->request_format("gb");
82     eval {$seqio = $gb->get_Stream_by_id([qw(J00522 AF303112 2981014)]);};
83     skip "Couldn't connect to complete GenBank batch tests with a tempfile with Bio::DB::GenBank.pm. Skipping those tests", 4 if $@;
84     my $done = 0;
85     while (my $s = $seqio->next_seq) {
86         is $s->length, $expected_lengths{$s->display_id};
87         undef $gb; # test the case where the db is gone,
88         # but a temp file should remain until seqio goes away.
89         $done++;
90     }
91     skip('No seqs returned', 4) if !$done;
92     is $done, 3;
95 $seq = $seqio = undef;
97 # test pipeline creation
98 ok $gb = Bio::DB::GenBank->new('-retrievaltype' => 'pipeline', '-delay' => 0);
99 SKIP: {
100     eval {$seq = $gb->get_Seq_by_id('J00522');};
101     skip "Couldn't connect to complete GenBank tests with a pipeline with Bio::DB::GenBank.pm. Skipping those tests", 6 if $@;
102     is $seq->length, $expected_lengths{$seq->display_id}, $seq->display_id;
103     eval {$seq = $gb->get_Seq_by_acc('AF303112');};
104     skip "Couldn't connect to complete GenBank tests with a pipeline with Bio::DB::GenBank.pm. Skipping those tests", 5 if $@;
105     is $seq->length, $expected_lengths{$seq->display_id}, $seq->display_id;
106     eval {$seqio = $gb->get_Stream_by_id([qw(J00522 AF303112 2981014)]);};
107     skip "Couldn't connect to complete GenBank tests with a pipeline with Bio::DB::GenBank.pm. Skipping those tests", 4 if $@;
108     my $done = 0;
109     while (my $s = $seqio->next_seq) {
110         is $s->length, $expected_lengths{$s->display_id}, $s->display_id;
111         undef $gb; # test the case where the db is gone,
112         # but the pipeline should remain until seqio goes away
113         $done++;
114     }
115     skip('No seqs returned', 4) if !$done;
116     is $done, 3;
119 $seq = $seqio = undef;
121 # test contig retrieval
122 ok $gb = Bio::DB::GenBank->new('-delay'  => 0, '-format' => 'gbwithparts');
123 SKIP: {
124     eval {$seq = $gb->get_Seq_by_id('JH374761');};
125     skip "Couldn't connect to GenBank with Bio::DB::GenBank.pm. Skipping those tests", 3 if $@;
126     is $seq->length, $expected_lengths{$seq->display_id}, $seq->display_id;
127     # now to check that postprocess_data in NCBIHelper catches CONTIG...
128     ok $gb = Bio::DB::GenBank->new('-delay' => 0, '-format' => 'gb');
129     eval {$seq = $gb->get_Seq_by_id('JH374761');};
130     skip "Couldn't connect to GenBank with Bio::DB::GenBank.pm. Skipping those tests", 1 if $@;
131     is $seq->length, $expected_lengths{$seq->display_id}, $seq->display_id;
134 $seq = $seqio = undef;
136 # bug 1405
137 my @result;
138 ok $gb = Bio::DB::GenBank->new(-format => 'Fasta', -seq_start  => 2, -seq_stop   => 7);
139 SKIP: {
140     eval {$seq = $gb->get_Seq_by_acc("A11111");};
141     skip "Couldn't connect to complete GenBank tests. Skipping those tests", 15 if $@;
142     is $seq->length, 6;
143     # complexity tests
144     ok $gb = Bio::DB::GenBank->new(-format => 'Fasta', -complexity => 0);
145     eval {$seqin = $gb->get_Stream_by_acc("5");};
146     skip "Couldn't connect to complete GenBank tests. Skipping those tests", 13 if $@;
147     @result = (1136, 'dna', 342, 'protein');
148     while ($seq = $seqin->next_seq) {
149         is $seq->length, shift(@result);
150         is $seq->alphabet, shift(@result);
151     }
152     is @result, 0;
153     # Real batch retrieval using epost/efetch
154     # these tests may change if integrated further into Bio::DB::Gen*
155     # Currently only useful for retrieving GI's via get_seq_stream
156     $gb = Bio::DB::GenBank->new();
157     eval {$seqin = $gb->get_seq_stream(-uids => [4887706 ,431229, 147460], -mode => 'batch');};
158     skip "Couldn't connect to complete GenBank batchmode epost/efetch tests. Skipping those tests", 8 if $@;
159     my %result = ('M59757' => 12611 ,'X76083'=> 3140, 'J01670'=> 1593);
160         my $ct = 0;
161     while ($seq = $seqin->next_seq) {
162                 $ct++;
163                 my $acc = $seq->accession;
164         ok exists $result{ $acc };
165         is $seq->length, $result{ $acc };
166                 delete $result{$acc};
167     }
168     skip('No seqs returned', 8) if !$ct;
169         is $ct, 3;
170     is %result, 0;