Example #1
0
def test_get_children_stats(f1_trio, person_set_id, male, female, unspecified,
                            count):
    psc = f1_trio.get_person_set_collection("phenotype")
    gh = GenotypeHelper(f1_trio, psc)

    children_stats = gh.get_children_stats(person_set_id)

    assert len(children_stats) == count
    assert children_stats["M"] == male
    assert children_stats["F"] == female
    assert children_stats["U"] == unspecified

    children_stats = gh.get_children_stats(person_set_id)
    assert len(children_stats) == count
Example #2
0
    def __init__(
            self, dataset, enrichment_tool, gene_syms):
        self.dataset = dataset
        self.gene_syms = gene_syms
        self.tool = enrichment_tool
        self.results = None
        enrichment_config = self.tool.config
        assert enrichment_config is not None
        effect_types = expand_effect_types(enrichment_config.effect_types)

        self.person_set_collection = self.dataset.get_person_set_collection(
            enrichment_config.selected_person_set_collections[0]
        )

        self.gh = GenotypeHelper(
            self.dataset, self.person_set_collection,
            effect_types=effect_types)
Example #3
0
def test_enrichment_tool(f1_trio, f1_trio_enrichment_config,
                         f1_trio_coding_len_background):
    variants = list(
        f1_trio.query_variants(inheritance=str(Inheritance.denovo.name)))
    event_counter = EventsCounter()
    enrichment_tool = EnrichmentTool(f1_trio_enrichment_config,
                                     f1_trio_coding_len_background,
                                     event_counter)
    psc = f1_trio.get_person_set_collection("phenotype")
    gh = GenotypeHelper(f1_trio, psc)
    # children_stats = gh.get_children_stats()
    children_by_sex = gh.children_by_sex("phenotype1")

    enrichment_events = enrichment_tool.calc(
        ["missense", "synonymous"],
        ["SAMD11", "PLEKHN1", "POGZ"],
        variants,
        children_by_sex,
    )

    assert len(enrichment_events["all"].events) == 2
    assert enrichment_events["all"].events == [["SAMD11"], ["SAMD11"]]
    assert enrichment_events["all"].expected == 2.0
    assert enrichment_events["all"].pvalue == 1.0
    assert len(enrichment_events["rec"].events) == 1
    assert enrichment_events["rec"].events == [["SAMD11"]]
    assert enrichment_events["rec"].expected == 1.0
    assert enrichment_events["rec"].pvalue == 1.0
    assert len(enrichment_events["male"].events) == 1
    assert enrichment_events["male"].events == [["SAMD11"]]
    assert enrichment_events["male"].expected == 1.0
    assert enrichment_events["male"].pvalue == 1.0
    assert len(enrichment_events["female"].events) == 1
    assert enrichment_events["female"].events == [["SAMD11"]]
    assert enrichment_events["female"].expected == 1.0
    assert enrichment_events["female"].pvalue == 1.0
    assert len(enrichment_events["unspecified"].events) == 0
    assert enrichment_events["unspecified"].events == []
    assert enrichment_events["unspecified"].expected is None
    assert enrichment_events["unspecified"].pvalue is None
Example #4
0
def test_events_counter(f1_trio):
    variants = list(
        f1_trio.query_variants(inheritance=str(Inheritance.denovo.name))
    )
    psc = f1_trio.get_person_set_collection("phenotype")
    gh = GenotypeHelper(f1_trio, psc)
    # children_stats = gh.get_children_stats()
    children_by_sex = gh.children_by_sex("phenotype1")
    effect_types = set(["missense", "synonymous"])
    event_counter = EventsCounter()
    print(children_by_sex)
    events = event_counter.events(variants, children_by_sex, effect_types)
    print(events)

    assert len(events["all"].events) == 2
    assert events["all"].events == [["SAMD11"], ["SAMD11"]]
    assert len(events["rec"].events) == 1
    assert events["rec"].events == [["SAMD11"]]
    assert len(events["male"].events) == 1
    assert events["male"].events == [["SAMD11"]]
    assert len(events["female"].events) == 1
    assert events["female"].events == [["SAMD11"]]
    assert len(events["unspecified"].events) == 0
def test_calc_stats(f1_trio, f1_trio_coding_len_background):

    variants = list(
        f1_trio.query_variants(inheritance=str(Inheritance.denovo.name)))

    for fv in variants:
        print(80 * "-")
        print(fv, fv.effects)
        for fa in fv.alleles:
            print("\t", fa, fa.effects)

    event_counter = EventsCounter()

    psc = f1_trio.get_person_set_collection("phenotype")
    gh = GenotypeHelper(f1_trio, psc)
    children_stats = gh.get_children_stats("phenotype1")
    children_by_sex = gh.children_by_sex("phenotype1")

    enrichment_events = event_counter.events(variants, children_by_sex,
                                             set(["missense", "synonymous"]))

    assert len(enrichment_events["all"].events) == 2
    assert enrichment_events["all"].events == [["SAMD11"], ["SAMD11"]]
    assert enrichment_events["all"].expected is None
    assert enrichment_events["all"].pvalue is None
    assert len(enrichment_events["rec"].events) == 1
    assert enrichment_events["rec"].events == [["SAMD11"]]
    assert enrichment_events["rec"].expected is None
    assert enrichment_events["rec"].pvalue is None
    assert len(enrichment_events["male"].events) == 1
    assert enrichment_events["male"].events == [["SAMD11"]]
    assert enrichment_events["male"].expected is None
    assert enrichment_events["male"].pvalue is None
    assert len(enrichment_events["female"].events) == 1
    assert enrichment_events["female"].events == [["SAMD11"]]
    assert enrichment_events["female"].expected is None
    assert enrichment_events["female"].pvalue is None
    assert len(enrichment_events["unspecified"].events) == 0
    assert enrichment_events["unspecified"].events == []
    assert enrichment_events["unspecified"].expected is None
    assert enrichment_events["unspecified"].pvalue is None

    ee = f1_trio_coding_len_background.calc_stats(
        ["missense", "synonymous"],
        enrichment_events,
        ["SAMD11", "PLEKHN1", "POGZ"],
        children_stats,
    )

    assert ee == enrichment_events

    assert len(ee["all"].events) == 2
    assert ee["all"].events == [
        ["SAMD11"],
        ["SAMD11"],
    ]
    assert ee["all"].expected == 2.0
    assert ee["all"].pvalue == 1.0
    assert len(ee["rec"].events) == 1
    assert ee["rec"].events == [["SAMD11"]]
    assert ee["rec"].expected == 1.0
    assert ee["rec"].pvalue == 1.0
    assert len(ee["male"].events) == 1
    assert ee["male"].events == [["SAMD11"]]
    assert ee["male"].expected == 1.0
    assert ee["male"].pvalue == 1.0
    assert len(ee["female"].events) == 1
    assert ee["female"].events == [["SAMD11"]]
    assert ee["female"].expected == 1.0
    assert ee["female"].pvalue == 1.0
    assert len(ee["unspecified"].events) == 0
    assert ee["unspecified"].events == []
    assert ee["unspecified"].expected is None
    assert ee["unspecified"].pvalue is None
def test_calc_stats(f1_trio, f1_trio_samocha_background):
    variants = list(
        f1_trio.query_variants(inheritance=str(Inheritance.denovo.name)))
    event_counter = EventsCounter()

    psc = f1_trio.get_person_set_collection("phenotype")
    gh = GenotypeHelper(f1_trio, psc)
    # children_stats = gh.get_children_stats()
    children_by_sex = gh.children_by_sex("phenotype1")

    enrichment_events = event_counter.events(variants, children_by_sex,
                                             set(["missense", "synonymous"]))

    assert len(enrichment_events["all"].events) == 2
    assert enrichment_events["all"].events == [["SAMD11"], ["SAMD11"]]
    assert enrichment_events["all"].expected is None
    assert enrichment_events["all"].pvalue is None
    assert len(enrichment_events["rec"].events) == 1
    assert enrichment_events["rec"].events == [["SAMD11"]]
    assert enrichment_events["rec"].expected is None
    assert enrichment_events["rec"].pvalue is None
    assert len(enrichment_events["male"].events) == 1
    assert enrichment_events["male"].events == [["SAMD11"]]
    assert enrichment_events["male"].expected is None
    assert enrichment_events["male"].pvalue is None
    assert len(enrichment_events["female"].events) == 1
    assert enrichment_events["female"].events == [["SAMD11"]]
    assert enrichment_events["female"].expected is None
    assert enrichment_events["female"].pvalue is None
    assert len(enrichment_events["unspecified"].events) == 0
    assert enrichment_events["unspecified"].events == []
    assert enrichment_events["unspecified"].expected is None
    assert enrichment_events["unspecified"].pvalue is None

    ee = f1_trio_samocha_background.calc_stats(
        "missense",
        enrichment_events,
        ["SAMD11", "PLEKHN1", "POGZ"],
        children_by_sex,
    )

    assert ee == enrichment_events

    assert len(ee["all"].events) == 2
    assert ee["all"].events == [["SAMD11"], ["SAMD11"]]
    assert ee["all"].expected == 30.0
    assert ee["all"].pvalue == pytest.approx(9.002e-11)
    assert len(ee["rec"].events) == 1
    assert ee["rec"].events == [["SAMD11"]]
    assert ee["rec"].expected == 15.0
    assert ee["rec"].pvalue == pytest.approx(9.788e-6, rel=1e-3)
    assert len(ee["male"].events) == 1
    assert ee["male"].events == [["SAMD11"]]
    assert ee["male"].expected == 15.0
    assert ee["male"].pvalue == pytest.approx(9.788e-06, rel=1e-3)
    assert len(ee["female"].events) == 1
    assert ee["female"].events == [
        ["SAMD11"],
    ]
    assert ee["female"].expected == 15.0
    assert ee["female"].pvalue == pytest.approx(9.788e-06, rel=1e-3)
    assert len(ee["unspecified"].events) == 0
    assert ee["unspecified"].events == []
    assert ee["unspecified"].expected is None
    assert ee["unspecified"].pvalue is None
Example #7
0
class EnrichmentBuilder(BaseEnrichmentBuilder):
    def __init__(
            self, dataset, enrichment_tool, gene_syms):
        self.dataset = dataset
        self.gene_syms = gene_syms
        self.tool = enrichment_tool
        self.results = None
        enrichment_config = self.tool.config
        assert enrichment_config is not None
        effect_types = expand_effect_types(enrichment_config.effect_types)

        self.person_set_collection = self.dataset.get_person_set_collection(
            enrichment_config.selected_person_set_collections[0]
        )

        self.gh = GenotypeHelper(
            self.dataset, self.person_set_collection,
            effect_types=effect_types)

    def build_people_group_selector(self, effect_types, person_set):
        children_stats = self.gh.get_children_stats(person_set.id)
        children_count = (
            children_stats["M"] + children_stats["F"] + children_stats["U"]
        )

        if children_count <= 0:
            return None

        results = {}
        for effect_type in effect_types:
            enrichment_results = self.tool.calc(
                effect_type,
                self.gene_syms,
                self.gh.get_denovo_variants(),
                self.gh.children_by_sex(person_set.id),
            )

            results[effect_type] = enrichment_results
        results["childrenStats"] = self.gh.get_children_stats(person_set.id)
        results["selector"] = person_set.name
        results["geneSymbols"] = list(self.gene_syms)
        results["peopleGroupId"] = self.person_set_collection.id
        results["peopleGroupValue"] = person_set.id
        results["datasetId"] = self.dataset.study_id

        return results

    def _build_results(self):
        results = []
        enrichment_config = self.tool.config
        assert enrichment_config is not None

        effect_types = enrichment_config.effect_types

        for person_set in self.person_set_collection.person_sets.values():
            res = self.build_people_group_selector(effect_types, person_set)
            if res:
                results.append(res)

        return results

    def build(self):
        results = self._build_results()

        serializer = EnrichmentSerializer(self.tool.config, results)
        results = serializer.serialize()

        self.results = results
        return self.results