避免重复加载功能中的文件

时间:2018-09-04 13:45:54

标签: python python-3.x function fasta

我正在尝试编写一个具有提取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是否采用对象而不是文件名(字符串)?

2 个答案:

答案 0 :(得分: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的优点是即使发生错误也可以关闭。

答案 1 :(得分: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)