-
Notifications
You must be signed in to change notification settings - Fork 1
/
ReadCMO.groovy
162 lines (143 loc) · 5.74 KB
/
ReadCMO.groovy
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
@Grapes([
@Grab(group='org.semanticweb.elk', module='elk-owlapi', version='0.4.3'),
@Grab(group='net.sourceforge.owlapi', module='owlapi-api', version='5.1.13'),
@Grab(group='net.sourceforge.owlapi', module='owlapi-apibinding', version='5.1.13'),
@Grab(group='net.sourceforge.owlapi', module='owlapi-impl', version='5.1.13'),
@Grab(group='net.sourceforge.owlapi', module='owlapi-parsers', version='5.1.13'),
@Grab(group='net.sourceforge.owlapi', module='owlapi-distribution', version='5.1.13'),
@GrabConfig(systemClassLoader=true)
])
import org.semanticweb.owlapi.model.IRI
import org.semanticweb.owlapi.model.parameters.*
import org.semanticweb.elk.owlapi.ElkReasonerFactory
import org.semanticweb.elk.owlapi.ElkReasonerConfiguration
import org.semanticweb.elk.reasoner.config.*
import org.semanticweb.owlapi.apibinding.OWLManager
import org.semanticweb.owlapi.reasoner.*
import org.semanticweb.owlapi.reasoner.structural.StructuralReasoner
import org.semanticweb.owlapi.vocab.OWLRDFVocabulary
import org.semanticweb.owlapi.model.*
import org.semanticweb.owlapi.io.*
import org.semanticweb.owlapi.owllink.*
import org.semanticweb.owlapi.util.*
import org.semanticweb.owlapi.search.*
import org.semanticweb.owlapi.manchestersyntax.renderer.*
import org.semanticweb.owlapi.reasoner.structural.*
import java.util.regex.Pattern
def and = { cl1, cl2 ->
fac.getOWLObjectIntersectionOf(cl1,cl2)
}
def some = { r, cl ->
fac.getOWLObjectSomeValuesFrom(r,cl)
}
def equiv = { cl1, cl2 ->
fac.getOWLEquivalentClassesAxiom(cl1, cl2)
}
def subclass = { cl1, cl2 ->
fac.getOWLSubClassOfAxiom(cl1, cl2)
}
def r = { String s ->
if (s == "part-of") {
fac.getOWLObjectProperty(IRI.create("http://purl.obolibrary.org/obo/BFO_0000050"))
} else if (s == "has-part") {
fac.getOWLObjectProperty(IRI.create("http://purl.obolibrary.org/obo/BFO_0000051"))
} else if (s == "inheres-in-part-of") {
fac.getOWLObjectProperty(IRI.create("http://purl.obolibrary.org/obo/RO_0002314"))
} else if (s == "inheres-in") {
fac.getOWLObjectProperty(IRI.create("http://purl.obolibrary.org/obo/RO_0000052"))
} else {
fac.getOWLObjectProperty(IRI.create("http://aber-owl.net/#"+s))
}
}
def c = { String s ->
if (s == "quality") {
fac.getOWLClass(IRI.create("http://purl.obolibrary.org/obo/PATO_0000001"))
} else {
fac.getOWLClass(IRI.create(onturi+s))
}
}
//def add = { OWLOntology o, OWLAxiom a -> this.manager.addAxiom(o, a) }
def labelMap = { ont ->
def map = [:]
def fac = OWLManager.createOWLOntologyManager().getOWLDataFactory()
ont.getClassesInSignature(true).each { cl ->
EntitySearcher.getAnnotations(cl, ont, fac.getRDFSLabel()).each { anno ->
OWLAnnotationValue val = anno.getValue()
if (val instanceof OWLLiteral) {
map[cl] = val.getLiteral()
}
}
}
map
}
def manager = OWLManager.createOWLOntologyManager()
fac = manager.getOWLDataFactory()
def ont = manager.createOntology(IRI.create("http://biohackathon.org/covid19/cmo-ext.owl"))
def cmo = manager.loadOntologyFromOntologyDocument(new File("cmo.owl"))
def uberon = manager.loadOntologyFromOntologyDocument(new File("uberon.owl"))
def chebi = manager.loadOntologyFromOntologyDocument(new File("chebi.owl"))
def pato = manager.loadOntologyFromOntologyDocument(new File("pato.owl"))
// ConsoleProgressMonitor progressMonitor = new ConsoleProgressMonitor()
// OWLReasonerConfiguration config = new SimpleConfiguration(progressMonitor)
// ElkReasonerFactory f1 = new ElkReasonerFactory()
// def cmoreasoner = f1.createReasoner(cmo,config)
// def uberonreasoner = f1.createReasoner(uberon,config)
// def chebireasoner = f1.createReasoner(chebi,config)
// def patoreasoner = f1.createReasoner(pato,config)
def cmolabels = labelMap(cmo)
def uberonlabels = labelMap(uberon)
def chebilabels = labelMap(chebi)
def patolabels = labelMap(pato)
def cmo2other = [:].withDefault { new LinkedHashSet() }
def cmo2uberon = [:].withDefault { new LinkedHashSet() }
def cmo2pato = [:].withDefault { new LinkedHashSet() }
def cmo2chebi = [:].withDefault { new LinkedHashSet() }
cmolabels.each { cl, lab ->
uberonlabels.each { k, v ->
v = Pattern.quote(v)
if (lab=~/(^|\s)${v}($|\s)/) {
println "$lab\t$k\t$cl"
cmo2other[cl].add(k)
cmo2uberon[cl].add(k)
}
}
patolabels.each { k, v ->
v = Pattern.quote(v)
if (lab=~/(^|\s)${v}($|\s)/) {
println "$lab\t$k\t$cl"
cmo2other[cl].add(k)
cmo2pato[cl].add(k)
}
}
chebilabels.each { k, v ->
v = Pattern.quote(v)
if (lab=~/(^|\s)${v}($|\s)/) {
println "$lab\t$k\t$cl"
cmo2other[cl].add(k)
cmo2chebi[cl].add(k)
}
}
}
cmo.getClassesInSignature(true).each { k ->
if (cmo2uberon[k] != null) {
v = cmo2uberon[k]
v.each { cl ->
manager.addAxiom(ont, subclass(k, some(r("has-part"), and(c("quality"), some(r("inheres-in-part-of"), cl)))))
}
}
if (cmo2uberon[k] != null && cmo2pato[k] != null) {
v1 = cmo2uberon[k]
v2 = cmo2pato[k]
v1.each { cl1 ->
v2.each { cl2 ->
manager.addAxiom(ont, subclass(k, some(r("has-part"), and(cl2, some(r("inheres-in"), cl1)))))
}
}
}
}
manager.applyChange(new AddImport(ont, fac.getOWLImportsDeclaration(IRI.create("http://purl.obolibrary.org/obo/mp.owl"))))
manager.applyChange(new AddImport(ont, fac.getOWLImportsDeclaration(IRI.create("http://purl.obolibrary.org/obo/pato.owl"))))
manager.applyChange(new AddImport(ont, fac.getOWLImportsDeclaration(IRI.create("http://purl.obolibrary.org/obo/uberon.owl"))))
manager.applyChange(new AddImport(ont, fac.getOWLImportsDeclaration(IRI.create("http://purl.obolibrary.org/obo/chebi.owl"))))
manager.applyChange(new AddImport(ont, fac.getOWLImportsDeclaration(IRI.create("http://purl.obolibrary.org/obo/cmo.owl"))))
manager.saveOntology(ont, IRI.create(new File("qt.owl").toURI()))