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

Unexpected error when spe$sample_id is a factor #151

Open
Nick-Eagles opened this issue May 29, 2024 · 1 comment
Open

Unexpected error when spe$sample_id is a factor #151

Nick-Eagles opened this issue May 29, 2024 · 1 comment

Comments

@Nick-Eagles
Copy link
Contributor

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

Nick-Eagles added a commit to LieberInstitute/visiumStitched that referenced this issue May 29, 2024
@drighelli
Copy link
Owner

drighelli commented Jun 19, 2024

Thanks Nick (@Nick-Eagles) for reporting this error, maybe Helena (@HelenaLC) can take a look into this or provide any feedback for possible solutions or violating constraints in case of a sample_id factor.

HelenaLC pushed a commit that referenced this issue Jun 20, 2024
HelenaLC pushed a commit that referenced this issue Jun 20, 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

2 participants