Exemple #1
0
        //read from pTop.pfd
        void pTop_Inter.pTop_read(string task_path, bool pParse, ref _Task _task)
        {
            string strLine = "";

            try
            {
                pTop.classes.File _file         = _task.T_File;
                Identification    _search       = _task.T_Identify;
                Quantitation      _quantitation = _task.T_Quantify;

                FileStream   fst = new FileStream(task_path + "\\param\\pTop.cfg", FileMode.Open);
                StreamReader sr  = new StreamReader(fst, Encoding.Default);
                strLine = sr.ReadLine();
                string subtitle = "";
                #region
                while (strLine != null)
                {
                    strLine = strLine.Trim();
                    if (strLine.Length > 0 && strLine[0] == '[' && strLine[strLine.Length - 1] == ']')
                    {
                        subtitle = strLine;
                    }
                    else if (strLine.Length > 0 && strLine.IndexOf("=") > -1)
                    {
                        if (subtitle.Equals("[spectrum]"))
                        {
                            // msmspath and input_format were read from pParseTD.cfg
                            if (strLine.Length > ("Activation").Length && strLine.Substring(0, strLine.LastIndexOf("=")).Equals("Activation"))
                            {
                                _file.Instrument = strLine.Substring(strLine.LastIndexOf("=") + 1);
                                _file.setInstrument_index();
                            }
                            else if (strLine.Length > ("Precursor_Tolerance").Length && strLine.Substring(0, strLine.LastIndexOf("=")).Equals("Precursor_Tolerance"))
                            {
                                _search.Ptl.Tl_value = double.Parse(strLine.Substring(strLine.LastIndexOf("=") + 1));
                            }
                            else if (strLine.Length > ("Fragment_Tolerance").Length && strLine.Substring(0, strLine.LastIndexOf("=")).Equals("Fragment_Tolerance"))
                            {
                                _search.Ftl.Tl_value = double.Parse(strLine.Substring(strLine.LastIndexOf("=") + 1));
                            }
                        }
                        else if (subtitle.Equals("[param]"))
                        {
                            if (strLine.Length > ("workflow").Length && strLine.Substring(0, strLine.LastIndexOf("=")).Equals("workflow"))
                            {
                                _search.SearchModeIndex = int.Parse(strLine.Substring(strLine.LastIndexOf("=") + 1));
                            }
                            else if (strLine.Length > ("output_top_k").Length && strLine.Substring(0, strLine.LastIndexOf("=")).Equals("output_top_k"))
                            {
                                _search.OutputTopK = int.Parse(strLine.Substring(strLine.LastIndexOf("=") + 1));
                            }
                            else if (strLine.Length > ("max_truncated_mass").Length && strLine.Substring(0, strLine.LastIndexOf("=")).Equals("max_truncated_mass"))
                            {
                                _search.MaxTruncatedMass = double.Parse(strLine.Substring(strLine.LastIndexOf("=") + 1));
                            }
                            else if (strLine.Length > ("second_search").Length && strLine.Substring(0, strLine.LastIndexOf("=")).Equals("second_search"))
                            {
                                if (strLine.Substring(strLine.LastIndexOf("=") + 1).Equals("1"))
                                {
                                    _search.SecondSearch = true;
                                }
                                else
                                {
                                    _search.SecondSearch = false;
                                }
                            }
                        }

                        else if (subtitle.Equals("[fixmodify]"))
                        {
                            if (strLine.Length > ("fixedModify_num").Length && strLine.Substring(0, strLine.LastIndexOf("=")).Equals("fixedModify_num"))
                            {
                                int fix_num = int.Parse(strLine.Substring(strLine.LastIndexOf("=") + 1));
                                _search.Fix_mods.Clear();
                                for (int i = 0; i < fix_num; i++)
                                {
                                    strLine = sr.ReadLine();
                                    string fixmod = strLine.Substring(strLine.LastIndexOf("=") + 1);
                                    _search.Fix_mods.Add(fixmod);
                                }
                            }
                        }

                        else if (subtitle.Equals("[modify]"))
                        {
                            if (strLine.Length > ("Max_mod_num").Length && strLine.Substring(0, strLine.LastIndexOf("=")).Equals("Max_mod_num"))
                            {
                                _search.Max_mod = int.Parse(strLine.Substring(strLine.LastIndexOf("=") + 1));
                            }
                            else if (strLine.Length > ("unexpected_mod").Length && strLine.Substring(0, strLine.LastIndexOf("=")).Equals("unexpected_mod"))
                            {
                                _search.UnexpectedModNum = int.Parse(strLine.Substring(strLine.LastIndexOf("=") + 1));
                            }
                            else if (strLine.Length > ("Max_mod_mass").Length && strLine.Substring(0, strLine.LastIndexOf("=")).Equals("Max_mod_mass"))
                            {
                                _search.MaxModMass = double.Parse(strLine.Substring(strLine.LastIndexOf("=") + 1));
                            }
                            else if (strLine.Length > ("Modify_num").Length && strLine.Substring(0, strLine.LastIndexOf("=")).Equals("Modify_num"))
                            {
                                int var_num = int.Parse(strLine.Substring(strLine.LastIndexOf("=") + 1));
                                _search.Var_mods.Clear();
                                for (int i = 0; i < var_num; i++)
                                {
                                    strLine = sr.ReadLine();
                                    string varmod = strLine.Substring(strLine.LastIndexOf("=") + 1);
                                    _search.Var_mods.Add(varmod);
                                }
                            }
                        }
                        else if (subtitle.Equals("[filter]"))
                        {
                            if (strLine.Length > ("threshold").Length && strLine.Substring(0, strLine.LastIndexOf("=")).Equals("threshold"))
                            {
                                _search.Filter.Fdr_value = double.Parse(strLine.Substring(strLine.LastIndexOf("=") + 1)) * 100;
                            }
                            else if (strLine.Length > ("separate_filtering").Length && strLine.Substring(0, strLine.LastIndexOf("=")).Equals("separate_filtering"))
                            {
                                if (strLine.Substring(strLine.LastIndexOf("=") + 1).Equals("1"))
                                {
                                    _search.Filter.SeparateFiltering = true;
                                }
                                else
                                {
                                    _search.Filter.SeparateFiltering = false;
                                }
                            }
                        }

                        #region [file]
                        if (subtitle.Equals("[file]"))
                        {
                            if (strLine.Length > ("Database").Length && strLine.Substring(0, strLine.LastIndexOf("=")).Equals("Database"))
                            {
                                string db_path = strLine.Substring(strLine.LastIndexOf("=") + 1);
                                if (db_path.Trim().Length > 0)
                                {
                                    DB db = new DB();
                                    db.Db_path = db_path;
                                    if (ConfigHelper.ReDBmap.Contains(db.Db_path))
                                    {
                                        db.Db_name       = ConfigHelper.ReDBmap[db.Db_path].ToString();
                                        _search.Db       = db;
                                        _search.Db_index = _search.setDatabaseIndex();
                                    }
                                    else     //the database is damaged
                                    {
                                        _search.Db_index = -1;
                                        _search.Db       = db;
                                    }
                                }
                                else   //when new a task
                                {
                                    _search.Db_index = -1;
                                    _search.Db       = new DB();
                                }
                                #region Todo
                                //当不存在指定数据库时
                                #endregion
                            }
                            else if (strLine.Length > ("pParseTD_cfg").Length && strLine.Substring(0, strLine.LastIndexOf("=")).Equals("pParseTD_cfg"))
                            {
                            }
                            else if (strLine.Length > ("pQuant_cfg").Length && strLine.Substring(0, strLine.LastIndexOf("=")).Equals("pQuant_cfg"))
                            {
                            }
                            else if (strLine.Length > ("outputpath").Length && strLine.Substring(0, strLine.LastIndexOf("=")).Equals("outputpath"))
                            {
                                string tpath = strLine.Substring(strLine.LastIndexOf("=") + 1);

                                if (tpath.Trim().Length > 0)
                                {
                                    if (task_path.EndsWith("\\"))
                                    {
                                        task_path = task_path.Substring(0, task_path.Length - 1);
                                    }
                                    _task.Path      = task_path + "\\";
                                    _task.Task_name = task_path.Substring(task_path.LastIndexOf("\\") + 1);
                                }
                            }
                        }
                        #endregion

                        else if (subtitle.Equals("[quantify]"))
                        {
                            if (strLine.Length > 5 && strLine.Substring(0, strLine.LastIndexOf("=")).Equals("quant"))
                            {
                                string   qt      = strLine.Substring(strLine.LastIndexOf("=") + 1);
                                string[] lb      = qt.Split('|');
                                int      lbcount = lb.GetUpperBound(0);
                                int      lbnum   = int.Parse(lb[0]);
                                #region 如果是MGF文件怎么办
                                #endregion
                                //labeling
                                if (lbnum == 1 && (lb[1].Equals("none") || lb[1].Equals("None")))
                                {
                                    _quantitation.Quantitation_type = (int)Quant_Type.Label_None;
                                }
                                else
                                {
                                    _quantitation.Quantitation_type           = (int)Quant_Type.Labeling; //if 15N, it will be changed in pQuant_Read
                                    _quantitation.Labeling.Multiplicity       = lbnum;
                                    _quantitation.Labeling.Multiplicity_index = lbnum - 1;
                                    if (lbnum == 1)
                                    {
                                        /*
                                         * string[] lbs = lb[1].Split(',');
                                         * foreach (string c in lbs)
                                         * {
                                         *  if (c.Trim() != ""&&c.ToLower()!="none")
                                         *  {
                                         *      _quantitation.Labeling.Medium_label.Add(c.Trim());
                                         *  }
                                         * }*/
                                        _quantitation.Labeling.Medium_label.Add(lb[1]);
                                    }
                                    else if (lbnum == 2)
                                    {
                                        _quantitation.Labeling.Light_label.Add(lb[1]);

                                        _quantitation.Labeling.Heavy_label.Add(lb[2]);
                                    }
                                    else if (lbnum == 3)
                                    {
                                        _quantitation.Labeling.Light_label.Add(lb[1]);

                                        _quantitation.Labeling.Medium_label.Add(lb[2]);

                                        _quantitation.Labeling.Heavy_label.Add(lb[3]);
                                    }
                                }
                            }
                        }
                    }
                    strLine = sr.ReadLine();
                }
                #endregion
                sr.Close();
                fst.Close();
            }
            catch (Exception exe)
            {
                throw new Exception("[pTop_read] line: " + strLine + "\n" + exe.Message);
            }
        }
Exemple #2
0
        //generate pTop.cfg
        void pTop_Inter.pTop_write(_Task _task)
        {
            try
            {
                pTop.classes.File _file         = _task.T_File;
                Identification    _search       = _task.T_Identify;
                Quantitation      _quantitation = _task.T_Quantify;

                StreamReader sr      = new StreamReader(@"pTop.ini", Encoding.Default);
                string       strLine = sr.ReadLine();
                string       thread  = "";
                while (strLine != null)
                {
                    if (strLine.Length > 6 && strLine.Substring(0, 6).Equals("thread"))
                    {
                        thread = strLine.Substring(strLine.LastIndexOf("=") + 1);
                    }
                    if (thread != "")
                    {
                        break;
                    }
                    strLine = sr.ReadLine();
                }

                string       filepath = _task.Path + "param\\pTop.cfg";
                FileStream   pFfst    = new FileStream(filepath, FileMode.Create, FileAccess.Write);
                StreamWriter pFsw     = new StreamWriter(pFfst, Encoding.Default);
                pFsw.WriteLine("# This is a standard pTop configuration file");
                pFsw.WriteLine("# For help: mail to [email protected]");
                pFsw.WriteLine("# Time: " + DateTime.Now.ToString());
                pFsw.WriteLine();
                pFsw.WriteLine("[Version]");
                pFsw.WriteLine("pTop_Version=EVA.2.0");
                pFsw.WriteLine();

                pFsw.WriteLine("[spectrum]");
                pFsw.WriteLine("msmsnum=" + _file.Data_file_list.Count.ToString());
                for (int i = 0; i < _file.Data_file_list.Count; i++)
                {
                    string mgfpath = _file.Data_file_list[i].FilePath;
                    pFsw.WriteLine("msmspath" + (i + 1).ToString() + "=" + mgfpath);
                }
                pFsw.WriteLine("input_format=" + _file.File_format);
                pFsw.WriteLine("Activation=" + _file.Instrument);
                //母离子误差指定为 Da
                pFsw.WriteLine("Precursor_Tolerance=" + _search.Ptl.Tl_value.ToString());
                //pFsw.WriteLine("mstolppm=" + _search.Ptl.Isppm.ToString());
                //碎片离子误差指定为 ppm
                pFsw.WriteLine("Fragment_Tolerance=" + _search.Ftl.Tl_value.ToString());
                //pFsw.WriteLine("msmstolppm=" + _search.Ftl.Isppm.ToString());
                pFsw.WriteLine();

                pFsw.WriteLine("[param]");
                pFsw.WriteLine("thread_num=" + thread);
                pFsw.WriteLine("workflow=" + _search.SearchModeIndex.ToString());
                pFsw.WriteLine("# 0 tag flow, 1 ion flow");
                pFsw.WriteLine("output_top_k=" + _search.OutputTopK.ToString());
                pFsw.WriteLine("max_truncated_mass=" + _search.MaxTruncatedMass.ToString());
                pFsw.WriteLine("second_search=" + (_search.SecondSearch ? "1" : "0"));
                pFsw.WriteLine();

                pFsw.WriteLine("[fixmodify]");
                pFsw.WriteLine("fixedModify_num=" + _search.Fix_mods.Count.ToString());
                for (int i = 0; i < _search.Fix_mods.Count; i++)
                {
                    if (_search.Fix_mods[i].Trim().Length > 0)
                    {
                        pFsw.WriteLine("fix_mod" + (i + 1).ToString() + "=" + _search.Fix_mods[i]);
                    }
                }
                pFsw.WriteLine();
                // 可变修饰参数
                pFsw.WriteLine("[modify]");
                pFsw.WriteLine("Max_mod_num=" + _search.Max_mod.ToString());
                pFsw.WriteLine("unexpected_mod=" + _search.UnexpectedModNum.ToString());
                pFsw.WriteLine("Max_mod_mass=" + _search.MaxModMass.ToString());
                pFsw.WriteLine("Modify_num=" + _search.Var_mods.Count.ToString());
                for (int i = 0; i < _search.Var_mods.Count; i++)
                {
                    if (_search.Var_mods[i].Trim().Length > 0)
                    {
                        pFsw.WriteLine("var_mod" + (i + 1).ToString() + "=" + _search.Var_mods[i]);
                    }
                }
                pFsw.WriteLine();

                pFsw.WriteLine("[filter]");
                pFsw.WriteLine("threshold=" + (_search.Filter.Fdr_value / 100).ToString());
                pFsw.WriteLine("separate_filtering=" + (_search.Filter.SeparateFiltering ? "1" : "0"));
                pFsw.WriteLine();

                pFsw.WriteLine("[file]");
                pFsw.WriteLine("Database=" + _search.Db.Db_path);
                pFsw.WriteLine("pParseTD_cfg=" + _task.Path + "param\\pParseTD.cfg");
                if (_quantitation.Quantitation_type != (int)Quant_Type.Label_None)
                {
                    pFsw.WriteLine("pQuant_cfg=" + _task.Path + "param\\pQuant.cfg");
                }
                else
                {
                    pFsw.WriteLine("pQuant_cfg=");
                }
                pFsw.WriteLine("outputpath=" + _task.Path);
                pFsw.WriteLine();

                #region Quant
                pFsw.WriteLine("[quantify]");
                if (_quantitation.Quantitation_type == (int)Quant_Type.Label_None)
                {
                    pFsw.WriteLine("quant=1|none");
                }
                else // quantify based on MS: Quant_Type.Labeling_15N || Quant_Type.Labeling_Dimethyl || Quant_Type.Labeling_SILAC || others
                {
                    string labels = "quant=" + _quantitation.Labeling.Multiplicity.ToString();    //Multiplicity
                    if (_quantitation.Labeling.Multiplicity == 1)  // 1种标记
                    {
                        if (_quantitation.Labeling.Medium_label.Count == 0)
                        {
                            labels += "|none";
                        }
                        else
                        {
                            labels += "|" + _quantitation.Labeling.Medium_label[0];
                        }
                    }
                    else if (_quantitation.Labeling.Multiplicity == 2)     // 2种标记
                    {
                        if (_quantitation.Labeling.Light_label.Count == 0) //light label
                        {
                            labels += "|none";
                        }
                        else
                        {
                            labels += "|" + _quantitation.Labeling.Light_label[0];
                        }
                        if (_quantitation.Labeling.Heavy_label.Count == 0)   //heavy label
                        {
                            labels += "|none";
                        }
                        else
                        {
                            labels += "|" + _quantitation.Labeling.Heavy_label[0];
                        }
                    }
                    else if (_quantitation.Labeling.Multiplicity == 3)     // 3种标记
                    {
                        if (_quantitation.Labeling.Light_label.Count == 0) //light label
                        {
                            labels += "|none";
                        }
                        else
                        {
                            labels += "|" + _quantitation.Labeling.Light_label[0];
                        }
                        if (_quantitation.Labeling.Medium_label.Count == 0)   //medium label
                        {
                            labels += "|none";
                        }
                        else
                        {
                            labels += "|" + _quantitation.Labeling.Medium_label[0];
                        }
                        if (_quantitation.Labeling.Heavy_label.Count == 0)   //heavy label
                        {
                            labels += "|none";
                        }
                        else
                        {
                            labels += "|" + _quantitation.Labeling.Heavy_label[0];
                        }
                    }
                    pFsw.WriteLine(labels);
                }
                pFsw.WriteLine();
                #endregion

                pFsw.WriteLine("[system]");
                pFsw.WriteLine("log=LOG_INFO");
                pFsw.Close();
                pFfst.Close();
            }
            catch (Exception exe)
            {
                throw new Exception(exe.Message);
            }
        }
Exemple #3
0
        //read from pQuant.qnt
        //wrm20150821:将来代码重构时可考虑在读参数文件时,先获取‘=’前和‘=’后的部分,再给参数赋值
        void pQuant_Inter.readpQuant_qnt(string task_path, ref _Task _task)
        {
            string strLine = "";

            try
            {
                FileStream   fst = new FileStream(task_path + "\\param\\pQuant.cfg", FileMode.Open);
                StreamReader sr  = new StreamReader(fst, Encoding.Default);
                strLine = sr.ReadLine();
                #region
                Quantitation _quant = _task.T_Quantify;

                string[] labels = { "" };;
                while (strLine != null)
                {
                    strLine = strLine.Trim();
                    if (strLine.Length > 0 && strLine.Contains("="))
                    {
                        if (strLine.Length > ("NUMBER_MAX_PSM_PER_BLOCK").Length && strLine.Substring(0, strLine.LastIndexOf("=")).Equals("NUMBER_MAX_PSM_PER_BLOCK"))
                        {
                            int tmp = strLine.LastIndexOf("=");
                            _quant.Quant_advanced.Number_max_psm_per_block = int.Parse(strLine.Substring(tmp + 1, strLine.LastIndexOf(";") - tmp - 1));
                        }
                        else if (strLine.Length > ("TYPE_START").Length && strLine.Substring(0, strLine.LastIndexOf("=")).Equals("TYPE_START"))
                        {
                            int tmp = strLine.LastIndexOf("=");
                            _quant.Quant_advanced.Type_start = int.Parse(strLine.Substring(tmp + 1, strLine.LastIndexOf(";") - tmp - 1));
                        }
                        else if (strLine.Length > ("EXTENSION_TEXT_MS1").Length && strLine.Substring(0, strLine.LastIndexOf("=")).Equals("EXTENSION_TEXT_MS1"))
                        {
                            int tmp = strLine.LastIndexOf("=");
                            _quant.Quant_advanced.Extension_text_ms1 = strLine.Substring(tmp + 1, strLine.LastIndexOf(";") - tmp - 1);
                        }
                        //else if (strLine.Length > ("LL_INFO_LABEL").Length && strLine.Substring(0, strLine.LastIndexOf("=")).Equals("LL_INFO_LABEL"))
                        //{
                        //    int tmp=strLine.LastIndexOf("=");
                        //    string qt = strLine.Substring(tmp + 1,strLine.LastIndexOf(";")-tmp-1);
                        //    labels = qt.Split('|');
                        //    lb = int.Parse(labels[0]);
                        //}
                        else if (strLine.Length > ("LL_ELEMENT_ENRICHMENT_CALIBRATION").Length && strLine.Substring(0, strLine.LastIndexOf("=")).Equals("LL_ELEMENT_ENRICHMENT_CALIBRATION"))
                        {
                            int    tmp  = strLine.LastIndexOf("=");
                            string lfec = strLine.Substring(tmp + 1, strLine.LastIndexOf(";") - tmp - 1);
                            _quant.Quant_advanced.Ll_element_enrichment_calibration = (int)LL_ELEMENT_ENRICHMENT_CALIBRATION.None;
                            if (lfec.ToLower() == "15n")
                            {
                                _quant.Quant_advanced.Ll_element_enrichment_calibration = (int)LL_ELEMENT_ENRICHMENT_CALIBRATION.N15;
                            }
                            else if (lfec.ToLower() == "13c")
                            {
                                _quant.Quant_advanced.Ll_element_enrichment_calibration = (int)LL_ELEMENT_ENRICHMENT_CALIBRATION.C13;
                            }
                            //bool is15N = false;
                            //if (lb == 2 && labels.Length == 3 && (labels[1].Equals(ConfigHelper.N15_label) || labels[2].Equals(ConfigHelper.N15_label)))
                            //{
                            //    is15N = true;
                            //}
                            //if (is15N && (flag == 1 || flag == 2))
                            //{
                            //    _quant.Quantitation_type = (int)Quant_Type.Labeling_15N;
                            //}
                        }
                        #region Todo LabelFree
                        else if (strLine.Length > ("LF_INFO_SAMPLE").Length && strLine.Substring(0, strLine.LastIndexOf("=")).Equals("LF_INFO_SAMPLE"))
                        {
                        }
                        #endregion
                        else if (strLine.Length > ("NUMBER_SCANS_HALF_CMTG").Length && strLine.Substring(0, strLine.LastIndexOf("=")).Equals("NUMBER_SCANS_HALF_CMTG"))
                        {
                            int tmp = strLine.LastIndexOf("=");
                            _quant.Quant_advanced.Number_scans_half_cmtg = int.Parse(strLine.Substring(tmp + 1, strLine.LastIndexOf(";") - tmp - 1));
                        }
                        else if (strLine.Length > ("PPM_FOR_CALIBRATION").Length && strLine.Substring(0, strLine.LastIndexOf("=")).Equals("PPM_FOR_CALIBRATION"))
                        {
                            int tmp = strLine.LastIndexOf("=");
                            _quant.Quant_advanced.Ppm_for_calibration = double.Parse(strLine.Substring(tmp + 1, strLine.LastIndexOf(";") - tmp - 1));
                        }
                        else if (strLine.Length > ("PPM_HALF_WIN_ACCURACY_PEAK").Length && strLine.Substring(0, strLine.LastIndexOf("=")).Equals("PPM_HALF_WIN_ACCURACY_PEAK"))
                        {
                            int tmp = strLine.LastIndexOf("=");
                            _quant.Quant_advanced.Ppm_half_win_accuracy_peak = double.Parse(strLine.Substring(tmp + 1, strLine.LastIndexOf(";") - tmp - 1));
                        }
                        else if (strLine.Length > ("NUMBER_HOLE_IN_CMTG").Length && strLine.Substring(0, strLine.LastIndexOf("=")).Equals("NUMBER_HOLE_IN_CMTG"))
                        {
                            int tmp = strLine.LastIndexOf("=");
                            _quant.Quant_advanced.Number_hole_in_cmtg = int.Parse(strLine.Substring(tmp + 1, strLine.LastIndexOf(";") - tmp - 1)) - 1;
                        }
                        else if (strLine.Length > ("TYPE_SAME_START_END_BETWEEN_EVIDENCE").Length && strLine.Substring(0, strLine.LastIndexOf("=")).Equals("TYPE_SAME_START_END_BETWEEN_EVIDENCE"))
                        {
                            int tmp = strLine.LastIndexOf("=");
                            _quant.Quant_advanced.Type_same_start_end_between_evidence = int.Parse(strLine.Substring(tmp + 1, strLine.LastIndexOf(";") - tmp - 1));
                        }
                        else if (strLine.Length > ("TYPE_PEPTIDE_RATIO").Length && strLine.Substring(0, strLine.LastIndexOf("=")).Equals("TYPE_PEPTIDE_RATIO"))
                        {
                            int tmp = strLine.LastIndexOf("=");
                            _quant.Quant_inference.Type_peptide_ratio = int.Parse(strLine.Substring(tmp + 1, strLine.LastIndexOf(";") - tmp - 1));
                        }
                        else if (strLine.Length > ("TYPE_PROTEIN_RATIO_CALCULATION").Length && strLine.Substring(0, strLine.LastIndexOf("=")).Equals("TYPE_PROTEIN_RATIO_CALCULATION"))
                        {
                            int tmp = strLine.LastIndexOf("=");
                            _quant.Quant_inference.Type_protein_ratio_calculation = int.Parse(strLine.Substring(tmp + 1, strLine.LastIndexOf(";") - tmp - 1));
                        }
                        else if (strLine.Length > ("TYPE_UNIQUE_PEPTIDE_ONLY").Length && strLine.Substring(0, strLine.LastIndexOf("=")).Equals("TYPE_UNIQUE_PEPTIDE_ONLY"))
                        {
                            int tmp = strLine.LastIndexOf("=");
                            _quant.Quant_inference.Type_unique_peptide_only = int.Parse(strLine.Substring(tmp + 1, strLine.LastIndexOf(";") - tmp - 1));
                        }
                        else if (strLine.Length > ("THRESHOLD_SCORE_INTERFERENCE").Length && strLine.Substring(0, strLine.LastIndexOf("=")).Equals("THRESHOLD_SCORE_INTERFERENCE"))
                        {
                            int tmp = strLine.LastIndexOf("=");
                            _quant.Quant_inference.Threshold_score_interference = double.Parse(strLine.Substring(tmp + 1, strLine.LastIndexOf(";") - tmp - 1));
                        }
                        else if (strLine.Length > ("THRESHOLD_SCORE_INTENSITY").Length && strLine.Substring(0, strLine.LastIndexOf("=")).Equals("THRESHOLD_SCORE_INTENSITY"))
                        {
                            int tmp = strLine.LastIndexOf("=");
                            _quant.Quant_inference.Threshold_score_intensity = double.Parse(strLine.Substring(tmp + 1, strLine.LastIndexOf(";") - tmp - 1));
                        }
                        else if (strLine.Length > ("TYPE_GET_GROUP").Length && strLine.Substring(0, strLine.LastIndexOf("=")).Equals("TYPE_GET_GROUP"))
                        {
                            int tmp = strLine.LastIndexOf("=");
                            _quant.Quant_inference.Type_get_group = int.Parse(strLine.Substring(tmp + 1, strLine.LastIndexOf(";") - tmp - 1));
                        }
                    }
                    strLine = sr.ReadLine();
                }
                #endregion
                sr.Close();
                fst.Close();
            }
            catch (Exception exe)
            {
                throw new Exception("[pQuant_read] line: " + strLine + "\n" + exe.Message);
            }
        }
        private void WritePeptides(Dictionary<string, ExperimentGroup> expgroups, string outputDirectory)
        {
            foreach (Peptide pep in Peptides.Values)
            {
                if (pep.ProteinGroups == null || pep.ProteinGroups.Count < 1) continue;
                pep.ProteinGroups.Sort(ProteinGroup.CompareIncreasing);
                pep.BestPG = pep.ProteinGroups[0];
            }
            // Loop over each experiment
            foreach (ExperimentGroup exp in expgroups.Values)
            {
                string experimentName = exp.Name;
                List<CsvFile> files = exp.CsvFiles;
                StreamWriter writer = null;
                if (IncludeNonFilteredResults)
                {
                    string filename = "";
                    if (string.IsNullOrEmpty(experimentName))
                    {
                        filename = Path.Combine(outputDirectory, "Parsimony_peptides.csv");
                    }
                    else
                    {
                        filename = Path.Combine(outputDirectory, string.Format("{0}_parsimony_peptides.csv", experimentName));
                    }
                    Log("Writing file {0}", filename);
                    writer = new StreamWriter(filename);
                }
                string filteredfilename = "";
                if (string.IsNullOrEmpty(experimentName))
                {
                    filteredfilename = Path.Combine(outputDirectory, "Parsimony_peptides_filtered.csv");
                }
                else
                {
                    filteredfilename = Path.Combine(outputDirectory, string.Format("{0}_parsimony_peptides_filtered.csv", experimentName));
                }
                Log("Writing file {0}", filteredfilename);
                StreamWriter filteredwriter = new StreamWriter(filteredfilename);

                string header = exp.Header + ",Experiment Name,# of Sharing PGs,Best PG Name";
                if (IncludeNonFilteredResults)
                {
                    writer.WriteLine(header);
                }
                filteredwriter.WriteLine(header);

                foreach (CsvFile csvfile in files)
                {
                    string sequenceString = "Peptide";
                    //string spectrumNumberString = "Spectrum number";
                    //string pvalueString = "P-value";

                    // Open up the csvfile and read it's contents, skipping the header
                    using (CsvReader reader = new CsvReader(new StreamReader(csvfile.FilePath), true))
                    {
                        bool pdOutput = false;
                        if (reader.GetFieldHeaders().Contains("XCorr"))
                        {
                            sequenceString = "Sequence";
                            //spectrumNumberString = "RT [min]";
                            //pvalueString = "PEP";
                            pdOutput = true;
                        }

                        int deflineIndex = reader.GetFieldIndex("Defline");
                        int modIndex = reader.GetFieldIndex("Mods");
                        int startRes = reader.GetFieldIndex("Start");
                        int stopRes = reader.GetFieldIndex("Stop");
                        Peptide pep = null;
                        // Read in each psm
                        while (reader.ReadNextRecord())
                        {
                            string seq = reader[sequenceString].ToUpper();
                            string leuSeq = seq.Replace('I', 'L');

                            string[] data = new string[reader.FieldCount];
                            if (Peptides.TryGetValue(leuSeq, out pep))
                            {
                                if (pep.BestPG == null || pep.BestPG.IsDecoy) continue;

                                foreach (ProteinGroup pg in pep.ProteinGroups)
                                {
                                    exp.ProteinGroups.Add(pg);
                                    int psms = 0;
                                    if (pg.PsmsPerGroup.TryGetValue(exp, out psms))
                                    {
                                        psms++;
                                        pg.PsmsPerGroup[exp] = psms;
                                    }
                                    else
                                    {
                                        pg.PsmsPerGroup.Add(exp, 1);
                                    }
                                    HashSet<Peptide> peptides = null;
                                    if (pg.UniquePeptidesPerGroup.TryGetValue(exp, out peptides))
                                    {
                                        peptides.Add(pep);
                                    }
                                    else
                                    {
                                        peptides = new HashSet<Peptide>();
                                        peptides.Add(pep);
                                        pg.UniquePeptidesPerGroup.Add(exp, peptides);
                                    }
                                }

                                // If performing quantitation
                                if (exp.UseQuant)
                                {
                                    // Only use unshared Peptides
                                    bool usePeptideInQuant = !pep.IsShared;

                                    if (usePeptideInQuant)
                                    {
                                        string mod_line = reader["Mods"];
                                        if (!string.IsNullOrEmpty(mod_line))
                                        {
                                            string[] mods = mod_line.Split(',');
                                            foreach (string mod in mods)
                                            {
                                                string mod_name = mod.Split(':')[0];
                                                Modification testmod = new Modification(mod_name);
                                                if (ModificationsToIgnore.Contains(testmod))
                                                {
                                                    usePeptideInQuant = false;
                                                    break;
                                                }
                                            }
                                        }
                                    }

                                    if (usePeptideInQuant)
                                    {
                                        Quantitation quant = null;
                                        double[] quantData = new double[4 * (int)exp.QuantPlex];
                                        bool keepPeptide = true;

                                        // Read in all the quant data from the csvfile
                                        int j = 0;
                                        for (int i = exp.TQStart; i <= exp.TQStop; i++)
                                        {
                                            double value = 0;
                                            if (double.TryParse(reader[i], out value))
                                            {
                                                quantData[j] = value;
                                            }
                                            else
                                            {
                                                quantData[j] = 0;
                                            }

                                            if (IgnorePeptideWithMissingData && quantData[j] == 0)
                                            {
                                                keepPeptide = false;
                                                break;
                                            }

                                            j++;
                                        }

                                        int detectedChannels = int.Parse(reader[exp.TQStop + 1]);
                                        int totalChannels = (int) exp.QuantPlex;
                                        bool isMissingChannel = detectedChannels != totalChannels;

                                        if (keepPeptide)
                                        {
                                            //pep.SetQuantData(quantData);

                                            // append/add the quant data to the best and only protein group
                                            if (pep.BestPG.Quantitation.TryGetValue(exp.Name, out quant))
                                            {
                                                quant.AddData(pep, quantData, isMissingChannel);
                                            }
                                            else
                                            {
                                                quant = new Quantitation(exp.QuantPlex, pep, quantData, isMissingChannel);
                                                pep.BestPG.Quantitation.Add(exp.Name, quant);
                                            }
                                        }
                                    }
                                }

                                StringBuilder sb = new StringBuilder();
                                for (int i = 0; i < reader.FieldCount; i++)
                                {
                                    data[i] = reader[i];
                                    if (!pdOutput)
                                    {
                                        if (i == deflineIndex)
                                        {
                                            data[i] = pep.BestPG.RepresentativeProtein.Description;
                                        }
                                        else if (i == modIndex)
                                        {
                                            if (data[i].Contains(','))
                                                data[i] = "\"" + data[i] + "\"";
                                        }
                                        else if (i == startRes)
                                        {
                                            data[i] =
                                                pep.BestPG.RepresentativeProtein.FindStartResidue(pep.LeucineSequence)
                                                    .ToString();
                                        }
                                        else if (i == stopRes)
                                        {
                                            data[i] =
                                                pep.BestPG.RepresentativeProtein.FindStopResidue(pep.LeucineSequence)
                                                    .ToString();
                                        }
                                    }
                                    //if(data[i].Contains(','))
                                    //    data[i] = "\"" + data[i] + "\"";
                                    sb.Append(data[i]);
                                    sb.Append(',');
                                }
                                sb.Append(exp.Name);
                                sb.Append(',');
                                sb.Append(pep.NumberOfSharingProteinGroups);
                                sb.Append(',');
                                foreach (ProteinGroup pg2 in pep.ProteinGroups)
                                {
                                    sb.Append(pg2.Name);
                                    sb.Append('|');
                                }
                                sb.Remove(sb.Length - 1, 1);
                                if (IncludeNonFilteredResults)
                                {
                                    writer.WriteLine(sb);
                                }
                                if (pep.BestPG.PassesFDR)
                                {
                                    filteredwriter.WriteLine(sb);
                                }
                            }
                            else
                            {
                                throw new Exception("Read in a PSM that isn't mapped! Did an input CSV's contents change?");
                            }
                        }
                    }
                }
                if (IncludeNonFilteredResults)
                    writer.Close();
                filteredwriter.Close();
            }
        }
Exemple #5
0
        //generate pQuant.qnt
        void pQuant_Inter.pQuant_write(_Task _task)
        {
            try
            {
                Quantitation      _quant   = _task.T_Quantify;
                pTop.classes.File _file    = _task.T_File;
                string            filepath = _task.Path + "param\\pQuant.cfg";
                FileStream        qfst     = new FileStream(filepath, FileMode.Create, FileAccess.Write);
                StreamWriter      qsw      = new StreamWriter(qfst, Encoding.Default);
                qsw.WriteLine("# This is a standard pQuant configure file.");
                qsw.WriteLine("# Dear user,After '=' and before ';' is the content you can modify.");
                qsw.WriteLine("# Please keep serious when configuring. For some of the options,you can use the default value.");
                qsw.WriteLine("# For help: mail to [email protected]");
                qsw.WriteLine("# Time: " + DateTime.Now.ToString());
                qsw.WriteLine();
                qsw.WriteLine("[INI]");
                qsw.WriteLine("PATH_INI_ELEMENT=" + ConfigHelper.startup_path + "\\element.ini;");
                qsw.WriteLine("PATH_INI_MODIFICATION=" + ConfigHelper.startup_path + "\\modification.ini;");
                qsw.WriteLine("PATH_INI_RESIDUE=" + ConfigHelper.startup_path + "\\aa.ini;");
                qsw.WriteLine();

                qsw.WriteLine("[Performance]");
                qsw.WriteLine("PATH_BIN=" + ConfigHelper.startup_path + ";");
                qsw.WriteLine("NUMBER_MAX_PSM_PER_BLOCK=" + _quant.Quant_advanced.Number_max_psm_per_block + ";");
                qsw.WriteLine("TYPE_START=" + _quant.Quant_advanced.Type_start + ";");
                qsw.WriteLine();

                qsw.WriteLine("[MS1]");
                string ms1path = "";
                for (int i = 0; i < _file.Data_file_list.Count; i++)
                {
                    string tmp = _file.Data_file_list[i].FilePath;
                    ms1path += tmp.Substring(0, tmp.LastIndexOf(".")) + ".pf1|";
                }
                qsw.WriteLine("PATH_MS1=" + ms1path + ";");
                qsw.WriteLine("EXTENSION_TEXT_MS1=" + _quant.Quant_advanced.Extension_text_ms1 + ";");
                qsw.WriteLine();

                qsw.WriteLine("[Identification]");
                #region TODO是否有多个spectra
                #endregion
                string resultfile = ConfigHelper.getFileBySuffix(_task.Path, "spectra");
                qsw.WriteLine("PATH_IDENTIFICATION_FILE=" + resultfile + "|;");
                qsw.WriteLine("TYPE_IDENTIFICATION_FILE=2;");  //0是老pBuild;1是prolucid;2是pFind3.0

                //2014.10.29 chihao:pQuant过滤阈值修改,保持与过滤时一致
                qsw.WriteLine("THRESHOLD_FDR=" + (_task.T_Identify.Filter.Fdr_value / 100.0).ToString() + ";");
                qsw.WriteLine();

                qsw.WriteLine("[Quantitation]");
                //迟浩:定量初始值设置为0,表示标记定量。在搜非标记数据时,也是跑标记流程(画三维图)
                string type_label = "0";
                if (_quant.Quantitation_type == (int)Quant_Type.Label_free)
                {
                    type_label = "1";
                }
                qsw.WriteLine("TYPE_LABEL=" + type_label + ";");
                string labelInfo = "";
                #region 代码冗余
                //迟浩:这一段代码写得不好,建议一起过一遍功能然后优化实现
                if (_quant.Quantitation_type == (int)Quant_Type.Label_None)
                {
                    labelInfo = "1|none";
                }
                else if (_quant.Quantitation_type == (int)Quant_Type.Labeling)  //multiplicity=2
                {
                    int label_num = _quant.Labeling.Multiplicity;
                    labelInfo = label_num.ToString();
                    if (label_num == 1)
                    {
                        labelInfo += "|";
                        if (_quant.Labeling.Medium_label.Count == 0)
                        {
                            labelInfo += "none";
                        }
                        else
                        {
                            labelInfo += _quant.Labeling.Medium_label[0];
                        }
                    }
                    else if (label_num == 2)
                    {
                        if (_quant.Labeling.Light_label.Count == 0 && _quant.Labeling.Heavy_label.Count == 0)
                        {
                            labelInfo = "1|none";
                        }
                        else
                        {
                            labelInfo += "|";
                            if (_quant.Labeling.Light_label.Count == 0)
                            {
                                labelInfo += "none";
                            }
                            else
                            {
                                labelInfo += _quant.Labeling.Light_label[0];
                            }
                            labelInfo += "|";
                            if (_quant.Labeling.Heavy_label.Count == 0)
                            {
                                labelInfo += "none";
                            }
                            else
                            {
                                labelInfo += _quant.Labeling.Heavy_label[0];
                            }
                        }
                    }
                    else if (label_num == 3)
                    {
                        labelInfo += "|";
                        if (_quant.Labeling.Light_label.Count == 0)
                        {
                            labelInfo += "none";
                        }
                        else
                        {
                            labelInfo += _quant.Labeling.Light_label[0];
                        }
                        labelInfo += "|";
                        if (_quant.Labeling.Medium_label.Count == 0)
                        {
                            labelInfo += "none";
                        }
                        else
                        {
                            labelInfo += _quant.Labeling.Medium_label[0];
                        }
                        labelInfo += "|";
                        if (_quant.Labeling.Heavy_label.Count == 0)
                        {
                            labelInfo += "none";
                        }
                        else
                        {
                            labelInfo += _quant.Labeling.Heavy_label[0];
                        }
                    }
                }
                qsw.WriteLine("LL_INFO_LABEL=" + labelInfo + ";");
                #endregion
                string lfec = "none";
                if (_quant.Quant_advanced.Ll_element_enrichment_calibration == (int)LL_ELEMENT_ENRICHMENT_CALIBRATION.N15)
                {
                    lfec = "15N";
                }
                else if (_quant.Quant_advanced.Ll_element_enrichment_calibration == (int)LL_ELEMENT_ENRICHMENT_CALIBRATION.C13)
                {
                    lfec = "13C";
                }
                qsw.WriteLine("LL_ELEMENT_ENRICHMENT_CALIBRATION=" + lfec + ";");
                #region Todo LabelFree
                //Label Free
                #endregion
                qsw.WriteLine("LF_INFO_SAMPLE=; //Label Free暂不考虑");
                qsw.WriteLine();

                qsw.WriteLine("[Evidence]");
                qsw.WriteLine("NUMBER_SCANS_HALF_CMTG=" + _quant.Quant_advanced.Number_scans_half_cmtg + ";");
                qsw.WriteLine("PPM_FOR_CALIBRATION=" + _quant.Quant_advanced.Ppm_for_calibration + ";");
                qsw.WriteLine("PPM_HALF_WIN_ACCURACY_PEAK=" + _quant.Quant_advanced.Ppm_half_win_accuracy_peak + ";");
                qsw.WriteLine("NUMBER_HOLE_IN_CMTG=" + (_quant.Quant_advanced.Number_hole_in_cmtg + 1).ToString() + ";");
                qsw.WriteLine("TYPE_SAME_START_END_BETWEEN_EVIDENCE=" + _quant.Quant_advanced.Type_same_start_end_between_evidence + ";");
                qsw.WriteLine();

                qsw.WriteLine("[Inference]");
                qsw.WriteLine("TYPE_PEPTIDE_RATIO=" + _quant.Quant_inference.Type_peptide_ratio.ToString() + ";");
                qsw.WriteLine("TYPE_PROTEIN_RATIO_CALCULATION=" + _quant.Quant_inference.Type_protein_ratio_calculation + ";");
                qsw.WriteLine("TYPE_UNIQUE_PEPTIDE_ONLY=" + _quant.Quant_inference.Type_unique_peptide_only + ";");
                qsw.WriteLine("THRESHOLD_SCORE_INTERFERENCE=" + _quant.Quant_inference.Threshold_score_interference + ";");
                qsw.WriteLine("THRESHOLD_SCORE_INTENSITY=" + _quant.Quant_inference.Threshold_score_intensity + ";");
                qsw.WriteLine("TYPE_GET_GROUP=" + _quant.Quant_inference.Type_get_group + ";");
                qsw.WriteLine("PATH_FASTA=" + _task.T_Identify.Db.Db_path + ";");
                qsw.WriteLine();

                qsw.WriteLine("[Export]");
                qsw.WriteLine("DIR_EXPORT=" + _task.Path + ";"); //+ "result\\;"
                qsw.WriteLine("FLAG_CREATE_NEW_FOLDER=0;");      //0表示直接使用dir_export的导出目录
                qsw.Close();
                qfst.Close();
            }
            catch (Exception exe)
            {
                throw new Exception(exe.Message);
            }
        }