Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
117 changes: 117 additions & 0 deletions dataload/extras/json2sssom/src/main/java/CurieMap.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
public CurieMap() {
addMapping("semapv", "https://w3id.org/semapv/vocab/");
addMapping("owl", "http://www.w3.org/2002/07/owl#");
addMapping("rdfs", "http://www.w3.org/2000/01/rdf-schema#");
addMapping("skos", "http://www.w3.org/2004/02/skos/core#");
addMapping("oboInOwl", "http://www.geneontology.org/formats/oboInOwl#");
addMapping("chebi", "http://purl.obolibrary.org/obo/chebi/");
Expand All @@ -31,10 +32,37 @@
addMapping("smiles", "https://bioregistry.io/smiles:");
}


private static boolean isIRI(String str) {
if (str == null || str.trim().isEmpty()) {
return false;
}

String trimmed = str.trim();

if (!trimmed.contains(":")) {
return false;
}

int colonIndex = trimmed.indexOf(':');
if (colonIndex == 0 || colonIndex == trimmed.length() - 1) {
return false;
}

String scheme = trimmed.substring(0, colonIndex);
if (scheme.isEmpty() || !scheme.matches("^[a-zA-Z][a-zA-Z0-9+.-]*$")) {
return false;
}

return true;
}


public CurieMapping mapEntity(JsonObject entityOrLinkedEntity) {

String iriOrUrl = null;


if(entityOrLinkedEntity.has("iri")) {
iriOrUrl = entityOrLinkedEntity.get("iri").getAsString();
} else if(entityOrLinkedEntity.has("url")) {
Expand Down Expand Up @@ -122,4 +150,93 @@
}


public CurieMapping mapEntity(JsonObject entityOrLinkedEntity, String valueIRIOrUrl) {

String iriOrUrl = null;


if(entityOrLinkedEntity.has("iri")) {
iriOrUrl = entityOrLinkedEntity.get("iri").getAsString();
} else if(entityOrLinkedEntity.has("url")) {
iriOrUrl = entityOrLinkedEntity.get("url").getAsString();
}

if(!entityOrLinkedEntity.has("curie")) {
if(iriOrUrl == null) {
return null;
}
CurieMapping mapping = new CurieMapping();
mapping.iriOrUrl = iriOrUrl;
return mapping;
}

String curie = JsonHelper.getFirstStringValue(entityOrLinkedEntity.get("curie"));

if(!curie.contains(":")) {
System.out.println("curie provided by OLS " + curie + " does not look like a curie, in entity/linkedEntity: " + gson.toJson(entityOrLinkedEntity));
// TODO ???
return null;
}

String curiePrefix = curie.split(":")[0];
String curieLocalPart = curie.split(":")[1];
if (iriOrUrl == null && isIRI(valueIRIOrUrl)) {
iriOrUrl = valueIRIOrUrl;
}
if(iriOrUrl == null || !iriOrUrl.endsWith(curieLocalPart)) {
System.out.println(iriOrUrl + " does not end with local part of curie " + curie + ". This mapping will be omitted from the results.");

// We can't print the iri/url in SSSOM and we can't put the CURIE in the prefix map
// TODO: Currently we just drop the mapping, maybe a better way to approach this.
//
return null;
// CurieMapping mapping = new CurieMapping();
// mapping.curiePrefix = curiePrefix;
// mapping.curieLocalPart = curieLocalPart;
// mapping.curie = curie;
// return mapping;
}

String curieNamespace = iriOrUrl.substring(0, iriOrUrl.length() - curieLocalPart.length());

if(curiePrefixToNamespace.containsKey(curiePrefix)) {

String existingNs = curiePrefixToNamespace.get(curiePrefix);

if(!existingNs.equals(curieNamespace)) {

String origCurieForDebugLog = curiePrefix;

Check notice

Code scanning / CodeQL

Unread local variable Note

Variable 'String origCurieForDebugLog' is never read.

Copilot Autofix

AI about 2 months ago

In general, an unread local variable should either be removed or used. Since origCurieForDebugLog is only relevant to a commented-out debug print and has no other role, the most straightforward fix that preserves existing functionality is to delete its declaration.

Concretely, in dataload/extras/json2sssom/src/main/java/CurieMap.java, within the second occurrence of the block that handles if (!existingNs.equals(curieNamespace)), remove the line:

String origCurieForDebugLog = curiePrefix;

No additional imports, methods, or definitions are needed. This change does not alter any runtime behavior because the variable was never read.

Suggested changeset 1
dataload/extras/json2sssom/src/main/java/CurieMap.java

Autofix patch

Autofix patch
Run the following command in your local git repository to apply this patch
cat << 'EOF' | git apply
diff --git a/dataload/extras/json2sssom/src/main/java/CurieMap.java b/dataload/extras/json2sssom/src/main/java/CurieMap.java
--- a/dataload/extras/json2sssom/src/main/java/CurieMap.java
+++ b/dataload/extras/json2sssom/src/main/java/CurieMap.java
@@ -214,8 +214,6 @@
 
             if(!existingNs.equals(curieNamespace)) {
 
-                String origCurieForDebugLog = curiePrefix;
-
                 // try to find a different curie prefix for this namespace
                 String nsToCp = namespaceToCuriePrefix.get(curieNamespace);
                 if(nsToCp != null) {
EOF
@@ -214,8 +214,6 @@

if(!existingNs.equals(curieNamespace)) {

String origCurieForDebugLog = curiePrefix;

// try to find a different curie prefix for this namespace
String nsToCp = namespaceToCuriePrefix.get(curieNamespace);
if(nsToCp != null) {
Copilot is powered by AI and may make mistakes. Always verify output.

// try to find a different curie prefix for this namespace
String nsToCp = namespaceToCuriePrefix.get(curieNamespace);
if(nsToCp != null) {
curiePrefix = nsToCp;
curie = curiePrefix + ":" + curieLocalPart;
} else {
// establish this namespace as a curie prefix
int n = 2;
while(curiePrefixToNamespace.containsKey(curiePrefix + "_" + n)) {
++ n;
}
curiePrefix = curiePrefix + "_" + n;
curie = curiePrefix + ":" + curieLocalPart;
addMapping(curiePrefix, curieNamespace);
}
// System.out.println("Namespace " + curieNamespace + " did not match existing namespace " + existingNs + " for curie prefix " + origCurieForDebugLog + ". Using " + curiePrefix + " instead. In entity/linkedEntity: " + gson.toJson(entityOrLinkedEntity));
}

} else {
addMapping(curiePrefix, curieNamespace);
}

CurieMapping mapping = new CurieMapping();
mapping.iriOrUrl = iriOrUrl;
mapping.curie = curie;
mapping.curiePrefix = curiePrefix;
mapping.curieLocalPart = curieLocalPart;
mapping.curieNamespace = curieNamespace;
return mapping;
}


}
40 changes: 40 additions & 0 deletions dataload/extras/json2sssom/src/main/java/JSON2SSSOM.java
Original file line number Diff line number Diff line change
Expand Up @@ -157,15 +157,55 @@ public static void writeMappingsForEntity(JsonObject entity, CSVPrinter writer,
JsonElement exactMatch = entity.get("http://www.w3.org/2004/02/skos/core#exactMatch");
if(exactMatch != null) {
writeMappingsForEntity(entity, "skos:exactMatch", exactMatch, null, null, ontologyProperties, writer, curieMap);
}
JsonElement narrowMatch = entity.get("http://www.w3.org/2004/02/skos/core#narrowMatch");
if(narrowMatch != null) {
writeMappingsForEntity(entity, "skos:narrowMatch", narrowMatch, null, null, ontologyProperties, writer, curieMap);
}
JsonElement broadMatch = entity.get("http://www.w3.org/2004/02/skos/core#broadMatch");
if(broadMatch != null) {
writeMappingsForEntity(entity, "skos:broadMatch", broadMatch, null, null, ontologyProperties, writer, curieMap);
}
JsonElement relatedMatch = entity.get("http://www.w3.org/2004/02/skos/core#relatedMatch");
if(relatedMatch != null) {
writeMappingsForEntity(entity, "skos:relatedMatch", relatedMatch, null, null, ontologyProperties, writer, curieMap);
}
JsonElement closeMatch = entity.get("http://www.w3.org/2004/02/skos/core#closeMatch");
if(closeMatch != null) {
writeMappingsForEntity(entity, "skos:closeMatch", closeMatch, null, null, ontologyProperties, writer, curieMap);
}

JsonElement hasDbXref = entity.get("http://www.geneontology.org/formats/oboInOwl#hasDbXref");
if(hasDbXref != null) {
writeMappingsForEntity(entity, "oboInOwl:hasDbXref", hasDbXref, null, null, ontologyProperties, writer, curieMap);
}


JsonElement equivalentClass = entity.get("http://www.w3.org/2002/07/owl#equivalentClass");
if(equivalentClass != null) {
writeMappingsForEntity(entity, "owl:equivalentClass", equivalentClass, null, null, ontologyProperties, writer, curieMap);

}
JsonElement equivalentProperty = entity.get("http://www.w3.org/2002/07/owl#equivalentProperty");
if(equivalentProperty != null) {
writeMappingsForEntity(entity, "owl:equivalentProperty", equivalentProperty, null, null, ontologyProperties, writer, curieMap);
}
JsonElement sameAs = entity.get("http://www.w3.org/2002/07/owl#sameAs");
if(sameAs != null) {
writeMappingsForEntity(entity, "owl:sameAs", sameAs, null, null, ontologyProperties, writer, curieMap);
}

JsonElement subClassOf = entity.get("http://www.w3.org/2000/01/rdf-schema#subClassOf");
if(subClassOf != null) {
writeMappingsForEntity(entity, "rdfs:subClassOf", subClassOf, null, null, ontologyProperties, writer, curieMap);

}
JsonElement subPropertyOf = entity.get("http://www.w3.org/2000/01/rdf-schema#subPropertyOf");
if(subPropertyOf != null) {
writeMappingsForEntity(entity, "rdfs:subPropertyOf", subPropertyOf, null, null, ontologyProperties, writer, curieMap);

}


// hacky special cases for chemical specific mapping predicates
//
Expand Down
Loading