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 org.biojava.nbio.structure.Atom;
024import org.biojava.nbio.structure.Calc;
025
026import java.util.ArrayList;
027import java.util.List;
028
029
030/**
031 * A grid cell to be used in contact calculation via geometric hashing algorithm.
032 *
033 * @author duarte_j
034 *
035 */
036public class GridCell {
037
038
039        private ArrayList<Integer> iIndices;
040        private ArrayList<Integer> jIndices;
041
042        public GridCell(){
043                iIndices = new ArrayList<Integer>();
044                jIndices = new ArrayList<Integer>();
045        }
046
047        public void addIindex(int serial){
048                iIndices.add(serial);
049        }
050
051        public void addJindex(int serial){
052                jIndices.add(serial);
053        }
054
055        public int getNumIindices() {
056                return iIndices.size();
057        }
058
059        public int getNumJindices() {
060                return jIndices.size();
061        }
062
063        /**
064         * Calculates all distances of atoms within this cell returning those that are within the given cutoff
065         * as a list of AtomContacts
066         * @param iAtoms the first set of atoms to which the iIndices correspond
067         * @param jAtoms the second set of atoms to which the jIndices correspond, if null distances are within the iAtoms only
068         * @param cutoff
069         * @return
070         */
071        public List<AtomContact> getContactsWithinCell(Atom[] iAtoms, Atom[] jAtoms, double cutoff){
072
073                List<AtomContact> contacts = new ArrayList<AtomContact>();
074
075                if (jAtoms==null) {
076                        for (int i:iIndices) {
077                                for (int j:iIndices) {
078                                        if (j>i) {
079                                                double distance = Calc.getDistance(iAtoms[i], iAtoms[j]);
080                                                if (distance<cutoff) contacts.add(new AtomContact(new Pair<Atom>(iAtoms[i],iAtoms[j]),distance));
081                                        }
082                                }
083                        }
084
085                } else {
086                        for (int i:iIndices) {
087                                for (int j:jIndices) {
088                                        double distance = Calc.getDistance(iAtoms[i], jAtoms[j]);
089                                        if (distance<cutoff) contacts.add(new AtomContact(new Pair<Atom>(iAtoms[i],jAtoms[j]),distance));
090                                }
091                        }
092                }
093
094                return contacts;
095        }
096
097        /**
098         * Calculates all distances of atoms between this cell and the given cell returning those that are
099         * within the given cutoff as a list of AtomContacts
100         * @param otherCell
101         * @param iAtoms the first set of atoms to which the iIndices correspond
102         * @param jAtoms the second set of atoms to which the jIndices correspond, if null distances are within the iAtoms only
103         * @param cutoff
104         * @return
105         */
106        public List<AtomContact> getContactsToOtherCell(GridCell otherCell , Atom[] iAtoms, Atom[] jAtoms, double cutoff){
107
108                List<AtomContact> contacts = new ArrayList<AtomContact>();
109
110                if (jAtoms==null) {
111
112                        for (int i:iIndices) {
113                                for (int j:otherCell.iIndices) {
114                                        if (j>i) {
115                                                double distance = Calc.getDistance(iAtoms[i], iAtoms[j]);
116                                                if (distance<cutoff) contacts.add(new AtomContact(new Pair<Atom>(iAtoms[i],iAtoms[j]),distance));
117                                        }
118                                }
119                        }
120
121                } else {
122
123                        for (int i:iIndices) {
124                                for (int j:otherCell.jIndices) {
125                                        double distance = Calc.getDistance(iAtoms[i], jAtoms[j]);
126                                        if (distance<cutoff) contacts.add(new AtomContact(new Pair<Atom>(iAtoms[i],jAtoms[j]),distance));
127                                }
128                        }
129
130                }
131
132                return contacts;
133        }
134
135}