-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMaxQuantModExtractor.cs
More file actions
741 lines (583 loc) · 32.4 KB
/
Copy pathMaxQuantModExtractor.cs
File metadata and controls
741 lines (583 loc) · 32.4 KB
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
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text.RegularExpressions;
using System.Xml.Linq;
using PRISM;
namespace MaxQuantParamFileModExtractor
{
internal class MaxQuantModExtractor : EventNotifier
{
// Ignore Spelling: acetyl, acetylation, carbamidomethyl, plex
public ModExtractorOptions Options { get; set; }
private readonly Regex mFindLeadingWhitespace = new("^[ \t]+", RegexOptions.Compiled);
private readonly Regex mFindTagName = new("<(?<TagName>[^>]+)>", RegexOptions.Compiled);
/// <summary>
/// Constructor
/// </summary>
/// <param name="options">Processing options</param>
public MaxQuantModExtractor(ModExtractorOptions options)
{
Options = options;
}
public bool ProcessFile(string inputFilePath)
{
const string WILDCARD_ASTERISK = "__WildCardAsterisk__";
const string WILDCARD_QUESTION_MARK = "__WildCardQuestionMark__";
try
{
var filesToProcess = new List<FileInfo>();
if (inputFilePath.IndexOfAny(new[] { '*', '?' }) >= 0)
{
var cleanFilePath = inputFilePath.Replace("*", WILDCARD_ASTERISK).Replace("?", WILDCARD_QUESTION_MARK);
var placeholderFile = new FileInfo(cleanFilePath);
if (placeholderFile.Directory == null)
{
OnWarningEvent("Unable to determine the parent directory of the input file");
return false;
}
var fileMask = placeholderFile.Name.Replace(WILDCARD_ASTERISK, "*").Replace(WILDCARD_QUESTION_MARK, "?");
foreach (var matchingFile in placeholderFile.Directory.GetFiles(fileMask))
{
filesToProcess.Add(matchingFile);
}
if (filesToProcess.Count == 0)
{
OnWarningEvent("No files matching {0} were found in {1}", fileMask, placeholderFile.Directory.FullName);
return false;
}
}
else
{
filesToProcess.Add(new FileInfo(inputFilePath));
if (!filesToProcess[0].Exists)
{
OnWarningEvent("File not found: {0}", inputFilePath);
OnWarningEvent("Full path: {0}", filesToProcess[0].FullName);
return false;
}
}
var failedFiles = new List<FileInfo>();
foreach (var inputFile in filesToProcess)
{
bool success;
if (Options.UpdateParameters)
{
success = UpdateParametersInMaxQuantParameterFile(inputFile);
}
else
{
success = ExtractModNodesFromParameterFile(inputFile);
}
if (!success)
failedFiles.Add(inputFile);
}
switch (failedFiles.Count)
{
case 0:
return true;
case 1:
OnWarningEvent("Error processing {0}", failedFiles[0]);
return false;
}
OnWarningEvent("Error processing {0} files", failedFiles.Count);
foreach (var file in failedFiles)
{
OnStatusEvent(" {0}", PathUtils.CompactPathString(file.FullName, 100));
}
return false;
}
catch (Exception ex)
{
OnErrorEvent("Error occurred in Processor->ProcessFile", ex);
return false;
}
}
private static void AddParameterToDelete(IDictionary<string, ParameterUpdateInfo> updates, string xmlTagName)
{
var updateInfo = new ParameterUpdateInfo(xmlTagName, ParameterUpdateInfo.UpdateAction.Delete);
updates.Add(xmlTagName, updateInfo);
}
private static void AddParameterToReplace(
IDictionary<string, ParameterUpdateInfo> updates,
string xmlTagName,
IEnumerable<string> replacementParameters)
{
var updateInfo = new ParameterUpdateInfo(xmlTagName, ParameterUpdateInfo.UpdateAction.Replace);
updateInfo.ParametersToAdd.AddRange(replacementParameters);
updates.Add(xmlTagName, updateInfo);
}
private static void AddParameterToUpdateValue(
IDictionary<string, ParameterUpdateInfo> updates,
string xmlTagName,
string newValue,
string oldValue = "")
{
var updateInfo = new ParameterUpdateInfo(xmlTagName, ParameterUpdateInfo.UpdateAction.UpdateValue)
{
OldValue = oldValue,
NewValue = newValue
};
updates.Add(xmlTagName, updateInfo);
}
private static void AddParametersToAppend(
IDictionary<string, ParameterUpdateInfo> updates,
string xmlTagName, IEnumerable<string> parametersToAppend,
bool AppendAfterClosingTag = true)
{
var updateInfo = new ParameterUpdateInfo(xmlTagName, ParameterUpdateInfo.UpdateAction.AppendParameters);
updateInfo.ParametersToAdd.AddRange(parametersToAppend);
updateInfo.AppendAfterClosingTag = AppendAfterClosingTag;
updates.Add(xmlTagName, updateInfo);
}
private void AppendParameters(StreamReader reader, TextWriter writer, string dataLine, ParameterUpdateInfo updateInfo)
{
var closingTag = GetClosingTag(updateInfo.TagName);
var leadingWhitespace = GetLeadingWhitespace(dataLine);
writer.WriteLine(dataLine);
if (updateInfo.AppendAfterClosingTag &&!dataLine.Contains(closingTag) && !reader.EndOfStream)
{
var nextLine = reader.ReadLine() ?? string.Empty;
if (!string.IsNullOrWhiteSpace(nextLine))
{
writer.WriteLine(nextLine);
}
if (!nextLine.Contains(closingTag))
{
OnWarningEvent("Closing tag not found in the current line or the next line: {0}", closingTag);
OnWarningEvent(dataLine);
OnWarningEvent(nextLine);
}
}
foreach (var parameter in updateInfo.ParametersToAdd)
{
var additionalWhitespace = updateInfo.AppendAfterClosingTag ? string.Empty : " ";
writer.WriteLine("{0}{1}{2}", leadingWhitespace, additionalWhitespace, parameter.Trim());
OnStatusEvent("Appended parameter: <{0}>", GetTagName(parameter));
}
}
private void DeleteParameter(StreamReader reader, string dataLine, ParameterUpdateInfo updateInfo)
{
var closingTag = GetClosingTag(updateInfo.TagName);
if (!dataLine.Contains(closingTag) && !reader.EndOfStream)
{
var nextLine = reader.ReadLine() ?? string.Empty;
if (!nextLine.Contains(closingTag))
{
OnWarningEvent("Closing tag not found in the current line or the next line: {0}", closingTag);
OnWarningEvent(" {0}", dataLine);
OnWarningEvent(" {0}", nextLine);
}
}
OnStatusEvent("Deleted parameter: {0}", updateInfo.TagName);
}
private string GetClosingTag(string tagName)
{
var trimmedName = tagName.Trim();
if (trimmedName.StartsWith("</"))
{
// The tag is already a closing tag
return tagName;
}
if (trimmedName.StartsWith("<"))
{
return string.Format("</{0}", trimmedName.Substring(1));
}
return string.Empty;
}
private string GetLeadingWhitespace(string dataLine)
{
var match = mFindLeadingWhitespace.Match(dataLine);
return match.Success ? match.Value : string.Empty;
}
private string GetTagName(string parameterValue)
{
var match = mFindTagName.Match(parameterValue);
return match.Success ? match.Groups["TagName"].Value : parameterValue;
}
private void ReplaceParameter(StreamReader reader, TextWriter writer, string dataLine, ParameterUpdateInfo updateInfo)
{
var closingTag = GetClosingTag(updateInfo.TagName);
var leadingWhitespace = GetLeadingWhitespace(dataLine);
if (!dataLine.Contains(closingTag) && !reader.EndOfStream)
{
var nextLine = reader.ReadLine() ?? string.Empty;
if (!nextLine.Contains(closingTag))
{
OnWarningEvent("Closing tag not found in the current line or the next line: {0}", closingTag);
OnWarningEvent(dataLine);
OnWarningEvent(nextLine);
}
}
foreach (var parameter in updateInfo.ParametersToAdd)
{
writer.WriteLine("{0}{1}", leadingWhitespace, parameter.Trim());
OnStatusEvent("Replaced parameter: {0}", updateInfo.TagName);
OnStatusEvent(" changed to");
OnStatusEvent(" <{0}>", GetTagName(parameter));
}
}
private void UpdateParameterValue(StreamReader reader, TextWriter writer, string dataLine, ParameterUpdateInfo updateInfo)
{
var closingTag = GetClosingTag(updateInfo.TagName);
var leadingWhitespace = GetLeadingWhitespace(dataLine);
if (!string.IsNullOrWhiteSpace(updateInfo.OldValue) && !dataLine.Contains(updateInfo.OldValue))
{
OnStatusEvent("Not changing parameter value since the existing value is not '{0}': {1}", updateInfo.OldValue, dataLine.Trim());
writer.WriteLine(dataLine);
return;
}
if (!dataLine.Contains(closingTag) && !reader.EndOfStream)
{
var nextLine = reader.ReadLine() ?? string.Empty;
if (!nextLine.Contains(closingTag))
{
OnWarningEvent("Closing tag not found in the current line or the next line: {0}", closingTag);
OnWarningEvent(dataLine);
OnWarningEvent(nextLine);
}
}
var tagName = GetTagName(updateInfo.TagName);
var updatedLine = string.Format("{0}<{1}>{2}</{1}>", leadingWhitespace, tagName, updateInfo.NewValue);
writer.WriteLine(updatedLine);
OnStatusEvent("Updated parameter value: {0}", updatedLine.Trim());
}
private bool ExtractModNodesFromParameterFile(FileSystemInfo inputFile)
{
try
{
Console.WriteLine();
OnStatusEvent("Reading: {0}", PathUtils.CompactPathString(inputFile.FullName, 100));
using var reader = new StreamReader(new FileStream(inputFile.FullName, FileMode.Open, FileAccess.Read, FileShare.ReadWrite));
// Note that XDocument supersedes XmlDocument and XPathDocument
// XDocument can often be easier to use since XDocument is LINQ-based
var doc = XDocument.Parse(reader.ReadToEnd());
// <fixedModifications>
// <string>Carbamidomethyl (C)</string>
// </fixedModifications>
// <variableModifications>
// <string>Oxidation (M)</string>
// <string>Acetyl (Protein N-term)</string>
// </variableModifications>
// ReSharper disable CommentTypo
// <isobaricLabels>
// <IsobaricLabelInfo>
// <internalLabel>TMT6plex-Lys126</internalLabel>
// <terminalLabel>TMT6plex-Nter126</terminalLabel>
// <correctionFactorM2>0</correctionFactorM2>
// <correctionFactorM1>0</correctionFactorM1>
// <correctionFactorP1>0</correctionFactorP1>
// <correctionFactorP2>0</correctionFactorP2>
// <tmtLike>True</tmtLike>
// </IsobaricLabelInfo>
// ...
// </isobaricLabels>
// ReSharper restore CommentTypo
var restrictModNodes = doc.Elements("MaxQuantParams").Elements("restrictMods").Elements("string").ToList();
var parameterGroupNodes = doc.Elements("MaxQuantParams").Elements("parameterGroups").Elements("parameterGroup").ToList();
// ReSharper disable once ConvertIfStatementToSwitchStatement
if (parameterGroupNodes.Count == 0)
{
OnWarningEvent("MaxQuant parameter file is missing the <parameterGroup> element; cannot extract modification info");
return false;
}
if (parameterGroupNodes.Count > 1)
{
OnWarningEvent("MaxQuant parameter file has more than one <parameterGroup> element; this is allowed, but not usually used");
}
Console.WriteLine();
var dynamicMods = new SortedSet<string>();
var groupNumber = 0;
foreach (var parameterGroup in parameterGroupNodes)
{
groupNumber++;
if (parameterGroupNodes.Count > 1)
{
Console.WriteLine("Parameter group {0}", groupNumber);
}
var firstSearchDynamicModNodes = parameterGroup.Elements("variableModificationsFirstSearch").Elements("string").ToList();
var fixedModNodes = parameterGroup.Elements("fixedModifications").Elements("string").ToList();
var dynamicModNodes = parameterGroup.Elements("variableModifications").Elements("string").ToList();
// Check for isobaric mods, e.g. 6-plex or 10-plex TMT
var internalIsobaricLabelNodes = parameterGroup.Elements("isobaricLabels").Elements("IsobaricLabelInfo").Elements("internalLabel").ToList();
var terminalIsobaricLabelNodes = parameterGroup.Elements("isobaricLabels").Elements("IsobaricLabelInfo").Elements("terminalLabel").ToList();
if (fixedModNodes.Count > 0)
{
Console.WriteLine(" <fixedModifications>");
foreach (var fixedMod in fixedModNodes)
{
Console.WriteLine(" <string>{0}</string>", fixedMod.Value);
}
Console.WriteLine(" </fixedModifications>");
}
if (dynamicModNodes.Count > 0 || firstSearchDynamicModNodes.Count > 0)
{
Console.WriteLine(" <variableModifications>");
foreach (var modName in dynamicModNodes.Select(dynamicMod => dynamicMod.Value))
{
Console.WriteLine(" <string>{0}</string>", modName);
// Add the modification, if not yet present
dynamicMods.Add(modName);
}
foreach (var modName in firstSearchDynamicModNodes.Select(dynamicMod => dynamicMod.Value))
{
if (dynamicMods.Contains(modName))
continue;
Console.WriteLine(" <string>{0}</string>", modName);
}
Console.WriteLine(" </variableModifications>");
}
if (internalIsobaricLabelNodes.Count > 0 || terminalIsobaricLabelNodes.Count > 0)
{
Console.WriteLine(" <isobaricLabels>");
Console.WriteLine(" <IsobaricLabelInfo>");
if (internalIsobaricLabelNodes.Count > 0)
{
Console.WriteLine(" <internalLabel>{0}</internalLabel>", internalIsobaricLabelNodes[0].Value);
}
if (terminalIsobaricLabelNodes.Count > 0)
{
Console.WriteLine(" <terminalLabel>{0}</terminalLabel>", terminalIsobaricLabelNodes[0].Value);
}
Console.WriteLine(" </IsobaricLabelInfo>");
Console.WriteLine(" </isobaricLabels>");
}
}
Console.WriteLine();
// Check whether any mods in the <restrictMods> section are other than oxidation or N-terminal acetylation
foreach (var modName in restrictModNodes.Select(dynamicMod => dynamicMod.Value))
{
if (modName.Equals("Oxidation (M)") || modName.Equals("Acetyl (Protein N-term)"))
continue;
ConsoleMsgUtils.ShowWarning(
"Dynamic mod {0} is defined in the <restrictMods> section, which means it will be considered during protein quantification;\n" +
" typically, only oxidized methionine and N-terminal acetylation should be defined here",
modName);
}
// Look for mods in the <restrictMods> section that are not in a dynamic mod defined for the main search
foreach (var modName in restrictModNodes.Select(dynamicMod => dynamicMod.Value))
{
if (!dynamicMods.Contains(modName))
{
ConsoleMsgUtils.ShowWarning(
"Dynamic mod {0} is defined in the <restrictMods> section, but is not defined in the <variableModifications> section;\n" +
" this is likely an error",
modName);
}
}
return true;
}
catch (Exception ex)
{
OnErrorEvent("Error occurred in ExtractModNodesFromParameterFile", ex);
return false;
}
}
private Dictionary<string, ParameterUpdateInfo> GetParameterFileUpdateInfo()
{
var updates = new Dictionary<string, ParameterUpdateInfo>();
// ReSharper disable StringLiteralTypo
AddParametersToAppend(updates, "<deNovoUseA2Score>", new List<string>
{
"<deNovoMassClusterTolDa>0</deNovoMassClusterTolDa>", "<deNovoScalingFactor>0</deNovoScalingFactor>"
});
AddParametersToAppend(updates, "<writeMzTab>", new List<string>
{
"<writeSdrf>False</writeSdrf>"
});
AddParametersToAppend(updates, "<proteinGroupingFile>", new List<string>
{
"<useAndromeda20>False</useAndromeda20>", "<useAndromeda20DefaultModel>False</useAndromeda20DefaultModel>", "<andromeda20AltModelPath></andromeda20AltModelPath>", "<intensityPredictionFolder></intensityPredictionFolder>"
});
AddParametersToAppend(
updates,
"<parameterGroup>",
new List<string> { "<andromeda20AltModelPath></andromeda20AltModelPath>", "<andromeda20DefaultModel>False</andromeda20DefaultModel>", "<useAndromeda20>False</useAndromeda20>" },
false);
AddParametersToAppend(updates, "<lfqMinRatioCount>", new List<string>
{
"<lfqMinRatioCountDia>2</lfqMinRatioCountDia>", "<lfqPrioritizeMs1Dia>True</lfqPrioritizeMs1Dia>"
});
AddParametersToAppend(updates, "</diaMsmsPaths>", new List<string>
{
"<diaLabelIndsForLibraryMatch>", "</diaLabelIndsForLibraryMatch>"
});
AddParametersToAppend(updates, "<diaScoreN>", new List<string>
{
"<diaScoreNAdditional>0</diaScoreNAdditional>"
});
AddParametersToAppend(updates, "<diaTopNForQuant>", new List<string>
{
"<diaTopNCorrelationForQuant>0</diaTopNCorrelationForQuant>", "<diaFragmentCorrelationForQuant>0</diaFragmentCorrelationForQuant>"
});
AddParametersToAppend(updates, "<diaMinPrecursorScore>", new List<string>
{
"<diaUseProfileCorrelation>False</diaUseProfileCorrelation>"
});
AddParametersToAppend(updates, "<diaTransferQvalue>", new List<string>
{
"<diaTransferQvalueBetweenLabels>0</diaTransferQvalueBetweenLabels>", "<diaTransferQvalueBetweenFractions>0</diaTransferQvalueBetweenFractions>", "<diaTransferQvalueBetweenFaims>0</diaTransferQvalueBetweenFaims>"
});
AddParametersToAppend(updates, "<diaUseFragMassesForMl>", new List<string>
{
"<diaMaxTrainInstances>0</diaMaxTrainInstances>", "<diaMaxFragmentCharge>0</diaMaxFragmentCharge>", "<diaAdaptiveMlScoring>False</diaAdaptiveMlScoring>", "<diaDynamicScoringMaxInstances>25000</diaDynamicScoringMaxInstances>", "<diaMaxPrecursorMz>0</diaMaxPrecursorMz>",
"<diaHardRtFilter>False</diaHardRtFilter>", "<diaConvertLibraryCharge2Fragments>False</diaConvertLibraryCharge2Fragments>", "<diaChargeNormalizationLibrary>False</diaChargeNormalizationLibrary>", "<diaChargeNormalizationSample>False</diaChargeNormalizationSample>",
"<diaDeleteIntermediateResults>False</diaDeleteIntermediateResults>", "<diaScoreWeightScanIndex>0</diaScoreWeightScanIndex>", "<diaScoreWeightScanValue>0</diaScoreWeightScanValue>", "<diaNumNonleadingMatches>0</diaNumNonleadingMatches>", "<diaUseDefaultFragmentModel>True</diaUseDefaultFragmentModel>",
"<diaAltFragmentModelPath></diaAltFragmentModelPath>", "<diaUseDefaultRtModel>True</diaUseDefaultRtModel>", "<diaAltRtModelPath></diaAltRtModelPath>", "<diaUseDefaultCcsModel>True</diaUseDefaultCcsModel>", "<diaAltCcsModelPath></diaAltCcsModelPath>", "<diaBatchProcessing>False</diaBatchProcessing>",
"<diaBatchSize>0</diaBatchSize>", "<diaFirstBatch>0</diaFirstBatch>", "<diaLastBatch>0</diaLastBatch>", "<diaOnlyPreprocess>False</diaOnlyPreprocess>", "<diaMultiplexQuantMethod>0</diaMultiplexQuantMethod>", "<diaOnlyPostprocess>False</diaOnlyPostprocess>", "<diaRequirePrecursor>False</diaRequirePrecursor>",
"<diaFuturePeptides>False</diaFuturePeptides>", "<diaOverrideRtWithPrediction>False</diaOverrideRtWithPrediction>", "<diaMaxModifications>0</diaMaxModifications>", "<diaMaxPositionings>0</diaMaxPositionings>", "<diaUseProbScore>False</diaUseProbScore>", "<diaProbScoreP>0</diaProbScoreP>",
"<diaProbScoreG>0</diaProbScoreG>", "<diaProbScoreStep>0</diaProbScoreStep>", "<isM2FragTypeOverride>False</isM2FragTypeOverride>", "<ms2FragTypeOverride>0</ms2FragTypeOverride>", "<classicLfqForSingleShots>True</classicLfqForSingleShots>", "<sequenceBasedModifier>False</sequenceBasedModifier>",
"<diaRtFromSamplesForExport>False</diaRtFromSamplesForExport>", "<diaCcsFromSamplesForExport>False</diaCcsFromSamplesForExport>", "<diaLibraryExport>0</diaLibraryExport>", "<diaUseApexWeightsForPtmLoc>False</diaUseApexWeightsForPtmLoc>", "<diaSecondScoreForMultiplex>False</diaSecondScoreForMultiplex>"
});
AddParametersToAppend(updates, "<IncludeAmmonia>", new List<string>
{
"<IncludeWaterCross>False</IncludeWaterCross>", "<IncludeAmmoniaCross>False</IncludeAmmoniaCross>"
});
AddParameterToDelete(updates, "<boxCarMode>");
AddParameterToDelete(updates, "<writePeptidesForSpectrumFile>");
AddParameterToDelete(updates, "<intensityPredictionsFile>");
AddParameterToDelete(updates, "</intensityPredictionsFile>");
AddParameterToDelete(updates, "<intensPred>");
AddParameterToDelete(updates, "<intensPredModelReTrain>");
AddParameterToDelete(updates, "<timsRearrangeSpectra>");
AddParameterToDelete(updates, "<diaPeptidePaths>");
AddParameterToDelete(updates, "</diaPeptidePaths>");
AddParameterToDelete(updates, "<diaPrecursorFilterType>");
AddParameterToDelete(updates, "<diaRtPrediction>");
AddParameterToDelete(updates, "<diaRtPredictionSecondRound>");
AddParameterToDelete(updates, "<ConnectedScore0>");
AddParameterToDelete(updates, "<ConnectedScore1>");
AddParameterToDelete(updates, "<ConnectedScore2>");
AddParameterToReplace(updates, "<intensityThresholdMs1>", new List<string>
{
"<intensityThresholdMs1Dda>0</intensityThresholdMs1Dda>", "<intensityThresholdMs1Dia>0</intensityThresholdMs1Dia>"
});
AddParameterToReplace(updates, "<diaMinProfileCorrelation>", new List<string>
{
"<diaMinPrecProfileCorrelation>0</diaMinPrecProfileCorrelation>", "<diaMinFragProfileCorrelation>0</diaMinFragProfileCorrelation>"
});
AddParameterToReplace(updates, "<diaMinPeaksForRecal>", new List<string>
{
"<diaMinPeaks>5</diaMinPeaks>"
});
AddParameterToReplace(updates, "<Connected>", new List<string>
{
"<UseIntensityPrediction>False</UseIntensityPrediction>", "<UseSequenceBasedModifier>False</UseSequenceBasedModifier>"
});
AddParameterToReplace(updates, "<minScore_Dipeptide>", new List<string>
{
"<minScoreDipeptide>40</minScoreDipeptide>"
});
AddParameterToReplace(updates, "<minScore_Monopeptide>", new List<string>
{
"<minScoreMonopeptide>0</minScoreMonopeptide>"
});
AddParameterToReplace(updates, "<minScore_PartialCross>", new List<string>
{
"<minScorePartialCross>10</minScorePartialCross>"
});
AddParameterToReplace(updates, "<diaLfqWeightedMedian>", new List<string>
{
"<diaLfqRatioType>0</diaLfqRatioType>"
});
AddParameterToUpdateValue(updates, "<maxQuantVersion>", "2.4.13.0");
AddParameterToUpdateValue(updates, "<fullMinMz>", "-1.79769313486232E+308");
AddParameterToUpdateValue(updates, "<fullMaxMz>", "1.79769313486232E+308");
AddParameterToUpdateValue(updates, "<lcmsRunType>", "Reporter MS2", "Reporter ion MS2");
// ReSharper restore StringLiteralTypo
return updates;
}
private bool UpdateParametersInMaxQuantParameterFile(FileInfo inputFile)
{
try
{
Console.WriteLine();
if (inputFile.DirectoryName == null)
{
OnErrorEvent("Unable to determine the parent directory of {0}", inputFile.FullName);
return false;
}
var outputFile = new FileInfo(Path.Combine(inputFile.DirectoryName, inputFile.Name + ".new"));
OnStatusEvent("Updating: {0}", PathUtils.CompactPathString(inputFile.FullName, 100));
var success = UpdateParametersWork(inputFile, outputFile);
if (!success)
return false;
var oldFile = new FileInfo(Path.Combine(inputFile.DirectoryName, inputFile.Name + ".old"));
OnStatusEvent(string.Empty);
if (oldFile.Exists)
{
OnStatusEvent("Created {0}, but not replacing the original file since the '.old' file already exists: {1}",
outputFile.Name,
PathUtils.CompactPathString(oldFile.FullName, 120));
return true;
}
var inputFilePath = inputFile.FullName;
inputFile.MoveTo(oldFile.FullName);
outputFile.MoveTo(inputFilePath);
OnStatusEvent("Updated parameters in {0}", PathUtils.CompactPathString(inputFilePath, 120));
return true;
}
catch (Exception ex)
{
OnErrorEvent("Error occurred in UpdateParametersInMaxQuantParameterFile", ex);
return false;
}
}
private bool UpdateParametersWork(FileSystemInfo inputFile, FileSystemInfo outputFile)
{
try
{
var xmlTagMatcher = new Regex("^(?<LeadingWhitespace> *)(?<TagName><[a-z/][^> ]+)", RegexOptions.Compiled | RegexOptions.IgnoreCase);
var updates = GetParameterFileUpdateInfo();
// Although an XML reader could be used, this method instead assumes the file is formatted in an expected manner and will instead use a text reader to read each line
using var reader = new StreamReader(new FileStream(inputFile.FullName, FileMode.Open, FileAccess.Read, FileShare.ReadWrite));
using var writer = new StreamWriter(new FileStream(outputFile.FullName, FileMode.Create, FileAccess.Write, FileShare.ReadWrite));
while (!reader.EndOfStream)
{
var dataLine = reader.ReadLine();
if (string.IsNullOrWhiteSpace(dataLine))
{
writer.WriteLine(dataLine);
continue;
}
var match = xmlTagMatcher.Match(dataLine);
if (!match.Success)
{
writer.WriteLine(dataLine);
continue;
}
var tagName = string.Format("{0}>", match.Groups["TagName"].Value);
if (!updates.TryGetValue(tagName, out var updateInfo))
{
writer.WriteLine(dataLine);
continue;
}
switch (updateInfo.Action)
{
case ParameterUpdateInfo.UpdateAction.AppendParameters:
AppendParameters(reader, writer, dataLine, updateInfo);
break;
case ParameterUpdateInfo.UpdateAction.Delete:
DeleteParameter(reader, dataLine, updateInfo);
break;
case ParameterUpdateInfo.UpdateAction.Replace:
ReplaceParameter(reader, writer, dataLine, updateInfo);
break;
case ParameterUpdateInfo.UpdateAction.UpdateValue:
UpdateParameterValue(reader, writer, dataLine, updateInfo);
break;
default:
throw new ArgumentOutOfRangeException();
}
}
return true;
}
catch (Exception ex)
{
OnErrorEvent("Error occurred in UpdateParametersWork", ex);
return false;
}
}
}
}