避免在函数中重复加载文件

问题描述 投票:0回答:2

我正在尝试编写一个带有函数的文件来获取fasta文件,并且(i)给出文件的概述,(ii)绘制序列长度分布的直方图。我成功编写了以下代码:

from Bio import SeqIO
from prettytable import PrettyTable
import pylab
import numpy as np
import sys
%matplotlib inline

def fasta_outlook(fasta_file):
    '''Summarize the fasta file with #ofseq, length(min,max). Takes filename as string'''
   => sizes = [len(rec) for rec in SeqIO.parse(fasta_file,"fasta")]
    table = PrettyTable(['Parameter', 'Stats'])
    table.add_row(['No. of Sequences', len(sizes)])
    table.add_row(['Shortest seq.length', min(sizes)])
    table.add_row(['Longest Seq.length', max(sizes)])
    print(table)

def fasta_burst(fasta_file):
    '''Reports the length of each fasta sequence in the file. Takes filename as string'''
    my_file = open("Seq_length.tab","w")
   => for rec in SeqIO.parse(fasta_file,"fasta"):
        my_file.write(rec.id+'\t'+str(len(rec))+'\n')
    print("Length report written in Seq_length.tab")

def fasta_lendist(fasta_file):
    '''plot the distribution of sequence length as histogram. Takes filename as string'''
   => sizes = [len(rec) for rec in SeqIO.parse(fasta_file,"fasta")]
    count,bins,_ = pylab.hist(sizes, bins=100, log=True, histtype='step',color='red')
    pylab.title("%i seq with len: %i to %i bp (range)\nBin Max: %i seq around %i bp"%(len(sizes),min(sizes),max(sizes),count.max(),bins[np.argmax(count)]))
    pylab.xlabel("Sequence length (bp)")
    pylab.ylabel("Log Count")
    pylab.savefig("Sequence_length_distribution_plot.png")
    print("Plot saved as Sequence_length_distribution_plot.png")

fasta = 'filename.fa'
fasta_outlook(fasta)
fasta_lendist(fasta)

这里的问题是,在所有函数中我重复加载文件(=>)。是否可以全局加载文件一次并在后续函数中使用该对象?函数的arg是否采用对象而不是filename(字符串)?

python python-3.x function fasta
2个回答
1
投票

一次读取所有记录,然后将它们传递给您的函数。如果您的FASTA文件非常大,这可能是一个非常糟糕的主意。在脚本的底部:

fasta = 'filename.fa'
records = [record for record in SeqIO.parse(fasta,"fasta")]
fasta_outlook(records)
fasta_lendist(records

你的一个功能现在看起来像这样:

def fasta_outlook(fasta_records):
    sizes = [len(rec) for rec in fasta_records]
    table = PrettyTable(['Parameter', 'Stats'])
    table.add_row(['No. of Sequences', len(sizes)])
    table.add_row(['Shortest seq.length', min(sizes)])
    table.add_row(['Longest Seq.length', max(sizes)])
    print(table)

2
投票

看起来您只使用文件中的记录长度和ID。您可以将它们加载到元组列表或两个单独的列表中并传递它们。当然没有理由一遍又一遍地解析您的文件。

首先编写一个函数来加载相关数据。我认为一对列表更好,因为你只使用一次ID:

def load_file(filename):
    data = [(rec.id, len(rec)) for rec in SeqIO.parse(fasta_file, "fasta")]
    # Transpose the data into two lists instead of list of pairs
    return tuple(map(list, zip(*data)))

现在你的函数调用应该是这样的

fasta = 'filename.fa'
ids, sizes = load_file(fasta)
fasta_outlook(sizes)
fasta_lendist(sizes)
fasta_burst(ids, sizes)

fasta_outlookfasta_lendist中,您只需将输入参数名称更改为sizes并删除计算这些值的理解。在fasta_burst中,您可以稍微简化循环:

def fasta_burst(ids, sizes):
'''Reports the length of each fasta sequence in the file. Takes filename as string'''
    with open("Seq_length.tab","w") as my_file:
        for id, rec in zip(ids, sizes):
            my_file.write('{}\t{}\n'.format(id, size))
    print("Length report written in Seq_length.tab")

使用with块确保您的文件在完成后关闭。你之前根本没有关闭,即使发生错误,with也有优势。

© www.soinside.com 2019 - 2024. All rights reserved.