Skip to content

aramanlab/NewickTreeTools.jl

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

12 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

NewickTreeTools

Stable Dev Build Status Coverage

Install

type:

]dev [email protected]:aramanlab/NewickTreeTools.jl.git

into the julia command line

Examples

using NewickTreeTools

# read in file
tree = readnw(read("filename.nw", String))

leafnames = getleafnames(tree)

# pairwise traversal distances between all leaves
full_Dn = network_distances(tree)
# pairwise patristic distances between all leaves
full_Dp = patristic_distances(tree)

What is the average patristic distance between tips within every sub-clade?

result = mapinternalnodes(tree) do node
    mean(patristic_distances(node))
end

# equivalent to...
result = mapinternalnodes(n->mean(patristic_distances(n)), tree)

What is the average patristic distance between every sub-clade that is also connected to a leaf node?

result = maplocalnodes(tree) do node
    mean(patristic_distance(node))
end

map leaves to external data.

using CSV, DataFrames
leafmetadf = CSV.read("leafmeta.csv", DataFrame)
result = maplocalnodes(tree) do node
    idx = indexin(getleafnames(node), leafmetadf.ID)
    somevals = leafmetadf[idx, :somecolumn]
    return sum(somevals)
end

Get vector of cluster ids from cutting the tree at a particular hight

θ = 10
clusts = cuttree(network_distance, tree, θ)
clustmapping = getleafnames.(clusts)
clusterids = Int.(vcat([zeros(length(c)) .+ j for (j, c) in enumerate(clustmapping)]...));

Write out tree from hierarchical clustering.

hc = hclust(distance_matrix, linkage=:single, branchorder=:optimal)
open("out.nw", "w") do io
    write(io, nwstr(newick(hc)) * "\n")
end

If you need tip labels that are not in the tree, use a vector of strings in the same order as the distance matrix.

open("out_1.nw", "w") do io
    tree = newick(hc, tiplabels)
    write(io, nwstr(tree) * "\n")
end

Plotting

plotting for trees in julia is not great at the moment.

For small trees NewickTree.jl has a plot recipe

using NewickTree # not needed if already set `using NewickTreeTools` as NewickTrees is reexported from this package
using StatsPlots
tree = readnw(read("filename.nw", String))
plot(tree)

For larger trees if you want to stick with julia try using Phylo.jl Phylo and NewickTrees do not play well together and have some conflicting functions, so it is best to do this in a new file or notebook

using StatsPlots
using Phylo
tree = open(parsenewick(), "filename.nw")
sort!(tree)
plot(tree)

The most featureful library for plotting trees is R's ggtree library. Which can read in newick format trees with the ape and treeio libraries.

useful links:

https://guangchuangyu.github.io/ggtree-book/chapter-ggtree.html

supressMessages({
    # library(tidyverse)
    library(ape)
    library(treeio)
    library(ggplot2)
    library(ggtree)
})
treefile <- "tree.newick"
tree <- read.tree(treefile)
p <- ggtree(tree,
    size = 2,
    ladderize = TRUE,
    # layout = "fan",
    layout = "rectangular",
    # layout = "slanted",
    ) +
    geom_tiplab(linesize = .2, size = 4)
    # geom_text(aes(label=node))
p

Citing

See CITATION.bib for the relevant reference(s).

About

No description, website, or topics provided.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages