-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPeptide.java
360 lines (313 loc) · 14.1 KB
/
Peptide.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
import java.util.*;
import java.io.*;
import com.google.common.collect.*;
import org.apache.commons.math3.geometry.euclidean.threed.*;
import org.jgrapht.*;
import org.jgrapht.graph.*;
import org.jgrapht.alg.*;
import java.util.concurrent.*;
/**
* This class represents a peptide. It is immutable.
*/
public class Peptide extends Molecule implements Immutable, Serializable, Comparable<Peptide>
{
public static final long serialVersionUID = 1L;
/** the sequence of residues in a peptide starting from the N terminal to the C terminal */
public final List<Residue> sequence;
public final EnergyBreakdown energyBreakdown;
/**
* Public constructor. To create a peptide, use the static factory method
* createPeptide() instead.
*/
public Peptide(String name, List<Atom> contents, SimpleWeightedGraph<Atom,DefaultWeightedEdge> connectivity,
List<Residue> sequence, EnergyBreakdown energyBreakdown)
{
super(name,contents,connectivity);
this.sequence = sequence;
this.energyBreakdown = energyBreakdown;
}
/**
* Replaces one of the residues with a new one with the same atoms, but of a new type.
* Meant for cis/trans proline changes.
*/
public Peptide replaceResidueType(Residue oldResidue, Residue newResidue)
{
List<Residue> newSequence = new LinkedList<>();
for (Residue r : sequence)
{
if ( r.equals(oldResidue) )
newSequence.add(newResidue);
else
newSequence.add(r);
}
return new Peptide(name, contents, connectivity, newSequence, energyBreakdown);
}
/**
* Factory method to create a peptide given a map of old atoms to new atoms. Should be used
* to move atoms.
* @param atomMap a map from old atoms to new atoms (does not have to include all atoms)
*/
public Peptide moveAtoms2(Map<Atom,Atom> atomMap)
{
// copy the list of vertices
List<Atom> newContents = new LinkedList<Atom>();
for (Atom a : contents)
{
if ( atomMap.containsKey(a) )
newContents.add(atomMap.get(a));
else
newContents.add(a);
}
// populate a new connectivity graph
SimpleWeightedGraph<Atom,DefaultWeightedEdge> newConnectivity = new SimpleWeightedGraph<Atom,DefaultWeightedEdge>(DefaultWeightedEdge.class);
for (Atom newAtom : newContents)
newConnectivity.addVertex(newAtom);
for (DefaultWeightedEdge e : connectivity.edgeSet())
{
// get old edge data
Double bondOrder = connectivity.getEdgeWeight(e);
Atom fromAtom = connectivity.getEdgeSource(e);
Atom toAtom = connectivity.getEdgeTarget(e);
// replace any changes
if ( atomMap.containsKey(fromAtom) )
fromAtom = atomMap.get(fromAtom);
if ( atomMap.containsKey(toAtom) )
toAtom = atomMap.get(toAtom);
// create new edge
DefaultWeightedEdge newEdge = newConnectivity.addEdge(fromAtom,toAtom);
newConnectivity.setEdgeWeight(newEdge, bondOrder);
}
// create new sequence/residues
List<Residue> newSequence = new LinkedList<>();
for (Residue r : sequence)
newSequence.add(r.moveAtoms(atomMap));
newSequence = ImmutableList.copyOf(newSequence);
// return result
// assumes the new peptide has the same name and won't have an EnergyBreakdown yet
return new Peptide(name, newContents, newConnectivity, newSequence, EnergyBreakdown.BLANK);
}
/** returns a new peptide that is exactly the same but has a new name */
public Peptide setName(String newName)
{
return new Peptide(newName, contents, connectivity, sequence, energyBreakdown);
}
/** returns a new peptide that has its EnergyBreakdown set */
public Peptide setEnergyBreakdown(EnergyBreakdown energyBreakdown)
{
return new Peptide(name, contents, connectivity, sequence, energyBreakdown);
}
public Peptide setMolecule(Molecule newMolecule)
{
Molecule oldMolecule = (Molecule)this;
Map<Atom,Atom> atomMap = getAtomMap(oldMolecule,newMolecule);
return moveAtoms2(atomMap);
}
public Peptide setMolecule(List<Vector3D> positions)
{
if ( contents.size() != positions.size() )
throw new IllegalArgumentException("size mismatch");
Map<Atom,Atom> atomMap = new HashMap<>();
for (int i=0; i < contents.size(); i++)
{
Atom oldAtom = contents.get(i);
Atom newAtom = oldAtom.moveAtom(positions.get(i));
atomMap.put(oldAtom,newAtom);
}
return moveAtoms2(atomMap);
}
/** assumes that molecule1 and molecule2 are different conformations
* returns a map from atoms in molecule 1 to atoms in molecule 2
* map will not include atoms that don't move
*/
public static Map<Atom,Atom> getAtomMap(Molecule molecule1, Molecule molecule2)
{
if ( molecule1.contents.size() != molecule2.contents.size() )
throw new IllegalArgumentException("atom list size mismatch");
Map<Atom,Atom> returnMap = new HashMap<>();
for (int i=0; i < molecule1.contents.size(); i++)
{
Atom a1 = molecule1.contents.get(i);
Atom a2 = molecule2.contents.get(i);
if ( ! a1.equals(a2) )
returnMap.put(a1,a2);
}
return returnMap;
}
/**
* Shakes the torsions until reasonable.
* Won't shake hairpin residues.
* @return the pseudo-minimized peptide
*/
public Peptide shake()
{
ThreadLocalRandom random = ThreadLocalRandom.current();
int numberOfResidues = sequence.size();
// shake torsions until reasonable
Peptide peptide = this;
double energy = peptide.getOPLSenergy();
for (int j=0; j<100; j++)
{
//System.out.println(j);
// choose a random residue to mutate
int residueIndex = random.nextInt(numberOfResidues);
Residue residue = peptide.sequence.get(residueIndex);
// don't shake any hairpin residues
if ( residue.description.indexOf("hairpin") > -1 )
{
//System.out.println("skipping " + residue.description);
j--;
continue;
}
// make mutations
Peptide tempPeptide = BackboneMutator.mutatePhiPsi(peptide, residue);
residue = tempPeptide.sequence.get(residueIndex);
tempPeptide = BackboneMutator.mutateOmega(tempPeptide, residue);
AminoAcid.RotamerType rotamerType = residue.aminoAcid.rotamerType;
if ( rotamerType == AminoAcid.RotamerType.IS_ROTAMERIC ||
rotamerType == AminoAcid.RotamerType.NON_ROTAMERIC )
{
residue = tempPeptide.sequence.get(residueIndex);
tempPeptide = RotamerMutator.mutateChis(tempPeptide, residue);
}
if ( tempPeptide.checkCloseContacts() == false )
{
peptide = tempPeptide;
break;
}
else
{
double thisEnergy = tempPeptide.getOPLSenergy();
if (thisEnergy < energy)
{
peptide = tempPeptide;
energy = thisEnergy;
}
}
}
return peptide;
}
/**
* Compares peptides on the basis of their total energy in
* energy breakdown (allows for sorting lists in ascending order)
* @param p2 the peptide to compare this peptide to
*/
@Override
public int compareTo(Peptide p2)
{
if ( this.energyBreakdown == null || p2.energyBreakdown == null )
throw new NullPointerException("null energy breakdown -- cannot compare");
return energyBreakdown.totalEnergy > p2.energyBreakdown.totalEnergy ? 1 : (energyBreakdown.totalEnergy < p2.energyBreakdown.totalEnergy ? -1 : 0);
}
public static void report(Peptide peptide, int j)
{
Residue r = peptide.sequence.get(j-1);
System.out.println(r.toString(peptide));
}
@Override
public int hashCode()
{
return Objects.hash(contents);
}
@Override
public boolean equals(Object obj)
{
if ( obj == null )
return false;
if ( obj == this )
return true;
if ( !(obj instanceof Peptide) )
return false;
Peptide anotherPeptide = (Peptide)obj;
if (contents.equals(anotherPeptide.contents))
return true;
/* if ( contents.size() == anotherPeptide.contents.size() )
{
for ( int i=0; i < contents.size(); i++ )
{
Atom a1 = contents.get(i);
Atom a2 = anotherPeptide.contents.get(i);
if ( ! a1.equals(a2) )
System.out.println("mismatch at index " + i + ": " + a1.toString() + " / " + a2.toString());
}
Scanner scanner = new Scanner(System.in);
scanner.nextLine();
}*/
return false;
}
/** makes a bunch of peptides and checks their invariants */
public static void main(String[] args)
{
System.out.println(OmegaLibrary.INSTANCE);
System.out.println(RamachandranLibrary.INSTANCE);
System.out.println(RotamerLibrary.getDescription());
List<Integer> indexList = ImmutableList.of(1,2,5,6);
List<Double> timings = new LinkedList<>();
for ( int i=0; i < 100; i++ )
{
long startTime = System.currentTimeMillis();
List<String> inputSequence = new LinkedList<>();
for (int j=0; j<5; j++)
inputSequence.add(AminoAcid.getRandom());
inputSequence.add("Dpro");
inputSequence.add("Gly");
for (int j=0; j<5; j++)
inputSequence.add(AminoAcid.getRandom());
System.out.println(inputSequence);
String[] array = inputSequence.toArray(new String[inputSequence.size()]);
List<ProtoAminoAcid> preSequence = ProtoAminoAcidLibrary.getSequence(array,false);
Peptide peptide = createPeptide(preSequence,false);
//for (Residue r : peptide.sequence )
// System.out.println(r.description);
double energy = peptide.getOPLSenergy();
boolean tooClose = peptide.checkCloseContacts();
for (int j=0; j < 100; j++)
{
System.out.print("iteration " + j + "\r");
Peptide lastPeptide = peptide;
// select a residue at random to mutate
int residueIndex = indexList.get( ThreadLocalRandom.current().nextInt(indexList.size()) ) - 1;
Residue residue = peptide.sequence.get(residueIndex);
// make mutations
Peptide tempPeptide = BackboneMutator.mutatePhiPsi(peptide, residue);
residue = tempPeptide.sequence.get(residueIndex);
tempPeptide = BackboneMutator.mutateOmega(tempPeptide, residue);
AminoAcid.RotamerType rotamerType = residue.aminoAcid.rotamerType;
if ( rotamerType == AminoAcid.RotamerType.IS_ROTAMERIC ||
rotamerType == AminoAcid.RotamerType.NON_ROTAMERIC )
{
residue = tempPeptide.sequence.get(residueIndex);
tempPeptide = RotamerMutator.mutateChis(tempPeptide, residue);
}
if ( j > 20 && tempPeptide.checkCloseContacts() == false )
{
peptide = tempPeptide;
energy = tempPeptide.getOPLSenergy();
break;
}
else
{
double thisEnergy = tempPeptide.getOPLSenergy();
if ( thisEnergy < energy )
{
peptide = tempPeptide;
energy = thisEnergy;
}
}
}
System.out.println();
GaussianInputFile gjf = new GaussianInputFile(peptide);
gjf.write(String.format("test_peptides/peptide_%04d.gjf", i));
TinkerXYZInputFile tinkerFile = new TinkerXYZInputFile(peptide);
tinkerFile.write(String.format("test_peptides/peptide_%04d.xyz", i));
long endTime = System.currentTimeMillis();
double elapsedTime = (endTime-startTime)/1000.0;
timings.add(elapsedTime);
System.out.println(String.format("Peptide %03d: elapsed = %.3f, close = %b, energy = %.2f\n", i, elapsedTime, tooClose, energy));
}
double averageTime = 0.0;
for (Double d : timings)
averageTime += d;
averageTime = averageTime / timings.size();
System.out.println(String.format("\nAverage time = %.3f s", averageTime));
}
}