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<Integer>(); 044 jIndices = new ArrayList<Integer>(); 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<Contact>(); 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 * @param iAtoms the first set of atom coordinates to which the iIndices correspond 107 * @param jAtoms the second set of atom coordinates to which the jIndices correspond, if null distances are within the iAtoms only 108 * @param cutoff 109 * @return 110 */ 111 public List<Contact> getContactsToOtherCell(GridCell otherCell){ 112 113 List<Contact> contacts = new ArrayList<Contact>(); 114 115 Point3d[] iAtoms = grid.getIAtoms(); 116 Point3d[] jAtoms = grid.getJAtoms(); 117 double cutoff = grid.getCutoff(); 118 119 120 if (jAtoms==null) { 121 122 for (int i:iIndices) { 123 for (int j:otherCell.iIndices) { 124 if (j>i) { 125 double distance = iAtoms[i].distance(iAtoms[j]); 126 if (distance<cutoff) contacts.add(new Contact(i, j, distance)); 127 } 128 } 129 } 130 131 } else { 132 133 for (int i:iIndices) { 134 for (int j:otherCell.jIndices) { 135 double distance = iAtoms[i].distance(jAtoms[j]); 136 if (distance<cutoff) contacts.add(new Contact(i, j, distance)); 137 } 138 } 139 140 } 141 142 return contacts; 143 } 144 145 /** 146 * Tests whether any atom in this cell has a contact with the specified query atom 147 * @param iAtoms the first set of atoms to which the iIndices correspond 148 * @param jAtoms the second set of atoms to which the jIndices correspond, or null 149 * @param query test point 150 * @param cutoff 151 * @return 152 */ 153 public boolean hasContactToAtom(Point3d[] iAtoms, Point3d[] jAtoms, Point3d query, double cutoff) { 154 for( int i : iIndices ) { 155 double distance = iAtoms[i].distance(query); 156 if( distance<cutoff) 157 return true; 158 } 159 if (jAtoms!=null) { 160 for( int i : jIndices ) { 161 double distance = jAtoms[i].distance(query); 162 if( distance<cutoff) 163 return true; 164 } 165 } 166 return false; 167 } 168 169 /* (non-Javadoc) 170 * @see java.lang.Object#toString() 171 */ 172 @Override 173 public String toString() { 174 return String.format("GridCell [%d iAtoms,%d jAtoms]",iIndices.size(),jIndices==null?"-":jIndices.size()); 175 } 176 177 178}