def test_ligandmod_generation(self): """Test generation of a transmodification nugget graph.""" enzyme_gene = Protoform("A") enzyme_region_actor = RegionActor( protoform=enzyme_gene, region=Region("Pkinase")) substrate = Protoform("B") automod = LigandModification( enzyme_region_actor, substrate, Residue("Y", 100, State("phosphorylation", True)), value=True, enzyme_bnd_region=Region("EbndRegion"), substrate_bnd_region=Region("SbndRegion"), substrate_bnd_site=Site("SbndSite")) corpus = KamiCorpus("test") identifier = EntityIdentifier( corpus.action_graph, corpus.get_action_graph_typing()) generator = LigandModGenerator(identifier) n, _, _, _ = generator.generate(automod) print_graph(n.graph) inter = LigandModification( enzyme=RegionActor(protoform=Protoform(uniprotid="P30530", hgnc_symbol="AXL"), region=Region(name="Tyr_kinase", interproid="IPR020635", start=536, end=807)), substrate=SiteActor(protoform=Protoform(uniprotid="P06239", hgnc_symbol="LCK"), site=Site(name="pY394", start=391, end=397)), target=Residue(aa="Y", loc=394, state=State("phosphorylation", False)), value=True, rate=10, enzyme_bnd_region=Region(name="Tyr_kinase", interproid="IPR020635", start=536, end=807), substrate_bnd_site=Site(name='pY394', start=391, end=397) ) corpus = KamiCorpus("test") corpus.add_interaction(inter, anatomize=False) print_graph(corpus.get_nugget('test_nugget_1'))
def test_site_residue_reconnect(self): sh2 = Region(name="SH2") b = Binding( SiteActor(protoform=Protoform(uniprotid="P00533", hgnc_symbol="EGFR", residues=[ Residue("Y", 1092, state=State( "phosphorylation", True)) ]), site=Site(name="motif1092", start=1088, end=1096)), RegionActor(protoform=Protoform("P62993", hgnc_symbol="GRB2"), region=sh2)) corpus = KamiCorpus("test") corpus.add_interaction(b)
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()
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 nugget1_id = TEST_CORPUS.add_interaction(interaction1) # Manually add a new protoform new_protoform_node = TEST_CORPUS.add_protoform(Protoform("P62993")) # Manually add a new components to an arbitrary protoform TEST_CORPUS.add_site(Site("New site"), new_protoform_node) grb2 = Protoform("P62993", hgnc_symbol="GRB2", states=[State("activity", True)]) grb2_sh2 = RegionActor(protoform=grb2, region=Region(name="SH2")) shc1 = Protoform("P29353", hgnc_symbol="SHC1") shc1_pY = SiteActor( protoform=shc1,
class TestBlackBox(object): """Test class for black box functionality.""" def __init__(self): """Initialize with an empty corpus.""" self.corpus = KamiCorpus("test") def test_simple_mod_nugget(self): """Simple modification interaction example.""" enz_res = Residue("S", 100, State("phospho", True)) enz_reg = Region(start=150, end=170, states=[State("activity", True)]) enzyme_entity = Protoform("P00533", regions=[enz_reg], residues=[enz_res]) sub_bound_1 = Protoform("P28482", states=[State("activity", True)]) sub_bound_2 = Protoform("P28482", states=[State("activity", True)]) substrate_entity = Protoform("P04049", bound_to=[sub_bound_1, sub_bound_2]) mod_state = State("activity", False) value = True mod1 = Modification(enzyme_entity, substrate_entity, mod_state, value) # Create corresponding nugget in the corpus self.corpus.add_interaction(mod1, add_agents=True, anatomize=False) def test_complex_mod_nugget(self): """Complex modification interaction example.""" enzyme_agent = Protoform("P04049") enzyme_region = Region(start=100, end=200, name="lala") enzyme = RegionActor(enzyme_agent, enzyme_region) state = State("phosphorylation", True) reg_residue = Residue("S", 550, state) substrate_region = Region(start=500, end=600, residues=[reg_residue]) substrate_residues = [ Residue("T", 100), Residue("S", 56, State("phosphorylation", True)) ] substrate_state = State("activity", True) next_level_bound = RegionActor(Protoform("P04637"), Region(start=224, end=234)) substrate_bound = Protoform("P12931", bound_to=[next_level_bound]) substrate = Protoform("P00533", regions=[substrate_region], residues=substrate_residues, states=[substrate_state], bound_to=[substrate_bound]) mod_target = Residue("S", "33", State("phosphorylation", False)) mod2 = Modification(enzyme, substrate, mod_target, True) self.corpus.add_interaction(mod2, add_agents=True, anatomize=False) 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")) 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) 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_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_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_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_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_site_residue_reconnect(self): sh2 = Region(name="SH2") b = Binding( SiteActor(protoform=Protoform(uniprotid="P00533", hgnc_symbol="EGFR", residues=[ Residue("Y", 1092, state=State( "phosphorylation", True)) ]), site=Site(name="motif1092", start=1088, end=1096)), RegionActor(protoform=Protoform("P62993", hgnc_symbol="GRB2"), region=sh2)) corpus = KamiCorpus("test") corpus.add_interaction(b) 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_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)