install.packages('Seurat')
library('Seurat')
library(devtools)
devtools::install_github("mojaveazure/seurat-disk")
報(bào)錯(cuò):
Unable to locate HDF5 compilation helper scripts 'h5cc' or 'h5pcc'.
Please specify --with-hdf5=<LOCATION> as the full path to h5cc or h5pcc.
# 返回linux終端
conda install conda-forge::hdf5
which h5cc
/PATH/miniconda3/envs/xxx/bin/h5cc
# 返回R
install.packages('hdf5r', configure.args = '--with-hdf5=/PATH/miniconda3/envs/xxx/bin/h5cc)
library(devtools)
devtools::install_github("mojaveazure/seurat-disk")
正式處理文件
Convert("10XIllumina.combined.TPM.h5ad" , "h5seurat" , overwrite = F , assay = "RNA")
seurat_obj <- LoadH5Seurat("10XIllumina.combined.TPM.h5seurat")
報(bào)錯(cuò)
Error: Missing required datasets ‘levels‘ and ‘values‘
# 那就先不load meta data
scRNA <- LoadH5Seurat("10XIllumina.combined.TPM.h5seurat", meta.data = FALSE, misc = FALSE)
head(scRNA)
library("rhdf5")
meta_data <- h5read("10XIllumina.combined.TPM.h5seurat", "meta.data")
str(meta_data)
>List of 6
$ ClusterFull :List of 2
..$ categories: chr [1:120(1d)] "In-house-N cE01 (Stem/TA-like)" "In-house-N cE02 (Stem/TA-like/Immature Goblet)" "In-house-N cE03 (Stem/TA-like prolif)" "In-house-N cE04 (Enterocyte 1)" ...
..$ codes : int [1:218966(1d)] 1 1 9 1 4 7 4 2 0 1 ...
$ ClusterMidway:List of 2
..$ categories: chr [1:23(1d)] "B" "DC" "Endo" "Epi" ...
..$ codes : int [1:218966(1d)] 3 3 3 3 3 3 3 3 3 3 ...
$ ClusterTop :List of 2
..$ categories: chr [1:7(1d)] "B" "Epi" "Mast" "Myeloid" ...
..$ codes : int [1:218966(1d)] 1 1 1 1 1 1 1 1 1 1 ...
$ condition :List of 2
..$ categories: chr [1:2(1d)] "normal" "tumor"
..$ codes : int [1:218966(1d)] 0 0 0 0 0 0 0 0 0 0 ...
$ library_id :List of 2
..$ categories: chr [1:202(1d)] "C103_T_1_1_0_c1_v2" "C103_T_1_1_3_c1_v2" "C104_T_1_1_0_c1_v2" "C104_T_1_1_3_c1_v2" ...
..$ codes : int [1:218966(1d)] 188 188 188 188 188 188 188 188 188 188 ...
$ sample_source:List of 2
..$ categories: chr [1:2(1d)] "bc295" "in-house"
..$ codes : int [1:218966(1d)] 1 1 1 1 1 1 1 1 1 1 ...
# 將values轉(zhuǎn)成labels形式
new_meta_data <- lapply(meta_data, function(x) {return(x$categories[x$codes])})
# 但是發(fā)現(xiàn)轉(zhuǎn)過(guò)來(lái)數(shù)量對(duì)不上,應(yīng)該是218966才對(duì)
str(new_meta_data)
>List of 6
$ ClusterFull : chr [1:216724(1d)] "In-house-N cE01 (Stem/TA-like)" "In-house-N cE01 (Stem/TA-like)" "In-house-N cE09 (Best4)" "In-house-N cE01 (Stem/TA-like)" ...
$ ClusterMidway: chr [1:204472(1d)] "Endo" "Endo" "Endo" "Endo" ...
$ ClusterTop : chr [1:204472(1d)] "B" "B" "B" "B" ...
$ condition : chr [1:146034(1d)] "normal" "normal" "normal" "normal" ...
$ library_id : chr [1:216116(1d)] "M-21-2089_PS019-N" "M-21-2089_PS019-N" "M-21-2089_PS019-N" "M-21-2089_PS019-N" ...
$ sample_source: chr [1:18966(1d)] "bc295" "bc295" "bc295" "bc295" ...
# 查看是否有非法索引
lapply(meta_data, function(x) {
invalid_codes <- x$codes[!x$codes %in% seq_along(x$categories)]
length(invalid_codes) # 返回非法索引的數(shù)量
})
#還真有!
$ClusterFull
[1] 2242
$ClusterMidway
[1] 14494
$ClusterTop
[1] 14494
$condition
[1] 72932
$library_id
[1] 2850
$sample_source
[1] 200000
# 查看是否有超出范圍的索引
lapply(meta_data, function(x) {
table(x$codes > length(x$categories)) # 檢查是否有超出范圍的索引
})
# 這個(gè)木有
$ClusterFull
FALSE
218966
$ClusterMidway
FALSE
218966
$ClusterTop
FALSE
218966
$condition
FALSE
218966
$library_id
FALSE
218966
$sample_source
FALSE
218966
# 替換非法索引為默認(rèn)值
new_meta_data <- lapply(meta_data, function(x) {
valid_codes <- ifelse(x$codes %in% seq_along(x$categories), x$codes, NA)
x$categories[valid_codes]
})
# 數(shù)量正常了,而且這個(gè)時(shí)候去試試table(new_meta_data$ClusterMidway)
# 竟然也沒(méi)有NA,神奇.....不知為何
str(new_meta_data)
>List of 6
$ ClusterFull : chr [1:218966(1d)] "In-house-N cE01 (Stem/TA-like)" "In-house-N cE01 (Stem/TA-like)" "In-house-N cE09 (Best4)" "In-house-N cE01 (Stem/TA-like)" ...
$ ClusterMidway: chr [1:218966(1d)] "Endo" "Endo" "Endo" "Endo" ...
$ ClusterTop : chr [1:218966(1d)] "B" "B" "B" "B" ...
$ condition : chr [1:218966(1d)] NA NA NA NA ...
$ library_id : chr [1:218966(1d)] "M-21-2089_PS019-N" "M-21-2089_PS019-N" "M-21-2089_PS019-N" "M-21-2089_PS019-N" ...
$ sample_source: chr [1:218966(1d)] "bc295" "bc295" "bc295" "bc295" ...
# 對(duì)scRNA重新賦值meta_data
scRNA$ClusterFull<-new_meta_data$ClusterFull
scRNA$ClusterMidway<-new_meta_data$ClusterMidway
scRNA$ClusterTop<-new_meta_data$ClusterTop
scRNA$library_id<-new_meta_data$library_id
# 查看meta_data是否有加進(jìn)去
head(scRNA)