R/prepare_data.R

setGeneric(".prepare_data_feature", function (sce, 
    mod="RNA",                                 
    type,
    feature) standardGeneric(".prepare_data_feature"))


setMethod(".prepare_data_feature", "SingleCellExperiment", function (sce,
    mod="RNA",                                                           
    type,
    feature) {
  
    if(mod=="RNA"){
  
        ind <- match(feature, rownames(sce))

        if (is.na(ind)) {
            stop("Gene cannot be found.")
        }

        x <- assays(sce)
  
        if(!type %in% names(x)){
            stop("Specify a valid assay type.")
        }
  
        x <- as.numeric(x[[which(names(x)==type)]][ind,])
        
    } else {
      
        if(!mod %in% altExpNames(sce)){
            stop("Specify a valid modularity.")
        }      
      
        if(!type %in% assayNames(altExp(sce))){
            stop("Specify a valid assay type.")
        }
      
        x <- assays(altExp(sce, mod))
        x <- x[[which(names(x)==type)]]
      
        ind <- match(feature, rownames(x))
      
        if (is.na(ind)) {
            stop("Feature cannot be found.")
        }
      
        x <- as.numeric(x[ind,])
    
    }
  
    return(x)
}) 

setMethod(".prepare_data_feature", "Seurat", function (sce,
    mod="RNA",                                                           
    type,
    feature) {
  
    if(mod=="RNA"){
      
        x <- GetAssayData(sce, type)    
      
        ind <- match(feature, rownames(x))
        
        if (is.na(ind)) {
            stop("Gene cannot be found.")
        }

        x <- as.numeric(x[ind,])

  } else{
    
    if(!mod %in% names(sce)){
        stop("Specify a valid modularity.")
    }
    
    if(!type %in% slotNames(GetAssay(sce, mod))){
        stop("Specify a valid assay type.")
    }
    
    x <- GetAssayData(sce, assay=mod, type)
    
    ind <- match(feature, rownames(x))
    
    if (is.na(ind)) {
        stop("Feature cannot be found.")
    }
    
    x <- as.numeric(x[ind,])
    
  }
  
  return(x)
})  

setGeneric(".prepare_data_meta", function (sce, 
    col) standardGeneric(".prepare_data_meta"))


setMethod(".prepare_data_meta", "SingleCellExperiment", function (sce,
    col) {

    if (any(!col %in% colnames(colData(sce)))) {
        stop("Column cannot be found in colData(sce).")
    }
  
    name_s <- paste0("sce$", col)
    func <- paste0("x <- ", name_s)
  
    eval(parse(text = func))
  
    return(x)
  
}) 

setMethod(".prepare_data_meta", "Seurat", function (sce,
    col) {
  
    if (any(!col %in% colnames(sce@meta.data))) {
        stop("Column cannot be found in slot(sce, 'meta.data').")
    }
  
    name_s <- paste0("sce$", col)
    func <- paste0("x <- ", name_s)
  
    eval(parse(text = func))
    
    return(x)

})  

  

Try the schex package in your browser

Any scripts or data that you put into this service are public.

schex documentation built on Nov. 8, 2020, 5:56 p.m.