| 1 |
import os |
| 2 |
import pysam |
| 3 |
import unittest |
| 4 |
import json |
| 5 |
import collections |
| 6 |
import struct |
| 7 |
import copy |
| 8 |
import array |
| 9 |
from pysam import CDEL, CDIFF, CEQUAL, CINS, CMATCH, CPAD, CREF_SKIP, CSOFT_CLIP |
| 10 |
|
| 11 |
from TestUtils import ( |
| 12 |
checkFieldEqual, |
| 13 |
make_data_files, |
| 14 |
BAM_DATADIR, |
| 15 |
get_temp_filename, |
| 16 |
get_temp_context, |
| 17 |
) |
| 18 |
|
| 19 |
|
| 20 |
def setUpModule(): |
| 21 |
make_data_files(BAM_DATADIR) |
| 22 |
|
| 23 |
|
| 24 |
class ReadTest(unittest.TestCase): |
| 25 |
def build_read(self): |
| 26 |
"""build an example read.""" |
| 27 |
|
| 28 |
header = pysam.AlignmentHeader.from_references( |
| 29 |
["chr1", "chr2"], [10000000, 10000000] |
| 30 |
) |
| 31 |
|
| 32 |
a = pysam.AlignedSegment(header) |
| 33 |
a.query_name = "read_12345" |
| 34 |
a.query_sequence = "ATGC" * 10 |
| 35 |
a.flag = 0 |
| 36 |
a.reference_id = 0 |
| 37 |
a.reference_start = 20 |
| 38 |
a.mapping_quality = 20 |
| 39 |
a.cigartuples = ((0, 10), (2, 1), (0, 9), (1, 1), (0, 20)) |
| 40 |
a.next_reference_id = 0 |
| 41 |
a.next_reference_start = 200 |
| 42 |
a.template_length = 167 |
| 43 |
a.query_qualities = pysam.qualitystring_to_array("1234") * 10 |
| 44 |
return a |
| 45 |
|
| 46 |
|
| 47 |
class TestAlignedSegment(ReadTest): |
| 48 |
|
| 49 |
"""tests to check if aligned read can be constructed |
| 50 |
and manipulated. |
| 51 |
""" |
| 52 |
|
| 53 |
def check_get_aligned_pairs_combos(self, a, exp): |
| 54 |
def positions(exp): return [(ppos, rpos) for ppos, rpos, base, cigar in exp] |
| 55 |
def with_seq(exp): return [(ppos, rpos, base) for ppos, rpos, base, cigar in exp] |
| 56 |
def with_cigar(exp): return [(ppos, rpos, cigar) for ppos, rpos, base, cigar in exp] |
| 57 |
|
| 58 |
self.assertEqual(a.get_aligned_pairs(), positions(exp)) |
| 59 |
self.assertEqual(a.get_aligned_pairs(with_seq=True), with_seq(exp)) |
| 60 |
self.assertEqual(a.get_aligned_pairs(with_cigar=True), with_cigar(exp)) |
| 61 |
self.assertEqual(a.get_aligned_pairs(with_seq=True, with_cigar=True), exp) |
| 62 |
|
| 63 |
exp = [(ppos, rpos, base, cigar) for ppos, rpos, base, cigar in exp if ppos is not None and rpos is not None] |
| 64 |
|
| 65 |
self.assertEqual(a.get_aligned_pairs(matches_only=True), positions(exp)) |
| 66 |
self.assertEqual(a.get_aligned_pairs(matches_only=True, with_seq=True), with_seq(exp)) |
| 67 |
self.assertEqual(a.get_aligned_pairs(matches_only=True, with_cigar=True), with_cigar(exp)) |
| 68 |
self.assertEqual(a.get_aligned_pairs(matches_only=True, with_seq=True, with_cigar=True), exp) |
| 69 |
|
| 70 |
def check_get_aligned_pairs_combos_without_MD(self, a, exp): |
| 71 |
def positions(exp): return [(ppos, rpos) for ppos, rpos, cigar in exp] |
| 72 |
|
| 73 |
self.assertEqual(a.get_aligned_pairs(), positions(exp)) |
| 74 |
with self.assertRaises(ValueError): a.get_aligned_pairs(with_seq=True) |
| 75 |
self.assertEqual(a.get_aligned_pairs(with_cigar=True), exp) |
| 76 |
with self.assertRaises(ValueError): a.get_aligned_pairs(with_seq=True, with_cigar=True) |
| 77 |
|
| 78 |
exp = [(ppos, rpos, cigar) for ppos, rpos, cigar in exp if ppos is not None and rpos is not None] |
| 79 |
|
| 80 |
self.assertEqual(a.get_aligned_pairs(matches_only=True), positions(exp)) |
| 81 |
with self.assertRaises(ValueError): a.get_aligned_pairs(matches_only=True, with_seq=True) |
| 82 |
self.assertEqual(a.get_aligned_pairs(matches_only=True, with_cigar=True), exp) |
| 83 |
with self.assertRaises(ValueError): a.get_aligned_pairs(matches_only=True, with_seq=True, with_cigar=True) |
| 84 |
|
| 85 |
def testEmpty(self): |
| 86 |
|
| 87 |
a = pysam.AlignedSegment() |
| 88 |
self.assertEqual(a.query_name, None) |
| 89 |
self.assertEqual(a.query_sequence, None) |
| 90 |
self.assertEqual(pysam.qualities_to_qualitystring(a.query_qualities), None) |
| 91 |
self.assertEqual(a.flag, 0) |
| 92 |
self.assertEqual(a.reference_id, -1) |
| 93 |
self.assertEqual(a.mapping_quality, 0) |
| 94 |
self.assertEqual(a.cigartuples, None) |
| 95 |
self.assertEqual(a.get_tags(), []) |
| 96 |
self.assertEqual(a.next_reference_id, -1) |
| 97 |
self.assertEqual(a.next_reference_start, -1) |
| 98 |
self.assertEqual(a.template_length, 0) |
| 99 |
|
| 100 |
def testStrOfEmptyRead(self): |
| 101 |
a = pysam.AlignedSegment() |
| 102 |
s = str(a) |
| 103 |
self.assertEqual("None\t0\t*\t0\t0\tNone\t*\t0\t0\tNone\tNone\t[]", s) |
| 104 |
|
| 105 |
def testSettingTagInEmptyRead(self): |
| 106 |
"""see issue 62""" |
| 107 |
a = pysam.AlignedSegment() |
| 108 |
a.tags = (("NM", 1),) |
| 109 |
a.query_qualities = None |
| 110 |
self.assertEqual(a.tags, [("NM", 1),]) |
| 111 |
|
| 112 |
def testCompare(self): |
| 113 |
"""check comparison functions.""" |
| 114 |
a = self.build_read() |
| 115 |
b = None |
| 116 |
|
| 117 |
self.assertFalse(a is b) |
| 118 |
self.assertFalse(a == b) |
| 119 |
self.assertEqual(-1, a.compare(b)) |
| 120 |
|
| 121 |
b = self.build_read() |
| 122 |
|
| 123 |
self.assertEqual(0, a.compare(b)) |
| 124 |
self.assertEqual(0, b.compare(a)) |
| 125 |
self.assertTrue(a == b) |
| 126 |
self.assertTrue(b == a) |
| 127 |
self.assertFalse(a != b) |
| 128 |
self.assertFalse(b != a) |
| 129 |
|
| 130 |
b.tid = 1 |
| 131 |
self.assertFalse(a == b) |
| 132 |
self.assertFalse(b == a) |
| 133 |
self.assertTrue(a != b) |
| 134 |
self.assertTrue(b != a) |
| 135 |
|
| 136 |
def testHashing(self): |
| 137 |
a = self.build_read() |
| 138 |
b = self.build_read() |
| 139 |
self.assertEqual(hash(a), hash(b)) |
| 140 |
b.tid = 1 |
| 141 |
self.assertNotEqual(hash(a), hash(b)) |
| 142 |
|
| 143 |
def testUpdate(self): |
| 144 |
"""check if updating fields affects other variable length data |
| 145 |
""" |
| 146 |
a = self.build_read() |
| 147 |
b = self.build_read() |
| 148 |
|
| 149 |
|
| 150 |
b.query_name = "read_123" |
| 151 |
checkFieldEqual(self, a, b, "query_name") |
| 152 |
b.query_name = "read_12345678" |
| 153 |
checkFieldEqual(self, a, b, "query_name") |
| 154 |
b.query_name = "read_12345" |
| 155 |
checkFieldEqual(self, a, b) |
| 156 |
|
| 157 |
|
| 158 |
b.cigartuples = ((0, 10),) |
| 159 |
checkFieldEqual(self, a, b, "cigartuples") |
| 160 |
b.cigartuples = ((0, 10), (2, 1), (0, 10)) |
| 161 |
checkFieldEqual(self, a, b, "cigartuples") |
| 162 |
b.cigartuples = ((0, 10), (2, 1), (0, 9), (1, 1), (0, 20)) |
| 163 |
checkFieldEqual(self, a, b) |
| 164 |
|
| 165 |
|
| 166 |
b.query_sequence = "ATGC" |
| 167 |
checkFieldEqual( |
| 168 |
self, a, b, ("query_sequence", "query_qualities", "query_length") |
| 169 |
) |
| 170 |
b.query_sequence = "ATGC" * 3 |
| 171 |
checkFieldEqual( |
| 172 |
self, a, b, ("query_sequence", "query_qualities", "query_length") |
| 173 |
) |
| 174 |
b.query_sequence = "ATGC" * 10 |
| 175 |
checkFieldEqual(self, a, b, ("query_qualities",)) |
| 176 |
|
| 177 |
|
| 178 |
b = self.build_read() |
| 179 |
|
| 180 |
def dual(name): |
| 181 |
if name.endswith('is_unmapped'): return name.replace('unmapped', 'mapped') |
| 182 |
elif name.endswith('is_mapped'): return name.replace('mapped', 'unmapped') |
| 183 |
elif name.endswith('is_reverse'): return name.replace('reverse', 'forward') |
| 184 |
elif name.endswith('is_forward'): return name.replace('forward', 'reverse') |
| 185 |
else: return name |
| 186 |
|
| 187 |
|
| 188 |
for x in ( |
| 189 |
"is_paired", |
| 190 |
"is_proper_pair", |
| 191 |
"is_unmapped", |
| 192 |
"mate_is_unmapped", |
| 193 |
"is_reverse", |
| 194 |
"mate_is_reverse", |
| 195 |
"is_read1", |
| 196 |
"is_read2", |
| 197 |
"is_secondary", |
| 198 |
"is_qcfail", |
| 199 |
"is_duplicate", |
| 200 |
"is_supplementary", |
| 201 |
): |
| 202 |
setattr(b, x, True) |
| 203 |
self.assertEqual(getattr(b, x), True) |
| 204 |
checkFieldEqual(self, a, b, ("flag", x, dual(x),)) |
| 205 |
setattr(b, x, False) |
| 206 |
self.assertEqual(getattr(b, x), False) |
| 207 |
checkFieldEqual(self, a, b) |
| 208 |
|
| 209 |
for x in ( |
| 210 |
"is_mapped", |
| 211 |
"mate_is_mapped", |
| 212 |
"is_forward", |
| 213 |
"mate_is_forward", |
| 214 |
): |
| 215 |
setattr(b, x, False) |
| 216 |
self.assertEqual(getattr(b, x), False) |
| 217 |
checkFieldEqual(self, a, b, ("flag", x, dual(x),)) |
| 218 |
setattr(b, x, True) |
| 219 |
self.assertEqual(getattr(b, x), True) |
| 220 |
checkFieldEqual(self, a, b) |
| 221 |
|
| 222 |
def testUpdate2(self): |
| 223 |
"""issue 135: inplace update of sequence and quality score. |
| 224 |
|
| 225 |
This does not work as setting the sequence will erase |
| 226 |
the quality scores. |
| 227 |
""" |
| 228 |
a = self.build_read() |
| 229 |
a.query_sequence = a.query_sequence[5:10] |
| 230 |
self.assertEqual(pysam.qualities_to_qualitystring(a.query_qualities), None) |
| 231 |
|
| 232 |
a = self.build_read() |
| 233 |
s = pysam.qualities_to_qualitystring(a.query_qualities) |
| 234 |
a.query_sequence = a.query_sequence[5:10] |
| 235 |
a.query_qualities = pysam.qualitystring_to_array(s[5:10]) |
| 236 |
|
| 237 |
self.assertEqual(pysam.qualities_to_qualitystring(a.query_qualities), s[5:10]) |
| 238 |
|
| 239 |
def testClearSequence(self): |
| 240 |
a = pysam.AlignedSegment() |
| 241 |
a.query_sequence = "ATGC" |
| 242 |
self.assertEqual(a.query_sequence, "ATGC") |
| 243 |
a.query_sequence = None |
| 244 |
self.assertEqual(a.query_length, 0) |
| 245 |
|
| 246 |
a.query_sequence = "ATGC" |
| 247 |
self.assertEqual(a.query_sequence, "ATGC") |
| 248 |
a.query_sequence = "" |
| 249 |
self.assertEqual(a.query_length, 0) |
| 250 |
|
| 251 |
a.query_sequence = "ATGC" |
| 252 |
self.assertEqual(a.query_sequence, "ATGC") |
| 253 |
a.query_sequence = "*" |
| 254 |
self.assertEqual(a.query_length, 0) |
| 255 |
|
| 256 |
def testUpdateSequenceEffects1(self): |
| 257 |
a = self.build_read() |
| 258 |
a.query_sequence = "ATGCATGC" |
| 259 |
a.cigarstring = "1S5M2S" |
| 260 |
self.assertEqual(a.query_alignment_sequence, "TGCAT") |
| 261 |
|
| 262 |
a.query_sequence = "AATTGGCC" |
| 263 |
self.assertEqual(a.query_alignment_sequence, "ATTGG") |
| 264 |
|
| 265 |
def testUpdateSequenceEffects2(self): |
| 266 |
a = self.build_read() |
| 267 |
a.query_sequence = "ATGCATGC" |
| 268 |
a.cigarstring = "1S5M2S" |
| 269 |
self.assertEqual(a.query_alignment_sequence, "TGCAT") |
| 270 |
|
| 271 |
a.query_sequence = "*" |
| 272 |
self.assertIsNone(a.query_sequence) |
| 273 |
self.assertIsNone(a.query_alignment_sequence) |
| 274 |
|
| 275 |
def testUpdateQual(self): |
| 276 |
"""Ensure SEQ and QUAL updates leading to absent QUAL set all bytes to 0xff""" |
| 277 |
|
| 278 |
a = self.build_read() |
| 279 |
with get_temp_context("absent_qual.bam") as fname: |
| 280 |
with pysam.AlignmentFile(fname, "wb", header=a.header) as outf: |
| 281 |
a.query_sequence = "ATGC" |
| 282 |
outf.write(a) |
| 283 |
|
| 284 |
a.query_sequence = "ATGCATGCATGC" |
| 285 |
outf.write(a) |
| 286 |
|
| 287 |
a.query_sequence = "ATGCATGC" |
| 288 |
a.query_qualities = pysam.qualitystring_to_array("<<<<<<<<") |
| 289 |
a.query_qualities = None |
| 290 |
outf.write(a) |
| 291 |
|
| 292 |
with pysam.BGZFile(fname) as f: |
| 293 |
|
| 294 |
(l_text,) = struct.unpack("<4xL", f.read(8)) |
| 295 |
f.read(l_text) |
| 296 |
(n_ref,) = struct.unpack("<L", f.read(4)) |
| 297 |
for i in range(n_ref): |
| 298 |
(l_name,) = struct.unpack("<L", f.read(4)) |
| 299 |
f.read(l_name + 4) |
| 300 |
|
| 301 |
|
| 302 |
while True: |
| 303 |
core = f.read(36) |
| 304 |
if len(core) != 36: break |
| 305 |
|
| 306 |
(block_size, l_read_name, n_cigar_op, l_seq) = struct.unpack("<L8xB3xH2xL12x", core) |
| 307 |
data = f.read(block_size - 32) |
| 308 |
qual = data[l_read_name + 4*n_cigar_op + ((l_seq+1) // 2):] |
| 309 |
|
| 310 |
self.assertEqual(qual, b'\xff' * l_seq) |
| 311 |
|
| 312 |
def testClearQual(self): |
| 313 |
a = pysam.AlignedSegment() |
| 314 |
a.query_sequence = "ATGC" |
| 315 |
a.query_qualities = pysam.qualitystring_to_array("qrst") |
| 316 |
a.query_qualities = None |
| 317 |
self.assertIsNone(a.query_qualities) |
| 318 |
|
| 319 |
def testClearQualStr(self): |
| 320 |
a = pysam.AlignedSegment() |
| 321 |
a.query_sequence = "ATGC" |
| 322 |
a.query_qualities_str = "qrst" |
| 323 |
self.assertEqual(a.query_qualities, pysam.qualitystring_to_array("qrst")) |
| 324 |
self.assertEqual(a.query_qualities_str, "qrst") |
| 325 |
|
| 326 |
a.query_qualities_str = None |
| 327 |
self.assertIsNone(a.query_qualities) |
| 328 |
self.assertIsNone(a.query_qualities_str) |
| 329 |
|
| 330 |
a.query_qualities_str = "qrst" |
| 331 |
a.query_qualities_str = "" |
| 332 |
self.assertIsNone(a.query_qualities) |
| 333 |
self.assertIsNone(a.query_qualities_str) |
| 334 |
|
| 335 |
a.query_qualities_str = "qrst" |
| 336 |
a.query_qualities_str = "*" |
| 337 |
self.assertIsNone(a.query_qualities) |
| 338 |
self.assertIsNone(a.query_qualities_str) |
| 339 |
|
| 340 |
def testUpdateQualArrayB(self): |
| 341 |
a = pysam.AlignedSegment() |
| 342 |
a.query_sequence = "ATGC" |
| 343 |
a.query_qualities = array.array('B', [80, 81, 82, 83]) |
| 344 |
self.assertEqual(len(a.query_qualities), 4) |
| 345 |
self.assertEqual(a.query_qualities_str, "qrst") |
| 346 |
|
| 347 |
def testUpdateQualArrayI(self): |
| 348 |
a = pysam.AlignedSegment() |
| 349 |
a.query_sequence = "ATGC" |
| 350 |
a.query_qualities = array.array('I', [80, 81, 82, 83]) |
| 351 |
self.assertEqual(len(a.query_qualities), 4) |
| 352 |
self.assertEqual(a.query_qualities_str, "qrst") |
| 353 |
|
| 354 |
def testUpdateQualList(self): |
| 355 |
a = pysam.AlignedSegment() |
| 356 |
a.query_sequence = "ATGC" |
| 357 |
qual = [80, 81, 82, 83] |
| 358 |
a.query_qualities = qual |
| 359 |
qual.pop() |
| 360 |
self.assertEqual(len(a.query_qualities), 4) |
| 361 |
self.assertEqual(a.query_qualities_str, "qrst") |
| 362 |
|
| 363 |
def testUpdateQualString(self): |
| 364 |
a = pysam.AlignedSegment() |
| 365 |
a.query_sequence = "ATGC" |
| 366 |
a.query_qualities = "qrst" |
| 367 |
self.assertEqual(len(a.query_qualities), 4) |
| 368 |
self.assertEqual(a.query_qualities_str, "qrst") |
| 369 |
self.assertEqual(a.qual, "qrst") |
| 370 |
|
| 371 |
def testUpdateQualString2(self): |
| 372 |
a = pysam.AlignedSegment() |
| 373 |
a.query_sequence = "ATGC" |
| 374 |
a.query_qualities_str = "qrst" |
| 375 |
self.assertEqual(len(a.query_qualities), 4) |
| 376 |
self.assertEqual(a.query_qualities_str, "qrst") |
| 377 |
self.assertEqual(a.qual, "qrst") |
| 378 |
|
| 379 |
def testUpdateQualTuple(self): |
| 380 |
a = pysam.AlignedSegment() |
| 381 |
a.query_sequence = "ATGC" |
| 382 |
a.query_qualities = (80, 81, 82, 83) |
| 383 |
self.assertEqual(len(a.query_qualities), 4) |
| 384 |
self.assertEqual(a.query_qualities_str, "qrst") |
| 385 |
|
| 386 |
def testLargeRead(self): |
| 387 |
"""build an example read.""" |
| 388 |
|
| 389 |
a = pysam.AlignedSegment() |
| 390 |
a.query_name = "read_12345" |
| 391 |
a.query_sequence = "ATGC" * 200 |
| 392 |
a.flag = 0 |
| 393 |
a.reference_id = -1 |
| 394 |
a.reference_start = 20 |
| 395 |
a.mapping_quality = 20 |
| 396 |
a.cigartuples = ((0, 4 * 200),) |
| 397 |
a.next_reference_id = 0 |
| 398 |
a.next_reference_start = 200 |
| 399 |
a.template_length = 167 |
| 400 |
a.query_qualities = pysam.qualitystring_to_array("1234") * 200 |
| 401 |
|
| 402 |
self.assertTrue(a) |
| 403 |
|
| 404 |
def testUpdateTlen(self): |
| 405 |
"""check if updating tlen works""" |
| 406 |
a = self.build_read() |
| 407 |
oldlen = a.template_length |
| 408 |
oldlen *= 2 |
| 409 |
a.template_length = oldlen |
| 410 |
self.assertEqual(a.template_length, oldlen) |
| 411 |
|
| 412 |
def testPositions(self): |
| 413 |
a = self.build_read() |
| 414 |
self.assertEqual( |
| 415 |
a.get_reference_positions(), |
| 416 |
[ |
| 417 |
20, |
| 418 |
21, |
| 419 |
22, |
| 420 |
23, |
| 421 |
24, |
| 422 |
25, |
| 423 |
26, |
| 424 |
27, |
| 425 |
28, |
| 426 |
29, |
| 427 |
31, |
| 428 |
32, |
| 429 |
33, |
| 430 |
34, |
| 431 |
35, |
| 432 |
36, |
| 433 |
37, |
| 434 |
38, |
| 435 |
39, |
| 436 |
40, |
| 437 |
41, |
| 438 |
42, |
| 439 |
43, |
| 440 |
44, |
| 441 |
45, |
| 442 |
46, |
| 443 |
47, |
| 444 |
48, |
| 445 |
49, |
| 446 |
50, |
| 447 |
51, |
| 448 |
52, |
| 449 |
53, |
| 450 |
54, |
| 451 |
55, |
| 452 |
56, |
| 453 |
57, |
| 454 |
58, |
| 455 |
59, |
| 456 |
], |
| 457 |
) |
| 458 |
|
| 459 |
self.check_get_aligned_pairs_combos_without_MD( |
| 460 |
a, |
| 461 |
[ |
| 462 |
(0, 20, CMATCH), |
| 463 |
(1, 21, CMATCH), |
| 464 |
(2, 22, CMATCH), |
| 465 |
(3, 23, CMATCH), |
| 466 |
(4, 24, CMATCH), |
| 467 |
(5, 25, CMATCH), |
| 468 |
(6, 26, CMATCH), |
| 469 |
(7, 27, CMATCH), |
| 470 |
(8, 28, CMATCH), |
| 471 |
(9, 29, CMATCH), |
| 472 |
(None, 30, CDEL), |
| 473 |
(10, 31, CMATCH), |
| 474 |
(11, 32, CMATCH), |
| 475 |
(12, 33, CMATCH), |
| 476 |
(13, 34, CMATCH), |
| 477 |
(14, 35, CMATCH), |
| 478 |
(15, 36, CMATCH), |
| 479 |
(16, 37, CMATCH), |
| 480 |
(17, 38, CMATCH), |
| 481 |
(18, 39, CMATCH), |
| 482 |
(19, None, CINS), |
| 483 |
(20, 40, CMATCH), |
| 484 |
(21, 41, CMATCH), |
| 485 |
(22, 42, CMATCH), |
| 486 |
(23, 43, CMATCH), |
| 487 |
(24, 44, CMATCH), |
| 488 |
(25, 45, CMATCH), |
| 489 |
(26, 46, CMATCH), |
| 490 |
(27, 47, CMATCH), |
| 491 |
(28, 48, CMATCH), |
| 492 |
(29, 49, CMATCH), |
| 493 |
(30, 50, CMATCH), |
| 494 |
(31, 51, CMATCH), |
| 495 |
(32, 52, CMATCH), |
| 496 |
(33, 53, CMATCH), |
| 497 |
(34, 54, CMATCH), |
| 498 |
(35, 55, CMATCH), |
| 499 |
(36, 56, CMATCH), |
| 500 |
(37, 57, CMATCH), |
| 501 |
(38, 58, CMATCH), |
| 502 |
(39, 59, CMATCH), |
| 503 |
], |
| 504 |
) |
| 505 |
|
| 506 |
self.assertEqual( |
| 507 |
a.get_reference_positions(), |
| 508 |
[ |
| 509 |
x[1] |
| 510 |
for x in a.get_aligned_pairs() |
| 511 |
if x[0] is not None and x[1] is not None |
| 512 |
], |
| 513 |
) |
| 514 |
|
| 515 |
self.assertEqual(a.reference_length, a.get_aligned_pairs()[-1][0] + 1) |
| 516 |
|
| 517 |
self.assertEqual(a.get_reference_positions()[-1], a.reference_end - 1) |
| 518 |
|
| 519 |
def testFullReferencePositions(self): |
| 520 |
"""see issue 26""" |
| 521 |
a = self.build_read() |
| 522 |
a.cigar = [(4, 30), (0, 20), (1, 3), (0, 47)] |
| 523 |
|
| 524 |
self.assertEqual(100, len(a.get_reference_positions(full_length=True))) |
| 525 |
|
| 526 |
def testBlocks(self): |
| 527 |
a = self.build_read() |
| 528 |
self.assertEqual(a.get_blocks(), [(20, 30), (31, 40), (40, 60)]) |
| 529 |
|
| 530 |
def test_infer_query_length(self): |
| 531 |
"""Test infer_query_length on M|=|X|I|D|H|S cigar ops""" |
| 532 |
a = self.build_read() |
| 533 |
a.cigarstring = "40M" |
| 534 |
self.assertEqual(a.infer_query_length(), 40) |
| 535 |
a.cigarstring = "40=" |
| 536 |
self.assertEqual(a.infer_query_length(), 40) |
| 537 |
a.cigarstring = "40X" |
| 538 |
self.assertEqual(a.infer_query_length(), 40) |
| 539 |
a.cigarstring = "20M5I20M" |
| 540 |
self.assertEqual(a.infer_query_length(), 45) |
| 541 |
a.cigarstring = "20M5D20M" |
| 542 |
self.assertEqual(a.infer_query_length(), 40) |
| 543 |
a.cigarstring = "5H35M" |
| 544 |
self.assertEqual(a.infer_query_length(), 35) |
| 545 |
a.cigarstring = "5S35M" |
| 546 |
self.assertEqual(a.infer_query_length(), 40) |
| 547 |
a.cigarstring = "35M5H" |
| 548 |
self.assertEqual(a.infer_query_length(), 35) |
| 549 |
a.cigarstring = "35M5S" |
| 550 |
self.assertEqual(a.infer_query_length(), 40) |
| 551 |
a.cigarstring = None |
| 552 |
self.assertEqual(a.infer_query_length(), None) |
| 553 |
|
| 554 |
def test_infer_read_length(self): |
| 555 |
"""Test infer_read_length on M|=|X|I|D|H|S cigar ops""" |
| 556 |
a = self.build_read() |
| 557 |
a.cigarstring = "40M" |
| 558 |
self.assertEqual(a.infer_read_length(), 40) |
| 559 |
a.cigarstring = "40=" |
| 560 |
self.assertEqual(a.infer_read_length(), 40) |
| 561 |
a.cigarstring = "40X" |
| 562 |
self.assertEqual(a.infer_read_length(), 40) |
| 563 |
a.cigarstring = "20M5I20M" |
| 564 |
self.assertEqual(a.infer_read_length(), 45) |
| 565 |
a.cigarstring = "20M5D20M" |
| 566 |
self.assertEqual(a.infer_read_length(), 40) |
| 567 |
a.cigarstring = "5H35M" |
| 568 |
self.assertEqual(a.infer_read_length(), 40) |
| 569 |
a.cigarstring = "5S35M" |
| 570 |
self.assertEqual(a.infer_read_length(), 40) |
| 571 |
a.cigarstring = "35M5H" |
| 572 |
self.assertEqual(a.infer_read_length(), 40) |
| 573 |
a.cigarstring = "35M5S" |
| 574 |
self.assertEqual(a.infer_read_length(), 40) |
| 575 |
a.cigarstring = None |
| 576 |
self.assertEqual(a.infer_read_length(), None) |
| 577 |
|
| 578 |
def test_get_aligned_pairs_soft_clipping(self): |
| 579 |
a = self.build_read() |
| 580 |
a.cigartuples = ((4, 2), (0, 35), (4, 3)) |
| 581 |
self.check_get_aligned_pairs_combos_without_MD( |
| 582 |
a, |
| 583 |
[(0, None, CSOFT_CLIP), (1, None, CSOFT_CLIP)] |
| 584 |
+ [ |
| 585 |
(qpos, refpos, CMATCH) |
| 586 |
for (qpos, refpos) in zip(range(2, 2 + 35), range(20, 20 + 35)) |
| 587 |
] |
| 588 |
+ [(37, None, CSOFT_CLIP), (38, None, CSOFT_CLIP), (39, None, CSOFT_CLIP)], |
| 589 |
) |
| 590 |
|
| 591 |
def test_get_aligned_pairs_hard_clipping(self): |
| 592 |
a = self.build_read() |
| 593 |
a.cigartuples = ((5, 2), (0, 35), (5, 3)) |
| 594 |
self.check_get_aligned_pairs_combos_without_MD( |
| 595 |
a, |
| 596 |
|
| 597 |
[ |
| 598 |
(qpos, refpos, CMATCH) |
| 599 |
for (qpos, refpos) in zip(range(0, 0 + 35), range(20, 20 + 35)) |
| 600 |
], |
| 601 |
) |
| 602 |
|
| 603 |
def test_get_aligned_pairs_skip(self): |
| 604 |
a = self.build_read() |
| 605 |
a.cigarstring = "2M100D38M" |
| 606 |
self.check_get_aligned_pairs_combos_without_MD( |
| 607 |
a, |
| 608 |
[(0, 20, CMATCH), (1, 21, CMATCH)] |
| 609 |
+ [(None, refpos, CDEL) for refpos in range(22, 22 + 100)] |
| 610 |
+ [ |
| 611 |
(qpos, refpos, CMATCH) |
| 612 |
for (qpos, refpos) in zip( |
| 613 |
range(2, 2 + 38), range(20 + 2 + 100, 20 + 2 + 100 + 38) |
| 614 |
) |
| 615 |
], |
| 616 |
) |
| 617 |
|
| 618 |
def test_get_aligned_pairs_match_mismatch(self): |
| 619 |
a = self.build_read() |
| 620 |
a.cigartuples = ((7, 20), (8, 20)) |
| 621 |
self.check_get_aligned_pairs_combos_without_MD( |
| 622 |
a, |
| 623 |
[ |
| 624 |
(qpos, refpos, CEQUAL if qpos < 20 else CDIFF) |
| 625 |
for (qpos, refpos) in zip(range(0, 0 + 40), range(20, 20 + 40)) |
| 626 |
], |
| 627 |
) |
| 628 |
|
| 629 |
def test_get_aligned_pairs_padding(self): |
| 630 |
a = self.build_read() |
| 631 |
a.cigartuples = ((0, 1), (6, 1), (0, 1)) |
| 632 |
|
| 633 |
|
| 634 |
self.check_get_aligned_pairs_combos_without_MD(a, |
| 635 |
[(0, 20, CMATCH), (1, None, CPAD), (2, 21, CMATCH)]) |
| 636 |
|
| 637 |
def test_get_aligned_pairs_padding_via_cigarstring(self): |
| 638 |
a = self.build_read() |
| 639 |
a.cigarstring = "1M1P1M" |
| 640 |
|
| 641 |
|
| 642 |
self.check_get_aligned_pairs_combos_without_MD(a, |
| 643 |
[(0, 20, CMATCH), (1, None, CPAD), (2, 21, CMATCH)]) |
| 644 |
|
| 645 |
def test_get_aligned_pairs_padding_with_seq(self): |
| 646 |
a = self.build_read() |
| 647 |
a.query_sequence = "AGT" |
| 648 |
a.cigarstring = "1M1P1M" |
| 649 |
a.set_tag("MD", "2") |
| 650 |
|
| 651 |
|
| 652 |
|
| 653 |
|
| 654 |
|
| 655 |
|
| 656 |
|
| 657 |
|
| 658 |
|
| 659 |
|
| 660 |
|
| 661 |
|
| 662 |
|
| 663 |
|
| 664 |
|
| 665 |
|
| 666 |
|
| 667 |
|
| 668 |
|
| 669 |
|
| 670 |
self.check_get_aligned_pairs_combos(a, |
| 671 |
[(0, 20, "A", CMATCH), (1, None, None, CPAD), (2, 21, "T", CMATCH)]) |
| 672 |
|
| 673 |
def test_get_aligned_pairs(self): |
| 674 |
a = self.build_read() |
| 675 |
a.query_sequence = "A" * 9 |
| 676 |
a.cigarstring = "9M" |
| 677 |
a.set_tag("MD", "9") |
| 678 |
self.check_get_aligned_pairs_combos( |
| 679 |
a, |
| 680 |
[ |
| 681 |
(0, 20, "A", CMATCH), |
| 682 |
(1, 21, "A", CMATCH), |
| 683 |
(2, 22, "A", CMATCH), |
| 684 |
(3, 23, "A", CMATCH), |
| 685 |
(4, 24, "A", CMATCH), |
| 686 |
(5, 25, "A", CMATCH), |
| 687 |
(6, 26, "A", CMATCH), |
| 688 |
(7, 27, "A", CMATCH), |
| 689 |
(8, 28, "A", CMATCH), |
| 690 |
], |
| 691 |
) |
| 692 |
|
| 693 |
a.set_tag("MD", "4C4") |
| 694 |
self.check_get_aligned_pairs_combos( |
| 695 |
a, |
| 696 |
[ |
| 697 |
(0, 20, "A", CMATCH), |
| 698 |
(1, 21, "A", CMATCH), |
| 699 |
(2, 22, "A", CMATCH), |
| 700 |
(3, 23, "A", CMATCH), |
| 701 |
(4, 24, "c", CMATCH), |
| 702 |
(5, 25, "A", CMATCH), |
| 703 |
(6, 26, "A", CMATCH), |
| 704 |
(7, 27, "A", CMATCH), |
| 705 |
(8, 28, "A", CMATCH), |
| 706 |
], |
| 707 |
) |
| 708 |
|
| 709 |
a.cigarstring = "5M2D4M" |
| 710 |
a.set_tag("MD", "4C^TT4") |
| 711 |
self.check_get_aligned_pairs_combos( |
| 712 |
a, |
| 713 |
[ |
| 714 |
(0, 20, "A", CMATCH), |
| 715 |
(1, 21, "A", CMATCH), |
| 716 |
(2, 22, "A", CMATCH), |
| 717 |
(3, 23, "A", CMATCH), |
| 718 |
(4, 24, "c", CMATCH), |
| 719 |
(None, 25, "T", CDEL), |
| 720 |
(None, 26, "T", CDEL), |
| 721 |
(5, 27, "A", CMATCH), |
| 722 |
(6, 28, "A", CMATCH), |
| 723 |
(7, 29, "A", CMATCH), |
| 724 |
(8, 30, "A", CMATCH), |
| 725 |
], |
| 726 |
) |
| 727 |
|
| 728 |
a.cigarstring = "5M2D2I2M" |
| 729 |
a.set_tag("MD", "4C^TT2") |
| 730 |
self.check_get_aligned_pairs_combos( |
| 731 |
a, |
| 732 |
[ |
| 733 |
(0, 20, "A", CMATCH), |
| 734 |
(1, 21, "A", CMATCH), |
| 735 |
(2, 22, "A", CMATCH), |
| 736 |
(3, 23, "A", CMATCH), |
| 737 |
(4, 24, "c", CMATCH), |
| 738 |
(None, 25, "T", CDEL), |
| 739 |
(None, 26, "T", CDEL), |
| 740 |
(5, None, None, CINS), |
| 741 |
(6, None, None, CINS), |
| 742 |
(7, 27, "A", CMATCH), |
| 743 |
(8, 28, "A", CMATCH), |
| 744 |
], |
| 745 |
) |
| 746 |
|
| 747 |
def test_get_aligned_pairs_with_malformed_MD_tag(self): |
| 748 |
|
| 749 |
a = self.build_read() |
| 750 |
a.query_sequence = "A" * 9 |
| 751 |
|
| 752 |
|
| 753 |
a.cigarstring = "64M2D85M2S" |
| 754 |
a.set_tag("MD", "64^TG86A0") |
| 755 |
self.assertRaises(AssertionError, a.get_aligned_pairs, with_seq=True) |
| 756 |
|
| 757 |
def test_get_aligned_pairs_skip_reference(self): |
| 758 |
a = self.build_read() |
| 759 |
a.query_sequence = "A" * 10 |
| 760 |
a.cigarstring = "5M1N5M" |
| 761 |
a.set_tag("MD", "10") |
| 762 |
|
| 763 |
self.check_get_aligned_pairs_combos( |
| 764 |
a, |
| 765 |
[ |
| 766 |
(0, 20, "A", CMATCH), |
| 767 |
(1, 21, "A", CMATCH), |
| 768 |
(2, 22, "A", CMATCH), |
| 769 |
(3, 23, "A", CMATCH), |
| 770 |
(4, 24, "A", CMATCH), |
| 771 |
(None, 25, None, CREF_SKIP), |
| 772 |
(5, 26, "A", CMATCH), |
| 773 |
(6, 27, "A", CMATCH), |
| 774 |
(7, 28, "A", CMATCH), |
| 775 |
(8, 29, "A", CMATCH), |
| 776 |
(9, 30, "A", CMATCH), |
| 777 |
] |
| 778 |
) |
| 779 |
|
| 780 |
def test_equivalence_matches_only_and_with_seq(self): |
| 781 |
a = self.build_read() |
| 782 |
a.query_sequence = "ACGT" * 2 |
| 783 |
a.cigarstring = "4M1D4M" |
| 784 |
a.set_tag("MD", "4^x4") |
| 785 |
self.check_get_aligned_pairs_combos( |
| 786 |
a, |
| 787 |
list(zip(range(0, 4), range(20, 24), "ACGT", [CMATCH] * 4)) |
| 788 |
+ [(None, 24, "x", CDEL)] |
| 789 |
+ list(zip(range(4, 8), range(25, 29), "ACGT", [CMATCH] * 4)), |
| 790 |
) |
| 791 |
|
| 792 |
a = self.build_read() |
| 793 |
a.query_sequence = "ACGT" * 2 |
| 794 |
a.cigarstring = "4M1N4M" |
| 795 |
a.set_tag("MD", "8") |
| 796 |
self.check_get_aligned_pairs_combos( |
| 797 |
a, |
| 798 |
list(zip(range(0, 4), range(20, 24), "ACGT", [CMATCH] * 4)) |
| 799 |
+ [(None, 24, None, 3)] |
| 800 |
+ list(zip(range(4, 8), range(25, 29), "ACGT", [CMATCH] * 4)), |
| 801 |
) |
| 802 |
|
| 803 |
def test_get_aligned_pairs_lowercase_md(self): |
| 804 |
a = self.build_read() |
| 805 |
a.query_sequence = "A" * 10 |
| 806 |
a.cigarstring = "10M" |
| 807 |
a.set_tag("MD", "5g4") |
| 808 |
self.assertEqual( |
| 809 |
a.get_aligned_pairs(with_seq=True), |
| 810 |
[ |
| 811 |
(0, 20, "A"), |
| 812 |
(1, 21, "A"), |
| 813 |
(2, 22, "A"), |
| 814 |
(3, 23, "A"), |
| 815 |
(4, 24, "A"), |
| 816 |
(5, 25, "g"), |
| 817 |
(6, 26, "A"), |
| 818 |
(7, 27, "A"), |
| 819 |
(8, 28, "A"), |
| 820 |
(9, 29, "A"), |
| 821 |
], |
| 822 |
) |
| 823 |
|
| 824 |
def test_get_aligned_pairs_uppercase_md(self): |
| 825 |
a = self.build_read() |
| 826 |
a.query_sequence = "A" * 10 |
| 827 |
a.cigarstring = "10M" |
| 828 |
a.set_tag("MD", "5G4") |
| 829 |
self.assertEqual( |
| 830 |
a.get_aligned_pairs(with_seq=True), |
| 831 |
[ |
| 832 |
(0, 20, "A"), |
| 833 |
(1, 21, "A"), |
| 834 |
(2, 22, "A"), |
| 835 |
(3, 23, "A"), |
| 836 |
(4, 24, "A"), |
| 837 |
(5, 25, "g"), |
| 838 |
(6, 26, "A"), |
| 839 |
(7, 27, "A"), |
| 840 |
(8, 28, "A"), |
| 841 |
(9, 29, "A"), |
| 842 |
], |
| 843 |
) |
| 844 |
|
| 845 |
def test_get_aligned_pairs_1character_md(self): |
| 846 |
a = self.build_read() |
| 847 |
a.query_sequence = "A" * 7 |
| 848 |
a.cigarstring = "7M" |
| 849 |
a.set_tag("MD", "7", value_type="A") |
| 850 |
self.assertEqual( |
| 851 |
a.get_aligned_pairs(with_seq=True), |
| 852 |
[ |
| 853 |
(0, 20, "A"), |
| 854 |
(1, 21, "A"), |
| 855 |
(2, 22, "A"), |
| 856 |
(3, 23, "A"), |
| 857 |
(4, 24, "A"), |
| 858 |
(5, 25, "A"), |
| 859 |
(6, 26, "A"), |
| 860 |
], |
| 861 |
) |
| 862 |
|
| 863 |
def test_get_aligned_pairs_bad_type_md(self): |
| 864 |
a = self.build_read() |
| 865 |
a.query_sequence = "A" * 7 |
| 866 |
a.cigarstring = "7M" |
| 867 |
a.set_tag("MD", 7) |
| 868 |
with self.assertRaises(TypeError): |
| 869 |
a.get_aligned_pairs(with_seq=True) |
| 870 |
|
| 871 |
def testNoSequence(self): |
| 872 |
"""issue 176: retrieving length without query sequence |
| 873 |
with soft-clipping. |
| 874 |
""" |
| 875 |
a = self.build_read() |
| 876 |
a.query_sequence = None |
| 877 |
a.cigarstring = "20M" |
| 878 |
self.assertEqual(a.query_alignment_length, 20) |
| 879 |
a.cigarstring = "20M1S" |
| 880 |
self.assertEqual(a.query_alignment_length, 20) |
| 881 |
a.cigarstring = "20M1H" |
| 882 |
self.assertEqual(a.query_alignment_length, 20) |
| 883 |
a.cigarstring = "1S20M" |
| 884 |
self.assertEqual(a.query_alignment_length, 20) |
| 885 |
a.cigarstring = "1H20M" |
| 886 |
self.assertEqual(a.query_alignment_length, 20) |
| 887 |
a.cigarstring = "1S20M1S" |
| 888 |
self.assertEqual(a.query_alignment_length, 20) |
| 889 |
a.cigarstring = "1H20M1H" |
| 890 |
self.assertEqual(a.query_alignment_length, 20) |
| 891 |
|
| 892 |
def test_query_length_is_limited(self): |
| 893 |
a = self.build_read() |
| 894 |
a.query_name = "A" * 1 |
| 895 |
a.query_name = "A" * 254 |
| 896 |
self.assertRaises(ValueError, setattr, a, "query_name", "A" * 255) |
| 897 |
|
| 898 |
def test_header_accessible(self): |
| 899 |
a = self.build_read() |
| 900 |
self.assertTrue(isinstance(a.header, pysam.AlignmentHeader)) |
| 901 |
|
| 902 |
def test_bin_values_for_unmapped_reads_ignore_length(self): |
| 903 |
a = self.build_read() |
| 904 |
|
| 905 |
a.cigarstring = "2000000M" |
| 906 |
self.assertEqual(a.bin, 9) |
| 907 |
|
| 908 |
a.is_unmapped = True |
| 909 |
self.assertTrue(a.is_unmapped) |
| 910 |
self.assertFalse(a.is_mapped) |
| 911 |
self.assertEqual(a.bin, 4681) |
| 912 |
|
| 913 |
|
| 914 |
a.reference_start = -1 |
| 915 |
self.assertEqual(a.reference_start, -1) |
| 916 |
self.assertEqual(a.bin, 4680) |
| 917 |
|
| 918 |
def test_bin_values_for_mapped_reads_are_updated(self): |
| 919 |
a = self.build_read() |
| 920 |
a.pos = 20000 |
| 921 |
self.assertFalse(a.is_unmapped) |
| 922 |
self.assertTrue(a.is_mapped) |
| 923 |
self.assertEqual(a.bin, 4682) |
| 924 |
|
| 925 |
|
| 926 |
a.cigarstring = "2000000M" |
| 927 |
self.assertEqual(a.bin, 9) |
| 928 |
|
| 929 |
|
| 930 |
a.cigarstring = "20M" |
| 931 |
self.assertEqual(a.bin, 4682) |
| 932 |
|
| 933 |
|
| 934 |
a.reference_start = 2000000 |
| 935 |
self.assertEqual(a.bin, 4803) |
| 936 |
|
| 937 |
|
| 938 |
class TestTidMapping(ReadTest): |
| 939 |
def test_reference_name_can_be_set_to_none(self): |
| 940 |
a = self.build_read() |
| 941 |
a.reference_name = None |
| 942 |
self.assertEqual(a.reference_name, None) |
| 943 |
self.assertEqual(a.reference_id, -1) |
| 944 |
|
| 945 |
def test_reference_name_can_be_set_to_asterisk(self): |
| 946 |
a = self.build_read() |
| 947 |
a.reference_name = "*" |
| 948 |
self.assertEqual(a.reference_name, None) |
| 949 |
self.assertEqual(a.reference_id, -1) |
| 950 |
|
| 951 |
def test_reference_name_can_be_set_to_chromosome(self): |
| 952 |
a = self.build_read() |
| 953 |
a.reference_name = "chr1" |
| 954 |
self.assertEqual(a.reference_name, "chr1") |
| 955 |
self.assertEqual(a.reference_id, 0) |
| 956 |
|
| 957 |
def test_reference_name_can_not_be_set_to_unknown_chromosome(self): |
| 958 |
a = self.build_read() |
| 959 |
self.assertRaises(ValueError, setattr, a, "reference_name", "chrX") |
| 960 |
|
| 961 |
def test_tid_can_be_set_to_missing(self): |
| 962 |
a = self.build_read() |
| 963 |
a.reference_id = -1 |
| 964 |
self.assertEqual(a.reference_id, -1) |
| 965 |
self.assertEqual(a.reference_name, None) |
| 966 |
|
| 967 |
def test_tid_can_be_set_to_missing_without_header(self): |
| 968 |
a = pysam.AlignedSegment() |
| 969 |
a.reference_id = -1 |
| 970 |
self.assertEqual(a.reference_id, -1) |
| 971 |
self.assertEqual(a.reference_name, None) |
| 972 |
|
| 973 |
def test_tid_can_be_set_without_header(self): |
| 974 |
a = pysam.AlignedSegment() |
| 975 |
a.reference_id = 1 |
| 976 |
self.assertRaises(ValueError, getattr, a, "reference_name") |
| 977 |
|
| 978 |
def test_tid_can_be_set_to_chromosome(self): |
| 979 |
a = self.build_read() |
| 980 |
a.reference_id = 0 |
| 981 |
self.assertEqual(a.reference_id, 0) |
| 982 |
self.assertEqual(a.reference_name, "chr1") |
| 983 |
|
| 984 |
def test_tid_can_not_be_set_to_unknown_chromosome(self): |
| 985 |
a = self.build_read() |
| 986 |
self.assertRaises(ValueError, setattr, a, "reference_id", 2) |
| 987 |
|
| 988 |
def test_unmapped_tid_is_asterisk_in_output(self): |
| 989 |
a = self.build_read() |
| 990 |
a.reference_id = -1 |
| 991 |
self.assertEqual(a.to_string().split("\t")[2], "*") |
| 992 |
|
| 993 |
|
| 994 |
class TestNextTidMapping(ReadTest): |
| 995 |
def test_next_reference_name_can_be_set_to_none(self): |
| 996 |
a = self.build_read() |
| 997 |
a.next_reference_name = None |
| 998 |
self.assertEqual(a.next_reference_name, None) |
| 999 |
self.assertEqual(a.next_reference_id, -1) |
| 1000 |
|
| 1001 |
def test_next_reference_name_can_be_set_to_asterisk(self): |
| 1002 |
a = self.build_read() |
| 1003 |
a.next_reference_name = "*" |
| 1004 |
self.assertEqual(a.next_reference_name, None) |
| 1005 |
self.assertEqual(a.next_reference_id, -1) |
| 1006 |
|
| 1007 |
def test_next_reference_name_can_be_set_to_chromosome(self): |
| 1008 |
a = self.build_read() |
| 1009 |
a.next_reference_name = "chr1" |
| 1010 |
self.assertEqual(a.next_reference_name, "chr1") |
| 1011 |
self.assertEqual(a.next_reference_id, 0) |
| 1012 |
|
| 1013 |
def test_next_reference_name_can_not_be_set_to_unknown_chromosome(self): |
| 1014 |
a = self.build_read() |
| 1015 |
self.assertRaises(ValueError, setattr, a, "next_reference_name", "chrX") |
| 1016 |
|
| 1017 |
def test_next_tid_can_be_set_to_missing(self): |
| 1018 |
a = self.build_read() |
| 1019 |
a.next_reference_id = -1 |
| 1020 |
self.assertEqual(a.next_reference_id, -1) |
| 1021 |
self.assertEqual(a.next_reference_name, None) |
| 1022 |
|
| 1023 |
def test_next_tid_can_be_set_to_equal(self): |
| 1024 |
a = self.build_read() |
| 1025 |
a.reference_name = "chr1" |
| 1026 |
a.next_reference_name = "=" |
| 1027 |
self.assertEqual(a.next_reference_id, a.reference_id) |
| 1028 |
self.assertEqual(a.next_reference_name, a.reference_name) |
| 1029 |
self.assertEqual(a.to_string().split("\t")[6], "=") |
| 1030 |
|
| 1031 |
def test_next_tid_can_be_set_to_missing_without_header(self): |
| 1032 |
a = pysam.AlignedSegment() |
| 1033 |
a.next_reference_id = -1 |
| 1034 |
self.assertEqual(a.next_reference_id, -1) |
| 1035 |
self.assertEqual(a.next_reference_name, None) |
| 1036 |
|
| 1037 |
def test_next_tid_can_be_set_without_header(self): |
| 1038 |
a = pysam.AlignedSegment() |
| 1039 |
a.next_reference_id = 1 |
| 1040 |
self.assertRaises(ValueError, getattr, a, "next_reference_name") |
| 1041 |
|
| 1042 |
def test_next_tid_can_be_set_to_chromosome(self): |
| 1043 |
a = self.build_read() |
| 1044 |
a.next_reference_id = 0 |
| 1045 |
self.assertEqual(a.next_reference_id, 0) |
| 1046 |
self.assertEqual(a.next_reference_name, "chr1") |
| 1047 |
|
| 1048 |
def test_next_tid_can_not_be_set_to_unknown_chromosome(self): |
| 1049 |
a = self.build_read() |
| 1050 |
self.assertRaises(ValueError, setattr, a, "next_reference_id", 2) |
| 1051 |
|
| 1052 |
def test_next_unmapped_tid_is_asterisk_in_output(self): |
| 1053 |
a = self.build_read() |
| 1054 |
a.next_reference_id = -1 |
| 1055 |
self.assertEqual(a.to_string().split("\t")[6], "*") |
| 1056 |
|
| 1057 |
|
| 1058 |
class TestCigar(ReadTest): |
| 1059 |
def testCigarString(self): |
| 1060 |
r = self.build_read() |
| 1061 |
self.assertEqual(r.cigarstring, "10M1D9M1I20M") |
| 1062 |
r.cigarstring = "20M10D20M" |
| 1063 |
self.assertEqual(r.cigartuples, [(0, 20), (2, 10), (0, 20)]) |
| 1064 |
|
| 1065 |
r.cigarstring = None |
| 1066 |
self.assertEqual(r.cigarstring, None) |
| 1067 |
|
| 1068 |
r.cigarstring = "40M" |
| 1069 |
self.assertEqual(r.cigartuples, [(0, 40)]) |
| 1070 |
r.cigarstring = "" |
| 1071 |
self.assertEqual(r.cigarstring, None) |
| 1072 |
|
| 1073 |
r.cigarstring = "40M" |
| 1074 |
self.assertEqual(r.cigartuples, [(0, 40)]) |
| 1075 |
r.cigarstring = "*" |
| 1076 |
self.assertEqual(r.cigarstring, None) |
| 1077 |
|
| 1078 |
def testCigar(self): |
| 1079 |
r = self.build_read() |
| 1080 |
self.assertEqual(r.cigartuples, [(0, 10), (2, 1), (0, 9), (1, 1), (0, 20)]) |
| 1081 |
|
| 1082 |
r.cigartuples = None |
| 1083 |
self.assertEqual(r.cigartuples, None) |
| 1084 |
|
| 1085 |
|
| 1086 |
class TestCigarStats(ReadTest): |
| 1087 |
def testStats(self): |
| 1088 |
a = self.build_read() |
| 1089 |
|
| 1090 |
a.cigarstring = None |
| 1091 |
self.assertEqual( |
| 1092 |
[list(x) for x in a.get_cigar_stats()], |
| 1093 |
[[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]], |
| 1094 |
) |
| 1095 |
|
| 1096 |
a.cigarstring = "10M" |
| 1097 |
self.assertEqual( |
| 1098 |
[list(x) for x in a.get_cigar_stats()], |
| 1099 |
[[10, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]], |
| 1100 |
) |
| 1101 |
|
| 1102 |
a.cigarstring = "10M2I2M" |
| 1103 |
self.assertEqual( |
| 1104 |
[list(x) for x in a.get_cigar_stats()], |
| 1105 |
[[12, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0], [2, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0]], |
| 1106 |
) |
| 1107 |
|
| 1108 |
for i, x in enumerate("MIDNSHP=X"): |
| 1109 |
a.cigarstring = "2{}".format(x) |
| 1110 |
expected = [[0] * 11, [0] * 11] |
| 1111 |
expected[0][i] = 2 |
| 1112 |
expected[1][i] = 1 |
| 1113 |
self.assertEqual([list(x) for x in a.get_cigar_stats()], expected) |
| 1114 |
|
| 1115 |
for i in range(1, 100): |
| 1116 |
cigarstring = "".join("10{}".format(x) |
| 1117 |
for x in iter("MIDNSHP=X")) * i |
| 1118 |
a.cigarstring = cigarstring |
| 1119 |
self.assertEqual(a.cigarstring, cigarstring) |
| 1120 |
expected = [[i * 10 for j in range(len("MIDNSHP=X"))] + [0, 0], |
| 1121 |
[i for j in range(len("MIDNSHP=X"))] + [0, 0]] |
| 1122 |
obtained = [list(x) for x in a.get_cigar_stats()] |
| 1123 |
self.assertEqual(obtained, expected) |
| 1124 |
|
| 1125 |
a.cigarstring = "10M" |
| 1126 |
a.set_tag("NM", 5) |
| 1127 |
self.assertEqual( |
| 1128 |
[list(x) for x in a.get_cigar_stats()], |
| 1129 |
[[10, 0, 0, 0, 0, 0, 0, 0, 0, 0, 5], [1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]], |
| 1130 |
) |
| 1131 |
|
| 1132 |
a.cigarstring = None |
| 1133 |
self.assertEqual( |
| 1134 |
[list(x) for x in a.get_cigar_stats()], |
| 1135 |
[[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 5], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]], |
| 1136 |
) |
| 1137 |
|
| 1138 |
|
| 1139 |
class TestAlignedPairs(unittest.TestCase): |
| 1140 |
filename = os.path.join(BAM_DATADIR, "example_aligned_pairs.bam") |
| 1141 |
|
| 1142 |
def testReferenceBases(self): |
| 1143 |
"""reference bases should always be the same nucleotide |
| 1144 |
""" |
| 1145 |
reference_bases = collections.defaultdict(list) |
| 1146 |
with pysam.AlignmentFile(self.filename) as inf: |
| 1147 |
for c in inf.pileup(): |
| 1148 |
for r in c.pileups: |
| 1149 |
for read, ref, base in r.alignment.get_aligned_pairs(with_seq=True): |
| 1150 |
if ref is None: |
| 1151 |
continue |
| 1152 |
reference_bases[ref].append(base.upper()) |
| 1153 |
|
| 1154 |
for x, y in reference_bases.items(): |
| 1155 |
self.assertEqual(len(set(y)), 1) |
| 1156 |
|
| 1157 |
|
| 1158 |
class TestBaseModifications(unittest.TestCase): |
| 1159 |
def testChebi(self): |
| 1160 |
"""reference bases should always be the same nucleotide |
| 1161 |
""" |
| 1162 |
filename = os.path.join(BAM_DATADIR, "MM-chebi.bam") |
| 1163 |
expect = { |
| 1164 |
("C", 0, "m"): [(6, 102), (17, 128), (20, 153), (31, 179), (34, 204)], |
| 1165 |
("N", 0, "n"): [(15, 212)], |
| 1166 |
("C", 0, 76792): [(19, 161), (34, 33)], |
| 1167 |
} |
| 1168 |
|
| 1169 |
with pysam.AlignmentFile(filename, check_sq=False) as inf: |
| 1170 |
r = next(iter(inf)) |
| 1171 |
self.assertDictEqual(r.modified_bases, expect) |
| 1172 |
|
| 1173 |
def testDouble(self): |
| 1174 |
"""reference bases should always be the same nucleotide |
| 1175 |
""" |
| 1176 |
filename = os.path.join(BAM_DATADIR, "MM-double.bam") |
| 1177 |
expect = { |
| 1178 |
("G", 1, "m"): [(1, 115), (12, 141), (13, 166), (22, 192)], |
| 1179 |
("C", 0, "m"): [(7, 128), (30, 153), (31, 179)], |
| 1180 |
("G", 0, "o"): [(13, 102)], |
| 1181 |
} |
| 1182 |
|
| 1183 |
with pysam.AlignmentFile(filename, check_sq=False) as inf: |
| 1184 |
r = next(iter(inf)) |
| 1185 |
self.assertDictEqual(r.modified_bases, expect) |
| 1186 |
|
| 1187 |
def testExplicit(self): |
| 1188 |
"""reference bases should always be the same nucleotide |
| 1189 |
""" |
| 1190 |
filename = os.path.join(BAM_DATADIR, "MM-explicit.bam") |
| 1191 |
expected_output = [ |
| 1192 |
{("C", 0, "m"): [(9, 200), (10, 50), (14, 160)], ("C", 0, "h"): [(9, 10), (10, 170), (14, 20)]}, |
| 1193 |
{("C", 0, "m"): [(9, 200), (10, 50), (13, 10), (14, 160), (16, 10)], |
| 1194 |
("C", 0, "h"): [(9, 10), (10, 170), (13, 5), (14, 20), (16, 5)]}, |
| 1195 |
{("C", 0, "m"): [(9, 200), (14, 160)], ("C", 0, "h"): [(9, 10), (10, 170), (13, 5), (14, 20), (16, 5)]}, |
| 1196 |
] |
| 1197 |
|
| 1198 |
with pysam.AlignmentFile(filename, check_sq=False) as inf: |
| 1199 |
for r, expected in zip(inf, expected_output): |
| 1200 |
self.assertDictEqual(r.modified_bases, expected) |
| 1201 |
|
| 1202 |
def testMulti(self): |
| 1203 |
"""reference bases should always be the same nucleotide |
| 1204 |
""" |
| 1205 |
filename = os.path.join(BAM_DATADIR, "MM-multi.bam") |
| 1206 |
expect = { |
| 1207 |
"r1": { |
| 1208 |
("C", 0, "m"): [(6, 128), (17, 153), (20, 179), (31, 204), (34, 230)], |
| 1209 |
("N", 0, "n"): [(15, 215), (18, 240)], |
| 1210 |
("C", 0, "h"): [(19, 159), (34, 6)], |
| 1211 |
}, |
| 1212 |
"r2": { |
| 1213 |
("C", 0, "m"): [ |
| 1214 |
(6, 77), |
| 1215 |
(17, 103), |
| 1216 |
(19, 128), |
| 1217 |
(20, 154), |
| 1218 |
(31, 179), |
| 1219 |
(34, 204), |
| 1220 |
], |
| 1221 |
("C", 0, "h"): [ |
| 1222 |
(6, 159), |
| 1223 |
(17, 133), |
| 1224 |
(19, 108), |
| 1225 |
(20, 82), |
| 1226 |
(31, 57), |
| 1227 |
(34, 31), |
| 1228 |
], |
| 1229 |
("N", 0, "n"): [(15, 240)], |
| 1230 |
}, |
| 1231 |
} |
| 1232 |
|
| 1233 |
with pysam.AlignmentFile(filename, check_sq=False) as inf: |
| 1234 |
for r in inf: |
| 1235 |
self.assertDictEqual(r.modified_bases, expect[r.query_name]) |
| 1236 |
|
| 1237 |
def testOrient(self): |
| 1238 |
"""reference bases should always be the same nucleotide |
| 1239 |
""" |
| 1240 |
filename = os.path.join(BAM_DATADIR, "MM-orient.bam") |
| 1241 |
expect = { |
| 1242 |
"top-fwd": [ |
| 1243 |
{("C", 0, "m"): [(7, 128), (30, 153), (31, 179)]}, |
| 1244 |
{("C", 0, "m"): [(7, 128), (30, 153), (31, 179)]}, |
| 1245 |
], |
| 1246 |
"top-rev": [ |
| 1247 |
{("C", 1, "m"): [(4, 179), (5, 153), (28, 128)]}, |
| 1248 |
{("C", 0, "m"): [(31, 179), (30, 153), (7, 128)]}, |
| 1249 |
], |
| 1250 |
"bot-fwd": [ |
| 1251 |
{("G", 1, "m"): [(1, 115), (2, 141), (18, 166), (23, 192)]}, |
| 1252 |
{("G", 1, "m"): [(1, 115), (2, 141), (18, 166), (23, 192)]}, |
| 1253 |
], |
| 1254 |
"bot-rev": [ |
| 1255 |
{("G", 0, "m"): [(12, 192), (17, 166), (33, 141), (34, 115)]}, |
| 1256 |
{("G", 1, "m"): [(23, 192), (18, 166), (2, 141), (1, 115)]}, |
| 1257 |
], |
| 1258 |
} |
| 1259 |
|
| 1260 |
with pysam.AlignmentFile(filename, check_sq=False) as inf: |
| 1261 |
for r in inf: |
| 1262 |
self.assertDictEqual(r.modified_bases, expect[r.query_name][0]) |
| 1263 |
self.assertDictEqual(r.modified_bases_forward, expect[r.query_name][1]) |
| 1264 |
for (B, s, _), mods in r.modified_bases.items(): |
| 1265 |
C = B.translate(str.maketrans("ACGTacgtNnXx", "TGCAtgcaNnXx")) |
| 1266 |
for pos, _ in mods: |
| 1267 |
if r.is_reverse: |
| 1268 |
if s == 1: |
| 1269 |
self.assertEqual( |
| 1270 |
C, r.query_sequence[pos], r.to_string() |
| 1271 |
) |
| 1272 |
else: |
| 1273 |
self.assertEqual( |
| 1274 |
C, r.query_sequence[pos], r.to_string() |
| 1275 |
) |
| 1276 |
else: |
| 1277 |
if s == 0: |
| 1278 |
self.assertEqual( |
| 1279 |
B, r.query_sequence[pos], r.to_string() |
| 1280 |
) |
| 1281 |
else: |
| 1282 |
self.assertEqual( |
| 1283 |
B, r.query_sequence[pos], r.to_string() |
| 1284 |
) |
| 1285 |
|
| 1286 |
|
| 1287 |
class TestTags(ReadTest): |
| 1288 |
def testMissingTag(self): |
| 1289 |
a = self.build_read() |
| 1290 |
self.assertRaises(KeyError, a.get_tag, "XP") |
| 1291 |
|
| 1292 |
def testEmptyTag(self): |
| 1293 |
a = self.build_read() |
| 1294 |
self.assertRaises(KeyError, a.get_tag, "XT") |
| 1295 |
|
| 1296 |
def testSetTag(self): |
| 1297 |
a = self.build_read() |
| 1298 |
self.assertEqual(False, a.has_tag("NM")) |
| 1299 |
a.set_tag("NM", 2) |
| 1300 |
self.assertEqual(True, a.has_tag("NM")) |
| 1301 |
self.assertEqual(a.get_tag("NM"), 2) |
| 1302 |
a.set_tag("NM", 3) |
| 1303 |
self.assertEqual(a.get_tag("NM"), 3) |
| 1304 |
a.set_tag("NM", None) |
| 1305 |
self.assertEqual(False, a.has_tag("NM")) |
| 1306 |
|
| 1307 |
a.set_tag("NM", None) |
| 1308 |
a.set_tag("NM", None) |
| 1309 |
|
| 1310 |
def testArrayTags(self): |
| 1311 |
read = self.build_read() |
| 1312 |
supported_dtypes = "bhBHf" |
| 1313 |
unsupported_dtypes = "lLd" |
| 1314 |
|
| 1315 |
for dtype in supported_dtypes: |
| 1316 |
key = "F" + dtype |
| 1317 |
read.set_tag(key, array.array(dtype, range(10))) |
| 1318 |
ary = read.get_tag(key) |
| 1319 |
|
| 1320 |
for dtype in unsupported_dtypes: |
| 1321 |
key = "F" + dtype |
| 1322 |
self.assertRaises( |
| 1323 |
ValueError, read.set_tag, key, array.array(dtype, range(10)) |
| 1324 |
) |
| 1325 |
|
| 1326 |
def testAddTagsType(self): |
| 1327 |
a = self.build_read() |
| 1328 |
a.tags = None |
| 1329 |
self.assertEqual(a.tags, []) |
| 1330 |
|
| 1331 |
a.setTag("X1", 5.0) |
| 1332 |
a.setTag("X2", "5.0") |
| 1333 |
a.setTag("X3", 5) |
| 1334 |
|
| 1335 |
self.assertEqual( |
| 1336 |
sorted(a.tags), sorted([("X1", 5.0), ("X2", "5.0"), ("X3", 5)]) |
| 1337 |
) |
| 1338 |
|
| 1339 |
|
| 1340 |
a.setTag("X4", 5, value_type="d") |
| 1341 |
self.assertEqual( |
| 1342 |
sorted(a.tags), sorted([("X1", 5.0), ("X2", "5.0"), ("X3", 5), ("X4", 5.0)]) |
| 1343 |
) |
| 1344 |
|
| 1345 |
|
| 1346 |
|
| 1347 |
a.setTag("X5", 5.2, value_type="i") |
| 1348 |
self.assertEqual( |
| 1349 |
sorted(a.tags), |
| 1350 |
sorted([("X1", 5.0), ("X2", "5.0"), ("X3", 5), ("X4", 5.0), ("X5", 5)]), |
| 1351 |
) |
| 1352 |
|
| 1353 |
|
| 1354 |
self.assertRaises(ValueError, a.set_tag, "X6", 5.2, "g") |
| 1355 |
|
| 1356 |
def testTagsUpdatingFloat(self): |
| 1357 |
a = self.build_read() |
| 1358 |
a.tags = [("NM", 1), ("RG", "L1"), ("PG", "P1"), ("XT", "U")] |
| 1359 |
|
| 1360 |
self.assertEqual(a.tags, [("NM", 1), ("RG", "L1"), ("PG", "P1"), ("XT", "U")]) |
| 1361 |
a.tags += [("XC", 5.0)] |
| 1362 |
self.assertEqual( |
| 1363 |
a.tags, [("NM", 1), ("RG", "L1"), ("PG", "P1"), ("XT", "U"), ("XC", 5.0)] |
| 1364 |
) |
| 1365 |
|
| 1366 |
def testAddTags(self): |
| 1367 |
a = self.build_read() |
| 1368 |
a.tags = [("NM", 1), ("RG", "L1"), ("PG", "P1"), ("XT", "U")] |
| 1369 |
|
| 1370 |
self.assertEqual( |
| 1371 |
sorted(a.tags), sorted([("NM", 1), ("RG", "L1"), ("PG", "P1"), ("XT", "U")]) |
| 1372 |
) |
| 1373 |
|
| 1374 |
a.setTag("X1", "C") |
| 1375 |
self.assertEqual( |
| 1376 |
sorted(a.tags), |
| 1377 |
sorted([("X1", "C"), ("NM", 1), ("RG", "L1"), ("PG", "P1"), ("XT", "U"),]), |
| 1378 |
) |
| 1379 |
a.setTag("X2", 5) |
| 1380 |
self.assertEqual( |
| 1381 |
sorted(a.tags), |
| 1382 |
sorted( |
| 1383 |
[ |
| 1384 |
("X2", 5), |
| 1385 |
("X1", "C"), |
| 1386 |
("NM", 1), |
| 1387 |
("RG", "L1"), |
| 1388 |
("PG", "P1"), |
| 1389 |
("XT", "U"), |
| 1390 |
] |
| 1391 |
), |
| 1392 |
) |
| 1393 |
|
| 1394 |
a.setTag("X2", 10) |
| 1395 |
self.assertEqual( |
| 1396 |
sorted(a.tags), |
| 1397 |
sorted( |
| 1398 |
[ |
| 1399 |
("X2", 10), |
| 1400 |
("X1", "C"), |
| 1401 |
("NM", 1), |
| 1402 |
("RG", "L1"), |
| 1403 |
("PG", "P1"), |
| 1404 |
("XT", "U"), |
| 1405 |
] |
| 1406 |
), |
| 1407 |
) |
| 1408 |
|
| 1409 |
|
| 1410 |
a.setTag("X2", 5, replace=False) |
| 1411 |
self.assertEqual( |
| 1412 |
sorted(a.tags), |
| 1413 |
sorted( |
| 1414 |
[ |
| 1415 |
("X2", 10), |
| 1416 |
("X1", "C"), |
| 1417 |
("X2", 5), |
| 1418 |
("NM", 1), |
| 1419 |
("RG", "L1"), |
| 1420 |
("PG", "P1"), |
| 1421 |
("XT", "U"), |
| 1422 |
] |
| 1423 |
), |
| 1424 |
) |
| 1425 |
|
| 1426 |
def testTagParsing(self): |
| 1427 |
"""test for tag parsing |
| 1428 |
|
| 1429 |
see http://groups.google.com/group/pysam-user-group/browse_thread/thread/67ca204059ea465a |
| 1430 |
""" |
| 1431 |
samfile = pysam.AlignmentFile(os.path.join(BAM_DATADIR, "ex8.bam"), "rb") |
| 1432 |
|
| 1433 |
for entry in samfile: |
| 1434 |
before = entry.get_tags() |
| 1435 |
entry.set_tags(before) |
| 1436 |
after = entry.get_tags() |
| 1437 |
self.assertEqual(after, before) |
| 1438 |
|
| 1439 |
def testMDTagMatchOnly(self): |
| 1440 |
a = self.build_read() |
| 1441 |
|
| 1442 |
|
| 1443 |
a.cigarstring = "21M" |
| 1444 |
a.query_sequence = "A" * 21 |
| 1445 |
a.set_tag("MD", "5C0T0G05C0G0T5") |
| 1446 |
self.assertEqual("AAAAActgAAAAAcgtAAAAA", a.get_reference_sequence()) |
| 1447 |
|
| 1448 |
a.cigarstring = "21M" |
| 1449 |
a.query_sequence = "A" * 21 |
| 1450 |
a.set_tag("MD", "5CTG5CGT5") |
| 1451 |
self.assertEqual("AAAAActgAAAAAcgtAAAAA", a.get_reference_sequence()) |
| 1452 |
|
| 1453 |
a.cigarstring = "11M" |
| 1454 |
a.query_sequence = "A" * 11 |
| 1455 |
a.set_tag("MD", "CTG5CGT") |
| 1456 |
self.assertEqual("ctgAAAAAcgt", a.get_reference_sequence()) |
| 1457 |
|
| 1458 |
def testMDTagInsertions(self): |
| 1459 |
a = self.build_read() |
| 1460 |
|
| 1461 |
|
| 1462 |
a.cigarstring = "5M1I5M" |
| 1463 |
a.query_sequence = "A" * 5 + "C" + "A" * 5 |
| 1464 |
a.set_tag("MD", "10") |
| 1465 |
self.assertEqual(a.get_reference_sequence(), "A" * 10) |
| 1466 |
|
| 1467 |
a.cigarstring = "1I10M" |
| 1468 |
a.query_sequence = "C" * 1 + "A" * 10 |
| 1469 |
self.assertEqual(a.get_reference_sequence(), "A" * 10) |
| 1470 |
|
| 1471 |
a.cigarstring = "10M1I" |
| 1472 |
a.query_sequence = "A" * 10 + "C" * 1 |
| 1473 |
self.assertEqual(a.get_reference_sequence(), "A" * 10) |
| 1474 |
|
| 1475 |
def testMDTagDeletions(self): |
| 1476 |
a = self.build_read() |
| 1477 |
|
| 1478 |
a.cigarstring = "5M1D5M" |
| 1479 |
a.query_sequence = "A" * 10 |
| 1480 |
a.set_tag("MD", "5^C5") |
| 1481 |
self.assertEqual("A" * 5 + "C" + "A" * 5, a.get_reference_sequence()) |
| 1482 |
|
| 1483 |
a.cigarstring = "5M3D5M" |
| 1484 |
a.query_sequence = "A" * 10 |
| 1485 |
a.set_tag("MD", "5^CCC5") |
| 1486 |
self.assertEqual("A" * 5 + "C" * 3 + "A" * 5, a.get_reference_sequence()) |
| 1487 |
|
| 1488 |
def testMDTagRefSkipping(self): |
| 1489 |
a = self.build_read() |
| 1490 |
|
| 1491 |
a.cigarstring = "5M1N5M" |
| 1492 |
a.query_sequence = "A" * 10 |
| 1493 |
a.set_tag("MD", "10") |
| 1494 |
self.assertEqual("A" * 10, a.get_reference_sequence()) |
| 1495 |
|
| 1496 |
a.cigarstring = "5M3N5M" |
| 1497 |
a.query_sequence = "A" * 10 |
| 1498 |
a.set_tag("MD", "10") |
| 1499 |
self.assertEqual("A" * 10, a.get_reference_sequence()) |
| 1500 |
|
| 1501 |
def testMDTagSoftClipping(self): |
| 1502 |
a = self.build_read() |
| 1503 |
|
| 1504 |
|
| 1505 |
a.cigarstring = "5S5M1D5M5S" |
| 1506 |
a.query_sequence = "G" * 5 + "A" * 10 + "G" * 5 |
| 1507 |
a.set_tag("MD", "5^C5") |
| 1508 |
self.assertEqual("A" * 5 + "C" + "A" * 5, a.get_reference_sequence()) |
| 1509 |
|
| 1510 |
|
| 1511 |
a.cigarstring = "5S5M1D5M1I5M5S" |
| 1512 |
a.query_sequence = "G" * 5 + "A" * 16 + "G" * 5 |
| 1513 |
a.set_tag("MD", "2C2^T10") |
| 1514 |
self.assertEqual("AAcAATAAAAAAAAAA", a.get_reference_sequence()) |
| 1515 |
|
| 1516 |
def testMDTagComplex(self): |
| 1517 |
a = self.build_read() |
| 1518 |
|
| 1519 |
a.cigarstring = "5S5M1I2D5M5S" |
| 1520 |
a.query_sequence = "G" * 5 + "A" * 11 + "G" * 5 |
| 1521 |
a.set_tag("MD", "2C2^TC5") |
| 1522 |
self.assertEqual("AAcAATCAAAAA", a.get_reference_sequence()) |
| 1523 |
|
| 1524 |
a.cigarstring = "5S5M2D1I5M5S" |
| 1525 |
a.query_sequence = "G" * 5 + "A" * 11 + "G" * 5 |
| 1526 |
a.set_tag("MD", "2C2^TC5") |
| 1527 |
self.assertEqual("AAcAATCAAAAA", a.get_reference_sequence()) |
| 1528 |
|
| 1529 |
|
| 1530 |
|
| 1531 |
|
| 1532 |
a.cigarstring = "2M4I1M3D3M" |
| 1533 |
a.set_tag("MD", "3^GGG3") |
| 1534 |
a.query_sequence = "AACCCCAAAA" |
| 1535 |
self.assertEqual("AAAGGGAAA", a.get_reference_sequence()) |
| 1536 |
|
| 1537 |
a.cigarstring = "5M2D2I2M" |
| 1538 |
a.set_tag("MD", "4C^TT2") |
| 1539 |
a.query_sequence = "A" * 9 |
| 1540 |
self.assertEqual("AAAAcTTAA", a.get_reference_sequence()) |
| 1541 |
|
| 1542 |
def testArrayTags(self): |
| 1543 |
|
| 1544 |
r = self.build_read() |
| 1545 |
|
| 1546 |
def c(r, l): |
| 1547 |
r.tags = [("ZM", l)] |
| 1548 |
self.assertEqual(list(r.opt("ZM")), list(l)) |
| 1549 |
|
| 1550 |
|
| 1551 |
c(r, (-1, 1)) |
| 1552 |
c(r, (-1, 100)) |
| 1553 |
c(r, (-1, 200)) |
| 1554 |
c(r, (-1, 1000)) |
| 1555 |
c(r, (-1, 30000)) |
| 1556 |
c(r, (-1, 50000)) |
| 1557 |
c(r, (1, -1)) |
| 1558 |
c(r, (1, -100)) |
| 1559 |
c(r, (1, -200)) |
| 1560 |
c(r, (1, -1000)) |
| 1561 |
c(r, (1, -30000)) |
| 1562 |
c(r, (1, -50000)) |
| 1563 |
|
| 1564 |
|
| 1565 |
c(r, (1, 100)) |
| 1566 |
c(r, (1, 1000)) |
| 1567 |
c(r, (1, 10000)) |
| 1568 |
c(r, (1, 100000)) |
| 1569 |
|
| 1570 |
|
| 1571 |
c(r, (1.0, 100.0)) |
| 1572 |
|
| 1573 |
def testLongTags(self): |
| 1574 |
"""see issue 115""" |
| 1575 |
|
| 1576 |
r = self.build_read() |
| 1577 |
rg = "HS2000-899_199.L3" |
| 1578 |
tags = [ |
| 1579 |
("XC", 85), |
| 1580 |
("XT", "M"), |
| 1581 |
("NM", 5), |
| 1582 |
("SM", 29), |
| 1583 |
("AM", 29), |
| 1584 |
("XM", 1), |
| 1585 |
("XO", 1), |
| 1586 |
("XG", 4), |
| 1587 |
("MD", "37^ACCC29T18"), |
| 1588 |
( |
| 1589 |
"XA", |
| 1590 |
"5,+11707,36M1I48M,2;21,-48119779,46M1I38M,2;hs37d5,-10060835,40M1D45M,3;5,+11508,36M1I48M,3;hs37d5,+6743812,36M1I48M,3;19,-59118894,46M1I38M,3;4,-191044002,6M1I78M,3;", |
| 1591 |
), |
| 1592 |
] |
| 1593 |
|
| 1594 |
r.tags = tags |
| 1595 |
r.tags += [("RG", rg)] * 100 |
| 1596 |
tags += [("RG", rg)] * 100 |
| 1597 |
|
| 1598 |
self.assertEqual(tags, r.tags) |
| 1599 |
|
| 1600 |
def testNegativeIntegers(self): |
| 1601 |
x = -2 |
| 1602 |
aligned_read = self.build_read() |
| 1603 |
aligned_read.tags = [("XD", int(x))] |
| 1604 |
self.assertEqual(aligned_read.opt("XD"), x) |
| 1605 |
|
| 1606 |
|
| 1607 |
def testNegativeIntegersWrittenToFile(self): |
| 1608 |
r = self.build_read() |
| 1609 |
x = -2 |
| 1610 |
r.tags = [("XD", x)] |
| 1611 |
with get_temp_context("negative_integers.bam") as fn: |
| 1612 |
with pysam.AlignmentFile( |
| 1613 |
fn, "wb", referencenames=("chr1",), referencelengths=(1000,) |
| 1614 |
) as outf: |
| 1615 |
outf.write(r) |
| 1616 |
with pysam.AlignmentFile(fn) as inf: |
| 1617 |
r = next(inf) |
| 1618 |
self.assertEqual(r.tags, [("XD", x)]) |
| 1619 |
|
| 1620 |
|
| 1621 |
class TestCopy(ReadTest): |
| 1622 |
def testCopy(self): |
| 1623 |
a = self.build_read() |
| 1624 |
b = copy.copy(a) |
| 1625 |
|
| 1626 |
self.assertEqual(a, b) |
| 1627 |
|
| 1628 |
|
| 1629 |
a.query_name = "ReadA" |
| 1630 |
b.query_name = "ReadB" |
| 1631 |
self.assertEqual(a.query_name, "ReadA") |
| 1632 |
self.assertEqual(b.query_name, "ReadB") |
| 1633 |
|
| 1634 |
def testDeepCopy(self): |
| 1635 |
a = self.build_read() |
| 1636 |
b = copy.deepcopy(a) |
| 1637 |
|
| 1638 |
self.assertEqual(a, b) |
| 1639 |
|
| 1640 |
|
| 1641 |
a.query_name = "ReadA" |
| 1642 |
b.query_name = "ReadB" |
| 1643 |
self.assertEqual(a.query_name, "ReadA") |
| 1644 |
self.assertEqual(b.query_name, "ReadB") |
| 1645 |
|
| 1646 |
|
| 1647 |
class TestSetTagGetTag(ReadTest): |
| 1648 |
def check_tag(self, tag, value, value_type, alt_value_type=None): |
| 1649 |
a = self.build_read() |
| 1650 |
a.set_tag(tag, value, value_type=value_type) |
| 1651 |
v, t = a.get_tag(tag, with_value_type=True) |
| 1652 |
self.assertEqual(v, value) |
| 1653 |
|
| 1654 |
if alt_value_type: |
| 1655 |
self.assertEqual(t, alt_value_type) |
| 1656 |
else: |
| 1657 |
self.assertEqual(t, value_type) |
| 1658 |
|
| 1659 |
def test_set_tag_with_A(self): |
| 1660 |
self.check_tag("TT", "x", value_type="A") |
| 1661 |
|
| 1662 |
def test_set_tag_with_a(self): |
| 1663 |
self.check_tag("TT", "x", value_type="a", alt_value_type="A") |
| 1664 |
|
| 1665 |
def test_set_tag_with_C(self): |
| 1666 |
self.check_tag("TT", 12, value_type="C") |
| 1667 |
|
| 1668 |
def test_set_tag_with_c(self): |
| 1669 |
self.check_tag("TT", 12, value_type="c") |
| 1670 |
|
| 1671 |
def test_set_tag_with_S(self): |
| 1672 |
self.check_tag("TT", 12, value_type="S") |
| 1673 |
|
| 1674 |
def test_set_tag_with_s(self): |
| 1675 |
self.check_tag("TT", 12, value_type="s") |
| 1676 |
|
| 1677 |
def test_set_tag_with_I(self): |
| 1678 |
self.check_tag("TT", 12, value_type="I") |
| 1679 |
|
| 1680 |
def test_set_tag_with_i(self): |
| 1681 |
self.check_tag("TT", 12, value_type="i") |
| 1682 |
|
| 1683 |
def test_set_tag_with_f(self): |
| 1684 |
self.check_tag("TT", 2.5, value_type="f") |
| 1685 |
|
| 1686 |
def test_set_tag_with_d(self): |
| 1687 |
self.check_tag("TT", 2.5, value_type="d") |
| 1688 |
|
| 1689 |
def test_set_tag_with_H(self): |
| 1690 |
self.check_tag("TT", "AE12", value_type="H") |
| 1691 |
|
| 1692 |
def test_set_tag_with_automated_type_detection(self): |
| 1693 |
self.check_tag("TT", -(1 << 7), value_type=None, alt_value_type="c") |
| 1694 |
self.check_tag("TT", -(1 << 7) - 1, value_type=None, alt_value_type="s") |
| 1695 |
self.check_tag("TT", -(1 << 15), value_type=None, alt_value_type="s") |
| 1696 |
self.check_tag("TT", -(1 << 15) - 1, value_type=None, alt_value_type="i") |
| 1697 |
self.check_tag("TT", -(1 << 31), value_type=None, alt_value_type="i") |
| 1698 |
self.assertRaises( |
| 1699 |
ValueError, |
| 1700 |
self.check_tag, |
| 1701 |
"TT", |
| 1702 |
-(1 << 31) - 1, |
| 1703 |
value_type=None, |
| 1704 |
alt_value_type="i", |
| 1705 |
) |
| 1706 |
|
| 1707 |
self.check_tag("TT", (1 << 8) - 1, value_type=None, alt_value_type="C") |
| 1708 |
self.check_tag("TT", (1 << 8), value_type=None, alt_value_type="S") |
| 1709 |
self.check_tag("TT", (1 << 16) - 1, value_type=None, alt_value_type="S") |
| 1710 |
self.check_tag("TT", (1 << 16), value_type=None, alt_value_type="I") |
| 1711 |
self.check_tag("TT", (1 << 32) - 1, value_type=None, alt_value_type="I") |
| 1712 |
self.assertRaises( |
| 1713 |
ValueError, |
| 1714 |
self.check_tag, |
| 1715 |
"TT", |
| 1716 |
(1 << 32), |
| 1717 |
value_type=None, |
| 1718 |
alt_value_type="I", |
| 1719 |
) |
| 1720 |
|
| 1721 |
def test_set_tag_invalid_value_type(self): |
| 1722 |
with self.assertRaises(ValueError): |
| 1723 |
self.check_tag("TT", "abc", value_type="#") |
| 1724 |
|
| 1725 |
def test_set_array_tag_invalid_value_type(self): |
| 1726 |
with self.assertRaises(ValueError): |
| 1727 |
self.check_tag("TT", array.array('I', range(4)), value_type='#') |
| 1728 |
|
| 1729 |
def test_set_array_tag_invalid_typecode(self): |
| 1730 |
with self.assertRaises(ValueError): |
| 1731 |
self.check_tag("TT", array.array('L', range(4)), value_type=None) |
| 1732 |
|
| 1733 |
|
| 1734 |
class TestSetTagsGetTag(TestSetTagGetTag): |
| 1735 |
def check_tag(self, tag, value, value_type, alt_value_type=None): |
| 1736 |
a = self.build_read() |
| 1737 |
a.set_tags([(tag, value, value_type)]) |
| 1738 |
v, t = a.get_tag(tag, with_value_type=True) |
| 1739 |
if alt_value_type: |
| 1740 |
self.assertEqual(t, alt_value_type) |
| 1741 |
else: |
| 1742 |
self.assertEqual(t, value_type) |
| 1743 |
self.assertEqual(v, value) |
| 1744 |
|
| 1745 |
|
| 1746 |
class TestEnums(unittest.TestCase): |
| 1747 |
def test_cigar_enums_are_defined(self): |
| 1748 |
self.assertEqual(pysam.CMATCH, 0) |
| 1749 |
self.assertEqual(pysam.CINS, 1) |
| 1750 |
self.assertEqual(pysam.CDEL, 2) |
| 1751 |
self.assertEqual(pysam.CREF_SKIP, 3) |
| 1752 |
self.assertEqual(pysam.CSOFT_CLIP, 4) |
| 1753 |
self.assertEqual(pysam.CHARD_CLIP, 5) |
| 1754 |
self.assertEqual(pysam.CPAD, 6) |
| 1755 |
self.assertEqual(pysam.CEQUAL, 7) |
| 1756 |
self.assertEqual(pysam.CDIFF, 8) |
| 1757 |
self.assertEqual(pysam.CBACK, 9) |
| 1758 |
|
| 1759 |
def test_sam_flags_are_defined(self): |
| 1760 |
self.assertEqual(pysam.FPAIRED, 1) |
| 1761 |
self.assertEqual(pysam.FPROPER_PAIR, 2) |
| 1762 |
self.assertEqual(pysam.FUNMAP, 4) |
| 1763 |
self.assertEqual(pysam.FMUNMAP, 8) |
| 1764 |
self.assertEqual(pysam.FREVERSE, 16) |
| 1765 |
self.assertEqual(pysam.FMREVERSE, 32) |
| 1766 |
self.assertEqual(pysam.FREAD1, 64) |
| 1767 |
self.assertEqual(pysam.FREAD2, 128) |
| 1768 |
self.assertEqual(pysam.FSECONDARY, 256) |
| 1769 |
self.assertEqual(pysam.FQCFAIL, 512) |
| 1770 |
self.assertEqual(pysam.FDUP, 1024) |
| 1771 |
self.assertEqual(pysam.FSUPPLEMENTARY, 2048) |
| 1772 |
|
| 1773 |
|
| 1774 |
class TestBuildingReadsWithoutHeader(unittest.TestCase): |
| 1775 |
def build_read(self): |
| 1776 |
"""build an example read, but without header information.""" |
| 1777 |
|
| 1778 |
a = pysam.AlignedSegment() |
| 1779 |
a.query_name = "read_12345" |
| 1780 |
a.query_sequence = "ATGC" * 10 |
| 1781 |
a.flag = 0 |
| 1782 |
a.reference_id = -1 |
| 1783 |
a.reference_start = 20 |
| 1784 |
a.mapping_quality = 20 |
| 1785 |
a.cigartuples = ((0, 10), (2, 1), (0, 9), (1, 1), (0, 20)) |
| 1786 |
a.next_reference_id = 0 |
| 1787 |
a.next_reference_start = 200 |
| 1788 |
a.template_length = 167 |
| 1789 |
a.query_qualities = pysam.qualitystring_to_array("1234") * 10 |
| 1790 |
|
| 1791 |
return a |
| 1792 |
|
| 1793 |
def test_read_can_be_constructed_without_header(self): |
| 1794 |
read = self.build_read() |
| 1795 |
self.assertEqual(read.query_name, "read_12345") |
| 1796 |
|
| 1797 |
def test_reference_id_can_be_set(self): |
| 1798 |
read = self.build_read() |
| 1799 |
read.reference_id = 2 |
| 1800 |
self.assertEqual(read.reference_id, 2) |
| 1801 |
|
| 1802 |
def test_reference_name_is_not_available(self): |
| 1803 |
read = self.build_read() |
| 1804 |
self.assertRaises(ValueError, setattr, read, "reference_name", "chr2") |
| 1805 |
|
| 1806 |
def test_read_can_be_written_to_file(self): |
| 1807 |
tmpfilename = get_temp_filename(".bam") |
| 1808 |
with pysam.AlignmentFile( |
| 1809 |
tmpfilename, |
| 1810 |
"wb", |
| 1811 |
reference_names=["chr1", "chr2", "chr3"], |
| 1812 |
reference_lengths=[1000, 2000, 3000], |
| 1813 |
) as outf: |
| 1814 |
read = self.build_read() |
| 1815 |
read.reference_id = 2 |
| 1816 |
outf.write(read) |
| 1817 |
|
| 1818 |
stdout = pysam.samtools.view(tmpfilename) |
| 1819 |
chromosome = stdout.split("\t")[2] |
| 1820 |
self.assertEqual(chromosome, "chr3") |
| 1821 |
os.unlink(tmpfilename) |
| 1822 |
|
| 1823 |
|
| 1824 |
class TestForwardStrandValues(ReadTest): |
| 1825 |
def test_sequence_is_complemented(self): |
| 1826 |
a = self.build_read() |
| 1827 |
a.is_reverse = False |
| 1828 |
fwd_seq = a.query_sequence |
| 1829 |
|
| 1830 |
rev_seq = fwd_seq.translate(str.maketrans("ACGTacgtNnXx", "TGCAtgcaNnXx"))[::-1] |
| 1831 |
self.assertEqual(fwd_seq, a.get_forward_sequence()) |
| 1832 |
a.is_reverse = True |
| 1833 |
self.assertEqual(fwd_seq, a.query_sequence) |
| 1834 |
self.assertEqual(rev_seq, a.get_forward_sequence()) |
| 1835 |
|
| 1836 |
def test_qualities_are_complemented(self): |
| 1837 |
a = self.build_read() |
| 1838 |
a.is_reverse = False |
| 1839 |
fwd_qual = a.query_qualities |
| 1840 |
rev_qual = fwd_qual[::-1] |
| 1841 |
self.assertEqual(fwd_qual, a.get_forward_qualities()) |
| 1842 |
a.is_reverse = True |
| 1843 |
self.assertEqual(fwd_qual, a.query_qualities) |
| 1844 |
self.assertEqual(rev_qual, a.get_forward_qualities()) |
| 1845 |
|
| 1846 |
|
| 1847 |
class TestExportImport(ReadTest): |
| 1848 |
def test_string_export(self): |
| 1849 |
a = self.build_read() |
| 1850 |
self.assertEqual( |
| 1851 |
a.to_string(), |
| 1852 |
"read_12345\t0\tchr1\t21\t20\t10M1D9M1I20M\t=\t201\t167\t" |
| 1853 |
"ATGCATGCATGCATGCATGCATGCATGCATGCATGCATGC\t1234123412341234123412341234123412341234", |
| 1854 |
) |
| 1855 |
|
| 1856 |
def test_string_export_import_without_tags(self): |
| 1857 |
a = self.build_read() |
| 1858 |
a.tags = [] |
| 1859 |
b = pysam.AlignedSegment.fromstring(a.to_string(), a.header) |
| 1860 |
self.assertEqual(a, b) |
| 1861 |
|
| 1862 |
def test_string_export_import_with_tags(self): |
| 1863 |
a = self.build_read() |
| 1864 |
a.tags = [("XD", 12), ("RF", "abc")] |
| 1865 |
b = pysam.AlignedSegment.fromstring(a.to_string(), a.header) |
| 1866 |
self.assertEqual(a, b) |
| 1867 |
|
| 1868 |
def test_to_string_without_alignment_file(self): |
| 1869 |
with open(os.path.join(BAM_DATADIR, "ex2.sam")) as samf: |
| 1870 |
reference = [x[:-1] for x in samf if not x.startswith("@")] |
| 1871 |
|
| 1872 |
with pysam.AlignmentFile(os.path.join(BAM_DATADIR, "ex2.bam"), "r") as pysamf: |
| 1873 |
for s, p in zip(reference, pysamf): |
| 1874 |
self.assertEqual(s, p.to_string()) |
| 1875 |
|
| 1876 |
def test_dict_export(self): |
| 1877 |
a = self.build_read() |
| 1878 |
a.tags = [("XD", 12), ("RF", "abc")] |
| 1879 |
|
| 1880 |
self.assertEqual( |
| 1881 |
a.to_dict(), |
| 1882 |
json.loads( |
| 1883 |
'{"name": "read_12345", "flag": "0", "ref_name": "chr1", "ref_pos": "21", ' |
| 1884 |
'"map_quality": "20", "cigar": "10M1D9M1I20M", "next_ref_name": "=", ' |
| 1885 |
'"next_ref_pos": "201", "length": "167", ' |
| 1886 |
'"seq": "ATGCATGCATGCATGCATGCATGCATGCATGCATGCATGC", ' |
| 1887 |
'"qual": "1234123412341234123412341234123412341234", "tags": ["XD:i:12", "RF:Z:abc"]}' |
| 1888 |
), |
| 1889 |
) |
| 1890 |
|
| 1891 |
def test_string_export_import_without_tags(self): |
| 1892 |
a = self.build_read() |
| 1893 |
a.tags = [] |
| 1894 |
b = pysam.AlignedSegment.from_dict(a.to_dict(), a.header) |
| 1895 |
self.assertEqual(a, b) |
| 1896 |
|
| 1897 |
def test_string_export_import_with_tags(self): |
| 1898 |
a = self.build_read() |
| 1899 |
a.tags = [("XD", 12), ("RF", "abc")] |
| 1900 |
b = pysam.AlignedSegment.from_dict(a.to_dict(), a.header) |
| 1901 |
self.assertEqual(a, b) |
| 1902 |
|
| 1903 |
|
| 1904 |
if __name__ == "__main__": |
| 1905 |
unittest.main() |