Пример #1
0
        public static void ExtractPrecursorMasses(this RawDataCollection rawData, IRawDataPlus rawFile)
        {
            if (rawData.Performed.Contains(Operations.PrecursorMasses))
            {
                return;
            }

            CheckIfDone.Check(rawData, rawFile, new List <Operations> {
                Operations.ScanIndex, Operations.TrailerExtras, Operations.PrecursorScans
            });

            Log.Information("Extracting precursor masses");

            rawData.precursorMasses = new Dictionary <int, PrecursorMassData>();
            IEnumerable <int> scans = rawData.scanIndex.ScanEnumerators[MSOrderType.Any];

            ProgressIndicator progress = new ProgressIndicator(scans.Count(), "Extracting precursor masses");

            foreach (int scan in scans)
            {
                try
                {
                    ExtractPrecursorMasses(rawData, rawFile, scan);
                }
                catch (Exception e)
                {
                    Log.Error(e, "Failed during extraction of precursor mass for scan {Scan}", scan);
                    throw e;
                }
                progress.Update();
            }
            progress.Done();
            rawData.Performed.Add(Operations.PrecursorMasses);
        }
Пример #2
0
        public static void CalcPeakRetTimesAndInts(this RawDataCollection rawData, IRawDataPlus rawFile)
        {
            CheckIfDone.Check(rawData, rawFile, new List <Operations> {
                Operations.ScanIndex, Operations.PrecursorMasses, Operations.PrecursorScans, Operations.Ms1CentroidStreams, Operations.RetentionTimes
            });

            if (rawData.Performed.Contains(Operations.PeakRetAndInt))
            {
                return;
            }

            int[] scans = rawData.scanIndex.ScanEnumerators[MSOrderType.Ms2];

            PrecursorPeakDataCollection peaks = new PrecursorPeakDataCollection();

            ProgressIndicator P = new ProgressIndicator(total: scans.Length, message: "Analyzing precursor peaks");

            foreach (int scan in scans)
            {
                peaks.Add(scan, OnePeak(rawData: rawData, monoIsoMass: rawData.precursorMasses[scan].MonoisotopicMZ, parentScan: rawData.precursorScans[scan].MasterScan, ddScan: scan));
                P.Update();
            }
            P.Done();

            rawData.peakData = peaks;
            rawData.Performed.Add(Operations.PeakRetAndInt);
            rawData.Performed.RemoveWhere(x => x == Operations.PeakArea);
        }
Пример #3
0
        public static void WriteMatrix(RawDataCollection rawData, ScanMetaDataCollection metaData, IRawDataPlus rawFile, QuantDataCollection quantData = null, string outputDirectory = null)
        {
            string fileName = ReadWrite.GetPathToFile(outputDirectory, rawData.rawFileName, "_Matrix.txt");

            CheckIfDone.Check(rawData, rawFile, new List <Operations> {
                Operations.ScanIndex, Operations.MethodData, Operations.PrecursorScans,
                Operations.RetentionTimes, Operations.PrecursorMasses, Operations.TrailerExtras, Operations.MetaData
            });

            using (StreamWriter f = new StreamWriter(fileName)) //Open a new file
            {
                List <int> scans;

                if (!rawData.isBoxCar)
                {
                    scans = rawData.scanIndex.ScanEnumerators[rawData.scanIndex.AnalysisOrder].ToList();
                }
                else
                {
                    scans = rawData.precursorScans.Keys.ToList();
                    scans.Sort();
                }

                ProgressIndicator progress = new ProgressIndicator(scans.Count(), "Writing matrix to disk");

                f.Write("MS3ScanNumber\tMS2ScanNumber\tMS1ScanNumber\tQuantScanRetTime\tParentScanRetTime\tDutyCycle" +
                        "\tMS2ScansPerCycle\tParentIonMass\tMonoisotopicMass\tPrecursorCharge\tMS1IsolationInterference");

                if (!rawData.isBoxCar)
                {
                    f.Write("\tParentPeakFound");
                }

                if (rawData.Performed.Contains(Operations.PeakArea) & !rawData.isBoxCar)
                {
                    f.Write("\tParentPeakArea");
                }

                if (!rawData.isBoxCar)
                {
                    f.Write("\tPeakFirstScan\tPeakMaxScan\tPeakLastScan\tBaseLinePeakWidth(s)\tPeakParentScanIntensity\tPeakMaxIntensity");
                }
                f.Write("\tMS1IonInjectionTime\tMS2IonInjectionTime" +
                        "\tMS3IonInjectionTime\tHCDEnergy\tMS1MedianIntensity\tMS2MedianIntensity\t");

                if (quantData != null)
                {
                    string reagents = quantData.LabelingReagents;
                    foreach (string label in new LabelingReagents().Reagents[reagents].Labels)
                    {
                        f.Write(label + "Intensity\t");
                    }
                    foreach (string label in new LabelingReagents().Reagents[reagents].Labels)
                    {
                        f.Write(label + "Mass\t");
                    }
                    foreach (string label in new LabelingReagents().Reagents[reagents].Labels)
                    {
                        f.Write(label + "Noise\t");
                    }
                    foreach (string label in new LabelingReagents().Reagents[reagents].Labels)
                    {
                        f.Write(label + "Resolution\t");
                    }
                    foreach (string label in new LabelingReagents().Reagents[reagents].Labels)
                    {
                        f.Write(label + "Baseline\t");
                    }
                }

                f.Write("\n");

                foreach (int scan in scans)
                {
                    int ms3scan, ms2scan, masterScan;

                    if (rawData.scanIndex.AnalysisOrder == MSOrderType.Ms3)
                    {
                        ms3scan    = rawData.precursorScans[scan].MS3Scan;
                        ms2scan    = rawData.precursorScans[scan].MS2Scan;
                        masterScan = rawData.precursorScans[scan].MasterScan;
                    }
                    else
                    {
                        ms3scan    = -1;
                        ms2scan    = rawData.precursorScans[scan].MS2Scan;
                        masterScan = rawData.precursorScans[scan].MasterScan;
                    }

                    f.Write(ms3scan.ToString() + "\t" + ms2scan.ToString() + "\t" + masterScan.ToString() + "\t");

                    f.Write(rawData.retentionTimes[scan].ToString() + "\t" + rawData.retentionTimes[masterScan].ToString() + "\t");
                    f.Write(metaData[masterScan].DutyCycle.ToString() + "\t" + metaData[masterScan].MS2ScansPerCycle.ToString() + "\t");

                    f.Write(rawData.precursorMasses[ms2scan].ParentMZ.ToString() + "\t");
                    f.Write(rawData.precursorMasses[ms2scan].MonoisotopicMZ.ToString() + "\t");

                    f.Write(rawData.trailerExtras[ms2scan].ChargeState.ToString() + "\t");

                    f.Write(rawData.metaData[scan].Ms1IsolationInterference.ToString() + "\t");

                    if (!rawData.isBoxCar)
                    {
                        f.Write(rawData.peakData[ms2scan].PeakFound.ToString() + "\t");
                    }

                    if (rawData.Performed.Contains(Operations.PeakArea) & !rawData.isBoxCar)
                    {
                        f.Write(rawData.peakData[ms2scan].Area.ToString() + "\t");
                    }

                    if (!rawData.isBoxCar)
                    {
                        f.Write(rawData.peakData[ms2scan].FirstScan.ToString() + "\t");
                        f.Write(rawData.peakData[ms2scan].MaxScan.ToString() + "\t");
                        f.Write(rawData.peakData[ms2scan].LastScan.ToString() + "\t");
                        f.Write((rawData.peakData[ms2scan].BaselineWidth * 60).ToString() + "\t");
                        f.Write(rawData.peakData[ms2scan].ParentIntensity.ToString() + "\t");
                        f.Write(rawData.peakData[ms2scan].MaximumIntensity.ToString() + "\t");
                    }

                    f.Write(rawData.trailerExtras[masterScan].InjectionTime.ToString() + "\t");

                    if (rawData.scanIndex.AnalysisOrder == MSOrderType.Ms3)
                    {
                        f.Write(rawData.trailerExtras[ms2scan].InjectionTime.ToString() + "\t");
                        f.Write(rawData.trailerExtras[ms3scan].InjectionTime.ToString() + "\t");
                    }
                    else
                    {
                        f.Write(rawData.trailerExtras[ms2scan].InjectionTime.ToString() + "\t");
                        f.Write("-1\t");
                    }

                    f.Write(rawData.trailerExtras[scan].HCDEnergy + "\t");

                    f.Write(metaData[masterScan].IntensityDistribution.P50 + "\t");
                    f.Write(metaData[ms2scan].IntensityDistribution.P50 + "\t");

                    if (quantData != null)
                    {
                        foreach (string label in quantData[scan].Keys)
                        {
                            f.Write(quantData[scan][label].Intensity + "\t");
                        }
                        foreach (string label in quantData[scan].Keys)
                        {
                            f.Write(quantData[scan][label].Mass + "\t");
                        }
                        foreach (string label in quantData[scan].Keys)
                        {
                            f.Write(quantData[scan][label].Noise + "\t");
                        }
                        foreach (string label in quantData[scan].Keys)
                        {
                            f.Write(quantData[scan][label].Resolution + "\t");
                        }
                        foreach (string label in quantData[scan].Keys)
                        {
                            f.Write(quantData[scan][label].Baseline + "\t");
                        }
                    }

                    f.Write("\n");

                    progress.Update();
                }
                progress.Done();
            }
        }
Пример #4
0
        public static void WriteChromatogram(this RawDataCollection rawData, IRawDataPlus rawFile, MSOrderType order, bool TIC, bool BP, string outputDirectory)
        {
            List <Operations> operations = new List <Operations>()
            {
                Operations.RetentionTimes
            };
            //MSOrderType order = (MSOrderType)msOrder;
            MassAnalyzerType analyzer = rawData.methodData.MassAnalyzers[order];

            if (analyzer == MassAnalyzerType.MassAnalyzerFTMS)
            {
                if (order == MSOrderType.Ms)
                {
                    operations.Add(Operations.Ms1CentroidStreams);
                }
                if (order == MSOrderType.Ms2)
                {
                    operations.Add(Operations.Ms2CentroidStreams);
                }
                if (order == MSOrderType.Ms3)
                {
                    operations.Add(Operations.Ms3CentroidStreams);
                }
            }
            else
            {
                if (order == MSOrderType.Ms)
                {
                    operations.Add(Operations.Ms1SegmentedScans);
                }
                if (order == MSOrderType.Ms2)
                {
                    operations.Add(Operations.Ms2SegmentedScans);
                }
                if (order == MSOrderType.Ms3)
                {
                    operations.Add(Operations.Ms3SegmentedScans);
                }
            }

            CheckIfDone.Check(rawData, rawFile, operations);

            int[] scans = rawData.scanIndex.ScanEnumerators[order];

            if (TIC)
            {
                ProgressIndicator progress = new ProgressIndicator(scans.Length, String.Format("Writing {0} TIC chromatogram", order));
                progress.Start();
                string fileName = ReadWrite.GetPathToFile(outputDirectory, rawData.rawFileName, "_" + order + "_TIC_chromatogram.txt");

                using (StreamWriter f = new StreamWriter(fileName))
                {
                    f.WriteLine("RetentionTime\tIntensity");

                    if (analyzer == MassAnalyzerType.MassAnalyzerFTMS)
                    {
                        foreach (int scan in scans)
                        {
                            if (rawData.centroidStreams[scan].Intensities.Length > 0)
                            {
                                f.WriteLine("{0}\t{1}", rawData.retentionTimes[scan], rawData.centroidStreams[scan].Intensities.Sum());
                            }
                            else
                            {
                                f.WriteLine("{0}\t{1}", rawData.retentionTimes[scan], 0);
                            }
                            progress.Update();
                        }
                    }
                    else
                    {
                        foreach (int scan in scans)
                        {
                            if (rawData.segmentedScans[scan].Intensities.Length > 0)
                            {
                                f.WriteLine("{0}\t{1}", rawData.retentionTimes[scan], rawData.segmentedScans[scan].Intensities.Sum());
                            }
                            else
                            {
                                f.WriteLine("{0}\t{1}", rawData.retentionTimes[scan], 0);
                            }
                            progress.Update();
                        }
                    }
                }
                progress.Done();
            }
            if (BP)
            {
                ProgressIndicator progress = new ProgressIndicator(scans.Length, String.Format("Writing {0} base peak chromatogram", order));
                progress.Start();

                string fileName = ReadWrite.GetPathToFile(outputDirectory, rawData.rawFileName, "_" + order + "_BP_chromatogram.txt");

                using (StreamWriter f = new StreamWriter(fileName))
                {
                    f.WriteLine("RetentionTime\tIntensity");

                    if (analyzer == MassAnalyzerType.MassAnalyzerFTMS)
                    {
                        foreach (int scan in scans)
                        {
                            if (rawData.centroidStreams[scan].Intensities.Length > 0)
                            {
                                f.WriteLine("{0}\t{1}", rawData.retentionTimes[scan], rawData.centroidStreams[scan].Intensities.Max());
                            }
                            else
                            {
                                f.WriteLine("{0}\t{1}", rawData.retentionTimes[scan], 0);
                            }
                            progress.Update();
                        }
                    }
                    else
                    {
                        foreach (int scan in scans)
                        {
                            if (rawData.segmentedScans[scan].Intensities.Length > 0)
                            {
                                f.WriteLine("{0}\t{1}", rawData.retentionTimes[scan], rawData.segmentedScans[scan].Intensities.Max());
                            }
                            else
                            {
                                f.WriteLine("{0}\t{1}", rawData.retentionTimes[scan], 0);
                            }
                            progress.Update();
                        }
                    }
                }
                progress.Done();
            }
        }
Пример #5
0
        public static void WriteMGF(RawDataCollection rawData, IRawDataPlus rawFile, string outputDirectory, double cutoff = 0, int[] scans = null, double intensityCutoff = 0.01)
        {
            double intCutoff = 0;
            string fileName  = ReadWrite.GetPathToFile(outputDirectory, rawData.rawFileName, ".mgf");

            MassAnalyzerType ms2MassAnalyzer = rawData.methodData.MassAnalyzers[MSOrderType.Ms2];

            List <Operations> operations = new List <Operations> {
                Operations.ScanIndex, Operations.MethodData, Operations.TrailerExtras, Operations.RetentionTimes
            };

            if (ms2MassAnalyzer == MassAnalyzerType.MassAnalyzerFTMS)
            {
                operations.Add(Operations.Ms2CentroidStreams);
            }
            else
            {
                operations.Add(Operations.Ms2SegmentedScans);
            }

            CheckIfDone.Check(rawData, rawFile, operations);

            const int BufferSize = 65536;                                                         // 64 Kilobytes

            using (StreamWriter f = new StreamWriter(fileName, false, Encoding.UTF8, BufferSize)) //Open a new file, the MGF file
            {
                // if the scans argument is null, use all scans
                if (scans == null)
                {
                    scans = rawData.scanIndex.ScanEnumerators[MSOrderType.Ms2];
                }

                ProgressIndicator progress = new ProgressIndicator(scans.Count(), String.Format("Writing MGF file"));

                foreach (int i in scans)
                {
                    f.WriteLine("\nBEGIN IONS");
                    f.WriteLine("RAWFILE={0}", rawData.rawFileName);
                    f.WriteLine("TITLE=Spectrum_{0}", i);
                    f.WriteLine("SCAN={0}", i);
                    f.WriteLine("RTINSECONDS={0}", rawData.retentionTimes[i]);
                    f.WriteLine("PEPMASS={0}", rawData.trailerExtras[i].MonoisotopicMZ);
                    f.WriteLine("CHARGE={0}", rawData.trailerExtras[i].ChargeState);

                    if (ms2MassAnalyzer == MassAnalyzerType.MassAnalyzerFTMS)
                    {
                        CentroidStreamData centroid = rawData.centroidStreams[i];

                        if (centroid.Intensities.Length > 0)
                        {
                            intCutoff = centroid.Intensities.Max() * intensityCutoff;
                        }
                        else
                        {
                            intCutoff = 0;
                        }

                        for (int j = 0; j < centroid.Masses.Length; j++)
                        {
                            //f.WriteLine(Math.Round(centroid.Masses[j], 4).ToString() + " " + Math.Round(centroid.Intensities[j], 4).ToString());
                            if (centroid.Masses[j] > cutoff & centroid.Intensities[j] > intCutoff)
                            {
                                f.WriteLine("{0} {1}", Math.Round(centroid.Masses[j], 5), Math.Round(centroid.Intensities[j], 4));
                            }
                        }
                    }
                    else
                    {
                        SegmentedScanData segments = rawData.segmentedScans[i];

                        if (segments.Intensities.Length > 0)
                        {
                            intCutoff = segments.Intensities.Max() * intensityCutoff;
                        }
                        else
                        {
                            intCutoff = 0;
                        }

                        for (int j = 0; j < segments.Positions.Length; j++)
                        {
                            if (segments.Positions[j] > cutoff & segments.Intensities[j] > intCutoff)
                            {
                                f.WriteLine("{0} {1}", Math.Round(segments.Positions[j], 5), Math.Round(segments.Intensities[j], 4));
                            }
                        }
                    }

                    f.WriteLine("END IONS");

                    progress.Update();
                }
                progress.Done();
            }
            Utilities.ConsoleUtils.ClearLastLine();
        }