| 1 |
|
| 2 |
'''unit testing code for pysam. |
| 3 |
|
| 4 |
Execute in the :file:`tests` directory as it requires the Makefile |
| 5 |
and data files located there. |
| 6 |
''' |
| 7 |
|
| 8 |
import unittest |
| 9 |
import os |
| 10 |
import re |
| 11 |
import copy |
| 12 |
from collections import OrderedDict as odict |
| 13 |
import pysam |
| 14 |
import pysam.samtools |
| 15 |
from TestUtils import get_temp_filename, make_data_files, BAM_DATADIR |
| 16 |
|
| 17 |
|
| 18 |
def setUpModule(): |
| 19 |
make_data_files(BAM_DATADIR) |
| 20 |
|
| 21 |
|
| 22 |
class TestHeaderConstruction(unittest.TestCase): |
| 23 |
"""testing header construction.""" |
| 24 |
|
| 25 |
header_dict = odict( |
| 26 |
[('SQ', [odict([('LN', 1575), ('SN', 'chr1'), ('AH', 'chr1:5000000-5010000')]), |
| 27 |
odict([('LN', 1584), ('SN', 'chr2'), ('AH', '*'), ('TP', 'linear')])]), |
| 28 |
('RG', [odict([('LB', 'SC_1'), ('ID', 'L1'), ('SM', 'NA12891'), |
| 29 |
('PU', 'SC_1_10'), ("CN", "name:with:colon")]), |
| 30 |
odict([('LB', 'SC_2'), ('ID', 'L2'), ('SM', 'NA12891'), |
| 31 |
('PU', 'SC_2_12'), ("CN", "name:with:colon")])]), |
| 32 |
('PG', [odict([('ID', 'P1'), ('VN', '1.0')]), |
| 33 |
odict([('ID', 'P2'), ('VN', '1.1')])]), |
| 34 |
('HD', odict([('VN', '1.0')])), |
| 35 |
('CO', ['this is a comment', 'this is another comment']), |
| 36 |
]) |
| 37 |
|
| 38 |
header_text = ("@HD\tVN:1.0\n" |
| 39 |
"@SQ\tSN:chr1\tLN:1575\tAH:chr1:5000000-5010000\n" |
| 40 |
"@SQ\tSN:chr2\tLN:1584\tAH:*\tTP:linear\n" |
| 41 |
"@RG\tID:L1\tPU:SC_1_10\tLB:SC_1\tSM:NA12891\tCN:name:with:colon\n" |
| 42 |
"@RG\tID:L2\tPU:SC_2_12\tLB:SC_2\tSM:NA12891\tCN:name:with:colon\n" |
| 43 |
"@PG\tID:P1\tVN:1.0\n" |
| 44 |
"@PG\tID:P2\tVN:1.1\n" |
| 45 |
"@CO\tthis is a comment\n" |
| 46 |
"@CO\tthis is another comment\n") |
| 47 |
|
| 48 |
|
| 49 |
header_from_references = odict( |
| 50 |
[('SQ', [odict([('LN', 1575), ('SN', 'chr1')]), |
| 51 |
odict([('LN', 1584), ('SN', 'chr2')])]), |
| 52 |
('RG', [odict([('LB', 'SC_1'), ('ID', 'L1'), ('SM', 'NA12891'), |
| 53 |
('PU', 'SC_1_10'), ("CN", "name:with:colon")]), |
| 54 |
odict([('LB', 'SC_2'), ('ID', 'L2'), ('SM', 'NA12891'), |
| 55 |
('PU', 'SC_2_12'), ("CN", "name:with:colon")])]), |
| 56 |
('PG', [odict([('ID', 'P1'), ('VN', '1.0')]), |
| 57 |
odict([('ID', 'P2'), ('VN', '1.1')])]), |
| 58 |
('HD', odict([('VN', '1.0')])), |
| 59 |
('CO', ['this is a comment', 'this is another comment']), |
| 60 |
]) |
| 61 |
|
| 62 |
header_without_text = odict( |
| 63 |
[('SQ', [odict([('LN', 1575), ('SN', 'chr1')]), |
| 64 |
odict([('LN', 1584), ('SN', 'chr2')])]), |
| 65 |
]) |
| 66 |
|
| 67 |
def compare_headers(self, test_header, ref_header=None): |
| 68 |
'''compare two headers a and b.''' |
| 69 |
test_header_dict = test_header.as_dict() |
| 70 |
if ref_header is None: |
| 71 |
ref_header = self.header_dict |
| 72 |
|
| 73 |
for ak, av in test_header_dict.items(): |
| 74 |
self.assertTrue(ak in self.header_dict, "key '%s' not in '%s' " % (ak, ref_header)) |
| 75 |
self.assertEqual(av, ref_header[ak]) |
| 76 |
for ak, av in ref_header.items(): |
| 77 |
self.assertTrue(ak in test_header_dict, "key '%s' not in '%s' " % (ak, test_header_dict)) |
| 78 |
self.assertEqual(av, test_header_dict[ak]) |
| 79 |
|
| 80 |
def check_name_mapping(self, test_header): |
| 81 |
for x, y in enumerate(("chr1", "chr2")): |
| 82 |
tid = test_header.get_tid(y) |
| 83 |
ref = test_header.get_reference_name(x) |
| 84 |
self.assertEqual(tid, x) |
| 85 |
self.assertEqual(ref, y) |
| 86 |
|
| 87 |
self.assertEqual(test_header.get_tid("chr?"), -1) |
| 88 |
self.assertRaises(ValueError, test_header.get_reference_name, 2) |
| 89 |
|
| 90 |
def test_header_constructed_from_dict(self): |
| 91 |
header = pysam.AlignmentHeader.from_dict(self.header_dict) |
| 92 |
self.compare_headers(header) |
| 93 |
self.check_name_mapping(header) |
| 94 |
|
| 95 |
def test_header_constructed_from_text(self): |
| 96 |
header = pysam.AlignmentHeader.from_text(self.header_text) |
| 97 |
self.compare_headers(header) |
| 98 |
self.check_name_mapping(header) |
| 99 |
|
| 100 |
def test_header_constructed_from_header(self): |
| 101 |
header = pysam.AlignmentHeader.from_text(self.header_text) |
| 102 |
self.compare_headers(header.copy()) |
| 103 |
self.check_name_mapping(header) |
| 104 |
|
| 105 |
def test_header_constructed_from_references(self): |
| 106 |
text = re.sub("@SQ[^\n]+\n", "", self.header_text) |
| 107 |
assert "@SQ" not in text |
| 108 |
header = pysam.AlignmentHeader.from_references( |
| 109 |
reference_names=["chr1", "chr2"], |
| 110 |
reference_lengths=[1575, 1584], |
| 111 |
text=text) |
| 112 |
self.compare_headers(header, self.header_from_references) |
| 113 |
self.check_name_mapping(header) |
| 114 |
|
| 115 |
def test_header_constructed_from_references_without_text(self): |
| 116 |
header = pysam.AlignmentHeader.from_references( |
| 117 |
reference_names=["chr1", "chr2"], |
| 118 |
reference_lengths=[1575, 1584]) |
| 119 |
self.compare_headers(header, self.header_without_text) |
| 120 |
self.check_name_mapping(header) |
| 121 |
|
| 122 |
|
| 123 |
class TestHeaderSAM(unittest.TestCase): |
| 124 |
"""testing header manipulation""" |
| 125 |
|
| 126 |
header = {'SQ': [{'LN': 1575, 'SN': 'chr1', 'AH': 'chr1:5000000-5010000'}, |
| 127 |
{'LN': 1584, 'SN': 'chr2', 'AH': '*'}], |
| 128 |
'RG': [{'LB': 'SC_1', 'ID': 'L1', 'SM': 'NA12891', |
| 129 |
'PU': 'SC_1_10', "CN": "name:with:colon"}, |
| 130 |
{'LB': 'SC_2', 'ID': 'L2', 'SM': 'NA12891', |
| 131 |
'PU': 'SC_2_12', "CN": "name:with:colon"}], |
| 132 |
'PG': [{'ID': 'P1', 'VN': '1.0'}, {'ID': 'P2', 'VN': '1.1'}], |
| 133 |
'HD': {'VN': '1.0'}, |
| 134 |
'CO': ['this is a comment', 'this is another comment'], |
| 135 |
} |
| 136 |
|
| 137 |
def compare_headers(self, a, b): |
| 138 |
'''compare two headers a and b.''' |
| 139 |
for ak, av in a.items(): |
| 140 |
self.assertTrue(ak in b, "key '%s' not in '%s' " % (ak, b)) |
| 141 |
self.assertEqual(av, b[ak]) |
| 142 |
|
| 143 |
def setUp(self): |
| 144 |
self.samfile = pysam.AlignmentFile( |
| 145 |
os.path.join(BAM_DATADIR, "ex3.sam"), |
| 146 |
"r") |
| 147 |
|
| 148 |
def test_header_content_is_as_expected(self): |
| 149 |
self.compare_headers(self.header, self.samfile.header.to_dict()) |
| 150 |
self.compare_headers(self.samfile.header.to_dict(), self.header) |
| 151 |
|
| 152 |
def test_text_access_works(self): |
| 153 |
self.assertEqual(self.samfile.text, self.samfile.header.__str__()) |
| 154 |
|
| 155 |
def test_name_mapping(self): |
| 156 |
for x, y in enumerate(("chr1", "chr2")): |
| 157 |
tid = self.samfile.gettid(y) |
| 158 |
ref = self.samfile.getrname(x) |
| 159 |
self.assertEqual(tid, x) |
| 160 |
self.assertEqual(ref, y) |
| 161 |
|
| 162 |
self.assertEqual(self.samfile.gettid("chr?"), -1) |
| 163 |
self.assertRaises(ValueError, self.samfile.getrname, 2) |
| 164 |
|
| 165 |
def test_dictionary_access_works(self): |
| 166 |
for key in self.header.keys(): |
| 167 |
self.compare_headers({key: self.header[key]}, |
| 168 |
{key: self.samfile.header[key]}) |
| 169 |
|
| 170 |
def test_dictionary_setting_raises_error(self): |
| 171 |
self.assertRaises(TypeError, |
| 172 |
self.samfile.header.__setitem__, |
| 173 |
"CO", |
| 174 |
["This is a final comment"]) |
| 175 |
|
| 176 |
def test_dictionary_len_works(self): |
| 177 |
self.assertEqual(len(self.header), len(self.samfile.header)) |
| 178 |
|
| 179 |
def test_dictionary_keys_works(self): |
| 180 |
|
| 181 |
self.assertEqual(sorted(self.header.keys()), |
| 182 |
sorted(self.samfile.header.keys())) |
| 183 |
|
| 184 |
def test_dictionary_values_works(self): |
| 185 |
self.assertEqual(len(self.header.values()), len(self.samfile.header.values())) |
| 186 |
|
| 187 |
def test_dictionary_get_works(self): |
| 188 |
self.assertEqual(self.header.get("HD"), {'VN': '1.0'}) |
| 189 |
self.assertEqual(self.header.get("UK", "xyz"), "xyz") |
| 190 |
self.assertEqual(self.header.get("UK"), None) |
| 191 |
|
| 192 |
def test_dictionary_contains_works(self): |
| 193 |
self.assertTrue("HD" in self.header) |
| 194 |
self.assertFalse("UK" in self.header) |
| 195 |
|
| 196 |
def tearDown(self): |
| 197 |
self.samfile.close() |
| 198 |
|
| 199 |
|
| 200 |
class TestHeaderBAM(TestHeaderSAM): |
| 201 |
|
| 202 |
def setUp(self): |
| 203 |
self.samfile = pysam.AlignmentFile( |
| 204 |
os.path.join(BAM_DATADIR, "ex3.bam"), |
| 205 |
"rb") |
| 206 |
|
| 207 |
|
| 208 |
class TestHeaderCRAM(TestHeaderSAM): |
| 209 |
|
| 210 |
def setUp(self): |
| 211 |
self.samfile = pysam.AlignmentFile( |
| 212 |
os.path.join(BAM_DATADIR, "ex3.cram"), |
| 213 |
"rc") |
| 214 |
|
| 215 |
def compare_headers(self, a, b): |
| 216 |
'''compare two headers a and b.''' |
| 217 |
def _strip(dd): |
| 218 |
for x in dd: |
| 219 |
for y in ("M5", "UR"): |
| 220 |
if y in x: |
| 221 |
del x[y] |
| 222 |
for ak, av in a.items(): |
| 223 |
_strip(av) |
| 224 |
self.assertTrue(ak in b, "key '%s' not in '%s' " % (ak, b)) |
| 225 |
_strip(b[ak]) |
| 226 |
|
| 227 |
self.assertEqual(av, b[ak]) |
| 228 |
|
| 229 |
|
| 230 |
class TestHeaderFromRefs(unittest.TestCase): |
| 231 |
'''see issue 144 |
| 232 |
|
| 233 |
reference names need to be converted to string for python 3 |
| 234 |
''' |
| 235 |
|
| 236 |
|
| 237 |
|
| 238 |
|
| 239 |
|
| 240 |
|
| 241 |
|
| 242 |
|
| 243 |
|
| 244 |
|
| 245 |
|
| 246 |
|
| 247 |
|
| 248 |
|
| 249 |
|
| 250 |
class TestHeaderWriteRead(unittest.TestCase): |
| 251 |
header = {'SQ': [{'LN': 1575, 'SN': 'chr1'}, |
| 252 |
{'LN': 1584, 'SN': 'chr2'}], |
| 253 |
'RG': [{'LB': 'SC_1', 'ID': 'L1', 'SM': 'NA12891', |
| 254 |
'PU': 'SC_1_10', "CN": "name:with:colon"}, |
| 255 |
{'LB': 'SC_2', 'ID': 'L2', 'SM': 'NA12891', |
| 256 |
'PU': 'SC_2_12', "CN": "name:with:colon"}], |
| 257 |
'PG': [{'ID': 'P1', 'VN': '1.0', 'CL': 'tool'}, |
| 258 |
{'ID': 'P2', 'VN': '1.1', 'CL': 'tool with in option -R a\tb', |
| 259 |
'PP': 'P1'}], |
| 260 |
'HD': {'VN': '1.0'}, |
| 261 |
'CO': ['this is a comment', 'this is another comment'], |
| 262 |
} |
| 263 |
|
| 264 |
def compare_headers(self, a, header_b): |
| 265 |
'''compare two headers a and b. |
| 266 |
|
| 267 |
Ignore M5 and UR field as they are set application specific. |
| 268 |
''' |
| 269 |
b = header_b.to_dict() |
| 270 |
for ak, av in a.items(): |
| 271 |
self.assertTrue(ak in b, "key '%s' not in '%s' " % (ak, b)) |
| 272 |
self.assertEqual( |
| 273 |
len(av), len(b[ak]), |
| 274 |
"unequal number of entries for key {}: {} vs {}" |
| 275 |
.format(ak, av, b[ak])) |
| 276 |
|
| 277 |
for row_a, row_b in zip(av, b[ak]): |
| 278 |
if isinstance(row_b, dict): |
| 279 |
for x in ["M5", "UR"]: |
| 280 |
try: |
| 281 |
del row_b[x] |
| 282 |
except KeyError: |
| 283 |
pass |
| 284 |
self.assertEqual(row_a, row_b) |
| 285 |
|
| 286 |
def check_read_write(self, flag_write, header): |
| 287 |
|
| 288 |
fn = get_temp_filename() |
| 289 |
with pysam.AlignmentFile( |
| 290 |
fn, |
| 291 |
flag_write, |
| 292 |
header=header, |
| 293 |
reference_filename=os.path.join(BAM_DATADIR, "ex1.fa")) as outf: |
| 294 |
a = pysam.AlignedSegment() |
| 295 |
a.query_name = "abc" |
| 296 |
a.flag = pysam.FUNMAP |
| 297 |
outf.write(a) |
| 298 |
|
| 299 |
with pysam.AlignmentFile(fn) as inf: |
| 300 |
read_header = inf.header |
| 301 |
|
| 302 |
os.unlink(fn) |
| 303 |
self.compare_headers(header, read_header) |
| 304 |
expected_lengths = dict([(x["SN"], x["LN"]) for x in header["SQ"]]) |
| 305 |
self.assertEqual(expected_lengths, |
| 306 |
dict(zip(read_header.references, |
| 307 |
read_header.lengths))) |
| 308 |
|
| 309 |
def test_SAM(self): |
| 310 |
self.check_read_write("wh", self.header) |
| 311 |
|
| 312 |
def test_BAM(self): |
| 313 |
self.check_read_write("wb", self.header) |
| 314 |
|
| 315 |
def test_CRAM(self): |
| 316 |
header = copy.copy(self.header) |
| 317 |
if "PG" in header: |
| 318 |
|
| 319 |
header['PG'][1]['CL'] = re.sub(r"\t", r"\\\\t", header['PG'][1]['CL']) |
| 320 |
self.check_read_write("wc", header) |
| 321 |
|
| 322 |
|
| 323 |
class TestHeaderLargeContigs(TestHeaderWriteRead): |
| 324 |
"""see issue 741""" |
| 325 |
|
| 326 |
header = {'SQ': [{'LN': 2147483647, 'SN': 'chr1'}, |
| 327 |
{'LN': 1584, 'SN': 'chr2'}], |
| 328 |
'HD': {'VN': '1.0'}} |