-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMetadataOutputFile.java
More file actions
380 lines (348 loc) · 16.2 KB
/
MetadataOutputFile.java
File metadata and controls
380 lines (348 loc) · 16.2 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
import java.io.*;
import java.util.*;
import com.google.common.collect.*;
/**
* Metadata files contain information about special atoms, torsions,
* , etc for ProtoAminoAcids. toString, equals, and hashCode not implemented.
* This class is immutable.
*/
public class MetadataOutputFile extends OutputFileFormat implements Serializable, Immutable
{
/** for serialization */
public static final long serialVersionUID = 1L;
// see Residue.java documentation
public final AminoAcid aminoAcid;
public final ResidueType residueType;
public final ProtoTorsion omega;
public final ProtoTorsion phi;
public final ProtoTorsion psi;
public final List<ProtoTorsion> chis;
public final Atom backboneHN;
public final Atom imidazoleHN;
public final Atom imidazoleN;
public final List<Atom> otherHNatoms;
public final Pair<Atom,Atom> NStickyConnection;
public final Pair<Atom,Atom> CStickyConnection;
public final Pair<Atom,Atom> prochiralConnection;
public final double referenceEnergy;
public final String description;
/** the geometry in the corresponding Tinker XYZ file */
public final Molecule molecule;
/** the Tinker XYZ file that contains the gometry for this amino acid */
public final TinkerXYZOutputFile geometryFile;
/** filename of the Tinker XYZ file that contains the geometry for this amino acid */
public final String XYZfilename;
/** public constructor */
public MetadataOutputFile(String filename)
{
super(filename);
// check that this is a valid file
String headerString = stringRepresentation.split("\n")[0];
if ( headerString.indexOf("Amino Acid Metadata") == -1 )
throw new IllegalArgumentException("this does not appear to be an amino acid metadata file");
// create temporary variables
// required, otherwise, we'd have to try and set the final variables inside the loop
// ...which isn't allowed
AminoAcid tempAminoAcid = null;
ResidueType tempResidueType = null;
ProtoTorsion tempOmega = null;
ProtoTorsion tempPhi = null;
ProtoTorsion tempPsi = null;
List<ProtoTorsion> tempChis = new LinkedList<>();
Atom tempBackboneHN = null;
Atom tempImidazoleHN = null;
Atom tempImidazoleN = null;
List<Atom> tempOtherHNatoms = new LinkedList<>();
Pair<Atom,Atom> tempNStickyConnection = null;
Pair<Atom,Atom> tempCStickyConnection = null;
Pair<Atom,Atom> tempProchiralConnection = null;
Double tempReferenceEnergy = null;
String tempDescription = "";
Molecule tempMolecule = null;
TinkerXYZOutputFile tempGeometryFile = null;
String tempXYZfilename = null;
// parse the things that don't depend on the molecular specification
for (List<String> line : fileContents)
{
// check for blank or comment lines
if ( line.size() < 2 || line.get(0).startsWith("!") )
continue;
String firstField = line.get(0).toLowerCase();
if ( firstField.equals("amino_acid") )
tempAminoAcid = AminoAcid.getAminoAcid(line.get(1));
else if ( firstField.equals("xyz_file") )
tempXYZfilename = line.get(1);
else if ( firstField.equals("residue_type") )
tempResidueType = ResidueType.getResidueType(line.get(1));
else if ( firstField.equals("reference_energy") )
tempReferenceEnergy = Double.parseDouble(line.get(1));
else if ( firstField.equals("description") )
{
for ( int i=1; i < line.size(); i++ )
{
tempDescription = tempDescription + line.get(i);
if ( i < line.size() )
tempDescription = tempDescription + " ";
}
}
}
// read the molecular specification
XYZfilename = tempXYZfilename;
geometryFile = new TinkerXYZOutputFile(Settings.AMINO_ACID_DIRECTORY + XYZfilename);
molecule = geometryFile.molecule;
// set all geometry-related fields
for (List<String> line : fileContents)
{
// check for blank or comment lines
if ( line.size() < 2 || line.get(0).startsWith("!") )
continue;
String firstField = line.get(0).toLowerCase();
// parse fields by reading the Tinker atom type
// and locating all the atoms that match that description
// in the current molecule
if ( firstField.equals("phi") )
{
tempPhi = parseProtoTorsion(line);
if ( tempPhi == null )
throw new NullPointerException("phi cannot be null");
if ( line.size() >= 6 && line.get(5).toUpperCase().equals("F") )
tempFrozenTorsions.add(tempPhi);
}
else if ( firstField.equals("psi") )
{
tempPsi = parseProtoTorsion(line);
if ( tempPsi == null )
throw new NullPointerException("psi cannot be null");
if ( line.size() >= 6 && line.get(5).toUpperCase().equals("F") )
tempFrozenTorsions.add(tempPsi);
}
else if ( firstField.equals("omega") )
{
tempOmega = parseProtoTorsion(line);
if ( tempOmega == null )
throw new NullPointerException("omega cannot be null");
if ( line.size() >= 6 && line.get(5).toUpperCase().equals("F") )
tempFrozenTorsions.add(tempOmega);
}
else if ( firstField.startsWith("chi") )
{
ProtoTorsion thisTorsion = null;
if ( line.size() >= 6 && ( line.get(5).toUpperCase().equals("A") || line.get(5).toUpperCase().equals("AF") ) )
thisTorsion = parseAtomQuadruple(line);
else
thisTorsion = parseProtoTorsion(line);
if ( thisTorsion != null )
{
tempChis.add(thisTorsion);
int expectedSize = Integer.parseInt(firstField.replaceAll("[^0-9]",""));
if ( tempChis.size() != expectedSize )
throw new IllegalArgumentException("are the chis out of order?");
if ( line.size() >= 6 && ( line.get(5).toUpperCase().equals("F") || line.get(5).toUpperCase().equals("AF") ) )
tempFrozenTorsions.add(thisTorsion);
}
}
else if ( firstField.equals("backbone_hn") )
tempBackboneHN = parseSingleAtom(line);
else if ( firstField.equals("imidazole_hn") )
tempImidazoleHN = parseSingleAtom(line);
else if ( firstField.equals("imidazole_n") )
tempImidazoleN = parseSingleAtom(line);
else if ( firstField.equals("other_hn") )
tempOtherHNatoms = parseMultipleAtoms(line);
else if ( firstField.equals("nstickyconnection") )
tempNStickyConnection = parsePair(line);
else if ( firstField.equals("cstickyconnection") )
tempCStickyConnection = parsePair(line);
else if ( firstField.equals("prochiral_connection") )
{
if ( line.size() >= 4 && line.get(3).toUpperCase().equals("A") )
tempProchiralConnection = parseAtomPair(line);
else
tempProchiralConnection = parsePair(line);
}
}
// consistency check
if ( tempNStickyConnection == null )
throw new NullPointerException("N-sticky connection is required!");
if ( tempNStickyConnection.getFirst().element != Element.NITROGEN ||
tempNStickyConnection.getSecond().element != Element.CARBON )
throw new IllegalArgumentException("Illegal atom type for NStickyConnection");
if ( tempCStickyConnection == null )
throw new NullPointerException("N-sticky connection is required!");
if ( tempCStickyConnection.getFirst().element != Element.CARBON ||
tempCStickyConnection.getSecond().element != Element.NITROGEN )
throw new IllegalArgumentException("Illegal atom type for CStickyConnection");
if ( tempAminoAcid == AminoAcid.HIS )
{
if ( tempImidazoleHN == null || tempImidazoleN == null )
throw new IllegalArgumentException("histidine HN and N must be specified!");
}
else
{
if ( tempImidazoleHN != null || tempImidazoleN != null )
throw new IllegalArgumentException("should not specify imidazole atoms for a non-histidine");
}
if ( tempBackboneHN == null && !tempAminoAcid.isProline() )
throw new NullPointerException("backbone HN must be specified");
if ( tempReferenceEnergy == null )
throw new NullPointerException("reference energy must be set");
if ( tempProchiralConnection == null )
throw new NullPointerException("must specify a prochiral connection!");
// clean up
aminoAcid = tempAminoAcid;
residueType = tempResidueType;
omega = tempOmega;
phi = tempPhi;
psi = tempPsi;
chis = ImmutableList.copyOf(tempChis);
backboneHN = tempBackboneHN;
imidazoleHN = tempImidazoleHN;
imidazoleN = tempImidazoleN;
otherHNatoms = ImmutableList.copyOf(tempOtherHNatoms);
NStickyConnection = tempNStickyConnection;
CStickyConnection = tempCStickyConnection;
prochiralConnection = tempProchiralConnection;
referenceEnergy = tempReferenceEnergy;
description = tempDescription;
}
/**
* Parses lines of the form:<p>
* field_title 1 2 3 4 [F]<p>
* where 1 2 3 4 are Tinker atom types.
* Will throw an error if there are multiple atoms corresponding to the same atom type.
* @param line the newline-separated fields of the current line
* @return the ProtoTorsion that corresponds to these atom types
*/
private ProtoTorsion parseProtoTorsion(List<String> line)
{
if ( line.get(1).toLowerCase().equals("null") )
return null;
int atom1type = Integer.parseInt(line.get(1));
int atom2type = Integer.parseInt(line.get(2));
int atom3type = Integer.parseInt(line.get(3));
int atom4type = Integer.parseInt(line.get(4));
Atom atom1 = getSingleAtom(atom1type);
Atom atom2 = getSingleAtom(atom2type);
Atom atom3 = getSingleAtom(atom3type);
Atom atom4 = getSingleAtom(atom4type);
return new ProtoTorsion(atom1, atom2, atom3, atom4);
}
/**
* Parses lines of the form:<p>
* field_title 1 2
* where 1 2 are Tinker atom types.
* Will throw an error if there are multiple atoms corresponding to the same atom type.
* @param line the newline-separated fields of the current line
* @return the pair that corresponds to these atom types
*/
private Pair<Atom,Atom> parsePair(List<String> line)
{
if ( line.get(1).toLowerCase().equals("null") )
return null;
int atom1type = Integer.parseInt(line.get(1));
int atom2type = Integer.parseInt(line.get(2));
Atom atom1 = getSingleAtom(atom1type);
Atom atom2 = getSingleAtom(atom2type);
return new Pair<Atom,Atom>(atom1, atom2);
}
/**
* Parses lines of the form:<p>
* field_title 1 2
* where 1 2 are atom numbers 1,2,...,N
* Will throw an error if the atom is not found.
* @param line the newline-separated fields of the current line
* @return the pair that corresponds to these atom types
*/
private Pair<Atom,Atom> parseAtomPair(List<String> line)
{
int atom1number = Integer.parseInt(line.get(1));
int atom2number = Integer.parseInt(line.get(2));
Atom atom1 = molecule.contents.get(atom1number-1);
Atom atom2 = molecule.contents.get(atom2number-1);
return new Pair<Atom,Atom>(atom1, atom2);
}
/**
* Parses lines of the form:<p>
* field_title 1 2
* where 1 2 are atom numbers 1,2,...,N
* Will throw an error if the atom is not found.
* @param line the newline-separated fields of the current line
* @return the pair that corresponds to these atom types
*/
private ProtoTorsion parseAtomQuadruple(List<String> line)
{
int atom1number = Integer.parseInt(line.get(1));
int atom2number = Integer.parseInt(line.get(2));
int atom3number = Integer.parseInt(line.get(3));
int atom4number = Integer.parseInt(line.get(4));
Atom atom1 = molecule.contents.get(atom1number-1);
Atom atom2 = molecule.contents.get(atom2number-1);
Atom atom3 = molecule.contents.get(atom3number-1);
Atom atom4 = molecule.contents.get(atom4number-1);
return new ProtoTorsion(atom1, atom2, atom3, atom4);
}
/**
* Parses lines of the form:<p>
* field_title 1 2 ...
* where 1 2 ... are Tinker atom types.
* @param line the newline-separated fields of the current line
* @return the Atoms that corresponds to these atom types
*/
private List<Atom> parseMultipleAtoms(List<String> line)
{
if ( line.get(1).toLowerCase().equals("null") )
return ImmutableList.of();
List<Atom> returnList = new LinkedList<Atom>();
for (int i=1; i < line.size(); i++)
{
String currentField = line.get(i);
if ( currentField.startsWith("!") )
break;
returnList.addAll(getMultipleAtoms(Integer.parseInt(currentField)));
}
return ImmutableList.copyOf(returnList);
}
/**
* Parses lines of the form:<p>
* field_title 1
* where 1 is a Tinker atom types.
* Will throw an error if there are multiple hits.
* @param line the newline-separated fields of the current line
* @return the Atom that corresponds to this atom types
*/
private Atom parseSingleAtom(List<String> line)
{
if ( line.get(1).toLowerCase().equals("null") )
return null;
return getSingleAtom(Integer.parseInt(line.get(1)));
}
// searches through the molecule for atoms matching this description
private List<Atom> getMultipleAtoms(int atomType)
{
List<Atom> results = new LinkedList<>();
for ( Atom a : molecule.contents )
{
if ( a.tinkerAtomType == atomType )
results.add(a);
}
return results;
}
// same as getAtom(int atomType), but only allows one result
private Atom getSingleAtom(int atomType)
{
List<Atom> results = getMultipleAtoms(atomType);
if ( results.size() == 0 )
throw new IllegalArgumentException("no matches found for atom type " + atomType);
else if ( results.size() > 1 )
throw new IllegalArgumentException("multiple matches found for atom type " + atomType);
return results.get(0);
}
/** for testing */
public static void main(String[] args)
{
MetadataOutputFile m = new MetadataOutputFile("amino_acids/Tyr.txt");
ProtoAminoAcid protoTyrosine = new ProtoAminoAcid(m);
System.out.println(protoTyrosine);
InputFileFormat.writeStringToDisk(protoTyrosine.molecule.toGaussianString(), "amino_acids/Tyr.gjf");
}
}