Ejemplo n.º 1
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();
        }
Ejemplo n.º 2
0
 public NGlycanSpectrumSearch(List <IGlycanPeak> glycans,
                              IProcess spectrumProcessor, EnvelopeProcess envelopeProcessor,
                              MonoisotopicSearcher monoisotopicSearcher,
                              int maxCharge = 3, double cutoff = 0.9)
 {
     this.glycans              = glycans;
     this.spectrumProcessor    = spectrumProcessor;
     this.envelopeProcessor    = envelopeProcessor;
     this.monoisotopicSearcher = monoisotopicSearcher;
     this.maxCharge            = maxCharge;
     this.cutoff = cutoff;
 }
Ejemplo n.º 3
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();
        }
Ejemplo n.º 4
0
        public MainWindow()
        {
            InitializeComponent();

            // build up searcher
            NGlycanTheoryPeaksBuilder builder = new NGlycanTheoryPeaksBuilder();

            builder.SetBuildType(true, false, false);
            glycans = builder.Build();

            IResultFactory       factory              = new NGlycanResultFactory();
            EnvelopeProcess      envelopeProcess      = new EnvelopeProcess(10, ToleranceBy.PPM);
            MonoisotopicSearcher monoisotopicSearcher = new MonoisotopicSearcher(factory);

            spectrumSearch = new NGlycanSpectrumSearch(glycans,
                                                       spectrumProcessor, envelopeProcess, monoisotopicSearcher);
        }
Ejemplo n.º 5
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);
            }
        }
Ejemplo n.º 6
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();
        }
Ejemplo n.º 7
0
        public void Run(string path, Counter counter, ISpectrumReader spectrumReader, NormalizerEngine normalizer)
        {
            NGlycanTheoryPeaksBuilder builder = new NGlycanTheoryPeaksBuilder();

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

            spectrumReader.Init(path);

            IResultSelect resultSelect = new ResultMaxSelect(SearchingParameters.Access.Threshold,
                                                             SearchingParameters.Access.PeakCutoff);
            List <IResult> final = new List <IResult>();

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

            Parallel.For(start, end, (i) =>
            {
                if (spectrumReader.GetMSnOrder(i) < 2)
                {
                    IResultFactory factory          = new NGlycanResultFactory();
                    EnvelopeProcess envelopeProcess = new EnvelopeProcess(
                        SearchingParameters.Access.Tolerance,
                        SearchingParameters.Access.ToleranceBy);
                    MonoisotopicSearcher monoisotopicSearcher = new MonoisotopicSearcher(factory);
                    IProcess spectrumProcess       = new LocalNeighborPicking();
                    ISpectrumSearch spectrumSearch = new NGlycanSpectrumSearch(glycans,
                                                                               spectrumProcess, envelopeProcess, monoisotopicSearcher,
                                                                               SearchingParameters.Access.MaxCharage, SearchingParameters.Access.Cutoff);

                    ISpectrum spectrum     = spectrumReader.GetSpectrum(i);
                    List <IResult> results = spectrumSearch.Search(spectrum);
                    lock (resultLock)
                    {
                        resultSelect.Add(results);
                        final.AddRange(results);
                    }
                }
                counter.Add(end - start);
            });

            //original
            string outputPath = Path.Combine(Path.GetDirectoryName(path),
                                             Path.GetFileNameWithoutExtension(path) + "_one_quant.csv");
            List <string> outputString = new List <string>();

            foreach (IResult present in final.OrderBy(r => r.GetRetention()))
            {
                int    scan  = present.GetScan();
                double rt    = present.GetRetention();
                double index = Math.Round(normalizer.Normalize(rt), 2);

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

                List <string> output;
                if (!normalizer.Initialized())
                {
                    output = new List <string>()
                    {
                        scan.ToString(), rt.ToString(),
                                         present.Glycan().GetGlycan().Name(),
                                         present.GetMZ().ToString(),
                                         area.ToString(),
                    };
                }
                else
                {
                    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));
            }

            using (FileStream ostrm = new FileStream(outputPath, FileMode.OpenOrCreate, FileAccess.Write))
            {
                using (StreamWriter writer = new StreamWriter(ostrm))
                {
                    if (!normalizer.Initialized())
                    {
                        writer.WriteLine("scan,time,glycan,mz,area");
                    }
                    else
                    {
                        writer.WriteLine("scan,time,GUI,glycan,mz,area");
                    }
                    foreach (string output in outputString)
                    {
                        writer.WriteLine(output);
                    }
                    writer.Flush();
                }
            }



            // merged
            string outputPathMerge = Path.Combine(Path.GetDirectoryName(path),
                                                  Path.GetFileNameWithoutExtension(path) + "_quant.csv");
            List <string> outputStringMerge = new List <string>();
            Dictionary <string, List <SelectResult> > resultContainer = resultSelect.Produce();

            foreach (string name in resultContainer.Keys)
            {
                List <SelectResult> selectResults = resultContainer[name];
                foreach (SelectResult select in selectResults)
                {
                    IResult present = select.Present;
                    int     scan    = present.GetScan();
                    double  rt      = present.GetRetention();
                    double  index   = Math.Round(normalizer.Normalize(rt), 2);

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

                    List <string> output;
                    if (!normalizer.Initialized())
                    {
                        output = new List <string>()
                        {
                            scan.ToString(), rt.ToString(),
                                             name,
                                             present.GetMZ().ToString(),
                                             area.ToString(),
                        };
                    }
                    else
                    {
                        output = new List <string>()
                        {
                            scan.ToString(), rt.ToString(),
                                             index.ToString(),
                                             name,
                                             present.GetMZ().ToString(),
                                             area.ToString(),
                        };
                    }
                    outputStringMerge.Add(string.Join(",", output));
                }
            }

            using (FileStream ostrm = new FileStream(outputPathMerge, FileMode.OpenOrCreate, FileAccess.Write))
            {
                using (StreamWriter writer = new StreamWriter(ostrm))
                {
                    if (!normalizer.Initialized())
                    {
                        writer.WriteLine("scan,time,glycan,mz,area");
                    }
                    else
                    {
                        writer.WriteLine("scan,time,GUI,glycan,mz,area");
                    }
                    foreach (string output in outputStringMerge)
                    {
                        writer.WriteLine(output);
                    }
                    writer.Flush();
                }
            }
        }
Ejemplo n.º 8
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;
                }
            }
        }
Ejemplo n.º 9
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);
        }