By wuzhenzhen, 30 June, 2026

treemix 是利用等位基因频率来推断群体间分化和杂合(基因流动或者基因渗入)的软件。
教程:https://flystar233.github.io/2020/09/07/treemix/

01 安装软件

conda create -n treemix
conda activate treemix
conda install -c bioconda treemix
conda deactivate

02 格式转换

# 格式转换:变异数据储存格式为vcf,首先将其转换为tped格式。
vcftools --vcf ../8Rmolle_re_LD.2050.1.LDfilter.vcf --plink-tped --out 8Rmolle_re_LD.2050.1.LDfilter

# 转换后的tfam文件如下:第一列为FID,第二列为IID,我们需要修改第一列数据为群体信息。
sed -i 's/^RB-[0-9]*/RB/g' 8Rmolle_re_LD.2050.1.LDfilter.tfam

# 然后再编辑一个群体信息文件(pop.cov),格式如下
XM wHAIPI052555-68	XM
XM wHAIPI052556-77	XM
XM wHAIPI052557-76	XM

# 编辑好之后,计算等位基因组频率:
plink --tfile test --freq  --within pop.cov

# 然后使用treemix 自带的脚本 plink2treemix.py进行格式转换,转换前需要对等位基因频率文件压缩
python2 plink2treemix.py plink.frq.strat.gz treemix_in.gz

03 基因渗入分析

treemix -se -bootstrap -k 1000 -m 1 -i treemix_in.gz
或者进行多个m和重复:
for m in {1..5}
   do
   for i in {1..20}
      do
      treemix -se -bootstrap \
         -i treemix_in.gz \
         -o TreeMix.${i}.${m} \
         -global \
         -m ${m} \
         -k 1000
      done 
done
# 其中m参数为 the number of migration edges,中文翻译过来其实就是基因渗入方向的个数,默认为0,可以尝试1-10,并在每个m中重复10次.

treemix -i treemix_in.gz -m 15 -o out_15_rep${i} -k 500 > treemix_15_rep${i}.log

04 最佳迁移边缘个数选择

# 将生成的llik、cov、modelcov 文件放置同一文件夹,使用 R 包OptM进行分析:
library(OptM)
linear = optM("./result")
plot_optM(linear)
# 生成图中,当Δm值最小时的 migration edges 为最佳迁移边缘个数。

05 基因渗入作图

# 确定最佳迁移边缘个数之后,我们使用 m=最佳迁移边缘个数的结果文件作图。
source("plotting_funcs.R") #treemix scr文件夹中R脚本
plot_tree("TreeMix") #TreeMix为结果文件前缀