<jupyter_start><jupyter_text>Import Python librariesrdflib - https://pypi.python.org/pypi/rdflib<jupyter_code>import os
import rdflib as rdf
#import csv for reading csv files
import csv<jupyter_output><empty_output><jupyter_text>Create new RDF graph<jupyter_code>g = rdf.Graph()<jupyter_output><empty_output><jupyter_text>Add namespaces Add a namespace for each one in the object model<jupyter_code>nidm = rdf.Namespace("http://nidm.nidash.org/")
prov = rdf.Namespace("http://www.w3.org/ns/prov#")
ncit = rdf.Namespace("http://ncitt.ncit.nih.gov/")
nidash = rdf.Namespace("http://purl.org/nidash/nidm/")
fbirn = rdf.Namespace("http://www.birncommunity.org/collaborators/function-birn/")
list(g.namespaces())<jupyter_output><empty_output><jupyter_text>Bind namespaces to graph<jupyter_code>g.bind('nidm', nidm)
g.bind('prov', prov)
g.bind('ncit', ncit)
g.bind('nidash', nidash)
g.bind('fbirn', fbirn)
list(g.namespaces())<jupyter_output><empty_output><jupyter_text>Create function to create element nodes<jupyter_code>def nidm_add_elements(g,line,assessment_name):
    #Create new node for each element with metadata consistent with NIDM-Experiment assessment acquistion
    g.add((nidash[assessment_name+line["SCORENAME"]], rdf.RDF.type, nidash["DataElement"]))
    g.add((nidash[assessment_name+line["SCORENAME"]], rdf.RDF.type, rdf.RDF.Property))
    g.add((nidash[assessment_name+line["SCORENAME"]], rdf.RDF.type, rdf.RDF.Property))
    g.add((nidash[assessment_name+line["SCORENAME"]], prov["label"], rdf.Literal(line["SCORENAME"])))
    g.add((nidash[assessment_name+line["SCORENAME"]], nidash["DataType"], rdf.Literal(line["SCORETYPE"])))
    g.add((nidash[assessment_name+line["SCORENAME"]], nidash["Question"], rdf.Literal(line["QUESTION"])))
    g.add((nidash[assessment_name+line["SCORENAME"]], nidash["Sequence"], rdf.Literal(line["SCORESEQ"])))   
    #make association with assessment
    g.add((nidash[assessment_name],prov["hadMember"], nidash[assessment_name+line["SCORENAME"]]))
def nidm_create_assessment(g, assessment_name):
    g.add((nidash[assessment_name], rdf.RDF.type, nidash["DataStructure"]))
    g.add((nidash[assessment_name],prov["label"],rdf.Literal(assessment_name)))
def nidm_add_codedproperty(g,line,valueset_id, codedvalue_id):
    g.add((nidash[codedvalue_id], rdf.RDF.type, nidash["CodedProperty"]))
    g.add((nidash[codedvalue_id], rdf.RDF.type, rdf.RDF.Property))
    g.add((nidash[codedvalue_id],nidash["code"], rdf.Literal(line["SCORECODE"])))
    g.add((nidash[codedvalue_id], prov["label"], rdf.Literal(line["SCORELABEL"])))
    g.add((nidash[valueset_id], prov["hadMember"], nidash[codedvalue_id]))<jupyter_output><empty_output><jupyter_text>Read in CSV file and convert to RDF graph<jupyter_code>import pandas as pd
xls = pd.ExcelFile("DEMOGRAPHICS_data_dictionary.xls")
variables = xls.parse(0)
value_sets = xls.parse(1)
#variables = pd.read_csv("FBIRNPhaseII_DEMOGRAPHICS_Variables.csv")
#value_sets = pd.read_csv("FBIRNPhaseII_DEMOGRAPHICS_ValueSets.csv")
assessment_name = "FBIRNPhaseIIDemographics"
#iterate over the variables in the data dictionary
for index, row in variables.iterrows():
    #add elements to RDF graph for variable definitions
    nidm_add_elements(g,row,assessment_name) 
    #print "looking for " + row['SCORENAME']
    #look for value sets in data dictionary that match the variable
    query_sets = value_sets[value_sets['SCORENAME'].str.contains(row['SCORENAME'])]
    #iterate over the value set rows and add to RDF graph
    for query_index, query_row in query_sets.iterrows():
        #print query_row['SCORECODE'] + "," + query_row['SCORELABEL'] 
        #add attribute to coded data element for valueset
        valueset_id = assessment_name+row["SCORENAME"]+"ValueSet"
        g.add((nidash[assessment_name+row["SCORENAME"]], nidash["ValueSet"], nidash[valueset_id]))
        #create value set collection
        g.add((nidash[valueset_id], rdf.RDF.type, prov["Collection"]))
        g.add((nidash[valueset_id], rdf.RDF.type, nidash["ValueSet"]))
        nidm_add_codedproperty(g, query_row, valueset_id ,row["SCORENAME"] + "_" + query_row["SCORECODE"] )
        

print g.serialize(format='turtle')<jupyter_output>@prefix fbirn: <http://www.birncommunity.org/collaborators/function-birn/> .
@prefix ncit: <http://ncitt.ncit.nih.gov/> .
@prefix nidash: <http://purl.org/nidash/nidm/> .
@prefix nidm: <http://nidm.nidash.org/> .
@prefix prov: <http://www.w3.org/ns/prov#> .
@prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> .
@prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#> .
@prefix xml: <http://www.w3.org/XML/1998/namespace> .
@prefix xsd: <http://www.w3.org/2001/XMLSchema#> .

nidash:FBIRNPhaseIIDemographics prov:hadMember nidash:FBIRNPhaseIIDemographicsAge,
        nidash:FBIRNPhaseIIDemographicsEducation,
        nidash:FBIRNPhaseIIDemographicsEthnicity,
        nidash:FBIRNPhaseIIDemographicsFathersEducation,
        nidash:FBIRNPhaseIIDemographicsFathersOccupation,
        nidash:FBIRNPhaseIIDemographicsGender,
        nidash:FBIRNPhaseIIDemographicsHandedness,
        nidash:FBIRNPhaseIIDemographicsLivingArrangement,
        nidash:FBIRNPhaseIIDemographicsMaritalStatus,
        nid[...]<jupyter_text>Save RDF Turtle file to disk<jupyter_code>with open("FBIRNPhaseII_DEMOGRAPHICS_DataDictionary.ttl",'w') as f:
    f.write(g.serialize(format='turtle'))<jupyter_output><empty_output><jupyter_text>Create RDF graph from DOT file<jupyter_code>from rdflib.tools import rdf2dot
with open("FBIRNPhaseII_DEMOGRAPHICS_DataDictionary.dot",'w') as f:
    s = rdf2dot.rdf2dot(g, f)

import pydot
g = pydot.graph_from_dot_file("FBIRNPhaseII_DEMOGRAPHICS_DataDictionary.dot")
g.write_png('"FBIRNPhaseII_DEMOGRAPHICS_DataDictionary.png')<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.14860412692907923, 'fraction_numerical': 0.005722212588867696, 'mean_word_length': 6.292035398230088, 'pattern_counts': {'":': 0, '<': 41, '<?xml version=': 0, '>': 41, 'https://': 1, 'lorem ipsum': 0, 'www.': 8, 'xml': 1}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '8', 'dir': 'jupyter-structured-clean-dedup', 'id': '68423', 'license': 'Apache-2.0', 'n_tokens_mistral': 2251, 'n_tokens_neox': 2186, 'n_words': 303, 'path': 'nidm/nidm-experiment/fbirn_clinical/FBIRNPhaseII_Demographics_to_NIDM.ipynb', 'repo_name': 'INCF/nidm', 'size': '5767'}