def test_write_record_with_escaping(header_samples, tmpdir_factory): O = vcfpy.OrderedDict S = record.Substitution # open temporary file and setup the Writer with header path = tmpdir_factory.mktemp("write_header").join("out.vcf") header, _ = header_samples w = writer.Writer.from_path(path, header) # construct record to write out from scratch r = record.Record( "20", 100, [], "C", [record.Substitution(record.SNV, "T")], None, [], O([("ANNO", ["Here,are%some chars", "%25"])]), ["GT", "FT"], [ record.Call("NA00001", O(GT="0/1", FT=["%25", "FOO"])), record.Call("NA00002", O(GT="0/0", FT=[])), record.Call("NA00003", O(GT="1/1", FT=[])), ], ) # write out the record, close file to ensure flushing to disk w.write_record(r) w.close() # compare actual result with expected RESULT = path.read() LINE = ("20\t100\t.\tC\tT\t.\t.\tANNO=Here%2Care%25some chars," "%2525\tGT:FT\t0/1:%2525;FOO\t0/0:.\t1/1:.\n") EXPECTED = MEDIUM_HEADER + LINE assert EXPECTED == RESULT
def test_write_annotated_record(header_samples, tmpdir_factory): O = vcfpy.OrderedDict S = record.Substitution # open temporary file and setup the Writer with header path = tmpdir_factory.mktemp("write_annotated_record").join("out.vcf") header, _ = header_samples w = writer.Writer.from_path(path, header) # construct record to write out from scratch r = record.Record( "20", 100, ["rs333", "CSN42"], "C", [ record.Substitution(record.SNV, "T"), record.Substitution(record.SNV, "G") ], 50, ["PASS"], O([("DP", 93), ("AF", [0.3, 0.2]), ("DB", True)]), ["GT", "DP", "GQ", "HQ"], [ record.Call("NA00001", O(GT="0/1", DP=30, GQ=40, HQ=[1, 2])), record.Call("NA00002", O(GT="0/2", DP=31, GQ=41, HQ=[3, 4])), record.Call("NA00003", O(GT="1/2", DP=32, GQ=42, HQ=[5, 6])), ], ) # write out the record, close file to ensure flushing to disk w.write_record(r) w.close() # compare actual result with expected RESULT = path.read() LINE = "20\t100\trs333;CSN42\tC\tT,G\t50\tPASS\tDP=93;AF=0.3,0.2;DB\tGT:DP:GQ:HQ\t0/1:30:40:1,2\t0/2:31:41:3,4\t1/2:32:42:5,6\n" EXPECTED = MEDIUM_HEADER + LINE assert EXPECTED == RESULT
def test_write_minimal_record(header_samples, tmpdir_factory): O = vcfpy.OrderedDict # open temporary file and setup the Writer with header path = tmpdir_factory.mktemp("write_header").join("out.vcf") header, _ = header_samples w = writer.Writer.from_path(path, header) # construct record to write out from scratch r = record.Record( "20", 100, [], "C", [record.Substitution(record.SNV, "T")], None, [], O(), ["GT"], [ record.Call("NA00001", O(GT="0/1")), record.Call("NA00002", O(GT="0/0")), record.Call("NA00003", O(GT="1/1")), ], ) # write out the record, close file to ensure flushing to disk w.write_record(r) w.close() # compare actual result with expected RESULT = path.read() LINE = "20\t100\t.\tC\tT\t.\t.\t.\tGT\t0/1\t0/0\t1/1\n" EXPECTED = MEDIUM_HEADER + LINE assert EXPECTED == RESULT
def test_write_minimal_record_writer_from_stream_path(header_samples, tmpdir_factory): O = vcfpy.OrderedDict # open temporary file and setup the Writer with header path = tmpdir_factory.mktemp("write_header").join("out.vcf.gz") header, _ = header_samples with open(str(path), "wb") as f: w = writer.Writer.from_stream(f, header, path=str(path)) # construct record to write out from scratch r = record.Record( "20", 100, [], "C", [record.Substitution(record.SNV, "T")], None, [], O(), ["GT"], [ record.Call("NA00001", O(GT="0/1")), record.Call("NA00002", O(GT="0/0")), record.Call("NA00003", O(GT="1/1")), ], ) # write out the record, close file to ensure flushing to disk w.write_record(r) w.close() # check the resulting record LINE = "20\t100\t.\tC\tT\t.\t.\t.\tGT\t0/1\t0/0\t1/1\n" check_file(path, LINE)
def test_gt_bases_0_2(): call = record.Call("sample", vcfpy.OrderedDict([("GT", "0|2")])) build_rec([call]) assert call.gt_bases == ("C", "A")
def test_gt_bases_1_1(): call = record.Call("sample", vcfpy.OrderedDict([("GT", "1|1")])) build_rec([call]) assert call.gt_bases == ("T", "T")
def test_gt_phase_char_slash(): call = record.Call("sample", vcfpy.OrderedDict([("GT", "0/1")])) assert call.gt_phase_char == "/"
def test_gt_phase_char_pipe(): call = record.Call("sample", vcfpy.OrderedDict([("GT", "0|1")])) assert call.gt_phase_char == "|"
def test_is_phased_false(): call = record.Call("sample", vcfpy.OrderedDict([("GT", "0/1")])) assert call.is_phased is False
def test_is_phased_mixed(): call = record.Call("sample", vcfpy.OrderedDict([("GT", "0/1|2")])) assert call.is_phased is True
def test_gt_type_filtered_no_ft(): call = record.Call("sample", vcfpy.OrderedDict([("GT", "1/1")])) assert not call.is_filtered()
def test_is_variant_hom_alt(): call = record.Call("sample", vcfpy.OrderedDict([("GT", "1/1")])) assert call.is_variant
def test_is_variant_hom_ref(): call = record.Call("sample", vcfpy.OrderedDict([("GT", "0/0")])) assert not call.is_variant
def test_is_het_het(): call = record.Call("sample", vcfpy.OrderedDict([("GT", "0|1")])) assert call.is_het
def test_gt_type_hom_alt(): call = record.Call("sample", vcfpy.OrderedDict([("GT", "1/1")])) assert call.gt_type == vcfpy.HOM_ALT
def test_gt_type_het(): call = record.Call("sample", vcfpy.OrderedDict([("GT", "0|1")])) assert call.gt_type == vcfpy.HET
def test_gt_type_hom_ref(): call = record.Call("sample", vcfpy.OrderedDict([("GT", "0/0")])) assert call.gt_type == vcfpy.HOM_REF
def test_gt_type_filtered_pass(): call = record.Call("sample", vcfpy.OrderedDict([("GT", "1/1"), ("FT", ["PASS"])])) assert not call.is_filtered()