org.obolibrary.obo2owl.IdTranslator Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of owlapi-oboformat Show documentation
Show all versions of owlapi-oboformat Show documentation
A java library for converting obo format documents to OWL, and for converting (a subset of) OWL to obo format. This version has been slightly modified to be included directly in the OWL API.
The upstream code for this module and its authors can be found at https://code.google.com/p/oboformat/.
The newest version!
package org.obolibrary.obo2owl;
import java.util.HashMap;
import java.util.Map;
import javax.annotation.Nullable;
import org.semanticweb.owlapi.model.IRI;
/**
* This class will eventually replace the id to IRI translation in Owl2Obo and OboO2Owl
* It is currently in-progress.
*
* @author cjm
*/
public class IdTranslator {
static final String OBO_IRI_PREFIX = "http://purl.obolibrary.org/obo/";
private final Map idspaceMap = new HashMap<>();
/**
* @param iri iri
* @return string for iri
*/
@Nullable
public static String translateIRI(@SuppressWarnings("unused") IRI iri) {
return null;
}
/**
* True if id starts with a standard URI prefix (http, ftp, https) followed by a ":". Does not
* check if it actually conforms to URI syntax.
*
* @param id id
* @return boolean
*/
public static boolean isURI(String id) {
if (id.startsWith("http:") || id.startsWith("ftp:") || id.startsWith("https:")) {
return true;
}
return false;
}
/**
* @param id id
* @return string for id
*/
@Nullable
public String translateIdToIRIString(String id) {
if (isURI(id)) {
return id;
}
if (id.contains(":")) {
// PREFIXED ID
int p = id.lastIndexOf(':');
String prefix = id.substring(0, p);
String localId = id.substring(p + 1);
if (!localId.isEmpty() && localId.replaceAll("[0-9]", "").isEmpty()) {
// CANONICAL
return expandPrefix(prefix) + localId;
}
}
return null;
}
/**
* Expands an OBO prefix such as "GO" to "http://purl.obolibrary.org/obo/GO_". By default a
* prefix XX maps to http://purl.obolibrary.org/obo/XX_
*
* @param prefix prefix
* @return expanded prefix
*/
public String expandPrefix(String prefix) {
if (idspaceMap.containsKey(prefix)) {
return idspaceMap.get(prefix);
}
return OBO_IRI_PREFIX + prefix + '_';
}
}