我正在尝试编写一个简单的脚本,从VCF文件中提取特定的数据,该文件显示基因组序列中的变体。你知道吗
脚本需要从文件中提取头文件,以及snv,同时省略任何indel。变量显示在两列中,ALT和REF。每列用空格隔开。Indels在ALT或REF中有2个字符,SNVs总是有1个字符。你知道吗
到目前为止,我提取的是标题(总是以###开头),而不是任何变量数据。你知道吗
original_file = open('/home/user/Documents/NA12878.vcf', 'r')
extracted_file = open('NA12878_SNV.txt', 'w+')
for line in original_file:
if '##' in line:
extracted_file.write(line)
# Extract SNVs while omitting indels
# Indels will have multiple entries in the REF or ALT column
# The ALT and REF columns appear at position 4 & 5 respectively
for line in original_file:
ref = str.split()[3]
alt = str.split()[4]
if len(ref) == 1 and len(alt) == 1:
extracted_file.write(line)
original_file.close()
extracted_file.close()
有两个问题:
所以我将代码设置为只使用一个循环执行,并将tab作为split参数。你知道吗
Adirmola给出的答案很好,但是您可以通过应用一些修改来提高代码质量:
我在你的文件上用python3.6测试了这个,最终得到了554个snv。 这里使用的一些语法(特别是对于列表解包)可能不适用于较旧的python版本。你知道吗
相关问题 更多 >
编程相关推荐