Пример #1
0
def create_ontology(identifier):
    ontology_uri = d1[identifier]
    o_ns = Namespace(ontology_uri+"#")
    r = requests.get(ontology_uri)
    xml = r.text.encode('utf-8')
    eml = ET.fromstring(xml)

    g = Graph()
    g.bind("rdfs", RDFS)
    g.bind("owl", OWL)

    ontology = infixowl.Ontology(ontology_uri, graph=g)
    datasetTitle = eml.find('dataset/title')
    if datasetTitle is not None:
        ontology.label = Literal(datasetTitle.text.strip())
    
    ontology.comment = Literal('\n'.join([x.text for x in eml.findall('dataset/abstract/para') if x is not None and x.text is not None]).strip())

    # Data Table:
    i = 0
    for datatable in eml.findall("dataset/dataTable"):
        i += 1
        entity_ident = str(i)
        entity = infixowl.Class(o_ns['_'.join(['d',entity_ident])],graph=g)
        entity.subClassOf = [oboe.Entity]
        entityLabel = []
        for e in ['entityName','entityDescription']:
            if datatable.find(e) is not None and datatable.find(e).text is not None:
                entityLabel.append(datatable.find(e).text.strip())
        entity.label = Literal(' '.join(entityLabel))
        if datatable.find('entityDescription') is not None and datatable.find('entityDescription').text is not None:
            entity.comment = Literal(datatable.find('entityDescription').text.strip())
        j = 0
        for attribute in datatable.findall('attributeList/attribute'):
            j +=1
            attribute_ident = str(j)
            characteristic = infixowl.Class(o_ns['_'.join(['d',entity_ident,'c',attribute_ident])],graph=g)
            characteristic.subClassOf = [oboe.Characteristic]
            label = []
            for e in ['.//attributeName','.//attributeLabel','.//attributeDefinition']:
                if attribute.find(e) is not None and attribute.find(e).text is not None:
                    label.append(attribute.find(e).text.strip())
            characteristic.label = Literal(' '.join(label))
            if attribute.find('.//attributeDefinition') is not None and attribute.find('.//attributeDefinition').text is not None:
                characteristic.comment = Literal(datatable.find('.//attributeDefinition').text.strip())

            measurement_type = infixowl.Class(o_ns['_'.join(['d',entity_ident,'a',attribute_ident])],graph=g)
            g.add((measurement_type.identifier, urn.entityId, Literal(entity_ident)))
            g.add((measurement_type.identifier, urn.attributeId, Literal(attribute_ident)))
            measurement_type.label = Literal(g.value(entity.identifier,RDFS.label, default="") + " " + g.value(characteristic.identifier,RDFS.label, default=""))
            measurement_type.comment = Literal(g.value(entity.identifier,RDFS.comment, default="") + " " + g.value(characteristic.identifier,RDFS.comment, default=""))
            measurement_type.subClassOf = [
                oboe.MeasurementType,
                infixowl.Restriction(oboe.measuresEntity, graph=g, someValuesFrom=entity),
                infixowl.Restriction(oboe.measuresCharacteristic, graph=g, someValuesFrom=characteristic)
            ]
    return g
Пример #2
0
def create_ontology(identifier):
    ontology_uri = d1[identifier]
    o_ns = Namespace(ontology_uri + "#")
    r = requests.get(ontology_uri)
    xml = r.text.encode('utf-8')
    eml = ET.fromstring(xml)

    g = Graph()
    g.bind("rdfs", RDFS)
    g.bind("owl", OWL)

    ontology = infixowl.Ontology(ontology_uri, graph=g)
    ontology.label = Literal(eml.find('dataset/title').text.strip())
    ontology.comment = Literal('\n'.join(
        [x.text for x in eml.find('dataset/abstract/para')]).strip())

    # Data Table:
    for datatable in eml.findall("dataset/dataTable"):
        entity_ident = datatable.attrib['id']
        entity = infixowl.Class(o_ns[entity_ident], graph=g)
        entity.subClassOf = [oboe.Entity]
        entity.label = Literal(datatable.find('entityName').text.strip())
        entity.comment = Literal(
            datatable.find('entityDescription').text.strip())
        for attribute in datatable.findall('attributeList/attribute'):
            attribute_ident = attribute.attrib['id']
            characteristic = infixowl.Class(o_ns[attribute_ident], graph=g)
            characteristic.subClassOf = [oboe.Characteristic]
            characteristic.label = Literal(
                datatable.find('.//attributeLabel').text.strip())
            characteristic.comment = Literal(
                datatable.find('.//attributeDefinition').text.strip())

            measurement_type = infixowl.Class(o_ns[entity_ident + "_" +
                                                   attribute_ident],
                                              graph=g)
            measurement_type.label = Literal(
                g.label(entity.identifier) + " " +
                g.label(characteristic.identifier))
            measurement_type.subClassOf = [
                oboe.MeasurementType,
                infixowl.Restriction(oboe.measuresEntity,
                                     graph=g,
                                     someValuesFrom=entity),
                infixowl.Restriction(oboe.measuresCharacteristic,
                                     graph=g,
                                     someValuesFrom=characteristic)
            ]
    return g
Пример #3
0
    def add_hierarchy(self, parent, edge, child):
        """ Helper function to simplify the addition of part_of style
            objectProperties to graphs. FIXME make a method of makeGraph?
        """
        if type(parent) != rdflib.URIRef:
            parent = self.check_thing(parent)

        if type(edge) != rdflib.URIRef:
            edge = self.check_thing(edge)

        if type(child) != infixowl.Class:
            if type(child) != rdflib.URIRef:
                child = self.check_thing(child)
            child = infixowl.Class(child, graph=self.g)

        restriction = infixowl.Restriction(edge,
                                           graph=self.g,
                                           someValuesFrom=parent)
        child.subClassOf = [restriction] + [c for c in child.subClassOf]
Пример #4
0
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
Пример #5
0
def clean_hbp_cell():
    #old graph
    g = rdflib.Graph()
    if __name__ == '__main__':
        embed()
    path = Path(devconfig.git_local_base,
                'methodsOntology/ttl/hbp_cell_ontology.ttl')
    if not path.exists():
        raise devconfig.MissingRepoError(f'repo for {path} does not exist')

    g.parse(path.as_posix(), format='turtle')
    g.remove((None, rdflib.OWL.imports, None))
    g.remove((None, rdflib.RDF.type, rdflib.OWL.Ontology))

    #new graph
    NAME = 'NIF-Neuron-HBP-cell-import'
    mg = makeGraph(NAME, prefixes=PREFIXES)
    ontid = 'http://ontology.neuinfo.org/NIF/ttl/generated/' + NAME + '.ttl'
    mg.add_trip(ontid, rdflib.RDF.type, rdflib.OWL.Ontology)
    mg.add_trip(ontid, rdflib.RDFS.label, 'NIF Neuron HBP cell import')
    mg.add_trip(ontid, rdflib.RDFS.comment, 'this file was automatically using pyontutils/hbp_cells.py')
    mg.add_trip(ontid, rdflib.OWL.versionInfo, date.isoformat(date.today()))
    newgraph = mg.g

    skip = {
        '0000000':'SAO:1813327414',  # cell
        #'0000001':NEURON,  # neuron  (equiv)
        #'0000002':'SAO:313023570',  # glia  (equiv)
        #'0000021':'NLXNEURNT:090804',  # glut  (equiv, but phen)
        #'0000022':'NLXNEURNT:090803',  # gaba  (equiv, but phen)

        '0000003':NEURON,
        '0000004':NEURON,
        '0000005':NEURON,
        '0000006':NEURON,
        '0000007':NEURON,
        '0000008':NEURON,
        '0000009':NEURON,
        '0000010':NEURON,
        '0000019':NEURON,
        '0000020':NEURON,
        '0000033':NEURON,
        '0000034':NEURON,
        '0000070':NEURON,
        '0000071':NEURON,
    }
    to_phenotype = {
        '0000021':('ilx:hasExpressionPhenotype', 'SAO:1744435799'),  # glut, all classes that might be here are equived out
        '0000022':('ilx:hasExperssionPhenotype', 'SAO:229636300'),  # gaba
    }
    lookup = {'NIFCELL', 'NIFNEURNT'}
    missing_supers = {
        'HBP_CELL:0000136',
        'HBP_CELL:0000137',
        'HBP_CELL:0000140',
    }

    replace = set()
    phen = set()
    equiv = {}
    for triple in sorted(g.triples((None, None, None))):
        id_suffix = newgraph.namespace_manager.compute_qname(triple[0].toPython())[2]
        try:
            obj_suffix = newgraph.namespace_manager.compute_qname(triple[2].toPython())[2]
        except:  # it wasn't a url
            pass
        # equiv insert for help
        if triple[1] == rdflib.OWL.equivalentClass and id_suffix not in skip and id_suffix not in to_phenotype:
            qnt = newgraph.namespace_manager.compute_qname(triple[2].toPython())
            #print(qnt)
            if qnt[0] in lookup:
                try:
                    lab = v.findById(qnt[0] + ':' + qnt[2])['labels'][0]
                    print('REMOTE', qnt[0] + ':' + qnt[2], lab)
                    #mg.add_trip(triple[2], rdflib.RDFS.label, lab)
                    #mg.add_trip(triple[0], PREFIXES['NIFRID'] + 'synonym', lab)  # so we can see it
                except TypeError:
                    if qnt[2].startswith('nlx'):
                        triple = (triple[0], triple[1], expand('NIFSTD:' + qnt[2]))
                    #print('bad identifier')

        #check for equiv
        if triple[0] not in equiv:
            eq = [o for o in g.objects(triple[0], rdflib.OWL.equivalentClass)]
            if eq and id_suffix not in skip and id_suffix not in to_phenotype:
                if len(eq) > 1:
                    print(eq)
                equiv[triple[0]] = eq[0]
                continue
        elif triple[0] in equiv:
            continue

        # edge replace
        if triple[1].toPython() == 'http://www.FIXME.org/nsupper#synonym':
            edge =  mg.expand('NIFRID:abbrev')
        elif triple[1].toPython() == 'http://www.FIXME.org/nsupper#definition':
            edge = rdflib.namespace.SKOS.definition
        else:
            edge = triple[1]

        # skip or to phenotype or equiv
        if id_suffix in skip:  # have to make a manual edit to rdflib to include 'Nd' in allowed 1st chars
            replace.add(triple[0])
            #print('MEEP MEEP')
        elif id_suffix in to_phenotype:  # have to make a manual edit to rdflib to include 'Nd' in allowed 1st chars
            phen.add(triple[0])
        elif triple[1] == rdflib.RDFS.label:  # fix labels
            if not triple[2].startswith('Hippocampus'):
                new_label = rdflib.Literal('Neocortex ' + triple[2], lang='en')
                newgraph.add((triple[0], edge, new_label))
            else:
                newgraph.add((triple[0], edge, triple[2]))
        elif triple[2] in replace:
            mg.add_trip(triple[0], edge, skip[obj_suffix])
        elif triple[2] in phen:
            edge_, rst_on = to_phenotype[obj_suffix]
            edge_ = expand(edge_)
            rst_on = expand(rst_on)

            this = triple[0]
            this = infixowl.Class(this, graph=newgraph)
            this.subClassOf = [expand(NEURON)] + [c for c in this.subClassOf]

            restriction = infixowl.Restriction(edge_, graph=newgraph, someValuesFrom=rst_on)
            this.subClassOf = [restriction] + [c for c in this.subClassOf]
        elif triple[2] in equiv:
            newgraph.add((triple[0], edge, equiv[triple[2]]))
        else:
            newgraph.add((triple[0], edge, triple[2]))

    # final cleanup for forward references (since we iterate through sorted)

    tt = rdflib.URIRef(expand('HBP_CELL:0000033'))
    tf = rdflib.URIRef(expand('HBP_CELL:0000034'))
    newgraph.remove((None, None, tt))
    newgraph.remove((None, None, tf))

    # add missing subClasses
    for nosub in missing_supers:
        mg.add_trip(nosub, rdflib.RDFS.subClassOf, NEURON)

    # cleanup for subClassOf
    for subject in sorted(newgraph.subjects(rdflib.RDFS.subClassOf, expand(NEURON))):
        sco = [a for a in newgraph.triples((subject, rdflib.RDFS.subClassOf, None))]
        #print('U WOT M8')
        if len(sco) > 1:
            #print('#############\n', sco)
            for s, p, o in sco:
                if 'hbp_cell_ontology' in o or 'NIF-Cell' in o and o != expand(NEURON): #or 'sao2128417084' in o:  # neocortex pyramidal cell
                    #print(sco)
                    newgraph.remove((subject, rdflib.RDFS.subClassOf, expand(NEURON)))
                    break

    # do ilx
    ilx_start = ilx_get_start()
    #ilx_conv_mem = memoize('hbp_cell_interlex.json')(ilx_conv)  # FIXME NOPE, also need to modify the graph :/
    ilx_labels, ilx_replace = ilx_conv(graph=newgraph, prefix='HBP_CELL', ilx_start=ilx_start)
    ilx_add_ids(ilx_labels)

    replace_map = ilx_replace
    for hbp, rep in skip.items():
        ori = 'HBP_CELL:'+hbp
        if ori in replace_map: raise KeyError('identifier already in!??! %s' % ori)
        replace_map[ori] = rep
    for hbp, (e, rep) in to_phenotype.items():
        ori = 'HBP_CELL:'+hbp
        if ori in replace_map: raise KeyError('identifier already in!??! %s' % ori)
        replace_map[ori] = edge, rep
    for hbp_iri, rep_iri in equiv.items():
        hbp = newgraph.compute_qname(hbp_iri)[2]
        rep = newgraph.qname(rep_iri)
        ori = 'HBP_CELL:'+hbp
        if ori in replace_map: raise KeyError('identifier already in!??! %s' % ori)
        replace_map[ori] = rep

    return mg, replace_map
Пример #6
0
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
Пример #7
0
        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]
Пример #8
0
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
Пример #9
0
 def _graphify(self, graph=None):
     if graph is None:
         graph = self.out_graph
     return infixowl.Restriction(onProperty=self.e,
                                 someValuesFrom=self.p,
                                 graph=graph)