def parse_ontology_file(terminology):
    print "Parsing " + terminology
    #get the URI of the terminology and create a new instance of ontology
    re= requests.get(terminology)
    jo = json.loads(re.text)
    onto = ontology.ontology(terminology)
    #get all terms of the termonology
    re1= requests.get("http://terminologies.gfbio.org/api/terminologies/" + jo["results"][0]["acronym"] + "/allterms")
    jo1 = json.loads(re1.text)
    tempElems = []
    #get all terms and create them as instances of onto_elem
    for term in jo1["results"]:
        elem = onto_element.onto_elem(term["uri"], "", "")
        elem.add_child(onto_element.onto_elem("{http://www.w3.org/2000/01/rdf-schema#}label",  "{http://www.w3.org/2000/01/rdf-schema#}label", term["label"]))
        #get the synonyms
        re2= requests.get("http://terminologies.gfbio.org/api/terminologies/" + jo["results"][0]["acronym"] + "/synonyms?uri=" + term["uri"])
        jo2 = json.loads(re2.text)
        if "synonyms" in jo2["results"]:
            elem.add_attribute("synonyms", jo2["results"]["synonyms"])
        #save the term
        tempElems.append(elem)
        #print elem.tostring()
    if onto != "" and len(tempElems) > 0:
        onto.add_elements(tempElems)
    if onto != "":
        #util.write2File("onto.txt", str(onto) + os.linesep, "a")
        #util.write2File("onto.txt", str(onto.get_elements()) + os.linesep, "a")
        return onto
    else:
        return ""
Beispiel #2
0
def parse_ontology_file(ontology_file):
    print "Parsing " + ontology_file
    tree = defusedxml.ElementTree.parse(ontology_file)
    root = tree.getroot()
    #check if the file could even be an OWL file in RDF XML syntax
    if root.tag == "{http://www.w3.org/1999/02/22-rdf-syntax-ns#}RDF":
        onto_info = root.findall("{http://www.w3.org/2002/07/owl#}Ontology")
        #look if there is exactly one 'Ontology' element from namespace 'owl'
        if onto_info[0].tag.replace("{http://www.w3.org/2002/07/owl#}", "") == "Ontology":
            #get the name of the ontology
            onto = ontology.ontology(onto_info[0].attrib["{http://www.w3.org/1999/02/22-rdf-syntax-ns#}about"])
            tempProps = []
            tempElems = []
            #go through all elements
            for item in root.getchildren():
                #get the properties of the ontology
                if re.match("{http://www.w3.org/2002/07/owl#}.*Property", item.tag):
                    new_prop = onto_element.onto_elem(item.attrib["{http://www.w3.org/1999/02/22-rdf-syntax-ns#}about"], item.tag.replace("{http://www.w3.org/2002/07/owl#}", ""), item.text)
                    add_children(new_prop, item.getchildren())
                    tempProps.append(new_prop)
                #get all class elements
                elif item.tag == "{http://www.w3.org/2002/07/owl#}Class":
                    #create a new ontology element
                    elem = onto_element.onto_elem(item.attrib["{http://www.w3.org/1999/02/22-rdf-syntax-ns#}about"], item.tag.replace("{http://www.w3.org/2002/07/owl#}", ""), item.text)
                    #util.write2File("test1.txt", "Created " + item.attrib["{http://www.w3.org/1999/02/22-rdf-syntax-ns#}about"] + ": " + str(elem) + "\n", "a")
                    for attr in item.attrib:
                        elem.add_attribute(attr, item.attrib[attr])
                    #get the children
                    add_children(elem, item.getchildren())
                    tempElems.append(elem)
                #if the ontology is populated with individuals, get them, too
                elif item.tag == "{http://www.w3.org/2002/07/owl#}NamedIndividual":
                    #create a new ontology element
                    elem = onto_element.onto_elem(item.attrib["{http://www.w3.org/1999/02/22-rdf-syntax-ns#}about"], item.tag.replace("{http://www.w3.org/2002/07/owl#}", ""), item.text)
                    #util.write2File("test1.txt", "Created " + item.attrib["{http://www.w3.org/1999/02/22-rdf-syntax-ns#}about"] + ": " + str(elem) + "\n", "a")
                    for attr in item.attrib:
                        elem.add_attribute(attr, item.attrib[attr])
                    #get the children
                    add_children(elem, item.getchildren())
                    tempElems.append(elem)
            if onto != "" and len(tempProps) > 0:
                onto.add_properties(tempProps)
            if onto != "" and len(tempElems) > 0:
                onto.add_elements(tempElems)
            #get the namespaces from the RDF element (the root) and put them into the ontology
            for attr in root.attrib:
                onto.add_namespace(attr, root.attrib[attr])
            if onto != "":
                #util.write2File("onto.txt", str(onto) + os.linesep, "a")
                #util.write2File("onto.txt", str(onto.get_elements()) + os.linesep, "a")
                return onto
            else:
                return ""
Beispiel #3
0
def add_children(node, children):
    for child in children:
        new_elem = onto_element.onto_elem(child.tag,  child.tag.split("}")[-1], child.text)
        for attr in child.attrib:
            new_elem.add_attribute(attr, child.attrib[attr])
        if len(child.getchildren()) > 0:
            add_children(new_elem, child)
        node.add_child(new_elem)
def parse_ontology_file(terminology_file):
    print "Parsing " + terminology_file
    #get the URI of the terminology and create a new instance of ontology
    json_content = util.readFileContentAsString(terminology_file)
    jo = json.loads(json_content)
    onto = ontology.ontology(os.path.basename(terminology_file))
    #get all terms of the termonology
    tempElems = []
    #get all terms and create them as instances of onto_elem
    for term in jo["results"]:
        elem = onto_element.onto_elem(term["uri"], "", "")
        elem.add_child(onto_element.onto_elem("{http://www.w3.org/2000/01/rdf-schema#}label",  "{http://www.w3.org/2000/01/rdf-schema#}label", term["label"]))
        #save the term
        tempElems.append(elem)
        #print elem.tostring()
    if onto != "" and len(tempElems) > 0:
        onto.add_elements(tempElems)
    if onto != "":
        #util.write2File("onto.txt", str(onto) + os.linesep, "a")
        #util.write2File("onto.txt", str(onto.get_elements()) + os.linesep, "a")
        return onto
    else:
        return ""