0

I have installed the Vienna package for Windows from the following link:

https://www.tbi.univie.ac.at/RNA/#

and I found that these package cannot be installed over conda in Windows, but I found a turnaround way that it was to call directly the exe file. The information I found it in this post:

https://www.biostars.org/p/394622/

So, I have tested by the command prompt and I got the following answer from a couple of sequences:

enter image description here

the program that I made in Python is the following:

from subprocess import Popen, PIPE
import subprocess
p = subprocess.Popen('RNAcofold.exe', stdin=PIPE,stdout=PIPE,shell=True)
ans = p.communicate('ATGTTGG&CCGTGT'.encode())
print (ans)

I would like to obtain the string that it says "minimum free energy=some value", but instead I got the following output:

(b'', None)

How can I obtain this output?

halfer
  • 19,824
  • 17
  • 99
  • 186
Little
  • 3,363
  • 10
  • 45
  • 74
  • Does this answer your question? [How do you use subprocess.check\_output() in Python?](https://stackoverflow.com/questions/14078117/how-do-you-use-subprocess-check-output-in-python) – buran Dec 19 '20 at 21:31
  • thanks @buran, but it does not answer the question, also I do not know why somebody could downvote the question without even posting a solution – Little Dec 19 '20 at 21:39
  • I don't see how it does not answer your question, when you want to obtain the output and then parse it. It shows exactly how to use `check_output`. – buran Dec 19 '20 at 21:42
  • @buran I have tried the solution posted and it does not work, also the person who answered it at the end says that he "but couldn't find any examples with arguments". Nevertheless, when I tried the example I got Command '['RNAcofold.exe', 'ATGTTGG&CCGTGT']' returned non-zero exit status 1. – Little Dec 19 '20 at 21:50

2 Answers2

1

I'm not sure if this will work with your program but may be able to use subprocess like below:

>>> output = subprocess.run(["RNAcofold.exe", "ATGTTGG&CCGTGT"], capture_output=True, shell=True).stdout
>>> output
b'this is\nthe content\nof test.txt\n'
>>>

You can then split the string and capture only the line you want:

>>> output.decode().split('\n')
['this is', 'the content', 'of test.txt', '']

I have used subprocess few times, but if I understand correctly from the documentation the communicate and stdin=PIPE,stdout=PIPE are handled automatically by run.

If you're interested in keeping the stderr and return code, just expand the first part as below:

>>> all_output = subprocess.run(["RNAcofold.exe", "ATGTTGG&CCGTGT"], capture_output=True, shell=True)
>>> all_output.stdout
b'this is\nthe content\nof test.txt\n'
>>> all_output.stderr
b''

And so on...
  • why only capture stdout and discard everything else? I'd put the whole `CompletedProcess` object in `output`, that way you can check the return code and stderr too. – Z4-tier Dec 19 '20 at 21:34
  • That's to make it short if all you need is the stdout. If the other information would be relevant, by all means store the output of the subprocess and get the stdout later. – Bernardo Trindade Dec 19 '20 at 21:36
  • 2
    since we're troubleshooting at this point (I'm not sure this solution is even going to work for OP), probably better to retain all information (what if this `RNAcofold` is, for some bizarre reason, writing it's output to stderr?). – Z4-tier Dec 19 '20 at 21:38
  • @Z4-tier That's reasonable. Just edited my answer to reflect what you said. Thanks. – Bernardo Trindade Dec 19 '20 at 21:40
  • thanks @BernardoTrindade, but I get CompletedProcess(args=['RNAcofold.exe', 'ATGTTGG&CCGTGT'], returncode=1, stdout=b'', stderr=b"'RNAcofold.exe' is not recognized as an internal, I added shell=True like in the original version. – Little Dec 19 '20 at 21:43
  • 1
    Have you tried using the full path to the executable? – Bernardo Trindade Dec 19 '20 at 21:49
1

After tweaking around, thanks to @Bernardo Trindade one solution was to play with the env variable like this:

p = subprocess.Popen('RNAcofold.exe', stdin=PIPE,stdout=PIPE,shell=True,env={'PATH': 'full_path'})
ans = p.communicate('ATGTTGG&CCGTGT'.encode())
print (ans)

still there is some data missing from the output, but I will still try other options.

Little
  • 3,363
  • 10
  • 45
  • 74