【数据处理】python将GO注释结果整理为WEGO文件

通常,比对NR库后为m8格式,通过NR和GO数据库对应关系文件,写代码整理为Gene——>GO文件,如下:
image.png
这里是一对一的关系,要转换为WEGO格式文件,即一对多关系,如下:
image.png
用python脚本处理代码示例如下:

#! /usr/bin/env python
import optargs
import os
import sys

usage='''
    tidy go annotation file to wego file
'''

option = optparse.OptionParser(usage)
option.add_option('','--infile',help='* annot file',default='')
option.add_option('','--outfile',help='* outfile',default='')

(opts,args) = option.parse_args()
infile = opts.infile
outfile = opts.outfile

def main():
    acc2go = {}
    res = open(outfile,'w')
    for line in open(infile,'r'):
        line = line.strip().split('	')
        if len(line) < 2: continue
        accession = line[0]
        go = line[1]
        acc2go.setdefault(accession, set()).add(go)

    for acc,goi in acc2go.items():
        res.write("%s	%s
"%(acc, "	".join(goi)))
    res.close()

if __name__ == '__main__':
    if len(sys.argv) < 2:
        os.system("python %s -h"%(sys.args[0]))
        sys.exit(1)
    else:
        main()

原文地址:https://www.cnblogs.com/jessepeng/p/13205633.html