【问题标题】:Python error: OSError: error when opening file `all_fprau.fasta`. Fasta file too large?Python 错误:OSError:打开文件“all_fprau.fasta”时出错。 Fasta 文件太大?
【发布时间】:2018-12-06 03:53:29
【问题描述】:

我有一个脚本可以搜索 csv 文件和 fasta 文件,两个文件中的任何 ID 都将用于从 fasta 文件中提取 fasta 序列。它看起来像这样:

import os
import shlex
import subprocess
import argparse
import glob
import pysam

from pysam import FastaFile

#setup options for this program
parser = argparse.ArgumentParser()
group = parser.add_argument_group('Options for annotation.py')
group.add_argument(
    '-f', '--fasta', help='FASTA file (RAST). all_fprau.fasta', required=True)
group.add_argument(
    '-c', '--csv', help='CSV file (parsed orthomcl groups). parsed_groups.csv',
required=True)
args = parser.parse_args()


fasta = FastaFile(args.fasta)

with open(args.csv) as infile:
    infile.readline()  ## skip header

    for index, line in enumerate(infile):
        strain, matches = line.strip().split("\t")
        if not len(matches):
            print('Warning: Skipping strain {}. No     matches'.format(strain))
        continue
    matches = set(matches.split(','))  # remove duplicates

    key = '{}.faa'.format(strain)
    with open(key, 'w') as out_file:
        for match in matches:
            name = 'fig|{}'.format(match)
            try:
                sequence = fasta[name]
            except KeyError:
                print('Sequence absent in FASTA file {0}: {1}'.format(args.fasta, name))
                continue
            out_file.write(">{0}\n{1}\n".format(name, sequence))

但是,我在运行此脚本时遇到了问题。我收到此错误:

File "pysam/libcfaidx.pyx", line 123, in pysam.libcfaidx.FastaFile.__cinit__
File "pysam/libcfaidx.pyx", line 183, in pysam.libcfaidx.FastaFile._open
OSError: error when opening file `all_fprau.fasta`

fasta 文件all_fprau.fasta 包含大量序列,我认为这可能是问题所在?当我减小文件的大小时,脚本可以工作,但是我需要所有序列都可用于此管道中的下一步工作。我试过用:

fasta = glob.glob("/home/brian/my_orthomcl_dir/annotations/*.fasta") 

代替

fasta = FastaFile(args.fasta)

试图单独搜索 fasta 文件,但我收到错误:TypeError: list indices must be integers or slices, not str,与上面脚本中的第 38 行相关:

sequence = fasta[name]

任何帮助或建议将不胜感激!

【问题讨论】:

  • 你试过用open(args.fasta)手动打开文件吗?
  • 是的,不幸的是仍然得到一个错误,TypeError: '_io.TextIOWrapper' object is not subscriptable, in related to the line sequence = FastaFile(args.fasta)
  • 这很有意义,不是吗,因为您无法使用 [] 访问以这种方式打开的文件。您是否还有适合您的 fasta 文件的索引文件?
  • 索引文件通常在脚本运行期间创建。很奇怪,我使用较小的文件,这让我很困惑!
  • 您测试它的文件有多大?

标签: python pysam


【解决方案1】:

尝试制定自己的解决方案来格式化和提取生物信息学数据以应对挑战是很好的,但通常之前已经完成,所以不要让自己头疼。您可以使用 Jim Kent 的 faSomeRecords(找到 here)或一些令人困惑的单行代码,例如:cut -c 2- EXAMPLE.TXT | xargs -n 1 samtools faidx EXAMPLE.FA,但它仍然需要 samtools。另外,如果你还不知道有一种SO for bioinformatics,还有SO bioinformatics

【讨论】:

    猜你喜欢
    • 2016-04-21
    • 2019-06-25
    • 2013-05-03
    • 1970-01-01
    • 1970-01-01
    • 2014-10-03
    • 2010-12-07
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多