首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在两个文件中搜索一个单词,然后用表格分隔的方式打印这两行

在两个文件中搜索一个单词,然后用表格分隔的方式打印这两行
EN

Stack Overflow用户
提问于 2016-01-11 17:11:00
回答 2查看 86关注 0票数 1

我试图使用Python编写一个程序,该程序从文件#1 (contig_numbers)中获取单词列表,并通过在两个不同的文件(#2注释和#3丰度)中逐行迭代来查找这个单词。如果在这两个文件中找到了单词,那么输出将是来自文件#2 (注释)的行、制表符分隔和行表单文件#3 (丰度)。如果在文件#2中找到这个单词,而不是在#3中找到,那么输出将仅仅是来自文件#2的行(只是注释)。这个词也可以在文件#2中多次找到(参见下面的示例)。

我向您展示了我编写的代码,但它似乎并不像我所希望的那样工作:

代码语言:javascript
复制
#!/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

代码语言:javascript
复制
contig-3
contig-2
contig-1
contig-10
contig-14
contig-27

注解

代码语言:javascript
复制
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    

丰裕度

代码语言:javascript
复制
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

所需输出的一个示例(注释选项卡分离丰度)(如果程序正确运行,这就是我所期望的)--这个文件不是程序的输出--这只是我介绍的一个示例:

代码语言:javascript
复制
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    
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2016-01-11 21:03:03

只是一些提示:

有多个不必要的括号:

代码语言:javascript
复制
keywords = ([line.strip() for line in f])

完全相同

代码语言:javascript
复制
keywords = [line.strip() for line in f]

代码语言:javascript
复制
line1 = (line) 

完全相同

代码语言:javascript
复制
line1 = line

在您的代码中还有更多的地方会发生这种情况。

您根本不需要正则表达式。Python有一个方便的startswith()方法:

而不是

代码语言:javascript
复制
if re.match (r"\b"+word+r"\b" , line1):

你应该写

代码语言:javascript
复制
if line1.startswith(word):

我没有看到这个要求的任何代码

如果在文件#2中找到这个单词,而不是在#3中找到,那么输出将仅仅是文件#2的一行。

否则,您的代码看起来并不遥远。你必须清楚什么是你期望的输出,以及计算机程序如何创建这个输出。从你写的,我知道你可能想要达到什么,并认为这可能是:

代码语言:javascript
复制
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存储为一组,以便快速进行成员资格测试。

代码语言:javascript
复制
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())
票数 1
EN

Stack Overflow用户

发布于 2016-01-11 20:18:53

虽然您没有提到您想要解决的问题,但由于您的代码运行,我将假设问题是输出文件为空。

其原因是,在程序退出之前,您不会刷新或关闭文件。只需在最后面添加一个new_file.close()就可以了。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/34727255

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档