Skip to content

Commit

Permalink
Merge ed63056 into 84ce6cb
Browse files Browse the repository at this point in the history
  • Loading branch information
julesjacobsen committed Mar 15, 2017
2 parents 84ce6cb + ed63056 commit acbfc76
Show file tree
Hide file tree
Showing 12 changed files with 556 additions and 92 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
import java.util.Map;
import java.util.Objects;
import java.util.Set;
import java.util.function.BiConsumer;
import java.util.zip.GZIPInputStream;

/**
Expand All @@ -34,13 +35,15 @@ public class Ontology {
private final OWLOntology owlOntology;

private final OWLOntologyManager ontologyManager;
private final OWLDataFactory owlDataFactory;

private Ontology(OntologySourceData sourceData, Concurrency concurrency) {
Objects.requireNonNull(sourceData, "Unable to create Ontology without data sources.");
this.sourceData = sourceData;
this.curieUtil = new CurieUtil(sourceData.getCuries());
this.ontologyManager = createOntologyManager(concurrency);
this.owlOntology = createEmptyOntology(ontologyManager);
this.owlDataFactory = ontologyManager.getOWLDataFactory();
loadOwlOntology();
}

Expand Down Expand Up @@ -81,12 +84,29 @@ public CurieUtil getCurieUtil() {
return curieUtil;
}

/**
* @param curie
* @return
*/
public OWLClass getOWLClass(String curie) {
return owlDataFactory.getOWLClass(toIri(curie));
}

public OWLNamedIndividual getOWLNamedIndividual(String curie) {
return owlDataFactory.getOWLNamedIndividual(toIri(curie));
}

public String toCurie(IRI iri) {
String iriString = iri.toString();
return curieUtil.getCurie(iriString).orElse(iriString);
}

private void loadOwlOntology() {
//Order matters here - don't change it.
mergeOntologies(sourceData.getOntologies());
mergeOntologies(sourceData.getDataOntologies());
loadDataFromTsv(sourceData.getDataTsvs());
loadDataFromPairwiseMappings(sourceData.getPairwiseMappings());
loadDataFromMap(sourceData.getIndividuals());
logger.info("Ontology loaded");
}

Expand Down Expand Up @@ -191,8 +211,20 @@ private OWLOntology loadDataFromTsvGzip(String path) {
return owlOntology;
}

private void loadDataFromPairwiseMappings(Map<String, String> pairwiseMappings) {
pairwiseMappings.forEach(this::addInstanceOf);
private void loadDataFromMap(Map<String, Collection<String>> individuals) {
if(!individuals.isEmpty()){
logger.info("Loading individuals from map");
}
//e.g. 'ORPHA:710': ['HP:0000194','HP:0000218','HP:0000262','HP:0000303','HP:0000316']
individuals.forEach(addIndividual());
}

private BiConsumer<String, Collection<String>> addIndividual() {
return (individual, annotations) -> {
for (String curie : annotations) {
addInstanceOf(individual, curie);
}
};
}

private void loadLineIntoDataOntology(String line) {
Expand All @@ -204,15 +236,18 @@ private void loadLineIntoDataOntology(String line) {
}

private void addInstanceOf(String individual, String ontologyClass) {
// logger.info("Adding axiom " + individual + ": " + ontologyClass);
OWLDataFactory owlDataFactory = ontologyManager.getOWLDataFactory();
OWLClass owlClass = owlDataFactory.getOWLClass(toIri(ontologyClass));
OWLNamedIndividual owlNamedIndividual = owlDataFactory.getOWLNamedIndividual(toIri(individual));
OWLClassAssertionAxiom axiom = owlDataFactory.getOWLClassAssertionAxiom(owlClass, owlNamedIndividual);
addAxiom(axiom);
Objects.requireNonNull(individual, "Individual identifier cannot be null. Check your input.");
Objects.requireNonNull(ontologyClass, "Class identifier(s) cannot be null. Check your input.");
if (!ontologyClass.isEmpty()) {
// logger.info("Adding axiom " + individual + ": " + ontologyClass);
OWLClass owlClass = getOWLClass(ontologyClass);
OWLNamedIndividual owlNamedIndividual = getOWLNamedIndividual(individual);
OWLClassAssertionAxiom axiom = owlDataFactory.getOWLClassAssertionAxiom(owlClass, owlNamedIndividual);
addAxiom(axiom);
}
}

private IRI toIri(String id) {
IRI toIri(String id) {
return IRI.create(curieUtil.getIri(id).orElse(id));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,16 +20,17 @@ public class OntologySourceData {
private final List<String> dataOntologies;

private final Map<String, String> curies;
//TODO: shouldn't this be individualsTsvs?
private final List<String> dataTsvs;
//TODO: add these so people can programmatically add individual assertions
private final Map<String, String> pairwiseMappings;
private final Map<String, Collection<String>> individuals;
//TODO - labels?

private OntologySourceData(Builder builder) {
this.ontologies = distinctImmutableListOf(builder.ontologies);
this.dataOntologies = distinctImmutableListOf(builder.dataOntologies);
this.curies = ImmutableMap.copyOf(builder.curies);
this.dataTsvs = distinctImmutableListOf(builder.dataTsvs);
this.pairwiseMappings = ImmutableMap.copyOf(builder.pairwiseMappings);
this.individuals = ImmutableMap.copyOf(builder.individuals);
}

private ImmutableList<String> distinctImmutableListOf(List<String> list) {
Expand All @@ -52,8 +53,8 @@ public List<String> getDataTsvs() {
return dataTsvs;
}

public Map<String, String> getPairwiseMappings() {
return pairwiseMappings;
public Map<String, Collection<String>> getIndividuals() {
return individuals;
}

@Override
Expand All @@ -65,12 +66,12 @@ public boolean equals(Object o) {
Objects.equals(dataOntologies, that.dataOntologies) &&
Objects.equals(curies, that.curies) &&
Objects.equals(dataTsvs, that.dataTsvs) &&
Objects.equals(pairwiseMappings, that.pairwiseMappings);
Objects.equals(individuals, that.individuals);
}

@Override
public int hashCode() {
return Objects.hash(ontologies, dataOntologies, curies, dataTsvs, pairwiseMappings);
return Objects.hash(ontologies, dataOntologies, curies, dataTsvs, individuals);
}

@Override
Expand All @@ -80,7 +81,7 @@ public String toString() {
", dataOntologies=" + dataOntologies +
", curies=" + curies +
", dataTsvs=" + dataTsvs +
", pairwiseMappings=" + pairwiseMappings +
", individuals=" + individuals +
'}';
}

Expand All @@ -94,7 +95,7 @@ public static class Builder {
//Curies need to be supplied if people are adding data using TSV files or pairwise mappings using curies.
private Map<String, String> curies = Collections.emptyMap();
private List<String> dataTsvs = new ArrayList<>();
private Map<String, String> pairwiseMappings = Collections.emptyMap();
private Map<String, Collection<String>> individuals = new HashMap<>();

private Builder(){
//use the static method.
Expand Down Expand Up @@ -176,6 +177,11 @@ public Builder dataTsv(Collection<String> paths) {
return this;
}

public Builder data(Map<String, ? extends Collection<String>> mappings) {
individuals.putAll(mappings);
return this;
}

public OntologySourceData build() {
if(ontologies.isEmpty()) {
throw new OntologyLoadException("No ontology defined.");
Expand All @@ -187,7 +193,7 @@ public OntologySourceData build() {
}

private boolean hasNonOntologyData() {
return !dataTsvs.isEmpty() || !pairwiseMappings.isEmpty();
return !dataTsvs.isEmpty() || !individuals.isEmpty();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,6 @@ public Loader loadCuries(Map<String, String> curies) {
* @param file
*/
public Loader loadOntology(File file) {
Math.random();
sourceDataBuilder.ontology(file);
return this;
}
Expand Down Expand Up @@ -118,6 +117,11 @@ public Loader loadDataFromTsv(Collection<String> paths) {
return this;
}

public Loader loadDataFromMap(Map<String, ? extends Collection<String>> data) {
sourceDataBuilder.data(data);
return this;
}

/**
* Creates an {@link OWLOntologyManager} that is configured with the standard parsers and storers and provides
* locking for concurrent access (default).
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
package org.monarchinitiative.owlsim.kb;

import java.util.Map;
import java.util.Set;

import com.googlecode.javaewah.EWAHCompressedBitmap;
import org.monarchinitiative.owlsim.io.OwlKnowledgeBase;
import org.monarchinitiative.owlsim.kb.impl.BMKnowledgeBaseOWLAPIImpl;
import org.monarchinitiative.owlsim.model.kb.Attribute;
import org.monarchinitiative.owlsim.model.kb.Entity;

import com.googlecode.javaewah.EWAHCompressedBitmap;
import java.util.Map;
import java.util.Set;

/**
* An interface to an ontology in which the fundamental unit of representation of
Expand Down Expand Up @@ -351,7 +351,9 @@ public interface BMKnowledgeBase {
public int getRootIndex();



public static OwlKnowledgeBase.Loader owlLoader() {
return OwlKnowledgeBase.loader();
}



Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
package org.monarchinitiative.owlsim.kb.impl;

import java.time.Duration;
import java.time.Instant;
import java.util.*;
import java.util.stream.Collectors;

import com.google.common.base.Preconditions;
import com.googlecode.javaewah.EWAHCompressedBitmap;
import com.hp.hpl.jena.query.*;
import com.hp.hpl.jena.rdf.model.Literal;
import com.hp.hpl.jena.rdf.model.Model;
import com.hp.hpl.jena.rdf.model.RDFNode;
import com.hp.hpl.jena.rdf.model.Resource;
import org.apache.log4j.Logger;
import org.monarchinitiative.owlsim.io.OWLLoader;
import org.monarchinitiative.owlsim.io.Ontology;
Expand All @@ -16,49 +18,16 @@
import org.monarchinitiative.owlsim.model.kb.Entity;
import org.monarchinitiative.owlsim.model.kb.KBMetadata;
import org.prefixcommons.CurieUtil;
import org.semanticweb.owlapi.model.AxiomType;
import org.semanticweb.owlapi.model.IRI;
import org.semanticweb.owlapi.model.OWLAnnotation;
import org.semanticweb.owlapi.model.OWLAnnotationAssertionAxiom;
import org.semanticweb.owlapi.model.OWLAnnotationProperty;
import org.semanticweb.owlapi.model.OWLAnnotationValue;
import org.semanticweb.owlapi.model.OWLClass;
import org.semanticweb.owlapi.model.OWLClassAssertionAxiom;
import org.semanticweb.owlapi.model.OWLClassExpression;
import org.semanticweb.owlapi.model.OWLDataFactory;
import org.semanticweb.owlapi.model.OWLDataProperty;
import org.semanticweb.owlapi.model.OWLDataPropertyAssertionAxiom;
import org.semanticweb.owlapi.model.OWLDisjointClassesAxiom;
import org.semanticweb.owlapi.model.OWLIndividual;
import org.semanticweb.owlapi.model.OWLIndividualAxiom;
import org.semanticweb.owlapi.model.OWLLiteral;
import org.semanticweb.owlapi.model.OWLNamedIndividual;
import org.semanticweb.owlapi.model.OWLNamedObject;
import org.semanticweb.owlapi.model.OWLObjectComplementOf;
import org.semanticweb.owlapi.model.OWLObjectProperty;
import org.semanticweb.owlapi.model.OWLObjectPropertyAssertionAxiom;
import org.semanticweb.owlapi.model.OWLObjectPropertyExpression;
import org.semanticweb.owlapi.model.OWLOntology;
import org.semanticweb.owlapi.model.OWLPropertyAssertionAxiom;
import org.semanticweb.owlapi.model.OWLPropertyAssertionObject;
import org.semanticweb.owlapi.model.OWLPropertyExpression;
import org.semanticweb.owlapi.model.*;
import org.semanticweb.owlapi.reasoner.Node;
import org.semanticweb.owlapi.reasoner.NodeSet;
import org.semanticweb.owlapi.reasoner.OWLReasoner;
import org.semanticweb.owlapi.reasoner.OWLReasonerFactory;

import com.google.common.base.Preconditions;
import com.googlecode.javaewah.EWAHCompressedBitmap;
import com.hp.hpl.jena.query.Query;
import com.hp.hpl.jena.query.QueryExecution;
import com.hp.hpl.jena.query.QueryExecutionFactory;
import com.hp.hpl.jena.query.QueryFactory;
import com.hp.hpl.jena.query.QuerySolution;
import com.hp.hpl.jena.query.ResultSet;
import com.hp.hpl.jena.rdf.model.Literal;
import com.hp.hpl.jena.rdf.model.Model;
import com.hp.hpl.jena.rdf.model.RDFNode;
import com.hp.hpl.jena.rdf.model.Resource;
import java.time.Duration;
import java.time.Instant;
import java.util.*;
import java.util.stream.Collectors;

/**
* Implementation of {@link BMKnowledgeBase} that uses the OWLAPI.
Expand Down Expand Up @@ -435,7 +404,7 @@ private void addOpposingClassPairAsym(OWLClass c, OWLClassExpression d) {
}

private void storeInferences() {

LOG.info("Storing inferences...");
// Note: if there are any nodes containing >1 class or individual, then
// the store method is called redundantly. This is unlikely to affect performance,
// and the semantics are unchanged
Expand Down Expand Up @@ -1065,7 +1034,7 @@ private OWLNamedIndividual getOWLNamedIndividual(IRI iri) {
* CURIE-style
* @return OWLAPI Class object
*/
public OWLNamedIndividual getOWLNamedIndividual(String id) {
private OWLNamedIndividual getOWLNamedIndividual(String id) {
Preconditions.checkNotNull(id);
//TODO: check - this is redundant code simply return getOWLNamedIndividual(IRI.create(curieUtil.getIri(id).orElse(id))); will suffice
if (curieUtil.getCurieMap().isEmpty()) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,26 +1,50 @@
package org.monarchinitiative.owlsim.model.kb;

import java.util.Objects;

/**
* @author cjm
*
*/
public class Attribute implements SimpleObject {

private String id;
private String label;
private final String id;
private final String label;

public Attribute(String id, String label) {
super();
this.id = id;
this.label = label;
}

@Override
public String getId() {
return id;
}

@Override
public String getLabel() {
return label;
}


@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Attribute attribute = (Attribute) o;
return Objects.equals(id, attribute.id) &&
Objects.equals(label, attribute.label);
}

@Override
public int hashCode() {
return Objects.hash(id, label);
}

@Override
public String toString() {
return "Attribute{" +
"id='" + id + '\'' +
", label='" + label + '\'' +
'}';
}
}
Loading

0 comments on commit acbfc76

Please sign in to comment.