use of owltools.io.ParserWrapper in project minerva by geneontology.
the class CommandLineInterface method owl2LegoJson.
/**
* Convert a GO-CAM owl file to a minerva json structure
* --owl-lego-to-json
*
* @param input
* @param output
* @param usePretty
* @throws Exception
*/
public static void owl2LegoJson(String input, String output, boolean usePretty) throws Exception {
// minimal inputs
if (input == null) {
System.err.println("No input model was configured.");
System.exit(-1);
return;
}
if (output == null) {
System.err.println("No output file was configured.");
System.exit(-1);
return;
}
// configuration
CurieHandler curieHandler = DefaultCurieHandler.getDefaultHandler();
GsonBuilder gsonBuilder = new GsonBuilder();
if (usePretty) {
gsonBuilder.setPrettyPrinting();
}
Gson gson = gsonBuilder.create();
// process each model
if (LOGGER.isInfoEnabled()) {
LOGGER.info("Loading model from file: " + input);
}
OWLOntology model = null;
final JsonModel jsonModel;
ParserWrapper pw = new ParserWrapper();
try {
// load model
model = pw.parseOWL(IRI.create(new File(input).getCanonicalFile()));
// TODO decide if we need reasoning
InferenceProvider inferenceProvider = null;
String modelId = null;
Optional<IRI> ontologyIRI = model.getOntologyID().getOntologyIRI();
if (ontologyIRI.isPresent()) {
modelId = curieHandler.getCuri(ontologyIRI.get());
}
// render json
final MolecularModelJsonRenderer renderer = new MolecularModelJsonRenderer(modelId, model, inferenceProvider, curieHandler);
jsonModel = renderer.renderModel();
} finally {
if (model != null) {
pw.getManager().removeOntology(model);
model = null;
}
}
// save as json string
final String json = gson.toJson(jsonModel);
final File outputFile = new File(output).getCanonicalFile();
try (OutputStream outputStream = new FileOutputStream(outputFile)) {
if (LOGGER.isInfoEnabled()) {
LOGGER.info("Saving json to file: " + outputFile);
}
IOUtils.write(json, outputStream);
}
}
use of owltools.io.ParserWrapper in project minerva by geneontology.
the class EcoMapperFactory method createEcoMapper.
/**
* Create an instance of a {@link EcoMapper}. Uses a the manager to load ECO via the
* PURL. Load mappings using the PURL.
*
* @param m
* @return mapper pair
* @throws OWLException
* @throws IOException
* @see EcoMapper#ECO_PURL
* @see EcoMapper#ECO_MAPPING_PURL
*/
public static OntologyMapperPair<EcoMapper> createEcoMapper(OWLOntologyManager m) throws OWLException, IOException {
ParserWrapper p = new ParserWrapper();
p.setManager(m);
return createEcoMapper(p);
}
use of owltools.io.ParserWrapper in project ontologies by enanomapper.
the class FindLabelDuplicationWithinOneMergedOntology method testOverlap.
public OverlapResult testOverlap(String ontologyName) throws Exception {
ParserWrapper pw = new ParserWrapper();
OWLOntology owlOntology = pw.parseOWL(ontologyName);
String iri = owlOntology.getOntologyID().getOntologyIRI().toString();
String iriChanged = iri + "_merged.owl";
System.out.println("IRI: " + iriChanged);
owlOntology = OntologyHelper.loadAllImportedAxioms(owlOntology, iriChanged);
OWLDataFactory fac = pw.getManager().getOWLDataFactory();
OverlapResult result = new OverlapResult();
for (OWLClass c : owlOntology.getClassesInSignature()) {
Set<OWLAnnotation> annos = c.getAnnotations(owlOntology, fac.getRDFSLabel());
for (OWLAnnotation anno : annos) {
String annoVal = anno.getValue().toString().replaceAll("\"", "").replaceAll("@", "");
String classIRI = c.toStringID();
if (annoVal != null && !annoVal.isEmpty()) {
result.allLabels.add(annoVal);
if (!result.labelsToURIs.containsKey(annoVal)) {
List<String> newList = new ArrayList<String>();
newList.add(classIRI);
result.labelsToURIs.put(annoVal, newList);
} else {
result.labelsToURIs.get(annoVal).add(classIRI);
}
}
}
}
// start analytics
for (String label : result.allLabels) {
List<String> uris = result.labelsToURIs.get(label);
if (uris.size() > 1) {
// There is a duplicate
result.labelsDuplicated.add(label);
}
}
return result;
}
use of owltools.io.ParserWrapper in project ontologies by enanomapper.
the class FindLabelOverlapBetweenOntologies method testOverlap.
public OverlapResult testOverlap(String ontologyOne, String ontologyTwo, boolean getImportsClosure) throws Exception {
ParserWrapper pw = new ParserWrapper();
OWLOntology owlOntoOne = pw.parseOWL(ontologyOne);
OWLOntology owlOntoTwo = pw.parseOWL(ontologyTwo);
if (getImportsClosure) {
String iriOne = owlOntoOne.getOntologyID().getOntologyIRI().toString();
String iriTwo = owlOntoTwo.getOntologyID().getOntologyIRI().toString();
String iriOneChanged = iriOne + "_merged.owl";
String iriTwoChanged = iriTwo + "_merged.owl";
System.out.println("ONE: " + iriOneChanged);
System.out.println("TWO: " + iriTwoChanged);
owlOntoOne = OntologyHelper.loadAllImportedAxioms(owlOntoOne, iriOneChanged);
owlOntoTwo = OntologyHelper.loadAllImportedAxioms(owlOntoTwo, iriTwoChanged);
}
OWLDataFactory fac = pw.getManager().getOWLDataFactory();
OverlapResult result = new OverlapResult();
for (OWLClass c : owlOntoOne.getClassesInSignature()) {
Set<OWLAnnotation> annos = c.getAnnotations(owlOntoOne, fac.getRDFSLabel());
for (OWLAnnotation anno : annos) {
String annoVal = anno.getValue().toString().replaceAll("\"", "").replaceAll("@", "");
String classIRI = c.toStringID();
if (annoVal != null && !annoVal.isEmpty()) {
result.allLabelsOne.add(annoVal);
result.labelsToURIsOne.put(annoVal, classIRI);
}
}
}
for (OWLClass c : owlOntoTwo.getClassesInSignature()) {
Set<OWLAnnotation> annos = c.getAnnotations(owlOntoTwo, fac.getRDFSLabel());
for (OWLAnnotation anno : annos) {
String annoVal = anno.getValue().toString().replaceAll("\"", "").replaceAll("@", "");
String classIRI = c.toStringID();
if (annoVal != null && !annoVal.isEmpty()) {
result.allLabelsTwo.add(annoVal);
result.labelsToURIsTwo.put(annoVal, classIRI);
}
}
}
// start analytics
for (String labelInOne : result.allLabelsOne) {
if (result.allLabelsTwo.contains(labelInOne)) {
if (result.labelsToURIsOne.get(labelInOne).equals(result.labelsToURIsTwo.get(labelInOne))) {
result.labelsAndIdsShared.add(labelInOne);
} else {
result.labelsShared.add(labelInOne);
}
} else {
result.labelsInOneNotTwo.add(labelInOne);
}
}
for (String labelInTwo : result.allLabelsTwo) {
if (!result.allLabelsOne.contains(labelInTwo)) {
result.labelsInTwoNotOne.add(labelInTwo);
}
}
return result;
}
use of owltools.io.ParserWrapper in project ontologies by enanomapper.
the class OntologyHelper method openOntologyFromURL.
public static OWLOntology openOntologyFromURL(String url) {
ParserWrapper pw = new ParserWrapper();
try {
URL website = new URL(url);
HttpURLConnection connection = (HttpURLConnection) website.openConnection();
InputStream is = openConnectionCheckRedirects(connection);
BufferedReader in = new BufferedReader(new InputStreamReader(is));
StringBuilder response = new StringBuilder();
String inputLine;
while ((inputLine = in.readLine()) != null) response.append(inputLine);
in.close();
// create a temp file to store the content
File temp = File.createTempFile("tempfile", url.lastIndexOf(".") > 0 ? url.substring(url.lastIndexOf(".")) : ".owl");
// write it
BufferedWriter bw = new BufferedWriter(new FileWriter(temp));
bw.write(response.toString());
bw.close();
OWLOntology o = pw.parse(temp.toString());
return o;
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
Aggregations