-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathScanpy2Seurat.pyR
47 lines (40 loc) · 3.47 KB
/
Scanpy2Seurat.pyR
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
def writeToCsv(adata, project_name):
if isinstance(adata.layers["counts"], np.ndarray):
counts_slot = pd.DataFrame(data=adata.layers["counts"], index=adata.obs_names, columns=adata.var_names).T
data_slot = pd.DataFrame(data=adata.X, index=adata.obs_names, columns=adata.var_names).T
scaledata_slot = pd.DataFrame(data=adata.layers["scaled"], index=adata.obs_names, columns=adata.var_names).T
else:
counts_slot = pd.DataFrame(data=adata.layers["counts"].toarray(), index=adata.obs_names, columns=adata.var_names).T
data_slot = pd.DataFrame(data=adata.X.toarray(), index=adata.obs_names, columns=adata.var_names).T
scaledata_slot = pd.DataFrame(data=adata.layers["scaled"], index=adata.obs_names, columns=adata.var_names).T
metadata = adata.obs
embedding_pca = pd.DataFrame(data=adata.obsm["X_pca"], index=adata.obs_names)
embedding_umap = pd.DataFrame(data=adata.obsm["X_umap"], index=adata.obs_names)
counts_slot.to_csv("%s.seu.counts_slot"%project_name, header=True, index=True, sep="\t", encoding='utf-8')
data_slot.to_csv("%s.seu.data_slot"%project_name, header=True, index=True, sep="\t", encoding='utf-8')
scaledata_slot.to_csv("%s.seu.scaledata_slot"%project_name, header=True, index=True, sep="\t", encoding='utf-8')
metadata.to_csv("%s.seu.metadata"%project_name, header=True, index=True, sep="\t", encoding='utf-8')
embedding_pca.to_csv("%s.seu.embedding_pca"%project_name, header=True, index=True, sep="\t", encoding='utf-8')
embedding_umap.to_csv("%s.seu.embedding_umap"%project_name, header=True, index=True, sep="\t", encoding='utf-8')
#################################################################################################
#################################################################################################
#################################################################################################
tic()
counts <-as.matrix(read.table(file="200409-ABM2-totalclustered.seu.counts_slot", sep="\t",header=T,row.names = 1,check.names=F))
data <-as.matrix(read.table(file="200409-ABM2-totalclustered.seu.data_slot", sep="\t",header=T,row.names = 1,check.names=F))
scale.data <-as.matrix(read.table(file="200409-ABM2-totalclustered.seu.scaledata_slot", sep="\t",header=T,row.names = 1,check.names=F))
meta.data <- read.table(file="200409-ABM2-totalclustered.seu.metadata", sep="\t",header=T,row.names = 1,check.names=F,encoding = "UTF-8")
embedding_pca <-as.matrix(read.table(file="200409-ABM2-totalclustered.seu.embedding_pca", sep="\t",header=T,row.names = 1,check.names=F))
embedding_umap <-as.matrix(read.table(file="200409-ABM2-totalclustered.seu.embedding_umap", sep="\t",header=T,row.names = 1,check.names=F))
toc()
tic()
seu<-CreateSeuratObject(counts=counts, project="ABM2ALP",meta.data = meta.data)
seu<-SetAssayData(seu, slot="data", new.data = data)
seu<-SetAssayData(seu, slot="scale.data", new.data = scale.data)
colnames(embedding_pca) <-paste0("PC_",1:50)
colnames(embedding_umap) <-paste0("UMAP_",1:2)
rownames(embedding_pca) <-rownames(seu[[]])
rownames(embedding_umap) <-rownames(seu[[]])
seu[["umap"]] <-CreateDimReducObject(embeddings = embedding_umap, key = "UMAP_", assay = DefaultAssay(seu))
seu[["pca"]] <-CreateDimReducObject(embeddings = embedding_pca, key = "PC_", assay = DefaultAssay(seu))
toc()