def test_complicated_site_actor(self): m = Modification(enzyme=SiteActor(protoform=Protoform( "P00519", sites=[Site(name="bob", start=120, end=150)], regions=[Region(name="alice", start=100, end=200)], residues=[Residue("Y", 122)]), site=Site(name="jack", start=550, end=600, residues=[Residue("T")]), region=Region( name="Pkinase", start=500, end=800, sites=[Site(name="billy")])), substrate=SiteActor(protoform=Protoform("P00533"), site=Site(name="target"), region=Region(name="bla")), target=Residue("Y", 100, State("phosphorylation", False)), value=True) corpus = KamiCorpus("test") corpus.add_interactions([m], anatomize=True) print_graph(corpus.get_nugget("test_nugget_1")) print(ag_to_edge_list(corpus))
def test_regionactor(self): # Phosphorylated and unphosphrylated states. phos = State("phosphorylation", True) unphos = State("phosphorylation", False) inters = [] # Phosphorylation with RegionActor as substrate. m = Modification(enzyme=Protoform("P00519"), substrate=RegionActor( Protoform("P00533", regions=[ Region(name='PVPEyINQS', start=280, end=290), Region(name="L receptor", start=57, end=167, states=[phos]) ]), Region(start=1000, end=1500)), target=Residue(aa="Y", loc=1092, state=unphos), value=True) inters.append(m) corpus = KamiCorpus("test") corpus.add_interactions(inters, anatomize=True) print_graph(corpus.get_nugget("test_nugget_1"))
def test_siteactor(self): """Generate sh2 pY bnd interactions.""" # General phosphorylation state. phos = State("phosphorylation", True) unphos = State("phosphorylation", False) # General SH2 regions. sh2 = Region(name="SH2") sh2n = Region(name="SH2", order=1) sh2c = Region(name="SH2", order=2) inters = [] enz_uniprot = 'P00519' sub_uniprot = 'P00533' rsd = 'Y1092' location = int(rsd[1:]) strt = location - 4 stop = location + 4 site_name = 'b%i' % location m = Modification(enzyme=Protoform(enz_uniprot), substrate=SiteActor(protoform=Protoform(sub_uniprot), site=Site(name=site_name, start=strt, end=stop)), target=Residue(aa="Y", loc=location, state=unphos), value=True) inters.append(m) corpus = KamiCorpus("test") corpus.add_interactions(inters, anatomize=True)
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())
def test_advanced_site_actor(self): # General phosphorylation state. phos = State("phosphorylation", True) unphos = State("phosphorylation", False) # General SH2 regions. sh2 = Region(name="SH2") sh2n = Region(name="SH2", order=1) sh2c = Region(name="SH2", order=2) # Test site. s1 = Site(name="test_site") inters = [] # Modification using SiteActor. m = Modification(enzyme=Protoform("P00519", hgnc_symbol="ABL1"), substrate=SiteActor(protoform=Protoform( "P00519", hgnc_symbol="ABL1"), site=Site(name="site800", start=796, end=804)), target=Residue("Y", 800, State("phosphorylation", False)), value=True) inters.append(m) m = Modification(enzyme=Protoform("P00519", hgnc_symbol="ABL1"), substrate=SiteActor(protoform=Protoform( "P00533", hgnc_symbol="EGFR"), site=Site(name="site800", start=796, end=804)), target=Residue("Y", 800, State("phosphorylation", False)), value=True) inters.append(m) # Binding using SiteActor. b = Binding( SiteActor(protoform=Protoform("P00533", hgnc_symbol="EGFR"), site=Site(name="site800", start=796, end=804)), RegionActor(protoform=Protoform("P62993", hgnc_symbol="GRB2"), region=sh2)) inters.append(b) corpus = KamiCorpus("test") corpus.add_interactions(inters, anatomize=True)
def test_sites(self): # Create genes. egfr = Protoform("P00533") grb2 = Protoform("P62993") # Create a RegionActor and a SiteActor fo GRB2. grb2_sh2 = RegionActor(protoform=grb2, region=Region(name="SH2")) grb2_site = SiteActor(protoform=grb2, site=Site(name="pY")) inters = [] # This works (RegionActor). # inters.append(BinaryBinding([egfr], [grb2_sh2])) # This does not work (SiteActor) inters.append(Binding(egfr, grb2_site)) corpus = KamiCorpus("test") corpus.add_interactions(inters, anatomize=True) print_graph(corpus.get_nugget("test_nugget_1"))
def test_bookkeeping(self): interactions = [] i1 = LigandModification( RegionActor(Protoform("P00533"), Region(start=712, end=979)), Protoform("P00533"), Residue("Y", 1092, state=State("phosphorylation", False))) interactions.append(i1) i2 = Modification( Protoform("P00519"), Protoform("P00533"), Residue("S", 800, state=State("phosphorylation", False))) interactions.append(i2) i3 = Binding(Protoform("P00424"), SiteActor(Protoform("P00533"), Site(start=799, end=900))) interactions.append(i3) corpus = KamiCorpus("test") corpus.add_interactions(interactions)
def test_multiple_sh2(self): """.""" phos = State("phosphorylation", True) sh2n = Region(name="SH2", order=1) sh2c = Region(name="SH2", order=2) pik3r1 = Protoform("P27986", synonyms=["PIK3R1", "PI3K1"]) pik3r1_sh2n = RegionActor(pik3r1, sh2n) pik3r1_sh2c = RegionActor(pik3r1, sh2c) frs2_py196 = Protoform("Q8WU20", synonyms=["FRS2"], residues=[Residue("Y", 196, phos)]) frs2_py349 = Protoform("Q8WU20", synonyms=["FRS2"], residues=[Residue("Y", 349, phos)]) bnds = [] bnds.append(Binding(frs2_py196, pik3r1_sh2n)) bnds.append(Binding(frs2_py349, pik3r1_sh2c)) corpus = KamiCorpus("test") corpus.add_interactions(bnds, anatomize=True)
def test_phospho_semantics(self): """Test black box processing using phosphorylation semantics.""" mek1 = Protoform("Q02750") stat3 = Protoform("P40763") mod_state = Residue("S", 727, State("phosphorylation", False)) value = True mod1 = Modification(mek1, stat3, mod_state, value) mod_state_1 = Residue("Y", 705, State("phosphorylation", False)) mod2 = Modification(mek1, stat3, mod_state_1, value) erk1 = Protoform("P27361") mod_state_2 = Residue("T", 201, State("phosphorylation", False)) mod3 = Modification(mek1, erk1, mod_state_2, value) erk2 = Protoform("P28482") mod_state_3 = Residue("T", 182, State("phosphorylation", False)) mod4 = Modification(mek1, erk2, mod_state_3, value) interactions = [mod1, mod2, mod3, mod4] corpus = KamiCorpus("test") corpus.add_interactions(interactions, add_agents=True, anatomize=True) print(corpus.action_graph.nodes()) print( corpus._hierarchy.get_relation("test_action_graph", "semantic_action_graph")) print(corpus) print_graph(corpus.action_graph) print( corpus._hierarchy.get_relation("test_action_graph", "semantic_action_graph"))
class TestKamiQL: """Unit tests for KamiQL.""" def __init__(self): """Initialize tests.""" # Create an empty KAMI corpus self.nxcorpus = KamiCorpus("EGFR_signalling") try: h = Neo4jHierarchy(uri="bolt://localhost:7687", user="******", password="******") h._clear() self.neo4jcorpus = KamiCorpus("egfr", backend="neo4j", uri="bolt://localhost:7687", user="******", password="******") except: warnings.warn("Neo4j is down, skipping Neo4j-related tests") self.neo4jcorpus = None # Create an interaction object egfr = Protoform("P00533") egf = Protoform("P01133") kinase = Region(name="Protein kinase", start=712, end=979, states=[State("activity", True)]) egfr_kinase = RegionActor(protoform=egfr, region=kinase) interaction1 = LigandModification( enzyme=egfr_kinase, substrate=egfr, target=Residue("Y", 1092, state=State("phosphorylation", False)), value=True, rate=1, desc="Phosphorylation of EGFR homodimer") # Aggregate the interaction object to the corpus self.nxcorpus.add_interaction(interaction1) self.neo4jcorpus.add_interaction(interaction1) grb2 = Protoform("P62993", states=[State("activity", True)]) grb2_sh2 = RegionActor(protoform=grb2, region=Region(name="SH2")) shc1 = Protoform("P29353") shc1_pY = SiteActor( protoform=shc1, site=Site( name="pY", residues=[Residue("Y", 317, State("phosphorylation", True))])) interaction1 = Binding(grb2_sh2, shc1_pY) grb2_sh2_with_residues = RegionActor(protoform=grb2, region=Region( name="SH2", residues=[ Residue("S", 90, test=True), Residue("D", 90, test=False) ])) egfr_pY = SiteActor( protoform=egfr, site=Site( name="pY", residues=[Residue("Y", 1092, State("phosphorylation", True))])) interaction2 = Binding(grb2_sh2_with_residues, egfr_pY) axl_PK = RegionActor(protoform=Protoform("P30530", hgnc_symbol="AXL"), region=Region("Protein kinase", start=536, end=807)) interaction3 = SelfModification(axl_PK, target=Residue( "Y", 821, State("phosphorylation", False)), value=True) interaction4 = AnonymousModification(RegionActor(protoform=Protoform( "P30530", hgnc_symbol="AXL", residues=[ Residue("Y", 703, state=State("phosphorylation", True)), Residue("Y", 779, state=State("phosphorylation", True)) ]), region=Region( "Protein kinase", start=536, end=807)), target=State("activity", False), value=True) egf_egfr = Protoform(egfr.uniprotid, bound_to=[egf]) interaction5 = Binding(egf_egfr, egf_egfr) interaction6 = Unbinding(egf_egfr, egf_egfr) interaction7 = LigandModification( egfr_kinase, shc1, target=Residue("Y", 317, State("phosphorylation", False)), value=True, enzyme_bnd_region=Region("egfr_BND"), enzyme_bnd_site=Site("egfr_BND"), substrate_bnd_region=Region("shc1_BND"), substrate_bnd_site=Site("sch1_BND")) interactions = [ interaction1, interaction2, interaction3, interaction4, interaction5, interaction6, interaction7 ] self.nxcorpus.add_interactions(interactions) self.neo4jcorpus.add_interactions(interactions) # Create a protein definition for GRB2 # protoform = Protoform( # "P62993", # regions=[Region( # name="SH2", # residues=[ # Residue("S", 90, test=True), # Residue("D", 90, test=False)])]) # ashl = Product("Ash-L", residues=[Residue("S", 90)]) # s90d = Product("S90D", residues=[Residue("D", 90)]) # grb3 = Product("Grb3", removed_components={"regions": [Region("SH2")]}) # self.grb2_definition = Definition(protoform, [ashl, s90d, grb3]) # self.model = self.nxcorpus.instantiate( # "EGFR_signalling_GRB2", [self.grb2_definition], # default_bnd_rate=0.1, # default_brk_rate=0.1, # default_mod_rate=0.1) self.query1 = (""" MATCH (:protoform)<--(r1:REGION)-->(i:interaction)-*-(n4:protoform) RETURN p1, i, p2; """) # def test_nx_ag_queries(self): # """Test queries on the action graph.""" # engine = KamiQLEngine(self.nxcorpus) # start_time = time.time() # instances = engine.query_action_graph(self.query1) # print("NX time: ", time.time() - start_time) # print(instances) # print() def test_neo4j_ag_queries(self): """Test queries on the action graph.""" engine = KamiQLEngine(self.neo4jcorpus) start_time = time.time() instances = engine.query_action_graph(self.query1) print("Neo4j time: ", time.time() - start_time) print(instances) print()
interaction5 = Binding(egf_egfr, egf_egfr) interaction6 = Unbinding(egf_egfr, egf_egfr) interaction7 = LigandModification(egfr_kinase, shc1, target=Residue( "Y", 317, State("phosphorylation", False)), value=True, enzyme_bnd_region=Region("egfr_BND"), enzyme_bnd_site=Site("egfr_BND"), substrate_bnd_region=Region("shc1_BND"), substrate_bnd_site=Site("sch1_BND")) nuggets = TEST_CORPUS.add_interactions([ interaction1, interaction2, interaction3, interaction4, interaction5, interaction6, interaction7 ]) # Create a protein definition for GRB2 protoform = Protoform("P62993", regions=[ Region(name="SH2", residues=[ Residue("S", 90, test=True), Residue("D", 90, test=False) ]) ]) ashl = Product("Ash-L", residues=[Residue("S", 90)]) s90d = Product("S90D", residues=[Residue("D", 90)]) grb3 = Product("Grb3", removed_components={"regions": [Region("SH2")]})
def test_sh2_py_semantics(self): """.""" phos = State("phosphorylation", True) dok1_py398 = Protoform("Q99704", synonyms=["DOK1", "p62DOK1"], residues=[Residue("Y", 398, phos)]) abl2 = Protoform("P42684", synonyms=["ABL2"]) sh2 = Region(name="SH2") abl2_sh2 = RegionActor(abl2, sh2) bnd = Binding(dok1_py398, abl2_sh2) corpus = KamiCorpus("test") nugget_id = corpus.add_interaction(bnd) semantic_entities = [ "sh2_domain", "sh2_domain_pY_bnd", "pY_site", "pY_residue", "phosphorylation" ] print(corpus.get_nugget(nugget_id).nodes()) assert ("pY_site" in corpus.get_nugget(nugget_id).nodes()) assert ("pY_residue" in corpus.get_nugget(nugget_id).nodes()) assert ("pY_residue_phospho" in corpus.get_nugget(nugget_id).nodes()) assert ((nugget_id, "sh2_pY_binding_semantic_nugget") in corpus.nugget_relations()) for entity in semantic_entities: assert (entity in corpus._hierarchy.get_relation( "sh2_pY_binding_semantic_nugget", nugget_id).keys()) site_actor_no_residue = SiteActor(Protoform("A"), Site("pY-site", start=100, end=150)) bnd = Binding(abl2_sh2, site_actor_no_residue) nugget_id = corpus.add_interaction(bnd) assert (len(corpus.get_nugget(nugget_id).nodes()) == 7) binding_nodes = [] for n in corpus.action_graph.nodes(): if corpus.get_action_graph_typing()[n] == "bnd": binding_nodes.append(n) assert (len(binding_nodes) == 1) assert ((nugget_id, "sh2_pY_binding_semantic_nugget") in corpus.nugget_relations()) for entity in semantic_entities: assert (entity in corpus._hierarchy.get_relation( "sh2_pY_binding_semantic_nugget", nugget_id).keys()) site_actor_no_phospho = SiteActor( Protoform("A"), Site("pY-site", start=100, end=150, residues=[Residue("Y")])) bnd = Binding(abl2_sh2, site_actor_no_phospho) nugget_id = corpus.add_interaction(bnd) assert (len(corpus.get_nugget(nugget_id).nodes()) == 8) binding_nodes = [] for n in corpus.action_graph.nodes(): if corpus.get_action_graph_typing()[n] == "bnd": binding_nodes.append(n) assert (len(binding_nodes) == 1) assert ((nugget_id, "sh2_pY_binding_semantic_nugget") in corpus.nugget_relations()) for entity in semantic_entities: assert (entity in corpus._hierarchy.get_relation( "sh2_pY_binding_semantic_nugget", nugget_id).keys()) site_actor_with_residue = SiteActor( Protoform("A"), Site("pY-site", start=100, end=150, residues=[ Residue("Y", loc=145, state=State("phosphorylation")) ])) bnd = Binding(abl2_sh2, site_actor_with_residue) nugget_id = corpus.add_interaction(bnd) assert (len(corpus.get_nugget(nugget_id).nodes()) == 7) binding_nodes = [] for n in corpus.action_graph.nodes(): if corpus.get_action_graph_typing()[n] == "bnd": binding_nodes.append(n) assert (len(binding_nodes) == 1) assert ((nugget_id, "sh2_pY_binding_semantic_nugget") in corpus.nugget_relations()) for entity in semantic_entities: assert (entity in corpus._hierarchy.get_relation( "sh2_pY_binding_semantic_nugget", nugget_id).keys()) site_actor_with_residue1 = SiteActor( Protoform("B"), Site("pY-site", residues=[ Residue("Y", loc=145, state=State("phosphorylation")) ])) site_actor_with_residue2 = SiteActor( Protoform("B"), Site("some site", residues=[ Residue("Y", loc=145, state=State("phosphorylation")) ])) bnd1 = Binding(abl2_sh2, site_actor_with_residue1) bnd2 = Binding(abl2_sh2, site_actor_with_residue2) corpus.add_interactions([bnd1, bnd2]) print_graph(corpus.action_graph)
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())