-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathConvert_DMP_from_JSON_to_RDF.py
338 lines (288 loc) · 21 KB
/
Convert_DMP_from_JSON_to_RDF.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
import json, re
from rdflib import Graph, Literal, Namespace, RDF, URIRef, SDO, DC, RDFS
#Different levels of things
#UseTemplate new relations, hasTemplate
#Template consists of DMP questions,
rdf = RDF
sdo = SDO
dc = DC
# Specify the path to the JSON files
path_to_files = 'C:/Users/MSI-NB/PycharmProjects/firstProject/JSON DMP Files/'
dmp_file_ids = [111527]
#Other files with 1 - VU DMP template 2021 (NWO & ZonMW certified) v1.3"
#[ 111527, 111548, 111764]112581
# Create an RDF graph
graph = Graph()
# Define namespaces
fdo = Namespace("https://fairdmp.online/eco-system/")
dmp_ns = Namespace(("https://fairdmp.online/dmp/vu/"+str(dmp_file_ids[0])))
fip = Namespace("https://peta-pico.github.io/FAIR-nanopubs/fip/index-en.html#https://w3id.org/fair/fip/terms/")
# Bind namespaces to prefixes
graph.bind("fip", fip)
graph.bind("fdo", fdo)
graph.bind("dmp_file", dmp_ns)
# remove the HTML tags that come from the JSON file
def remove_html_tags(text):
"""Remove html tags from a string"""
clean = re.compile('<.*?>')
return re.sub(clean, '', text)
# Iterate over the JSON files and convert to TTL triples
for file_id in dmp_file_ids:
file_name = str(file_id) + '.json'
with open(path_to_files + file_name) as file:
data = json.load(file)
dmp = data[0][0]
# Extract and add template information
file_node = URIRef(dmp_ns)
graph.add((file_node, RDFS.label, Literal("DMP with the id "+str(file_id))))
template = dmp['template']
template_title = template.get('title')
template_id = template.get('id')
if template_title == "1 - VU DMP template 2021 (NWO & ZonMW certified) v1.3":
vu_template = URIRef("https://fairdmp.online/dmp/vu/VU-DMP-template-2021-NWO-ZonMW-certified-v1.3")
graph.add((URIRef(file_node), fdo.usesDMPTemplate, vu_template))
else:
graph.add((URIRef(file_node), fdo.usesDMPTemplate, Literal(template_title)))
# Extract and add data contact information
data_contact = dmp.get('data_contact')
if data_contact:
data_contact_name = data_contact.get('name')
data_contact_email = data_contact.get('email')
graph.add((file_node, fdo.hasDataContact, fdo.DMPDataContact))
if data_contact_name:
graph.add((fdo.DMPDataContact, sdo.name, Literal(data_contact_name)))
if data_contact_email:
graph.add((fdo.DMPDataContact, sdo.email, Literal(data_contact_email)))
# Extract and add project funder information
funder = dmp.get('funder')
if funder:
funder_name = funder.get('name')
if funder_name:
graph.add((file_node, sdo.funder, Literal(funder_name)))
#Extract and add DMP description
description = dmp.get('description')
if description:
graph.add((file_node, dc.description, Literal(remove_html_tags(description))))
# Extract and add section data
plan_contents = dmp.get('plan_content', [])
for plan in plan_contents:
sections = plan.get('sections', [])
for section in sections:
section_title = remove_html_tags(section.get('title'))
section_number = int(section.get('number')-1)
section_node = URIRef(dmp_ns + "/section/" + str(section_number))
section_description = remove_html_tags(section.get('description'))
if section_node:
graph.add((file_node, fdo.consistOf, section_node))
graph.add((section_node, rdf.type, fdo.Section))
graph.add((section_node, RDFS.label, Literal("Section " + str(section_number) + " of DMP" + str(file_id))))
if section_title:
graph.add((section_node, dc.title, Literal(section_title)))
# Add section number if it exists
if section_number:
graph.add((section_node, sdo.identifier, Literal(section_number)))
if section_description:
graph.add((section_node, dc.description, Literal(section_description)))
# Extract and add section questions
questions = section.get('questions', [])
for question in questions:
question_text = remove_html_tags(question.get('text'))
question_number = question.get('number')
answer = question.get('answer')
question_node = URIRef(dmp_ns + "/section/" + str(section_number) + "/question/" + str(question_number))
graph.add((question_node, RDFS.label, Literal("Section "+str(section_number)+ " Question "+ str(question_number) + " of DMP" + str(file_id))))
#Get the relations to the FAIR Principles
#real section index 0
if section_number == 0:
if question_number == 4:
graph.add((question_node, fdo["questionRefersToPrincipal"], fip.F1))
# Duty level+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
graph.add((sdo.Researcher, fdo.isResponsibleForDMPQuestion, question_node))
#Impact level funder has to be a class
graph.add((fdo.UniversityResearchDataManagement, fdo.hasImpactOn, question_node))
graph.add((fdo.FacultyResearchDataManagement, fdo.hasImpactOn, question_node))
graph.add((fdo.DepartmentResearchDataManagement, fdo.hasImpactOn, question_node))
graph.add((question_node, fdo.recommendedAnswer, Literal("Full name, Role, University Email, Faculty, Department and ORCID")))
if funder_name:
graph.add((Literal(funder_name), fdo.hasImpactOn, fdo.question_node))
#Get Author
name_pattern = r"<p>(.*?)</p>"
match = re.search(name_pattern, str(answer))
if match:
name = match.group(1)
graph.add((file_node, sdo.author, Literal(name)))
if question_number == 5:
graph.add((question_node, fdo["questionRefersToPrincipal"], fip.F1))
# Duty level+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
graph.add((sdo.Researcher, fdo.isResponsibleForDMPQuestion, question_node))
# Impact level
graph.add((fdo.UniversityResearchDataManagement, fdo.hasImpactOn, question_node))
graph.add((fdo.FacultyResearchDataManagement, fdo.hasImpactOn, question_node))
graph.add((fdo.DepartmentResearchDataManagement, fdo.hasImpactOn, question_node))
graph.add((question_node, fdo.recommendedAnswer, Literal("Full name, Role, University Email, Faculty, Department and ORCID")))
if funder_name:
graph.add((Literal(funder_name), fdo.hasImpactOn, fdo.question_node))
if question_number == 6:
graph.add((question_node, fdo["questionRefersToPrincipal"], fip.F1))
# Duty level+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
graph.add((sdo.Researcher, fdo.isResponsibleForDMPQuestion, question_node))
#Impact level
if funder_name:
graph.add((Literal(funder_name), fdo.hasImpactOn, fdo.question_node))
if question_number == 7:
graph.add((question_node, fdo["questionRefersToPrincipal"], fip.F1))
if question_number == 8:
graph.add((question_node, fdo["questionRefersToPrincipal"], fip.F1))
# Duty level+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
graph.add((sdo.Researcher, fdo.isResponsibleForDMPQuestion, question_node))
# Impact level
graph.add((fdo.UniversityResearchDataManagement, fdo.hasImpactOn, question_node))
graph.add((fdo.FacultyResearchDataManagement, fdo.hasImpactOn, question_node))
graph.add((fdo.DepartmentResearchDataManagement, fdo.hasImpactOn, question_node))
if funder_name:
graph.add((Literal(funder_name), fdo.hasImpactOn, fdo.question_node))
#get data steward if exists
if answer:
answer_text = (answer.get('text', ''))
graph.add((file_node, fip.hasDataSteward, (Literal(remove_html_tags(answer_text)))))
#real section index 1-------------------------------------------------------------------------------
if section_number == 1:
if question_number in (4,5,6,7,8,9):
graph.add((question_node, fdo.requiredBy, fdo.VuLegalTeam))
#real section index 2-------------------------------------------------------------------------------
if section_number == 2:
if question_number in (1,2,4,5):
graph.add((question_node, fdo.requiredBy, fdo.VuLegalTeam))
#real section index 3-------------------------------------------------------------------------------
if section_number == 3:
if question_number in (1,3,4,5,7):
graph.add((question_node, fdo.requiredBy, fdo.VuLegalTeam))
#real Section index 4-------------------------------------------------------------------------------
if section_number == 4:
if question_number == 2:
graph.add((question_node, fdo["questionRefersToPrincipal"], fip.F4))
# Duty level+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
graph.add((sdo.Researcher, fdo.isResponsibleForDMPQuestion, question_node))
#Impact level
graph.add((fdo.FacultyResearchDataManagement, fdo.hasImpactOn, question_node))
graph.add((fdo.RDMPlatform, fdo.hasImpactOn, question_node))
#Suggested Answer
graph.add((question_node, fdo.recommendedAnswer, Literal("Yoda (rather than the two old ones), DataverseNL, OSF")))
if question_number == 3:
graph.add((question_node, fdo["questionRefersToPrincipal"], fip.F4))
# Duty level+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
graph.add((sdo.Researcher, fdo.isResponsibleForDMPQuestion, question_node))
#Impact level
graph.add((fdo.FacultyResearchDataManagement, fdo.hasImpactOn, question_node))
if question_number == 4:
graph.add((question_node, fdo.recommendedAnswer, Literal("10 Years")))
if question_number == 5:
graph.add((question_node, fdo.recommendedAnswer, Literal("10 Years")))
if question_number == 6:
graph.add((question_node, fdo["questionRefersToPrincipal"], fip.F4))
# Duty level+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
graph.add((fdo.RDMPlatform, fdo.isResponsibleForDMPQuestion, question_node))
# Impact level
graph.add((fdo.RDMPlatform, fdo.hasImpactOn, question_node))
graph.add((question_node, fdo.recommendedAnswer, Literal("Yoda (rather than the two old ones), DataverseNL, OSF")))
if question_number == 7:
graph.add((question_node, fdo["questionRefersToPrincipal"], fip.F4))
# Duty level+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
graph.add((fdo.RDMPlatform, fdo.isResponsibleForDMPQuestion, question_node))
# Impact level
graph.add((fdo.RDMPlatform, fdo.hasImpactOn, question_node))
graph.add((question_node, fdo.recommendedAnswer, Literal("Yoda (rather than the two old ones), DataverseNL, OSF")))
if question_number == 8:
graph.add((question_node, fdo["questionRefersToPrincipal"], fip.F1))
# Duty level+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
graph.add((sdo.Researcher, fdo.isResponsibleForDMPQuestion, question_node))
# Impact level
graph.add((fdo.UniversityResearchDataManagement, fdo.hasImpactOn, question_node))
graph.add((fdo.FacultyResearchDataManagement, fdo.hasImpactOn, question_node))
graph.add((fdo.DepartmentResearchDataManagement, fdo.hasImpactOn, question_node))
graph.add((fdo.RDMPlatform, fdo.hasImpactOn, question_node))
graph.add((question_node, fdo.recommendedAnswer, Literal("RDMSupport ensures that each dataset has a PID")))
if funder_name:
graph.add((Literal(funder_name), fdo.hasImpactOn, fdo.question_node))
if question_number == 9:
graph.add((question_node, fdo["questionRefersToPrincipal"], fip.F1))
# Duty level+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
graph.add((sdo.Researcher, fdo.isResponsibleForDMPQuestion, question_node))
# Impact level
graph.add((fdo.UniversityResearchDataManagement, fdo.hasImpactOn, question_node))
graph.add((fdo.FacultyResearchDataManagement, fdo.hasImpactOn, question_node))
graph.add((fdo.DepartmentResearchDataManagement, fdo.hasImpactOn, question_node))
graph.add((fdo.RDMPlatform, fdo.hasImpactOn, question_node))
if funder_name:
graph.add((Literal(funder_name), fdo.hasImpactOn, fdo.question_node))
if question_number == 10:
graph.add((question_node, fdo["questionRefersToPrincipal"], fip["A1.2"]))
# Duty level+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
graph.add((sdo.Researcher, fdo.isResponsibleForDMPQuestion, question_node))
# Impact level
graph.add((fdo.RDMPlatform, fdo.hasImpactOn, question_node))
graph.add((question_node, fdo.recommendedAnswer, Literal("Use VU data-classification tool to find out the level of security needed")))
if question_number == 11:
graph.add((question_node, fdo["questionRefersToPrincipal"], fip["A1.2"]))
# Duty level+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
graph.add((sdo.Researcher, fdo.isResponsibleForDMPQuestion, question_node))
graph.add((fdo.RDMSupport, fdo.isResponsibleForDMPQuestion, question_node))
# Impact level
graph.add((fdo.RDMPlatform, fdo.hasImpactOn, question_node))
if question_number == 12:
# Duty level+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
graph.add((sdo.Researcher, fdo.isResponsibleForDMPQuestion, question_node))
graph.add((fdo.RDMSupport, fdo.isResponsibleForDMPQuestion, question_node))
if question_number == 13:
graph.add((question_node, fdo["questionRefersToPrincipal"], fip["R1.1"]))
# Duty level+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
graph.add((sdo.Researcher, fdo.isResponsibleForDMPQuestion, question_node))
#Legal Team
if template_title == "1 - VU DMP template 2021 (NWO & ZonMW certified) v1.3" and question_number in (4,5,6,10,11,12):
graph.add((question_node, fdo.requiredBy, fdo.VuLegalTeam))
#real Section index 5-------------------------------------------------------------------------------
if section_number == 5:
if question_number == 1:
graph.add((question_node, fdo["questionRefersToPrincipal"], fip["R1.2"]))
graph.add((question_node, fdo["questionRefersToPrincipal"], fip["F2"]))
# Duty level+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
graph.add((fdo.RDMPlatform, fdo.isResponsibleForDMPQuestion, question_node))
graph.add((sdo.Researcher, fdo.isResponsibleForDMPQuestion, question_node))
# Impact level
graph.add((fdo.RDMPlatform, fdo.hasImpactOn, question_node))
graph.add((question_node, fdo.recommendedAnswer, Literal("Description of the data: use minimal metadata schema")))
if question_number == 2:
graph.add((question_node, fdo["questionRefersToPrincipal"], fip["R1.3"]))
graph.add((question_node, fdo["questionRefersToPrincipal"], fip.F2))
# Duty level+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
graph.add((fdo.RDMPlatform, fdo.isResponsibleForDMPQuestion, question_node))
graph.add((sdo.Researcher, fdo.isResponsibleForDMPQuestion, question_node))
# Impact level
graph.add((fdo.RDMPlatform, fdo.hasImpactOn, question_node))
graph.add((question_node, fdo.recommendedAnswer, Literal("Description of the data: use minimal metadata schema")))
if question_number == 3:
graph.add((question_node, fdo["questionRefersToPrincipal"], fip["R1.2"]))
# Duty level+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
graph.add((fdo.RDMPlatform, fdo.isResponsibleForDMPQuestion, question_node))
graph.add((sdo.Researcher, fdo.isResponsibleForDMPQuestion, question_node))
#Impact level
#real Section index 6-------------------------------------------------------------------------------
if section_number == 6:
if question_number == 3:
graph.add((question_node, fdo["questionRefersToPrincipal"], fip["A1.2"]))
# Duty level+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
graph.add((sdo.Researcher, fdo.isResponsibleForDMPQuestion, question_node))
graph.add((fdo.UniversityResearchDataManagement, fdo.isResponsibleForDMPQuestion, question_node))
graph.add((fdo.FacultyResearchDataManagement, fdo.isResponsibleForDMPQuestion, question_node))
# Impact level
graph.add((fdo.RDMPlatform, fdo.hasImpactOn, question_node))
if question_number and question_text:
graph.add((question_node, rdf.type, fdo.DataManagementPlanQuestion))
graph.add((question_node, dc.title, Literal(question_text)))
graph.add((section_node, fdo.consistsOf, question_node))
# Add answer text if it exists
if answer:
answer_text = remove_html_tags(answer.get('text', ''))
graph.add((question_node, sdo.acceptedAnswer, (Literal(answer_text))))
# Serialize the graph to TTL format
graph.serialize(destination='C:/Users/MSI-NB/PycharmProjects/firstProject/ttl_files/DMP'+str(file_id)+'_converted.ttl', format='turtle')