public void Test1()
        {
            string path = @"D:\Raw\ZC_20171218_N14_R2.raw";

            ISpectrumReader    reader  = new ThermoRawSpectrumReader();
            LocalMaximaPicking picking = new LocalMaximaPicking(10);

            reader.Init(path);


            Dictionary <int, int> scanMap = new Dictionary <int, int>();
            int current = -1;
            int start   = reader.GetFirstScan();
            int end     = reader.GetLastScan();

            for (int i = start; i < end; i++)
            {
                if (reader.GetMSnOrder(i) == 1)
                {
                    current = i;
                }
                else if (reader.GetMSnOrder(i) == 2)
                {
                    scanMap[i] = current;
                }
            }

            double searchRange = 1;

            int scan_num = 6223;

            if (scanMap.ContainsKey(scan_num))
            {
                int       paranet_scan = scanMap[scan_num];
                ISpectrum ms1          = reader.GetSpectrum(paranet_scan);

                double       mz       = reader.GetPrecursorMass(scan_num, reader.GetMSnOrder(scan_num));
                List <IPeak> ms1Peaks = FilterPeaks(ms1.GetPeaks(), mz, searchRange);

                if (ms1Peaks.Count() == 0)
                {
                    return;
                }

                // insert pseudo peaks for large gap
                List <IPeak> peaks     = new List <IPeak>();
                double       precision = 0.02;
                double       last      = ms1Peaks.First().GetMZ();
                foreach (IPeak peak in ms1Peaks)
                {
                    if (peak.GetMZ() - last > precision)
                    {
                        peaks.Add(new GeneralPeak(last + precision / 2, 0));
                        peaks.Add(new GeneralPeak(peak.GetMZ() - precision / 2, 0));
                    }
                    peaks.Add(peak);
                    last = peak.GetMZ();
                }

                List <IPeak> majorPeaks = picking.Process(peaks);

                //Console.WriteLine("mz,intensity");
                //foreach (IPeak pk in peaks)
                //{
                //    Console.WriteLine(pk.GetMZ().ToString() + "," + pk.GetIntensity().ToString());
                //}

                Fourier   charger  = new Fourier();
                int       charge   = charger.Charge(peaks, mz - searchRange, mz + searchRange);
                Patterson charger2 = new Patterson();

                PattersonFourierCombine charger3 = new PattersonFourierCombine();

                Console.WriteLine(charge.ToString() + " "
                                  + charger2.Charge(peaks, mz - searchRange, mz + searchRange).ToString() + " "
                                  + charger3.Charge(peaks, mz - searchRange, mz + searchRange).ToString());
            }
        }
Esempio n. 2
0
        public void ParallelRun(string path, string outputDir, AveragineType type, ChargerType chargerType)
        {
            string file   = Path.GetFileNameWithoutExtension(path) + ".mgf";
            string output = Path.Combine(outputDir, file);

            ThermoRawSpectrumReader reader  = new ThermoRawSpectrumReader();
            LocalMaximaPicking      picking = new LocalMaximaPicking(ms1PrcisionPPM);

            reader.Init(path);

            Dictionary <int, List <int> > scanGroup = new Dictionary <int, List <int> >();
            int current = -1;
            int start   = reader.GetFirstScan();
            int end     = reader.GetLastScan();

            for (int i = start; i < end; i++)
            {
                if (reader.GetMSnOrder(i) == 1)
                {
                    current      = i;
                    scanGroup[i] = new List <int>();
                }
                else if (reader.GetMSnOrder(i) == 2)
                {
                    scanGroup[current].Add(i);
                }
            }

            List <MS2Info> ms2Infos = new List <MS2Info>();

            Parallel.ForEach(scanGroup, (scanPair) =>
            {
                if (scanPair.Value.Count > 0)
                {
                    ISpectrum ms1 = reader.GetSpectrum(scanPair.Key);
                    foreach (int i in scanPair.Value)
                    {
                        double mz             = reader.GetPrecursorMass(i, reader.GetMSnOrder(i));
                        List <IPeak> ms1Peaks = FilterPeaks(ms1.GetPeaks(), mz, searchRange);

                        if (ms1Peaks.Count() == 0)
                        {
                            continue;
                        }

                        // insert pseudo peaks for large gap
                        List <IPeak> peaks = new List <IPeak>();
                        double precision   = 0.02;
                        double last        = ms1Peaks.First().GetMZ();
                        foreach (IPeak peak in ms1Peaks)
                        {
                            if (peak.GetMZ() - last > precision)
                            {
                                peaks.Add(new GeneralPeak(last + precision / 2, 0));
                                peaks.Add(new GeneralPeak(peak.GetMZ() - precision / 2, 0));
                            }
                            peaks.Add(peak);
                            last = peak.GetMZ();
                        }
                        List <IPeak> majorPeaks = picking.Process(peaks);
                        ICharger charger        = new Patterson();
                        if (chargerType == ChargerType.Fourier)
                        {
                            charger = new Fourier();
                        }
                        else if (chargerType == ChargerType.Combined)
                        {
                            charger = new PattersonFourierCombine();
                        }
                        int charge = charger.Charge(peaks, mz - searchRange, mz + searchRange);

                        // find evelope cluster
                        EnvelopeProcess envelope = new EnvelopeProcess();
                        var cluster = envelope.Cluster(majorPeaks, mz, charge);
                        if (cluster.Count == 0)
                        {
                            continue;
                        }

                        // find monopeak
                        Averagine averagine           = new Averagine(type);
                        BrainCSharp braincs           = new BrainCSharp();
                        MonoisotopicSearcher searcher = new MonoisotopicSearcher(averagine, braincs);
                        MonoisotopicScore result      = searcher.Search(mz, charge, cluster);
                        double precursorMZ            = result.GetMZ();

                        // write mgf
                        ISpectrum ms2      = reader.GetSpectrum(i);
                        IProcess processer = new WeightedAveraging(new LocalNeighborPicking());
                        ms2 = processer.Process(ms2);

                        MS2Info ms2Info = new MS2Info
                        {
                            PrecursorMZ     = result.GetMZ(),
                            PrecursorCharge = charge,
                            Scan            = ms2.GetScanNum(),
                            Retention       = ms2.GetRetention(),
                            Peaks           = ms2.GetPeaks()
                        };
                        lock (resultLock)
                        {
                            ms2Infos.Add(ms2Info);
                        }
                    }
                }
                readingProgress.Add(scanGroup.Count);
            });

            ms2Infos = ms2Infos.OrderBy(m => m.Scan).ToList();
            using (FileStream ostrm = new FileStream(output, FileMode.OpenOrCreate, FileAccess.Write))
            {
                using (StreamWriter writer = new StreamWriter(ostrm))
                {
                    foreach (MS2Info ms2 in ms2Infos)
                    {
                        WriteMGF(writer, path + ",SCANS=" + ms2.Scan.ToString() + ",PRECURSOR=" + ms2.PrecursorMZ, ms2.PrecursorMZ, ms2.PrecursorCharge,
                                 ms2.Scan, ms2.Retention * 60, reader.GetActivation(ms2.Scan), ms2.Peaks);
                        writer.Flush();
                    }
                }
            }

            // update progress
            progress.Add();
        }
        public static Spectrum GetMS2Spectrum(ref ThermoRawSpectrumReader reader,
                                              int scan, AveragineType type, ChargerType chargerType, LocalMaximaPicking picking, IProcess process,
                                              ISpectrum ms1)
        {
            // scan header
            Spectrum spectrum = new Spectrum
            {
                id = "scan=" + scan.ToString()
            };

            double dLowMass           = 0;
            double dHighMass          = 0;
            double dTIC               = 0;
            double dBasePeakMass      = 0;
            double dBasePeakIntensity = 0;

            reader.GetScanHeaderInfoForScanNum(scan, ref dLowMass, ref dHighMass,
                                               ref dTIC, ref dBasePeakMass, ref dBasePeakIntensity);
            SetScanHeader(spectrum, dLowMass, dHighMass, dTIC,
                          dBasePeakMass, dBasePeakIntensity);

            // binary data
            spectrum.binaryDataArrayList = new BinaryDataArrayList();
            SetBinaryDataArrayHeader(spectrum.binaryDataArrayList);

            spectrum.cvParam[0] = new Component.CVParam()
            {
                cvRef     = "MS",
                accession = "MS:1000511",
                name      = "ms level",
                value     = "2",
            };

            double       mz       = reader.GetPrecursorMass(scan, reader.GetMSnOrder(scan));
            List <IPeak> ms1Peaks = FilterPeaks(ms1.GetPeaks(), mz, searchRange);

            if (ms1Peaks.Count() == 0)
            {
                return(null);
            }

            // insert pseudo peaks for large gaps
            List <IPeak> peaks     = new List <IPeak>();
            double       precision = 0.02;
            double       last      = ms1Peaks.First().GetMZ();

            foreach (IPeak peak in ms1Peaks)
            {
                if (peak.GetMZ() - last > precision)
                {
                    peaks.Add(new GeneralPeak(last + precision / 2, 0));
                    peaks.Add(new GeneralPeak(peak.GetMZ() - precision / 2, 0));
                }
                peaks.Add(peak);
                last = peak.GetMZ();
            }
            List <IPeak> majorPeaks = picking.Process(peaks);
            ICharger     charger    = new Patterson();

            if (chargerType == ChargerType.Fourier)
            {
                charger = new Fourier();
            }
            else if (chargerType == ChargerType.Combined)
            {
                charger = new PattersonFourierCombine();
            }
            int charge = charger.Charge(peaks, mz - searchRange, mz + searchRange);

            // find evelope cluster
            EnvelopeProcess envelope = new EnvelopeProcess();
            var             cluster  = envelope.Cluster(majorPeaks, mz, charge);

            if (cluster.Count == 0)
            {
                return(null);
            }

            // find monopeak
            Averagine            averagine = new Averagine(type);
            BrainCSharp          braincs   = new BrainCSharp();
            MonoisotopicSearcher searcher  = new MonoisotopicSearcher(averagine, braincs);
            MonoisotopicScore    result    = searcher.Search(mz, charge, cluster);

            // process spectrum
            ISpectrum ms2 = reader.GetSpectrum(scan);

            List <IPeak> ms2Peaks = process.Process(ms2).GetPeaks();

            spectrum.binaryDataArrayList.binaryDataArray[0].binary =
                ms2Peaks.SelectMany(p => BitConverter.GetBytes(p.GetMZ())).ToArray();
            spectrum.binaryDataArrayList.binaryDataArray[1].binary =
                ms2Peaks.SelectMany(p => BitConverter.GetBytes(p.GetIntensity())).ToArray();
            spectrum.defaultArrayLength = ms2Peaks.Count.ToString();

            spectrum.precursorList = new PrecursorList
            {
                count     = "1",
                precursor = new Precursor[1]
            };
            for (int i = 0; i < spectrum.precursorList.precursor.Length; i++)
            {
                spectrum.precursorList.precursor[i] = new Precursor();
            }

            spectrum.precursorList.precursor[0].selectedIonList = new SelectedIonList
            {
                count       = "1",
                selectedIon = new SelectedIon[1]
            };
            for (int i = 0; i < spectrum.precursorList.precursor[0].selectedIonList.selectedIon.Length; i++)
            {
                spectrum.precursorList.precursor[0].selectedIonList.selectedIon[i] = new SelectedIon();
            }
            spectrum.precursorList.precursor[0].selectedIonList.selectedIon[0].cvParam    = new Component.CVParam[2];
            spectrum.precursorList.precursor[0].selectedIonList.selectedIon[0].cvParam[0] = new Component.CVParam()
            {
                cvRef         = "MS",
                accession     = "MS:1000744",
                name          = "selected ion m/z",
                value         = result.GetMZ().ToString(),
                unitCvRef     = "MS",
                unitAccession = "MS:1000040",
                unitName      = "m/z"
            };
            spectrum.precursorList.precursor[0].selectedIonList.selectedIon[0].cvParam[1] = new Component.CVParam()
            {
                cvRef     = "MS",
                accession = "MS:1000041",
                name      = "charge state",
                value     = charge.ToString()
            };
            spectrum.precursorList.precursor[0].activation = new Activation
            {
                cvParam = new Component.CVParam[1]
            };
            spectrum.precursorList.precursor[0].activation.cvParam[0] =
                ActivationCVParam(reader.GetActivation(scan));

            spectrum.binaryDataArrayList.binaryDataArray[0].encodedLength =
                Convert.ToBase64String(spectrum.binaryDataArrayList.binaryDataArray[0].binary).Length.ToString();
            spectrum.binaryDataArrayList.binaryDataArray[1].encodedLength =
                Convert.ToBase64String(spectrum.binaryDataArrayList.binaryDataArray[1].binary).Length.ToString();
            return(spectrum);
        }