From 9b13a29cc896529750596cd0062ddba8e724c92b Mon Sep 17 00:00:00 2001 From: Jose Manuel Duarte Date: Thu, 17 Jan 2019 10:16:58 -0800 Subject: [PATCH 1/4] Fixing no neighbor issue and a test --- .../nbio/structure/asa/AsaCalculator.java | 7 ++++ .../nbio/structure/asa/TestAsaCalc.java | 41 +++++++++++++++++++ 2 files changed, 48 insertions(+) diff --git a/biojava-structure/src/main/java/org/biojava/nbio/structure/asa/AsaCalculator.java b/biojava-structure/src/main/java/org/biojava/nbio/structure/asa/AsaCalculator.java index 8cb45ee7f5..7e6dbd60d1 100644 --- a/biojava-structure/src/main/java/org/biojava/nbio/structure/asa/AsaCalculator.java +++ b/biojava-structure/src/main/java/org/biojava/nbio/structure/asa/AsaCalculator.java @@ -442,6 +442,13 @@ int[][] findNeighborIndicesSpatialHashing() { nbsIndices[entry.getKey()] = indicesArray; } + // important: some atoms might have no neighbors at all: we need to initialise to empty arrays + for (int i=0; i listOfMatchingIndices = new ArrayList<>(); + for (int i = 0; i < nbsSh.length; i++) { + for (int j = 0; j < nbs.length; j++) { + if (nbs[j] == nbsSh[i]) { + listOfMatchingIndices.add(j); + break; + } + } + } + + assertEquals(nbs.length, nbsSh.length); + + assertEquals(nbs.length, listOfMatchingIndices.size()); + } + + } } From 2cbabae4b69fe39163223f59dd06f4a7152d8706 Mon Sep 17 00:00:00 2001 From: Jose Manuel Duarte Date: Thu, 17 Jan 2019 10:27:24 -0800 Subject: [PATCH 2/4] Bugfix: case of 0-length atoms array. Also implemented suggestion from @kanishka-azimi --- .../nbio/structure/asa/AsaCalculator.java | 17 ++++++----------- 1 file changed, 6 insertions(+), 11 deletions(-) diff --git a/biojava-structure/src/main/java/org/biojava/nbio/structure/asa/AsaCalculator.java b/biojava-structure/src/main/java/org/biojava/nbio/structure/asa/AsaCalculator.java index 7e6dbd60d1..19ece0ab72 100644 --- a/biojava-structure/src/main/java/org/biojava/nbio/structure/asa/AsaCalculator.java +++ b/biojava-structure/src/main/java/org/biojava/nbio/structure/asa/AsaCalculator.java @@ -457,7 +457,12 @@ Point3d[] getAtomCoords() { } private List calcContacts() { - double maxRadius = maxValue(radii); + if (atomCoords.length == 0) + return new ArrayList<>(); + double maxRadius = 0; + OptionalDouble optionalDouble = Arrays.stream(radii).max(); + if (optionalDouble.isPresent()) + maxRadius = optionalDouble.getAsDouble(); double cutoff = maxRadius + maxRadius + probe + probe; logger.debug("Max radius is {}, cutoff is {}", maxRadius, cutoff); Grid grid = new Grid(cutoff); @@ -465,16 +470,6 @@ private List calcContacts() { return grid.getIndicesContacts(); } - private static double maxValue(double[] array) { - double max = array[0]; - for (int i = 0; i < array.length; i++) { - if (array[i] > max) { - max = array[i]; - } - } - return max; - } - private double calcSingleAsa(int i) { Point3d atom_i = atomCoords[i]; From 3c97b85d9e6a8f56127ab178adc56edef16372a0 Mon Sep 17 00:00:00 2001 From: Jose Duarte Date: Fri, 18 Jan 2019 08:18:17 -0800 Subject: [PATCH 3/4] New unit test --- .../nbio/structure/asa/TestAsaCalc.java | 21 ++++++++++++++++++- 1 file changed, 20 insertions(+), 1 deletion(-) diff --git a/biojava-structure/src/test/java/org/biojava/nbio/structure/asa/TestAsaCalc.java b/biojava-structure/src/test/java/org/biojava/nbio/structure/asa/TestAsaCalc.java index 552fda884d..9da5ce12c8 100644 --- a/biojava-structure/src/test/java/org/biojava/nbio/structure/asa/TestAsaCalc.java +++ b/biojava-structure/src/test/java/org/biojava/nbio/structure/asa/TestAsaCalc.java @@ -20,7 +20,11 @@ */ package org.biojava.nbio.structure.asa; -import org.biojava.nbio.structure.*; +import org.biojava.nbio.structure.Atom; +import org.biojava.nbio.structure.Structure; +import org.biojava.nbio.structure.StructureException; +import org.biojava.nbio.structure.StructureIO; +import org.biojava.nbio.structure.StructureTools; import org.biojava.nbio.structure.io.mmcif.ChemCompGroupFactory; import org.biojava.nbio.structure.io.mmcif.DownloadChemCompProvider; import static org.junit.Assert.*; @@ -221,4 +225,19 @@ public void testNoNeighborsIssue() throws StructureException, IOException { } } + + @Test + public void testNoAtomsAsaCalc() { + + // in case of no atoms at all, the calculation should not fail and return an empty array + Atom[] atoms = new Atom[0]; + + AsaCalculator asaCalc = new AsaCalculator(atoms, + AsaCalculator.DEFAULT_PROBE_SIZE, + 1000, 1); + double[] asas = asaCalc.calculateAsas(); + assertNotNull(asas); + assertEquals(0, asas.length); + + } } From 834092843a4450aaa88bbcdfccd18f97568b12c2 Mon Sep 17 00:00:00 2001 From: Jose Duarte Date: Fri, 18 Jan 2019 08:27:30 -0800 Subject: [PATCH 4/4] Converting to unit test --- .../nbio/structure/asa/TestAsaCalc.java | 36 ++++++++++++++----- 1 file changed, 27 insertions(+), 9 deletions(-) diff --git a/biojava-structure/src/test/java/org/biojava/nbio/structure/asa/TestAsaCalc.java b/biojava-structure/src/test/java/org/biojava/nbio/structure/asa/TestAsaCalc.java index 9da5ce12c8..79598bd410 100644 --- a/biojava-structure/src/test/java/org/biojava/nbio/structure/asa/TestAsaCalc.java +++ b/biojava-structure/src/test/java/org/biojava/nbio/structure/asa/TestAsaCalc.java @@ -20,7 +20,10 @@ */ package org.biojava.nbio.structure.asa; +import org.biojava.nbio.structure.AminoAcidImpl; import org.biojava.nbio.structure.Atom; +import org.biojava.nbio.structure.AtomImpl; +import org.biojava.nbio.structure.Element; import org.biojava.nbio.structure.Structure; import org.biojava.nbio.structure.StructureException; import org.biojava.nbio.structure.StructureIO; @@ -186,15 +189,13 @@ public void testPerformance() throws StructureException, IOException { } @Test - public void testNoNeighborsIssue() throws StructureException, IOException { - // important: without this the tests can fail when running in maven (but not in IDE) - // that's because it depends on the order on how tests were run - JD 2018-03-10 - ChemCompGroupFactory.setChemCompProvider(new DownloadChemCompProvider()); - - Structure structure = StructureIO.getStructure("1EMI"); + public void testNoNeighborsIssue() { - // chain B, atom with index 35 does not have any neighbors. The calculation should not fail - Atom[] atoms = StructureTools.getAllNonHAtomArray(structure.getPolyChainByPDB("B"), false); + Atom[] atoms = { + getAtom(1.0, 1.0, 1.0), + getAtom(10.0, 1.0, 1.0), + getAtom(13.0, 1.0, 1.0) + }; AsaCalculator asaCalc = new AsaCalculator(atoms, AsaCalculator.DEFAULT_PROBE_SIZE, @@ -204,8 +205,10 @@ public void testNoNeighborsIssue() throws StructureException, IOException { int[][] allNbs = asaCalc.findNeighborIndices(); + assertEquals(3, allNbs.length); + assertEquals(3, allNbsSh.length); + for (int indexToTest =0; indexToTest < asaCalc.getAtomCoords().length; indexToTest++) { - //int indexToTest = 198; int[] nbsSh = allNbsSh[indexToTest]; int[] nbs = allNbs[indexToTest]; @@ -224,6 +227,21 @@ public void testNoNeighborsIssue() throws StructureException, IOException { assertEquals(nbs.length, listOfMatchingIndices.size()); } + // first atom should have no neighbors + assertEquals(0, allNbsSh[0].length); + } + + private Atom getAtom(double x, double y, double z) { + Atom atom = new AtomImpl(); + AminoAcidImpl g = new AminoAcidImpl(); + g.setAminoType('A'); + atom.setGroup(g); + atom.setName("CA"); + atom.setElement(Element.C); + atom.setX(x); + atom.setY(y); + atom.setZ(z); + return atom; } @Test