@@ -38,13 +38,19 @@ public abstract class SpectrumWriter : ISpectrumWriter
3838 /// </summary>
3939 protected StreamWriter Writer ;
4040
41+ /// <summary>
42+ /// Precursor cache
43+ /// </summary>
44+ private static Util . LimitedSizeDictionary < int , Scan > precursorCache ;
45+
4146 /// <summary>
4247 /// Constructor.
4348 /// </summary>
4449 /// <param name="parseInput">the parse input object</param>
4550 protected SpectrumWriter ( ParseInput parseInput )
4651 {
4752 ParseInput = parseInput ;
53+ precursorCache = new Util . LimitedSizeDictionary < int , Scan > ( 10 ) ;
4854 }
4955
5056 /// <inheritdoc />
@@ -191,35 +197,41 @@ public static IReaction GetReaction(IScanEvent scanEvent, int scanNumber)
191197 double precursorIntensity = 0 ;
192198 double halfWidth = isolationWidth is null || isolationWidth == 0 ? 0 : DefaultIsolationWindowLowerOffset ; // that is how it is made in MSConvert (why?)
193199
194- // Get the precursor scan from the RAW file
195- var scan = Scan . FromFile ( rawFile , precursorScanNumber ) ;
196-
197- double [ ] masses ;
198- double [ ] intensities ;
199-
200- if ( useProfile )
200+ // Get the precursor scan from the RAW file or cache
201+ Scan scan ;
202+ if ( precursorCache . ContainsKey ( precursorScanNumber ) )
201203 {
202- masses = scan . SegmentedScan . Positions ;
203- intensities = scan . SegmentedScan . Intensities ;
204+ scan = precursorCache [ precursorScanNumber ] ;
204205 }
205206 else
206207 {
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
208+ scan = Scan . FromFile ( rawFile , precursorScanNumber ) ;
209+ //check if it is necessary to centroid a profile scan
210+ if ( ! useProfile && ! scan . HasCentroidStream )
214211 {
215212 var scanEvent = rawFile . GetScanEventForScanNumber ( precursorScanNumber ) ;
216213 var centroidedScan = scanEvent . ScanData == ScanDataType . Profile //only centroid profile spectra
217- ? Scan . ToCentroid ( scan ) . SegmentedScan
218- : scan . SegmentedScan ;
214+ ? Scan . ToCentroid ( scan )
215+ : scan ;
219216
220- masses = centroidedScan . Positions ;
221- intensities = centroidedScan . Intensities ;
217+ precursorCache . Add ( precursorScanNumber , centroidedScan ) ;
222218 }
219+
220+ precursorCache . Add ( precursorScanNumber , scan ) ;
221+ }
222+
223+ double [ ] masses ;
224+ double [ ] intensities ;
225+
226+ if ( ! useProfile && scan . HasCentroidStream ) //use centroid stream if it exist
227+ {
228+ masses = scan . CentroidScan . Masses ;
229+ intensities = scan . CentroidScan . Intensities ;
230+ }
231+ else //profile spectra were centroided earlier
232+ {
233+ masses = scan . SegmentedScan . Positions ;
234+ intensities = scan . SegmentedScan . Intensities ;
223235 }
224236
225237 var index = masses . FastBinarySearch ( precursorMass - halfWidth ) ; //set index to the first peak inside isolation window
0 commit comments