Skip to content

Commit 75876e2

Browse files
committed
Precursor Intensity
1 parent fadd141 commit 75876e2

2 files changed

Lines changed: 49 additions & 40 deletions

File tree

Writer/MzMlSpectrumWriter.cs

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -2029,24 +2029,24 @@ private PrecursorListType ConstructPrecursorList(IScanEventBase scanEvent, int?
20292029
});
20302030
}
20312031

2032-
//Precursor intensity is disabled for now
2033-
//if (selectedIonMz > ZeroDelta)
2034-
//{
2035-
// var selectedIonIntensity = CalculatePrecursorPeakIntensity(_rawFile, precursorScanNumber, selectedIonMz);
2036-
// if (selectedIonIntensity != null)
2037-
// {
2038-
// ionCvParams.Add(new CVParamType
2039-
// {
2040-
// name = "peak intensity",
2041-
// value = selectedIonIntensity.ToString(),
2042-
// accession = "MS:1000042",
2043-
// cvRef = "MS",
2044-
// unitAccession = "MS:1000131",
2045-
// unitCvRef = "MS",
2046-
// unitName = "number of detector counts"
2047-
// });
2048-
// }
2049-
//}
2032+
if (selectedIonMz > ZeroDelta)
2033+
{
2034+
var selectedIonIntensity = CalculatePrecursorPeakIntensity(_rawFile, precursorScanNumber, selectedIonMz, isolationWidth,
2035+
ParseInput.NoPeakPicking.Contains((int)msLevel - 1));
2036+
if (selectedIonIntensity != null)
2037+
{
2038+
ionCvParams.Add(new CVParamType
2039+
{
2040+
name = "peak intensity",
2041+
value = selectedIonIntensity.ToString(),
2042+
accession = "MS:1000042",
2043+
cvRef = "MS",
2044+
unitAccession = "MS:1000131",
2045+
unitCvRef = "MS",
2046+
unitName = "number of detector counts"
2047+
});
2048+
}
2049+
}
20502050

20512051
precursor.selectedIonList.selectedIon[0].cvParam = ionCvParams.ToArray();
20522052

Writer/SpectrumWriter.cs

Lines changed: 31 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
using log4net;
66
using ThermoFisher.CommonCore.Data;
77
using ThermoFisher.CommonCore.Data.Business;
8+
using ThermoFisher.CommonCore.Data.FilterEnums;
89
using ThermoFisher.CommonCore.Data.Interfaces;
910

1011
namespace ThermoRawFileParser.Writer
@@ -176,49 +177,57 @@ public static IReaction GetReaction(IScanEvent scanEvent, int scanNumber)
176177
}
177178

178179
/// <summary>
179-
/// Calculate the precursor peak intensity.
180+
/// Calculate the precursor peak intensity (similar to modern MSConvert).
181+
/// Sum intensities of all peaks in the isolation window.
180182
/// </summary>
181183
/// <param name="rawFile">the RAW file object</param>
182184
/// <param name="precursorScanNumber">the precursor scan number</param>
183185
/// <param name="precursorMass">the precursor mass</param>
186+
/// <param name="isolationWidth">the isolation width</param>
187+
/// <param name="useProfile">profile/centroid switch</param>
184188
protected static double? CalculatePrecursorPeakIntensity(IRawDataPlus rawFile, int precursorScanNumber,
185-
double precursorMass)
189+
double precursorMass, double? isolationWidth, bool useProfile)
186190
{
187-
double? precursorIntensity = null;
188-
double tolerance;
191+
double precursorIntensity = 0;
192+
double halfWidth = isolationWidth is null || isolationWidth == 0 ? 0 : DefaultIsolationWindowLowerOffset; // that is how it is made in MSConvert (why?)
189193

190194
// Get the precursor scan from the RAW file
191195
var scan = Scan.FromFile(rawFile, precursorScanNumber);
192196

193-
//Select centroid stream if it exists, otherwise use profile one
194197
double[] masses;
195198
double[] intensities;
196199

197-
if (scan.HasCentroidStream)
198-
{
199-
masses = scan.CentroidScan.Masses;
200-
intensities = scan.CentroidScan.Intensities;
201-
tolerance = 0.01; //high resolution scan
202-
}
203-
else
200+
if (useProfile)
204201
{
205202
masses = scan.SegmentedScan.Positions;
206203
intensities = scan.SegmentedScan.Intensities;
207-
tolerance = 0.5; //low resolution scan
208204
}
209-
210-
//find closest peak in a stream
211-
var bestDelta = tolerance;
212-
for (var i = 0; i < masses.Length; i++)
205+
else
213206
{
214-
var delta = precursorMass - masses[i];
215-
if (Math.Abs(delta) < bestDelta)
207+
//use centroids if they exist, otherwise perform centroiding
208+
if (scan.HasCentroidStream)
209+
{
210+
masses = scan.CentroidScan.Masses;
211+
intensities = scan.CentroidScan.Intensities;
212+
}
213+
else
216214
{
217-
bestDelta = delta;
218-
precursorIntensity = intensities[i];
215+
var scanEvent = rawFile.GetScanEventForScanNumber(precursorScanNumber);
216+
var centroidedScan = scanEvent.ScanData == ScanDataType.Profile //only centroid profile spectra
217+
? Scan.ToCentroid(scan).SegmentedScan
218+
: scan.SegmentedScan;
219+
220+
masses = centroidedScan.Positions;
221+
intensities = centroidedScan.Intensities;
219222
}
223+
}
224+
225+
var index = masses.FastBinarySearch(precursorMass - halfWidth); //set index to the first peak inside isolation window
220226

221-
if (delta < -1 * tolerance) break;
227+
while (index > 0 && index < masses.Length && masses[index] < precursorMass + halfWidth) //negative index means value was not found
228+
{
229+
precursorIntensity += intensities[index];
230+
index++;
222231
}
223232

224233
return precursorIntensity;

0 commit comments

Comments
 (0)