PPI_network&calc_ppi
# -*- 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的更多相关文章
随机推荐
- work5
这一次写的内容是黄金豆小游戏,由于现在偏重写服务器端.对于算法层面其实涉及不多,更多偏于工程上的架构. 总而言之本次作业的服务器核心是用web.py所写,而且为了方便其他用户写客户端,架构非常简单. ...
- PID参数整定快速入门(调节器参数整定方法)
PID调节器参数整定方法很多,常见的工程整定方法有临界比例度法.衰减曲线法和经验法.云润仪表以图文形式分别介绍调节器参数整定方法. 临界比例度法一个调节系统,在阶跃干扰作用下,出现既不发散也不衰减的等 ...
- AutoCAD.NET二次开发:创建自定义菜单(AcCui)
从CAD2007之后,Autodesk提供了一个新的程序集AcCui.dll,使用这个程序集,我们可以方便地做一些界面方面的操作,比如创建自定义菜单. 下面介绍一下菜单的创建过程: 1.在项目中添加引 ...
- [C语言 - 7] 结构体struct
A. 基本知识 与数组的对比 数组: 构造类型 只能有多个相同类型的数据构成 结构体: 结构体类型 可以由多个不同类型的数据构成 1. 定义类型 struct Student { int ...
- C#学习笔记(六):可空类型、匿名方法和迭代器
可空类型 为啥要引入可空类型? 在数据库中,字段是可以为null值的,那么在C#中为了方便的操作数据库的值,微软引入了可空类型. 声明可空类型 我们可以使用两种方法声明一个可空类型: Nullable ...
- C++中动态申请二维数组并释放方法
C/C++中动态开辟一维.二维数组是非常常用的,以前没记住,做题时怎么也想不起来,现在好好整理一下. C++中有三种方法来动态申请多维数组 (1)C中的malloc/free (2)C++中的new/ ...
- iOS中多控制器的使用
通常情况下,一个app由多个控制器组成,当app中有多个控制器的时候,我们就需要对这些控制器进行管理. 在开发过程中,当有多个View时,可以用一个大的view去管理多个小的view,控制器也是如此, ...
- 【LINUX】编程笔记
a storage class can only be specified for objects and functions extern修饰一个struct报错,错误原因如上,C++中存储类修 ...
- const修饰的双重指针赋值解惑
在c程序中,我们可能经常会使用到指针之间的赋值. 传统的赋值操作: char *cp = "c"; const char *ccp; ccp = cp; printf(" ...
- mysql5.5主从配置
mysql主从同步# 一:mysql数据库的主从 mysql数据库5.5之后的版本和5.5以前的版本数据库主从存在差异,这里是针对数据库5.5之后的配置. 1.主库编辑my.cnf(linux的my. ...