Skip to content

Commit f5b325f

Browse files
committed
Incorporating suggestions from @josemduarte
See comments on biojava#774
1 parent 62fb2a4 commit f5b325f

6 files changed

Lines changed: 23 additions & 16 deletions

File tree

biojava-structure/src/main/java/org/biojava/nbio/structure/cath/CathInstallation.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -636,7 +636,7 @@ private void parseCathDomall(BufferedReader bufferedReader) throws IOException{
636636

637637
protected void downloadFileFromRemote(URL remoteURL, File localFile) throws IOException{
638638
// System.out.println("downloading " + remoteURL + " to: " + localFile);
639-
LOGGER.info("Downloaded file {} to local file {}", remoteURL, localFile);
639+
LOGGER.info("Downloading file {} to local file {}", remoteURL, localFile);
640640

641641
long timeS = System.currentTimeMillis();
642642
File tempFile = File.createTempFile(FileDownloadUtils.getFilePrefix(localFile), "."+ FileDownloadUtils.getFileExtension(localFile));

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

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535
import java.io.IOException;
3636
import java.io.InputStream;
3737
import java.net.URL;
38+
import java.nio.file.Files;
3839
import java.text.ParseException;
3940
import java.text.SimpleDateFormat;
4041
import java.util.*;
@@ -405,8 +406,9 @@ public void prefetchStructure(String pdbId) throws IOException {
405406
* Attempts to delete all versions of a structure from the local directory.
406407
* @param pdbId
407408
* @return True if one or more files were deleted
409+
* @throws IOException if the file cannot be deleted
408410
*/
409-
public boolean deleteStructure(String pdbId){
411+
public boolean deleteStructure(String pdbId) throws IOException{
410412
boolean deleted = false;
411413
// Force getLocalFile to check in obsolete locations
412414
ObsoleteBehavior obsolete = getObsoleteBehavior();
@@ -663,8 +665,9 @@ protected File getDir(String pdbId, boolean obsolete) {
663665
* Searches for previously downloaded files
664666
* @param pdbId
665667
* @return A file pointing to the existing file, or null if not found
668+
* @throws IOException If the file exists but is empty and can't be deleted
666669
*/
667-
public File getLocalFile(String pdbId) {
670+
public File getLocalFile(String pdbId) throws IOException {
668671

669672
// Search for existing files
670673

@@ -692,11 +695,8 @@ public File getLocalFile(String pdbId) {
692695
if ( f.exists()) {
693696
// delete files that are too short to have contents
694697
if( f.length() < MIN_PDB_FILE_SIZE ) {
695-
boolean success = f.delete();
696-
if( ! success) {
697-
return null;
698-
}
699-
assert(!f.exists());
698+
Files.delete(f.toPath());
699+
return null;
700700
}
701701
return f;
702702
}
@@ -708,9 +708,11 @@ public File getLocalFile(String pdbId) {
708708
}
709709

710710
protected boolean checkFileExists(String pdbId){
711-
File path = getLocalFile(pdbId);
712-
if ( path != null)
713-
return true;
711+
try {
712+
File path = getLocalFile(pdbId);
713+
if ( path != null)
714+
return true;
715+
} catch(IOException e) {}
714716
return false;
715717
}
716718

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

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ public static ChemComp getChemComp(String recordName){
6969
* code executed afterwards.
7070
* <p>
7171
* Changing the provider also resets the cache, so any groups
72-
* previously accessed will be re-downloaded and reread.
72+
* previously accessed will be reread or re-downloaded.
7373
*
7474
* @param provider
7575
*/
@@ -85,7 +85,9 @@ public static ChemCompProvider getChemCompProvider(){
8585
}
8686

8787
/**
88-
* Force the cache to be reset.
88+
* Force the in-memory cache to be reset.
89+
*
90+
* Note that the ChemCompProvider may have additional memory or disk caches that need to be cleared too.
8991
*/
9092
public static void clearCache() {
9193
cache.clear();

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -306,6 +306,7 @@ public ChemComp getChemComp(String recordName) {
306306
fallback = new ReducedChemCompProvider();
307307
}
308308

309+
logger.warn("Falling back to ReducedChemCompProvider for {}. This could indicate a network error.", recordName);
309310
return fallback.getChemComp(recordName);
310311

311312
}
@@ -347,6 +348,8 @@ private static boolean fileExists(String recordName){
347348

348349
// delete files that are too short to have contents
349350
if( f.length() < LocalPDBDirectory.MIN_PDB_FILE_SIZE ) {
351+
// Delete defensively.
352+
// Note that if delete is unsuccessful, we re-download the file anyways
350353
f.delete();
351354
return false;
352355
}

biojava-structure/src/test/java/org/biojava/nbio/structure/TestAtomCache.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ public class TestAtomCache {
4646
private AtomCache cache;
4747

4848
@Before
49-
public void setUp() {
49+
public void setUp() throws IOException {
5050
cache = new AtomCache();
5151

5252
// Delete files which were cached in previous tests

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -404,7 +404,7 @@ public void testEmptyChemComp() throws IOException, StructureException {
404404
assertTrue(chem.getAtoms().size() > 0);
405405
assertEquals("NON-POLYMER", chem.getType());
406406
} finally {
407-
// FileDownloadUtils.deleteDirectory(tmpCache);
407+
FileDownloadUtils.deleteDirectory(tmpCache);
408408
}
409409
}
410410

@@ -467,7 +467,7 @@ public void testEmptyGZChemComp() throws IOException, StructureException {
467467
assertTrue(chem.getAtoms().size() > 0);
468468
assertEquals("NON-POLYMER", chem.getType());
469469
} finally {
470-
// FileDownloadUtils.deleteDirectory(tmpCache);
470+
FileDownloadUtils.deleteDirectory(tmpCache);
471471
}
472472
}
473473

0 commit comments

Comments
 (0)