-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmonocle_to_anndata.R
166 lines (125 loc) · 4.26 KB
/
monocle_to_anndata.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
nb_rna <-
readRDS(
file = "data_generated/rna_decontaminated.rds")
nb_rna_meta <-
readRDS(
file = "data_generated/metadata.rds")
# rna_integrated_monocle.rds
# sce <- SingleCellExperiment(list(counts=counts))
# exprs(nb_rna)
# pData(nb_rna) %>%
# data.frame() %>%
# View()
# reducedDims(nb_rna)[['PCA']]
# reducedDims(nb_rna)[["Aligned"]]
colsum_nb_rna <- Matrix::colSums(exprs(nb_rna))
sampled_cell_isd <- sample(colsum_nb_rna,73872)
mat <- exprs(nb_rna)
mat1 <- mat[, which(colnames(mat) %in% names(sampled_cell_isd))]
mat2 <- nb_rna_meta[which(colnames(mat) %in% names(sampled_cell_isd)),]
# sce <-
# SingleCellExperiment(
# list(counts=exprs(nb_rna)),
# metadata = pData(nb_rna)
# )
sce <-
SingleCellExperiment(
list(counts=mat1),
metadata =mat2
)
sce <-
SingleCellExperiment(
list(counts=exprs(nb_rna)),
metadata = nb_rna_meta
)
col_order <- colnames(sce)
pca <- reducedDims(nb_rna)[['PCA']]
pca <- pca[which(rownames(pca) %in% names(sampled_cell_isd)),]
pca <- pca[order(factor(rownames(pca), levels=col_order)),]
umap <- reducedDims(nb_rna)[['UMAP']]
umap <- umap[which(rownames(umap) %in% names(sampled_cell_isd)),]
umap <- umap[order(factor(rownames(umap), levels=col_order)),]
tsne <- reducedDims(nb_rna)[['tSNE']]
tsne <- tsne[which(rownames(tsne) %in% names(sampled_cell_isd)),]
tsne <- tsne[order(factor(rownames(tsne), levels=col_order)),]
aligned <- reducedDims(nb_rna)[['Aligned']]
aligned <- aligned[which(rownames(aligned) %in% names(sampled_cell_isd)), ]
aligned <- aligned[order(factor(rownames(aligned), levels=col_order)),]
# reducedDim(sce, "PCA") <- pca #reducedDims(nb_rna)[['PCA']]
# reducedDim(sce, "UMAP") <- umap #reducedDims(nb_rna)[['UMAP']]
# reducedDim(sce, "tSNE") <- tsne #reducedDims(nb_rna)[['tSNE']]
# reducedDim(sce, "Aligned") <- aligned #reducedDims(nb_rna)[['Aligned']]
reducedDim(sce, "PCA") <- reducedDims(nb_rna)[['PCA']]
reducedDim(sce, "UMAP") <- reducedDims(nb_rna)[['UMAP']]
reducedDim(sce, "tSNE") <- reducedDims(nb_rna)[['tSNE']]
reducedDim(sce, "Aligned") <- reducedDims(nb_rna)[['Aligned']]
## Save H5AD file
sceasy::convertFormat(
Cluster_myeloid,
main_layer = 'counts',
from="seurat",
to="anndata",
outFile='myeloid.h5ad')
library(zellkonverter)
SCE2AnnData(sce)
sce_rna <-
SCE2AnnData(
sce,
X_name = 'counts',
#obs = TRUE,
skip_assays = FALSE)
anndata::write_h5ad(
sce_rna,
filename = "sce_rna_wes_sampled.h5ad",
compression = "gzip",
compression_opts=9,
as_dense = "X"
)
myeloid.only <- sce[, sce@metadata$cellont_abbr == "M"]
ncol(counts(myeloid.only))
colData(myeloid.only)
mat3 <- mat[, which(colnames(mat) %in% colnames(myeloid.only))]
mat4 <- nb_rna_meta[which(colnames(mat) %in% colnames(myeloid.only)),]
sce_myeloid <-
SingleCellExperiment(
list(counts = mat3),
metadata = mat4
)
col_order <- colnames(sce_myeloid)
pca <- reducedDims(nb_rna)[['PCA']]
pca <- pca[which(rownames(pca) %in% colnames(myeloid.only)),]
pca <- pca[order(factor(rownames(pca), levels=col_order)),]
umap <- reducedDims(nb_rna)[['UMAP']]
umap <- umap[which(rownames(umap) %in% colnames(myeloid.only)),]
umap <- umap[order(factor(rownames(umap), levels=col_order)),]
tsne <- reducedDims(nb_rna)[['tSNE']]
tsne <- tsne[which(rownames(tsne) %in% colnames(myeloid.only)),]
tsne <- tsne[order(factor(rownames(tsne), levels=col_order)),]
aligned <- reducedDims(nb_rna)[['Aligned']]
aligned <- aligned[which(rownames(aligned) %in% colnames(myeloid.only)), ]
aligned <- aligned[order(factor(rownames(aligned), levels=col_order)),]
reducedDim(sce_myeloid, "PCA") <- pca #reducedDims(nb_rna)[['PCA']]
reducedDim(sce_myeloid, "UMAP") <- umap #reducedDims(nb_rna)[['UMAP']]
reducedDim(sce_myeloid, "tSNE") <- tsne #reducedDims(nb_rna)[['tSNE']]
reducedDim(sce_myeloid, "Aligned") <- aligned #reducedDims(nb_rna)[['Aligned']]
library(zellkonverter)
sce_rna <-
SCE2AnnData(
sce_myeloid,
X_name = 'counts',
#obs = TRUE,
skip_assays = FALSE)
anndata::write_h5ad(
sce_rna,
filename = "sce_rna_myeloid.h5ad",
compression = "gzip",
compression_opts=9,
as_dense = "X"
)
## Save H5AD file
sceasy::convertFormat(
sce_myeloid,
#main_layer = 'counts',
from="sce",
to="anndata",
outFile='myeloid_rna.h5ad')