Skip to content

Unexpected error when spe$sample_id is a factor #151

@Nick-Eagles

Description

@Nick-Eagles

Hello,

I constructed a SpatialExperiment where spe$sample_id was a factor, which did not throw errors, and felt intuitive, given that there were a lot of columns (spots) in my data with only a handful of possible sample IDs. However, this causes problems in various internal functions, where sample ID is assumed to be character (at least this code). This leads to difficult-to-understand errors when performing normal operations on the SpatialExperiment. Perhaps either factors should be supported or errors should be thrown when attempting to set spe$sample_id to a factor.

Here's a reproducible example creating the error:

n <- length(z <- letters)
y <- list(counts = matrix(0, nrow = n, ncol = n))
cd <- DataFrame(a = seq(n), x = seq(n), y = seq(n), z)

spe1 <- SpatialExperiment(
    assay = y, 
    colData = cd, 
    spatialCoordsNames = c("x", "y")
)

url <- "https://i.redd.it/3pw5uah7xo041.jpg"
spe1 <- addImg(
    spe1, 
    sample_id = "sample01", 
    image_id = "pomeranian",
    imageSource = url, 
    scaleFactor = NA_real_, 
    load = TRUE
)

#   Works fine, because spe1$sample_id is a character vector
getImg(spe1, sample_id = spe1$sample_id[1], image_id = "pomeranian")

#   Fails with difficult-to-understand error when spe1$sample_id is a factor
#   of characters
spe1$sample_id = factor(spe1$sample_id)
getImg(spe1, sample_id = spe1$sample_id[1], image_id = "pomeranian")

The error message is Error in .get_img_idx(x, sample_id, image_id) : object 'sid' not found, again because the if statement here doesn't handle factors, leading to sid being undefined here.

I actually first encountered the error when trying to use spatialLIBD::vis_gene() ( I understand that getImg() is not going to be frequently called directly-- it just provokes the error most directly).

Best,
-Nick

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions