R/pointmut.R

Defines functions sptBClstRds

Documented in sptBClstRds

#' Visualize Somatic Alterations of fresh frozen Spatial Transcriptomics Data
#'
#' @param files a vector of spotIndex file, each barcode one txt file
#' @param data1 filtered_feature_bc.csv generated by spaceranger
#' @param data2 Graph-Based csv file exported from 10X Loupe Browser when open Loupe.Loupe
#' @param path Path to the tissue_positions_list.csv under spatial/ folder
#'
#' @importFrom stringr str_replace str_split
#' @importFrom dplyr filter %>%
#' @importFrom utils read.csv read.table
#'
#' @return A list of two data frame
#' @export
#'
#' @examples \dontrun{
#' files <- list.files(path = system.file("extdata/spotIndex",package = "spamut"),
#'     pattern = ".txt", full.names = TRUE, recursive = FALSE)
#' data1 <- read.csv(system.file("extdata/","filtered_feature_bc.csv",
#'     package = "spamut"), header = TRUE)
#' data2 <- read.csv(system.file("extdata/","Graph-Based.csv", package = "spamut"), header = TRUE)
#' path <- system.file("extdata/","tissue_positions_list.csv", package = "spamut"
#' df <- sptBClstRds(files=files,data1=data1, data2=data2, path=path)
#' }
sptBClstRds <- function(files,data1, data2, path){
  stopifnot(is.character(files), is.data.frame(data1), is.data.frame(data2), is.character(path))

  barcode <- NULL # this code to avoid the note "no visible binding for global variable ‘barcode’"
  nrow <- length(colnames(data1)[-1])

  # Count total reads of each spot
  data1 <- data1[,-1]
  df0 <- data.frame(colnames(data1),colSums(data1)) # spotBcode and total reads of each spot
  colnames(df0) <- c("barcode", "TotalRDs")
  df0$barcode <- str_replace(df0$barcode,".1","-1")
  df1 <- data.frame(matrix(nrow = nrow, ncol = 2))
  lapply(files, function(x){
    tab <- read.table(x, sep = "\t", header = FALSE)
    spts <- str_split(x,"/", simplify = TRUE)
    spts <- str_split(x,"/", simplify = TRUE)[length(spts)]
    sptn <- substr(spts,1,(nchar(spts)-4))
    i <- as.numeric(substr(sptn,5,nchar(spts))) + 1
    df1[i,1] <<- tab[1,1]
    df1[i,2] <<- sptn
  })
  colnames(df1) <- c("barcode","spot")
  df1 <- merge(df1, df0, by = "barcode")

  # generate barcode-spotName-cluster dataframe
  colnames(data2) <- c("barcode","cluster")
  df2 <- merge(data2,df1, by = "barcode")

  # add spot coordinates
  spotloc <- read.csv(path, header = FALSE)
  colnames(spotloc) <- c("barcode","in_tissue","array_row","array_col","pxl_row_in_fullres","pxl_col_in_fullres")
  spotloc <- spotloc[,c(1,3,4)]
  spotloc <- spotloc %>% filter(barcode %in% df2$barcode)
  df2 <- merge(df2, spotloc, by = "barcode")
  returnList <- list(df1, df2)

  return(returnList)
}
limin321/spamut documentation built on May 13, 2022, 12:50 a.m.