@@ -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 ) ;
0 commit comments