Example #1
0
        /// <summary>
        /// Align retention times with a target.
        /// For the MS2 Id's that are found in both the target and the timesToAlign, the MS2 id's
        /// are plotted against each other, and a linear regression is performed.
        /// In cases where there is more than one MS2 id in either file, only the earliest MS2 id from
        /// each file is used.
        /// </summary>
        public static AlignedRetentionTimes AlignLibraryRetentionTimes(IDictionary <Target, double> target, IDictionary <Target, double> originalTimes, double refinementThreshhold, RegressionMethodRT regressionMethod,
                                                                       Func <bool> isCanceled)
        {
            var calculator      = new DictionaryRetentionScoreCalculator("alignment", originalTimes); // Not L10N
            var targetTimesList = new List <MeasuredRetentionTime>();

            foreach (var entry in calculator.RetentionTimes)
            {
                double targetTime;
                if (!target.TryGetValue(entry.Key, out targetTime))
                {
                    continue;
                }
                MeasuredRetentionTime measuredRetentionTime;
                try
                {
                    measuredRetentionTime = new MeasuredRetentionTime(entry.Key, targetTime);
                }
                catch
                {
                    continue;
                }
                targetTimesList.Add(measuredRetentionTime);
            }
            RetentionTimeStatistics regressionStatistics;
            var regression = RetentionTimeRegression.CalcRegression(XmlNamedElement.NAME_INTERNAL, new[] { calculator }, regressionMethod, targetTimesList, out regressionStatistics);

            if (regression == null)
            {
                return(null);
            }
            RetentionTimeRegression regressionRefined;
            RetentionTimeStatistics regressionRefinedStatistics = regressionStatistics;
            HashSet <int>           outIndexes = new HashSet <int>();

            if (regressionStatistics.R >= refinementThreshhold)
            {
                regressionRefined = regression;
            }
            else
            {
                var cache = new RetentionTimeScoreCache(new[] { calculator }, new MeasuredRetentionTime[0], null);
                regressionRefined = regression.FindThreshold(refinementThreshhold, null, 0,
                                                             targetTimesList.Count, new MeasuredRetentionTime[0], targetTimesList, null, regressionStatistics,
                                                             calculator, regressionMethod, cache, isCanceled, ref regressionRefinedStatistics,
                                                             ref outIndexes);
            }

            return(new AlignedRetentionTimes
            {
                TargetTimes = target,
                OriginalTimes = originalTimes,
                Regression = regression,
                RegressionStatistics = regressionStatistics,
                RegressionRefined = regressionRefined,
                RegressionRefinedStatistics = regressionRefinedStatistics,
                OutlierIndexes = outIndexes,
                Calculator = calculator
            });
        }
        /// <summary>
        /// Align retention times with a target.
        /// For the MS2 Id's that are found in both the target and the timesToAlign, the MS2 id's 
        /// are plotted against each other, and a linear regression is performed.
        /// In cases where there is more than one MS2 id in either file, only the earliest MS2 id from
        /// each file is used.
        /// </summary>
        public static AlignedRetentionTimes AlignLibraryRetentionTimes(IDictionary<string, double> target, IDictionary<string, double> originalTimes, double refinementThreshhold, Func<bool> isCanceled)
        {
            var calculator = new DictionaryRetentionScoreCalculator("alignment", originalTimes); // Not L10N
            var targetTimesList = new List<MeasuredRetentionTime>();
            foreach (var entry in calculator.RetentionTimes)
            {
                double targetTime;
                if (!target.TryGetValue(entry.Key, out targetTime))
                {
                    continue;
                }
                MeasuredRetentionTime measuredRetentionTime;
                try
                {
                    measuredRetentionTime = new MeasuredRetentionTime(entry.Key, targetTime);
                }
                catch
                {
                    continue;
                }
                targetTimesList.Add(measuredRetentionTime);
            }
            RetentionTimeStatistics regressionStatistics;
            var regression = RetentionTimeRegression.CalcRegression(XmlNamedElement.NAME_INTERNAL, new[] {calculator}, targetTimesList, out regressionStatistics);
            if (regression == null)
            {
                return null;
            }
            RetentionTimeRegression regressionRefined;
            RetentionTimeStatistics regressionRefinedStatistics = regressionStatistics;
            HashSet<int> outIndexes = new HashSet<int>();
            if (regressionStatistics.R >= refinementThreshhold)
            {
                regressionRefined = regression;
            }
            else
            {
                var cache = new RetentionTimeScoreCache(new[] {calculator}, new MeasuredRetentionTime[0], null);
                regressionRefined = regression.FindThreshold(refinementThreshhold, null, 0,
                                                                targetTimesList.Count, new MeasuredRetentionTime[0], targetTimesList, regressionStatistics,
                                                                calculator, cache, isCanceled, ref regressionRefinedStatistics,
                                                                ref outIndexes);
            }

            return new AlignedRetentionTimes
                       {
                           TargetTimes = target,
                           OriginalTimes = originalTimes,
                           Regression = regression,
                           RegressionStatistics = regressionStatistics,
                           RegressionRefined = regressionRefined,
                           RegressionRefinedStatistics = regressionRefinedStatistics,
                           OutlierIndexes = outIndexes,
                       };
        }
            public GraphData(SrmDocument document, GraphData dataPrevious,
                             int resultIndex, double threshold, int?thresholdPrecision, bool refine, bool bestResult, PointsTypeRT pointsType)
            {
                _document           = document;
                _resultIndex        = resultIndex;
                _bestResult         = bestResult;
                _threshold          = threshold;
                _thresholdPrecision = thresholdPrecision;
                _pointsType         = pointsType;
                _peptidesIndexes    = new List <PeptideDocumentIndex>();
                _peptidesTimes      = new List <MeasuredRetentionTime>();
                int index = -1;

                var standards = new HashSet <string>();

                if (RTGraphController.PointsType == PointsTypeRT.standards)
                {
                    standards = document.GetRetentionTimeStandards();
                }

                // CONSIDER: Retention time prediction for small molecules?
                foreach (var nodePeptide in document.Peptides)
                {
                    index++;

                    switch (RTGraphController.PointsType)
                    {
                    default:
                        if (nodePeptide.IsDecoy)
                        {
                            continue;
                        }
                        break;

                    case PointsTypeRT.standards:
                        if (!standards.Contains(document.Settings.GetModifiedSequence(nodePeptide)))
                        {
                            continue;
                        }
                        break;

                    case PointsTypeRT.decoys:
                        if (!nodePeptide.IsDecoy)
                        {
                            continue;
                        }
                        break;
                    }

                    float?rt = null;
                    if (!bestResult)
                    {
                        rt = nodePeptide.GetSchedulingTime(resultIndex);
                    }
                    else
                    {
                        int iBest = nodePeptide.BestResult;
                        if (iBest != -1)
                        {
                            rt = nodePeptide.GetSchedulingTime(iBest);
                        }
                    }
                    if (!rt.HasValue)
                    {
                        rt = 0;
                    }

                    _peptidesIndexes.Add(new PeptideDocumentIndex(nodePeptide, index));
                    string modSeq = _document.Settings.GetSourceTextId(nodePeptide);
                    _peptidesTimes.Add(new MeasuredRetentionTime(modSeq, rt.Value));
                }

                _calculatorName = Settings.Default.RTCalculatorName;
                RetentionScoreCalculatorSpec calc = !string.IsNullOrEmpty(_calculatorName)
                                                        ? Settings.Default.GetCalculatorByName(Settings.Default.RTCalculatorName)
                                                        : null;

                if (calc == null)
                {
                    // Initialize all calculators
                    Settings.Default.RTScoreCalculatorList.Initialize(null);

                    //This call will pick the best calculator, disqualifying any iRT Calcs that do not have
                    //connected databases
                    _regressionAll = RetentionTimeRegression.CalcRegression(XmlNamedElement.NAME_INTERNAL,
                                                                            Settings.Default.RTScoreCalculatorList,
                                                                            _peptidesTimes,
                                                                            _scoreCache,
                                                                            true,
                                                                            out _statisticsAll,
                                                                            out _calculator);
                }
                else
                {
                    // Initialize the one calculator
                    calc = Settings.Default.RTScoreCalculatorList.Initialize(null, calc);

                    _regressionAll = RetentionTimeRegression.CalcRegression(XmlNamedElement.NAME_INTERNAL,
                                                                            new[] { calc },
                                                                            _peptidesTimes,
                                                                            _scoreCache,
                                                                            true,
                                                                            out _statisticsAll,
                                                                            out _calculator);

                    //If _regressionAll is null, it is safe to assume that the calculator is an iRT Calc with
                    //its database disconnected.
                    if (_regressionAll == null)
                    {
                        var tryIrtCalc = calc as RCalcIrt;
                        //Only show an error message if the user specifically chooses this calculator.
                        if (dataPrevious != null && !ReferenceEquals(calc, dataPrevious.Calculator) && tryIrtCalc != null)
                        {
                            throw new DatabaseNotConnectedException(tryIrtCalc);
                        }
                    }
                }

                if (_regressionAll != null)
                {
                    _scoreCache = new RetentionTimeScoreCache(new[] { _calculator }, _peptidesTimes,
                                                              dataPrevious != null ? dataPrevious._scoreCache : null);

                    if (dataPrevious != null && !ReferenceEquals(_calculator, dataPrevious._calculator))
                    {
                        _scoreCache.RecalculateCalcCache(_calculator);
                    }

                    _scoresRefined = _statisticsAll.ListHydroScores.ToArray();
                    _timesRefined  = _statisticsAll.ListRetentionTimes.ToArray();
                }

                _regressionPredict = document.Settings.PeptideSettings.Prediction.RetentionTime;
                if (_regressionPredict != null)
                {
                    if (!Equals(_calculator, _regressionPredict.Calculator))
                    {
                        _regressionPredict = null;
                    }
                    else
                    {
                        IDictionary <string, double> scoreCache = null;
                        if (_regressionAll != null && ReferenceEquals(_regressionAll.Calculator, _regressionPredict.Calculator))
                        {
                            scoreCache = _statisticsAll.ScoreCache;
                        }
                        _statisticsPredict = _regressionPredict.CalcStatistics(_peptidesTimes, scoreCache);
                    }
                }

                // Only refine, if not already exceeding the threshold
                _refine = refine && !IsRefined();
            }
            public GraphData(SrmDocument document, GraphData dataPrevious,
                int resultIndex, double threshold, int? thresholdPrecision, bool refine, bool bestResult)
            {
                _document = document;
                _resultIndex = resultIndex;
                _bestResult = bestResult;
                _threshold = threshold;
                _thresholdPrecision = thresholdPrecision;
                _peptidesIndexes = new List<PeptideDocumentIndex>();
                _peptidesTimes = new List<MeasuredRetentionTime>();
                int index = -1;
                // CONSIDER: Retention time prediction for small molecules?
                foreach (var nodePeptide in document.Peptides)
                {
                    index++;
                    float? rt = null;
                    if (!bestResult)
                        rt = nodePeptide.GetSchedulingTime(resultIndex);
                    else
                    {
                        int iBest = nodePeptide.BestResult;
                        if (iBest != -1)
                            rt = nodePeptide.GetSchedulingTime(iBest);
                    }
                    if (!rt.HasValue)
                        rt = 0;

                    _peptidesIndexes.Add(new PeptideDocumentIndex(nodePeptide, index));
                    string modSeq = _document.Settings.GetSourceTextId(nodePeptide);
                    _peptidesTimes.Add(new MeasuredRetentionTime(modSeq, rt.Value));
                }

                _calculatorName = Settings.Default.RTCalculatorName;
                RetentionScoreCalculatorSpec calc = !string.IsNullOrEmpty(_calculatorName)
                                                        ? Settings.Default.GetCalculatorByName(Settings.Default.RTCalculatorName)
                                                        : null;
                if (calc == null)
                {
                    // Initialize all calculators
                    Settings.Default.RTScoreCalculatorList.Initialize(null);

                    //This call will pick the best calculator, disqualifying any iRT Calcs that do not have
                    //connected databases
                    _regressionAll = RetentionTimeRegression.CalcRegression(XmlNamedElement.NAME_INTERNAL,
                                                                            Settings.Default.RTScoreCalculatorList,
                                                                            _peptidesTimes,
                                                                            _scoreCache,
                                                                            true,
                                                                            out _statisticsAll,
                                                                            out _calculator);
                }
                else
                {
                    // Initialize the one calculator
                    calc = Settings.Default.RTScoreCalculatorList.Initialize(null, calc);

                    _regressionAll = RetentionTimeRegression.CalcRegression(XmlNamedElement.NAME_INTERNAL,
                                                                            new[] {calc},
                                                                            _peptidesTimes,
                                                                            _scoreCache,
                                                                            true,
                                                                            out _statisticsAll,
                                                                            out _calculator);

                    //If _regressionAll is null, it is safe to assume that the calculator is an iRT Calc with
                    //its database disconnected.
                    if(_regressionAll == null)
                    {
                        var tryIrtCalc = calc as RCalcIrt;
                        //Only show an error message if the user specifically chooses this calculator.
                        if (dataPrevious != null && !ReferenceEquals(calc, dataPrevious.Calculator) && tryIrtCalc != null)
                        {
                            throw new DatabaseNotConnectedException(tryIrtCalc);
                        }
                    }
                }

                if (_regressionAll != null)
                {
                    _scoreCache = new RetentionTimeScoreCache(new[] { _calculator }, _peptidesTimes,
                                                              dataPrevious != null ? dataPrevious._scoreCache : null);

                    if (dataPrevious != null && !ReferenceEquals(_calculator, dataPrevious._calculator))
                        _scoreCache.RecalculateCalcCache(_calculator);

                    _scoresRefined = _statisticsAll.ListHydroScores.ToArray();
                    _timesRefined = _statisticsAll.ListRetentionTimes.ToArray();
                }

                _regressionPredict = document.Settings.PeptideSettings.Prediction.RetentionTime;
                if (_regressionPredict != null)
                {
                    if (!Equals(_calculator, _regressionPredict.Calculator))
                        _regressionPredict = null;
                    else
                    {
                        IDictionary<string, double> scoreCache = null;
                        if (_regressionAll != null && ReferenceEquals(_regressionAll.Calculator, _regressionPredict.Calculator))
                            scoreCache = _statisticsAll.ScoreCache;
                        _statisticsPredict = _regressionPredict.CalcStatistics(_peptidesTimes, scoreCache);
                    }
                }

                // Only refine, if not already exceeding the threshold
                _refine = refine && !IsRefined();
            }