Skip to content

Commit 32e5241

Browse files
committed
Add more tests for ontologies and fix a few minor issues
Raise an exception of forceIndex is used on an ontology that does not have a cache name. Exclude tests involving ontology providers as those are slow and depend on external data. Use the temporary directory by default if ontology.cache.dir is not set.
1 parent 4affc9c commit 32e5241

17 files changed

Lines changed: 417 additions & 321 deletions

pom.xml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -393,6 +393,8 @@
393393
<include>**/*Test.java</include>
394394
</includes>
395395
<excludes>
396+
<!-- these tests should be run manually since they require external data -->
397+
<exclude>**/providers/*Test.java</exclude>
396398
<exclude>**/AllTests.java</exclude>
397399
<exclude>**/gui/Test*.java</exclude>
398400
<exclude>**/AbstractTest*.java</exclude>

src/ontology.properties

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
1+
# set to true to enable specific ontologies
22
load.diseaseOntology=false
33
load.fmaOntology=false
44
load.chebiOntology=false
@@ -32,10 +32,9 @@ url.uberonOntology=http://purl.obolibrary.org/obo/uberon.owl
3232
url.efOntology=https://www.ebi.ac.uk/efo/efo.owl
3333
url.obiOntology=http://purl.obolibrary.org/obo/obi.owl
3434

35-
3635
# no longer actively used.
3736
url.fmaOntology=http://purl.obolibrary.org/obo/fma.owl
3837

39-
ontology.index.dir=ontology.index.dir
38+
ontology.index.dir=
4039
ontology.cache.dir=
4140
ncbo.api.key=

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

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@
1616

1717
import com.hp.hpl.jena.ontology.OntModel;
1818

19-
import javax.annotation.Nullable;
2019
import java.io.InputStream;
2120

2221
/**
@@ -33,7 +32,7 @@ protected boolean getProcessImport() {
3332

3433
@Override
3534
protected OntModel loadModel() {
36-
return OntologyLoader.loadMemoryModel( this.getOntologyUrl(), this.getOntologyName(), this.getProcessImport() );
35+
return OntologyLoader.loadMemoryModel( this.getOntologyUrl(), this.getCacheName(), this.getProcessImport() );
3736
}
3837

3938
@Override

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

Lines changed: 22 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@
4444
import ubic.basecode.util.Configuration;
4545

4646
import javax.annotation.Nullable;
47+
import java.io.IOException;
4748
import java.io.InputStream;
4849
import java.util.*;
4950
import java.util.concurrent.locks.Lock;
@@ -108,6 +109,15 @@ private void initialize( @Nullable InputStream stream, boolean forceLoad, boolea
108109
String ontologyName = getOntologyName();
109110
String cacheName = getCacheName();
110111

112+
// Detect configuration problems.
113+
if ( StringUtils.isBlank( ontologyUrl ) ) {
114+
throw new IllegalStateException( "URL not defined for %s: ontology cannot be loaded. (" + this + ")" );
115+
}
116+
117+
if ( cacheName == null && forceIndexing ) {
118+
throw new IllegalArgumentException( String.format( "No cache directory is set for %s, cannot force indexing.", this ) );
119+
}
120+
111121
boolean loadOntology = isEnabled();
112122

113123
// If loading ontologies is disabled in the configuration, return
@@ -117,16 +127,6 @@ private void initialize( @Nullable InputStream stream, boolean forceLoad, boolea
117127
return;
118128
}
119129

120-
// Detect configuration problems.
121-
if ( StringUtils.isBlank( ontologyUrl ) ) {
122-
throw new IllegalStateException( "URL not defined for %s: ontology cannot be loaded. (" + this + ")" );
123-
}
124-
125-
// This thread indexes ontology and creates local cache for uri->ontology terms mappings.
126-
if ( !forceIndexing ) {
127-
log.info( "{} index will *not* be refreshed unless the ontology has changed or the index is missing", this );
128-
}
129-
130130
log.info( "Loading ontology: {}...", this );
131131
StopWatch loadTime = StopWatch.createStarted();
132132

@@ -150,22 +150,18 @@ private void initialize( @Nullable InputStream stream, boolean forceLoad, boolea
150150
.filterKeep( new RestrictionWithOnPropertyFilter( additionalProperties ) )
151151
.toSet();
152152

153-
//Checks if the current ontology has changed since it was last loaded.
154-
boolean changed = cacheName == null || OntologyLoader.hasChanged( cacheName );
155-
boolean indexExists = cacheName != null && OntologyIndexer.getSubjectIndex( cacheName ) != null;
156-
boolean forceReindexing = forceLoad && forceIndexing;
157-
158-
/*
159-
* Indexing is slow, don't do it if we don't have to.
160-
*/
161-
boolean force = forceReindexing || changed || !indexExists;
162-
163153
// indexing is lengthy, don't bother if we're interrupted
164154
if ( checkIfInterrupted() )
165155
return;
166156

167157
if ( cacheName != null ) {
168-
index = OntologyIndexer.indexOntology( cacheName, model, force );
158+
//Checks if the current ontology has changed since it was last loaded.
159+
boolean changed = OntologyLoader.hasChanged( cacheName );
160+
boolean indexExists = OntologyIndexer.getSubjectIndex( cacheName ) != null;
161+
boolean forceReindexing = forceLoad && forceIndexing;
162+
// indexing is slow, don't do it if we don't have to.
163+
index = OntologyIndexer.indexOntology( cacheName, model,
164+
forceReindexing || changed || !indexExists );
169165
} else {
170166
index = null;
171167
}
@@ -183,7 +179,11 @@ private void initialize( @Nullable InputStream stream, boolean forceLoad, boolea
183179
this.isInitialized = true;
184180
if ( cacheName != null ) {
185181
// now that the terms have been replaced, we can clear old caches
186-
OntologyLoader.deleteOldCache( cacheName );
182+
try {
183+
OntologyLoader.deleteOldCache( cacheName );
184+
} catch ( IOException e ) {
185+
log.error( String.format( String.format( "Failed to delete old cache directory for %s.", this ), e ) );
186+
}
187187
}
188188
} finally {
189189
lock.unlock();

0 commit comments

Comments
 (0)