Skip to content

Commit

Permalink
increased version
Browse files Browse the repository at this point in the history
removed monoisotopic mass userparam when value is zero
changed selected ion mz in mzML to monoisotopic mass where available
  • Loading branch information
nielshulstaert committed May 16, 2019
1 parent 5adf776 commit 534accb
Show file tree
Hide file tree
Showing 4 changed files with 81 additions and 67 deletions.
4 changes: 2 additions & 2 deletions Properties/AssemblyInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
// You can specify all the values or you can default the Build and Revision Numbers
// by using the '*' as shown below:
// [assembly: AssemblyVersion("1.0.*")]
[assembly: AssemblyVersion("1.1.5.0")]
[assembly: AssemblyFileVersion("1.1.5.0")]
[assembly: AssemblyVersion("1.1.6.0")]
[assembly: AssemblyFileVersion("1.1.6.0")]

[assembly: log4net.Config.XmlConfigurator(ConfigFile = "log4net.config")]
48 changes: 5 additions & 43 deletions Writer/MgfSpectrumWriter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,6 @@ public class MgfSpectrumWriter : SpectrumWriter

private const string PositivePolarity = "+";
private const string NegativePolarity = "-";
private const double PrecursorMzDelta = 0.0001;
private const double DefaultIsolationWindowLowerOffset = 1.5;
private const double DefaultIsolationWindowUpperOffset = 2.5;

// Precursor scan number for reference in the precursor element of an MS2 spectrum
private int _precursorScanNumber;
Expand Down Expand Up @@ -88,7 +85,7 @@ public override void Write(IRawDataPlus rawFile, int firstScanNumber, int lastSc
// trailer extra data list
var trailerData = rawFile.GetTrailerExtraInformation(scanNumber);
int? charge = null;
double? monoisotopicMass = null;
double? monoisotopicMz = null;
double? isolationWidth = null;
for (var i = 0; i < trailerData.Length; i++)
{
Expand All @@ -99,7 +96,7 @@ public override void Write(IRawDataPlus rawFile, int firstScanNumber, int lastSc

if (trailerData.Labels[i] == "Monoisotopic M/Z:")
{
monoisotopicMass = double.Parse(trailerData.Values[i], NumberStyles.Any,
monoisotopicMz = double.Parse(trailerData.Values[i], NumberStyles.Any,
CultureInfo.CurrentCulture);
}

Expand All @@ -112,45 +109,10 @@ public override void Write(IRawDataPlus rawFile, int firstScanNumber, int lastSc

if (reaction != null)
{
var truePrecursorMass = reaction.PrecursorMass;

// take isolation width from the reaction if no value was found in the trailer data
if (isolationWidth == null || isolationWidth < ZeroDelta)
{
isolationWidth = reaction.IsolationWidth;
}

isolationWidth = isolationWidth / 2;

if (monoisotopicMass != null && monoisotopicMass > ZeroDelta
&& Math.Abs(
reaction.PrecursorMass - monoisotopicMass.Value) >
PrecursorMzDelta)
{
truePrecursorMass = monoisotopicMass.Value;

// check if the monoisotopic mass lies in the precursor mass isolation window
// otherwise take the precursor mass
if (isolationWidth <= 2.0)
{
if ((truePrecursorMass <
(reaction.PrecursorMass - DefaultIsolationWindowLowerOffset * 2)) ||
(truePrecursorMass >
(reaction.PrecursorMass + DefaultIsolationWindowUpperOffset)))
{
truePrecursorMass = reaction.PrecursorMass;
}
}
else if ((truePrecursorMass < (reaction.PrecursorMass - isolationWidth)) ||
(truePrecursorMass > (reaction.PrecursorMass + isolationWidth)))
{
truePrecursorMass = reaction.PrecursorMass;
}
}
var selectedIonMz = CalculateSelectedIonMz(reaction, monoisotopicMz, isolationWidth);

Writer.WriteLine("PEPMASS=" +
truePrecursorMass.ToString("0.0000000",
CultureInfo.InvariantCulture));
selectedIonMz.ToString(CultureInfo.InvariantCulture));
}

// charge
Expand Down Expand Up @@ -183,7 +145,7 @@ public override void Write(IRawDataPlus rawFile, int firstScanNumber, int lastSc
centroidStream.Masses[i].ToString("0.0000000",
CultureInfo.InvariantCulture)
+ " "
+ centroidStream.Intensities[i].ToString("0.0000000",
+ centroidStream.Intensities[i].ToString("0.0000000000",
CultureInfo.InvariantCulture));
}
}
Expand Down
31 changes: 14 additions & 17 deletions Writer/MzMlSpectrumWriter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -814,7 +814,7 @@ private SpectrumType ConstructSpectrum(int scanNumber)
// Trailer extra data list
var trailerData = _rawFile.GetTrailerExtraInformation(scanNumber);
int? charge = null;
double? monoisotopicMass = null;
double? monoisotopicMz = null;
double? ionInjectionTime = null;
double? isolationWidth = null;
for (var i = 0; i < trailerData.Length; i++)
Expand All @@ -829,7 +829,7 @@ private SpectrumType ConstructSpectrum(int scanNumber)

if (trailerData.Labels[i] == "Monoisotopic M/Z:")
{
monoisotopicMass = double.Parse(trailerData.Values[i], NumberStyles.Any,
monoisotopicMz = double.Parse(trailerData.Values[i], NumberStyles.Any,
CultureInfo.CurrentCulture);
}

Expand All @@ -847,7 +847,7 @@ private SpectrumType ConstructSpectrum(int scanNumber)
}

// Construct and set the scan list element of the spectrum
var scanListType = ConstructScanList(scanNumber, scan, scanFilter, scanEvent, monoisotopicMass,
var scanListType = ConstructScanList(scanNumber, scan, scanFilter, scanEvent, monoisotopicMz,
ionInjectionTime);
spectrum.scanList = scanListType;

Expand Down Expand Up @@ -892,7 +892,7 @@ private SpectrumType ConstructSpectrum(int scanNumber)

// Construct and set the precursor list element of the spectrum
var precursorListType =
ConstructPrecursorList(scanEvent, charge, scanFilter.MSOrder, isolationWidth);
ConstructPrecursorList(scanEvent, charge, scanFilter.MSOrder, monoisotopicMz, isolationWidth);
spectrum.precursorList = precursorListType;
break;
case MSOrderType.Ms3:
Expand All @@ -903,7 +903,8 @@ private SpectrumType ConstructSpectrum(int scanNumber)
name = "MSn spectrum",
value = ""
});
precursorListType = ConstructPrecursorList(scanEvent, charge, scanFilter.MSOrder, isolationWidth);
precursorListType = ConstructPrecursorList(scanEvent, charge, scanFilter.MSOrder, monoisotopicMz,
isolationWidth);
spectrum.precursorList = precursorListType;
break;
default:
Expand Down Expand Up @@ -1191,10 +1192,11 @@ private SpectrumType ConstructSpectrum(int scanNumber)
/// <param name="scanEvent">the scan event</param>
/// <param name="charge">the charge</param>
/// <param name="msLevel">the MS level</param>
/// <param name="monoisotopicMz">the monoisotopic m/z value</param>
/// <param name="isolationWidth">the isolation width</param>
/// <returns>the precursor list</returns>
private PrecursorListType ConstructPrecursorList(IScanEventBase scanEvent, int? charge, MSOrderType msLevel,
double? isolationWidth)
double? monoisotopicMz, double? isolationWidth)
{
// Construct the precursor
var precursorList = new PrecursorListType
Expand Down Expand Up @@ -1240,7 +1242,7 @@ private PrecursorListType ConstructPrecursorList(IScanEventBase scanEvent, int?
};

IReaction reaction = null;
var precursorMass = 0.0;
var precursorMz = 0.0;
try
{
switch (msLevel)
Expand All @@ -1253,26 +1255,21 @@ private PrecursorListType ConstructPrecursorList(IScanEventBase scanEvent, int?
break;
}

precursorMass = reaction.PrecursorMass;

// take isolation width from the reaction if no value was found in the trailer data
if (isolationWidth == null || isolationWidth < ZeroDelta)
{
isolationWidth = reaction.IsolationWidth;
}
precursorMz = reaction.PrecursorMass;
}
catch (ArgumentOutOfRangeException exception)
{
//do nothing
}

// Selected ion MZ
var selectedIonMz = CalculateSelectedIonMz(reaction, monoisotopicMz, isolationWidth);
var ionCvParams = new List<CVParamType>
{
new CVParamType
{
name = "selected ion m/z",
value = precursorMass.ToString(CultureInfo.InvariantCulture),
value = selectedIonMz.ToString(CultureInfo.InvariantCulture),
accession = "MS:1000744",
cvRef = "MS",
unitCvRef = "MS",
Expand Down Expand Up @@ -1320,7 +1317,7 @@ private PrecursorListType ConstructPrecursorList(IScanEventBase scanEvent, int?
{
accession = "MS:1000827",
name = "isolation window target m/z",
value = precursorMass.ToString(CultureInfo.InvariantCulture),
value = precursorMz.ToString(CultureInfo.InvariantCulture),
cvRef = "MS",
unitCvRef = "MS",
unitAccession = "MS:1000040",
Expand Down Expand Up @@ -1534,7 +1531,7 @@ private ScanListType ConstructScanList(int scanNumber, Scan scan, IScanFilter sc
};

// Monoisotopic mass
if (monoisotopicMass.HasValue)
if (monoisotopicMass.HasValue && monoisotopicMass.Value > ZeroDelta)
{
scanType.userParam = new UserParamType[1];
scanType.userParam[0] = new UserParamType
Expand Down
65 changes: 60 additions & 5 deletions Writer/SpectrumWriter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,11 @@ namespace ThermoRawFileParser.Writer
{
public abstract class SpectrumWriter : ISpectrumWriter
{
private const double Tolerance = 0.01;
private const double Tolerance = 0.01;
protected const double ZeroDelta = 0.0001;
private const double PrecursorMzDelta = 0.0001;
private const double DefaultIsolationWindowLowerOffset = 1.5;
private const double DefaultIsolationWindowUpperOffset = 2.5;

/// <summary>
/// The parse input object
Expand Down Expand Up @@ -45,21 +48,23 @@ protected void ConfigureWriter(string extension)
var fullExtension = ParseInput.Gzip ? extension + ".gzip" : extension;
if (!ParseInput.Gzip || ParseInput.OutputFormat == OutputFormat.IndexMzML)
{
Writer = File.CreateText(ParseInput.OutputDirectory + "//" + ParseInput.RawFileNameWithoutExtension +
Writer = File.CreateText(ParseInput.OutputDirectory + "//" +
ParseInput.RawFileNameWithoutExtension +
extension);
}
else
{
var fileStream = File.Create(ParseInput.OutputDirectory + "//" + ParseInput.RawFileNameWithoutExtension + fullExtension);
var fileStream = File.Create(ParseInput.OutputDirectory + "//" +
ParseInput.RawFileNameWithoutExtension + fullExtension);
var compress = new GZipStream(fileStream, CompressionMode.Compress);
Writer = new StreamWriter(compress);
}
}
}
else
{
if (!ParseInput.Gzip || ParseInput.OutputFormat == OutputFormat.IndexMzML)
{
Writer = File.CreateText(ParseInput.OutputFile);
Writer = File.CreateText(ParseInput.OutputFile);
}
else
{
Expand All @@ -86,6 +91,56 @@ protected static string ConstructSpectrumTitle(int scanNumber)
return "controllerType=0 controllerNumber=1 scan=" + scanNumber;
}

/// <summary>
/// Calculate the selected ion m/z value. This is necessary because the precursor mass found in the reaction
/// isn't always the monoisotopic mass.
/// https://github.com/ProteoWizard/pwiz/blob/master/pwiz/data/vendor_readers/Thermo/SpectrumList_Thermo.cpp#L564-L574
/// </summary>
/// <param name="reaction">the scan event reaction</param>
/// <param name="monoisotopicMz">the monoisotopic m/z value</param>
/// <param name="isolationWidth">the scan event reaction</param>
protected static double CalculateSelectedIonMz(IReaction reaction, double? monoisotopicMz,
double? isolationWidth)
{
var selectedIonMz = reaction.PrecursorMass;

// take the isolation width from the reaction if no value was found in the trailer data
if (isolationWidth == null || isolationWidth < ZeroDelta)
{
isolationWidth = reaction.IsolationWidth;
}

isolationWidth = isolationWidth / 2;

if (monoisotopicMz != null && monoisotopicMz > ZeroDelta
&& Math.Abs(
reaction.PrecursorMass - monoisotopicMz.Value) >
PrecursorMzDelta)
{
selectedIonMz = monoisotopicMz.Value;

// check if the monoisotopic mass lies in the precursor mass isolation window
// otherwise take the precursor mass
if (isolationWidth <= 2.0)
{
if ((selectedIonMz <
(reaction.PrecursorMass - DefaultIsolationWindowLowerOffset * 2)) ||
(selectedIonMz >
(reaction.PrecursorMass + DefaultIsolationWindowUpperOffset)))
{
selectedIonMz = reaction.PrecursorMass;
}
}
else if ((selectedIonMz < (reaction.PrecursorMass - isolationWidth)) ||
(selectedIonMz > (reaction.PrecursorMass + isolationWidth)))
{
selectedIonMz = reaction.PrecursorMass;
}
}

return selectedIonMz;
}

/// <summary>
/// Get the spectrum intensity.
/// </summary>
Expand Down

0 comments on commit 534accb

Please sign in to comment.