-
Notifications
You must be signed in to change notification settings - Fork 14
/
PsmReader.cs
486 lines (421 loc) · 22.7 KB
/
PsmReader.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
480
481
482
483
484
485
486
using FlashLFQ;
using System;
using System.Collections.Generic;
using System.Globalization;
using System.IO;
using System.Linq;
namespace Util
{
enum PsmFileType { MetaMorpheus, Morpheus, MaxQuant, PeptideShaker, Generic, Unknown }
public class PsmReader
{
private static int _fileNameCol;
private static int _baseSequCol;
private static int _fullSequCol;
private static int _monoMassCol;
private static int _msmsRetnCol;
private static int _chargeStCol;
private static int _protNameCol;
private static int _decoyCol;
private static int _qValueCol;
private static int _qValueNotchCol;
// optional columns
private static int _geneNameCol;
private static int _organismCol;
private static Dictionary<string, double> _modSequenceToMonoMass;
private static Dictionary<string, ProteinGroup> allProteinGroups;
private static readonly Dictionary<PsmFileType, string[]> delimiters = new Dictionary<PsmFileType, string[]>
{
{ PsmFileType.MetaMorpheus, new string[] { "|", " or " } },
{ PsmFileType.Morpheus, new string[] { ";" } },
{ PsmFileType.MaxQuant, new string[] { ";" } },
{ PsmFileType.Generic, new string[] { ";" } },
{ PsmFileType.PeptideShaker, new string[] { ", " } },
};
public static List<Identification> ReadPsms(string filepath, bool silent, List<SpectraFileInfo> rawfiles)
{
if (_modSequenceToMonoMass == null)
{
_modSequenceToMonoMass = new Dictionary<string, double>();
}
if (allProteinGroups == null)
{
allProteinGroups = new Dictionary<string, ProteinGroup>();
}
var rawFileDictionary = rawfiles.ToDictionary(p => p.FilenameWithoutExtension, v => v);
List<Identification> ids = new List<Identification>();
PsmFileType fileType = PsmFileType.Unknown;
if (!silent)
{
Console.WriteLine("Opening PSM file " + filepath);
}
StreamReader reader;
try
{
reader = new StreamReader(filepath);
}
catch (Exception e)
{
if (!silent)
{
Console.WriteLine("Error reading file " + filepath + "\n" + e.Message);
}
return new List<Identification>();
}
int lineNum = 0;
while (reader.Peek() > 0)
{
string line = reader.ReadLine();
lineNum++;
try
{
if (lineNum == 1)
{
fileType = GetFileTypeFromHeader(line);
if (fileType == PsmFileType.Unknown)
{
throw new Exception("Could not interpret PSM header labels from file: " + filepath);
}
}
else
{
var param = line.Split('\t');
// only quantify PSMs below 1% FDR
if (fileType == PsmFileType.MetaMorpheus && double.Parse(param[_qValueCol], CultureInfo.InvariantCulture) > 0.01)
{
break;
}
else if (fileType == PsmFileType.Morpheus && double.Parse(param[_qValueCol], CultureInfo.InvariantCulture) > 1.00)
{
break;
}
// only quantify PSMs below 1% notch FDR
if (fileType == PsmFileType.MetaMorpheus && double.Parse(param[_qValueNotchCol], CultureInfo.InvariantCulture) > 0.01)
{
continue;
}
// skip decoys
if ((fileType == PsmFileType.MetaMorpheus || fileType == PsmFileType.Morpheus) &&
param[_decoyCol].Contains("D"))
{
continue;
}
// spectrum file name
string fileName = param[_fileNameCol];
// base sequence
string baseSequence = param[_baseSequCol];
// modified sequence
string modSequence = param[_fullSequCol];
// skip ambiguous sequence in MetaMorpheus output
if (fileType == PsmFileType.MetaMorpheus && (modSequence.Contains(" or ") || modSequence.Contains("|") || modSequence.ToLowerInvariant().Contains("too long")))
{
continue;
}
// monoisotopic mass
if (double.TryParse(param[_monoMassCol], NumberStyles.Number, CultureInfo.InvariantCulture, out double monoisotopicMass))
{
if (_modSequenceToMonoMass.TryGetValue(modSequence, out double storedMonoisotopicMass))
{
if (storedMonoisotopicMass != monoisotopicMass)
{
if (!silent)
{
Console.WriteLine("Caution! PSM with sequence " + modSequence + " at line " +
lineNum + " could not be read; " +
"a peptide with the same modified sequence but a different monoisotopic mass has already been added");
}
continue;
}
}
else
{
_modSequenceToMonoMass.Add(modSequence, monoisotopicMass);
}
}
else
{
if (!silent)
{
Console.WriteLine("PSM with sequence " + modSequence + " at line " +
lineNum + " could not be read; " +
"monoisotopic mass was not interpretable: \"" + param[_monoMassCol] + "\"");
}
continue;
}
// retention time
if (double.TryParse(param[_msmsRetnCol], out double ms2RetentionTime))
{
if (fileType == PsmFileType.PeptideShaker)
{
// peptide shaker RT is in seconds - convert to minutes
ms2RetentionTime = ms2RetentionTime / 60.0;
}
if (ms2RetentionTime < 0)
{
if (!silent)
{
Console.WriteLine("Caution! PSM with sequence " + modSequence + " at line " +
lineNum + " could not be read; retention time was negative");
}
continue;
}
}
else
{
if (!silent)
{
Console.WriteLine("PSM with sequence " + modSequence + " at line " +
lineNum + " could not be read; " +
"retention time was not interpretable: \"" + param[_msmsRetnCol] + "\"");
}
continue;
}
// charge state
int chargeState;
if (fileType == PsmFileType.PeptideShaker)
{
string chargeStringNumbersOnly = new String(param[_chargeStCol].Where(Char.IsDigit).ToArray());
if (string.IsNullOrWhiteSpace(chargeStringNumbersOnly))
{
if (!silent)
{
Console.WriteLine("PSM with sequence " + modSequence + " at line " +
lineNum + " could not be read; " +
"charge state was not interpretable: \"" + param[_chargeStCol] + "\"");
}
continue;
}
else
{
if (!int.TryParse(chargeStringNumbersOnly, out chargeState))
{
if (!silent)
{
Console.WriteLine("PSM with sequence " + modSequence + " at line " +
lineNum + " could not be read; " +
"charge state was not interpretable: \"" + param[_chargeStCol] + "\"");
}
continue;
}
}
}
else
{
if (!double.TryParse(param[_chargeStCol], out double chargeStateDouble))
{
if (!silent)
{
Console.WriteLine("PSM with sequence " + modSequence + " at line " +
lineNum + " could not be read; " +
"charge state was not interpretable: \"" + param[_chargeStCol] + "\"");
}
continue;
}
chargeState = (int)chargeStateDouble;
}
// protein groups
// use all proteins listed
List<ProteinGroup> proteinGroups = new List<ProteinGroup>();
var proteins = param[_protNameCol].Split(delimiters[fileType], StringSplitOptions.None);
string[] genes = null;
if (_geneNameCol >= 0)
{
genes = param[_geneNameCol].Split(delimiters[fileType], StringSplitOptions.None);
}
string[] organisms = null;
if (_organismCol >= 0)
{
organisms = param[_organismCol].Split(delimiters[fileType], StringSplitOptions.None);
}
for (int pr = 0; pr < proteins.Length; pr++)
{
string proteinName = proteins[pr];
string gene = "";
string organism = "";
if (genes != null)
{
if (genes.Length == 1)
{
gene = genes[0];
}
else if (genes.Length == proteins.Length)
{
gene = genes[pr];
}
else if (proteins.Length == 1)
{
gene = param[_geneNameCol];
}
}
if (organisms != null)
{
if (organisms.Length == 1)
{
organism = organisms[0];
}
else if (organisms.Length == proteins.Length)
{
organism = organisms[pr];
}
else if (proteins.Length == 1)
{
organism = param[_organismCol];
}
}
if (allProteinGroups.TryGetValue(proteinName, out ProteinGroup pg))
{
proteinGroups.Add(pg);
}
else
{
ProteinGroup newPg = new ProteinGroup(proteinName, gene, organism);
allProteinGroups.Add(proteinName, newPg);
proteinGroups.Add(newPg);
}
}
// get file name and look up file name object
var fileNameNoExt = Path.GetFileNameWithoutExtension(fileName);
if (!rawFileDictionary.TryGetValue(fileNameNoExt, out SpectraFileInfo spectraFileInfoToUse))
{
// skip PSMs for files with no spectrum data input
continue;
}
// construct id
var ident = new Identification(spectraFileInfoToUse, baseSequence, modSequence, monoisotopicMass, ms2RetentionTime, chargeState, proteinGroups);
ids.Add(ident);
}
}
catch (Exception)
{
if (!silent)
{
Console.WriteLine("Problem reading line " + lineNum + " of the identification file");
}
return new List<Identification>();
}
}
reader.Close();
if (!silent)
{
Console.WriteLine("Done reading PSMs; found " + ids.Count);
}
return ids;
}
private static PsmFileType GetFileTypeFromHeader(string header)
{
PsmFileType type = PsmFileType.Unknown;
var split = header.Split('\t').Select(p => p.ToLowerInvariant()).ToArray();
// MetaMorpheus MS/MS input
if (split.Contains("File Name".ToLowerInvariant())
&& split.Contains("Base Sequence".ToLowerInvariant())
&& split.Contains("Full Sequence".ToLowerInvariant())
&& split.Contains("Peptide Monoisotopic Mass".ToLowerInvariant())
&& split.Contains("Scan Retention Time".ToLowerInvariant())
&& split.Contains("Precursor Charge".ToLowerInvariant())
&& split.Contains("Protein Accession".ToLowerInvariant())
&& split.Contains("Decoy/Contaminant/Target".ToLowerInvariant())
&& split.Contains("QValue".ToLowerInvariant())
&& split.Contains("QValue Notch".ToLowerInvariant()))
{
_fileNameCol = Array.IndexOf(split, "File Name".ToLowerInvariant());
_baseSequCol = Array.IndexOf(split, "Base Sequence".ToLowerInvariant());
_fullSequCol = Array.IndexOf(split, "Full Sequence".ToLowerInvariant());
_monoMassCol = Array.IndexOf(split, "Peptide Monoisotopic Mass".ToLowerInvariant());
_msmsRetnCol = Array.IndexOf(split, "Scan Retention Time".ToLowerInvariant());
_chargeStCol = Array.IndexOf(split, "Precursor Charge".ToLowerInvariant());
_protNameCol = Array.IndexOf(split, "Protein Accession".ToLowerInvariant());
_decoyCol = Array.IndexOf(split, "Decoy/Contaminant/Target".ToLowerInvariant());
_qValueCol = Array.IndexOf(split, "QValue".ToLowerInvariant());
_qValueNotchCol = Array.IndexOf(split, "QValue Notch".ToLowerInvariant());
_geneNameCol = Array.IndexOf(split, "Gene Name".ToLowerInvariant());
_organismCol = Array.IndexOf(split, "Organism Name".ToLowerInvariant());
return PsmFileType.MetaMorpheus;
}
// Morpheus MS/MS input
else if (split.Contains("Filename".ToLowerInvariant())
&& split.Contains("Base Peptide Sequence".ToLowerInvariant())
&& split.Contains("Peptide Sequence".ToLowerInvariant())
&& split.Contains("Theoretical Mass (Da)".ToLowerInvariant())
&& split.Contains("Retention Time (minutes)".ToLowerInvariant())
&& split.Contains("Precursor Charge".ToLowerInvariant())
&& split.Contains("Protein Description".ToLowerInvariant())
&& split.Contains("Decoy?".ToLowerInvariant())
&& split.Contains("Q-Value (%)".ToLowerInvariant()))
{
_fileNameCol = Array.IndexOf(split, "Filename".ToLowerInvariant());
_baseSequCol = Array.IndexOf(split, "Base Peptide Sequence".ToLowerInvariant());
_fullSequCol = Array.IndexOf(split, "Peptide Sequence".ToLowerInvariant());
_monoMassCol = Array.IndexOf(split, "Theoretical Mass (Da)".ToLowerInvariant());
_msmsRetnCol = Array.IndexOf(split, "Retention Time (minutes)".ToLowerInvariant());
_chargeStCol = Array.IndexOf(split, "Precursor Charge".ToLowerInvariant());
_protNameCol = Array.IndexOf(split, "Protein Description".ToLowerInvariant());
_decoyCol = Array.IndexOf(split, "Decoy?".ToLowerInvariant());
_qValueCol = Array.IndexOf(split, "Q-Value (%)".ToLowerInvariant());
_geneNameCol = Array.IndexOf(split, "Gene Name".ToLowerInvariant()); // probably doesn't exist
_organismCol = Array.IndexOf(split, "Organism Name".ToLowerInvariant());
return PsmFileType.Morpheus;
}
// MaxQuant MS/MS input
else if (split.Contains("Raw file".ToLowerInvariant())
&& split.Contains("Sequence".ToLowerInvariant())
&& split.Contains("Modified sequence".ToLowerInvariant())
&& split.Contains("Mass".ToLowerInvariant())
&& split.Contains("Retention time".ToLowerInvariant())
&& split.Contains("Charge".ToLowerInvariant())
&& split.Contains("Proteins".ToLowerInvariant()))
{
_fileNameCol = Array.IndexOf(split, "Raw file".ToLowerInvariant());
_baseSequCol = Array.IndexOf(split, "Sequence".ToLowerInvariant());
_fullSequCol = Array.IndexOf(split, "Modified sequence".ToLowerInvariant());
_monoMassCol = Array.IndexOf(split, "Mass".ToLowerInvariant());
_msmsRetnCol = Array.IndexOf(split, "Retention time".ToLowerInvariant());
_chargeStCol = Array.IndexOf(split, "Charge".ToLowerInvariant());
_protNameCol = Array.IndexOf(split, "Proteins".ToLowerInvariant());
_geneNameCol = Array.IndexOf(split, "Gene Names".ToLowerInvariant());
_organismCol = Array.IndexOf(split, "Organism Name".ToLowerInvariant());
return PsmFileType.MaxQuant;
}
// Peptide Shaker Input
else if (split.Contains("Spectrum File".ToLowerInvariant())
&& split.Contains("Sequence".ToLowerInvariant())
&& split.Contains("Modified Sequence".ToLowerInvariant())
&& split.Contains("Theoretical Mass".ToLowerInvariant())
&& split.Contains("RT".ToLowerInvariant())
&& split.Contains("Identification Charge".ToLowerInvariant())
&& split.Contains("Protein(s)".ToLowerInvariant()))
{
_fileNameCol = Array.IndexOf(split, "Spectrum File".ToLowerInvariant());
_baseSequCol = Array.IndexOf(split, "Sequence".ToLowerInvariant());
_fullSequCol = Array.IndexOf(split, "Modified Sequence".ToLowerInvariant());
_monoMassCol = Array.IndexOf(split, "Theoretical Mass".ToLowerInvariant());
_msmsRetnCol = Array.IndexOf(split, "RT".ToLowerInvariant());
_chargeStCol = Array.IndexOf(split, "Identification Charge".ToLowerInvariant());
_protNameCol = Array.IndexOf(split, "Protein(s)".ToLowerInvariant());
_geneNameCol = Array.IndexOf(split, "Gene Name".ToLowerInvariant()); // probably doesn't exist
_organismCol = Array.IndexOf(split, "Organism Name".ToLowerInvariant());
return PsmFileType.PeptideShaker;
}
// Generic MS/MS input
if (split.Contains("File Name".ToLowerInvariant())
&& split.Contains("Base Sequence".ToLowerInvariant())
&& split.Contains("Full Sequence".ToLowerInvariant())
&& split.Contains("Peptide Monoisotopic Mass".ToLowerInvariant())
&& split.Contains("Scan Retention Time".ToLowerInvariant())
&& split.Contains("Precursor Charge".ToLowerInvariant())
&& split.Contains("Protein Accession".ToLowerInvariant()))
{
_fileNameCol = Array.IndexOf(split, "File Name".ToLowerInvariant());
_baseSequCol = Array.IndexOf(split, "Base Sequence".ToLowerInvariant());
_fullSequCol = Array.IndexOf(split, "Full Sequence".ToLowerInvariant());
_monoMassCol = Array.IndexOf(split, "Peptide Monoisotopic Mass".ToLowerInvariant());
_msmsRetnCol = Array.IndexOf(split, "Scan Retention Time".ToLowerInvariant());
_chargeStCol = Array.IndexOf(split, "Precursor Charge".ToLowerInvariant());
_protNameCol = Array.IndexOf(split, "Protein Accession".ToLowerInvariant());
_geneNameCol = Array.IndexOf(split, "Gene Name".ToLowerInvariant()); // probably doesn't exist
_organismCol = Array.IndexOf(split, "Organism Name".ToLowerInvariant());
return PsmFileType.Generic;
}
return type;
}
}
}