Ejemplo n.º 1
0
        public bool CalcRegressionWith(IRetentionTimeProvider retentionTimes, IEnumerable <DbIrtPeptide> standardPeptideList, DbIrtPeptide[] items)
        {
            if (items.Any())
            {
                // Attempt to get a regression based on shared peptides
                var calculator    = new CurrentCalculator(standardPeptideList, items);
                var peptidesTimes = retentionTimes.PeptideRetentionTimes.ToArray();
                var regression    = RetentionTimeRegression.FindThreshold(RCalcIrt.MIN_IRT_TO_TIME_CORRELATION,
                                                                          RetentionTimeRegression.ThresholdPrecision,
                                                                          peptidesTimes,
                                                                          new MeasuredRetentionTime[0],
                                                                          peptidesTimes, null,
                                                                          calculator,
                                                                          RegressionMethodRT.linear,
                                                                          () => false);

                var startingCount   = peptidesTimes.Length;
                var regressionCount = regression != null ? regression.PeptideTimes.Count : 0;
                if (regression != null && RCalcIrt.IsAcceptableStandardCount(startingCount, regressionCount))
                {
                    // Finally must recalculate the regression, because it is transposed from what
                    // we want.
                    var statTimes = new Statistics(regression.PeptideTimes.Select(pt => pt.RetentionTime));
                    var statIrts  = new Statistics(regression.PeptideTimes.Select(pt =>
                                                                                  calculator.ScoreSequence(pt.PeptideSequence) ?? calculator.UnknownScore));

                    RegressionRefined = new RegressionLine(statIrts.Slope(statTimes), statIrts.Intercept(statTimes));
                    RegressionSuccess = true;
                    return(true);
                }
            }
            return(false);
        }
            private GraphData RefineCloned(double threshold, int?precision, Func <bool> isCanceled)
            {
                // Create list of deltas between predicted and measured times
                _outlierIndexes = new HashSet <int>();
                // Start with anything assigned a zero retention time as outliers
                for (int i = 0; i < _peptidesTimes.Count; i++)
                {
                    if (_peptidesTimes[i].RetentionTime == 0)
                    {
                        _outlierIndexes.Add(i);
                    }
                }

                // Now that we have added iRT calculators, RecalcRegression
                // cannot go and mark as outliers peptides at will anymore. It must know which peptides, if any,
                // are required by the calculator for a regression. With iRT calcs, the standard is required.
                if (!_calculator.IsUsable)
                {
                    return(null);
                }

                HashSet <string> standardNames;

                try
                {
                    var names = _calculator.GetStandardPeptides(_peptidesTimes.Select(pep => pep.PeptideSequence));
                    standardNames = new HashSet <string>(names);
                }
                catch (CalculatorException)
                {
                    standardNames = new HashSet <string>();
                }

                var standardPeptides = _peptidesTimes.Where(pep => standardNames.Contains(pep.PeptideSequence)).ToArray();
                var variablePeptides = _peptidesTimes.Where(pep => !standardNames.Contains(pep.PeptideSequence)).ToArray();

                //Throws DatabaseNotConnectedException
                _regressionRefined = (_regressionAll == null
                                          ? null
                                          : _regressionAll.FindThreshold(threshold,
                                                                         precision,
                                                                         0,
                                                                         variablePeptides.Length,
                                                                         standardPeptides,
                                                                         variablePeptides,
                                                                         _statisticsAll,
                                                                         _calculator,
                                                                         _scoreCache,
                                                                         isCanceled,
                                                                         ref _statisticsRefined,
                                                                         ref _outlierIndexes));

                if (ReferenceEquals(_regressionRefined, _regressionAll))
                {
                    return(null);
                }

                // Separate lists into acceptable and outliers
                var listScoresRefined  = new List <double>();
                var listTimesRefined   = new List <double>();
                var listScoresOutliers = new List <double>();
                var listTimesOutliers  = new List <double>();

                for (int i = 0; i < _scoresRefined.Length; i++)
                {
                    if (_outlierIndexes.Contains(i))
                    {
                        listScoresOutliers.Add(_scoresRefined[i]);
                        listTimesOutliers.Add(_timesRefined[i]);
                    }
                    else
                    {
                        listScoresRefined.Add(_scoresRefined[i]);
                        listTimesRefined.Add(_timesRefined[i]);
                    }
                }
                _scoresRefined  = listScoresRefined.ToArray();
                _timesRefined   = listTimesRefined.ToArray();
                _scoresOutliers = listScoresOutliers.ToArray();
                _timesOutliers  = listTimesOutliers.ToArray();

                return(this);
            }