• PPI_network&calc_ppi


      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
    View Code
     1 # -*- coding: utf-8 -*-
     2 # __author__ = 'JieYao'
     3 
     4 import os
     5 import argparse
     6 from biocluster.config import Config
     7 import shutil
     8 import networkx
     9 
    10 global name_list
    11 name_list = ["0"]
    12 
    13 def search(node_name):
    14     global name_list
    15     for i in range(len(name_list)):
    16         if node_name == name_list[i]:
    17             return i
    18     name_list += [node_name]
    19     return len(name_list)-1
    20             
    21 
    22 parser = argparse.ArgumentParser(description='输入蛋白质相互作用网络,输出网络信息')
    23 parser.add_argument('-i', "--PPI_network", help="输入的PPI网络", required = True)
    24 parser.add_argument('-c', "--cut", help='蛋白相互作用阈值', required = False)
    25 parser.add_argument('-o', "--output", help = "输出文件输出路径", required = True)
    26 #parser.add_argument('-top', "--top", help = "First k important interaction in graph", required = False)
    27 args = vars(parser.parse_args())
    28 
    29 inFile = args["PPI_network"]
    30 outFile = args["output"]
    31 if not args["cut"]:
    32     cut = -1
    33 else:
    34     cut = args["cut"]
    35     
    36 G = networkx.Graph()
    37 with open(inFile, "r") as tmp_file:
    38     data = tmp_file.readlines()
    39 for i in range(1,len(data)):
    40     s = data[i].rstrip().split("	")
    41     if eval(s[15]) >= cut:
    42         G.add_edge(search(s[0]), search(s[1]), weight = eval(s[15]))
    43 
    44 Transitivity = networkx.transitivity(G)
    45 Clustering = networkx.clustering(G)
    46 Degree_distribution = networkx.degree_histogram(G)
    47 Degree_Centrality = networkx.degree_centrality(G)
    48 Closeness_Centrality = networkx.closeness_centrality(G)
    49 Betweenness_Centrality = networkx.betweenness_centrality(G)
    50 with open(os.path.join(args["output"], "protein_interaction_network_degree_distribution.txt"), "w") as tmp_file:
    51     tmp_file.write("Degree	Node_Num
    ")
    52     for i in range(len(Degree_distribution)):
    53         tmp_file.write(str(i)+"	"+str(Degree_distribution[i]))
    54 with open(os.path.join(args["output"], "protein_interaction_network_by_cut.txt"), "w") as tmp_file:
    55     tmp_file.write("Node_Num = " + str(len(G.nodes())) + "
    ")
    56     tmp_file.write("Edge_Num = " + str(len(G.edges())) + "
    ")
    57     tmp_file.write("Node1_Name	Node2_Name	Weight
    ")
    58     for i in G.edges():
    59         tmp_file.write(name_list[i[0]]+"	"+name_list[i[1]]+"	"+str(G[i[0]][i[1]]["weight"])+"
    ")
    60 with open(os.path.join(args["output"], "protein_interaction_network_node_degree.txt"), "w") as tmp_file:
    61     tmp_file.write("Node_ID	Node_Name	Degree
    ")
    62     for i in range(1,len(G)+1):
    63         tmp_file.write(str(i)+"	"+name_list[i]+"	")
    64         tmp_file.write(str(G.degree(i))+"
    ")
    65 with open(os.path.join(args["output"], "protein_interaction_network_centrality.txt"), "w") as tmp_file:
    66     tmp_file.write("Node_ID	Node_Name	Degree_Centrality	")
    67     tmp_file.write("Closeness_Centrality	Betweenness_Centrality
    ")
    68     for i in range(1,len(G)+1):
    69         tmp_file.write(str(i)+"	"+name_list[i]+"	")
    70         tmp_file.write(str(Degree_Centrality[i])+"	")
    71         tmp_file.write(str(Closeness_Centrality[i])+"	")
    72         tmp_file.write(str(Betweenness_Centrality[i])+"
    ")
    73         
    74 with open(os.path.join(args["output"], "protein_interaction_network_clustering.txt"), "w") as tmp_file:
    75     tmp_file.write("Node_ID	Protein_Name	Clustering
    ")
    76     for i in range(1,len(G)+1):
    77         tmp_file.write(str(i)+"	"+name_list[i]+"	"+str(Clustering[i])+"
    ")
    78 
    79 with open(os.path.join(args["output"], "protein_interaction_network_transitivity.txt"), "w") as tmp_file:
    80     tmp_file.write("Transitivity
    ")
    81     tmp_file.write(str(Transitivity)+"
    ")
    calc_ppi
  • 相关阅读:
    cp备份操作时如何忽略指定的目录
    Wordpress“固定链接”页面出现404原因及解决方法
    CentOS7上搭建WEB服务器
    坑爹的云服务安全组
    java spring hibernate
    电脑性能
    android subclipse subversive
    android 开发 程序中下载安装APK文件 问题汇总 解析程序包时出现问题
    android sqlite datetime demo
    android SurfaceView中播放视频 按视频的原始比例播放
  • 原文地址:https://www.cnblogs.com/neverforget/p/5776801.html
Copyright © 2020-2023  润新知