@@ -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}
0 commit comments