@@ -79,7 +79,7 @@ public override void Write(IRawDataPlus rawFile, int firstScanNumber, int lastSc
7979 var chromatogramOffSets = new OrderedDictionary ( ) ;
8080
8181 ConfigureWriter ( ".mzML" ) ;
82-
82+
8383 XmlSerializer serializer ;
8484 var settings = new XmlWriterSettings { Indent = true , Encoding = Encoding . UTF8 } ;
8585 var sha1 = SHA1 . Create ( ) ;
@@ -1290,34 +1290,36 @@ private SpectrumType ConstructMSSpectrum(int scanNumber)
12901290 cvRef = "MS"
12911291 } ) ;
12921292
1293- double ? basePeakMass = null ;
1294- double ? basePeakIntensity = null ;
1293+ double ? basePeakMass ;
1294+ double ? basePeakIntensity ;
12951295 double ? lowestObservedMz = null ;
12961296 double ? highestObservedMz = null ;
1297- double [ ] masses = null ;
1298- double [ ] intensities = null ;
1297+ double [ ] masses ;
1298+ double [ ] intensities ;
12991299
13001300 if ( ! ParseInput . NoPeakPicking )
13011301 {
1302+ //Spectrum will be centroided
1303+ spectrumCvParams . Add ( new CVParamType
1304+ {
1305+ accession = "MS:1000127" ,
1306+ cvRef = "MS" ,
1307+ name = "centroid spectrum" ,
1308+ value = ""
1309+ } ) ;
1310+
13021311 // Check if the scan has a centroid stream
13031312 if ( scan . HasCentroidStream )
13041313 {
1314+ basePeakMass = scan . CentroidScan . BasePeakMass ;
1315+ basePeakIntensity = scan . CentroidScan . BasePeakIntensity ;
1316+ masses = scan . CentroidScan . Masses ;
1317+ intensities = scan . CentroidScan . Intensities ;
1318+
13051319 if ( scan . CentroidScan . Length > 0 )
13061320 {
1307- spectrumCvParams . Add ( new CVParamType
1308- {
1309- accession = "MS:1000127" ,
1310- cvRef = "MS" ,
1311- name = "centroid spectrum" ,
1312- value = ""
1313- } ) ;
1314-
1315- basePeakMass = scan . CentroidScan . BasePeakMass ;
1316- basePeakIntensity = scan . CentroidScan . BasePeakIntensity ;
13171321 lowestObservedMz = scan . CentroidScan . Masses [ 0 ] ;
13181322 highestObservedMz = scan . CentroidScan . Masses [ scan . CentroidScan . Masses . Length - 1 ] ;
1319- masses = scan . CentroidScan . Masses ;
1320- intensities = scan . CentroidScan . Intensities ;
13211323 }
13221324 }
13231325 else // otherwise take the segmented (low res) scan
@@ -1330,56 +1332,50 @@ private SpectrumType ConstructMSSpectrum(int scanNumber)
13301332 ? Scan . ToCentroid ( scan ) . SegmentedScan
13311333 : scan . SegmentedScan ;
13321334
1335+ masses = segmentedScan . Positions ;
1336+ intensities = segmentedScan . Intensities ;
1337+
13331338 if ( segmentedScan . PositionCount > 0 )
13341339 {
1340+ lowestObservedMz = segmentedScan . Positions [ 0 ] ;
1341+ highestObservedMz = segmentedScan . Positions [ segmentedScan . PositionCount - 1 ] ;
1342+ }
1343+ }
1344+ }
1345+ else // use the segmented data as is
1346+ {
1347+ switch ( scanEvent . ScanData ) //check if the data centroided already
1348+ {
1349+ case ScanDataType . Centroid :
13351350 spectrumCvParams . Add ( new CVParamType
13361351 {
13371352 accession = "MS:1000127" ,
13381353 cvRef = "MS" ,
13391354 name = "centroid spectrum" ,
13401355 value = ""
13411356 } ) ;
1342-
1343- lowestObservedMz = segmentedScan . Positions [ 0 ] ;
1344- highestObservedMz = segmentedScan . Positions [ segmentedScan . PositionCount - 1 ] ;
1345- masses = segmentedScan . Positions ;
1346- intensities = segmentedScan . Intensities ;
1347- }
1357+ break ;
1358+ case ScanDataType . Profile :
1359+ spectrumCvParams . Add ( new CVParamType
1360+ {
1361+ accession = "MS:1000128" ,
1362+ cvRef = "MS" ,
1363+ name = "profile spectrum" ,
1364+ value = ""
1365+ } ) ;
1366+ break ;
13481367 }
1349- }
1350- else // use the segmented data as is
1351- {
1368+
13521369 basePeakMass = scan . ScanStatistics . BasePeakMass ;
13531370 basePeakIntensity = scan . ScanStatistics . BasePeakIntensity ;
1371+ masses = scan . SegmentedScan . Positions ;
1372+ intensities = scan . SegmentedScan . Intensities ;
1373+
13541374
13551375 if ( scan . SegmentedScan . Positions . Length > 0 )
13561376 {
1357- switch ( scanEvent . ScanData ) //check if the data centroided already
1358- {
1359- case ScanDataType . Centroid :
1360- spectrumCvParams . Add ( new CVParamType
1361- {
1362- accession = "MS:1000127" ,
1363- cvRef = "MS" ,
1364- name = "centroid spectrum" ,
1365- value = ""
1366- } ) ;
1367- break ;
1368- case ScanDataType . Profile :
1369- spectrumCvParams . Add ( new CVParamType
1370- {
1371- accession = "MS:1000128" ,
1372- cvRef = "MS" ,
1373- name = "profile spectrum" ,
1374- value = ""
1375- } ) ;
1376- break ;
1377- }
1378-
13791377 lowestObservedMz = scan . SegmentedScan . Positions [ 0 ] ;
13801378 highestObservedMz = scan . SegmentedScan . Positions [ scan . SegmentedScan . Positions . Length - 1 ] ;
1381- masses = scan . SegmentedScan . Positions ;
1382- intensities = scan . SegmentedScan . Intensities ;
13831379 }
13841380 }
13851381
@@ -1450,15 +1446,17 @@ private SpectrumType ConstructMSSpectrum(int scanNumber)
14501446 var binaryData = new List < BinaryDataArrayType > ( ) ;
14511447
14521448 // M/Z Data
1453- if ( ! masses . IsNullOrEmpty ( ) )
1449+ if ( masses != null )
14541450 {
14551451 // Set the spectrum default array length
14561452 spectrum . defaultArrayLength = masses . Length ;
14571453
14581454 var massesBinaryData =
14591455 new BinaryDataArrayType
14601456 {
1461- binary = ParseInput . NoZlibCompression ? Get64BitArray ( masses ) : GetZLib64BitArray ( masses )
1457+ binary = masses . Length > 0
1458+ ? ParseInput . NoZlibCompression ? Get64BitArray ( masses ) : GetZLib64BitArray ( masses )
1459+ : new byte [ 0 ] // zero length array encoded by GZip produces non-zero length array; some downstream tools do not like it
14621460 } ;
14631461 massesBinaryData . encodedLength =
14641462 ( 4 * Math . Ceiling ( ( double ) massesBinaryData
@@ -1495,7 +1493,7 @@ private SpectrumType ConstructMSSpectrum(int scanNumber)
14951493 }
14961494
14971495 // Intensity Data
1498- if ( ! intensities . IsNullOrEmpty ( ) )
1496+ if ( intensities != null )
14991497 {
15001498 // Set the spectrum default array length if necessary
15011499 if ( spectrum . defaultArrayLength == 0 )
@@ -1506,9 +1504,9 @@ private SpectrumType ConstructMSSpectrum(int scanNumber)
15061504 var intensitiesBinaryData =
15071505 new BinaryDataArrayType
15081506 {
1509- binary = ParseInput . NoZlibCompression
1510- ? Get64BitArray ( intensities )
1511- : GetZLib64BitArray ( intensities )
1507+ binary = intensities . Length > 0
1508+ ? ParseInput . NoZlibCompression ? Get64BitArray ( intensities ) : GetZLib64BitArray ( intensities )
1509+ : new byte [ 0 ] // zero length array encoded by GZip produces non-zero length array; some downstream tools do not like it
15121510 } ;
15131511 intensitiesBinaryData . encodedLength =
15141512 ( 4 * Math . Ceiling ( ( double ) intensitiesBinaryData
0 commit comments