Skip to content

Instantly share code, notes, and snippets.

@balhoff
Created September 29, 2021 20:31
Show Gist options
  • Save balhoff/51dbe97a305798d22c8df008be45bae0 to your computer and use it in GitHub Desktop.
Save balhoff/51dbe97a305798d22c8df008be45bae0 to your computer and use it in GitHub Desktop.
Check if provided taxon constraints are implied by those already in an ontology
// scala 2.13.3
// ammonite 2.3.8
import $ivy.`net.sourceforge.owlapi:owlapi-distribution:4.5.20`
import $ivy.`org.semanticweb.elk:elk-owlapi:0.4.3`
import $ivy.`org.phenoscape::scowl:1.3.4`
import $ivy.`com.outr::scribe-slf4j:2.7.12`
import scala.io.Source
import org.phenoscape.scowl._
import org.semanticweb.owlapi.apibinding.OWLManager
import org.semanticweb.elk.owlapi.ElkReasonerFactory
import org.semanticweb.owlapi.reasoner.InferenceType
import org.semanticweb.owlapi.model.OWLClass
import org.semanticweb.owlapi.model.OWLClassExpression
val InTaxon = ObjectProperty("http://purl.obolibrary.org/obo/RO_0002162")
final case class Constraint(goTerm: OWLClass, goLabel: String, taxonTerm: OWLClass, taxonLabel: String) {
def expression: OWLClassExpression = goTerm and (InTaxon some taxonTerm)
}
def readConstraintsFile(file: os.Path): List[Constraint] = {
Source
.fromFile(file.toIO, "utf-8")
.getLines()
.drop(1)
.map { line =>
val columns = line.split("\t", -1)
val goTerm = Class(columns(0).trim().replaceFirst("GO:", "http://purl.obolibrary.org/obo/GO_"))
val goLabel = columns(1).trim()
val taxonTerm = Class(
columns(2)
.trim()
.replaceFirst("NCBITaxon:", "http://purl.obolibrary.org/obo/NCBITaxon_")
.replaceFirst("NCBITaxon_Union:", "http://purl.obolibrary.org/obo/NCBITaxon_Union_")
)
val taxonLabel = columns(3).trim()
Constraint(goTerm, goLabel, taxonTerm, taxonLabel)
}
.to(List)
}
@main
def main(onlyConstraintsFile: os.Path, neverConstraintsFile: os.Path, ontologyFile: os.Path) = {
val manager = OWLManager.createOWLOntologyManager()
val ontology = OWLManager.createOWLOntologyManager().loadOntologyFromOntologyDocument(ontologyFile.toIO)
val reasoner = new ElkReasonerFactory().createReasoner(ontology)
reasoner.precomputeInferences(InferenceType.CLASS_HIERARCHY)
for {
constraint @ Constraint(goTerm, goLabel, taxonTerm, taxonLabel) <- readConstraintsFile(onlyConstraintsFile)
} {
val equivalents = reasoner.getEquivalentClasses(constraint.expression)
if (equivalents.contains(OWLNothing))
println(s"$goLabel\t${goTerm.getIRI()}\t$taxonLabel\t${taxonTerm.getIRI()}\tonly in taxon\tno longer compatible")
else if (!equivalents.contains(goTerm))
println(s"$goLabel\t${goTerm.getIRI()}\t$taxonLabel\t${taxonTerm.getIRI()}\tonly in taxon\tno longer constrained")
}
for {
constraint @ Constraint(goTerm, goLabel, taxonTerm, taxonLabel) <- readConstraintsFile(neverConstraintsFile)
} {
val equivalents = reasoner.getEquivalentClasses(constraint.expression)
if (!equivalents.contains(OWLNothing))
println(
s"$goLabel\t${goTerm.getIRI()}\t$taxonLabel\t${taxonTerm.getIRI()}\tnever in taxon\tno longer constrained"
)
else if (equivalents.contains(goTerm))
println(s"$goLabel\t${goTerm.getIRI()}\t$taxonLabel\t${taxonTerm.getIRI()}\tnever in taxon\tno longer compatible")
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment