예제 #1
0
        private void SaveDataToCdf(MsSpectrum spec, string fpath)
        {
            DataSetFactory.SearchFolder("Microsoft.Research.Science.Data.NetCDF4.dll");
            DataSetFactory.SearchFolder("Microsoft.Research.Science.Data.dll");
            DataSetFactory.SearchFolder("Microsoft.Research.Science.Data.Imperative.dll");

            int    nControllerType = 0;                      // 0 == mass spec device
            int    nContorllerNumber = 1;                    // first MS device
            int    totalNumScans = 0;                        // Number of scans
            int    firstScanNumber = 0, lastScanNumber = -1; // Number of first and last scan
            string scanFilter             = null;            // Scan filter line
            int    numDataPoints          = -1;              // points in both the m/z and intensity arrays
            double retentionTimeInMinutes = -1;
            double minObservedMZ          = -1;
            double maxObservedMZ          = -1;
            double totalIonCurrent        = -1;
            double basePeakMZ             = -1;
            double basePeakIntensity      = -1;
            int    channel     = 0; // unused
            int    uniformTime = 0; // unused
            double frequency   = 0; // unused

            int      arraySize            = -1;
            object   rawData              = null; // rawData wil come as Double[,]
            object   peakFlags            = null;
            string   szFilter             = null; // No filter
            int      intensityCutoffType  = 1;    // No cutoff
            int      intensityCutoffValue = 0;    // No cutoff
            int      maxNumberOfPeaks     = 0;    // 0 : return all data peaks
            double   centroidPeakWidth    = 0;    // No centroiding
            int      centroidThisScan     = 0;    // No centroiding
            DateTime creationDate         = DateTime.MinValue;

            var scanPath = Path.Combine(fpath, $"{spec.Id}.cdf");

            var cdf = DataSet.Open(scanPath);

            cdf.IsAutocommitEnabled = false;
            AddMetadata(cdf);
            IXRawfile5 rawFile = (IXRawfile5) new MSFileReader_XRawfile();

            rawFile.Open(spec.RawFilePath);
            rawFile.SetCurrentController(nControllerType, nContorllerNumber);
            rawFile.GetNumSpectra(ref totalNumScans);

            rawFile.GetFirstSpectrumNumber(ref firstScanNumber);
            rawFile.GetLastSpectrumNumber(ref lastScanNumber);
            rawFile.GetCreationDate(ref creationDate);


            var massVar           = cdf.Add <double>("mass_values", new[] { "point_number" });
            var intensities       = cdf.Add <Int32>("intensity_values", new[] { "point_number" });
            var acqTimeVar        = cdf.Add <double>("scan_acquisition_time", new[] { "scan_number" });
            var scIndexVar        = cdf.Add <int>("scan_index", new[] { "scan_number" });
            var scCountVar        = cdf.Add <int>("point_count", new[] { "scan_number" });
            var totalIntensityVar = cdf.Add <double>("total_intensity", new[] { "scan_number" });

            massVar.Metadata["scale_factor"]     = 1.0;
            intensities.Metadata["scale_factor"] = 1.0;

            try
            {
                var retTimes   = new double[lastScanNumber];
                var ticArr     = new double[lastScanNumber];
                var scanIndex  = new Int32[lastScanNumber];
                var pointCount = new Int32[lastScanNumber];

                for (int curScanNum = firstScanNumber; curScanNum <= lastScanNumber; curScanNum++)
                {
                    rawFile.GetScanHeaderInfoForScanNum(curScanNum,
                                                        ref numDataPoints,
                                                        ref retentionTimeInMinutes,
                                                        ref minObservedMZ,
                                                        ref maxObservedMZ,
                                                        ref totalIonCurrent,
                                                        ref basePeakMZ,
                                                        ref basePeakIntensity,
                                                        ref channel,     // unused
                                                        ref uniformTime, // unused
                                                        ref frequency    // unused
                                                        );
                    scanFilter = null;
                    rawFile.GetFilterForScanNum(curScanNum, ref scanFilter);

                    peakFlags            = null;
                    centroidPeakWidth    = 0;
                    arraySize            = 0;
                    intensityCutoffType  = 1;
                    intensityCutoffValue = (int)(0.001 * basePeakIntensity);
                    rawFile.GetMassListFromScanNum(
                        ref curScanNum,
                        szFilter,              // filter
                        intensityCutoffType,   // intensityCutoffType
                        intensityCutoffValue,  // intensityCutoffValue
                        maxNumberOfPeaks,      // maxNumberOfPeaks
                        centroidThisScan,      // centroid result?
                        ref centroidPeakWidth, // centroidingPeakWidth
                        ref rawData,           // daw data
                        ref peakFlags,         // peakFlags
                        ref arraySize);        // array size

                    ;
                    var datArray = (Array)rawData;
                    var massArr  = new double[arraySize];
                    var intArr   = new int[arraySize];
                    for (var j = 0; j < arraySize; j++)
                    {
                        massArr[j] = (double)datArray.GetValue(0, j);
                        intArr[j]  = Convert.ToInt32(datArray.GetValue(1, j));
                    }
                    //Console.Write($"curScan = {curScanNum}\n");
                    var ind = curScanNum - 1;
                    pointCount[ind] = arraySize;
                    if (ind == 0)
                    {
                        scanIndex[ind]     = 0;
                        scanIndex[ind + 1] = arraySize;
                    }
                    else
                    {
                        scanIndex[ind] += scanIndex[ind - 1];
                        if (ind + 1 < lastScanNumber)
                        {
                            scanIndex[ind + 1] = arraySize;
                        }
                    }
                    retTimes[ind] = TimeSpan.FromMinutes(retentionTimeInMinutes).TotalSeconds;
                    ticArr[ind]   = totalIonCurrent;
                    massVar.Append(massArr);
                    intensities.Append(intArr);
                    rawData = null;
                }

                totalIntensityVar.PutData(ticArr);
                scIndexVar.PutData(scanIndex);
                acqTimeVar.PutData(retTimes);
                scCountVar.PutData(pointCount);

                cdf.Commit();
            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message);
            }
            finally
            {
                cdf.Dispose();
                rawFile.Close();
            }
        }
예제 #2
0
        private void SleepT()
        {
            if (filedirectory_array.Length != 0)
            {
                #region
                for (int i_filedirectory = 0; i_filedirectory < pathway.Items.Count; i_filedirectory++)
                {
                    SetTextMessage_rawfile_num(i_filedirectory + 1);

                    //Utilize the MSFileReader to obtain the information from the DIA data;
                    filedirectory = (string)pathway.Items[i_filedirectory];
                    MSFileReader_XRawfile rawfile  = new MSFileReader_XRawfile();
                    IXRawfile5            rawfile5 = (IXRawfile5) new MSFileReader_XRawfile();
                    string rawpath = filedirectory;
                    rawfile.Open(rawpath);
                    rawfile.SetCurrentController(0, 1);
                    string rawpath5 = filedirectory;
                    rawfile5.Open(rawpath5);
                    rawfile5.SetCurrentController(0, 1);

                    string filename    = Path.GetFileName(filedirectory);
                    string newfilepath = Path.GetDirectoryName(filedirectory);
                    #region
                    if (extracttype == "MS1MS2Data")
                    {
                        //creat the output file names
                        string       outfilems1 = newfilepath + "\\" + "MS1Data_" + filename + ".txt";
                        StreamWriter swms1      = File.AppendText(outfilems1);
                        string       outfilems2 = newfilepath + "\\" + "MS2Data_" + filename + ".txt";
                        StreamWriter swms2      = File.AppendText(outfilems2);

                        //write the 9 columns information in the .txt file
                        swms1.Write("ScanNum\tMSOrder\tPeakNumber\tPeakNumberAfter\tRT\tm/z\tIntensity\tNoise\tBaseline\n");
                        swms2.Write("ScanNum\tMSOrder\tPeakNumber\tPeakNumberAfter\tRT\tm/z\tIntensity\tNoise\tBaseline\n");

                        int specnum = 0;
                        rawfile5.GetNumSpectra(ref specnum);
                        for (int i = 1; i <= specnum; i++)
                        {
                            System.Threading.Thread.Sleep(0);
                            SetTextMessage(100 * i / specnum);

                            int nOrder = 0;
                            rawfile5.GetMSOrderForScanNum(i, ref nOrder);
                            double scanRT = 0;
                            rawfile5.RTFromScanNum(i, ref scanRT);
                            object varlabels = null;
                            object varflags  = null;
                            rawfile5.GetLabelData(ref varlabels, ref varflags, ref i);
                            var labels = (double[, ])varlabels;
                            int dim    = labels.GetLength(1);

                            double massNeutron      = 1.00335; // massC13 - massC12
                            double mztol            = Convert.ToDouble(ionmtoltextbox.Text);
                            double mzdecontol       = Convert.ToDouble(ionmtoltextbox.Text);
                            double ms1sonthreshold  = Convert.ToDouble(ms1sontextbox.Text);
                            double ms2sonthreshold  = Convert.ToDouble(ms2sontextbox.Text);
                            double ms1resolutiontol = Convert.ToDouble(ms1unitoltextbox.Text);
                            double ms2resolutiontol = Convert.ToDouble(ms2unitoltextbox.Text);

                            if (nOrder == 1)
                            {
                                swms1.Write("{0}\t", i);
                                swms1.Write("{0}\t", nOrder);
                                swms1.Write("{0}\t", dim);

                                List <double> ms1mzlist        = new List <double>();
                                List <double> ms1intensitylist = new List <double>();
                                List <double> ms1chargelist    = new List <double>();
                                List <double> ms1noiselist     = new List <double>();
                                List <double> ms1baselinelist  = new List <double>();

                                for (int inx = 0; inx < dim; inx++)
                                {
                                    double dMassjms1 = labels[0, inx];
                                    double dInms1    = labels[1, inx];
                                    double dBasems1  = labels[3, inx];
                                    double dNoisems1 = labels[4, inx];
                                    double dChams1   = labels[5, inx];
                                    double sonms1    = 0;
                                    if (ms1rms_checkbox == "ms1rms")
                                    {
                                        //if checked, the S/N is changed to RMS S/N(root mean square signal over noise);
                                        sonms1 = (dInms1 - dBasems1) / (dNoisems1 - dBasems1);
                                    }
                                    else
                                    {
                                        sonms1 = dInms1 / dNoisems1;
                                    }

                                    if (sonms1 >= ms1sonthreshold)
                                    {
                                        ms1chargelist.Add(dChams1);
                                        ms1mzlist.Add(dMassjms1);
                                        ms1intensitylist.Add(dInms1);
                                        ms1noiselist.Add(dNoisems1);
                                        ms1baselinelist.Add(dBasems1);
                                    }
                                    else
                                    {
                                        //if the S/N is too low, set their values to zero
                                        ms1chargelist.Add(0);
                                        ms1mzlist.Add(0);
                                        ms1intensitylist.Add(0);
                                        ms1noiselist.Add(0);
                                        ms1baselinelist.Add(0);
                                    }
                                }

                                //remove the peaks, whose values of (m/z, intensity, charge, noise, baseline) are equal to zero;
                                List <int> ms1mzzeroindex = new List <int>();
                                for (int izero = 0; izero < ms1mzlist.Count; izero++)
                                {
                                    if (ms1mzlist[izero] == 0)
                                    {
                                        ms1mzzeroindex.Add(izero);
                                    }
                                }
                                for (int izero1 = 0; izero1 < ms1mzzeroindex.Count(); izero1++)
                                {
                                    ms1mzlist.RemoveAt(ms1mzzeroindex[izero1] - izero1);
                                    ms1intensitylist.RemoveAt(ms1mzzeroindex[izero1] - izero1);
                                    ms1baselinelist.RemoveAt(ms1mzzeroindex[izero1] - izero1);
                                    ms1noiselist.RemoveAt(ms1mzzeroindex[izero1] - izero1);
                                    ms1chargelist.RemoveAt(ms1mzzeroindex[izero1] - izero1);
                                }

                                //keep only one peak in a tolerance;
                                for (int ims1reso = 0; ims1reso < ms1mzlist.Count; ims1reso++)
                                {
                                    if (ms1mzlist[ims1reso] != 0)
                                    {
                                        double ms1mzreso        = ms1mzlist[ims1reso] * ms1intensitylist[ims1reso];
                                        double ms1intensityreso = ms1intensitylist[ims1reso];
                                        double ms1baselinereso  = ms1baselinelist[ims1reso];
                                        for (int jms1reso = ims1reso + 1; jms1reso < ms1mzlist.Count; jms1reso++)
                                        {
                                            double ms1resodiff = ms1mzlist[jms1reso] - ms1mzlist[jms1reso - 1];
                                            if (ms1resodiff > ms1resolutiontol)
                                            {
                                                break;
                                            }
                                            if (ms1resodiff <= ms1resolutiontol)
                                            {
                                                ms1mzreso                  = ms1mzreso + ms1mzlist[jms1reso] * ms1intensitylist[jms1reso];
                                                ms1intensityreso           = ms1intensityreso + ms1intensitylist[jms1reso];
                                                ms1intensitylist[jms1reso] = 0;
                                            }
                                        }
                                        ms1mzlist[ims1reso]        = ms1mzreso / ms1intensityreso;
                                        ms1intensitylist[ims1reso] = ms1intensityreso;
                                    }
                                }

                                List <int> ms1intensityzeroafterresoindex = new List <int>();
                                for (int izero = 0; izero < ms1mzlist.Count; izero++)
                                {
                                    if (ms1intensitylist[izero] == 0)
                                    {
                                        ms1intensityzeroafterresoindex.Add(izero);
                                    }
                                }
                                for (int izero1 = 0; izero1 < ms1intensityzeroafterresoindex.Count(); izero1++)
                                {
                                    ms1mzlist.RemoveAt(ms1intensityzeroafterresoindex[izero1] - izero1);
                                    ms1intensitylist.RemoveAt(ms1intensityzeroafterresoindex[izero1] - izero1);
                                    ms1baselinelist.RemoveAt(ms1intensityzeroafterresoindex[izero1] - izero1);
                                    ms1noiselist.RemoveAt(ms1intensityzeroafterresoindex[izero1] - izero1);
                                    ms1chargelist.RemoveAt(ms1intensityzeroafterresoindex[izero1] - izero1);
                                }

                                //if checked, the peaks are deisotoped, which could remove the isotope peaks;
                                if (deisotope_checkbox == "deisotope")
                                {
                                    List <int> ms1deisotopindexbefore = new List <int>();

                                    for (int ims1 = 0; ims1 < ms1mzlist.Count; ims1++)
                                    {
                                        if (ms1chargelist[ims1] != 0)
                                        {
                                            for (int jms1 = ims1 + 1; jms1 < ms1mzlist.Count; jms1++)
                                            {
                                                double ms1mzdiff   = ms1mzlist[jms1] - ms1mzlist[ims1];
                                                double ms1tolvalue = Math.Abs(massNeutron / ms1chargelist[ims1] - ms1mzdiff);
                                                if (ms1tolvalue < mztol && ms1intensitylist[jms1] < ms1intensitylist[ims1])
                                                {
                                                    ms1deisotopindexbefore.Add(jms1);
                                                }
                                            }
                                        }
                                    }

                                    List <int> ms1deisotopindex = new List <int>();
                                    foreach (int ms1deiso in ms1deisotopindexbefore)
                                    {
                                        if (!ms1deisotopindex.Contains(ms1deiso))
                                        {
                                            ms1deisotopindex.Add(ms1deiso);
                                        }
                                    }
                                    ms1deisotopindex.Sort();
                                    for (int ide1 = 0; ide1 < ms1deisotopindex.Count; ide1++)
                                    {
                                        ms1mzlist.RemoveAt(ms1deisotopindex[ide1] - ide1);
                                        ms1intensitylist.RemoveAt(ms1deisotopindex[ide1] - ide1);
                                        ms1baselinelist.RemoveAt(ms1deisotopindex[ide1] - ide1);
                                        ms1noiselist.RemoveAt(ms1deisotopindex[ide1] - ide1);
                                        ms1chargelist.RemoveAt(ms1deisotopindex[ide1] - ide1);
                                    }
                                }

                                //if checked, the peaks are deconvoluted to one charge;
                                if (deconvolution_checkbox == "deconvolution")
                                {
                                    for (int idems1 = 0; idems1 < ms1mzlist.Count; idems1++)
                                    {
                                        if (ms1chargelist[idems1] != 0)
                                        {
                                            ms1mzlist[idems1] = ms1mzlist[idems1] * ms1chargelist[idems1] - ms1chargelist[idems1] * 1.0079;
                                        }
                                    }
                                    List <int> ms1deconvolutionindexbefore = new List <int>();
                                    for (int ideconms1 = 0; ideconms1 < ms1mzlist.Count; ideconms1++)
                                    {
                                        if (ms1mzlist[ideconms1] != 0)
                                        {
                                            double ms1mzdecon        = ms1mzlist[ideconms1];
                                            double ms1intensitydecon = ms1intensitylist[ideconms1];
                                            double ms1baselinedecon  = ms1baselinelist[ideconms1];
                                            int    ims1deconcount    = 1;
                                            for (int jdeconms1 = ideconms1 + 1; jdeconms1 < ms1mzlist.Count; jdeconms1++)
                                            {
                                                double ms1mzdecondiff = Math.Abs(ms1mzlist[jdeconms1] - ms1mzlist[ideconms1]);
                                                if (ms1mzdecondiff <= mzdecontol)
                                                {
                                                    ms1deconvolutionindexbefore.Add(jdeconms1);
                                                    ms1mzdecon        = ms1mzdecon + ms1mzlist[jdeconms1];
                                                    ms1intensitydecon = ms1intensitydecon + ms1intensitylist[jdeconms1];
                                                    ims1deconcount++;
                                                }
                                            }
                                            ms1mzlist[ideconms1]        = ms1mzdecon / ims1deconcount;
                                            ms1intensitylist[ideconms1] = ms1intensitydecon;
                                        }
                                    }

                                    List <int> ms1deconvolutionindex = new List <int>();
                                    foreach (int ms1deiso in ms1deconvolutionindexbefore)
                                    {
                                        if (!ms1deconvolutionindex.Contains(ms1deiso))
                                        {
                                            ms1deconvolutionindex.Add(ms1deiso);
                                        }
                                    }
                                    ms1deconvolutionindex.Sort();
                                    for (int ide1 = 0; ide1 < ms1deconvolutionindex.Count; ide1++)
                                    {
                                        ms1mzlist.RemoveAt(ms1deconvolutionindex[ide1] - ide1);
                                        ms1intensitylist.RemoveAt(ms1deconvolutionindex[ide1] - ide1);
                                        ms1baselinelist.RemoveAt(ms1deconvolutionindex[ide1] - ide1);
                                        ms1noiselist.RemoveAt(ms1deconvolutionindex[ide1] - ide1);
                                        ms1chargelist.RemoveAt(ms1deconvolutionindex[ide1] - ide1);
                                    }
                                }

                                //write the data into the .txt file;
                                swms1.Write("{0}\t", ms1mzlist.Count);
                                swms1.Write("{0}\t", scanRT);
                                for (int ims1mz = 0; ims1mz < ms1mzlist.Count; ims1mz++)
                                {
                                    swms1.Write("{0};", ms1mzlist[ims1mz]);
                                }
                                swms1.Write("\t");
                                for (int ims1in = 0; ims1in < ms1intensitylist.Count; ims1in++)
                                {
                                    swms1.Write("{0};", ms1intensitylist[ims1in]);
                                }
                                swms1.Write("\t");
                                for (int ims1in = 0; ims1in < ms1noiselist.Count; ims1in++)
                                {
                                    swms1.Write("{0};", ms1noiselist[ims1in]);
                                }
                                swms1.Write("\t");
                                for (int ims1base = 0; ims1base < ms1baselinelist.Count; ims1base++)
                                {
                                    swms1.Write("{0};", ms1baselinelist[ims1base]);
                                }
                                swms1.Write("\n");
                            }
                            else //extract the ms/ms data, the process is similar with above;
                            {
                                swms2.Write("{0}\t", i);
                                swms2.Write("{0}\t", nOrder);
                                swms2.Write("{0}\t", dim);

                                List <double> ms2mzlist        = new List <double>();
                                List <double> ms2intensitylist = new List <double>();
                                List <double> ms2noiselist     = new List <double>();
                                List <double> ms2chargelist    = new List <double>();
                                List <double> ms2baselinelist  = new List <double>();


                                for (int inx = 0; inx < dim; inx++)
                                {
                                    double dMassjms2 = labels[0, inx];
                                    double dInms2    = labels[1, inx];
                                    double dBasems2  = labels[3, inx];
                                    double dNoisems2 = labels[4, inx];
                                    double dChams2   = labels[5, inx];
                                    double sonms2    = 0;
                                    if (ms2rms_checkbox == "ms2rms")
                                    {
                                        sonms2 = (dInms2 - dBasems2) / (dNoisems2 - dBasems2);
                                    }
                                    else
                                    {
                                        sonms2 = dInms2 / dNoisems2;
                                    }

                                    if (sonms2 >= ms2sonthreshold)
                                    {
                                        ms2chargelist.Add(dChams2);
                                        ms2mzlist.Add(dMassjms2);
                                        ms2intensitylist.Add(dInms2);
                                        ms2noiselist.Add(dNoisems2);
                                        ms2baselinelist.Add(dBasems2);
                                    }
                                    else
                                    {
                                        ms2chargelist.Add(0);
                                        ms2mzlist.Add(0);
                                        ms2intensitylist.Add(0);
                                        ms2noiselist.Add(0);
                                        ms2baselinelist.Add(0);
                                    }
                                }

                                List <int> ms2mzzeroindex = new List <int>();
                                for (int izero = 0; izero < ms2mzlist.Count; izero++)
                                {
                                    if (ms2mzlist[izero] == 0)
                                    {
                                        ms2mzzeroindex.Add(izero);
                                    }
                                }
                                for (int izero1 = 0; izero1 < ms2mzzeroindex.Count(); izero1++)
                                {
                                    ms2mzlist.RemoveAt(ms2mzzeroindex[izero1] - izero1);
                                    ms2intensitylist.RemoveAt(ms2mzzeroindex[izero1] - izero1);
                                    ms2baselinelist.RemoveAt(ms2mzzeroindex[izero1] - izero1);
                                    ms2noiselist.RemoveAt(ms2mzzeroindex[izero1] - izero1);
                                    ms2chargelist.RemoveAt(ms2mzzeroindex[izero1] - izero1);
                                }

                                for (int ims2reso = 0; ims2reso < ms2mzlist.Count; ims2reso++)
                                {
                                    if (ms2mzlist[ims2reso] != 0)
                                    {
                                        double ms2mzreso        = ms2mzlist[ims2reso] * ms2intensitylist[ims2reso];
                                        double ms2intensityreso = ms2intensitylist[ims2reso];
                                        double ms2baselinereso  = ms2baselinelist[ims2reso];
                                        int    iresocount       = 1;
                                        for (int jms2reso = ims2reso + 1; jms2reso < ms2mzlist.Count; jms2reso++)
                                        {
                                            double ms2resodiff = ms2mzlist[jms2reso] - ms2mzlist[jms2reso - 1];
                                            if (ms2resodiff > ms2resolutiontol)
                                            {
                                                break;
                                            }
                                            if (ms2resodiff <= ms2resolutiontol)
                                            {
                                                ms2mzreso                  = ms2mzreso + ms2mzlist[jms2reso] * ms2intensitylist[jms2reso];
                                                ms2intensityreso           = ms2intensityreso + ms2intensitylist[jms2reso];
                                                ms2intensitylist[jms2reso] = 0;
                                                iresocount++;
                                            }
                                        }
                                        ms2mzlist[ims2reso]        = ms2mzreso / ms2intensityreso;
                                        ms2intensitylist[ims2reso] = ms2intensityreso;
                                    }
                                }

                                List <int> ms2intensityzeroafterresoindex = new List <int>();
                                for (int izero = 0; izero < ms2mzlist.Count; izero++)
                                {
                                    if (ms2intensitylist[izero] == 0)
                                    {
                                        ms2intensityzeroafterresoindex.Add(izero);
                                    }
                                }
                                for (int izero1 = 0; izero1 < ms2intensityzeroafterresoindex.Count(); izero1++)
                                {
                                    ms2mzlist.RemoveAt(ms2intensityzeroafterresoindex[izero1] - izero1);
                                    ms2intensitylist.RemoveAt(ms2intensityzeroafterresoindex[izero1] - izero1);
                                    ms2baselinelist.RemoveAt(ms2intensityzeroafterresoindex[izero1] - izero1);
                                    ms2noiselist.RemoveAt(ms2intensityzeroafterresoindex[izero1] - izero1);
                                    ms2chargelist.RemoveAt(ms2intensityzeroafterresoindex[izero1] - izero1);
                                }

                                if (deisotope_checkbox == "deisotope")
                                {
                                    List <int> ms2deisotopindexbefore = new List <int>();
                                    for (int ims2 = 0; ims2 < ms2mzlist.Count(); ims2++)
                                    {
                                        if (ms2chargelist[ims2] != 0)
                                        {
                                            for (int jms2 = ims2 + 1; jms2 < ms2mzlist.Count(); jms2++)
                                            {
                                                double ms2mzdiff   = ms2mzlist[jms2] - ms2mzlist[ims2];
                                                double ms2tolvalue = Math.Abs(massNeutron / ms2chargelist[ims2] - ms2mzdiff);
                                                if (ms2tolvalue <= mztol && ms2intensitylist[jms2] < ms2intensitylist[ims2])
                                                {
                                                    ms2deisotopindexbefore.Add(jms2);
                                                }
                                            }
                                        }
                                    }

                                    List <int> ms2deisotopindex = new List <int>();
                                    foreach (int ms2deiso in ms2deisotopindexbefore)
                                    {
                                        if (!ms2deisotopindex.Contains(ms2deiso))
                                        {
                                            ms2deisotopindex.Add(ms2deiso);
                                        }
                                    }
                                    ms2deisotopindex.Sort();
                                    for (int ide2 = 0; ide2 < ms2deisotopindex.Count(); ide2++)
                                    {
                                        ms2mzlist.RemoveAt(ms2deisotopindex[ide2] - ide2);
                                        ms2intensitylist.RemoveAt(ms2deisotopindex[ide2] - ide2);
                                        ms2noiselist.RemoveAt(ms2deisotopindex[ide2] - ide2);
                                        ms2baselinelist.RemoveAt(ms2deisotopindex[ide2] - ide2);
                                        ms2chargelist.RemoveAt(ms2deisotopindex[ide2] - ide2);
                                    }
                                }

                                if (deconvolution_checkbox == "deconvolution")
                                {
                                    for (int idems2 = 0; idems2 < ms2mzlist.Count; idems2++)
                                    {
                                        if (ms2chargelist[idems2] != 0)
                                        {
                                            ms2mzlist[idems2] = ms2mzlist[idems2] * ms2chargelist[idems2] - (ms2chargelist[idems2] - 1) * 1.0079;
                                        }
                                    }
                                    List <int> ms2deconvolutionindexbefore = new List <int>();
                                    for (int ideconms2 = 0; ideconms2 < ms2mzlist.Count; ideconms2++)
                                    {
                                        if (ms2mzlist[ideconms2] != 0)
                                        {
                                            double ms2mzdecon        = ms2mzlist[ideconms2];
                                            double ms2intensitydecon = ms2intensitylist[ideconms2];
                                            int    ims2deconcount    = 1;
                                            for (int jdeconms2 = ideconms2 + 1; jdeconms2 < ms2mzlist.Count; jdeconms2++)
                                            {
                                                double ms2mzdecondiff = Math.Abs(ms2mzlist[jdeconms2] - ms2mzlist[ideconms2]);
                                                if (ms2mzdecondiff <= mzdecontol)
                                                {
                                                    ms2deconvolutionindexbefore.Add(jdeconms2);
                                                    ms2mzdecon        = ms2mzdecon + ms2mzlist[jdeconms2];
                                                    ms2intensitydecon = ms2intensitydecon + ms2intensitylist[jdeconms2];
                                                    ims2deconcount++;
                                                }
                                            }
                                            ms2mzlist[ideconms2]        = ms2mzdecon / ims2deconcount;
                                            ms2intensitylist[ideconms2] = ms2intensitydecon;
                                        }
                                    }
                                    List <int> ms2deconvolutionindex = new List <int>();
                                    foreach (int ms2deiso in ms2deconvolutionindexbefore)
                                    {
                                        if (!ms2deconvolutionindex.Contains(ms2deiso))
                                        {
                                            ms2deconvolutionindex.Add(ms2deiso);
                                        }
                                    }
                                    ms2deconvolutionindex.Sort();
                                    for (int ide2 = 0; ide2 < ms2deconvolutionindex.Count; ide2++)
                                    {
                                        ms2mzlist.RemoveAt(ms2deconvolutionindex[ide2] - ide2);
                                        ms2intensitylist.RemoveAt(ms2deconvolutionindex[ide2] - ide2);
                                        ms2baselinelist.RemoveAt(ms2deconvolutionindex[ide2] - ide2);
                                        ms2noiselist.RemoveAt(ms2deconvolutionindex[ide2] - ide2);
                                        ms2chargelist.RemoveAt(ms2deconvolutionindex[ide2] - ide2);
                                    }
                                }

                                swms2.Write("{0}\t", ms2mzlist.Count);
                                swms2.Write("{0}\t", scanRT);

                                for (int ims2mz = 0; ims2mz < ms2mzlist.Count; ims2mz++)
                                {
                                    swms2.Write("{0};", ms2mzlist[ims2mz]);
                                }
                                swms2.Write("\t");
                                for (int ims2in = 0; ims2in < ms2intensitylist.Count; ims2in++)
                                {
                                    swms2.Write("{0};", ms2intensitylist[ims2in]);
                                }
                                swms2.Write("\t");
                                for (int ims2noise = 0; ims2noise < ms2noiselist.Count; ims2noise++)
                                {
                                    swms2.Write("{0};", ms2noiselist[ims2noise]);
                                }
                                swms2.Write("\t");
                                for (int ims2base = 0; ims2base < ms2baselinelist.Count; ims2base++)
                                {
                                    swms2.Write("{0};", ms2baselinelist[ims2base]);
                                }
                                swms2.Write("\n");
                            }
                        }
                        swms1.Flush();
                        swms1.Close();
                        swms2.Flush();
                        swms2.Close();
                    }
                    #endregion
                    #region
                    else if (extracttype == "LocalData(SMPR)")
                    {
                        // creat a new file name for "LocalData(SMPR)";
                        string       outfile = newfilepath + "\\" + "LocalData(SMPR)_" + filename + ".txt";
                        StreamWriter sw      = File.AppendText(outfile);

                        sw.Write("ScanNum\tMSOrder\tPeakNumber\tRT\n");

                        int specnum = 0;
                        rawfile.GetNumSpectra(ref specnum);
                        for (int i = 1; i <= specnum; i++)
                        {
                            System.Threading.Thread.Sleep(0);
                            SetTextMessage(100 * i / specnum);

                            int nOrder = 0;
                            rawfile5.GetMSOrderForScanNum(i, ref nOrder);
                            double scanRT = 0;
                            rawfile.RTFromScanNum(i, ref scanRT);

                            sw.Write("{0}\t", i);
                            sw.Write("{0}\t", nOrder);

                            object varlabels = null;
                            object varflags  = null;
                            rawfile5.GetLabelData(ref varlabels, ref varflags, ref i);
                            var labels = (double[, ])varlabels;
                            int dim    = labels.GetLength(1);
                            sw.Write("{0}\t", dim);
                            sw.Write("{0}\n", scanRT);
                        }
                        sw.Flush();
                        sw.Close();
                    }
                    //
                    else
                    {
                        MessageBox.Show("Please Select a type below!");
                    }
                    #endregion
                }
                MessageBox.Show("Extract Done!");
            }
            else
            {
                MessageBox.Show("Please Select a File!");
            }
            #endregion
        }