How can I pass a file from a python program to a perl script and then reopen it in python?
Question: I have a python program that opens and manipulates a file. It takes a file
with a single fasta entry (ex1) and splits it into a mulit-fasta file (ex2).

ex1:

    
    
    >genome_fasta
    ATCG.... (thousands of chars)
    

ex 2:

    
    
    >genome_split_1
    ATCG.... (300 chars)
    >genome_split_2
    GCTT.... (300 chars)
    >genome_split_3
    ATGG....(300 chars, etc.)
    

That is working fine. But now I want to get the penta-nucleotide frequencies
for each one of these fasta entries. I have a perl script that does this
nicely and formats the output how I want it.

How can I pass my file (ex2) to this perl script, run it and get my output
file, then pass the file back into my python script for further manipulation?

I have tried to understand the subprocess module because that seems like the
easiest idea (i.e. just run a subprocess on the terminal that runs the
./perl_script.pl), but can't figure it out.


Answer: You can use the popen method of the subprocess module.

    
    
    from subprocess import Popen, PIPE
    
    p = Popen(['perl', 'myscript.perl'], stdin=PIPE, stdout=PIPE, stderr=PIPE)
    output, err = p.communicate(b"input data that is passed to subprocess' stdin")
    rc = p.returncode
    

output variable is then your text.

