我有一个脚本可以搜索 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]
任何帮助或建议将不胜感激!