Skip to content

Commit 2e41a42

Browse files
committed
Make additional properties configurable per-ontology
1 parent 645877d commit 2e41a42

3 files changed

Lines changed: 41 additions & 9 deletions

File tree

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

Lines changed: 31 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
import com.hp.hpl.jena.rdf.arp.ParseException;
2525
import com.hp.hpl.jena.rdf.model.Property;
2626
import com.hp.hpl.jena.rdf.model.Resource;
27+
import com.hp.hpl.jena.rdf.model.ResourceFactory;
2728
import com.hp.hpl.jena.util.iterator.ExtendedIterator;
2829
import org.apache.commons.lang3.RandomStringUtils;
2930
import org.apache.commons.lang3.StringUtils;
@@ -65,19 +66,20 @@ public abstract class AbstractOntologyService implements OntologyService {
6566
/**
6667
* Properties through which propagation is allowed for {@link #getParents(Collection, boolean, boolean)}}
6768
*/
68-
private static final Set<Property> additionalProperties;
69+
private static final Set<String> DEFAULT_ADDITIONAL_PROPERTIES;
6970

7071
static {
71-
additionalProperties = new HashSet<>();
72-
additionalProperties.add( BFO.partOf );
73-
additionalProperties.add( RO.properPartOf );
72+
DEFAULT_ADDITIONAL_PROPERTIES = new HashSet<>();
73+
DEFAULT_ADDITIONAL_PROPERTIES.add( BFO.partOf.getURI() );
74+
DEFAULT_ADDITIONAL_PROPERTIES.add( RO.properPartOf.getURI() );
7475
}
7576

7677
/* settings (applicable for next initialization) */
7778
private LanguageLevel nextLanguageLevel = LanguageLevel.FULL;
7879
private InferenceMode nextInferenceMode = InferenceMode.TRANSITIVE;
7980
private boolean nextProcessImports = true;
8081
private boolean nextSearchEnabled = true;
82+
private Set<String> nextAdditionalPropertyUris = DEFAULT_ADDITIONAL_PROPERTIES;
8183

8284
/**
8385
* Lock used to prevent reads while the ontology is being initialized.
@@ -99,6 +101,8 @@ public abstract class AbstractOntologyService implements OntologyService {
99101
private Boolean processImports = null;
100102
@Nullable
101103
private Boolean searchEnabled = null;
104+
@Nullable
105+
private Set<String> additionalPropertyUris = null;
102106

103107
@Override
104108
public LanguageLevel getLanguageLevel() {
@@ -164,6 +168,25 @@ public void setSearchEnabled( boolean searchEnabled ) {
164168
this.nextSearchEnabled = searchEnabled;
165169
}
166170

171+
/**
172+
* The set of properties relation to use when inferring parents and children.
173+
* <p>
174+
* The default is to use {@link BFO#partOf} and {@link RO#properPartOf}.
175+
*/
176+
public Set<String> getAdditionalPropertyUris() {
177+
Lock lock = rwLock.readLock();
178+
try {
179+
lock.lock();
180+
return additionalPropertyUris != null ? additionalPropertyUris : nextAdditionalPropertyUris;
181+
} finally {
182+
lock.unlock();
183+
}
184+
}
185+
186+
public void setAdditionalPropertyUris( Set<String> additionalPropertyUris ) {
187+
this.nextAdditionalPropertyUris = additionalPropertyUris;
188+
}
189+
167190
public void initialize( boolean forceLoad, boolean forceIndexing ) {
168191
initialize( null, forceLoad, forceIndexing );
169192
}
@@ -178,9 +201,12 @@ private void initialize( @Nullable InputStream stream, boolean forceLoad, boolea
178201
return;
179202
}
180203

204+
// making a copy of all we need
181205
String ontologyUrl = getOntologyUrl();
182206
String ontologyName = getOntologyName();
183207
String cacheName = getCacheName();
208+
Set<Property> additionalProperties = nextAdditionalPropertyUris.stream()
209+
.map( ResourceFactory::createProperty ).collect( Collectors.toSet() );
184210
LanguageLevel languageLevel = nextLanguageLevel;
185211
InferenceMode inferenceMode = nextInferenceMode;
186212
boolean processImports = nextProcessImports;
@@ -278,6 +304,7 @@ private void initialize( @Nullable InputStream stream, boolean forceLoad, boolea
278304
this.inferenceMode = inferenceMode;
279305
this.processImports = processImports;
280306
this.searchEnabled = searchEnabled;
307+
this.additionalPropertyUris = additionalProperties.stream().map( Property::getURI ).collect( Collectors.toSet() );
281308
if ( cacheName != null ) {
282309
// now that the terms have been replaced, we can clear old caches
283310
try {

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

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,26 +18,28 @@
1818
*/
1919
package ubic.basecode.ontology.jena;
2020

21-
import java.util.Collection;
22-
import java.util.HashSet;
23-
import java.util.Set;
24-
2521
import com.hp.hpl.jena.ontology.ObjectProperty;
2622
import com.hp.hpl.jena.ontology.OntClass;
2723
import com.hp.hpl.jena.ontology.OntResource;
2824
import com.hp.hpl.jena.ontology.Restriction;
2925
import com.hp.hpl.jena.util.iterator.ExtendedIterator;
3026
import ubic.basecode.ontology.model.OntologyTerm;
3127

28+
import javax.annotation.Nullable;
29+
import java.util.Collection;
30+
import java.util.HashSet;
31+
import java.util.Set;
32+
3233
/**
3334
* @author pavlidis
3435
*/
3536
class ObjectPropertyImpl extends OntologyPropertyImpl implements ubic.basecode.ontology.model.ObjectProperty {
3637

3738
private final com.hp.hpl.jena.ontology.ObjectProperty resource;
39+
@Nullable
3840
private final Set<Restriction> additionalRestrictions;
3941

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

test/ubic/basecode/ontology/OntologyTermTest.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,9 @@ public void testGetChildren() throws Exception {
4444
InputStream is = new GZIPInputStream( requireNonNull( this.getClass().getResourceAsStream( "/data/doid.short.owl.gz" ) ) );
4545
s.initialize( is, false );
4646

47+
assertTrue( s.getAdditionalPropertyUris().contains( "http://purl.obolibrary.org/obo/BFO_0000050" ) );
48+
assertTrue( s.getAdditionalPropertyUris().contains( "http://www.obofoundry.org/ro/ro.owl#proper_part_of" ) );
49+
4750
OntologyTerm t = s.getTerm( "http://purl.obolibrary.org/obo/DOID_4159" );
4851
assertNotNull( t );
4952

0 commit comments

Comments
 (0)