-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathReadOWLAndAttachInformation.java
More file actions
432 lines (403 loc) · 15.6 KB
/
Copy pathReadOWLAndAttachInformation.java
File metadata and controls
432 lines (403 loc) · 15.6 KB
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
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
package com.spirit.DMRE.Utils;
import java.io.BufferedReader;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map.Entry;
import java.util.logging.Logger;
import org.eclipse.rdf4j.model.IRI;
import org.eclipse.rdf4j.model.Literal;
import org.eclipse.rdf4j.model.Model;
import org.eclipse.rdf4j.model.Statement;
import org.eclipse.rdf4j.model.Value;
import org.eclipse.rdf4j.model.ValueFactory;
import org.eclipse.rdf4j.model.impl.SimpleBNode;
import org.eclipse.rdf4j.model.impl.SimpleValueFactory;
import org.eclipse.rdf4j.model.util.ModelBuilder;
import org.eclipse.rdf4j.model.vocabulary.OWL;
import org.eclipse.rdf4j.model.vocabulary.RDF;
import org.eclipse.rdf4j.rio.*;
import org.hl7.fhir.instance.model.api.IBaseBundle;
import org.hl7.fhir.instance.model.api.IBaseResource;
import org.hl7.fhir.r4.model.Bundle;
import org.hl7.fhir.r4.model.Bundle.BundleEntryComponent;
import org.hl7.fhir.r4.model.Observation;
import org.hl7.fhir.r4.model.Patient;
import com.jayway.jsonpath.JsonPath;
import com.spirit.DMRE.SparqlJsonClient;
import ca.uhn.fhir.context.FhirContext;
import ca.uhn.fhir.rest.client.api.IGenericClient;
import ca.uhn.fhir.rest.gclient.ReferenceClientParam;
import ca.uhn.fhir.rest.gclient.TokenClientParam;
import ca.uhn.fhir.util.BundleUtil;
/**
*
* @author gerhard
*
*/
public class ReadOWLAndAttachInformation {
FhirContext ctx = FhirContext.forR4();
IGenericClient client;
HashMap <String, List<String>> resourcesMap= new HashMap<String,List<String>>();
ModelBuilder builder = new ModelBuilder();
ValueFactory factory = SimpleValueFactory.getInstance();
private static Logger logger = Logger.getLogger("com.spirit.DMRE.ReadOWLAndAttachInformation");
public ReadOWLAndAttachInformation() {
}
/**
*
* @param streamForParsing
* @param patientID
* @throws RDFParseException
* @throws UnsupportedRDFormatException
* @throws IOException
* @throws InstantiationException
* @throws IllegalAccessException
* @throws ClassNotFoundException
*/
public void parseOWLforIRIs(InputStream streamForParsing, String patientID) throws RDFParseException, UnsupportedRDFormatException, IOException, InstantiationException, IllegalAccessException, ClassNotFoundException {
System.out.println("==> " + this.getClass().getCanonicalName() + " " +
"parseOWLforIRIs" + " "
);
HashMap<String,List<String>> classProperties = new HashMap<String,List<String>>();
List<String> propertiesList = null;
ByteArrayOutputStream baos = new ByteArrayOutputStream();
byte[] buf = new byte[1024];
int n = 0;
while ((n = streamForParsing.read(buf)) >= 0)
baos.write(buf, 0, n);
byte[] content = baos.toByteArray();
//doubling the inpuStream for the model and for the submission to the json-query-endpoint
InputStream is1 = new ByteArrayInputStream(content);
InputStream is2 = new ByteArrayInputStream(content);
InputStreamReader isReader = new InputStreamReader(is2);
BufferedReader reader = new BufferedReader(isReader);
StringBuffer sb = new StringBuffer();
String str;
while((str = reader.readLine())!= null){
sb.append(str);
}
//Model model = Rio.parse(streamForParsing,"",RDFFormat.RDFXML);
Model model = Rio.parse(is1,"",RDFFormat.RDFXML);
for(Statement statement:model) {
if(!(statement.getSubject() instanceof SimpleBNode)) {
IRI subject = (IRI)statement.getSubject();
IRI predicate = statement.getPredicate();
Value object = statement.getObject();
logger.fine("===checking for the predicates ===" + str);
logger.finer(statement.toString());
logger.fine("Subject: " + subject.getLocalName());
if(object.equals(OWL.CLASS)) {
String sparqlQuery = this.getSPARQLQueryForProperties("<"+subject.toString()+">");
System.out.println(sparqlQuery);
//askSparqlQuery by using a Client to a http-endpoint
SparqlJsonClient sjc = new SparqlJsonClient();
propertiesList = sjc.generateRequest(sparqlQuery, sb.toString());
classProperties.put(subject.getLocalName(), propertiesList);
}
}
}
//used for asking the FHIR-Store for the classes.
this.client = ctx.newRestfulGenericClient("http://hapi.fhir.org/baseR4");
Bundle bundle = null;
List<IBaseResource> patients = new ArrayList<>();
List<IBaseResource> otherResources = new ArrayList<>();
List<String> jsonPathesObservation = new ArrayList<String>();
List<String> jsonPathesPatient = new ArrayList<String>();
List<String> jsonPathesMedication = new ArrayList<String>();
for(String a : classProperties.keySet()) {
if(a.equalsIgnoreCase("Observation")) {
for (String s : classProperties.get(a)) {
jsonPathesObservation.add("$."+this.cutPrefix("http://dmre/", s));
}
}
if(a.equalsIgnoreCase("Patient")) {
for (String s : classProperties.get(a)) {
jsonPathesPatient.add("$."+this.cutPrefix("http://dmre/", s));
}
}
if(a.equalsIgnoreCase("Medication")) {
for (String s : classProperties.get(a)) {
jsonPathesMedication.add("$."+this.cutPrefix("http://dmre/", s));
}
}
}
for(String fhirObject : classProperties.keySet()) {
for(String propertyOfClass : classProperties.get(fhirObject)) {
String propToWhere = this.cutPrefix("http://dmre/", propertyOfClass.toLowerCase());
if(fhirObject.equalsIgnoreCase("patient")) {
try {
bundle = (Bundle) client.search()
.forResource(fhirObject)
.where(new TokenClientParam("_id").exactly().code(patientID))
.returnBundle(Bundle.class)
.execute();
patients.addAll(BundleUtil.toListOfResources(ctx, bundle));
getValueFromBundleAndPutToMap(bundle,jsonPathesPatient);
while(bundle.getLink(IBaseBundle.LINK_NEXT) != null) {
bundle = client.loadPage().next(bundle).execute();
patients.addAll(BundleUtil.toListOfResources(ctx, bundle));
getValueFromBundleAndPutToMap(bundle,jsonPathesPatient);
}
}catch(Exception e) {
logger.fine("Exception happened, but proceeding");
}
}else {
try {
bundle = (Bundle) client.search()
.forResource(fhirObject)
.where(new ReferenceClientParam("patient").hasId(patientID))
.returnBundle(Bundle.class)
.execute();
getValueFromBundleAndPutToMap(bundle,jsonPathesObservation);
otherResources.addAll(BundleUtil.toListOfResources(ctx, bundle));
while(bundle.getLink(IBaseBundle.LINK_NEXT) != null) {
bundle = client.loadPage().next(bundle).execute();
getValueFromBundleAndPutToMap(bundle,jsonPathesObservation);
}
}catch(Exception e) {
logger.fine("Exception happened, but proceeding");
}
}
}
}
this.putFHIRBundleInformationToOWL(resourcesMap, classProperties, model);
}
/**
*
* @param resourcesMap2
* @param classProperties
* @param model
*/
public void putFHIRBundleInformationToOWL(HashMap<String, List<String>> resourcesMap2, HashMap<String,List<String>> classProperties, Model model) {
System.out.println("==> " + this.getClass().getCanonicalName() + " " +
"putFHIRBundleInformationToOWL" + " "
);
for(String a : classProperties.keySet()) {
logger.fine(a);
for (String s : classProperties.get(a)) {
logger.fine("- " +s);
addInfoToModel(a, s, model);
}
}
Rio.write(model,System.out,RDFFormat.RDFXML);
}
/**
*
* @param topClass
* @param property
* @param model
* @return
*/
private Model addInfoToModel(String topClass, String property, Model model) {
System.out.println("==> " + this.getClass().getCanonicalName() + " " +
"addInfoToModel" + " "
);
property = this.cutPrefix("http://dmre/",property);
for(Entry<String, List<String>> ID : resourcesMap.entrySet()) {
if(ID.getKey().contains(topClass)) {
//System.out.println(ID.getKey() + " " + ID.getValue());
for(String savedProperty : ID.getValue()) {
if(savedProperty.contains(property)) {
//tidy
if(savedProperty != null && property != null && savedProperty.startsWith(property)) {
savedProperty = savedProperty.substring(property.length());
}
//getting the property
savedProperty = savedProperty.substring(savedProperty.indexOf(":")+1);
savedProperty.trim();
//replacing brackets
savedProperty = savedProperty.replaceAll("\\[", "(").replaceAll("\\]", ")");
property = property.replaceAll("\\[", "(").replaceAll("\\]", ")");
//creating the needed IRIs and Literals for the graph
IRI entity = factory.createIRI(ID.getKey());
IRI topClassIRI = factory.createIRI("http://dmre/" + topClass);
IRI propConnectionToOwnIdentity = factory.createIRI("http://dmre/"+property);
Literal propToAdd = factory.createLiteral(savedProperty);
model.add(entity,RDF.TYPE,topClassIRI);
model.add(entity,propConnectionToOwnIdentity,propToAdd);
}
}
}
}
return model;
}
/**
*
* @param allEntries
* @param resourceType equals to fhirObject in the code -> resourceType is the FHIR-wording
* @param property
* @throws Exception
*/
public void getValueFromBundleAndPutToMap(Bundle bundle, List<String> jsonpath) throws Exception{
System.out.println("==> " + this.getClass().getCanonicalName() + " " +
"getValueFromBundleAndPutToMap" + " "
);
for (int i = 0; i< bundle.getEntry().size();i++) {
List<String> allProperties = new ArrayList<String>();
BundleEntryComponent bt = bundle.getEntry().get(i);
for(String singlejsonPath : jsonpath) {
String value = JsonPath.read(ctx.newJsonParser()
.encodeResourceToString(bt.getResource())
, singlejsonPath).toString();
allProperties.add(singlejsonPath+":"+value);
}
resourcesMap.put(bt.getResource().getId(), new ArrayList<>(allProperties));
}
}
/**
*
* @param allEntries
* @param resourceType
* @param property
*/
public void getFhirValueFromObjectProperty(List<Bundle> allEntries, String resourceType, String property) {
System.out.println("==> " + this.getClass().getCanonicalName() + " " +
"getFhirValueFromObjectProperty" + " "
);
// System.out.println("getFhirValueFromObjectProperty");
// System.out.println("allEntriesSize: " + allEntries.size());
// System.out.println("RESOURCEType: " + resourceType);
// System.out.println("PROPERTY: " + property);
for(Bundle singleBundle : allEntries) {
for(BundleEntryComponent bundleEntry : singleBundle.getEntry()) {
if((bundleEntry.getResource().getResourceType().toString().equalsIgnoreCase(resourceType))) {
//System.out.println("Entry having ResourceType " + resourceType );
}else {
System.out.println("something strange happened");
System.out.println(bundleEntry.getResource().getResourceType().toString());
System.out.println(resourceType);
System.out.println("--------");
}
}
}
}
/**
*
* @param owlClassName
* @return
*/
public String getSPARQLQueryForProperties(String owlClassName) {
System.out.println("==> " + this.getClass().getCanonicalName() + " " +
"getSPARQLQueryForProperties" + " "
);
String sparql =
"prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>\n" +
"prefix owl: <http://www.w3.org/2002/07/owl#>\n" +
"prefix xsd: <http://www.w3.org/2001/XMLSchema#>\n" +
"prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#>\n" +
"prefix dmre: <http://dmre>\n" +
"select ?class ?property where { \n" +
" ?property rdfs:domain "+owlClassName+" .\n" +
" ?property a owl:DatatypeProperty .\n" +
" }";
return sparql;
}
/**
*
* @param prefix
* @param toCut
* @return
*/
public String cutPrefix(String prefix, String toCut) {
System.out.println("==> " + this.getClass().getCanonicalName() + " " +
"cutPrefix" + " "
);
if(toCut != null && prefix != null && toCut.startsWith(prefix)) {
toCut = toCut.substring(prefix.length());
}
//replace () by [] - >this is needed because rdf4j complains about []
toCut = toCut.replaceAll("\\(", "[").replaceAll("\\)", "]");
return toCut;
}
/**
*
* @param args
* @throws RDFParseException
* @throws UnsupportedRDFormatException
* @throws IOException
* @throws InstantiationException
* @throws IllegalAccessException
* @throws ClassNotFoundException
*/
public static void main(String[] args) throws RDFParseException, UnsupportedRDFormatException, IOException, InstantiationException, IllegalAccessException, ClassNotFoundException {
ReadOWLAndAttachInformation a = new ReadOWLAndAttachInformation();
//a.ReadOWL();
String data = "definingOntology.owl;
File initialFile = new File(data);
InputStream targetStream = new FileInputStream(initialFile);
System.out.println("Testcase 1 - lot's of entries");
a.parseOWLforIRIs(targetStream,"5a5f527f-ff2f-4486-94f3-8c3494356137");
System.out.println("Testcase 2 - one patient, one observation");
//a.parseOWLforIRIs(targetStream,"1567076");
System.out.println("\nExiting");
}
public void devHelper() {
// Create a context and a client
FhirContext ctx = FhirContext.forR4();
String serverBase = "http://hapi.fhir.org/baseR4";
IGenericClient client = ctx.newRestfulGenericClient(serverBase);
// We'll populate this list
List<IBaseResource> patients = new ArrayList<>();
// We'll do a search for all Patients and extract the first page
Bundle bundle = client
.search()
.forResource(Patient.class)
.where(Patient.NAME.matches().value("orlitsch"))
.returnBundle(Bundle.class)
.execute();
System.out.println("addint patient to ListOfResources");
patients.addAll(BundleUtil.toListOfResources(ctx, bundle));
// Load the subsequent pages
while (bundle.getLink(IBaseBundle.LINK_NEXT) != null) {
bundle = client
.loadPage()
.next(bundle)
.execute();
System.out.println("addint patient to ListOfResources-additional");
patients.addAll(BundleUtil.toListOfResources(ctx, bundle));
}
System.out.println("Loaded " + patients.size() + " patients!");
for(IBaseResource p : patients) {
System.out.println(p.getClass());
if(p instanceof Patient) {
Patient p1 = (Patient) p;
System.out.println(ctx.newJsonParser().encodeResourceToString(p1));
System.out.println("i am instance of patient");
}
}
System.out.println(ctx.newJsonParser().setPrettyPrint(true).encodeResourceToString(patients.get(0)));
Patient p = (Patient) patients.get(0);
String prop = p.getNameFirstRep().getFamily();
System.out.println(prop);
//select patientID from patient-resources and get Observations
p.getIdentifierFirstRep().getValueElement().getValue();
String patientID = p.getIdElement().getIdPart();
System.out.println("The id is: " + p.getId());
System.out.println("The id is: " + p.getIdElement().getIdPart());
//Query for Observations
Bundle observations = (Bundle) client.search()
.forResource(Observation.class)
.where(new ReferenceClientParam("subject").hasId(patientID))
.returnBundle(Bundle.class)
.execute();
for(BundleEntryComponent e : observations.getEntry()) {
Observation obs = (Observation) e.getResource();
//use JSONPath for getting the properties
System.out.println(ctx.newJsonParser().encodeResourceToString(obs));
String JsonObs = ctx.newJsonParser().setPrettyPrint(true).encodeResourceToString((IBaseResource) e);
//System.out.println(JsonObs);
String name = JsonPath.read(JsonObs, "$.subject.reference");
String valueQ = JsonPath.read(JsonObs, "$.valueQuantity.value").toString();
System.out.println("JSONValueQ is: " + valueQ);
System.out.println("JSONPAthResult is : " +name);
}
}
}