我有以下功能,几个月来一直很好用.我没有更新我的Python版本(除非它发生在幕后?).
def Blast(type, protein_sequence, start, end, genomic_sequence):
result = []
M = re.search('M', protein_sequence)
if M:
query = protein_sequence[M.start():]
temp = open("temp.ORF", "w")
print >>temp, '>blasting'
print >>temp, query
temp.close()
cline = blastp(query="'temp.ORF'", db="DB.blast.txt",
evalue=0.01, outfmt=5, out=type + ".BLAST")
os.system(str(cline))
blast_out = open(type + ".BLAST")
string = str(blast_out.read())
DEF = re.search("<Hit_def>((E|L)\d)</Hit_def>", string)
Run Code Online (Sandbox Code Playgroud)
我收到blast_out=open(type+".BLAST")无法找到指定文件的错误.此文件作为os.system调用调用的程序输出的一部分创建.这通常需要大约30秒才能完成.但是,当我尝试运行程序时,它立即给出了我上面提到的错误.
我以为os.system()应该等待完成?
我应该以某种方式强迫等待吗?(我不想硬编码等待时间).
编辑:我已经在BLAST程序的命令行版本中运行了cline输出.一切似乎都很好.
os.system等等.但是在它调用的程序中可能存在错误,因此不会创建该文件.在继续之前,您应该检查被调用程序的返回值.通常,程序在正常结束时应返回0,在出现错误时返回另一个值:
if os.system(str(cline)):
raise RuntimeError('program {} failed!'.format(str(cline)))
blast_out=open(type+".BLAST")
Run Code Online (Sandbox Code Playgroud)
您也可以从Blast函数返回,或尝试以其他方式处理它,而不是引发异常.
更新:从命令行运行的被调用程序只能告诉您程序本身没有任何问题.blast当出现问题时,程序是否会返回有用的错误或消息?如果是这样,请考虑使用subprocess.Popen()代替os.system,并捕获标准输出:
prog = subprocess.Popen(cline, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
out, err = prog.communicate()
# Now you can use `prog.returncode`, and inspect the `out` and `err`
# strings to check for things that went wrong.
Run Code Online (Sandbox Code Playgroud)
小智 5
您还可以使用subprocess.check_call替换对os.system的调用,如果命令失败,则会引发异常:
import subprocess as subp
subp.check_call(str(cline), shell=True)
Run Code Online (Sandbox Code Playgroud)