def test_oneLineage_returnsOnlyElement(self):
        l1 = Lineage(major="4", minor="1.3.4")
        classifier = Classifier()
        sample_idx = 0

        actual = classifier.call_sample_lineage([l1], sample_idx)
        expected = str(l1)

        assert actual == expected
    def test_emptyList_returnsNone(self):
        lineages = []
        classifier = Classifier()
        sample_idx = 0

        expected = "unknown"
        actual = classifier.call_sample_lineage(lineages, sample_idx, default=expected)

        assert actual == expected
    def test_variantRefDoesntMatchPanel_returnsFalse(self, mock_variant):
        index = {
            1: PanelVariant(Lineage("1"), 1, "G", "A"),
        }
        classifier = Classifier(index)
        mock_variant.POS = 1
        mock_variant.REF = "d"
        mock_variant.FILTER = None

        assert not classifier.is_variant_valid(mock_variant)
    def test_oneLineageTooManyHets_returnsTooManyHets(self):
        l1 = Lineage(major="4", minor="1.3.4")
        classifier = Classifier(max_het=1)
        sample_idx = 0
        classifier.het_counts[sample_idx] = 5

        actual = classifier.call_sample_lineage([l1], sample_idx)
        expected = "too_many_hets"

        assert actual == expected
    def test_twoLineagesSameMajor_returnsLowestOnTree(self):
        l1 = Lineage(major="4", minor="1.3.4")
        l2 = Lineage(major="4", minor="8.3")
        classifier = Classifier()
        sample_idx = 0

        actual = classifier.call_sample_lineage([l1, l2], sample_idx)
        expected = str(l1)

        assert actual == expected
    def test_twoLineagesDiffMajor_returnsMixedDisregardMaxAlt(self):
        l1 = Lineage(major="4", minor="1.3.4")
        l2 = Lineage(major="5", minor="8.3")
        classifier = Classifier(max_alt_lineages=5)
        sample_idx = 0

        actual = classifier.call_sample_lineage([l1, l2], sample_idx)
        expected = "mixed"

        assert actual == expected
    def test_posNotInPanel_returnsFalse(self, mock_variant):
        index = {
            1: PanelVariant(Lineage("1"), 1, "G", "A"),
        }
        classifier = Classifier(index)
        mock_variant.POS = 4
        mock_variant.REF = "G"
        mock_variant.FILTER = None

        assert not classifier.is_variant_valid(mock_variant)
    def test_panelVariantFailsFilter_returnsFalse(self, mock_variant):
        index = {
            1: PanelVariant(Lineage("1"), 1, "G", "A"),
        }
        classifier = Classifier(index)
        mock_variant.POS = 1
        mock_variant.REF = "G"
        mock_variant.ALT = ["T", "A"]
        mock_variant.FILTER = "FAIL"

        assert not classifier.is_variant_valid(mock_variant)
    def test_refLineagePosPanelRefInAlt_returnsTrue(self, mock_variant):
        index = {
            1: PanelVariant(Lineage("1"), 1, "G", "A"),
        }
        classifier = Classifier(index)
        mock_variant.POS = 1
        mock_variant.REF = "G"
        mock_variant.ALT = ["T", "C"]
        mock_variant.FILTER = None

        assert not classifier.is_variant_valid(mock_variant)
    def test_panelVariantAltNotInVariantAlts_returnsFalse(self, mock_variant):
        index = {
            1: PanelVariant(Lineage("1"), 1, "G", "A"),
        }
        classifier = Classifier(index)
        mock_variant.POS = 1
        mock_variant.REF = "G"
        mock_variant.ALT = ["T", "C"]
        mock_variant.FILTER = None

        assert not classifier.is_variant_valid(mock_variant)
    def test_mixedLineagesBelowThreshold_returnsMostCommon(self):
        l1 = Lineage(major="4", minor="1.3.4")
        l2 = Lineage(major="4", minor="1.3")
        l3 = Lineage(major="5", minor="8.3")
        classifier = Classifier(max_alt_lineages=1)
        sample_idx = 0

        actual = classifier.call_sample_lineage([l1, l2, l3], sample_idx)
        expected = str(l1)

        assert actual == expected
    def test_panelVariantAltIsNotVariantRef_returnsFalse(self, mock_variant):
        pos = 1692141
        index = {
            pos: PanelVariant(Lineage.from_str("4.10"), position=pos, ref="C", alt="T"),
        }
        classifier = Classifier(index, ref_lineage_position=pos)
        mock_variant.POS = pos
        mock_variant.REF = "A"
        mock_variant.ALT = ["C"]
        mock_variant.FILTER = None

        assert not classifier.is_variant_valid(mock_variant)
    def test_hetSampleDoesntHaveLineageVariant_returnsEmpty(self, mocked_variant):
        index = {
            1: PanelVariant(Lineage("1"), 1, "G", "A"),
        }
        classifier = Classifier(index)
        mocked_variant.POS = 1
        mocked_variant.REF = "G"
        mocked_variant.ALT = ["T", "A", "N"]
        mocked_variant.genotypes = [[-1, -1, False], [1, 3, False]]
        mocked_variant.format.side_effect = KeyError

        actual = classifier.samples_with_lineage_variant(mocked_variant)
        expected = []

        assert actual == expected
    def test_allSamplesHaveLineageVariant_returnsAllIndices(self, mocked_variant):
        index = {
            1: PanelVariant(Lineage("1"), 1, "G", "A"),
        }
        classifier = Classifier(index)
        mocked_variant.POS = 1
        mocked_variant.REF = "G"
        mocked_variant.ALT = ["T", "A"]
        mocked_variant.genotypes = [[2, -1, False], [2, 2, False]]
        mocked_variant.format.side_effect = KeyError

        actual = classifier.samples_with_lineage_variant(mocked_variant)
        expected = [0, 1]

        assert actual == expected
    def test_posNotInPanel_returnsEmpty(self, mocked_variant):
        index = {
            4: PanelVariant(Lineage("1"), 1, "G", "A"),
        }
        classifier = Classifier(index)
        mocked_variant.POS = 1
        mocked_variant.REF = "G"
        mocked_variant.ALT = ["T", "A"]
        mocked_variant.genotypes = [[2, -1, False], [2, 2, False]]
        mocked_variant.format.side_effect = KeyError

        actual = classifier.samples_with_lineage_variant(mocked_variant)
        expected = []

        assert actual == expected
    def test_sampleHasLineageVariantAndPassesFilter_returnsIndex(self, mocked_variant):
        index = {
            1: PanelVariant(Lineage("1"), 1, "G", "A"),
        }
        classifier = Classifier(index)
        mocked_variant.POS = 1
        mocked_variant.REF = "G"
        mocked_variant.ALT = ["T", "A", "N"]
        mocked_variant.genotypes = [[-1, -1, False], [2, 2, False]]
        mocked_variant.format.return_value = ["PASS", "PASS"]

        actual = classifier.samples_with_lineage_variant(mocked_variant)
        expected = [1]

        assert actual == expected
    def test_hetSampleHasLineageVariantIncludeHetAndIncrementHetCount_returnsHetIndex(
        self, mocked_variant
    ):
        index = {
            1: PanelVariant(Lineage("1"), 1, "G", "A"),
        }
        classifier = Classifier(index)
        mocked_variant.POS = 1
        mocked_variant.REF = "G"
        mocked_variant.ALT = ["T", "A"]
        mocked_variant.genotypes = [[-1, -1, False], [2, 0, False]]
        mocked_variant.format.side_effect = KeyError

        actual = classifier.samples_with_lineage_variant(mocked_variant)
        expected = [1]

        assert actual == expected
        assert classifier.het_counts[1] == 1
    def test_panelVariantAltIsVariantRefForRefLineagePos_returnsIndex(
        self, mock_variant
    ):
        pos = 1692141
        index = {
            pos: PanelVariant(Lineage.from_str("4.10"), position=pos, ref="C", alt="A"),
        }
        classifier = Classifier(index, ref_lineage_position=pos)
        mock_variant.POS = pos
        mock_variant.REF = "A"
        mock_variant.ALT = ["C"]
        mock_variant.genotypes = [[0, 0, False], [1, 1, False]]
        mock_variant.format.return_value = ["PASS", "PASS"]

        actual = classifier.samples_with_lineage_variant(mock_variant)
        expected = [0]

        assert actual == expected