【R语言】计算遗传距离和Fst
·
我这里有个文档,行为样本,列为SNP基因型。计算遗传距离和Fst方法如下:
# 遗传距离,遗传分化系数
setwd("D:/task")
rm(list = ls())
library(corehunter)
library(adegenet)
library(hierfstat)
library(ggplot2)
library(factoextra)
library(dplyr)
library(SNPRelate)
library(gdsfmt)
snp_data <- read.csv("snpdat.csv", row.names = 1)
# 将基因型转换为数值格式(例如:AG -> 1, AA -> 0, GG -> 2)
snp_numeric <- apply(snp_data, 2, function(x) {
x <- as.character(x)
# 提取所有等位基因(如A/G)
alleles <- sort(unique(unlist(strsplit(x, ""))))
# 编码为0/1/2(假设双等位)
code <- ifelse(x == paste0(alleles[1], alleles[1]), 0,
ifelse(x == paste0(alleles[1], alleles[2]), 1,
ifelse(x == paste0(alleles[2], alleles[1]), 1,
ifelse(x == paste0(alleles[2], alleles[2]), 2, NA))))
return(code)
})
# 处理缺失值(删除有缺失的位点)
snp_clean <- as.data.frame(snp_numeric) %>%
select(where(~ mean(is.na(.)) == 0))
# 处理缺失值(删除列和为0的位点)
snp_clean <- snp_clean %>%
select(where(~ sum(., na.rm = TRUE) != 0))
# 删除常数列
snp_clean <- snp_clean %>%
select(where(~ n_distinct(.) > 1))
rownames(snp_clean) <- rownames(snp_data)
# --------------------------
# 0. 数据准备
# --------------------------
# 分群
pca_result <- prcomp(snp_clean, scale = TRUE)
# 可视化PCA
fviz_pca_ind(pca_result,
geom = "point",
col.ind = "black",
addEllipses = TRUE,
title = "PCA of SNP Data") +
theme_minimal()
# 使用PCA前3个主成分进行聚类
pca_scores <- pca_result$x[, 1:3]
# 确定最佳K值(肘部法则)
fviz_nbclust(pca_scores, kmeans, method = "wss") +
ggtitle("Optimal Number of Clusters (Elbow Method)")
# 假设选择K=2个群体
set.seed(123)
kmeans_clusters <- kmeans(pca_scores, centers = 2)
cluster_labels <- as.factor(kmeans_clusters$cluster)
# 将聚类结果添加到数据中
snp_clean_with_clusters <- data.frame(Cluster = cluster_labels, snp_clean)
rownames(snp_clean_with_clusters) <- rownames(snp_data)
# 将数据转换为hierfstat格式# 二倍体数据
snp_clean_char <- as.data.frame(lapply(snp_clean, function(x) {
x <- as.character(x)
x[x == "0"] <- "0/0"
x[x == "1"] <- "0/1"
x[x == "2"] <- "1/1"
return(x)
}))
genind_obj <- df2genind(snp_clean_char, ploidy = 2, sep = "/")
# hierfstat_data <- df2genind(snp_clean_char, ploidy = 2, sep = "")
# hierfstat_data@pop <- cluster_labels # 使用聚类结果作为群体标签
genind_obj@pop <- cluster_labels
genpop_obj <- genind2genpop(genind_obj)
genetic_distance <- dist.genpop(genpop_obj, method = "Nei")
print(genetic_distance)
# --------------------------
# 1. 遗传距离计算
# --------------------------
# 转换为gds格式
gds_file <- "snp_data.gds"
snpgdsCreateGeno(gds_file, genmat = t(snp_clean), sample.id = rownames(snp_clean),
snp.id = colnames(snp_clean), snpfirstdim = FALSE)
# 打开gds文件
genofile <- snpgdsOpen(gds_file)
nei_distance <- genet.dist(hierfstat_data, method = "Nei")
print(as.matrix(nei_distance))
# --------------------------
# 2. 遗传分化系数Fst计算
# --------------------------
# 计算全局FST
fst_global <- genet.dist(hierfstat_data, method = "WC84") # Weir&Cockerham FST
cat("Global FST:", fst_global, "\n")
# --------------------------
# 3. 可视化群体结构
# --------------------------
# 绘制带聚类的PCA
fviz_pca_ind(pca_result,
geom = "point",
col.ind = cluster_labels,
palette = "jco",
addEllipses = TRUE,
legend.title = "Cluster",
title = "PCA with K-means Clusters (K=2)")
# 绘制遗传距离热图
pdf('heatmap.pdf')
heatmap(as.matrix(nei_distance),
symm = TRUE,
main = "Nei's Genetic Distance Heatmap",
margins = c(10, 10))
dev.off()
DAMO开发者矩阵,由阿里巴巴达摩院和中国互联网协会联合发起,致力于探讨最前沿的技术趋势与应用成果,搭建高质量的交流与分享平台,推动技术创新与产业应用链接,围绕“人工智能与新型计算”构建开放共享的开发者生态。
更多推荐



所有评论(0)