h5ad文件转为seuratObject

install.packages('Seurat')
library('Seurat')
library(devtools)
devtools::install_github("mojaveazure/seurat-disk")

报错:
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")

报错
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转成labels形式
new_meta_data <- lapply(meta_data, function(x) {return(x$categories[x$codes])})

# 但是发现转过来数量对不上,应该是218966才对
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)  # 返回非法索引的数量
})

#还真有!
$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))  # 检查是否有超出范围的索引
})

# 这个木有
$ClusterFull
 FALSE 
218966 
$ClusterMidway
 FALSE 
218966 
$ClusterTop
 FALSE 
218966 
$condition
 FALSE 
218966 
$library_id
 FALSE 
218966 
$sample_source
 FALSE 
218966 

# 替换非法索引为默认值
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]
})

# 数量正常了,而且这个时候去试试table(new_meta_data$ClusterMidway)
# 竟然也没有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" ...


 # 对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是否有加进去
head(scRNA)
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容