This is a test version of Biostars. For the public version, visit https://www.biostars.org.
How to redirect HyPhy output to text file

I want to run FUBAR from a python script and redirect the output to a text file so that I can perform some additional analysis on it. I have this working line of code:

os.system("echo `(echo '1'; echo '4'; echo '/myfile.fasta'; echo '/myfile.nwk') | hyphy `")

How do I modify it to redirect the output to a text file in the same directory?

fubar bash hyphy python

2 answers

I figured it out.

os.system("echo `(echo '1'; echo '4'; echo '/myfile.fasta'; echo '/mytree.nwk') | hyphy ` > /results.txt")

I think you need to use subprocess for better control of your file descriptors.

import subprocess

cmd = "hyphy --alignment myfile.fasta --tree myfile.nwk"
p = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
out, err = p.communicate()

Thank you for your comment. I'm aware that subprocess is preferred these days but it is more complicated and I think os.system is good enough for this purpose.

Log in to answer this question.