Skip to content

Commit 8218f9c

Browse files
committed
MS Levels
new key -L / --msLevel allowing to select which spectra levels to output
1 parent ded9953 commit 8218f9c

File tree

4 files changed

+77
-2
lines changed

4 files changed

+77
-2
lines changed

MainClass.cs

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@
99
using ThermoRawFileParser.Query;
1010
using ThermoRawFileParser.XIC;
1111
using System.Globalization;
12+
using System.Collections.Generic;
13+
using System.Text.RegularExpressions;
1214

1315
namespace ThermoRawFileParser
1416
{
@@ -422,6 +424,11 @@ private static void RegularParametersParsing(string[] args)
422424
"e|ignoreInstrumentErrors", "Ignore missing properties by the instrument.",
423425
v => parseInput.IgnoreInstrumentErrors = v != null
424426
},
427+
{
428+
"L=|msLevel=",
429+
"MS Levels (i.e. MS1, MS2 etc) to include in the output, should be comma separated list of integers ( 1,2,3 ) or intervals ( 1-3 ), open end intervals ( 1- ) are allowed",
430+
v => parseInput.MsLevel = ParseMsLevel(v)
431+
},
425432
{
426433
"u:|s3_url:",
427434
"Optional property to write directly the data into S3 Storage.",
@@ -756,5 +763,64 @@ private static void ShowHelp(string message, OptionException optionException, Op
756763
optionSet.WriteOptionDescriptions(Console.Error);
757764
Environment.Exit(-1);
758765
}
766+
767+
private static HashSet<int> ParseMsLevel(string inputString)
768+
{
769+
HashSet<int> result = new HashSet<int>();
770+
Regex valid = new Regex(@"^[\d,\-\s]+$");
771+
Regex interval = new Regex(@"^\s*(\d+)?\s*(-)?\s*(\d+)?\s*$");
772+
773+
if (!valid.IsMatch(inputString))
774+
throw new OptionException("Invalid characters in msLevel key", "msLevel");
775+
776+
foreach (var piece in inputString.Split(new char[] { ',' }))
777+
{
778+
try
779+
{
780+
int start;
781+
int end;
782+
783+
var intervalMatch = interval.Match(piece);
784+
785+
if (!intervalMatch.Success)
786+
throw new OptionException();
787+
788+
if (intervalMatch.Groups[2].Success) //it is interval
789+
{
790+
if (intervalMatch.Groups[1].Success)
791+
start = Math.Max(1, int.Parse(intervalMatch.Groups[1].Value));
792+
else
793+
start = 1;
794+
795+
if (intervalMatch.Groups[3].Success)
796+
end = Math.Min(10, int.Parse(intervalMatch.Groups[3].Value));
797+
else
798+
end = 10;
799+
}
800+
else
801+
{
802+
if (intervalMatch.Groups[1].Success)
803+
end = start = int.Parse(intervalMatch.Groups[1].Value);
804+
else
805+
throw new OptionException();
806+
807+
if (intervalMatch.Groups[3].Success)
808+
throw new OptionException();
809+
}
810+
811+
for (int l = start; l <= end; l++)
812+
{
813+
result.Add(l);
814+
}
815+
}
816+
817+
catch (Exception ex)
818+
{
819+
throw new OptionException(String.Format("Cannot parse part of msLevel input: '{0}'", piece), "msLevel", ex);
820+
}
821+
}
822+
823+
return result;
824+
}
759825
}
760826
}

ParseInput.cs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,15 @@
11
using System;
2+
using System.Collections.Generic;
23
using System.IO;
34
using ThermoRawFileParser.Writer;
45

56
namespace ThermoRawFileParser
67
{
78
public class ParseInput
89
{
10+
//all ms levels
11+
private readonly HashSet<int> allLevels = new HashSet<int>(new int[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 });
12+
913
/// <summary>
1014
/// The RAW file path.
1115
/// </summary>
@@ -71,6 +75,8 @@ public string RawFilePath
7175

7276
public bool IgnoreInstrumentErrors { get; set; }
7377

78+
public HashSet<int> MsLevel { get; set; }
79+
7480
private S3Loader S3Loader { get; set; }
7581

7682
public string S3AccessKeyId { get; set; }
@@ -101,6 +107,7 @@ public ParseInput()
101107
LogFormat = LogFormat.DEFAULT;
102108
IgnoreInstrumentErrors = false;
103109
AllDetectors = false;
110+
MsLevel = allLevels;
104111
}
105112

106113
public ParseInput(string rawFilePath, string rawDirectoryPath, string outputDirectory, OutputFormat outputFormat

Writer/MgfSpectrumWriter.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ public class MgfSpectrumWriter : SpectrumWriter
2020

2121
public MgfSpectrumWriter(ParseInput parseInput) : base(parseInput)
2222
{
23+
ParseInput.MsLevel.Remove(1); //MS1 spectra are not supposed to be in MGF
2324
}
2425

2526
/// <inheritdoc />
@@ -60,7 +61,7 @@ public override void Write(IRawDataPlus rawFile, int firstScanNumber, int lastSc
6061
var scanEvent = rawFile.GetScanEventForScanNumber(scanNumber);
6162

6263
// don't include MS1 spectra
63-
if (scanFilter.MSOrder != MSOrderType.Ms)
64+
if (ParseInput.MsLevel.Contains((int)scanFilter.MSOrder))
6465
{
6566
IReaction reaction = GetReaction(scanEvent, scanNumber);
6667

Writer/MzMlSpectrumWriter.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -334,7 +334,8 @@ public override void Write(IRawDataPlus rawFile, int firstScanNumber, int lastSc
334334
}
335335

336336
var spectrum = ConstructMSSpectrum(scanNumber);
337-
if (spectrum != null)
337+
var level = int.Parse(spectrum.cvParam.Where(p => p.accession == "MS:1000511").First().value);
338+
if (spectrum != null && ParseInput.MsLevel.Contains(level))
338339
{
339340
spectrum.index = index.ToString();
340341
if (_doIndexing)

0 commit comments

Comments
 (0)