1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676
|
# -*- coding: utf-8 -*-
import os
import glob
import sys
import unittest
import pysam
import shutil
import gzip
import subprocess
try:
from pathlib import Path
except ImportError:
Path = None
from TestUtils import get_temp_filename, check_lines_equal, load_and_convert, CBCF_DATADIR, get_temp_context
def read_header(filename):
data = []
if filename.endswith(".gz"):
for line in gzip.open(filename):
line = line.decode("ascii")
if line.startswith("#"):
data.append(line)
else:
with open(filename) as f:
for line in f:
if line.startswith("#"):
data.append(line)
return data
class TestMissingGenotypes(unittest.TestCase):
filename = "missing_genotypes.vcf"
def setUp(self):
self.compare = load_and_convert(
os.path.join(CBCF_DATADIR, self.filename),
encode=False)
def check(self, filename):
"""see issue 203 - check for segmentation fault"""
fn = os.path.join(CBCF_DATADIR, filename)
self.assertEqual(True, os.path.exists(fn))
v = pysam.VariantFile(fn)
for site in v:
for ss, rec in site.samples.items():
a, b = ss, rec
v = pysam.VariantFile(fn)
for x, site in enumerate(v):
for ss, rec in site.samples.items():
a, b = ss, rec.alleles
a, b = ss, rec.allele_indices
def testVCF(self):
self.check(self.filename)
def testVCFGZ(self):
self.check(self.filename + ".gz")
class TestMissingSamples(unittest.TestCase):
filename = "gnomad.vcf"
def setUp(self):
self.compare = load_and_convert(
os.path.join(CBCF_DATADIR, self.filename),
encode=False)
def check(self, filename):
"""see issue #593"""
fn = os.path.join(CBCF_DATADIR, filename)
self.assertEqual(True, os.path.exists(fn))
expect_fail = not "fixed" in self.filename
with pysam.VariantFile(fn) as inf:
rec = next(inf.fetch())
if expect_fail:
self.assertRaises(ValueError, rec.info.__getitem__, "GC")
else:
self.assertEqual(rec.info["GC"], (27, 35, 16))
def testVCF(self):
self.check(self.filename)
def testVCFGZ(self):
self.check(self.filename + ".gz")
class TestMissingSamplesFixed(TestMissingSamples):
# workaround for NUMBER=G in INFO records:
# perl 's/Number=G/Number=./ if (/INFO/)'
filename = "gnomad_fixed.vcf"
class TestOpening(unittest.TestCase):
def testMissingFile(self):
self.assertRaises(IOError, pysam.VariantFile,
"missing_file.vcf")
def testMissingFileVCFGZ(self):
self.assertRaises(IOError, pysam.VariantFile,
"missing_file.vcf.gz")
def testEmptyFileVCF(self):
with get_temp_context("tmp_testEmptyFile.vcf") as fn:
with open(fn, "w"):
pass
self.assertRaises(ValueError, pysam.VariantFile, fn)
if Path and sys.version_info >= (3, 6):
def testEmptyFileVCFFromPath(self):
with get_temp_context("tmp_testEmptyFile.vcf") as fn:
with open(fn, "w"):
pass
self.assertRaises(ValueError, pysam.VariantFile,
Path(fn))
def testEmptyFileVCFGZWithIndex(self):
with get_temp_context("tmp_testEmptyFile.vcf") as fn:
with open(fn, "w"):
pass
# tabix_index will automatically compress
pysam.tabix_index(fn,
preset="vcf",
force=True)
self.assertRaises(ValueError, pysam.VariantFile, fn + ".gz")
def testEmptyFileVCFGZWithoutIndex(self):
with get_temp_context("tmp_testEmptyFileWithoutIndex.vcf") as fn:
with open(fn, "w"):
pass
pysam.tabix_compress(fn,
fn + ".gz",
force=True)
self.assertRaises(ValueError, pysam.VariantFile, fn + ".gz")
def testEmptyFileVCFOnlyHeader(self):
with pysam.VariantFile(os.path.join(
CBCF_DATADIR,
"example_vcf42_only_header.vcf")) as inf:
self.assertEqual(len(list(inf.fetch())), 0)
def testEmptyFileVCFGZOnlyHeader(self):
with pysam.VariantFile(os.path.join(
CBCF_DATADIR,
"example_vcf42_only_header.vcf")) as inf:
self.assertEqual(len(list(inf.fetch())), 0)
def testDetectVCF(self):
with pysam.VariantFile(os.path.join(CBCF_DATADIR,
"example_vcf40.vcf")) as inf:
self.assertEqual(inf.category, 'VARIANTS')
self.assertEqual(inf.format, 'VCF')
self.assertEqual(inf.compression, 'NONE')
self.assertFalse(inf.is_remote)
self.assertFalse(inf.is_stream)
self.assertEqual(len(list(inf.fetch())), 5)
def testDetectVCFGZ(self):
with pysam.VariantFile(os.path.join(CBCF_DATADIR,
"example_vcf40.vcf.gz")) as inf:
self.assertEqual(inf.category, 'VARIANTS')
self.assertEqual(inf.format, 'VCF')
self.assertEqual(inf.compression, 'BGZF')
self.assertFalse(inf.is_remote)
self.assertFalse(inf.is_stream)
self.assertEqual(len(list(inf.fetch())), 5)
def testDetectBCF(self):
with pysam.VariantFile(os.path.join(
CBCF_DATADIR,
"example_vcf40.bcf")) as inf:
self.assertEqual(inf.category, 'VARIANTS')
self.assertEqual(inf.format, 'BCF')
self.assertEqual(inf.compression, 'BGZF')
self.assertFalse(inf.is_remote)
self.assertFalse(inf.is_stream)
self.assertEqual(len(list(inf.fetch())), 5)
class TestIndexFormatsVCF(unittest.TestCase):
vcf_filename = os.path.join(CBCF_DATADIR, "example_vcf40.vcf")
bcf_filename = os.path.join(CBCF_DATADIR, "example_vcf40.bcf")
def test_vcf_with_tbi_index(self):
with get_temp_context("tmp_fn.vcf") as fn:
shutil.copyfile(self.vcf_filename, fn)
pysam.tabix_index(fn, preset="vcf", force=True)
self.assertTrue(os.path.exists(fn + ".gz" + ".tbi"))
self.assertFalse(os.path.exists(fn + ".gz" + ".csi"))
with pysam.VariantFile(fn + ".gz") as inf:
self.assertEqual(len(list(inf.fetch("20"))), 3)
def test_vcf_with_csi_index(self):
with get_temp_context("tmp_fn.vcf") as fn:
shutil.copyfile(self.vcf_filename, fn)
pysam.tabix_index(fn, preset="vcf", force=True, csi=True)
self.assertTrue(os.path.exists(fn + ".gz" + ".csi"))
self.assertFalse(os.path.exists(fn + ".gz" + ".tbi"))
with pysam.VariantFile(fn + ".gz") as inf:
self.assertEqual(len(list(inf.fetch("20"))), 3)
def test_bcf_with_prebuilt_csi(self):
with get_temp_context("tmp_fn.bcf") as fn:
shutil.copyfile(self.bcf_filename, fn)
shutil.copyfile(self.bcf_filename + ".csi", fn + ".csi")
self.assertTrue(os.path.exists(fn + ".csi"))
self.assertFalse(os.path.exists(fn + ".tbi"))
with pysam.VariantFile(fn) as inf:
self.assertEqual(len(list(inf.fetch("20"))), 3)
def test_bcf_with_tbi_index_will_produce_csi(self):
with get_temp_context("tmp_fn.bcf") as fn:
shutil.copyfile(self.bcf_filename, fn)
pysam.tabix_index(fn, preset="bcf", force=True, csi=False)
self.assertTrue(os.path.exists(fn + ".csi"))
self.assertFalse(os.path.exists(fn + ".tbi"))
with pysam.VariantFile(fn) as inf:
self.assertEqual(len(list(inf.fetch("20"))), 3)
def test_bcf_with_csi_index(self):
with get_temp_context("tmp_fn.bcf") as fn:
shutil.copyfile(self.bcf_filename, fn)
pysam.tabix_index(fn, preset="vcf", force=True, csi=True)
self.assertTrue(os.path.exists(fn + ".csi"))
self.assertFalse(os.path.exists(fn + ".tbi"))
with pysam.VariantFile(fn) as inf:
self.assertEqual(len(list(inf.fetch("20"))), 3)
class TestHeader(unittest.TestCase):
filename = "example_vcf40.vcf"
def testStr(self):
fn = os.path.join(CBCF_DATADIR, self.filename)
v = pysam.VariantFile(fn)
ref = read_header(fn)
comp = str(v.header).splitlines(True)
self.assertEqual(sorted(ref),
sorted(comp))
def testIterator(self):
fn = os.path.join(CBCF_DATADIR, self.filename)
v = pysam.VariantFile(fn)
ref = read_header(fn)
# remove last header line starting with #CHROM
ref.pop()
ref = sorted(ref)
comp = sorted(str(x) for x in v.header.records)
self.assertEqual(len(ref), len(comp))
for x, y in zip(ref, comp):
self.assertEqual(x, y)
# These tests need to be separate and start from newly opened files. This
# is because htslib's parser is lazy and the pysam API needs to trigger
# appropriate parsing when accessing each time of data. Failure to do so
# will result in crashes or return of incorrect data. Thus this test suite
# is testing both the triggering of the lazy parser and the results of the
# parser.
class TestParsing(unittest.TestCase):
filename = "example_vcf40.vcf.gz"
def testChrom(self):
fn = os.path.join(CBCF_DATADIR, self.filename)
v = pysam.VariantFile(fn)
chrom = [rec.chrom for rec in v]
self.assertEqual(chrom, ['M', '17', '20', '20', '20'])
if Path and sys.version_info >= (3, 6):
def testChromFromPath(self):
fn = os.path.join(CBCF_DATADIR, self.filename)
v = pysam.VariantFile(Path(fn))
chrom = [rec.chrom for rec in v]
self.assertEqual(chrom, ['M', '17', '20', '20', '20'])
def testPos(self):
fn = os.path.join(CBCF_DATADIR, self.filename)
v = pysam.VariantFile(fn)
pos = [rec.pos for rec in v]
self.assertEqual(pos, [1230237, 14370, 17330, 1110696, 1234567])
def testStart(self):
fn = os.path.join(CBCF_DATADIR, self.filename)
v = pysam.VariantFile(fn)
start = [rec.start for rec in v]
self.assertEqual(start, [1230236, 14369, 17329, 1110695, 1234566])
def testStop(self):
fn = os.path.join(CBCF_DATADIR, self.filename)
v = pysam.VariantFile(fn)
stop = [rec.stop for rec in v]
self.assertEqual(stop, [1230237, 14370, 17330, 1110696, 1234570])
def testId(self):
fn = os.path.join(CBCF_DATADIR, self.filename)
v = pysam.VariantFile(fn)
ids = [rec.id for rec in v]
self.assertEqual(
ids, [None, 'rs6054257', None, 'rs6040355', 'microsat1'])
def testRef(self):
fn = os.path.join(CBCF_DATADIR, self.filename)
v = pysam.VariantFile(fn)
ref = [rec.ref for rec in v]
self.assertEqual(ref, ['T', 'G', 'T', 'A', 'GTCT'])
def testAlt(self):
fn = os.path.join(CBCF_DATADIR, self.filename)
v = pysam.VariantFile(fn)
alts = [rec.alts for rec in v]
self.assertEqual(alts, [None, ('A',), ('A',),
('G', 'T'), ('G', 'GTACT')])
def testAlleles(self):
fn = os.path.join(CBCF_DATADIR, self.filename)
v = pysam.VariantFile(fn)
alleles = [rec.alleles for rec in v]
self.assertEqual(alleles, [
('T',), ('G', 'A'), ('T', 'A'), ('A', 'G', 'T'), ('GTCT', 'G', 'GTACT')])
def testQual(self):
fn = os.path.join(CBCF_DATADIR, self.filename)
v = pysam.VariantFile(fn)
qual = [rec.qual for rec in v]
self.assertEqual(qual, [47.0, 29.0, 3.0, 67.0, 50.0])
def testFilter(self):
fn = os.path.join(CBCF_DATADIR, self.filename)
v = pysam.VariantFile(fn)
filter = [rec.filter.keys() for rec in v]
self.assertEqual(filter, [['PASS'], ['PASS'],
['q10'], ['PASS'], ['PASS']])
def testInfo(self):
fn = os.path.join(CBCF_DATADIR, self.filename)
v = pysam.VariantFile(fn)
info = [rec.info.items() for rec in v]
self.assertEqual(info, [[('NS', 3), ('DP', 13), ('AA', 'T')],
[('NS', 3), ('DP', 14), ('AF', (0.5,)),
('DB', True), ('H2', True)],
[('NS', 3), ('DP', 11),
('AF', (0.017000000923871994,))],
[('NS', 2), ('DP', 10), ('AF', (0.3330000042915344, 0.6669999957084656)),
('AA', 'T'), ('DB', True)],
[('NS', 3), ('DP', 9), ('AA', 'G')]])
def testFormat(self):
fn = os.path.join(CBCF_DATADIR, self.filename)
v = pysam.VariantFile(fn)
format = [rec.format.keys() for rec in v]
self.assertEqual(format, [['GT', 'GQ', 'DP', 'HQ'],
['GT', 'GQ', 'DP', 'HQ'],
['GT', 'GQ', 'DP', 'HQ'],
['GT', 'GQ', 'DP', 'HQ'],
['GT', 'GQ', 'DP']])
def testSampleAlleles(self):
fn = os.path.join(CBCF_DATADIR, self.filename)
v = pysam.VariantFile(fn)
alleles = [s.alleles for rec in v for s in rec.samples.values()]
self.assertEqual(alleles, [('T', 'T'), ('T', 'T'), ('T', 'T'),
('G', 'G'), ('A', 'G'), ('A', 'A'),
('T', 'T'), ('T', 'A'), ('T', 'T'),
('G', 'T'), ('T', 'G'), ('T', 'T'),
('GTCT', 'G'), ('GTCT', 'GTACT'),
('G', 'G')])
def testSampleFormats(self):
fn = os.path.join(CBCF_DATADIR, self.filename)
v = pysam.VariantFile(fn)
format = [s.items() for rec in v for s in rec.samples.values()]
self.assertEqual(format, [[('GT', (0, 0)), ('GQ', 54), ('DP', 7), ('HQ', (56, 60))],
[('GT', (0, 0)), ('GQ', 48),
('DP', 4), ('HQ', (51, 51))],
[('GT', (0, 0)), ('GQ', 61),
('DP', 2), ('HQ', (None,))],
[('GT', (0, 0)), ('GQ', 48),
('DP', 1), ('HQ', (51, 51))],
[('GT', (1, 0)), ('GQ', 48),
('DP', 8), ('HQ', (51, 51))],
[('GT', (1, 1)), ('GQ', 43),
('DP', 5), ('HQ', (None, None))],
[('GT', (0, 0)), ('GQ', 49),
('DP', 3), ('HQ', (58, 50))],
[('GT', (0, 1)), ('GQ', 3),
('DP', 5), ('HQ', (65, 3))],
[('GT', (0, 0)), ('GQ', 41),
('DP', 3), ('HQ', (None,))],
[('GT', (1, 2)), ('GQ', 21),
('DP', 6), ('HQ', (23, 27))],
[('GT', (2, 1)), ('GQ', 2),
('DP', 0), ('HQ', (18, 2))],
[('GT', (2, 2)), ('GQ', 35),
('DP', 4), ('HQ', (None,))],
[('GT', (0, 1)), ('GQ', 35), ('DP', 4)],
[('GT', (0, 2)), ('GQ', 17), ('DP', 2)],
[('GT', (1, 1)), ('GQ', 40), ('DP', 3)]])
def testSampleAlleleIndices(self):
fn = os.path.join(CBCF_DATADIR, self.filename)
v = pysam.VariantFile(fn)
indices = [s.allele_indices for rec in v for s in rec.samples.values()]
self.assertEqual(indices, [(0, 0), (0, 0), (0, 0), (0, 0), (1, 0),
(1, 1), (0, 0), (0, 1), (0, 0), (1, 2),
(2, 1), (2, 2), (0, 1), (0, 2), (1, 1)])
class TestIndexFilename(unittest.TestCase):
filenames = [('example_vcf40.vcf.gz', 'example_vcf40.vcf.gz.tbi'),
('example_vcf40.vcf.gz', 'example_vcf40.vcf.gz.csi'),
('example_vcf40.bcf', 'example_vcf40.bcf.csi')]
def testOpen(self):
for fn, idx_fn in self.filenames:
fn = os.path.join(CBCF_DATADIR, fn)
idx_fn = os.path.join(CBCF_DATADIR, idx_fn)
with pysam.VariantFile(fn, index_filename=idx_fn) as inf:
self.assertEqual(len(list(inf.fetch('20'))), 3)
class TestConstructionVCFWithContigs(unittest.TestCase):
"""construct VariantFile from scratch."""
filename = "example_vcf42_withcontigs.vcf"
compression = 'NONE'
description = 'VCF version 4.2 variant calling text'
def testBase(self):
with pysam.VariantFile(os.path.join(CBCF_DATADIR, self.filename)) as inf:
self.assertEqual(inf.category, 'VARIANTS')
self.assertEqual(inf.format, 'VCF')
self.assertEqual(inf.version, (4, 2))
self.assertEqual(inf.compression, self.compression)
self.assertEqual(inf.description, self.description)
self.assertTrue(inf.is_open)
self.assertEqual(inf.is_read, True)
self.assertEqual(inf.is_write, False)
def complete_check(self, fn_in, fn_out):
self.maxDiff = None
check_lines_equal(
self, fn_in, fn_out, sort=True,
filter_f=lambda x: x.startswith("##contig"))
os.unlink(fn_out)
def testConstructionWithRecords(self):
fn_in = os.path.join(CBCF_DATADIR, self.filename)
fn_out = get_temp_filename(suffix=".vcf")
vcf_in = pysam.VariantFile(fn_in)
header = pysam.VariantHeader()
for record in vcf_in.header.records:
header.add_record(record)
for sample in vcf_in.header.samples:
header.add_sample(sample)
vcf_out = pysam.VariantFile(fn_out, "w", header=header)
for record in vcf_in:
record.translate(header)
vcf_out.write(record)
vcf_in.close()
vcf_out.close()
self.complete_check(fn_in, fn_out)
def testConstructionFromCopy(self):
fn_in = os.path.join(CBCF_DATADIR, self.filename)
fn_out = get_temp_filename(suffix=".vcf")
vcf_in = pysam.VariantFile(fn_in)
vcf_out = pysam.VariantFile(fn_out, "w", header=vcf_in.header)
for record in vcf_in:
vcf_out.write(record)
vcf_in.close()
vcf_out.close()
self.complete_check(fn_in, fn_out)
def testConstructionWithLines(self):
fn_in = os.path.join(CBCF_DATADIR, self.filename)
fn_out = get_temp_filename(suffix=".vcf")
vcf_in = pysam.VariantFile(fn_in)
header = pysam.VariantHeader()
for sample in vcf_in.header.samples:
header.add_sample(sample)
for hr in vcf_in.header.records:
header.add_line(str(hr))
vcf_out = pysam.VariantFile(fn_out, "w", header=header)
for record in vcf_in:
vcf_out.write(record)
vcf_out.close()
vcf_in.close()
self.complete_check(fn_in, fn_out)
# class TestConstructionVCFWithoutContigs(TestConstructionVCFWithContigs):
# """construct VariantFile from scratch."""
# filename = "example_vcf40.vcf"
class TestConstructionVCFGZWithContigs(TestConstructionVCFWithContigs):
"""construct VariantFile from scratch."""
filename = "example_vcf42_withcontigs.vcf.gz"
compression = 'BGZF'
description = 'VCF version 4.2 BGZF-compressed variant calling data'
class TestConstructionVCFGZWithoutContigs(TestConstructionVCFWithContigs):
"""construct VariantFile from scratch."""
filename = "example_vcf42.vcf.gz"
compression = 'BGZF'
description = 'VCF version 4.2 BGZF-compressed variant calling data'
class TestSettingRecordValues(unittest.TestCase):
filename = "example_vcf40.vcf"
def testBase(self):
with pysam.VariantFile(os.path.join(CBCF_DATADIR, self.filename)) as inf:
self.assertEqual(inf.category, 'VARIANTS')
self.assertEqual(inf.format, 'VCF')
self.assertEqual(inf.version, (4, 0))
self.assertEqual(inf.compression, 'NONE')
self.assertEqual(
inf.description, 'VCF version 4.0 variant calling text')
self.assertTrue(inf.is_open)
self.assertEqual(inf.is_read, True)
self.assertEqual(inf.is_write, False)
def testSetQual(self):
with pysam.VariantFile(os.path.join(CBCF_DATADIR, self.filename)) as inf:
record = next(inf)
self.assertEqual(record.qual, 47)
record.qual = record.qual
self.assertEqual(record.qual, 47)
record.qual = 10
self.assertEqual(record.qual, 10)
self.assertEqual(str(record).split("\t")[5], "10")
def testGenotype(self):
with pysam.VariantFile(os.path.join(CBCF_DATADIR, self.filename)) as inf:
record = next(inf)
sample = record.samples["NA00001"]
print(sample["GT"])
self.assertEqual(sample["GT"], (0, 0))
sample["GT"] = sample["GT"]
class TestMultiThreading(unittest.TestCase):
filename = os.path.join(CBCF_DATADIR, "example_vcf42.vcf.gz")
def testSingleThreadEqualsMultithreadResult(self):
with pysam.VariantFile(self.filename) as inf:
header = inf.header
single = [r for r in inf]
with pysam.VariantFile(self.filename, threads=2) as inf:
multi = [r for r in inf]
for r1, r2 in zip(single, multi):
assert str(r1) == str(r2)
bcf_out = get_temp_filename(suffix=".bcf")
with pysam.VariantFile(bcf_out, mode='wb',
header=header,
threads=2) as out:
for r in single:
out.write(r)
with pysam.VariantFile(bcf_out) as inf:
multi_out = [r for r in inf]
for r1, r2 in zip(single, multi_out):
assert str(r1) == str(r2)
def testNoMultiThreadingWithIgnoreTruncation(self):
with self.assertRaises(ValueError):
pysam.VariantFile(self.filename,
threads=2,
ignore_truncation=True)
class TestSubsetting(unittest.TestCase):
filename = "example_vcf42.vcf.gz"
def testSubsetting(self):
with pysam.VariantFile(os.path.join(CBCF_DATADIR,
self.filename)) as inf:
inf.subset_samples(["NA00001"])
class TestVCFVersions(unittest.TestCase):
def setUp(self):
self.files_to_test = (glob.glob(os.path.join(CBCF_DATADIR, "example_v*.vcf.gz")) +
glob.glob(os.path.join(CBCF_DATADIR, "example_v*.vcf")) +
glob.glob(os.path.join(CBCF_DATADIR, "example_v*.bcf")))
def test_all_records_can_be_fetched(self):
for fn in self.files_to_test:
with pysam.VariantFile(fn) as inf:
records = list(inf.fetch())
class TestUnicode(unittest.TestCase):
filename = "example_vcf43_with_utf8.vcf.gz"
def test_record_with_unicode(self):
with pysam.VariantFile(os.path.join(CBCF_DATADIR,
self.filename)) as inf:
records = list(inf.fetch("20", 2345677, 2345678))
self.assertEqual(len(records), 1)
record = records.pop()
self.assertEqual(
record.info["CLNVI"],
('Institute_of_Human_Genetics', u'Friedrich-Alexander-Universität_Erlangen-Nürnberg'))
self.assertEqual(record.samples[0]["AN"], "alpha")
self.assertEqual(record.samples[1]["AN"], u"ä")
self.assertEqual(record.samples[2]["AN"], u"ü")
if __name__ == "__main__":
# build data files
print("building data files")
subprocess.call("make -C %s" % CBCF_DATADIR, shell=True)
print("starting tests")
unittest.main()
print("completed tests")
|