Phylogenetic trees are often distributed in Newick format. GraphML offers another way to represent a tree as nodes and edges, ready to open in tools such as Cytoscape. The Newick-to-GraphML repository contains two scripts to make that conversion.

How it works

The first script uses Biopython to read a Newick tree and assign identifiers to clades that do not already have names. The second reads the resulting tree in R, converts it to a graph, and writes a GraphML file.

Try it with an example tree

You'll need Python with Biopython and R with the ape, phytools, and igraph packages. After installing those dependencies, clone the repository:

git clone https://github.com/GildasLepennetier/newick_to_graphml.git
cd newick_to_graphml

Create a file named example.newick in the repository and paste in:

(
  ((Human:0.1,Chimpanzee:0.1):0.2,(Mouse:0.15,Rat:0.15):0.15):0.2,
  ((Dog:0.12,Cat:0.12):0.18,(Whale:0.1,Cow:0.1):0.2):0.2
);

This tree and its branch lengths are illustrative, not a scientific reconstruction.

Run the scripts in order:

python3 newick_fixInternal_nodes.py example.newick named.newick
sed -E 's/:0([.]0+)?;$/;/' named.newick > ready.newick
Rscript newick2graphml.R ready.newick tree.graphml

The sed step removes the zero-length root edge that Biopython can add to this example. The current phytools reader used by the conversion script can otherwise get stuck on that root edge.

Open tree.graphml in Cytoscape to explore the graph. The R script can also be adapted to add node attributes before writing the file.

The result

The diagram below shows the nodes and edges from the generated GraphML, arranged as a tree using Graphviz. Internal nodes were named by the Python script. This conversion does not include branch lengths in the GraphML output.

Illustrative tree with eight animal labels, connected by named internal nodes

For requirements, further details, and usage conditions, see the repository. Its README also links to the related Cytoscape automation documentation.