Skip to content

Commit 62fb2a4

Browse files
committed
Set and restore the ChemCompGroupFactory singleton
This is necessary when changing the cache path.
1 parent 6ea9ae7 commit 62fb2a4

3 files changed

Lines changed: 31 additions & 7 deletions

File tree

biojava-structure/src/main/java/org/biojava/nbio/structure/io/LocalPDBDirectory.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -127,8 +127,8 @@ public static enum FetchBehavior {
127127

128128
protected static final String lineSplit = System.getProperty("file.separator");
129129

130-
/** Minimum size for a valid file, in bytes */
131-
public static final long MIN_PDB_FILE_SIZE = 40;
130+
/** Minimum size for a valid structure file (CIF or PDB), in bytes */
131+
public static final long MIN_PDB_FILE_SIZE = 40; // Empty gzip files are 20bytes. Add a few more for buffer.
132132

133133
private File path;
134134
private List<String> extensions;

biojava-structure/src/main/java/org/biojava/nbio/structure/io/mmcif/ChemCompGroupFactory.java

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -68,9 +68,8 @@ public static ChemComp getChemComp(String recordName){
6868
* again. Note that this change can have unexpected behavior of
6969
* code executed afterwards.
7070
* <p>
71-
* Changing the provider does not reset the cache, so Chemical
72-
* Component definitions already downloaded from previous providers
73-
* will be used. To reset the cache see {@link #getCache()).
71+
* Changing the provider also resets the cache, so any groups
72+
* previously accessed will be re-downloaded and reread.
7473
*
7574
* @param provider
7675
*/
@@ -84,6 +83,13 @@ public static void setChemCompProvider(ChemCompProvider provider) {
8483
public static ChemCompProvider getChemCompProvider(){
8584
return chemCompProvider;
8685
}
86+
87+
/**
88+
* Force the cache to be reset.
89+
*/
90+
public static void clearCache() {
91+
cache.clear();
92+
}
8793

8894
public static Group getGroupFromChemCompDictionary(String recordName) {
8995

biojava-structure/src/test/java/org/biojava/nbio/structure/align/util/AtomCacheTest.java

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,9 @@
5454
import org.biojava.nbio.structure.io.LocalPDBDirectory.FetchBehavior;
5555
import org.biojava.nbio.structure.io.LocalPDBDirectory.ObsoleteBehavior;
5656
import org.biojava.nbio.structure.io.MMCIFFileReader;
57+
import org.biojava.nbio.structure.io.mmcif.ChemCompGroupFactory;
58+
import org.biojava.nbio.structure.io.mmcif.ChemCompProvider;
59+
import org.biojava.nbio.structure.io.mmcif.DownloadChemCompProvider;
5760
import org.biojava.nbio.structure.io.mmcif.model.ChemComp;
5861
import org.biojava.nbio.structure.io.util.FileDownloadUtils;
5962
import org.biojava.nbio.structure.scop.ScopDatabase;
@@ -77,6 +80,7 @@ public class AtomCacheTest {
7780
private String previousPDB_DIR;
7881
private String previousPDB_CACHE_DIR;
7982
private AtomCache cleanCache = new AtomCache();
83+
private ChemCompProvider previousChemCompProvider = ChemCompGroupFactory.getChemCompProvider();
8084

8185
@Before
8286
public void setUp() {
@@ -85,8 +89,10 @@ public void setUp() {
8589
cache = new AtomCache();
8690
cache.setObsoleteBehavior(ObsoleteBehavior.FETCH_OBSOLETE);
8791
StructureIO.setAtomCache(cache);
92+
8893
// Use a fixed SCOP version for stability
8994
ScopFactory.setScopDatabase(ScopFactory.VERSION_1_75B);
95+
logger.warn("setUp()");
9096
}
9197

9298
@After
@@ -96,6 +102,7 @@ public void tearDown() {
96102
System.setProperty(UserConfiguration.PDB_CACHE_DIR, previousPDB_CACHE_DIR);
97103
}
98104
StructureIO.setAtomCache(cleanCache);
105+
ChemCompGroupFactory.setChemCompProvider(previousChemCompProvider);
99106
}
100107

101108
/**
@@ -363,7 +370,8 @@ public void testEmptyChemComp() throws IOException, StructureException {
363370
cache.setPath(tmpCache.toString());
364371
cache.setCachePath(tmpCache.toString());
365372
cache.setUseMmCif(true);
366-
373+
ChemCompGroupFactory.setChemCompProvider(new DownloadChemCompProvider(tmpCache.toString()));
374+
367375
// Create an empty chemcomp
368376
Path chemCompCif = tmpCache.resolve(Paths.get("chemcomp", "ATP.cif.gz"));
369377
Files.createDirectories(chemCompCif.getParent());
@@ -381,6 +389,10 @@ public void testEmptyChemComp() throws IOException, StructureException {
381389
// Load structure
382390
Structure s = cache.getStructure("1ABC");
383391

392+
// Should have re-downloaded the file
393+
assertTrue(Files.size(chemCompCif) > LocalPDBDirectory.MIN_PDB_FILE_SIZE);
394+
395+
// Structure should have valid ChemComp now
384396
assertNotNull(s);
385397

386398
Group g = s.getChainByPDB("A").getAtomGroup(0);
@@ -417,6 +429,8 @@ public void testEmptyGZChemComp() throws IOException, StructureException {
417429
cache.setPath(tmpCache.toString());
418430
cache.setCachePath(tmpCache.toString());
419431
cache.setUseMmCif(true);
432+
ChemCompGroupFactory.setChemCompProvider(new DownloadChemCompProvider(tmpCache.toString()));
433+
420434

421435
// Create an empty chemcomp
422436
Path sub = tmpCache.resolve(Paths.get("chemcomp", "ATP.cif.gz"));
@@ -426,7 +440,7 @@ public void testEmptyGZChemComp() throws IOException, StructureException {
426440
out.flush();
427441
}
428442
assertTrue(Files.exists(sub));
429-
assertTrue(0 < Files.size(sub));
443+
assertTrue(0 < Files.size(sub) && Files.size(sub) < LocalPDBDirectory.MIN_PDB_FILE_SIZE);
430444

431445
// Copy stub file into place
432446
Path testCif = tmpCache.resolve(Paths.get("data", "structures", "divided", "mmCIF", "ab","1abc.cif.gz"));
@@ -438,6 +452,10 @@ public void testEmptyGZChemComp() throws IOException, StructureException {
438452
// Load structure
439453
Structure s = cache.getStructure("1ABC");
440454

455+
// Should have re-downloaded the file
456+
assertTrue(Files.size(sub) > LocalPDBDirectory.MIN_PDB_FILE_SIZE);
457+
458+
// Structure should have valid ChemComp
441459
assertNotNull(s);
442460

443461
Group g = s.getChainByPDB("A").getAtomGroup(0);

0 commit comments

Comments
 (0)