如何從CisBP數據庫生成ArchR使用的Motif PWM文件

1. 下載Cisbp數據庫中的motif文件

(1) 下載文件

CISBP數據庫

CISBP

點擊Bulk downloads進入到Bulk downloads界面
Bulk downloads

選擇想對應的物種,然后點擊Downloa Entire dataset Archive下載數據

(2) 理解文件

我下載下來的文件名字是Macaca_fascicularis_2024_09_12_9:10_am.zip
然后進行解壓,解壓完之后的文件如下所示


Macaca_fascicularis_2024_09_12

a. logos_all_motifs文件夾

logos_all_motifs文件夾放的是motif的圖像


image.png

以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個文件


pwms_all_motifs

cat M09067_2.00.txt


M09067_2.00.txt

c.TF轉錄因子相關文件

TF_Information_all_motifs_plus.txt


TF_Information_all_motifs_plus

TF_Information_all_motifs.txt


TF_Information_all_motifs

TF_Information.txt
TF_Information
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

image.png

到了這個頁面點擊download excel spreadsheet

image.png

(http://cisbp2.ccbr.utoronto.ca/downloads.php)

但解壓后PWM.txt文件是空的,也不知道為什么


image.png

總之反正我摸索了下,得從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


image.png

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了,我真棒呀哈哈哈哈

?著作權歸作者所有,轉載或內容合作請聯系作者
  • 序言:七十年代末,一起剝皮案震驚了整個濱河市,隨后出現的幾起案子,更是在濱河造成了極大的恐慌,老刑警劉巖,帶你破解...
    沈念sama閱讀 227,572評論 6 531
  • 序言:濱河連續發生了三起死亡事件,死亡現場離奇詭異,居然都是意外死亡,警方通過查閱死者的電腦和手機,發現死者居然都...
    沈念sama閱讀 98,071評論 3 414
  • 文/潘曉璐 我一進店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人,你說我怎么就攤上這事。” “怎么了?”我有些...
    開封第一講書人閱讀 175,409評論 0 373
  • 文/不壞的土叔 我叫張陵,是天一觀的道長。 經常有香客問我,道長,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 62,569評論 1 307
  • 正文 為了忘掉前任,我火速辦了婚禮,結果婚禮上,老公的妹妹穿的比我還像新娘。我一直安慰自己,他們只是感情好,可當我...
    茶點故事閱讀 71,360評論 6 404
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著,像睡著了一般。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發上,一...
    開封第一講書人閱讀 54,895評論 1 321
  • 那天,我揣著相機與錄音,去河邊找鬼。 笑死,一個胖子當著我的面吹牛,可吹牛的內容都是我干的。 我是一名探鬼主播,決...
    沈念sama閱讀 42,979評論 3 440
  • 文/蒼蘭香墨 我猛地睜開眼,長吁一口氣:“原來是場噩夢啊……” “哼!你這毒婦竟也來了?” 一聲冷哼從身側響起,我...
    開封第一講書人閱讀 42,123評論 0 286
  • 序言:老撾萬榮一對情侶失蹤,失蹤者是張志新(化名)和其女友劉穎,沒想到半個月后,有當地人在樹林里發現了一具尸體,經...
    沈念sama閱讀 48,643評論 1 333
  • 正文 獨居荒郊野嶺守林人離奇死亡,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內容為張勛視角 年9月15日...
    茶點故事閱讀 40,559評論 3 354
  • 正文 我和宋清朗相戀三年,在試婚紗的時候發現自己被綠了。 大學時的朋友給我發了我未婚夫和他白月光在一起吃飯的照片。...
    茶點故事閱讀 42,742評論 1 369
  • 序言:一個原本活蹦亂跳的男人離奇死亡,死狀恐怖,靈堂內的尸體忽然破棺而出,到底是詐尸還是另有隱情,我是刑警寧澤,帶...
    沈念sama閱讀 38,250評論 5 356
  • 正文 年R本政府宣布,位于F島的核電站,受9級特大地震影響,放射性物質發生泄漏。R本人自食惡果不足惜,卻給世界環境...
    茶點故事閱讀 43,981評論 3 346
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧,春花似錦、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 34,363評論 0 25
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至,卻和暖如春,著一層夾襖步出監牢的瞬間,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 35,622評論 1 280
  • 我被黑心中介騙來泰國打工, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留,地道東北人。 一個月前我還...
    沈念sama閱讀 51,354評論 3 390
  • 正文 我出身青樓,卻偏偏與公主長得像,于是被迫代替她去往敵國和親。 傳聞我的和親對象是個殘疾皇子,可洞房花燭夜當晚...
    茶點故事閱讀 47,707評論 2 370

推薦閱讀更多精彩內容