-
Notifications
You must be signed in to change notification settings - Fork 35
Expand file tree
/
Copy pathFoldBasedPredictor.java
More file actions
416 lines (380 loc) · 15.9 KB
/
FoldBasedPredictor.java
File metadata and controls
416 lines (380 loc) · 15.9 KB
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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
package owltools.gaf.inference;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;
import org.apache.log4j.Level;
import org.apache.log4j.Logger;
import org.geneontology.reasoner.ExpressionMaterializingReasoner;
import org.geneontology.reasoner.ExpressionMaterializingReasonerFactory;
import org.semanticweb.elk.owlapi.ElkReasonerFactory;
import org.semanticweb.owlapi.model.AddImport;
import org.semanticweb.owlapi.model.IRI;
import org.semanticweb.owlapi.model.OWLAxiom;
import org.semanticweb.owlapi.model.OWLClass;
import org.semanticweb.owlapi.model.OWLClassExpression;
import org.semanticweb.owlapi.model.OWLDataFactory;
import org.semanticweb.owlapi.model.OWLImportsDeclaration;
import org.semanticweb.owlapi.model.OWLObjectProperty;
import org.semanticweb.owlapi.model.OWLObjectPropertyExpression;
import org.semanticweb.owlapi.model.OWLObjectSomeValuesFrom;
import org.semanticweb.owlapi.model.OWLOntology;
import org.semanticweb.owlapi.model.OWLOntologyID;
import org.semanticweb.owlapi.model.OWLOntologyManager;
import org.semanticweb.owlapi.reasoner.OWLReasoner;
import org.semanticweb.owlapi.reasoner.OWLReasonerFactory;
import org.semanticweb.owlapi.util.OWLClassExpressionVisitorAdapter;
import com.google.common.base.Optional;
import owltools.gaf.Bioentity;
import owltools.gaf.ExtensionExpression;
import owltools.gaf.GafDocument;
import owltools.gaf.GeneAnnotation;
import owltools.graph.OWLGraphWrapper;
import owltools.vocab.OBOUpperVocabulary;
/**
* Use a reasoner to find more specific named classes for annotations with extension expressions.
*
* see http://code.google.com/p/owltools/wiki/AnnotationExtensionFolding
* @author cjm
*/
public class FoldBasedPredictor extends AbstractAnnotationPredictor implements AnnotationPredictor {
private static final Logger LOG = Logger.getLogger(FoldBasedPredictor.class);
private OWLReasoner reasoner = null;
private Set<OWLClass> relevantClasses;
private OWLObjectProperty partOf;
private OWLObjectProperty occursIn;
private Set<OWLObjectProperty> defaultProperties;
private boolean isInitialized = false;
private final boolean throwExceptions;
public FoldBasedPredictor(GafDocument gafDocument, OWLGraphWrapper graph, boolean throwExceptions) {
super(gafDocument, graph);
this.throwExceptions = throwExceptions;
isInitialized = init();
Logger.getLogger("org.semanticweb.elk").setLevel(Level.ERROR);
}
@Override
public boolean isInitialized() {
return isInitialized;
}
public boolean init() {
OWLReasonerFactory reasonerFactory = new ElkReasonerFactory();
reasoner = reasonerFactory.createReasoner(getGraph().getSourceOntology());
boolean consistent = reasoner.isConsistent();
if (!consistent) {
LOG.error("The ontology is not consistent. Impossible to make proper predictions.");
if (throwExceptions) {
throw new RuntimeException("The ontology is not consistent. Impossible to make proper predictions.");
}
return false;
}
relevantClasses = new HashSet<OWLClass>();
// add GO
// biological process
OWLClass bp = getGraph().getOWLClassByIdentifier("GO:0008150");
addRelevant(bp, reasoner, getGraph(), relevantClasses);
// molecular function
OWLClass mf = getGraph().getOWLClassByIdentifier("GO:0003674");
addRelevant(mf, reasoner, getGraph(), relevantClasses);
// cellular component
OWLClass cc = getGraph().getOWLClassByIdentifier("GO:0005575");
addRelevant(cc, reasoner, getGraph(), relevantClasses);
// properties
partOf = OBOUpperVocabulary.BFO_part_of.getObjectProperty(getGraph().getDataFactory());
occursIn = OBOUpperVocabulary.BFO_occurs_in.getObjectProperty(getGraph().getDataFactory());
defaultProperties = Collections.unmodifiableSet(new HashSet<OWLObjectProperty>(Arrays.asList(partOf, occursIn)));
if (relevantClasses.isEmpty()) {
LOG.error("No valid classes found for fold based prediction folding.");
if (throwExceptions) {
throw new RuntimeException("No valid classes found for fold based prediction folding.");
}
return false;
}
return true;
}
private static void addRelevant(OWLClass root, OWLReasoner r, OWLGraphWrapper g, Set<OWLClass> relevant) {
if (root != null) {
Set<OWLClass> candidates = r.getSubClasses(root, false).getFlattened();
for (OWLClass candidate : candidates) {
// hack to restric to GO ids!
String identifier = g.getIdentifier(candidate.getIRI());
if (identifier.startsWith("GO:")) {
relevant.add(candidate);
}
}
}
}
@Override
public List<Prediction> predictForBioEntities(Map<Bioentity, ? extends Collection<GeneAnnotation>> annMap) {
final List<Prediction> predictions = new ArrayList<Prediction>();
final OWLGraphWrapper g = getGraph();
final OWLDataFactory f = g.getDataFactory();
final OWLOntologyManager m = g.getManager();
// step 0: generate a throw away ontology which imports the source ontology
final OWLOntology generatedContainer;
try {
generatedContainer = g.getManager().createOntology(IRI.generateDocumentIRI());
// import source
OWLOntology source = g.getSourceOntology();
OWLOntologyID sourceId = source.getOntologyID();
Optional<IRI> ontologyIRI = sourceId.getOntologyIRI();
if (ontologyIRI.isPresent()) {
OWLImportsDeclaration sourceImportDeclaration = f.getOWLImportsDeclaration(ontologyIRI.get());
m.applyChange(new AddImport(generatedContainer, sourceImportDeclaration));
}
else {
String msg = "Could not setup container ontology, missing ontology ID";
LOG.error(msg);
throw new RuntimeException(msg);
}
}
catch(Exception e) {
String msg = "Could not setup container ontology";
LOG.error(msg, e);
if (throwExceptions) {
throw new RuntimeException(msg, e);
}
return predictions;
}
ExpressionMaterializingReasoner reasoner = null;
try {
// step 1: prepare ontology
final Map<OWLClass, PredicationDataContainer> sourceData = new HashMap<OWLClass, PredicationDataContainer>();
final Map<Bioentity, Set<OWLClass>> allExistingAnnotations = new HashMap<Bioentity, Set<OWLClass>>();
final Map<Bioentity, Set<OWLClass>> allGeneratedClasses = generateAxioms(generatedContainer, annMap, allExistingAnnotations, sourceData);
// step 2: reasoner
reasoner = new ExpressionMaterializingReasonerFactory(new ElkReasonerFactory()).createReasoner(generatedContainer);
reasoner.setIncludeImports(true);
LOG.info("Materializing expressions for props: "+defaultProperties);
reasoner.materializeExpressions(defaultProperties);
// step 3: find folded classes
BioentityLoop: for(Entry<Bioentity, Set<OWLClass>> entry : allGeneratedClasses.entrySet()) {
final Set<OWLClass> used = new HashSet<OWLClass>();
final Bioentity e = entry.getKey();
final Set<OWLClass> existing = allExistingAnnotations.get(e);
final Set<OWLClass> generatedClasses = entry.getValue();
// step 3.1: check for direct equivalent classes
final Set<OWLClass> checkForDirectSuper = new HashSet<OWLClass>(generatedClasses);
for(OWLClass generated : generatedClasses) {
Set<OWLClass> equivalents = reasoner.getEquivalentClasses(generated).getEntitiesMinus(generated);
final PredicationDataContainer source = sourceData.get(generated);
for (OWLClass equivalentCls : equivalents) {
if (equivalentCls.isBottomEntity()) {
// if the class is unsatisfiable skip the bioentity, no safe prediction possible
LOG.warn("skipping folding prediction for '"+e.getId()+"' due unsatisfiable expression.");
break BioentityLoop;
}
if (generatedClasses.contains(equivalentCls)) {
continue;
}
if (relevantClasses.contains(equivalentCls) == false) {
continue;
}
checkForDirectSuper.remove(generated);
if (existing != null && existing.contains(equivalentCls)) {
continue;
}
if (used.add(equivalentCls) && source != null) {
Prediction prediction = getPrediction(source.ann, equivalentCls, e.getId(), source.ann.getCls());
prediction.setReason(generateReason(equivalentCls, source.cls, source.extensionExpression, source.expressions, source.evidence, g));
predictions.add(prediction);
}
}
}
// step 3.2: check for direct super classes (including default properties such as part_of and occurs_in),
// but only for classes which did not have a named relevant equivalent class
for(final OWLClass generated : checkForDirectSuper) {
Set<OWLClass> parents = reasoner.getSuperClasses(generated, true).getFlattened();
final PredicationDataContainer source = sourceData.get(generated);
for (final OWLClass parent : parents) {
if (parent.isBuiltIn()) {
continue;
}
if (generatedClasses.contains(parent)) {
continue;
}
if (relevantClasses.contains(parent) == false) {
continue;
}
if (existing != null && existing.contains(parent)) {
continue;
}
if (used.add(parent) && source != null) {
Prediction prediction = getPrediction(source.ann, parent, e.getId(), source.ann.getCls());
prediction.setReason(generateReason(parent, source.cls, source.extensionExpression, source.expressions, source.evidence, g));
predictions.add(prediction);
}
}
// check also for occurs_in and part_of
Set<OWLClassExpression> superClassesExpressions = reasoner.getSuperClassExpressions(generated, true);
for (final OWLClassExpression ce : superClassesExpressions) {
ce.accept(new OWLClassExpressionVisitorAdapter(){
@Override
public void visit(OWLObjectSomeValuesFrom svf) {
OWLClassExpression superClsExpr = svf.getFiller();
OWLObjectPropertyExpression p = svf.getProperty();
if(defaultProperties.contains(p) && superClsExpr.isAnonymous() == false) {
final OWLClass cls = superClsExpr.asOWLClass();
if (cls.isBuiltIn()) {
return;
}
if (relevantClasses.contains(cls) == false) {
return;
}
if (existing != null && existing.contains(cls)) {
return;
}
if (used.add(cls) && source != null) {
Prediction prediction = getPrediction(source.ann, cls, e.getId(), source.ann.getCls());
prediction.setReason(generateReason(cls, source.cls, source.extensionExpression, source.expressions, source.evidence, g));
predictions.add(prediction);
}
}
}
});
}
}
}
return predictions;
} finally {
if (reasoner != null) {
reasoner.dispose();
}
if (generatedContainer != null) {
m.removeOntology(generatedContainer);
}
}
}
private static class PredicationDataContainer {
final GeneAnnotation ann;
final OWLClass cls;
final List<ExtensionExpression> expressions;
final OWLClassExpression extensionExpression;
final String evidence;
PredicationDataContainer(GeneAnnotation source, OWLClass annotatedCls, String evidence,
OWLClassExpression extensionExpression, List<ExtensionExpression> expressions) {
super();
this.ann = source;
this.cls = annotatedCls;
this.extensionExpression = extensionExpression;
this.expressions = expressions;
this.evidence = evidence;
}
}
private Map<Bioentity, Set<OWLClass>> generateAxioms(OWLOntology generatedContainer, Map<Bioentity, ? extends Collection<GeneAnnotation>> annMap, Map<Bioentity, Set<OWLClass>> allExistingAnnotations, Map<OWLClass, PredicationDataContainer> sourceData) {
final OWLGraphWrapper g = getGraph();
final OWLDataFactory f = g.getDataFactory();
final OWLOntologyManager m = g.getManager();
Map<Bioentity, Set<OWLClass>> allGeneratedClasses = new HashMap<Bioentity, Set<OWLClass>>();
for(Entry<Bioentity, ? extends Collection<GeneAnnotation>> entry : annMap.entrySet()) {
Set<OWLClass> generatedClasses = new HashSet<OWLClass>();
Set<OWLClass> existingAnnotations = new HashSet<OWLClass>();
Bioentity e = entry.getKey();
for (GeneAnnotation ann : entry.getValue()) {
// skip ND evidence annotations
String evidenceString = ann.getShortEvidence();
if ("ND".equals(evidenceString)) {
continue;
}
// parse annotation cls
String annotatedToClassString = ann.getCls();
OWLClass annotatedToClass = g.getOWLClassByIdentifierNoAltIds(annotatedToClassString);
if (annotatedToClass == null) {
LOG.warn("Skipping annotation for prediction. Could not find cls for id: "+annotatedToClassString);
continue;
}
// add annotation class (and its super classes as known annotation)
existingAnnotations.add(annotatedToClass);
existingAnnotations.addAll(reasoner.getSuperClasses(annotatedToClass, false).getFlattened());
// parse c16 expressions
List<List<ExtensionExpression>> extensionExpressionGroups = ann.getExtensionExpressions();
if (extensionExpressionGroups != null && !extensionExpressionGroups.isEmpty()) {
for (List<ExtensionExpression> group : extensionExpressionGroups) {
Set<OWLClassExpression> units = new HashSet<OWLClassExpression>();
for (ExtensionExpression ext : group) {
String extClsString = ext.getCls();
String extRelString = ext.getRelation();
OWLClass extCls = f.getOWLClass(g.getIRIByIdentifier(extClsString));
OWLObjectProperty extRel = g.getOWLObjectPropertyByIdentifier(extRelString);
if (extRel == null) {
continue;
}
units.add(f.getOWLObjectSomeValuesFrom(extRel, extCls));
}
if (units.isEmpty()) {
continue;
}
units.add(annotatedToClass);
final OWLClassExpression groupExpression = f.getOWLObjectIntersectionOf(units);
OWLClass generatedClass = f.getOWLClass(IRI.generateDocumentIRI());
OWLAxiom axiom = f.getOWLEquivalentClassesAxiom(generatedClass, groupExpression);
m.addAxiom(generatedContainer, axiom);
generatedClasses.add(generatedClass);
sourceData.put(generatedClass, new PredicationDataContainer(ann, annotatedToClass, evidenceString, groupExpression, group));
}
}
}
if (generatedClasses.isEmpty() == false) {
allGeneratedClasses.put(e, generatedClasses);
allExistingAnnotations.put(e, existingAnnotations);
}
}
return allGeneratedClasses;
}
private String generateReason(OWLClass foldedClass, OWLClass annotatedToClass, OWLClassExpression groupExpression, List<ExtensionExpression> expressions, String evidence, OWLGraphWrapper g) {
StringBuilder sb = new StringBuilder();
sb.append(g.getIdentifier(foldedClass));
sb.append('\t');
String foldedClassLabel = g.getLabel(foldedClass);
if (foldedClassLabel != null) {
sb.append(foldedClassLabel);
}
sb.append('\t');
sb.append(FoldBasedPredictor.class.getSimpleName());
sb.append('\t');
sb.append(g.getIdentifier(annotatedToClass));
sb.append('\t');
String annotatedToClassLabel = g.getLabel(annotatedToClass);
if (annotatedToClassLabel != null) {
sb.append(annotatedToClassLabel);
}
for(ExtensionExpression ext : expressions) {
sb.append('\t');
sb.append(ext.getRelation());
sb.append('\t');
sb.append(ext.getCls());
sb.append('\t');
OWLClass extCls = g.getOWLClassByIdentifierNoAltIds(ext.getCls());
if (extCls != null) {
String extClsLabel = g.getLabel(extCls);
if (extClsLabel != null) {
sb.append(extClsLabel);
}
}
}
sb.append('\t');
sb.append(evidence);
return sb.toString();
}
protected Prediction getPrediction(GeneAnnotation ann, OWLClass c, String bioentity, String with) {
GeneAnnotation annP = new GeneAnnotation(ann);
annP.setBioentity(bioentity);
annP.setCls(getGraph().getIdentifier(c));
annP.setEvidence("IC", null);
annP.setWithInfos(Collections.singleton(with));
annP.setAssignedBy("GOC-OWL");
Prediction prediction = new Prediction(annP);
return prediction;
}
@Override
public void dispose() {
if (reasoner != null) {
reasoner.dispose();
}
}
}