/
OWLAPIOwl2Obo.java
2040 lines (1955 loc) · 82 KB
/
OWLAPIOwl2Obo.java
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
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
package org.obolibrary.obo2owl;
import java.io.UnsupportedEncodingException;
import java.text.ParseException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.Vector;
import java.util.logging.Level;
import java.util.logging.Logger;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import org.obolibrary.obo2owl.Obo2OWLConstants.Obo2OWLVocabulary;
import org.obolibrary.obo2owl.OwlStringTools.OwlStringException;
import org.obolibrary.oboformat.model.Clause;
import org.obolibrary.oboformat.model.Frame;
import org.obolibrary.oboformat.model.Frame.FrameType;
import org.obolibrary.oboformat.model.OBODoc;
import org.obolibrary.oboformat.model.QualifierValue;
import org.obolibrary.oboformat.model.Xref;
import org.obolibrary.oboformat.parser.OBOFormatConstants;
import org.obolibrary.oboformat.parser.OBOFormatConstants.OboFormatTag;
import org.semanticweb.owlapi.model.AxiomType;
import org.semanticweb.owlapi.model.IRI;
import org.semanticweb.owlapi.model.OWLAnnotation;
import org.semanticweb.owlapi.model.OWLAnnotationAssertionAxiom;
import org.semanticweb.owlapi.model.OWLAnnotationProperty;
import org.semanticweb.owlapi.model.OWLAnnotationValue;
import org.semanticweb.owlapi.model.OWLAsymmetricObjectPropertyAxiom;
import org.semanticweb.owlapi.model.OWLAxiom;
import org.semanticweb.owlapi.model.OWLClass;
import org.semanticweb.owlapi.model.OWLClassAssertionAxiom;
import org.semanticweb.owlapi.model.OWLClassExpression;
import org.semanticweb.owlapi.model.OWLDataFactory;
import org.semanticweb.owlapi.model.OWLDatatype;
import org.semanticweb.owlapi.model.OWLDeclarationAxiom;
import org.semanticweb.owlapi.model.OWLDisjointClassesAxiom;
import org.semanticweb.owlapi.model.OWLDisjointObjectPropertiesAxiom;
import org.semanticweb.owlapi.model.OWLEntity;
import org.semanticweb.owlapi.model.OWLEquivalentClassesAxiom;
import org.semanticweb.owlapi.model.OWLEquivalentObjectPropertiesAxiom;
import org.semanticweb.owlapi.model.OWLFunctionalObjectPropertyAxiom;
import org.semanticweb.owlapi.model.OWLInverseFunctionalObjectPropertyAxiom;
import org.semanticweb.owlapi.model.OWLInverseObjectPropertiesAxiom;
import org.semanticweb.owlapi.model.OWLLiteral;
import org.semanticweb.owlapi.model.OWLNamedObject;
import org.semanticweb.owlapi.model.OWLNaryPropertyAxiom;
import org.semanticweb.owlapi.model.OWLObject;
import org.semanticweb.owlapi.model.OWLObjectAllValuesFrom;
import org.semanticweb.owlapi.model.OWLObjectCardinalityRestriction;
import org.semanticweb.owlapi.model.OWLObjectComplementOf;
import org.semanticweb.owlapi.model.OWLObjectExactCardinality;
import org.semanticweb.owlapi.model.OWLObjectIntersectionOf;
import org.semanticweb.owlapi.model.OWLObjectMaxCardinality;
import org.semanticweb.owlapi.model.OWLObjectMinCardinality;
import org.semanticweb.owlapi.model.OWLObjectProperty;
import org.semanticweb.owlapi.model.OWLObjectPropertyDomainAxiom;
import org.semanticweb.owlapi.model.OWLObjectPropertyExpression;
import org.semanticweb.owlapi.model.OWLObjectPropertyRangeAxiom;
import org.semanticweb.owlapi.model.OWLObjectSomeValuesFrom;
import org.semanticweb.owlapi.model.OWLObjectUnionOf;
import org.semanticweb.owlapi.model.OWLOntology;
import org.semanticweb.owlapi.model.OWLOntologyManager;
import org.semanticweb.owlapi.model.OWLQuantifiedObjectRestriction;
import org.semanticweb.owlapi.model.OWLReflexiveObjectPropertyAxiom;
import org.semanticweb.owlapi.model.OWLSubAnnotationPropertyOfAxiom;
import org.semanticweb.owlapi.model.OWLSubClassOfAxiom;
import org.semanticweb.owlapi.model.OWLSubObjectPropertyOfAxiom;
import org.semanticweb.owlapi.model.OWLSubPropertyChainOfAxiom;
import org.semanticweb.owlapi.model.OWLSymmetricObjectPropertyAxiom;
import org.semanticweb.owlapi.model.OWLTransitiveObjectPropertyAxiom;
import org.semanticweb.owlapi.vocab.Namespaces;
import org.semanticweb.owlapi.vocab.OWL2Datatype;
/** owlapi version of Owl2Obo */
public class OWLAPIOwl2Obo {
private static Logger LOG = Logger.getLogger(OWLAPIOwl2Obo.class.getName());
protected OWLOntologyManager manager;
protected OWLOntology owlOntology;
protected OWLDataFactory fac;
protected OBODoc obodoc;
protected Set<OWLAxiom> untranslatableAxioms;
protected Map<String, String> idSpaceMap;
/** annotation map */
public static Map<String, String> annotationPropertyMap = initAnnotationPropertyMap();
protected Set<OWLAnnotationProperty> apToDeclare;
protected String ontologyId;
protected boolean strictConversion;
protected boolean discardUntranslatable = false;
protected void init() {
idSpaceMap = new HashMap<String, String>();
// legacy:
idSpaceMap.put("http://www.obofoundry.org/ro/ro.owl#", "OBO_REL");
untranslatableAxioms = new HashSet<OWLAxiom>();
fac = manager.getOWLDataFactory();
apToDeclare = new HashSet<OWLAnnotationProperty>();
}
/**
* @param translationManager
* translationManager
*/
public OWLAPIOwl2Obo(OWLOntologyManager translationManager) {
manager = translationManager;
init();
}
protected static HashMap<String, String> initAnnotationPropertyMap() {
HashMap<String, String> map = new HashMap<String, String>();
for (String key : OWLAPIObo2Owl.ANNOTATIONPROPERTYMAP.keySet()) {
IRI propIRI = OWLAPIObo2Owl.ANNOTATIONPROPERTYMAP.get(key);
map.put(propIRI.toString(), key);
}
return map;
}
/**
* @param b
* strict conversion
*/
public void setStrictConversion(boolean b) {
strictConversion = b;
}
/** @return strict conversion */
public boolean getStrictConversion() {
return strictConversion;
}
/** @return the discardUntranslatable */
public boolean isDiscardUntranslatable() {
return discardUntranslatable;
}
/**
* @param discardUntranslatable
* the discardUntranslatable to set
*/
public void setDiscardUntranslatable(boolean discardUntranslatable) {
this.discardUntranslatable = discardUntranslatable;
}
/** @return manager */
public OWLOntologyManager getManager() {
return manager;
}
/**
* @param manager
* manager
*/
public void setManager(OWLOntologyManager manager) {
this.manager = manager;
}
/** @return obo doc */
public OBODoc getObodoc() {
return obodoc;
}
/**
* @param obodoc
* obodoc
*/
public void setObodoc(OBODoc obodoc) {
this.obodoc = obodoc;
}
/**
* @param ont
* ont
* @return obo doc
*/
public OBODoc convert(OWLOntology ont) {
owlOntology = ont;
if (ont != null) {
ontologyId = getOntologyId(ont);
} else {
ontologyId = "TODO";
}
init();
return tr();
}
/** @return the untranslatableAxioms */
public Collection<OWLAxiom> getUntranslatableAxioms() {
return untranslatableAxioms;
}
/** @return translated obodoc */
protected OBODoc tr() {
obodoc = new OBODoc();
preProcess();
tr(owlOntology);
for (OWLAxiom ax : owlOntology.getAxioms()) {
if (ax instanceof OWLDeclarationAxiom) {
tr((OWLDeclarationAxiom) ax);
} else if (ax instanceof OWLSubClassOfAxiom) {
tr((OWLSubClassOfAxiom) ax);
} else if (ax instanceof OWLDisjointClassesAxiom) {
tr((OWLDisjointClassesAxiom) ax);
} else if (ax instanceof OWLEquivalentClassesAxiom) {
tr((OWLEquivalentClassesAxiom) ax);
} else if (ax instanceof OWLClassAssertionAxiom) {
tr((OWLClassAssertionAxiom) ax);
} else if (ax instanceof OWLEquivalentObjectPropertiesAxiom) {
tr((OWLEquivalentObjectPropertiesAxiom) ax);
} else if (ax instanceof OWLSubAnnotationPropertyOfAxiom) {
tr((OWLSubAnnotationPropertyOfAxiom) ax);
} else if (ax instanceof OWLSubObjectPropertyOfAxiom) {
tr((OWLSubObjectPropertyOfAxiom) ax);
} else if (ax instanceof OWLObjectPropertyRangeAxiom) {
tr((OWLObjectPropertyRangeAxiom) ax);
} else if (ax instanceof OWLFunctionalObjectPropertyAxiom) {
tr((OWLFunctionalObjectPropertyAxiom) ax);
} else if (ax instanceof OWLSymmetricObjectPropertyAxiom) {
tr((OWLSymmetricObjectPropertyAxiom) ax);
} else if (ax instanceof OWLAsymmetricObjectPropertyAxiom) {
tr((OWLAsymmetricObjectPropertyAxiom) ax);
} else if (ax instanceof OWLObjectPropertyDomainAxiom) {
tr((OWLObjectPropertyDomainAxiom) ax);
} else if (ax instanceof OWLInverseFunctionalObjectPropertyAxiom) {
tr((OWLInverseFunctionalObjectPropertyAxiom) ax);
} else if (ax instanceof OWLInverseObjectPropertiesAxiom) {
tr((OWLInverseObjectPropertiesAxiom) ax);
} else if (ax instanceof OWLDisjointObjectPropertiesAxiom) {
tr((OWLDisjointObjectPropertiesAxiom) ax);
} else if (ax instanceof OWLReflexiveObjectPropertyAxiom) {
tr((OWLReflexiveObjectPropertyAxiom) ax);
} else if (ax instanceof OWLTransitiveObjectPropertyAxiom) {
tr((OWLTransitiveObjectPropertyAxiom) ax);
} else if (ax instanceof OWLSubPropertyChainOfAxiom) {
tr((OWLSubPropertyChainOfAxiom) ax);
} else {
if (!(ax instanceof OWLAnnotationAssertionAxiom)) {
error(ax);
} else {
// we presume this has been processed
}
}
}
if (untranslatableAxioms.isEmpty() == false
&& discardUntranslatable == false) {
try {
String axiomString = OwlStringTools.translate(
untranslatableAxioms, manager);
if (axiomString != null) {
Frame headerFrame = obodoc.getHeaderFrame();
if (headerFrame == null) {
headerFrame = new Frame(FrameType.HEADER);
obodoc.setHeaderFrame(headerFrame);
}
headerFrame.addClause(new Clause(
OboFormatTag.TAG_OWL_AXIOMS, axiomString));
}
} catch (OwlStringException e) {
throw new RuntimeException(e);
}
}
return obodoc;
}
protected void preProcess() {
// converse of postProcess in obo2owl
String viewRel = null;
for (OWLAnnotation ann : owlOntology.getAnnotations()) {
if (ann.getProperty()
.getIRI()
.equals(Obo2OWLVocabulary.IRI_OIO_LogicalDefinitionViewRelation
.getIRI())) {
OWLAnnotationValue v = ann.getValue();
if (v instanceof OWLLiteral) {
viewRel = ((OWLLiteral) v).getLiteral();
} else {
viewRel = getIdentifierUsingBaseOntology((IRI) v);
}
break;
}
}
if (viewRel != null) {
// OWLObjectProperty vp = fac.getOWLObjectProperty(pIRI);
Set<OWLAxiom> rmAxioms = new HashSet<OWLAxiom>();
Set<OWLAxiom> newAxioms = new HashSet<OWLAxiom>();
for (OWLEquivalentClassesAxiom eca : owlOntology
.getAxioms(AxiomType.EQUIVALENT_CLASSES)) {
int numNamed = 0;
Set<OWLClassExpression> xs = new HashSet<OWLClassExpression>();
for (OWLClassExpression x : eca.getClassExpressions()) {
if (x instanceof OWLClass) {
xs.add(x);
numNamed++;
continue;
} else if (x instanceof OWLObjectSomeValuesFrom) {
OWLObjectProperty p = (OWLObjectProperty) ((OWLObjectSomeValuesFrom) x)
.getProperty();
if (this.getIdentifier(p).equals(viewRel) == false) {
LOG.log(Level.SEVERE, "Expected: " + viewRel
+ " got: " + p + " in " + eca);
}
xs.add(((OWLObjectSomeValuesFrom) x).getFiller());
} else {
LOG.log(Level.SEVERE, "Unexpected: " + eca);
}
}
if (numNamed == 1) {
rmAxioms.add(eca);
newAxioms.add(fac.getOWLEquivalentClassesAxiom(xs));
} else {
LOG.log(Level.SEVERE, "ECA did not fit expected pattern: "
+ eca);
}
}
manager.removeAxioms(owlOntology, rmAxioms);
manager.addAxioms(owlOntology, newAxioms);
}
}
protected void add(Frame f) {
if (f != null) {
try {
obodoc.addFrame(f);
} catch (Exception ex) {
LOG.log(Level.SEVERE, ex.getMessage(), ex);
}
}
}
protected boolean trObjectProperty(OWLObjectProperty prop, String tag,
String value, Set<OWLAnnotation> annotations) {
if (prop == null || value == null) {
return false;
}
Frame f = getTypedefFrame(prop);
Clause clause;
if (OboFormatTag.TAG_ID.getTag().equals(tag)) {
clause = f.getClause(tag);
if (tag != null) {
clause.setValue(value);
} else {
clause = new Clause(tag, value);
f.addClause(clause);
}
} else {
clause = new Clause(tag, value);
f.addClause(clause);
}
addQualifiers(clause, annotations);
return true;
}
protected boolean trObjectProperty(OWLObjectProperty prop, String tag,
Boolean value, Set<OWLAnnotation> annotations) {
if (prop == null || value == null) {
return false;
}
Frame f = getTypedefFrame(prop);
Clause clause = new Clause(tag);
clause.addValue(value);
f.addClause(clause);
addQualifiers(clause, annotations);
return true;
}
protected void trNaryPropertyAxiom(
OWLNaryPropertyAxiom<OWLObjectPropertyExpression> ax, String tag) {
Set<OWLObjectPropertyExpression> set = ax.getProperties();
if (set.size() > 1) {
boolean first = true;
OWLObjectProperty prop = null;
String disjointFrom = null;
for (OWLObjectPropertyExpression ex : set) {
if (ex.isBottomEntity() || ex.isTopEntity()) {
error(tag
+ " using Top or Bottom entities are not supported in OBO.",
ax);
return;
}
if (first) {
first = false;
if (ex instanceof OWLObjectProperty) {
prop = (OWLObjectProperty) ex;
}
} else {
disjointFrom = this.getIdentifier(ex); // getIdentifier(ex);
}
}
if (trObjectProperty(prop, tag, disjointFrom, ax.getAnnotations())) {
return;
}
}
error(ax);
}
protected void tr(OWLSubPropertyChainOfAxiom ax) {
OWLObjectPropertyExpression pEx = ax.getSuperProperty();
if (pEx.isAnonymous()) {
error(ax);
return;
}
OWLObjectProperty p = pEx.asOWLObjectProperty();
Frame f = getTypedefFrame(p);
if (p.isBottomEntity() || p.isTopEntity()) {
error("Property chains using Top or Bottom entities are not supported in OBO.",
ax);
return;
}
List<OWLObjectPropertyExpression> list = ax.getPropertyChain();
if (list.size() != 2) {
error(ax);
return;
}
final OWLObjectPropertyExpression exp1 = list.get(0);
final OWLObjectPropertyExpression exp2 = list.get(1);
if (exp1.isBottomEntity() || exp1.isTopEntity()
|| exp2.isBottomEntity() || exp2.isTopEntity()) {
error("Property chains using Top or Bottom entities are not supported in OBO.",
ax);
return;
}
String rel1 = getIdentifier(exp1);
String rel2 = getIdentifier(exp2);
if (rel1 == null || rel2 == null) {
error(ax);
return;
}
Clause clause;
// set of unprocessed annotations
final Set<OWLAnnotation> unprocessedAnnotations = new HashSet<OWLAnnotation>(
ax.getAnnotations());
if (rel1.equals(f.getId())) {
clause = new Clause(OboFormatTag.TAG_TRANSITIVE_OVER, rel2);
} else {
OboFormatTag tag = OboFormatTag.TAG_HOLDS_OVER_CHAIN;
for (OWLAnnotation ann : ax.getAnnotations()) {
if (OWLAPIObo2Owl.IRI_PROP_ISREVERSIBLEPROPERTYCHAIN.equals(ann
.getProperty().getIRI().toString())) {
tag = OboFormatTag.TAG_EQUIVALENT_TO_CHAIN;
// remove annotation from unprocessed set.
unprocessedAnnotations.remove(ann);
break;
}
}
clause = new Clause(tag);
clause.addValue(rel1);
clause.addValue(rel2);
}
f.addClause(clause);
addQualifiers(clause, unprocessedAnnotations);
}
protected void tr(OWLEquivalentObjectPropertiesAxiom ax) {
trNaryPropertyAxiom(ax, OboFormatTag.TAG_EQUIVALENT_TO.getTag());
}
protected void tr(OWLTransitiveObjectPropertyAxiom ax) {
OWLObjectPropertyExpression prop = ax.getProperty();
if (prop instanceof OWLObjectProperty) {
if (trObjectProperty((OWLObjectProperty) prop,
OboFormatTag.TAG_IS_TRANSITIVE.getTag(), Boolean.TRUE,
ax.getAnnotations())) {
return;
}
}
error(ax);
}
protected void tr(OWLDisjointObjectPropertiesAxiom ax) {
trNaryPropertyAxiom(ax, OboFormatTag.TAG_DISJOINT_FROM.getTag());
}
protected void tr(OWLReflexiveObjectPropertyAxiom ax) {
OWLObjectPropertyExpression prop = ax.getProperty();
if (prop instanceof OWLObjectProperty) {
if (trObjectProperty((OWLObjectProperty) prop,
OboFormatTag.TAG_IS_REFLEXIVE.getTag(), Boolean.TRUE,
ax.getAnnotations())) {
return;
}
}
error(ax);
}
protected void tr(OWLInverseFunctionalObjectPropertyAxiom ax) {
OWLObjectPropertyExpression prop = ax.getProperty();
if (prop instanceof OWLObjectProperty) {
if (trObjectProperty((OWLObjectProperty) prop,
OboFormatTag.TAG_IS_INVERSE_FUNCTIONAL.getTag(),
Boolean.TRUE, ax.getAnnotations())) {
return;
}
}
error(ax);
}
protected void tr(OWLInverseObjectPropertiesAxiom ax) {
OWLObjectPropertyExpression prop1 = ax.getFirstProperty();
OWLObjectPropertyExpression prop2 = ax.getSecondProperty();
if (prop1 instanceof OWLObjectProperty
&& prop2 instanceof OWLObjectProperty) {
if (trObjectProperty((OWLObjectProperty) prop1,
OboFormatTag.TAG_INVERSE_OF.getTag(),
this.getIdentifier(prop2), ax.getAnnotations())) {
return;
}
}
error(ax);
}
protected void tr(OWLObjectPropertyDomainAxiom ax) {
final OWLClassExpression domain = ax.getDomain();
OWLObjectPropertyExpression propEx = ax.getProperty();
if (propEx.isAnonymous()) {
error(ax);
return;
}
OWLObjectProperty prop = propEx.asOWLObjectProperty();
if (domain.isBottomEntity() || domain.isTopEntity()) {
// at least get the type def frame
getTypedefFrame(prop);
// now throw the error
error("domains using top or bottom entities are not translatable to OBO.",
ax);
return;
}
String range = this.getIdentifier(domain);
if (range != null) {
if (trObjectProperty(prop, OboFormatTag.TAG_DOMAIN.getTag(), range,
ax.getAnnotations())) {
return;
}
}
error(ax);
}
protected void tr(OWLAsymmetricObjectPropertyAxiom ax) {
OWLObjectPropertyExpression prop = ax.getProperty();
if (prop instanceof OWLObjectProperty) {
if (trObjectProperty((OWLObjectProperty) prop,
OboFormatTag.TAG_IS_ASYMMETRIC.getTag(), Boolean.TRUE,
ax.getAnnotations())) {
return;
}
}
error(ax);
}
protected void tr(OWLSymmetricObjectPropertyAxiom ax) {
OWLObjectPropertyExpression prop = ax.getProperty();
if (prop instanceof OWLObjectProperty) {
if (trObjectProperty((OWLObjectProperty) prop,
OboFormatTag.TAG_IS_SYMMETRIC.getTag(), Boolean.TRUE,
ax.getAnnotations())) {
return;
}
}
error(ax);
}
protected void tr(OWLFunctionalObjectPropertyAxiom ax) {
OWLObjectPropertyExpression prop = ax.getProperty();
if (prop instanceof OWLObjectProperty) {
if (trObjectProperty((OWLObjectProperty) prop,
OboFormatTag.TAG_IS_FUNCTIONAL.getTag(), Boolean.TRUE,
ax.getAnnotations())) {
return;
}
}
error(ax);
}
protected void tr(OWLObjectPropertyRangeAxiom ax) {
final OWLClassExpression owlRange = ax.getRange();
OWLObjectPropertyExpression propEx = ax.getProperty();
if (propEx.isAnonymous()) {
error(ax);
}
OWLObjectProperty prop = propEx.asOWLObjectProperty();
if (owlRange.isBottomEntity() || owlRange.isTopEntity()) {
// at least create the property frame
getTypedefFrame(prop);
// error message
error("ranges using top or bottom entities are not translatable to OBO.",
ax);
return;
}
String range = this.getIdentifier(owlRange); // getIdentifier(ax.getRange());
if (range != null) {
if (trObjectProperty(prop, OboFormatTag.TAG_RANGE.getTag(), range,
ax.getAnnotations())) {
return;
}
}
error(ax);
}
protected void tr(OWLSubObjectPropertyOfAxiom ax) {
OWLObjectPropertyExpression sup = ax.getSuperProperty();
OWLObjectPropertyExpression sub = ax.getSubProperty();
if (sub.isBottomEntity() || sub.isTopEntity() || sup.isBottomEntity()
|| sub.isTopEntity()) {
error("SubProperties using Top or Bottom entites are not supported in OBO.");
return;
}
if (sub instanceof OWLObjectProperty
&& sup instanceof OWLObjectProperty) {
String supId = this.getIdentifier(sup);
if (supId.startsWith("owl:")) {
return;
}
Frame f = getTypedefFrame((OWLObjectProperty) sub);
Clause clause = new Clause(OboFormatTag.TAG_IS_A, supId);
f.addClause(clause);
addQualifiers(clause, ax.getAnnotations());
} else {
error(ax);
}
}
protected void tr(OWLSubAnnotationPropertyOfAxiom ax) {
OWLAnnotationProperty sup = ax.getSuperProperty();
OWLAnnotationProperty sub = ax.getSubProperty();
if (sub.isBottomEntity() || sub.isTopEntity() || sup.isBottomEntity()
|| sub.isTopEntity()) {
error("SubAnnotationProperties using Top or Bottom entites are not supported in OBO.");
return;
}
String _tag = owlObjectToTag(sup);
if (OboFormatTag.TAG_SYNONYMTYPEDEF.getTag().equals(_tag)) {
String name = "";
String scope = null;
for (OWLAnnotationAssertionAxiom axiom : sub
.getAnnotationAssertionAxioms(owlOntology)) {
String tg = owlObjectToTag(axiom.getProperty());
if (OboFormatTag.TAG_NAME.getTag().equals(tg)) {
name = ((OWLLiteral) axiom.getValue()).getLiteral();
} else if (OboFormatTag.TAG_SCOPE.getTag().equals(tg)) {
scope = owlObjectToTag(axiom.getValue());
}
}
Frame hf = obodoc.getHeaderFrame();
Clause clause = new Clause(OboFormatTag.TAG_SYNONYMTYPEDEF);
clause.addValue(this.getIdentifier(sub));
clause.addValue(name);
if (scope != null) {
clause.addValue(scope);
}
addQualifiers(clause, ax.getAnnotations());
if (!hf.getClauses().contains(clause)) {
hf.addClause(clause);
} else {
LOG.log(Level.WARNING, "duplicate clause: " + clause
+ " in header");
}
return;
} else if (OboFormatTag.TAG_SUBSETDEF.getTag().equals(_tag)) {
String comment = "";
for (OWLAnnotationAssertionAxiom axiom : sub
.getAnnotationAssertionAxioms(owlOntology)) {
String tg = owlObjectToTag(axiom.getProperty());
if (OboFormatTag.TAG_COMMENT.getTag().equals(tg)) {
comment = ((OWLLiteral) axiom.getValue()).getLiteral();
if (comment != null) {
break;
}
}
}
Frame hf = obodoc.getHeaderFrame();
Clause clause = new Clause(OboFormatTag.TAG_SUBSETDEF);
clause.addValue(this.getIdentifier(sub));
clause.addValue(comment);
if (!hf.getClauses().contains(clause)) {
hf.addClause(clause);
} else {
LOG.log(Level.WARNING, "duplicate clause: " + clause
+ " in header");
}
addQualifiers(clause, ax.getAnnotations());
return;
}
if (sub instanceof OWLObjectProperty
&& sup instanceof OWLObjectProperty) {
String supId = this.getIdentifier(sup); // getIdentifier(sup);
if (supId.startsWith("owl:")) {
return;
}
Frame f = getTypedefFrame(sub);
Clause clause = new Clause(OboFormatTag.TAG_IS_A, supId);
f.addClause(clause);
addQualifiers(clause, ax.getAnnotations());
} else {
error(ax);
}
}
protected Pattern absoulteURLPattern = Pattern.compile("<\\s*http.*?>");
protected void tr(OWLAnnotationAssertionAxiom aanAx, Frame frame) {
boolean success = tr(aanAx.getProperty(), aanAx.getValue(),
aanAx.getAnnotations(), frame);
if (!success) {
untranslatableAxioms.add(aanAx);
}
}
protected boolean tr(OWLAnnotationProperty prop, OWLAnnotationValue annVal,
Set<OWLAnnotation> qualifiers, Frame frame) {
String tagString = owlObjectToTag(prop);
OboFormatTag tag = null;
if (tagString != null) {
tag = OBOFormatConstants.getTag(tagString);
}
if (tag == null) {
if (annVal instanceof IRI && FrameType.TERM.equals(frame.getType())) {
if (isMetadataTag(prop)) {
String propId = this.getIdentifier(prop);
if (propId != null) {
Clause clause = new Clause(
OboFormatTag.TAG_RELATIONSHIP);
clause.addValue(propId);
clause.addValue(getIdentifier((IRI) annVal));
addQualifiers(clause, qualifiers);
frame.addClause(clause);
return true;
}
}
}
// annotation property does not correspond to a mapping to a tag in
// the OBO syntax -
// use the property_value tag
return trGenericPropertyValue(prop, annVal, qualifiers, frame);
}
String value = getValue(annVal, tagString);
if (!value.isEmpty()) {
if (tag == OboFormatTag.TAG_ID) {
if (frame.getId().equals(value) == false) {
error("Conflicting id definitions: 1) " + frame.getId()
+ " 2)" + value);
return false;
}
return true;
}
Clause clause = new Clause(tag);
if (tag == OboFormatTag.TAG_DATE) {
try {
clause.addValue(OBOFormatConstants.headerDateFormat.get()
.parseObject(value));
} catch (ParseException e) {
error("Could not parse date string: " + value);
return false;
}
} else {
clause.addValue(value);
}
Set<OWLAnnotation> unprocessedQualifiers = new HashSet<OWLAnnotation>(
qualifiers);
if (tag == OboFormatTag.TAG_DEF) {
for (OWLAnnotation aan : qualifiers) {
String propId = owlObjectToTag(aan.getProperty());
if ("xref".equals(propId)) {
OWLAnnotationValue v = aan.getValue();
String xrefValue;
if (v instanceof IRI) {
xrefValue = ((IRI) v).toString();
} else {
xrefValue = ((OWLLiteral) v).getLiteral();
}
Xref xref = new Xref(xrefValue);
clause.addXref(xref);
unprocessedQualifiers.remove(aan);
}
}
} else if (tag == OboFormatTag.TAG_XREF) {
Xref xref = new Xref(value);
for (OWLAnnotation annotation : qualifiers) {
if (fac.getRDFSLabel().equals(annotation.getProperty())) {
OWLAnnotationValue owlAnnotationValue = annotation
.getValue();
if (owlAnnotationValue instanceof OWLLiteral) {
unprocessedQualifiers.remove(annotation);
String xrefAnnotation = ((OWLLiteral) owlAnnotationValue)
.getLiteral();
if (xrefAnnotation != null) {
xrefAnnotation = xrefAnnotation.trim();
if (xrefAnnotation.length() > 0) {
xref.setAnnotation(xrefAnnotation);
}
}
}
}
}
clause.setValue(xref);
} else if (tag == OboFormatTag.TAG_EXACT
|| tag == OboFormatTag.TAG_NARROW
|| tag == OboFormatTag.TAG_BROAD
|| tag == OboFormatTag.TAG_RELATED) {
handleSynonym(qualifiers, tag.getTag(), clause,
unprocessedQualifiers);
} else if (tag == OboFormatTag.TAG_SYNONYM) {
// This should never happen.
// All synonyms need to be qualified with a type.
String synonymType = null;
handleSynonym(qualifiers, synonymType, clause,
unprocessedQualifiers);
}
addQualifiers(clause, unprocessedQualifiers);
// before adding the clause check for redundant clauses
boolean redundant = false;
for (Clause frameClause : frame.getClauses()) {
if (clause.equals(frameClause)) {
redundant = handleDuplicateClause(frame, frameClause);
}
}
if (!redundant) {
frame.addClause(clause);
}
} else {
return false;
}
return true;
}
private boolean isMetadataTag(OWLAnnotationProperty p) {
final IRI metadataTagIRI = IRI
.create(Obo2OWLConstants.OIOVOCAB_IRI_PREFIX
+ OboFormatTag.TAG_IS_METADATA_TAG.getTag());
Set<OWLAnnotationAssertionAxiom> axioms = owlOntology
.getAnnotationAssertionAxioms(p.getIRI());
for (OWLAnnotationAssertionAxiom ax : axioms) {
if (metadataTagIRI.equals(ax.getProperty().getIRI())) {
return true;
}
}
return false;
}
protected void handleSynonym(Set<OWLAnnotation> qualifiers, String scope,
Clause clause, Set<OWLAnnotation> unprocessedQualifiers) {
clause.setTag(OboFormatTag.TAG_SYNONYM.getTag());
String type = null;
clause.setXrefs(new Vector<Xref>());
for (OWLAnnotation aan : qualifiers) {
String propId = owlObjectToTag(aan.getProperty());
if (OboFormatTag.TAG_XREF.getTag().equals(propId)) {
OWLAnnotationValue v = aan.getValue();
String xrefValue;
if (v instanceof IRI) {
xrefValue = ((IRI) v).toString();
} else {
xrefValue = ((OWLLiteral) v).getLiteral();
}
Xref xref = new Xref(xrefValue);
clause.addXref(xref);
unprocessedQualifiers.remove(aan);
} else if (OboFormatTag.TAG_HAS_SYNONYM_TYPE.getTag()
.equals(propId)) {
type = getIdentifier(aan.getValue());
unprocessedQualifiers.remove(aan);
}
}
if (scope != null) {
clause.addValue(scope);
if (type != null) {
clause.addValue(type);
}
}
}
/**
* Handle a duplicate clause in a frame during translation.
*
* @param frame
* frame
* @param clause
* clause
* @return true if the clause is to be marked as redundant and will not be
* added to the
*/
protected boolean handleDuplicateClause(Frame frame, Clause clause) {
// default is to report it via the logger and remove it.
LOG.log(Level.WARNING, "Duplicate clause '" + clause
+ "' generated in frame: " + frame.getId());
return true;
}
protected boolean trGenericPropertyValue(OWLAnnotationProperty prop,
OWLAnnotationValue annVal, Set<OWLAnnotation> qualifiers,
Frame frame) {
// no built-in obo tag for this: use the generic property_value tag
Clause clause = new Clause(OboFormatTag.TAG_PROPERTY_VALUE.getTag());
String propId = this.getIdentifier(prop);
addQualifiers(clause, qualifiers);
if (propId.equals("shorthand") == false) {
clause.addValue(propId);
if (annVal instanceof OWLLiteral) {
OWLLiteral owlLiteral = (OWLLiteral) annVal;
clause.addValue(owlLiteral.getLiteral());
OWLDatatype datatype = owlLiteral.getDatatype();
IRI dataTypeIri = datatype.getIRI();
if (!OWL2Datatype.isBuiltIn(dataTypeIri)) {
error("Untranslatable axiom due to unknown data type: "
+ annVal);
return false;
}
if (Namespaces.XSD.inNamespace(dataTypeIri)) {
clause.addValue(dataTypeIri.prefixedBy("xsd:"));
} else if (dataTypeIri.isPlainLiteral()) {
clause.addValue("xsd:string");
} else {
clause.addValue(dataTypeIri.toString());
}
} else if (annVal instanceof IRI) {
clause.addValue(getIdentifierUsingBaseOntology((IRI) annVal));
}
frame.addClause(clause);
}
return true;
}
protected String getValue(OWLAnnotationValue annVal, String tag) {
String value = annVal.toString();
if (annVal instanceof OWLLiteral) {
value = ((OWLLiteral) annVal).getLiteral();
} else if (annVal instanceof IRI) {
value = getIdentifierUsingBaseOntology((IRI) annVal);
}
if (OboFormatTag.TAG_EXPAND_EXPRESSION_TO.getTag().equals(tag)) {
Matcher matcher = absoulteURLPattern.matcher(value);
while (matcher.find()) {
String m = matcher.group();
m = m.replace("<", "");
m = m.replace(">", "");
int i = m.lastIndexOf("/");
m = m.substring(i + 1);
value = value.replace(matcher.group(), m);
}
}
return value;
}
protected void addQualifiers(Clause c, Set<OWLAnnotation> qualifiers) {
for (OWLAnnotation ann : qualifiers) {
String prop = owlObjectToTag(ann.getProperty());
if (prop == null) {
prop = ann.getProperty().getIRI().toString();
}
if (prop.equals("gci_relation") || prop.equals("gci_filler")
|| prop.equals("cardinality")
|| prop.equals("minCardinality")
|| prop.equals("maxCardinality") || prop.equals("all_some")
|| prop.equals("all_only")) {
continue;
}
String value = ann.getValue().toString();
if (ann.getValue() instanceof OWLLiteral) {
value = ((OWLLiteral) ann.getValue()).getLiteral();
} else if (ann.getValue() instanceof IRI) {
value = getIdentifierUsingBaseOntology((IRI) ann.getValue()); // getIdentifier((IRI)aanAx.getValue());
}
QualifierValue qv = new QualifierValue(prop, value);
c.addQualifierValue(qv);
}
}
/**
* if does not match this pattern, then retain original IRI
*
* @param ontology
* ontology
* @return The OBO ID of the ontology
*/
public static String getOntologyId(OWLOntology ontology) {
return getOntologyId(ontology.getOntologyID().getOntologyIRI());
}
/**
* @param iriObj
* iriObj
* @return ontology id
*/
public static String getOntologyId(IRI iriObj) {
// String id = getIdentifier(ontology.getOntologyID().getOntologyIRI());
String iri = iriObj.toString();
String id;
if (iri.startsWith("http://purl.obolibrary.org/obo/")) {
id = iri.replace("http://purl.obolibrary.org/obo/", "");
if (id.endsWith(".owl")) {
id = id.replaceFirst(".owl$", "");
}
} else {
id = iri;
}
return id;