Skip to content

Commit b207d34

Browse files
committed
Make tests for #703 fail with an assertion
The main change hasn't been implemented, so we want tests to fail. However, the tests exposed some NPE and IO exceptions. These are now fixed, so the tests fail in the expected manner. - Use ATP ligand, which is not covered by the ReducedChemCompProvider - Use the ReducedChemCompProvider as a fallback consistently, preventing null chemComp - Defensive parsing in SimpleMMcifConsumer - Robust test, doesn't require internet!
1 parent c3cec91 commit b207d34

File tree

4 files changed

+64
-16
lines changed

4 files changed

+64
-16
lines changed

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

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,8 @@ public class DownloadChemCompProvider implements ChemCompProvider {
8585
protectedIDs.add("AUX");
8686
protectedIDs.add("NUL");
8787
}
88+
89+
private static ChemCompProvider fallback = null; // Fallback provider if the download fails
8890

8991
/** by default we will download only some of the files. User has to request that all files should be downloaded...
9092
*
@@ -272,7 +274,10 @@ public ChemComp getChemComp(String recordName) {
272274

273275
ChemComp chemComp = dict.getChemComp(recordName);
274276

275-
return chemComp;
277+
// May be null if the file was corrupt. Fall back on ReducedChemCompProvider in that case
278+
if(chemComp != null) {
279+
return chemComp;
280+
}
276281

277282
} catch (IOException e) {
278283

@@ -296,9 +301,11 @@ public ChemComp getChemComp(String recordName) {
296301

297302
// see https://github.com/biojava/biojava/issues/315
298303
// probably a network error happened. Try to use the ReducedChemCOmpProvider
299-
ReducedChemCompProvider reduced = new ReducedChemCompProvider();
304+
if( fallback == null) {
305+
fallback = new ReducedChemCompProvider();
306+
}
300307

301-
return reduced.getChemComp(recordName);
308+
return fallback.getChemComp(recordName);
302309

303310
}
304311

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

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -328,7 +328,13 @@ public void newAtomSite(AtomSite atom) {
328328

329329
String recordName = atom.getGroup_PDB();
330330
String residueNumberS = atom.getAuth_seq_id();
331-
Integer residueNrInt = Integer.parseInt(residueNumberS);
331+
Integer residueNrInt;
332+
if(residueNumberS != null) {
333+
residueNrInt = Integer.parseInt(residueNumberS);
334+
} else {
335+
String label_seq_id = atom.getLabel_seq_id();
336+
residueNrInt = Integer.parseInt(label_seq_id);
337+
}
332338

333339
// the 3-letter name of the group:
334340
String groupCode3 = atom.getLabel_comp_id();

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

Lines changed: 47 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -29,10 +29,10 @@
2929
import java.io.File;
3030
import java.io.FileOutputStream;
3131
import java.io.IOException;
32+
import java.net.URL;
3233
import java.nio.file.Files;
3334
import java.nio.file.Path;
3435
import java.nio.file.Paths;
35-
import java.nio.file.attribute.FileAttribute;
3636
import java.text.ParseException;
3737
import java.text.SimpleDateFormat;
3838
import java.util.Date;
@@ -53,8 +53,9 @@
5353
import org.biojava.nbio.structure.io.LocalPDBDirectory;
5454
import org.biojava.nbio.structure.io.LocalPDBDirectory.FetchBehavior;
5555
import org.biojava.nbio.structure.io.LocalPDBDirectory.ObsoleteBehavior;
56-
import org.biojava.nbio.structure.io.util.FileDownloadUtils;
5756
import org.biojava.nbio.structure.io.MMCIFFileReader;
57+
import org.biojava.nbio.structure.io.mmcif.model.ChemComp;
58+
import org.biojava.nbio.structure.io.util.FileDownloadUtils;
5859
import org.biojava.nbio.structure.scop.ScopDatabase;
5960
import org.biojava.nbio.structure.scop.ScopFactory;
6061
import org.junit.After;
@@ -364,17 +365,34 @@ public void testEmptyChemComp() throws IOException, StructureException {
364365
cache.setUseMmCif(true);
365366

366367
// Create an empty chemcomp
367-
Path sub = tmpCache.resolve(Paths.get("chemcomp", "ALA.cif.gz"));
368-
Files.createDirectories(sub.getParent());
369-
Files.createFile(sub);
370-
assertTrue(Files.exists(sub));
371-
assertEquals(0, Files.size(sub));
368+
Path chemCompCif = tmpCache.resolve(Paths.get("chemcomp", "ATP.cif.gz"));
369+
Files.createDirectories(chemCompCif.getParent());
370+
Files.createFile(chemCompCif);
371+
assertTrue(Files.exists(chemCompCif));
372+
assertEquals(0, Files.size(chemCompCif));
373+
374+
// Copy stub file into place
375+
Path testCif = tmpCache.resolve(Paths.get("data", "structures", "divided", "mmCIF", "ab","1abc.cif.gz"));
376+
Files.createDirectories(testCif.getParent());
377+
URL resource = AtomCacheTest.class.getResource("/atp.cif.gz");
378+
File src = new File(resource.getPath());
379+
FileDownloadUtils.copy(src, testCif.toFile());
372380

373-
Structure s = cache.getStructure("1A4W");
381+
// Load structure
382+
Structure s = cache.getStructure("1ABC");
374383

375384
assertNotNull(s);
385+
386+
Group g = s.getChainByPDB("A").getAtomGroup(0);
387+
assertTrue(g.getPDBName().equals("ATP"));
388+
389+
// should be unknown
390+
ChemComp chem = g.getChemComp();
391+
assertNotNull(chem);
392+
assertTrue(chem.getAtoms().size() > 0);
393+
assertEquals("NON-POLYMER", chem.getType());
376394
} finally {
377-
FileDownloadUtils.deleteDirectory(tmpCache);
395+
// FileDownloadUtils.deleteDirectory(tmpCache);
378396
}
379397
}
380398

@@ -387,6 +405,7 @@ public void testEmptyChemComp() throws IOException, StructureException {
387405
*/
388406
@Test
389407
public void testEmptyGZChemComp() throws IOException, StructureException {
408+
390409
Path tmpCache = Paths.get(System.getProperty("java.io.tmpdir"),"BIOJAVA_TEST_CACHE");
391410
logger.info("Testing AtomCache at {}", tmpCache.toString());
392411
System.setProperty(UserConfiguration.PDB_DIR, tmpCache.toString());
@@ -400,7 +419,7 @@ public void testEmptyGZChemComp() throws IOException, StructureException {
400419
cache.setUseMmCif(true);
401420

402421
// Create an empty chemcomp
403-
Path sub = tmpCache.resolve(Paths.get("chemcomp", "ALA.cif.gz"));
422+
Path sub = tmpCache.resolve(Paths.get("chemcomp", "ATP.cif.gz"));
404423
Files.createDirectories(sub.getParent());
405424
try(GZIPOutputStream out = new GZIPOutputStream(new FileOutputStream(sub.toFile()))) {
406425
// don't write anything
@@ -409,12 +428,28 @@ public void testEmptyGZChemComp() throws IOException, StructureException {
409428
assertTrue(Files.exists(sub));
410429
assertTrue(0 < Files.size(sub));
411430

412-
Structure s = cache.getStructure("1A4W");
431+
// Copy stub file into place
432+
Path testCif = tmpCache.resolve(Paths.get("data", "structures", "divided", "mmCIF", "ab","1abc.cif.gz"));
433+
Files.createDirectories(testCif.getParent());
434+
URL resource = AtomCacheTest.class.getResource("/atp.cif.gz");
435+
File src = new File(resource.getPath());
436+
FileDownloadUtils.copy(src, testCif.toFile());
437+
438+
// Load structure
439+
Structure s = cache.getStructure("1ABC");
413440

414441
assertNotNull(s);
415442

443+
Group g = s.getChainByPDB("A").getAtomGroup(0);
444+
assertTrue(g.getPDBName().equals("ATP"));
445+
446+
// should be unknown
447+
ChemComp chem = g.getChemComp();
448+
assertNotNull(chem);
449+
assertTrue(chem.getAtoms().size() > 0);
450+
assertEquals("NON-POLYMER", chem.getType());
416451
} finally {
417-
FileDownloadUtils.deleteDirectory(tmpCache);
452+
// FileDownloadUtils.deleteDirectory(tmpCache);
418453
}
419454
}
420455

842 Bytes
Binary file not shown.

0 commit comments

Comments
 (0)