Exemplo n.º 1
0
    def _write_fdr_result(self, conf, fdr_score):
        resFunc = CResultFunction(conf)
        taskid = resFunc.get_taskid(conf, False)
        res_file_list = resFunc.find_single_res_file(conf)

        pep_score, pep_line = {}, {}
        head_line = ""
        title_score, title_line = {}, {}
        for file in res_file_list:
            with open(file) as f:
                lines = f.readlines()
            head_line = lines[0].strip()
            for i in range(1, len(lines)):
                line = lines[i].strip()
                tmp = line.split('\t')
                title = tmp[0]
                sq_key = tmp[2] + '@' + tmp[4] + '@' + str(
                    int(float(tmp[3]) + 0.5))
                is_target = (tmp[8] == "True")
                if not is_target: continue
                s = float(tmp[5])
                if s <= fdr_score: break
                title_score[title] = s
                title_line[title] = line
                if sq_key not in pep_score or pep_score[sq_key] < s:
                    pep_score[sq_key] = s
                    pep_line[sq_key] = line

        sorted_res = sorted(title_score.items(),
                            key=lambda d: d[1],
                            reverse=True)
        write_path = os.path.join(conf.result_folder,
                                  conf.SINGLE_PSM + '-' + taskid + '.txt')
        conf.single_res_path = write_path
        with open(write_path, 'w') as f:
            f.write(head_line + '\n')
            for (r, s) in sorted_res:
                one_line = title_line[r]
                f.write(one_line + '\n')

        sorted_res = sorted(pep_score.items(),
                            key=lambda d: d[1],
                            reverse=True)
        write_path = os.path.join(conf.result_folder,
                                  conf.SINGLE_PEP + '-' + taskid + '.txt')
        with open(write_path, 'w') as f:
            f.write(head_line + '\n')
            for (r, s) in sorted_res:
                one_line = pep_line[r]
                f.write(one_line + '\n')
Exemplo n.º 2
0
 def _compute_fdr_score(self):
     conf = self.conf
     res_fun = CResultFunction(conf)
     cand_file_list = res_fun.find_single_res_file(conf)
     print("[Info] Candidate file list", cand_file_list)
     psm_list = []
     for file_path in cand_file_list:
         psm_list += self._load_result(file_path)
     psm_list.sort(key=lambda k: k[1].score, reverse=True)  #按照score排序
     target_num, decoy_num, all_num = 0, 0, len(psm_list)
     for (title, pep, decoy, ranker, delta_score) in psm_list:
         if decoy:
             decoy_num += 1
         else:
             target_num += 1
     print("[Info] #PSMs is {0}, #Target is {1} and #Decoy is {2}".format(
         all_num, target_num, decoy_num))
     cur_target_num, cur_decoy_num = 0, 0
     if len(psm_list) == 0: return 0.0
     score_t = psm_list[0][1].score + 1.0
     if target_num > 0 and decoy_num * 1.0 / target_num <= conf.fdr_value:
         score_t = psm_list[-1][1].score
         return score_t
     for (title, pep, decoy, ranker, delta_score) in psm_list[::-1]:
         if decoy:
             cur_decoy_num += 1
         else:
             cur_target_num += 1
         if target_num == cur_target_num:
             fdr_value = 100
         else:
             fdr_value = (decoy_num - cur_decoy_num) * 1.0 / (
                 target_num - cur_target_num)
         # print(decoy_num,cur_decoy_num, target_num, cur_target_num, fdr_value)
         if fdr_value <= conf.fdr_value:
             score_t = pep.score
             return score_t
     return score_t