我正试图使用Python编写一个程序,该程序从文件#1(contig_numbers)中获取单词列表,并通过在两个不同的文件(#2注释和#3丰度)中逐行迭代来查找该单词。如果在两个文件中找到单词,则输出将是来自文件#2的行(注释)、制表符分隔和来自文件#3的行(丰度)。如果这个单词是在文件#2中找到的,而不是在#3中,那么输出将只是文件#2中的一行(只是注释)。这个词也可以在文件#2中找到很多次(见下面的例子)
我向你展示了我写的代码,但它似乎没有像我希望的那样工作:
#!/usr/bin/env python3
# coding: utf-8
import re
import sys
annotation = open('annotation', 'r')
abundance = open('abundance', 'r')
with open('contig_numbers', 'r') as f:
keywords = ([line.strip() for line in f])
new_file = open ('/home/Bureau/test-script/output_file_test', 'w')
for line in annotation:
line1 = (line)
for word in keywords:
if re.match (r"b"+word+r"b" , line1):
match1 = (line1.strip())
for line2 in abundance:
line2 =(line2)
if re.match (r"b"+word+r"b" , line1):
match2= (line2.strip())
print (match1+"t"+match2, file = new_file)
break
文件示例:contig_numbers
contig-3
contig-2
contig-1
contig-10
contig-14
contig-27
注释
contig-1 out.27-Actinomycetales gene_id_947 NULL NULL NULL NULL NULL NULL
contig-1 out.27-Actinomycetales gene_id_948 NULL NULL NULL NULL NULL NULL NULL
contig-1 out.27-Actinomycetales gene_id_949 NULL NULL NULL NULL NULL NULL NULL NULL
contig-3 out.24-NULL gene_id_3294 NULL NULL NULL NULL NULL NULL NULL NULL NULL
contig-3 out.24-NULL gene_id_3295 NULL NULL NULL NULL NULL NULL NULL NULL NULL
contig-10 out.23-NULL gene_id_11670 NULL NULL NULL NULL NULL NULL NULL NULL
contig-10 out.23-NULL gene_id_11671 NULL NULL NULL NULL NULL NULL NULL NULL NULL
contig-14 out.23-NULL gene_id_16640 NULL NULL NULL NULL NULL NULL NULL NULL
contig-27 out.31-NULL gene_id_32333 NULL NULL NULL NULL NULL NULL NULL NULL NULL
contig-27 out.31-NULL gene_id_32334 NULL NULL NULL NULL NULL NULL NULL
丰度
contig-3 4578 29.5413
contig-2 1091 13.6616
contig-1 2608 11.5441
contig-8 8194 34.0362
contig-9 1457 10.5831
contig-10 1236 8.48298
所需输出的示例(注释选项卡分隔丰富)(如果程序正确运行)该文件不是程序的输出这只是我举的一个例子:
contig-1 out.27-Actinomycetales gene_id_947 NULL NULL NULL NULL NULL NULL 2608 11.5441
contig-1 out.27-Actinomycetales gene_id_948 NULL NULL NULL NULL NULL NULL NULL 2608 11.5441
contig-1 out.27-Actinomycetales gene_id_949 NULL NULL NULL NULL NULL NULL NULL NULL 2608 11.5441
contig-3 out.24-NULL gene_id_3294 NULL NULL NULL NULL NULL NULL NULL NULL NULL 4578 29.5413
contig-3 out.24-NULL gene_id_3295 NULL NULL NULL NULL NULL NULL NULL NULL NULL 4578 29.5413
contig-10 out.23-NULL gene_id_11670 NULL NULL NULL NULL NULL NULL NULL NULL
contig-10 out.23-NULL gene_id_11671 NULL NULL NULL NULL NULL NULL NULL NULL NULL
contig-14 out.23-NULL gene_id_16640 NULL NULL NULL NULL NULL NULL NULL NULL
contig-27 out.31-NULL gene_id_32333 NULL NULL NULL NULL NULL NULL NULL NULL NULL
contig-27 out.31-NULL gene_id_32334 NULL NULL NULL NULL NULL NULL NULL
只是一些提示:
有多个不必要的括号:
keywords = ([line.strip() for line in f])
与完全相同
keywords = [line.strip() for line in f]
和
line1 = (line)
与完全相同
line1 = line
在您的代码中还有更多发生这种情况的地方。
您根本不需要正则表达式。Python字符串有一个方便的startswith()
方法:
代替
if re.match (r"b"+word+r"b" , line1):
你应该写
if line1.startswith(word):
我没有看到这个要求的任何代码
如果该单词在文件#2中找到,而不是在#3中找到,则输出将仅为文件#2中的行。
否则,您的代码似乎不会太远。你必须清楚你期望的输出是什么,以及计算机程序如何创建这种输出。从你所写的内容来看,我对你可能想要实现的目标有一些想法,并认为这可能就是:
annotations = open('annotation', 'r')
abundances = open('abundance', 'r').readlines() # read the file into memory, otherwise you'd have to "rewind" the file for each inner loop.
with open('contig_numbers', 'r') as f:
keywords = [line.strip() for line in f]
for annotation in annotations: # use meaningful variable names
for keyword in keywords:
if annotation.startswith(keyword+" "): # the +" " is neccessary to avoid clashes with 'contig-2' and 'contig-27'
for abundance in abundances:
if abundance.startswith(keyword+" "):
print (annotation.strip()+"t"+abundance.strip())
break
else: # Python magic: executed when the for loop is not left by a break, but because the iterator is empty.
print(annotation.strip())
进一步假设丰度在丰度文件中最多出现一次,将它们存储在字典中是有意义的,而不是一次又一次地迭代整个文件。。。
还有另一个改进:将contig_numbers存储为一个集合,以便快速进行成员资格测试。
abundances = {}
with open('abundance', 'r') as f:
for line in f:
contig, rest = line.strip().split(maxsplit=1)
abundances[contig] = rest
with open('contig_numbers', 'r') as f:
contig_numbers = set(line.strip() for line in f)
annotations = open('annotation', 'r')
for annotation in annotations:
key = annotation.split(maxsplit=1)[0]
if key in contig_numbers:
if key in abundances:
print (annotation.strip() + "t" + abundances[key])
else:
print(annotation.strip())
虽然您没有提到您试图解决的问题,但由于您的代码正在运行,我认为问题是输出文件是空的。
原因是在程序退出之前没有刷新或关闭文件。只需在最后添加一个new_file.close()
,就可以开始了。