1

我正在编写一个 Python 脚本,通过自动使用 BLAST 程序 DIAMOND 来执行 BLAST。该脚本在 Ubuntu 14.04 的终端中执行命令。

我的 Python 脚本是:

import subprocess

data_location = "/home/markschuurman/Desktop/Onderzoek_BioCentre/data_course_4/"
input_fasta_file = "@HWI-M02942_file1.fasta"
diamond_temp_dir = "/home/markschuurman/Desktop/DIAMOND_temp_dir/"
diamond_blast_database_location = "/home/markschuurman/Desktop/Onderzoek_BioCentre/BLAST_with_DIAMOND/DIAMOND_BLAST_databases/"
diamond_blast_output_file_directory = "/home/markschuurman/Desktop/Onderzoek_BioCentre/BLAST_with_DIAMOND/output_files/"
diamond_blast_output_filemame_daa = "matches.daa"
diamond_blast_output_filemame_tsv = "matches.tsv"

max_hits_per_read = "5"
max_evalue = "10"

commands = ["cd " + data_location,
            "diamond blastx -d " + diamond_blast_database_location + "tcdb -q " + input_fasta_file + " -a " + diamond_blast_output_file_directory + diamond_blast_output_filemame_daa + " -t " + diamond_temp_dir + " -k " + max_hits_per_read + " -e " + max_evalue,
            "diamond view -a " + diamond_blast_output_file_directory + diamond_blast_output_filemame_daa + " -o " + diamond_blast_output_file_directory + diamond_blast_output_filemame_tsv]

for command in commands:

    print "Command : " + command
    p = subprocess.Popen(command, stdout=subprocess.PIPE, shell=True)

    p_status = p.wait()

    print "Command finished"

在将正确的文件路径和文件名分配给变量后,该脚本创建要执行的命令。

当我尝试运行此脚本时,出现以下错误:

/usr/bin/python2.7 /home/markschuurman/Desktop/Onderzoek_BioCentre/BLAST_with_DIAMOND/scripts_to_parse_DIAMOND_output/execute_DIAMOND_BLAST.py
Command : cd /home/markschuurman/Desktop/Onderzoek_BioCentre/data_course_4/
Command finished
Command : diamond blastx -d /home/markschuurman/Desktop/Onderzoek_BioCentre/BLAST_with_DIAMOND/DIAMOND_BLAST_databases/tcdb -q @HWI-M02942_file1.fasta -a /home/markschuurman/Desktop/Onderzoek_BioCentre/BLAST_with_DIAMOND/output_files/matches.daa -t /home/markschuurman/Desktop/DIAMOND_temp_dir/ -k 5 -e 10
Error: function Input_stream::Input_stream(const string&, bool) line 63. Error opening file @HWI-M02942_file1.fasta
Command finished
Command : diamond view -a /home/markschuurman/Desktop/Onderzoek_BioCentre/BLAST_with_DIAMOND/output_files/matches.daa -o /home/markschuurman/Desktop/Onderzoek_BioCentre/BLAST_with_DIAMOND/output_files/matches.tsv
Error: function Input_stream::Input_stream(const string&, bool) line 75. Error opening file /home/markschuurman/Desktop/Onderzoek_BioCentre/BLAST_with_DIAMOND/output_files/matches.daa
Command finished

我确信这些命令是正确的,因为当我在终端中分别执行第 20 行中打印的命令时,没有错误并且 BLAST 应用程序的输出是正确的。

为什么在执行此 Python 脚本中的命令而不是在终端中单独执行命令时会出现此错误,以及如何解决此错误?

4

1 回答 1

1

这里的问题是 subprocess.Popen() 在命令完成运行时退出的单独进程中运行命令。cd命令和diamond命令在不同的进程中运行。

这意味着在您运行命令的目录中diamond查找。@HWI-M02942_file1.fasta

您在这里简单地使用绝对路径的解决方案可能是最简单的。

于 2015-04-28T05:33:31.760 回答