-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFragmentGenerationOPLScalculator.java
330 lines (266 loc) · 15.6 KB
/
FragmentGenerationOPLScalculator.java
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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
import java.io.*;
import java.util.*;
import com.google.common.collect.*;
import org.apache.commons.math3.geometry.euclidean.threed.*;
import org.jgrapht.*;
import org.jgrapht.graph.*;
import org.jgrapht.alg.*;
/**
* This class enables quick calculation of a conformer's OPLS energy given a mutation and the previous state of the peptide.
* This class takes advantage of the insight that mutations in dihedral angles do not affect angles and bond distances.
* A general formula that encapsulates this approach is E_conformer = E_previous + delta(E).
* This class is designed for use in fixed sequence Monte Carlo simulations with mutations involving mutations at a reisdue's backbone angle and rotamer packing.
* It is meant for use in the Fragment Generation Monte Carlo
* The OPLS force field is developed in <a href="http://pubs.acs.org/doi/abs/10.1021/ja9621760">Jorgensen et al., 1996</a>. */
public class FragmentGenerationOPLScalculator extends FixedSequenceOPLScalculator
{
/** The total of all the torsional energy terms for the current conformation */
public double currentSidechainTorsionalEnergy;
/** The total of all the torsional energy terms for the previous conformation */
public double previousSidechainTorsionalEnergy;
/** This constructor finds the energy of the sidechain torsions (all chis) in a peptide.
* This will allow for rapid computation of the energy difference between confromers in the Monte Carlo */
public FragmentGenerationOPLScalculator(Peptide startingPeptide)
{
super(startingPeptide);
// Calculate total sidechain torsional energy
double tempTorsionalEnergy = 0.0;
for (Residue r : startingPeptide.sequence)
{
for (ProtoTorsion chi : r.chis)
{
Set<List<Integer>> sidechainTorsionIndices = getDihedralChanges(chi, startingPeptide);
for (List<Integer> torsionIndices : sidechainTorsionIndices)
tempTorsionalEnergy += getDihedralEnergy(torsionIndices, startingPeptide);
}
// If residue is proline, add chi 4 and its dependencies
if (r.aminoAcid.isProline())
{
// Atom 3 in chi 3 is the delta carbon
Atom Cdelta = r.chis.get(2).atom3;
Atom N = r.N;
// First find all dependencies
// Get adjacent atoms not including a2 or a3 to make combinations of a-a2-a3-a
Set<Atom> connectedToAtom2 = startingPeptide.getAdjacentAtoms(Cdelta);
connectedToAtom2.remove(N);
Set<Atom> connectedToAtom3 = startingPeptide.getAdjacentAtoms(N);
connectedToAtom3.remove(Cdelta);
for (Atom a1 : connectedToAtom2)
{
for (Atom a4 : connectedToAtom3)
{
List<Integer> torsionIndices = new LinkedList<>();
torsionIndices.add(startingPeptide.contents.indexOf(a1));
torsionIndices.add(startingPeptide.contents.indexOf(Cdelta));
torsionIndices.add(startingPeptide.contents.indexOf(N));
torsionIndices.add(startingPeptide.contents.indexOf(a4));
tempTorsionalEnergy += getDihedralEnergy(torsionIndices, startingPeptide);
}
}
}
}
this.currentSidechainTorsionalEnergy = tempTorsionalEnergy;
this.previousSidechainTorsionalEnergy = 0.0;
}
/** A method that returns to the state before the last mutation.
* This is useful because we can calculate an energy for a potential Monte Carlo move, reject the change, and then revert to the state before the change.
*/
public void undoMutation()
{
super.undoMutation();
currentSidechainTorsionalEnergy = previousSidechainTorsionalEnergy;
}
/** Calculates the energy of a new conformation following a residue's backbone angles being mutated and rotamer packing.
* This method adds the change in energy of dihedrals and nonbonded interactions.
* @param newConformation the mutated conformation whose energy will be calculated
* @param mutatedResidue the residue whose backbone angles are being changed
* @return the energy of the new conformation in the OPLS force field
*/
public double calculateEnergy(Residue mutatedResidue, Peptide newConformation)
{
double energyChange = 0.0;
// Find energy change in dihedrals of mutatedResidue
System.out.println("Calculating energy change...");
Residue previousResidue = currentConformation.sequence.get(newConformation.sequence.indexOf(mutatedResidue));
// Angle mutations occur at phi, psi, omega
ProtoTorsion oldPhi = previousResidue.phi;
ProtoTorsion oldPsi = previousResidue.psi;
ProtoTorsion oldOmega = previousResidue.omega;
// Find all dihedrals that are changing as a result of the backbone mutations
Set<List<Integer>> backboneTorsionIndices = new HashSet<>();
backboneTorsionIndices = getDihedralChanges(oldPhi, currentConformation);
backboneTorsionIndices.addAll(getDihedralChanges(oldOmega, currentConformation));
backboneTorsionIndices.addAll(getDihedralChanges(oldPsi, currentConformation));
List<List<Integer>> test = new LinkedList<>();
test.addAll(getDihedralChanges(oldPhi, currentConformation));
test.addAll(getDihedralChanges(oldOmega, currentConformation));
test.addAll(getDihedralChanges(oldPsi, currentConformation));
// Call dihedral energy for each torsion that is changing
for (List<Integer> torsionIndices : backboneTorsionIndices)
energyChange += (getDihedralEnergy(torsionIndices,newConformation) - getDihedralEnergy(torsionIndices,currentConformation));
System.out.println("Backbone torsional energy change is: " + energyChange);
int numberTorsionChanges = backboneTorsionIndices.size();
System.out.println("Backbone torsion number: " + numberTorsionChanges);
// Find energy change in all chis that are changed as a result of rotamer packing
double newSidechainTorsionalEnergy = 0.0;
Set<List<Integer>> allTorsionChanges = backboneTorsionIndices;
for (Residue r : newConformation.sequence)
{
for (ProtoTorsion chi : r.chis)
{
Set<List<Integer>> sidechainTorsionIndices = getDihedralChanges(chi, newConformation);
for (List<Integer> torsionIndices : sidechainTorsionIndices)
{
newSidechainTorsionalEnergy += getDihedralEnergy(torsionIndices, newConformation);
if (r.equals(mutatedResidue))
{
numberTorsionChanges++;
allTorsionChanges.add(torsionIndices);
test.add(torsionIndices);
}
}
}
// Proline correction
if (r.aminoAcid.isProline())
{
// Add additional torsion that are missing from list of side chain torsions
// chi 4 and its dependencies must be added
// Atom 3 in chi 3 is the delta carbon
Atom Cdelta = mutatedResidue.chis.get(2).atom3;
Atom N = mutatedResidue.N;
// First find all dependencies
// Get adjacent atoms not including a2 or a3 to make combinations of a-a2-a3-a
Set<Atom> connectedToAtom2 = newConformation.getAdjacentAtoms(Cdelta);
connectedToAtom2.remove(N);
Set<Atom> connectedToAtom3 = newConformation.getAdjacentAtoms(N);
connectedToAtom3.remove(Cdelta);
for (Atom a1 : connectedToAtom2)
{
for (Atom a4 : connectedToAtom3)
{
List<Integer> torsionIndices = new LinkedList<>();
torsionIndices.add(newConformation.contents.indexOf(a1));
torsionIndices.add(newConformation.contents.indexOf(Cdelta));
torsionIndices.add(newConformation.contents.indexOf(N));
torsionIndices.add(newConformation.contents.indexOf(a4));
if (r.equals(mutatedResidue))
{
allTorsionChanges.add(torsionIndices);
test.add(torsionIndices);
numberTorsionChanges++;
}
newSidechainTorsionalEnergy += getDihedralEnergy(torsionIndices, newConformation);
}
}
}
}
System.out.println("Number of total torsion changes = " + numberTorsionChanges);
for (List<Integer> indices : test)
System.out.printf("%d - %d - %d - %d\n", indices.get(0) + 1, indices.get(1) + 1, indices.get(2) + 1, indices.get(3) + 1);
energyChange += (newSidechainTorsionalEnergy - currentSidechainTorsionalEnergy);
// For debugging
System.out.println("The total torsional energy change is " + energyChange);
// Proline correction
if (mutatedResidue.aminoAcid.isProline())
{
// This connection is not included in the list of chis
Atom CDelta = mutatedResidue.chis.get(2).atom3;
Atom N = mutatedResidue.N;
// 1. Add bond change
// Calculate previous bond energy
Atom previousCDelta = previousResidue.chis.get(2).atom3;
Atom previousN = previousResidue.N;
double previousDistance = Molecule.getDistance(previousCDelta, previousN);
double PROLINE_CDELTA_N_SPRING_CONSTANT = FixedSequenceOPLScalculator.PROLINE_CDELTA_N_SPRING_CONSTANT;
double PROLINE_CDELTA_N_BOND_LENGTH = FixedSequenceOPLScalculator.PROLINE_CDELTA_N_BOND_LENGTH;
double previousBondEnergy = PROLINE_CDELTA_N_SPRING_CONSTANT * Math.pow(previousDistance - PROLINE_CDELTA_N_BOND_LENGTH, 2);
double newDistance = Molecule.getDistance(CDelta, N);
double newBondEnergy = PROLINE_CDELTA_N_SPRING_CONSTANT * Math.pow(newDistance - PROLINE_CDELTA_N_BOND_LENGTH, 2);
double bondEnergyChange = newBondEnergy - previousBondEnergy;
System.out.println("The bond energy change is : " + bondEnergyChange);
energyChange += bondEnergyChange;
// Add angle changes
// Build all angles with N-Cdelta-a or a-Cdelta-N
Set<Atom> connectedToCDelta = newConformation.getAdjacentAtoms(CDelta);
connectedToCDelta.remove(N);
Set<Atom> connectedToN = newConformation.getAdjacentAtoms(N);
connectedToN.remove(CDelta);
Set<List<Integer>> anglesChanging = new HashSet<>();
// 2. Add angles of N-Cdelta-a
for (Atom a : connectedToCDelta)
{
List<Integer> angleIndices = new LinkedList<>();
angleIndices.add(newConformation.contents.indexOf(N));
angleIndices.add(newConformation.contents.indexOf(CDelta));
angleIndices.add(newConformation.contents.indexOf(a));
anglesChanging.add(angleIndices);
}
// Add angles of a-N-Cdelta
for (Atom a : connectedToN)
{
List<Integer> angleIndices = new LinkedList<>();
angleIndices.add(newConformation.contents.indexOf(a));
angleIndices.add(newConformation.contents.indexOf(N));
angleIndices.add(newConformation.contents.indexOf(CDelta));
anglesChanging.add(angleIndices);
}
double angleEnergyChange = 0.0;
for (List<Integer> angle : anglesChanging)
angleEnergyChange += (getAngleEnergy(angle, newConformation) - getAngleEnergy(angle, currentConformation));
System.out.println("Angle energy change: " + angleEnergyChange);
energyChange += angleEnergyChange;
}
// Recalculate the non bonded energy
double newNonBondedEnergy = getNonBondedEnergy(newConformation);
System.out.println("Previous non bonded energy : " + currentNonBondedEnergy);
System.out.println("New non bonded energy : " + newNonBondedEnergy);
energyChange += (newNonBondedEnergy - currentNonBondedEnergy);
System.out.println("The overall energy change is: " + energyChange);
// Reset conformations and return new energy
double oldEnergy = currentConformation.energyBreakdown.totalEnergy;
double newEnergy = oldEnergy + energyChange;
// Address solvation energy
newConformation = newConformation.setEnergyBreakdown(new EnergyBreakdown(null, newEnergy, 0.0, newEnergy, null, Forcefield.OPLS));
// update conformations
previousConformation = currentConformation;
previousNonBondedEnergy = currentNonBondedEnergy;
currentConformation = newConformation;
currentNonBondedEnergy = newNonBondedEnergy;
// update torsional energy
previousSidechainTorsionalEnergy = currentSidechainTorsionalEnergy;
currentSidechainTorsionalEnergy = newSidechainTorsionalEnergy;
return newEnergy;
}
public static void main(String[] args)
{
// Create peptide
DatabaseLoader.go();
List<ProtoAminoAcid> sequence = ProtoAminoAcidDatabase.getSpecificSequence("arg","met","standard_ala","gly","d_proline", "gly", "phe", "val", "hd", "l_pro");
Peptide peptide = PeptideFactory.createPeptide(sequence);
TinkerXYZInputFile tinkerTest = new TinkerXYZInputFile(peptide, Forcefield.OPLS);
tinkerTest.write("test/original_peptide.xyz");
// Create OPLS calculator
FragmentGenerationOPLScalculator calculator = new FragmentGenerationOPLScalculator(peptide);
// Make a mutation
// Pick a random residue and change omega, phi, psi
int residueNumber = 4;
Peptide newPeptide = BackboneMutator.mutateOmega(peptide, residueNumber);
newPeptide = BackboneMutator.mutatePhiPsi(peptide, residueNumber);
// Change chis -- rotamer pack (to add)
double calculatorPotentialEnergy = calculator.calculateEnergy(newPeptide.sequence.get(residueNumber), newPeptide);
//System.out.println("New nonbonded energy is: " + calculator.currentNonBondedEnergy);
// Call Tinker on mutated peptide
TinkerAnalysisJob tinkerAnalysisJob = new TinkerAnalysisJob(newPeptide, Forcefield.OPLS);
TinkerAnalysisJob.TinkerAnalysisResult result = tinkerAnalysisJob.call();
TinkerAnalyzeOutputFile outputFile = result.tinkerAnalysisFile;
double tinkerPotentialEnergy = outputFile.totalEnergy;
TinkerXYZInputFile mutated = new TinkerXYZInputFile(newPeptide, Forcefield.OPLS);
mutated.write("test/mutated_peptide.xyz");
// now find non bonded energy terms, torsional energy, and all other terms summed for the mutated peptide
// Compare energy from Tinker with energy from OPLS calculator
if (calculatorPotentialEnergy == tinkerPotentialEnergy)
System.out.println("Success");
else
System.out.println("The tinker energy is : " + tinkerPotentialEnergy + " and the calculator PE is : " + calculatorPotentialEnergy);
}
}