def test_indexing_to_custom_location_works(self): '''test indexing a file with a non-default location.''' index_path = get_temp_filename(suffix='custom.tbi') pysam.tabix_index(self.tmpfilename, preset="gff", index=index_path, force=True) self.assertTrue(checkBinaryEqual(index_path, self.filename_idx)) os.unlink(index_path)
def testSaveStdout(self): outfile = get_temp_filename(suffix=".tsv") r = pysam.samtools.flagstat(os.path.join(BAM_DATADIR, "ex1.bam"), save_stdout=outfile) self.assertEqual(r, None) with open(outfile) as inf: r = inf.read() self.assertTrue(len(r) > 0)
def testSaveStdout(self): outfile = get_temp_filename(suffix=".tsv") r = pysam.samtools.flagstat( os.path.join(DATADIR, "ex1.bam"), save_stdout=outfile) self.assertEqual(r, None) with open(outfile) as inf: r = inf.read() self.assertTrue(len(r) > 0)
def test_indexing_to_custom_location_works(self): '''test indexing a file with a non-default location.''' index_path = get_temp_filename(suffix='custom.tbi') pysam.tabix_index(self.tmpfilename, preset="gff", index=index_path, force=True) self.assertTrue(checkGZBinaryEqual(index_path, self.filename_idx)) os.unlink(index_path)
def test_read_can_be_written_to_file(self): tmpfilename = get_temp_filename(".bam") with pysam.AlignmentFile(tmpfilename, "wb", reference_names=["chr1", "chr2", "chr3"], reference_lengths=[1000, 2000, 3000]) as outf: read = self.build_read() read.reference_id = 2 outf.write(read) stdout = pysam.samtools.view(tmpfilename) chromosome = stdout.split("\t")[2] self.assertEqual(chromosome, "chr3") os.unlink(tmpfilename)
def testConstructionFromCopy(self): fn_in = os.path.join(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_out.close() self.complete_check(fn_in, fn_out)
def test_open_file_with_explicit_abritrarily_named_index_succeeds(self): tmpfilename = get_temp_filename(self.data_suffix) shutil.copyfile(self.filename, tmpfilename) filepath_index = self.filename + ".fai" filepath_index_compressed = self.filename + ".gzi" if not os.path.exists(filepath_index_compressed): filepath_index_compressed = None with pysam.FastaFile(tmpfilename, filepath_index=filepath_index, filepath_index_compressed=filepath_index_compressed) as inf: self.assertEqual(len(inf), 2) # index should not be auto-generated self.assertFalse(os.path.exists(tmpfilename + ".fai")) os.unlink(tmpfilename)
def check_read_write(self, flag_write, header): fn = get_temp_filename() with pysam.AlignmentFile( fn, flag_write, header=header, reference_filename=os.path.join(BAM_DATADIR, "ex1.fa")) as outf: a = pysam.AlignedSegment() a.query_name = "abc" outf.write(a) with pysam.AlignmentFile(fn) as inf: read_header = inf.header os.unlink(fn) self.compare_headers(header, read_header)
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 testConstructionWithRecords(self): fn_in = os.path.join(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) fn = str("tmp_VariantFileTest_testConstructionWithRecords") + ".vcf" vcf_out = pysam.VariantFile(fn, "w", header=header) for record in vcf_in: # currently segfaults here: # vcf_out.write(record) pass return 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)
def check_read_write(self, flag_write, header): fn = get_temp_filename() print(fn) with pysam.AlignmentFile(fn, flag_write, header=header, reference_filename=os.path.join( BAM_DATADIR, "ex1.fa")) as outf: a = pysam.AlignedSegment() a.query_name = "abc" outf.write(a) with pysam.AlignmentFile(fn) as inf: read_header = inf.header # os.unlink(fn) self.compare_headers(header, read_header) expected_lengths = dict([(x["SN"], x["LN"]) for x in header["SQ"]]) self.assertEqual( expected_lengths, dict(zip(read_header.references, read_header.lengths)))
def testConstructionWithLines(self): fn_in = os.path.join(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)
def setUp(self): self.tmpfilename = get_temp_filename(suffix="vcf") shutil.copyfile(self.filename, self.tmpfilename) pysam.tabix_index(self.tmpfilename, preset="vcf")
def setUp(self): self.tmpfilename = get_temp_filename(suffix="gtf.gz") shutil.copyfile(self.filename, self.tmpfilename)
def setUp(self): self.tmpfilename = get_temp_filename(suffix="gtf") with gzip.open(self.filename, "rb") as infile, \ open(self.tmpfilename, "wb") as outfile: outfile.write(infile.read())