A test to ensure Bio::PrimarySeqI->trunc() doesn't use clone() for a Bio::Seq::RichSe...
[bioperl-live.git] / t / Seq / Seq.t
blob3712066696f7fc5d8f6e19423431c47ca08548a5
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 => 76);
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                        );
24 isa_ok($seq,"Bio::AnnotatableI");
26 ok $seq->is_circular;
27 ok not $seq->is_circular(0);
28 ok not $seq->is_circular;
30 my $trunc = $seq->trunc(1,4);
31 is $trunc->length, 4, 'truncated sequence length';
33 is $trunc->seq, 'ACTG', 'truncated sequence string';
35 # test ability to get str function
36 is $seq->seq(),  'ACTGTGGCGTCAACT' ;
38 ok $seq = Bio::Seq->new(-seq=>'actgtggcgtcaact',
39                         -desc=>'Sample Bio::Seq object',
40                         -display_id => 'something',
41                         -accession_number => 'accnum',
42                         -alphabet => 'dna' );
44 is uc $seq->alphabet, 'DNA' , 'alphabet';
46 # basic methods
48 is $seq->id(), 'something',  "id";
49 is $seq->accession_number, 'accnum', "accession number";
50 is $seq->subseq(5, 9),  'tggcg', "subseq";
52 # check IdentifiableI and DescribableI interfaces
53 isa_ok $seq, 'Bio::IdentifiableI';
54 isa_ok $seq, 'Bio::DescribableI';
55 # make sure all methods are implemented
56 is $seq->authority("bioperl.org"), "bioperl.org";
57 is $seq->namespace("t"), "t";
58 is $seq->version(0), 0;
59 is $seq->lsid_string(), "bioperl.org:t:accnum";
60 is $seq->namespace_string(), "t:accnum.0";
61 is $seq->description(), 'Sample Bio::Seq object';
62 is $seq->display_name(), "something";
64 # check that feature accession works regardless of lazy things going on
65 is scalar($seq->top_SeqFeatures()), 0;
66 is scalar($seq->flush_SeqFeatures()), 0;
68 my $newfeat = Bio::SeqFeature::Generic->new( -start => 10,
69                                              -end => 12,
70                                              -primary => 'silly',
71                                              -source => 'stuff');
74 $seq->add_SeqFeature($newfeat);
75 is $seq->feature_count, 1;
77 my $species = Bio::Species->new
78     (-verbose => 1,
79      -classification => [ qw( sapiens Homo Hominidae
80                               Catarrhini Primates Eutheria
81                               Mammalia Vertebrata Chordata
82                               Metazoa Eukaryota )]);
83 $seq->species($species);
84 is $seq->species->binomial, 'Homo sapiens';
85 $seq->annotation->add_Annotation('description',
86                  Bio::Annotation::SimpleValue->new(-value => 'desc-here'));
87 my ($descr) = $seq->annotation->get_Annotations('description');
88 is $descr->value(), 'desc-here';
89 is $descr->tagname(), 'description';
92 #  translation tests
95 my $trans = $seq->translate();
96 is  $trans->seq(), 'TVAST' , 'translated sequence';
98 # unambiguous two character codons like 'ACN' and 'GTN' should give out an amino
99 # acid ...with the addendum that there should be no assumption by the method
100 # to complete the codon unless specified, using the -complete_codons flag.
102 $seq->seq('ACTGTGGCGTCAACN');
103 $trans = $seq->translate();
104 is $trans->seq(), 'TVAST', 'translated sequence with explicit unambiguous codons';
106 $seq->seq('ACTGTGGCGTCAAC');
107 $trans = $seq->translate();
108 is $trans->seq(), 'TVAS', 'translated sequence with unknown unambiguous codons';
110 $seq->seq('ACTGTGGCGTCAAC');
111 $trans = $seq->translate(-complete_codons => 1);
112 is $trans->seq(), 'TVAST', 'translated sequence with unknown unambiguous codons, completed';
114 $seq->seq('ACTGTGGCGTCAACA');
115 $trans = $seq->translate();
116 is $trans->seq(), 'TVAST', 'translated sequence with unambiguous codons';
118 $seq->seq('ACTGTGGCGTCAACAG');
119 $trans = $seq->translate();
120 is $trans->seq(), 'TVAST', 'translated sequence with unambiguous codons';
122 $seq->seq('ACTGTGGCGTCAACAGT');
123 $trans = $seq->translate(-complete_codons => 1);
124 is $trans->seq(), 'TVASTV', 'translated sequence with unknown unambiguous codons, completed';
126 $seq->seq('ACTGTGGCGTCAACAGTA');
127 $trans = $seq->translate();
128 is $trans->seq(), 'TVASTV', 'translated sequence with unambiguous codons';
130 $seq->seq('AC');
131 is $seq->translate(-complete_codons => 1)->seq , 'T', 'translated sequence with unknown unambiguous codons, completed';
133 #difference between the default and full CDS translation
135 $seq->seq('atgtggtaa');
136 $trans = $seq->translate();
137 is $trans->seq(), 'MW*' , 'translated sequence with stop';
139 $seq->seq('atgtggtaa');
140 $trans = $seq->translate(undef,undef,undef,undef,1);
141 is $trans->seq(), 'MW', 'translated sequence';
143 #frame
144 my $string;
145 my @frames = (0, 1, 2);
146 foreach my $frame (@frames) {
147     $string .= $seq->translate(undef, undef, $frame)->seq;
148     $string .= $seq->revcom->translate(undef, undef, $frame)->seq;
150 is $string, 'MW*LPHCGYHVVTT';
152 #Translating with all codon tables using method defaults
153 $string = '';
154 my @codontables = qw( 1 2 3 4 5 6 9 10 11 12 13 14 15 16 21 22 23);
155 foreach my $ct (@codontables) {
156     $string .= $seq->translate(undef, undef, undef, $ct)->seq;
158 is $string, 'MW*MW*MW*MW*MW*MWQMW*MW*MW*MW*MW*MWYMW*MW*MW*MW*MW*';
160 # CDS translation set to throw an exception for internal stop codons
161 $seq->seq('atgtggtaataa');
162 eval {
163     $seq->translate(undef, undef, undef, undef, 'CDS' , 'throw');
165 like ($@, qr/EX/);
167 $seq->seq('atgtggtaataa');
168 is( $seq->translate('J', '-',)->seq, 'MWJJ');
170 # tests for RichSeq
171 ok my $richseq = Bio::Seq::RichSeq->new( -seq => 'atgtggtaataa',
172                                       -accession_number => 'AC123',
173                                       -alphabet => 'rna',
174                                       -molecule => 'mRNA',
175                                       -id => 'id1',
176                                       -dates => [ '2001/1/1' ],
177                                       -pid => '887821',
178                                       -keywords => 'JUNK1;JUNK2',
179                                       -division => 'Fungi',
180                                       -secondary_accessions => 'AC1152' );
182 is ($richseq->seq, 'atgtggtaataa');
183 is ($richseq->display_id, 'id1');
184 is (($richseq->get_dates)[0], '2001/1/1');
185 is (($richseq->get_secondary_accessions)[0], 'AC1152');
186 is ($richseq->accession_number, 'AC123');
187 is ($richseq->alphabet, 'rna');
188 is ($richseq->molecule, 'mRNA');
189 is ($richseq->pid, 887821);
190 is ($richseq->division, 'Fungi');
191 is ($richseq->keywords, 'JUNK1; JUNK2');
192 $richseq->seq_version('2');
193 is ($richseq->seq_version, 2);
195 # Test adding a feature to a RichSeq type, then
196 # trunc() and see if the feature vanishes (we shouldn't
197 # be using clone() for RichSeq types)
198 $richseq->add_SeqFeature($newfeat);
199 is $richseq->feature_count, 1;
200 my $newrichseq = $richseq->trunc(1,5);
201 is $newrichseq->feature_count, 0, "Don't use clone for trunc of Bio::Seq::RichSeq";
202 is $newrichseq->length, 5;
204 # tests for subtle misbehaviors
205 $seq = Bio::Seq->new(-primary_id => 'blah', -accession_number => 'foo');
206 is ($seq->accession_number, $seq->primary_seq->accession_number);
207 is ($seq->primary_id, $seq->primary_seq->primary_id);
208 $seq->accession_number('blurb');
209 $seq->primary_id('bar');
210 is ($seq->accession_number, $seq->primary_seq->accession_number);
211 is ($seq->primary_id, $seq->primary_seq->primary_id);
214 # Bug #2864:
216 $seq = Bio::Seq->new(-display_id => 0, -seq => 'GATC');
218 is $seq->display_id, 0, "Bug #2864";
220 # transcribe/rev_transcribe
222 $seq = Bio::Seq->new( -id => 'seq1', -alphabet=>'dna',
223                       -seq=> 'attTcgcatgT' );
224 ok my $xseq = $seq->transcribe;
225 is $xseq->alphabet, 'rna';
226 ok !($xseq->seq =~ /[tT]/);
227 is $xseq->seq, 'auuUcgcaugU';
228 ok !$xseq->transcribe;
229 ok $seq = $xseq->rev_transcribe;
230 is $seq->seq, 'attTcgcatgT';
231 is $seq->alphabet, 'dna';