-
Notifications
You must be signed in to change notification settings - Fork 0
/
MSSearcher.cs
479 lines (447 loc) · 19 KB
/
MSSearcher.cs
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
/*
* Copyright 2013 Olivier Caron-Lizotte
* olivierlizotte@gmail.com
* Licensed under the MIT license: <http://www.opensource.org/licenses/mit-license.php>
*/
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Proteomics.Utilities;
namespace Trinity
{
/// <summary>
/// Replicates (lists precursors for a given replicate (aka all fractions)
/// </summary>
public class clReplicate : GraphML_Node
{
public int id;
public GraphML_List<Precursor> precursors;
public clReplicate()
{
precursors = new GraphML_List<Precursor>();
}
public void Add(Precursor precursor)
{
precursors.Add(precursor);
}
public double ProbabilityScore(Peptide peptide)
{
double score = 0;
foreach (Precursor precursor in precursors)
score += /*(1 - score) * */precursor.ProbabilityScore(peptide, true);
return score;
}
public double Rt()
{
double tmpRt = 0;
int nbRep = 0;
foreach (Precursor precursor in precursors)
{
nbRep++;
tmpRt += precursor.Track.RT;
}
return tmpRt / (double)nbRep;
}
public double Mz()
{
double tmpMz = 0;
int nbRep = 0;
foreach (Precursor precursor in precursors)
{
nbRep++;
tmpMz += precursor.Track.MZ;
}
return tmpMz / (double)nbRep;
}
}
/// <summary>
/// Condition (list of replicates) were a replicate is found
/// </summary>
public class clCondition : GraphML_Node
{
public int id;
public GraphML_List<clReplicate> replicates;
public clCondition()
{
replicates = new GraphML_List<clReplicate>();
}
public clCondition(int nbReplicates)
{
replicates = new GraphML_List<clReplicate>();
for(int i = 0; i < nbReplicates; i++)
replicates.Add(null);
}
public void Add(Precursor precursor)
{
if (replicates[precursor.sample.PROJECT.REPLICATE - 1] == null)
replicates[precursor.sample.PROJECT.REPLICATE - 1] = new clReplicate();
replicates[precursor.sample.PROJECT.REPLICATE - 1].Add(precursor);
}
public double Reproducibility()
{
int nbElem = 0;
foreach (clReplicate replicate in replicates)
if (replicate != null)
nbElem++;
return nbElem / (double) replicates.Count;
}
public double ProbabilityScore(Peptide peptide)
{
double score = 0;
foreach (clReplicate replicate in replicates)
if (replicate != null)
score += /*(1 - score) * */replicate.ProbabilityScore(peptide);
return score;
}
public double Rt()
{
double tmpRt = 0;
int nbRep = 0;
foreach (clReplicate replicate in replicates)
if (replicate != null)
{
nbRep++;
tmpRt += replicate.Rt();
}
return tmpRt / (double)nbRep;
}
public double Mz()
{
double tmpMz = 0;
int nbRep = 0;
foreach (clReplicate replicate in replicates)
if (replicate != null)
{
nbRep++;
tmpMz += replicate.Mz();
}
return tmpMz / (double)nbRep;
}
}
/// <summary>
/// A cluster is a group of ions, corresponding to the same peptide sequence, seen accross multiple samples
/// </summary>
public class Cluster : GraphML_Node
{
public GraphML_List<clCondition> conditions;
private Dictionary<int, List<int>> samples;
public Cluster()
{
conditions = new GraphML_List<clCondition>();
}
public Cluster(Dictionary<int, List<int>> samples)
{
this.conditions = new GraphML_List<clCondition>();
foreach (int c in samples.Keys)
this.conditions.Add(null);
this.samples = samples;
}
public double Reproducibility()
{
//Reproducibility is average number of replicates for conditions where seen
double cumul = 0;
int nbSeen = 0;
foreach (clCondition condition in conditions)
if (condition != null)
{
double coverage = condition.Reproducibility();
if (coverage > 0)
{
cumul += coverage;
nbSeen++;
}
}
return (cumul / (double)nbSeen);
}//*/
public double GetPrecursorMassError(Peptide peptide)
{
//Reproducibility is average number of replicates for conditions where seen
double cumul = 0;
int nbSeen = 0;
foreach (clCondition condition in conditions)
if(condition != null)
foreach(clReplicate replicate in condition.replicates)
foreach (Precursor precursor in replicate.precursors)
{
cumul += peptide.MonoisotopicMass - precursor.Mass;
nbSeen++;
}
return (cumul / (double)nbSeen);
}//*/
public void Add(Precursor precursor)
{
if (conditions[precursor.sample.PROJECT.CONDITION - 1] == null)
conditions[precursor.sample.PROJECT.CONDITION - 1] = new clCondition(samples[precursor.sample.PROJECT.CONDITION].Count);
conditions[precursor.sample.PROJECT.CONDITION - 1].Add(precursor);
//score = -1;
}
public double ProbabilityScore(Peptide peptide)
{
double score = 0;
int nbCond = 0;
foreach (clCondition condition in conditions)
if (condition != null)
{
double tmp = condition.ProbabilityScore(peptide);
if (tmp > 0)
{
score += tmp;
nbCond++;
}
}
if (nbCond > 0)
return score;// / (double)nbCond;
else
return 0.0;
}
public double Rt()
{
double tmpRt = 0;
int nbRep = 0;
foreach (clCondition condition in conditions)
if (condition != null)
{
nbRep++;
tmpRt += condition.Rt();
}
return tmpRt / (double)nbRep;
}
public double Mz()
{
double tmpMz = 0;
int nbRep = 0;
foreach (clCondition condition in conditions)
if (condition != null)
{
nbRep++;
tmpMz += condition.Mz();
}
return tmpMz / (double)nbRep;
}
public List<PeptideSpectrumMatch> OptimizedBestPsms()
{
List<PeptideSpectrumMatch> matches = new List<PeptideSpectrumMatch>();
foreach (clCondition condition in conditions)
if(condition != null)
foreach (clReplicate replicate in condition.replicates)
foreach (Precursor precursor in replicate.precursors)
foreach (PeptideSpectrumMatch psm in precursor.OptimizedBestPsms())
if (matches.Count == 0 || matches[0].ProbabilityScore() == psm.ProbabilityScore())
matches.Add(psm);
else
if (matches[0].ProbabilityScore() < psm.ProbabilityScore())
{
matches.Clear();
matches.Add(psm);
}
return matches;
}
public Peptide ComputeBestPeptide()
{
Dictionary<Peptide, double> peptides = new Dictionary<Peptide, double>();
foreach (clCondition condition in conditions)
if(condition != null)
foreach (clReplicate replicate in condition.replicates)
foreach (Precursor precursor in replicate.precursors)
foreach (PeptideSpectrumMatch psm in precursor.OptimizedBestPsms())
if(!peptides.ContainsKey(psm.Peptide))
peptides.Add(psm.Peptide, ProbabilityScore(psm.Peptide));
double best = -1;
Peptide bestPeptide = null;
foreach(Peptide key in peptides.Keys)
if (peptides[key] > best || (peptides[key] == best && bestPeptide.Decoy))
{
best = peptides[key];
bestPeptide = key;
}
return bestPeptide;
}
/*
public PeptideSpectrumMatch OptimizedBestPsm(Peptide peptide, bool checkMods)
{
PeptideSpectrumMatch bestPsm = null;
foreach (clCondition condition in conditions)
foreach (clReplicate replicate in condition.replicates)
foreach (Precursor precursor in replicate.precursors)
{
PeptideSpectrumMatch match = precursor.OptimizedBestPsm(peptide, checkMods);
if(bestPsm == null || bestPsm.OptimizedScore() < match.OptimizedScore())
bestPsm = match;
}
return bestPsm;
}//*/
public Precursor OptimizedBestPrecursor(Peptide peptide, bool checkMods = false)
{
double score = 0;
Precursor best = null;
foreach (clCondition condition in conditions)
if(condition != null)
foreach (clReplicate replicate in condition.replicates)
foreach (Precursor precursor in replicate.precursors)
{
double tmpScore = precursor.ProbabilityScore(peptide, checkMods);
if (tmpScore > score)
{
score = tmpScore;
best = precursor;
}
}
return best;
}
}
/// <summary>
/// Methods to cluster (aggregate) precursors seen in more than one sample
/// </summary>
public class MSSearcher
{
public DBOptions options;
public Dictionary<int, List<int>> samples = new Dictionary<int, List<int>>();
public Precursors precursors = new Precursors();
public MSSearcher(DBOptions options, Samples project)
{
this.options = options;
foreach(Sample sample in project)
{
if (!samples.ContainsKey(sample.PROJECT.CONDITION))
samples.Add(sample.PROJECT.CONDITION, new List<int>());
if (!samples[sample.PROJECT.CONDITION].Contains(sample.PROJECT.REPLICATE))
samples[sample.PROJECT.CONDITION].Add(sample.PROJECT.REPLICATE);
}
}
public void CumulPsm(List<Precursor> matches)
{
foreach (Precursor precursor in matches)
{
Sample entry = precursor.sample;
precursors.Add(precursor);//Remove blank psm (unmatched spectrum/query duos)
/* if (!samples.ContainsKey(entry.PROJECT.CONDITION))
samples.Add(entry.PROJECT.CONDITION, new List<int>());
if (!samples[entry.PROJECT.CONDITION].Contains(entry.PROJECT.REPLICATE))
samples[entry.PROJECT.CONDITION].Add(entry.PROJECT.REPLICATE);//*/
}
}
private int NbCommonSequences(Precursor a, Precursor b)
{
int common = 0;
foreach (PeptideSpectrumMatch psmA in a.psms)
{
bool seen = true;
foreach (PeptideSpectrumMatch psmB in b.psms)
if (psmA.Peptide.IsSamePeptide(psmB.Peptide, true))
{
seen = true;
break;
}
if (seen)
common++;
}
return common;
}
private double Score(Precursor a, Precursor b)
{
if (a.sample != b.sample && NbCommonSequences(a, b) > 0)
{
//Zero based score
//double tmp = Math.Abs(MassTolerance.CalculateMassError(a.Track.MZ, b.Track.MZ, MassToleranceUnits.ppm) / options.MzTol);
double tmp = Math.Abs(Numerics.MzDifference(a.Track.MZ, b.Track.MZ, options.precursorMassTolerance.Units)) / options.precursorMassTolerance.Value;
if (tmp < 1)
{
tmp = 0.2 * (2 * tmp +
Math.Abs(a.Track.RT - b.Track.RT) / options.ComputedRetentionTimeDiff + //TODO check if it is in seconds?
(a.Charge == b.Charge ? 0 : 1) +
0.1 * Math.Abs(Math.Log10(a.Track.INTENSITY) - Math.Log10(b.Track.INTENSITY)));
if (tmp < 1)
return 1 - tmp;
}
}
return 0;
}
public static int DescendingScoreComparison(Query left, Query right)
{
return - left.Score.CompareTo(right.Score);
}
public static int DescendingProteinScoreComparison(PeptideSpectrumMatch left, PeptideSpectrumMatch right)
{
return -left.ProteinScore.CompareTo(right.ProteinScore);
}
public GraphML_List<Cluster> Search(Precursors precursors, bool runCluster)
{
options.ConSole.WriteLine("Grouping precursors based on common features...");
precursors.Sort(Precursor.CompareProbabilityScore);
GraphML_List<Cluster> clusters = new GraphML_List<Cluster>();
bool[] done = new bool[precursors.Count];
for(int i = 0; i < done.Length; i++)
done[i] = false;
//Step 1 : Regroup psms based on mz/rt/intensity/Sequence proximity score (ProteoProfile Code)
for (int i = 0; i < precursors.Count; i++)
{
if(!done[i])
{
Cluster group = new Cluster(samples);
group.Add(precursors[i]);
if (runCluster)
{
for (int j = i + 1; j < precursors.Count; j++)
{
if (!done[j] && precursors[i].sample != precursors[j].sample)
{
double score = Score(precursors[i], precursors[j]);
//TODO Implement ProteoProfile Clustering algorithm, or anything on the litterature, as long as its backed by the scoring function
if (score > 0.75)//TODO Should we put a threshold here? Can it be computed dynamically?
{
group.Add(precursors[j]);
done[j] = true;
}
}
}
}
clusters.Add(group);
}
}
options.ConSole.WriteLine("Created " + clusters.Count + " clusters");
return clusters;
//TODO I should not use psms in more than one cluster...
}
public static void Export(string filename, List<Precursor> precursors)
{
vsCSVWriter writer = new vsCSVWriter(filename);
writer.AddLine("Index.Mz,Rt,Precursor Mz,Charge,Most Intense Charge,Precursor Mass,Peptide Mass,Sequence,Modified Sequence,Precursor Score,Product Score,Intensity Score,Final Score,Precursor Mass Error,Decoy?,Protein Score");
foreach (Precursor precursor in precursors)
{
string line = precursor.INDEX + "," + precursor.Track.RT + "," + precursor.Track.MZ + "," + precursor.Charge + "," + precursor.GetMostIntenseCharge() + "," + precursor.Mass + ",";
PeptideSpectrumMatch match = precursor.OptimizedBestPsm();
if (match != null)
line += match.Peptide.MonoisotopicMass + "," + match.Peptide.BaseSequence + "," + match.Peptide.Sequence + "," + match.PrecursorScore + "," + match.ProductScore + "," + match.IntensityScore + "," + precursor.ProbabilityScore(match.Peptide) + "," +
match.PrecursorMzError + "," + match.Decoy + "," + match.ProteinScore;
writer.AddLine(line);
}
writer.WriteToFile();
}
public static void Export(string filename, IEnumerable<Query> queries)
{
vsCSVWriter writer = new vsCSVWriter(filename);
writer.AddLine("Spectrum Precursor Mz,Rt,Charge,BaseSequence,Sequence,Precursor Score,Product Score,Intensity Score,Final Score,Precursor Mass Error,Decoy?,Protein Score");
foreach (Query query in queries)
{
string line = query.spectrum.PrecursorMZ + "," + query.precursor.Track.RT + "," + query.precursor.Charge + ",";
PeptideSpectrumMatch match = query.precursor.OptimizedBestPsm();
if(match != null)
line += match.Peptide.BaseSequence + "," + match.Peptide.Sequence + "," + match.PrecursorScore + "," + match.ProductScore + "," + match.IntensityScore + "," + query.ScoreFct(match.Peptide) + "," +
match.PrecursorMzError + "," + match.Decoy + "," + match.ProteinScore;
writer.AddLine(line);
}
writer.WriteToFile();
}
public static void Export(string filename, List<PeptideSpectrumMatch> psms)
{
vsCSVWriter writer = new vsCSVWriter(filename);
writer.AddLine("Mz,Rt,Charge,Sequence,Modifications,Precursor Score,Product Score,Intensity Score,Final Score,Precursor Mass Error,Decoy?,Protein Score");
foreach (PeptideSpectrumMatch psm in psms)
writer.AddLine(psm.Query.precursor.Track.MZ +","+ psm.Query.spectrum.RetentionTimeInMin + ","+psm.Query.precursor.Charge +
"," + psm.Peptide.BaseSequence + "," + psm.Peptide.Sequence + "," + psm.PrecursorScore + "," + psm.ProductScore + "," + psm.IntensityScore + "," + psm.ProbabilityScore() + "," +
psm.PrecursorMzError + "," + psm.Decoy + "," + psm.ProteinScore);
writer.WriteToFile();
}
}
}