Skip to content
This repository
Newer
Older
100644 222 lines (205 sloc) 11.065 kb
07663481 »
2009-11-11 Starting new unit test specifically for SeqRecord object
1 # Copyright 2009 by Peter Cock. All rights reserved.
2 # This code is part of the Biopython distribution and governed by its
3 # license. Please see the LICENSE file that should have been included
4 # as part of this package.
5
6 """SeqFeature related tests for SeqRecord objects from Bio.SeqIO.
7
8 Initially this takes matched tests of GenBank and FASTA files from the NCBI
9 and confirms they are consistent using our different parsers.
10 """
11 import unittest
5187b171 »
2010-11-15 Unit test for SeqRecord addition with dbxrefs
12 from Bio import SeqIO
07663481 »
2009-11-11 Starting new unit test specifically for SeqRecord object
13 from Bio.Alphabet import generic_dna, generic_rna, generic_protein
14 from Bio.Seq import Seq
15 from Bio.SeqRecord import SeqRecord
ecd7698f »
2009-11-18 Fixed SeqRecord slicing for SeqFeature with OneOfPosition
16 from Bio.SeqFeature import SeqFeature, FeatureLocation, ExactPosition
17 from Bio.SeqFeature import WithinPosition, BeforePosition, AfterPosition, OneOfPosition
07663481 »
2009-11-11 Starting new unit test specifically for SeqRecord object
18
19 class SeqRecordCreation(unittest.TestCase):
50932959 »
2009-11-11 Basic SeqRecord slicing and addition unit tests
20 """Test basic creation of SeqRecords."""
07663481 »
2009-11-11 Starting new unit test specifically for SeqRecord object
21 def test_annotations(self):
50932959 »
2009-11-11 Basic SeqRecord slicing and addition unit tests
22 """Pass in annotations to SeqRecords"""
07663481 »
2009-11-11 Starting new unit test specifically for SeqRecord object
23 rec = SeqRecord(Seq("ACGT", generic_dna),
24 id="Test", name="Test", description="Test")
25 self.assertEqual(rec.annotations, {})
26 rec = SeqRecord(Seq("ACGT", generic_dna),
27 id="Test", name="Test", description="Test",
28 annotations={"test" : ["a test"]})
29 self.assertEqual(rec.annotations["test"], ["a test"])
30
31 def test_letter_annotations(self):
50932959 »
2009-11-11 Basic SeqRecord slicing and addition unit tests
32 """Pass in letter annotations to SeqRecords"""
07663481 »
2009-11-11 Starting new unit test specifically for SeqRecord object
33 rec = SeqRecord(Seq("ACGT", generic_dna),
34 id="Test", name="Test", description="Test")
35 self.assertEqual(rec.annotations, {})
36 rec = SeqRecord(Seq("ACGT", generic_dna),
37 id="Test", name="Test", description="Test",
38 letter_annotations={"test" : [1, 2, 3, 4]})
39 self.assertEqual(rec.letter_annotations["test"], [1, 2, 3, 4])
40 #Now try modifying it to a bad value...
41 try:
42 rec.letter_annotations["bad"] = "abc"
75dd0b74 »
2010-07-05 Use assertTrue in unit tests (added in Python 2.4) since the earlier …
43 self.assertTrue(False, "Adding a bad letter_annotation should fail!")
07663481 »
2009-11-11 Starting new unit test specifically for SeqRecord object
44 except (TypeError, ValueError), e:
45 pass
46 #Now try setting it afterwards to a bad value...
47 rec = SeqRecord(Seq("ACGT", generic_dna),
48 id="Test", name="Test", description="Test")
49 try:
50 rec.letter_annotations={"test" : [1, 2, 3]}
75dd0b74 »
2010-07-05 Use assertTrue in unit tests (added in Python 2.4) since the earlier …
51 self.assertTrue(False, "Changing to bad letter_annotations should fail!")
07663481 »
2009-11-11 Starting new unit test specifically for SeqRecord object
52 except (TypeError, ValueError), e:
53 pass
54 #Now try setting it at creation time to a bad value...
55 try:
56 rec = SeqRecord(Seq("ACGT", generic_dna),
57 id="Test", name="Test", description="Test",
58 letter_annotations={"test" : [1, 2, 3]})
75dd0b74 »
2010-07-05 Use assertTrue in unit tests (added in Python 2.4) since the earlier …
59 self.assertTrue(False, "Wrong length letter_annotations should fail!")
07663481 »
2009-11-11 Starting new unit test specifically for SeqRecord object
60 except (TypeError, ValueError), e:
61 pass
62
b7e23c93 »
2009-11-11 More SeqRecord slicing tests
63 class SeqRecordMethods(unittest.TestCase):
64 """Test SeqRecord methods."""
50932959 »
2009-11-11 Basic SeqRecord slicing and addition unit tests
65
b7e23c93 »
2009-11-11 More SeqRecord slicing tests
66 def setUp(self) :
c7791605 »
2009-11-18 Basic unit test for SeqRecord slicing with a full length source feature
67 f0 = SeqFeature(FeatureLocation(0,26), type="source",
68 qualifiers={"mol_type":["fake protein"]})
ecd7698f »
2009-11-18 Fixed SeqRecord slicing for SeqFeature with OneOfPosition
69 f1 = SeqFeature(FeatureLocation(0,ExactPosition(10)))
c52e986a »
2011-09-28 Make Bio.SeqFeature position objects act like and subclass int
70 f2 = SeqFeature(FeatureLocation(WithinPosition(12, left=12,right=15),BeforePosition(22)))
ecd7698f »
2009-11-18 Fixed SeqRecord slicing for SeqFeature with OneOfPosition
71 f3 = SeqFeature(FeatureLocation(AfterPosition(16),
c52e986a »
2011-09-28 Make Bio.SeqFeature position objects act like and subclass int
72 OneOfPosition(26, [ExactPosition(25),AfterPosition(26)])))
b7e23c93 »
2009-11-11 More SeqRecord slicing tests
73 self.record = SeqRecord(Seq("ABCDEFGHIJKLMNOPQRSTUVWZYX", generic_protein),
74 id="TestID", name="TestName", description="TestDescr",
75 dbxrefs=["TestXRef"], annotations={"k":"v"},
76 letter_annotations = {"fake":"X"*26},
c7791605 »
2009-11-18 Basic unit test for SeqRecord slicing with a full length source feature
77 features = [f0,f1,f2,f3])
b7e23c93 »
2009-11-11 More SeqRecord slicing tests
78
79 def test_slice_variantes(self):
80 """Simple slices using different start/end values"""
81 for start in range(-30,30)+[None] :
82 for end in range(-30,30)+[None] :
83 if start is None and end is None : continue
84 rec = self.record[start:end]
85 seq = self.record.seq[start:end]
86 seq_str = str(self.record.seq)[start:end]
87 self.assertEqual(seq_str, str(seq))
88 self.assertEqual(seq_str, str(rec.seq))
c46a1edb »
2009-11-11 Fixed a boundary condition and negative slice coordindates problem in…
89 self.assertEqual("X"*len(seq_str), rec.letter_annotations["fake"])
b7e23c93 »
2009-11-11 More SeqRecord slicing tests
90
91 def test_slice_simple(self):
50932959 »
2009-11-11 Basic SeqRecord slicing and addition unit tests
92 """Simple slice"""
b7e23c93 »
2009-11-11 More SeqRecord slicing tests
93 rec = self.record
50932959 »
2009-11-11 Basic SeqRecord slicing and addition unit tests
94 self.assertEqual(len(rec), 26)
95 left = rec[:10]
96 self.assertEqual(str(left.seq), str(rec.seq[:10]))
97 right = rec[-10:]
98 self.assertEqual(str(right.seq), str(rec.seq[-10:]))
99 mid = rec[12:22]
100 self.assertEqual(str(mid.seq), str(rec.seq[12:22]))
101 for sub in [left, right, mid] :
102 self.assertEqual(len(sub), 10)
103 self.assertEqual(sub.id, "TestID")
104 self.assertEqual(sub.name, "TestName")
105 self.assertEqual(sub.description, "TestDescr")
106 self.assertEqual(sub.letter_annotations, {"fake":"X"*10})
107 self.assertEqual(sub.dbxrefs, []) # May change this...
108 self.assertEqual(sub.annotations, {}) # May change this...
c46a1edb »
2009-11-11 Fixed a boundary condition and negative slice coordindates problem in…
109 self.assertEqual(len(sub.features), 1)
110 #By construction, each feature matches the full sliced region:
111 self.assertEqual(str(sub.features[0].extract(sub.seq)), str(sub.seq))
112 self.assertEqual(sub.features[0].extract(str(sub.seq)), str(sub.seq))
50932959 »
2009-11-11 Basic SeqRecord slicing and addition unit tests
113
00447bdf »
2011-07-24 Fix slicing zero length SeqRecord
114 def test_slice_zero(self):
115 """Zero slice"""
116 rec = self.record
117 self.assertEqual(len(rec), 26)
118 self.assertEqual(len(rec[2:-2]), 22)
119 self.assertEqual(len(rec[5:2]), 0)
120 self.assertEqual(len(rec[5:2][2:-2]), 0)
121
b7e23c93 »
2009-11-11 More SeqRecord slicing tests
122 def test_add_simple(self):
50932959 »
2009-11-11 Basic SeqRecord slicing and addition unit tests
123 """Simple addition"""
b7e23c93 »
2009-11-11 More SeqRecord slicing tests
124 rec = self.record + self.record
50932959 »
2009-11-11 Basic SeqRecord slicing and addition unit tests
125 self.assertEqual(len(rec), 52)
126 self.assertEqual(rec.id, "TestID")
127 self.assertEqual(rec.name, "TestName")
128 self.assertEqual(rec.description, "TestDescr")
129 self.assertEqual(rec.dbxrefs, ["TestXRef"])
130 self.assertEqual(rec.annotations, {"k":"v"})
b7e23c93 »
2009-11-11 More SeqRecord slicing tests
131 self.assertEqual(rec.letter_annotations, {"fake":"X"*52})
c7791605 »
2009-11-18 Basic unit test for SeqRecord slicing with a full length source feature
132 self.assertEqual(len(rec.features), 2*len(self.record.features))
07663481 »
2009-11-11 Starting new unit test specifically for SeqRecord object
133
f3a6b26c »
2009-11-11 More SeqRecord addition tests
134 def test_add_seq(self):
135 """Simple addition of Seq or string"""
136 for other in [Seq("BIO"), "BIO"] :
e5afce76 »
2009-11-18 Unit test and fix for SeqFeature locations in new SeqRecord __radd__ …
137 rec = self.record + other # will use SeqRecord's __add__ method
f3a6b26c »
2009-11-11 More SeqRecord addition tests
138 self.assertEqual(len(rec), 26+3)
139 self.assertEqual(str(rec.seq), str(self.record.seq)+"BIO")
140 self.assertEqual(rec.id, "TestID")
141 self.assertEqual(rec.name, "TestName")
142 self.assertEqual(rec.description, "TestDescr")
143 self.assertEqual(rec.dbxrefs, ["TestXRef"])
144 self.assertEqual(rec.annotations, {"k":"v"})
145 self.assertEqual(rec.letter_annotations, {})
c7791605 »
2009-11-18 Basic unit test for SeqRecord slicing with a full length source feature
146 self.assertEqual(len(rec.features), len(self.record.features))
e5afce76 »
2009-11-18 Unit test and fix for SeqFeature locations in new SeqRecord __radd__ …
147 self.assertEqual(rec.features[0].type, "source")
148 self.assertEqual(rec.features[0].location.nofuzzy_start, 0)
149 self.assertEqual(rec.features[0].location.nofuzzy_end, 26) #not +3
f3a6b26c »
2009-11-11 More SeqRecord addition tests
150
5187b171 »
2010-11-15 Unit test for SeqRecord addition with dbxrefs
151 def test_add_seqrecord(self):
152 """Simple left addition of SeqRecord from genbank file."""
153 other = SeqIO.read("GenBank/dbsource_wrap.gb", "gb")
154 other.dbxrefs = ["dummy"]
155 rec = self.record + other
156 self.assertEqual(len(rec), len(self.record)+len(other))
157 self.assertEqual(str(rec.seq), str(self.record.seq)+str(other.seq))
158 self.assertEqual(rec.id, "<unknown id>")
159 self.assertEqual(rec.name, "<unknown name>")
160 self.assertEqual(rec.description, "<unknown description>")
161 self.assertEqual(rec.dbxrefs, ["TestXRef", "dummy"])
162 self.assertEqual(len(rec.annotations), 0)
163 self.assertEqual(len(rec.letter_annotations),0)
164 self.assertEqual(len(rec.features),
165 len(self.record.features) + len(other.features))
166 self.assertEqual(rec.features[0].type, "source")
167 self.assertEqual(rec.features[0].location.nofuzzy_start, 0)
168 self.assertEqual(rec.features[0].location.nofuzzy_end, len(self.record)) #not +3
169 i = len(self.record.features)
170 self.assertEqual(rec.features[i].type, "source")
171 self.assertEqual(rec.features[i].location.nofuzzy_start, len(self.record))
172 self.assertEqual(rec.features[i].location.nofuzzy_end, len(rec))
173
f3a6b26c »
2009-11-11 More SeqRecord addition tests
174 def test_add_seq_left(self):
175 """Simple left addition of Seq or string"""
176 for other in [Seq("BIO"), "BIO"] :
e5afce76 »
2009-11-18 Unit test and fix for SeqFeature locations in new SeqRecord __radd__ …
177 rec = other + self.record # will use SeqRecord's __radd__ method
f3a6b26c »
2009-11-11 More SeqRecord addition tests
178 self.assertEqual(len(rec), 26+3)
179 self.assertEqual(str(rec.seq), "BIO"+str(self.record.seq))
180 self.assertEqual(rec.id, "TestID")
181 self.assertEqual(rec.name, "TestName")
182 self.assertEqual(rec.description, "TestDescr")
183 self.assertEqual(rec.dbxrefs, ["TestXRef"])
184 self.assertEqual(rec.annotations, {"k":"v"})
185 self.assertEqual(rec.letter_annotations, {})
c7791605 »
2009-11-18 Basic unit test for SeqRecord slicing with a full length source feature
186 self.assertEqual(len(rec.features), len(self.record.features))
e5afce76 »
2009-11-18 Unit test and fix for SeqFeature locations in new SeqRecord __radd__ …
187 self.assertEqual(rec.features[0].type, "source")
188 self.assertEqual(rec.features[0].location.nofuzzy_start, 3)
189 self.assertEqual(rec.features[0].location.nofuzzy_end, 26+3)
190
f3a6b26c »
2009-11-11 More SeqRecord addition tests
191 def test_slice_add_simple(self):
192 """Simple slice and add"""
193 for cut in range(27) :
194 rec = self.record[:cut] + self.record[cut:]
195 self.assertEqual(str(rec.seq), str(self.record.seq))
196 self.assertEqual(len(rec), 26)
197 self.assertEqual(rec.id, "TestID")
198 self.assertEqual(rec.name, "TestName")
199 self.assertEqual(rec.description, "TestDescr")
200 self.assertEqual(rec.dbxrefs, []) # May change this...
201 self.assertEqual(rec.annotations, {}) # May change this...
202 self.assertEqual(rec.letter_annotations, {"fake":"X"*26})
75dd0b74 »
2010-07-05 Use assertTrue in unit tests (added in Python 2.4) since the earlier …
203 self.assertTrue(len(rec.features) <= len(self.record.features))
f3a6b26c »
2009-11-11 More SeqRecord addition tests
204
205 def test_slice_add_shift(self):
206 """Simple slice and add to shift"""
207 for cut in range(27) :
208 rec = self.record[cut:] + self.record[:cut]
209 self.assertEqual(str(rec.seq), str(self.record.seq[cut:] + self.record.seq[:cut]))
210 self.assertEqual(len(rec), 26)
211 self.assertEqual(rec.id, "TestID")
212 self.assertEqual(rec.name, "TestName")
213 self.assertEqual(rec.description, "TestDescr")
214 self.assertEqual(rec.dbxrefs, []) # May change this...
215 self.assertEqual(rec.annotations, {}) # May change this...
216 self.assertEqual(rec.letter_annotations, {"fake":"X"*26})
75dd0b74 »
2010-07-05 Use assertTrue in unit tests (added in Python 2.4) since the earlier …
217 self.assertTrue(len(rec.features) <= len(self.record.features))
f3a6b26c »
2009-11-11 More SeqRecord addition tests
218
07663481 »
2009-11-11 Starting new unit test specifically for SeqRecord object
219 if __name__ == "__main__":
220 runner = unittest.TextTestRunner(verbosity = 2)
221 unittest.main(testRunner=runner)
Something went wrong with that request. Please try again.