Reputation: 717
I am writing a Python script to perform a BLAST by using the BLAST program DIAMOND automatically. The script executes commands in the terminal of Ubuntu 14.04.
My Python script is:
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"
The script creates the commands to execute after assigning the correct file paths and file names to the variables.
When I try to run this script I get the following error:
/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
I am sure that the commands are correct because, when I execute the commands printed in line 20 separately in terminal there are no errors and the output of the BLAST application is correct.
Why does this error occur while executing the commands in this Python script and not separately in terminal and how to solve this error?
Upvotes: 1
Views: 148
Reputation: 1055
The problem here is that subprocess.Popen() runs the command in the separate process that exits when the command has completed running. The cd
command and diamond
commands are run in separate processes.
This means that diamond
is looking for @HWI-M02942_file1.fasta
in the directory where you run the command from.
Your solution to simply use the absolute path here is probably the simplest.
Upvotes: 1