Skip to content

Commit 6a99c84

Browse files
committed
query error handling and refactoring
1 parent e5a906d commit 6a99c84

File tree

3 files changed

+88
-44
lines changed

3 files changed

+88
-44
lines changed

MainClass.cs

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -261,6 +261,10 @@ private static void SpectrumQueryParametersParsing(string[] args)
261261
"s|stdout",
262262
"Pipes the output into standard output. Logging is being turned off",
263263
v => parameters.stdout = v != null
264+
},
265+
{
266+
"w|warningsAreErrors", "Return non-zero exit code for warnings; default only for errors",
267+
v => parameters.Vigilant = v != null
264268
}
265269
};
266270

@@ -325,7 +329,10 @@ private static void SpectrumQueryParametersParsing(string[] args)
325329
try
326330
{
327331
QueryExecutor.Run(parameters);
328-
exitCode = 0;
332+
333+
Log.Info($"Processing completed {parameters.Errors} errors, {parameters.Warnings} warnings");
334+
335+
exitCode = parameters.Vigilant ? parameters.Errors + parameters.Warnings : parameters.Errors;
329336
}
330337
catch (Exception ex)
331338
{

Query/ProxiSpectrumReader.cs

Lines changed: 51 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -56,14 +56,12 @@ public List<ProxiSpectrum> Retrieve()
5656
foreach (var scanNumber in queryParameters.scanNumbers)
5757
{
5858
var proxiSpectrum = new ProxiSpectrum();
59-
double monoisotopicMz = 0.0;
59+
6060
try
6161
{
6262
// Get each scan from the RAW file
6363
var scan = Scan.FromFile(rawFile, scanNumber);
6464

65-
// Check to see if the RAW file contains label (high-res) data and if it is present
66-
// then look for any data that is out of order
6765
var time = rawFile.RetentionTimeFromScanNumber(scanNumber);
6866

6967
// Get the scan filter for this scan number
@@ -86,36 +84,32 @@ public List<ProxiSpectrum> Retrieve()
8684
value: ((int) scanFilter.MSOrder).ToString(CultureInfo.InvariantCulture));
8785

8886
// trailer extra data list
89-
var trailerData = rawFile.GetTrailerExtraInformation(scanNumber);
90-
var charge = 0;
91-
var isolationWidth = 0.0;
92-
for (var i = 0; i < trailerData.Length; i++)
87+
//TODO Switch to Trailer object; check for empty trailerst
88+
ScanTrailer trailerData;
89+
try
9390
{
94-
if (trailerData.Labels[i] == "Ion Injection Time (ms):")
95-
{
96-
proxiSpectrum.AddAttribute(accession: "MS:10000927", name: "ion injection time",
97-
value: trailerData.Values[i], cvGroup: cvGroup.ToString());
98-
proxiSpectrum.AddAttribute(accession: "UO:0000028", name: "millisecond",
99-
cvGroup: cvGroup.ToString());
100-
cvGroup++;
101-
}
102-
103-
if (trailerData.Labels[i] == "Charge State:")
104-
{
105-
charge = Convert.ToInt32(trailerData.Values[i]);
106-
}
91+
trailerData = new ScanTrailer(rawFile.GetTrailerExtraInformation(scanNumber));
92+
}
93+
catch (Exception ex)
94+
{
95+
Log.WarnFormat("Cannot load trailer infromation for scan {0} due to following exception\n{1}", scanNumber, ex.Message);
96+
queryParameters.NewWarn();
97+
trailerData = new ScanTrailer();
98+
}
10799

108-
if (trailerData.Labels[i] == "Monoisotopic M/Z:")
109-
{
110-
monoisotopicMz = double.Parse(trailerData.Values[i], NumberStyles.Any,
111-
CultureInfo.CurrentCulture);
112-
}
100+
int? charge = trailerData.AsPositiveInt("Charge State:");
101+
double? monoisotopicMz = trailerData.AsDouble("Monoisotopic M/Z:");
102+
double? ionInjectionTime = trailerData.AsDouble("Ion Injection Time (ms):");
103+
double? isolationWidth = trailerData.AsDouble("MS" + (int)scanFilter.MSOrder + " Isolation Width:");
113104

114-
if (trailerData.Labels[i] == "MS" + (int) scanFilter.MSOrder + " Isolation Width:")
115-
{
116-
isolationWidth = double.Parse(trailerData.Values[i], NumberStyles.Any,
117-
CultureInfo.CurrentCulture);
118-
}
105+
//injection time
106+
if (ionInjectionTime != null)
107+
{
108+
proxiSpectrum.AddAttribute(accession: "MS:10000927", name: "ion injection time",
109+
value: ionInjectionTime.ToString(), cvGroup: cvGroup.ToString());
110+
proxiSpectrum.AddAttribute(accession: "UO:0000028", name: "millisecond",
111+
cvGroup: cvGroup.ToString());
112+
cvGroup++;
119113
}
120114

121115
if (reaction != null)
@@ -130,13 +124,13 @@ public List<ProxiSpectrum> Retrieve()
130124
value: selectedIonMz.ToString(CultureInfo.InvariantCulture));
131125

132126
// Store the isolation window information
133-
var isolationHalfWidth = isolationWidth / 2;
127+
var offset = isolationWidth.Value / 2 + reaction.IsolationWidthOffset;
134128
proxiSpectrum.AddAttribute(accession: "MS:1000828",
135129
name: "isolation window lower offset",
136-
value: isolationHalfWidth.ToString(CultureInfo.InvariantCulture));
130+
value: (isolationWidth.Value - offset).ToString());
137131
proxiSpectrum.AddAttribute(accession: "MS:1000829",
138132
name: "isolation window upper offset",
139-
value: isolationHalfWidth.ToString(CultureInfo.InvariantCulture));
133+
value: offset.ToString());
140134
}
141135

142136
// scan polarity
@@ -152,13 +146,19 @@ public List<ProxiSpectrum> Retrieve()
152146
}
153147

154148
// charge state
155-
proxiSpectrum.AddAttribute(accession: "MS:10000041", name: "charge state",
156-
value: charge.ToString(CultureInfo.InvariantCulture));
149+
if (charge != null)
150+
{
151+
proxiSpectrum.AddAttribute(accession: "MS:10000041", name: "charge state",
152+
value: charge.ToString());
153+
}
157154

158155
// write the filter string
159156
proxiSpectrum.AddAttribute(accession: "MS:10000512", name: "filter string",
160157
value: scanEvent.ToString());
161158

159+
double[] masses = null;
160+
double[] intensities = null;
161+
162162
if (!queryParameters.noPeakPicking) // centroiding requested
163163
{
164164
// check if the scan has a centroid stream
@@ -169,8 +169,8 @@ public List<ProxiSpectrum> Retrieve()
169169
proxiSpectrum.AddAttribute(accession: "MS:1000525", name: "spectrum representation",
170170
value: "centroid spectrum", valueAccession: "MS:1000127");
171171

172-
proxiSpectrum.AddMz(scan.CentroidScan.Masses);
173-
proxiSpectrum.AddIntensities(scan.CentroidScan.Intensities);
172+
masses = scan.CentroidScan.Masses;
173+
intensities = scan.CentroidScan.Intensities;
174174
}
175175
}
176176
else // otherwise take the low res segmented data
@@ -185,8 +185,8 @@ public List<ProxiSpectrum> Retrieve()
185185
proxiSpectrum.AddAttribute(accession: "MS:1000525", name: "spectrum representation",
186186
value: "centroid spectrum", valueAccession: "MS:1000127");
187187

188-
proxiSpectrum.AddMz(segmentedScan.Positions);
189-
proxiSpectrum.AddIntensities(segmentedScan.Intensities);
188+
masses = segmentedScan.Positions;
189+
intensities = segmentedScan.Intensities;
190190
}
191191
}
192192
}
@@ -209,18 +209,27 @@ public List<ProxiSpectrum> Retrieve()
209209
break;
210210
}
211211

212-
proxiSpectrum.AddMz(scan.SegmentedScan.Positions);
213-
proxiSpectrum.AddIntensities(scan.SegmentedScan.Intensities);
212+
masses = scan.SegmentedScan.Positions;
213+
intensities = scan.SegmentedScan.Intensities;
214214
}
215215
}
216216

217+
if (masses != null && intensities != null)
218+
{
219+
Array.Sort(masses, intensities);
220+
221+
proxiSpectrum.AddMz(masses);
222+
proxiSpectrum.AddIntensities(intensities);
223+
}
224+
217225
resultList.Add(proxiSpectrum);
218226
}
219227
catch (Exception ex)
220228
{
221229
if (ex.GetBaseException() is IndexOutOfRangeException)
222230
{
223-
// ignore
231+
Log.WarnFormat("Spectrum #{0} outside of file boundries", scanNumber);
232+
queryParameters.NewWarn();
224233
}
225234
else
226235
{

Query/QueryParameters.cs

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,26 @@ namespace ThermoRawFileParser.Query
66
{
77
public class QueryParameters
88
{
9+
private int _errors;
10+
11+
private int _warnings;
12+
913
public bool help { get; set; }
1014
public string rawFilePath { get; set; }
1115
public string scans { get; set; }
1216
public string outputFile { get; set; }
1317
public bool noPeakPicking { get; set; }
1418
public HashSet<int> scanNumbers { get; set; }
1519
public bool stdout { get; set; }
16-
20+
public bool Vigilant { get; set; }
21+
public int Errors
22+
{
23+
get => _errors;
24+
}
25+
public int Warnings
26+
{
27+
get => _warnings;
28+
}
1729

1830
public QueryParameters()
1931
{
@@ -24,6 +36,9 @@ public QueryParameters()
2436
noPeakPicking = false;
2537
scanNumbers = new HashSet<int>();
2638
stdout = false;
39+
Vigilant = false;
40+
_errors = 0;
41+
_warnings = 0;
2742
}
2843

2944
public QueryParameters(QueryParameters copy)
@@ -36,6 +51,19 @@ public QueryParameters(QueryParameters copy)
3651
scanNumbers = new HashSet<int>();
3752
foreach (int s in copy.scanNumbers) scanNumbers.Add(s);
3853
stdout = copy.stdout;
54+
Vigilant = copy.Vigilant;
55+
_errors = copy.Errors;
56+
_warnings = copy.Warnings;
57+
}
58+
59+
public void NewError()
60+
{
61+
_errors++;
62+
}
63+
64+
public void NewWarn()
65+
{
66+
_warnings++;
3967
}
4068
}
4169
}

0 commit comments

Comments
 (0)