Skip to content

Commit 5845b73

Browse files
committed
Smallfix
Unifying changes in metadata and instrument configurations parsing; check against NCName
1 parent 2b9e28f commit 5845b73

File tree

2 files changed

+33
-30
lines changed

2 files changed

+33
-30
lines changed

Writer/MetadataWriter.cs

Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -61,9 +61,7 @@ public void WriteMetadata(IRawDataPlus rawFile, int firstScanNumber, int lastSca
6161
// Keep track of the number of MS<MS level> spectra
6262
if (msTypes.ContainsKey(scanFilter.MSOrder.ToString()))
6363
{
64-
var value = msTypes[scanFilter.MSOrder.ToString()];
65-
value += 1;
66-
msTypes[scanFilter.MSOrder.ToString()] = value;
64+
msTypes[scanFilter.MSOrder.ToString()] += 1;
6765
}
6866
else
6967
msTypes.Add(scanFilter.MSOrder.ToString(), 1);
@@ -95,17 +93,21 @@ public void WriteMetadata(IRawDataPlus rawFile, int firstScanNumber, int lastSca
9593
}
9694

9795
// trailer extra data list
98-
var trailerData = rawFile.GetTrailerExtraInformation(scanNumber);
99-
for (var i = 0; i < trailerData.Length; i++)
96+
try
97+
{
98+
var trailerData = new ScanTrailer(rawFile.GetTrailerExtraInformation(scanNumber));
99+
int? charge = trailerData.AsPositiveInt("Charge State:");
100+
101+
if (charge.HasValue && charge.Value > maxCharge)
102+
maxCharge = charge.Value;
103+
104+
if (charge.HasValue && charge.Value < minCharge)
105+
minCharge = charge.Value;
106+
}
107+
catch (Exception ex)
100108
{
101-
if (trailerData.Labels[i] == "Charge State:")
102-
{
103-
if (int.Parse(trailerData.Values[i]) > maxCharge)
104-
maxCharge = int.Parse(trailerData.Values[i]);
105-
106-
if (int.Parse(trailerData.Values[i]) < minCharge)
107-
minCharge = int.Parse(trailerData.Values[i]);
108-
}
109+
Log.WarnFormat("Cannot load trailer infromation for scan {0} due to following exception\n{1}", scanNumber, ex.Message);
110+
_parseInput.NewWarn();
109111
}
110112
}
111113
}

Writer/MzMlSpectrumWriter.cs

Lines changed: 18 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@
1616
using ThermoFisher.CommonCore.Data.Business;
1717
using ThermoFisher.CommonCore.Data.FilterEnums;
1818
using ThermoFisher.CommonCore.Data.Interfaces;
19-
using ThermoRawFileParser.Util;
2019
using ThermoRawFileParser.Writer.MzML;
2120
using zlib;
2221

@@ -251,7 +250,7 @@ public override void Write(IRawDataPlus rawFile, int firstScanNumber, int lastSc
251250
value = instrumentData.SerialNumber
252251
});
253252
_writer.WriteEndElement(); // referenceableParamGroup
254-
_writer.WriteEndElement(); // referenceableParamGroupList
253+
_writer.WriteEndElement(); // referenceableParamGroupList
255254

256255
// SoftwareList
257256
_writer.WriteStartElement("softwareList");
@@ -269,6 +268,7 @@ public override void Write(IRawDataPlus rawFile, int firstScanNumber, int lastSc
269268
_writer.WriteEndElement(); // software
270269
_writer.WriteEndElement(); // softwareList
271270

271+
Log.Debug("Populating instrument configurations");
272272
PopulateInstrumentConfigurationList(firstScanNumber, lastScanNumber, instrumentModel);
273273

274274
// DataProcessingList
@@ -309,8 +309,7 @@ public override void Write(IRawDataPlus rawFile, int firstScanNumber, int lastSc
309309

310310
// Run
311311
_writer.WriteStartElement("run");
312-
//TODO: validate id against NCName
313-
_writer.WriteAttributeString("id", ParseInput.RawFileNameWithoutExtension);
312+
_writer.WriteAttributeString("id", GetNCName(ParseInput.RawFileNameWithoutExtension));
314313
_writer.WriteAttributeString("defaultInstrumentConfigurationRef", "IC1");
315314
_writer.WriteAttributeString("startTimeStamp",
316315
XmlConvert.ToString(_rawFile.CreationDate, XmlDateTimeSerializationMode.Utc));
@@ -378,8 +377,6 @@ public override void Write(IRawDataPlus rawFile, int firstScanNumber, int lastSc
378377

379378
Serialize(serializer, spectrum);
380379

381-
Log.Debug("Spectrum added to list of spectra -- ID " + spectrum.id);
382-
383380
index++;
384381
}
385382
}
@@ -628,6 +625,18 @@ public override void Write(IRawDataPlus rawFile, int firstScanNumber, int lastSc
628625
}
629626
}
630627

628+
private string GetNCName(string filename)
629+
{
630+
string result = Regex.Replace(filename, @"[^\d\w\.\-]+", "_");
631+
632+
if (Regex.IsMatch(result, @"^\d"))
633+
{
634+
result = $"_{result}";
635+
}
636+
637+
return result;
638+
}
639+
631640
private string GetTotalScanNumber()
632641
{
633642
// Save the last selected instrument
@@ -674,11 +683,8 @@ private string GetTotalScanNumber()
674683
private void PopulateInstrumentConfigurationList(int firstScanNumber, int lastScanNumber,
675684
CVParamType instrumentModel)
676685
{
677-
// Go over the first scans until an MS2 scan is encountered
678-
// to collect all mass analyzer and ionization types
679-
var encounteredMs2 = false;
680-
var scanNumber = firstScanNumber;
681-
do
686+
// Go over scan filters to collect all mass analyzer and ionization types
687+
for (int scanNumber = firstScanNumber; scanNumber <= lastScanNumber; scanNumber++)
682688
{
683689
// Get the scan filter for this scan number
684690
try
@@ -715,10 +721,6 @@ private void PopulateInstrumentConfigurationList(int firstScanNumber, int lastSc
715721
_massAnalyzers.Add(scanFilter.MassAnalyzer, "IC" + (_massAnalyzers.Count + 1));
716722
}
717723

718-
if (scanFilter.MSOrder == MSOrderType.Ms2)
719-
{
720-
encounteredMs2 = true;
721-
}
722724
}
723725
catch (Exception)
724726
{
@@ -734,8 +736,7 @@ private void PopulateInstrumentConfigurationList(int firstScanNumber, int lastSc
734736
}
735737
}
736738

737-
scanNumber++;
738-
} while (!encounteredMs2 && scanNumber <= lastScanNumber);
739+
}
739740

740741
// Add a default analyzer if none were found
741742
if (_massAnalyzers.Count == 0)

0 commit comments

Comments
 (0)