Ejemplo n.º 1
0
 public RemoteChromDataProvider(SrmDocument document, IRetentionTimePredictor retentionTimePredictor, ChromFileInfo chromFileInfo, ProgressStatus progressStatus, int startPercent,
     int endPercent, ILoadMonitor loader)
     : base(chromFileInfo, progressStatus, startPercent, endPercent, loader)
 {
     _document = document;
     ChorusUrl chorusUrl = (ChorusUrl)chromFileInfo.FilePath;
     _chorusAccount = chorusUrl.FindChorusAccount(Settings.Default.ChorusAccountList);
     var chromatogramRequestProviders = new List<ChromatogramRequestProvider>();
     foreach (bool firstPass in new[] {true, false})
     {
         if (null == retentionTimePredictor && !firstPass)
         {
             continue;
         }
         var chromatogramRequestProvider = new ChromatogramRequestProvider(document, chorusUrl,
             retentionTimePredictor, firstPass);
         if (0 == chromatogramRequestProvider.ChromKeys.Count)
         {
             continue;
         }
         chromatogramRequestProviders.Add(chromatogramRequestProvider);
     }
     _chromatogramRequestProviders = chromatogramRequestProviders.ToArray();
     _chromTaskLists = new ChromTaskList[_chromatogramRequestProviders.Length];
 }
Ejemplo n.º 2
0
        public RemoteChromDataProvider(SrmDocument document, IRetentionTimePredictor retentionTimePredictor, ChromFileInfo chromFileInfo, IProgressStatus progressStatus, int startPercent,
                                       int endPercent, ILoadMonitor loader)
            : base(chromFileInfo, progressStatus, startPercent, endPercent, loader)
        {
            _document = document;
            ChorusUrl chorusUrl = (ChorusUrl)chromFileInfo.FilePath;

            _chorusAccount = chorusUrl.FindChorusAccount(Settings.Default.RemoteAccountList);
            var chromatogramRequestProviders = new List <ChromatogramRequestProvider>();

            foreach (bool firstPass in new[] { true, false })
            {
                if (null == retentionTimePredictor && !firstPass)
                {
                    continue;
                }
                var chromatogramRequestProvider = new ChromatogramRequestProvider(document, chorusUrl,
                                                                                  retentionTimePredictor, firstPass);
                if (0 == chromatogramRequestProvider.ChromKeys.Count)
                {
                    continue;
                }
                chromatogramRequestProviders.Add(chromatogramRequestProvider);
            }
            _chromatogramRequestProviders = chromatogramRequestProviders.ToArray();
            _chromTaskLists = new ChromTaskList[_chromatogramRequestProviders.Length];
        }
Ejemplo n.º 3
0
 /// <summary>
 /// Calculate the predicted NETs of the evidences
 /// </summary>
 /// <param name="predictor">Retention Time Predictor</param>
 /// <param name="evidences">Evidences to process</param>
 /// <remarks>
 /// For each evidence, it passes the clean peptide sequence through the peptideCache alongside
 /// the predictor to determine the predicted NET. If the peptide has been seen before, it has
 /// already been added into a dictionary and so it simply looks up the relevant NET for the
 /// peptide and returns that. Otherwise, it passes the peptide through the predictor's
 /// GetElutionTime method, adds that value to the peptide cache with the sequence as the key
 /// so that if it is seen again it will get the value faster.
 /// </remarks>
 public void CalculatePredictedNet(IRetentionTimePredictor predictor, IEnumerable <Evidence> evidences)
 {
     Parallel.ForEach(evidences, evidence =>
     {
         evidence.PredictedNet = ComputePeptideNET(evidence.CleanPeptide, predictor);
     });
 }
Ejemplo n.º 4
0
        /// <summary>
        /// Displays the selected dataset.
        /// </summary>
        private void ShowSelectedDataset()
        {
            try
            {
                Analysis analysis = GetSelectedAnalysis();
                if (analysis != null)
                {
                    m_selectedAnalysis = analysis;

                    // If the analysis has not been processed, then do so...
                    if (analysis.ProcessedState != ProcessingState.Processed)
                    {
                        //LockUserInterface(true);

                        IRetentionTimePredictor predictor = RetentionTimePredictorFactory.CreatePredictor(m_options.PredictorType);
                        m_processor.ProcessAnalysisJob(analysis,
                                                       m_options,
                                                       predictor);
                    }
                    else
                    {
                        ctlChartScanVsNET.SeriesCollection.Clear();
                        DisplayScansVsNet(analysis);
                    }
                }
                else
                {
                    OnStatus("No analysis was selected.");
                }
            }
            catch (Exception ex)
            {
                OnStatus(ex.Message);
            }
        }
 public ChromatogramRequestProvider(SrmDocument srmDocument, ChorusUrl chorusUrl, IRetentionTimePredictor retentionTimePredictor, bool firstPass)
 {
     _srmDocument = srmDocument;
     _chorusUrl = chorusUrl;
     _retentionTimePredictor = retentionTimePredictor;
     _firstPass = firstPass;
     // Create a SpectrumFilter without an IRetentionTimeProvider in order to get the list of ChromKeys that we will eventually provide.
     SpectrumFilter spectrumFilter = new SpectrumFilter(_srmDocument, _chorusUrl, null);
     _chromKeys = ImmutableList.ValueOf(ListChromKeys(GetChromatogramRequestDocument(spectrumFilter)));
 }
Ejemplo n.º 6
0
        public ChromatogramRequestProvider(SrmDocument srmDocument, ChorusUrl chorusUrl, IRetentionTimePredictor retentionTimePredictor, bool firstPass)
        {
            _srmDocument            = srmDocument;
            _chorusUrl              = chorusUrl;
            _retentionTimePredictor = retentionTimePredictor;
            _firstPass              = firstPass;
            // Create a SpectrumFilter without an IRetentionTimeProvider in order to get the list of ChromKeys that we will eventually provide.
            SpectrumFilter spectrumFilter = new SpectrumFilter(_srmDocument, _chorusUrl, null);

            _chromKeys = ImmutableList.ValueOf(ListChromKeys(GetChromatogramRequestDocument(spectrumFilter)));
        }
Ejemplo n.º 7
0
        /// <summary>
        /// Calculate the predicted NET of a single peptide
        /// </summary>
        /// <param name="peptide"></param>
        /// <param name="predictor">Retention Time Predictor</param>
        /// <returns></returns>
        public double ComputePeptideNET(string peptide, IRetentionTimePredictor predictor)
        {
            double predictedNET;

            if (!PeptideCache.TryGetValue(peptide, out predictedNET))
            {
                predictedNET = predictor.GetElutionTime(peptide);
                PeptideCache.Add(peptide, predictedNET);
            }

            return(predictedNET);
        }
Ejemplo n.º 8
0
        /// <summary>
        /// Configure and return the appropriate Retention Time Predictor
        /// </summary>
        /// <param name="type"></param>
        /// <returns></returns>
        public static IRetentionTimePredictor CreatePredictor(RetentionTimePredictionType type)
        {
            IRetentionTimePredictor predictor = null;

            switch (type)
            {
            case RetentionTimePredictionType.KROKHIN:
                predictor = new KrokhinPredictor();
                break;

            case RetentionTimePredictionType.KANGAS:
                predictor = new KangasPredictor();
                break;
            }

            return(predictor);
        }
Ejemplo n.º 9
0
        private bool CanSchedule(SrmDocument document, IRetentionTimePredictor retentionTimePredictor)
        {
            bool canSchedule;

            if (RetentionTimeFilterType.scheduling_windows == _fullScan.RetentionTimeFilterType)
            {
                canSchedule =
                    document.Settings.PeptideSettings.Prediction.CanSchedule(document, PeptidePrediction.SchedulingStrategy.any) ||
                    null != retentionTimePredictor;
            }
            else if (RetentionTimeFilterType.ms2_ids == _fullScan.RetentionTimeFilterType)
            {
                canSchedule = true;
            }
            else
            {
                canSchedule = false;
            }
            return(canSchedule);
        }
Ejemplo n.º 10
0
        public static IRetentionTimePredictor CreatePredictor(RetentionTimePredictionType type)
        {
            IRetentionTimePredictor predictor = null;

            switch (type)
            {
            case RetentionTimePredictionType.Krokhin:
                predictor = new KrokhinPredictor();
                break;

            case RetentionTimePredictionType.Kangas:
                predictor = new KangasPredictor();
                break;

            default:
                break;
            }

            return(predictor);
        }
Ejemplo n.º 11
0
        /// <summary>
        /// Processes an analysis job (SEQUEST or X!Tandem)
        /// </summary>
        /// <param name="analysis">Job to process</param>
        /// <param name="options">Options to use for processing.</param>
        /// <param name="massTagDatabase">Mass tag database to update.</param>
        /// <returns>True if successful, false if processing failed.</returns>
        public void ProcessAnalysisJob(Analysis analysis, Options options, IRetentionTimePredictor predictor)
        {
            lock (m_threadpool)
            {
                bool hasAnalysis = m_threadpool.ContainsKey(analysis);
                if (hasAnalysis)
                {
                    return;
                }

                ParameterizedThreadStart start = new ParameterizedThreadStart(ProcessAnalysisJobInternal);
                Thread analysisthread          = new Thread(start);

                AnalysisJob data = new AnalysisJob();
                data.Analysis  = analysis;
                data.Options   = options;
                data.Predictor = predictor;
                analysisthread.Start(data);

                m_threadpool.Add(analysis, analysisthread);
            }
        }
Ejemplo n.º 12
0
        /// <summary>
        /// Perform target alignment
        /// </summary>
        /// <param name="evidences">Evidences to align</param>
        /// <param name="baseline">Baseline evidences</param>
        /// <returns></returns>
        public LinearRegressionResult AlignTargets(List <Evidence> evidences, List <Evidence> baseline)
        {
            var observed  = new List <double>();
            var predicted = new List <double>();

            foreach (var evidence in baseline)
            {
                Predictor             = RetentionTimePredictorFactory.CreatePredictor(Options.PredictorType);
                evidence.PredictedNet = Predictor.GetElutionTime(Evidence.CleanSequence(evidence.Sequence));

                observed.Add(evidence.ObservedNet);
                predicted.Add(evidence.PredictedNet);
            }

            Regressor = LinearRegressorFactory.Create(Options.RegressionType);
            var result = Regressor.CalculateRegression(observed, predicted);

            foreach (var evidence in evidences)
            {
                evidence.ObservedNet = Regressor.Transform(result, evidence.ObservedNet);
            }

            return(result);
        }
Ejemplo n.º 13
0
        public SpectrumFilter(SrmDocument document, MsDataFileUri msDataFileUri, IFilterInstrumentInfo instrumentInfo,
                              IRetentionTimePredictor retentionTimePredictor = null, bool firstPass = false)
        {
            _fullScan          = document.Settings.TransitionSettings.FullScan;
            _instrument        = document.Settings.TransitionSettings.Instrument;
            _acquisitionMethod = _fullScan.AcquisitionMethod;
            if (instrumentInfo != null)
            {
                _isWatersFile = instrumentInfo.IsWatersFile;
            }
            IsFirstPass = firstPass;

            var comparer = PrecursorTextId.PrecursorTextIdComparerInstance;
            var dictPrecursorMzToFilter = new SortedDictionary <PrecursorTextId, SpectrumFilterPair>(comparer);

            if (EnabledMs || EnabledMsMs)
            {
                if (EnabledMs)
                {
                    _isHighAccMsFilter = !Equals(_fullScan.PrecursorMassAnalyzer,
                                                 FullScanMassAnalyzerType.qit);

                    if (!firstPass)
                    {
                        var key = new PrecursorTextId(SignedMz.ZERO, null, ChromExtractor.summed);  // TIC
                        dictPrecursorMzToFilter.Add(key, new SpectrumFilterPair(key, PeptideDocNode.UNKNOWN_COLOR, dictPrecursorMzToFilter.Count,
                                                                                _instrument.MinTime, _instrument.MaxTime, null, null, 0, _isHighAccMsFilter, _isHighAccProductFilter));
                        key = new PrecursorTextId(SignedMz.ZERO, null, ChromExtractor.base_peak);   // BPC
                        dictPrecursorMzToFilter.Add(key, new SpectrumFilterPair(key, PeptideDocNode.UNKNOWN_COLOR, dictPrecursorMzToFilter.Count,
                                                                                _instrument.MinTime, _instrument.MaxTime, null, null, 0, _isHighAccMsFilter, _isHighAccProductFilter));
                    }
                }
                if (EnabledMsMs)
                {
                    _isHighAccProductFilter = !Equals(_fullScan.ProductMassAnalyzer,
                                                      FullScanMassAnalyzerType.qit);

                    if (_fullScan.AcquisitionMethod == FullScanAcquisitionMethod.DIA &&
                        _fullScan.IsolationScheme.IsAllIons)
                    {
                        if (instrumentInfo != null)
                        {
                            _isWatersMse  = _isWatersFile;
                            _isAgilentMse = instrumentInfo.IsAgilentFile;
                        }
                        _mseLevel = 1;
                    }
                }

                Func <double, double> calcWindowsQ1 = _fullScan.GetPrecursorFilterWindow;
                Func <double, double> calcWindowsQ3 = _fullScan.GetProductFilterWindow;
                _minTime = _instrument.MinTime;
                _maxTime = _instrument.MaxTime;
                bool canSchedule = CanSchedule(document, retentionTimePredictor);
                // TODO: Figure out a way to turn off time sharing on first SIM scan so that
                //       times can be shared for MS1 without SIM scans
                _isSharedTime = !canSchedule;

                // If we're using bare measured drift times from spectral libraries, go get those now
                var libraryIonMobilityInfo = document.Settings.PeptideSettings.Prediction.UseLibraryDriftTimes
                    ? document.Settings.GetIonMobilities(msDataFileUri)
                    : null;

                foreach (var nodePep in document.Molecules)
                {
                    if (firstPass && !retentionTimePredictor.IsFirstPassPeptide(nodePep))
                    {
                        continue;
                    }

                    foreach (TransitionGroupDocNode nodeGroup in nodePep.Children)
                    {
                        if (nodeGroup.Children.Count == 0)
                        {
                            continue;
                        }

                        double?       minTime = _minTime, maxTime = _maxTime;
                        double?       startDriftTimeMsec = null, endDriftTimeMsec = null;
                        double        windowDT;
                        double        highEnergyDriftTimeOffsetMsec = 0;
                        DriftTimeInfo centerDriftTime               = document.Settings.PeptideSettings.Prediction.GetDriftTime(
                            nodePep, nodeGroup, libraryIonMobilityInfo, out windowDT);
                        if (centerDriftTime.DriftTimeMsec(false).HasValue)
                        {
                            startDriftTimeMsec            = centerDriftTime.DriftTimeMsec(false) - windowDT / 2; // Get the low energy drift time
                            endDriftTimeMsec              = startDriftTimeMsec + windowDT;
                            highEnergyDriftTimeOffsetMsec = centerDriftTime.HighEnergyDriftTimeOffsetMsec;
                        }

                        if (canSchedule)
                        {
                            if (RetentionTimeFilterType.scheduling_windows == _fullScan.RetentionTimeFilterType)
                            {
                                double?centerTime = null;
                                double windowRT   = 0;
                                if (retentionTimePredictor != null)
                                {
                                    centerTime = retentionTimePredictor.GetPredictedRetentionTime(nodePep);
                                }
                                else
                                {
                                    var prediction = document.Settings.PeptideSettings.Prediction;
                                    if (prediction.RetentionTime == null || !prediction.RetentionTime.IsAutoCalculated)
                                    {
                                        centerTime = document.Settings.PeptideSettings.Prediction.PredictRetentionTimeForChromImport(
                                            document, nodePep, nodeGroup, out windowRT);
                                    }
                                }
                                // Force the center time to be at least zero
                                if (centerTime.HasValue && centerTime.Value < 0)
                                {
                                    centerTime = 0;
                                }
                                if (_fullScan.RetentionTimeFilterLength != 0)
                                {
                                    windowRT = _fullScan.RetentionTimeFilterLength * 2;
                                }
                                if (centerTime != null)
                                {
                                    double startTime = centerTime.Value - windowRT / 2;
                                    double endTime   = startTime + windowRT;
                                    minTime = Math.Max(minTime ?? 0, startTime);
                                    maxTime = Math.Min(maxTime ?? double.MaxValue, endTime);
                                }
                            }
                            else if (RetentionTimeFilterType.ms2_ids == _fullScan.RetentionTimeFilterType)
                            {
                                var times = document.Settings.GetBestRetentionTimes(nodePep, msDataFileUri);
                                if (times.Length > 0)
                                {
                                    minTime = Math.Max(minTime ?? 0, times.Min() - _fullScan.RetentionTimeFilterLength);
                                    maxTime = Math.Min(maxTime ?? double.MaxValue, times.Max() + _fullScan.RetentionTimeFilterLength);
                                }
                            }
                        }

                        SpectrumFilterPair filter;
                        string             textId = nodePep.RawTextId; // Modified Sequence for peptides, or some other string for custom ions
                        var mz  = new SignedMz(nodeGroup.PrecursorMz, nodeGroup.PrecursorCharge < 0);
                        var key = new PrecursorTextId(mz, textId, ChromExtractor.summed);
                        if (!dictPrecursorMzToFilter.TryGetValue(key, out filter))
                        {
                            filter = new SpectrumFilterPair(key, nodePep.Color, dictPrecursorMzToFilter.Count, minTime, maxTime,
                                                            startDriftTimeMsec, endDriftTimeMsec, highEnergyDriftTimeOffsetMsec,
                                                            _isHighAccMsFilter, _isHighAccProductFilter);
                            dictPrecursorMzToFilter.Add(key, filter);
                        }

                        if (!EnabledMs)
                        {
                            filter.AddQ3FilterValues(from TransitionDocNode nodeTran in nodeGroup.Children
                                                     select nodeTran.Mz, calcWindowsQ3);
                        }
                        else if (!EnabledMsMs)
                        {
                            filter.AddQ1FilterValues(GetMS1MzValues(nodeGroup), calcWindowsQ1);
                        }
                        else
                        {
                            filter.AddQ1FilterValues(GetMS1MzValues(nodeGroup), calcWindowsQ1);
                            filter.AddQ3FilterValues(from TransitionDocNode nodeTran in nodeGroup.Children
                                                     where !nodeTran.IsMs1
                                                     select nodeTran.Mz, calcWindowsQ3);
                        }
                    }
                }
                _filterMzValues = dictPrecursorMzToFilter.Values.ToArray();

                var listChromKeyFilterIds = new List <ChromKey>();
                foreach (var spectrumFilterPair in _filterMzValues)
                {
                    spectrumFilterPair.AddChromKeys(listChromKeyFilterIds);
                }
                _productChromKeys = listChromKeyFilterIds.ToArray();

                // Sort a copy of the filter pairs by maximum retention time so that we can detect when
                // filters are no longer active.
                _filterRTValues = new SpectrumFilterPair[_filterMzValues.Length];
                Array.Copy(_filterMzValues, _filterRTValues, _filterMzValues.Length);
                Array.Sort(_filterRTValues, CompareByRT);
            }

            InitRTLimits();
        }
Ejemplo n.º 14
0
        /// <summary>
        /// Processes a list of dataset objects and returns a MTDB.
        /// </summary>
        private MassTagDatabase CreateDatabaseInternal(
            Options options,
            List <Analysis> analysisDescriptions)
        {
            MassTagDatabase         massTagDatabase = new MassTagDatabase();
            IRetentionTimePredictor predictor       = RetentionTimePredictorFactory.CreatePredictor(options.PredictorType);
            bool success = true;

            // Analyze each of the datasets, putting the results into the mass tag database
            // To expedite this process, only analyze if the analysis has not already been processed.
            int analysisNumber = 1;

            foreach (Analysis analysis in analysisDescriptions)
            {
                try
                {
                    if (analysis.ProcessedState != ProcessingState.Processed)
                    {
                        ProcessAnalysisJobInternal(analysis, options, predictor);
                    }

                    massTagDatabase.AddResults(analysis.Targets,
                                               options.Regression,
                                               predictor);
                    massTagDatabase.AddMetaData(analysis);

                    GC.Collect();
                    GC.WaitForPendingFinalizers();
                    GC.Collect();
                    GC.WaitForPendingFinalizers();

                    if (AnalysisCompleted != null)
                    {
                        AnalysisCompleted(this, new AnalysisCompletedEventArgs(analysisNumber++, analysisDescriptions.Count, analysis));
                    }
                }
                catch (IOException ex)
                {
                    analysis.ProcessedState = ProcessingState.NotProcessed;
                    success = false;
                    OnError(string.Format("Failed to open the analysis file {0} - {1}", analysis.FilePath, ex.Message));
                    break;
                }
                catch (AnalysisToolException ex)
                {
                    analysis.ProcessedState = ProcessingState.NotProcessed;
                    success = false;
                    OnError(string.Format("The analysis failed for {0} - {1}", analysis.Name, ex.Message));
                    break;
                }
                catch (Exception ex)
                {
                    analysis.ProcessedState = ProcessingState.NotProcessed;
                    success = false;
                    OnError(string.Format("The analysis failed for {0} - {1}", analysis.Name, ex.Message));
                    break;
                }
            }

            if (success)
            {
                massTagDatabase.FinalizeDatabase();
            }
            else
            {
                massTagDatabase = null;

                if (this.AnalysisFailed != null)
                {
                    AnalysisFailed(this, new DatabaseCreatedEventArgs(null));
                }
            }
            return(massTagDatabase);
        }
Ejemplo n.º 15
0
        private void ProcessAnalysisJobInternal(Analysis analysis, Options options, IRetentionTimePredictor predictor)
        {
            analysis.ProcessedState = ProcessingState.Processing;
            if (this.ProcessingAnalysis != null)
            {
                ProcessingAnalysis(this, new AnalysisCompletedEventArgs(1, 1, analysis));
            }

            IAnalysisReader reader = SequenceAnalysisToolsFactory.CreateReader(analysis.Tool);
            ITargetFilter   filter = TargetFilterFactory.CreateFilters(analysis.Tool, options);

            IRegressionAlgorithm regressor = RegressorFactory.CreateRegressor(RegressorType.LcmsRegressor, options.Regression);
            Analysis             results   = reader.Read(analysis.FilePath, analysis.Name);


            List <Target> targets = new List <Target>();
            Dictionary <string, Protein> proteins = new Dictionary <string, Protein>();

            foreach (var target in results.Targets)
            {
                if (options.ShouldFilter(target))
                {
                    continue;
                }

                targets.Add(target);

                foreach (var protein in target.Proteins)
                {
                    if (!proteins.ContainsKey(protein.Reference))
                    {
                        analysis.Proteins.Add(protein);
                        proteins.Add(protein.Reference, protein);
                    }
                }
            }
            analysis.AddTargets(targets);

            // Calculate the regression based on the target data
            List <float> predicted = new List <float>();
            List <float> scans     = new List <float>();

            int maxScan = 0;
            int minScan = 0;



            foreach (Target target in analysis.Targets)
            {
                // Filter the target here...based on use for alignment.
                if (filter.ShouldFilter(target))
                {
                    continue;
                }

                maxScan = Math.Max(maxScan, target.Scan);
                minScan = Math.Min(minScan, target.Scan);

                target.IsPredicted  = true;
                target.NetPredicted = predictor.GetElutionTime(target.CleanSequence);
                scans.Add(target.Scan);
                predicted.Add(Convert.ToSingle(target.NetPredicted));
            }
            analysis.RegressionResults = regressor.CalculateRegression(scans, predicted);
            //analysis.RegressionResults.Regressor = regressor;

            analysis.RegressionResults.MinScan = minScan;
            analysis.RegressionResults.MaxScan = maxScan;

            // Make the regression line data.
            int scan  = 0;
            int delta = 5;

            while (scan <= maxScan)
            {
                double y = regressor.GetTransformedNET(scan);
                scan += delta;

                analysis.RegressionResults.XValues.Add(Convert.ToDouble(scan));
                analysis.RegressionResults.YValues.Add(y);
            }


            // Then make sure we align all against the predicted self
            regressor.ApplyTransformation(analysis.Targets);

            analysis.ProcessedState = ProcessingState.Processed;
        }
Ejemplo n.º 16
0
        /// <summary>
        /// Adds a list of mass tags to the database.
        /// </summary>
        /// <param name="targets"></param>
        /// <param name="predictor"></param>
        private void AddMassTags(List <Target> targets,
                                 IRetentionTimePredictor predictor)
        {
            foreach (Target target in targets)
            {
                string peptideWithMod = target.Sequence + target.SequenceData.ModificationDescription;
                double highNorm       = target.HighNormalizedScore;

                // Here we see if the mass tag has already been added to the database
                // If it has then update the tag's data if appropriate for the max values.
                if (m_massTagMap.ContainsKey(peptideWithMod))
                {
                    ConsensusTarget consensusTarget = m_massTagMap[peptideWithMod];
                    if (consensusTarget.HighNormalizedScore < highNorm)
                    {
                        consensusTarget.HighNormalizedScore = highNorm;
                    }
                    if (consensusTarget.LogEValue > target.LogPeptideEValue)
                    {
                        consensusTarget.LogEValue = target.LogPeptideEValue;
                    }

                    // Map the proteins
                    target.Proteins.ForEach(x => consensusTarget.AddProtein(x));
                    target.ParentTarget = consensusTarget;
                    consensusTarget.Targets.Add(target);


                    // Increment the number of observations
                    consensusTarget.NumberOfObservations++;
                }
                else
                {
                    // Otherwise we create a new mass tag entry into the database.
                    ConsensusTarget consensusTarget = new ConsensusTarget();
                    consensusTarget.MonoisotopicMass = target.MonoisotopicMass - (target.Charge * ConsensusTarget.PROTON_MASS);
                    consensusTarget.Id                      = m_massTags.Count;
                    consensusTarget.LogEValue               = target.LogPeptideEValue;
                    consensusTarget.HighNormalizedScore     = highNorm;
                    consensusTarget.NumberOfObservations    = 1;
                    consensusTarget.ModificationCount       = target.SequenceData.ModificationCount;
                    consensusTarget.ModificationDescription = target.SequenceData.ModificationDescription;
                    consensusTarget.MultipleProteins        = target.MultiProteinCount;
                    consensusTarget.PmtQualityScore         = 0;
                    consensusTarget.Sequence                = target.Sequence;
                    try
                    {
                        consensusTarget.CleanSequence = Target.CleanPeptide(consensusTarget.Sequence);
                    }
                    catch (Exception ex)
                    {
                        int x = 9;
                        x++;
                        if (x < 8)
                        {
                        }
                    }
                    consensusTarget.NetPredicted = predictor.GetElutionTime(target.CleanSequence);
                    consensusTarget.PeptideObservationCountPassingFilter = 0;
                    consensusTarget.Targets.Add(target);

                    // Then link the child target to these parents
                    target.ParentTarget = consensusTarget;

                    m_massTags.Add(consensusTarget);
                    m_massTagMap.Add(peptideWithMod, consensusTarget);
                }
            }


            foreach (var consensusTarget in m_massTags)
            {
                Dictionary <string, Protein> proteins = new Dictionary <string, Protein>();
                foreach (var target in consensusTarget.Targets)
                {
                    foreach (var protein in target.Proteins)
                    {
                        if (!proteins.ContainsKey(protein.Reference))
                        {
                            consensusTarget.AddProtein(protein);
                        }
                    }
                }
            }
        }
Ejemplo n.º 17
0
 /// <summary>
 /// Adds a list of peptide results to the database.
 /// </summary>
 /// <param name="peptides"></param>
 /// <param name="type"></param>
 public void AddResults(List <Target> targets,
                        RegressionTypeIdentifier type,
                        IRetentionTimePredictor predictor)
 {
     AddMassTags(targets, predictor);
 }
Ejemplo n.º 18
0
 private bool CanSchedule(SrmDocument document, IRetentionTimePredictor retentionTimePredictor)
 {
     bool canSchedule;
     if (RetentionTimeFilterType.scheduling_windows == _fullScan.RetentionTimeFilterType)
     {
         canSchedule =
             document.Settings.PeptideSettings.Prediction.CanSchedule(document, PeptidePrediction.SchedulingStrategy.any) ||
             null != retentionTimePredictor;
     }
     else if (RetentionTimeFilterType.ms2_ids == _fullScan.RetentionTimeFilterType)
     {
         canSchedule = true;
     }
     else
     {
         canSchedule = false;
     }
     return canSchedule;
 }
Ejemplo n.º 19
0
        public SpectraChromDataProvider(MsDataFileImpl dataFile,
            ChromFileInfo fileInfo,
            SrmDocument document,
            IRetentionTimePredictor retentionTimePredictor,
            string cachePath, // We'll write tempfiles in this directory
            ProgressStatus status,
            int startPercent,
            int endPercent,
            IProgressMonitor loader)
            : base(fileInfo, status, startPercent, endPercent, loader)
        {
            _document = document;
            _filePath = dataFile.FilePath;
            _cachePath = cachePath;

            // If no SRM spectra, then full-scan filtering must be enabled
            _isSrm = dataFile.HasSrmSpectra;
            if (!_isSrm)
            {
                if (!document.Settings.TransitionSettings.FullScan.IsEnabled)
                    throw new NoFullScanFilteringException(dataFile.FilePath);

                // Only use the retention time predictor on non-SRM data, and only when
                // there are enough transitions to cause performance issues with extracting
                // full-gradient in a single pass, and then trimming.
                if (_document.Settings.TransitionSettings.FullScan.RetentionTimeFilterType == RetentionTimeFilterType.scheduling_windows &&
                    _document.MoleculeTransitionCount > MAX_FULL_GRADIENT_TRANSITIONS)
                {
                    _retentionTimePredictor = retentionTimePredictor;
                }
            }

            // Only mzXML from mzWiff requires the introduction of zero values
            // during interpolation.
            _isProcessedScans = dataFile.IsMzWiffXml;

            UpdatePercentComplete();

            // Create the filter responsible for chromatogram extraction
            bool firstPass = (_retentionTimePredictor != null);
            _filter = new SpectrumFilter(_document, FileInfo.FilePath, new DataFileInstrumentInfo(dataFile),
                _retentionTimePredictor, firstPass);

            if (!_isSrm && (_filter.EnabledMs || _filter.EnabledMsMs))
            {
                // Full-scan filtering should always match a single precursor
                // m/z value to a single precursor node in the document tree,
                // because that is the way the filters are constructed in the
                // first place.
                _isSingleMzMatch = true;
            }

            // Get data object used to graph all of the chromatograms.
            if (_loader.HasUI)
                _allChromData = LoadingStatus.Transitions;

            try
            {
                InitSpectrumReader(dataFile);
                InitChromatogramExtraction();
            }
            catch(Exception)
            {
                // If exception thrown before construction is complete than Dispose will not be called.
                if (_spectra == null)
                    dataFile.Dispose();
                else
                    _spectra.Dispose();

                throw;
            }
        }
Ejemplo n.º 20
0
        public SpectrumFilter(SrmDocument document, MsDataFileUri msDataFileUri, IFilterInstrumentInfo instrumentInfo,
            IRetentionTimePredictor retentionTimePredictor = null, bool firstPass = false)
        {
            _fullScan = document.Settings.TransitionSettings.FullScan;
            _instrument = document.Settings.TransitionSettings.Instrument;
            _acquisitionMethod = _fullScan.AcquisitionMethod;
            if (instrumentInfo != null)
                _isWatersFile = instrumentInfo.IsWatersFile;
            IsFirstPass = firstPass;

            var comparer = PrecursorTextId.PrecursorTextIdComparerInstance;
            var dictPrecursorMzToFilter = new SortedDictionary<PrecursorTextId, SpectrumFilterPair>(comparer);

            if (EnabledMs || EnabledMsMs)
            {
                if (EnabledMs)
                {
                    _isHighAccMsFilter = !Equals(_fullScan.PrecursorMassAnalyzer,
                        FullScanMassAnalyzerType.qit);

                    if (!firstPass)
                    {
                        var key = new PrecursorTextId(0, null, ChromExtractor.summed);  // TIC
                        dictPrecursorMzToFilter.Add(key, new SpectrumFilterPair(key, PeptideDocNode.UNKNOWN_COLOR, dictPrecursorMzToFilter.Count,
                            _instrument.MinTime, _instrument.MaxTime, null, null, 0, _isHighAccMsFilter, _isHighAccProductFilter));
                        key = new PrecursorTextId(0, null, ChromExtractor.base_peak);   // BPC
                        dictPrecursorMzToFilter.Add(key, new SpectrumFilterPair(key, PeptideDocNode.UNKNOWN_COLOR, dictPrecursorMzToFilter.Count,
                            _instrument.MinTime, _instrument.MaxTime, null, null, 0, _isHighAccMsFilter, _isHighAccProductFilter));
                    }
                }
                if (EnabledMsMs)
                {
                    _isHighAccProductFilter = !Equals(_fullScan.ProductMassAnalyzer,
                        FullScanMassAnalyzerType.qit);

                    if (_fullScan.AcquisitionMethod == FullScanAcquisitionMethod.DIA &&
                        _fullScan.IsolationScheme.IsAllIons)
                    {
                        if (instrumentInfo != null)
                        {
                            _isWatersMse = _isWatersFile;
                            _isAgilentMse = instrumentInfo.IsAgilentFile;
                        }
                        _mseLevel = 1;
                    }
                }

                Func<double, double> calcWindowsQ1 = _fullScan.GetPrecursorFilterWindow;
                Func<double, double> calcWindowsQ3 = _fullScan.GetProductFilterWindow;
                _minTime = _instrument.MinTime;
                _maxTime = _instrument.MaxTime;
                bool canSchedule = CanSchedule(document, retentionTimePredictor);
                // TODO: Figure out a way to turn off time sharing on first SIM scan so that
                //       times can be shared for MS1 without SIM scans
                _isSharedTime = !canSchedule;

                // If we're using bare measured drift times from spectral libraries, go get those now
                var libraryIonMobilityInfo = document.Settings.PeptideSettings.Prediction.UseLibraryDriftTimes
                    ? document.Settings.GetIonMobilities(msDataFileUri)
                    : null;

                foreach (var nodePep in document.Molecules)
                {
                    if (firstPass && !retentionTimePredictor.IsFirstPassPeptide(nodePep))
                        continue;

                    foreach (TransitionGroupDocNode nodeGroup in nodePep.Children)
                    {
                        if (nodeGroup.Children.Count == 0)
                            continue;

                        double? minTime = _minTime, maxTime = _maxTime;
                        double? startDriftTimeMsec = null, endDriftTimeMsec = null;
                        double windowDT;
                        double highEnergyDriftTimeOffsetMsec = 0;
                        DriftTimeInfo centerDriftTime = document.Settings.PeptideSettings.Prediction.GetDriftTime(
                            nodePep, nodeGroup, libraryIonMobilityInfo, out windowDT);
                        if (centerDriftTime.DriftTimeMsec(false).HasValue)
                        {
                            startDriftTimeMsec = centerDriftTime.DriftTimeMsec(false) - windowDT / 2; // Get the low energy drift time
                            endDriftTimeMsec = startDriftTimeMsec + windowDT;
                            highEnergyDriftTimeOffsetMsec = centerDriftTime.HighEnergyDriftTimeOffsetMsec;
                        }

                        if (canSchedule)
                        {
                            if (RetentionTimeFilterType.scheduling_windows == _fullScan.RetentionTimeFilterType)
                            {
                                double? centerTime = null;
                                double windowRT = 0;
                                if (retentionTimePredictor != null)
                                {
                                    centerTime = retentionTimePredictor.GetPredictedRetentionTime(nodePep);
                                }
                                else
                                {
                                    var prediction = document.Settings.PeptideSettings.Prediction;
                                    if (prediction.RetentionTime == null || !prediction.RetentionTime.IsAutoCalculated)
                                    {
                                        centerTime = document.Settings.PeptideSettings.Prediction.PredictRetentionTimeForChromImport(
                                            document, nodePep, nodeGroup, out windowRT);
                                    }
                                }
                                // Force the center time to be at least zero
                                if (centerTime.HasValue && centerTime.Value < 0)
                                    centerTime = 0;
                                if (_fullScan.RetentionTimeFilterLength != 0)
                                {
                                    windowRT = _fullScan.RetentionTimeFilterLength * 2;
                                }
                                if (centerTime != null)
                                {
                                    double startTime = centerTime.Value - windowRT / 2;
                                    double endTime = startTime + windowRT;
                                    minTime = Math.Max(minTime ?? 0, startTime);
                                    maxTime = Math.Min(maxTime ?? double.MaxValue, endTime);
                                }
                            }
                            else if (RetentionTimeFilterType.ms2_ids == _fullScan.RetentionTimeFilterType)
                            {
                                var times = document.Settings.GetBestRetentionTimes(nodePep, msDataFileUri);
                                if (times.Length > 0)
                                {
                                    minTime = Math.Max(minTime ?? 0, times.Min() - _fullScan.RetentionTimeFilterLength);
                                    maxTime = Math.Min(maxTime ?? double.MaxValue, times.Max() + _fullScan.RetentionTimeFilterLength);
                                }
                            }
                        }

                        SpectrumFilterPair filter;
                        string textId = nodePep.RawTextId; // Modified Sequence for peptides, or some other string for custom ions
                        double mz = nodeGroup.PrecursorMz;
                        var key = new PrecursorTextId(mz, textId, ChromExtractor.summed);
                        if (!dictPrecursorMzToFilter.TryGetValue(key, out filter))
                        {
                            filter = new SpectrumFilterPair(key, nodePep.Color, dictPrecursorMzToFilter.Count, minTime, maxTime,
                                startDriftTimeMsec, endDriftTimeMsec, highEnergyDriftTimeOffsetMsec,
                                _isHighAccMsFilter, _isHighAccProductFilter);
                            dictPrecursorMzToFilter.Add(key, filter);
                        }

                        if (!EnabledMs)
                        {
                            filter.AddQ3FilterValues(from TransitionDocNode nodeTran in nodeGroup.Children
                                                     select nodeTran.Mz, calcWindowsQ3);
                        }
                        else if (!EnabledMsMs)
                        {
                            filter.AddQ1FilterValues(GetMS1MzValues(nodeGroup), calcWindowsQ1);
                        }
                        else
                        {
                            filter.AddQ1FilterValues(GetMS1MzValues(nodeGroup), calcWindowsQ1);
                            filter.AddQ3FilterValues(from TransitionDocNode nodeTran in nodeGroup.Children
                                                     where !nodeTran.IsMs1
                                                     select nodeTran.Mz, calcWindowsQ3);
                        }
                    }
                }
                _filterMzValues = dictPrecursorMzToFilter.Values.ToArray();

                var listChromKeyFilterIds = new List<ChromKey>();
                foreach (var spectrumFilterPair in _filterMzValues)
                {
                    spectrumFilterPair.AddChromKeys(listChromKeyFilterIds);
                }
                _productChromKeys = listChromKeyFilterIds.ToArray();

                // Sort a copy of the filter pairs by maximum retention time so that we can detect when
                // filters are no longer active.
                _filterRTValues = new SpectrumFilterPair[_filterMzValues.Length];
                Array.Copy(_filterMzValues, _filterRTValues, _filterMzValues.Length);
                Array.Sort(_filterRTValues, CompareByRT);
            }

            InitRTLimits();
        }