Skip to content
Snippets Groups Projects
Commit 999265a8 authored by Cyril Pommier's avatar Cyril Pommier
Browse files

Handle trait class generation

parent aa151487
No related branches found
No related tags found
No related merge requests found
......@@ -15,6 +15,30 @@ from pathlib import Path, PurePosixPath
co = Namespace("http://www.cropontology.org/rdf/")
trait_classes={}
def get_trait_class(output_graph, a_row):
co_prefix=a_row.get('Trait ID').split(":")[0]
if a_row.get('Trait class') not in trait_classes:
trait_class_id=co_prefix+":900000"+str(len(trait_classes))
trait_class_URI = "http://www.cropontology.org/rdf/"+trait_class_id
trait_classes[a_row.get('Trait class')]=trait_class_URI
add_triple(output_graph, URIRef(trait_class_URI), RDF.type, co.TraitClass )
add_triple(output_graph, URIRef(trait_class_URI), co.hasId, Literal(trait_class_id) )
add_triple(output_graph, URIRef(trait_class_URI), co.hasName, Literal(a_row.get('Trait class')) )
return trait_classes[a_row.get('Trait class')]
# dict is of the form: label:id
#def add_trait_class_triples(output_graph, a_dict):
# for a_key in a_dict:
# trait_class_URI="http://www.cropontology.org/rdf/" + a_dict[a_key]
# add_triple(output_graph, trait_class_URI, RDF.type, co.TraitClass )
# add_triple(output_graph, trait_class_URI, co.hasId, Literal(a_dict[a_key]) )
# add_triple(output_graph, trait_class_URI, co.hasName, Literal(a_key) )
def add_triple(output_graph, s, p, o):
if o:
output_graph.add((s, p, o))
......@@ -75,7 +99,8 @@ def load_csv2graph(csv_file) -> Graph():
add_triple(output_graph,URIRef(traitURI), co.hasTraitId, to_literal(row, 'Trait ID'))
add_triple(output_graph,URIRef(traitURI), RDF.type, co.Trait)
add_triple(output_graph,URIRef(traitURI), co.hasName, to_literal_lang(row, 'Trait name'))
add_triple(output_graph,URIRef(traitURI), co.hasTraitClass, to_literal(row, 'Trait class'))
#add_triple(output_graph,URIRef(traitURI), co.hasTraitClass, to_literal(row, 'Trait class'))
add_triple(output_graph, URIRef(get_trait_class(output_graph, row)) , co.hasTrait, URIRef(traitURI))
add_triple(output_graph,URIRef(traitURI), co.hasDescription, to_literal_lang(row, 'Trait description'))
for tsyn in row['Trait synonyms'].split(','):
add_triple(output_graph,URIRef(traitURI), co.hasSynonym, Literal(tsyn))
......@@ -115,6 +140,9 @@ def load_csv2graph(csv_file) -> Graph():
add_triple(output_graph,URIRef(scaleURI), co.hasCategory9, Literal(row['Category 9']))
add_triple(output_graph,URIRef(scaleURI), co.hasCategory10, Literal(row['Category 10']))
# add trait classes to the graph
#add_trait_class_triples(output_graph, trait_classes)
return output_graph
def read_graph_and_write_to_file(file_name):
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment