Skip to content

Commit

Permalink
Fixed issue #2; Break up declaration of connection elements; merging …
Browse files Browse the repository at this point in the history
…connection references
  • Loading branch information
dustine32 committed Mar 22, 2018
1 parent 88b82bb commit 65a98b4
Show file tree
Hide file tree
Showing 3 changed files with 117 additions and 58 deletions.
53 changes: 35 additions & 18 deletions pathway_connections.py
Original file line number Diff line number Diff line change
Expand Up @@ -85,20 +85,35 @@ def print(self):
idb=self.id_b))

def declare_entities(self, model):
# Classes
self.declare_a(model)
self.declare_b(model)

return model

def declare_a(self, model):
# Class
if self.full_id_a() not in model.classes:
model.declare_class(self.full_id_a())
if self.full_id_b() not in model.classes:
model.declare_class(self.full_id_b())

# Individuals
# if self.full_id_a() not in model.individuals:
if self.full_id_a() not in self.individuals:
if self.a_is_complex():
uri_a = self.complex_a.declare_entities(model)
else:
uri_a = model.declare_individual(self.full_id_a())
self.individuals[self.full_id_a()] = uri_a

self.mechanism["uri"] = model.declare_individual(self.mechanism["term"])
self.individuals[self.mechanism["term"]] = self.mechanism["uri"]

return model

def declare_b(self, model):
# Class
if self.full_id_b() not in model.classes:
model.declare_class(self.full_id_b())

# Individuals
if self.full_id_b() not in self.individuals and self.regulated_activity["uri"] is None:
if self.b_is_complex():
uri_b = self.complex_b.declare_entities(model)
Expand All @@ -110,11 +125,8 @@ def declare_entities(self, model):
for t in model.writer.writer.graph.triples((self.regulated_activity["uri"],ENABLED_BY,None)):
self.individuals[self.full_id_b()] = t[2]

self.mechanism["uri"] = model.declare_individual(self.mechanism["term"])
self.individuals[self.mechanism["term"]] = self.mechanism["uri"]
self.individuals[self.regulated_activity["term"]] = self.regulated_activity["uri"]


return model

def full_id_a(self):
Expand Down Expand Up @@ -148,17 +160,19 @@ def full_statement_bnode_in_model(self, model):
# mechanism["term"] REGULATES regulated_activity["term"]
graph = model.writer.writer.graph

a_enables_triples = []
for id_a in model.uri_list_for_individual(self.full_id_a()):
for mech_uri in model.uri_list_for_individual(self.mechanism["term"]):
if (mech_uri, ENABLED_BY, id_a) in graph:
a_enables_triples.append((mech_uri, ENABLED_BY, id_a))
# a_enables_triples = []
# for id_a in model.uri_list_for_individual(self.full_id_a()):
# for mech_uri in model.uri_list_for_individual(self.mechanism["term"]):
# if (mech_uri, ENABLED_BY, id_a) in graph:
# a_enables_triples.append((mech_uri, ENABLED_BY, id_a))
a_enables_triples = model.triples_by_ids(self.mechanism["term"], ENABLED_BY, self.full_id_a())

b_enables_triples = []
for id_b in model.uri_list_for_individual(self.full_id_b()):
for reg_act in model.uri_list_for_individual(self.regulated_activity["term"]):
if (reg_act, ENABLED_BY, id_b) in graph:
b_enables_triples.append((reg_act, ENABLED_BY, id_b))
# b_enables_triples = []
# for id_b in model.uri_list_for_individual(self.full_id_b()):
# for reg_act in model.uri_list_for_individual(self.regulated_activity["term"]):
# if (reg_act, ENABLED_BY, id_b) in graph:
# b_enables_triples.append((reg_act, ENABLED_BY, id_b))
b_enables_triples = model.triples_by_ids(self.regulated_activity["term"], ENABLED_BY, self.full_id_b())

for a_triple in a_enables_triples:
for b_triple in b_enables_triples:
Expand Down Expand Up @@ -210,7 +224,10 @@ def __init__(self, filename):
# if not (pc.id_a.startswith("SIGNOR") or pc.id_b.startswith("SIGNOR") or line["TYPEA"] == "phenotype" or line["TYPEB"] == "phenotype"):
acceptable_types = ['protein','complex']
if line["TYPEA"] in acceptable_types and line["TYPEB"] in acceptable_types:
self.append(pc)
if self.find(pc):
self.append_reference(pc)
else:
self.append(pc)


def append(self, pathway_connection):
Expand Down
101 changes: 63 additions & 38 deletions pathway_importer.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ def test_label_finding(model):
def main():
args = parser.parse_args()

model = GoCamModel("superfamily_test.ttl")
model = GoCamModel("fix_evidence_enabled_bys.ttl")
# p_connections = PathwayConnectionSet("SIGNOR-G2-M_trans_02_03_18.tsv")
p_connections = PathwayConnectionSet(args.filename)
linenum = 1
Expand Down Expand Up @@ -86,44 +86,69 @@ def main():
# if pc.mechanism["term"] in pc.individuals and not model_contains_statement(model, pc.individuals[pc.mechanism["term"]], ENABLED_BY, pc.full_id_a()):
full_statement = pc.full_statement_bnode_in_model(model)
# if pc.mechanism["term"] in pc.individuals and full_statement is None:
if full_statement is None:
print("Hey " + pc.full_id_a())
# if pc.id_a == "Q13315" and pc.id_b == "P38398":
# print("Dang " + pc.pmid[0])
model = pc.declare_entities(model)

enabled_by_stmt_a_triple = (pc.mechanism["uri"], ENABLED_BY, pc.individuals[pc.full_id_a()])
if enabled_by_stmt_a_triple in model.writer.writer.graph:
enabled_by_stmt_a = next(model.writer.writer.graph.triples(enabled_by_stmt_a_triple))
else:
enabled_by_stmt_a = model.writer.emit(enabled_by_stmt_a_triple[0], enabled_by_stmt_a_triple[1], enabled_by_stmt_a_triple[2])
axiom_a = model.add_axiom(enabled_by_stmt_a)
enabled_by_stmt_b_triple = (pc.regulated_activity["uri"], ENABLED_BY, pc.individuals[pc.full_id_b()])
if enabled_by_stmt_b_triple in model.writer.writer.graph:
enabled_by_stmt_b = next(model.writer.writer.graph.triples(enabled_by_stmt_b_triple))
else:
enabled_by_stmt_b = model.writer.emit(enabled_by_stmt_b_triple[0], enabled_by_stmt_b_triple[1], enabled_by_stmt_b_triple[2])
axiom_b = model.add_axiom(enabled_by_stmt_b)

# Connect the two activities
# source_id = model.individuals[pc.mechanism_go_term]
try:
source_id = pc.mechanism["uri"]
except KeyError as err:
pc.print()
print(pc.individuals)
raise err
property_id = URIRef(expand_uri(pc.relation))
# target_id = model.individuals[pc.regulated_activity_term]
target_id = pc.regulated_activity["uri"]
if not model_contains_statement(model, source_id, property_id, pc.regulated_activity["term"]):
# Annotate source MF GO term NamedIndividual with relation code-target MF term URI
model.writer.emit(source_id, property_id, target_id)
# Add axiom (Source=MF term URI, Property=relation code, Target=MF term URI)
relation_axiom = model.writer.emit_axiom(source_id, property_id, target_id)
model.add_evidence(relation_axiom, "EXP", ["PMID:" + pmid for pmid in pc.pmid])
# if full_statement is None:
# print("Hey " + pc.full_id_a())
# if pc.id_a == "Q13315" and pc.id_b == "P38398":
# print("Dang " + pc.pmid[0])
# model = pc.declare_entities(model)

# enabled_by_stmt_a_triple = (pc.mechanism["uri"], ENABLED_BY, pc.individuals[pc.full_id_a()])
if pc.a_is_complex():
entity_a = pc.complex_a.uri_in_model(model)
else:
entity_a = pc.full_id_a()
if entity_a is not None:
enabled_by_stmt_a_triples = model.triples_by_ids(pc.mechanism["term"], ENABLED_BY, entity_a)
else:
enabled_by_stmt_a_triples = []
if len(enabled_by_stmt_a_triples) == 0:
# If triple A doesn't exist for entities, declare individuals and create it
# enabled_by_stmt_a = model.writer.emit(pc.mechanism["term"], ENABLED_BY, pc.full_id_a())
model = pc.declare_a(model)
enabled_by_stmt_a = model.writer.emit(pc.mechanism["uri"], ENABLED_BY, pc.individuals[pc.full_id_a()])
axiom_a = model.add_axiom(enabled_by_stmt_a)
else:
enabled_by_stmt_a = enabled_by_stmt_a_triples[0]
if pc.b_is_complex():
entity_b = pc.complex_b.uri_in_model(model)
else:
entity_b = pc.full_id_b()
if entity_b is not None:
enabled_by_stmt_b_triples = model.triples_by_ids(pc.regulated_activity["term"], ENABLED_BY, entity_b)
else:
print("2")
enabled_by_stmt_b_triples = []
regulated_activity_uris = []
for b_triple in enabled_by_stmt_b_triples:
regulated_activity_uris.append(b_triple[0])
if len(regulated_activity_uris) == 0:
model = pc.declare_b(model)
enabled_by_stmt_b = model.writer.emit(pc.regulated_activity["uri"], ENABLED_BY,
pc.individuals[pc.full_id_b()])
axiom_b = model.add_axiom(enabled_by_stmt_b)
regulated_activity_uris.append(enabled_by_stmt_b[0])
# if enabled_by_stmt_b_triple in model.writer.writer.graph:
# enabled_by_stmt_b = next(model.writer.writer.graph.triples(enabled_by_stmt_b_triple))
# else:
# enabled_by_stmt_b = model.writer.emit(enabled_by_stmt_b_triple[0], enabled_by_stmt_b_triple[1], enabled_by_stmt_b_triple[2])
# axiom_b = model.add_axiom(enabled_by_stmt_b)

# Connect the two activities
# Decouple this from ENABLED_BY statements to allow multiple regulation relations from one GP-MF node - issue #2
# source_id = model.individuals[pc.mechanism_go_term]
source_id = enabled_by_stmt_a[0]
property_id = URIRef(expand_uri(pc.relation))
# target_id = model.individuals[pc.regulated_activity_term]
# target_id = pc.regulated_activity["uri"] # This should be an array of target activities (individual_list)
# if not model_contains_statement(model, source_id, property_id, pc.regulated_activity["term"]): # Make into for loop
for reg_activity_uri in regulated_activity_uris:
target_id = reg_activity_uri
# Annotate source MF GO term NamedIndividual with relation code-target MF term URI
model.writer.emit(source_id, property_id, target_id)
# Add axiom (Source=MF term URI, Property=relation code, Target=MF term URI)
relation_axiom = model.writer.emit_axiom(source_id, property_id, target_id)
model.add_evidence(relation_axiom, "EXP", ["PMID:" + pmid for pmid in pc.pmid])
# else:
# print("2")

# pc.print()

Expand Down
21 changes: 19 additions & 2 deletions signor_complex.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
from rdflib.namespace import RDFS
from prefixcommons.curie_util import expand_uri

HAS_PART = URIRef(expand_uri("BFO:0000051"))
complexes = []

# class SignorComplex():
Expand All @@ -13,26 +14,42 @@ def __init__(self, signor_id, name, entities):
self.name = name
self.entities = entities


class SignorComplex(SignorGrouping):
def declare_entities(self, model):
uri = model.declare_individual("GO:0032991")
model.writer.writer.graph.add((uri, RDFS.label, Literal(str(self.name))))
for entity in self.entities:
entity_full_id = NamingConvention.full_id(entity)
entity_uri = model.declare_individual(entity_full_id)
part_of_stmt = model.writer.emit(uri, URIRef(expand_uri("BFO:0000051")), entity_uri)
part_of_stmt = model.writer.emit(uri, HAS_PART, entity_uri)
model.add_axiom(part_of_stmt)
"uri BFO:0000051 entity_uri"
return uri

def uri_in_model(self, model):
graph = model.writer.writer.graph
complex_term = "GO:0032991"
complex_uris = model.uri_list_for_individual(complex_term)
for c_uri in complex_uris:
found_entities = [] # Collect connected entities for set comparison
for entity in self.entities:
for entity_uri in model.uri_list_for_individual(NamingConvention.full_id(entity)):
if (c_uri, HAS_PART, entity_uri) in graph:
found_entities.append(entity)
if set(self.entities) == set(found_entities):
print(c_uri)
return c_uri


class SignorProteinFamily(SignorGrouping):
def declare_entities(self, model):
uri = model.declare_individual("GO:0032991")
model.writer.writer.graph.add((uri, RDFS.label, Literal(str(self.name))))
for entity in self.entities:
entity_full_id = NamingConvention.full_id(entity)
entity_uri = model.declare_individual(entity_full_id)
part_of_stmt = model.writer.emit(uri, URIRef(expand_uri("BFO:0000051")), entity_uri)
part_of_stmt = model.writer.emit(uri, HAS_PART, entity_uri)
model.add_axiom(part_of_stmt)
"uri BFO:0000051 entity_uri"
return uri
Expand Down

0 comments on commit 65a98b4

Please sign in to comment.