Example #1
0
        public void CalibrateHitsAndComponents(RegressionForestModel bestCf)
        {
            foreach (SpectrumMatch hit in all_topdown_hits)
            {
                hit.mz = hit.mz - bestCf.Predict(new double[] { hit.mz, hit.ms1_scan.RetentionTime, Math.Log(hit.ms1_scan.TotalIonCurrent), hit.ms1_scan.InjectionTime.HasValue ? Math.Log(hit.ms1_scan.InjectionTime.Value) : double.NaN });
            }
            foreach (Component c in Sweet.lollipop.calibration_components.Where(h => h.input_file.lt_condition == raw_file.lt_condition && h.input_file.biological_replicate == raw_file.biological_replicate && h.input_file.fraction == raw_file.fraction && h.input_file.technical_replicate == raw_file.technical_replicate))
            {
                foreach (ChargeState cs in c.charge_states)
                {
                    int  scanNumber = myMsDataFile.GetClosestOneBasedSpectrumNumber(c.rt_apex);
                    var  scan       = myMsDataFile.GetOneBasedScan(scanNumber);
                    bool ms1Scan    = scan.MsnOrder == 1;
                    while (!ms1Scan)
                    {
                        scanNumber--;
                        scan    = myMsDataFile.GetOneBasedScan(scanNumber);
                        ms1Scan = scan.MsnOrder == 1;
                    }

                    cs.mz_centroid = cs.mz_centroid - bestCf.Predict(new double[] { cs.mz_centroid, scan.RetentionTime, Math.Log(scan.TotalIonCurrent), scan.InjectionTime.HasValue ? Math.Log(scan.InjectionTime.Value) : double.NaN });
                }
            }
            foreach (var a in myMsDataFile.GetAllScansList().Where(s => s.MsnOrder == 1))
            {
                Func <MzPeak, double> theFunc = x => x.Mz - bestCf.Predict(new double[] { x.Mz, a.RetentionTime, Math.Log(a.TotalIonCurrent), a.InjectionTime.HasValue ? Math.Log(a.InjectionTime.Value) : double.NaN });
                a.MassSpectrum.ReplaceXbyApplyingFunction(theFunc);
            }
        }
        private void CalibrateSpectra(IPredictorModel <double> ms1predictor, IPredictorModel <double> ms2predictor)
        {
            Parallel.ForEach(Partitioner.Create(1, MyMsDataFile.NumSpectra + 1), new ParallelOptions {
                MaxDegreeOfParallelism = commonParameters.MaxThreadsToUsePerFile
            }, (fff, loopState) =>
            {
                for (int i = fff.Item1; i < fff.Item2; i++)
                {
                    // Stop loop if canceled
                    if (GlobalVariables.StopLoops)
                    {
                        loopState.Stop();
                        return;
                    }

                    var scan = MyMsDataFile.GetOneBasedScan(i);

                    if (scan.MsnOrder == 2)
                    {
                        var precursorScan = MyMsDataFile.GetOneBasedScan(scan.OneBasedPrecursorScanNumber.Value);

                        if (!scan.SelectedIonMonoisotopicGuessIntensity.HasValue && scan.SelectedIonMonoisotopicGuessMz.HasValue)
                        {
                            scan.ComputeMonoisotopicPeakIntensity(precursorScan.MassSpectrum);
                        }

                        double theFunc(MzPeak x) => x.Mz - ms2predictor.Predict(new double[] { x.Mz, scan.RetentionTime, Math.Log(scan.TotalIonCurrent), scan.InjectionTime.HasValue ? Math.Log(scan.InjectionTime.Value) : double.NaN, Math.Log(x.Intensity) });

                        double theFuncForPrecursor(MzPeak x) => x.Mz - ms1predictor.Predict(new double[] { x.Mz, precursorScan.RetentionTime, Math.Log(precursorScan.TotalIonCurrent), precursorScan.InjectionTime.HasValue ? Math.Log(precursorScan.InjectionTime.Value) : double.NaN, Math.Log(x.Intensity) });

                        scan.TransformMzs(theFunc, theFuncForPrecursor);
                    }
                    else
                    {
                        Func <MzPeak, double> theFunc = x => x.Mz - ms1predictor.Predict(new double[] { x.Mz, scan.RetentionTime, Math.Log(scan.TotalIonCurrent), scan.InjectionTime.HasValue ? Math.Log(scan.InjectionTime.Value) : double.NaN, Math.Log(x.Intensity) });
                        scan.MassSpectrum.ReplaceXbyApplyingFunction(theFunc);
                    }
                }
            }
                             );
        }
Example #3
0
        private void DrawPsm(int oneBasedScanNumber, string fullSequence = null)
        {
            MsDataScan msDataScanToDraw        = MsDataFile.GetOneBasedScan(oneBasedScanNumber);
            IEnumerable <MetaDrawPsm> scanPsms = peptideSpectralMatches.Where(p => p.ScanNum == oneBasedScanNumber);

            if (fullSequence != null)
            {
                scanPsms = scanPsms.Where(p => p.FullSequence == fullSequence);
            }

            MetaDrawPsm psmToDraw = scanPsms.FirstOrDefault();

            // draw annotated spectrum
            mainViewModel.DrawPeptideSpectralMatch(msDataScanToDraw, psmToDraw);

            // draw annotated base sequence
            DrawAnnotatedBaseSequence(psmToDraw);
        }
        protected override MetaMorpheusEngineResults RunSpecific()
        {
            // don't try to localize mass differences for ambiguous peptides
            foreach (PeptideSpectralMatch psm in AllResultingIdentifications.Where(b => b.FullSequence != null))
            {
                // Stop loop if canceled
                if (GlobalVariables.StopLoops)
                {
                    break;
                }

                MsDataScan scan = MyMsDataFile.GetOneBasedScan(psm.ScanNumber);
                Ms2ScanWithSpecificMass     scanWithSpecificMass = new Ms2ScanWithSpecificMass(scan, psm.ScanPrecursorMonoisotopicPeakMz, psm.ScanPrecursorCharge, psm.FullFilePath, CommonParameters);
                PeptideWithSetModifications peptide = psm.BestMatchingPeptides.First().Peptide;
                double massDifference = psm.ScanPrecursorMass - peptide.MonoisotopicMass;

                // this section will iterate through all residues of the peptide and try to localize the mass-diff at each residue and report a score for each residue
                var localizedScores = new List <double>();
                for (int r = 0; r < peptide.Length; r++)
                {
                    // create new PeptideWithSetMods with unidentified mass difference at the given residue
                    PeptideWithSetModifications peptideWithLocalizedMassDiff = peptide.Localize(r, massDifference);

                    // this is the list of theoretical products for this peptide with mass-difference on this residue
                    List <Product> productsWithLocalizedMassDiff = peptideWithLocalizedMassDiff.Fragment(CommonParameters.DissociationType, CommonParameters.DigestionParams.FragmentationTerminus).ToList();

                    var matchedIons = MatchFragmentIons(scanWithSpecificMass, productsWithLocalizedMassDiff, CommonParameters);

                    // score when the mass-diff is on this residue
                    double localizedScore = CalculatePeptideScore(scan, matchedIons);

                    localizedScores.Add(localizedScore);
                }

                psm.LocalizedScores = localizedScores;
            }

            return(new LocalizationEngineResults(this));
        }
Example #5
0
        protected override MetaMorpheusEngineResults RunSpecific()
        {
            TerminusType terminusType = ProductTypeMethods.IdentifyTerminusType(ProductTypes);

            foreach (PeptideSpectralMatch psm in AllResultingIdentifications)
            {
                // Stop loop if canceled
                if (GlobalVariables.StopLoops)
                {
                    break;
                }

                psm.MatchedIonSeriesDict            = new Dictionary <ProductType, int[]>();
                psm.MatchedIonMassToChargeRatioDict = new Dictionary <ProductType, double[]>();
                psm.ProductMassErrorDa        = new Dictionary <ProductType, double[]>();
                psm.ProductMassErrorPpm       = new Dictionary <ProductType, double[]>();
                psm.MatchedIonIntensitiesDict = new Dictionary <ProductType, double[]>();
                var    theScan          = MyMsDataFile.GetOneBasedScan(psm.ScanNumber);
                double thePrecursorMass = psm.ScanPrecursorMass;
                foreach (ProductType productType in ProductTypes)
                {
                    var sortedTheoreticalProductMasses = psm.CompactPeptides.First().Key.ProductMassesMightHaveDuplicatesAndNaNs(new List <ProductType> {
                        productType
                    });
                    Array.Sort(sortedTheoreticalProductMasses);
                    List <int>    matchedIonSeriesList            = new List <int>();
                    List <double> matchedIonMassToChargeRatioList = new List <double>();
                    List <double> productMassErrorDaList          = new List <double>();
                    List <double> productMassErrorPpmList         = new List <double>();
                    List <double> matchedIonIntensityList         = new List <double>();

                    //populate the above lists
                    MatchIonsOld(theScan, commonParameters.ProductMassTolerance, sortedTheoreticalProductMasses, matchedIonSeriesList, matchedIonMassToChargeRatioList, productMassErrorDaList, productMassErrorPpmList, matchedIonIntensityList, thePrecursorMass, productType, commonParameters.AddCompIons);

                    psm.MatchedIonSeriesDict.Add(productType, matchedIonSeriesList.ToArray());
                    psm.MatchedIonMassToChargeRatioDict.Add(productType, matchedIonMassToChargeRatioList.ToArray());
                    psm.ProductMassErrorDa.Add(productType, productMassErrorDaList.ToArray());
                    psm.ProductMassErrorPpm.Add(productType, productMassErrorPpmList.ToArray());
                    psm.MatchedIonIntensitiesDict.Add(productType, matchedIonIntensityList.ToArray());
                }
            }

            foreach (PeptideSpectralMatch psm in AllResultingIdentifications.Where(b => b.NumDifferentCompactPeptides == 1))
            {
                // Stop loop if canceled
                if (GlobalVariables.StopLoops)
                {
                    break;
                }

                var    theScan          = MyMsDataFile.GetOneBasedScan(psm.ScanNumber);
                double thePrecursorMass = psm.ScanPrecursorMass;

                if (psm.FullSequence == null)
                {
                    continue;
                }

                PeptideWithSetModifications representative = psm.CompactPeptides.First().Value.Item2.First();

                var localizedScores = new List <double>();
                for (int indexToLocalize = 0; indexToLocalize < representative.Length; indexToLocalize++)
                {
                    PeptideWithSetModifications localizedPeptide = representative.Localize(indexToLocalize, psm.ScanPrecursorMass - representative.MonoisotopicMass);

                    var gg = localizedPeptide.CompactPeptide(terminusType).ProductMassesMightHaveDuplicatesAndNaNs(ProductTypes);
                    Array.Sort(gg);
                    var score = CalculatePeptideScoreOld(theScan, commonParameters.ProductMassTolerance, gg, thePrecursorMass, DissociationTypes, commonParameters.AddCompIons, 0);
                    localizedScores.Add(score);
                }

                psm.LocalizedScores = localizedScores;
            }
            return(new LocalizationEngineResults(this));
        }
Example #6
0
        private void DrawPsm(int oneBasedScanNumber, string fullSequence = null, string fileName = null)
        {
            MsDataScan msDataScanToDraw       = MsDataFile.GetOneBasedScan(oneBasedScanNumber);
            IEnumerable <PsmFromTsv> scanPsms = filteredListOfPsms.Where(p => p.Ms2ScanNumber == oneBasedScanNumber);

            if (fullSequence != null)
            {
                scanPsms = scanPsms.Where(p => p.FullSequence == fullSequence);
            }

            PsmFromTsv psmToDraw = scanPsms.FirstOrDefault();

            // if this spectrum has child scans, draw them in the "advanced" tab
            if ((psmToDraw.ChildScanMatchedIons != null && psmToDraw.ChildScanMatchedIons.Count > 0) ||
                (psmToDraw.BetaPeptideChildScanMatchedIons != null && psmToDraw.BetaPeptideChildScanMatchedIons.Count > 0))
            {
                ParentChildScanView.Visibility = Visibility.Visible;
                ParentScanView.Visibility      = Visibility.Visible;

                // draw parent scans
                var        parentPsmModel = new PsmAnnotationViewModel();
                MsDataScan parentScan     = MsDataFile.GetOneBasedScan(psmToDraw.Ms2ScanNumber);

                parentPsmModel.DrawPeptideSpectralMatch(parentScan, psmToDraw, metaDrawGraphicalSettings.ShowMzValues,
                                                        metaDrawGraphicalSettings.ShowAnnotationCharges, metaDrawGraphicalSettings.AnnotatedFontSize, metaDrawGraphicalSettings.BoldText);

                string parentAnnotation = "Scan: " + parentScan.OneBasedScanNumber.ToString()
                                          + " Dissociation Type: " + parentScan.DissociationType.ToString()
                                          + " MsOrder: " + parentScan.MsnOrder.ToString()
                                          + " Selected Mz: " + parentScan.SelectedIonMZ.Value.ToString("0.##")
                                          + " Retention Time: " + parentScan.RetentionTime.ToString("0.##");

                itemsControlSampleViewModel.AddNewRow(parentPsmModel, parentAnnotation, null);

                // draw child scans
                HashSet <int> scansDrawn = new HashSet <int>();
                var           allChildScanMatchedIons = psmToDraw.ChildScanMatchedIons;
                if (psmToDraw.BetaPeptideChildScanMatchedIons != null)
                {
                    allChildScanMatchedIons = allChildScanMatchedIons.Concat(psmToDraw.BetaPeptideChildScanMatchedIons).ToDictionary(p => p.Key, q => q.Value);
                }
                foreach (var childScanMatchedIons in allChildScanMatchedIons)
                {
                    int scanNumber = childScanMatchedIons.Key;

                    if (scansDrawn.Contains(scanNumber))
                    {
                        continue;
                    }
                    scansDrawn.Add(scanNumber);

                    List <MatchedFragmentIon> matchedIons = childScanMatchedIons.Value;

                    var        childPsmModel = new PsmAnnotationViewModel();
                    MsDataScan childScan     = MsDataFile.GetOneBasedScan(scanNumber);

                    childPsmModel.DrawPeptideSpectralMatch(childScan, psmToDraw, metaDrawGraphicalSettings.ShowMzValues,
                                                           metaDrawGraphicalSettings.ShowAnnotationCharges, metaDrawGraphicalSettings.AnnotatedFontSize, metaDrawGraphicalSettings.BoldText);

                    string childAnnotation = "Scan: " + scanNumber.ToString()
                                             + " Dissociation Type: " + childScan.DissociationType.ToString()
                                             + " MsOrder: " + childScan.MsnOrder.ToString()
                                             + " Selected Mz: " + childScan.SelectedIonMZ.Value.ToString("0.##")
                                             + " RetentionTime: " + childScan.RetentionTime.ToString("0.##");

                    Canvas aCanvas = new Canvas {
                        Height = 60
                    };
                    DrawAnnotatedBaseSequence(aCanvas, psmToDraw, true);

                    itemsControlSampleViewModel.AddNewRow(childPsmModel, childAnnotation, aCanvas);
                }
            }
            else
            {
                ParentChildScanView.Visibility = Visibility.Collapsed;
                ParentScanView.Visibility      = Visibility.Collapsed;
            }

            // if this is a crosslink spectrum match, there are two base sequence annotations to draw
            // this makes the canvas taller to fit both of these peptide sequences
            if (psmToDraw.BetaPeptideBaseSequence != null)
            {
                int height = 150;

                canvas.Height = height;
                PsmAnnotationGrid.RowDefinitions[1].Height = new GridLength(height);
            }
            else
            {
                int height = 60;

                canvas.Height = height;
                PsmAnnotationGrid.RowDefinitions[1].Height = new GridLength(height);
            }

            // draw annotated spectrum
            mainViewModel.DrawPeptideSpectralMatch(msDataScanToDraw, psmToDraw, metaDrawGraphicalSettings.ShowMzValues,
                                                   metaDrawGraphicalSettings.ShowAnnotationCharges, metaDrawGraphicalSettings.AnnotatedFontSize, metaDrawGraphicalSettings.BoldText);

            // draw annotated base sequence
            DrawAnnotatedBaseSequence(canvas, psmToDraw);
        }
Example #7
0
        protected override MetaMorpheusEngineResults RunSpecific()
        {
            Status("Extracting data points:");
            // The final training point list

            int numMs1MassChargeCombinationsConsidered = 0;
            int numMs1MassChargeCombinationsThatAreIgnoredBecauseOfTooManyPeaks = 0;
            int numMs2MassChargeCombinationsConsidered = 0;
            int numMs2MassChargeCombinationsThatAreIgnoredBecauseOfTooManyPeaks = 0;
            List <LabeledDataPoint> Ms1List = new List <LabeledDataPoint>();
            List <LabeledDataPoint> Ms2List = new List <LabeledDataPoint>();

            object lockObj  = new object();
            object lockObj2 = new object();

            int maxThreadsPerFile = CommonParameters.MaxThreadsToUsePerFile;

            int[] threads = Enumerable.Range(0, maxThreadsPerFile).ToArray();
            Parallel.ForEach(threads, (matchIndex) =>
            {
                for (; matchIndex < GoodIdentifications.Count; matchIndex += maxThreadsPerFile)
                {
                    // Stop loop if canceled
                    if (GlobalVariables.StopLoops)
                    {
                        return;
                    }

                    PeptideSpectralMatch identification = GoodIdentifications[matchIndex];

                    // Each identification has an MS2 spectrum attached to it.
                    int ms2scanNumber = identification.ScanNumber;
                    int peptideCharge = identification.ScanPrecursorCharge;
                    if (identification.FullSequence == null || identification.BestMatchingPeptides.Any(p => p.Peptide.AllModsOneIsNterminus.Any(m => m.Value.ChemicalFormula == null)))
                    {
                        continue;
                    }

                    var representativeSinglePeptide = identification.BestMatchingPeptides.First().Peptide;

                    // Get the peptide, don't forget to add the modifications!!!!
                    var SequenceWithChemicalFormulas = representativeSinglePeptide.SequenceWithChemicalFormulas;
                    if (SequenceWithChemicalFormulas == null || representativeSinglePeptide.AllModsOneIsNterminus.Any(b => b.Value.NeutralLosses != null))
                    {
                        continue;
                    }

                    Peptide coolPeptide = new Peptide(SequenceWithChemicalFormulas);

                    var ms2tuple = SearchMS2Spectrum(MyMsDataFile.GetOneBasedScan(ms2scanNumber), identification);

                    lock (lockObj2)
                    {
                        Ms2List.AddRange(ms2tuple);
                    }

                    // Calculate isotopic distribution of the full peptide
                    var dist = IsotopicDistribution.GetDistribution(coolPeptide.GetChemicalFormula(), FineResolutionForIsotopeDistCalculation, 0.001);

                    double[] theoreticalMasses      = dist.Masses.ToArray();
                    double[] theoreticalIntensities = dist.Intensities.ToArray();

                    Array.Sort(theoreticalIntensities, theoreticalMasses, Comparer <double> .Create((x, y) => y.CompareTo(x)));

                    var ms1tupleBack = SearchMS1Spectra(theoreticalMasses, theoreticalIntensities, ms2scanNumber, -1, peptideCharge, identification);

                    var ms1tupleForward = SearchMS1Spectra(theoreticalMasses, theoreticalIntensities, ms2scanNumber, 1, peptideCharge, identification);

                    lock (lockObj)
                    {
                        Ms1List.AddRange(ms1tupleBack.Item1);
                        numMs1MassChargeCombinationsConsidered += ms1tupleBack.Item2;
                        numMs1MassChargeCombinationsThatAreIgnoredBecauseOfTooManyPeaks += ms1tupleBack.Item3;
                        Ms1List.AddRange(ms1tupleForward.Item1);
                        numMs1MassChargeCombinationsConsidered += ms1tupleForward.Item2;
                        numMs1MassChargeCombinationsThatAreIgnoredBecauseOfTooManyPeaks += ms1tupleForward.Item3;
                    }
                }
            });

            // datapoints are ordered because they were acquired in a parallized search and we want repeatable results
            return(new DataPointAquisitionResults(this,
                                                  GoodIdentifications,
                                                  Ms1List.OrderBy(p => p.ScanNumber).ThenBy(p => p.ExperimentalMz).ToList(),
                                                  Ms2List.OrderBy(p => p.ScanNumber).ThenBy(p => p.ExperimentalMz).ToList(),
                                                  numMs1MassChargeCombinationsConsidered,
                                                  numMs1MassChargeCombinationsThatAreIgnoredBecauseOfTooManyPeaks,
                                                  numMs2MassChargeCombinationsConsidered,
                                                  numMs2MassChargeCombinationsThatAreIgnoredBecauseOfTooManyPeaks
                                                  ));
        }
Example #8
0
        protected override MetaMorpheusEngineResults RunSpecific()
        {
            // don't try to localize mass differences for ambiguous peptides
            PeptideSpectralMatch[] unambiguousPsms = AllResultingIdentifications.Where(b => b.FullSequence != null).ToArray();

            double psmsSearched       = 0;
            int    oldPercentProgress = 0;

            ReportProgress(new ProgressEventArgs(oldPercentProgress, "Localizing mass-differences... ", NestedIds));

            Parallel.ForEach(Partitioner.Create(0, unambiguousPsms.Length),
                             new ParallelOptions {
                MaxDegreeOfParallelism = CommonParameters.MaxThreadsToUsePerFile
            },
                             (range, loopState) =>
            {
                for (int i = range.Item1; i < range.Item2; i++)
                {
                    PeptideSpectralMatch psm = unambiguousPsms[i];

                    // Stop loop if canceled
                    if (GlobalVariables.StopLoops)
                    {
                        break;
                    }

                    MsDataScan scan = MyMsDataFile.GetOneBasedScan(psm.ScanNumber);
                    Ms2ScanWithSpecificMass scanWithSpecificMass = new Ms2ScanWithSpecificMass(scan, psm.ScanPrecursorMonoisotopicPeakMz, psm.ScanPrecursorCharge, psm.FullFilePath, CommonParameters);
                    PeptideWithSetModifications peptide          = psm.BestMatchingPeptides.First().Peptide;
                    double massDifference = psm.ScanPrecursorMass - peptide.MonoisotopicMass;

                    // this section will iterate through all residues of the peptide and try to localize the mass-diff at each residue and report a score for each residue
                    var localizedScores = new List <double>();
                    for (int r = 0; r < peptide.Length; r++)
                    {
                        // create new PeptideWithSetMods with unidentified mass difference at the given residue
                        PeptideWithSetModifications peptideWithLocalizedMassDiff = peptide.Localize(r, massDifference);

                        // this is the list of theoretical products for this peptide with mass-difference on this residue
                        List <Product> productsWithLocalizedMassDiff = peptideWithLocalizedMassDiff.Fragment(CommonParameters.DissociationType, CommonParameters.DigestionParams.FragmentationTerminus).ToList();

                        var matchedIons = MatchFragmentIons(scanWithSpecificMass, productsWithLocalizedMassDiff, CommonParameters);

                        // score when the mass-diff is on this residue
                        double localizedScore = CalculatePeptideScore(scan, matchedIons);

                        localizedScores.Add(localizedScore);
                    }

                    psm.LocalizedScores = localizedScores;

                    // report search progress (PSM mass differences localized so far out of total)
                    psmsSearched++;
                    var percentProgress = (int)((psmsSearched / unambiguousPsms.Length) * 100);

                    if (percentProgress > oldPercentProgress)
                    {
                        oldPercentProgress = percentProgress;
                        ReportProgress(new ProgressEventArgs(percentProgress, "Localizing mass-differences... ", NestedIds));
                    }
                }
            });

            return(new LocalizationEngineResults(this));
        }