예제 #1
0
파일: Program.cs 프로젝트: Ykobe/metaCode
                    }
                }
                writer.Flush();
                startNum += 1;
            }
            writer.Close();

        }

        public static void getMSdataFromRT(double startRT, int endRT, string filePath, double lowMS = 0.00, double highMS = 0.00)
        {
            IRawDataPlus plus = RawFileReaderAdapter.FileFactory(filePath);

            plus.SelectInstrument(Device.MS, 1);
            if (highMS - 0.00 <= 0.01)
            {
                highMS = plus.RunHeaderEx.HighMass;
            }
            int startNum = plus.ScanNumberFromRetentionTime(startRT);
            int endNum = plus.ScanNumberFromRetentionTime(endRT);
            int maxNum = plus.RunHeaderEx.LastSpectrum > endNum ? endNum : plus.RunHeaderEx.LastSpectrum;

            string fileName = DateTime.Now.ToLocalTime().ToString().Replace("/", "").Replace(":", "").Replace(" ", "_") + ".csv";
            FileStream stream = new FileStream(fileName, FileMode.Create, FileAccess.Write);
            StreamWriter writer = new StreamWriter(stream, Encoding.UTF8);
            writer.WriteLine("ScanNumber, RT, Mass, Resolution, Intensity, Noise, Filter");
            while (startNum <= endNum)
            {
                Scan scan = Scan.FromFile(plus, startNum);
                string filter = plus.GetFilterForScanNumber(startNum).ToString();
                int cnt = 0;
                while (cnt < scan.PreferredMasses.Length)
                {
                    if (lowMS < scan.PreferredMasses[cnt] && scan.PreferredMasses[cnt] < highMS)
                    {
                        double rt = plus.RetentionTimeFromScanNumber(startNum);
                        double msval = scan.PreferredMasses[cnt];
                        double resolution = (scan.PreferredResolutions.Length == 0) ? 0.0 : scan.PreferredResolutions[cnt];
                        double intensity = (scan.PreferredIntensities.Length == 0) ? 0.0 : scan.PreferredIntensities[cnt];
                        double noise = (scan.PreferredNoises.Length == 0) ? 0.0 : scan.PreferredNoises[cnt];
                        //writer.WriteLine("{0},{1},{2},{3},{4},{5},{6}", startNum.ToString(), rt.ToString(), msval.ToString(), resolution.ToString(), intensity.ToString(), noise.ToString(), filter.ToString());

                        writer.WriteLine("{0},{1},{2},{3}", startNum.ToString(), rt.ToString(), msval.ToString(), intensity.ToString());
                    }
                    cnt += 1;
                }
                startNum += 1;
            }
            writer.Close();
        }

        public static void getSRMIacdMZ(string filePath, double valMS,int recVal)
예제 #2
0
        private void InitializeChromatogramPlot()
        {
            chromatogram = new PlotModel();

            chromatogram.Axes.Add(new LinearAxis {
                Position = AxisPosition.Left, AbsoluteMinimum = 0
            });
            chromatogram.Axes.Add(new LinearAxis {
                Position = AxisPosition.Bottom, AbsoluteMinimum = 0, AbsoluteMaximum = TotalTime
            });
            chromatogram.Axes[x].MajorGridlineStyle = LineStyle.Solid;
            chromatogram.Axes[y].MajorGridlineStyle = LineStyle.Solid;
            //chromatogram.Axes[y].IsZoomEnabled = false;

            var currentScanLine = new LineAnnotation()
            {
                Type  = LineAnnotationType.Vertical,
                X     = 0,
                Color = OxyColors.Black
            };

            var mouseLocationLine = new LineAnnotation()
            {
                Type  = LineAnnotationType.Vertical,
                X     = 0,
                Color = OxyColors.DimGray
            };

            var chroSeries = new AreaSeries();

            chroSeries.Color = Colors.ColorBrewer8ClassSet2(255).Last();

            for (int i = 0; i < ChromatogramData.PositionsArray[0].Length; i++)
            {
                chroSeries.Points.Add(new DataPoint(ChromatogramData.PositionsArray[0][i], ChromatogramData.IntensitiesArray[0][i]));
            }

            chromatogram.Series.Add(chroSeries);

            double max = (from i in ChromatogramData.IntensitiesArray[0] select i).Max() * 1.5;

            if (max <= 0)
            {
                max = 1000;
            }

            chromatogram.Axes[y].AbsoluteMaximum = max;
            chromatogram.Axes[y].Maximum         = chromatogram.Axes[y].AbsoluteMaximum;
            chromatogram.Axes[y].MajorStep       = chromatogram.Axes[y].Maximum / 3;
            chromatogram.Axes[y].StringFormat    = "0.00E00";

            chromatogram.Axes[y].TransformChanged += (object sender, EventArgs e) =>
            {
                chromatogram.Axes[y].MajorStep = (chromatogram.Axes[y].ActualMaximum - chromatogram.Axes[y].ActualMinimum) / 3;
            };

            plotViewChromatogram.MouseEnter += (s, e) =>
            {
                plotViewChromatogram.Cursor = Cursors.Cross;
            };

            chromatogram.MouseDown += (s, e) =>
            {
                if (e.ChangedButton == OxyMouseButton.Left)
                {
                    double rt = chromatogram.Axes[x].InverseTransform(e.Position.X, e.Position.Y, chromatogram.Axes[y]).X;

                    scanNumber.Text = RawData.ScanNumberFromRetentionTime(rt).ToString();

                    currentScanLine.X = RawData.RetentionTimeFromScanNumber(Convert.ToInt32(scanNumber.Text));

                    e.Handled = true;
                    chromatogram.InvalidatePlot(false);
                }
            };

            chromatogram.MouseMove += (s, e) =>
            {
                try
                {
                    mouseLocationLine.X = currentScanLine.InverseTransform(e.Position).X;
                }
                catch (Exception)
                {
                    //do nothing
                }

                e.Handled = false;
                chromatogram.InvalidatePlot(false);
            };


            chromatogram.MouseUp += (s, e) =>
            {
                plotViewChromatogram.Cursor = Cursors.Cross;
                chromatogram.InvalidatePlot(true);
                e.Handled = false;
            };

            chromatogram.MouseLeave += (s, e) =>
            {
                mouseLocationLine.X = 0;

                chromatogram.InvalidatePlot(false);
                e.Handled = true;
            };

            chromatogram.Annotations.Add(currentScanLine);
            chromatogram.Annotations.Add(mouseLocationLine);

            currentScanLine.MouseDown += (s, e) =>
            {
                if (e.ChangedButton != OxyMouseButton.Left)
                {
                    return;
                }

                plotViewChromatogram.Cursor = Cursors.SizeWE;
                chromatogram.InvalidatePlot(false);
                e.Handled = true;
            };

            currentScanLine.MouseMove += (s, e) =>
            {
                double rt = currentScanLine.InverseTransform(e.Position).X;

                currentScanLine.X   = rt;
                mouseLocationLine.X = rt;

                scanNumber.Text = RawData.ScanNumberFromRetentionTime(rt).ToString();

                chromatogram.InvalidatePlot(false);
                e.Handled = true;
            };

            currentScanLine.MouseUp += (s, e) =>
            {
                plotViewChromatogram.Cursor = Cursors.Cross;
                e.Handled = true;
            };


            chromatogram.InvalidatePlot(true);
        }
예제 #3
0
 //int maxNum = plus.RunHeaderEx.LastSpectrum > endNum ? endNum : plus.RunHeaderEx.LastSpectrum;

            LimitedSizeDictionary<string, int> precursorMs2ScanNumbers = new LimitedSizeDictionary<string, int>(40);

            plus.SelectInstrument(Device.MS, 1);
            if(highMS - 0.00 <= 0.01)
            {
                highMS = plus.RunHeaderEx.HighMass;
            }

            double maxMs = plus.RunHeaderEx.HighMass;
            double minMs = plus.RunHeaderEx.LowMass;

            lowMS = minMs;
            highMS = maxMs;

            string fileName = DateTime.Now.ToLocalTime().ToString().Replace("/", "").Replace(":", "").Replace(" ", "_") + ".csv";
            FileStream stream = new FileStream(fileName, FileMode.Create, FileAccess.Write);
            StreamWriter writer = new StreamWriter(stream, Encoding.UTF8);
            
            writer.WriteLine("ScanNumber, RT, Mass, Resolution, Intensity, Noise, Filter,MSLevel,collisionEnergy, precursorNum,precursorMS");
            while (startNum <= endNum)
            {
                var levelInfo = "";
                var spectrumRef = "";
                Scan scan = Scan.FromFile(plus, startNum);
                IScanFilter scanFilter = plus.GetFilterForScanNumber(startNum);

                IScanEvent scanEvent = plus.GetScanEventForScanNumber(startNum);

                switch (scanFilter.MSOrder)
                {
                    case MSOrderType.Ms:
                        // Keep track of scan number for precursor reference
                        precursorMs1ScanNumber = startNum;
                        reaction = scanEvent.GetReaction(0);
                        value = reaction.CollisionEnergy.ToString(CultureInfo.InvariantCulture).ToString();
                        levelInfo = scanFilter.MSOrder.ToString() + "," + "" + "," + "" + "," + "";
                        break;
                    case MSOrderType.Ms2:

                        // Keep track of scan number and isolation m/z for precursor reference                   
                        var result = Regex.Match(scanEvent.ToString(), FilterStringIsolationMzPattern);
                        if (result.Success)
                        {
                            if (precursorMs2ScanNumbers.ContainsKey(result.Groups[1].Value))
                            {
                                precursorMs2ScanNumbers.Remove(result.Groups[1].Value);
                            }

                            precursorMs2ScanNumbers.Add(result.Groups[1].Value, startNum);
                        }
                        spectrumRef = precursorMs1ScanNumber.ToString();
                        reaction = scanEvent.GetReaction(0);
                        value = reaction.CollisionEnergy.ToString(CultureInfo.InvariantCulture).ToString();
                        precursorMS = reaction.PrecursorMass;
                        levelInfo = scanFilter.MSOrder.ToString() + "," + value + "," + spectrumRef.ToString() + "," +  precursorMS.ToString()  ;
                        break;
                    case MSOrderType.Ms3:
                        var precursorMs2ScanNumber = precursorMs2ScanNumbers.Keys.FirstOrDefault(isolationMz => scanEvent.ToString().Contains(isolationMz));
                        spectrumRef = precursorMs2ScanNumbers[precursorMs2ScanNumber].ToString();
                        if (!precursorMs2ScanNumber.IsNullOrEmpty())
                        {
                            reaction = scanEvent.GetReaction(1);
                        }
                        else
                        {
                            throw new InvalidOperationException("Couldn't find a MS2 precursor scan for MS3 scan " + scanEvent);
                        }
                        precursorMS = reaction.PrecursorMass;
                        value = reaction.CollisionEnergy.ToString(CultureInfo.InvariantCulture).ToString();
                        levelInfo = scanFilter.MSOrder.ToString() + "," + value +  "," + spectrumRef.ToString() + "," + precursorMS.ToString();
                        break;
                    default:
                        throw new ArgumentOutOfRangeException();
                }

                //var scanStatistics = plus.GetScanStatsForScanNumber(startNum);
                //// Get the segmented (low res and profile) scan data
                //var segmentedScan = plus.GetSegmentedScanFromScanNumber(startNum, scanStatistics);
                //var masses = segmentedScan.Positions;
                //var intensitis = segmentedScan.Intensities;
                //Console.WriteLine(masses.Length);
                //Console.WriteLine(intensitis.Length);
                //Console.WriteLine(filterObj.MSOrder);

                int cnt = 0;
                while(cnt < scan.PreferredMasses.Length)
                {
                    if(lowMS < scan.PreferredMasses[cnt] && scan.PreferredMasses[cnt] < highMS )
                    {
                        double rt = plus.RetentionTimeFromScanNumber(startNum);
                        double msval = scan.PreferredMasses[cnt];
                        double resolution = (scan.PreferredResolutions.Length == 0) ? 0.0 : scan.PreferredResolutions[cnt];
                        double intensity = (scan.PreferredIntensities.Length == 0) ? 0.0 : scan.PreferredIntensities[cnt];
                        double noise = (scan.PreferredNoises.Length == 0) ? 0.0 : scan.PreferredNoises[cnt];

                        writer.WriteLine("{0},{1},{2},{3},{4},{5},{6},{7}", startNum.ToString(), rt.ToString(),
                            msval.ToString(), resolution.ToString(), intensity.ToString(), noise.ToString(),
                            scanFilter.ToString(), levelInfo);
                    }
                    cnt += 1;
                }
                startNum += 1;
            }
            writer.Close();

        }

        public static void getMSdataFromRT(double startRT, int endRT, string filePath, double lowMS = 0.00, double highMS = 0.00)
        {
            IRawDataPlus plus = RawFileReaderAdapter.FileFactory(filePath);

            plus.SelectInstrument(Device.MS, 1);
            if (highMS - 0.00 <= 0.01)
            {
                highMS = plus.RunHeaderEx.HighMass;
            }
            int startNum = plus.ScanNumberFromRetentionTime(startRT);
            int endNum = plus.ScanNumberFromRetentionTime(endRT);
            int maxNum = plus.RunHeaderEx.LastSpectrum > endNum ? endNum : plus.RunHeaderEx.LastSpectrum;

            string fileName = DateTime.Now.ToLocalTime().ToString().Replace("/", "").Replace(":", "").Replace(" ", "_") + ".csv";
            FileStream stream = new FileStream(fileName, FileMode.Create, FileAccess.Write);
            StreamWriter writer = new StreamWriter(stream, Encoding.UTF8);
            writer.WriteLine("ScanNumber, RT, Mass, Resolution, Intensity, Noise, Filter");
            while (startNum <= endNum)
            {
                Scan scan = Scan.FromFile(plus, startNum);
                string filter = plus.GetFilterForScanNumber(startNum).ToString();
                int cnt = 0;
                while (cnt < scan.PreferredMasses.Length)
                {
                    if (lowMS < scan.PreferredMasses[cnt] && scan.PreferredMasses[cnt] < highMS)
                    {
                        double rt = plus.RetentionTimeFromScanNumber(startNum);
                        double msval = scan.PreferredMasses[cnt];
                        double resolution = (scan.PreferredResolutions.Length == 0) ? 0.0 : scan.PreferredResolutions[cnt];
                        double intensity = (scan.PreferredIntensities.Length == 0) ? 0.0 : scan.PreferredIntensities[cnt];
                        double noise = (scan.PreferredNoises.Length == 0) ? 0.0 : scan.PreferredNoises[cnt];
                        writer.WriteLine("{0},{1},{2},{3},{4},{5},{6}", startNum.ToString(), rt.ToString(), msval.ToString(), resolution.ToString(), intensity.ToString(), noise.ToString(), filter.ToString());
                    }
                    cnt += 1;
                }
                startNum += 1;
            }
            writer.Close();
        }