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 * Created on May 27, 2006
021 *
022 */
023package org.biojava.nbio.structure.align.helper;
024
025import org.biojava.nbio.structure.Atom;
026import org.biojava.nbio.structure.AtomImpl;
027import org.biojava.nbio.structure.Calc;
028import org.biojava.nbio.structure.jama.Matrix;
029
030public class AlignTools {
031
032        /** get a subset of Atoms based by their positions
033         *
034         * @param caall
035         * @param idx an array where each element is a position of all the Atoms to return
036         * @return at Atom[] array
037         */
038        public static Atom[] getFragmentFromIdxList(Atom[] caall, int[] idx){
039                Atom[] subset = new Atom[idx.length];
040
041                for (int p = 0 ; p < idx.length;p++){
042
043                        int pos1 =  idx[p];
044                        subset[p] =  (Atom) caall[pos1].clone();
045                }
046                return subset;
047        }
048
049        /** get a continue subset of Atoms based by the starting position and the length
050         *
051         * @param caall
052         * @param pos ... the start position
053         * @param fragmentLength .. the length of the subset to extract.
054         * @return an Atom[] array
055         */
056        public static Atom[] getFragment(Atom[] caall, int pos, int fragmentLength){
057
058                if ( pos+fragmentLength > caall.length)
059                        return null;
060
061                Atom[] tmp = new Atom[fragmentLength];
062
063                for (int i=0;i< fragmentLength;i++){
064                        tmp[i] = (Atom)caall[i+pos].clone();
065                }
066                return tmp;
067
068        }
069
070
071        /** get a continue subset of Atoms based by the starting position and the length
072         * does not clone the original atoms.
073         *
074         * @param caall
075         * @param pos ... the start position
076         * @param fragmentLength .. the length of the subset to extract.
077         * @return an Atom[] array
078         */
079        public static Atom[] getFragmentNoClone(Atom[] caall, int pos, int fragmentLength){
080
081                if ( pos+fragmentLength > caall.length)
082                        return null;
083
084                Atom[] tmp = new Atom[fragmentLength];
085
086                for (int i=0;i< fragmentLength;i++){
087                        tmp[i] = caall[i+pos];
088                }
089                return tmp;
090
091        }
092
093        /** get the centroid for the set of atoms starting fromposition pos, length fragmentLenght
094         *
095         * @param ca
096         * @param pos
097         * @param fragmentLength
098         * @return an Atom
099         */
100        public static Atom getCenter(Atom[] ca, int pos, int fragmentLength){
101                Atom center = new AtomImpl();
102
103                if ( pos+fragmentLength > ca.length) {
104                        System.err.println("pos ("+pos+"), fragL ("+fragmentLength +") > ca.length"+ca.length);
105                        return center;
106                }
107
108                Atom[] tmp = getFragmentNoClone(ca,pos,fragmentLength);
109
110                return Calc.getCentroid(tmp);
111        }
112
113
114        /* Get distances along diagonal k from coordinate array coords.
115         *
116         * @param atoms set of atoms to be used
117         * @param k number of diagonal to be used
118         */
119        public static double[] getDiagonalAtK (Atom[] atoms,int k) {
120
121                int l = atoms.length;
122
123                double[] dk = new double[(l-k)];
124
125                for ( int i = 0 ; i< (l-k); i++){
126
127                        double dist = Calc.getDistance(atoms[i],atoms[i+k]);
128                        dk[i] = dist;
129
130                }
131
132                return dk;
133        }
134
135        /**
136         * Given distance matrix diagonals dk1, dk2, get the rmsd of a fpair.
137         * i,j is the fpair start in mol1 and mol2, l is the length of the fragment
138         *
139         * @param dk1 distances of structure 1
140         * @param dk2 distance of structure 2
141         * @param i position in structure 1
142         * @param j position in structure 2
143         * @param l length of the fragments
144         * @param k diagonal used
145         * @return a double
146         */
147        public static double rms_dk_diag(double[] dk1, double[] dk2, int i, int j, int l, int k) {
148                //        dk = 0
149                //        for x in range(l-k):
150                //            dk += (dk1[x+i]-dk2[x+j])*(dk1[x+i]-dk2[x+j])
151                //        dk /= (l-k)
152                //        return math.sqrt(dk)
153
154                double dk = 0.0;
155                for (int x = 0 ; x < (l-k) ; x++)
156                        dk += (dk1[x+i]-dk2[x+j])*(dk1[x+i]-dk2[x+j]);
157
158                dk /= ( l-k);
159                return Math.sqrt(dk);
160
161        }
162
163        /**
164         * Matrix of all distances between two sets of Atoms. Does not
165         * superimpose or modify the Atoms.
166         *
167         * @param ca1
168         * @param ca2
169         * @return a Matrix
170         */
171        public static Matrix getDistanceMatrix(Atom[] ca1, Atom[] ca2){
172
173                int r = ca1.length;
174                int c = ca2.length;
175
176                Matrix out = new Matrix(r,c);
177
178                for (int i=0; i<r; i++) {
179                        Atom a1 = ca1[i];
180                        for (int j=0;j<c;j++){
181                                Atom b1 = ca2[j];
182
183                                double d = Calc.getDistance(a1,b1);
184                                out.set(i,j,d);
185                        }
186                }
187                return out;
188        }
189
190}