Skip to content

Commit 6ee904c

Browse files
committed
Implement ion charges
1 parent 95c7e22 commit 6ee904c

File tree

6 files changed

+111
-16
lines changed

6 files changed

+111
-16
lines changed

MainClass.cs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -499,7 +499,7 @@ private static void RegularParametersParsing(string[] args)
499499
h => help = h != null
500500
},
501501
{
502-
"version", "Prints out the version of the executable.",
502+
"v|version", "Prints out the version of the executable.",
503503
v => version = v != null
504504
},
505505
{
@@ -585,6 +585,10 @@ private static void RegularParametersParsing(string[] args)
585585
"N|noiseData", "Include noise data in mzML output",
586586
v => parseInput.NoiseData = v != null
587587
},
588+
{
589+
"C|chargeData", "Include instrument detected charge states in mzML output (only for high resolution centroided data)",
590+
v => parseInput.ChargeData = v != null
591+
},
588592
{
589593
"w|warningsAreErrors", "Return non-zero exit code for warnings; default only for errors",
590594
v => parseInput.Vigilant = v != null

ParseInput.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,8 @@ public int Warnings
102102

103103
public bool NoiseData { get; set; }
104104

105+
public bool ChargeData { get; set; }
106+
105107
public bool StdOut { get; set; }
106108

107109
public bool Vigilant { get; set; }
@@ -135,6 +137,7 @@ public ParseInput()
135137
MgfPrecursor = false;
136138
StdOut = false;
137139
NoiseData = false;
140+
ChargeData = false;
138141
Vigilant = false;
139142
_errors = 0;
140143
_warnings = 0;

RawFileParserException.cs

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,6 @@ public RawFileParserException()
99
{
1010
}
1111

12-
protected RawFileParserException(SerializationInfo info, StreamingContext context) : base(info, context)
13-
{
14-
}
15-
1612
public RawFileParserException(string message) : base(message)
1713
{
1814
}

ThermoRawFileParser.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
<OutputType>Exe</OutputType>
55
<TargetFramework>net8.0</TargetFramework>
66
<ImplicitUsings>disable</ImplicitUsings>
7-
<Nullable>enable</Nullable>
7+
<Nullable>annotations</Nullable>
88
<StartupObject>ThermoRawFileParser.MainClass</StartupObject>
99
<PlatformTarget>x64</PlatformTarget>
1010
<Platforms>x64</Platforms>

Writer/MgfSpectrumWriter.cs

Lines changed: 25 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -203,19 +203,22 @@ public override void Write(IRawDataPlus rawFile, int firstScanNumber, int lastSc
203203
Writer.WriteLine($"CHARGE={charge}{polarity}");
204204
}
205205

206-
// Write the filter string
207-
//Writer.WriteLine($"SCANEVENT={scanEvent.ToString()}");
208-
209206
double[] masses;
210207
double[] intensities;
208+
double[] charges = null;
209+
double[] raw_masses; //copy of original mass array used for sorting
211210

212211
if (!ParseInput.NoPeakPicking.Contains(msLevel))
213212
{
214213
// Check if the scan has a centroid stream
215214
if (scan.HasCentroidStream)
216215
{
217216
masses = scan.CentroidScan.Masses;
217+
raw_masses = (double[])masses.Clone();
218218
intensities = scan.CentroidScan.Intensities;
219+
220+
if (ParseInput.ChargeData)
221+
charges = scan.CentroidScan.Charges;
219222
}
220223
else // Otherwise take segmented (low res) scan data
221224
{
@@ -225,22 +228,38 @@ public override void Write(IRawDataPlus rawFile, int firstScanNumber, int lastSc
225228
: scan.SegmentedScan;
226229

227230
masses = segmentedScan.Positions;
231+
raw_masses = (double[])masses.Clone();
228232
intensities = segmentedScan.Intensities;
229233
}
230234
}
231235
else // Use the segmented data as is
232236
{
233237
masses = scan.SegmentedScan.Positions;
238+
raw_masses = (double[])masses.Clone();
234239
intensities = scan.SegmentedScan.Intensities;
235240
}
236241

237242
if (!(masses is null) && masses.Length > 0)
238243
{
239-
Array.Sort(masses, intensities);
244+
//Sorting masses and intensities
245+
Array.Sort(masses);
246+
Array.Sort((double[])raw_masses.Clone(), intensities);
240247

241-
for (var i = 0; i < masses.Length; i++)
248+
if (!(charges is null) && charges.Length > 0)
242249
{
243-
Writer.WriteLine(String.Format("{0:f5} {1:f3}", masses[i], intensities[i]));
250+
//Sorting charges
251+
Array.Sort((double[])raw_masses.Clone(), charges);
252+
for (var i = 0; i < masses.Length; i++)
253+
{
254+
Writer.WriteLine(String.Format("{0:f5} {1:f3} {2:d}", masses[i], intensities[i], (int)charges[i]));
255+
}
256+
}
257+
else
258+
{
259+
for (var i = 0; i < masses.Length; i++)
260+
{
261+
Writer.WriteLine(String.Format("{0:f5} {1:f3}", masses[i], intensities[i]));
262+
}
244263
}
245264
}
246265

Writer/MzMlSpectrumWriter.cs

Lines changed: 77 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1477,6 +1477,8 @@ private SpectrumType ConstructMSSpectrum(int scanNumber)
14771477
double? highestObservedMz = null;
14781478
double[] masses;
14791479
double[] intensities;
1480+
double[] charges = null;
1481+
double[] raw_masses; //this array is a copy of masses used for sorting
14801482

14811483
if (!ParseInput.NoPeakPicking.Contains((int) scanFilter.MSOrder))
14821484
{
@@ -1496,10 +1498,18 @@ private SpectrumType ConstructMSSpectrum(int scanNumber)
14961498
basePeakIntensity = scan.CentroidScan.BasePeakIntensity;
14971499

14981500
masses = scan.CentroidScan.Masses;
1501+
raw_masses = (double[])masses.Clone();//Copy of original (unsorted) masses
14991502
intensities = scan.CentroidScan.Intensities;
15001503

1504+
if (ParseInput.ChargeData)
1505+
{
1506+
charges = scan.CentroidScan.Charges;
1507+
}
1508+
15011509
if (scan.CentroidScan.Length > 0)
15021510
{
1511+
//Sort masses
1512+
Array.Sort(masses);
15031513
lowestObservedMz = scan.CentroidScan.Masses[0];
15041514
highestObservedMz = scan.CentroidScan.Masses[scan.CentroidScan.Masses.Length - 1];
15051515
}
@@ -1515,10 +1525,13 @@ private SpectrumType ConstructMSSpectrum(int scanNumber)
15151525
: scan.SegmentedScan;
15161526

15171527
masses = segmentedScan.Positions;
1528+
raw_masses = (double[]) masses.Clone();
15181529
intensities = segmentedScan.Intensities;
15191530

15201531
if (segmentedScan.PositionCount > 0)
15211532
{
1533+
//Sort masses
1534+
Array.Sort(masses);
15221535
lowestObservedMz = segmentedScan.Positions[0];
15231536
highestObservedMz = segmentedScan.Positions[segmentedScan.PositionCount - 1];
15241537
}
@@ -1551,10 +1564,13 @@ private SpectrumType ConstructMSSpectrum(int scanNumber)
15511564
basePeakMass = scan.ScanStatistics.BasePeakMass;
15521565
basePeakIntensity = scan.ScanStatistics.BasePeakIntensity;
15531566
masses = scan.SegmentedScan.Positions;
1567+
raw_masses = (double[])masses.Clone();
15541568
intensities = scan.SegmentedScan.Intensities;
15551569

15561570
if (scan.SegmentedScan.Positions.Length > 0)
15571571
{
1572+
//Sort masses
1573+
Array.Sort(masses);
15581574
lowestObservedMz = scan.SegmentedScan.Positions[0];
15591575
highestObservedMz = scan.SegmentedScan.Positions[scan.SegmentedScan.Positions.Length - 1];
15601576
}
@@ -1629,9 +1645,6 @@ private SpectrumType ConstructMSSpectrum(int scanNumber)
16291645
// M/Z Data
16301646
if (masses != null)
16311647
{
1632-
//sorting
1633-
Array.Sort(masses, intensities);
1634-
16351648
// Set the spectrum default array length
16361649
spectrum.defaultArrayLength = masses.Length;
16371650

@@ -1687,8 +1700,11 @@ private SpectrumType ConstructMSSpectrum(int scanNumber)
16871700
}
16881701

16891702
// Intensity Data
1690-
if (intensities != null)
1703+
if (masses != null && intensities != null)
16911704
{
1705+
//sorting intensities based on masses
1706+
Array.Sort((double[])raw_masses.Clone(), intensities);
1707+
16921708
// Set the spectrum default array length if necessary
16931709
if (spectrum.defaultArrayLength == 0)
16941710
{
@@ -1747,6 +1763,63 @@ private SpectrumType ConstructMSSpectrum(int scanNumber)
17471763
binaryData.Add(intensitiesBinaryData);
17481764
}
17491765

1766+
// Optional Charge Data
1767+
if (masses != null && charges != null)
1768+
{
1769+
//sorting charges based on masses
1770+
Array.Sort((double[])raw_masses.Clone(), charges);
1771+
1772+
var chargesBinaryData =
1773+
new BinaryDataArrayType
1774+
{
1775+
binary = ParseInput.NoZlibCompression
1776+
? Get64BitArray(charges)
1777+
: GetZLib64BitArray(charges)
1778+
};
1779+
chargesBinaryData.encodedLength =
1780+
(4 * Math.Ceiling((double)chargesBinaryData
1781+
.binary.Length / 3)).ToString(CultureInfo.InvariantCulture);
1782+
1783+
var chargesBinaryDataCvParams = new List<CVParamType>
1784+
{
1785+
new CVParamType
1786+
{
1787+
accession = "MS:1000516",
1788+
name = "charge array",
1789+
cvRef = "MS",
1790+
value = ""
1791+
},
1792+
new CVParamType {accession = "MS:1000523", name = "64-bit float", cvRef = "MS", value = ""}
1793+
};
1794+
1795+
if (!ParseInput.NoZlibCompression)
1796+
{
1797+
chargesBinaryDataCvParams.Add(
1798+
new CVParamType
1799+
{
1800+
accession = "MS:1000574",
1801+
name = "zlib compression",
1802+
cvRef = "MS",
1803+
value = ""
1804+
});
1805+
}
1806+
else
1807+
{
1808+
chargesBinaryDataCvParams.Add(
1809+
new CVParamType
1810+
{
1811+
accession = "MS:1000576",
1812+
name = "no compression",
1813+
cvRef = "MS",
1814+
value = ""
1815+
});
1816+
}
1817+
1818+
chargesBinaryData.cvParam = chargesBinaryDataCvParams.ToArray();
1819+
1820+
binaryData.Add(chargesBinaryData);
1821+
}
1822+
17501823
// Include optional noise data
17511824
if (ParseInput.NoiseData)
17521825
{

0 commit comments

Comments
 (0)