views:

315

answers:

6

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:\Python26\opuntia.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:\Python26\opuntia.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:\Python26\muscleIO.py", line 11, in <module>
    align=AlignIO.read(child.stdout,"fasta")
  File "C:\Python26\Lib\site-packages\Bio\AlignIO\__init__.py", line 423, in read
    raise ValueError("No records found in handle")
ValueError: No records found in handle
+1  A: 

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

Use 'r' to get raw strings, i.e. r'c:\Python26\opuntia.phy'.

unwind
Still not working :(
Or use os.join to join parts of paths
Mark
This is irrelevant to the error.
KennyTM
A: 

I mean line

align=AlignIO.read(child.stdout,"fasta") is not working with exceptions.ValueError: No records found in handle

This doesnt answer your question. If you want to provide more information, update your original question. This is how StackOverflow works. Welcome. =)
mizipzor
+1  A: 

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]
joaquin
A: 

Still encounter error at line --align=AlignIO.read(child.communicate()[0],"fasta")

import sys,os, subprocess

from Bio import AlignIO

from subprocess import Popen, PIPE

from Bio.Align.Applications import MuscleCommandline

cline = MuscleCommandline(input="c:\Python26\opuntia.fasta")

child= subprocess.Popen(str(cline),

                     stdout = subprocess.PIPE,

                     stderr=subprocess.PIPE,

                    shell=(sys.platform!="win32"))

align=AlignIO.read(child.communicate()[0],"fasta")

outfile=open(r"c:\Python26\opuntia.phy","w")

AlignIO.write([align],outfile,'phylip')

outfile.close()

I dont know why i am facing too much problem with this subprocess

+3  A: 
Brad Chapman
Now i understand the problem thanks for your help. The opuntia.phy file has been generated, can i draw phylogenetic tree from it? If yes then what drawing phylip module i can use. I have found phyloxml tutorials regarding drawing trees but my output is not .xml
You've only done an alignment (and then converted the format) and have not yet built a phylogenetic tree. Phylogenetics is a huge topic but here a Python tutorial with PyCogent using Muscle for alignment and FastTree for building the tree: http://telliott99.blogspot.com/2009/12/pycogent-14-fasttree.html
Brad Chapman
What im trying to do is:- 1) Take fasta input and do MSA (Muscle) and output alignment file (.phy or .aln) 2) Perform phylogency neighbor joining method (NJ,UPGMA) using phylip 3) And Create tree. Finally my results can be viewed as- MSA, phylogency and tree I haven't used PyCogent so i am trying to complete this project with Biopython right now
and with open() is not supported in python 2.5.*
Give the EMBASSY wrappers for phylip a try, as they provide a nice command line interface: http://emboss.sourceforge.net/apps/release/6.2/embassy/phylipnew/fneighbor.html. There are not wrapper classes in Biopython, but you can generate the command line directly and use subprocess.
Brad Chapman
A: 

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.)

Eric Talevich
Is Biopython 1.54 beta version? And i am wondering in your example whether it should be input or infile. Because input parameter doesn't seems to work for me.
Can i save this phylo newick tree as a picture?
For ClustalW the parameter is infile, not input. (Sorry)To save the tree as a picture, use something like (fixing the whitespace): import pylab; Phylo.draw_graphviz(tree); pylab.show(); pylab.savefig("egfr-family.png")
Eric Talevich