-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTinkerMinimizationJob.java
More file actions
459 lines (404 loc) · 18.7 KB
/
TinkerMinimizationJob.java
File metadata and controls
459 lines (404 loc) · 18.7 KB
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
import java.util.*;
import java.io.*;
import java.util.concurrent.atomic.*;
import org.apache.commons.math3.geometry.euclidean.threed.*;
/**
* This class represents a TINKER minimization job.
* It is assumed that TINKER is in the path.
*/
public class TinkerMinimizationJob implements WorkUnit, Serializable, Immutable
{
/** for serialization */
public static final long serialVersionUID = 1L;
/** counter for generating unique filename */
public static final AtomicInteger index = new AtomicInteger();
/** default keywords for AMOEBA */
public static final String DEFAULT_AMOEBA_KEYWORDS = "parameters amoebapro13.prm\nwriteout 200\n\n";
/** default keywords for OPLS */
public static final String DEFAULT_OPLS_KEYWORDS = "parameters oplsaal.prm\nwriteout 200\n\n";
/** jobs are killed after this length of time in seconds */
public static final double MAX_JOB_TIME = 300.0;
/** the xyz file that tinker will read from */
public final TinkerXYZInputFile tinkerXYZInputFile;
/** the key file that tinker will read from */
public final TinkerKeyFile tinkerKeyFile;
/**
* Creates a job for minimizing some molecule.
* @param molecule the molecule whose geometry is to be minimized
* @param forcefield which forcefield to minimize on
* @param extraKeywords any extra keywords that are desired (newlines required)
*/
public TinkerMinimizationJob(Molecule molecule, Forcefield forcefield, String extraKeywords)
{
if ( molecule == null )
throw new NullPointerException("molecule cannot be null when constructing a tinker minimization job");
String keywords = "";
if ( forcefield == Forcefield.AMOEBA )
{
keywords = DEFAULT_AMOEBA_KEYWORDS + extraKeywords;
tinkerXYZInputFile = new TinkerXYZInputFile(molecule, Forcefield.AMOEBA);
}
else if ( forcefield == Forcefield.OPLS )
{
keywords = DEFAULT_OPLS_KEYWORDS + extraKeywords;
tinkerXYZInputFile = new TinkerXYZInputFile(molecule, Forcefield.OPLS);
}
else
throw new IllegalArgumentException("unrecognized forcefield type");
tinkerKeyFile = new TinkerKeyFile(keywords);
}
/** will run with standard keywords only */
public TinkerMinimizationJob(Molecule molecule, Forcefield forcefield)
{
this(molecule,forcefield,"");
}
/**
* Auto-selects a filename and runs the minimization calculation.
* It is assumed that the TINKER binaries are in the path.
* @return the result of the calculation
*/
public TinkerMinimizationResult call()
{
// choose an appropriate base filename
// try up to TINKER_MINIMIZATION_MAX_FILENAMES times to make a unique set of filenames
String baseFilename = "";
counting:
for (int i=0; i < Settings.TINKER_MINIMIZATION_MAX_FILENAMES; i++)
{
// get a new ID number for this job
int currentIndex = index.getAndIncrement();
baseFilename = String.format("%s_tinker_minimization_job_%010d", Settings.HOSTNAME, currentIndex);
// don't allow this choice of filenames if any files with this prefix already exist
for ( File f : new File(Settings.TINKER_MINIMIZATION_JOB_DIRECTORY).listFiles() )
{
if ( f.getName().startsWith(baseFilename) )
{
baseFilename = "";
continue counting;
}
}
break;
}
if ( baseFilename.length() == 0 )
throw new IllegalArgumentException("Unable to set filename!");
// reset counter if necessary
if ( index.get() > Settings.TINKER_MINIMIZATION_MAX_FILENAMES )
index.getAndSet(0);
// write input files to disk
tinkerXYZInputFile.write( Settings.TINKER_MINIMIZATION_JOB_DIRECTORY + baseFilename + ".xyz" );
tinkerKeyFile.write ( Settings.TINKER_MINIMIZATION_JOB_DIRECTORY + baseFilename + ".key" );
// call minimize
double elapsedTime = 0.0;
int exitValue = -1;
//boolean badGeometry = false;
try
{
if ( Settings.PLATFORM == Settings.Platform.DOS )
throw new IllegalArgumentException("mandor doesn't work on DOS yet");
else if ( Settings.PLATFORM == Settings.Platform.LINUX )
{
long startTime = System.currentTimeMillis();
String runString = Settings.TINKER_MINIMIZATION_JOB_DIRECTORY + "run_tinker_minimization.sh " +
Settings.TINKER_MINIMIZATION_JOB_DIRECTORY + " " + baseFilename;
//System.out.println(runString);
Process process = Runtime.getRuntime().exec(runString);
while ( true )
{
// check if the process has terminated
GeneralThreadService.wait(500);
/*try
{
TinkerMinimizationLogFile tempOutput = new TinkerMinimizationLogFile(Settings.TINKER_MINIMIZATION_JOB_DIRECTORY + baseFilename + ".out");
if ( tempOutput.stringRepresentation.indexOf("Incomplete Convergence due to BadIntpln") > -1 )
badGeometry = true;
}
catch (Exception e) {}*/
boolean done = true;
try { exitValue = process.exitValue(); }
catch (Exception e) { done = false; }
//if ( done || badGeometry )
if ( done )
break;
long now = System.currentTimeMillis();
elapsedTime = (now - startTime)/1000.0;
//if ( elapsedTime > 45.0 || badGeometry )
if ( elapsedTime > MAX_JOB_TIME )
{
process.destroy();
break;
}
}
long endTime = System.currentTimeMillis();
elapsedTime = (endTime - startTime) / 1000.0;
}
}
catch (Exception e)
{
System.out.println("Error while running Tinker job:");
System.out.println(baseFilename);
e.printStackTrace();
}
// remove files on abnormal termination
if ( exitValue != 0 )
{
try
{
File[] files = new File(Settings.TINKER_MINIMIZATION_JOB_DIRECTORY).listFiles();
for ( File f : files )
{
String filename = f.getName();
if ( f.getName().startsWith(baseFilename) )
{
//f.delete();
//System.out.println(f.getName() + " deleted.");
}
}
}
catch (Exception e)
{
System.out.println("Error while trying to delete files:");
e.printStackTrace();
}
}
// check if the job completed correctly
if ( exitValue == -1 )
throw new IllegalArgumentException(baseFilename + " exceeded the allotted time");
//else if ( badGeometry )
// throw new IllegalArgumentException(baseFilename + ": interpolation error");
else if ( exitValue != 0 )
{
String tail = "";
try
{
TinkerMinimizationLogFile errorOutput = new TinkerMinimizationLogFile(Settings.TINKER_MINIMIZATION_JOB_DIRECTORY + baseFilename + ".out");
String[] lines = errorOutput.stringRepresentation.split("\n");
int length = lines.length;
for (int i=Math.max(length-10,0); i < length; i++)
tail += lines[i] + "\n";
}
catch (Exception e)
{
}
throw new IllegalArgumentException("error code " + exitValue + " while runing tinker minimization job: " + baseFilename + "!\n" + tail);
}
// retrieve output XYZ File
TinkerXYZOutputFile xyzOutput = new TinkerXYZOutputFile(Settings.TINKER_MINIMIZATION_JOB_DIRECTORY + baseFilename + "_minimized.xyz");
// retrieve file that contains energy
TinkerMinimizationLogFile output = new TinkerMinimizationLogFile(Settings.TINKER_MINIMIZATION_JOB_DIRECTORY + baseFilename + ".out");
// delete files after normal termination
try
{
File[] files = new File(Settings.TINKER_MINIMIZATION_JOB_DIRECTORY).listFiles();
for ( File f : files )
{
String filename = f.getName();
if ( f.getName().startsWith(baseFilename) )
{
f.delete();
//System.out.println(f.getName() + " deleted.");
}
}
}
catch (Exception e)
{
System.out.println("Error while trying to delete files:");
e.printStackTrace();
}
// construct and return result
return new TinkerMinimizationResult(output, elapsedTime, xyzOutput);
}
/** A class representing output files from a minimization call. Contains the molecule and log files produced from minimize call */
public static class TinkerMinimizationResult implements Result, Serializable
{
/** for serialization */
public static final long serialVersionUID = 1L;
/** The Tinker Minimiization Log File that is produced by minimize containing the energy of the optimized molecule */
public final TinkerMinimizationLogFile tinkerMinimizationLogFile;
/** The run time of the call in seconds. */
public final double elapsedTime;
/** The xyz file that is produced by a minimize call */
public final TinkerXYZOutputFile tinkerXYZOutputFile;
public TinkerMinimizationResult(TinkerMinimizationLogFile tinkerMinimizationLogFile, double elapsedTime, TinkerXYZOutputFile tinkerXYZOutputFile)
{
this.tinkerMinimizationLogFile = tinkerMinimizationLogFile;
this.elapsedTime = elapsedTime;
this.tinkerXYZOutputFile = tinkerXYZOutputFile;
}
public Molecule getMolecule()
{
return tinkerXYZOutputFile.molecule;
}
public double getEnergy()
{
return tinkerMinimizationLogFile.energy;
}
@Override
public int hashCode()
{
return Objects.hash(tinkerMinimizationLogFile, elapsedTime, tinkerXYZOutputFile);
}
@Override
public boolean equals(Object obj)
{
if ( obj == null )
return false;
if ( obj == this )
return true;
if ( !(obj instanceof TinkerMinimizationResult) )
return false;
TinkerMinimizationResult result = (TinkerMinimizationResult)obj;
if ( Objects.equals(this.tinkerMinimizationLogFile, result.tinkerMinimizationLogFile) &&
this.elapsedTime == result.elapsedTime &&
Objects.equals(tinkerXYZOutputFile, result.tinkerXYZOutputFile) )
return true;
return false;
}
@Override
public String toString()
{
return tinkerMinimizationLogFile.toString() + String.format(" time = %.3f s\n\n", elapsedTime) + tinkerXYZOutputFile.toString();
}
}
/** A class representing the output from a minimization TINKER call */
public static class TinkerMinimizationLogFile extends OutputFileFormat implements FileFormat
{
/** The minimized energy in kcal/mol. */
public final Double energy;
/** The final RMS gradient */
public final Double gradient;
/** The number of iterations */
public final Integer iterations;
/** the filename */
public final String filename;
public TinkerMinimizationLogFile(String filename)
{
super(filename);
this.filename = filename;
// determine minimized energy
Double readEnergy = null;
Double readGradient = null;
Integer readIterations = null;
/*for ( int i=0; i < stringRepresentation.length(); i++ )
{
String currentLetter = stringRepresentation.substring(i,i+1);
char thisChar = currentLetter.charAt(0);
int code = (int)thisChar;
System.out.println(currentLetter + " (" + code + ")");
}*/
for (String line : stringRepresentation.split("\n"))
{
//System.out.println(index + " " + line);
String[] fields = line.trim().split("\\s+");
if ( line.indexOf("Final Function Value") > -1 )
readEnergy = Double.valueOf(fields[4]);
else if ( line.indexOf("Final RMS Gradient") > -1 )
{
try { readGradient = Double.valueOf(fields[4]); }
catch (Exception e) { readGradient = 999.9; }
//System.out.println(readGradient + " : " + line);
}
else
{
try
{
readIterations = Integer.valueOf(fields[0]);
}
catch (NumberFormatException e)
{
}
}
}
if ( readEnergy == null )
{
String exceptionString = "Energy not found!\n";
int start = Math.max(0, fileContents.size()-5);
for (int i=start; i < fileContents.size(); i++)
exceptionString += fileContents.get(i) + "\n";
throw new IllegalArgumentException(exceptionString);
}
if ( readGradient == null )
throw new IllegalArgumentException("Gradient not found!");
if ( readIterations == null )
throw new IllegalArgumentException("Number of iterations not found!");
this.energy = readEnergy;
this.gradient = readGradient;
this.iterations = readIterations;
}
@Override
public int hashCode()
{
return Objects.hash(stringRepresentation, energy);
}
@Override
public boolean equals(Object obj)
{
if ( obj == null )
return false;
if ( obj == this )
return true;
if ( !(obj instanceof TinkerMinimizationLogFile) )
return false;
TinkerMinimizationLogFile file = (TinkerMinimizationLogFile)obj;
if ( Objects.equals(this.stringRepresentation, file.stringRepresentation) &&
Objects.equals(this.energy, file.energy ) )
return true;
return false;
}
@Override
public String toString()
{
return String.format("TinkerMinimizationLogFile: energy = %.3f", energy);
}
}
@Override
public int hashCode()
{
return Objects.hash(tinkerXYZInputFile, tinkerKeyFile);
}
@Override
public boolean equals(Object obj)
{
if ( obj == null )
return false;
if ( obj == this )
return true;
if ( !(obj instanceof TinkerMinimizationJob) )
return false;
TinkerMinimizationJob job = (TinkerMinimizationJob)obj;
if ( Objects.equals(this.tinkerXYZInputFile, job.tinkerXYZInputFile) &&
Objects.equals(this.tinkerKeyFile, job.tinkerKeyFile) )
return true;
return false;
}
@Override
public String toString()
{
return "TinkerJob:\n" + tinkerXYZInputFile.stringRepresentation + "\n" + tinkerKeyFile.stringRepresentation;
}
/** For testing. */
public static void main(String[] args)
{
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);
for (int i=0; i < peptide.sequence.size(); i++)
{
peptide = BackboneMutator.mutateOmega(peptide, i);
peptide = BackboneMutator.mutatePhiPsi(peptide, i);
peptide = RotamerMutator.mutateChis(peptide, i);
}
peptide = PeptideFactory.setHairpinAngles(peptide);
TinkerMinimizationJob job = new TinkerMinimizationJob(peptide, Forcefield.AMOEBA, "maxiter 2000\n\n");
TinkerMinimizationJob.TinkerMinimizationResult result = job.call();
Molecule minimizedMolecule = result.tinkerXYZOutputFile.molecule;
Peptide newPeptide = peptide.setPositions(minimizedMolecule);
double time = result.elapsedTime;
TinkerMinimizationJob.TinkerMinimizationLogFile logFile = result.tinkerMinimizationLogFile;
double energy = logFile.energy;
double gradient = logFile.gradient;
int iterations = logFile.iterations;
for (int i=0; i < peptide.contents.size(); i++)
System.out.println(peptide.contents.get(i).toFullString() + " " + newPeptide.contents.get(i).toFullString());
System.out.printf("\ntime %.1f s energy %.2f kcal RMS gradient %.4f %d iterations\n", time, energy, gradient, iterations);
}
}