Debian Codegraph @

tests/tabixproxies_test.py

raw symbols SCIP
1 import unittest
2 import pysam
3 import os
4 import re
5 import copy
6 import gzip
7 from TestUtils import load_and_convert, make_data_files, TABIX_DATADIR
8
9
10 def setUpModule():
11 make_data_files(TABIX_DATADIR)
12
13
14 class TestBED(unittest.TestCase):
15
16 filename = os.path.join(TABIX_DATADIR, "fivecolumns.bed.gz")
17
18 def setUp(self):
19 self.tabix = pysam.TabixFile(self.filename)
20
21 def tearDown(self):
22 self.tabix.close()
23
24 def testAssignmentToTargetList(self):
25 for row in self.tabix.fetch(parser=pysam.asTuple()):
26 # Test that *others gets the right columns...
27 contig, start, end, *others = row
28 self.assertEqual(3 + len(others), len(row))
29
30 # ...and that a TupleProxy can be assigned from more than once
31 contig, *others = row
32 self.assertEqual(1 + len(others), len(row))
33
34
35 class TestParser(unittest.TestCase):
36
37 filename = os.path.join(TABIX_DATADIR, "example.gtf.gz")
38
39 def setUp(self):
40
41 self.tabix = pysam.TabixFile(self.filename)
42 self.compare = load_and_convert(self.filename)
43
44 def tearDown(self):
45 self.tabix.close()
46
47 def testRead(self):
48
49 for x, r in enumerate(self.tabix.fetch(parser=pysam.asTuple())):
50 c = self.compare[x]
51 self.assertEqual(c, list(r))
52 self.assertEqual(len(c), len(r))
53
54 # test indexing
55 for y in range(0, len(r)):
56 self.assertEqual(c[y], r[y])
57
58 # test slicing access
59 for y in range(0, len(r) - 1):
60 for cc in range(y + 1, len(r)):
61 self.assertEqual(c[y:cc],
62 r[y:cc])
63 self.assertEqual("\t".join(map(str, c)),
64 str(r))
65
66 def testAssignmentToTargetList(self):
67 for x, r in enumerate(self.tabix.fetch(parser=pysam.asTuple())):
68 col1, col2, *others, colN = r
69 self.assertEqual(2 + len(others) + 1, len(r))
70
71 def testWrite(self):
72
73 for x, r in enumerate(self.tabix.fetch(parser=pysam.asTuple())):
74 self.assertEqual(self.compare[x], list(r))
75 c = list(r)
76 for y in range(len(r)):
77 r[y] = "test_%05i" % y
78 c[y] = "test_%05i" % y
79 self.assertEqual([x for x in c], list(r))
80 self.assertEqual("\t".join(c), str(r))
81 # check second assignment
82 for y in range(len(r)):
83 r[y] = "test_%05i" % y
84 self.assertEqual([x for x in c], list(r))
85 self.assertEqual("\t".join(c), str(r))
86
87 def testUnset(self):
88 for x, r in enumerate(self.tabix.fetch(parser=pysam.asTuple())):
89 self.assertEqual(self.compare[x], list(r))
90 c = list(r)
91 e = list(r)
92 for y in range(len(r)):
93 r[y] = None
94 c[y] = None
95 e[y] = ""
96 self.assertEqual(c, list(r))
97 self.assertEqual("\t".join(e), str(r))
98
99 def testIteratorCompressed(self):
100 '''test iteration from compressed file.'''
101 with gzip.open(self.filename) as infile:
102 for x, r in enumerate(pysam.tabix_iterator(
103 infile, pysam.asTuple())):
104 self.assertEqual(self.compare[x], list(r))
105 self.assertEqual(len(self.compare[x]), len(r))
106
107 # test indexing
108 for c in range(0, len(r)):
109 self.assertEqual(self.compare[x][c], r[c])
110
111 # test slicing access
112 for c in range(0, len(r) - 1):
113 for cc in range(c + 1, len(r)):
114 self.assertEqual(self.compare[x][c:cc],
115 r[c:cc])
116
117 def testIteratorUncompressed(self):
118 '''test iteration from uncompressed file.'''
119 tmpfilename = 'tmp_testIteratorUncompressed'
120 with gzip.open(self.filename, "rb") as infile, \
121 open(tmpfilename, "wb") as outfile:
122 outfile.write(infile.read())
123
124 with open(tmpfilename) as infile:
125 for x, r in enumerate(pysam.tabix_iterator(
126 infile, pysam.asTuple())):
127 self.assertEqual(self.compare[x], list(r))
128 self.assertEqual(len(self.compare[x]), len(r))
129
130 # test indexing
131 for c in range(0, len(r)):
132 self.assertEqual(self.compare[x][c], r[c])
133
134 # test slicing access
135 for c in range(0, len(r) - 1):
136 for cc in range(c + 1, len(r)):
137 self.assertEqual(self.compare[x][c:cc],
138 r[c:cc])
139
140 os.unlink(tmpfilename)
141
142 def testCopy(self):
143 a = next(self.tabix.fetch(parser=pysam.asTuple()))
144 b = copy.copy(a)
145 self.assertEqual(a, b)
146
147 a = next(self.tabix.fetch(parser=pysam.asGTF()))
148 b = copy.copy(a)
149 self.assertEqual(a, b)
150
151
152 class TestGTF(TestParser):
153
154 parser = pysam.asGTF
155
156 def build_attribute_string(self, d):
157 """build attribute string from dictionary d"""
158 s = "; ".join(["{} \"{}\"".format(x, y) for (x, y) in d.items()]) + ";"
159 # remove quotes around numeric values
160 s = re.sub(r'"(\d+)"', r'\1', s)
161 return s
162
163 def testRead(self):
164
165 for x, r in enumerate(self.tabix.fetch(parser=self.parser())):
166 c = self.compare[x]
167 self.assertEqual(len(c), len(r))
168 self.assertEqual(list(c), list(r))
169 self.assertEqual(c, str(r).split("\t"))
170 self.assertTrue(r.gene_id.startswith("ENSG"))
171 if r.feature != 'gene':
172 self.assertTrue(r.transcript_id.startswith("ENST"))
173 self.assertEqual(c[0], r.contig)
174 self.assertEqual("\t".join(map(str, c)),
175 str(r))
176
177 def test_setting_fields(self):
178
179 r = next(self.tabix.fetch(parser=self.parser()))
180
181 r.contig = r.contig + "_test_contig"
182 r.source = r.source + "_test_source"
183 r.feature = r.feature + "_test_feature"
184 r.start += 10
185 r.end += 10
186 r.score = 20
187 r.strand = "+"
188 r.frame = 0
189 r.attributes = 'gene_id "0001";'
190 r.transcript_id = "0002"
191 sr = str(r)
192 self.assertTrue("_test_contig" in sr)
193 self.assertTrue("_test_source" in sr)
194 self.assertTrue("_test_feature" in sr)
195 self.assertTrue("gene_id \"0001\"" in sr)
196 self.assertTrue("transcript_id \"0002\"" in sr)
197
198 def test_setAttribute_makes_changes(self):
199
200 r = next(self.tabix.fetch(parser=self.parser()))
201 r.setAttribute("transcript_id", "abcd")
202 sr = str(r)
203 self.assertEqual(r.transcript_id, "abcd")
204 self.assertTrue("transcript_id \"abcd\"" in sr)
205
206 def test_added_attribute_is_output(self):
207 r = next(self.tabix.fetch(parser=self.parser()))
208
209 r.new_int_attribute = 12
210 self.assertTrue("new_int_attribute 12" in str(r).split("\t")[8])
211
212 r.new_float_attribute = 12.0
213 self.assertTrue("new_float_attribute 12.0" in str(r).split("\t")[8])
214
215 r.new_text_attribute = "abc"
216 self.assertTrue("new_text_attribute \"abc\"" in str(r).split("\t")[8])
217
218 def test_setting_start_is_one_based(self):
219
220 r = next(self.tabix.fetch(parser=self.parser()))
221 r.start = 1800
222 self.assertEqual(r.start, 1800)
223 self.assertEqual(str(r).split("\t")[3], "1801")
224
225 def test_setting_end_is_one_based(self):
226
227 r = next(self.tabix.fetch(parser=self.parser()))
228 r.end = 2100
229 self.assertEqual(r.end, 2100)
230 self.assertEqual(str(r).split("\t")[4], "2100")
231
232 def test_setting_frame_to_none_produces_dot(self):
233
234 r = next(self.tabix.fetch(parser=self.parser()))
235 r.frame = None
236 self.assertEqual(str(r).split("\t")[7], ".")
237
238 r.frame = 2
239 self.assertEqual(str(r).split("\t")[7], "2")
240
241 r = next(self.tabix.fetch(parser=self.parser()))
242 r.frame = "."
243 self.assertEqual(r.frame, None)
244 self.assertEqual(str(r).split("\t")[7], ".")
245
246 def test_setting_source_to_none_produces_dot(self):
247
248 r = next(self.tabix.fetch(parser=self.parser()))
249 r.source = None
250 self.assertEqual(str(r).split("\t")[1], ".")
251
252 r.source = "source"
253 self.assertEqual(str(r).split("\t")[1], "source")
254
255 r = next(self.tabix.fetch(parser=self.parser()))
256 r.source = "."
257 self.assertEqual(r.source, None)
258 self.assertEqual(str(r).split("\t")[1], ".")
259
260 def test_setting_feature_to_none_produces_dot(self):
261
262 r = next(self.tabix.fetch(parser=self.parser()))
263 r.feature = None
264 self.assertEqual(str(r).split("\t")[2], ".")
265
266 r.feature = "feature"
267 self.assertEqual(str(r).split("\t")[2], "feature")
268
269 r = next(self.tabix.fetch(parser=self.parser()))
270 r.feature = "."
271 self.assertEqual(r.feature, None)
272 self.assertEqual(str(r).split("\t")[2], ".")
273
274 def test_setting_strand_to_none_produces_dot(self):
275
276 r = next(self.tabix.fetch(parser=self.parser()))
277 r.strand = None
278 self.assertEqual(str(r).split("\t")[6], ".")
279
280 r.strand = "-"
281 self.assertEqual(str(r).split("\t")[6], "-")
282
283 r = next(self.tabix.fetch(parser=self.parser()))
284 r.strand = "."
285 self.assertEqual(r.strand, None)
286 self.assertEqual(str(r).split("\t")[6], ".")
287
288 def test_setting_score_to_none_produces_dot(self):
289
290 r = next(self.tabix.fetch(parser=self.parser()))
291 r.score = None
292 self.assertEqual(str(r).split("\t")[5], ".")
293
294 r.score = 12.0
295 self.assertEqual(str(r).split("\t")[5], "12.0")
296
297 r.score = -12.0
298 self.assertEqual(str(r).split("\t")[5], "-12.0")
299
300 r = next(self.tabix.fetch(parser=self.parser()))
301 r.score = "."
302 self.assertEqual(r.score, None)
303 self.assertEqual(str(r).split("\t")[5], ".")
304
305 r.score = 12
306 self.assertEqual(str(r).split("\t")[5], "12")
307
308 r.score = -12
309 self.assertEqual(str(r).split("\t")[5], "-12")
310
311 def test_asdict_contains_attributes(self):
312 r = next(self.tabix.fetch(parser=self.parser()))
313 d = r.to_dict()
314 c = self.compare[0]
315 s = self.build_attribute_string(d)
316 self.assertEqual(s, c[8])
317
318 def test_asdict_can_be_modified(self):
319 r = next(self.tabix.fetch(parser=self.parser()))
320 d = r.to_dict()
321 d["gene_id"] = "new_gene_id"
322 self.assertTrue("gene_id \"new_gene_id\"", str(r))
323
324
325 class TestGFF3(TestGTF):
326
327 parser = pysam.asGFF3
328 filename = os.path.join(TABIX_DATADIR, "example.gff3.gz")
329
330 def build_attribute_string(self, d):
331 """build attribute string from dictionary d"""
332 s = ";".join(["{}={}".format(x, y) for (x, y) in d.items()]) + ";"
333 return s
334
335 def build_attribute_string(self, d):
336 """build attribute string from dictionary d"""
337 s = ";".join(["{}={}".format(x, y) for (x, y) in d.items()]) + ";"
338 return s
339
340 def testRead(self):
341 for x, r in enumerate(self.tabix.fetch(parser=self.parser())):
342 c = self.compare[x]
343 self.assertEqual(len(c), len(r))
344 self.assertEqual(list(c), list(r))
345 self.assertEqual(c, str(r).split("\t"))
346 self.assertEqual(c[0], r.contig)
347 self.assertEqual("\t".join(map(str, c)),
348 str(r))
349 self.assertTrue(r.ID.startswith("MI00"))
350
351 def test_setting_fields(self):
352
353 for r in self.tabix.fetch(parser=self.parser()):
354 r.contig = r.contig + "_test_contig"
355 r.source = "test_source"
356 r.feature = "test_feature"
357 r.start += 10
358 r.end += 10
359 r.score = 20
360 r.strand = "+"
361 r.frame = 0
362 r.ID = "test"
363 sr = str(r)
364 self.assertTrue("test_contig" in sr)
365 self.assertTrue("test_source" in sr)
366 self.assertTrue("test_feature" in sr)
367 self.assertTrue("ID=test" in sr)
368
369 def test_setAttribute_makes_changes(self):
370
371 r = next(self.tabix.fetch(parser=self.parser()))
372 r.setAttribute("transcript_id", "abcd")
373 sr = str(r)
374 self.assertEqual(r.transcript_id, "abcd")
375 self.assertTrue("transcript_id=abcd" in sr)
376
377 def test_added_attribute_is_output(self):
378 r = next(self.tabix.fetch(parser=self.parser()))
379
380 r.new_int_attribute = 12
381 self.assertTrue("new_int_attribute=12" in str(r).split("\t")[8])
382
383 r.new_float_attribute = 12.0
384 self.assertTrue("new_float_attribute=12.0" in str(r).split("\t")[8])
385
386 r.new_text_attribute = "abc"
387 self.assertTrue("new_text_attribute=abc" in str(r).split("\t")[8])
388
389
390 if __name__ == "__main__":
391 unittest.main()