001/* 002 * BioJava development code 003 * 004 * This code may be freely distributed and modified under the 005 * terms of the GNU Lesser General Public Licence. This should 006 * be distributed with the code. If you do not have a copy, 007 * see: 008 * 009 * http://www.gnu.org/copyleft/lesser.html 010 * 011 * Copyright for this code is held jointly by the individual 012 * authors. These should be listed in @author doc comments. 013 * 014 * For more information on the BioJava project and its aims, 015 * or to join the biojava-l mailing list, visit the home page 016 * at: 017 * 018 * http://www.biojava.org/ 019 * 020 */ 021package org.biojava.nbio.structure.contact; 022 023import java.util.ArrayList; 024import java.util.List; 025 026import javax.vecmath.Point3d; 027 028 029/** 030 * A grid cell to be used in contact calculation via spatial hashing algorithm. 031 * 032 * @author Jose Duarte 033 * 034 */ 035public class GridCell { 036 037 038 private Grid grid; 039 private ArrayList<Integer> iIndices; 040 private ArrayList<Integer> jIndices; 041 042 public GridCell(Grid parent){ 043 iIndices = new ArrayList<>(); 044 jIndices = new ArrayList<>(); 045 this.grid = parent; 046 } 047 048 public void addIindex(int serial){ 049 iIndices.add(serial); 050 } 051 052 public void addJindex(int serial){ 053 jIndices.add(serial); 054 } 055 056 public int getNumIindices() { 057 return iIndices.size(); 058 } 059 060 public int getNumJindices() { 061 return jIndices.size(); 062 } 063 064 /** 065 * Calculates all distances of atoms within this cell returning those that are within the given cutoff 066 * as a list of Contacts containing the indices of the pair and the calculated distance. 067 * 068 * If {@link Grid#getJAtoms()} is null, distances are within the iAtoms only 069 * @return 070 */ 071 public List<Contact> getContactsWithinCell(){ 072 073 List<Contact> contacts = new ArrayList<>(); 074 075 Point3d[] iAtoms = grid.getIAtoms(); 076 Point3d[] jAtoms = grid.getJAtoms(); 077 double cutoff = grid.getCutoff(); 078 079 if (jAtoms==null) { 080 for (int i:iIndices) { 081 for (int j:iIndices) { 082 if (j>i) { 083 double distance = iAtoms[i].distance(iAtoms[j]); 084 if (distance<cutoff) contacts.add(new Contact(i, j, distance)); 085 } 086 } 087 } 088 089 } else { 090 for (int i:iIndices) { 091 for (int j:jIndices) { 092 double distance = iAtoms[i].distance(jAtoms[j]); 093 if (distance<cutoff) contacts.add(new Contact(i, j, distance)); 094 } 095 } 096 } 097 098 return contacts; 099 } 100 101 /** 102 * Calculates all distances of atoms between this cell and the given cell returning those that are 103 * within the given cutoff as a list of Contacts containing the indices of the pair and the calculated distance. 104 * 105 * @param otherCell 106 * @return 107 */ 108 public List<Contact> getContactsToOtherCell(GridCell otherCell){ 109 110 List<Contact> contacts = new ArrayList<>(); 111 112 Point3d[] iAtoms = grid.getIAtoms(); 113 Point3d[] jAtoms = grid.getJAtoms(); 114 double cutoff = grid.getCutoff(); 115 116 117 if (jAtoms==null) { 118 119 for (int i:iIndices) { 120 for (int j:otherCell.iIndices) { 121 if (j>i) { 122 double distance = iAtoms[i].distance(iAtoms[j]); 123 if (distance<cutoff) contacts.add(new Contact(i, j, distance)); 124 } 125 } 126 } 127 128 } else { 129 130 for (int i:iIndices) { 131 for (int j:otherCell.jIndices) { 132 double distance = iAtoms[i].distance(jAtoms[j]); 133 if (distance<cutoff) contacts.add(new Contact(i, j, distance)); 134 } 135 } 136 137 } 138 139 return contacts; 140 } 141 142 /** 143 * Tests whether any atom in this cell has a contact with the specified query atom 144 * @param iAtoms the first set of atoms to which the iIndices correspond 145 * @param jAtoms the second set of atoms to which the jIndices correspond, or null 146 * @param query test point 147 * @param cutoff 148 * @return 149 */ 150 public boolean hasContactToAtom(Point3d[] iAtoms, Point3d[] jAtoms, Point3d query, double cutoff) { 151 for( int i : iIndices ) { 152 double distance = iAtoms[i].distance(query); 153 if( distance<cutoff) 154 return true; 155 } 156 if (jAtoms!=null) { 157 for( int i : jIndices ) { 158 double distance = jAtoms[i].distance(query); 159 if( distance<cutoff) 160 return true; 161 } 162 } 163 return false; 164 } 165 166 /* (non-Javadoc) 167 * @see java.lang.Object#toString() 168 */ 169 @Override 170 public String toString() { 171 return String.format("GridCell [%d iAtoms,%d jAtoms]",iIndices.size(),jIndices==null?"-":jIndices.size()); 172 } 173 174 175}