第一次使用python来处理excel,主要是为了记录下合并单元格,单元格样式
示例:从左边 变成右边,将第一列的 实现合并单元格
图片.png
工具:python3, xlrd ,xlwt
import xlrd
import sys
import xlwt
import os
#根据化疗位点,来提取突变检出
def Chemo25Sites(path):
chemosites = dict()
file = open(path)
lines = file.readlines()
rows = len(lines)
for i in range(1, rows):
aa = lines[i].strip().split('\t')
chr, pos, rsid, ref, wild_type = aa
chemosites[rsid] = wild_type+'\t'+chr+'\t'+pos+'\t'+ref
file.close()
return chemosites
def getChemoVariants(path, vcf):
#vcf : sample.vatriants_all.vcf
chemosites = Chemo25Sites(path)
outfile = open('./chemo_result_genotype.txt','w')
chemovariants = dict()
for key in chemosites.keys():
cmd = 'grep -w {key} {infile}'.format(key=key, infile=vcf)
res = os.popen(cmd).readlines()
if len(res) == 0: #该化疗位点没有检出
value = chemosites[key].split('\t')
genotype, chrom, pos, ref = value
#print('\t'.join([key, chrom, pos, ref, "*", "*", "*", "*", genotype]))
chemovariants[key] = [key, chrom, pos, ref, "*", "*", "*", "*", genotype]
outfile.write('\t'.join([key, chrom, pos, ref, "*", "*", "*", "*", genotype])+'\n')
elif len(res) == 1: #该化疗位点有检出
line = res[0].strip().split('\t')
chr, start, _, ref, alt, depth, ad, freq = line[0:8]
if float(