-
Notifications
You must be signed in to change notification settings - Fork 36
/
Modification.java
372 lines (324 loc) · 11.8 KB
/
Modification.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
package edu.ucsd.msjava.msutil;
import edu.ucsd.msjava.msgf.NominalMass;
import java.util.Comparator;
import java.util.HashMap;
/**
* A class representing a modification.
*
* @author sangtaekim
*/
public class Modification {
/**
* Threshold to use when determining if two modifications have the same mass
*/
public static final double MOD_MASS_COMPARISON_THRESHOLD = 0.01;
private final String name;
private final double mass;
private final int nominalMass;
private String modId = "";
/**
* Empirical formula or modification mass of this modification
* This is null in certain instances (e.g. custom amino acid residue or non-standard modifications)
*/
private Composition composition;
private Modification(String name, Composition composition) {
this.name = name;
this.mass = composition.getAccurateMass();
this.nominalMass = composition.getNominalMass();
this.composition = composition;
}
private Modification(String name, double mass) {
this.name = name;
this.mass = mass;
this.nominalMass = NominalMass.toNominalMass((float) mass);
}
/**
* Modification name
*
* @return
*/
public String getName() {
return name;
}
/**
* Modification mass (as a float)
*
* @return
*/
public float getMass() {
return (float) mass;
}
/**
* Modification mass (as a double)
*
* @return
*/
public double getAccurateMass() {
return mass;
}
/**
* Modification mass (as an integer)
*
* @return
*/
public int getNominalMass() {
return nominalMass;
}
/**
* Modification identifier (used in mzid output)
*
* @return
*/
public String getModId() {
return modId;
}
/**
* Empirical formula or modification mass of this modification
* This is null in certain instances (e.g. custom amino acid residue or non-standard modifications)
*/
public Composition getComposition() {
if (composition == null)
return null;
return composition;
}
/**
* List of default modifications
*
* @return
*/
public static Modification[] getDefaultModList() {
return defaultModList;
}
/**
* Looks for an existing mod with the given name
*
* @param name Modification name (case-sensitive); getAminoAcidSetFromXMLFile uses 'residueStr + " " + modMass'
* @param mass Monoisotopic mass
* @return True if an existing mod exists, and the mass is different (by more than 0.001 Da); otherwise false
*/
public static boolean isModConflict(String name, double mass) {
return isModConflict(name, mass, MOD_MASS_COMPARISON_THRESHOLD);
}
/**
* Looks for an existing mod with the given name
*
* @param name Modification name (case-sensitive); getAminoAcidSetFromXMLFile uses 'residueStr + " " + modMass'
* @param mass Monoisotopic mass
* @return True if an existing mod exists, and the mass is different (by more than massTolerance Da); otherwise false
*/
public static boolean isModConflict(String name, double mass, double massTolerance) {
Modification existingMod = modTable.get(name);
if (existingMod == null)
return false;
if (Math.abs(existingMod.mass - mass) > massTolerance)
return true;
return false;
}
/**
* Looks for an existing mod with the given name
*
* @param name Modification name (case-sensitive)
* @param composition Modification empirical formula
* @return True if an existing mod exists, and the mass is different (by more than 0.001 Da); otherwise false
*/
public static boolean isModConflict(String name, Composition composition) {
return isModConflict(name, composition.getAccurateMass(), MOD_MASS_COMPARISON_THRESHOLD);
}
/**
* Looks for an existing mod with the given name
*
* @param name Modification name (case-sensitive)
* @param composition Modification empirical formula
* @return True if an existing mod exists, and the mass is different (by more than massTolerance Da); otherwise false
*/
public static boolean isModConflict(String name, Composition composition, double massTolerance) {
return isModConflict(name, composition.getAccurateMass(), massTolerance);
}
/**
* Register a modification by name and mass
*
* @param modName Modification name (though getAminoAcidSetFromXMLFile uses 'residueStr + " " + modMass')
* @param mass Monoisotopic mass
* @return
*/
public static Modification register(String modName, double mass) {
Modification mod = new Modification(modName, mass);
setModIdentifier(mod);
modTable.put(modName, mod);
return mod;
}
/**
* Register a modification by name and composition
*
* @param name Modification name
* @param composition Modification empirical formula
* @return
*/
public static Modification register(String name, Composition composition) {
Modification mod = new Modification(name, composition);
setModIdentifier(mod);
modTable.put(name, mod);
return mod;
}
/**
* Set the mod identifiers for any mods that do not have one.
* This allows user-specified modifications to take precedence over built-in default modifications
*/
public static void setModIdentifiers() {
for (Modification mod : modTable.values()) {
if (mod.getModId().equals("")) {
setModIdentifier(mod);
}
}
}
/**
* Generate a unique identifier for the modification to be used in mzid output (in peptide and peptideEvidence IDs)
*
* @param mod
*/
private static void setModIdentifier(Modification mod) {
double mass = mod.getAccurateMass();
String baseId = "";
if (mass >= 0) {
baseId += "+";
}
baseId += Math.round(mod.getAccurateMass());
String id = baseId;
int count = 0;
while (true) {
boolean foundConflict = false;
for (Modification existing : modTable.values()) {
if (existing.modId.equals(id)) {
// massMatch: if composition is not null, match on composition; otherwise, match on double-precision mass.
boolean massMatch = Composition.equals(existing.composition, mod.composition);
if (existing.composition == null) {
massMatch = existing.mass == mod.mass;
}
// If a modification has the same name and composition (or modification mass), give it the same identifier
boolean isFullMassMatch = existing.name.equals(mod.name) && massMatch;
if (!isFullMassMatch) {
foundConflict = true;
break;
}
}
}
if (!foundConflict) {
break;
}
id = baseId + "#" + (++count);
}
mod.modId = id;
}
public static Modification getModByName(String name) {
return modTable.get(name);
}
public static final Modification Carbamidomethyl = new Modification("Carbamidomethyl", new Composition(2, 3, 1, 1, 0));
public static final Modification Carboxymethyl = new Modification("Carboxymethyl", new Composition(2, 2, 2, 0, 0));
public static final Modification NIPCAM = new Modification("NIPCAM", new Composition(5, 9, 1, 1, 0));
public static final Modification Oxidation = new Modification("Oxidation", new Composition(0, 0, 0, 1, 0));
public static final Modification Phospho = new Modification("Phospho", Composition.getMass("HO3P"));
public static final Modification Methyl = new Modification("Methyl", new Composition(1, 2, 0, 0, 0));
public static final Modification PyroGluQ = new Modification("Gln->pyro-Glu", Composition.getMass("H-3N-1")); // Pyro-glu from Q
public static final Modification PyroGluE = new Modification("Glu->pyro-Glu", Composition.getMass("H-2O-1")); // Pyro-glu from E
public static final Modification Carbamyl = new Modification("Carbamyl", new Composition(1, 1, 1, 1, 0));
public static final Modification Acetyl = new Modification("Acetyl", new Composition(2, 2, 0, 1, 0));
public static final Modification PyroCarbamidomethyl = new Modification("Pyro-carbamidomethyl", Composition.getMass("H-3N-1"));
// static member
private static final Modification[] defaultModList =
{
Carbamidomethyl,
Carboxymethyl,
NIPCAM,
Oxidation,
Phospho,
Methyl,
PyroGluQ,
PyroGluE,
Carbamyl,
Acetyl,
PyroCarbamidomethyl
};
/**
* Keys are modification names
* Values are modification details
*/
private static final HashMap<String, Modification> modTable;
static {
modTable = new HashMap<>();
for (Modification mod : defaultModList) {
modTable.put(mod.getName(), mod);
}
}
public enum Location {
Anywhere,
N_Term,
C_Term,
Protein_N_Term,
Protein_C_Term,
}
/**
* A class representing the modification instance.
*
* @author sangtaekim
*/
public static class Instance {
private final Modification mod;
private final char residue; // if null, no amino acid specificity
private Location location; // N_Term, C_Term, Anywhere
private boolean isFixedModification = false;
public Instance(Modification mod, char residue, Location location) {
this.mod = mod;
this.residue = residue;
this.location = location;
}
public Instance(Modification mod, char residue) {
this(mod, residue, Location.Anywhere);
}
public Instance fixedModification() {
isFixedModification = true;
return this;
}
public Modification getModification() {
return mod;
}
public char getResidue() {
return residue;
}
public Location getLocation() {
return location;
}
public boolean isFixedModification() {
return isFixedModification;
}
public String toString() {
return mod.getName() + " " +
residue + " " +
location + ", " +
(isFixedModification ? "Fixed (static)" : "Variable (dynamic)");
}
@Override
public boolean equals(Object obj) {
if (obj instanceof Instance) {
Instance other = (Instance) obj;
return this.mod == other.mod &&
this.residue == other.residue &&
this.location == other.location &&
this.isFixedModification == other.isFixedModification;
}
return false;
}
@Override
public int hashCode() {
return mod.getName().hashCode() +
new Character(residue).hashCode() +
location.hashCode() +
new Boolean(isFixedModification).hashCode();
}
}
public static class MassComparator implements Comparator<Modification> {
@Override
public int compare(Modification a, Modification b) {
return Double.compare(a.getAccurateMass(), b.getAccurateMass());
}
}
}