gpt4 book ai didi

python - 如何填充 TSV 文件中缺失的序列行

转载 作者:太空宇宙 更新时间:2023-11-03 15:58:51 24 4
gpt4 key购买 nike

我仍然是一个初学者,所以对于初学者来说,对于这个问题可能有一个明显的答案感到抱歉,对于困惑的代码感到抱歉,但我有一万行的文件。我正在使用某种窗口框架技术来沿着我的文件滑动,因此我需要确保每个窗口都在其中。然而,我的一些输入文件遗漏了某些行,所以我尝试用 Python 编写代码来添加这些行和我想要的信息,以使文件完整。这是代码的样子:

#!/usr/bin/env python

outfile = open ("missing_test.txt", "w")

with open("add_missing.txt", "r") as file:
last_line = 0 #This is where it starts for bin 1
lines = []
header_line = next(file)
outfile.write(header_line)
CHROM = 'BABA_1'
for line in file: #go through every line to check its existence and rewrite to new file
nums = line.split("\t")
num1 = nums[0] #no integer because this is a string: name individual
num2 = int(nums[1]) #integer for window
num3 = int(nums[2]) #integer for coverage (here always 10000 to met treshold)
num4 = int(nums[3]) #integer for SNP count
if num1 == CHROM: #
while num2 != last_line + 10000:
#A line is missing, so a new line is added with 0 SNPs:
NUM2 = last_line + 10000 # New window, the one that was missing
NUM4 = 0 #0 SNPs found
#lines.append((num1, NUM2, num3, NUM4))
OUTLINE = "%s\t%s\t%s\t%s" % (num1, NUM2, num3, NUM4) #write new line to outfile
outfile.write(OUTLINE + "\n")
last_line += 10000
lines.append((num1,num2,num3,num4))
last_line += 10000 #also add 10000 here otherwise the while loop makes no sense
outline = "%s\t%s\t%s\t%s" % (num1, num2, num3, num4)
outfile.write(outline + "\n") #write all existing lines to outfile

else:
CHROM = num1
last_line = 0

outfile.close()

因此,只要第一个“CHROM”的第一个窗口等于 0,这就可以很好地工作,但情况并非总是如此。在后一种情况下,循环将是无限的。这是例如输入和 DESIRED 输出的样子:

输入:

indiv   window  coverage    SNP
BABA_1 20000 10000 7
BABA_1 30000 10000 1
BABA_1 50000 10000 2
BABA_1 60000 10000 3
BABA_1 80000 10000 1
BABA_10 20000 10000 1
BABA_10 30000 10000 16
BABA_10 80000 10000 9

期望的输出:

indiv   window  coverage    SNP
BABA_1 10000 10000 0
BABA_1 20000 10000 7
BABA_1 30000 10000 1
BABA_1 40000 10000 0
BABA_1 50000 10000 2
BABA_1 60000 10000 3
BABA_1 70000 10000 0
BABA_1 80000 10000 1
BABA_10 10000 10000 0
BABA_10 20000 10000 1
BABA_10 30000 10000 16
BABA_10 40000 10000 0
BABA_10 50000 10000 0
BABA_10 60000 10000 0
BABA_10 70000 10000 0
BABA_10 80000 10000 9

我一直在努力寻找答案来让我的 while 循环工作而不是无限地继续下去,但我真的没有看到我的缺陷。有没有人告诉我如何解决这个问题?

非常感谢任何帮助,提前致谢!

最佳答案

按照这些思路尝试一些事情:

#!/usr/bin/python

outfile = open ("missing_test.txt", "w")

def write_line(indiv, window, coverage, snp):
outline = "%s\t%s\t%s\t%s\n" % (indiv, window, coverage, snp)
outfile.write(outline)

with open("add_missing.txt", "r") as file:
lines = file.readlines()
write_line(*lines.pop(0).rstrip().split("\t"))
first_line = lines[0].split("\t")
last_indiv = first_line[0]
last_window = int(first_line[1])

for line in lines:
indiv, window, coverage, snp = line.split("\t")
window = int(window)
coverage = int(coverage)
snp = int(snp)

if indiv == last_indiv:
# If the current window is higher than expected,
# insert a line with the missing window.
# Repeat until we get to the expected window.
while window > last_window + 10000:
write_line(indiv, last_window + 10000, coverage, 0)
last_window += 10000
last_window = window
else:
last_indiv = indiv
last_window = window
write_line(indiv, window, coverage, snp)

它不包含期望某个窗口编号是给定 indiv 中的第一个窗口,因为您没有定义该行为并且您对此的评论相当困惑。


运行此脚本后 missing_test.txt 的内容:

indiv window  coverage    SNP
BABA_1 20000 10000 7
BABA_1 30000 10000 1
BABA_1 40000 10000 0
BABA_1 50000 10000 2
BABA_1 60000 10000 3
BABA_1 70000 10000 0
BABA_1 80000 10000 1
BABA_10 20000 10000 1
BABA_10 30000 10000 16
BABA_10 40000 10000 0
BABA_10 50000 10000 0
BABA_10 60000 10000 0
BABA_10 70000 10000 0
BABA_10 80000 10000 9

关于python - 如何填充 TSV 文件中缺失的序列行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41698817/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com