Exemple #1
0
def test_header_header_line():
    line1 = header.HeaderLine("key", "value")
    line2 = header.HeaderLine("key", "value")
    line3 = header.HeaderLine("key2", "value")
    assert line1 == line2
    assert line1 != line3
    assert str(line1) == "HeaderLine('key', 'value')"
    assert repr(line1) == "HeaderLine('key', 'value')"
    assert line1.value == "value"
    assert line1.serialize() == "##key=value"
    with pytest.raises(TypeError):
        hash(line1)
Exemple #2
0
def test_header_without_lines():
    lines = [
        header.HeaderLine("foo", "bar"),
        header.HeaderLine("foo2", "bar2")
    ]
    samples = header.SamplesInfos(["one", "two", "three"])
    hdr = header.Header(lines, samples)
    hdr.add_filter_line(vcfpy.OrderedDict([("ID", "PASS")]))
    hdr.add_filter_line(vcfpy.OrderedDict([("ID", "q30")]))
    assert len(hdr.lines) == 4

    hdr2 = header.header_without_lines(hdr, [("foo", "bar"),
                                             ("FILTER", "q30")])
    assert len(hdr2.lines) == 2
    assert hdr2.samples == hdr.samples
def test_add_simple_line(vcf_header):
    assert len(vcf_header.lines) == 18
    line = header.HeaderLine("somekey", "somevalue")
    vcf_header.add_line(line)
    assert len(vcf_header.lines) == 19

    assert vcf_header.lines[-1].key == "somekey"
    assert vcf_header.lines[-1].value == "somevalue"
Exemple #4
0
def test_header_checker_missing_fileformat():
    # construct Header
    lines = [header.HeaderLine("key", "value")]
    hdr = header.Header(lines=lines, samples=header.SamplesInfos(["NA001"]))
    # setup HeaderChecker
    stream = io.StringIO()
    checker = parser.HeaderChecker()
    # execute
    with pytest.raises(exceptions.InvalidHeaderException):
        checker.run(hdr)
Exemple #5
0
def test_header_checker_known_vcf_version():
    # construct Header
    lines = [header.HeaderLine("fileformat", "VCFv4.3")]
    hdr = header.Header(lines=lines, samples=header.SamplesInfos(["NA001"]))
    # setup HeaderChecker
    stream = io.StringIO()
    checker = parser.HeaderChecker()
    # execute
    checker.run(hdr)
    # check result
    EXPECTED = ""
    assert stream.getvalue() == EXPECTED
Exemple #6
0
def test_header_header():
    lines1 = [
        header.HeaderLine("foo", "bar"),
        header.HeaderLine("foo2", "bar2")
    ]
    samples1 = header.SamplesInfos(["one", "two", "three"])
    hdr1 = header.Header(lines1, samples1)

    lines2 = [
        header.HeaderLine("foo", "bar"),
        header.HeaderLine("foo2", "bar2")
    ]
    samples2 = header.SamplesInfos(["one", "two", "three"])
    hdr2 = header.Header(lines2, samples2)

    lines3 = [
        header.HeaderLine("foo3", "bar"),
        header.HeaderLine("foo2", "bar2")
    ]
    samples3 = header.SamplesInfos(["one", "two", "three"])
    hdr3 = header.Header(lines3, samples3)

    assert hdr1 == hdr2
    assert hdr1 != hdr3
    EXPECTED = (
        "Header(lines=[HeaderLine('foo', 'bar'), HeaderLine('foo2', 'bar2')], "
        "samples=SamplesInfos(names=['one', 'two', 'three'], "
        "name_to_idx={'one': 0, 'three': 2, 'two': 1}))")
    assert str(hdr1) == EXPECTED

    with pytest.raises(TypeError):
        hash(hdr1)
Exemple #7
0
def test_write_header_no_samples(tmpdir_factory):
    # Create header to write out from scratch
    hdr = header.Header(
        lines=[header.HeaderLine("fileformat", "VCFv4.0")], samples=header.SamplesInfos([])
    )
    # Write out header
    path = tmpdir_factory.mktemp("write_header").join("out.vcf")
    w = writer.Writer.from_path(path, hdr)
    w.close()
    # Compare result
    RESULT = path.read()
    EXPECTED = textwrap.dedent(
        r"""
    ##fileformat=VCFv4.0
    #CHROM	POS	ID	REF	ALT	QUAL	FILTER	INFO
    """
    ).lstrip()
    assert RESULT == EXPECTED
Exemple #8
0
def test_write_record_no_samples(tmpdir_factory):
    O = vcfpy.OrderedDict
    # Create header without samples
    hdr = header.Header(lines=[header.HeaderLine("fileformat", "VCFv4.0")],
                        samples=header.SamplesInfos([]))
    # construct record to write out from scratch
    r = record.Record("20", 100, [], "C",
                      [record.Substitution(record.SNV, "T")], None, [], O())
    # Write out header and record
    path = tmpdir_factory.mktemp("write_header").join("out.vcf")
    w = writer.Writer.from_path(path, hdr)
    w.write_record(r)
    w.close()
    # Compare result
    RESULT = path.read()
    EXPECTED = textwrap.dedent("""
    ##fileformat=VCFv4.0
    #CHROM\tPOS\tID\tREF\tALT\tQUAL\tFILTER\tINFO
    20\t100\t.\tC\tT\t.\t.\t.
    """).lstrip()
    assert RESULT == EXPECTED