Skip to content

Instantly share code, notes, and snippets.

@balhoff
Created July 27, 2017 13:42
Show Gist options
  • Save balhoff/5a6d5c28e321fb6d46439e262bc1cb09 to your computer and use it in GitHub Desktop.
Save balhoff/5a6d5c28e321fb6d46439e262bc1cb09 to your computer and use it in GitHub Desktop.
Scala Ammonite script to generate graph containing only HPO terms with layperson labels
import $ivy.`org.phenoscape::scowl:1.3`
import $ivy.`net.sourceforge.owlapi:owlapi-distribution:4.3.2`
import $ivy.`org.semanticweb.elk:elk-owlapi:0.4.3`
import java.io.File
import java.util.UUID
import scala.collection.JavaConverters._
import org.phenoscape.scowl._
import org.semanticweb.elk.owlapi.ElkReasonerFactory
import org.semanticweb.owlapi.apibinding.OWLManager
import org.semanticweb.owlapi.formats.RioTurtleDocumentFormat
import org.semanticweb.owlapi.model.AddImport
import org.semanticweb.owlapi.model.IRI
import org.semanticweb.owlapi.model.OWLAxiom
val manager = OWLManager.createOWLOntologyManager()
val factory = manager.getOWLDataFactory
val hpo = manager.loadOntology(IRI.create("http://purl.obolibrary.org/obo/hp.owl"))
val HasSynonymType = AnnotationProperty("http://www.geneontology.org/formats/oboInOwl#hasSynonymType")
val LaypersonTerm = IRI.create("http://purl.obolibrary.org/obo/hp.owl#layperson")
val Dummy = Class(s"http://example.org/${UUID.randomUUID}")
val (labelAxioms, mappings, eqAxioms) = (for {
AnnotationAssertion(annotations, _, term: IRI, synonymValue) <- hpo.getAxioms().asScala
if annotations.collectFirst { case Annotation(_, HasSynonymType, LaypersonTerm) => true }.nonEmpty
temp = Class(s"http://example.org/${UUID.randomUUID}")
} yield (
AnnotationAssertion(RDFSLabel, term, synonymValue),
temp -> Class(term),
temp EquivalentTo (Class(term) and Dummy)))
.unzip3
val termMappings = mappings.toMap
val tempOnt = manager.createOntology(eqAxioms.toSet[OWLAxiom].asJava)
manager.applyChange(new AddImport(tempOnt, factory.getOWLImportsDeclaration(hpo.getOntologyID.getOntologyIRI.get)))
val reasoner = new ElkReasonerFactory().createReasoner(tempOnt)
val subClassAxioms = for {
(tempTerm, term) <- termMappings
tempSuperClass <- reasoner.getSuperClasses(tempTerm, true).getFlattened.asScala
superClass <- termMappings.get(tempSuperClass)
} yield term SubClassOf superClass
reasoner.dispose()
val laypersonOnt = manager.createOntology((labelAxioms ++ subClassAxioms).asJava, IRI.create("http://purl.obolibrary.org/obo/hp/hp-layperson.owl"))
manager.saveOntology(laypersonOnt, new RioTurtleDocumentFormat(), IRI.create(new File("hp-layperson.owl")))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment