Skip to content

Commit b8b0c8e

Browse files
U-NAT-SDU\vgorU-NAT-SDU\vgor
authored andcommitted
Two methods added and isolotaionwidth bug fixed
1 parent d763546 commit b8b0c8e

File tree

2 files changed

+90
-45
lines changed

2 files changed

+90
-45
lines changed

Writer/MzMlSpectrumWriter.cs

Lines changed: 31 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1146,6 +1146,7 @@ private SpectrumType ConstructMSSpectrum(int scanNumber)
11461146
int? charge = null;
11471147
double? monoisotopicMz = null;
11481148
double? ionInjectionTime = null;
1149+
double? isolationWidth = null;
11491150
double? FAIMSCV = null;
11501151
List<double> SPSMasses = new List<double>();
11511152

@@ -1173,6 +1174,8 @@ private SpectrumType ConstructMSSpectrum(int scanNumber)
11731174
}
11741175

11751176
/*var trailerData = _rawFile.GetTrailerExtraInformation(scanNumber);
1177+
double? isolationWidth = null;
1178+
List<double> SPSMasses = new List<double>();
11761179
for (var i = 0; i < trailerData.Length; i++)
11771180
{
11781181
if (trailerData.Labels[i] == "Charge State:")
@@ -1183,6 +1186,11 @@ private SpectrumType ConstructMSSpectrum(int scanNumber)
11831186
}
11841187
}
11851188
1189+
if (trailerData.Labels[i] == "MS" + (int)scanFilter.MSOrder + " Isolation Width:")
1190+
{
1191+
isolationWidth = double.Parse(trailerData.Values[i], NumberStyles.Any);
1192+
}
1193+
11861194
if (trailerData.Labels[i] == "Monoisotopic M/Z:")
11871195
{
11881196
monoisotopicMz = double.Parse(trailerData.Values[i], NumberStyles.Any,
@@ -1267,7 +1275,7 @@ private SpectrumType ConstructMSSpectrum(int scanNumber)
12671275

12681276
// Construct and set the precursor list element of the spectrum
12691277
var precursorListType =
1270-
ConstructPrecursorList(scanEvent, charge, scanFilter.MSOrder, monoisotopicMz, SPSMasses);
1278+
ConstructPrecursorList(scanEvent, charge, scanFilter.MSOrder, monoisotopicMz, isolationWidth, SPSMasses);
12711279
spectrum.precursorList = precursorListType;
12721280
break;
12731281
case MSOrderType.Ms3:
@@ -1278,7 +1286,7 @@ private SpectrumType ConstructMSSpectrum(int scanNumber)
12781286
name = "MSn spectrum",
12791287
value = ""
12801288
});
1281-
precursorListType = ConstructPrecursorList(scanEvent, charge, scanFilter.MSOrder, monoisotopicMz, SPSMasses);
1289+
precursorListType = ConstructPrecursorList(scanEvent, charge, scanFilter.MSOrder, monoisotopicMz, isolationWidth, SPSMasses);
12821290
spectrum.precursorList = precursorListType;
12831291
break;
12841292
default:
@@ -1821,9 +1829,10 @@ private SpectrumType ConstructPDASpectrum(int scanNumber, int instrumentNumber)
18211829
/// <param name="msLevel">the MS level</param>
18221830
/// <param name="monoisotopicMz">the monoisotopic m/z value</param>
18231831
/// <param name="isolationWidth">the isolation width</param>
1832+
/// <param name="SPSMasses">masses selected for SPS</param>
18241833
/// <returns>the precursor list</returns>
18251834
private PrecursorListType ConstructPrecursorList(IScanEventBase scanEvent, int? charge, MSOrderType msLevel,
1826-
double? monoisotopicMz, List<double> SPSMasses)
1835+
double? monoisotopicMz, double? isolationWidth, List<double> SPSMasses)
18271836
{
18281837
// Construct the precursor
18291838
var precursorList = new PrecursorListType
@@ -1836,7 +1845,7 @@ private PrecursorListType ConstructPrecursorList(IScanEventBase scanEvent, int?
18361845
int precursorScanNumber = _precursorMs1ScanNumber;
18371846
IReaction reaction = null;
18381847
var precursorMz = 0.0;
1839-
double? isolationWidth = null;
1848+
18401849
try
18411850
{
18421851
switch (msLevel)
@@ -1866,7 +1875,6 @@ private PrecursorListType ConstructPrecursorList(IScanEventBase scanEvent, int?
18661875
}
18671876

18681877
precursorMz = reaction.PrecursorMass;
1869-
isolationWidth = reaction.IsolationWidth;
18701878
}
18711879
catch (ArgumentOutOfRangeException)
18721880
{
@@ -1910,23 +1918,24 @@ private PrecursorListType ConstructPrecursorList(IScanEventBase scanEvent, int?
19101918
}
19111919

19121920
//Precursor intensity is disabled for now
1913-
//if (selectedIonMz > ZeroDelta)
1914-
//{
1915-
// var selectedIonIntensity = CalculatePrecursorPeakIntensity(_rawFile, precursorScanNumber, selectedIonMz);
1916-
// if (selectedIonIntensity != null)
1917-
// {
1918-
// ionCvParams.Add(new CVParamType
1919-
// {
1920-
// name = "peak intensity",
1921-
// value = selectedIonIntensity.ToString(),
1922-
// accession = "MS:1000042",
1923-
// cvRef = "MS",
1924-
// unitAccession = "MS:1000131",
1925-
// unitCvRef = "MS",
1926-
// unitName = "number of detector counts"
1927-
// });
1928-
// }
1929-
//}
1921+
if (selectedIonMz > ZeroDelta)
1922+
{
1923+
var selectedIonIntensity = CalculatePrecursorPeakIntensity(_rawFile, precursorScanNumber, selectedIonMz, isolationWidth, ParseInput.NoPeakPicking); //new Pwiz way
1924+
//var selectedIonIntensity = CalculatePrecursorPeakIntensity2(_rawFile, precursorScanNumber, selectedIonMz, isolationWidth); //old Pwiz way
1925+
if (selectedIonIntensity != null)
1926+
{
1927+
ionCvParams.Add(new CVParamType
1928+
{
1929+
name = "peak intensity",
1930+
value = selectedIonIntensity.ToString(),
1931+
accession = "MS:1000042",
1932+
cvRef = "MS",
1933+
unitAccession = "MS:1000131",
1934+
unitCvRef = "MS",
1935+
unitName = "number of detector counts"
1936+
});
1937+
}
1938+
}
19301939

19311940
precursor.selectedIonList.selectedIon[0].cvParam = ionCvParams.ToArray();
19321941

Writer/SpectrumWriter.cs

Lines changed: 59 additions & 23 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,84 @@ 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;
189-
191+
double precursorIntensity = 0;
192+
double halfWidth = isolationWidth is null ? 0 : (double) isolationWidth / 2; //if isolationWidth was not defined set it to 0
193+
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)
216209
{
217-
bestDelta = delta;
218-
precursorIntensity = intensities[i];
210+
masses = scan.CentroidScan.Masses;
211+
intensities = scan.CentroidScan.Intensities;
219212
}
213+
else
214+
{
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;
222+
}
223+
}
224+
225+
var index = masses.FastBinarySearch(precursorMass - halfWidth); //set index to the first peak inside isolation window
226+
227+
while (index < masses.Length && masses[index] < precursorMass + halfWidth)
228+
{
229+
precursorIntensity += intensities[index];
230+
index++;
231+
}
232+
233+
return precursorIntensity;
234+
}
220235

221-
if (delta < -1 * tolerance) break;
236+
/// <summary>
237+
/// Calculate the precursor peak intensity (similar to leagcy MSConvert).
238+
/// Get base-peak intensity inside isolation window.
239+
/// </summary>
240+
/// <param name="rawFile">the RAW file object</param>
241+
/// <param name="precursorScanNumber">the precursor scan number</param>
242+
/// <param name="precursorMass">the precursor mass</param>
243+
/// <param name="isolationWidth">the isolation width</param>
244+
protected static double? CalculatePrecursorPeakIntensity2(IRawDataPlus rawFile, int precursorScanNumber, double precursorMass, double? isolationWidth)
245+
{
246+
double? precursorIntensity = null;
247+
double halfWidth = isolationWidth is null ? 0 : (double)isolationWidth / 2; //if isolationWidth was not defined set it to 0
248+
249+
var chrosettings = new ChromatogramTraceSettings();
250+
chrosettings.MassRanges = new Range[] { new Range(precursorMass - halfWidth, precursorMass + halfWidth) };
251+
chrosettings.Trace = TraceType.BasePeak;
252+
253+
var chrodata = rawFile.GetChromatogramData(new ChromatogramTraceSettings[] { chrosettings }, precursorScanNumber, precursorScanNumber);
254+
255+
if (chrodata != null && chrodata.Length > 0)
256+
{
257+
precursorIntensity = chrodata.IntensitiesArray[0][0];
222258
}
223259

224260
return precursorIntensity;

0 commit comments

Comments
 (0)