Convert newick tree to png or jpg image file

1,205 views
Skip to first unread message

Amine

unread,
Jan 28, 2011, 9:03:25 AM1/28/11
to The ETE toolkit
Dear,
How can I save newick tree into png or jpg image file using ete
toolkit.
thanks

Jaime Huerta Cepas

unread,
Jan 28, 2011, 9:08:53 AM1/28/11
to eteto...@googlegroups.com
In short:

from ete2 import Tree
tree = Tree("((a,b),c);")
tree.render("/tmp/my_tree_image.png")

But, there are plenty of more options which are explained in detail in the ETE tutorial and examples (http://ete.cgenomics.org/documentation)

Jaime

monkey79

unread,
May 17, 2012, 10:05:37 AM5/17/12
to eteto...@googlegroups.com
Hi Jaime,

I tried to run the script using the following newick file (see file 1) but does not print my tree into a png image format. Instead the script returns something like in the file 2.

any helps, please?
file 1.rtf
file 2.rtf

Jaime Huerta Cepas

unread,
May 17, 2012, 10:29:15 AM5/17/12
to eteto...@googlegroups.com
Hi Barbara, 
the render() function returns a pixel map of the png image. This is what you see in file2 (just ignore it). 
The PNG file will be written into a file, whose name corresponds to the one you provided to the render method. For instance: 

my_img_map = tree.render("myFileName.png")

cheers, 
Jaime

monkey79

unread,
May 18, 2012, 11:43:57 AM5/18/12
to eteto...@googlegroups.com
Hey Jaime, 

sorry again, but the PNG file does not includes my bootstrap values.

how I can include those in my image, please?


thanks

barbara

monkey79

unread,
May 18, 2012, 12:03:50 PM5/18/12
to eteto...@googlegroups.com
I do have one more problem. If I run my script for more than two input files and I ask to print put as PNG, it returns files (see attached files) that I cant open. Strangely if I used t.render ("pdf") it works and I can open the same files without problem.

So at the end what I need to do is produce more than 300 PNG files with bootstrap values.

many thanks

barbara

Jaime Huerta Cepas

unread,
May 18, 2012, 3:18:52 PM5/18/12
to eteto...@googlegroups.com
Hi Barbara,

Your PNG files are empty (0 bytes). Please, attach your script so we can see where is the problem.
Regarding the bootstrap values, see this example: http://packages.python.org/ete2/tutorial/tutorial_drawing.html#show-leaf-node-names-branch-length-and-branch-support

Jaime

monkey79

unread,
May 19, 2012, 4:40:00 AM5/19/12
to eteto...@googlegroups.com
Hi Jaime,

yes I know. Please attached you will find my script (branch_values.py) and my input files(OG2 and OG9). Basically I want to  be able to process my files and save the output as different PNG images for each input, including my bootstrap values.

thank you very much,

barbara
OG2.txt.fa.clustalw.phylip.root_phyml_tree.rooted_tree
OG9.txt.fa.clustalw.phylip.root_phyml_tree.rooted_tree
branch_values.py

Salvador Capella

unread,
May 19, 2012, 4:49:15 AM5/19/12
to eteto...@googlegroups.com
Hi Barbara,

Let me suggest some changes that could make your script works.

You could format your output filename before calling to the render function. I have modified it as well the name of the output file to include ".png" instead of "_png", on this way the render knows which format you are asking for. Finally, you shall use "tree_style" argument to have the .png similar to the picture you see when you invoke show() method.

    output_filename = os.path.join(output_directory, filename_base+'.tree.png')
    t.render(output_filename, units="px", tree_style = ts)

I hope this helps you.

S

Jaime Huerta Cepas

unread,
May 19, 2012, 5:05:15 AM5/19/12
to eteto...@googlegroups.com
The render() method autodetects image format based on filename extension. As Salva suggests (thanks salva), by making your filename to end as ".png", you will solve the problem.
I take note of this issue, so I can add some warnings when render is not called correctly.

Jaime Huerta Cepas

unread,
May 19, 2012, 5:14:52 AM5/19/12
to eteto...@googlegroups.com
Wait, I have just realized that your problem is not in the render method. You are indeed creating a png file called "files.png", but I don't know why you create an empty file by the end of the script.....
Forget about the "open" line. Use the example that Salva provided.

monkey79

unread,
May 19, 2012, 6:18:03 AM5/19/12
to eteto...@googlegroups.com
Hey there,

thanks so much to both. I changed the script, but I still having the same problem.  I cant open the output file, its saying:"Couldn’t open the file. It may be corrupt or a file format that Preview doesn’t recognize" 

Sorry I do not know how solve it!

thanks
B

monkey79

unread,
May 19, 2012, 6:43:42 AM5/19/12
to eteto...@googlegroups.com
Sorry guys....script fixed!

thanks so much for prompt suggestions and helps, as always!

cheers

B
Reply all
Reply to author
Forward
0 new messages