Beispiel #1
0
class TestKamiCorpus(object):
    """Class for testing KAMI model."""
    def __init__(self):
        """Initialize test class with a model."""
        self.model = KamiCorpus("test")

        egfr = Protoform("P00533")
        fgfr1 = Protoform("P11362")
        fgfr1_pysite = SiteActor(
            Protoform("P11362"),
            Site("pY",
                 residues=[Residue("Y", 463, State("phosphorylation", True))]))
        abl1_sh2 = RegionActor(Protoform("P00519"), Region("SH2"))

        target = Residue("Y", 463, State("phosphorylation", False))
        modification = Modification(enzyme=egfr,
                                    substrate=fgfr1,
                                    target=target)
        binding = Binding(abl1_sh2, fgfr1_pysite)

        self.model.add_interactions([modification, binding])

    def test_empty_hierarchy(self):
        """Test getters for various model components."""
        model = KamiCorpus("test")
        assert (model.action_graph is not None)
        assert (len(model.nuggets()) == 0)
        assert (model.empty())

        model.export_json("test_empty_hierarchy.json")
        new_model = KamiCorpus.load_json("test", "test_empty_hierarchy.json")
        assert (isinstance(new_model, KamiCorpus))
        # assert(new_model._hierarchy == model._hierarchy)

        assert (model.mod_template is
                model._hierarchy.get_graph("mod_template"))
        assert (model.bnd_template is
                model._hierarchy.get_graph("bnd_template"))
        assert (model.semantic_action_graph is
                model._hierarchy.get_graph("semantic_action_graph"))

    def test_non_empty_hierarchy(self):
        """."""
        model = KamiCorpus("test")

        plcg1_pY1253 = Protoform("P19174",
                                 synonyms=["PLCG1"],
                                 residues=[
                                     Residue("Y",
                                             1253,
                                             state=State(
                                                 "phosphorylation", True))
                                 ])
        sh2 = Region(name="SH2")
        abl1 = Protoform("P00519", synonyms=["ABL1"])
        abl1_sh2 = RegionActor(abl1, sh2)
        bnd = Binding(plcg1_pY1253, abl1_sh2)

        model.add_interactions([bnd], model)
        assert (model.empty() is False)

        model.export_json("test_non_empty_hierarchy.json")
        new_model = KamiCorpus.load_json("test",
                                         "test_non_empty_hierarchy.json")
        assert (isinstance(new_model, KamiCorpus))
        assert (("test_action_graph", "meta_model")
                in new_model._hierarchy.edges())
        # print(model._hierarchy.graphs())
        # print(new_model._hierarchy.graphs())
        # assert(model._hierarchy == new_model._hierarchy)

    def test_add_gene_component(self):
        identifier = EntityIdentifier(self.model.action_graph,
                                      self.model.get_action_graph_typing())
        protoform = identifier.identify_protoform(Protoform("P00533"))

        residue = Residue("Y", 200)
        residue_id = self.model.add_residue(residue, protoform)
        assert ((residue_id, protoform) in self.model.action_graph.edges())
        for region in self.model.get_attached_regions(protoform):
            edge = get_edge(self.model.action_graph, region, protoform)
            if "start" in edge.keys() and\
               "end" in edge.keys() and\
               min(edge["start"]) < 200 and\
               max(edge["end"]) > 200:
                assert ((residue_id, region)
                        in self.model.action_graph.edges())
        for site in self.model.get_attached_sites(protoform):
            edge = get_edge(self.model.action_graph, site, protoform)
            if "start" in edge.keys() and\
               "end" in edge.keys() and\
               min(edge["start"]) < 200 and\
               max(edge["end"]) > 200:
                assert ((residue_id, site) in self.model.action_graph.edges())

        site = Site("TestSite", start=200, end=250)
        site_id = self.model.add_site(site, protoform, semantics="pY_site")

        assert ((site_id, protoform) in self.model.action_graph.edges())

        for region in self.model.get_attached_regions(protoform):
            edge = get_edge(self.model.action_graph, region, protoform)
            if "start" in edge.keys() and\
               "end" in edge.keys() and\
               min(edge["start"]) < 200 and\
               max(edge["end"]) > 250:
                assert ((site_id, region) in self.model.action_graph.edges())

        for residue in self.model.get_attached_residues(protoform):
            edge = get_edge(self.model.action_graph, residue, protoform)
            if "loc" in edge.keys() and\
               list(edge["loc"])[0] > 200 and\
               list(edge["loc"])[0] < 250:
                assert ((residue, site_id) in self.model.action_graph.edges())

        region = Region("TestRegion", start=100, end=500)
        region_id = self.model.add_region(region,
                                          protoform,
                                          semantics="protein_kinase")

        for residue in self.model.get_attached_residues(protoform):
            edge = get_edge(self.model.action_graph, residue, protoform)
            if "loc" in edge.keys() and\
               list(edge["loc"])[0] > 100 and\
               list(edge["loc"])[0] < 500:
                assert ((residue, region_id)
                        in self.model.action_graph.edges())

        for site in self.model.get_attached_sites(protoform):
            edge = get_edge(self.model.action_graph, site, protoform)
            if "start" in edge.keys() and\
               "end" in edge.keys() and\
               min(edge["start"]) > 100 and\
               max(edge["end"]) < 500:
                assert ((site, region_id) in self.model.action_graph.edges())
Beispiel #2
0
class TestIdentifiers(object):
    """Test identifiers of entities in the action graph."""
    def __init__(self):
        """Initialize with common hierarchy."""
        self.hierarchy = KamiCorpus("test")

        protoform = Protoform("A")
        self.gene_id = self.hierarchy.add_protoform(protoform)

        named_region = Region("Kinase")
        interval_region = Region(start=100, end=200)
        named_ordered_region1 = Region("SH2", order=1)
        named_ordered_region2 = Region("SH2", order=2)
        self.named_region = self.hierarchy.add_region(named_region,
                                                      self.gene_id)
        self.interval_region = self.hierarchy.add_region(
            interval_region, self.gene_id)
        self.named_ordered_region1 = self.hierarchy.add_region(
            named_ordered_region1, self.gene_id)
        self.named_ordered_region2 = self.hierarchy.add_region(
            named_ordered_region2, self.gene_id)

        named_site = Site("ATP binding")
        interval_site = Site(start=100, end=200)
        named_ordered_site1 = Site("pY", order=1)
        named_ordered_site2 = Site("pY", order=2)
        self.named_site = self.hierarchy.add_site(named_site, self.gene_id)
        self.interval_site = self.hierarchy.add_site(interval_site,
                                                     self.gene_id)
        self.named_ordered_site1 = self.hierarchy.add_site(
            named_ordered_site1, self.gene_id)
        self.named_ordered_site2 = self.hierarchy.add_site(
            named_ordered_site2, self.gene_id)

        residue = Residue("Y", 150)
        residue_no_loc = Residue("T")
        self.residue = self.hierarchy.add_residue(residue, self.gene_id)
        self.residue_no_loc = self.hierarchy.add_residue(
            residue_no_loc, self.gene_id)

        residue_state = State("activity", True)
        site_state = State("activity", True)
        region_state = State("activity", True)
        gene_state = State("activity", True)

        self.residue_state = self.hierarchy.add_state(residue_state,
                                                      self.residue)
        self.site_state = self.hierarchy.add_state(site_state, self.named_site)
        self.region_state = self.hierarchy.add_state(region_state,
                                                     self.named_region)
        self.gene_state = self.hierarchy.add_state(gene_state, self.gene_id)

    def test_identify_protoform(self):
        """Test protoform identification."""
        gene1 = Protoform("A")
        gene2 = Protoform("B")
        identifier = EntityIdentifier(self.hierarchy.action_graph,
                                      self.hierarchy.get_action_graph_typing())
        result1 = identifier.identify_protoform(gene1)
        result2 = identifier.identify_protoform(gene2)
        assert (result1 == self.gene_id)
        assert (result2 is None)

    def test_identify_region(self):
        """Test region identification."""
        identifier = EntityIdentifier(self.hierarchy.action_graph,
                                      self.hierarchy.get_action_graph_typing())
        res = identifier.identify_region(Region("Protein kinase"),
                                         self.gene_id)
        assert (res == self.named_region)
        res = identifier.identify_region(Region(start=101, end=199),
                                         self.gene_id)
        assert (res == self.interval_region)
        res = identifier.identify_region(Region("SH2"), self.gene_id)
        assert (res is None)
        res = identifier.identify_region(Region("SH2", order=1), self.gene_id)
        assert (res == self.named_ordered_region1)
        res = identifier.identify_region(Region("SH2", order=5), self.gene_id)
        assert (res is None)
        res = identifier.identify_region(
            Region("SH2", start=101, end=185, order=2), self.gene_id)
        assert (res == self.interval_region)

    def test_identify_site(self):
        """Test site identification."""
        identifier = EntityIdentifier(self.hierarchy.action_graph,
                                      self.hierarchy.get_action_graph_typing())
        res = identifier.identify_site(Site("ATP bind"), self.gene_id)
        assert (res == self.named_site)
        res = identifier.identify_site(Site("ATP binding site"), self.gene_id)
        assert (res == self.named_site)

        res = identifier.identify_site(Site(start=101, end=199), self.gene_id)
        assert (res == self.interval_site)
        res = identifier.identify_site(Site("pY"), self.gene_id)
        assert (res is None)
        res = identifier.identify_site(Site("pY", order=1), self.gene_id)
        assert (res == self.named_ordered_site1)
        res = identifier.identify_site(Site("pY", order=5), self.gene_id)
        assert (res is None)
        res = identifier.identify_site(Site("pY", start=101, end=185, order=2),
                                       self.gene_id)
        assert (res == self.interval_site)

    def test_identify_residue(self):
        """Test residue identification."""
        identifier = EntityIdentifier(self.hierarchy.action_graph,
                                      self.hierarchy.get_action_graph_typing())
        res = identifier.identify_residue(Residue("S", 150), self.gene_id)
        assert (res == self.residue)
        res = identifier.identify_residue(Residue("T"), self.gene_id)
        assert (res == self.residue_no_loc)
        res = identifier.identify_residue(Residue("S"), self.gene_id)
        assert (res is None)

    def test_identify_state(self):
        """Test state identification."""
        identifier = EntityIdentifier(self.hierarchy.action_graph,
                                      self.hierarchy.get_action_graph_typing())
        res = identifier.identify_state(State("activity", False), self.residue)
        assert (res == self.residue_state)
        res = identifier.identify_state(State("activity", False),
                                        self.named_site)
        assert (res == self.site_state)
        res = identifier.identify_state(State("activity", False),
                                        self.named_region)
        assert (res == self.region_state)
        res = identifier.identify_state(State("activity", False), self.gene_id)
        assert (res == self.gene_state)