marzamKI / spatial_atac

Code for the analysis of spatial ATAC-seq data
MIT License
4 stars 3 forks source link

image files for mouse embryos #2

Closed dhoneyi closed 1 year ago

dhoneyi commented 1 year ago

Hi Margherita, Thanks for your great work and sharing the data and code.I am using your mouse embryos spatial-ATAC data in mu project. While I have some problems in adding the image files and position files in my Seurat object. I have seen that you using 10x cellranger count to process the spatial-ATAC, I wonder know do you have tied to use space ranger count to process? Or do you have the scalefactors_json.json file or do you know how to generate the files? Which may help us add the spatial information to the object easier, thanks!

All the best, Danni

marzamKI commented 1 year ago

Dear Danni,

Thank you for reaching out and for your interest in our data. Indeed it's not straightforward to add the spatial information to the object. This is what we did in brief: We treated the data as if they were scATAC-seq data and mapped them with Cellranger-ATAC, so we disregarded the spatial information at this stage (we couldn't use Spatial ranger because it doesn't support our type of data). The spatial information was, thus, added manually afterwards: we loaded the images in Loupe and got the coordinates in a tsv file (which is the one you can find in our meta directory). Then we calculated the scale factors and used this together with coordinates and images to create an STutility object. This is a Seurat object that contains the spatial data under object@tools[["Staffli"]]. Because this tools is also developed for spatial transcriptomics (and not spatial ATAC), we had to add the chromatin accessibility data manually. For this, the easier way was to create two objects, a Signac object built from Cellranger-ATAC output and a STutility object containing the spatial info (i.e., Staffli). We finally just copied the Staffli info over to the Signac object.

I hope this explanation makes the code more clear. You find all the code needed at: https://github.com/marzamKI/spatial_atac/blob/main/code/embryo_createobj.R. In particular:

  1. Create coordinate file compatible with STutility from the tissue.tsv files uploaded in meta.

    
    # make tissue position files
    for(i in seq_along(dirs)){
    spotfile <- read.table(infoTable$tissue_paths[i], sep = "\t", header = T)
    
    spotfile_tissue <- cbind(
    spotfile$row, #array_row
    spotfile$col, #array_col
    spotfile$x, #pxl_row_in_fullres
    spotfile$y #pxl_col_in_fullres
    ) %>% as.data.frame()
    tissue <- rep(1, nrow(spotfile_tissue))
    spotfile_tissue <- cbind(tissue, spotfile_tissue)
    rownames(spotfile_tissue) <- paste0(spotfile$barcode, "-1")
    
    # save new tissue position file with the correct columns for creating STutility object
    write.csv(spotfile_tissue, 
            paste0(strsplit(infoTable$tissue_paths[i], "tsv"), "csv"))

}


2. Load data, tissue coordinates, and images to create ST object

add image

image is manually cropped so that it only shows the capture area

table <- list() for(i in dirs){ table[[i]] <- c(samples = paste0("data/", i, "/outs/encode_peak_bc_matrix_gr12_13_15.h5"), spotfiles = paste0("meta/", i, "_tissue.csv"), imgs = paste0("images/", i, "_cropped.jpg")) }

infoTable <- do.call("rbind", table) %>% as.data.frame()


3. Calculate scale factors

for (i in 1:length(dirs)) {

Read *_tissue.csv file

xy.raw <- setNames(read.csv(file = infoTable$spotfiles[i]), nm = c("barcode", "tissue", "y", "x", "pixel_y", "pixel_x")) xy <- xy.raw[, c("x", "y")]

img_path <- infoTable$imgs[i] img <- readJPEG(img_path)

sf <- c(ncol(img)/128, nrow(img)/78) xy$x <- xy$xsf[1] xy$y <- xy$ysf[2]

Create a new spot selection table with proper image pixel coordinates which match the cropped images

spotfile <- data.frame(xy.raw$barcode, xy.raw$tissue, xy.raw$y, xy.raw$x, round(xy$y), round(xy$x)) write.table(spotfile, file = paste0(strsplit(infoTable$spotfiles[i], ".csv"), "_positions_list.csv"), sep = ",", quote = F, row.names = F, col.names = F) }


4. Create ST object

infoTable$spotfiles <- paste0(strsplit(infoTable$spotfiles, ".csv"), "_positions_list.csv") se <- InputFromTable(infoTable, scaleVisium = 1) se <- LoadImages(se, time.resolve = F)


5. Copy spatial info over to the Signac object

combined@tools[["Staffli"]] <- se@tools$Staffli



Let me know if you have any more issues or question!