forked from bio-ontology-research-group/deepgozero
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAxioms.groovy
185 lines (162 loc) · 7.64 KB
/
Axioms.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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
@Grapes([
@Grab(group='org.semanticweb.elk', module='elk-owlapi', version='0.4.2'),
@Grab(group='net.sourceforge.owlapi', module='owlapi-api', version='4.1.0'),
@Grab(group='net.sourceforge.owlapi', module='owlapi-apibinding', version='4.1.0'),
@Grab(group='net.sourceforge.owlapi', module='owlapi-impl', version='4.1.0'),
@Grab(group='net.sourceforge.owlapi', module='owlapi-parsers', version='4.1.0'),
@GrabConfig(systemClassLoader=true)
])
import org.semanticweb.owlapi.model.parameters.*
import org.semanticweb.owlapi.apibinding.OWLManager;
import org.semanticweb.owlapi.reasoner.*
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.elk.owlapi.ElkReasonerFactory;
import org.semanticweb.elk.owlapi.ElkReasonerConfiguration
import org.semanticweb.elk.reasoner.config.*
import org.semanticweb.owlapi.manchestersyntax.renderer.*
import org.semanticweb.owlapi.formats.*
import java.util.*
OWLOntologyManager manager = OWLManager.createOWLOntologyManager()
OWLOntology ont = manager.loadOntologyFromOntologyDocument(
new File("data/go-plus.owl"))
OWLDataFactory dataFactory = manager.getOWLDataFactory()
ConsoleProgressMonitor progressMonitor = new ConsoleProgressMonitor()
OWLReasonerConfiguration config = new SimpleConfiguration(progressMonitor)
ElkReasonerFactory reasonerFactory = new ElkReasonerFactory()
OWLReasoner reasoner = reasonerFactory.createReasoner(ont, config)
reasoner.precomputeInferences(InferenceType.CLASS_HIERARCHY)
def renderer = new ManchesterOWLSyntaxOWLObjectRendererImpl()
def shortFormProvider = new SimpleShortFormProvider()
def getName = { cl ->
def iri = cl.toString()
def name = iri
if (iri.startsWith("<http://purl.obolibrary.org/obo/")) {
name = iri.substring(32, iri.length() - 1)
} else if (iri.startsWith("<http://aber-owl.net/")) {
name = iri.substring(21, iri.length() - 1)
}
return name
}
def ignoredWords = ["\\r\\n|\\r|\\n", "[()]"]
int dgID = 1;
Map<OWLClassExpression, OWLClass> dgMap = new HashMap<OWLClassExpression, OWLClass>();
ont.getTBoxAxioms().each { axiom ->
if (axiom.getAxiomType() == AxiomType.SUBCLASS_OF) {
OWLSubClassOfAxiom subAxiom = (OWLSubClassOfAxiom) axiom;
OWLClassExpression sub = subAxiom.getSubClass();
OWLClassExpression sup = subAxiom.getSuperClass();
OWLClassExpression newSub = null;
OWLClassExpression newSup = null;
boolean ok = true;
if (sub.getClassExpressionType() == ClassExpressionType.valueOf("OBJECT_SOME_VALUES_FROM")) {
if (!dgMap.containsKey(sub)) {
IRI iri = IRI.create("http://deepgo.bio2vec.net/DG_" + String.format("%07d", dgID++));
newSub = dataFactory.getOWLClass(iri);
OWLEquivalentClassesAxiom newDef =
dataFactory.getOWLEquivalentClassesAxiom(newSub, sub);
manager.addAxiom(ont, newDef);
dgMap.put(sub, newSub);
} else {
newSub = dgMap.get(sub);
}
ok = false;
} else {
newSub = sub;
}
if (sup.getClassExpressionType() == ClassExpressionType.valueOf("OBJECT_SOME_VALUES_FROM")) {
if (!dgMap.containsKey(sup)) {
IRI iri = IRI.create("http://deepgo.bio2vec.net/DG_" + String.format("%07d", dgID++));
newSup = dataFactory.getOWLClass(iri);
OWLEquivalentClassesAxiom newDef =
dataFactory.getOWLEquivalentClassesAxiom(newSup, sup);
manager.addAxiom(ont, newDef);
dgMap.put(sup, newSup);
} else {
newSup = dgMap.get(sup);
}
ok = false;
} else {
newSup = sup;
}
if (!ok) {
manager.removeAxiom(ont, axiom);
OWLSubClassOfAxiom newAxiom = dataFactory.getOWLSubClassOfAxiom(newSub, newSup);
manager.addAxiom(ont, newAxiom);
}
} else if (axiom.getAxiomType() == AxiomType.EQUIVALENT_CLASSES) {
OWLEquivalentClassesAxiom eqAxiom = (OWLEquivalentClassesAxiom) axiom;
List<OWLClassExpression> eqAxioms = eqAxiom.getClassExpressionsAsList();
List<OWLClassExpression> newAxioms = new ArrayList<OWLClassExpression>();
eqAxioms.each { expr ->
if (expr.getClassExpressionType() == ClassExpressionType.valueOf("OWL_CLASS")) {
newAxioms.add(expr);
} else if (expr.getClassExpressionType() == ClassExpressionType.valueOf("OBJECT_SOME_VALUES_FROM")) {
OWLClass newExpr = null;
if (!dgMap.containsKey(expr)) {
IRI iri = IRI.create("http://deepgo.bio2vec.net/DG_" + String.format("%07d", dgID++));
newExpr = dataFactory.getOWLClass(iri);
OWLEquivalentClassesAxiom newDef =
dataFactory.getOWLEquivalentClassesAxiom(newExpr, expr);
manager.addAxiom(ont, newDef);
dgMap.put(expr, newExpr);
} else {
newExpr = dgMap.get(expr);
}
newAxioms.add(newExpr);
} else if (expr.getClassExpressionType() == ClassExpressionType.valueOf("OBJECT_INTERSECTION_OF")) {
Set<OWLClass> exprSet = new HashSet<OWLClass>();
expr.asConjunctSet().each { cexpr ->
if (cexpr.getClassExpressionType() != ClassExpressionType.valueOf("OWL_CLASS")) {
OWLClass newCexpr = null;
if (!dgMap.containsKey(cexpr)) {
IRI iri = IRI.create("http://deepgo.bio2vec.net/DG_" + String.format("%07d", dgID++));
newCexpr = dataFactory.getOWLClass(iri);
OWLEquivalentClassesAxiom newDef =
dataFactory.getOWLEquivalentClassesAxiom(newCexpr, cexpr);
manager.addAxiom(ont, newDef);
dgMap.put(cexpr, newCexpr);
} else {
newCexpr = dgMap.get(cexpr);
}
exprSet.add(newCexpr);
} else {
exprSet.add((OWLClass)cexpr);
}
}
OWLClassExpression newExpr = dataFactory.getOWLObjectIntersectionOf(exprSet);
newAxioms.add(newExpr);
} else if (expr.getClassExpressionType() == ClassExpressionType.valueOf("OBJECT_UNION_OF")) {
newAxioms.add(expr);
}
}
OWLEquivalentClassesAxiom newAxiom = dataFactory.getOWLEquivalentClassesAxiom(newAxioms);
manager.removeAxiom(ont, axiom);
manager.addAxiom(ont, newAxiom);
} else if (axiom.getAxiomType() == AxiomType.DISJOINT_CLASSES) {
} else {
}
}
reasoner = reasonerFactory.createReasoner(ont, config)
reasoner.precomputeInferences(InferenceType.CLASS_HIERARCHY)
def ec = 0
new InferredEquivalentClassAxiomGenerator().createAxioms(dataFactory, reasoner).each { ax ->
manager.addAxiom(ont, ax)
ec += 1
}
println("Inferred Equivalent Classes: " + ec)
def sc = 0
new InferredSubClassAxiomGenerator().createAxioms(dataFactory, reasoner).each { ax ->
manager.addAxiom(ont, ax)
sc += 1
}
println("Inferred SubClasses: " + sc)
File newOntFile = new File("data/newGO.owl");
manager.saveOntology(ont, IRI.create(newOntFile.toURI()))
def out = new PrintWriter(
new BufferedWriter(new FileWriter("data/newgo.sup")))
out.flush()
out.close()