Skip to content

Commit ddeb759

Browse files
committed
Efficient neighbor index finding with SH
1 parent 1100299 commit ddeb759

File tree

1 file changed

+30
-22
lines changed

1 file changed

+30
-22
lines changed

biojava-structure/src/main/java/org/biojava/nbio/structure/asa/AsaCalculator.java

Lines changed: 30 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -375,34 +375,42 @@ int[][] findNeighborIndices() {
375375
*/
376376
int[][] findNeighborIndicesSpatialHashing() {
377377

378-
int[][] nbsIndices = new int[atomCoords.length][];
379-
380378
List<Contact> contactList = calcContacts();
379+
Map<Integer, List<Integer>> indices = new HashMap<>();
380+
for (Contact contact : contactList) {
381381

382-
for (int k=0; k<atomCoords.length; k++) {
383-
double radius = radii[k] + probe + probe;
382+
int i = contact.getI();
383+
int j = contact.getJ();
384384

385-
List<Integer> thisNbIndices = new ArrayList<>();
385+
List<Integer> iIndices;
386+
List<Integer> jIndices;
387+
if (indices.get(i)==null) {
388+
iIndices = new ArrayList<>();
389+
indices.put(i, iIndices);
390+
} else {
391+
iIndices = indices.get(i);
392+
}
393+
if (indices.get(j)==null) {
394+
jIndices = new ArrayList<>();
395+
indices.put(j, jIndices);
396+
} else {
397+
jIndices = indices.get(j);
398+
}
386399

387-
// TODO make this the outer loop
388-
for (Contact contact : contactList) {
389-
double dist = contact.getDistance();
390-
int i;
391-
if (contact.getJ() == k) {
392-
i = contact.getI();
393-
} else if (contact.getI() == k) {
394-
i = contact.getJ();
395-
} else {
396-
continue;
397-
}
398-
if (dist < radius + radii[i]) {
399-
thisNbIndices.add(i);
400-
}
400+
double radius = radii[i] + probe + probe;
401+
double dist = contact.getDistance();
402+
if (dist < radius + radii[j]) {
403+
iIndices.add(j);
404+
jIndices.add(i);
401405
}
406+
}
402407

403-
int[] indicesArray = new int[thisNbIndices.size()];
404-
for (int i=0;i<thisNbIndices.size();i++) indicesArray[i] = thisNbIndices.get(i);
405-
nbsIndices[k] = indicesArray;
408+
int[][] nbsIndices = new int[atomCoords.length][];
409+
for (Map.Entry<Integer, List<Integer>> entry : indices.entrySet()) {
410+
List<Integer> list = entry.getValue();
411+
int[] indicesArray = new int[list.size()];
412+
for (int i=0;i<entry.getValue().size();i++) indicesArray[i] = list.get(i);
413+
nbsIndices[entry.getKey()] = indicesArray;
406414
}
407415

408416
return nbsIndices;

0 commit comments

Comments
 (0)