예제 #1
0
def test_formula_init():
    fmula = Formula("AAF", None, False, "title")
    assert fmula.Y == Term("AAF", None)
    assert fmula.X == One()

    fmula = Formula("AAF~", None, False, "title")
    assert fmula.Y == Term("AAF", None)
    assert fmula.X == One()

    fmula = Formula("MEAN(AAF) ~ CHROM", None, True, "title")
    assert fmula.Y.group == Term("CHROM", None)
    assert fmula.passed

    fmula = Formula("MEAN(AAF) ~ FILTER", None, True, "title")
    assert not fmula.passed

    fmula = Formula("MEAN(AAF, FILTER[PASS], group=VARTYPE) ~ CHROM", None,
                    True, "title")
    assert isinstance(fmula.Y, Aggr)
    assert isinstance(fmula.X, Term)
예제 #2
0
def test_formula_init():
	fmula = Formula('AAF', None, False, 'title')
	assert fmula.Y == Term('AAF', None)
	assert fmula.X == Term('1', None)
	assert fmula._terms == {}

	fmula = Formula('AAF~', None, False, 'title')
	assert fmula.Y == Term('AAF', None)
	assert fmula.X == Term('1', None)
	assert fmula._terms == {}

	fmula = Formula('MEAN(AAF) ~ CHROM', None, True, 'title')
	assert fmula.Y.group == Term('CHROM', None)
	assert fmula._terms == {'TERM0': Term('AAF', None)}
	assert fmula.passed

	fmula = Formula('MEAN(AAF) ~ FILTER', None, True, 'title')
	assert not fmula.passed

	with pytest.raises(ValueError):
		Formula('MEAN(AAF, FILTER, CHROM, VARTYPE) ~ FILTER', None, True, 'title')

	fmula = Formula('MEAN(AAF, FILTER[PASS], group=VARTYPE) ~ CHROM', None, True, 'title')
	assert isinstance(fmula.Y, Aggr)
	assert isinstance(fmula.X, Term)

	with pytest.raises(ValueError):
		Formula('MEAN(AAF, FILTER[PASS], group1=VARTYPE) ~ CHROM', None, True, 'title')
예제 #3
0
def test_term_init():
    term = Term("AAF", None)
    assert term.name == "AAF"
    assert term.term == MACROS["AAF"]
    assert term.samples == None
    assert term.subsets == None

    term = Term("AAF", items=[".05", ".95"])
    assert term.subsets == [0.05, 0.95]

    term = One()
    assert term.name == "_ONE"

    with pytest.raises(ValueError):
        Term("NoSuchTerm", None)

    MACROS["NOTYPE"] = {}
    with pytest.raises(TypeError):
        Term("NOTYPE", None)
    del MACROS["NOTYPE"]

    term = Term("AFs", samples=["1"])
    assert term.samples == ["1"]

    term = Term("AFs", [None, 0.5], ["sample2"])
    term.set_samples(["sample1", "sample2"])
    assert term.samples == [1]
    assert term.subsets == [None, 0.5]

    term2 = Term("AFs", [None, 0.5], ["sample2"])
    term2.set_samples(["sample1", "sample2"])
    assert term2.samples == [1]
    assert term2.subsets == [None, 0.5]
    assert repr(term2) == "<Term AFs(subsets=[None, 0.5], samples=[1])>"
    assert term2 == term
    assert term2 != 1

    with pytest.raises(KeyError):
        Term("AFs", [1, 2, 3], None)
예제 #4
0
def test_aggr_run(variants):
    aggr = Aggr("COUNT",
                One(),
                filter=Term("FILTER", ["PASS"]),
                group=Term("VARTYPE"))
    aggr.run(variants[0], passed=True)
    assert len(aggr.cache) == 0

    aggr2 = Aggr("COUNT", One(), filter=Term("FILTER", ["PASS"]))
    with pytest.raises(RuntimeError):
        aggr2.run(variants[5], passed=True)

    aggr3 = Aggr("COUNT",
                 One(),
                 filter=Term("FILTER", ["PASS"]),
                 group=Term("FILTER2"))
    aggr3.run(variants[5], passed=False)
    assert len(aggr3.cache) == 0

    aggr4 = Aggr("COUNT", One(), Term("GTTYPEs"))
    with pytest.raises(ValueError):
        aggr4.run(variants[0], passed=False)

    aggr5 = Aggr("COUNT", One(), Term("VARTYPE"))
    aggr5.run(variants[0], passed=False)
    assert aggr5.cache == {"snp": [1]}
    aggr5.run(variants[1], passed=False)
    assert aggr5.cache == {"snp": [1, 1]}
    aggr5.run(variants[3], passed=False)
    assert aggr5.cache == {"snp": [1, 1], "indel": [1]}

    assert aggr5.dump() == {"snp": 2, "indel": 1}

    aggr5.cache.clear()
    aggr5.setxgroup(Term("FILTER", None))
    aggr5.run(variants[0], passed=False)
    assert aggr5.cache == {"MinMQ": {"snp": [1]}}

    aggr5.cache.clear()
    aggr5.setxgroup(Term("FILTER2", None))
    aggr5.run(variants[0], passed=False)
    assert aggr5.cache == {"MinMQ": {"snp": [1]}}
    aggr5.run(variants[5], passed=False)
    assert aggr5.cache == {"MinMQ": {"snp": [1]}}
    aggr5.run(variants[1], passed=False)
    assert aggr5.cache == {"MinMQ": {"snp": [1, 1]}}
    assert aggr5.dump() == {"MinMQ": [(2, "snp")]}

    aggr5.setxgroup(Term("GTTYPEs", ["HOM_REF", "HET"]))
    with pytest.raises(ValueError):
        aggr5.run(variants[0], passed=False)

    aggr6 = Aggr("MEAN", Term("AAF", [".2", None]), Term("CHROM"))
    aggr6.run(variants[0], passed=False)  # .125
    assert len(aggr6.cache) == 0
예제 #5
0
def test_aggr_init():
    with pytest.raises(ValueError):
        Aggr("COUNT", None)
    with pytest.raises(ValueError):
        Aggr("NoSuchAggr", "")
    aggr = Aggr("COUNT", One())
    assert aggr.term == One()
    assert aggr.filter is None
    assert aggr.group is None
    assert not aggr.has_filter()

    aggr = Aggr("COUNT", One(), Term("FILTER", ["PASS"]))
    assert aggr.term == One()
    assert aggr.group == Term("FILTER", ["PASS"])
    assert aggr.filter is None

    aggr = Aggr("COUNT", One(), filter=Term("FILTER", ["PASS"]))
    assert aggr.term == One()
    assert aggr.filter == Term("FILTER", ["PASS"])
    assert aggr.group is None
    assert aggr.has_filter()
    aggr.setxgroup(Term("VARTYPE", None))
    assert aggr.group == Term("VARTYPE", None)
    assert aggr.xgroup is None

    aggr = Aggr("COUNT",
                One(),
                filter=Term("FILTER", ["PASS"]),
                group=Term("VARTYPE"))
    assert aggr.term == One()
    assert aggr.filter == Term("FILTER", ["PASS"])
    assert aggr.group == Term("VARTYPE", None)
    assert aggr.xgroup is None
    aggr.setxgroup(Term("GTTYPEs", ["A"]))
    assert aggr.xgroup == Term("GTTYPEs", ["A"])
    assert (
        repr(aggr) ==
        "<Aggr COUNT(<Term _ONE()>, filter=<Term FILTER(subsets=['PASS'])>, group=<Term VARTYPE()>)>"
    )
    assert aggr.has_filter()

    aggr = Aggr("COUNT", One(), filter=Term("FILTER"), group=Term("VARTYPE"))
    assert aggr.term == One()
    assert aggr.filter == Term("FILTER", None)
    assert aggr.group == Term("VARTYPE", None)

    aggr = Aggr("COUNT", One(), filter=Term("GTTYPEs"))
    assert aggr.term == One()
    assert aggr.filter == Term("GTTYPEs", None)
    assert aggr.group == None

    with pytest.raises(ValueError):
        Aggr("Nosuchaggr", 1)

    with pytest.raises(ValueError):
        Aggr("COUNT", None)
예제 #6
0
def test_term_run(variants):
    term = Term("FILTER", "PASS")
    assert term.run(variants[0], passed=True) == False
    assert term.run(variants[5], passed=True) == ["PASS"]

    term = Term("FILTER2")
    assert term.run(variants[5], passed=True) == False

    term = Term("GTTYPEs", None, ["0"])
    term.set_samples(variants[-1])
    assert term.run(variants[0], passed=False) == ["HOM_REF"]

    term = Term("AAF", [0.126, None])
    # .125
    assert term.run(variants[0], passed=False) == False
    assert term.run(variants[2], passed=False) == [0.25]
    term = Term("AAF", [None, 0.24])
    # .25
    assert term.run(variants[0], passed=False) == [0.125]
    assert term.run(variants[2], passed=False) == False

    term = Term("FILTER", "PASS")
    assert term.run(variants[0], passed=False) == False
    assert term.run(variants[5], passed=False) == ["PASS"]
예제 #7
0
def test_term_run(variants):
	term = Term('FILTER[PASS]', None)
	assert term.run(variants[0], passed = True) == False
	assert term.run(variants[5], passed = True) == ['PASS']

	term = Term('FILTER2', None)
	assert term.run(variants[5], passed = True) == False

	term = Term('GTTYPEs{0}', ['A', 'B', 'C', 'D'])
	assert term.run(variants[0], passed = False) == ['HOM_REF']

	term = Term('AAF[.126,]', None)
	# .125
	assert term.run(variants[0], passed = False) == False
	assert term.run(variants[2], passed = False) == [.25]
	term = Term('AAF[,.24]', None)
	# .25
	assert term.run(variants[0], passed = False) == [.125]
	assert term.run(variants[2], passed = False) == False

	term = Term('FILTER[PASS]', None)
	assert term.run(variants[0], passed = False) == False
	assert term.run(variants[5], passed = False) == ['PASS']
예제 #8
0
def test_term_init():
	term = Term('AAF', None)
	assert term.name == 'AAF'
	assert term.term == MACROS['AAF']
	assert term.samples == None
	assert term.subsets == None

	term = Term('AAF[.05, .95]', None)
	assert term.subsets == [.05, .95]

	term = Term('1', None)
	assert term.name == '1'

	with pytest.raises(ValueError):
		Term('NoSuchTerm', None)

	MACROS['NOTYPE'] = {}
	with pytest.raises(TypeError):
		Term('NOTYPE', None)
	del MACROS['NOTYPE']

	term = Term('AFs{1}', ['sample1', 'sample2'])
	assert term.samples == [1]

	with pytest.raises(ValueError):
		Term('AFs{sample2][,0.5]', ['sample1', 'sample2'])

	term = Term('AFs{sample2}[,0.5]', ['sample1', 'sample2'])
	assert term.samples == [1]
	assert term.subsets == ['', 0.5]

	with pytest.raises(ValueError):
		Term('AFs[sample2}{,0.5}', ['sample1', 'sample2'])

	term2 = Term('AFs[,.5]{sample2}', ['sample1', 'sample2'])
	assert term2.samples == [1]
	assert term2.subsets == ['', 0.5]
	assert repr(term2) == "<Term AFs(subsets=['', 0.5], samples=[1])>"
	assert term2 == term
	assert term2 != 1

	with pytest.raises(ValueError):
		Term('AFs[)', None)

	with pytest.raises(ValueError):
		Term('AFs{sample}', ['sample1', 'sample2'])

	with pytest.raises(KeyError):
		Term('AFs[1,2,3]', None)
예제 #9
0
def test_aggr_run(variants):
	aggr = Aggr('COUNT(1, filter = FILTER[PASS], group = VARTYPE)', {'1': Term('1', None), 'FILTER[PASS]': Term('FILTER[PASS]', None), 'VARTYPE': Term('VARTYPE', None)})
	aggr.run(variants[0], passed = True)
	assert len(aggr.cache) == 0

	aggr2 = Aggr('COUNT(1, filter = FILTER[PASS])', {'1': Term('1', None), 'FILTER[PASS]': Term('FILTER[PASS]', None), 'VARTYPE': Term('VARTYPE', None)})
	with pytest.raises(RuntimeError):
		aggr2.run(variants[5], passed = True)

	aggr3 = Aggr('COUNT(1, filter = FILTER[PASS], group = FILTER2)', {'1': Term('1', None), 'FILTER[PASS]': Term('FILTER[PASS]', None), 'FILTER2': Term('FILTER2', None)})
	aggr3.run(variants[5], passed = False)
	assert len(aggr3.cache) == 0

	aggr4 = Aggr('COUNT(1, group = GTTYPEs)', {'1': Term('1', None), 'GTTYPEs': Term('GTTYPEs', None)})
	with pytest.raises(ValueError):
		aggr4.run(variants[0], passed = False)

	aggr5 = Aggr('COUNT(1, group = VARTYPE)', {'1': Term('1', None), 'VARTYPE': Term('VARTYPE', None)})
	aggr5.run(variants[0], passed = False)
	assert aggr5.cache == {'snp': [1]}
	aggr5.run(variants[1], passed = False)
	assert aggr5.cache == {'snp': [1,1]}
	aggr5.run(variants[3], passed = False)
	assert aggr5.cache == {'snp': [1,1], 'indel': [1]}

	assert aggr5.dump() == {'snp': 2, 'indel': 1}

	aggr5.cache.clear()
	aggr5.setxgroup(Term('FILTER', None))
	aggr5.run(variants[0], passed = False)
	assert aggr5.cache == {'MinMQ': {'snp': [1]}}

	aggr5.cache.clear()
	aggr5.setxgroup(Term('FILTER2', None))
	aggr5.run(variants[0], passed = False)
	assert aggr5.cache == {'MinMQ': {'snp': [1]}}
	aggr5.run(variants[5], passed = False)
	assert aggr5.cache == {'MinMQ': {'snp': [1]}}
	aggr5.run(variants[1], passed = False)
	assert aggr5.cache == {'MinMQ': {'snp': [1,1]}}
	assert aggr5.dump() == {'MinMQ': [(2, 'snp')]}

	aggr5.setxgroup(Term('GTTYPEs', ['A', 'B', 'C', 'D']))
	with pytest.raises(ValueError):
		aggr5.run(variants[0], passed = False)

	aggr6 = Aggr('MEAN(AAF[.2,], group = CHROM)', {'AAF[.2,]': Term('AAF[.2,]', None), 'CHROM': Term('CHROM', None)})
	aggr6.run(variants[0], passed = False) # .125
	assert len(aggr6.cache) == 0
예제 #10
0
def test_aggr_init():
	with pytest.raises(ValueError):
		Aggr('COUNT', {})
	with pytest.raises(ValueError):
		Aggr('COUNT(', {})
	with pytest.raises(ValueError):
		Aggr('NoSuchAggr()', {})
	aggr = Aggr('COUNT(1)', {'1': Term('1', None)})
	assert aggr.term == Term('1', None)
	assert aggr.filter is None
	assert aggr.group is None
	assert not aggr.hasFILTER()

	aggr = Aggr('COUNT(1, FILTER[PASS])', {'1': Term('1', None), 'FILTER[PASS]': Term('FILTER[PASS]', None)})
	assert aggr.term == Term('1', None)
	assert aggr.filter == Term('FILTER[PASS]', None)
	assert aggr.group is None

	aggr = Aggr('COUNT(1, filter = FILTER[PASS])', {'1': Term('1', None), 'FILTER[PASS]': Term('FILTER[PASS]', None)})
	assert aggr.term == Term('1', None)
	assert aggr.filter == Term('FILTER[PASS]', None)
	assert aggr.group is None
	assert aggr.hasFILTER()
	aggr.setxgroup(Term('VARTYPE', None))
	assert aggr.group == Term('VARTYPE', None)
	assert aggr.xgroup is None

	aggr = Aggr('COUNT(1, filter = FILTER[PASS], group = VARTYPE)', {'1': Term('1', None), 'FILTER[PASS]': Term('FILTER[PASS]', None), 'VARTYPE': Term('VARTYPE', None)})
	assert aggr.term == Term('1', None)
	assert aggr.filter == Term('FILTER[PASS]', None)
	assert aggr.group == Term('VARTYPE', None)
	assert aggr.xgroup is None
	aggr.setxgroup(Term('GTTYPEs{0}', ['A']))
	assert aggr.xgroup == Term('GTTYPEs{0}', ['A'])
	assert repr(aggr) == "<Aggr COUNT(<Term 1(subsets=None, samples=None)>, filter=<Term FILTER(subsets=['PASS'], samples=None)>, group=<Term VARTYPE(subsets=None, samples=None)>)>"
	assert aggr.hasFILTER()

	aggr = Aggr('COUNT(1, FILTER, VARTYPE)', {'1': Term('1', None), 'FILTER': Term('FILTER', None), 'VARTYPE': Term('VARTYPE', None)})
	assert aggr.term == Term('1', None)
	assert aggr.filter == Term('FILTER', None)
	assert aggr.group == Term('VARTYPE', None)

	aggr = Aggr('COUNT(1, GTTYPEs)', {'1': Term('1', None), 'GTTYPEs': Term('GTTYPEs', None)})
	assert aggr.term == Term('1', None)
	assert aggr.filter == Term('GTTYPEs', None)
	assert aggr.group == None

	with pytest.raises(TypeError):
		Aggr('COUNT(FILTER)', {'FILTER': Term('FILTER', None)})

	with pytest.raises(TypeError):
		Aggr('COUNT(1, group = AAF)', {'AAF': Term('AAF', None), '1': Term('1', None)})