/
DEECalculator.java
469 lines (431 loc) · 21.4 KB
/
DEECalculator.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
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
import java.io.*;
import java.util.*;
import com.google.common.collect.*;
import java.util.concurrent.*;
import java.util.concurrent.atomic.*;
import org.apache.commons.math3.geometry.euclidean.threed.Vector3D;
import org.apache.commons.io.FileUtils;
import org.apache.commons.math3.distribution.NormalDistribution;
/**
* This class collects together some methods for calling DEE on a given peptide.
*/
public class DEECalculator implements Immutable
{
/**
* Calculates one- and two-center energies, performs DEE, and then enumerates the best poses using parallel A*.
* Could result in no solution (causes an exception). The best poses from A* will be returned.
* @param peptide the peptide to pack
* @param rotamerSpace the rotamer space to use
* @param maxPoses the maximum number of peptides to return
* @return the best poses, along with some randomly mutated versions of them
*/
public static List<Peptide> generatePoses(Peptide peptide, RotamerSpace rotamerSpace, int maxPoses)
{
// calculate the required energies
DEEenergyCalculator calculator = DEEenergyCalculator.analyze(rotamerSpace);
// DEE
int rounds = 0;
int eliminatedThisRound = 0;
int totalEliminated = 0;
int totalSize = 0;
DEESingles thisRound = null;
List<List<Rotamer>> currentRotamerSpace = rotamerSpace.rotamerSpace;
// perform first order split singles
System.out.printf("Split singles...");
do
{
if ( RotamerSpace.countRotamers(currentRotamerSpace) < 100 ) break;
rounds++;
thisRound = new DEESingles(calculator.rotamerSelfEnergies, calculator.rotamerInteractionEnergies,
currentRotamerSpace, rotamerSpace.incompatiblePairs);
List<List<Rotamer>> newRotamerSpace = thisRound.eliminate(DEESingles.JobType.SPLIT_FIRST_ORDER);
// figure out how many rotamers were eliminated in this round
eliminatedThisRound = 0;
totalSize = 0;
for (int i=0; i < currentRotamerSpace.size(); i++)
{
int originalSize = currentRotamerSpace.get(i).size();
int newSize = newRotamerSpace.get(i).size();
totalSize += newSize;
eliminatedThisRound += originalSize - newSize;
//System.out.printf(" %3d rotamers eliminated from position %d\n", originalSize-newSize, i);
}
currentRotamerSpace = newRotamerSpace;
totalEliminated += eliminatedThisRound;
System.out.printf("%d, ", eliminatedThisRound);
}
while ( eliminatedThisRound > 0 );
System.out.printf("done. Eliminated %d rotamers, %d remain.\n", totalEliminated, totalSize);
// perform magic bullet split singles
rounds = 0;
eliminatedThisRound = 0;
totalEliminated = 0;
System.out.printf("Magic bullet split singles...");
do
{
if ( RotamerSpace.countRotamers(currentRotamerSpace) < 100 ) break;
rounds++;
thisRound = new DEESingles(calculator.rotamerSelfEnergies, calculator.rotamerInteractionEnergies,
currentRotamerSpace, rotamerSpace.incompatiblePairs);
List<List<Rotamer>> newRotamerSpace = thisRound.eliminate(DEESingles.JobType.MB_SPLIT_FIRST_ORDER);
// figure out how many rotamers were eliminated in this round
eliminatedThisRound = 0;
totalSize = 0;
for (int i=0; i < currentRotamerSpace.size(); i++)
{
int originalSize = currentRotamerSpace.get(i).size();
int newSize = newRotamerSpace.get(i).size();
totalSize += newSize;
eliminatedThisRound += originalSize - newSize;
//System.out.printf(" %3d rotamers eliminated from position %d\n", originalSize-newSize, i);
}
currentRotamerSpace = newRotamerSpace;
totalEliminated += eliminatedThisRound;
System.out.printf("%d, ", eliminatedThisRound);
}
while ( eliminatedThisRound > 0 );
System.out.printf("done. Eliminated %d rotamers, %d remain.\n", totalEliminated, totalSize);
// perform magic bullet doubles
thisRound = new DEESingles(calculator.rotamerSelfEnergies, calculator.rotamerInteractionEnergies,
currentRotamerSpace, rotamerSpace.incompatiblePairs);
rounds = 0;
totalEliminated = 0;
System.out.printf("Magic bullet doubles...");
do
{
if ( RotamerSpace.countRotamers(currentRotamerSpace) < 100 ) break;
rounds++;
eliminatedThisRound = 0;
DEEDoubles doubles = DEEDoubles.createDEEDoubles(thisRound);
doubles.performMagicBulletDoubles();
int oldIncompatibleSize = thisRound.incompatiblePairs.size();
thisRound = doubles.getDEESingles();
eliminatedThisRound = thisRound.incompatiblePairs.size() - oldIncompatibleSize;
totalEliminated += eliminatedThisRound;
System.out.printf("%d, ", eliminatedThisRound);
}
while (eliminatedThisRound > 0 && rounds <= 2 );
System.out.printf("done. Eliminated %d rotamer pairs, %d total.\n", totalEliminated, thisRound.incompatiblePairs.size());
// split singles again
rounds = 0;
totalEliminated = 0;
System.out.printf("Split singles repeat...");
do
{
if ( RotamerSpace.countRotamers(currentRotamerSpace) < 100 ) break;
rounds++;
List<List<Rotamer>> newRotamerSpace = thisRound.eliminate(DEESingles.JobType.SPLIT_FIRST_ORDER);
// figure out how many rotamers were eliminated in this round
eliminatedThisRound = 0;
totalSize = 0;
for (int i=0; i < currentRotamerSpace.size(); i++)
{
int originalSize = currentRotamerSpace.get(i).size();
int newSize = newRotamerSpace.get(i).size();
totalSize += newSize;
eliminatedThisRound += originalSize - newSize;
//System.out.printf(" %3d rotamers eliminated from position %d\n", originalSize-newSize, i);
}
currentRotamerSpace = newRotamerSpace;
totalEliminated += eliminatedThisRound;
System.out.printf("%d, ", eliminatedThisRound);
}
while ( eliminatedThisRound > 0 );
System.out.printf("done. Eliminated %d rotamers, %d remain.\n", totalEliminated, totalSize);
// split magic double singles again
rounds = 0;
eliminatedThisRound = 0;
totalEliminated = 0;
System.out.printf("Magic bullet split singles repeat...");
do
{
if ( RotamerSpace.countRotamers(currentRotamerSpace) < 100 ) break;
rounds++;
thisRound = new DEESingles(thisRound.rotamerSelfEnergies, thisRound.rotamerInteractionEnergies,
currentRotamerSpace, thisRound.incompatiblePairs);
List<List<Rotamer>> newRotamerSpace = thisRound.eliminate(DEESingles.JobType.MB_SPLIT_FIRST_ORDER);
// figure out how many rotamers were eliminated in this round
eliminatedThisRound = 0;
totalSize = 0;
for (int i=0; i < currentRotamerSpace.size(); i++)
{
int originalSize = currentRotamerSpace.get(i).size();
int newSize = newRotamerSpace.get(i).size();
totalSize += newSize;
eliminatedThisRound += originalSize - newSize;
//System.out.printf(" %3d rotamers eliminated from position %d\n", originalSize-newSize, i);
}
currentRotamerSpace = newRotamerSpace;
totalEliminated += eliminatedThisRound;
System.out.printf("%d, ", eliminatedThisRound);
}
while ( eliminatedThisRound > 0 );
System.out.printf("done. Eliminated %d rotamers, %d remain.\n", totalEliminated, totalSize);
// check there is a possible solution
RotamerSpace.checkRotamerSpace(currentRotamerSpace, peptide, thisRound.incompatiblePairs);
// print out the rotamer space after elimination
RotamerSpace.printRotamerSizes("Rotamer space after elimination:", currentRotamerSpace);
for (int i=0; i < currentRotamerSpace.size(); i++)
{
List<Rotamer> list = currentRotamerSpace.get(i);
System.out.printf(" [ position %2d, %2d rotamers ] ", i, list.size());
Set<String> descriptions = new TreeSet<>();
for (Rotamer r : list)
{
String description = r.description;
String[] fields = description.split("_");
String alteredDescription = description.replaceAll(fields[0] + "_","");
descriptions.add(alteredDescription);
}
for (String s : descriptions)
System.out.print(s + " ");
System.out.println();
}
System.out.println(thisRound.incompatiblePairs.size() + " incompatible pairs.");
// A* graph traversal
System.out.println("\nA* Search");
RotamerIterator iterator = new RotamerIterator(currentRotamerSpace, thisRound.rotamerSelfEnergies,
thisRound.rotamerInteractionEnergies, maxPoses);
List<RotamerIterator.Node> solutions = iterator.iterate();
if ( solutions.size() == 0 )
throw new IllegalArgumentException("no valid solutions found");
/*System.out.println(solutions.get(0));
Peptide bestPeptide = Rotamer.reconstitute(peptide, solutions.get(0).rotamers);
GaussianInputFile gjf = new GaussianInputFile(bestPeptide);
gjf.write("test_peptides/best.gjf");
TinkerXYZInputFile xyz = new TinkerXYZInputFile(bestPeptide);
xyz.write("test_peptides/best.xyz");*/
// take the best poses
List<Future<Result>> futures = new LinkedList<>();
List<Peptide> poses = Collections.synchronizedList(new ArrayList<Peptide>(maxPoses));
for (RotamerIterator.Node node : solutions)
{
if ( futures.size() >= maxPoses )
break;
List<Rotamer> rotamers = node.rotamers;
// check charge
int charge = PeptideChargeCalculator.getCharge(peptide, rotamers);
if ( charge < 0 || charge > 1 )
continue;
PeptideJob job = new PeptideJob(rotamers, peptide, poses);
Future<Result> f = GeneralThreadService.submit(job);
futures.add(f);
}
// make the peptides in parallel
GeneralThreadService.silentWaitForFutures(futures);
System.out.printf("%d poses generated.\n", poses.size());
for (Peptide p : poses)
System.out.println(p.name);
return poses;
}
/** Reconstitutes a set of rotamers in parallel. */
public static class PeptideJob implements WorkUnit
{
public final List<Rotamer> rotamers;
public final Peptide templatePeptide;
public final List<Peptide> targetList;
public PeptideJob(List<Rotamer> rotamers, Peptide templatePeptide, List<Peptide> targetList)
{
this.rotamers = rotamers;
this.templatePeptide = templatePeptide;
this.targetList = targetList;
}
public Result call()
{
Peptide newPeptide = Rotamer.reconstitute(templatePeptide, rotamers);
targetList.add(newPeptide);
return null;
}
}
/**
* Checks to make sure that the arginine and TS oxygen are in proper contact.
*/
public static boolean hasArgContact(Peptide peptide)
{
Atom TSoxygen = RotamerFactory.locateSingleAtom(ImmutableSet.of(408,428), peptide.contents);
List<Atom> arginineHs = RotamerFactory.locateAtoms(ImmutableSet.of(209,212), peptide.contents);
for (Atom arginineAtom : arginineHs)
{
if ( Molecule.getDistance(TSoxygen, arginineAtom) < 2.2 )
return true;
}
return false;
}
/**
* Ensures the transition state oxygen and a backbone HN are in proper contact.
*/
public static boolean hasBackboneContact(Peptide peptide)
{
Atom TSoxygen = RotamerFactory.locateSingleAtom(ImmutableSet.of(408,428), peptide.contents);
for (Residue r : peptide.sequence)
{
if ( r.HN != null && Molecule.getDistance(r.HN, TSoxygen) < 2.2 &&
Molecule.getAngle(r.N, r.HN, TSoxygen) > 120.0 )
return true;
}
return false;
}
/**
* Returns a string of the form 0_2_4 where the numbers are the indices of the
* TS, histidine, and arginine, respectively. If something is not found or found twice
* an exception is thrown.
* @param peptide the peptide to analyze
* @return the index string
*/
public static String getSignature(Peptide peptide)
{
String returnString = "";
int TSindex = -1;
int histidineIndex = -1;
int arginineIndex = -1;
for (int i=0; i < peptide.sequence.size(); i++)
{
Residue residue = peptide.sequence.get(i);
String description = residue.description;
if ( description.indexOf("transition_state") > -1 )
{
if ( TSindex == -1 )
TSindex = i;
else throw new IllegalArgumentException("duplicate TS");
}
else if ( description.indexOf("histidine") > -1 )
{
if ( histidineIndex == -1 )
histidineIndex = i;
else throw new IllegalArgumentException("duplicate histidine");
}
else if ( description.indexOf("arginine") > -1 )
{
if ( arginineIndex == -1 )
arginineIndex = i;
else throw new IllegalArgumentException("duplicate arginine");
}
}
if ( TSindex == -1 || histidineIndex == -1 || arginineIndex == -1 )
throw new IllegalArgumentException(String.format("Indices not found: TS %d, histidine %d, arginine %d", TSindex, histidineIndex, arginineIndex));
//returnString = String.format("%d_%d_%d", TSindex, histidineIndex, arginineIndex);
Set<Integer> set = ImmutableSet.of(TSindex, histidineIndex, arginineIndex);
set = new TreeSet<>(set);
for (Integer i : set)
returnString += i + "_";
return returnString.substring(0,returnString.length()-1);
}
/** for testing */
public static void main(String[] args)
{
// make some beta sheet templates
DatabaseLoader.go();
List<Peptide> sheets = BetaSheetGenerator.generateSheets(5, 100, 100000, 0.001);
Collections.sort(sheets);
// remove duplicates
int maxResults = 1000;
double RMSDthreshold = 0.5;
List<Peptide> results = new ArrayList<>();
results.add(sheets.get(0));
for (int i=1; i < sheets.size(); i++)
{
Peptide candidatePeptide = sheets.get(i);
Superposition superposition = Superposition.superimpose(candidatePeptide, results);
boolean accept = true;
for (Double RMSD : superposition.RMSDs)
{
// reject this candidate if it's too similar to an existing peptide
if ( RMSD <= RMSDthreshold )
{
accept = false;
break;
}
}
if ( accept )
results.add(candidatePeptide);
if ( results.size() >= maxResults )
break;
}
System.out.printf("%d unique sheets generated\n", results.size());
//Peptide.writeGJFs(results, "test_peptides/sheet_", 3, maxResults);
// find interesting tuples
List<Peptide> interestingPeptides = Collections.synchronizedList(new ArrayList<Peptide>());
List<Future<Result>> futures = new ArrayList<>();
for (Peptide peptide : results)
{
RotamerFactory.InterestingJob job = new RotamerFactory.InterestingJob(peptide, interestingPeptides);
Future<Result> f = GeneralThreadService.submit(job);
futures.add(f);
}
GeneralThreadService.waitForFutures(futures);
if ( interestingPeptides.size() == 0 )
throw new IllegalArgumentException("no interesting peptides found");
// write out results
System.out.printf("\n%d peptides generated\n", interestingPeptides.size());
//Peptide.writeGJFs(interestingPeptides, "test_peptides/result_", 3, 10);
//Peptide.writeCHKs(interestingPeptides, "test_peptides/result_", 3, 10);
// rotamer pack
List<Peptide> poses = new ArrayList<>();
int maxPoses = 10000;
for (int i=0; i < interestingPeptides.size(); i++)
{
if ( poses.size() > maxPoses )
{
System.out.println("Got enough poses, so not packing anymore.");
break;
}
System.out.printf("[ Rotamer packing design %d of %d (%d poses generated so far)... ]\n", i+1, interestingPeptides.size(), poses.size());
Peptide peptide = interestingPeptides.get(i);
peptide = HydrogenBondMutator.unmutate(peptide);
try
{
CatalystRotamerSpace catalystRotamerSpace = new CatalystRotamerSpace(peptide,true);
List<Peptide> thisPoses = generatePoses(peptide, catalystRotamerSpace, 10);
poses.addAll(thisPoses);
}
catch (Exception e)
{
if ( e instanceof IllegalArgumentException )
System.out.println(e.getMessage());
else
e.printStackTrace();
}
}
//Peptide.writeGJFs(poses, "test_peptides/original_poses_", 5, maxPoses);
// mutate back to close contact forcefield
poses = HydrogenBondMutator.mutate(poses);
// minimize poses
System.out.println("Minimizing all poses:");
List<Peptide> minimizedPoses = BetaSheetGenerator.minimizeSheets(poses, 2000, Forcefield.AMOEBA);
System.out.println("\nDone minimizing.");
//Peptide.writeGJFs(minimizedPoses, "test_peptides/minimized_poses_", 5, maxPoses);
// check the poses are actually sheets
Map<String,List<Peptide>> resultMap = new HashMap<>(); // map from indices of TS,his,arg to peptides
for (Peptide p : minimizedPoses)
{
boolean isSheet = BetaSheetGenerator.isSheet(p,1);
boolean hasBackboneContact = hasBackboneContact(p);
boolean hasArgContact = hasArgContact(p);
String signature = getSignature(p);
boolean pass = isSheet && hasBackboneContact && hasArgContact;
//System.out.printf("%10s sheet: %5b backbone: %5b arg: %5b pass: %5b\n", signature, isSheet, hasBackboneContact, hasArgContact, pass);
if ( !pass )
continue;
// adjust peptide energy by reference energy
double referenceEnergy = Interaction.getAMOEBAReferenceEnergy(p);
EnergyBreakdown energyBreakdown = p.energyBreakdown.addReferenceEnergy(referenceEnergy);
Peptide adjustedPeptide = p.setEnergyBreakdown(energyBreakdown);
// place in result map
List<Peptide> list = resultMap.get(signature);
if ( list == null )
{
list = new ArrayList<>();
resultMap.put(signature,list);
}
list.add(adjustedPeptide);
}
// write out the results
for (String signature : resultMap.keySet())
{
List<Peptide> list = resultMap.get(signature);
Collections.sort(list); // sort by energy
Peptide.writeGJFs(list, "test_peptides/good_" + signature + "_", 3, maxPoses);
Peptide.writeCHKs(list, "test_peptides/good_" + signature + "_", 3, maxPoses);
}
}
}