跟着Nature ecology&evolution学python:toytree模块画进化树
2023-02-19 12:27:43 时间
论文
Replicated radiation of a plant clade along a cloud forest archipelago
https://www.nature.com/articles/s41559-022-01823-x#data-availability
数据代码链接
https://zenodo.org/record/5504439#.YtpqTXZBzic
https://github.com/eaton-lab/Oreinotinus-phylogeny
论文里提供了几乎所有数据和代码,要好好学习一下这篇论文
今天推文的主要内容是重复其中 Extended Data Fig. 1 ,不对绘图代码进行仔细研究了,知道python里有一个 toytree的模块可以展示进化树,看了代码,应该是不如ggtree好用 帮助文档链接 https://toytree.readthedocs.io/en/latest/3-installation.html
github主页链接https://github.com/eaton-lab/toytree
还有一个模块是ipyrad,用来做群体遗传分析,帮助文档链接 https://ipyrad.readthedocs.io/en/master/
完整的作图代码
import toytree
import pandas as pd
fulldata = pd.read_csv("../Oreinotinus-phylogeny-main/Raw_data/oreinotinus_samples_database.csv")
colors = pd.read_csv("../Oreinotinus-phylogeny-main/Raw_data/oreinotinus_color_codes.csv")
sdata = fulldata[["NameInAssembly","Lastest_SP_name", "Num_for_Publication", "UltimateName"]]
namedict = {}
for i in range(sdata.shape[0]):
if sdata.iloc[i, 2]:
number = " (" + str(sdata.iloc[i, 2]) + ")"
else:
number = ""
namedict[sdata.iloc[i, 0]] = f"V. {sdata.iloc[i, 1]}{number}"
# load color data and put in a dictionary
colordata = colors[["Species","Color"]]
colordict = {colordata.iloc[i, 0]: str(colordata.iloc[i, 1]) for i in range(colordata.shape[0])}
treeFile = "../Oreinotinus-phylogeny-main/Analyses/Individual-level-tree-reconstruction/analysis-raxml/RAxML_bipartitions.fulldataset_withAyava_10scaff_mcov025_rmcov01_mar2021"
tre = toytree.tree(treeFile)
rtre = tre.root(wildcard="dentatum")
# do some rotations to fit with geo
for i in [309,262,251,252,239,233]:
rtre.idx_dict[i].children.reverse()
rtre._coords.update()
# set new names
labels_updated = [namedict[i] for i in rtre.get_tip_labels()]
color_labels = []
# set color base on leaf form
for i in labels_updated:
result = "Black"
for key, item in colordict.items():
if i.find(key) > -1:
result = item
color_labels.append(result)
# collapse weak supported nodes
# rtre = rtre.collapse_nodes(min_support=75)
# define threshold
support_value_threshold = 84
# plot the tree
canvas, axes, marks = rtre.draw(
height=1800, width=600,
use_edge_lengths=True,
tip_labels_align=True,
tip_labels_style={"font-size": "12px"},
tip_labels=labels_updated,
# tip_labels_colors=color_labels,
node_sizes=[5 if i else 0 for i in rtre.get_node_values()],
node_colors=['black' if (i and int(i) > support_value_threshold) else 'white' for i in rtre.get_node_values('support', 1, 1)],
# node_colors=colors,
node_style={"stroke": "black", "stroke-width": 1},
# node_labels="support"
node_labels=['' if (i and int(i) > support_value_threshold) else i for i in rtre.get_node_values('support', 1, 0)],
node_labels_style= {
"-toyplot-anchor-shift": "10px",
"baseline-shift": "0px",
"text-shadow": "0.5px 0.5px #fff, -0.5px 0.5px #fff, 0.5px -0.5px #fff, -0.5px -0.5px #fff",
"fill": "#000",
"font-size": 8,
},
# node_labels="idx",
);
import toyplot.pdf
toyplot.svg.render(canvas, "./RAxML_bipartitions.fulldataset_withAyava_10scaff_mcov025_rmcov01_mar2021.svg")
保存的是svg格式,试了下保存pdf,遇到了报错,暂时不知道什么原因
最终结果
输出结果太长,就不在这里截图展示了
示例数据和代码可以自己到论文中获取
相关文章
- 基于Python的人脸识别考勤监控
- Python答题游戏
- Python调用百度API实现图像识别
- Python全栈疫情分析项目
- Python-基础-if语句快速入门
- Python-基础-循环语句快速入门
- Python-GUI签名
- 【2】VScode 搭建python和tensorflow环境
- python 简易时钟
- 【1】windows系统如何安装后缀是whl的python库
- 【2】超级详细Python-matplotlib画图,手把手教你画图!(线条颜色、大小、线形、标签)
- 【4】python读写文件操作---详细讲解!
- 【2】Anaconda下:ipython文件的打开方式,Jupyter Notebook中运行.py文件,快速打开ipython文件的方法!
- 【编写环境一】遇到常见python函数处理方式
- 【编写环境二】python库scipy.stats各种分布函数生成、以及随机数生成【泊松分布、正态分布等】
- Python xlwt数据保存到 Excel中以及xlrd读取excel文件画图
- 强化学习技巧三:Python多进程
- 强化学习技巧五:numba提速python程序
- 【7】python_matplotlib 输出(保存)矢量图方法;画图时图例说明(legend)放到图像外侧;Python_matplotlib图例放在外侧保存时显示不完整问题解决
- 【8】python_matplotlib改变横坐标和纵坐标上的刻度(ticks)、sagemath-list_plot()调整图例(legend)中点的数量、Matplotlib画各种论文图