Skip to content

Commit 56b19ae

Browse files
committed
Cache mz-int arrays instead of full scans
1 parent 36ff5ff commit 56b19ae

File tree

3 files changed

+41
-30
lines changed

3 files changed

+41
-30
lines changed

ThermoRawFileParser.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -197,6 +197,7 @@
197197
<Compile Include="RawFileParserException.cs" />
198198
<Compile Include="RawFileParser.cs" />
199199
<Compile Include="Util\LimitedSizeDictionary.cs" />
200+
<Compile Include="Util\MZArray.cs" />
200201
<Compile Include="Util\Peptide.cs" />
201202
<Compile Include="Writer\ScanTrailer.cs" />
202203
<Compile Include="XIC\JSONInputUnit.cs" />

Util/MZArray.cs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
namespace ThermoRawFileParser.Util
2+
{
3+
struct MZArray
4+
{
5+
public double[] Masses { get; set; }
6+
public double[] Intensities { get; set; }
7+
}
8+
}

Writer/SpectrumWriter.cs

Lines changed: 32 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,6 @@ public abstract class SpectrumWriter : ISpectrumWriter
1616
private static readonly ILog Log =
1717
LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
1818

19-
private const string MsFilter = "ms";
20-
private const double Tolerance = 0.01;
2119
protected const double ZeroDelta = 0.0001;
2220

2321
/// <summary>
@@ -42,7 +40,7 @@ public abstract class SpectrumWriter : ISpectrumWriter
4240
/// <summary>
4341
/// Precursor cache
4442
/// </summary>
45-
private static LimitedSizeDictionary<int, Scan> precursorCache;
43+
private static LimitedSizeDictionary<int, MZArray> precursorCache;
4644

4745
/// <summary>
4846
/// Constructor.
@@ -51,7 +49,7 @@ public abstract class SpectrumWriter : ISpectrumWriter
5149
protected SpectrumWriter(ParseInput parseInput)
5250
{
5351
ParseInput = parseInput;
54-
precursorCache = new LimitedSizeDictionary<int, Scan>(10);
52+
precursorCache = new LimitedSizeDictionary<int, MZArray>(10);
5553
}
5654

5755
/// <inheritdoc />
@@ -198,41 +196,45 @@ public static IReaction GetReaction(IScanEvent scanEvent, int scanNumber)
198196
double precursorIntensity = 0;
199197
double halfWidth = isolationWidth is null || isolationWidth == 0 ? 0 : DefaultIsolationWindowLowerOffset; // that is how it is made in MSConvert (why?)
200198

201-
// Get the precursor scan from the RAW file or cache
202-
Scan scan;
199+
double[] masses;
200+
double[] intensities;
201+
202+
// Get the mz-array from RAW file or cache
203203
if (precursorCache.ContainsKey(precursorScanNumber))
204204
{
205-
scan = precursorCache[precursorScanNumber];
205+
masses = precursorCache[precursorScanNumber].Masses;
206+
intensities = precursorCache[precursorScanNumber].Intensities;
206207
}
207208
else
208209
{
209-
scan = Scan.FromFile(rawFile, precursorScanNumber);
210-
//check if it is necessary to centroid a profile scan
211-
if (!useProfile && !scan.HasCentroidStream)
212-
{
213-
var scanEvent = rawFile.GetScanEventForScanNumber(precursorScanNumber);
214-
var centroidedScan = scanEvent.ScanData == ScanDataType.Profile //only centroid profile spectra
215-
? Scan.ToCentroid(scan)
216-
: scan;
210+
Scan scan = Scan.FromFile(rawFile, precursorScanNumber);
217211

218-
precursorCache.Add(precursorScanNumber, centroidedScan);
212+
if (useProfile) //get the profile data
213+
{
214+
masses = scan.SegmentedScan.Positions;
215+
intensities = scan.SegmentedScan.Intensities;
219216
}
217+
else
218+
{
219+
if (scan.HasCentroidStream) //use centroids if possible
220+
{
221+
masses = scan.CentroidScan.Masses;
222+
intensities = scan.CentroidScan.Intensities;
223+
}
224+
else
225+
{
226+
var scanEvent = rawFile.GetScanEventForScanNumber(precursorScanNumber);
227+
var centroidedScan = scanEvent.ScanData == ScanDataType.Profile //only centroid profile spectra
228+
? Scan.ToCentroid(scan).SegmentedScan
229+
: scan.SegmentedScan;
220230

221-
precursorCache.Add(precursorScanNumber, scan);
222-
}
223-
224-
double[] masses;
225-
double[] intensities;
231+
masses = centroidedScan.Positions;
232+
intensities = centroidedScan.Intensities;
233+
}
234+
}
226235

227-
if (!useProfile && scan.HasCentroidStream) //use centroid stream if it exist
228-
{
229-
masses = scan.CentroidScan.Masses;
230-
intensities = scan.CentroidScan.Intensities;
231-
}
232-
else //profile spectra were centroided earlier
233-
{
234-
masses = scan.SegmentedScan.Positions;
235-
intensities = scan.SegmentedScan.Intensities;
236+
//save to cache
237+
precursorCache.Add(precursorScanNumber, new MZArray { Masses = masses, Intensities = intensities });
236238
}
237239

238240
var index = masses.FastBinarySearch(precursorMass - halfWidth); //set index to the first peak inside isolation window

0 commit comments

Comments
 (0)