注释信息合并处理脚本

将所有的注释信息合并到一块,先写python脚本,再用shell进行批处理
test.py

import sys
import os
import os.path
filename = sys.argv[1]
#filename="xaa"
def readfile(file):
    f = open(file)
    h = f.readlines()
    return h
def readfile2(dir):
    hh = []
    for parent,dirnames,filenames in os.walk(dir):
        for file in filenames:
            f = open(os.path.join(dir,file),'rb')
            h = f.readlines()
            f.close()
            hh.extend(h)
    return hh
def makejson(pro,h,j):
    find = 0
    str = "{"
    i=j
    while (i<=len(h)-1) and (pro in h[i]):
        if find == 0:
            find = 1
            str+="[" + h[i][:-1].replace("\t", ",") + "]"
        else:
            str += ",[" + h[i][:-1].replace("\t", ",") + "]"
        i = i+1
    if find==1:
        return [str+"}", i]
    else:
        return ["null", i]

dir = "fungi_data"
dir1 = "fungi_mw_ip_res"
dir2 = "fungi-cazyme"
dir3 = "fungi-pdb"
dir4 = "fungi-swiss"
dir5 = "fungi-lipop"
dir6 = "fungi-targetp"

dir_w = "fungi_z"

file = os.path.join(dir,filename)
file1 = os.path.join(dir1,filename)
file2 = os.path.join(dir2,"diamond.out."+filename)
file3 = os.path.join(dir3,"pdb.out."+filename)
file4 = os.path.join(dir4,"swiss.out."+filename)
file5 = os.path.join(dir5,filename)
newdir6 = os.path.join(dir6,filename)
fw = open(os.path.join(dir_w,filename),"w")

h_data = readfile(file)
h_mp = readfile(file1)
h_cazy = readfile(file2)
h_pdb = readfile(file3)
h_swiss = readfile(file4)
h_lipop = readfile(file5)
h_targetp = readfile2(newdir6)
#print (h_targetp)
mi=0
ci=0
pi=0
si=0

pro="xx"
seq="xx"

for i in range(len(h_data)):
    if h_data[i][0] == ">":
        pro = h_data[i][1:-1]
    else:
        seq = h_data[i][:-1]
        pro_name = pro.split("|")[-1]
        pro_id = pro.split("|")[2]
        mw="null"
        ip="null"
        for mi in range(len(h_mp)):
            if pro_name in h_mp[mi]:
                mw = h_mp[mi+2].split()[3]
                ip = h_mp[mi+4].split(" = ")[1][:-1]
                #print (mw+ip)
                break
        [str_cazy, ci]=makejson(pro, h_cazy, ci)
        [str_pdb, pi] = makejson(pro, h_pdb, pi)
        [str_swiss, si] = makejson(pro, h_swiss, si)
        mytype="null"
        score="null"
        margin = "null"
        cleavage="null"
        for li in range(len(h_lipop)):
            if h_lipop[li][0]=="#" and (pro in h_lipop[li]):
                line = h_lipop[li][:-1].split(" ")
                mytype = line[2]
                score  = line[3].split("=")[1]
                if len(line)>=6:
                    margin = line[4].split("=")[1]
                    cleavage = line[5].split("=")[1]
                break
        mylen = "null"
        mTP = "null"
        SP = "null"
        other = "null"
        Loc = "null"
        RC = "null"
        for ti in range(len(h_targetp)):
            if h_targetp[ti].decode()[0]=='j':
                line = h_targetp[ti][:-1].decode().split()
                if len(pro)>20:
                    tmp_pro = pro[0:20]
                else:
                    tmp_pro = pro
                if line[0] == tmp_pro:
                   # print (line)
                    mylen = line[1]
                    mTP = line[2]
                    SP = line[3]
                    other = line[4]
                    Loc = line[5]
                    RC = line[6]
                    break
        wstr = pro + "\t" + seq + "\t" + mw + "\t" + ip + "\t" + str_cazy + "\t" + str_pdb + "\t" + str_swiss + "\t" + mytype + "\t" + score + "\t" + margin + "\t" + cleavage +"\t"+ mylen + "\t" + mTP + "\t" + SP + "\t" + other + "\t" + Loc + "\t" + RC + "\n"
        fw.write(wstr)
        #print(wstr)

2.批处理,约50个进程

#!/bin/bash
start=`date +%s` #定义脚本运行的开始时间

for file in ~/huangle/fungi_annotation/fungi_data/*
do
{
   python test.py ${file##*/}

        echo 'success annotation '$file' !';
 }&
done
wait
end=`date +%s`

echo "TIME:`expr $end - $start`"

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 官网 中文版本 好的网站 Content-type: text/htmlBASH Section: User ...
    不排版阅读 4,464评论 0 5
  • [TOC] 简介: Shell 是一个用C语言编写的程序,它是用户使用Linux的桥梁。Shell既是一种命令语言...
    黄海滨_x阅读 861评论 0 2
  • 我自己的心路历程来说,破除鸡汤思维就是从认知“自私的基因”开始的。你不需要也不可能拥有和记忆非常多知识,也没啥用。...
    v孩子爸阅读 190评论 0 0
  • 窗外 小雨淅淅沥沥 叹一场秋雨寒意袭 猛然想起 那久违的花草 爱怜起心底 修枝浇水施肥 毫不迟疑精心打理 花草报以...
    富华杰阅读 4,664评论 70 241
  • 如果你也在失眠,不妨想一想过去的岁月。 时光荏苒,白驹过隙,感动却依然热烈。 如果你也在失眠,请想一想曾经的梦想。...
    春春春春节阅读 185评论 0 0