-
Notifications
You must be signed in to change notification settings - Fork 0
/
Rotamer.java
136 lines (124 loc) · 5.16 KB
/
Rotamer.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
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.*;
/**
* Represents a sidechain at a particular position in a given peptide.
* Note: if we start to do amino acids with two substituents instead of one, we'll run into a problem with
* the way rotamer is defined, since a mutation from one amino acid to another won't preserve the backbone.
*/
public class Rotamer implements Immutable
{
/** the atoms in the sidechain */
public final List<Atom> atoms;
/** the position in the sequence of the original peptide */
public final int sequenceIndex;
/** the chi angles this rotamer has */
public final List<Double> chis;
/** the description field copied from Residue */
public final String description;
/**
* Constructor. Just copies the fields in the constructor.
*/
public Rotamer(List<Atom> atoms, int sequenceIndex, List<Double> chis, String description)
{
if ( atoms == null )
throw new NullPointerException("null atoms not allowed");
if ( atoms.size() == 0 )
throw new IllegalArgumentException("every rotamer must have some atoms");
this.atoms = ImmutableList.copyOf(atoms);
if ( sequenceIndex < 0 )
throw new IllegalArgumentException("no negative sequence indices are allowed");
this.sequenceIndex = sequenceIndex;
if ( chis == null )
throw new NullPointerException("null chis not allowed -- use empty list instead");
this.chis = ImmutableList.copyOf(chis);
if ( description == null || description.length() == 0 )
throw new NullPointerException("must have a description");
this.description = description;
}
/**
* Mutates the given peptide to the specified rotamer. This will automatically
* perform the mutation at the correct location in the original peptide. If the identity of the residue in
* startingPeptide is wrong, we fix it first.
* @param startingPeptide the peptide to make the mutation on
* @param rotamer the rotamer we want in the resulting peptide
* @return the mutated peptide
*/
public static Peptide reconstitute(Peptide startingPeptide, Rotamer rotamer)
{
// mutate residue identity if necessary
Peptide peptide = startingPeptide;
if ( rotamer.sequenceIndex > startingPeptide.contents.size() - 1 )
throw new IllegalArgumentException("sequence index out of bounds");
Residue residue = startingPeptide.sequence.get(rotamer.sequenceIndex);
if ( ! rotamer.description.equals(residue.description) )
{
ProtoAminoAcid protoAminoAcid = ProtoAminoAcidDatabase.getTemplate(rotamer.description);
peptide = SidechainMutator.mutateSidechain(peptide, residue, protoAminoAcid);
residue = peptide.sequence.get(rotamer.sequenceIndex);
}
// adjust chis (even if the chis are identical, we adjust anyways)
if ( rotamer.chis.size() != residue.chis.size() )
throw new IllegalArgumentException("chi size mismatch for " + rotamer.description + " at index " + rotamer.sequenceIndex);
if (rotamer.chis.size() > 0)
peptide = RotamerMutator.setChis(peptide, residue, rotamer.chis);
return peptide;
}
/**
* Mutates the given peptide to the specified rotamers. No checks.
* @param startingPeptide the starting peptide
* @param rotamerList the rotamers to use
* @return the mutated peptide
*/
public static Peptide reconstitute(Peptide startingPeptide, List<Rotamer> rotamerList)
{
Peptide returnPeptide = startingPeptide;
Set<Integer> checkSet = new HashSet<>();
for (Rotamer r : rotamerList)
{
if ( checkSet.contains(r.sequenceIndex) )
throw new IllegalArgumentException("duplicate sequence index");
else
checkSet.add(r.sequenceIndex);
}
for (Rotamer r : rotamerList)
returnPeptide = reconstitute(returnPeptide, r);
return returnPeptide;
}
@Override
public boolean equals(Object obj)
{
if ( obj == null )
return false;
if ( obj == this )
return true;
if ( !(obj instanceof Rotamer) )
return false;
Rotamer r = (Rotamer)obj;
if ( sequenceIndex == r.sequenceIndex &&
chis.equals(r.chis) &&
description.equals(r.description) )
return true;
return false;
}
@Override
public int hashCode()
{
return Objects.hash(sequenceIndex, chis, description);
}
@Override
public String toString()
{
String chiString = "";
for (Double d : chis)
chiString += String.format("%.1f, ", d);
if ( chiString.length() > 0 )
chiString = chiString.substring(0, chiString.length()-2);
return String.format("[%d] %s: [%s]", sequenceIndex, description, chiString);
}
}