public Dictionary <int, int> MakeSequence(string path)
        {
            Dictionary <int, int> sequence = new Dictionary <int, int>();
            ISpectrumReader       reader   = new ThermoRawSpectrumReader();

            reader.Init(path);
            ISpectrumProcess process = new PeakPicking();

            IFinder finder = new MSFinder(tol);

            for (int i = reader.GetFirstScan(); i < reader.GetLastScan(); i++)
            {
                if (reader.GetMSnOrder(i) < 2)
                {
                    ISpectrum spectrum = reader.GetSpectrum(i);
                    process.Process(spectrum);
                    int units = finder.FindGlucoseUnits(spectrum);
                    if (units > 0)
                    {
                        sequence[i] = units;
                    }
                }
            }

            return(sequence);
        }
Пример #2
0
        public void test2()
        {
            string path = @"C:\Users\Rui Zhang\Downloads\Serum_1_C18_03292019_Ali.raw";

            ISpectrumReader reader = new ThermoRawSpectrumReader();

            reader.Init(path);
            RidgeLineFinder coeffMatrix = new RidgeLineFinder(1.0, 2, 1, 2);
            IProcess        processer   = new PeakPickingCWT(coeffMatrix);

            //for (int i = reader.GetFirstScan(); i < reader.GetLastScan(); i++)
            int i = 347;
            {
                if (reader.GetMSnOrder(i) < 2)
                {
                    ISpectrum    spectrum = reader.GetSpectrum(i);
                    List <IPeak> peaks    = spectrum.GetPeaks();
                    spectrum = processer.Process(spectrum);

                    IAreaCalculator    calculator     = new TrapezoidalRule();
                    IBounder           bounder        = new PerpendicularDrop();
                    PeakAreaCalculator areaCalculator = new PeakAreaCalculator(calculator, bounder);
                    areaCalculator.Init(peaks);

                    foreach (IPeak peak in spectrum.GetPeaks())
                    {
                        Console.WriteLine(peak.GetMZ() + " : " + areaCalculator.Area(peak).ToString());
                    }
                }
            }
        }
Пример #3
0
        public void AreaTest()
        {
            NGlycanTheoryPeaksBuilder builder = new NGlycanTheoryPeaksBuilder();

            builder.SetBuildType(true, false, false);
            List <IGlycanPeak> glycans = builder.Build();

            ISpectrumReader spectrumReader = new ThermoRawSpectrumReader();

            spectrumReader.Init(@"C:\Users\iruiz\Downloads\GUI\compare\data\HBS1_dextrinspkd_C18_10252018.raw");

            List <double> ions = new List <double>();

            ions.Add(Calculator.proton);
            Calculator.To.SetChargeIons(ions);

            using (StreamWriter file = new(@"C:\Users\iruiz\Downloads\GUI\compare\data\WriteLines2.csv"))
            {
                file.WriteLine("glycan,mass,mz,charge,score,area");
                //for (int scan = spectrumReader.GetFirstScan(); scan <= spectrumReader.GetLastScan(); scan++)
                //{
                int scan = 2931; // 3943;
                if (spectrumReader.GetMSnOrder(scan) != 1)
                {
                    return;
                }
                ISpectrum spectrum = spectrumReader.GetSpectrum(scan);

                IResultFactory       factory              = new NGlycanResultFactory();
                EnvelopeProcess      envelopeProcess      = new EnvelopeProcess(10, ToleranceBy.PPM);
                MonoisotopicSearcher monoisotopicSearcher = new MonoisotopicSearcher(factory);
                IProcess             spectrumProcess      = new LocalNeighborPicking();
                ISpectrumSearch      spectrumSearch       = new NGlycanSpectrumSearch(glycans,
                                                                                      spectrumProcess, envelopeProcess, monoisotopicSearcher);

                List <IResult> results = spectrumSearch.Search(spectrum);

                //IAreaCalculator areaCalculator = new TrapezoidalRule();
                IXIC xicer = new TIQ3XIC(spectrumReader, 0.01, ToleranceBy.Dalton);
                //IXIC xicer = new PeakXIC(areaCalculator, spectrumReader, 0.01, ToleranceBy.Dalton);
                foreach (IResult r in results)
                {
                    double area   = xicer.Area(r);
                    string output = r.Glycan().GetGlycan().Name() + "," +
                                    r.Glycan().GetGlycan().Mass().ToString() + "," +
                                    r.GetMZ().ToString() + "," +
                                    r.GetCharge().ToString() + "," +
                                    r.Score().ToString() + "," +
                                    area.ToString();
                    file.WriteLine(output);
                }
                //}
            }
            Assert.Pass();
        }
Пример #4
0
        public void NGlycanSearch()
        {
            IResultFactory       factory              = new NGlycanResultFactory();
            EnvelopeProcess      envelopeProcess      = new EnvelopeProcess(10, ToleranceBy.PPM);
            MonoisotopicSearcher monoisotopicSearcher = new MonoisotopicSearcher(factory);

            NGlycanTheoryPeaksBuilder builder = new NGlycanTheoryPeaksBuilder();

            builder.SetBuildType(true, false, false);
            List <IGlycanPeak> glycans = builder.Build();

            IProcess        spectrumProcess = new LocalNeighborPicking();
            ISpectrumSearch spectrumSearch  = new NGlycanSpectrumSearch(glycans,
                                                                        spectrumProcess, envelopeProcess, monoisotopicSearcher);

            ISpectrumReader spectrumReader = new ThermoRawSpectrumReader();

            spectrumReader.Init(@"C:\Users\iruiz\Downloads\Serum_dextrinspiked_C18_10162018_2.raw");
            for (int scan = spectrumReader.GetFirstScan(); scan <= spectrumReader.GetLastScan(); scan++)
            {
                if (spectrumReader.GetMSnOrder(scan) != 1)
                {
                    continue;
                }
                ISpectrum spectrum = spectrumReader.GetSpectrum(scan);

                List <IResult> results = spectrumSearch.Search(spectrum);
                foreach (IResult r in results)
                {
                    Console.WriteLine(r.Glycan().GetGlycan().Name()
                                      + ": " + r.Glycan().GetGlycan().Mass().ToString());

                    List <double> mzList = Calculator.To.ComputeMZ(r.Glycan().HighestPeak(), 3);
                    Console.WriteLine(string.Join(",", mzList));


                    Console.WriteLine(r.Score());
                    foreach (IPeak pk in r.Matches())
                    {
                        Console.WriteLine(pk.GetMZ() + " " + pk.GetIntensity());
                    }
                    Console.WriteLine();
                }

                break;
            }


            Assert.Pass();
        }
Пример #5
0
        public void test1()
        {
            var watch = new System.Diagnostics.Stopwatch();

            watch.Start();

            string path = @"C:\Users\Rui Zhang\Downloads\Serum_1_C18_03292019_Ali.raw";

            ISpectrumReader reader = new ThermoRawSpectrumReader();

            reader.Init(path);

            //for (int i = reader.GetFirstScan(); i < reader.GetLastScan(); i++)
            int             i           = 342;
            RidgeLineFinder coeffMatrix = new RidgeLineFinder(1.0, 2, 1, 2);

            {
                if (reader.GetMSnOrder(i) < 2)
                {
                    ISpectrum    spectrum = reader.GetSpectrum(i);
                    List <IPeak> peaks    = spectrum.GetPeaks();

                    double[] signal = peaks.Select(p => p.GetIntensity()).ToArray();
                    SortedDictionary <double, List <double> > matrix =
                        new SortedDictionary <double, List <double> >();
                    for (double a = 1; a <= 120; a += 6)
                    {
                        double[] processed = CWT.Transform(signal, a);
                        matrix[a] = processed.ToList();
                    }

                    List <RidgeLine> lines = coeffMatrix.Find(spectrum.GetPeaks().Select(p => p.GetMZ()).ToList(),
                                                              matrix);
                    Console.WriteLine(lines.Count);
                    Console.WriteLine(peaks.Count);
                    foreach (RidgeLine line in lines)
                    {
                        Console.WriteLine(line.Pos);
                    }
                }

                //break;
            }

            Console.WriteLine($"Execution Time: {watch.ElapsedMilliseconds} ms");
            //Console.Read();
        }
Пример #6
0
        public void Read()
        {
            ISpectrumReader spectrumReader = new ThermoRawSpectrumReader();

            spectrumReader.Init(@"C:\Users\iruiz\Downloads\Serum_dextrinspiked_C18_10162018_1.raw");

            int count = 0;

            for (int scan = spectrumReader.GetFirstScan(); scan <= spectrumReader.GetLastScan(); scan++)
            {
                if (spectrumReader.GetMSnOrder(scan) == 1)
                {
                    count++;
                }
            }

            Console.WriteLine(count);
            Assert.Pass();
        }
Пример #7
0
        public void test2()
        {
            string path = @"C:\Users\Rui Zhang\Downloads\Serum_1_C18_03292019_Ali.raw";

            ISpectrumReader reader = new ThermoRawSpectrumReader();

            reader.Init(path);
            RidgeLineFinder coeffMatrix = new RidgeLineFinder(1.0, 2, 1, 2);
            IProcess        processer   = new PeakPickingCWT(coeffMatrix);

            //for (int i = reader.GetFirstScan(); i < reader.GetLastScan(); i++)
            int i = 342;
            {
                if (reader.GetMSnOrder(i) < 2)
                {
                    ISpectrum spectrum = reader.GetSpectrum(i);
                    spectrum = processer.Process(spectrum);
                    foreach (var peak in spectrum.GetPeaks())
                    {
                        Console.WriteLine(peak.GetMZ().ToString() + ": " + peak.GetIntensity().ToString());
                    }
                }
            }
        }
Пример #8
0
        static void Main(string[] args)
        {
            string dir = @"C:\Users\iruiz\Downloads\GUI\compare\data";

            string[] files = Directory.GetFiles(dir);

            List <double> ions = new List <double>();

            ions.Add(Calculator.proton);
            Calculator.To.SetChargeIons(ions);

            foreach (string path in files)
            {
                Stopwatch stopWatch = new Stopwatch();
                stopWatch.Start();
                NGlycanTheoryPeaksBuilder builder = new NGlycanTheoryPeaksBuilder();
                builder.SetBuildType(true, false, false);
                List <IGlycanPeak> glycans = builder.Build();

                IResultFactory       factory              = new NGlycanResultFactory();
                EnvelopeProcess      envelopeProcess      = new EnvelopeProcess(10, ToleranceBy.PPM);
                MonoisotopicSearcher monoisotopicSearcher = new MonoisotopicSearcher(factory);
                IProcess             spectrumProcess      = new LocalNeighborPicking();
                ISpectrumSearch      spectrumSearch       = new NGlycanSpectrumSearch(glycans,
                                                                                      spectrumProcess, envelopeProcess, monoisotopicSearcher);

                ISpectrumReader spectrumReader = new ThermoRawSpectrumReader();
                spectrumReader.Init(path);


                ICurveFitting Fitter = new PolynomialFitting();



                IResultSelect resultSelect = new ResultMaxSelect();
                List <GUI>    GuiPoints    = Init(ref Fitter, spectrumReader);

                List <IResult> final = new List <IResult>();
                for (int scan = spectrumReader.GetFirstScan(); scan <= spectrumReader.GetLastScan(); scan++)
                {
                    if (spectrumReader.GetMSnOrder(scan) != 1)
                    {
                        continue;
                    }
                    ISpectrum spectrum = spectrumReader.GetSpectrum(scan);

                    List <IResult> results = spectrumSearch.Search(spectrum);
                    resultSelect.Add(results);
                    final.AddRange(results);
                }

                // original
                List <string> outputString = new List <string>();
                foreach (IResult present in final)
                {
                    int    scan  = present.GetScan();
                    double rt    = present.GetRetention();
                    double index = Math.Round(Normalize(Fitter, rt), 2);

                    TIQ3XIC xicer = new TIQ3XIC(spectrumReader);
                    double  area  = xicer.OneArea(present);

                    List <string> output = new List <string>()
                    {
                        scan.ToString(), rt.ToString(),
                                  index > 0? index.ToString():"0",
                                  present.Glycan().GetGlycan().Name(),
                                  present.GetMZ().ToString(),
                                  area.ToString(),
                    };
                    outputString.Add(string.Join(",", output));
                }
                string outputPath = Path.Combine(Path.GetDirectoryName(path),
                                                 Path.GetFileNameWithoutExtension(path) + "_one_quant.csv");
                using (FileStream ostrm = new FileStream(outputPath, FileMode.OpenOrCreate, FileAccess.Write))
                {
                    using (StreamWriter writer = new StreamWriter(ostrm))
                    {
                        writer.WriteLine("scan,time,GUI,glycan,mz,area");
                        foreach (string output in outputString)
                        {
                            writer.WriteLine(output);
                        }
                        writer.Flush();
                    }
                }


                // merged
                List <string> outputStringMerge = new List <string>();
                Dictionary <string, List <SelectResult> > resultContainer = resultSelect.Produce();

                foreach (string name in resultContainer.Keys)
                {
                    //if (name == "5-6-1-2-0")
                    //    Console.WriteLine("here");

                    List <SelectResult> selectResults = resultContainer[name];
                    foreach (SelectResult select in selectResults)
                    {
                        if (select.Results.Count == 0)
                        {
                            continue;
                        }
                        IResult present = select.Present;
                        int     scan    = present.GetScan();
                        double  rt      = present.GetRetention();
                        double  index   = Math.Round(Normalize(Fitter, rt), 2);

                        IXIC   xicer = new TIQ3XIC(spectrumReader);
                        double area  = xicer.Area(select);

                        List <string> output = new List <string>()
                        {
                            scan.ToString(), rt.ToString(),
                                      index > 0? index.ToString():"0",
                                      name,
                                      present.GetMZ().ToString(),
                                      area.ToString()
                        };
                        outputStringMerge.Add(string.Join(",", output));
                    }
                }

                string outputPathMerge = Path.Combine(Path.GetDirectoryName(path),
                                                      Path.GetFileNameWithoutExtension(path) + "_quant.csv");
                using (FileStream ostrm = new FileStream(outputPathMerge, FileMode.OpenOrCreate, FileAccess.Write))
                {
                    using (StreamWriter writer = new StreamWriter(ostrm))
                    {
                        writer.WriteLine("scan,time,GUI,glycan,mz,area");
                        foreach (string output in outputStringMerge)
                        {
                            writer.WriteLine(output);
                        }
                        writer.Flush();
                    }
                }

                stopWatch.Stop();
                TimeSpan ts = stopWatch.Elapsed;

                string elapsedTime = String.Format("{0:00}:{1:00}:{2:00}.{3:00}",
                                                   ts.Hours, ts.Minutes, ts.Seconds,
                                                   ts.Milliseconds / 10);
                Console.WriteLine("RunTime " + elapsedTime);
            }
        }
Пример #9
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 Run Read(string path, AveragineType type, ChargerType charger, string defaultDataProcessingRef,
                        ProgressUpdate updater)
        {
            Run data = new Run();

            // init reader
            ThermoRawSpectrumReader reader  = new ThermoRawSpectrumReader();
            LocalMaximaPicking      picking = new LocalMaximaPicking(ms1PrcisionPPM);
            IProcess process = new WeightedAveraging(new LocalNeighborPicking());

            reader.Init(path);

            data.spectrumList = new SpectrumList();
            Dictionary <int, Spectrum> spectrumMap = new Dictionary <int, Spectrum>();

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

            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);
                }
            }

            Parallel.ForEach(scanGroup, (scanPair) => {
                if (scanPair.Value.Count > 0)
                {
                    int parentScan          = scanPair.Key;
                    ISpectrum ms1           = null;
                    List <IPeak> majorPeaks = new List <IPeak>();
                    Spectrum ms1Spectrum    =
                        ThermoRawRunFactoryHelper.GetMS1Spectrum(ref reader, parentScan, ref ms1);
                    if (ms1Spectrum != null)
                    {
                        lock (resultLock)
                        {
                            spectrumMap[parentScan] = ms1Spectrum;
                        }
                    }

                    foreach (int scan in scanPair.Value)
                    {
                        Spectrum ms2Spectrum =
                            ThermoRawRunFactoryHelper.GetMS2Spectrum(ref reader, scan, type, charger, picking, process, ms1);
                        if (ms2Spectrum != null)
                        {
                            lock (resultLock)
                            {
                                spectrumMap[scan] = ms2Spectrum;
                            }
                        }
                    }
                }
                updater(scanGroup.Count);
            });

            List <Spectrum> spectrumList =
                spectrumMap.OrderBy(s => s.Key).Select(s => s.Value).ToList();

            data.spectrumList.spectrum = new Spectrum[spectrumList.Count];
            spectrumList = spectrumList.OrderBy(x => int.Parse(x.id.Substring(5))).ToList();
            for (int i = 0; i < spectrumList.Count; i++)
            {
                int scan = int.Parse(spectrumList[i].id.Substring(5));
                data.spectrumList.spectrum[i]       = spectrumList[i];
                data.spectrumList.spectrum[i].index = (scan - 1).ToString();
                data.spectrumList.spectrum[i].defaultArrayLength = spectrumList[i].defaultArrayLength;
            }
            data.spectrumList.count = spectrumList.Count.ToString();
            data.spectrumList.defaultDataProcessingRef = defaultDataProcessingRef;

            return(data);
        }
        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());
            }
        }
Пример #12
0
        public void Run(string path, Counter counter)
        {
            ISpectrumReader reader = new ThermoRawSpectrumReader();

            reader.Init(path);

            IGUIFinder finder  = new BinarySearchFinder(PPM);
            IProcess   picking = new LocalNeighborPicking();
            Dictionary <int, List <GUI> > pointMaps = new Dictionary <int, List <GUI> >();

            int start = reader.GetFirstScan();
            int end   = reader.GetLastScan();

            Parallel.For(start, end, (i) =>
            {
                if (reader.GetMSnOrder(i) < 2)
                {
                    ISpectrum spectrum = picking.Process(reader.GetSpectrum(i));
                    lock (resultLock)
                    {
                        pointMaps[i] = finder.FindGlucoseUnits(spectrum);
                    }
                }
                counter.Add(end - start);
            });
            List <List <GUI> > points =
                pointMaps.OrderBy(p => p.Key).Select(p => p.Value).ToList();

            IGUISequencer sequencer = new DynamicProgrammingSequencer();
            List <GUI>    guiPoints = sequencer.Choose(points);

            Dictionary <int, GUI> guiSelected = new Dictionary <int, GUI>();

            foreach (GUI gui in guiPoints)
            {
                if (guiSelected.ContainsKey(gui.Unit))
                {
                    if (guiSelected[gui.Unit].Peak.GetIntensity() < gui.Peak.GetIntensity())
                    {
                        guiSelected[gui.Unit] = gui;
                    }
                }
                else
                {
                    guiSelected[gui.Unit] = gui;
                }
            }

            Retention.Clear();
            Guis.Clear();

            List <GUI> looped = guiSelected.Values.OrderBy(g => g.Scan).ToList();
            string     output = Path.Combine(Path.GetDirectoryName(path),
                                             Path.GetFileNameWithoutExtension(path) + ".csv");

            using (FileStream ostrm = new FileStream(output, FileMode.OpenOrCreate, FileAccess.Write))
            {
                using (StreamWriter writer = new StreamWriter(ostrm))
                {
                    writer.WriteLine("scan,time,gui,peak,intensity");
                    foreach (GUI gui in looped)
                    {
                        int    scan = gui.Scan;
                        double time = reader.GetRetentionTime(scan);
                        Retention.Add(time);
                        Guis.Add(gui.Unit);
                        writer.WriteLine(scan.ToString() + "," +
                                         time.ToString() + "," +
                                         gui.Unit.ToString() + "," +
                                         gui.Peak.GetMZ().ToString() + "," +
                                         gui.Peak.GetIntensity().ToString());
                    }
                    writer.Flush();
                }
            }
            Fitter.Fit(Retention, Guis);
        }
Пример #13
0
        public void Test1()
        {
            string path  = @"C:\Users\Rui Zhang\Downloads\ZC_20171218_C16_R1.raw";
            string fasta = @"C:\Users\Rui Zhang\Downloads\haptoglobin.fasta";
            // peptides
            IProteinReader  proteinReader = new FastaReader();
            List <IProtein> proteins      = proteinReader.Read(fasta);
            List <IProtein> decoyProteins = new List <IProtein>();

            foreach (IProtein protein in proteins)
            {
                IProtein p = new BaseProtein();
                p.SetSequence(Reverse(protein.Sequence()));
                decoyProteins.Add(p);
            }

            List <Proteases> proteases = new List <Proteases>()
            {
                Proteases.Trypsin, Proteases.GluC
            };

            HashSet <string> peptides = new HashSet <string>();

            ProteinDigest proteinDigest = new ProteinDigest(2, 5, proteases[0]);

            foreach (IProtein protein in decoyProteins)
            {
                peptides.UnionWith(proteinDigest.Sequences(protein.Sequence(),
                                                           ProteinPTM.ContainsNGlycanSite));
            }

            for (int i = 1; i < proteases.Count; i++)
            {
                proteinDigest.SetProtease(proteases[i]);
                List <string> peptidesList = peptides.ToList();
                foreach (string seq in peptidesList)
                {
                    peptides.UnionWith(proteinDigest.Sequences(seq,
                                                               ProteinPTM.ContainsNGlycanSite));
                }
            }

            Assert.True(peptides.Contains("KDNLTYVGDGETR"));

            // build glycan
            GlycanBuilder glycanBuilder = new GlycanBuilder();

            glycanBuilder.Build();


            // search
            List <SearchResult> searchResults = new List <SearchResult>();

            ThermoRawSpectrumReader reader  = new ThermoRawSpectrumReader();
            LocalMaximaPicking      picking = new LocalMaximaPicking();
            IProcess process = new LocalNeighborPicking();

            reader.Init(path);
            double       searchRange = 2;
            ISpectrum    ms1         = null;
            List <IPeak> majorPeaks  = new List <IPeak>();

            ISearch <string> oneSearcher      = new BucketSearch <string>(ToleranceBy.PPM, 10);
            PrecursorMatch   precursorMatcher = new PrecursorMatch(oneSearcher);

            precursorMatcher.Init(peptides.ToList(), glycanBuilder.GlycanMaps());

            ISearch <string> moreSearcher     = new BucketSearch <string>(ToleranceBy.Dalton, 0.01);
            SequenceSearch   sequenceSearcher = new SequenceSearch(moreSearcher);

            ISearch <int> extraSearcher  = new BucketSearch <int>(ToleranceBy.Dalton, 0.01);
            GlycanSearch  glycanSearcher = new GlycanSearch(extraSearcher, glycanBuilder.GlycanMaps());

            SearchAnalyzer searchAnalyzer = new SearchAnalyzer();

            for (int i = reader.GetFirstScan(); i < reader.GetLastScan(); i++)
            {
                if (reader.GetMSnOrder(i) < 2)
                {
                    ms1        = reader.GetSpectrum(i);
                    majorPeaks = picking.Process(ms1.GetPeaks());
                }
                else
                {
                    double mz = reader.GetPrecursorMass(i, reader.GetMSnOrder(i));
                    if (ms1.GetPeaks()
                        .Where(p => p.GetMZ() > mz - searchRange && p.GetMZ() < mz + searchRange)
                        .Count() == 0)
                    {
                        continue;
                    }

                    Patterson charger = new Patterson();
                    int       charge  = charger.Charge(ms1.GetPeaks(), 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(AveragineType.GlycoPeptide);
                    BrainCSharp          braincs   = new BrainCSharp();
                    MonoisotopicSearcher searcher  = new MonoisotopicSearcher(averagine, braincs);
                    MonoisotopicScore    result    = searcher.Search(mz, charge, cluster);
                    double precursorMZ             = result.GetMZ();

                    // search
                    ISpectrum ms2 = reader.GetSpectrum(i);
                    ms2 = process.Process(ms2);

                    //precursor match
                    var pre_results = precursorMatcher.Match(precursorMZ, charge);
                    if (pre_results.Count == 0)
                    {
                        continue;
                    }

                    // spectrum search
                    var peptide_results = sequenceSearcher.Search(ms2.GetPeaks(), charge, pre_results);
                    if (peptide_results.Count == 0)
                    {
                        continue;
                    }

                    var glycan_results = glycanSearcher.Search(ms2.GetPeaks(), charge, pre_results);
                    if (glycan_results.Count == 0)
                    {
                        continue;
                    }

                    var temp_results = searchAnalyzer.Analyze(i, ms2.GetPeaks(), peptide_results, glycan_results);
                    break;
                }
            }
        }
Пример #14
0
        public void Test1()
        {
            string path = @"C:\Users\Rui Zhang\Downloads\ZC_20171218_C16_R1.raw";

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

            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);
                }
            }

            double searchRange = 2;

            foreach (KeyValuePair <int, List <int> > ScanPair in scanGroup)
            {
                if (ScanPair.Value.Count > 0)
                {
                    ISpectrum    ms1        = reader.GetSpectrum(ScanPair.Key);
                    List <IPeak> majorPeaks = picking.Process(ms1.GetPeaks());
                    foreach (int i in ScanPair.Value)
                    {
                        double mz = reader.GetPrecursorMass(i, reader.GetMSnOrder(i));
                        if (ms1.GetPeaks()
                            .Where(p => p.GetMZ() > mz - searchRange && p.GetMZ() < mz + searchRange)
                            .Count() == 0)
                        {
                            continue;
                        }

                        //Patterson charger = new Patterson();
                        if (ms1.GetPeaks()
                            .Where(p => p.GetMZ() > mz - searchRange && p.GetMZ() < mz + searchRange)
                            .Count() < 2)
                        {
                            continue;
                        }
                        Fourier   charger  = new Fourier();
                        int       charge   = charger.Charge(ms1.GetPeaks(), mz - searchRange, mz + searchRange);
                        Patterson charger2 = new Patterson();

                        Console.WriteLine(charge.ToString() + " "
                                          + charger2.Charge(ms1.GetPeaks(), mz - searchRange, mz + searchRange).ToString());
                    }
                }
            }
        }
Пример #15
0
        void GenerateTasks()
        {
            if (Path.GetExtension(msPath) == ".mgf")
            {
                MGFSpectrumReader reader = new MGFSpectrumReader();
                reader.Init(msPath);

                Dictionary <int, MS2Spectrum> spectraData = reader.GetSpectrum();
                foreach (int scan in spectraData.Keys)
                {
                    MS2Spectrum spectrum   = spectraData[scan];
                    SearchTask  searchTask = new SearchTask(spectrum,
                                                            spectrum.PrecursorMZ(), spectrum.PrecursorCharge());
                    tasks.Enqueue(searchTask);
                    readingCounter.Add(spectraData.Count);
                }
            }
            else
            {
                ISpectrumReader    reader  = new ThermoRawSpectrumReader();
                LocalMaximaPicking picking = new LocalMaximaPicking();
                IProcess           process = new WeightedAveraging(new LocalNeighborPicking());
                reader.Init(msPath);

                int start = reader.GetFirstScan();
                int end   = reader.GetLastScan();

                Dictionary <int, List <int> > scanGroup = new Dictionary <int, List <int> >();
                int current = -1;
                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);
                    }
                }

                Parallel.ForEach(scanGroup,
                                 new ParallelOptions {
                    MaxDegreeOfParallelism = SearchingParameters.Access.ThreadNums
                },
                                 (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;
                            }

                            ICharger charger = new Patterson();
                            int charge       = charger.Charge(ms1Peaks, mz - searchRange, mz + searchRange);

                            // 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);

                            // 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(AveragineType.GlycoPeptide);
                            BrainCSharp braincs           = new BrainCSharp();
                            MonoisotopicSearcher searcher = new MonoisotopicSearcher(averagine, braincs);
                            MonoisotopicScore result      = searcher.Search(mz, charge, cluster);
                            double precursorMZ            = result.GetMZ();

                            // search
                            ISpectrum ms2 = reader.GetSpectrum(i);
                            ms2           = process.Process(ms2);

                            SearchTask searchTask = new SearchTask(ms2, precursorMZ, charge);
                            tasks.Enqueue(searchTask);
                        }
                    }
                    readingCounter.Add(scanGroup.Count);
                });
            }
        }
        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);
        }