def _graphify(self, graph=None): if graph is None: graph = self.out_graph members = [] for pe in self.pes: # FIXME fails to work properly for negative phenotypes... members.append(pe._graphify(graph=graph)) return infixowl.BooleanClass(operator=self.expand(self.op), members=members, graph=graph)
def _graphify(self, *args, graph=None): # defined """ Lift phenotypeEdges to Restrictions """ if graph is None: graph = self.out_graph members = [self.expand(NIFCELL_NEURON)] for pe in self.pes: target = pe._graphify(graph=graph) if isinstance(pe, NegPhenotype ): # isinstance will match NegPhenotype -> Phenotype #self.Class.disjointWith = [target] # FIXME for defined neurons this is what we need and I think it is strong than the complementOf version djc = infixowl.Class( graph=graph ) # TODO for generic neurons this is what we need djc.complementOf = target members.append(djc) else: members.append(target) # FIXME negative logical phenotypes :/ intersection = infixowl.BooleanClass(members=members, graph=graph) # FIXME dupes ec = [intersection] self.Class.equivalentClass = ec return self.Class
def make_defined(graph, ilx_start, label, phenotype_id, restriction_edge, parent=None): ilx_start += 1 id_ = ilx_base.format(ilx_start) id_ = graph.expand(id_) restriction = infixowl.Restriction(graph.expand(restriction_edge), graph=graph.g, someValuesFrom=phenotype_id) defined = infixowl.Class(id_, graph=graph.g) defined.label = rdflib.Literal(label + ' neuron') intersection = infixowl.BooleanClass(members=(graph.expand(NIFCELL_NEURON), restriction), graph=graph.g) #intersection = infixowl.BooleanClass(members=(restriction,), graph=self.graph.g) defined.equivalentClass = [intersection] if parent is not None: defined.subClassOf = [graph.expand(parent)] return ilx_start
def make_neurons(syn_mappings, pedges, ilx_start_, defined_graph): ilx_start = ilx_start_ cheating = { 'vasoactive intestinal peptide': 'VIP', 'star': None, # is a morphological phen that is missing but hits scigraph } ng = makeGraph('NIF-Neuron', prefixes=PREFIXES) #""" It seemed like a good idea at the time... nif_cell = '~/git/NIF-Ontology/ttl/NIF-Cell.ttl' # need to be on neurons branch cg = rdflib.Graph() cg.parse(os.path.expanduser(nif_cell), format='turtle') missing = ( 'NIFCELL:nifext_55', 'NIFCELL:nifext_56', 'NIFCELL:nifext_57', 'NIFCELL:nifext_59', 'NIFCELL:nifext_81', 'NIFCELL:nlx_cell_091205', NIFCELL_NEURON, 'NIFCELL:sao2128417084', 'NIFCELL:sao862606388', # secondary, not explicitly in the hbp import ) for m in missing: m = ng.expand(m) for s, p, o in cg.triples((m, None, None)): ng.add_trip(s, p, o) #cg.remove((None, rdflib.OWL.imports, None)) # DONOTWANT NIF-Cell imports #for t in cg.triples((None, None, None)): #ng.add_trip(*t) # only way to clean prefixes :/ #cg = None #""" hbp_cell = '~/git/NIF-Ontology/ttl/generated/NIF-Neuron-HBP-cell-import.ttl' # need to be on neurons branch _temp = rdflib.Graph() # use a temp to strip nasty namespaces _temp.parse(os.path.expanduser(hbp_cell), format='turtle') for s, p, o in _temp.triples((None, None, None)): if s != rdflib.URIRef( 'http://ontology.neuinfo.org/NIF/ttl/generated/NIF-Neuron-HBP-cell-import.ttl' ): ng.g.add((s, p, o)) base = 'http://ontology.neuinfo.org/NIF/ttl/' ontid = base + ng.name + '.ttl' ng.add_trip(ontid, rdflib.RDF.type, rdflib.OWL.Ontology) ng.add_trip(ontid, rdflib.OWL.imports, base + 'NIF-Neuron-Phenotype.ttl') ng.add_trip(ontid, rdflib.OWL.imports, base + 'NIF-Neuron-Defined.ttl') ng.add_trip(ontid, rdflib.OWL.imports, base + 'hbp-special.ttl') #ng.add_trip(ontid, rdflib.OWL.imports, base + 'NIF-Cell.ttl') # NO! #ng.add_trip(ontid, rdflib.OWL.imports, base + 'external/uberon.owl') #ng.add_trip(ontid, rdflib.OWL.imports, base + 'external/pr.owl') ng.replace_uriref('ilx:hasMolecularPhenotype', 'ilx:hasExpressionPhenotype') #defined_graph = makeGraph('NIF-Neuron-Defined', prefixes=PREFIXES, graph=_g) defined_graph.add_trip(base + defined_graph.name + '.ttl', rdflib.RDF.type, rdflib.OWL.Ontology) defined_graph.add_trip(base + defined_graph.name + '.ttl', rdflib.OWL.imports, base + 'NIF-Neuron-Phenotype.ttl') done = True #False done_ = set() for pedge in pedges: for s, p, o_lit in ng.g.triples((None, pedge, None)): o = o_lit.toPython() success = False true_o = None true_id = None if o in syn_mappings: id_ = syn_mappings[o] ng.add_hierarchy(id_, p, s) ng.g.remove((s, p, o_lit)) #print('SUCCESS, substituting', o, 'for', id_) success = True true_o = o_lit true_id = id_ elif 'Location' in p.toPython() or 'LocatedIn' in p.toPython( ): # lift location to restrictions if o.startswith('http://'): ng.add_hierarchy(o_lit, p, s) ng.g.remove((s, p, o_lit)) data = sgv.findById(o) label = data['labels'][0] ng.add_trip(o, rdflib.RDF.type, rdflib.OWL.Class) ng.add_trip(o, rdflib.RDFS.label, label) success = True true_o = label true_id = o_lit else: if o in cheating: o = cheating[o] data = sgv.findByTerm(o) if data: print('SCIGRAPH', [(d['curie'], d['labels']) for d in data]) for d in data: if 'PR:' in d['curie']: sgt = ng.expand(d['curie']) ng.add_hierarchy(sgt, p, s) ng.g.remove((s, p, o_lit)) label = d['labels'][0] ng.add_trip(sgt, rdflib.RDF.type, rdflib.OWL.Class) ng.add_trip(sgt, rdflib.RDFS.label, label) success = True true_o = label true_id = sgt break if not success: for d in data: if 'NIFMOL:' in d['curie']: sgt = ng.expand(d['curie']) ng.add_hierarchy(sgt, p, s) ng.g.remove((s, p, o_lit)) label = d['labels'][0] ng.add_trip(sgt, rdflib.RDF.type, rdflib.OWL.Class) ng.add_trip(sgt, rdflib.RDFS.label, label) success = True true_o = label true_id = sgt break if o not in done_ and success: done_.add(o) t = tuple( defined_graph.g.triples( (None, rdflib.OWL.someValuesFrom, true_id))) if t: print('ALREADY IN', t) else: ilx_start += 1 id_ = ng.expand(ilx_base.format(ilx_start)) defined_graph.add_trip(id_, rdflib.RDF.type, rdflib.OWL.Class) restriction = infixowl.Restriction(p, graph=defined_graph.g, someValuesFrom=true_id) intersection = infixowl.BooleanClass( members=(defined_graph.expand(NIFCELL_NEURON), restriction), graph=defined_graph.g) this = infixowl.Class(id_, graph=defined_graph.g) this.equivalentClass = [intersection] this.subClassOf = [ defined_graph.expand(defined_class_parent) ] this.label = rdflib.Literal(true_o + ' neuron') print('make_neurons ilx_start', ilx_start, list(this.label)[0]) if not done: embed() done = True defined_graph.add_class(defined_class_parent, NIFCELL_NEURON, label='defined class neuron') defined_graph.add_trip(defined_class_parent, rdflib.namespace.SKOS.definition, 'Parent class For all defined class neurons') defined_graph.write() ng.write() for sub, syn in [ _ for _ in ng.g.subject_objects(ng.expand('NIFRID:synonym')) ] + [_ for _ in ng.g.subject_objects(rdflib.RDFS.label)]: syn = syn.toPython() if syn in syn_mappings: print('ERROR duplicate synonym!', syn, sub) syn_mappings[syn] = sub return ilx_start
def _row_post(self): # defined class lookup = { graph.expand('ilx:AxonPhenotype'): rdflib.URIRef('http://axon.org'), graph.expand('ilx:AxonMorphologicalPhenotype'): None, graph.expand('ilx:DendritePhenotype'): rdflib.URIRef('http://dendrite.org'), graph.expand('ilx:DendriteMorphologicalPhenotype'): None, graph.expand('ilx:SomaPhenotype'): rdflib.URIRef('http://soma.org'), graph.expand('ilx:SomaMorphologicalPhenotype'): None, graph.expand('ilx:NeuronPhenotype'): graph.expand(NIFCELL_NEURON), graph.expand('ilx:CellPhenotype'): None, graph.expand('ilx:Phenotype'): graph.expand('ilx:Phenotype'), } if self.id_ in lookup: return #elif 'Petilla' in self.id_: #return #else: #print(self.id_) # hidden label for consturctions graph2.add_trip(self.id_, rdflib.namespace.SKOS.hiddenLabel, self._label.rsplit(' Phenotype')[0]) self.ilx_start += 1 id_ = defined_graph.expand(ilx_base.format(self.ilx_start)) defined = infixowl.Class(id_, graph=defined_graph.g) #defined.label = rdflib.Literal(self._label.rstrip(' Phenotype') + ' neuron') # the extra space in rstrip removes 'et ' as well WTF! defined.label = rdflib.Literal( self._label.rstrip('Phenotype') + 'neuron') #print(self._label) print('_row_post ilx_start', self.ilx_start, list(defined.label)[0]) def getPhenotypeEdge(phenotype): print(phenotype) edge = 'ilx:hasPhenotype' # TODO in neuronManager... return edge edge = getPhenotypeEdge(self.id_) restriction = infixowl.Restriction(graph.expand(edge), graph=defined_graph.g, someValuesFrom=self.id_) parent = [p for p in self.child_parent_map[self.id_] if p] if parent: parent = parent[0] while 1: if parent == defined_graph.expand('ilx:NeuronPhenotype'): #defined.subClassOf = [graph.expand(defined_class_parent)] # XXX this does not produce what we want break #else: #print(parent, graph.expand('ilx:NeuronPhenotype')) #print('xxxxxxxxxxxxxxxx', parent) new_parent = [ p for p in self.child_parent_map[parent] if p ] if new_parent: parent = new_parent[0] else: break phenotype_equiv = lookup[parent] else: return intersection = infixowl.BooleanClass(members=(phenotype_equiv, restriction), graph=defined_graph.g) ##intersection = infixowl.BooleanClass(members=(restriction,), graph=self.graph.g) defined.equivalentClass = [intersection]
def make_table1(syn_mappings, ilx_start, phenotypes): # TODO when to explicitly subClassOf? I think we want this when the higher level phenotype bag is shared # it may turn out that things like the disjointness exist at a higher level while correlated properties # should be instantiated together as sub classes, for example if cck and # FIXME disagreement about nest basket cells # TODO hasPhenotypes needs to be function to get phenotypeOf to work via reasoner??? this seems wrong. # this also works if phenotypeOf is inverseFunctional # hasPhenotype shall be asymmetric, irreflexive, and intransitive # XXX in answer to Maryann's question about why we need the morphological phenotypes by themselves: # if we don't have them we can't agregate across orthogonal phenotypes since owl correctly keeps the classes distinct # TODO disjointness axioms work really well on defined classes and propagate excellently # TODO add 'Petilla' or something like that to the phenotype definitions # we want this because 'Petilla' denotes the exact ANALYSIS used to determine the phenotype # there are some additional 'protocol' related restrictions on what you can apply analysis to # but we don't have to model those explicitly which would be a nightmare and break the # orthogonality of the cell type decomposition # TODO to make this explicit we need to include that phenotypes require 2 things # 1) a type of data (data type?) 2) a way to classify that data (analysis protocol) # # need a full type restriction... property chain? graph = makeGraph('hbp-special', prefixes=PREFIXES) # XXX fix all prefixes with open(refile(__file__, 'resources/26451489 table 1.csv'), 'rt') as f: rows = [list(r) for r in zip(*csv.reader(f))] base = 'http://ontology.neuinfo.org/NIF/ttl/' ontid = base + graph.name + '.ttl' graph.add_trip(ontid, rdflib.RDF.type, rdflib.OWL.Ontology) graph.add_trip(ontid, rdflib.OWL.imports, base + 'NIF-Neuron-Phenotype.ttl') graph.add_trip(ontid, rdflib.OWL.imports, base + 'NIF-Neuron-Defined.ttl') def lsn(word): syn_mappings[word] = graph.expand( sgv.findByTerm(word)[0]['curie']) # cheating lsn('Parvalbumin') lsn('neuropeptide Y') lsn('VIP peptides') lsn('somatostatin') syn_mappings['calbindin'] = graph.expand('PR:000004967') # cheating syn_mappings['calretinin'] = graph.expand('PR:000004968') # cheating t = table1(graph, rows, syn_mappings, ilx_start) ilx_start = t.ilx_start # adding fake mouse data #with open(refile(__file__, 'resources/26451489 table 1.csv'), 'rt') as f: # FIXME annoying #rows = [list(r) for r in zip(*csv.reader(f))] #t2 = table1(graph, rows, syn_mappings, ilx_start, species='NCBITaxon:10090') # FIXME double SOM+ phenos etc #ilx_start = t2.ilx_start def do_graph(d): sgt = graph.expand(d['curie']) label = d['labels'][0] graph.add_trip(sgt, rdflib.RDF.type, rdflib.OWL.Class) graph.add_trip(sgt, rdflib.RDFS.label, label) done = set() for s, p, o in graph.g.triples( (None, None, None)): #(rdflib.RDFS.subClassOf,rdflib.OWL.Thing)): if o not in done and type(o) == rdflib.term.URIRef: done.add(o) if not [_ for _ in graph.g.objects((o, rdflib.RDFS.label))]: d = sgv.findById(o) if d: if 'PR:' in d['curie']: do_graph(d) elif 'NIFMOL:' in d['curie']: do_graph(d) elif 'UBERON:' in d['curie']: do_graph(d) elif 'NCBITaxon:' in d['curie']: do_graph(d) elif 'NIFCELL:' in d['curie']: do_graph(d) # FIXME this is a dupe with defined_class #graph.add_trip(defined_class_parent, rdflib.RDF.type, rdflib.OWL.Class) #graph.add_trip(defined_class_parent, rdflib.RDFS.label, 'defined class neuron') #graph.add_trip(defined_class_parent, rdflib.namespace.SKOS.description, 'Parent class For all defined class neurons') #graph.add_trip(defined_class_parent, rdflib.RDFS.subClassOf, NIFCELL_NEURON) #graph.add_trip(morpho_defined, rdflib.RDFS.subClassOf, defined_class_parent) #graph.add_trip(morpho_defined, rdflib.RDFS.label, 'Morphologically classified neuron') # FIXME -- need asserted in here... #graph.add_trip(ephys_defined, rdflib.RDFS.subClassOf, defined_class_parent) #graph.add_trip(ephys_defined, rdflib.RDFS.label, 'Electrophysiologically classified neuron') graph.add_class(expression_defined, NIFCELL_NEURON, autogen=True) graph.add_class('ilx:NeuroTypeClass', NIFCELL_NEURON, label='Neuron TypeClass') graph.g.commit() phenotype_dju_dict = add_types(graph, phenotypes) for pheno, disjoints in phenotype_dju_dict.items(): name = ' '.join(re.findall( r'[A-Z][a-z]*', pheno.split(':')[1])[:-1]) #-1: drops Phenotype ilx_start += 1 # = make_defined(graph, ilx_start, name + ' neuron type', pheno, 'ilx:hasPhenotype') id_ = graph.expand(ilx_base.format(ilx_start)) typeclass = infixowl.Class(id_, graph=graph.g) typeclass.label = rdflib.Literal(name + ' neuron type') restriction = infixowl.Restriction(graph.expand('ilx:hasPhenotype'), graph=graph.g, someValuesFrom=pheno) #typeclass.subClassOf = [restriction, graph.expand('ilx:NeuroTypeClass')] ntc = graph.expand('ilx:NeuroTypeClass') intersection = infixowl.BooleanClass(members=(ntc, restriction), graph=graph.g) typeclass.equivalentClass = [intersection] # FIXME not clear that we should be doing typeclasses this way.... :/ # requires more thought, on the plus side you do get better reasoning... disjointunion = disjointUnionOf(graph=graph.g, members=list(disjoints)) graph.add_trip(id_, rdflib.OWL.disjointUnionOf, disjointunion) graph.write() return t