1. 下載Cisbp數據庫中的motif文件
(1) 下載文件
點擊Bulk downloads進入到Bulk downloads界面
選擇想對應的物種,然后點擊Downloa Entire dataset Archive下載數據
(2) 理解文件
我下載下來的文件名字是Macaca_fascicularis_2024_09_12_9:10_am.zip
然后進行解壓,解壓完之后的文件如下所示
a. logos_all_motifs文件夾
logos_all_motifs文件夾放的是motif的圖像
以M08124_2.00_fwd.png 和M09199_2.00_rev.png為例子
M08124_2.00_fwd.png:該文件表示 M08124_2.00 基序在正鏈(forward strand)上的圖像或可視化結果。這是基序在正向方向上的表現。
M09199_2.00_rev.png:該文件表示 M09199_2.00 基序在反鏈(reverse strand)上的圖像或可視化結果。它是基序在反向方向上的表現,通常是正鏈的互補序列。
我一共下載下來的是5899個motifs
cd Macaca_fascicularis_cisbp_20240912/logos_all_motifs
ls | grep rev | wc -l
# 5899
ls | grep png | wc -l
# 11798
ls | grep rev | wc -l
# 5899
b. pwms_all_motifs文件夾
pwms_all_motifs文件夾放的是motifs的pwms的文件,也是5899個文件
cat M09067_2.00.txt
c.TF轉錄因子相關文件
TF_Information_all_motifs_plus.txt
TF_Information_all_motifs.txt
TF_Information.txt
cat TF_Information_all_motifs_plus.txt | wc -l # 70015行 包含了TF和所有的motif
cat TF_Information_all_motifs.txt | wc -l # 70015行 包含了TF和所有的motif 感覺和TF_Information_all_motifs_plus.txt文件一樣
cat TF_Information.txt | wc -l # 1340行 是和motif結合的轉錄因子的信息
這個1340行,我是從官網home頁面進去后選擇物種點擊Go
到了這個頁面點擊download excel spreadsheet
(http://cisbp2.ccbr.utoronto.ca/downloads.php)
但解壓后PWM.txt文件是空的,也不知道為什么
總之反正我摸索了下,得從Bulk downloads界面去下載你所需要的物種的motifs,那么解壓下來就會有motif_pwm.txt文件了
2. 格式轉換:將CisBP Motif文件處理為ArchR兼容的Motif PWMs格式
(1) 循環讀取每個 PWM 文件構建PWMatrixLists文件
這一步我是參考這個
rm(list = ls())
# Macaca_fascicularis_cisbp_20240912/pwms_all_motifs從http://cisbp-rna.ccbr.utoronto.ca/bulk.php選擇相應物種,然后download entire dataset archive
library(TFBSTools) # 參考https://blog.csdn.net/u012110870/article/details/102804617
motif_dir <- "Macaca_fascicularis_cisbp_20240912/pwms_all_motifs"
PWList <- list()
# 初始化跳過的文件計數器
skipped_files <- 0
# 循環讀取每個 PWM 文件
for (file in list.files(motif_dir, pattern = ".txt", full.names = TRUE)) {
df <- read.table(file, header = TRUE, row.names = 1)
# 檢查文件是否為空或是否有有效的堿基頻率信息
if (nrow(df) == 0 || all(is.na(df))) {
message(paste("Skipping file:", file, "- No valid data"))
skipped_files <- skipped_files + 1 # 增加跳過文件計數
next
}
# 將數據轉換為矩陣
mt <- as.matrix(df)
# 提取文件名作為 motif ID
motif_id <- substr(basename(file), 1, 6)
# 創建 PWM 對象并存儲
PWList[[motif_id]] <- PWMatrix(ID = motif_id, profileMatrix = t(mt))
}
# 將 PWM 對象組合成 PWMatrixList
PWMatrixLists <- do.call(PWMatrixList, PWList) # 5899
# 打印結果
print(PWMatrixLists)
# 打印跳過的文件數量
message(paste("Total number of skipped files:", skipped_files)) # 919 # 一共是6818,減去919還剩5899
(2) 添加TF的信息
# 嘗試使用 fill 參數讀取文件
tf_info <- read.table(
"Macaca_fascicularis_cisbp_20240912/TF_Information_all_motifs_plus.txt",
header = TRUE,
sep = "\t",
stringsAsFactors = FALSE,
fill = TRUE # 自動填充不完整的行
)
tf_info$Motif_ID <- substr(tf_info$Motif_ID, 1, 6)
length(unique(tf_info$TF_ID)) # 1340
tf_info <- tf_info[tf_info$Motif_ID %in% names(PWMatrixLists), ]
length(unique(tf_info$TF_ID))
# [1] 846 過濾了, 有些沒有PWMS文件
motif_ids <- names(PWMatrixLists)
# 將 profileMatrix 的列名設為 NULL
# tmp <- PWMatrixLists
# 循環遍歷每個 PWM 對象并補充元數據
for (motif_id in motif_ids) {
# 提取對應的 motif 信息
motif_info <- tf_info %>% filter(Motif_ID == motif_id)
if (nrow(motif_info) > 0) {
# 提取相關信息
motif_name <- motif_info$TF_ID[1]
family_name <- motif_info$Family_Name[1]
species <- "Macaca fascicularis" # 統一物種信息
medline <- motif_info$PMID[1]
motif_type <- motif_info$Motif_Type[1]
collection <- motif_info$MSource_Identifier[1]
# 補充到 PWMatrix 對象中
PWMatrixLists[[motif_id]] <- PWMatrix(
ID = motif_id,
name = motif_name,
matrixClass = family_name,
profileMatrix = PWMatrixLists[[motif_id]]@profileMatrix, # 保留原始矩陣
strand = "+", # 假設為正鏈
tags = list(
comment = "Motif information added from TF_Information_all_motifs_plus.txt",
medline = medline,
type = motif_type,
collection = collection,
species = species
)
)
} else {
message(paste("No information found for motif:", motif_id))
}
}
# 檢查更新后的 PWMatrixLists 因為pwm的colnames需要是NULL,所以多這一步
print(PWMatrixLists)
for (i in seq_along(PWMatrixLists)) {
# 將 profileMatrix 的列名設為 NULL
colnames(PWMatrixLists[[i]]@profileMatrix) <- NULL
}
(3) 去除上面列和不是1的motif 列
其實這個是因為要么原始文件行和加起來不是1,要么就是R中浮點精度的問題,是個坑
我是根據這個Issue 提示然后過濾掉這些error motifs的,我還沒想到最終怎么去解決這些error motifs,先這樣子
# 初始化一個空列表來存儲不符合標準的 PWM 名稱
error_list <- list()
# 遍歷 PWMatrixLists 中的所有 PWM
for (i in seq_along(PWMatrixLists)) {
# 獲取當前 PWM 的名字
pwm_name <- names(PWMatrixLists)[i]
# 獲取當前 PWM 的 profileMatrix
profile_matrix <- PWMatrixLists[[i]]@profileMatrix
# 檢查列的和是否為 1
colsum_check <- all.equal(colSums(as.matrix(profile_matrix)), rep(1, ncol(profile_matrix)))
# 如果不符合條件,將 PWM 名稱加入到錯誤列表中
if (!isTRUE(colsum_check)) {
error_list[[pwm_name]] <- colsum_check
}
}
# 輸出錯誤列表的 PWM 名稱
print(names(error_list))
length(error_list)
PWMatrixLists_new <- PWMatrixLists[names(PWMatrixLists) %ni% names(error_list)] # %ni% 不在額返回true,就是提取不是error_list的PWMatrixLists
PWMatrixLists_new <- ArchR:::.summarizeJASPARMotifs(PWMatrixLists_new)
saveRDS(PWMatrixLists_new$motifs, "mcf_cisbp_PWMatrixLists_new_remove_errorlist.rds")
最終從原先的5899 motifs變成了5418 motifs
3. 在ArchR中使用addMotifAnnotations函數運行Motif PWMs文件
rm(list = ls())
setwd("~/analysis/20240914_atac_analysis")
library(ArchR)
library(motifmatchr)
library(BSgenome.Mfascicularis.Ensembl.mcf6)
addArchRThreads(threads = 12)
mcf_motifs <- readRDS("mcf_cisbp_PWMatrixLists_new_remove_errorlist.rds")
projHeme5 <- loadArchRProject(path = "Save-ProjHeme_MACS2_addpeaks/", force = FALSE, showLogo = TRUE)
print("projHeme5_addMotifAnnotations_mcf_motifs_500.rds")
projHeme5 <- addMotifAnnotations(
ArchRProj = projHeme5,
annoName = "mcf_motifs_500",
motifPWMs = mcf_motifs,
motifSet = NULL,
collection = NULL,
species = NULL
)
saveRDS(projHeme5, "projHeme5_addMotifAnnotations_mcf_motifs_allls.rds")
摸索了很久,解決了各種bug,終于總算可以自己構建成功了,可以用來跑archr中的addMotifAnnotations了,我真棒呀哈哈哈哈