from rdflib.graph import ConjunctiveGraph
from rdflib.namespace import RDF, OWL


def main():
    make_property_graph()


def make_property_graph():
    graph = ConjunctiveGraph()
    output_graph = ConjunctiveGraph()

    ontologies = [
        'https://raw.githubusercontent.com/monarch-initiative/SEPIO-ontology/master/src/ontology/sepio.owl',
        'https://raw.githubusercontent.com/monarch-initiative/GENO-ontology/develop/src/ontology/geno.owl',
        'https://raw.githubusercontent.com/oborel/obo-relations/master/ro.owl'
    ]

    for ontology in ontologies:
        print(ontology)
        graph.parse(ontology, format='xml')

    # Get object properties
    query = """
                SELECT ?property
                WHERE {
                    ?property a owl:ObjectProperty .
                }
            """
    query_result = graph.query(query)
    output_graph = add_to_graph(query_result, output_graph)

    # Get annotation properties
    query = """
               SELECT ?property
               WHERE {
                    ?property a owl:AnnotationProperty .
                }
            """
    query_result = graph.query(query)
    output_graph = add_to_graph(query_result, output_graph)

    # Get data properties
    query = """
                SELECT ?property
                WHERE {
                    ?property a owl:DatatypeProperty .
                }
            """
    query_result = graph.query(query)
    output_graph = add_to_graph(query_result, output_graph)
    output_graph.serialize('property-graph.ttl', format="turtle")

    return


def add_to_graph(results, graph):
    for row in results:
        graph.add((row[0], RDF['type'], OWL['ObjectProperty']))
    return graph


if __name__ == "__main__":
    main()


@@ -880,33 +880,51 @@
 graph = add_
-to_graph(
+property_to_graph(%0A        
 query_result
@@ -929,32 +929,55 @@
 lt, output_graph
+, OWL%5B'ObjectProperty'%5D
 )%0A%0A    # Get ann
@@ -1222,43 +1222,88 @@
 add_
-to_graph(query_result, output_graph
+property_to_graph(%0A        query_result, output_graph, OWL%5B'AnnotationProperty'%5D
 )%0A%0A 
@@ -1550,25 +1550,43 @@
 h = add_
-to_graph(
+property_to_graph(%0A        
 query_re
@@ -1603,16 +1603,41 @@
 ut_graph
+, OWL%5B'DatatypeProperty'%5D
 )%0A    ou
@@ -1718,16 +1718,25 @@
 def add_
+property_
 to_graph
@@ -1750,16 +1750,26 @@
 s, graph
+, property
 ):%0A    f
@@ -1827,37 +1827,24 @@
 type'%5D, 
-OWL%5B'ObjectP
+p
 roperty
-'%5D
 ))%0A    r

fix property graph script