python34

import re
import os
import sys
import argparse
import gzip

parser = argparse.ArgumentParser(description="pipeline")
parser.add_argument('-i', '--input', help = 'the pathway of the input vcf file ', required = True)
parser.add_argument('-o', '--output', help = 'the pathway of the output vcf file,filter vcf', required = True)
argv = vars(parser.parse_args())
ifile = os.path.abspath(argv['input'].strip())
ofile = os.path.abspath(argv['output'].strip())

chrID=['1','2','3','4','5','6','7','8','9','10','11','12','13','14','15','16','17','18','19','20','21','22','X','Y','MT']

oc=open(ofile,'w')
vfile = gzip.open(ifile,'rb')
for vi in vfile.readlines():
    vic = vi.decode()
    if vic.startswith('#'):
        if 'contig=<ID' in vic:
            vi1=vi.strip().split('=')[2]
            vi2=vi1.strip().split(',')[0]
            if vi2 in chrID:
                oc.write(str(vi))
        else:
            oc.write(str(vi))
    elif str(vic.strip().split('\t')[0]) in chrID and 'RefCall' not in vic:
        oc.write(str(vi))

每一次寫腳本都是在細節(jié)中不斷優(yōu)化

import re
import os
import sys
import argparse
import gzip

parser = argparse.ArgumentParser(description="pipeline")
parser.add_argument('-i', '--input', help = 'the pathway of the input vcf file ', required = True)
parser.add_argument('-o', '--output', help = 'the pathway of the output vcf file,filter vcf', required = True)
argv = vars(parser.parse_args())
ifile = os.path.abspath(argv['input'].strip())
ofile = os.path.abspath(argv['output'].strip())

chrID=['1','2','3','4','5','6','7','8','9','10','11','12','13','14','15','16','17','18','19','20','21','22','X','Y','MT']

oc=gzip.open(ofile,'wb')
vfile = gzip.open(ifile,'rb')
for vi in vfile.readlines():
    vic = vi.decode()
    if vic.startswith('#'):
        if 'contig=<ID' in vic:
            vi1=vi.strip().split('=')[2]
            vi2=vi1.strip().split(',')[0]
            if vi2 in chrID:
                oc.write(str(vic))
        else:
            oc.write(str(vic))
    elif str(vic.strip().split('\t')[0]) in chrID and 'RefCall' not in str(vic):
        oc.write(str(vic))

vfile.close()
oc.close()
最后編輯于
?著作權歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

相關閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容