-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRotamerFactory.java
1254 lines (1136 loc) · 62.6 KB
/
RotamerFactory.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
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
import java.util.*;
import java.io.*;
import org.apache.commons.math3.geometry.euclidean.threed.*;
import org.jgrapht.*;
import org.jgrapht.graph.*;
import org.jgrapht.alg.*;
import com.google.common.collect.*;
import java.util.concurrent.*;
/**
* This class makes rotamers from a given peptide.
*/
public class RotamerFactory
{
/** This class is not instantiable. */
private RotamerFactory()
{
throw new IllegalArgumentException("not instantiable");
}
/**
* Creates a single rotamer using the atoms that are already in the residue.
* @return a list that only contains the current rotamer
*/
public static List<Rotamer> getOneRotamer(Peptide peptide, Residue residue, boolean includeHN)
{
// return one rotamer that is just the current rotamer
List<Atom> singleRotamerAtoms = new ArrayList<>(getSidechainAtoms(peptide, residue, includeHN));
// determine the chis
List<Double> singleRotamerChiList = null;
if ( residue.chis.size() == 0 )
singleRotamerChiList = ImmutableList.of();
else
{
singleRotamerChiList = new ArrayList<>(residue.chis.size());
for (ProtoTorsion t : residue.chis)
singleRotamerChiList.add(t.getDihedralAngle());
singleRotamerChiList = ImmutableList.copyOf(singleRotamerChiList);
}
int sequenceIndex = peptide.sequence.indexOf(residue);
if ( sequenceIndex == -1 )
throw new IllegalArgumentException("residue not found in sequence");
Rotamer singleRotamer = new Rotamer(singleRotamerAtoms, sequenceIndex, singleRotamerChiList, residue.description);
List<Rotamer> returnList = new ArrayList<>(1);
returnList.add(singleRotamer);
return returnList;
}
/**
* Finds the possible rotamers. The method will draw chis for normal residues (not TS, histidine or hairpin)
* Assumes that the residue is already of the correct type. This method does not check the resulting rotamers for
* clashes with anything (backbone or other rotamers).
* If there are no rotamers possible, an empty list is returned.
* @param peptide the peptide to analyze
* @param residue the residue to generate rotamers for
* @param includeHN if set to true, the backboneHN of this residue will be included in the atoms list of the resulting rotamers
* @param inputChis if non-null, these are lists of chi values in degrees that will be used
* @return a list of rotamers that are possible at the specified position
*/
public static List<Rotamer> generateRotamers(Peptide peptide, Residue residue, boolean includeHN, List<List<Double>> inputChis)
{
// check this is an appropriate residue
if ( !peptide.sequence.contains(residue) )
throw new IllegalArgumentException("residue not in peptide");
// setup some variables
int sequenceIndex = peptide.sequence.indexOf(residue);
String description = residue.description;
AminoAcid aminoAcid = residue.aminoAcid;
if ( inputChis == null && aminoAcid.chirality == Chirality.D )
throw new IllegalArgumentException("can't use this method to generate rotamers for " + description);
List<Rotamer> returnList = new ArrayList<>(); // result rotamers will be placed here
// get possible chi angles
List<List<Double>> possibleChis = null;
if ( inputChis != null )
{
// no chis so return no rotamers
if ( inputChis.size() == 0 )
return returnList;
// the method has been called with specific chi values, so try to use those
// first check if there's the expected number of chis
for (List<Double> list : inputChis)
if ( list.size() != residue.chis.size() )
throw new IllegalArgumentException("unexpected number of chis");
possibleChis = inputChis;
}
else
{
// the method has not been called with specific chi values
if ( residue.aminoAcid.rotamerType == AminoAcid.RotamerType.HAS_NO_ROTAMERS )
{
// return one rotamer that is just the current rotamer
return getOneRotamer(peptide, residue, includeHN);
}
// D amino acids are not supported
else if ( residue.aminoAcid.chirality == Chirality.D )
throw new IllegalArgumentException("no data for D amino acids");
// draw normal chis
possibleChis = RotamerSummarizer.getPossibleRotamers(residue);
}
if ( possibleChis.size() == 0 )
throw new IllegalArgumentException("no chis for " + description);
// get the torsions that describe where the sidechain is
LinkedList<ProtoTorsion> oldProtoTorsions = new LinkedList<>(residue.chis);
// get the connectivity graph
// if this is a proline, make a copy of the graph and disconnect the chi3 bond to the backbone
SimpleWeightedGraph<Atom,DefaultWeightedEdge> connectivity = peptide.connectivity;
if ( aminoAcid.isProline() )
{
// clone the graph
connectivity = Molecule.cloneConnectivity(connectivity);
// remove the bond that connects the proline ring to the backbone at the beta carbon
ProtoTorsion chi3 = residue.chis.get(2);
Atom atom3 = chi3.atom3;
Atom atom4 = chi3.atom4;
DefaultWeightedEdge e = connectivity.removeEdge(atom3,atom4);
if ( e == null )
throw new NullPointerException("unexpected null edge for proline connection");
// don't set the last chi angle
oldProtoTorsions.removeLast();
}
// get the atoms in the sidechain
Pair<Atom,Atom> prochiralConnection = residue.prochiralConnection;
List<Atom> allAtoms = new ArrayList<>(getHalfGraph(connectivity, prochiralConnection.getFirst(), prochiralConnection.getSecond()));
if ( includeHN && residue.HN != null )
allAtoms.add(residue.HN);
// add the backbone atoms we need for the first torsions
allAtoms.add(oldProtoTorsions.get(0).atom1);
allAtoms.add(oldProtoTorsions.get(0).atom2);
// cycle through all the desired torsion angles, not moving anything, but collecting the indices of what to move
Map<ProtoTorsion,List<Integer>> atomsToMoveMap = new HashMap<>();
Map<ProtoTorsion,Integer> atom1indices = new HashMap<>();
Map<ProtoTorsion,Integer> atom2indices = new HashMap<>();
Map<ProtoTorsion,Integer> atom3indices = new HashMap<>();
Map<ProtoTorsion,Integer> atom4indices = new HashMap<>();
for (ProtoTorsion torsion : oldProtoTorsions)
{
// make a list of the atom indices to move
// indices are defined as the indices (0...N-1) in allAtoms
List<Integer> atomIndicesToMove = new LinkedList<>();
// get the atoms to move from the connectivity graph
// and add the indices (as defined in allAtoms) to a list
// this way, we can look up a prototorsion and then see what atom indices we need to move
//Set<Atom> check = peptide.getHalfGraph(torsion.atom3, torsion.atom4);
//System.out.println(check.size());
Set<Atom> atomsToMove = getHalfGraph(connectivity, torsion.atom2, torsion.atom3);
//System.out.println(atomsToMove.size());
for (Atom a : atomsToMove)
atomIndicesToMove.add(allAtoms.indexOf(a));
atomsToMoveMap.put(torsion,atomIndicesToMove);
// do the same thing for the prototorsion atoms so after each time we move a torsion
// we can reconsitute the prototorsion
int atom1index = allAtoms.indexOf(torsion.atom1);
int atom2index = allAtoms.indexOf(torsion.atom2);
int atom3index = allAtoms.indexOf(torsion.atom3);
int atom4index = allAtoms.indexOf(torsion.atom4);
// must deal with atoms that are part of the torsion but won't be in allAtoms
atom1indices.put(torsion, atom1index);
atom2indices.put(torsion, atom2index);
atom3indices.put(torsion, atom3index);
atom4indices.put(torsion, atom4index);
}
// cycle through all the desired torsion angles, actually moving the atoms this time
for (int j=0; j < possibleChis.size(); j++)
{
List<Double> requestedAngles = possibleChis.get(j);
// make a new list of atoms
List<Atom> newAtoms = new ArrayList<>(allAtoms);
// check the number of torsions we have is the same as the
//if ( oldProtoTorsions.size() != requestedAngles.size() )
// throw new IllegalArgumentException("torsion angle list size mismatch");
// set the chi angles one after the other
for (int k=0; k < oldProtoTorsions.size(); k++)
{
// get the old prototorsion
ProtoTorsion oldProtoTorsion = oldProtoTorsions.get(k);
// make a new prototorsion
// this is necessary because the atoms are changing between successive operations
Atom atom1 = newAtoms.get(atom1indices.get(oldProtoTorsion));
Atom atom2 = newAtoms.get(atom2indices.get(oldProtoTorsion));
Atom atom3 = newAtoms.get(atom3indices.get(oldProtoTorsion));
Atom atom4 = newAtoms.get(atom4indices.get(oldProtoTorsion));
ProtoTorsion newProtoTorsion = new ProtoTorsion(atom1, atom2, atom3, atom4);
// figure out what angle we want to set the dihedral to
double theta = requestedAngles.get(k);
// figure out which atoms we have to move
Set<Atom> atomsToMove = new HashSet<>();
for (Integer l : atomsToMoveMap.get(oldProtoTorsion))
atomsToMove.add(newAtoms.get(l));
// move the atoms
newAtoms = setDihedral(newAtoms, atomsToMove, newProtoTorsion, theta);
}
// remove the extra atoms
newAtoms.remove(newAtoms.size()-1);
newAtoms.remove(newAtoms.size()-1);
// return the new Rotamer
Rotamer newRotamer = new Rotamer(newAtoms, sequenceIndex, requestedAngles, description);
returnList.add(newRotamer);
}
// return the result
return returnList;
}
/**
* Rotates the atoms in the specified list by the desired amount.
* The advantage in using this over the related method {@link Molecule#setDihedral(ProtoTorsion,double)} is that
* it's faster. We don't have to rebuild the entire connectivity graph. We need a ProtoTorsion so we know
* how much to rotate by, but we don't have to search the entire connectivity graph over and over
* for the atoms we need to rotate. The order of the atoms in the original list is preserved.
* @param allAtoms all of the atoms in this fragment
* @param atomsToRotate the atoms we should be rotating
* @param protoTorsion the torsion to rotate
* @param theta the angle we want to end up with (in degrees)
* @return the rotated fragment
*/
public static List<Atom> setDihedral(List<Atom> allAtoms, Collection<Atom> atomsToRotate, ProtoTorsion protoTorsion, double theta)
{
// determine how much rotation is needed
double currentDihedralAngle = protoTorsion.getDihedralAngle();
double requiredRotation = currentDihedralAngle - theta;
// get the indices of the atoms to rotate
List<Integer> indicesToRotate = new LinkedList<>();
for (Atom a : atomsToRotate)
{
int index = allAtoms.indexOf(a);
if ( index == -1 )
throw new IllegalArgumentException("atom to rotate is not in allAtoms");
indicesToRotate.add(index);
}
// determine the rotation axis and create the rotation
Vector3D atom2position = protoTorsion.atom2.position;
Vector3D atom3position = protoTorsion.atom3.position;
Vector3D rotationAxis = atom2position.subtract(atom3position);
Rotation rotation = new Rotation(rotationAxis, Math.toRadians(requiredRotation));
// perform the rotation
List<Atom> returnList = new ArrayList<>(allAtoms.size());
for (int i=0; i < allAtoms.size(); i++)
{
Atom a = allAtoms.get(i);
if ( indicesToRotate.contains(i) )
{
// set the origin to atom3 by translating
Vector3D newPosition = a.position.subtract(atom3position);
// apply the rotation
newPosition = rotation.applyTo(newPosition);
// undo the translation
newPosition = newPosition.add(atom3position);
// apply the result
Atom newAtom = a.moveAtom(newPosition);
returnList.add(newAtom);
}
else
returnList.add(a);
}
// return the result
return returnList;
}
/**
* Given a bond between includeAtom and excludeAtom, returns a set of atoms
* containing all the atoms on the includeAtom side of the bond, including
* includeAtom. Will throw an exception if includeAtom and excludeAtom are not
* directly bonded. Will throw an exception if includeAtom and excludeAtom
* form a ring.
* @param connectivity the connectivity graph
* @param excludeAtom this atom will not be included in the result
* @param includeAtom this atom will be included in the result
* @return the atoms on the includeAtom side of the graph
*/
public static Set<Atom> getHalfGraph(SimpleWeightedGraph<Atom,DefaultWeightedEdge> connectivity, Atom excludeAtom, Atom includeAtom)
{
// if these atoms are not directly bonded, then return an empty set
if ( !connectivity.containsEdge(excludeAtom,includeAtom) )
throw new IllegalArgumentException("atoms not connected");
// preform a breadth-first search of one branch of the graph only
Set<Atom> returnSet = new HashSet<Atom>();
LinkedList<Atom> searchQueue = new LinkedList<Atom>();
Set<Atom> searched = new HashSet<>();
for (Atom a : getAdjacentAtoms(connectivity,includeAtom))
{
searchQueue.add(a);
returnSet.add(a);
}
searchQueue.remove(excludeAtom);
returnSet.remove(excludeAtom);
returnSet.add(includeAtom);
searched.add(includeAtom);
Atom lastNode = includeAtom;
while (searchQueue.size() > 0)
{
Atom currentNode = searchQueue.remove();
Set<Atom> adjacent = getAdjacentAtoms(connectivity,currentNode);
adjacent.remove(lastNode);
for (Atom a : adjacent)
{
if ( a == excludeAtom)
{
// if the excluded atom is found, this is a ring!
throw new IllegalArgumentException("RotamerFactory.getHalfGraph found a ring");
}
// if this is an atom we haven't already searched, mark it
if ( ! searched.contains(a) )
{
searchQueue.add(a);
searched.add(a);
returnSet.add(a);
}
}
}
return(returnSet);
}
/**
* Returns the directly bonded neighbors of the specified atom.
* @param connectivity the connectivity graph to search
* @param atom the target vertex to search
*/
public static Set<Atom> getAdjacentAtoms(SimpleWeightedGraph<Atom,DefaultWeightedEdge> connectivity, Atom atom)
{
Set<Atom> returnSet = new HashSet<Atom>();
if ( ! connectivity.containsVertex(atom) )
throw new IllegalArgumentException("atom must be within this connectivity graph!");
for (DefaultWeightedEdge e : connectivity.edgesOf(atom))
{
returnSet.add(connectivity.getEdgeSource(e));
returnSet.add(connectivity.getEdgeTarget(e));
}
returnSet.remove(atom);
return(returnSet);
}
/**
* Returns the sidechain atoms for a given residue.
* @param peptide the peptide the residue is in
* @param residue the residue to get the sidechain atoms of
* @param includeHN if true, the backbone HN will be included in the result
* @return the sidechain atoms of the specified residue
*/
public static Set<Atom> getSidechainAtoms(Peptide peptide, Residue residue, boolean includeHN)
{
Set<Atom> sidechainAtoms = null;
Atom atomA = residue.prochiralConnection.getFirst();
Atom atomB = residue.prochiralConnection.getSecond();
if ( residue.aminoAcid.isProline() )
{
// this is a proline, so create another molecule where the proline
// sidechain has been disconnected
ProtoTorsion chi3 = residue.chis.get(2);
Atom atom3 = chi3.atom3;
Atom atom4 = chi3.atom4;
// temporarily disconnect proline ring bond
SimpleWeightedGraph<Atom,DefaultWeightedEdge> connectivity = Molecule.cloneConnectivity(peptide.connectivity);
DefaultWeightedEdge edge = connectivity.removeEdge(atom3, atom4);
if ( edge == null )
throw new NullPointerException("expected edge for proline");
// traverse the sidechain subgraph
sidechainAtoms = getHalfGraph(connectivity, atomA, atomB);
}
else
{
// just add the sidechain atoms
sidechainAtoms = peptide.getHalfGraph(atomA, atomB);
if ( includeHN )
{
if ( residue.HN == null )
throw new NullPointerException("expected HN");
sidechainAtoms.add(residue.HN);
}
}
return sidechainAtoms;
}
/**
* Returns the atoms that are in the backbone. Backbone atoms are defined as those that will never move during
* rotamer packing. If a position has no rotamers, all atoms at the position are considered part of the backbone.
* If a position is marked as variable, its sidechain atoms will not be included in the result.
* @param peptide the peptide to analyze
* @param variablePositions indices in the peptide sequence where we want to vary the rotamers
* @param includeHN if true, the backbone HNs are considered part of the sidechain
* @return the atoms in the backbone
*/
public static List<Atom> getBackboneAtoms(Peptide peptide, List<Integer> variablePositions, boolean includeHN)
{
List<Atom> backboneAtoms = new ArrayList<>(peptide.contents);
for (int i=0; i < peptide.sequence.size(); i++)
{
Residue residue = peptide.sequence.get(i);
if ( variablePositions.contains(i) && residue.aminoAcid.rotamerType != AminoAcid.RotamerType.HAS_NO_ROTAMERS )
backboneAtoms.removeAll( getSidechainAtoms(peptide, residue, includeHN) );
}
return backboneAtoms;
}
/** how many points to use to search for TS rotamers */
public static final int TS_GRID_SIZE = 13;
/**
* Finds the chi values for a transition state amino acid, under the constraint that the
* anionic oxygen must be near a backbone HN. Clashes with the backbone are crudely checked.
* Assumes this is a beta hairpin.
* @param peptide the peptide to analyze
* @param residue the residue to mutate
* @param includeHN if true, the backbone HN will be included in the rotamer
* @return lists of lists (chi1, chi2) for transition rotamers that satisfy the constraint
*/
public static List<List<Double>> getTransitionStateChis(Peptide peptide, Residue residue, boolean includeHN)
{
// check invariants
if ( residue.description.indexOf("transition_state") == -1 )
throw new IllegalArgumentException("must be a transition state");
int sequenceIndex = peptide.sequence.indexOf(residue);
if ( sequenceIndex == -1 )
throw new IllegalArgumentException("bad sequence index");
// get the backbone atoms
int sequenceLength = peptide.sequence.size();
if ( sequenceLength % 2 != 0 )
throw new IllegalArgumentException("expecting even sequence length");
List<Integer> variablePositions = new ArrayList<>(sequenceLength-2);
int forbiddenIndex = (sequenceLength/2) - 1;
if ( sequenceIndex == forbiddenIndex || sequenceIndex == forbiddenIndex + 1 )
throw new IllegalArgumentException("can't place TS at a hairpin position");
for (int i=0; i < sequenceLength; i++)
{
if ( i == forbiddenIndex )
{
i++;
continue;
}
variablePositions.add(i);
}
List<Atom> backboneAtoms = getBackboneAtoms(peptide, variablePositions, includeHN);
//for (Atom a : backboneAtoms)
// System.out.print(peptide.getAtomString(a) + ", " );
//System.out.println();
// get the rotation axes
ProtoTorsion chi1torsion = residue.chis.get(0);
ProtoTorsion chi2torsion = residue.chis.get(1);
// get the sidechain positions
Pair<Atom,Atom> prochiralConnection = residue.prochiralConnection;
List<Atom> allAtoms = new ArrayList<>(peptide.getHalfGraph(prochiralConnection.getFirst(), prochiralConnection.getSecond()));
allAtoms.add(chi1torsion.atom1);
allAtoms.add(chi1torsion.atom2);
// add the backboneHN if requested
if ( residue.HN == null )
throw new NullPointerException("null HN for transition state");
if ( includeHN )
allAtoms.add(residue.HN);
// we don't want to check atoms that are part of the backbone for clashes
// but just in case there are numerical issues we refer to them by index instead of reference
HashSet<Integer> ignoreAtomIndices = new HashSet<>();
ignoreAtomIndices.add(allAtoms.indexOf(chi1torsion.atom1));
ignoreAtomIndices.add(allAtoms.indexOf(chi1torsion.atom2));
ignoreAtomIndices.add(allAtoms.indexOf(chi1torsion.atom3));
if ( includeHN && residue.HN != null )
ignoreAtomIndices.add(allAtoms.indexOf(residue.HN));
// the atoms to rotate for each torsion
List<Atom> chi1atoms = new ArrayList<>(peptide.getHalfGraph(chi1torsion.atom2, chi1torsion.atom3));
List<Atom> chi2atoms = new ArrayList<>(peptide.getHalfGraph(chi2torsion.atom2, chi2torsion.atom3));
// the indices to rotate for chi2
List<Integer> chi2indices = new ArrayList<>();
for (Atom a : chi2atoms)
{
int atomIndex = allAtoms.indexOf(a);
chi2indices.add(atomIndex);
}
// the indices of the torsion for chi2
List<Integer> chi2torsionIndices = new ArrayList<>();
chi2torsionIndices.add(allAtoms.indexOf(chi2torsion.atom1));
chi2torsionIndices.add(allAtoms.indexOf(chi2torsion.atom2));
chi2torsionIndices.add(allAtoms.indexOf(chi2torsion.atom3));
chi2torsionIndices.add(allAtoms.indexOf(chi2torsion.atom4));
// get the index of the transition state oxygen
Integer atomOindex = null;
for (Atom a : allAtoms)
{
if ( a.type1 == 408 )
{
if ( atomOindex != null )
throw new IllegalArgumentException("two TS oxygens found");
atomOindex = allAtoms.indexOf(a);
}
}
if ( atomOindex == null )
throw new NullPointerException("null oxygen index");
// find compatible HNs
// this must be the HN adjacent to the position and the HN must not be hydrogen
// bonded in the sheet; this means that there may be no solution for some residues
List<Integer> HNindices = new ArrayList<>(); // sequence indices of adjacent residues with accessible, non-sheet HNs
for (int k=sequenceIndex-1; k < sequenceIndex+2; k++)
{
if ( k >= 0 && k < sequenceLength && // must be inside the sequence
k != forbiddenIndex && k != forbiddenIndex+1 ) // must not be a hairpin position
{
if ( ( sequenceLength / 2 ) % 2 == 0 ) // n/2 even; n = 8, 12, 16, ...
{
if ( k < forbiddenIndex && k % 2 == 1 ) // before hairpin, odd
HNindices.add(k);
else if ( k > forbiddenIndex + 1 && k % 2 == 0 ) // after hairpin, even
HNindices.add(k);
}
else if ( ( sequenceLength / 2 ) % 2 == 1 ) // n/2 odd; n = 10, 14, 18, ...
{
if ( k < forbiddenIndex && k % 2 == 0 ) // before hairpin, even
HNindices.add(k);
else if ( k > forbiddenIndex + 1 && k % 2 == 1 ) // after hairpin, odd
HNindices.add(k);
}
}
}
//System.out.println(sequenceIndex + " : " + HNindices);
// rotate chi1 and chi2 on a grid
// check for the desired contact
List<List<Double>> returnList = new ArrayList<>();
double stepSize = 360.0 / (TS_GRID_SIZE-1.0);
double chi1 = -180.0;
for (int i=0; i < TS_GRID_SIZE; i++)
{
if ( HNindices.size() == 0 )
break;
List<Atom> thisAtoms = setDihedral(allAtoms, chi1atoms, chi1torsion, chi1);
chi2atoms.clear();
for (Integer index : chi2indices)
chi2atoms.add(thisAtoms.get(index));
Atom atom1 = thisAtoms.get(chi2torsionIndices.get(0));
Atom atom2 = thisAtoms.get(chi2torsionIndices.get(1));
Atom atom3 = thisAtoms.get(chi2torsionIndices.get(2));
Atom atom4 = thisAtoms.get(chi2torsionIndices.get(3));
chi2torsion = new ProtoTorsion(atom1, atom2, atom3, atom4);
double chi2 = -180.0;
for (int j=0; j < TS_GRID_SIZE; j++)
{
// try this rotamer
List<Atom> thisAtoms2 = setDihedral(thisAtoms, chi2atoms, chi2torsion, chi2);
// get the oxygen
Atom atomO = thisAtoms2.get(atomOindex);
// check that the oxygen forms a hydrogen bond to a backbone NH
// check for N-H...O contact distance and angle
boolean acceptable = false;
for (Integer HNindex : HNindices)
{
Residue peptideResidue = peptide.sequence.get(HNindex);
Atom atomN = peptideResidue.N;
Atom atomHN = peptideResidue.HN;
// check distance and angle
double distance = Vector3D.distance(atomO.position, atomHN.position);
if ( distance < Settings.MAXIMUM_HBOND_DISTANCE && distance > 1.25 &&
Molecule.getAngle(atomO.position, atomHN.position, atomN.position) > Settings.MINIMUM_HBOND_ANGLE )
{
acceptable = true;
break;
}
}
// if acceptable, check for clashes and add it to the list of chis to return if it's ok
if ( acceptable )
{
// check for backbone atom clashes
HashSet<Atom> ignoreAtoms = new HashSet<>();
for (Integer index : ignoreAtomIndices)
ignoreAtoms.add(thisAtoms2.get(index));
// check if this rotamer now clashes with the backbone
checking:
for (Atom rotamerAtom : thisAtoms2)
{
if ( ignoreAtoms.contains(rotamerAtom) )
continue;
for (Atom backboneAtom : backboneAtoms)
{
double distance = Vector3D.distance(rotamerAtom.position, backboneAtom.position);
if ( distance < Settings.MINIMUM_INTERATOMIC_DISTANCE )
{
//System.out.println(rotamerAtom.type1);
//System.out.println(peptide.getAtomString(backboneAtom) + " " + distance);
acceptable = false;
break checking;
}
}
}
if (acceptable)
{
List<Double> thisChis = ImmutableList.of(chi1,chi2);
returnList.add(thisChis);
}
}
// increment chi2
chi2 += stepSize;
}
// increment chi1
chi1 += stepSize;
}
//System.out.println("ts: " + returnList);
return ImmutableList.copyOf(returnList);
}
/**
* Determines if the specified residue is up or down. Exceptions will be thrown if this isn't a hairpin or a hairpin position is given.
* @param sequenceLength the length of the sequence
* @param sequenceIndex the index of the residue
* @return true if the substituent is up, false if it is down (the absolute direction is arbitrary but is always consistent)
*/
public static boolean isUp(int sequenceLength, int sequenceIndex)
{
if ( sequenceLength % 2 != 0 )
throw new IllegalArgumentException("expecting even sequence length");
int halfway = sequenceLength/2 - 1; // the index of the first hairpin position (i.e., d-pro)
if ( sequenceIndex == halfway || sequenceIndex == halfway + 1 )
throw new IllegalArgumentException("this is a hairpin position");
if ( sequenceIndex < 0 || sequenceIndex > sequenceLength-1 )
throw new IllegalArgumentException("sequence index exceeds sequence length");
if ( (sequenceLength/2) % 2 == 0 )
{
// n = 12, 16, ...
if ( sequenceIndex < halfway )
{
if ( sequenceIndex % 2 == 0 )
return false;
return true;
}
else
{
if ( sequenceIndex % 2 == 0 )
return true;
return false;
}
}
else if ( (sequenceLength/2) % 2 == 1 )
{
// n = 10, 14, ...
if ( sequenceIndex < halfway )
{
if ( sequenceIndex % 2 == 0 )
return true;
return false;
}
else
{
if ( sequenceIndex % 2 == 0 )
return false;
return true;
}
}
throw new IllegalArgumentException("unreachable");
}
/**
* Takes a poly-gly template and creates interesting pairs of transition states and histidines.
* @param betaSheet the poly-gly template
* @param includeHN whether to include the backboneHN in the sidechain
* @return pairs of TS and histidine rotamers
*/
public static List<Pair<Rotamer,Rotamer>> generateInterestingPairs(Peptide betaSheet, boolean includeHN)
{
// create peptides that have each kind of transition state at each non-hairpin position
List<ProtoAminoAcid> tsTemplates = new ArrayList<>(3);
tsTemplates.add(ProtoAminoAcidDatabase.getTemplate("ts1"));
tsTemplates.add(ProtoAminoAcidDatabase.getTemplate("ts2"));
tsTemplates.add(ProtoAminoAcidDatabase.getTemplate("ts3"));
int sequenceLength = betaSheet.sequence.size();
if ( sequenceLength % 2 != 0 )
throw new IllegalArgumentException("even sequence length expected");
int forbiddenIndex = (sequenceLength/2) - 1;
Map<Peptide,Integer> transitionStatePeptides = new HashMap<>(); // peptides mapped to TS sequence index
for (int i=0; i < sequenceLength; i++)
{
if ( i == forbiddenIndex )
{
i++;
continue;
}
for (ProtoAminoAcid tsTemplate : tsTemplates)
{
Peptide newPeptide = SidechainMutator.mutateSidechain(betaSheet, betaSheet.sequence.get(i), tsTemplate);
transitionStatePeptides.put(newPeptide, i);
}
}
// generate pairs of transition state rotamers and histidine rotamers
List<Pair<Rotamer,Rotamer>> returnList = new ArrayList<>();
for (Peptide tsPeptide : transitionStatePeptides.keySet())
{
// generate transition state rotamers
int TSindex = transitionStatePeptides.get(tsPeptide);
List<List<Double>> TSchis = getTransitionStateChis(tsPeptide, tsPeptide.sequence.get(TSindex), includeHN);
List<Rotamer> transitionStateRotamers = generateRotamers(tsPeptide, tsPeptide.sequence.get(TSindex), includeHN, TSchis);
//System.out.printf("%d TS rotamers generated\n", transitionStateRotamers.size());
// generate histidine rotamers
for (Rotamer TSrotamer : transitionStateRotamers)
{
List<Pair<Rotamer,Rotamer>> rotamerPairs = getHistidineRotamerPairs(tsPeptide, TSrotamer, includeHN);
//System.out.printf(" %d histidine rotamers generated\n", rotamerPairs.size());
returnList.addAll(rotamerPairs);
}
}
return returnList;
}
/** how many points to use to search for histidine rotamers */
public static final int HISTIDINE_GRID_SIZE = 13;
/**
* Finds the chi values for a histidine amino acid, under the constraint that the pi-nitrogen must be near a
* transition state hydroxyl.
* @param inputPeptide the peptide to generate histidine rotamers for
* @param transitionStateRotamer the transition state rotamer to find compatible histidines with
* @param includeHN if true, include the HN in the resulting rotamers
* @return pairs of transition state and histidine rotamers that should be reactive
*/
public static List<Pair<Rotamer,Rotamer>> getHistidineRotamerPairs(Peptide inputPeptide, Rotamer transitionStateRotamer, boolean includeHN)
{
// make a list of where we can put histidines
// the histidine should be on the same face of the hairpin as the transition state
// and can't be where the transition state or hairpin residues are
int sequenceLength = inputPeptide.sequence.size();
int sequenceIndex = transitionStateRotamer.sequenceIndex;
boolean transitionStateIsUp = isUp(sequenceLength, sequenceIndex); // figure out whether the transition state rotamer is up or down
int forbiddenIndex = (sequenceLength/2) - 1;
List<Integer> availablePositions = new ArrayList<>(sequenceLength/2);
for (int i=0; i < sequenceLength; i++)
{
// skip over hairpin positions
if ( i == forbiddenIndex )
{
i++;
continue;
}
// can't place a histidine at the same position as the transition state
if ( i == sequenceIndex )
continue;
// if this residue is on the same side as the transition state, mark the position as open
boolean positionIsUp = isUp(sequenceLength,i);
if ( ( transitionStateIsUp && positionIsUp ) || ( ! transitionStateIsUp && ! positionIsUp ) )
availablePositions.add(i);
}
if ( availablePositions.size() == 0 )
throw new IllegalArgumentException("should have at least one available position");
// make peptides containing a histidine at each valid position, noting that we need both HID and HIE tautomers
// map is from peptides to the sequence index of the histidine
ProtoAminoAcid histidine_HID = ProtoAminoAcidDatabase.getTemplate("histidine_hd");
ProtoAminoAcid histidine_HIE = ProtoAminoAcidDatabase.getTemplate("histidine_he");
Map<Peptide,Integer> histidinePeptides = new HashMap<>();
for (Integer i : availablePositions)
{
histidinePeptides.put(SidechainMutator.mutateSidechain(inputPeptide, inputPeptide.sequence.get(i), histidine_HID), i);
histidinePeptides.put(SidechainMutator.mutateSidechain(inputPeptide, inputPeptide.sequence.get(i), histidine_HIE), i);
}
// get the transition state hydroxyl atoms
Atom atomH = locateSingleAtom(ImmutableSet.of(401), transitionStateRotamer.atoms);
Atom atomO = locateSingleAtom(ImmutableSet.of(402), transitionStateRotamer.atoms);
// for each peptide, rotate the sidechain on a grid and see if it gets close to the transition state hydroxyl
List<Pair<Rotamer,Rotamer>> returnList = new ArrayList<>();
for (Peptide histidinePeptide : histidinePeptides.keySet())
{
// get backbone atoms
// the backbone will include everything but the histidine sidechain
// this makes sense if the peptide is a poly-gly with one transition state
// if we run into glycine Hs, we will have a problem with any other sidechain anyways
int histidineResidueIndex = histidinePeptides.get(histidinePeptide);
List<Atom> backboneAtoms = getBackboneAtoms(histidinePeptide, ImmutableList.of(histidineResidueIndex), includeHN);
// get the rotation axes
Residue residue = histidinePeptide.sequence.get(histidineResidueIndex);
ProtoTorsion chi1torsion = residue.chis.get(0);
ProtoTorsion chi2torsion = residue.chis.get(1);
// get the sidechain positions
Pair<Atom,Atom> prochiralConnection = residue.prochiralConnection;
List<Atom> allAtoms = new ArrayList<>(histidinePeptide.getHalfGraph(prochiralConnection.getFirst(), prochiralConnection.getSecond()));
allAtoms.add(chi1torsion.atom1);
allAtoms.add(chi1torsion.atom2);
// add the backboneHN if requested
if ( residue.HN == null )
throw new NullPointerException("null HN for histidine");
if ( includeHN )
allAtoms.add(residue.HN);
// we don't want to check atoms that are part of the backbone for clashes
// but just in case there are numerical issues we refer to them by index instead of reference
HashSet<Integer> ignoreAtomIndices = new HashSet<>();
ignoreAtomIndices.add(allAtoms.indexOf(chi1torsion.atom1));
ignoreAtomIndices.add(allAtoms.indexOf(chi1torsion.atom2));
ignoreAtomIndices.add(allAtoms.indexOf(chi1torsion.atom3));
if ( includeHN && residue.HN != null )
ignoreAtomIndices.add(allAtoms.indexOf(residue.HN));
// the atoms to rotate for each torsion
List<Atom> chi1atoms = new ArrayList<>(histidinePeptide.getHalfGraph(chi1torsion.atom2, chi1torsion.atom3));
List<Atom> chi2atoms = new ArrayList<>(histidinePeptide.getHalfGraph(chi2torsion.atom2, chi2torsion.atom3));
// the indices to rotate for chi2
List<Integer> chi2indices = new ArrayList<>();
for (Atom a : chi2atoms)
{
int atomIndex = allAtoms.indexOf(a);
chi2indices.add(atomIndex);
}
// the indices of the torsion for chi2
List<Integer> chi2torsionIndices = new ArrayList<>();
chi2torsionIndices.add(allAtoms.indexOf(chi2torsion.atom1));
chi2torsionIndices.add(allAtoms.indexOf(chi2torsion.atom2));
chi2torsionIndices.add(allAtoms.indexOf(chi2torsion.atom3));
chi2torsionIndices.add(allAtoms.indexOf(chi2torsion.atom4));
// get the index of the histidine pi nitrogen
Atom histidinePiNitrogen = locateSingleAtom(ImmutableSet.of(130,126), allAtoms);
Integer histidinePiNitrogenIndex = allAtoms.indexOf(histidinePiNitrogen);
// rotate chi1 and chi2 on a grid and check for the desired contact
double stepSize = 360.0 / (HISTIDINE_GRID_SIZE-1.0);
double chi1 = -180.0;
for (int i=0; i < HISTIDINE_GRID_SIZE; i++)
{
List<Atom> thisAtoms = setDihedral(allAtoms, chi1atoms, chi1torsion, chi1);
chi2atoms.clear();
for (Integer index : chi2indices)
chi2atoms.add(thisAtoms.get(index));
Atom atom1 = thisAtoms.get(chi2torsionIndices.get(0));
Atom atom2 = thisAtoms.get(chi2torsionIndices.get(1));
Atom atom3 = thisAtoms.get(chi2torsionIndices.get(2));
Atom atom4 = thisAtoms.get(chi2torsionIndices.get(3));
chi2torsion = new ProtoTorsion(atom1, atom2, atom3, atom4);
double chi2 = -180.0;
for (int j=0; j < HISTIDINE_GRID_SIZE; j++)
{
// try this rotamer
List<Atom> thisAtoms2 = setDihedral(thisAtoms, chi2atoms, chi2torsion, chi2);
// get the atoms in the putative hydrogen bond
Atom atomN = thisAtoms2.get(histidinePiNitrogenIndex);
// check that the oxygen forms a hydrogen bond to a backbone NH
// check for N-H...O contact distance and angle
double distance = Vector3D.distance(atomN.position, atomH.position);
if ( distance < Settings.MAXIMUM_HBOND_DISTANCE && distance > 1.25 &&
Molecule.getAngle(atomN.position, atomH.position, atomO.position) > 150.0 )
//Molecule.getAngle(atomN.position, atomH.position, atomO.position) > Settings.MINIMUM_HBOND_ANGLE )
{
// check for backbone atom clashes
HashSet<Atom> ignoreAtoms = new HashSet<>();
for (Integer index : ignoreAtomIndices)
ignoreAtoms.add(thisAtoms2.get(index));
// check if this rotamer now clashes with the backbone
boolean acceptable = true;
checking:
for (Atom rotamerAtom : thisAtoms2)
{
if ( ignoreAtoms.contains(rotamerAtom) )
continue;
for (Atom backboneAtom : backboneAtoms)
{
if ( Vector3D.distance(rotamerAtom.position, backboneAtom.position) < Settings.MINIMUM_INTERATOMIC_DISTANCE )
{
acceptable = false;
break checking;
}
}
}
if ( acceptable )
{
// build the actual rotamer
List<List<Double>> thisChis = new ArrayList<>(1);
thisChis.add(ImmutableList.of(chi1, chi2));
Rotamer histidineRotamer = generateRotamers(histidinePeptide, residue, includeHN, thisChis).get(0);
Pair<Rotamer,Rotamer> thisRotamerPair = new Pair<>(transitionStateRotamer, histidineRotamer);
returnList.add(thisRotamerPair);
}
}
// increment chi2
chi2 += stepSize;
}
// increment chi1
chi1 += stepSize;
}
}
// return pairs of interesting rotamers: (transition state rotamer, histidine rotamer)
return returnList;
}
/**
* Trys to add arginine to the specified sheets.
* For every peptide specified, it tries to place an arginine at every non-hairpin, non-transition-state location.
* If the rotamer doesn't clash and contains a hydrogen bond with the transition state it is minimized. All operations
* in this method occur in serial.
* @param sheets the input peptides, which should be on the close contact forcefield, and have one TS and one histidine
* @return peptides with arg on the close contact forcefield
*/
public static List<Peptide> addArginine(List<Peptide> sheets)
{
List<Peptide> returnList = new ArrayList<>();
ProtoAminoAcid arginine = ProtoAminoAcidDatabase.getTemplate("arginine");
for (Peptide sheetPeptide : sheets)
{
// determine what positions are valid for placing arginine
// will only place this on the same side as the transition state
Peptide inputPeptide = HydrogenBondMutator.unmutate(sheetPeptide);
List<Integer> validPositions = new ArrayList<>();
int sequenceLength = inputPeptide.sequence.size();
int forbiddenIndex = (sequenceLength/2) - 1;
Integer TSindex = null;
Atom transitionStateOatom = null;
for (int i=0; i < sequenceLength; i++)
{
Residue residue = inputPeptide.sequence.get(i);
if ( residue.aminoAcid == AminoAcid.TS )
{
if ( TSindex == null )
{
TSindex = i;
for (Atom a : residue.atoms)
{
if ( a.type1 == 408 )
{
if ( transitionStateOatom == null )
transitionStateOatom = a;
else
throw new IllegalArgumentException("duplicate TS O atoms");
}
}
}
else
throw new IllegalArgumentException("two TSs not allowed");
}
}
if ( TSindex == null )
throw new NullPointerException("TS index not found");
if ( transitionStateOatom == null )
throw new NullPointerException("TS O atom not found");