예제 #1
0
        public List <DbIrtPeptide> RecalibrateStandards(DbIrtPeptide[] standardPeptideList)
        {
            var peptideAllIrtTimes = new Dictionary <Target, List <Tuple <double, double> > >(); // peptide -> list of (irt, time)

            foreach (var data in ProviderData)
            {
                foreach (var peptide in data.FilteredPeptides)
                {
                    if (!peptideAllIrtTimes.TryGetValue(peptide.Target, out var pepTimes))
                    {
                        pepTimes = new List <Tuple <double, double> >();
                        peptideAllIrtTimes[peptide.Target] = pepTimes;
                    }
                    pepTimes.Add(Tuple.Create(peptide.Irt, peptide.RetentionTime.Value));
                }
            }
            var peptideBestIrtTimes = new Dictionary <Target, Tuple <double, double> >(); // peptide -> (percentile irt, percentile time)

            foreach (var peptide in peptideAllIrtTimes)
            {
                var statIrts   = new Statistics(peptide.Value.Select(p => p.Item1));
                var statTimes  = new Statistics(peptide.Value.Select(p => p.Item2));
                var percentile = IrtStandard.GetSpectrumTimePercentile(peptide.Key);
                peptideBestIrtTimes[peptide.Key] = Tuple.Create(statIrts.Percentile(percentile), statTimes.Percentile(percentile));
            }
            DbIrtPeptide min = null, max = null;

            foreach (var standard in standardPeptideList) // loop over list of standard peptides to find min/max that we have values for
            {
                if ((min == null || standard.Irt < min.Irt) && peptideBestIrtTimes.ContainsKey(standard.ModifiedTarget))
                {
                    min = standard;
                }
                if ((max == null || standard.Irt > max.Irt) && peptideBestIrtTimes.ContainsKey(standard.ModifiedTarget))
                {
                    max = standard;
                }
            }
            if (min == null || max == null)
            {
                throw new Exception(Resources.EditIrtCalcDlg_RecalibrateStandards_Could_not_get_a_minimum_or_maximum_standard_peptide_);
            }

            var statX                  = new Statistics(peptideBestIrtTimes[min.ModifiedTarget].Item2, peptideBestIrtTimes[max.ModifiedTarget].Item2);
            var statY                  = new Statistics(peptideBestIrtTimes[min.ModifiedTarget].Item1, peptideBestIrtTimes[max.ModifiedTarget].Item1);
            var line                   = new RegressionLine(statY.Slope(statX), statY.Intercept(statX));
            var newStandardPeptideList = new List <DbIrtPeptide>();

            foreach (var peptide in standardPeptideList)
            {
                if (!peptideBestIrtTimes.TryGetValue(peptide.ModifiedTarget, out var times))
                {
                    throw new Exception(Resources.ProcessedIrtAverages_RecalibrateStandards_A_standard_peptide_was_missing_when_trying_to_recalibrate_);
                }
                newStandardPeptideList.Add(new DbIrtPeptide(peptide)
                {
                    Irt = line.GetY(times.Item2)
                });
            }
            return(newStandardPeptideList);
        }