1 # -*- coding: utf-8 -*-
2 # __author__ = 'JieYao'
3 from biocluster.agent import Agent
4 from biocluster.tool import Tool
5 import os
6 import types
7 import subprocess
8 from biocluster.core.exceptions import OptionError
9
10 class PpinetworkAgent(Agent):
11 """
12 需要calc_ppi.py
13 version 1.0
14 author: JieYao
15 last_modified: 2016.8.15
16 """
17
18 def __init__(self, parent):
19 super(PpinerworkAgent, self).__init__(parent)
20 options = [
21 {"name": "ppitable", "type": "infile"},
22 {"name": "cut", "type": "string", "default": "-1"}
23 ]
24 self.add_option(options)
25 self.step.add_steps('PpinetworkAnalysis')
26 self.on('start', self.step_start)
27 self.on('end', self.step.end)
28
29 def step_start(self):
30 self.step.PpinetworkAnalysis.start()
31 self.step.update()
32
33 def step_end(self):
34 self.step.PpinetworkAnalysis.finish()
35 self.step.update()
36
37 def check_options(self):
38 """
39 重写参数检查
40 """
41 if not self.option('ppitable').is_set():
42 raise OptionError('必须提供PPI网络表')
43 if not os.path.exists(self.option('ppitable')):
44 raise OptionError('PPI网络表路径错误')
45 ppi_list = open( self.option('ppitable'), "r").readline.strip().split(" ")
46 if "combined_score" not in ppi_list:
47 raise OptionError("PPI网络表缺少结合分数")
48 if ("yfrom" not in ppi_list) or ("to" not in ppi_list):
49 raise OptionError("PPI网络缺少相互作用蛋白信息")
50 try:
51 eval(self.option('cut'))
52 except:
53 raise OptionError("Cut参数值异常,无法转换")
54 return True
55
56 def set_resource(self):
57 """
58 设置所需资源
59 """
60 self._cpu = 2
61 self._memory = ''
62
63 def end():
64 result_dir = self.add_upload_dir(self.output_dir)
65 result_dir.add_repath_rules([
66 [".", "", "PPI网络分析结果输出目录"],
67 ["./protein_interaction_network_centrality.txt", "txt", "PPI网络中心系数表"],
68 ["./protein_interaction_network_clustering.txt", "txt", "PPI网络节点聚类系数表"],
69 ["./protein_interaction_network_transitivity.txt", "txt", "PPI网络传递性"],
70 ["./protein_interaction_network_by_cut.txt", "txt", "Cut值约束后的PPI网络"]
71 ["./protein_interaction_network_degree_distribution.txt", "txt", "PPI网络度分布表"],
72 ["./protein_interaction_network_node_degree.txt", "txt", "PPI网络节点度属性表"]
73 ])
74 print self.get_upload_files()
75 super(PpinetworkAgent, self).end()
76
77
78 class PpinetworkTool(Tool):
79 def __init__(self, config):
80 super(PpinetworkTool, self).__init__(config)
81 self._version = "1.0.1"
82 self.cmd_path = self.config.SOFTWARE_DIR + "/bioinfo/rna/scripts/calc_ppi.py"
83 self.ppi_table = self.option('ppitable')
84 self.out_files = ['protein_interaction_network_centrality.txt', 'protein_interaction_network_clustering.txt', 'protein_interaction_network_transitivity.txt', 'protein_interaction_network_by_cut.txt', 'protein_interaction_network_degree_distribution.txt', 'protein_interaction_network_node_degree.txt']
85
86 def run(self):
87 """
88 运行
89 """
90 super(PpinetworkTool, self).run()
91 self.run_ppi_network_py()
92
93 def run_ppi_network_py(self):
94 """
95 运行calc_ppi.py
96 """
97 real_ppi_table = self.ppi_table
98 cmd = self.config.SOFTWARE_DIR + '.program/Python/bin/python'
99 cmd += self.cmd_path
100 cmd += " -i %s -o %s" %(real_ppi_table, self.work_dir + '.ppi_network')
101 if self.option("cut").is_set:
102 cmd += " -c %s" %(self.option('cut'))
103 self.logger.info("开始运行calc_ppi.py")
104
105 try:
106 subprocess.check_output(cmd, shell=True)
107 self.logger.info('PPI_Network计算完成')
108 except subprocess.CalledProcessError:
109 self.logger.info('PPI_Network计算失败')
110 self.ser_error("运行calc_ppi.py失败")
111 allfiles = self.get_filesname()
112 for i in range(len(self.out_files)):
113 self.linkfile(allfiles[i], self.out_files[i])
114 self.end()
115
116 def linkfile(self, oldfile, newname):
117 """
118 link文件到output文件夹
119 :param oldfile 资源文件路径
120 :param newname 新的文件名
121 :return
122 """
123 newpath = os.path.join(self.output_dir, newname)
124 if os.path.exists(newpath):
125 os.remove(newpath)
126 os.link(oldfile, newpath)
127
128 def get_filesname(self):
129 files_status = [None, None, None, None, None, None]
130 for paths,d,filelist in os.walk(self.work_dir + '/ppi_network'):
131 for filename in filelist:
132 name = os.path.join(paths, filename)
133 for i in range(len(self.out_files)):
134 if self.out_files[i] in name:
135 files_status[i] = name
136 for i in range(len(self.out_files)):
137 if not files_status[i]:
138 self.set_error('未知原因,结果文件生成出错或丢失')
139 return files_status