# -*- coding: utf-8 -*-
# __author__ = 'JieYao'
from biocluster.agent import Agent
from biocluster.tool import Tool
import os
import types
import subprocess
from biocluster.core.exceptions import OptionError class PpinetworkAgent(Agent):
"""
需要calc_ppi.py
version 1.0
author: JieYao
last_modified: 2016.8.15
""" def __init__(self, parent):
super(PpinerworkAgent, self).__init__(parent)
options = [
{"name": "ppitable", "type": "infile"},
{"name": "cut", "type": "string", "default": "-1"}
]
self.add_option(options)
self.step.add_steps('PpinetworkAnalysis')
self.on('start', self.step_start)
self.on('end', self.step.end) def step_start(self):
self.step.PpinetworkAnalysis.start()
self.step.update() def step_end(self):
self.step.PpinetworkAnalysis.finish()
self.step.update() def check_options(self):
"""
重写参数检查
"""
if not self.option('ppitable').is_set():
raise OptionError('必须提供PPI网络表')
if not os.path.exists(self.option('ppitable')):
raise OptionError('PPI网络表路径错误')
ppi_list = open( self.option('ppitable'), "r").readline.strip().split("\t")
if "combined_score" not in ppi_list:
raise OptionError("PPI网络表缺少结合分数")
if ("yfrom" not in ppi_list) or ("to" not in ppi_list):
raise OptionError("PPI网络缺少相互作用蛋白信息")
try:
eval(self.option('cut'))
except:
raise OptionError("Cut参数值异常,无法转换")
return True def set_resource(self):
"""
设置所需资源
"""
self._cpu = 2
self._memory = '' def end():
result_dir = self.add_upload_dir(self.output_dir)
result_dir.add_repath_rules([
[".", "", "PPI网络分析结果输出目录"],
["./protein_interaction_network_centrality.txt", "txt", "PPI网络中心系数表"],
["./protein_interaction_network_clustering.txt", "txt", "PPI网络节点聚类系数表"],
["./protein_interaction_network_transitivity.txt", "txt", "PPI网络传递性"],
["./protein_interaction_network_by_cut.txt", "txt", "Cut值约束后的PPI网络"]
["./protein_interaction_network_degree_distribution.txt", "txt", "PPI网络度分布表"],
["./protein_interaction_network_node_degree.txt", "txt", "PPI网络节点度属性表"]
])
print self.get_upload_files()
super(PpinetworkAgent, self).end() class PpinetworkTool(Tool):
def __init__(self, config):
super(PpinetworkTool, self).__init__(config)
self._version = "1.0.1"
self.cmd_path = self.config.SOFTWARE_DIR + "/bioinfo/rna/scripts/calc_ppi.py"
self.ppi_table = self.option('ppitable')
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'] def run(self):
"""
运行
"""
super(PpinetworkTool, self).run()
self.run_ppi_network_py() def run_ppi_network_py(self):
"""
运行calc_ppi.py
"""
real_ppi_table = self.ppi_table
cmd = self.config.SOFTWARE_DIR + '.program/Python/bin/python'
cmd += self.cmd_path
cmd += " -i %s -o %s" %(real_ppi_table, self.work_dir + '.ppi_network')
if self.option("cut").is_set:
cmd += " -c %s" %(self.option('cut'))
self.logger.info("开始运行calc_ppi.py") try:
subprocess.check_output(cmd, shell=True)
self.logger.info('PPI_Network计算完成')
except subprocess.CalledProcessError:
self.logger.info('PPI_Network计算失败')
self.ser_error("运行calc_ppi.py失败")
allfiles = self.get_filesname()
for i in range(len(self.out_files)):
self.linkfile(allfiles[i], self.out_files[i])
self.end() def linkfile(self, oldfile, newname):
"""
link文件到output文件夹
:param oldfile 资源文件路径
:param newname 新的文件名
:return
"""
newpath = os.path.join(self.output_dir, newname)
if os.path.exists(newpath):
os.remove(newpath)
os.link(oldfile, newpath) def get_filesname(self):
files_status = [None, None, None, None, None, None]
for paths,d,filelist in os.walk(self.work_dir + '/ppi_network'):
for filename in filelist:
name = os.path.join(paths, filename)
for i in range(len(self.out_files)):
if self.out_files[i] in name:
files_status[i] = name
for i in range(len(self.out_files)):
if not files_status[i]:
self.set_error('未知原因,结果文件生成出错或丢失')
return files_status
 # -*- coding: utf-8 -*-
# __author__ = 'JieYao' import os
import argparse
from biocluster.config import Config
import shutil
import networkx global name_list
name_list = [""] def search(node_name):
global name_list
for i in range(len(name_list)):
if node_name == name_list[i]:
return i
name_list += [node_name]
return len(name_list)-1 parser = argparse.ArgumentParser(description='输入蛋白质相互作用网络,输出网络信息')
parser.add_argument('-i', "--PPI_network", help="输入的PPI网络", required = True)
parser.add_argument('-c', "--cut", help='蛋白相互作用阈值', required = False)
parser.add_argument('-o', "--output", help = "输出文件输出路径", required = True)
#parser.add_argument('-top', "--top", help = "First k important interaction in graph", required = False)
args = vars(parser.parse_args()) inFile = args["PPI_network"]
outFile = args["output"]
if not args["cut"]:
cut = -1
else:
cut = args["cut"] G = networkx.Graph()
with open(inFile, "r") as tmp_file:
data = tmp_file.readlines()
for i in range(1,len(data)):
s = data[i].rstrip().split("\t")
if eval(s[15]) >= cut:
G.add_edge(search(s[0]), search(s[1]), weight = eval(s[15])) Transitivity = networkx.transitivity(G)
Clustering = networkx.clustering(G)
Degree_distribution = networkx.degree_histogram(G)
Degree_Centrality = networkx.degree_centrality(G)
Closeness_Centrality = networkx.closeness_centrality(G)
Betweenness_Centrality = networkx.betweenness_centrality(G)
with open(os.path.join(args["output"], "protein_interaction_network_degree_distribution.txt"), "w") as tmp_file:
tmp_file.write("Degree\tNode_Num\n")
for i in range(len(Degree_distribution)):
tmp_file.write(str(i)+"\t"+str(Degree_distribution[i]))
with open(os.path.join(args["output"], "protein_interaction_network_by_cut.txt"), "w") as tmp_file:
tmp_file.write("Node_Num = " + str(len(G.nodes())) + "\n")
tmp_file.write("Edge_Num = " + str(len(G.edges())) + "\n")
tmp_file.write("Node1_Name\tNode2_Name\tWeight\n")
for i in G.edges():
tmp_file.write(name_list[i[0]]+"\t"+name_list[i[1]]+"\t"+str(G[i[0]][i[1]]["weight"])+"\n")
with open(os.path.join(args["output"], "protein_interaction_network_node_degree.txt"), "w") as tmp_file:
tmp_file.write("Node_ID\tNode_Name\tDegree\n")
for i in range(1,len(G)+1):
tmp_file.write(str(i)+"\t"+name_list[i]+"\t")
tmp_file.write(str(G.degree(i))+"\n")
with open(os.path.join(args["output"], "protein_interaction_network_centrality.txt"), "w") as tmp_file:
tmp_file.write("Node_ID\tNode_Name\tDegree_Centrality\t")
tmp_file.write("Closeness_Centrality\tBetweenness_Centrality\n")
for i in range(1,len(G)+1):
tmp_file.write(str(i)+"\t"+name_list[i]+"\t")
tmp_file.write(str(Degree_Centrality[i])+"\t")
tmp_file.write(str(Closeness_Centrality[i])+"\t")
tmp_file.write(str(Betweenness_Centrality[i])+"\n") with open(os.path.join(args["output"], "protein_interaction_network_clustering.txt"), "w") as tmp_file:
tmp_file.write("Node_ID\tProtein_Name\tClustering\n")
for i in range(1,len(G)+1):
tmp_file.write(str(i)+"\t"+name_list[i]+"\t"+str(Clustering[i])+"\n") with open(os.path.join(args["output"], "protein_interaction_network_transitivity.txt"), "w") as tmp_file:
tmp_file.write("Transitivity\n")
tmp_file.write(str(Transitivity)+"\n")

calc_ppi

PPI_network&calc_ppi的更多相关文章

随机推荐

  1. 第二百四十二天 how can I 坚持

    今天... 貌似没啥啊. 第一天带帽子上班. 还有回来买了两个柚子吃了,有点上火啊. 还有今天雾霾爆表啊,pm2.5  600多啊. 还有看了部电影<蚁人>,挺好看.希望不会出二.三.四. ...

  2. Android View事件传递机制

    ViewGroup dispatchTouchEvent onInterceptTouchEvent onTouch View dispatchTouchEvent onTouch 假设View的层级 ...

  3. 新浪云sae 邮件服务 quicksend()

    <?php header("Content-Type: text/html;charset=utf-8"); $mail = new SaeMail(); $form_Con ...

  4. javascript 获取HTML DOM父、子、临近节点

    在Web应用程序特别是Web2.0程序开发中,经常要获取页面中某个元素,然后更新该元素的样式.内容等.如何获取要更新的元素,是首先要解决的问题.令人欣慰的是,使用JavaScript获取节点的方法有很 ...

  5. 对iOS中MVC的理解

    总结于斯坦福大学的白头发老头的公开课 模型-控制器-视图(MVC)是一种将应用中给所有类组织起来的策略 模型(Model)实际上考虑的是“什么”的问题,即你的程序是什么? 以纸牌匹配游戏为例子,模型就 ...

  6. uva11324 The Largest Clique --- 强连通+dp

    给一个有向图G,求一个子图要求当中随意两点至少有一边可达. 问这个子图中最多含多少个顶点. 首先找SCC缩点建图.每一个点的权值就是该点包括点的个数. 要求当中随意两点可达,实际上全部边仅仅能同方向, ...

  7. ZOJ 1151 Word Reversal反转单词 (string字符串处理)

    链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=151 For each list of words, output a l ...

  8. PS常见错误-无法完成请求,因为文件格式模块不能解析该文件

    无法完成请求,因为文件格式模块不能解析该文件 将图片格式变成.jpg格式就可以了

  9. 2015南阳CCPC D - Pick The Sticks dp

    D - Pick The Sticks Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 无 Description The story happened lon ...

  10. Codeforces Gym 100286G Giant Screen 水题

    Problem G.Giant ScreenTime Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://acm.hust.edu.cn/vjudge/con ...