public SrmDocument RemoveBelowCutoffs(SrmDocument document)
        {
            var intersection = new HashSet <int>();

            foreach (var data in Data)
            {
                if (_progressMonitor != null && _progressMonitor.IsCanceled())
                {
                    throw new OperationCanceledException();
                }

                if (_progressMonitor != null)
                {
                    _progressMonitor.ProcessMolecule(null);
                }

                var filterByCutoff = data.ToArray();

                var toRemove = IndicesBelowCutoff(_adjustedPValCutoff, _foldChangeCutoff, _msLevel, null,
                                                  filterByCutoff);

                if (intersection.Count == 0)
                {
                    intersection.UnionWith(toRemove);
                }
                else
                {
                    intersection.IntersectWith(toRemove);
                }
            }

            return((SrmDocument)document.RemoveAll(intersection));
        }
Esempio n. 2
0
        private bool UpdateProgressAndCheckForCancellation(SrmSettingsChangeMonitor progressMonitor, int pctComplete)
        {
            if (progressMonitor == null)
            {
                return(true);  // It certainly wasn't cancelled
            }
            if (progressMonitor.IsCanceled())
            {
                return(false);
            }
            var message = Resources.BackgroundProteome_GetUniquenessDict_Examining_background_proteome_for_uniqueness_constraints;

            progressMonitor.ChangeProgress(s => s.ChangeMessage(message).ChangePercentComplete(pctComplete));
            return(true);
        }
Esempio n. 3
0
        public static List <GroupComparisonResult> ComputeResults(GroupComparer groupComparer, SrmDocument document,
                                                                  CancellationToken?cancellationToken, object _lock, SrmSettingsChangeMonitor progressMonitor = null)
        {
            var results       = new List <GroupComparisonResult>();
            var peptideGroups = document.MoleculeGroups.ToArray();

            for (int i = 0; i < peptideGroups.Length; i++)
            {
                if (_lock != null)
                {
                    lock (_lock)
                    {
                        if (cancellationToken.HasValue)
                        {
                            cancellationToken.Value.ThrowIfCancellationRequested();
                        }
                    }
                }

                var peptideGroup = peptideGroups[i];
                IEnumerable <PeptideDocNode> peptides;
                if (groupComparer.ComparisonDef.PerProtein)
                {
                    peptides = new PeptideDocNode[] { null };
                }
                else
                {
                    peptides = peptideGroup.Molecules;
                }
                foreach (var peptide in peptides)
                {
                    if (progressMonitor != null && progressMonitor.IsCanceled())
                    {
                        throw new OperationCanceledException();
                    }
                    if (progressMonitor != null)
                    {
                        progressMonitor.ProcessMolecule(peptide);
                    }
                    results.AddRange(groupComparer.CalculateFoldChanges(peptideGroup, peptide));
                }
            }

            return(results);
        }
Esempio n. 4
0
 // For use in PeptideSettingsUI, where we may need to force completion for peptide uniqueness constraints
 public BackgroundProteome LoadForeground(PeptideSettings settings, SrmSettingsChangeMonitor monitor)
 {
     if (monitor.IsCanceled())
     {
         return(null);
     }
     Assume.IsTrue(ForegroundLoadRequested); // Caller should have called BeginForegroundLoad()
     lock (_lockLoadBackgroundProteome)      // Wait for background loader, if any, to notice
     {
         _monitor = monitor;
         try
         {
             return(Load(null, settings, null, false));
         }
         finally
         {
             _monitor = null;
         }
     }
 }
Esempio n. 5
0
        public IEnumerable <PeptideDocNode> GetPeptideNodes(SrmSettings settings, bool useFilter, SrmSettingsChangeMonitor monitor = null)
        {
            // FASTA sequences can generate a comprehensive list of available peptides.
            FastaSequence fastaSeq = Id as FastaSequence;

            if (fastaSeq != null)
            {
                foreach (PeptideDocNode nodePep in fastaSeq.CreatePeptideDocNodes(settings, useFilter, null))
                {
                    if (monitor != null && monitor.IsCanceled())
                    {
                        throw new OperationCanceledException();
                    }
                    yield return(nodePep);
                }
            }
            // Peptide lists without variable modifications just return their existing children.
            else if (!settings.PeptideSettings.Modifications.HasVariableModifications)
            {
                foreach (PeptideDocNode nodePep in Children)
                {
                    if (monitor != null && monitor.IsCanceled())
                    {
                        throw new OperationCanceledException();
                    }
                    if (!nodePep.HasVariableMods)
                    {
                        yield return(nodePep);
                    }
                }
            }
            // If there are variable modifications, fill out the available list.
            else
            {
                var            setNonExplicit = new HashSet <Peptide>();
                IPeptideFilter filter         = (useFilter ? settings : PeptideFilter.UNFILTERED);
                foreach (PeptideDocNode nodePep in Children)
                {
                    if (monitor != null && monitor.IsCanceled())
                    {
                        throw new OperationCanceledException();
                    }
                    if (nodePep.Peptide.IsCustomMolecule) // Modifications mean nothing to custom ions // TODO(bspratt) but static isotope labels do?
                    {
                        yield return(nodePep);
                    }
                    else if (nodePep.HasExplicitMods && !nodePep.HasVariableMods)
                    {
                        yield return(nodePep);
                    }
                    else if (!setNonExplicit.Contains(nodePep.Peptide))
                    {
                        bool returnedResult = false;
                        var  peptide        = nodePep.Peptide;
                        // The peptide will be returned as the Id of the unmodified instance of this
                        // peptide.  If the peptide DocNode is explicitly modified this will cause
                        // two nodes in the tree to have the same Id.  So, use a copy instead.
                        if (nodePep.HasExplicitMods)
                        {
                            peptide = (Peptide)peptide.Copy();
                        }
                        foreach (PeptideDocNode nodePepResult in peptide.CreateDocNodes(settings, filter))
                        {
                            yield return(nodePepResult);

                            returnedResult = true;
                        }
                        // Make sure the peptide is not removed due to filtering
                        if (!returnedResult)
                        {
                            yield return(nodePep);
                        }
                        setNonExplicit.Add(nodePep.Peptide);
                    }
                }
            }
        }
Esempio n. 6
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));
        }