library(spacexr)
library(spASE)
library(tibble)
library(data.table)
library(Matrix)
library(dplyr)
library(tidyr)
cerebellum_ref <- readRDS('../inst/extdata/reference_scrna/cerebellum_sn_ref_spacexr.rds')
PATH <- '../inst/extdata/slideseq/cere3'
positions1 <- fread(file.path(PATH, 'Puck_221014_23_barcode_matching.txt.gz'), header=F, col.names = c('bead','idk1','x','y')) |>
distinct(bead, .keep_all=T) |>
mutate(bead = paste0(bead, '_1'))
counts1 <- fread(file.path(PATH, '2022-12-11_Puck_221014_23_combined_atropos_polyAGtrim_nm3.csv')) |>
mutate(bead = paste0(substr(bead, 1, 14), '_1')) |>
filter(bead %in% positions1$bead)
positions2 <- fread(file.path(PATH, 'Puck_221014_24_barcode_matching.txt.gz'), header=F, col.names = c('bead','idk1','x','y')) |>
distinct(bead, .keep_all=T) |>
mutate(bead = paste0(bead, '_2'))
counts2 <- fread(file.path(PATH, '2022-12-11_Puck_221014_24_combined_atropos_polyAGtrim_nm3.csv')) |>
mutate(bead = paste0(substr(bead, 1, 14), '_2')) |>
filter(bead %in% positions2$bead)
counts <- counts1 |>
bind_rows(counts2) |>
arrange(bead, gene) |>
mutate(total = CAST+`129`+Unassigned) |>
mutate(bead = as.factor(bead), gene = as.factor(gene))
counts_matrix <- sparseMatrix(i=counts$gene, j=counts$bead, x=counts$total)
rownames(counts_matrix) <- levels(counts$gene)
colnames(counts_matrix) <- levels(counts$bead)
# MANUAL ALIGNING THE PUCKS
# rotation
xcenter <- (max(positions2$x)+min(positions2$x))/2
ycenter <- (max(positions2$y)+min(positions2$y))/2
xshift <- 748
yshift <- -241
angle <- 86
x <- positions2$x
y <- positions2$y
xnew <- (x-xcenter)*cos(angle*pi/180) - (y-ycenter)*sin(angle*pi/180)+xcenter + xshift
ynew <- (x-xcenter)*sin(angle*pi/180) + (y-ycenter)*cos(angle*pi/180)+ycenter + yshift
positions2$x <- xnew
positions2$y <- ynew
maternal_counts<- counts1 |>
bind_rows(counts2) |>
arrange(bead, gene) |>
mutate(bead = as.factor(bead), gene = as.factor(gene))
maternal_counts_matrix <- sparseMatrix(i=maternal_counts$gene, j=maternal_counts$bead, x=maternal_counts$CAST)
rownames(maternal_counts_matrix) <- levels(maternal_counts$gene)
colnames(maternal_counts_matrix) <- levels(maternal_counts$bead)
paternal_counts <- counts1 |>
bind_rows(counts2) |>
arrange(bead, gene) |>
mutate(bead = as.factor(bead), gene = as.factor(gene))
paternal_counts_matrix <- sparseMatrix(i=paternal_counts$gene, j=paternal_counts$bead, x=paternal_counts$`129`)
rownames(paternal_counts_matrix) <- levels(paternal_counts$gene)
colnames(paternal_counts_matrix) <- levels(paternal_counts$bead)
positions <- positions1 |> bind_rows(positions2) |>
select(bead,x,y) |>
column_to_rownames('bead')
counts <- rbind(counts1, counts2)
# First run RCTD
puck <- SpatialRNA(positions, counts_matrix, maternalCounts = maternal_counts_matrix, paternalCounts = paternal_counts_matrix)
mycere <- create.RCTD(puck, cerebellum_ref, max_cores=8)
mycere <- run.RCTD(mycere, doublet_mode = 'doublet')
saveRDS(mycere, file = 'results/rctd_cere_3.rds')
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.