English 中文(简体)
Subprocess fails to catch the standard output
原标题:

I am trying to generate tree with fasta file input and Alignment with MuscleCommandline

import sys,os, subprocess
from Bio import AlignIO
from Bio.Align.Applications import MuscleCommandline
cline = MuscleCommandline(input="c:Python26opuntia.fasta")
child= subprocess.Popen(str(cline),
                         stdout = subprocess.PIPE,
                         stderr=subprocess.PIPE,
                        shell=(sys.platform!="win32"))
align=AlignIO.read(child.stdout,"fasta")
outfile=open( c:Python26opuntia.phy , w )
AlignIO.write([align],outfile, phylip )
outfile.close()

I always encounter with these problems

Traceback (most recent call last):
  File "<string>", line 244, in run_nodebug
  File "C:Python26muscleIO.py", line 11, in <module>
    align=AlignIO.read(child.stdout,"fasta")
  File "C:Python26Libsite-packagesBioAlignIO\__init__.py", line 423, in read
    raise ValueError("No records found in handle")
ValueError: No records found in handle
最佳回答

Biopython 1.54 was released today with a stable version of the Bio.Phylo module. I ve updated the documentation with an example pipeline for generating trees. For simplicity, the example uses ClustalW to align sequences and generate a tree, instead of Muscle and Phylip, but most of the code is still the same or similar.

http://biopython.org/wiki/Phylo#Example_pipeline

If you ve already generated a tree with Phylip (using the .phy alignment as input), you can still follow the Phylo examples in general. Phylip creates a Newick file with a name like "outttree" or "foo.tree".

(Feel free to merge this with Brad s answer; I can t write a comment in that thread yet.)

问题回答

A couple of things are giving problems here:

  1. You need a child.wait() after the subprocess call so that your code will wait until the external program is done running.

  2. Muscle does not actually write to stdout, even though the help documentation says it does, at least with v3.6 that I have here. I believe the latest is v3.8 so this may be fixed.

Biopython is telling you that the stdout you are passing it is empty, which is the error you are seeing. Try running the commandline directly:

muscle -in opuntia.fasta

and see if you see FASTA output. Here is a version that fixes the wait problem and uses an intermediate output file:


import sys,os, subprocess
from Bio import AlignIO
from Bio.Align.Applications import MuscleCommandline
out_file = "opuntia.aln"
cline = MuscleCommandline(input="opuntia.fasta", out=out_file)
child= subprocess.Popen(str(cline),
                         stdout = subprocess.PIPE,
                         stderr=subprocess.PIPE,
                        shell=(sys.platform!="win32"))
child.wait()
with open(out_file) as align_handle:
    align=AlignIO.read(align_handle,"fasta")
outfile=open( opuntia.phy , w )
AlignIO.write([align],outfile, phylip )
outfile.close()
os.remove(out_file)

From the documentation of the subproccess library:

Warning

Use communicate() rather than .stdin.write, .stdout.read or .stderr.read to avoid deadlocks due to any of the other OS pipe buffers filling up and blocking the child process.

so maybe you could try something like:

mydata = child.communicate()[0]

You have an unprotected backslash in your output filename, that is never good.

Use r to get raw strings, i.e. r c:Python26opuntia.phy .





相关问题
Can Django models use MySQL functions?

Is there a way to force Django models to pass a field to a MySQL function every time the model data is read or loaded? To clarify what I mean in SQL, I want the Django model to produce something like ...

An enterprise scheduler for python (like quartz)

I am looking for an enterprise tasks scheduler for python, like quartz is for Java. Requirements: Persistent: if the process restarts or the machine restarts, then all the jobs must stay there and ...

How to remove unique, then duplicate dictionaries in a list?

Given the following list that contains some duplicate and some unique dictionaries, what is the best method to remove unique dictionaries first, then reduce the duplicate dictionaries to single ...

What is suggested seed value to use with random.seed()?

Simple enough question: I m using python random module to generate random integers. I want to know what is the suggested value to use with the random.seed() function? Currently I am letting this ...

How can I make the PyDev editor selectively ignore errors?

I m using PyDev under Eclipse to write some Jython code. I ve got numerous instances where I need to do something like this: import com.work.project.component.client.Interface.ISubInterface as ...

How do I profile `paster serve` s startup time?

Python s paster serve app.ini is taking longer than I would like to be ready for the first request. I know how to profile requests with middleware, but how do I profile the initialization time? I ...

Pragmatically adding give-aways/freebies to an online store

Our business currently has an online store and recently we ve been offering free specials to our customers. Right now, we simply display the special and give the buyer a notice stating we will add the ...

Converting Dictionary to List? [duplicate]

I m trying to convert a Python dictionary into a Python list, in order to perform some calculations. #My dictionary dict = {} dict[ Capital ]="London" dict[ Food ]="Fish&Chips" dict[ 2012 ]="...

热门标签