Skip to content

Commit 65f14e5

Browse files
ZSPACE-12467 Solve code review issues.
1 parent 467506b commit 65f14e5

File tree

4 files changed

+322
-302
lines changed

4 files changed

+322
-302
lines changed

RawFileParser.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -161,7 +161,7 @@ private static void ProcessFile(ParseInput parseInput)
161161
metadataWriter.WriteMetadata(rawFile, firstScanNumber, lastScanNumber);
162162
}
163163

164-
if (rawFile.SelectMsData() && parseInput.OutputFormat != OutputFormat.NONE)
164+
if (parseInput.OutputFormat != OutputFormat.NONE)
165165
{
166166
SpectrumWriter spectrumWriter;
167167
switch (parseInput.OutputFormat)

Writer/MgfSpectrumWriter.cs

Lines changed: 152 additions & 149 deletions
Original file line numberDiff line numberDiff line change
@@ -41,208 +41,211 @@ public override void Write(IRawDataPlus rawFile, int firstScanNumber, int lastSc
4141
ConfigureWriter(".mgf");
4242
using (Writer)
4343
{
44-
Log.Info("Processing " + (lastScanNumber - firstScanNumber + 1) + " scans");
45-
46-
var lastScanProgress = 0;
47-
for (var scanNumber = firstScanNumber; scanNumber <= lastScanNumber; scanNumber++)
44+
if (rawFile.SelectMsData())
4845
{
49-
if (ParseInput.LogFormat == LogFormat.DEFAULT)
46+
Log.Info("Processing " + (lastScanNumber - firstScanNumber + 1) + " scans");
47+
48+
var lastScanProgress = 0;
49+
for (var scanNumber = firstScanNumber; scanNumber <= lastScanNumber; scanNumber++)
5050
{
51-
var scanProgress = (int) ((double) scanNumber / (lastScanNumber - firstScanNumber + 1) * 100);
52-
if (scanProgress % ProgressPercentageStep == 0)
51+
if (ParseInput.LogFormat == LogFormat.DEFAULT)
5352
{
54-
if (scanProgress != lastScanProgress)
53+
var scanProgress = (int)((double)scanNumber / (lastScanNumber - firstScanNumber + 1) * 100);
54+
if (scanProgress % ProgressPercentageStep == 0)
5555
{
56-
Console.Write("" + scanProgress + "% ");
57-
lastScanProgress = scanProgress;
56+
if (scanProgress != lastScanProgress)
57+
{
58+
Console.Write("" + scanProgress + "% ");
59+
lastScanProgress = scanProgress;
60+
}
5861
}
5962
}
60-
}
6163

62-
_precursorScanNumber = 0;
64+
_precursorScanNumber = 0;
6365

64-
// Get the scan from the RAW file
65-
var scan = Scan.FromFile(rawFile, scanNumber);
66+
// Get the scan from the RAW file
67+
var scan = Scan.FromFile(rawFile, scanNumber);
6668

67-
// Get the retention time
68-
var retentionTime = rawFile.RetentionTimeFromScanNumber(scanNumber);
69+
// Get the retention time
70+
var retentionTime = rawFile.RetentionTimeFromScanNumber(scanNumber);
6971

70-
// Get the scan filter for this scan number
71-
var scanFilter = rawFile.GetFilterForScanNumber(scanNumber);
72+
// Get the scan filter for this scan number
73+
var scanFilter = rawFile.GetFilterForScanNumber(scanNumber);
7274

73-
// Get the scan event for this scan number
74-
var scanEvent = rawFile.GetScanEventForScanNumber(scanNumber);
75+
// Get the scan event for this scan number
76+
var scanEvent = rawFile.GetScanEventForScanNumber(scanNumber);
7577

76-
// Trailer extra data list
77-
ScanTrailer trailerData;
78+
// Trailer extra data list
79+
ScanTrailer trailerData;
7880

79-
try
80-
{
81-
trailerData = new ScanTrailer(rawFile.GetTrailerExtraInformation(scanNumber));
82-
}
83-
catch (Exception ex)
84-
{
85-
Log.WarnFormat("Cannot load trailer infromation for scan {0} due to following exception\n{1}", scanNumber, ex.Message);
86-
ParseInput.NewWarn();
87-
trailerData = new ScanTrailer();
88-
}
89-
90-
// Get scan ms level
91-
var msLevel = (int)scanFilter.MSOrder;
92-
93-
// Construct the precursor reference string for the title
94-
var precursorReference = "";
95-
96-
if (ParseInput.MgfPrecursor)
97-
{
98-
if (msLevel == 1)
81+
try
9982
{
100-
// Keep track of the MS1 scan number for precursor reference
101-
_precursorScanNumbers[""] = scanNumber;
83+
trailerData = new ScanTrailer(rawFile.GetTrailerExtraInformation(scanNumber));
10284
}
103-
else
85+
catch (Exception ex)
10486
{
105-
// Keep track of scan number and isolation m/z for precursor reference
106-
var result = _filterStringIsolationMzPattern.Match(scanEvent.ToString());
107-
if (result.Success)
108-
{
109-
if (_precursorScanNumbers.ContainsKey(result.Groups[1].Value))
110-
{
111-
_precursorScanNumbers.Remove(result.Groups[1].Value);
112-
}
87+
Log.WarnFormat("Cannot load trailer infromation for scan {0} due to following exception\n{1}", scanNumber, ex.Message);
88+
ParseInput.NewWarn();
89+
trailerData = new ScanTrailer();
90+
}
11391

114-
_precursorScanNumbers.Add(result.Groups[1].Value, scanNumber);
115-
}
92+
// Get scan ms level
93+
var msLevel = (int)scanFilter.MSOrder;
94+
95+
// Construct the precursor reference string for the title
96+
var precursorReference = "";
11697

117-
//update precursor scan if it is provided in trailer data
118-
var trailerMasterScan = trailerData.AsPositiveInt("Master Scan Number:");
119-
if (trailerMasterScan.HasValue)
98+
if (ParseInput.MgfPrecursor)
99+
{
100+
if (msLevel == 1)
120101
{
121-
_precursorScanNumber = trailerMasterScan.Value;
102+
// Keep track of the MS1 scan number for precursor reference
103+
_precursorScanNumbers[""] = scanNumber;
122104
}
123-
else //try getting it from the scan filter
105+
else
124106
{
125-
var parts = Regex.Split(result.Groups[1].Value, " ");
126-
127-
//find the position of the first (from the end) precursor with a different mass
128-
//to account for possible supplementary activations written in the filter
129-
var lastIonMass = parts.Last().Split('@').First();
130-
int last = parts.Length;
131-
while (last > 0 &&
132-
parts[last - 1].Split('@').First() == lastIonMass)
107+
// Keep track of scan number and isolation m/z for precursor reference
108+
var result = _filterStringIsolationMzPattern.Match(scanEvent.ToString());
109+
if (result.Success)
133110
{
134-
last--;
111+
if (_precursorScanNumbers.ContainsKey(result.Groups[1].Value))
112+
{
113+
_precursorScanNumbers.Remove(result.Groups[1].Value);
114+
}
115+
116+
_precursorScanNumbers.Add(result.Groups[1].Value, scanNumber);
135117
}
136118

137-
string parentFilter = String.Join(" ", parts.Take(last));
138-
if (_precursorScanNumbers.ContainsKey(parentFilter))
119+
//update precursor scan if it is provided in trailer data
120+
var trailerMasterScan = trailerData.AsPositiveInt("Master Scan Number:");
121+
if (trailerMasterScan.HasValue)
139122
{
140-
_precursorScanNumber = _precursorScanNumbers[parentFilter];
123+
_precursorScanNumber = trailerMasterScan.Value;
124+
}
125+
else //try getting it from the scan filter
126+
{
127+
var parts = Regex.Split(result.Groups[1].Value, " ");
128+
129+
//find the position of the first (from the end) precursor with a different mass
130+
//to account for possible supplementary activations written in the filter
131+
var lastIonMass = parts.Last().Split('@').First();
132+
int last = parts.Length;
133+
while (last > 0 &&
134+
parts[last - 1].Split('@').First() == lastIonMass)
135+
{
136+
last--;
137+
}
138+
139+
string parentFilter = String.Join(" ", parts.Take(last));
140+
if (_precursorScanNumbers.ContainsKey(parentFilter))
141+
{
142+
_precursorScanNumber = _precursorScanNumbers[parentFilter];
143+
}
144+
}
145+
146+
if (_precursorScanNumber > 0)
147+
{
148+
precursorReference = ConstructSpectrumTitle((int)Device.MS, 1, _precursorScanNumber);
149+
}
150+
else
151+
{
152+
Log.Error($"Failed finding precursor for {scanNumber}");
153+
ParseInput.NewError();
141154
}
142155
}
156+
}
143157

144-
if (_precursorScanNumber > 0)
158+
if (ParseInput.MsLevel.Contains(msLevel))
159+
{
160+
var reaction = GetReaction(scanEvent, scanNumber);
161+
162+
Writer.WriteLine("BEGIN IONS");
163+
if (!ParseInput.MgfPrecursor)
145164
{
146-
precursorReference = ConstructSpectrumTitle((int)Device.MS, 1, _precursorScanNumber);
165+
Writer.WriteLine($"TITLE={ConstructSpectrumTitle((int)Device.MS, 1, scanNumber)}");
147166
}
148167
else
149168
{
150-
Log.Error($"Failed finding precursor for {scanNumber}");
151-
ParseInput.NewError();
169+
Writer.WriteLine(
170+
$"TITLE={ConstructSpectrumTitle((int)Device.MS, 1, scanNumber)} [PRECURSOR={precursorReference}]");
152171
}
153-
}
154-
}
155172

156-
if (ParseInput.MsLevel.Contains(msLevel))
157-
{
158-
var reaction = GetReaction(scanEvent, scanNumber);
159-
160-
Writer.WriteLine("BEGIN IONS");
161-
if (!ParseInput.MgfPrecursor)
162-
{
163-
Writer.WriteLine($"TITLE={ConstructSpectrumTitle((int) Device.MS, 1, scanNumber)}");
164-
}
165-
else
166-
{
173+
Writer.WriteLine($"SCANS={scanNumber}");
167174
Writer.WriteLine(
168-
$"TITLE={ConstructSpectrumTitle((int) Device.MS, 1, scanNumber)} [PRECURSOR={precursorReference}]");
169-
}
170-
171-
Writer.WriteLine($"SCANS={scanNumber}");
172-
Writer.WriteLine(
173-
$"RTINSECONDS={(retentionTime * 60).ToString(CultureInfo.InvariantCulture)}");
175+
$"RTINSECONDS={(retentionTime * 60).ToString(CultureInfo.InvariantCulture)}");
174176

175-
int? charge = trailerData.AsPositiveInt("Charge State:");
176-
double? monoisotopicMz = trailerData.AsDouble("Monoisotopic M/Z:");
177-
double? isolationWidth =
178-
trailerData.AsDouble("MS" + msLevel + " Isolation Width:");
177+
int? charge = trailerData.AsPositiveInt("Charge State:");
178+
double? monoisotopicMz = trailerData.AsDouble("Monoisotopic M/Z:");
179+
double? isolationWidth =
180+
trailerData.AsDouble("MS" + msLevel + " Isolation Width:");
179181

180-
if (reaction != null)
181-
{
182-
var selectedIonMz =
183-
CalculateSelectedIonMz(reaction, monoisotopicMz, isolationWidth);
182+
if (reaction != null)
183+
{
184+
var selectedIonMz =
185+
CalculateSelectedIonMz(reaction, monoisotopicMz, isolationWidth);
184186

185-
Writer.WriteLine("PEPMASS=" +
186-
selectedIonMz.ToString(CultureInfo.InvariantCulture));
187-
}
187+
Writer.WriteLine("PEPMASS=" +
188+
selectedIonMz.ToString(CultureInfo.InvariantCulture));
189+
}
188190

189-
// Charge
190-
if (charge != null)
191-
{
192-
// Scan polarity
193-
var polarity = PositivePolarity;
194-
if (scanFilter.Polarity == PolarityType.Negative)
191+
// Charge
192+
if (charge != null)
195193
{
196-
polarity = NegativePolarity;
197-
}
194+
// Scan polarity
195+
var polarity = PositivePolarity;
196+
if (scanFilter.Polarity == PolarityType.Negative)
197+
{
198+
polarity = NegativePolarity;
199+
}
198200

199-
Writer.WriteLine($"CHARGE={charge}{polarity}");
200-
}
201+
Writer.WriteLine($"CHARGE={charge}{polarity}");
202+
}
201203

202-
// Write the filter string
203-
//Writer.WriteLine($"SCANEVENT={scanEvent.ToString()}");
204+
// Write the filter string
205+
//Writer.WriteLine($"SCANEVENT={scanEvent.ToString()}");
204206

205-
double[] masses;
206-
double[] intensities;
207+
double[] masses;
208+
double[] intensities;
207209

208-
if (!ParseInput.NoPeakPicking.Contains(msLevel))
209-
{
210-
// Check if the scan has a centroid stream
211-
if (scan.HasCentroidStream)
210+
if (!ParseInput.NoPeakPicking.Contains(msLevel))
212211
{
213-
masses = scan.CentroidScan.Masses;
214-
intensities = scan.CentroidScan.Intensities;
212+
// Check if the scan has a centroid stream
213+
if (scan.HasCentroidStream)
214+
{
215+
masses = scan.CentroidScan.Masses;
216+
intensities = scan.CentroidScan.Intensities;
217+
}
218+
else // Otherwise take segmented (low res) scan data
219+
{
220+
// If the spectrum is profile perform centroiding
221+
var segmentedScan = scanEvent.ScanData == ScanDataType.Profile
222+
? Scan.ToCentroid(scan).SegmentedScan
223+
: scan.SegmentedScan;
224+
225+
masses = segmentedScan.Positions;
226+
intensities = segmentedScan.Intensities;
227+
}
215228
}
216-
else // Otherwise take segmented (low res) scan data
229+
else // Use the segmented data as is
217230
{
218-
// If the spectrum is profile perform centroiding
219-
var segmentedScan = scanEvent.ScanData == ScanDataType.Profile
220-
? Scan.ToCentroid(scan).SegmentedScan
221-
: scan.SegmentedScan;
222-
223-
masses = segmentedScan.Positions;
224-
intensities = segmentedScan.Intensities;
231+
masses = scan.SegmentedScan.Positions;
232+
intensities = scan.SegmentedScan.Intensities;
225233
}
226-
}
227-
else // Use the segmented data as is
228-
{
229-
masses = scan.SegmentedScan.Positions;
230-
intensities = scan.SegmentedScan.Intensities;
231-
}
232-
233-
if (!(masses is null) && masses.Length > 0)
234-
{
235-
Array.Sort(masses, intensities);
236234

237-
for (var i = 0; i < masses.Length; i++)
235+
if (!(masses is null) && masses.Length > 0)
238236
{
239-
Writer.WriteLine(String.Format("{0:f5} {1:f3}", masses[i], intensities[i]));
237+
Array.Sort(masses, intensities);
238+
239+
for (var i = 0; i < masses.Length; i++)
240+
{
241+
Writer.WriteLine(String.Format("{0:f5} {1:f3}", masses[i], intensities[i]));
242+
}
240243
}
241-
}
242244

243-
Writer.WriteLine("END IONS");
245+
Writer.WriteLine("END IONS");
244246

245-
Log.Debug("Spectrum written to file -- SCAN# " + scanNumber);
247+
Log.Debug("Spectrum written to file -- SCAN# " + scanNumber);
248+
}
246249
}
247250
}
248251

0 commit comments

Comments
 (0)