Skip to content

Commit

Permalink
Merge branch 'master' into quantInterfaces
Browse files Browse the repository at this point in the history
  • Loading branch information
mzhastings committed Aug 20, 2024
2 parents 9806eb0 + a78eea2 commit 6c4ffec
Show file tree
Hide file tree
Showing 12 changed files with 525 additions and 297 deletions.
146 changes: 83 additions & 63 deletions mzLib/FlashLFQ/ChromatographicPeak.cs
Original file line number Diff line number Diff line change
@@ -1,20 +1,33 @@
using Chemistry;
using MathNet.Numerics.Statistics;
using MzLibUtil;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using ClassExtensions = Chemistry.ClassExtensions;

namespace FlashLFQ
{
public class ChromatographicPeak
{
public double Intensity;
public double ApexRetentionTime => Apex?.IndexedPeak.RetentionTime ?? -1;
public readonly SpectraFileInfo SpectraFileInfo;
public List<IsotopicEnvelope> IsotopicEnvelopes;
public int ScanCount => IsotopicEnvelopes.Count;
public double SplitRT;
public readonly bool IsMbrPeak;
public double MbrScore;
public double PredictedRetentionTime { get; init; }

/// <summary>
/// A score bounded by 100 and 0, with more confident MBR-detections receiving higher scores
/// </summary>
public double MbrScore { get; private set; }

/// The four scores below are bounded by 0 and 1, with higher scores being better
public double PpmScore { get; private set; }
public double IntensityScore { get; private set; }
public double RtScore { get; private set; }
public double ScanCountScore { get; private set; }

public ChromatographicPeak(Identification id, bool isMbrPeak, SpectraFileInfo fileInfo)
{
Expand All @@ -29,70 +42,18 @@ public ChromatographicPeak(Identification id, bool isMbrPeak, SpectraFileInfo fi
SpectraFileInfo = fileInfo;
}

public ChromatographicPeak(Identification id, bool isMbrPeak, SpectraFileInfo fileInfo, double predictedRetentionTime) :
this(id, isMbrPeak, fileInfo)
{
PredictedRetentionTime = predictedRetentionTime;
}

public IsotopicEnvelope Apex { get; private set; }
public List<Identification> Identifications { get; private set; }
public int NumChargeStatesObserved { get; private set; }
public int NumIdentificationsByBaseSeq { get; private set; }
public int NumIdentificationsByFullSeq { get; private set; }
public double MassError { get; private set; }
/// <summary>
/// Expected retention time for MBR acceptor peaks (mean)
/// </summary>
public double? RtHypothesis { get; private set; }
/// <summary>
/// Std. Dev of retention time differences between MBR acceptor file and donor file, used if # calibration points < 6
/// </summary>
public double? RtStdDev { get; private set; }
/// <summary>
/// Interquartile range of retention time differences between MBR acceptor file and donor file, used if # calibration points >= 6
/// </summary>
public double? RtInterquartileRange { get; private set; }

public static string TabSeparatedHeader
{
get
{
var sb = new StringBuilder();
sb.Append("File Name" + "\t");
sb.Append("Base Sequence" + "\t");
sb.Append("Full Sequence" + "\t");
sb.Append("Protein Group" + "\t");
sb.Append("Peptide Monoisotopic Mass" + "\t");
sb.Append("MS2 Retention Time" + "\t");
sb.Append("Precursor Charge" + "\t");
sb.Append("Theoretical MZ" + "\t");
sb.Append("Peak intensity" + "\t");
sb.Append("Peak RT Start" + "\t");
sb.Append("Peak RT Apex" + "\t");
sb.Append("Peak RT End" + "\t");
sb.Append("Peak MZ" + "\t");
sb.Append("Peak Charge" + "\t");
sb.Append("Num Charge States Observed" + "\t");
sb.Append("Peak Detection Type" + "\t");
sb.Append("MBR Score" + "\t");
sb.Append("PSMs Mapped" + "\t");
sb.Append("Base Sequences Mapped" + "\t");
sb.Append("Full Sequences Mapped" + "\t");
sb.Append("Peak Split Valley RT" + "\t");
sb.Append("Peak Apex Mass Error (ppm)");
sb.Append("\tMBR Predicted RT");
//sb.Append("Timepoints");
return sb.ToString();
}
}

/// <summary>
/// Sets retention time information for a given peak. Used for MBR peaks
/// </summary>
/// <param name="rtHypothesis"> Expected retention time for peak, based on alignment between a donor and acceptor file </param>
/// <param name="rtStdDev"> Standard deviation in the retention time differences between aligned peaks </param>
/// <param name="rtInterquartileRange"> Interquartile range og the retention time differences between aligned peaks</param>
internal void SetRtWindow(double rtHypothesis, double? rtStdDev, double? rtInterquartileRange)
{
RtHypothesis = rtHypothesis;
RtStdDev = rtStdDev;
RtInterquartileRange = rtInterquartileRange;
}

public void CalculateIntensityForThisFeature(bool integrate)
{
Expand Down Expand Up @@ -146,7 +107,7 @@ public void MergeFeatureWith(ChromatographicPeak otherFeature, bool integrate)
this.Identifications = this.Identifications
.Union(otherFeature.Identifications)
.Distinct()
.OrderBy(p => p.PosteriorErrorProbability).ToList();
.ToList();
ResolveIdentifications();
this.IsotopicEnvelopes.AddRange(otherFeature.IsotopicEnvelopes
.Where(p => !thisFeaturesPeaks.Contains(p.IndexedPeak)));
Expand All @@ -163,6 +124,60 @@ public void ResolveIdentifications()
this.NumIdentificationsByFullSeq = Identifications.Select(v => v.ModifiedSequence).Distinct().Count();
}

/// <summary>
/// Calculates four component scores and one overarching Mbr score for an MBR peak.
/// MBR Score is equal to 100 * the geometric mean of the four component scores.
/// </summary>
/// <param name="scorer"> An MbrScorer specific to the file where this peak was found </param>
/// <param name="donorPeak"> The donor peak used as the basis for the MBR identification. </param>
internal void CalculateMbrScore(MbrScorer scorer, ChromatographicPeak donorPeak)
{
if (SpectraFileInfo != scorer.AcceptorFile) throw new MzLibException("Error when performing match-between-runs: Mismatch between scorer and peak.");

IntensityScore = scorer.CalculateIntensityScore(this, donorPeak);
RtScore = scorer.CalculateRetentionTimeScore(this, donorPeak);
PpmScore = scorer.CalculatePpmErrorScore(this);
ScanCountScore = scorer.CalculateScanCountScore(this);

MbrScore = 100 * Math.Pow(IntensityScore * RtScore * PpmScore * ScanCountScore, 0.25);
}

public static string TabSeparatedHeader
{
get
{
var sb = new StringBuilder();
sb.Append("File Name" + "\t");
sb.Append("Base Sequence" + "\t");
sb.Append("Full Sequence" + "\t");
sb.Append("Protein Group" + "\t");
sb.Append("Organism" + '\t');
sb.Append("Peptide Monoisotopic Mass" + "\t");
sb.Append("MS2 Retention Time" + "\t");
sb.Append("Precursor Charge" + "\t");
sb.Append("Theoretical MZ" + "\t");
sb.Append("Peak intensity" + "\t");
sb.Append("Peak RT Start" + "\t");
sb.Append("Peak RT Apex" + "\t");
sb.Append("Peak RT End" + "\t");
sb.Append("Peak MZ" + "\t");
sb.Append("Peak Charge" + "\t");
sb.Append("Num Charge States Observed" + "\t");
sb.Append("Peak Detection Type" + "\t");
sb.Append("MBR Score" + "\t");
sb.Append("Ppm Score" + "\t");
sb.Append("Intensity Score" + "\t");
sb.Append("Rt Score" + "\t");
sb.Append("Scan Count Score" + "\t");
sb.Append("PSMs Mapped" + "\t");
sb.Append("Base Sequences Mapped" + "\t");
sb.Append("Full Sequences Mapped" + "\t");
sb.Append("Peak Split Valley RT" + "\t");
sb.Append("Peak Apex Mass Error (ppm)");
return sb.ToString();
}
}

public override string ToString()
{
StringBuilder sb = new StringBuilder();
Expand Down Expand Up @@ -193,10 +208,12 @@ public override string ToString()
if (t.Any())
{
sb.Append(string.Join(";", t) + '\t');
sb.Append(string.Join(";", Identifications.SelectMany(id => id.ProteinGroups).Select(p => p.Organism).Distinct()) + '\t');
}
else
{
sb.Append("" + '\t');
sb.Append("" + '\t');
}

sb.Append("" + Identifications.First().MonoisotopicMass + '\t');
Expand Down Expand Up @@ -244,13 +261,16 @@ public override string ToString()
}

sb.Append("" + (IsMbrPeak ? MbrScore.ToString() : "") + "\t");
sb.Append("" + (IsMbrPeak ? PpmScore.ToString() : "") + "\t");
sb.Append("" + (IsMbrPeak ? IntensityScore.ToString() : "") + "\t");
sb.Append("" + (IsMbrPeak ? RtScore.ToString() : "") + "\t");
sb.Append("" + (IsMbrPeak ? ScanCountScore.ToString() : "") + "\t");

sb.Append("" + Identifications.Count + "\t");
sb.Append("" + NumIdentificationsByBaseSeq + "\t");
sb.Append("" + NumIdentificationsByFullSeq + "\t");
sb.Append("" + SplitRT + "\t");
sb.Append("" + MassError);
sb.Append("\t" + (IsMbrPeak ? RtHypothesis.ToString() : ""));

return sb.ToString();
}
Expand Down
2 changes: 1 addition & 1 deletion mzLib/FlashLFQ/FlashLFQResults.cs
Original file line number Diff line number Diff line change
Expand Up @@ -549,7 +549,7 @@ public void CalculateProteinResultsMedianPolish(bool useSharedPeptides)
}
}

public void WriteResults(string peaksOutputPath, string modPeptideOutputPath, string proteinOutputPath, string bayesianProteinQuantOutput, bool silent)
public void WriteResults(string peaksOutputPath, string modPeptideOutputPath, string proteinOutputPath, string bayesianProteinQuantOutput, bool silent = true)
{
if (!silent)
{
Expand Down
Loading

0 comments on commit 6c4ffec

Please sign in to comment.