Skip to content

Commit 229e1f0

Browse files
committed
PDA scans refine + selected ion intensity
1 parent d60d320 commit 229e1f0

2 files changed

Lines changed: 102 additions & 96 deletions

File tree

Writer/MzMlSpectrumWriter.cs

Lines changed: 82 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -91,8 +91,6 @@ public override void Write(IRawDataPlus rawFile, int firstScanNumber, int lastSc
9191

9292
try
9393
{
94-
Log.Info("Processing " + (lastScanNumber - firstScanNumber) + " scans");
95-
9694
_writer.WriteStartDocument();
9795

9896
if (_doIndexing)
@@ -267,6 +265,9 @@ public override void Write(IRawDataPlus rawFile, int firstScanNumber, int lastSc
267265
//MS Spectra
268266
var index = 0;
269267
var lastScanProgress = 0;
268+
269+
Log.Info("Processing " + (lastScanNumber - firstScanNumber) + " MS scans");
270+
270271
for (var scanNumber = firstScanNumber; scanNumber <= lastScanNumber; scanNumber++)
271272
{
272273
if (ParseInput.LogFormat == LogFormat.DEFAULT)
@@ -309,6 +310,11 @@ public override void Write(IRawDataPlus rawFile, int firstScanNumber, int lastSc
309310
}
310311
}
311312

313+
if (ParseInput.LogFormat == LogFormat.DEFAULT)
314+
{
315+
Console.WriteLine();
316+
}
317+
312318
// PDA spectra
313319
if (ParseInput.AllDetectors && _rawFile.GetInstrumentCountOfType(Device.Pda) > 0)
314320
{
@@ -319,6 +325,8 @@ public override void Write(IRawDataPlus rawFile, int firstScanNumber, int lastSc
319325
lastScanNumber = _rawFile.RunHeader.LastSpectrum;
320326
lastScanProgress = 0;
321327

328+
Log.Info("Processing " + (lastScanNumber - firstScanNumber) + " PDA scans from Device #" + (nrI + 1));
329+
322330
for (var scanNumber = firstScanNumber; scanNumber <= lastScanNumber; scanNumber++)
323331
{
324332
if (ParseInput.LogFormat == LogFormat.DEFAULT)
@@ -362,6 +370,7 @@ public override void Write(IRawDataPlus rawFile, int firstScanNumber, int lastSc
362370
}
363371
}
364372
}
373+
365374
if (ParseInput.LogFormat == LogFormat.DEFAULT)
366375
{
367376
Console.WriteLine();
@@ -373,7 +382,7 @@ public override void Write(IRawDataPlus rawFile, int firstScanNumber, int lastSc
373382
var chromatograms = ConstructChromatograms(firstScanNumber, lastScanNumber);
374383
if (!chromatograms.IsNullOrEmpty())
375384
{
376-
// chromatogramList
385+
//chromatogramList
377386
_writer.WriteStartElement("chromatogramList");
378387
_writer.WriteAttributeString("count", chromatograms.Count.ToString());
379388
_writer.WriteAttributeString("defaultDataProcessingRef", "ThermoRawFileParserProcessing");
@@ -1380,34 +1389,34 @@ private SpectrumType ConstructPDASpectrum(int scanNumber, int instrumentNumber)
13801389
});
13811390

13821391
//Scan data
1383-
double? basePeakMass = null;
1392+
double? basePeakPosition = null;
13841393
double? basePeakIntensity = null;
1385-
double? lowestObservedMz = null;
1386-
double? highestObservedMz = null;
1387-
double[] masses = null;
1394+
double? lowestPosition = null;
1395+
double? highestPosition = null;
1396+
double[] positions = null;
13881397
double[] intensities = null;
13891398

13901399

1391-
basePeakMass = scan.ScanStatistics.BasePeakMass;
1400+
basePeakPosition = scan.ScanStatistics.BasePeakMass;
13921401
basePeakIntensity = scan.ScanStatistics.BasePeakIntensity;
13931402

13941403
if (scan.SegmentedScan.Positions.Length > 0)
13951404
{
1396-
lowestObservedMz = scan.SegmentedScan.Positions[0];
1397-
highestObservedMz = scan.SegmentedScan.Positions[scan.SegmentedScan.Positions.Length - 1];
1398-
masses = scan.SegmentedScan.Positions;
1405+
lowestPosition = scan.SegmentedScan.Positions[0];
1406+
highestPosition = scan.SegmentedScan.Positions[scan.SegmentedScan.Positions.Length - 1];
1407+
positions = scan.SegmentedScan.Positions;
13991408
intensities = scan.SegmentedScan.Intensities;
14001409
}
14011410

14021411

14031412
// Base peak m/z
1404-
if (basePeakMass != null)
1413+
if (basePeakPosition != null)
14051414
{
14061415
spectrumCvParams.Add(new CVParamType
14071416
{
14081417
name = "base peak m/z",
14091418
accession = "MS:1000504",
1410-
value = basePeakMass.Value.ToString(CultureInfo.InvariantCulture),
1419+
value = basePeakPosition.Value.ToString(CultureInfo.InvariantCulture),
14111420
unitCvRef = "MS",
14121421
unitName = "m/z",
14131422
unitAccession = "MS:1000040",
@@ -1430,32 +1439,32 @@ private SpectrumType ConstructPDASpectrum(int scanNumber, int instrumentNumber)
14301439
});
14311440
}
14321441

1433-
// Lowest observed mz
1434-
if (lowestObservedMz != null)
1442+
// Lowest observed wavelength
1443+
if (lowestPosition != null)
14351444
{
14361445
spectrumCvParams.Add(new CVParamType
14371446
{
1438-
name = "lowest observed m/z",
1439-
accession = "MS:1000528",
1440-
value = lowestObservedMz.Value.ToString(CultureInfo.InvariantCulture),
1447+
name = "lowest observed wavelength",
1448+
accession = "MS:1000619",
1449+
value = lowestPosition.Value.ToString(CultureInfo.InvariantCulture),
14411450
unitCvRef = "MS",
1442-
unitAccession = "MS:1000040",
1443-
unitName = "m/z",
1444-
cvRef = "MS"
1451+
unitAccession = "UO:0000018",
1452+
unitName = "nanometer",
1453+
cvRef = "UO"
14451454
});
14461455
}
14471456

1448-
// Highest observed mz
1449-
if (highestObservedMz != null)
1457+
// Highest observed wavelength
1458+
if (highestPosition != null)
14501459
{
14511460
spectrumCvParams.Add(new CVParamType
14521461
{
1453-
name = "highest observed m/z",
1454-
accession = "MS:1000527",
1455-
value = highestObservedMz.Value.ToString(CultureInfo.InvariantCulture),
1456-
unitAccession = "MS:1000040",
1457-
unitName = "m/z",
1458-
unitCvRef = "MS",
1462+
name = "highest observed wavelength",
1463+
accession = "MS:1000618",
1464+
value = highestPosition.Value.ToString(CultureInfo.InvariantCulture),
1465+
unitAccession = "UO:0000018",
1466+
unitName = "nanometer",
1467+
unitCvRef = "UO",
14591468
cvRef = "MS"
14601469
});
14611470
}
@@ -1466,37 +1475,37 @@ private SpectrumType ConstructPDASpectrum(int scanNumber, int instrumentNumber)
14661475
// Binary data array list
14671476
var binaryData = new List<BinaryDataArrayType>();
14681477

1469-
// M/Z Data
1470-
if (!masses.IsNullOrEmpty())
1478+
// Spectral data
1479+
if (!positions.IsNullOrEmpty())
14711480
{
14721481
// Set the spectrum default array length
1473-
spectrum.defaultArrayLength = masses.Length;
1482+
spectrum.defaultArrayLength = positions.Length;
14741483

1475-
var massesBinaryData =
1484+
var positionsBinaryData =
14761485
new BinaryDataArrayType
14771486
{
1478-
binary = ParseInput.NoZlibCompression ? Get64BitArray(masses) : GetZLib64BitArray(masses)
1487+
binary = ParseInput.NoZlibCompression ? Get64BitArray(positions) : GetZLib64BitArray(positions)
14791488
};
1480-
massesBinaryData.encodedLength =
1481-
(4 * Math.Ceiling((double)massesBinaryData
1489+
positionsBinaryData.encodedLength =
1490+
(4 * Math.Ceiling((double)positionsBinaryData
14821491
.binary.Length / 3)).ToString(CultureInfo.InvariantCulture);
1483-
var massesBinaryDataCvParams = new List<CVParamType>
1492+
var positionsBinaryDataCvParams = new List<CVParamType>
14841493
{
14851494
new CVParamType
14861495
{
1487-
accession = "MS:1000514",
1488-
name = "m/z array",
1496+
accession = "MS:1000617",
1497+
name = "wavelength array",
14891498
cvRef = "MS",
1490-
unitName = "m/z",
1499+
unitName = "nanometer",
14911500
value = "",
1492-
unitCvRef = "MS",
1493-
unitAccession = "MS:1000040"
1501+
unitCvRef = "UO",
1502+
unitAccession = "UO:0000018"
14941503
},
14951504
new CVParamType {accession = "MS:1000523", name = "64-bit float", cvRef = "MS", value = ""}
14961505
};
14971506
if (!ParseInput.NoZlibCompression)
14981507
{
1499-
massesBinaryDataCvParams.Add(
1508+
positionsBinaryDataCvParams.Add(
15001509
new CVParamType
15011510
{
15021511
accession = "MS:1000574",
@@ -1506,9 +1515,9 @@ private SpectrumType ConstructPDASpectrum(int scanNumber, int instrumentNumber)
15061515
});
15071516
}
15081517

1509-
massesBinaryData.cvParam = massesBinaryDataCvParams.ToArray();
1518+
positionsBinaryData.cvParam = positionsBinaryDataCvParams.ToArray();
15101519

1511-
binaryData.Add(massesBinaryData);
1520+
binaryData.Add(positionsBinaryData);
15121521
}
15131522

15141523
// Intensity Data
@@ -1517,7 +1526,7 @@ private SpectrumType ConstructPDASpectrum(int scanNumber, int instrumentNumber)
15171526
// Set the spectrum default array length if necessary
15181527
if (spectrum.defaultArrayLength == 0)
15191528
{
1520-
spectrum.defaultArrayLength = masses.Length;
1529+
spectrum.defaultArrayLength = intensities.Length;
15211530
}
15221531

15231532
var intensitiesBinaryData =
@@ -1537,9 +1546,9 @@ private SpectrumType ConstructPDASpectrum(int scanNumber, int instrumentNumber)
15371546
accession = "MS:1000515",
15381547
name = "intensity array",
15391548
cvRef = "MS",
1540-
unitCvRef = "MS",
1541-
unitAccession = "MS:1000131",
1542-
unitName = "number of counts",
1549+
unitCvRef = "UO",
1550+
unitAccession = "UO:0000269",
1551+
unitName = "absorbance unit",
15431552
value = ""
15441553
},
15451554
new CVParamType {accession = "MS:1000523", name = "64-bit float", cvRef = "MS", value = ""}
@@ -1670,6 +1679,21 @@ private PrecursorListType ConstructPrecursorList(IScanEventBase scanEvent, int?
16701679
cvRef = "MS"
16711680
});
16721681
}
1682+
1683+
if (selectedIonMz > ZeroDelta)
1684+
{
1685+
var selectedIonIntensity = CalculatePrecursorPeakIntensity(_rawFile, precursorScanNumber, selectedIonMz);
1686+
ionCvParams.Add(new CVParamType
1687+
{
1688+
name = "peak intensity",
1689+
value = selectedIonIntensity.ToString(),
1690+
accession = "MS:1000042",
1691+
cvRef = "MS",
1692+
unitAccession = "MS:1000131",
1693+
unitCvRef = "MS",
1694+
unitName = "number of detector counts"
1695+
});
1696+
}
16731697

16741698
precursor.selectedIonList.selectedIon[0].cvParam = ionCvParams.ToArray();
16751699

@@ -1964,6 +1988,7 @@ private ScanListType ConstructScanList(int scanNumber, Scan scan)
19641988
value = ""
19651989
};
19661990

1991+
//scan start time
19671992
var scanTypeCvParams = new List<CVParamType>
19681993
{
19691994
new CVParamType
@@ -1979,10 +2004,6 @@ private ScanListType ConstructScanList(int scanNumber, Scan scan)
19792004
}
19802005
};
19812006

1982-
// Scan start time
1983-
1984-
// Scan filter string
1985-
19862007
var scanType = new ScanType
19872008
{
19882009
instrumentConfigurationRef = "null",
@@ -2003,21 +2024,21 @@ private ScanListType ConstructScanList(int scanNumber, Scan scan)
20032024
{
20042025
name = "scan window lower limit",
20052026
accession = "MS:1000501",
2006-
value = scan.ScanStatistics.LowMass.ToString(CultureInfo.InvariantCulture),
2027+
value = scan.ScanStatistics.ShortWavelength.ToString(CultureInfo.InvariantCulture),
20072028
cvRef = "MS",
2008-
unitAccession = "MS:1000040",
2009-
unitCvRef = "MS",
2010-
unitName = "m/z"
2029+
unitAccession = "UO:0000018",
2030+
unitCvRef = "UO",
2031+
unitName = "nanometer"
20112032
};
20122033
scanWindow.cvParam[1] = new CVParamType
20132034
{
20142035
name = "scan window upper limit",
20152036
accession = "MS:1000500",
2016-
value = scan.ScanStatistics.HighMass.ToString(CultureInfo.InvariantCulture),
2037+
value = scan.ScanStatistics.LongWavelength.ToString(CultureInfo.InvariantCulture),
20172038
cvRef = "MS",
2018-
unitAccession = "MS:1000040",
2019-
unitCvRef = "MS",
2020-
unitName = "m/z"
2039+
unitAccession = "UO:0000018",
2040+
unitCvRef = "UO",
2041+
unitName = "nanometer"
20212042
};
20222043

20232044
scanType.scanWindowList.scanWindow[0] = scanWindow;

Writer/SpectrumWriter.cs

Lines changed: 20 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -179,51 +179,36 @@ public static IReaction GetReaction(IScanEvent scanEvent, int scanNumber)
179179
{
180180
double? precursorIntensity = null;
181181

182-
// Get the scan from the RAW file
182+
// Get the precursor scan from the RAW file
183183
var scan = Scan.FromFile(rawFile, precursorScanNumber);
184184

185-
// Check if the scan has a centroid stream
185+
//Select centroid stream if it exists, otherwise use profile one
186+
double[] masses;
187+
double[] intensities;
188+
186189
if (scan.HasCentroidStream)
187190
{
188-
var centroidStream = rawFile.GetCentroidStream(precursorScanNumber, false);
189-
if (scan.CentroidScan.Length > 0)
190-
{
191-
for (var i = 0; i < centroidStream.Length; i++)
192-
{
193-
if (Math.Abs(precursorMass - centroidStream.Masses[i]) < Tolerance)
194-
{
195-
//Console.WriteLine(Math.Abs(precursorMass - centroidStream.Masses[i]));
196-
//Console.WriteLine(precursorMass + " - " + centroidStream.Masses[i] + " - " +
197-
// centroidStream.Intensities[i]);
198-
precursorIntensity = centroidStream.Intensities[i];
199-
break;
200-
}
201-
}
202-
}
191+
masses = scan.CentroidScan.Masses;
192+
intensities = scan.CentroidScan.Intensities;
203193
}
204194
else
205195
{
206-
rawFile.SelectInstrument(Device.MS, 1);
207-
208-
IChromatogramSettings[] allSettings =
209-
{
210-
new ChromatogramTraceSettings(TraceType.BasePeak)
211-
{
212-
Filter = MsFilter,
213-
MassRanges = new[]
214-
{
215-
new Range(precursorMass, precursorMass)
216-
}
217-
}
218-
};
196+
masses = scan.SegmentedScan.Positions;
197+
intensities = scan.SegmentedScan.Intensities;
198+
}
219199

220-
var data = rawFile.GetChromatogramData(allSettings, precursorScanNumber,
221-
precursorScanNumber);
222-
var chromatogramTrace = ChromatogramSignal.FromChromatogramData(data);
223-
if (!chromatogramTrace.IsNullOrEmpty())
200+
//find closest peak in a stream
201+
var bestDelta = Tolerance;
202+
for (var i = 0; i < masses.Length; i++)
203+
{
204+
var delta = precursorMass - masses[i];
205+
if (Math.Abs(delta) < bestDelta)
224206
{
225-
precursorIntensity = chromatogramTrace[0].Intensities[0];
207+
bestDelta = delta;
208+
precursorIntensity = intensities[i];
226209
}
210+
211+
if (delta < -1 * Tolerance) break;
227212
}
228213

229214
return precursorIntensity;

0 commit comments

Comments
 (0)