Exemple #1
0
        public void ExcelOut(List <MatchedData> lst, string sFileName)
        {
            System.Reflection.Missing miss = System.Reflection.Missing.Value;
            MatchedData matchecdData       = new MatchedData();

            Excel.Application excelApplication = new Excel.Application();
            Excel.Workbook    excelWorkbook    = excelApplication.Workbooks.Add();
            excelApplication.UserControl = true;
            excelApplication.Application.DisplayAlerts = false;

            Excel.Worksheet excelWorksheet = (Excel.Worksheet)excelWorkbook.Sheets.Add(miss, miss, miss, miss);
            excelWorksheet.Name = "Protein";

            int iRow = 0;

            try
            {
                for (int i = excelWorkbook.Sheets.Count; i >= 1; i--)
                {
                    Excel.Worksheet excelWorksheetTemp = (Excel.Worksheet)excelWorkbook.Sheets[i];
                    if (excelWorksheetTemp.Name != "Protein")
                    {
                        excelWorksheetTemp.Delete();
                    }
                }

                //Header
                iRow = 1;
                excelWorksheet.Cells[iRow, 1]  = "Index";
                excelWorksheet.Cells[iRow, 2]  = "ID";
                excelWorksheet.Cells[iRow, 3]  = "MOD_RES";
                excelWorksheet.Cells[iRow, 4]  = "PTM_Score";
                excelWorksheet.Cells[iRow, 5]  = "TotalPTM_Score";
                excelWorksheet.Cells[iRow, 6]  = "Z";
                excelWorksheet.Cells[iRow, 7]  = "DatasetFile";
                excelWorksheet.Cells[iRow, 8]  = "Database";
                excelWorksheet.Cells[iRow, 9]  = "Repository";
                excelWorksheet.Cells[iRow, 10] = "ScreeningApproach";
                excelWorksheet.Cells[iRow, 11] = "ExpMZ";
                excelWorksheet.Cells[iRow, 12] = "TheoMZ";
                excelWorksheet.Cells[iRow, 13] = "Error_PPM";
                excelWorksheet.Cells[iRow, 14] = "AccessionNumber";
                excelWorksheet.Cells[iRow, 15] = "ProteinName";
                excelWorksheet.Cells[iRow, 16] = "Length";
                excelWorksheet.Cells[iRow, 17] = "Function";
                excelWorksheet.Cells[iRow, 18] = "Sequence";
                excelWorksheet.Cells[iRow, 19] = "NMFs";
                excelWorksheet.Cells[iRow, 20] = "Actual_PMFs";

                //Value
                iRow = 2;
                for (int i = 0; i < lst.Count; i++)
                {
                    matchecdData = lst[i];

                    excelWorksheet.Cells[iRow, 1]  = matchecdData.Index;
                    excelWorksheet.Cells[iRow, 2]  = matchecdData.ID;
                    excelWorksheet.Cells[iRow, 3]  = matchecdData.MOD_RES.Replace(";", "").Replace(",", "");
                    excelWorksheet.Cells[iRow, 4]  = matchecdData.DataBase_PTM_SCORE;
                    excelWorksheet.Cells[iRow, 5]  = matchecdData.Total_PTM_Score;
                    excelWorksheet.Cells[iRow, 6]  = matchecdData.Z;
                    excelWorksheet.Cells[iRow, 7]  = matchecdData.DatasetFile;
                    excelWorksheet.Cells[iRow, 8]  = matchecdData.Database;
                    excelWorksheet.Cells[iRow, 9]  = matchecdData.Repository;
                    excelWorksheet.Cells[iRow, 10] = matchecdData.ScreeningApproach;
                    excelWorksheet.Cells[iRow, 11] = matchecdData.ExpMZ;
                    excelWorksheet.Cells[iRow, 12] = matchecdData.TheoMZ;
                    excelWorksheet.Cells[iRow, 13] = matchecdData.Error_PPM;
                    excelWorksheet.Cells[iRow, 14] = matchecdData.AccessionNumber;
                    excelWorksheet.Cells[iRow, 15] = matchecdData.ProteinName;
                    excelWorksheet.Cells[iRow, 16] = matchecdData.Length;
                    excelWorksheet.Cells[iRow, 17] = matchecdData.Function;
                    excelWorksheet.Cells[iRow, 18] = matchecdData.Sequence;
                    excelWorksheet.Cells[iRow, 19] = matchecdData.NMFS;
                    excelWorksheet.Cells[iRow, 20] = matchecdData.Actualpmfs;

                    iRow++;
                }

                excelWorkbook.SaveAs(sFileName, miss, miss, miss, miss, miss, Excel.XlSaveAsAccessMode.xlNoChange, miss, miss, miss, miss, miss);
                excelWorkbook.Close(false, miss, miss);
            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
                matchecdData = null;

                if (excelApplication != null)
                {
                    excelApplication.Workbooks.Close();
                    excelApplication.Quit();
                    System.Runtime.InteropServices.Marshal.ReleaseComObject(excelApplication.Workbooks);
                    System.Runtime.InteropServices.Marshal.ReleaseComObject(excelApplication);
                    System.GC.Collect();
                }

                if (excelApplication != null)
                {
                    IntPtr t = new IntPtr(excelApplication.Hwnd);
                    int    k = 0;
                    GetWindowThreadProcessId(t, out k);
                    System.Diagnostics.Process p = System.Diagnostics.Process.GetProcessById(k);
                    p.Kill();
                }

                excelApplication = null;
                GC.Collect();
            }
        }
        private void SetMSChart(MatchedData MatchData,string sFileName)
        {
            double[] expX = MatchData.MatchedMsDataExp.Select(md => md.Mass).ToArray<double>();
               double[] expY = MatchData.MatchedMsDataExp.Select(md => md.IntensityPercentage * 100).ToArray<double>();

               double[] theoX = MatchData.MatchedMsDataTheo.Select(md => md.Mass).ToArray<double>();
               double[] theoY = MatchData.MatchedMsDataTheo.Select(md => md.Intensity * 100).ToArray<double>();

               double[] iPAD = MatchData.MatchedMsDataExp.Select(md => md.IPAD_R).ToArray<double>();
               double[] iPMD = MatchData.MatchedMsDataExp.Select(md => md.IPMD_R).ToArray<double>();

               double minexpX = 0d;
               double maxexpX = 0d;
               double mintheoX = 0d;
               double maxtheoX = 0d;

               if (MatchData.MatchedMsDataExp.Count != 0)
               {
               minexpX = MatchData.MatchedMsDataExp.Min(md => SetMinValue(md.Mass));
               maxexpX = MatchData.MatchedMsDataExp.Max(md => SetMaxValue(md.Mass));
               }

               if (MatchData.MatchedMsDataTheo.Count != 0)
               {
               mintheoX = MatchData.MatchedMsDataTheo.Min(md => SetMinValue(md.Mass));
               maxtheoX = MatchData.MatchedMsDataTheo.Max(md => SetMaxValue(md.Mass));
               }

               if (minexpX == 0)
               minexpX = mintheoX;

               ChartCommon chart = new ChartCommon();

               if (iPAD.Length != 0)
               {
               string chartAreasTopName = "chartAreasTop";
               chart.CreateChartAreas(chartAreasTopName);
               chart.SetChartAreasSetting(chartAreasTopName);
               chart.SetChartAreasX(chartAreasTopName, Math.Min(minexpX, mintheoX), Math.Max(maxexpX, maxtheoX));
               chart.SetChartAreasYInterval(chartAreasTopName, 100);
               chart.SetChartAreasXInterval(chartAreasTopName, 0.5);
               chart.ChartE.ChartAreas[chartAreasTopName].Position.Height = 20;
               chart.ChartE.ChartAreas[chartAreasTopName].Position.Width = 100;
               chart.ChartE.ChartAreas[chartAreasTopName].Position.X = 0;
               chart.ChartE.ChartAreas[chartAreasTopName].Position.Y = 0;
               chart.ChartE.ChartAreas[chartAreasTopName].AxisX.MajorGrid.Enabled = true;
               chart.ChartE.ChartAreas[chartAreasTopName].AxisX.MajorGrid.LineColor = Color.Transparent;
               chart.ChartE.ChartAreas[chartAreasTopName].AxisX.MajorTickMark.Enabled = true;
               chart.ChartE.ChartAreas[chartAreasTopName].AxisX.MajorTickMark.LineColor = Color.Transparent;
               chart.ChartE.ChartAreas[chartAreasTopName].AxisX.LabelStyle.Enabled = true;
               chart.ChartE.ChartAreas[chartAreasTopName].AxisX.LineColor = Color.Transparent;
               chart.ChartE.ChartAreas[chartAreasTopName].AxisX.LabelStyle.ForeColor = Color.Transparent;
               chart.ChartE.ChartAreas[chartAreasTopName].AxisY.MajorGrid.Enabled = true;
               chart.ChartE.ChartAreas[chartAreasTopName].AxisY.MajorGrid.LineColor = Color.Transparent;
               chart.ChartE.ChartAreas[chartAreasTopName].AxisY.MajorTickMark.Enabled = true;
               chart.ChartE.ChartAreas[chartAreasTopName].AxisY.MajorTickMark.LineColor = Color.Transparent;
               chart.ChartE.ChartAreas[chartAreasTopName].AxisY.LabelStyle.Enabled = true;
               chart.ChartE.ChartAreas[chartAreasTopName].AxisY.LineColor = Color.Transparent;
               chart.ChartE.ChartAreas[chartAreasTopName].AxisY.LabelStyle.ForeColor = Color.Transparent;
               chart.SetChartAreasYTitle(chartAreasTopName, "Relative Abundance (%)");
               chart.ChartE.ChartAreas[chartAreasTopName].AxisY.TitleForeColor = Color.Transparent;

               string seriesNamePMD_PAD = "iPMD_iPAD";
               chart.CreateSeries(seriesNamePMD_PAD, chartAreasTopName, SeriesChartType.Spline, "", Color.Red);
               chart.ChartE.Series[seriesNamePMD_PAD].IsVisibleInLegend = false;
               chart.ChartE.Series[seriesNamePMD_PAD].Color = Color.Transparent;

               if (theoX.Length > 1)
               {
                   chart.ChartE.Series[seriesNamePMD_PAD].Points.AddXY(theoX[0] - (theoX[1] - theoX[0]), 0);
                   chart.ChartE.Series[seriesNamePMD_PAD].Points[0].Label = "IPAD" + "\n" + "IPMD";
               }
               else
               {
                   chart.ChartE.Series[seriesNamePMD_PAD].Points.AddXY(theoX[0] - 5, 0);
                   chart.ChartE.Series[seriesNamePMD_PAD].Points[0].Label = "IPAD" + "\n" + "IPMD";
               }

               for (int x = 0; x < theoX.Length; x++)
               {
                   chart.ChartE.Series[seriesNamePMD_PAD].Points.AddXY(theoX[x], 0);

                   if (iPMD[x] > -10000)
                       chart.ChartE.Series[seriesNamePMD_PAD].Points[x + 1].Label = ((int)iPAD[x]).ToString() + "\n" + ((int)iPMD[x]).ToString();
                   else
                       chart.ChartE.Series[seriesNamePMD_PAD].Points[x + 1].Label = "".ToString() + "\n" + "".ToString();
               }
               }

               string chartAreasName = "chartAreas";
               chart.CreateChartAreas(chartAreasName);
               chart.SetChartAreasSetting(chartAreasName);

               if (MatchData.MatchedMsDataExp.Count != 0)
               chart.SetChartAreasX(chartAreasName, Math.Min(minexpX, mintheoX), Math.Max(maxexpX, maxtheoX));
               else
               chart.SetChartAreasX(chartAreasName, mintheoX, maxtheoX);

               chart.SetChartAreasY(chartAreasName, 0, 100);
               chart.SetChartAreasSetting(chartAreasName);
               chart.SetChartAreasXInterval(chartAreasName, 0.5);
               chart.SetChartAreasYInterval(chartAreasName, 20);
               chart.SetChartAreasXTitle(chartAreasName, "m/z");
               chart.SetChartAreasYTitle(chartAreasName, "Relative Abundance (%)");
               chart.SetChartAreasXMajorTickMark(chartAreasName, true);
               chart.SetChartAreasYMajorTickMark(chartAreasName, true);

               chart.ChartE.ChartAreas[chartAreasName].Position.Height = 80;
               chart.ChartE.ChartAreas[chartAreasName].Position.Width = 100;
               chart.ChartE.ChartAreas[chartAreasName].Position.Y = 20;

               string legendName = "legend";
               chart.CreateLegend(legendName);

               if (MatchData.MatchedMsDataExp.Count != 0)
               {
               string seriesNameExp = "exp";
               chart.CreateSeries(seriesNameExp, chartAreasName, SeriesChartType.Candlestick, legendName, Color.Black);
               chart.SetSeriesAddPoint(seriesNameExp, expX, expY);
               chart.SetSeriesAddLegendText(seriesNameExp, "Experimental");
               }

               if (MatchData.MatchedMsDataTheo.Count != 0)
               {
               string seriesNameTheo = "theo";
               chart.CreateSeries(seriesNameTheo, chartAreasName, SeriesChartType.Point, legendName, Color.Red);
               chart.SetSeriesAddPoint(seriesNameTheo, theoX, theoY);
               chart.SetSeriesAddLegendText(seriesNameTheo, "Theoretical");
               }

               chart.ChartE.Size = size;
               chart.ChartE.SaveImage(sFileName, ChartImageFormat.Jpeg);
        }
        public List<MatchedData> getMSMatchs(MassScan msScan2)
        {
            testParameter.IPMD = ParameterModel.MS_IPMD;
            testParameter.IPACO = ParameterModel.MS_IPACO;
            testParameter.IPAD = ParameterModel.MS_IPAD;
            testParameter.IPADOM = ParameterModel.MS_IPADOM;
            testParameter.IPMDOM = ParameterModel.MS_IPMDOM;
            testParameter.IPADO = ParameterModel.MS_IPADO;
            testParameter.IPMDO = ParameterModel.MS_IPMDO;

            if (testParameter.IPMD > testParameter.IPMDOM)
                testParameter.IPMDOM = testParameter.IPMD;

            if (testParameter.IPAD > testParameter.IPADOM)
                testParameter.IPADOM = testParameter.IPAD;

            //testParameter.Window = 5;
            testParameter.PAT = ParameterModel.MS_PAT;
            List<MatchedData> matchedDataList = new List<MatchedData>();
            List<ModResModel> modResFromDB = peptideModResDAL.getPmFromDB(testParameter.IPACO);
            MatchedData matchedData = null;
            List<MassPoint> msFromDB = null;
            for (int k = 0; k < modResFromDB.Count; k++)
            {
                msFromDB = modResFromDB[k].Mass_Point;
                matchedData = new MatchedData();
                //matchedData.MatchedMsDataExp = msReturn.ToList();
                matchedData.MatchedMsDataExp = new List<MassPoint>();
                //matchedData.MatchedMsDataExp = msReturn.ToList();
                matchedData.MatchedMsDataTheo = msFromDB;
                matchedData.ID = modResFromDB[k].ID;
                matchedData.MOD_RES = modResFromDB[k].Mod_Res;
                matchedData.Z = modResFromDB[k].Z;
                matchedData.Z = 15;
                matchedData.Repository = "";
                matchedData.TheoMZ = modResFromDB[k].M_Z.ToString(); matchedData.NMFS = "";
                matchedData.Sequence = modResFromDB[k].Sequence;
                matchedData.Actualpmfs = "";
                //matchedData.Index = 1;
                matchedDataList.Add(matchedData);
            }

            if (matchedDataList.Count > 0)
            {
                for (int j = 0; j < matchedDataList.Count; j++)
                {
                    matchedData = matchedDataList[j];

                    if (ParameterModel.MS2_SA == 1)
                    {
                        getMS2MatchsTopDown(msScan2, matchedData);
                        matchedDataList[j].ScreeningApproach = "TopDown";
                    }
                    else
                    {
                        getMS2MatchsTargeted(msScan2, matchedData);
                        matchedDataList[j].ScreeningApproach = "Targeted";
                    }
                    try
                    {
                        matchedDataList[j].NMFS = matchedDataList[j].MatchedMs2.Count().ToString();
                    }
                    catch
                    {
                        matchedDataList[j].NMFS = "0";
                    }
                    matchedData.Index = "1";
                    //Console.WriteLine("matched MS2:\t" + matchedData.MatchedMs2.Count.ToString());
                }
            }

            return matchedDataList;
        }
        public List<MatchedData> getMSMatchs(MassScan msScan, MassScan msScan2)
        {
            List<MatchedData> matchedDataList = new List<MatchedData>();

            testParameter.IPMD = ParameterModel.MS_IPMD;
            testParameter.IPACO = ParameterModel.MS_IPACO;
            testParameter.IPAD = ParameterModel.MS_IPAD;
            testParameter.IPADOM = ParameterModel.MS_IPADOM;
            testParameter.IPMDOM = ParameterModel.MS_IPMDOM;
            testParameter.IPADO = ParameterModel.MS_IPADO;
            testParameter.IPMDO = ParameterModel.MS_IPMDO;

            if (testParameter.IPMD > testParameter.IPMDOM)
                testParameter.IPMDOM = testParameter.IPMD;

            if (testParameter.IPAD > testParameter.IPADOM)
                testParameter.IPADOM = testParameter.IPAD;

            testParameter.Window = Convert.ToInt16(ParameterModel.MS_WINDOW / 2);
            testParameter.PAT = ParameterModel.MS_PAT;

            double ms2Point = msScan2.PrecursorMz;
            double maxInten = msScan.BasePeakIntensity;
            MatchedData matchedData = new MatchedData();
            //Console.WriteLine("scanMS.Index Begin2          " + DateTime.Now);

            //Console.WriteLine("scanMS.Index Begin3          " + DateTime.Now);
            List<MassPoint> msFromFile;
            List<MassPoint> msFromDB = null;
            //���ļ�ȡ��MS��ͼ
            msFromFile = Base64Convert.CalcMz(msScan.Peaks, msScan.PeaksCount);
            Console.WriteLine("scanMS.Index Begin4          " + DateTime.Now);
            //����MS2����
            List<MassPoint> msFromFileOpenWindow = getOpenWindowFromFile(msFromFile, testParameter.Window, ms2Point);
            //List<MassPoint>  msFromFileOpenWindow = msFromFile;
            Console.WriteLine("scanMS.Index Begin5          " + DateTime.Now);
            //���˵�5%���µķ�
            List<MassPoint> msFromFileReal = getRealMS2FromFile(msFromFileOpenWindow, testParameter.PAT, maxInten);
            // Console.WriteLine("scanMS.Index Begin6          " + DateTime.Now);
            //List<MassPoint> msFromFileReal = msFromFileOpenWindow;
            int timesFlag = 0;
            int maxMZIndex = 0;
            while (timesFlag < 1000)
            {
                //ȡ����ߵ�1
                maxMZIndex = getMaxMZ(msFromFileReal);
                //Console.WriteLine("maxMZIndex.ToString()maxMZIndex.ToString()maxMZIndex.ToString()maxMZIndex.ToString()                     " + maxMZIndex.ToString());
                if (maxMZIndex == -1)
                {
                    break;
                }
                else
                {
                    timesFlag = timesFlag + 1;
                }

                //����ƫ��
                double deviationInten = msFromFileReal[maxMZIndex].Mass - msFromFileReal[maxMZIndex].Mass / (testParameter.IPMD / 1000000.0 + 1.0);

                //MS��ߵ���DBƥ�� �������㴦��
                //Console.WriteLine("scanMS.Index Begin7          " + DateTime.Now);
                List<ModResModel> modResFromDB = peptideModResDAL.getPmFromDB(msFromFileReal[maxMZIndex].Mass, deviationInten, testParameter.IPACO);
                //Console.WriteLine("scanMS.Index Begin8          " + DateTime.Now);
                msFromFileReal[maxMZIndex].IsSearch = false;

                if (modResFromDB.Count == 0)
                {
                    //û��ƥ����
                    continue;

                }
                else
                {

                    //����ƥ��������߷������ƥ���ϵ��б��
                    bool compareFlag = false;
                    string Forumla = "";
                    List<MassPoint> msReturn = new List<MassPoint>(); ;
                    for (int k = 0; k < modResFromDB.Count; k++)
                    {

                        msFromDB = modResFromDB[k].Mass_Point;

                        if (Forumla == modResFromDB[k].Forumla && compareFlag == true)
                        {
                            matchedData = new MatchedData();
                            //matchedData.MatchedMsDataExp = msReturn.ToList();
                            matchedData.MatchedMsDataExp = new List<MassPoint>();
                            foreach (MassPoint item in msReturn)
                            {
                                MassPoint cloneMasspoint = new MassPoint();
                                cloneMasspoint.Index = item.Index;
                                cloneMasspoint.Intensity = item.Intensity;
                                cloneMasspoint.IntensityPercentage = item.IntensityPercentage;
                                cloneMasspoint.IPAD_R = item.IPAD_R;
                                cloneMasspoint.IPADOM_R = item.IPADOM_R;
                                cloneMasspoint.IPMD_R = item.IPMD_R;
                                cloneMasspoint.IPMDOM_R = item.IPMDOM_R;
                                cloneMasspoint.Mass = item.Mass;
                                cloneMasspoint.MatchIndex = item.MatchIndex;

                                matchedData.MatchedMsDataExp.Add(cloneMasspoint);
                            }
                            //matchedData.MatchedMsDataExp = msReturn.ToList();
                            matchedData.MatchedMsDataTheo = msFromDB;
                            matchedData.ID = modResFromDB[k].ID;
                            matchedData.MOD_RES = modResFromDB[k].Mod_Res;
                            matchedData.Z = modResFromDB[k].Z;
                            matchedData.Repository = "";
                            matchedData.ExpMZ = (msFromFileReal[maxMZIndex].Mass).ToString();
                            matchedData.TheoMZ = modResFromDB[k].M_Z.ToString();
                            matchedData.Error_PPM = Convert.ToString(((msFromFileReal[maxMZIndex].Mass - modResFromDB[k].M_Z) / modResFromDB[k].M_Z) * 1000000);
                            matchedData.Index = msScan2.Index.ToString();
                            matchedData.ExpMass_DA = Convert.ToString(((msFromFileReal[maxMZIndex].Mass - modResFromDB[k].M_Z) / modResFromDB[k].M_Z) * 1000000);
                            matchedData.NMFS = "";
                            matchedData.Actualpmfs = "";
                            matchedData.Sequence = modResFromDB[k].Sequence;
                            matchedDataList.Add(matchedData);
                            continue;
                        }
                        Forumla = modResFromDB[k].Forumla;
                        // MZ_FromDB = modResFromDB[0].M;
                        compareFlag = false;
                        int maxMZindex_DB = Convert.ToInt16(modResFromDB[k].M);
                        //��һƥ����ͼ

                        msReturn = comparePMAll(msFromFileReal, maxMZIndex, maxMZindex_DB, msFromDB);                        //��֤ƥ������ƥ��
                        if (getComparePercentage(msReturn, maxMZindex_DB, msFromDB))
                        {
                            compareFlag = true;
                            matchedData = new MatchedData();
                            //matchedData.MatchedMsDataExp = new List<MassPoint>();
                            matchedData.MatchedMsDataExp = new List<MassPoint>();
                            foreach (MassPoint item in msReturn)
                            {
                                MassPoint cloneMasspoint = new MassPoint();
                                cloneMasspoint.Index = item.Index;
                                cloneMasspoint.Intensity = item.Intensity;
                                cloneMasspoint.IntensityPercentage = item.IntensityPercentage;
                                cloneMasspoint.IPAD_R = item.IPAD_R;
                                cloneMasspoint.IPADOM_R = item.IPADOM_R;
                                cloneMasspoint.IPMD_R = item.IPMD_R;
                                cloneMasspoint.IPMDOM_R = item.IPMDOM_R;
                                cloneMasspoint.Mass = item.Mass;
                                cloneMasspoint.MatchIndex = item.MatchIndex;
                                matchedData.MatchedMsDataExp.Add(cloneMasspoint);
                            }
                            matchedData.MatchedMsDataTheo = msFromDB;
                            matchedData.ID = modResFromDB[k].ID;
                            matchedData.MOD_RES = modResFromDB[k].Mod_Res;
                            matchedData.Z = modResFromDB[k].Z;
                            matchedData.Index = msScan2.Index.ToString();
                            matchedData.Repository = "";
                            matchedData.ExpMZ = (msFromFileReal[maxMZIndex].Mass).ToString();
                            matchedData.TheoMZ = modResFromDB[k].M_Z.ToString();
                            matchedData.Error_PPM = Convert.ToString(((msFromFileReal[maxMZIndex].Mass - modResFromDB[k].M_Z) / modResFromDB[k].M_Z) * 1000000);

                            matchedData.ExpMass_DA = Convert.ToString(((msFromFileReal[maxMZIndex].Mass - modResFromDB[k].M_Z) / modResFromDB[k].M_Z) * 1000000);
                            matchedData.NMFS = "";
                            matchedData.Actualpmfs = "";
                            matchedData.Sequence = modResFromDB[k].Sequence;
                            matchedDataList.Add(matchedData);
                            //break;
                            for (int i = 0; i < msFromFileReal.Count; i++)
                            {

                                double baseIntens = 0.0;
                                for (int j = 0; j < msReturn.Count; j++)
                                {
                                    baseIntens = msFromFileReal[j].Intensity / msReturn[j].IntensityPercentage;
                                    msFromFileReal[msReturn[j].MatchIndex].Intensity =
                                            msFromFileReal[msReturn[j].MatchIndex].Intensity - (baseIntens * msFromDB[j].Intensity);
                                }
                            }

                        }
                    }
                    if (matchedData != null)
                        break;
                }
            }

            if (matchedDataList.Count > 0)
            {
                for (int j = 0; j < matchedDataList.Count; j++)
                {
                    matchedData = matchedDataList[j];

                    if (ParameterModel.MS2_SA == 1)
                    {
                        getMS2MatchsTopDown(msScan2, matchedData);
                        matchedDataList[j].ScreeningApproach = "TopDown";
                    }
                    else
                    {
                        getMS2MatchsTargeted(msScan2, matchedData);
                        matchedDataList[j].ScreeningApproach = "Targeted";
                    }

                    if (matchedDataList[j].MatchedMs2 == null)
                        matchedDataList[j].NMFS = "0";
                    else
                        matchedDataList[j].NMFS = matchedDataList[j].MatchedMs2.Count().ToString();

                    //Console.WriteLine("matched MS2:\t" + matchedData.MatchedMs2.Count.ToString());
                }
            }

            return matchedDataList;
        }
        public void getMS2MatchsTopDown(MassScan msScan2, MatchedData matchedData)
        {
            testParameter.IPMD = ParameterModel.MS2_IPMD;
            testParameter.IPACO = ParameterModel.MS2_IPACO;
            testParameter.IPAD = ParameterModel.MS2_IPAD;
            testParameter.IPADOM = ParameterModel.MS2_IPADOM;
            testParameter.IPMDOM = ParameterModel.MS2_IPMDOM;
            testParameter.IPADO = ParameterModel.MS2_IPADO;
            testParameter.IPMDO = ParameterModel.MS2_IPMDO;
            if (testParameter.IPMD > testParameter.IPMDOM)
                testParameter.IPMDOM = testParameter.IPMD;

            if (testParameter.IPAD > testParameter.IPADOM)
                testParameter.IPADOM = testParameter.IPAD;
            //testParameter.Window = 5;
            testParameter.PAT = ParameterModel.MS2_FAT;
            List<MassPoint> ms2FromFile = null;
            List<MassPoint> msFromDB = null;
            //ModResModel ms2Matched = new ModResModel();
            List<ModResModel> MatchedMs2 = new List<ModResModel>();
            double maxInten = msScan2.BasePeakIntensity;
            //���˵�5%���µķ�
            ms2FromFile = Base64Convert.CalcMz(msScan2.Peaks, msScan2.PeaksCount);
            List<MassPoint> msFromFileReal = getRealMS2FromFile(ms2FromFile, testParameter.PAT, maxInten);

            List<string[]> fmValue = peptideModResDAL.getFmFromDB(matchedData.ID, matchedData.MOD_RES, msScan2.ScanType, matchedData.Sequence);
            if (fmValue.Count == 0)
            {
                return;
            }
            while (true)
            {
                //ȡ����ߵ�1
                int maxMZIndex = getMaxMZ(msFromFileReal);
                if (maxMZIndex == -1)
                {
                    break;
                }
                if (maxMZIndex == 157)
                {
                    maxMZIndex = 157;
                }
                //����ƫ��
                double deviationInten = msFromFileReal[maxMZIndex].Mass - msFromFileReal[maxMZIndex].Mass / (testParameter.IPMD / 1000000.0 + 1.0);

                //MS��ߵ���DBƥ�� �������㴦��
                List<ModResModel> modResFromDB = getFMPointInfo(msFromFileReal[maxMZIndex].Mass, deviationInten, fmValue, matchedData.Z, testParameter.IPACO);
                msFromFileReal[maxMZIndex].IsSearch = false;
                if (modResFromDB == null)
                {
                    //û��ƥ����
                    continue;

                }
                else
                {
                    for (int k = 0; k < modResFromDB.Count; k++)
                    {
                        msFromDB = modResFromDB[k].Mass_Point;
                        int maxMZindex_DB = Convert.ToInt16(modResFromDB[k].M_Z);
                        //��һƥ����ͼ
                        List<MassPoint> msReturn = comparePMAll(msFromFileReal, maxMZIndex, maxMZindex_DB, msFromDB);
                        if (modResFromDB[k].ID == "B5")
                        {
                            modResFromDB[k].ID = "B5";
                        }
                        //��֤ƥ������ƥ��
                        if (getComparePercentage(msReturn, maxMZindex_DB, msFromDB))
                        {
                            ModResModel ms2Matched = new ModResModel();
                            double baseIntens = msFromFileReal[maxMZIndex].Intensity;
                            ms2Matched.Mass_Point = new List<MassPoint>(msFromDB.ToArray());
                            ms2Matched.Mass_Point_FromFile = new List<MassPoint>();
                            for (int j = 0; j < msReturn.Count; j++)
                            {
                                MassPoint massPoint = new MassPoint();
                                massPoint.Index = msReturn[j].Index;
                                //massPoint.Intensity = new Double();
                                massPoint.Intensity = msReturn[j].Intensity;
                                massPoint.IPAD_R = msReturn[j].IPAD_R;
                                massPoint.IPADOM_R = msReturn[j].IPADOM_R;
                                massPoint.IPMD_R = msReturn[j].IPMD_R;
                                massPoint.IPMDOM_R = msReturn[j].IPMDOM_R;
                                massPoint.Mass = msReturn[j].Mass;
                                massPoint.Index = msReturn[j].Index;
                                massPoint.IntensityPercentage = msReturn[j].IntensityPercentage;
                                ms2Matched.Mass_Point_FromFile.Add(massPoint);
                            }
                            ms2Matched.Z = modResFromDB[k].Z;
                            ms2Matched.ID = modResFromDB[k].ID;

                            MatchedMs2.Add(ms2Matched);
                            //matchedData.MatchedMs2DataExp = ms2Matched;

                            for (int j = 0; j < msReturn.Count; j++)
                            {
                                baseIntens = msFromFileReal[j].Intensity / msReturn[j].IntensityPercentage;
                                msFromFileReal[msReturn[j].MatchIndex].Intensity =
                                        msFromFileReal[msReturn[j].MatchIndex].Intensity - (baseIntens * msFromDB[j].Intensity);
                            }
                        }
                    }
                }
            }
            matchedData.MatchedMs2 = MatchedMs2;
        }
        public void getMS2MatchsTargeted(MassScan msScan2, MatchedData matchedData)
        {
            //testParameter.IPMD = 10;
            //testParameter.IPACO = 5;
            //testParameter.IPAD = 50;
            //testParameter.IPADOM = 100;
            //testParameter.IPMDOM = 30;
            //testParameter.IPADO = 20;
            //testParameter.IPMDO = 20;
            //testParameter.Window = 5;
            //testParameter.PAT = 0.2;

            testParameter.IPMD = ParameterModel.MS2_IPMD;
            testParameter.IPACO = ParameterModel.MS2_IPACO;
            testParameter.IPAD = ParameterModel.MS2_IPAD;
            testParameter.IPADOM = ParameterModel.MS2_IPADOM;
            testParameter.IPMDOM = ParameterModel.MS2_IPMDOM;
            testParameter.IPADO = ParameterModel.MS2_IPADO;
            testParameter.IPMDO = ParameterModel.MS2_IPMDO;
            if (testParameter.IPMD > testParameter.IPMDOM)
                testParameter.IPMDOM = testParameter.IPMD;

            if (testParameter.IPAD > testParameter.IPADOM)
                testParameter.IPADOM = testParameter.IPAD;

            //testParameter.Window = 5;
            testParameter.PAT = ParameterModel.MS2_FAT;

            //if (msScan2.ScanType == "CID")
            //{
            //    return;
            //}
            List<MassPoint> ms2FromFile = null;
            List<MassPoint> msFromDB = null;
            //ModResModel ms2Matched = new ModResModel();
            List<ModResModel> MatchedMs2 = new List<ModResModel>();
            double maxInten = msScan2.BasePeakIntensity;
            //���˵�5%���µķ�
            ms2FromFile = Base64Convert.CalcMz(msScan2.Peaks, msScan2.PeaksCount);
            List<MassPoint> msFromFileReal = getRealMS2FromFile(ms2FromFile, testParameter.PAT, maxInten);

            List<string[]> fmValue = peptideModResDAL.getFmFromDB(matchedData.ID, matchedData.MOD_RES, msScan2.ScanType, matchedData.Sequence);
            if (fmValue.Count == 0)
            {
                return;
            }
            //msFromDB = getFMDataPoints(fmValue);
            for (int i = 0; i < fmValue[0].Count(); i++)
            {
                //��һƥ����ͼ
                List<ModResModel> modResFromDB = comparePMFromDB(fmValue[0][i], fmValue[1][i], fmValue[2][i], msFromFileReal, matchedData.Z, testParameter.IPACO);
                if (modResFromDB.Count > 1)
                {
                    //modResFromDB = null;
                }
                for (int m = 0; m < modResFromDB.Count; m++)
                {
                    if (modResFromDB[m].ID == "Y40")
                    {
                        modResFromDB[m].ID = "Y40";
                    }
                    msFromDB = modResFromDB[m].Mass_Point;
                    int maxMZIndex = Convert.ToInt16(modResFromDB[m].M);//��ʱʹ��
                    int maxMZindex_DB = Convert.ToInt16(modResFromDB[m].M_Z);
                    //��һƥ����ͼ
                    List<MassPoint> msReturn = comparePMAll(msFromFileReal, maxMZIndex, maxMZindex_DB, msFromDB);
                    //��֤ƥ������ƥ��
                    if (getComparePercentage(msReturn, maxMZindex_DB, msFromDB))
                    {
                        ModResModel ms2Matched = new ModResModel();
                        double baseIntens = msFromFileReal[maxMZIndex].Intensity;
                        ms2Matched.Mass_Point = new List<MassPoint>(msFromDB.ToArray());
                        ms2Matched.Mass_Point_FromFile = new List<MassPoint>();
                        for (int j = 0; j < msReturn.Count; j++)
                        {
                            MassPoint massPoint = new MassPoint();
                            massPoint.Index = msReturn[j].Index;
                            //massPoint.Intensity = new Double();
                            massPoint.Intensity = msReturn[j].Intensity;
                            massPoint.IPAD_R = msReturn[j].IPAD_R;
                            massPoint.IPADOM_R = msReturn[j].IPADOM_R;
                            massPoint.IPMD_R = msReturn[j].IPMD_R;
                            massPoint.IPMDOM_R = msReturn[j].IPMDOM_R;
                            massPoint.Mass = msReturn[j].Mass;
                            massPoint.Index = msReturn[j].Index;
                            massPoint.IntensityPercentage = msReturn[j].IntensityPercentage;
                            ms2Matched.Mass_Point_FromFile.Add(massPoint);
                        }
                        //matchedData.ID = modResFromDB[0].ID;
                        //matchedData.MOD_RES = modResFromDB[0].Mod_Res;
                        //matchedData.Z = modResFromDB[0].Z;
                        ms2Matched.Z = modResFromDB[m].Z;
                        ms2Matched.ID = modResFromDB[m].ID;
                        MatchedMs2.Add(ms2Matched);
                        //matchedData.MatchedMs2DataExp = ms2Matched;

                        for (int j = 0; j < msReturn.Count; j++)
                        {
                            //ms2Matched.Mass_Point_FromFile[j].Intensity = new Double();
                            //ms2Matched.Mass_Point_FromFile[j].Intensity = msFromFileReal[msReturn[j].MatchIndex].Intensity;
                            baseIntens = msFromFileReal[j].Intensity / msReturn[j].IntensityPercentage;
                            msFromFileReal[msReturn[j].MatchIndex].Intensity =
                                    msFromFileReal[msReturn[j].MatchIndex].Intensity - (baseIntens * msFromDB[j].Intensity);
                            //if (maxInten * testParameter.IPACO / 100 > msFromFileReal[msReturn[j].MatchIndex].Intensity)
                            //{
                            //    msFromFileReal[msReturn[j].MatchIndex].Intensity = 0;
                            //}
                        }

                    }
                }
            }

            matchedData.MatchedMs2 = MatchedMs2;
            //for (int i = 0; i < MatchedMs2.Count; i++)
            //{
            //    Console.WriteLine("Ion:  " + MatchedMs2[i].ID + "\t" + "Z:  " + MatchedMs2[i].Z);
            //    for (int j = 0; j < MatchedMs2[i].Mass_Point.Count; j++)
            //    {
            //        Console.WriteLine("Mass: \t " + MatchedMs2[i].Mass_Point[j].Mass + "\t" + MatchedMs2[i].Mass_Point_FromFile[j].Mass + "\tIntensity: \t" + MatchedMs2[i].Mass_Point[j].Intensity + "\t" + MatchedMs2[i].Mass_Point_FromFile[j].IntensityPercentage + "\t" + MatchedMs2[i].Mass_Point_FromFile[j].IPAD_R);
            //    }

            //}
        }