Example #1
0
        public AreaCVRefinementData(SrmDocument document, AreaCVRefinementSettings settings,
                                    CancellationToken?token = null)
        {
            _settings = settings;
            if (document == null || !document.Settings.HasResults)
            {
                return;
            }

            var replicates  = document.MeasuredResults.Chromatograms.Count;
            var areas       = new List <AreaInfo>(replicates);
            var annotations = AnnotationHelper.GetPossibleAnnotations(document.Settings, settings.Group, AnnotationDef.AnnotationTarget.replicate);

            if (!annotations.Any() && settings.Group == null)
            {
                annotations = new string[] { null }
            }
            ;

            _internalData = new List <InternalData>();
            var hasHeavyMods       = document.Settings.PeptideSettings.Modifications.HasHeavyModifications;
            var hasGlobalStandards = document.Settings.HasGlobalStandardArea;
            var ms1 = settings.MsLevel == AreaCVMsLevel.precursors;

            // Avoid using not-MS1 with a document that is only MS1
            if (!ms1 && document.MoleculeTransitions.All(t => t.IsMs1))
            {
                ms1 = true;
            }
            double?qvalueCutoff = null;

            if (ShouldUseQValues(document))
            {
                qvalueCutoff = _settings.QValueCutoff;
            }

            int?minDetections = null;

            if (_settings.MinimumDetections != -1)
            {
                minDetections = _settings.MinimumDetections;
            }

            MedianInfo medianInfo = null;

            if (_settings.NormalizationMethod == AreaCVNormalizationMethod.medians)
            {
                medianInfo = CalculateMedianAreas(document);
            }

            foreach (var peptideGroup in document.MoleculeGroups)
            {
                foreach (var peptide in peptideGroup.Molecules)
                {
                    foreach (var transitionGroupDocNode in peptide.TransitionGroups)
                    {
                        if (_settings.PointsType == PointsTypePeakArea.decoys != transitionGroupDocNode.IsDecoy)
                        {
                            continue;
                        }

                        foreach (var a in annotations)
                        {
                            areas.Clear();

                            if (a != _settings.Annotation && (_settings.Group == null || _settings.Annotation != null))
                            {
                                continue;
                            }

                            foreach (var i in AnnotationHelper.GetReplicateIndices(document.Settings, _settings.Group, a))
                            {
                                if (token.HasValue && token.Value.IsCancellationRequested)
                                {
                                    throw new Exception(@"Cancelled");
                                }
                                var groupChromInfo = transitionGroupDocNode.GetSafeChromInfo(i)
                                                     .FirstOrDefault(c => c.OptimizationStep == 0);
                                if (groupChromInfo == null)
                                {
                                    continue;
                                }

                                if (qvalueCutoff.HasValue)
                                {
                                    if (!(groupChromInfo.QValue.HasValue && groupChromInfo.QValue.Value < qvalueCutoff.Value))
                                    {
                                        continue;
                                    }
                                }

                                if (!groupChromInfo.Area.HasValue)
                                {
                                    continue;
                                }
                                var index   = i;
                                var sumArea = transitionGroupDocNode.Transitions.Where(t =>
                                {
                                    if (ms1 != t.IsMs1 || !t.ExplicitQuantitative)
                                    {
                                        return(false);
                                    }

                                    var chromInfo = t.GetSafeChromInfo(index)
                                                    .FirstOrDefault(c => c.OptimizationStep == 0);
                                    if (chromInfo == null)
                                    {
                                        return(false);
                                    }
                                    if (_settings.Transitions == AreaCVTransitions.best)
                                    {
                                        return(chromInfo.RankByLevel == 1);
                                    }
                                    if (_settings.Transitions == AreaCVTransitions.all)
                                    {
                                        return(true);
                                    }

                                    return(chromInfo.RankByLevel <= _settings.CountTransitions);
                                    // ReSharper disable once PossibleNullReferenceException
                                }).Sum(t => (double)t.GetSafeChromInfo(index).FirstOrDefault(c => c.OptimizationStep == 0).Area);

                                var normalizedArea = sumArea;
                                if (_settings.NormalizationMethod == AreaCVNormalizationMethod.medians)
                                {
                                    normalizedArea /= medianInfo.Medians[i] / medianInfo.MedianMedian;
                                }
                                else if (_settings.NormalizationMethod == AreaCVNormalizationMethod.global_standards && hasGlobalStandards)
                                {
                                    normalizedArea =
                                        NormalizeToGlobalStandard(document, transitionGroupDocNode, i, sumArea);
                                }
                                else if (_settings.NormalizationMethod == AreaCVNormalizationMethod.ratio && hasHeavyMods && _settings.RatioIndex >= 0)
                                {
                                    var ci = transitionGroupDocNode.GetSafeChromInfo(i).FirstOrDefault(c => c.OptimizationStep == 0);
                                    if (ci != null)
                                    {
                                        var ratioValue = ci.GetRatio(_settings.RatioIndex);
                                        if (ratioValue == null)
                                        {
                                            continue;   // Skip the standards
                                        }
                                        normalizedArea = ratioValue.Ratio;
                                    }
                                }
                                areas.Add(new AreaInfo(sumArea, normalizedArea));
                            }

                            if (qvalueCutoff.HasValue && minDetections.HasValue && areas.Count < minDetections.Value)
                            {
                                continue;
                            }

                            _settings.AddToInternalData(_internalData, areas, peptideGroup, peptide, transitionGroupDocNode, a);
                        }
                    }
                }
            }
            Data = ImmutableList <CVData> .ValueOf(_internalData.GroupBy(i => i, (key, grouped) =>
            {
                var groupedArray = grouped.ToArray();
                return(new CVData(
                           groupedArray.Select(idata => new PeptideAnnotationPair(idata.PeptideGroup, idata.Peptide, idata.TransitionGroup, idata.Annotation, idata.CV)),
                           key.CVBucketed, key.Area, groupedArray.Length));
            }).OrderBy(d => d.CV));
        }
Example #2
0
        public AreaCVRefinementData(SrmDocument document, AreaCVRefinementSettings settings,
                                    CancellationToken token, SrmSettingsChangeMonitor progressMonitor = null)
        {
            _settings = settings;
            if (document == null || !document.Settings.HasResults)
            {
                return;
            }

            var replicates  = document.MeasuredResults.Chromatograms.Count;
            var areas       = new List <AreaInfo>(replicates);
            var annotations = AnnotationHelper.GetPossibleAnnotations(document, settings.Group).ToArray();

            if (!annotations.Any() && settings.Group == null)
            {
                annotations = new string[] { null }
            }
            ;

            _internalData = new List <InternalData>();
            var hasHeavyMods       = document.Settings.PeptideSettings.Modifications.HasHeavyModifications;
            var hasGlobalStandards = document.Settings.HasGlobalStandardArea;
            var ms1 = settings.MsLevel == AreaCVMsLevel.precursors;

            // Avoid using not-MS1 with a document that is only MS1
            if (!ms1 && document.MoleculeTransitions.All(t => t.IsMs1))
            {
                ms1 = true;
            }
            double?qvalueCutoff = null;

            if (ShouldUseQValues(document))
            {
                qvalueCutoff = _settings.QValueCutoff;
            }

            int?minDetections = null;

            if (_settings.MinimumDetections != -1)
            {
                minDetections = _settings.MinimumDetections;
            }

            MedianInfo medianInfo = null;
            int?       ratioIndex = null;

            if (settings.NormalizeOption.IsRatioToLabel)
            {
                var isotopeLabelTypeName = (settings.NormalizeOption.NormalizationMethod as NormalizationMethod.RatioToLabel)
                                           ?.IsotopeLabelTypeName;
                ratioIndex =
                    document.Settings.PeptideSettings.Modifications.RatioInternalStandardTypes.IndexOf(type =>
                                                                                                       type.Name == isotopeLabelTypeName);
            }
            if (_settings.NormalizeOption.Is(NormalizationMethod.EQUALIZE_MEDIANS))
            {
                medianInfo = CalculateMedianAreas(document);
            }
            NormalizationData normalizationData = null;

            foreach (var peptideGroup in document.MoleculeGroups)
            {
                foreach (var peptide in peptideGroup.Molecules)
                {
                    if (progressMonitor != null)
                    {
                        progressMonitor.ProcessMolecule(peptide);
                    }

                    if (_settings.PointsType == PointsTypePeakArea.decoys != peptide.IsDecoy)
                    {
                        continue;
                    }

                    CalibrationCurveFitter calibrationCurveFitter = null;
                    CalibrationCurve       calibrationCurve       = null;
                    IEnumerable <TransitionGroupDocNode> transitionGroups;
                    if (_settings.NormalizeOption == NormalizeOption.CALIBRATED ||
                        _settings.NormalizeOption == NormalizeOption.DEFAULT)
                    {
                        if (!peptide.TransitionGroups.Any())
                        {
                            continue;
                        }
                        var peptideQuantifier = PeptideQuantifier.GetPeptideQuantifier(() =>
                        {
                            return(normalizationData = normalizationData ?? NormalizationData.GetNormalizationData(document, false, null));
                        }, document.Settings, peptideGroup, peptide);
                        calibrationCurveFitter = new CalibrationCurveFitter(peptideQuantifier, document.Settings);
                        transitionGroups       = new[] { peptide.TransitionGroups.First() };
                        if (_settings.NormalizeOption == NormalizeOption.CALIBRATED)
                        {
                            calibrationCurve = calibrationCurveFitter.GetCalibrationCurve();
                            if (calibrationCurve == null)
                            {
                                continue;
                            }
                        }
                    }
                    else
                    {
                        transitionGroups = peptide.TransitionGroups;
                    }
                    foreach (var transitionGroupDocNode in transitionGroups)
                    {
                        foreach (var a in annotations)
                        {
                            areas.Clear();

                            if (!Equals(a, _settings.Annotation) && (_settings.Group == null || _settings.Annotation != null))
                            {
                                continue;
                            }

                            foreach (var replicateIndex in AnnotationHelper.GetReplicateIndices(document,
                                                                                                _settings.Group, a))
                            {
                                if (progressMonitor != null && progressMonitor.IsCanceled())
                                {
                                    throw new OperationCanceledException();
                                }

                                token.ThrowIfCancellationRequested();
                                var groupChromInfo = transitionGroupDocNode.GetSafeChromInfo(replicateIndex)
                                                     .FirstOrDefault(c => c.OptimizationStep == 0);
                                if (groupChromInfo == null)
                                {
                                    continue;
                                }

                                if (qvalueCutoff.HasValue)
                                {
                                    if (!(groupChromInfo.QValue.HasValue &&
                                          groupChromInfo.QValue.Value < qvalueCutoff.Value))
                                    {
                                        continue;
                                    }
                                }

                                double sumArea, normalizedArea;
                                if (calibrationCurveFitter != null)
                                {
                                    double?value;
                                    if (calibrationCurve != null)
                                    {
                                        value = calibrationCurveFitter.GetCalculatedConcentration(calibrationCurve,
                                                                                                  replicateIndex);
                                    }
                                    else
                                    {
                                        value = calibrationCurveFitter.GetNormalizedPeakArea(
                                            new CalibrationPoint(replicateIndex, null));
                                    }
                                    if (!value.HasValue)
                                    {
                                        continue;
                                    }

                                    sumArea        = value.Value;
                                    normalizedArea = value.Value;
                                }
                                else
                                {
                                    if (!groupChromInfo.Area.HasValue)
                                    {
                                        continue;
                                    }
                                    var index = replicateIndex;
                                    sumArea = transitionGroupDocNode.Transitions.Where(t =>
                                    {
                                        if (ms1 != t.IsMs1 || !t.ExplicitQuantitative)
                                        {
                                            return(false);
                                        }

                                        var chromInfo = t.GetSafeChromInfo(index)
                                                        .FirstOrDefault(c => c.OptimizationStep == 0);
                                        if (chromInfo == null)
                                        {
                                            return(false);
                                        }
                                        if (_settings.Transitions == AreaCVTransitions.best)
                                        {
                                            return(chromInfo.RankByLevel == 1);
                                        }
                                        if (_settings.Transitions == AreaCVTransitions.all)
                                        {
                                            return(true);
                                        }

                                        return(chromInfo.RankByLevel <= _settings.CountTransitions);
                                        // ReSharper disable once PossibleNullReferenceException
                                    }).Sum(t => (double)t.GetSafeChromInfo(index)
                                           .FirstOrDefault(c => c.OptimizationStep == 0).Area);

                                    normalizedArea = sumArea;
                                    if (_settings.NormalizeOption.Is(NormalizationMethod.EQUALIZE_MEDIANS))
                                    {
                                        normalizedArea /= medianInfo.Medians[replicateIndex] / medianInfo.MedianMedian;
                                    }
                                    else if (_settings.NormalizeOption.Is(NormalizationMethod.GLOBAL_STANDARDS) &&
                                             hasGlobalStandards)
                                    {
                                        normalizedArea =
                                            NormalizeToGlobalStandard(document, transitionGroupDocNode, replicateIndex,
                                                                      sumArea);
                                    }
                                    else if (_settings.NormalizeOption.Is(NormalizationMethod.TIC))
                                    {
                                        var denominator = document.Settings.GetTicNormalizationDenominator(
                                            replicateIndex, groupChromInfo.FileId);
                                        if (!denominator.HasValue)
                                        {
                                            continue;
                                        }

                                        normalizedArea /= denominator.Value;
                                    }
                                    else if (hasHeavyMods &&
                                             _settings.NormalizeOption.NormalizationMethod is NormalizationMethod
                                             .RatioToLabel)
                                    {
                                        var ci = transitionGroupDocNode.GetSafeChromInfo(replicateIndex)
                                                 .FirstOrDefault(c => c.OptimizationStep == 0);
                                        RatioValue ratioValue = null;
                                        if (ratioIndex.HasValue && ratioIndex.Value >= 0 &&
                                            ratioIndex.Value < ci.Ratios.Count)
                                        {
                                            ratioValue = ci.Ratios[ratioIndex.Value];
                                        }

                                        if (ratioValue == null)
                                        {
                                            continue;
                                        }

                                        normalizedArea = ratioValue.Ratio;
                                    }
                                }
                                areas.Add(new AreaInfo(sumArea, normalizedArea));
                            }

                            if (qvalueCutoff.HasValue && minDetections.HasValue && areas.Count < minDetections.Value)
                            {
                                continue;
                            }

                            _settings.AddToInternalData(_internalData, areas, peptideGroup, peptide, transitionGroupDocNode, a);
                        }
                    }
                }
            }
            Data = ImmutableList <CVData> .ValueOf(_internalData.GroupBy(i => i, (key, grouped) =>
            {
                var groupedArray = grouped.ToArray();
                return(new CVData(
                           groupedArray.Select(idata => new PeptideAnnotationPair(idata.PeptideGroup, idata.Peptide, idata.TransitionGroup, idata.Annotation, idata.CV)),
                           key.CVBucketed, key.Area, groupedArray.Length));
            }).OrderBy(d => d.CV));
        }