1414 */
1515package ubic .basecode .ontology .model ;
1616
17+ import com .hp .hpl .jena .ontology .*;
18+ import com .hp .hpl .jena .rdf .model .*;
19+ import com .hp .hpl .jena .util .iterator .ExtendedIterator ;
20+ import org .apache .commons .collections4 .CollectionUtils ;
21+
1722import java .util .Collection ;
1823import java .util .HashSet ;
24+ import java .util .Objects ;
1925import java .util .Set ;
2026
21- import org .apache .commons .lang3 .ObjectUtils ;
22-
23- import com .hp .hpl .jena .ontology .AllValuesFromRestriction ;
24- import com .hp .hpl .jena .ontology .ConversionException ;
25- import com .hp .hpl .jena .ontology .Individual ;
26- import com .hp .hpl .jena .ontology .OntClass ;
27- import com .hp .hpl .jena .ontology .OntProperty ;
28- import com .hp .hpl .jena .ontology .OntResource ;
29- import com .hp .hpl .jena .ontology .Restriction ;
30- import com .hp .hpl .jena .ontology .SomeValuesFromRestriction ;
31- import com .hp .hpl .jena .rdf .model .Property ;
32- import com .hp .hpl .jena .rdf .model .RDFNode ;
33- import com .hp .hpl .jena .rdf .model .Resource ;
34- import com .hp .hpl .jena .rdf .model .ResourceFactory ;
35- import com .hp .hpl .jena .rdf .model .Statement ;
36- import com .hp .hpl .jena .rdf .model .StmtIterator ;
37- import com .hp .hpl .jena .util .iterator .ExtendedIterator ;
38-
3927/**
4028 * Represents a class in an ontology
4129 *
@@ -45,30 +33,43 @@ public class OntologyTermImpl extends AbstractOntologyResource implements Ontolo
4533
4634 private static final String HAS_ALTERNATE_ID = "http://www.geneontology.org/formats/oboInOwl#hasAlternativeId" ;
4735 private static final String NOTHING = "http://www.w3.org/2002/07/owl#Nothing" ;
48- private static Set <String > REJECT_PARENT_URI = new HashSet <>();
4936
5037 /**
51- *
38+ * Properties through which propagation is allowed for {@link #getChildren(boolean)}
5239 */
53- private static final long serialVersionUID = 1L ;
40+ private static final Set < String > PROPAGATE_FROM_URIS = new HashSet <>() ;
5441
5542 /**
56- * Should has_proper_part be used to indicate additional parent/child relations.
43+ * Properties through which propagation is allowed for {@link #getParents(boolean)}
44+ */
45+ private static final Set <String > PROPAGATE_INTO_URIS = new HashSet <>();
46+
47+ private static final Set <String > REJECT_PARENT_URIS = new HashSet <>();
48+
49+ /**
50+ *
5751 */
58- private static final boolean USE_PROPER_PART_RESTRICTIONS = true ;
52+ private static final long serialVersionUID = 1L ;
53+
5954 static {
60- REJECT_PARENT_URI .add ( "http://www.ifomis.org/bfo/1.1/snap#IndependentContinuant" );
61- REJECT_PARENT_URI .add ( "http://www.ifomis.org/bfo/1.1/snap#Continuant" );
62- REJECT_PARENT_URI .add ( "http://www.ifomis.org/bfo/1.1/snap#MaterialEntity" );
55+ CollectionUtils .addAll ( PROPAGATE_INTO_URIS ,
56+ "http://www.obofoundry.org/ro/ro.owl#proper_part_of" ,
57+ "http://purl.obolibrary.org/obo/BFO_0000050" );
58+ CollectionUtils .addAll ( PROPAGATE_FROM_URIS ,
59+ "http://www.obofoundry.org/ro/ro.owl#has_proper_part" ,
60+ "http://purl.obolibrary.org/obo/BFO_0000051" );
61+ REJECT_PARENT_URIS .add ( "http://www.ifomis.org/bfo/1.1/snap#IndependentContinuant" );
62+ REJECT_PARENT_URIS .add ( "http://www.ifomis.org/bfo/1.1/snap#Continuant" );
63+ REJECT_PARENT_URIS .add ( "http://www.ifomis.org/bfo/1.1/snap#MaterialEntity" );
6364
6465 // anatomical entity
65- REJECT_PARENT_URI .add ( "http://ontology.neuinfo.org/NIF/BiomaterialEntities/NIF-GrossAnatomy.owl#birnlex_6" );
66+ REJECT_PARENT_URIS .add ( "http://ontology.neuinfo.org/NIF/BiomaterialEntities/NIF-GrossAnatomy.owl#birnlex_6" );
6667 }
6768
6869 private String label = null ;
6970 private String localName = null ;
7071
71- private transient OntClass ontResource = null ;
72+ private final transient OntClass ontResource ;
7273
7374 public OntologyTermImpl ( OntClass resource ) {
7475 this .ontResource = resource ;
@@ -88,17 +89,18 @@ public boolean equals( Object obj ) {
8889
8990 final OntologyTermImpl that = ( OntologyTermImpl ) obj ;
9091 if ( this .getUri () != null ) {
91- return ObjectUtils .equals ( this .getUri (), that .getUri () );
92+ return Objects .equals ( this .getUri (), that .getUri () );
9293 }
93- return ObjectUtils .equals ( this .getTerm (), that .getTerm () );
94+ return Objects .equals ( this .getTerm (), that .getTerm () );
9495 }
9596
9697 @ Override
9798 public Collection <String > getAlternativeIds () {
9899 Collection <String > results = new HashSet <>();
99100
100101 Property alternate = ResourceFactory .createProperty ( HAS_ALTERNATE_ID );
101- for ( StmtIterator it = this .ontResource .listProperties ( alternate ); it .hasNext (); ) {
102+ StmtIterator it = this .ontResource .listProperties ( alternate );
103+ while ( it .hasNext () ) {
102104 Statement statement = it .next ();
103105 results .add ( statement .asTriple ().getMatchObject ().getLiteralLexicalForm () );
104106 }
@@ -152,10 +154,6 @@ public Collection<OntologyIndividual> getIndividuals() {
152154 return getIndividuals ( true );
153155 }
154156
155- /**
156- * @param direct
157- * @return
158- */
159157 @ Override
160158 public Collection <OntologyIndividual > getIndividuals ( boolean direct ) {
161159 Collection <OntologyIndividual > inds = new HashSet <>();
@@ -201,14 +199,12 @@ public Collection<OntologyRestriction> getRestrictions() {
201199 ExtendedIterator <OntClass > iterator = ontResource .listSuperClasses ( false );
202200 while ( iterator .hasNext () ) {
203201 OntClass c = iterator .next ();
204- Restriction r = null ;
202+ Restriction r ;
205203 try {
206- r = c .asRestriction ();
207- result .add ( RestrictionFactory .asRestriction ( r ) );
204+ result .add ( RestrictionFactory .asRestriction ( c .asRestriction () ) );
208205 } catch ( Exception e ) {
209206
210207 }
211-
212208 }
213209
214210 // Check superclasses for any ADDITIONAL restrictions.
@@ -221,9 +217,10 @@ public Collection<OntologyRestriction> getRestrictions() {
221217 } catch ( Exception e ) {
222218 // not a restriction, but a superclass that might have restrictions
223219 ExtendedIterator <OntClass > supClassesIt = c .listSuperClasses ( false );
224- loop : while ( supClassesIt .hasNext () ) {
220+ loop :
221+ while ( supClassesIt .hasNext () ) {
225222 OntClass sc = supClassesIt .next ();
226- Restriction sr = null ;
223+ Restriction sr ;
227224 try {
228225 sr = sc .asRestriction ();
229226
@@ -253,7 +250,7 @@ public Collection<OntologyRestriction> getRestrictions() {
253250 */
254251 @ Override
255252 public String getTerm () {
256- String res = null ;
253+ String res ;
257254 if ( this .label != null ) {
258255 res = this .label ;
259256 } else if ( this .localName != null ) {
@@ -322,11 +319,11 @@ public boolean isTermObsolete() {
322319
323320 if ( parentOntologyTerm .getUri () != null
324321 && parentOntologyTerm .getUri ().equalsIgnoreCase (
325- "http://bioontology.org/projects/ontologies/birnlex#_birnlex_retired_class" )
322+ "http://bioontology.org/projects/ontologies/birnlex#_birnlex_retired_class" )
326323 || parentOntologyTerm
327- .getUri ()
328- .equalsIgnoreCase (
329- "http://ontology.neuinfo.org/NIF/Backend/BIRNLex_annotation_properties.owl#_birnlex_retired_class" ) ) {
324+ .getUri ()
325+ .equalsIgnoreCase (
326+ "http://ontology.neuinfo.org/NIF/Backend/BIRNLex_annotation_properties.owl#_birnlex_retired_class" ) ) {
330327 return true ;
331328 }
332329 }
@@ -389,13 +386,13 @@ private void getChildren( boolean direct, Collection<OntologyTerm> work ) {
389386 // some reasoners will infer owl#Nothing as a subclass of everything
390387 if ( c .getURI () == null || c .getURI ().equals ( NOTHING ) ) continue ;
391388
392- if ( USE_PROPER_PART_RESTRICTIONS && c .isRestriction () ) {
389+ if ( c .isRestriction () ) {
393390
394391 Restriction restriction = c .asRestriction ();
395392
396393 OntProperty onProperty = restriction .getOnProperty ();
397394
398- if ( !onProperty .getURI (). equals ( "http://www.obofoundry.org/ro/ro.owl#has_proper_part" ) ) {
395+ if ( !PROPAGATE_FROM_URIS . contains ( onProperty .getURI () ) ) {
399396 continue ;
400397 }
401398
@@ -417,41 +414,39 @@ private void getChildren( boolean direct, Collection<OntologyTerm> work ) {
417414 // log.info( c );
418415 }
419416
420- if ( USE_PROPER_PART_RESTRICTIONS ) {
421- ExtendedIterator <OntClass > sciterator = this .ontResource .listSuperClasses ( true );
422- while ( sciterator .hasNext () ) {
423- OntClass c = sciterator .next ();
424- if ( !c .isRestriction () ) {
425- continue ;
426- }
417+ ExtendedIterator <OntClass > sciterator = this .ontResource .listSuperClasses ( true );
418+ while ( sciterator .hasNext () ) {
419+ OntClass c = sciterator .next ();
420+ if ( !c .isRestriction () ) {
421+ continue ;
422+ }
427423
428- Restriction restriction = c .asRestriction ();
424+ Restriction restriction = c .asRestriction ();
429425
430- try {
431- OntProperty onProperty = restriction .getOnProperty ();
432- if ( !onProperty .getURI ().equals ( "http://www.obofoundry.org/ro/ro.owl#has_proper_part" ) ) {
433- continue ;
434- }
435- } catch ( ConversionException e ) {
426+ try {
427+ OntProperty onProperty = restriction .getOnProperty ();
428+ if ( !PROPAGATE_FROM_URIS .contains ( onProperty .getURI () ) ) {
436429 continue ;
437430 }
431+ } catch ( ConversionException e ) {
432+ continue ;
433+ }
438434
439- Resource r = getRestrictionValue ( restriction );
440- if ( r == null ) continue ;
441-
442- // if ( !( r instanceof OntClass ) ) {
443- // // means our owl file is incomplete, is in tests.
444- // log.info( r );
445- // continue;
446- // }
435+ Resource r = getRestrictionValue ( restriction );
436+ if ( r == null ) continue ;
447437
448- OntologyTerm child = fromOntClass ( ( OntClass ) r );
449- if ( ! work . contains ( child ) ) {
450- work . add ( child );
451- if ( ! direct ) ( ( OntologyTermImpl ) child ). getChildren ( false , work ) ;
452- }
438+ // if ( !( r instanceof OntClass ) ) {
439+ // // means our owl file is incomplete, is in tests.
440+ // log.info( r );
441+ // continue ;
442+ // }
453443
444+ OntologyTerm child = fromOntClass ( ( OntClass ) r );
445+ if ( !work .contains ( child ) ) {
446+ work .add ( child );
447+ if ( !direct ) ( ( OntologyTermImpl ) child ).getChildren ( false , work );
454448 }
449+
455450 }
456451 }
457452
@@ -472,28 +467,27 @@ private void getParents( boolean direct, Collection<OntologyTerm> work ) {
472467 try {
473468 OntClass c = iterator .next ();
474469
475- if ( USE_PROPER_PART_RESTRICTIONS && c .isRestriction () ) {
470+ if ( c .isRestriction () ) {
476471 Restriction restriction = c .asRestriction ();
477472
478473 OntProperty onProperty = restriction .getOnProperty ();
479474
480475 assert onProperty != null ;
481476
482477 // We ignore this... hack.
483- if ( !onProperty .getURI (). equals ( "http://www.obofoundry.org/ro/ro.owl#proper_part_of" ) ) {
478+ if ( !PROPAGATE_INTO_URIS . contains ( onProperty .getURI () ) ) {
484479 continue ;
485480 }
486481
487482 Resource r = getRestrictionValue ( restriction );
488483
489484 if ( r == null ) continue ;
490485
491- assert r != null ;
492486 if ( log .isDebugEnabled () ) log .debug ( " Some from:" + r + " " + onProperty .getURI () );
493487
494488 OntologyTerm parent = fromOntClass ( ( OntClass ) r );
495489
496- if ( REJECT_PARENT_URI .contains ( parent .getUri () ) ) continue ;
490+ if ( REJECT_PARENT_URIS .contains ( parent .getUri () ) ) continue ;
497491
498492 // avoid endless regression
499493 if ( !work .contains ( parent ) ) {
@@ -505,7 +499,7 @@ private void getParents( boolean direct, Collection<OntologyTerm> work ) {
505499 // not a restriction.
506500 OntologyTerm parent = this .fromOntClass ( c );
507501
508- if ( REJECT_PARENT_URI .contains ( parent .getUri () ) ) continue ;
502+ if ( REJECT_PARENT_URIS .contains ( parent .getUri () ) ) continue ;
509503
510504 if ( !work .contains ( parent ) ) {
511505 work .add ( parent );
@@ -517,7 +511,6 @@ private void getParents( boolean direct, Collection<OntologyTerm> work ) {
517511 }
518512 } catch ( ConversionException e ) {
519513 if ( log .isDebugEnabled () ) log .debug ( e .getMessage () );
520- continue ;
521514 }
522515
523516 }
0 commit comments