tag fourth (and hopefully last) alpha
[bioperl-live.git] / branch-1-6 / t / Seq / Seq.t
blob02dd5b38138e91052ed40745f5c59dfec1b68ce7
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 => 62);
11         
12         use_ok('Bio::Seq');
13         use_ok('Bio::Seq::RichSeq');
14         use_ok('Bio::SeqFeature::Generic');
15         use_ok('Bio::Species');
16         use_ok('Bio::Annotation::SimpleValue');
19 ok my $seq = Bio::Seq->new(-seq=>'ACTGTGGCGTCAACT',
20                         -desc=>'Sample Bio::Seq object',
21                         -alphabet => 'dna',
22                         -is_circular => 1
23                        );
25 ok $seq->is_circular;
26 ok not $seq->is_circular(0);
27 ok not $seq->is_circular;
29 my $trunc = $seq->trunc(1,4);
30 is $trunc->length, 4, 'truncated sequence length';
32 is $trunc->seq, 'ACTG', 'truncated sequence string';
34 # test ability to get str function
35 is $seq->seq(),  'ACTGTGGCGTCAACT' ;
37 ok $seq = Bio::Seq->new(-seq=>'actgtggcgtcaact',
38                      -desc=>'Sample Bio::Seq object',
39                      -display_id => 'something',
40                      -accession_number => 'accnum',
41                      -alphabet => 'dna' );
43 is uc $seq->alphabet, 'DNA' , 'alphabet';
45 # basic methods
47 is $seq->id(), 'something',  "id";
48 is $seq->accession_number, 'accnum', "accession number";
49 is $seq->subseq(5, 9),  'tggcg', "subseq";
51 # check IdentifiableI and DescribableI interfaces
52 isa_ok $seq, 'Bio::IdentifiableI';
53 isa_ok $seq, 'Bio::DescribableI';
54 # make sure all methods are implemented
55 is $seq->authority("bioperl.org"), "bioperl.org";
56 is $seq->namespace("t"), "t";
57 is $seq->version(0), 0;
58 is $seq->lsid_string(), "bioperl.org:t:accnum";
59 is $seq->namespace_string(), "t:accnum.0";
60 is $seq->description(), 'Sample Bio::Seq object';
61 is $seq->display_name(), "something";
63 # check that feature accession works regardless of lazy things going on
64 is scalar($seq->top_SeqFeatures()), 0;
65 is scalar($seq->flush_SeqFeatures()), 0;
67 my $newfeat = Bio::SeqFeature::Generic->new( -start => 10,
68                                              -end => 12,
69                                              -primary => 'silly',
70                                              -source => 'stuff');
73 $seq->add_SeqFeature($newfeat);
74 is $seq->feature_count, 1;
76 my $species = Bio::Species->new
77     (-verbose => 1, 
78      -classification => [ qw( sapiens Homo Hominidae
79                               Catarrhini Primates Eutheria
80                               Mammalia Vertebrata Chordata
81                               Metazoa Eukaryota )]);
82 $seq->species($species);
83 is $seq->species->binomial, 'Homo sapiens';
84 $seq->annotation->add_Annotation('description',
85                  Bio::Annotation::SimpleValue->new(-value => 'desc-here'));
86 my ($descr) = $seq->annotation->get_Annotations('description');
87 is $descr->value(), 'desc-here';
88 is $descr->tagname(), 'description';
91 #  translation tests
94 my $trans = $seq->translate();
95 is  $trans->seq(), 'TVAST' , 'translated sequence';
97 # unambiguous two character codons like 'ACN' and 'GTN' should give out an amino acid
98 $seq->seq('ACTGTGGCGTCAAC');
99 $trans = $seq->translate();
100 is $trans->seq(), 'TVAST', 'translated sequence with unambiguous codons';
102 $seq->seq('ACTGTGGCGTCAACA');
103 $trans = $seq->translate();
104 is $trans->seq(), 'TVAST', 'translated sequence with unambiguous codons';
106 $seq->seq('ACTGTGGCGTCAACAG');
107 $trans = $seq->translate();
108 is $trans->seq(), 'TVAST', 'translated sequence with unambiguous codons';
110 $seq->seq('ACTGTGGCGTCAACAGT');
111 $trans = $seq->translate();
112 is $trans->seq(), 'TVASTV', 'translated sequence with unambiguous codons';
114 $seq->seq('ACTGTGGCGTCAACAGTA');
115 $trans = $seq->translate();
116 is $trans->seq(), 'TVASTV', 'translated sequence with unambiguous codons';
118 $seq->seq('AC');
119 is $seq->translate->seq , 'T', 'translated sequence with unambigious 2 char codon';
121 #difference between the default and full CDS translation
123 $seq->seq('atgtggtaa');
124 $trans = $seq->translate();
125 is $trans->seq(), 'MW*' , 'translated sequence with stop';
127 $seq->seq('atgtggtaa');
128 $trans = $seq->translate(undef,undef,undef,undef,1);
129 is $trans->seq(), 'MW', 'translated sequence';
131 #frame 
132 my $string;
133 my @frames = (0, 1, 2);
134 foreach my $frame (@frames) {
135     $string .= $seq->translate(undef, undef, $frame)->seq;
136     $string .= $seq->revcom->translate(undef, undef, $frame)->seq;
138 is $string, 'MW*LPHCGYHVVTT';
140 #Translating with all codon tables using method defaults
141 $string = '';
142 my @codontables = qw( 1 2 3 4 5 6 9 10 11 12 13 14 15 16 21 22 23);
143 foreach my $ct (@codontables) {
144     $string .= $seq->translate(undef, undef, undef, $ct)->seq;
146 is $string, 'MW*MW*MW*MW*MW*MWQMW*MW*MW*MW*MW*MWYMW*MW*MW*MW*MW*';
148 # CDS translation set to throw an exception for internal stop codons
149 $seq->seq('atgtggtaataa');
150 eval {
151     $seq->translate(undef, undef, undef, undef, 'CDS' , 'throw');
153 like ($@, qr/EX/);
155 $seq->seq('atgtggtaataa');
156 is( $seq->translate('J', '-',)->seq, 'MWJJ');
158 # tests for RichSeq
159 ok my $richseq = Bio::Seq::RichSeq->new( -seq => 'atgtggtaataa',
160                                       -accession_number => 'AC123',
161                                       -alphabet => 'rna',
162                                       -molecule => 'mRNA',              
163                                       -id => 'id1',
164                                       -dates => [ '2001/1/1' ],
165                                       -pid => '887821',
166                                       -keywords => 'JUNK1;JUNK2',
167                                       -division => 'Fungi',
168                                       -secondary_accessions => 'AC1152' );
170 is ($richseq->seq, 'atgtggtaataa');
171 is ($richseq->display_id, 'id1');
172 is (($richseq->get_dates)[0], '2001/1/1');
173 is (($richseq->get_secondary_accessions)[0], 'AC1152');
174 is ($richseq->accession_number, 'AC123');
175 is ($richseq->alphabet, 'rna');
176 is ($richseq->molecule, 'mRNA');
177 is ($richseq->pid, 887821);
178 is ($richseq->division, 'Fungi');
179 is ($richseq->keywords, 'JUNK1; JUNK2');
180 $richseq->seq_version('2');
181 is ($richseq->seq_version, 2);
183 # tests for subtle misbehaviors
184 $seq = Bio::Seq->new(-primary_id => 'blah', -accession_number => 'foo');
185 is ($seq->accession_number, $seq->primary_seq->accession_number);
186 is ($seq->primary_id, $seq->primary_seq->primary_id);
187 $seq->accession_number('blurb');
188 $seq->primary_id('bar');
189 is ($seq->accession_number, $seq->primary_seq->accession_number);
190 is ($seq->primary_id, $seq->primary_seq->primary_id);
193 # Bug #2864:
195 $seq = Bio::Seq->new(-display_id => 0, -seq => 'GATC');
197 is $seq->display_id, 0, "Bug #2864";