Skip to content

Commit 5f93bba

Browse files
Merge pull request #1143 from EBISPOT/sssom_predicates_update
Added missing SSSOM predicates
2 parents e5f94fa + 51c2704 commit 5f93bba

File tree

2 files changed

+170
-1
lines changed

2 files changed

+170
-1
lines changed

dataload/extras/json2sssom/src/main/java/CurieMap.java

Lines changed: 126 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ public static class CurieMapping {
2323
public CurieMap() {
2424
addMapping("semapv", "https://w3id.org/semapv/vocab/");
2525
addMapping("owl", "http://www.w3.org/2002/07/owl#");
26+
addMapping("rdfs", "http://www.w3.org/2000/01/rdf-schema#");
2627
addMapping("skos", "http://www.w3.org/2004/02/skos/core#");
2728
addMapping("oboInOwl", "http://www.geneontology.org/formats/oboInOwl#");
2829
addMapping("chebi", "http://purl.obolibrary.org/obo/chebi/");
@@ -31,10 +32,37 @@ public CurieMap() {
3132
addMapping("smiles", "https://bioregistry.io/smiles:");
3233
}
3334

35+
36+
private static boolean isIRI(String str) {
37+
if (str == null || str.trim().isEmpty()) {
38+
return false;
39+
}
40+
41+
String trimmed = str.trim();
42+
43+
if (!trimmed.contains(":")) {
44+
return false;
45+
}
46+
47+
int colonIndex = trimmed.indexOf(':');
48+
if (colonIndex == 0 || colonIndex == trimmed.length() - 1) {
49+
return false;
50+
}
51+
52+
String scheme = trimmed.substring(0, colonIndex);
53+
if (scheme.isEmpty() || !scheme.matches("^[a-zA-Z][a-zA-Z0-9+.-]*$")) {
54+
return false;
55+
}
56+
57+
return true;
58+
}
59+
60+
3461
public CurieMapping mapEntity(JsonObject entityOrLinkedEntity) {
3562

3663
String iriOrUrl = null;
3764

65+
3866
if(entityOrLinkedEntity.has("iri")) {
3967
iriOrUrl = entityOrLinkedEntity.get("iri").getAsString();
4068
} else if(entityOrLinkedEntity.has("url")) {
@@ -122,4 +150,102 @@ private void addMapping(String curiePrefix, String curieNamespace) {
122150
}
123151

124152

153+
public CurieMapping mapEntity(JsonObject entityOrLinkedEntity, String valueIRIOrUrl) {
154+
155+
String iriOrUrl = null;
156+
157+
158+
if(entityOrLinkedEntity.has("iri")) {
159+
iriOrUrl = entityOrLinkedEntity.get("iri").getAsString();
160+
} else if(entityOrLinkedEntity.has("url")) {
161+
iriOrUrl = entityOrLinkedEntity.get("url").getAsString();
162+
}
163+
164+
if(!entityOrLinkedEntity.has("curie")) {
165+
if(iriOrUrl == null) {
166+
return null;
167+
}
168+
CurieMapping mapping = new CurieMapping();
169+
mapping.iriOrUrl = iriOrUrl;
170+
return mapping;
171+
}
172+
173+
String curie = JsonHelper.getFirstStringValue(entityOrLinkedEntity.get("curie"));
174+
175+
if(!curie.contains(":")) {
176+
if (curie.length() > 100) {
177+
curie = curie.substring(0, 100);
178+
System.out.println("Curie provided by OLS " + curie +
179+
" does not look like a curie, in entity/linkedEntity: " + gson.toJson(entityOrLinkedEntity) +
180+
" . Curie truncated to 100 characters.");
181+
} else {
182+
System.out.println("Curie provided by OLS " + curie +
183+
184+
" does not look like a curie, in entity/linkedEntity: " + gson.toJson(entityOrLinkedEntity));
185+
}
186+
// TODO ???
187+
return null;
188+
}
189+
190+
String curiePrefix = curie.split(":")[0];
191+
String curieLocalPart = curie.split(":")[1];
192+
if (iriOrUrl == null && isIRI(valueIRIOrUrl)) {
193+
iriOrUrl = valueIRIOrUrl;
194+
}
195+
if(iriOrUrl == null || !iriOrUrl.endsWith(curieLocalPart)) {
196+
System.out.println(iriOrUrl + " does not end with local part of curie " + curie + ". This mapping will be omitted from the results.");
197+
198+
// We can't print the iri/url in SSSOM and we can't put the CURIE in the prefix map
199+
// TODO: Currently we just drop the mapping, maybe a better way to approach this.
200+
//
201+
return null;
202+
// CurieMapping mapping = new CurieMapping();
203+
// mapping.curiePrefix = curiePrefix;
204+
// mapping.curieLocalPart = curieLocalPart;
205+
// mapping.curie = curie;
206+
// return mapping;
207+
}
208+
209+
String curieNamespace = iriOrUrl.substring(0, iriOrUrl.length() - curieLocalPart.length());
210+
211+
if(curiePrefixToNamespace.containsKey(curiePrefix)) {
212+
213+
String existingNs = curiePrefixToNamespace.get(curiePrefix);
214+
215+
if(!existingNs.equals(curieNamespace)) {
216+
217+
String origCurieForDebugLog = curiePrefix;
218+
219+
// try to find a different curie prefix for this namespace
220+
String nsToCp = namespaceToCuriePrefix.get(curieNamespace);
221+
if(nsToCp != null) {
222+
curiePrefix = nsToCp;
223+
curie = curiePrefix + ":" + curieLocalPart;
224+
} else {
225+
// establish this namespace as a curie prefix
226+
int n = 2;
227+
while(curiePrefixToNamespace.containsKey(curiePrefix + "_" + n)) {
228+
++ n;
229+
}
230+
curiePrefix = curiePrefix + "_" + n;
231+
curie = curiePrefix + ":" + curieLocalPart;
232+
addMapping(curiePrefix, curieNamespace);
233+
}
234+
// System.out.println("Namespace " + curieNamespace + " did not match existing namespace " + existingNs + " for curie prefix " + origCurieForDebugLog + ". Using " + curiePrefix + " instead. In entity/linkedEntity: " + gson.toJson(entityOrLinkedEntity));
235+
}
236+
237+
} else {
238+
addMapping(curiePrefix, curieNamespace);
239+
}
240+
241+
CurieMapping mapping = new CurieMapping();
242+
mapping.iriOrUrl = iriOrUrl;
243+
mapping.curie = curie;
244+
mapping.curiePrefix = curiePrefix;
245+
mapping.curieLocalPart = curieLocalPart;
246+
mapping.curieNamespace = curieNamespace;
247+
return mapping;
248+
}
249+
250+
125251
}

dataload/extras/json2sssom/src/main/java/JSON2SSSOM.java

Lines changed: 44 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -157,15 +157,55 @@ public static void writeMappingsForEntity(JsonObject entity, CSVPrinter writer,
157157
JsonElement exactMatch = entity.get("http://www.w3.org/2004/02/skos/core#exactMatch");
158158
if(exactMatch != null) {
159159
writeMappingsForEntity(entity, "skos:exactMatch", exactMatch, null, null, ontologyProperties, writer, curieMap);
160+
}
161+
JsonElement narrowMatch = entity.get("http://www.w3.org/2004/02/skos/core#narrowMatch");
162+
if(narrowMatch != null) {
163+
writeMappingsForEntity(entity, "skos:narrowMatch", narrowMatch, null, null, ontologyProperties, writer, curieMap);
160164
}
165+
JsonElement broadMatch = entity.get("http://www.w3.org/2004/02/skos/core#broadMatch");
166+
if(broadMatch != null) {
167+
writeMappingsForEntity(entity, "skos:broadMatch", broadMatch, null, null, ontologyProperties, writer, curieMap);
168+
}
169+
JsonElement relatedMatch = entity.get("http://www.w3.org/2004/02/skos/core#relatedMatch");
170+
if(relatedMatch != null) {
171+
writeMappingsForEntity(entity, "skos:relatedMatch", relatedMatch, null, null, ontologyProperties, writer, curieMap);
172+
}
173+
JsonElement closeMatch = entity.get("http://www.w3.org/2004/02/skos/core#closeMatch");
174+
if(closeMatch != null) {
175+
writeMappingsForEntity(entity, "skos:closeMatch", closeMatch, null, null, ontologyProperties, writer, curieMap);
176+
}
177+
161178
JsonElement hasDbXref = entity.get("http://www.geneontology.org/formats/oboInOwl#hasDbXref");
162179
if(hasDbXref != null) {
163180
writeMappingsForEntity(entity, "oboInOwl:hasDbXref", hasDbXref, null, null, ontologyProperties, writer, curieMap);
164181
}
182+
183+
165184
JsonElement equivalentClass = entity.get("http://www.w3.org/2002/07/owl#equivalentClass");
166185
if(equivalentClass != null) {
167186
writeMappingsForEntity(entity, "owl:equivalentClass", equivalentClass, null, null, ontologyProperties, writer, curieMap);
187+
168188
}
189+
JsonElement equivalentProperty = entity.get("http://www.w3.org/2002/07/owl#equivalentProperty");
190+
if(equivalentProperty != null) {
191+
writeMappingsForEntity(entity, "owl:equivalentProperty", equivalentProperty, null, null, ontologyProperties, writer, curieMap);
192+
}
193+
JsonElement sameAs = entity.get("http://www.w3.org/2002/07/owl#sameAs");
194+
if(sameAs != null) {
195+
writeMappingsForEntity(entity, "owl:sameAs", sameAs, null, null, ontologyProperties, writer, curieMap);
196+
}
197+
198+
JsonElement subClassOf = entity.get("http://www.w3.org/2000/01/rdf-schema#subClassOf");
199+
if(subClassOf != null) {
200+
writeMappingsForEntity(entity, "rdfs:subClassOf", subClassOf, null, null, ontologyProperties, writer, curieMap);
201+
202+
}
203+
JsonElement subPropertyOf = entity.get("http://www.w3.org/2000/01/rdf-schema#subPropertyOf");
204+
if(subPropertyOf != null) {
205+
writeMappingsForEntity(entity, "rdfs:subPropertyOf", subPropertyOf, null, null, ontologyProperties, writer, curieMap);
206+
207+
}
208+
169209

170210
// hacky special cases for chemical specific mapping predicates
171211
//
@@ -269,7 +309,7 @@ public static void writeMappingsForEntity(
269309

270310
JsonObject linkedEntity = linkedEntityElem.getAsJsonObject();
271311

272-
CurieMap.CurieMapping objCurie = curieMap.mapEntity(linkedEntity);
312+
CurieMap.CurieMapping objCurie = curieMap.mapEntity(linkedEntity, value);
273313

274314
if(objCurie == null) {
275315
return;
@@ -318,6 +358,9 @@ public static void writeMappingsForEntity(
318358
}
319359
}
320360

361+
private static boolean isNotOWLAxiom(){
362+
return true;
363+
}
321364

322365

323366

0 commit comments

Comments
 (0)