Name seurat function in r with name of each experiment/variable

Viewed 59

I am using seurat to analyze some scRNAseq data, I have managed to put all the SCT integration one line codes from satijalab into a function with basically

SCT_normalization <- function (f1, f2) { 
      f_merge <- merge (f1, y=f2) 
      f.list <- SplitObject(f_merge, split.by = "stim")
      f.list <- lapply(X = f.list, FUN = SCTransform)
      features <- SelectIntegrationFeatures(object.list = f.list, nfeatures = 3000)
      f.list <<- PrepSCTIntegration(object.list = f.list, anchor.features = features)
 return (f.list)
}

so that I will have f.list in the global environment for downstream analysis and making plots. The problem I am running into is that, every time I run the function, the output would be f.list, I want it to be specific to the input value name (i.e., f1 and/or f2). Basically something that I can set so that I would know which input value was used to generate the final output. I saw something using the assign function but someone wrote a warning about "the evil and wrong..." so I am not sure as to how to approach this.

1 Answers

From what it sounds like you don't need to use the super assign function <<-. In my opinion, I don't think <<- should be used as it can cause unexpected changes in objects. This is what I assume the other person was saying. For example, if you have the following function:

AverageVector <- function(v) x <<- mean(v, rm.na = TRUE)

Now you're trying to find the average of a vector you have, along with more analysis

library(tidyverse)
x <- unique(iris$Species)
avg_sl <- AverageVector(iris$Sepal.Length)

Now where x used to be a character vector, it's not a numeric vector with a length of 1.

So I would remove the <<- and call your function like this

object_list_1_2 <- SCT_normalize(object1, object2)

If you wanted a slightly more programatic way you could do something like this to keep track of objects you could do something like this:

SCT_normalization <- function(f1, f2) { 
      f_merge <- merge (f1, y = f2) 
      f.list <- SplitObject(f_merge, split.by = "stim")
      f.list <- lapply(X = f.list, FUN = SCTransform)
      features <- SelectIntegrationFeatures(object.list = f.list, nfeatures = 3000)
      f.list <- PrepSCTIntegration(object.list = f.list, anchor.features = features)
      to_return <- list(inputs = list(f1, f2), normalized = f.list)
      return(to_return)
    }
Related