注释信息合并处理脚本

将所有的注释信息合并到一块,先写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`"

你可能感兴趣的:(注释信息合并处理脚本)