Skip to content

Commit 0017b9b

Browse files
committed
Refactoring
1 parent 34eda0c commit 0017b9b

File tree

1 file changed

+49
-118
lines changed

1 file changed

+49
-118
lines changed

Writer/MzMlSpectrumWriter.cs

Lines changed: 49 additions & 118 deletions
Original file line numberDiff line numberDiff line change
@@ -1253,7 +1253,7 @@ private SpectrumType ConstructMSSpectrum(int scanNumber)
12531253
var scanEvent = _rawFile.GetScanEventForScanNumber(scanNumber);
12541254
var spectrum = new SpectrumType
12551255
{
1256-
id = ConstructSpectrumTitle((int) Device.MS, 1, scanNumber),
1256+
id = ConstructSpectrumTitle((int)Device.MS, 1, scanNumber),
12571257
defaultArrayLength = 0
12581258
};
12591259

@@ -1289,7 +1289,7 @@ private SpectrumType ConstructMSSpectrum(int scanNumber)
12891289
foreach (var label in trailerData.MatchKeys(_spSentry))
12901290
{
12911291
var mass = trailerData.AsDouble(label).GetValueOrDefault(0);
1292-
if (mass > 0) SPSMasses.Add((double) mass); //zero means mass does not exist
1292+
if (mass > 0) SPSMasses.Add((double)mass); //zero means mass does not exist
12931293
}
12941294
}
12951295

@@ -1299,7 +1299,7 @@ private SpectrumType ConstructMSSpectrum(int scanNumber)
12991299
foreach (var labelvalue in trailerData.MatchValues(_spSentry3))
13001300
{
13011301
foreach (var mass in labelvalue.Trim()
1302-
.Split(new char[] {','}, StringSplitOptions.RemoveEmptyEntries))
1302+
.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries))
13031303
{
13041304
SPSMasses.Add(double.Parse(mass));
13051305
}
@@ -1334,109 +1334,70 @@ private SpectrumType ConstructMSSpectrum(int scanNumber)
13341334
_precursorTree[scanNumber] = new PrecursorInfo();
13351335

13361336
}
1337-
else if (msLevel > 1)
1338-
{
1339-
spectrumCvParams.Add(new CVParamType
1337+
else
1338+
{
1339+
Match result = null;
1340+
1341+
if (msLevel > 1)
13401342
{
1343+
spectrumCvParams.Add(new CVParamType
1344+
{
13411345
accession = "MS:1000580",
13421346
cvRef = "MS",
13431347
name = "MSn spectrum",
13441348
value = ""
1345-
});
1346-
1347-
spectrumCvParams.Add(new CVParamType
1348-
{
1349-
name = "ms level",
1350-
accession = "MS:1000511",
1351-
value = msLevel.ToString(),
1352-
cvRef = "MS"
1353-
});
1349+
});
13541350

1355-
// Keep track of scan number and isolation m/z for precursor reference
1356-
var result = _filterStringIsolationMzPattern.Match(scanEvent.ToString());
1357-
if (result.Success)
1358-
{
1359-
if (_precursorScanNumbers.ContainsKey(result.Groups[1].Value))
1351+
spectrumCvParams.Add(new CVParamType
13601352
{
1361-
_precursorScanNumbers.Remove(result.Groups[1].Value);
1362-
}
1363-
1364-
_precursorScanNumbers.Add(result.Groups[1].Value, scanNumber);
1365-
}
1353+
name = "ms level",
1354+
accession = "MS:1000511",
1355+
value = msLevel.ToString(),
1356+
cvRef = "MS"
1357+
});
13661358

1367-
//update precursor scan if it is provided in trailer data
1368-
var trailerMasterScan = trailerData.AsPositiveInt("Master Scan Number:");
1369-
if (trailerMasterScan.HasValue)
1370-
{
1371-
_precursorScanNumber = trailerMasterScan.Value;
1359+
// Keep track of scan number and isolation m/z for precursor reference
1360+
result = _filterStringIsolationMzPattern.Match(scanEvent.ToString());
13721361
}
1373-
else //try getting it from the scan filter
1362+
else if (msLevel == (int)MSOrderType.Par)
13741363
{
1375-
_precursorScanNumber = GetParentFromScanString(result.Groups[1].Value);
1376-
}
1364+
spectrumCvParams.Add(new CVParamType
1365+
{
1366+
accession = "MS:1000341",
1367+
cvRef = "MS",
1368+
name = "precursor ion spectrum",
1369+
value = ""
1370+
});
13771371

1378-
//finding precursor scan failed
1379-
if (_precursorScanNumber == -2 || !_precursorTree.ContainsKey(_precursorScanNumber))
1380-
{
1381-
Log.Warn($"Cannot find precursor scan for scan# {scanNumber}");
1382-
_precursorTree[_precursorScanNumber] = new PrecursorInfo(0, msLevel, FindLastReaction(scanEvent, msLevel), new PrecursorType[0]);
1383-
ParseInput.NewWarn();
1372+
// Keep track of scan number and isolation m/z for precursor reference
1373+
result = _filterStringParentMzPattern.Match(scanEvent.ToString());
13841374
}
1385-
1386-
try
1375+
else if (msLevel == (int)MSOrderType.Nl)
13871376
{
1388-
try //since there is no direct way to get the number of reactions available, it is necessary to try and fail
1389-
{
1390-
scanEvent.GetReaction(_precursorTree[_precursorScanNumber].ReactionCount);
1391-
}
1392-
catch (ArgumentOutOfRangeException ex)
1377+
spectrumCvParams.Add(new CVParamType
13931378
{
1394-
Log.Debug($"Using Tribrid decision tree fix for scan# {scanNumber}");
1395-
//Is it a decision tree scheduled scan on tribrid?
1396-
if (msLevel == _precursorTree[_precursorScanNumber].MSLevel)
1397-
{
1398-
_precursorScanNumber = GetParentFromScanString(result.Groups[1].Value);
1399-
}
1400-
else
1401-
{
1402-
throw new RawFileParserException(
1403-
$"Tribrid decision tree fix failed - cannot get reaction# {_precursorTree[_precursorScanNumber].ReactionCount} from {scanEvent.ToString()}",
1404-
ex);
1405-
}
1406-
}
1407-
1408-
// Construct and set the precursor list element of the spectrum
1409-
spectrum.precursorList =
1410-
ConstructPrecursorList(_precursorScanNumber, scanEvent, charge, monoisotopicMz, isolationWidth,
1411-
SPSMasses, out var reactionCount);
1412-
1413-
//save precursor information for later reference
1414-
_precursorTree[scanNumber] = new PrecursorInfo(_precursorScanNumber, msLevel, reactionCount, spectrum.precursorList.precursor);
1379+
accession = "MS:1000326",
1380+
cvRef = "MS",
1381+
name = "constant neutral loss spectrum",
1382+
value = ""
1383+
});
14151384
}
1416-
catch (Exception e)
1385+
else if (msLevel == (int)MSOrderType.Ng)
14171386
{
1418-
var extra = (e.InnerException is null) ? "" : $"\n{e.InnerException.StackTrace}";
1419-
1420-
Log.Warn($"Failed creating precursor list for scan# {scanNumber} - precursor information for this and dependent scans will be empty\nException details:{e.Message}\n{e.StackTrace}\n{extra}");
1421-
ParseInput.NewWarn();
1422-
1423-
_precursorTree[scanNumber] = new PrecursorInfo(_precursorScanNumber, 1, 0, new PrecursorType[0]);
1424-
1387+
spectrumCvParams.Add(new CVParamType
1388+
{
1389+
accession = "MS:1000325",
1390+
cvRef = "MS",
1391+
name = "constant neutral gain spectrum",
1392+
value = ""
1393+
});
14251394
}
1426-
}
1427-
else if (msLevel == (int)MSOrderType.Par)
1428-
{
1429-
spectrumCvParams.Add(new CVParamType
1395+
else
14301396
{
1431-
accession = "MS:1000341",
1432-
cvRef = "MS",
1433-
name = "precursor ion spectrum",
1434-
value = ""
1435-
});
1397+
throw new ArgumentOutOfRangeException($"Unknown msLevel: {msLevel}");
1398+
}
14361399

1437-
// Keep track of scan number and isolation m/z for precursor reference
1438-
var result = _filterStringParentMzPattern.Match(scanEvent.ToString());
1439-
if (result.Success)
1400+
if (result != null && result.Success)
14401401
{
14411402
if (_precursorScanNumbers.ContainsKey(result.Groups[1].Value))
14421403
{
@@ -1454,7 +1415,7 @@ private SpectrumType ConstructMSSpectrum(int scanNumber)
14541415
}
14551416
else //try getting it from the scan filter
14561417
{
1457-
_precursorScanNumber = GetParentFromScanString(result.Groups[1].Value);
1418+
_precursorScanNumber = GetParentFromScanString(result == null ? "" : result.Groups[1].Value);
14581419
}
14591420

14601421
//finding precursor scan failed
@@ -1506,36 +1467,6 @@ private SpectrumType ConstructMSSpectrum(int scanNumber)
15061467

15071468
}
15081469
}
1509-
else if (msLevel == (int)MSOrderType.Nl)
1510-
{
1511-
spectrumCvParams.Add(new CVParamType
1512-
{
1513-
accession = "MS:1000326",
1514-
cvRef = "MS",
1515-
name = "constant neutral loss spectrum",
1516-
value = ""
1517-
});
1518-
// Keep track of scan number for precursor reference
1519-
_precursorScanNumbers[""] = scanNumber;
1520-
_precursorTree[scanNumber] = new PrecursorInfo();
1521-
}
1522-
else if (msLevel == (int)MSOrderType.Ng)
1523-
{
1524-
spectrumCvParams.Add(new CVParamType
1525-
{
1526-
accession = "MS:1000325",
1527-
cvRef = "MS",
1528-
name = "constant neutral gain spectrum",
1529-
value = ""
1530-
});
1531-
// Keep track of scan number for precursor reference
1532-
_precursorScanNumbers[""] = scanNumber;
1533-
_precursorTree[scanNumber] = new PrecursorInfo();
1534-
}
1535-
else
1536-
{
1537-
throw new ArgumentOutOfRangeException($"Unknown msLevel: {msLevel}");
1538-
}
15391470

15401471
// Scan polarity
15411472
var polarityType = scanFilter.Polarity;

0 commit comments

Comments
 (0)