Skip to content

Commit ef8d698

Browse files
author
stefanks
authored
Merge branch 'master' into StefanBranch
2 parents 8e29419 + 63e58ce commit ef8d698

21 files changed

+278
-191
lines changed

EngineLayer/Analysis/AnalysisEngine.cs

Lines changed: 111 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,9 @@ public class AnalysisEngine : MyEngine
3333
private readonly bool doParsimony;
3434
private readonly bool noOneHitWonders;
3535
private readonly bool doHistogramAnalysis;
36+
private readonly bool quantify;
37+
private readonly double quantifyRtTol;
38+
private readonly double quantifyPpmTol;
3639
private readonly List<ProductType> lp;
3740
private readonly InitiatorMethionineBehavior initiatorMethionineBehavior;
3841
private readonly List<string> nestedIds;
@@ -42,7 +45,7 @@ public class AnalysisEngine : MyEngine
4245

4346
#region Public Constructors
4447

45-
public AnalysisEngine(PsmParent[][] newPsms, Dictionary<CompactPeptide, HashSet<PeptideWithSetModifications>> compactPeptideToProteinPeptideMatching, List<Protein> proteinList, List<ModificationWithMass> variableModifications, List<ModificationWithMass> fixedModifications, List<ModificationWithMass> localizeableModifications, Protease protease, List<SearchMode> searchModes, IMsDataFile<IMsDataScan<IMzSpectrum<IMzPeak>>> myMSDataFile, Tolerance fragmentTolerance, Action<BinTreeStructure, string> action1, Action<List<NewPsmWithFdr>, string> action2, Action<List<ProteinGroup>, string> action3, bool doParsimony, bool noOneHitWonders, int maximumMissedCleavages, int maxModIsoforms, bool doHistogramAnalysis, List<ProductType> lp, double binTol, InitiatorMethionineBehavior initiatorMethionineBehavior, List<string> nestedIds)
48+
public AnalysisEngine(PsmParent[][] newPsms, Dictionary<CompactPeptide, HashSet<PeptideWithSetModifications>> compactPeptideToProteinPeptideMatching, List<Protein> proteinList, List<ModificationWithMass> variableModifications, List<ModificationWithMass> fixedModifications, List<ModificationWithMass> localizeableModifications, Protease protease, List<SearchMode> searchModes, IMsDataFile<IMsDataScan<IMzSpectrum<IMzPeak>>> myMSDataFile, Tolerance fragmentTolerance, Action<BinTreeStructure, string> action1, Action<List<NewPsmWithFdr>, string> action2, Action<List<ProteinGroup>, string> action3, bool doParsimony, bool noOneHitWonders, int maximumMissedCleavages, int maxModIsoforms, bool doHistogramAnalysis, List<ProductType> lp, double binTol, InitiatorMethionineBehavior initiatorMethionineBehavior, List<string> nestedIds, bool Quantify, double QuantifyRtTol, double QuantifyPpmTol)
4649
{
4750
this.doParsimony = doParsimony;
4851
this.noOneHitWonders = noOneHitWonders;
@@ -65,6 +68,9 @@ public AnalysisEngine(PsmParent[][] newPsms, Dictionary<CompactPeptide, HashSet<
6568
this.lp = lp;
6669
this.binTol = binTol;
6770
this.initiatorMethionineBehavior = initiatorMethionineBehavior;
71+
this.quantify = Quantify;
72+
this.quantifyRtTol = QuantifyRtTol;
73+
this.quantifyPpmTol = QuantifyPpmTol;
6874
this.nestedIds = nestedIds;
6975
}
7076

@@ -74,8 +80,6 @@ public AnalysisEngine(PsmParent[][] newPsms, Dictionary<CompactPeptide, HashSet<
7480

7581
public Dictionary<CompactPeptide, HashSet<PeptideWithSetModifications>> ApplyProteinParsimony(out List<ProteinGroup> proteinGroups)
7682
{
77-
Status("Applying protein parsimony...", nestedIds);
78-
7983
var proteinToPeptidesMatching = new Dictionary<Protein, HashSet<CompactPeptide>>();
8084
var parsimonyDict = new Dictionary<Protein, HashSet<CompactPeptide>>();
8185
var proteinsWithUniquePeptides = new Dictionary<Protein, HashSet<CompactPeptide>>();
@@ -483,6 +487,101 @@ public List<ProteinGroup> DoProteinFdr(List<ProteinGroup> proteinGroups)
483487
return sortedProteinGroups;
484488
}
485489

490+
public void RunQuantification(List<NewPsmWithFdr> psms, double rtTolerance, double ppmTolerance)
491+
{
492+
var baseSeqToPsmMatching = new Dictionary<string, HashSet<NewPsmWithFdr>>();
493+
494+
foreach (var psm in psms)
495+
{
496+
if (!baseSeqToPsmMatching.ContainsKey(psm.thisPSM.FullSequence))
497+
{
498+
var psmList = new HashSet<NewPsmWithFdr>();
499+
psmList.Add(psm);
500+
baseSeqToPsmMatching.Add(psm.thisPSM.FullSequence, psmList);
501+
}
502+
else
503+
{
504+
HashSet<NewPsmWithFdr> psmList;
505+
baseSeqToPsmMatching.TryGetValue(psm.thisPSM.FullSequence, out psmList);
506+
psmList.Add(psm);
507+
}
508+
}
509+
510+
foreach (var kvp in baseSeqToPsmMatching)
511+
{
512+
// calculate apex intensity
513+
var rt1 = kvp.Value.Select(r => r.thisPSM.newPsm.scanRetentionTime).Min();
514+
var rt2 = kvp.Value.Select(r => r.thisPSM.newPsm.scanRetentionTime).Max();
515+
516+
double theoreticalMz = Chemistry.ClassExtensions.ToMz(kvp.Value.First().thisPSM.PeptideMonoisotopicMass, kvp.Value.First().thisPSM.newPsm.scanPrecursorCharge);
517+
518+
double mzTol = ((ppmTolerance / 1e6) * kvp.Value.First().thisPSM.PeptideMonoisotopicMass) / kvp.Value.First().thisPSM.newPsm.scanPrecursorCharge;
519+
520+
var spectraInThisWindow = myMsDataFile.GetMsScansInTimeRange(rt1 - rtTolerance, rt2 + rtTolerance).ToList();
521+
var ms1SpectraInThisWindow = spectraInThisWindow.Where(s => s.MsnOrder == 1).ToList();
522+
523+
var intensities = new List<double>();
524+
var retentionTimes = new List<double>();
525+
526+
foreach (var spectrum in ms1SpectraInThisWindow)
527+
{
528+
var i = spectrum.MassSpectrum.Where(s => (Math.Abs(s.Mz - theoreticalMz) < mzTol)).ToList();
529+
foreach (var p in i)
530+
{
531+
intensities.Add(p.Intensity);
532+
retentionTimes.Add(spectrum.RetentionTime);
533+
}
534+
}
535+
536+
double apexIntensity = 0;
537+
if(intensities.Any())
538+
apexIntensity = intensities.Max();
539+
540+
foreach(var p in kvp.Value)
541+
p.thisPSM.newPsm.apexIntensity = apexIntensity;
542+
543+
// calculate full width half max (peak quality)
544+
var apexIntensityIndex = intensities.IndexOf(apexIntensity);
545+
546+
double leftHalfMax = double.NaN;
547+
double rightHalfMax = double.NaN;
548+
double fullWidthHalfMax = double.NaN;
549+
550+
// left width half max
551+
for (int i = apexIntensityIndex; i >= 0; i--)
552+
{
553+
if (intensities[i] < (apexIntensity / 2))
554+
{
555+
leftHalfMax = retentionTimes[i];
556+
break;
557+
}
558+
}
559+
560+
// right width half max
561+
if (apexIntensityIndex >= 0)
562+
{
563+
for (int i = apexIntensityIndex; i < intensities.Count; i++)
564+
{
565+
if (intensities[i] < (apexIntensity / 2))
566+
{
567+
rightHalfMax = retentionTimes[i];
568+
break;
569+
}
570+
}
571+
}
572+
573+
if (!double.IsNaN(leftHalfMax) && !double.IsNaN(rightHalfMax))
574+
{
575+
fullWidthHalfMax = rightHalfMax - leftHalfMax;
576+
}
577+
578+
foreach (var p in kvp.Value)
579+
p.thisPSM.newPsm.fullWidthHalfMax = fullWidthHalfMax;
580+
581+
// calculate SNR (TODO**)
582+
}
583+
}
584+
486585
#endregion Public Methods
487586

488587
#region Protected Methods
@@ -551,6 +650,7 @@ protected override MyResults RunSpecific()
551650
List<ProteinGroup>[] proteinGroups = null;
552651
if (doParsimony)
553652
{
653+
Status("Applying protein parsimony...", nestedIds);
554654
proteinGroups = new List<ProteinGroup>[searchModes.Count];
555655
// TODO**: make this faster (only apply parsimony once but make multiple instances of the same ProteinGroups
556656
for (int i = 0; i < searchModes.Count; i++)
@@ -575,6 +675,13 @@ protected override MyResults RunSpecific()
575675

576676
Status("Running FDR analysis...", nestedIds);
577677
var orderedPsmsWithFDR = DoFalseDiscoveryRateAnalysis(orderedPsmsWithPeptides, searchModes[j]);
678+
679+
if (quantify)
680+
{
681+
Status("Quantifying peptides...", nestedIds);
682+
RunQuantification(orderedPsmsWithFDR, quantifyRtTol, quantifyPpmTol);
683+
}
684+
578685
writePsmsAction(orderedPsmsWithFDR, searchModes[j].FileNameAddition);
579686

580687
if (doHistogramAnalysis)
@@ -588,6 +695,7 @@ protected override MyResults RunSpecific()
588695
writeHistogramPeaksAction(myTreeStructure, searchModes[j].FileNameAddition);
589696
}
590697
}
698+
591699
else
592700
{
593701
Status("Running FDR analysis on unique peptides...", nestedIds);

EngineLayer/ClassicSearch/ClassicSearchEngine.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -150,7 +150,7 @@ protected override MyResults RunSpecific()
150150
//}
151151

152152
var score = PsmWithMultiplePossiblePeptides.MatchIons(scan.TheScan, productMassTolerance, sortedProductMasses, matchedIonsArray);
153-
var psm = new PsmClassic(yyy, fileName, scan.RetentionTime, scan.MonoisotopicPrecursorIntensity, scan.MonoisotopicPrecursorMass, scan.OneBasedScanNumber, scan.PrecursorCharge, scan.NumPeaks, scan.TotalIonCurrent, scan.MonoisotopicPrecursorMZ, score, theTuple.Item2);
153+
var psm = new PsmClassic(yyy, fileName, scan.RetentionTime, scan.MonoisotopicPrecursorIntensity, scan.MonoisotopicPrecursorMass, scan.OneBasedScanNumber, scan.OneBasedPrecursorScanNumber, scan.PrecursorCharge, scan.NumPeaks, scan.TotalIonCurrent, scan.MonoisotopicPrecursorMZ, score, theTuple.Item2);
154154
if (psm.score > 1)
155155
{
156156
PsmClassic current_best_psm = psms[aede][scan.OneBasedScanNumber - 1];

EngineLayer/ClassicSearch/PsmClassic.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ public class PsmClassic : PsmParent
2222

2323
#region Public Constructors
2424

25-
public PsmClassic(PeptideWithSetModifications ps, string fileName, double scanRetentionTime, double scanPrecursorIntensity, double scanPrecursorMass, int scanNumber, int scanPrecursorCharge, int scanExperimentalPeaks, double totalIonCurrent, double scanPrecursorMZ, double score, int notch) : base(fileName, scanRetentionTime, scanPrecursorIntensity, scanPrecursorMass, scanNumber, scanPrecursorCharge, scanExperimentalPeaks, totalIonCurrent, scanPrecursorMZ, score, notch)
25+
public PsmClassic(PeptideWithSetModifications ps, string fileName, double scanRetentionTime, double scanPrecursorIntensity, double scanPrecursorMass, int scanNumber, int scanPrecursorNumber, int scanPrecursorCharge, int scanExperimentalPeaks, double totalIonCurrent, double scanPrecursorMZ, double score, int notch) : base(fileName, scanRetentionTime, scanPrecursorIntensity, scanPrecursorMass, scanNumber, scanPrecursorNumber, scanPrecursorCharge, scanExperimentalPeaks, totalIonCurrent, scanPrecursorMZ, score, notch)
2626
{
2727
this.ps = ps;
2828
}

EngineLayer/LocalMs2Scan.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ public LocalMS2Scan(IMsDataScanWithPrecursor<IMzSpectrum<IMzPeak>> b)
2222
MonoisotopicPrecursorMass = MonoisotopicPrecursorMZ.ToMass(PrecursorCharge);
2323

2424
OneBasedScanNumber = b.OneBasedScanNumber;
25+
OneBasedPrecursorScanNumber = b.OneBasedPrecursorScanNumber;
2526

2627
RetentionTime = b.RetentionTime;
2728

@@ -49,6 +50,7 @@ public LocalMS2Scan(double precursorMass)
4950

5051
internal IMsDataScanWithPrecursor<IMzSpectrum<IMzPeak>> TheScan { get; private set; }
5152
internal int OneBasedScanNumber { get; private set; }
53+
internal int OneBasedPrecursorScanNumber { get; private set; }
5254
internal double RetentionTime { get; private set; }
5355
internal int PrecursorCharge { get; private set; }
5456
internal int NumPeaks { get; private set; }

EngineLayer/ModernSearch/ModernSearchEngine.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,7 @@ protected override MyResults RunSpecific()
137137
CompactPeptide theBestPeptide = bestPeptides[j];
138138
if (theBestPeptide != null)
139139
{
140-
newPsms[j][thisScan.OneBasedScanNumber - 1] = new PsmModern(theBestPeptide, Path.GetFileNameWithoutExtension(fileToSearch), thisScan.RetentionTime, thisScan.MonoisotopicPrecursorIntensity, thisScanprecursorMass, thisScan.OneBasedScanNumber, thisScan.PrecursorCharge, thisScan.NumPeaks, thisScan.TotalIonCurrent, thisScan.MonoisotopicPrecursorMZ, bestScores[j], bestNotches[j]);
140+
newPsms[j][thisScan.OneBasedScanNumber - 1] = new PsmModern(theBestPeptide, Path.GetFileNameWithoutExtension(fileToSearch), thisScan.RetentionTime, thisScan.MonoisotopicPrecursorIntensity, thisScanprecursorMass, thisScan.OneBasedScanNumber, thisScan.OneBasedPrecursorScanNumber, thisScan.PrecursorCharge, thisScan.NumPeaks, thisScan.TotalIonCurrent, thisScan.MonoisotopicPrecursorMZ, bestScores[j], bestNotches[j]);
141141
}
142142
}
143143
}

EngineLayer/ModernSearch/PsmModern.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ public class PsmModern : PsmParent
1414

1515
#region Public Constructors
1616

17-
public PsmModern(CompactPeptide theBestPeptide, string fileName, double scanRetentionTime, double scanPrecursorIntensity, double scanPrecursorMass, int scanNumber, int scanPrecursorCharge, int scanExperimentalPeaks, double totalIonCurrent, double scanPrecursorMZ, double score, int notch) : base(fileName, scanRetentionTime, scanPrecursorIntensity, scanPrecursorMass, scanNumber, scanPrecursorCharge, scanExperimentalPeaks, totalIonCurrent, scanPrecursorMZ, score, notch)
17+
public PsmModern(CompactPeptide theBestPeptide, string fileName, double scanRetentionTime, double scanPrecursorIntensity, double scanPrecursorMass, int scanNumber, int scanPrecursorNumber, int scanPrecursorCharge, int scanExperimentalPeaks, double totalIonCurrent, double scanPrecursorMZ, double score, int notch) : base(fileName, scanRetentionTime, scanPrecursorIntensity, scanPrecursorMass, scanNumber, scanPrecursorNumber, scanPrecursorCharge, scanExperimentalPeaks, totalIonCurrent, scanPrecursorMZ, score, notch)
1818
{
1919
compactPeptide = theBestPeptide;
2020
}

EngineLayer/ProteinGroup.cs

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -277,10 +277,6 @@ public void ScoreThisProteinGroup(List<ModificationWithMass> variableModificatio
277277
{
278278
proteinGroupScore += psm.thisPSM.Score;
279279
}
280-
281-
//TODO**: give option to ignore protein groups with one peptide
282-
//if (StrictPeptideList.Count == 1)
283-
// proteinGroupScore = 0;
284280
}
285281

286282
public void CalculateSequenceCoverage()

EngineLayer/PsmParent.cs

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ public abstract class PsmParent
1414

1515
public readonly string fileName;
1616
public readonly int scanNumber;
17+
public readonly int precursorScanNumber;
1718
public readonly double scanRetentionTime;
1819
public readonly int scanExperimentalPeaks;
1920
public readonly double totalIonCurrent;
@@ -22,6 +23,8 @@ public abstract class PsmParent
2223
public readonly double scanPrecursorMZ;
2324
public readonly double scanPrecursorMass;
2425
public readonly double score;
26+
public double apexIntensity;
27+
public double fullWidthHalfMax;
2528

2629
public Dictionary<ProductType, double[]> matchedIonsList;
2730
public List<double> LocalizedScores;
@@ -36,10 +39,11 @@ public abstract class PsmParent
3639

3740
#region Protected Constructors
3841

39-
protected PsmParent(string fileName, double scanRetentionTime, double scanPrecursorIntensity, double scanPrecursorMass, int scanNumber, int scanPrecursorCharge, int scanExperimentalPeaks, double totalIonCurrent, double scanPrecursorMZ, double score, int notch)
42+
protected PsmParent(string fileName, double scanRetentionTime, double scanPrecursorIntensity, double scanPrecursorMass, int scanNumber, int precursorScanNumber, int scanPrecursorCharge, int scanExperimentalPeaks, double totalIonCurrent, double scanPrecursorMZ, double score, int notch)
4043
{
4144
this.fileName = fileName;
4245
this.scanNumber = scanNumber;
46+
this.precursorScanNumber = precursorScanNumber;
4347
this.scanRetentionTime = scanRetentionTime;
4448
this.scanExperimentalPeaks = scanExperimentalPeaks;
4549
this.totalIonCurrent = totalIonCurrent;
@@ -72,6 +76,8 @@ public override string ToString()
7276
sb.Append(scanPrecursorMass.ToString("F5", CultureInfo.InvariantCulture) + '\t');
7377
sb.Append(score.ToString("F3", CultureInfo.InvariantCulture) + '\t');
7478
sb.Append(notch.ToString("F3", CultureInfo.InvariantCulture) + '\t');
79+
sb.Append(apexIntensity.ToString("F3", CultureInfo.InvariantCulture) + '\t');
80+
sb.Append(fullWidthHalfMax.ToString("F3", CultureInfo.InvariantCulture) + '\t');
7581

7682
sb.Append("[");
7783
foreach (var kvp in matchedIonsList)
@@ -112,6 +118,8 @@ internal static string GetTabSeparatedHeader()
112118
sb.Append("scanPrecursorMass" + '\t');
113119
sb.Append("score" + '\t');
114120
sb.Append("notch" + '\t');
121+
sb.Append("apexIntensity" + '\t');
122+
sb.Append("fullWidthHalfMax" + '\t');
115123

116124
sb.Append("matched ions" + '\t');
117125
sb.Append("matched ion counts" + '\t');

0 commit comments

Comments
 (0)