Skip to content

Commit fbb3877

Browse files
committed
Few cleanups and improvements for the ontology API
Add utilities for retrieving annotation by URI. Move some of the method from OntologyTerm to OntologyResource. Remove unused code and fix all warnings.
1 parent 9dcd5ec commit fbb3877

25 files changed

Lines changed: 177 additions & 195 deletions

src/ubic/basecode/ontology/jena/AbstractOntologyResource.java

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@
3535
*/
3636
abstract class AbstractOntologyResource implements OntologyResource {
3737

38-
protected static Logger log = LoggerFactory.getLogger( AbstractOntologyResource.class );
38+
protected static final Logger log = LoggerFactory.getLogger( AbstractOntologyResource.class );
3939

4040
private static final Comparator<OntologyResource> comparator = Comparator
4141
.comparing( OntologyResource::getScore, nullsLast( reverseOrder() ) )
@@ -60,6 +60,12 @@ public String getUri() {
6060
return res.getURI();
6161
}
6262

63+
@Override
64+
public String getLocalName() {
65+
return res.getLocalName();
66+
}
67+
68+
@Override
6369
public String getLabel() {
6470
String label = res.getLabel( "EN" );
6571
if ( label == null ) {
@@ -68,6 +74,16 @@ public String getLabel() {
6874
return label;
6975
}
7076

77+
@Nullable
78+
@Override
79+
public String getComment() {
80+
String label = res.getComment( "EN" );
81+
if ( label == null ) {
82+
label = res.getLabel( null );
83+
}
84+
return label;
85+
}
86+
7187
@Override
7288
public boolean isObsolete() {
7389
return res.hasLiteral( OWL2.deprecated, true );
@@ -94,9 +110,8 @@ public boolean equals( Object obj ) {
94110
if ( other.getLabel() != null ) return false;
95111
} else if ( !getLabel().equals( other.getLabel() ) ) return false;
96112
if ( getUri() == null ) {
97-
if ( other.getUri() != null ) return false;
98-
} else if ( !getUri().equals( other.getUri() ) ) return false;
99-
return true;
113+
return other.getUri() == null;
114+
} else return getUri().equals( other.getUri() );
100115
}
101116

102117
@Override

src/ubic/basecode/ontology/jena/AbstractOntologyService.java

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -729,17 +729,15 @@ private static class State {
729729
private final OntModel model;
730730
@Nullable
731731
private SearchIndex index;
732-
@Nullable
733732
private final Set<Restriction> additionalRestrictions;
734733
private final LanguageLevel languageLevel;
735734
private final InferenceMode inferenceMode;
736735
private final boolean processImports;
737-
@Nullable
738736
private final Set<String> additionalPropertyUris;
739737
@Nullable
740738
private Map<String, String> alternativeIDs;
741739

742-
State( OntModel model, @Nullable SearchIndex index, @Nullable Set<Restriction> additionalRestrictions, @Nullable LanguageLevel languageLevel, InferenceMode inferenceMode, boolean processImports, @Nullable Set<String> additionalPropertyUris ) {
740+
private State( OntModel model, @Nullable SearchIndex index, Set<Restriction> additionalRestrictions, @Nullable LanguageLevel languageLevel, InferenceMode inferenceMode, boolean processImports, Set<String> additionalPropertyUris ) {
743741
this.model = model;
744742
this.index = index;
745743
this.additionalRestrictions = additionalRestrictions;

src/ubic/basecode/ontology/jena/AnnotationPropertyImpl.java

Lines changed: 28 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -33,34 +33,18 @@
3333
*/
3434
class AnnotationPropertyImpl extends AbstractOntologyResource implements AnnotationProperty {
3535

36-
private final String contents;
37-
3836
private final com.hp.hpl.jena.ontology.AnnotationProperty property;
37+
@Nullable
38+
private final RDFNode object;
3939

4040
/**
4141
* @param prop property for the statement
42-
* @param source ontology this relates to.
4342
* @param object of the statement
4443
*/
4544
public AnnotationPropertyImpl( com.hp.hpl.jena.ontology.AnnotationProperty prop, @Nullable RDFNode object ) {
4645
super( prop );
4746
this.property = prop;
48-
49-
if ( object == null ) {
50-
this.contents = null;
51-
} else if ( object.isResource() ) {
52-
// need a level of indirection...
53-
Resource r = ( Resource ) object;
54-
Statement s = r.getProperty( RDFS.label );
55-
if ( s != null ) {
56-
this.contents = s.getObject().toString();
57-
} else {
58-
this.contents = null;
59-
}
60-
} else {
61-
this.contents = JenaUtils.asString( object );
62-
}
63-
47+
this.object = object;
6448
}
6549

6650
@Override
@@ -69,23 +53,12 @@ public boolean equals( @Nullable Object obj ) {
6953
if ( obj == null ) return false;
7054
if ( getClass() != obj.getClass() ) return false;
7155
final AnnotationPropertyImpl other = ( AnnotationPropertyImpl ) obj;
72-
if ( contents == null ) {
73-
if ( other.contents != null ) return false;
74-
} else if ( !contents.equals( other.contents ) ) return false;
56+
if ( object == null ) {
57+
if ( other.object != null ) return false;
58+
} else if ( !object.equals( other.object ) ) return false;
7559
if ( property == null ) {
76-
if ( other.property != null ) return false;
77-
} else if ( !property.equals( other.property ) ) return false;
78-
return true;
79-
}
80-
81-
@Override
82-
public String getUri() {
83-
return property.getURI();
84-
}
85-
86-
@Override
87-
public String getContents() {
88-
return contents;
60+
return other.property == null;
61+
} else return property.equals( other.property );
8962
}
9063

9164
@Override
@@ -99,6 +72,24 @@ public String getProperty() {
9972
}
10073
}
10174

75+
@Override
76+
public String getContents() {
77+
if ( this.object == null ) {
78+
return null;
79+
} else if ( object.isResource() ) {
80+
// need a level of indirection...
81+
Resource r = ( Resource ) object;
82+
Statement s = r.getProperty( RDFS.label );
83+
if ( s != null ) {
84+
return s.getObject().toString();
85+
} else {
86+
return null;
87+
}
88+
} else {
89+
return JenaUtils.asString( object );
90+
}
91+
}
92+
10293
@Override
10394
public boolean isObsolete() {
10495
return super.isObsolete() || property.hasSuperProperty( OBO.ObsoleteProperty, false );
@@ -108,14 +99,14 @@ public boolean isObsolete() {
10899
public int hashCode() {
109100
final int PRIME = 31;
110101
int result = 1;
111-
result = PRIME * result + ( ( contents == null ) ? 0 : contents.hashCode() );
102+
result = PRIME * result + ( ( object == null ) ? 0 : object.hashCode() );
112103
result = PRIME * result + ( ( property == null ) ? 0 : property.hashCode() );
113104
return result;
114105
}
115106

116107
@Override
117108
public String toString() {
118-
return property.getLocalName() + " " + contents;
109+
return property.getLocalName() + " " + object;
119110
}
120111

121112
}

src/ubic/basecode/ontology/jena/IndexerSelector.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,7 @@ public boolean test( Statement s ) {
113113
try {
114114
retain = wantedForIndexing.contains( s.getObject().as( Property.class ) );
115115
} catch ( ConversionException e ) {
116-
log.warn( "Conversion failed for " + s.getObject(), e );
116+
log.warn( "Conversion of " + s.getObject() + " to " + Property.class.getName() + " failed.", e );
117117
}
118118
}
119119

src/ubic/basecode/ontology/jena/JenaUtils.java

Lines changed: 22 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121

2222
class JenaUtils {
2323

24-
protected static Logger log = LoggerFactory.getLogger( JenaUtils.class );
24+
protected static final Logger log = LoggerFactory.getLogger( JenaUtils.class );
2525

2626
public static Collection<OntClass> getParents( OntModel model, Collection<OntClass> ontClasses, boolean direct, @Nullable Set<Restriction> additionalRestrictions ) {
2727
Collection<OntClass> parents = getParentsInternal( model, ontClasses, direct, additionalRestrictions );
@@ -43,15 +43,9 @@ private static Collection<OntClass> getParentsInternal( OntModel model, Collecti
4343
ontClasses = ontClasses.stream()
4444
.map( t -> t.inModel( model ) )
4545
.filter( t -> t.canAs( OntClass.class ) )
46-
.map( t -> {
47-
try {
48-
return t.as( OntClass.class );
49-
} catch ( ConversionException e ) {
50-
log.warn( "Conversion failed for " + t, e );
51-
return null;
52-
}
53-
} )
54-
.filter( Objects::nonNull )
46+
.map( t -> as( t, OntClass.class ) )
47+
.filter( Optional::isPresent )
48+
.map( Optional::get )
5549
.collect( Collectors.toList() );
5650
if ( ontClasses.isEmpty() ) {
5751
return Collections.emptySet();
@@ -113,15 +107,9 @@ public static Collection<OntClass> getChildrenInternal( OntModel model, Collecti
113107
terms = terms.stream()
114108
.map( t -> t.inModel( model ) )
115109
.filter( t -> t.canAs( OntClass.class ) )
116-
.map( t -> {
117-
try {
118-
return t.as( OntClass.class );
119-
} catch ( ConversionException e ) {
120-
log.warn( "Conversion failed for " + t, e );
121-
return null;
122-
}
123-
} )
124-
.filter( Objects::nonNull )
110+
.map( t -> as( t, OntClass.class ) )
111+
.filter( Optional::isPresent )
112+
.map( Optional::get )
125113
.collect( Collectors.toList() );
126114
if ( terms.isEmpty() ) {
127115
return Collections.emptySet();
@@ -149,15 +137,9 @@ public static Collection<OntClass> getChildrenInternal( OntModel model, Collecti
149137
for ( Restriction r : restrictions ) {
150138
result.addAll( model.listResourcesWithProperty( subClassOf, r )
151139
.filterDrop( new BnodeFilter<>() )
152-
.mapWith( r2 -> {
153-
try {
154-
return r2.as( OntClass.class );
155-
} catch ( ConversionException e ) {
156-
log.warn( "Conversion failed for " + r2, e );
157-
return null;
158-
}
159-
} )
160-
.filterKeep( where( Objects::nonNull ) )
140+
.mapWith( r2 -> as( r2, OntClass.class ) )
141+
.filterKeep( where( Optional::isPresent ) )
142+
.mapWith( Optional::get )
161143
.toSet() );
162144
}
163145
}
@@ -238,4 +220,16 @@ public Object visitURI( Resource r, String uri ) {
238220
public static <T> Filter<T> where( Predicate<T> predicate ) {
239221
return new PredicateFilter<>( predicate );
240222
}
223+
224+
public static <T extends RDFNode> Optional<T> as( Resource resource, Class<T> clazz ) {
225+
if ( !resource.canAs( clazz ) ) {
226+
return Optional.empty();
227+
}
228+
try {
229+
return Optional.of( resource.as( clazz ) );
230+
} catch ( ConversionException e ) {
231+
log.warn( "Conversion of " + resource + " to " + clazz.getName() + " failed." );
232+
return Optional.empty();
233+
}
234+
}
241235
}

src/ubic/basecode/ontology/jena/ObjectPropertyImpl.java

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@
2525
import com.hp.hpl.jena.util.iterator.ExtendedIterator;
2626
import ubic.basecode.ontology.model.OntologyTerm;
2727

28-
import javax.annotation.Nullable;
2928
import java.util.Collection;
3029
import java.util.HashSet;
3130
import java.util.Set;
@@ -36,10 +35,9 @@
3635
class ObjectPropertyImpl extends OntologyPropertyImpl implements ubic.basecode.ontology.model.ObjectProperty {
3736

3837
private final com.hp.hpl.jena.ontology.ObjectProperty resource;
39-
@Nullable
4038
private final Set<Restriction> additionalRestrictions;
4139

42-
public ObjectPropertyImpl( ObjectProperty resource, @Nullable Set<Restriction> additionalRestrictions ) {
40+
public ObjectPropertyImpl( ObjectProperty resource, Set<Restriction> additionalRestrictions ) {
4341
super( resource );
4442
this.resource = resource;
4543
this.additionalRestrictions = additionalRestrictions;

src/ubic/basecode/ontology/jena/OntologyCardinalityRestrictionImpl.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@
2121
import com.hp.hpl.jena.ontology.Restriction;
2222
import ubic.basecode.ontology.model.OntologyCardinalityRestriction;
2323

24-
import javax.annotation.Nullable;
2524
import java.util.Set;
2625

2726
/**
@@ -33,7 +32,7 @@ class OntologyCardinalityRestrictionImpl extends OntologyRestrictionImpl impleme
3332
private final int cardinality;
3433
private final CardinalityType cardType;
3534

36-
public OntologyCardinalityRestrictionImpl( Restriction resource, @Nullable Set<Restriction> additionalRestrictions ) {
35+
public OntologyCardinalityRestrictionImpl( Restriction resource, Set<Restriction> additionalRestrictions ) {
3736
super( resource, additionalRestrictions );
3837

3938
if ( resource.isMaxCardinalityRestriction() ) {

src/ubic/basecode/ontology/jena/OntologyClassRestrictionImpl.java

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@
2323
import ubic.basecode.ontology.model.OntologyClassRestriction;
2424
import ubic.basecode.ontology.model.OntologyTerm;
2525

26-
import javax.annotation.Nullable;
2726
import java.util.Set;
2827

2928
/**
@@ -34,7 +33,7 @@ class OntologyClassRestrictionImpl extends OntologyRestrictionImpl implements On
3433
private final OntologyTerm restrictedTo;
3534
private final Set<Restriction> additionalRestrictions;
3635

37-
public OntologyClassRestrictionImpl( Restriction term, @Nullable Set<Restriction> additionalRestrictions ) {
36+
public OntologyClassRestrictionImpl( Restriction term, Set<Restriction> additionalRestrictions ) {
3837
super( term, additionalRestrictions );
3938
this.additionalRestrictions = additionalRestrictions;
4039

@@ -82,7 +81,7 @@ public String toString() {
8281
if ( restrictedTo != null ) {
8382
return "Class restriction: " + this.getRestrictionOn() + " class=" + this.getRestrictedTo();
8483
} else if ( value != null ) {
85-
return "Class restriction: " + this.getRestrictionOn() + " = " + this.value.toString() + "[value]";
84+
return "Class restriction: " + this.getRestrictionOn() + " = " + this.value + "[value]";
8685
} else {
8786
throw new IllegalStateException( "Value or restriction class must be non-null" );
8887
}

src/ubic/basecode/ontology/jena/OntologyDatatypeRestrictionImpl.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@
2121
import com.hp.hpl.jena.ontology.Restriction;
2222
import ubic.basecode.ontology.model.OntologyDatatypeRestriction;
2323

24-
import javax.annotation.Nullable;
2524
import java.util.Set;
2625

2726
/**
@@ -31,7 +30,7 @@ class OntologyDatatypeRestrictionImpl extends OntologyRestrictionImpl implements
3130

3231
private final Class<?> type;
3332

34-
public OntologyDatatypeRestrictionImpl( Restriction resource, @Nullable Set<Restriction> additionalRestrictions ) {
33+
public OntologyDatatypeRestrictionImpl( Restriction resource, Set<Restriction> additionalRestrictions ) {
3534
super( resource, additionalRestrictions );
3635
assert restrictionOn != null;
3736
this.type = PropertyFactory.convertType( resource.getOnProperty().asDatatypeProperty() );

src/ubic/basecode/ontology/jena/OntologyIndexer.java

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -58,10 +58,6 @@ public static SearchIndex getSubjectIndex( String name ) {
5858
return getSubjectIndex( name, analyzer );
5959
}
6060

61-
public static SearchIndex indexOntology( String name, OntModel model ) throws IOException {
62-
return indexOntology( name, model, false );
63-
}
64-
6561
/**
6662
* Loads or creates an index from an existing OntModel. Any existing index will loaded unless force=true. It will be
6763
* created if there isn't one already, or if force=true.

0 commit comments

Comments
 (0)