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.xtal;
022
023import org.biojava.nbio.structure.Calc;
024import org.biojava.nbio.structure.Chain;
025import org.biojava.nbio.structure.Structure;
026import org.biojava.nbio.structure.StructureTools;
027import org.biojava.nbio.structure.contact.BoundingBox;
028
029import java.util.List;
030
031import javax.vecmath.Matrix4d;
032import javax.vecmath.Vector3d;
033
034/**
035 * A class to contain the BoundingBoxes of all polymeric molecules in a full unit cell.
036 *
037 * @author Jose Duarte
038 *
039 */
040public class UnitCellBoundingBox {
041
042        /**
043         * An array with dimensions numOperatorsSg x numPolyChainsAu to contain all
044         * bounding boxes of all chains of all AUs in unit cell
045         * e.g. chainBbs[0] would be the bounding boxes for all chains in the original AU
046         */
047        private BoundingBox[][] chainBbs;
048
049        /**
050         * An array with dimensions numOperatorsSg to contain all bounding boxes of
051         * all AUs in unit cell
052         */
053        private BoundingBox[] auBbs;
054
055        private int numOperatorsSg; // i.e. multiplicity of space group
056        private int numPolyChainsAu;
057
058        public UnitCellBoundingBox(int numOperatorsSg, int numPolyChainsAu) {
059                this.numOperatorsSg = numOperatorsSg;
060                this.numPolyChainsAu = numPolyChainsAu;
061                this.chainBbs = new BoundingBox[numOperatorsSg][numPolyChainsAu];
062                this.auBbs = new BoundingBox[numOperatorsSg];
063        }
064
065        public void setBbs(Structure structure, Matrix4d[] ops, boolean includeHetAtoms) {
066
067                setBb(structure, includeHetAtoms, 0);
068                for (int i=1;i<ops.length;i++) {
069                        Structure sym = structure.clone();
070                        Calc.transform(sym, ops[i]);
071                        setBb(sym, includeHetAtoms, i);
072                }
073
074        }
075
076        private void setBb(Structure s, boolean includeHetAtoms, int i) {
077                chainBbs[i] = new BoundingBox[numPolyChainsAu];
078                List<Chain> polyChains = s.getPolyChains();
079                int j = 0;
080                for (Chain polyChain : polyChains) {
081                        chainBbs[i][j] = new BoundingBox(StructureTools.getAllNonHCoordsArray(polyChain, includeHetAtoms));
082                        j++;
083                }
084                auBbs[i] = new BoundingBox(chainBbs[i]);
085        }
086
087        /**
088         * Get the chain BoundingBox for the given cell index (cellIdx=0 would be original AU)
089         * and chain index
090         * @param cellIdx
091         * @param chainIdx
092         * @return
093         */
094        public BoundingBox getChainBoundingBox(int cellIdx, int chainIdx) {
095                return chainBbs[cellIdx][chainIdx];
096        }
097
098        /**
099         * Get the AU BoundingBox for the given cell index (cellIdx=0 would be original AU)
100         * The AU BoundingBox is the BoundingBox that bounds all chains belonging to the AU
101         * @param cellIdx
102         * @return
103         */
104        public BoundingBox getAuBoundingBox(int cellIdx) {
105                return auBbs[cellIdx];
106        }
107
108        /**
109         * Returns a new BoundingBoxes object containing the same bounds as this
110         * BoundingBoxes object translated by the given translation
111         * @param translation
112         * @return
113         */
114        public UnitCellBoundingBox getTranslatedBbs(Vector3d translation) {
115                UnitCellBoundingBox translatedBbs = new UnitCellBoundingBox(numOperatorsSg, numPolyChainsAu);
116
117                for (int i=0; i<numOperatorsSg; i++) {
118                        for (int j = 0;j<numPolyChainsAu; j++) {
119                                translatedBbs.chainBbs[i][j] = new BoundingBox(this.chainBbs[i][j]);
120                                translatedBbs.chainBbs[i][j].translate(translation);
121                        }
122                        translatedBbs.auBbs[i] = new BoundingBox(translatedBbs.chainBbs[i]);
123                }
124
125                return translatedBbs;
126        }
127
128}