private void Area_Click(object sender, RoutedEventArgs e)
        {
            if (path.Length == 0)
            {
                MessageBox.Show("Please input the spectrum file (*.Raw) path!");
                return;
            }

            int scan;

            int.TryParse(ScanNumber.Text, out scan);
            if (scan > 0 && scan < spectrumReader.GetLastScan() &&
                spectrumReader.GetMSnOrder(scan) == 1)
            {
                ISpectrum      spectrum = spectrumReader.GetSpectrum(scan);
                List <IResult> results  = spectrumSearch.Search(spectrum);
                double         rt       = spectrumReader.GetRetentionTime(scan);
                double         index    = Math.Round(engine.Normalize(rt), 2);

                ConcurrentDictionary <IResult, double> quant = new ConcurrentDictionary <IResult, double>();
                Parallel.ForEach(results, (r) =>
                {
                    IXIC xicer  = new TIQ3XIC(spectrumReader, 0.01, ToleranceBy.Dalton);
                    double area = xicer.Area(r);
                    quant[r]    = area;
                });

                string outputPath = System.IO.Path.Combine(System.IO.Path.GetDirectoryName(path),
                                                           System.IO.Path.GetFileNameWithoutExtension(path) + ".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,factor");
                        foreach (IResult r in quant.Keys)
                        {
                            List <string> output = new List <string>()
                            {
                                scan.ToString(), rt.ToString(),
                                         index > 0? index.ToString():"0",
                                         r.Glycan().GetGlycan().Name(), r.GetMZ().ToString(),
                                         quant[r].ToString()
                            };

                            writer.WriteLine(string.Join(",", output));
                        }
                        writer.Flush();
                    }
                }
            }
            else
            {
                MessageBox.Show("Please input a valid scan number!");
                return;
            }
        }
Exemple #2
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();
        }
Exemple #3
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);
            }
        }
        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();
                }
            }
        }