Ejemplo n.º 1
0
        private PeptideGroupDocNode Refine(PeptideGroupDocNode nodePepGroup,
            SrmDocument document,
            ICollection<int> outlierIds,
            ICollection<RefinementIdentity> includedPeptides,
            ICollection<RefinementIdentity> repeatedPeptides,
            Dictionary<RefinementIdentity, List<int>> acceptedPeptides,
            SrmSettingsChangeMonitor progressMonitor)
        {
            var listPeptides = new List<PeptideDocNode>();
            int minPrecursors = MinPrecursorsPerPeptide ?? 0;
            foreach (PeptideDocNode nodePep in nodePepGroup.Children)
            {
                if (progressMonitor != null)
                    progressMonitor.ProcessMolecule(nodePep);

                if (outlierIds.Contains(nodePep.Id.GlobalIndex))
                    continue;

                // If there is a set of accepted peptides, and this is not one of them
                // then skip it.
                List<int> acceptedCharges = null;
                if (acceptedPeptides != null &&
                    !acceptedPeptides.TryGetValue(AcceptModified ? new RefinementIdentity(nodePep.RawTextId) : new RefinementIdentity(nodePep.RawUnmodifiedTextId), out acceptedCharges))
                {
                    continue;
                }

                int bestResultIndex = (UseBestResult ? nodePep.BestResult : -1);
                float? peakFoundRatio = nodePep.GetPeakCountRatio(bestResultIndex);
                if (!peakFoundRatio.HasValue)
                {
                    if (RemoveMissingResults)
                        continue;
                }
                else
                {
                    if (MinPeakFoundRatio.HasValue)
                    {
                        if (peakFoundRatio < MinPeakFoundRatio.Value)
                            continue;
                    }
                    if (MaxPeakFoundRatio.HasValue)
                    {
                        if (peakFoundRatio > MaxPeakFoundRatio.Value)
                            continue;
                    }
                }

                PeptideDocNode nodePepRefined = nodePep;
                if (AutoPickPrecursorsAll && nodePep.AutoManageChildren == AutoPickChildrenOff)
                {
                    nodePepRefined = (PeptideDocNode) nodePepRefined.ChangeAutoManageChildren(!AutoPickChildrenOff);
                    var settings = document.Settings;
                    if (!settings.TransitionSettings.Filter.AutoSelect && !AutoPickChildrenOff)
                        settings = settings.ChangeTransitionFilter(filter => filter.ChangeAutoSelect(!AutoPickChildrenOff));
                    nodePepRefined = nodePepRefined.ChangeSettings(settings,
                        new SrmSettingsDiff(false, false, true, false, AutoPickTransitionsAll, false));
                }

                nodePepRefined = Refine(nodePepRefined, document, bestResultIndex, acceptedCharges);
                // Always remove peptides if all precursors have been removed by refinement
                if (!ReferenceEquals(nodePep, nodePepRefined) && nodePepRefined.Children.Count == 0)
                    continue;
                if (nodePepRefined.Children.Count < minPrecursors)
                    continue;

                if (includedPeptides != null)
                {
                    var identity = nodePepRefined.Peptide.IsCustomIon
                        ? new RefinementIdentity(nodePep.Peptide.CustomIon)
                        : new RefinementIdentity(document.Settings.GetModifiedSequence(nodePepRefined));
                    // Skip peptides already added
                    if (includedPeptides.Contains(identity))
                    {
                        // Record repeated peptides for removing duplicate peptides later
                        if (repeatedPeptides != null)
                            repeatedPeptides.Add(identity);
                        continue;
                    }
                    // Record all peptides seen
                    includedPeptides.Add(identity);
                }

                listPeptides.Add(nodePepRefined);
            }

            if (MaxPepPeakRank.HasValue)
            {
                // Calculate the average peak area for each peptide
                int countPeps = listPeptides.Count;
                var listAreaIndexes = new List<PepAreaSortInfo>();
                var internalStandardTypes = document.Settings.PeptideSettings.Modifications.InternalStandardTypes;
                for (int i = 0; i < countPeps; i++)
                {
                    var nodePep = listPeptides[i];
                    // Only peptides with children can possible be ranked by area
                    // Those without should be removed by this operation
                    if (nodePep.Children.Count == 0)
                        continue;
                    int bestResultIndex = (UseBestResult ? nodePep.BestResult : -1);
                    var sortInfo = new PepAreaSortInfo(nodePep, internalStandardTypes, bestResultIndex, listAreaIndexes.Count);
                    listAreaIndexes.Add(sortInfo);
                }

                listAreaIndexes.Sort((p1, p2) => Comparer.Default.Compare(p2.Area, p1.Area));

                // Store area ranks
                var arrayAreaIndexes = new PepAreaSortInfo[listAreaIndexes.Count];
                int iRank = 1;
                foreach (var areaIndex in listAreaIndexes)
                {
                    areaIndex.Rank = iRank++;
                    arrayAreaIndexes[areaIndex.Index] = areaIndex;
                }

                // Add back all peptides with low enough rank.
                listPeptides.Clear();
                foreach (var areaIndex in arrayAreaIndexes)
                {
                    if (areaIndex.Area == 0 || areaIndex.Rank > MaxPepPeakRank.Value)
                        continue;
                    listPeptides.Add(areaIndex.Peptide);
                }
            }

            // Change the children, but only change auto-management, if the child
            // identities have changed, not if their contents changed.
            var childrenNew = listPeptides.ToArray();
            bool updateAutoManage = !PeptideGroupDocNode.AreEquivalentChildren(nodePepGroup.Children, childrenNew);
            return (PeptideGroupDocNode)nodePepGroup.ChangeChildrenChecked(childrenNew, updateAutoManage);
        }
Ejemplo n.º 2
0
            private void FinishLoad(string documentPath, MeasuredResults resultsLoad, bool changeSets)
            {
                if (resultsLoad == null)
                {
                    // Loading was cancelled
                    _manager.EndProcessing(_document);
                    return;
                }

                SrmDocument docNew, docCurrent;
                do
                {
                    docCurrent = _container.Document;
                    var results = docCurrent.Settings.MeasuredResults;
                    // If current document has no results, then cancel
                    if (results == null)
                    {
                        CancelLoad(resultsLoad);
                        return;
                    }

                    try
                    {
                        using (var settingsChangeMonitor = new SrmSettingsChangeMonitor(new LoadMonitor(_manager, _container, null),
                                                                                        Resources.Loader_FinishLoad_Updating_peak_statistics,
                                                                                        _container, docCurrent))
                        {
                            if (changeSets)
                            {
                                // Result is empty cache, due to cancelation
                                results = resultsLoad.Chromatograms.Count != 0 ? resultsLoad : null;
                                docNew = docCurrent.ChangeMeasuredResults(results, settingsChangeMonitor);
                            }
                            else
                            {
                                // Otherwise, switch to new cache
                                results = results.UpdateCaches(documentPath, resultsLoad);
                                docNew = docCurrent.ChangeMeasuredResults(results, settingsChangeMonitor);
                            }
                        }
                    }
                    catch (OperationCanceledException)
                    {
                        // Restart the processing form the top
                        docNew = null;
                    }
                }
                while (docNew == null || !_manager.CompleteProcessing(_container, docNew, docCurrent));

                // Force a document changed event to keep progressive load going
                // until it is complete
                _manager.OnDocumentChanged(_container, new DocumentChangedEventArgs(docCurrent));
            }
Ejemplo n.º 3
0
        public SrmDocument Refine(SrmDocument document, SrmSettingsChangeMonitor progressMonitor)
        {
            HashSet<int> outlierIds = new HashSet<int>();
            if (RTRegressionThreshold.HasValue)
            {
                // TODO: Move necessary code into Model.
                var outliers = RTLinearRegressionGraphPane.CalcOutliers(document,
                    RTRegressionThreshold.Value, RTRegressionPrecision, UseBestResult);

                foreach (var nodePep in outliers)
                    outlierIds.Add(nodePep.Id.GlobalIndex);
            }

            HashSet<RefinementIdentity> includedPeptides = (RemoveRepeatedPeptides ? new HashSet<RefinementIdentity>() : null);
            HashSet<RefinementIdentity> repeatedPeptides = (RemoveDuplicatePeptides ? new HashSet<RefinementIdentity>() : null);
            Dictionary<RefinementIdentity, List<int>> acceptedPeptides = null;
            if (AcceptedPeptides != null)
            {
                acceptedPeptides = new Dictionary<RefinementIdentity, List<int>>();
                foreach (var peptideCharge in AcceptedPeptides)
                {
                    List<int> charges;
                    if (!acceptedPeptides.TryGetValue(new RefinementIdentity(peptideCharge.Sequence), out charges))
                    {
                        charges = (peptideCharge.Charge.HasValue ? new List<int> {peptideCharge.Charge.Value} : null);
                        acceptedPeptides.Add(new RefinementIdentity(peptideCharge.Sequence), charges);
                    }
                    else if (charges != null)
                    {
                        if (peptideCharge.Charge.HasValue)
                            charges.Add(peptideCharge.Charge.Value);
                        else
                            acceptedPeptides[new RefinementIdentity(peptideCharge.Sequence)] = null;
                    }
                }
            }
            HashSet<string> acceptedProteins = (AcceptedProteins != null ? new HashSet<string>(AcceptedProteins) : null);

            var listPepGroups = new List<PeptideGroupDocNode>();
            // Excluding proteins with too few peptides, since they can impact results
            // of the duplicate peptide check.
            int minPeptides = MinPeptidesPerProtein ?? 0;
            foreach (PeptideGroupDocNode nodePepGroup in document.Children)
            {
                if (progressMonitor != null)
                    progressMonitor.ProcessGroup(nodePepGroup);

                if (acceptedProteins != null && !acceptedProteins.Contains(GetAcceptProteinKey(nodePepGroup)))
                    continue;

                PeptideGroupDocNode nodePepGroupRefined = nodePepGroup;
                // If auto-managing all peptides, make sure this flag is set correctly,
                // and update the peptides list, if necessary.
                if (AutoPickPeptidesAll && nodePepGroup.AutoManageChildren == AutoPickChildrenOff)
                {
                    nodePepGroupRefined =
                        (PeptideGroupDocNode) nodePepGroupRefined.ChangeAutoManageChildren(!AutoPickChildrenOff);
                    var settings = document.Settings;
                    if (!AutoPickChildrenOff && !settings.PeptideSettings.Filter.AutoSelect)
                        settings = settings.ChangePeptideFilter(filter => filter.ChangeAutoSelect(true));
                    nodePepGroupRefined = nodePepGroupRefined.ChangeSettings(settings,
                        new SrmSettingsDiff(true, false, false, false, false, false));
                }

                nodePepGroupRefined = Refine(nodePepGroupRefined, document, outlierIds,
                        includedPeptides, repeatedPeptides, acceptedPeptides, progressMonitor);

                if (nodePepGroupRefined.Children.Count < minPeptides)
                    continue;

                listPepGroups.Add(nodePepGroupRefined);
            }

            // Need a second pass, if all duplicate peptides should be removed,
            // and duplicates were found.
            if (repeatedPeptides != null && repeatedPeptides.Count > 0)
            {
                var listPepGroupsFiltered = new List<PeptideGroupDocNode>();
                foreach (PeptideGroupDocNode nodePepGroup in listPepGroups)
                {
                    var listPeptides = new List<PeptideDocNode>();
                    foreach (PeptideDocNode nodePep in nodePepGroup.Children)
                    {
                        var identity = nodePep.Peptide.IsCustomIon
                            ? new RefinementIdentity(nodePep.Peptide.CustomIon)
                            : new RefinementIdentity(document.Settings.GetModifiedSequence(nodePep));
                        if (!repeatedPeptides.Contains(identity))
                            listPeptides.Add(nodePep);
                    }

                    PeptideGroupDocNode nodePepGroupRefined = (PeptideGroupDocNode)
                        nodePepGroup.ChangeChildrenChecked(listPeptides.ToArray(), true);

                    if (nodePepGroupRefined.Children.Count < minPeptides)
                        continue;

                    listPepGroupsFiltered.Add(nodePepGroupRefined);
                }

                listPepGroups = listPepGroupsFiltered;
            }

            return (SrmDocument) document.ChangeChildrenChecked(listPepGroups.ToArray(), true);
        }