Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bug (and fix) in LogSeuratCommand when called by Seurat IntegrateData #215

Open
jernest1 opened this issue Jul 25, 2024 · 0 comments
Open

Comments

@jernest1
Copy link

When I run Seurat IntegrateData(), the commands slot of the resulting Seurat object contains withCallingHandlers rather than IntegrateData.

require(Seurat)
require(SeuratData)
require(glmGamPoi)

seurat_obj <- LoadData("panc8")
seurat_split_list <- SplitObject(object = seurat_obj, split.by = "tech")
seurat_split_list <- lapply(X = seurat_split_list, FUN = SCTransform, method = "glmGamPoi")

integ_features <- SelectIntegrationFeatures(
  object.list = seurat_split_list,
  nfeatures = 3000
)

seurat_split_list <- PrepSCTIntegration(
  object.list = seurat_split_list,
  anchor.features = integ_features
)

seurat_split_list <- lapply(X = seurat_split_list, FUN = RunPCA, features = integ_features)

integ_anchors <- FindIntegrationAnchors(
  object.list = seurat_split_list,
  reduction = "rpca",
  normalization.method = "SCT",
  dims = 1:30,
  k.anchor = 5, # default = 5, increase to strengthen alignment
  anchor.features = integ_features
)

seurat_integrated <- IntegrateData(
  anchorset = integ_anchors,
  normalization.method = "SCT",
  dims = 1:30
)

names(seurat_integrated@commands)
# [1] "FindIntegrationAnchors" "withCallingHandlers"  

I traced it to an issue in LogSeuratCommand with parsing sys.calls(), line 12 below.

  time.stamp <- Sys.time()
  object <- UpdateSlots(object = object)
  #capture function name
  which.frame <- sys.nframe() - 1
  if (which.frame < 1) {
    stop("'LogSeuratCommand' cannot be called at the top level", call. = FALSE)
  }
  if (as.character(x = sys.calls()[[1]])[1] == "do.call") {
    call.string <- deparse(expr = sys.calls()[[1]])
    command.name <- as.character(x = sys.calls()[[1]])[2]
  } else {
    command.name <- as.character(x = deparse(expr = sys.calls()[[which.frame]])) #THE ISSUE IS HERE
    command.name <- gsub(
      pattern = "\\.Seurat",
      replacement = "",
      x = command.name
    )
    call.string <- command.name
    command.name <- ExtractField(
      string = command.name,
      field = 1,
      delim = "\\("
    )
  }

If I add a second exception for handling IntegrateData, it works normally and returns command information for IntegrateData as I would expect.

  time.stamp <- Sys.time()
  object <- UpdateSlots(object = object)
  #capture function name
  which.frame <- sys.nframe() - 1
  if (which.frame < 1) {
    stop("'LogSeuratCommand' cannot be called at the top level", call. = FALSE)
  }
  if (as.character(x = sys.calls()[[1]])[1] == "do.call") {
    call.string <- deparse(expr = sys.calls()[[1]])
    command.name <- as.character(x = sys.calls()[[1]])[2]
  } else if(as.character(x = sys.calls()[[1]])[1] == "IntegrateData") {
    call.string <- deparse(expr = sys.calls()[[1]])
    command.name <- as.character(x = sys.calls()[[1]])[1]
  } else {
    command.name <- as.character(x = deparse(expr = sys.calls()[[which.frame]]))
    command.name <- gsub(
      pattern = "\\.Seurat",
      replacement = "",
      x = command.name
    )
    call.string <- command.name
    command.name <- ExtractField(
      string = command.name,
      field = 1,
      delim = "\\("
    )
  }

Not sure if that's the most stable or robust solution, but would it be possible to look into the issue and fix it? Happy to submit a PR if you'd like. I'm using the latest versions of Seurat (5.1.0) and SeuratObject (5.0.2). I also saw the issue when I went back to versions 5.0.1 for both packages, and using the standard NormalizeData/ScaleData/FindVariableFeatures approach instead of SCTransform.

@jernest1 jernest1 changed the title Bug in LogSeuratCommand when called by Seurat IntegrateData Bug (and fix) in LogSeuratCommand when called by Seurat IntegrateData Aug 2, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant