-
Notifications
You must be signed in to change notification settings - Fork 0
/
TinkerAnalysisJob.java
334 lines (297 loc) · 13.7 KB
/
TinkerAnalysisJob.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
import java.util.*;
import java.io.*;
import java.util.concurrent.atomic.*;
import org.apache.commons.math3.geometry.euclidean.threed.*;
/**
* This class represents a TINKER analysis job.
* It is assumed that TINKER is in the path.
* Note that this will not work with "solvate gk" because of a bug in Tinker.
* It will work for "solvate gb," but because the tinker solvation terms are arbitrarily
* partitioned, using this might have weird consequences.
*/
public class TinkerAnalysisJob 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\n\n";
/** default keywords for OPLS */
public static final String DEFAULT_OPLS_KEYWORDS = "parameters oplsaal.prm\n\n";
/** the xyz file that tinker will read from */
public final TinkerXYZInputFile tinkerXYZInputFile;
/** the key file that tinker will read from */
public final TinkerKeyFile tinkerKeyFile;
/** the peptide for which the Analysis job is being carried out */
public final Peptide peptide;
/**
* Creates a job for minimizing some peptide.
* @param peptide the peptide whose energy is to be analyzed
* @param forcefield which forcefield to minimize on
* @param extraKeywords any extra keywords that are desired (newlines required)
*/
public TinkerAnalysisJob(Peptide peptide, Forcefield forcefield, String extraKeywords)
{
if ( peptide == null )
throw new NullPointerException("molecule cannot be null when constructing a tinker minimization job");
this.peptide = peptide;
String keywords = "";
if ( forcefield == Forcefield.AMOEBA )
{
keywords = DEFAULT_AMOEBA_KEYWORDS + extraKeywords;
tinkerXYZInputFile = new TinkerXYZInputFile(peptide, Forcefield.AMOEBA);
}
else if ( forcefield == Forcefield.OPLS )
{
keywords = DEFAULT_OPLS_KEYWORDS + extraKeywords;
tinkerXYZInputFile = new TinkerXYZInputFile(peptide, Forcefield.OPLS);
}
else
throw new IllegalArgumentException("unrecognized forcefield type");
tinkerKeyFile = new TinkerKeyFile(keywords);
}
/** will run with standard keywords only */
public TinkerAnalysisJob(Peptide peptide, Forcefield forcefield)
{
this(peptide,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 TinkerAnalysisResult call()
{
// choose an appropriate base filename
// try up to TINKER_ANALYSIS_MAX_FILENAMES times to make a unique set of filenames
String baseFilename = "";
counting:
for (int i=0; i < Settings.TINKER_ANALYSIS_MAX_FILENAMES; i++)
{
// get a new ID number for this job
int currentIndex = index.getAndIncrement();
baseFilename = String.format("%s_tinker_analysis_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_ANALYSIS_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_ANALYSIS_MAX_FILENAMES )
index.getAndSet(0);
// write input files to disk
tinkerXYZInputFile.write( Settings.TINKER_ANALYSIS_JOB_DIRECTORY + baseFilename + ".xyz" );
tinkerKeyFile.write ( Settings.TINKER_ANALYSIS_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_ANALYSIS_JOB_DIRECTORY + "run_tinker_analysis.sh " +
Settings.TINKER_ANALYSIS_JOB_DIRECTORY + " " + baseFilename;
//System.out.println(runString);
Process process = Runtime.getRuntime().exec(runString);
process.waitFor();
long endTime = System.currentTimeMillis();
exitValue = process.exitValue();
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_ANALYSIS_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
{
OutputFileFormat errorOutput = new OutputFileFormat(Settings.TINKER_ANALYSIS_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 analysis job: " + baseFilename + "!\n" + tail);
}
// retrieve output analysis file
TinkerAnalyzeOutputFile analysisOutput = new TinkerAnalyzeOutputFile(Settings.TINKER_ANALYSIS_JOB_DIRECTORY + baseFilename + ".txt", peptide);
// delete files after normal termination
try
{
File[] files = new File(Settings.TINKER_ANALYSIS_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 TinkerAnalysisResult(analysisOutput, elapsedTime);
}
/**
* A class representing output files from a analysis call.
* Contains the TinkerAnalyzeOutputFile produced from the analysis call.
*/
public static class TinkerAnalysisResult implements Result, Serializable
{
/** for serialization */
public static final long serialVersionUID = 1L;
/** The Tinker Analysis File that is produced by analyze containing the energy breakdown of a peptide */
public final TinkerAnalyzeOutputFile tinkerAnalysisFile;
/** The run time of the call in seconds. */
public final double elapsedTime;
public TinkerAnalysisResult(TinkerAnalyzeOutputFile tinkerAnalysisFile, double elapsedTime)
{
this.elapsedTime = elapsedTime;
this.tinkerAnalysisFile = tinkerAnalysisFile;
}
@Override
public int hashCode()
{
return Objects.hash(tinkerAnalysisFile, elapsedTime);
}
@Override
public boolean equals(Object obj)
{
if ( obj == null )
return false;
if ( obj == this )
return true;
if ( !(obj instanceof TinkerAnalysisResult) )
return false;
TinkerAnalysisResult result = (TinkerAnalysisResult)obj;
if ( Objects.equals(this.tinkerAnalysisFile, result.tinkerAnalysisFile) &&
this.elapsedTime == result.elapsedTime )
return true;
return false;
}
@Override
public String toString()
{
return tinkerAnalysisFile.toString() + String.format(" time = %.3f s\n\n", elapsedTime);
}
}
@Override
public int hashCode()
{
return Objects.hash(tinkerXYZInputFile, tinkerKeyFile, peptide);
}
@Override
public boolean equals(Object obj)
{
if ( obj == null )
return false;
if ( obj == this )
return true;
if ( !(obj instanceof TinkerAnalysisJob) )
return false;
TinkerAnalysisJob job = (TinkerAnalysisJob)obj;
if ( Objects.equals(this.tinkerXYZInputFile, job.tinkerXYZInputFile) &&
Objects.equals(this.tinkerKeyFile, job.tinkerKeyFile) && Objects.equals(this.peptide, job.peptide))
return true;
return false;
}
@Override
public String toString()
{
return "TinkerJob:\n" + tinkerXYZInputFile.stringRepresentation + "\n" + tinkerKeyFile.stringRepresentation + "\n" + peptide.toString();
}
/** For testing. */
public static void main(String[] args)
{
DatabaseLoader.go();
System.out.println("building");
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);
System.out.println("minimizing");
TinkerMinimizationJob job = new TinkerMinimizationJob(peptide, Forcefield.OPLS, "maxiter 2000\n\n");
//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);
System.out.println("grad is " + result.tinkerMinimizationLogFile.gradient);
System.out.println(result.tinkerMinimizationLogFile.iterations + " iterations performed");
System.out.println("amoeba gas phase");
TinkerAnalysisJob job2 = new TinkerAnalysisJob(newPeptide, Forcefield.AMOEBA, "\n\n");
TinkerAnalysisJob.TinkerAnalysisResult result2 = job2.call();
TinkerAnalyzeOutputFile outputFile2 = result2.tinkerAnalysisFile;
System.out.println(outputFile2.energyByResidue);
System.out.println(outputFile2.totalEnergy);
System.out.println("opls with gb");
TinkerAnalysisJob job3 = new TinkerAnalysisJob(newPeptide, Forcefield.OPLS, "solvate gb\n\n");
TinkerAnalysisJob.TinkerAnalysisResult result3 = job3.call();
TinkerAnalyzeOutputFile outputFile3 = result3.tinkerAnalysisFile;
System.out.println(outputFile3.energyByResidue);
System.out.println(outputFile3.totalEnergy);
System.out.println("amoeba with gk");
TinkerAnalysisJob job4 = new TinkerAnalysisJob(newPeptide, Forcefield.AMOEBA, "solvate gk\n\n");
TinkerAnalysisJob.TinkerAnalysisResult result4 = job4.call();
TinkerAnalyzeOutputFile outputFile4 = result4.tinkerAnalysisFile;
System.out.println(outputFile4.energyByResidue);
System.out.println(outputFile4.totalEnergy);
}
}