Exemplo n.º 1
0
        public static MassCalibrationResults RunMassCalibration(IEnumerable <Lipid> lipidList, LcMsRun lcmsRun, double hcdMassError, IProgress <int> progress = null)
        {
            var ppmErrorList = new List <double>();

            var lipidsGroupedByTarget = lipidList.OrderBy(x => x.LipidTarget.Composition.Mass).GroupBy(x => x.LipidTarget).ToList();

            foreach (var kvp in lipidsGroupedByTarget)
            {
                var target = kvp.Key;

                var spectrumSearchResultList = InformedWorkflow.RunInformedWorkflow(target, lcmsRun, hcdMassError, 500, scoreModel: null);

                if (spectrumSearchResultList.Any())
                {
                    var targetIon = new Ion(target.Composition - Composition.Hydrogen, 1);
                    var targetMz  = targetIon.GetMonoIsotopicMz();

                    var bestSpectrumSearchResult = spectrumSearchResultList.OrderBy(x => x.Score).First();

                    var massSpectrum = bestSpectrumSearchResult.PrecursorSpectrum.Peaks;
                    var closestPeak  = massSpectrum.OrderBy(x => Math.Abs(x.Mz - targetMz)).First();

                    var ppmError = LipidUtil.PpmError(targetMz, closestPeak.Mz);
                    ppmErrorList.Add(ppmError);
                }
            }

            var ppmHistogram = QcUtil.CalculateHistogram(ppmErrorList, hcdMassError, 0.25);

            var massCalibrationResults = new MassCalibrationResults(ppmHistogram);

            return(massCalibrationResults);
        }
Exemplo n.º 2
0
        private async void ProcessAllTargetsButtonClick(object sender, RoutedEventArgs e)
        {
            var fragmentationMode = (FragmentationMode)FragmentationModeComboBox.SelectedItem;
            var precursorError    = double.Parse(PrecursorErrorTextBox.Text);
            var hcdMassError      = double.Parse(HcdErrorTextBox.Text);
            var cidMassError      = double.Parse(CidErrorTextBox.Text);
            var resultsPerScan    = int.Parse(ResultsPerScanTextBox.Text);

            LipidGroupSearchResultsDataGrid.Visibility = Visibility.Hidden;
            ExportGlobalResultsButton.Visibility       = Visibility.Hidden;
            ExportAllGlobalResultsButton.Visibility    = Visibility.Hidden;
            await Task.Run(() => SingleTargetViewModel.OnProcessAllTarget(hcdMassError, cidMassError, fragmentationMode, resultsPerScan));

            LipidGroupSearchResultsDataGrid.Visibility = Visibility.Visible;
            ExportGlobalResultsButton.Visibility       = Visibility.Visible;
            ExportAllGlobalResultsButton.Visibility    = Visibility.Visible;

            // Select the best spectrum search result
            if (SingleTargetViewModel.LipidGroupSearchResultList.Count > 0)
            {
                double currentMz = 0;
                List <LipidGroupSearchResult> removeList = new List <LipidGroupSearchResult>();
                foreach (LipidGroupSearchResult entry in SingleTargetViewModel.LipidGroupSearchResultList)
                {
                    var targetMz = entry.LipidTarget.MzRounded;
                    if (entry.SpectrumSearchResult.PrecursorSpectrum != null)
                    {
                        var massSpectrum = entry.SpectrumSearchResult.PrecursorSpectrum.Peaks;
                        var closestPeak  = massSpectrum.OrderBy(x => Math.Abs(x.Mz - targetMz)).First();
                        currentMz = closestPeak.Mz;
                    }
                    else
                    {
                        var isolationMz = entry.SpectrumSearchResult.HcdSpectrum?.IsolationWindow.IsolationWindowTargetMz ??
                                          entry.SpectrumSearchResult.CidSpectrum.IsolationWindow.IsolationWindowTargetMz;

                        currentMz = isolationMz;
                    }
                    double currentPpmError = LipidUtil.PpmError(targetMz, currentMz);
                    if (Math.Abs(currentPpmError) > Math.Abs(precursorError))
                    {
                        removeList.Add(entry);
                    }
                }
                foreach (LipidGroupSearchResult remove in removeList)
                {
                    SingleTargetViewModel.LipidGroupSearchResultList.Remove(remove);
                }

                var dataGrid = LipidGroupSearchResultsDataGrid;

                dataGrid.SelectedItem = SingleTargetViewModel.LipidGroupSearchResultList[0];
                dataGrid.ScrollIntoView(SingleTargetViewModel.LipidGroupSearchResultList[0]);
            }
        }
Exemplo n.º 3
0
        private static void OutputResultsToTsv(
            IReadOnlyCollection <LipidGroupSearchResult> lipidGroupSearchResults,
            string fileLocation,
            string rawFileName,
            IProgress <int> progress = null,
            bool writeHeader         = true)
        {
            using (TextWriter textWriter = new StreamWriter(fileLocation, true))
            {
                if (writeHeader)
                {
                    textWriter.WriteLine("Raw Data File\tLM_ID\tCommon Name\tAdduct\tCategory\tMain Class\tSub Class\tExact m/z\tFormula\tObserved m/z\tppm Error\tApex RT\tPrecursor RT\tApex NET\tIntensity\tPeak Area\tScore\tMS/MS Scan\tPrecursor Scan\tApex Scan\tPUBCHEM_SID\tPUBCHEM_CID\tINCHI_KEY\tKEGG_ID\tHMDBID\tCHEBI_ID\tLIPIDAT_ID\tLIPIDBANK_ID");
                }
                var progressCounter = 0;

                foreach (var lipidGroupSearchResult in lipidGroupSearchResults)
                {
                    var lipidTarget          = lipidGroupSearchResult.LipidTarget;
                    var spectrumSearchResult = lipidGroupSearchResult.SpectrumSearchResult;
                    var Precursor            = spectrumSearchResult.PrecursorSpectrum != null;

                    var targetMz   = lipidTarget.MzRounded;
                    var observedMz = spectrumSearchResult.HcdSpectrum?.IsolationWindow.IsolationWindowTargetMz ??
                                     spectrumSearchResult.CidSpectrum.IsolationWindow.IsolationWindowTargetMz;

                    if (Precursor)
                    {
                        var massSpectrum = spectrumSearchResult.PrecursorSpectrum.Peaks;
                        var closestPeak  = massSpectrum.OrderBy(x => Math.Abs(x.Mz - targetMz)).First();
                        observedMz = closestPeak.Mz;
                    }

                    var score    = lipidGroupSearchResult.Score;
                    var msmsScan = spectrumSearchResult.HcdSpectrum?.ScanNum ?? spectrumSearchResult.CidSpectrum.ScanNum;
                    var ppmError = LipidUtil.PpmError(targetMz, observedMz);

                    foreach (var lipid in lipidGroupSearchResult.LipidList)
                    {
                        var line = new StringBuilder();
                        line.Append(rawFileName + "\t");
                        line.Append(lipid.LipidMapsId + "\t");
                        line.Append(lipidTarget.StrippedDisplay + "\t");
                        line.Append(lipid.AdductFull + "\t");
                        line.Append(lipid.Category + "\t");
                        line.Append(lipid.MainClass + "\t");
                        line.Append(lipid.SubClass + "\t");
                        line.Append(lipidTarget.MzRounded + "\t");
                        line.Append(lipidTarget.EmpiricalFormula + "\t");
                        line.Append(observedMz + "\t");
                        line.Append(ppmError + "\t");
                        line.Append(spectrumSearchResult.RetentionTime + "\t");
                        if (Precursor)
                        {
                            line.Append(spectrumSearchResult.PrecursorSpectrum.ElutionTime + "\t");
                        }
                        else
                        {
                            line.Append("\t");
                        }
                        line.Append(spectrumSearchResult.NormalizedElutionTime + "\t");
                        line.Append(spectrumSearchResult.ApexIntensity + "\t");
                        line.Append(spectrumSearchResult.PeakArea + "\t");
                        line.Append(score + "\t");
                        line.Append(msmsScan + "\t");
                        if (Precursor)
                        {
                            line.Append(spectrumSearchResult.PrecursorSpectrum.ScanNum + "\t");
                        }
                        else
                        {
                            line.Append("\t");
                        }
                        line.Append(spectrumSearchResult.ApexScanNum + "\t");
                        line.Append(lipid.PubChemSid + "\t");
                        line.Append(lipid.PubChemCid + "\t");
                        line.Append(lipid.InchiKey + "\t");
                        line.Append(lipid.KeggId + "\t");
                        line.Append(lipid.HmdbId + "\t");
                        line.Append(lipid.ChebiId + "\t");
                        line.Append(lipid.LipidatId + "\t");
                        line.Append(lipid.LipidBankId + "\t");

                        textWriter.WriteLine(line.ToString());
                    }
                    if (progress != null)
                    {
                        progressCounter++;
                        var currentProgress = (int)(progressCounter / (double)lipidGroupSearchResults.Count * 100);
                        progress.Report(currentProgress);
                    }
                }
            }
        }
Exemplo n.º 4
0
        private static void OutputResultsToMzTab(IReadOnlyCollection <LipidGroupSearchResult> lipidGroupSearchResults,
                                                 string fileLocation, string rawFileName, IProgress <int> progress = null)
        {
            using (TextWriter textWriter = new StreamWriter(fileLocation))
            {
                var progressCounter = 0;
                var mods            = new List <string>();
                foreach (var lipidGroupSearchResult in lipidGroupSearchResults)
                {
                    foreach (var lipid in lipidGroupSearchResult.LipidList)
                    {
                        if (!mods.Contains(lipid.AdductFull))
                        {
                            mods.Add(lipid.AdductFull);
                        }
                    }
                }

                //Write meta-data
                textWriter.WriteLine("MTD\tmzTabVersion\t1.0 rc5");
                textWriter.WriteLine("MTD\tmzTab-mode\tComplete");
                textWriter.WriteLine("MTD\tmzTab-type\tQuantification");
                textWriter.WriteLine("MTD\tsoftware[1]\t[, , LIQUID, ]");
                textWriter.WriteLine("MTD\tsmallmolecule_search_engine_score[1]\t[, , LIQUID_Score_Analyzer, ]");
                textWriter.WriteLine("MTD\tfixed_mod[1]\t[MS, MS:1002038, unlabeled sample, ]");
                foreach (var variableMod in mods)
                {
                    textWriter.WriteLine("MTD\tvariable_mod[1]\t[, , {0}]", variableMod);
                }
                textWriter.WriteLine("MTD\tquantification_method\t[, , LIQUID_Analysis, ]");
                textWriter.WriteLine("MTD\tsmall_molecule-quantification_unit\t[PRIDE, PRIDE:0000330, Arbitrary quantification unit, ]");
                //Get the raw/mzml location

                textWriter.WriteLine("MTD\tms_run[1]-location\t{0}", rawFileName); //TODO:
                textWriter.WriteLine("MTD\tassay[1]-quantification_reagent\t[MS, MS:1002038, unlabeled sample, ]");
                textWriter.WriteLine("MTD\tassay[1]-ms_run_ref\tms_run[1]");
                textWriter.WriteLine("MTD\tstudy_variable[1]-assay_refs\tassay[1]");
                textWriter.WriteLine("MTD\tstudy_variable[1]-description\tLIQUID Quantification");
                textWriter.WriteLine("MTD\tcolunit-small_molecule\tretention_time=[UO, UO:0000031, minute, ]");
                textWriter.WriteLine("");

                //Write small molecule section headers
                textWriter.WriteLine("SMH\tidentifier\tchemical_formula\tsmiles\tinchi_key\tdescription\texp_mass_to_charge\tcalc_mass_to_charge\tcharge\tretention_time\ttaxid\tspecies\tdatabase\tdatabase_version\tspectra_ref\tsearch_engine\tbest_search_engine_score[1]\tsearch_engine_score[1]_ms_run[1]\tmodification\tsmallmolecule_abundance_assay[1]\tsmallmolecule_abundance_study_variable[1]\tsmallmolecule_abundance_stdev_study_variable[1]\tsmallmolecule_abundance_std_error_study_variable[1]");

                //Write small molecule section datas
                foreach (var lipidGroupSearchResult in lipidGroupSearchResults)
                {
                    var lipidTarget          = lipidGroupSearchResult.LipidTarget;
                    var spectrumSearchResult = lipidGroupSearchResult.SpectrumSearchResult;
                    var targetMz             = lipidTarget.MzRounded;
                    var massSpectrum         = spectrumSearchResult.PrecursorSpectrum.Peaks;
                    var closestPeak          = massSpectrum.OrderBy(x => Math.Abs(x.Mz - targetMz)).First();
                    var observedMz           = closestPeak.Mz;
                    var ppmError             = LipidUtil.PpmError(targetMz, closestPeak.Mz);
                    var score    = lipidGroupSearchResult.Score;
                    var msmsScan = spectrumSearchResult.HcdSpectrum?.ScanNum ?? spectrumSearchResult.CidSpectrum.ScanNum;

                    //TODO: var charge = calculatedFromMZ

                    foreach (var lipid in lipidGroupSearchResult.LipidList)
                    {
                        var line = new StringBuilder();
                        line.Append("SML\t");

                        //var indexToStartRemove = lipid.LipidTarget.StrippedDisplay.IndexOf("/");
                        //var id = lipid.LipidTarget.StrippedDisplay.Substring(0, indexToStartRemove);
                        //id = id.Replace("(", "");
                        var id = lipid.LipidTarget.StrippedDisplay;
                        line.Append(id + "\t");
                        //if (!string.IsNullOrWhiteSpace(lipid.LipidMapsId))
                        //    line.Append(lipid.LipidMapsId + "\t"); // identifier
                        //else
                        //    line.Append("null" + "\t");
                        line.Append(lipidTarget.EmpiricalFormula + "\t");   // chemical_formula
                        line.Append("null" + "\t");                         // smiles
                        if (!string.IsNullOrWhiteSpace(lipid.InchiKey))
                        {
                            line.Append(lipid.InchiKey + "\t"); // inchi_key
                        }
                        else
                        {
                            line.Append("null" + "\t");
                        }
                        line.Append(lipid.SubClass + " : " + lipidTarget.FragmentationMode.ToString() + " charge" + "\t"); // description
                        line.Append(observedMz + "\t");                                                                    // exp_mass_to_charge
                        line.Append(lipidTarget.MzRounded + "\t");                                                         // calc_mass_to_charge
                        line.Append("1" + "\t");                                                                           // charge
                        line.Append(spectrumSearchResult.RetentionTime + "\t");                                            // retention_time
                        line.Append("null" + "\t");                                                                        // taxid
                        line.Append("null" + "\t");                                                                        // species
                        line.Append("null" + "\t");                                                                        // database
                        line.Append("null" + "\t");                                                                        // database_version
                        //line.Append(spectrumSearchResult.PrecursorSpectrum.ScanNum + "\t"); // spectra_ref
                        line.Append(msmsScan + "\t");                                                                      // spectra_ref
                        line.Append("[, , Liquid, ]" + "\t");                                                              // search_engine
                        line.Append(score + "\t");                                                                         // best_search_engine_score[1]
                        line.Append(score + "\t");                                                                         // search_engine_score[1]_ms_run[1]
                        line.Append(lipid.AdductFull + "\t");                                                              // FROM ADDUCTFULL
                        line.Append(spectrumSearchResult.ApexIntensity + "\t");                                            // small_molecule_abundance_assay[1]
                        line.Append("null" + "\t");                                                                        // ^^Study_variable[1]
                        line.Append("null" + "\t");                                                                        // stdev_study_variable[1]
                        line.Append("null" + "\t");                                                                        // std_err_study_variable[1]

                        textWriter.WriteLine(line.ToString());
                    }
                    if (progress != null)
                    {
                        progressCounter++;
                        var currentProgress = (int)(progressCounter / (double)lipidGroupSearchResults.Count * 100);
                        progress.Report(currentProgress);
                    }
                }
            }
        }
Exemplo n.º 5
0
        public static List <SpectrumSearchResult> RunInformedWorkflow(
            LipidTarget target,
            LcMsRun lcmsRun,
            double hcdMassError,
            double cidMassError,
            ScoreModel scoreModel = null)
        {
            IEnumerable <MsMsSearchUnit> msMsSearchUnits = target.GetMsMsSearchUnits();

            // I have to subtract an H for the target Ion since InformedProteomics will assume protenated
            var targetIon    = new Ion(target.Composition - Composition.Hydrogen, 1);
            var targetMz     = target.MzRounded;
            var hcdTolerance = new Tolerance(hcdMassError, ToleranceUnit.Ppm);
            var cidTolerance = new Tolerance(cidMassError, ToleranceUnit.Ppm);
            var activationMethodCombination = GlobalWorkflow.FigureOutActivationMethodCombination(lcmsRun);

            // Find out which MS/MS scans have a precursor m/z that matches the target
            //List<int> matchingMsMsScanNumbers = lcmsRun.GetFragmentationSpectraScanNums(targetIon).ToList();
            var matchingMsMsScanNumbers = lcmsRun.GetFragmentationSpectraScanNums(targetMz).ToList();

            var spectrumSearchResultList = new List <SpectrumSearchResult>();

            for (var i = 0; i + 1 < matchingMsMsScanNumbers.Count; i += 2)
            {
                var firstScanNumber  = matchingMsMsScanNumbers[i];
                var secondScanNumber = matchingMsMsScanNumbers[i + 1];

                // Scan numbers should be consecutive.

                /*
                 * if (secondScanNumber - firstScanNumber != 1)
                 * {
                 *  i--;
                 *  continue;
                 * }
                 * */
                ProductSpectrum firstMsMsSpectrum  = null;
                ProductSpectrum secondMsMsSpectrum = null;

                if (activationMethodCombination == ActivationMethodCombination.CidThenHcd ||
                    activationMethodCombination == ActivationMethodCombination.HcdThenCid)
                {
                    // Lookup the MS/MS Spectrum
                    firstMsMsSpectrum = lcmsRun.GetSpectrum(firstScanNumber) as ProductSpectrum;
                    if (firstMsMsSpectrum == null)
                    {
                        continue;
                    }

                    // Lookup the MS/MS Spectrum
                    secondMsMsSpectrum = lcmsRun.GetSpectrum(secondScanNumber) as ProductSpectrum;
                    if (secondMsMsSpectrum == null)
                    {
                        continue;
                    }
                }
                else if (activationMethodCombination == ActivationMethodCombination.CidOnly ||
                         activationMethodCombination == ActivationMethodCombination.HcdOnly)
                {
                    firstMsMsSpectrum = lcmsRun.GetSpectrum(firstScanNumber) as ProductSpectrum;
                }

                if (firstMsMsSpectrum == null)
                {
                    continue;
                }

                // Filter MS/MS Spectrum based on mass error
                var msMsPrecursorMz = firstMsMsSpectrum.IsolationWindow.IsolationWindowTargetMz;
                //if (Math.Abs(msMsPrecursorMz - targetMz) > 0.4) continue;
                var ppmError = LipidUtil.PpmError(targetMz, msMsPrecursorMz);
                if (Math.Abs(ppmError) > hcdMassError)
                {
                    continue;
                }


                // Assign each MS/MS spectrum to HCD or CID
                ProductSpectrum hcdSpectrum;
                ProductSpectrum cidSpectrum;
                if (firstMsMsSpectrum.ActivationMethod == ActivationMethod.HCD)
                {
                    hcdSpectrum = firstMsMsSpectrum;
                    cidSpectrum = secondMsMsSpectrum;
                }
                else
                {
                    hcdSpectrum = secondMsMsSpectrum;
                    cidSpectrum = firstMsMsSpectrum;
                }

                // Get all matching peaks
                var hcdSearchResultList = hcdSpectrum != null ? (from msMsSearchUnit in msMsSearchUnits let peak = hcdSpectrum.FindPeak(msMsSearchUnit.Mz, hcdTolerance) select new MsMsSearchResult(msMsSearchUnit, peak)).ToList() : new List <MsMsSearchResult>();
                var cidSearchResultList = cidSpectrum != null ? (from msMsSearchUnit in msMsSearchUnits let peak = cidSpectrum.FindPeak(msMsSearchUnit.Mz, cidTolerance) select new MsMsSearchResult(msMsSearchUnit, peak)).ToList() : new List <MsMsSearchResult>();

                // Find the MS1 data
                //Xic xic = lcmsRun.GetPrecursorExtractedIonChromatogram(targetMz, hcdTolerance, firstScanNumber);
                var precursorScanNumber = 0;
                if (lcmsRun.MinMsLevel == 1) //Make sure there are precursor scans in file
                {
                    precursorScanNumber = lcmsRun.GetPrecursorScanNum(matchingMsMsScanNumbers[i]);
                }
                var precursorSpectrum = lcmsRun.GetSpectrum(precursorScanNumber);
                var xic = lcmsRun.GetFullPrecursorIonExtractedIonChromatogram(targetMz, hcdTolerance);


                // Bogus data
                if (precursorSpectrum != null && (xic.GetApexScanNum() < 0 || xic.GetSumIntensities() <= 0))
                {
                    continue;
                }

                SpectrumSearchResult spectrumSearchResult;
                if (precursorSpectrum != null)
                {
                    spectrumSearchResult = new SpectrumSearchResult(hcdSpectrum, cidSpectrum, precursorSpectrum, hcdSearchResultList, cidSearchResultList, xic, lcmsRun, scoreModel, target)
                    {
                        PrecursorTolerance = new Tolerance(hcdMassError, ToleranceUnit.Ppm)
                    };
                }
                else //If there are no precursor scans in this file
                {
                    spectrumSearchResult = new SpectrumSearchResult(hcdSpectrum, cidSpectrum, hcdSearchResultList, cidSearchResultList, lcmsRun, scoreModel, target)
                    {
                        PrecursorTolerance = new Tolerance(hcdMassError, ToleranceUnit.Ppm),
                    };
                }
                spectrumSearchResultList.Add(spectrumSearchResult);
            }

            return(spectrumSearchResultList);
        }