1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38 package ffx.potential.bonded;
39
40 import static java.lang.String.format;
41
42 import java.util.Arrays;
43 import java.util.HashMap;
44 import java.util.List;
45 import java.util.logging.Level;
46 import java.util.logging.Logger;
47
48
49
50
51
52
53
54
55
56
57 public class ResidueState {
58
59 private static final Logger logger = Logger.getLogger(ResidueState.class.getName());
60
61
62
63
64
65
66 private final Residue parent;
67
68 private final Residue residue;
69 private final HashMap<Atom, double[]> atomMap;
70 private final Atom[] atoms;
71
72
73
74
75
76
77
78 public ResidueState(Residue parent, Residue residue) {
79 this.parent = parent;
80 this.residue = residue;
81
82 List<Atom> atomList = residue.getAtomList();
83 int nAtoms = atomList.size();
84 atomMap = new HashMap<>(nAtoms);
85 atoms = new Atom[nAtoms];
86
87 atomList.toArray(atoms);
88 for (Atom atom : atoms) {
89 double[] atXYZ = new double[3];
90 atom.getXYZ(atXYZ);
91 atomMap.put(atom, atXYZ);
92 }
93 }
94
95
96
97
98
99
100 public ResidueState(Residue residue) {
101 this(residue, residue);
102 if (residue instanceof MultiResidue) {
103 throw new IllegalArgumentException(format(
104 " Residue %s is a MultiResidue: this ResidueState has been incorrectly constructed!",
105 residue));
106 }
107 }
108
109
110
111
112
113
114
115 public static void revertAllCoordinates(List<Residue> residueList, ResidueState[] states) {
116 revertAllCoordinates(residueList.toArray(new Residue[0]), states);
117 }
118
119
120
121
122
123
124
125
126 public static void revertAtomicCoordinates(Atom[] atoms, double[][] coords) {
127 int nAtoms = atoms.length;
128 if (coords.length != nAtoms) {
129 throw new IllegalArgumentException(
130 format(" Length %d of atoms " + "array does not match length %d of coordinates array",
131 nAtoms, coords.length));
132 }
133 for (int i = 0; i < nAtoms; i++) {
134 atoms[i].setXYZ(coords[i]);
135 }
136 }
137
138
139
140
141
142
143
144 public static ResidueState[] storeAllCoordinates(List<Residue> residueList) {
145 return storeAllCoordinates(residueList.toArray(new Residue[0]));
146 }
147
148
149
150
151
152
153
154 public static ResidueState[] storeAllCoordinates(Residue[] residues) {
155 int nResidues = residues.length;
156 ResidueState[] states = new ResidueState[nResidues];
157 for (int i = 0; i < nResidues; i++) {
158 states[i] = residues[i].storeState();
159 }
160 return states;
161 }
162
163
164
165
166
167
168
169 public static double[][] storeAtomicCoordinates(Atom[] atoms) {
170 int nAtoms = atoms.length;
171 double[][] coords = new double[nAtoms][3];
172 for (int i = 0; i < nAtoms; i++) {
173 atoms[i].getXYZ(coords[i]);
174 }
175 return coords;
176 }
177
178
179
180
181
182
183
184 private static void revertAllCoordinates(Residue[] residueArray, ResidueState[] states) {
185 int nResidues = residueArray.length;
186 if (nResidues != states.length) {
187 throw new IllegalArgumentException(
188 format("Length of residue " + "array %d and residue state array %d do not match.",
189 nResidues, states.length));
190 }
191
192 for (int i = 0; i < nResidues; i++) {
193 Residue resi = residueArray[i];
194
195 if (resi.equals(states[i].getParent())) {
196 resi.revertState(states[i]);
197 } else {
198
199 boolean matchFound = false;
200 for (int j = 0; j < nResidues; j++) {
201 if (resi.equals(states[j].getParent())) {
202 matchFound = true;
203 resi.revertState(states[j]);
204 break;
205 }
206 }
207
208 if (!matchFound) {
209 throw new IllegalArgumentException(
210 format("Could not " + "find match for residue %s among residue states array.", resi));
211 }
212 }
213 }
214 }
215
216 public double compareTo(ResidueState residueState) {
217 logger.info("Comparing rotamers using the compareTo method in ResidueState");
218 double[][] tempx1 = new double[this.atoms.length][3];
219 double[][] tempx2 = new double[residueState.atoms.length][3];
220 double[] x1 = new double[this.atoms.length * 3];
221 double[] x2 = new double[residueState.atoms.length * 3];
222
223
224 Atom[] x1atoms = this.atoms;
225 Atom[] x2atoms = residueState.atoms;
226
227
228
229
230
231 for (int atomCount = 0; atomCount < x1atoms.length; atomCount++) {
232 Atom exampleAtom = x1atoms[atomCount];
233 tempx1[atomCount] = this.atomMap.get(exampleAtom);
234 }
235
236
237 int x1count = 0;
238 for (double[] coordSet : tempx1) {
239 for (int coordCount = 0; coordCount < 3; coordCount++) {
240 x1[x1count] = coordSet[coordCount];
241 x1count++;
242 }
243 }
244
245
246
247 for (int atomCount = 0; atomCount < x2atoms.length; atomCount++) {
248 Atom exampleAtom = x2atoms[atomCount];
249 tempx2[atomCount] = residueState.atomMap.get(exampleAtom);
250 }
251
252
253 int x2count = 0;
254 for (double[] coordSet : tempx2) {
255 for (int coordCount = 0; coordCount < 3; coordCount++) {
256 x2[x2count] = coordSet[coordCount];
257 x2count++;
258 }
259 }
260
261
262 double[] mass = new double[x1.length];
263 Arrays.fill(mass, 1);
264 return ffx.potential.utils.Superpose.rmsd(x1, x2, mass);
265 }
266
267
268
269
270
271
272 public Atom[] getAtoms() {
273 return atoms;
274 }
275
276
277
278
279
280
281 public Residue getParent() {
282 return parent;
283 }
284
285
286 @Override
287 public String toString() {
288 return format(" ResidueState with parent residue %s, state residue " + "%s, number of atoms %d",
289 parent, residue, atoms.length);
290 }
291
292
293
294
295
296
297 Residue getStateResidue() {
298 return residue;
299 }
300
301
302
303
304
305
306
307 double[] getAtomCoords(Atom atom) {
308 double[] xyz = new double[3];
309 if (!atomMap.containsKey(atom)) {
310 logger.info(
311 format(" Illegal call to ResidueState.getAtomCoords: atom %s not found: hashcode %d", atom,
312 atom.hashCode()));
313 for (Atom ratom : residue.getAtomList()) {
314 logger.info(format(" Atoms in residue: %s hashcode: %d", ratom, ratom.hashCode()));
315 }
316 for (Atom matom : atomMap.keySet()) {
317 logger.info(
318 format(" Atoms in ResidueState atom cache: %s hashcode: %d", matom, matom.hashCode()));
319 }
320 logger.log(Level.SEVERE, " Error in ResidueState.getAtomCoords.", new IllegalStateException());
321 }
322 System.arraycopy(atomMap.get(atom), 0, xyz, 0, 3);
323 return xyz;
324 }
325 }