Remove manipulation of @INC and use of lib - require install for use.
[bioperl-live.git] / t / SeqIO / embl.t
blob15f821cfd5e0e3bb785e27648272ac9e129f979a
1 # -*-Perl-*- Test Harness script for Bioperl
2 # $Id$
4 use strict;
6 BEGIN {
7     use Bio::Root::Test;
9     test_begin(-tests => 100);
11     use_ok('Bio::SeqIO::embl');
14 my $verbose = test_debug();
16 my $ast = Bio::SeqIO->new( -format => 'embl',
17                            -verbose => $verbose,
18                            -file => test_input_file('roa1.dat'));
19 $ast->verbose($verbose);
20 my $as = $ast->next_seq();
21 ok defined $as->seq;
22 is($as->display_id, 'HSHNCPA1');
23 is($as->accession_number, 'X79536');
24 is($as->seq_version, 1);
25 is($as->version, 1);
26 is($as->desc, 'H.sapiens mRNA for hnRNPcore protein A1');
27 is($as->molecule, 'RNA');
28 is($as->alphabet, 'rna');
29 is(scalar $as->all_SeqFeatures(), 4);
30 is($as->length, 1198);
31 is($as->species->binomial(), 'Homo sapiens');
33 # EMBL Release 87 changes (8-17-06)
35 $ast = Bio::SeqIO->new( -format => 'embl',
36                         -verbose => $verbose,
37                         -file => test_input_file('roa1_v2.dat'));
38 $ast->verbose($verbose);
39 $as = $ast->next_seq();
40 ok defined $as->seq;
41 # accession # same as display name now
42 is($as->display_id, 'X79536'); 
43 is($as->accession_number, 'X79536');
44 is($as->seq_version, 1);
45 is($as->version, 1);
46 is($as->desc, 'H.sapiens mRNA for hnRNPcore protein A1');
47 # mRNA instead of RNA
48 is($as->molecule, 'mRNA');
49 is($as->alphabet, 'rna');
50 is(scalar $as->all_SeqFeatures(), 4);
51 is($as->length, 1198);
52 is($as->species->binomial(), 'Homo sapiens');
54 my $ent = Bio::SeqIO->new( -file => test_input_file('test.embl'),
55                            -format => 'embl');
56 my $seq = $ent->next_seq();
58 is(defined $seq->seq(), 1,
59    'success reading Embl with ^ location and badly split double quotes');
60 is(scalar $seq->annotation->get_Annotations('reference'), 3);
62 my $out_file = test_output_file();
63 my $out = Bio::SeqIO->new(-file=> ">$out_file",
64                           -format => 'embl');
65 is($out->write_seq($seq),1,
66    'success writing Embl format with ^ < and > locations');
68 # embl with no FT
69 $ent = Bio::SeqIO->new( -file => test_input_file('test.embl'),
70                         -format => 'embl');
71 $seq = $ent->next_seq();
73 ok($seq);
74 is(lc($seq->subseq(1,10)),'gatcagtaga');
75 is($seq->length, 4870);
77 # embl with no FH
78 my $noFH = Bio::SeqIO->new( -file => test_input_file('no_FH.embl'),
79                             -format => 'embl');
80 is(scalar($noFH->next_seq->get_SeqFeatures), 4);
82 # bug 1571
83 $ent = Bio::SeqIO->new( -format => 'embl',
84                         -file   => test_input_file('test.embl2sq'));
85 is($ent->next_seq->length,4877);
87 # embl repbase
88 $ent = Bio::SeqIO->new(-file => test_input_file('BEL16-LTR_AG.embl'), -format => 'embl');
89 $seq = $ent->next_seq;
90 is($seq->display_id,'BEL16-LTR_AG');
92 # test secondary accessions in EMBL (bug #1332)
93 my $seqio = Bio::SeqIO->new( -format => 'embl',
94                              -file => test_input_file('ECAPAH02.embl'));
95 $seq = $seqio->next_seq;
96 is($seq->accession_number, 'D10483');
97 is($seq->seq_version, 2);
98 my @accs = $seq->get_secondary_accessions();
99 is($accs[0], 'J01597');
100 is($accs[-1], 'X56742');
102 ### TPA TESTS - Thanks to Richard Adams ###
103 # test Third Party Annotation entries in EMBL/Gb format 
104 # to ensure compatability with parsers.
105 my $str = Bio::SeqIO->new( -format =>'embl',
106                            -file => test_input_file('BN000066-tpa.embl'));
107 $seq = $str->next_seq;
108 ok(defined $seq);
109 is($seq->accession_number, 'BN000066');
110 is($seq->alphabet, 'dna');
111 is($seq->display_id, 'AGA000066');
112 is($seq->length, 5195);
113 is($seq->division, 'INV');
114 is($seq->get_dates, 2);
115 is($seq->keywords, 'acetylcholinesterase; achE1 gene; Third Party Annotation; TPA.');
116 is($seq->seq_version, 1);
117 is($seq->feature_count, 15);
119 my $spec_obj = $seq->species;
120 is ($spec_obj->common_name, 'African malaria mosquito');
121 is ($spec_obj->species, 'gambiae');
122 is ($spec_obj->genus, 'Anopheles');
123 is ($spec_obj->binomial, 'Anopheles gambiae');
125 my $ac = $seq->annotation;
126 my $reference =  ($ac->get_Annotations('reference') )[1];
127 is ($reference->title,'"A novel acetylcholinesterase gene in mosquitoes codes for the insecticide target and is non-homologous to the ace gene in Drosophila"');
128 is ($reference->authors,'Weill M., Fort P., Berthomi eu A., Dubois M.P., Pasteur N., Raymond M.');
129 my $cmmnt =  ($ac->get_Annotations('comment') )[0];
130 is($cmmnt->text, 'see also AJ488492 for achE-1 from Kisumu strain Third Party Annotation Database: This TPA record uses Anopheles gambiae trace archive data (http://trace.ensembl.org) ');
133 $ent = Bio::SeqIO->new( -file => test_input_file('test.embl'),
134                         -format => 'embl');
135 $ent->verbose($verbose);
136 $seq = $ent->next_seq();
137 my $species = $seq->species();
138 my @cl = $species->classification();
139 is( $cl[3] ne $species->genus(), 1, 'genus duplication test');
140 $ent->close();
143 ## read-write - test embl writing of a PrimarySeq
145 my $primaryseq = Bio::PrimarySeq->new( -seq => 'AGAGAGAGATA',
146                                       -id  => 'myid',
147                                       -desc => 'mydescr',
148                                       -alphabet => 'DNA',
149                                       -accession_number => 'myaccession');
153 $verbose = -1 unless $ENV{'BIOPERLDEBUG'};  # silence warnings unless we are debuggin
155 my $embl = Bio::SeqIO->new(-format => 'embl',
156                           -verbose => $verbose,
157                           -file => ">$out_file");
159 ok($embl->write_seq($primaryseq));
161 # this should generate a warning
162 my $scalar = "test";
163 eval {
164     $embl->write_seq($scalar);
166 ok ($@);
168 # CDS records
169 # (which have nonstandard 'PA' and 'OX' tags)
170 # see http://bioperl.org/pipermail/bioperl-l/2009-February/029252.html
171 # and the rest of that thread
172 my $cds_file = Bio::SeqIO->new(-format =>'embl',
173                                -file => test_input_file('cds_sample.embl'));
174 my $cds_seq = $cds_file->next_seq;
175 ok(defined $cds_seq);
176 is($cds_seq->display_id, 'EAL24309');
177 is($cds_seq->accession_number, 'CH236947.1', 'CDS - accession on PA line');
178 is($cds_seq->alphabet, 'dna');
179 is($cds_seq->length, 192);
180 is($cds_seq->species->binomial(), 'Homo sapiens');
181 is($cds_seq->seq_version, 1);
182 is($cds_seq->feature_count, 2);
183 my $cds_annot = $cds_seq->annotation;
184 ok(defined $cds_annot);
185 my $cds_dblink = ($cds_annot->get_Annotations('dblink'))[0];
186 ok(defined $cds_dblink);
187 is($cds_dblink->tagname, 'dblink', 'CDS - OX tagname');
188 is($cds_dblink->database, 'NCBI_TaxID', 'CDS - OX database');
189 is($cds_dblink->primary_id, '9606', 'CDS - OX primary_id');
191 #bug 2982 - parsing contig descriptions sans sequence data
193 ok( $embl = Bio::SeqIO->new( -file => test_input_file('bug2982.embl'),
194                              -format => 'embl') );
195 my $i;
196 for ($i=0; my $seq = $embl->next_seq; $i++) {
197     ok !$seq->seq;
198     ok ( my $ann = ($seq->annotation->get_Annotations('contig'))[0] );
199     like $ann->value, qr/join\(/;
201 is $i, 4;
204 # bug 3086 - parsing long lines correctly
206 ok( $embl = Bio::SeqIO->new(-file => test_input_file('bug3086.embl'),
207                             -format => 'embl',
208                             -verbose => '$verbose') );
209 $seq = $embl->next_seq;
210 foreach my $feature ($seq->top_SeqFeatures) {
211     if ($feature->has_tag('product')) {
212         my ($product) = $feature->get_tag_values('product');
213         is($product,
214            'bifunctional phosphoribosylaminoimidazolecarboxamide formyltransferase/IMP cyclohydrolase',
215            'Check if product was parsed correctly');
216     }
219 # long labels handled
222     # Create sequence with feature with a long label qualifier
223     my $seq=Bio::Seq->new(-seq=>'actg');
224     my $feature=Bio::SeqFeature::Generic->new(-primary=>'CDS', -start=>1, -end=>4);
225     $feature->add_tag_value(label=>'1 2 3 4 5 6 7 8 9 a b c d e f g h i j k l m n o p q r');
226     $seq->add_SeqFeature($feature);
228     # Write EMBL
229     my $string;
230     open my $str_fh, '>', \$string or skip("Could not write string, skipping", 2);
231     
232     my $out=Bio::SeqIO->new(-format=>'embl', -fh => $str_fh);
233     $out->write_seq($seq);
235     # Read EMBL
236     my $in=Bio::SeqIO->new(-format=>'embl', -string => $string);
237     my $ret=eval { my $embl=$in->next_seq };
238     my $error;
239     $error=$@ if (!$ret);
240     ok($ret, 'Parse long qualifier');
241     is($error, undef);
244 # NCBI TaxIDs should roundtrip
246     my $seq=Bio::Seq->new(-seq=>'actg');
247     my $species = Bio::Species->new(-ncbi_taxid => 7165, -classification=>
248                                     [ 'Anopheles gambiae', 'Anopheles', 'Culicoidea',
249                                       'Nematocera', 'Diptera', 'Endopterygota',
250                                       'Neoptera', 'Pterygota', 'Insecta', 'Hexapoda',
251                                       'Arthropoda', 'Metazoa', 'Eukaryota' ]);
253     $seq->species($species);
254     is($seq->species->ncbi_taxid, 7165, 'TaxID set correctly');
256     # Write EMBL
257     my $string;
258     open my $str_fh, '>', \$string or skip("Could not write string, skipping", 2);
260     my $out=Bio::SeqIO->new(-format=>'embl', -fh => $str_fh);
261     $out->write_seq($seq);
263     # Read EMBL
264     my $in=Bio::SeqIO->new(-format=>'embl', -string => $string);
265     my $embl_seq;
266     my $ret=eval { $embl_seq=$in->next_seq };
267     my $error;
268     $error=$@ if (!$ret);
270     # Check that TaxID has roundtripped
271     my $embl_species = $embl_seq->species;
272     ok(defined $embl_species, "The read sequence has a species object");
273     is($embl_species->ncbi_taxid, 7165, "NCBI TaxID has roundtripped");
274     is($embl_species->binomial(), 'Anopheles gambiae', "Name has roundtripped");
277 # a taxon db_xref on a source feature should override an OX line
279     my $seq=Bio::Seq->new(-seq=>'actg');
280     my $species = Bio::Species->new(-ncbi_taxid => 7165, -classification=>
281                                     [ 'Anopheles gambiae', 'Anopheles', 'Culicoidea',
282                                       'Nematocera', 'Diptera', 'Endopterygota',
283                                       'Neoptera', 'Pterygota', 'Insecta', 'Hexapoda',
284                                       'Arthropoda', 'Metazoa', 'Eukaryota' ]);
286     $seq->species($species);
287     is($seq->species->ncbi_taxid, 7165, 'TaxID set correctly');
289     my $seq_feature = Bio::SeqFeature::Generic->new(-primary=>'source',
290                                                     -start => 1,
291                                                     -end=> length($seq->seq));
293     $seq_feature->add_tag_value('db_xref', 'taxon:9606');
294     $seq->add_SeqFeature($seq_feature);
296     # Write EMBL
297     my $string;
298     open my $str_fh, '>', \$string or skip("Could not write string, skipping", 2);
300     my $out=Bio::SeqIO->new(-format=>'embl', -fh => $str_fh);
301     $out->write_seq($seq);
303     # Read EMBL
304     my $in=Bio::SeqIO->new(-format=>'embl', -string => $string);
305     my $embl_seq;
306     my $ret=eval { $embl_seq=$in->next_seq };
307     my $error;
308     $error=$@ if (!$ret);
310     # Check that TaxID has roundtripped
311     my $embl_species = $embl_seq->species;
312     ok(defined $embl_species, "The read sequence has a species object");
313     is($embl_species->ncbi_taxid, 9606, "The taxid of the source feature overrides that of the OX line");
314     is($embl_species->binomial(), 'Anopheles gambiae', "Name has roundtripped");
317 # Handle Seq objects that only define an ID, not an accession number
319     my $seq = Bio::Seq->new(-seq=>'actg', -id=>'test_id');
321     my $string;
322     open my $str_fh, '>', \$string or skip("Could not write string, skipping", 1);
324     my $out = Bio::SeqIO->new(-format=>'embl', -fh=>$str_fh);
325     $out->write_seq($seq);
327     ok($string =~ m/ID   test_id;/, "The ID field was written correctly");
330 # Test lenient handling of space after '=' sign in qualifiers:
332     my $ent = Bio::SeqIO->new( -file => test_input_file('test_space.embl'),
333                                -format => 'embl');
334     my $seq;
335     eval { $seq = $ent->next_seq(); };
336     my $error=$@;
337     is($error, '', 'EMBL format with space after equal sign parses');
339     my ($feature)=$seq->all_SeqFeatures;
340     is($feature->primary_tag, 'CDS', 'CDS read');
342     ok($feature->has_tag('product'), '/product found');
344     my ($value)=$feature->get_tag_values('product');
345     is($value, 'somewordandt extthatisquite lon gandthereforewraps', 'Qualifier /product value matches');