bash if statement skipping python command
0
0
Entering edit mode
8 weeks ago

Hi all,

I'm writing a new script where I want to iterate over fasta files based on an array, and if the fasta files is present to make a directory, move the fasta to the directory, align the fasta with mafft to then run a python script that executes another function.

However, the script appears to be making the directory, moving the file and aligning it, but then it ignores the python command.

Array=(PB2 PB1 PA HA NP NA MP NS)   
for Entry in "${Array[@]}" do
         if [-e Sample_"$Entry".fasta ]     
         then
         mkdir ./Sample_"$Entry"_Aligned    
         mv Sample_"$Entry".fasta ./Sample_"$Entry"_Aligned/Sample_"$Entry".fasta           
         mafft --auto --anysymbol --thread "$Cores" ./Sample_"$Entry"_Aligned/Sample_"$Entry".fasta > ./Sample_"$Entry"_Aligned/Sample_"$Entry".aln.fasta   
         python /path/to/python/script/pythonscript.py ./Sample_"$Entry"_Aligned/Sample_"$Entry".aln.fasta
         fi
done

If I run the python script on the output file separately, it works fine, so the files aren't malformed.

I've had a look around on here and Stackoverflow for a suggestion as to why this might be happening, but can't seem to find a reason. Does anyone have an idea how to fix this?

python bash statement if • 438 views
ADD COMMENT
1
Entering edit mode

Your tests are telling us that you can run a python script from within bash - so your script is being called, and executing - just not giving the output you expect. You mention that your script responds with a usage message, if the input args are not "accepted". Perhaps it is getting the args, but failing elsewhere. It seems like you need to debug your script some more. Can you insert print lines in your script to see how far it gets? (i.e. insert "hello world" at the top to prove to yourself that it is running, and then dig further from there).

ADD REPLY
1
Entering edit mode

Thanks @Joe and @seidel for you comments.

I ended up trying you approach @seidel and adding print commands after each step of the python script and all were running through to completion but still not producing an output. Then I realised that I was supplying one of the arguments to the python script using a relative path (./) rather than an absolute (/home/user/) which meant that the way I was making the output file in the python script wasn't correct. Once I rectified this, the script worked.

Thanks again for you time.

ADD REPLY
0
Entering edit mode

(1) If you insert a python script that simply says "hello world"...does that work to produce any output? (2) Could you be getting a different python when running it by hand versus running within a script? (what if you insert python --version in place of the python line...does it match your command-line version? If you're using screen or tmux you might also be getting a different python version.) (3) would your python script give you an error message if it ran but failed to find the input file?

ADD REPLY
0
Entering edit mode

Hi @seidel, thanks for getting back to me. I thought I'd responded to your post earlier today but can't find it so apologies if you're getting two responses from me.

(1) I made a script called HelloWorld.py that prints Hello World. I added this to my script and it produced an output (printed Hello World) as expected.

(2) I replaced the current python command with python --version and this provided the python version (3.9.7), which is the same version as I have outside of the script. Just to note, I'm running the script from within a conda env so the python version should be fixed.

(3) My python script has a usage function that would be instigated if it was not accepting the input args, but even this is not being executed.

ADD REPLY
0
Entering edit mode

I think we need to see the content of the python script. There is no reason that python itself shouldn't be being called (unless mafft is failing maybe) that I can see at a glance, so I doubt it's ignoring the script, and instead might just be failing silently for some reason. You could try also running the bash script in debug mode (-x if memory serves) and see where things are breaking.

What is the expected output of the python script? Does it print the result to screen?

ADD REPLY

Login before adding your answer.

Traffic: 3061 users visited in the last hour
Help About
FAQ
Access RSS
API
Stats

Use of this site constitutes acceptance of our User Agreement and Privacy Policy.

Powered by the version 2.3.6