Skip to content

Commit

Permalink
Merge branch 'ToppicResultReading' of https://github.com/nbollis/mzLib
Browse files Browse the repository at this point in the history
…into ToppicResultReading
  • Loading branch information
nbollis committed Oct 17, 2023
2 parents 58a4aa2 + 05624ee commit cfea3c1
Show file tree
Hide file tree
Showing 48 changed files with 4,906 additions and 199 deletions.
12 changes: 9 additions & 3 deletions mzLib/Chemistry/ClassExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -76,11 +76,17 @@ public static int GetClosestIndex(this double[] sortedArray, double target,
return defaultImplementationIndex;
}

defaultImplementationIndex = ~defaultImplementationIndex; // point to the first value larger than the target.
if (defaultImplementationIndex == 0)
return 0;
// Negative indices mean no exact match. Taking there bitwise complement yield the index
// of the first element with a value greater than the target.
defaultImplementationIndex = ~defaultImplementationIndex;

if (defaultImplementationIndex == sortedArray.Length)
// This implies that the target value was greater than the largest element in the array
return defaultImplementationIndex - 1;
if (defaultImplementationIndex == 0)
// This implies that the target value was less than the smallest element in the array
return 0;


int closestIndex;
switch (searchType)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ namespace Development.Deconvolution
/// </remarks>
/// </summary>
[TestFixture]
[Ignore("Only needed when developing deconvolution methods")]
[ExcludeFromCodeCoverage]
public class StandardDeconvolutionTest
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ namespace Development.Deconvolution

{
[TestFixture]
[Ignore("Only needed when developing deconvolution methods")]
[ExcludeFromCodeCoverage]
public static class TestDevelopmentTestCases
{
Expand Down
19 changes: 19 additions & 0 deletions mzLib/FlashLFQ/ChromatographicPeak.cs
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,25 @@ public override string ToString()
sb.Append(string.Join("|", Identifications.Select(p => p.BaseSequence).Distinct()) + '\t');
sb.Append(string.Join("|", Identifications.Select(p => p.ModifiedSequence).Distinct()) + '\t');

//The semi-colon here splitting the protein groups requires some explanation
//During protein parsimony, you can get situations where all peptides are shared between two or more proteins. In other words, there is no unique peptide that could resolve the parsimony.
//In this case you would see something like P00001 | P00002.

//That’s the easy part and you already understand that.

// Now imagine another scenario where you have some other peptides(that are not in either P00001 or P00002) that give you a second group, like the one above.Let’s call it P00003 | P00004.
// Everything is still fine her.

// Now you have two protein groups each with two proteins.

// Here is where the semi - colon comes in.
//Imagine you now find a new peptide(totally different from any of the peptides used to create the two original protein groups) that is shared across all four proteins.The original peptides
//require that two different protein groups exist, but this new peptide could come from either or both.We don’t know. So, the quantification of that peptide must be allowed to be
//either/ both groups. For this peptide, the protein accession in the output will be P00001 | P00002; P00003 | P00004.

// You could see an output that looks like P0000A; P0000B.Here there is only one protein in each protein group(as decided by parsimony).And you have a peptide that is shared.This would
// not ever be reported as P0000A | P0000B because each protein has a unique peptide that confirms its existence.

var t = Identifications.SelectMany(p => p.ProteinGroups.Select(v => v.ProteinGroupName)).Distinct().OrderBy(p => p);
if (t.Any())
{
Expand Down
30 changes: 15 additions & 15 deletions mzLib/FlashLFQ/FlashLfqEngine.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using MathNet.Numerics.Distributions;
using MathNet.Numerics.Statistics;
using MzLibUtil;
using Proteomics.AminoAcidPolymer;
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
Expand Down Expand Up @@ -294,15 +295,15 @@ private void CalculateTheoreticalIsotopeDistributions()

var isotopicMassesAndNormalizedAbundances = new List<(double massShift, double abundance)>();

if (formula == null)
if(formula is null)
{
double massDiff = id.MonoisotopicMass;
if (!String.IsNullOrEmpty(id.BaseSequence))
formula = new ChemicalFormula();
if (id.BaseSequence.AllSequenceResiduesAreValid())
{
Proteomics.AminoAcidPolymer.Peptide baseSequence = new Proteomics.AminoAcidPolymer.Peptide(id.BaseSequence);
formula = baseSequence.GetChemicalFormula();
// add averagine for any unknown mass difference (i.e., a modification)
massDiff -= baseSequence.MonoisotopicMass;
// there are sometimes non-parsable sequences in the base sequence input
formula = new Proteomics.AminoAcidPolymer.Peptide(id.BaseSequence).GetChemicalFormula();
double massDiff = id.MonoisotopicMass;
massDiff -= formula.MonoisotopicMass;

if (Math.Abs(massDiff) > 20)
{
Expand All @@ -317,14 +318,13 @@ private void CalculateTheoreticalIsotopeDistributions()
}
else
{
double averagines = massDiff / averagineMass;
string averagineFormulaString =
"C" + (int)Math.Round(averagines * averageC, 0) +
"H" + (int)Math.Round(averagines * averageH, 0) +
"O" + (int)Math.Round(averagines * averageO, 0) +
"N" + (int)Math.Round(averagines * averageN, 0) +
"S" + (int)Math.Round(averagines * averageS, 0);
formula = ChemicalFormula.ParseFormula(averagineFormulaString);
double averagines = id.MonoisotopicMass / averagineMass;

formula.Add("C", (int)Math.Round(averagines * averageC, 0));
formula.Add("H", (int)Math.Round(averagines * averageH, 0));
formula.Add("O", (int)Math.Round(averagines * averageO, 0));
formula.Add("N", (int)Math.Round(averagines * averageN, 0));
formula.Add("S", (int)Math.Round(averagines * averageS, 0));
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,9 +74,16 @@ public override IEnumerable<IsotopicEnvelope> Deconvolute(MzSpectrum spectrumToD
1.1) //if we're past a Th spacing, we're no longer looking at the closest isotope
{
//get the lower bound charge state
int charge =
(int)Math.Floor(1 /
deltaMass); //e.g. deltaMass = 0.4 Th, charge is now 2 (but might be 3)
int charge = 0;
if (deconParams.Polarity == Polarity.Negative)
{
charge = (int)Math.Floor(-1 / deltaMass); //e.g. deltaMass = 0.4 Th, charge is now 2 (but might be 3)
}
else
{
charge = (int)Math.Floor(1 / deltaMass); //e.g. deltaMass = 0.4 Th, charge is now 2 (but might be 3)
}

if (charge >= deconParams.MinAssumedChargeState && charge <= deconParams.MaxAssumedChargeState)
{
allPossibleChargeStates.Add(charge);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,16 @@
namespace MassSpectrometry
{
[ExcludeFromCodeCoverage]
public class ExampleNewDeconvolutionAlgorithm : DeconvolutionAlgorithm
public class ExampleNewDeconvolutionAlgorithmTemplate : DeconvolutionAlgorithm
{
public ExampleNewDeconvolutionAlgorithm(DeconvolutionParameters deconParameters) : base(deconParameters)
public ExampleNewDeconvolutionAlgorithmTemplate(DeconvolutionParameters deconParameters) : base(deconParameters)
{

}

public override IEnumerable<IsotopicEnvelope> Deconvolute(MzSpectrum spectrum, MzRange range = null)
{
var deconParams = DeconvolutionParameters as ExampleNewDeconvolutionParameters ?? throw new MzLibException("Deconvolution params and algorithm do not match");
var deconParams = DeconvolutionParameters as ExampleNewDeconvolutionParametersTemplate ?? throw new MzLibException("Deconvolution params and algorithm do not match");
range ??= spectrum.Range;

throw new NotImplementedException();
Expand Down
8 changes: 4 additions & 4 deletions mzLib/MassSpectrometry/Deconvolution/Deconvoluter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ namespace MassSpectrometry
public enum DeconvolutionType
{
ClassicDeconvolution,
AlexDeconvolution,
ExampleNewDeconvolutionTemplate,
}

/// <summary>
Expand Down Expand Up @@ -46,7 +46,7 @@ public IEnumerable<IsotopicEnvelope> Deconvolute(MsDataScan scan, MzRange rangeT
case DeconvolutionType.ClassicDeconvolution:
break;

case DeconvolutionType.AlexDeconvolution:
case DeconvolutionType.ExampleNewDeconvolutionTemplate:
break;
}

Expand All @@ -69,8 +69,8 @@ private void ConstructDeconvolutionAlgorithm(DeconvolutionParameters deconParame
DeconvolutionAlgorithm = new ClassicDeconvolutionAlgorithm(deconParameters);
break;

case DeconvolutionType.AlexDeconvolution:
DeconvolutionAlgorithm = new ExampleNewDeconvolutionAlgorithm(deconParameters);
case DeconvolutionType.ExampleNewDeconvolutionTemplate:
DeconvolutionAlgorithm = new ExampleNewDeconvolutionAlgorithmTemplate(deconParameters);
break;

default: throw new MzLibException("DeconvolutionType not yet supported");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ public static class DeconvoluterExtensions
/// </summary>
/// <param name="deconvoluter">performs deconvolution</param>
/// <param name="spectrum">spectrum to deconvolute</param>
/// <param name="range">mz range of returned peaks</param>
/// <param name="range">mz range of returned peaks, if null will deconvolute entire spectrum</param>
/// <returns></returns>
/// <exception cref="MzLibException"></exception>
public static IEnumerable<IsotopicEnvelope> Deconvolute(this Deconvoluter deconvoluter,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,6 @@ namespace MassSpectrometry
/// </summary>
public class ClassicDeconvolutionParameters : DeconvolutionParameters
{
public int MinAssumedChargeState { get; set; }
public int MaxAssumedChargeState { get; set; }
public double DeconvolutionTolerancePpm { get; set; }
public double IntensityRatioLimit { get; set; }

Expand All @@ -25,12 +23,11 @@ public class ClassicDeconvolutionParameters : DeconvolutionParameters
/// <param name="deconPpm"></param>
/// <param name="intensityRatio"></param>
/// <param name="range">Isolation range of the scan to be deconvoluted</param>
public ClassicDeconvolutionParameters(int minCharge, int maxCharge, double deconPpm, double intensityRatio) : base()
public ClassicDeconvolutionParameters(int minCharge, int maxCharge, double deconPpm, double intensityRatio, Polarity polarity = Polarity.Positive)
: base(minCharge, maxCharge, polarity)
{
IntensityRatioLimit = intensityRatio;
DeconvolutionTolerancePpm = deconPpm;
MinAssumedChargeState = minCharge;
MaxAssumedChargeState = maxCharge;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,20 @@ namespace MassSpectrometry
/// </summary>
public abstract class DeconvolutionParameters
{
public int MinAssumedChargeState { get; set; }
public int MaxAssumedChargeState { get; set; }
public Polarity Polarity { get; set; }

/// <summary>
/// Constructor should initialize all fields that are used by every deconvolution algorithm
/// </summary>
public DeconvolutionParameters()
public DeconvolutionParameters(int minCharge, int maxCharge, Polarity polarity = Polarity.Positive)
{

MinAssumedChargeState = minCharge;
MaxAssumedChargeState = maxCharge;
Polarity = polarity;
}
}
}


This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
using System;
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace MassSpectrometry
{
[ExcludeFromCodeCoverage]
public class ExampleNewDeconvolutionParametersTemplate : DeconvolutionParameters
{
public ExampleNewDeconvolutionParametersTemplate(int minCharge, int maxCharge, Polarity polarity = Polarity.Positive)
: base(minCharge, maxCharge, polarity)
{

}
}
}
31 changes: 18 additions & 13 deletions mzLib/MassSpectrometry/MsDataFile.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,10 @@
// You should have received a copy of the GNU Lesser General Public
// License along with MassSpectrometry. If not, see <http://www.gnu.org/licenses/>.

using Chemistry;
using MzLibUtil;
using System;
using System.Collections.Concurrent;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace MassSpectrometry
{
Expand All @@ -32,9 +29,9 @@ namespace MassSpectrometry
/// <summary>
/// A class for interacting with data collected from a Mass Spectrometer, and stored in a file
/// </summary>
public abstract class MsDataFile
public abstract class MsDataFile : IEnumerable<MsDataScan>
{
public MsDataScan[] Scans { get; set; }
public MsDataScan[] Scans { get; protected set; }
public SourceFile SourceFile { get; set; }
public int NumSpectra => Scans.Length;
public string FilePath { get; }
Expand Down Expand Up @@ -96,7 +93,7 @@ public virtual IEnumerable<MsDataScan> GetMS1Scans()
for (int i = 1; i <= NumSpectra; i++)
{
var scan = GetOneBasedScan(i);
if (scan != null && scan.MsnOrder == 1)
if (scan.MsnOrder == 1)
{
yield return scan;
}
Expand All @@ -107,7 +104,8 @@ public virtual MsDataScan GetOneBasedScan(int scanNumber)
{
if (!CheckIfScansLoaded())
LoadAllStaticData();
return Scans.SingleOrDefault(i => i.OneBasedScanNumber == scanNumber);

return Scans[scanNumber - 1];
}

public virtual IEnumerable<MsDataScan> GetMsScansInIndexRange(int firstSpectrumNumber, int lastSpectrumNumber)
Expand Down Expand Up @@ -164,11 +162,6 @@ public virtual int GetClosestOneBasedSpectrumNumber(double retentionTime)
return NumSpectra;
}

public virtual IEnumerator<MsDataScan> GetEnumerator()
{
return GetMsScansInIndexRange(1, NumSpectra).GetEnumerator();
}

public virtual int[] GetMsOrderByScanInDynamicConnection()
{
throw new NotImplementedException();
Expand All @@ -180,5 +173,17 @@ public virtual bool CheckIfScansLoaded()
{
return (Scans != null && Scans.Length > 0);
}

public IEnumerator<MsDataScan> GetEnumerator()
{
return Scans.Where(scan => scan is not null).GetEnumerator();
}

IEnumerator IEnumerable.GetEnumerator()
{
return GetEnumerator();
}
}


}
1 change: 1 addition & 0 deletions mzLib/MassSpectrometry/MsDataScan.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

using MzLibUtil;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;

Expand Down
7 changes: 4 additions & 3 deletions mzLib/MassSpectrometry/MzSpectra/IsotopicEnvelope.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,11 @@
using System;
using System.Collections.Generic;
using System.Linq;
using Chemistry;

namespace MassSpectrometry
{
public class IsotopicEnvelope
public class IsotopicEnvelope : IHasMass
{
public readonly List<(double mz, double intensity)> Peaks;
public double MonoisotopicMass { get; private set; }
Expand Down Expand Up @@ -35,7 +36,7 @@ public IsotopicEnvelope(List<(double mz, double intensity)> bestListOfPeaks, dou

public double GetMostAbundantObservedIsotopicMass(List<(double mz, double intensity)> peaks, int charge)
{
return peaks.MaxBy(p => p.intensity).mz * charge;
return peaks.MaxBy(p => p.intensity).mz * Math.Abs(charge);
}

public override string ToString()
Expand All @@ -46,7 +47,7 @@ public override string ToString()
private double ScoreIsotopeEnvelope() //likely created by Stefan Solntsev using peptide data
{
return Peaks.Count >= 2 ?
TotalIntensity / Math.Pow(StDev, 0.13) * Math.Pow(Peaks.Count, 0.4) / Math.Pow(Charge, 0.06) :
TotalIntensity / Math.Pow(StDev, 0.13) * Math.Pow(Peaks.Count, 0.4) / Math.Pow(Math.Abs(Charge), 0.06) :
0;
}

Expand Down
Loading

0 comments on commit cfea3c1

Please sign in to comment.