Giter Club home page Giter Club logo

Comments (5)

enblacar avatar enblacar commented on June 8, 2024 1

Hi @fatimasnc,

That explains it!

It is normal that if there are no cells to plot, as the factor level is empty, you get that error. By how SCpubr::do_DimPlot() is designed, you can not plot empty plots. The plots have several layers and at least one cell per level is required for it to work (because when you provide split.by it checks whether it is a factor or a character metadata column. If it is a factor, it will use the levels, and then plot the cells associated to the levels, hence your error when it cannot find them).

I would suggest, if you still want to keep the empty factor level, to use idents.keep and manually provide the values that have cells associated to them:

# Bug: Using factor metadata variables with empty levels (with no associated cells).

cols_black_22 <- rep("black", times = 22) # Hardcoded lenght might lead to errors later on
names(cols_black_22) <- levels(seurat$Idents) # Empty factor levels in seurat$Idents trigger the error message.
SCpubr::do_DimPlot(seurat, 
                   split.by = "Idents",  
                   legend.position = "none", 
                   font.size = 24, 
                   colors.use = cols_black_22)


# Fix: Retrieving the factor levels with cells.

library("rlang")
library("magrittr")

# Get the levels that actually have at least one cell.
levels.use <- table(seurat$Idents) %>% # Get the number of cells associated to each factor level.
              data.frame() %>% # Turn it into a df (with default column names "Freq" and "Var1").
              dplyr::filter(.data$Freq > 0) %>% # Filter empty factor levels.
              dplyr::pull(.data$Var1) %>% # Retrieve the factor levels.
              as.character() # Turn it into a character vector.

# Generate color palette.
cols_black_22 <- rep("black", times = length(levels.use))
names (cols_black_22) <- levels.use

# Plot using idents.keep to bypass the empty factor level.
SCpubr::do_DimPlot(seurat, 
                   split.by = "Idents",  
                   legend.position = "none", 
                   font.size = 24, 
                   colors.use = cols_black_22, 
                   idents.keep = levels.use)

I will close this Issue for now. I will give it a go on how to either point out this issue when the error is triggered or code a workaround for it with a warning stating that levels were dropped.

Thanks for the insights! Feel free to reopen the Issue if the fix did not work for you (it worked for me after reproducing the error in my sample).

Happy plotting!
Enrique

from scpubr.

enblacar avatar enblacar commented on June 8, 2024

Hi @fatimasnc,

Thanks for using my package!

First of all, which version of SCpubr are you using? I am quite sure the piece of code that generates the error in SCpubr::do_DimPlot(), that makes use of colors.use[[iteration]] is no longer present after version 1.1.0.

You can check the version like this:

utils::packageVersion("SCpubr")

If it says that is higher or equal to 1.1.0, keep reading, if not please update and try again!


If it is higher, it is really weird that you get such behavior, as I was able to compute the plot with the same code as you (with minor tweaks to adjust it to my test Seurat object):

# Reproduce bug.
seurat <- sample # My seurat object.
Seurat::Idents(seurat) <- seurat$seurat_clusters # Set the idents to the clusters.
seurat$Idents <- Seurat::Idents(seurat) # Generate a metadata column "Idents" with the identitites.

times <- length(unique(seurat$Idents)) # times = 10
cols_black_22 <- rep("black", times = times) # Vector of "black" 10 times
names(cols_black_22) <- levels(seurat$Idents) # Named vector where all colors are black.
SCpubr::do_DimPlot(seurat, 
                   split.by = "Idents",  
                   legend.position = "none", 
                   font.size = 24, 
                   colors.use = cols_black_22)

This neatly outputs the following plot, which goes on the lines of what I guess you want to plot:
24125607-d70b-4934-9110-6c38b2dd2134

So, ruling out a problem in SCpubr's code, I would guess the problem lies in your Seurat object and metadata Idents column. Can you double-check again this?

message(length(cols_black_22))
message(length(levels(seurat$Idents)))
message(length(unique(seurat$Idents)))

They should all have the same values (in my case, 10). If not, that should point out the problem. You can also send me the metadata column Idents by mail to [email protected] if it does not work, so that I can have a look at it!

However, if what you want is to just color everything as black, you can simply use this:

SCpubr::do_DimPlot(seurat, 
                   split.by = "Idents",  
                   legend.position = "none", 
                   font.size = 24, 
                   colors.use = "black")

Let me know how it goes!

Best,
Enrique

from scpubr.

fatimasnc avatar fatimasnc commented on June 8, 2024

Hi @enblacar! Thanks so much for your quick reply.
I have upgraded to version 1.1.2.9000 (I was using 1.0.4) and run it again, then got the error Error: No cells found

I have checked:

> message(length(cols_black_22))
22
> message(length(levels(Neu$Morphogen)))
22
> message(length(unique(Neu$Morphogen)))
21

and investigating it a bit it turns out that I have 22 different Idents but one of them has no cells in it. However, I would still like to plot it and I definitely wouldn't like to completely remove the level from my data.

I have also tried your second suggestion and it also returns Error: No cells found. Is there any workaround for this?

Thank you for your patience!
Fátima

from scpubr.

fatimasnc avatar fatimasnc commented on June 8, 2024

Hi @enblacar!

First of all thank you for the code above, it managed to fix my errors :)
I am reopening this as I'm getting a related error now while trying to use the argument idents.keep together with colors.use. Here's a bit of the code:

# These are levels of my metadata column (dose) that I want to extract from all the levels of that column
levels.use <- c("A", "B", "C", "D", "E")

# I want to use the palette my_colors for the kept identities
my_colors <- c("#FFDDAD", "#FFC370", "#FF990A", "#FE7913", "#C66611")
names (my_colors) <- levels.use

Idents (seurat) <- "dose"
SCpubr::do_DimPlot (seurat, idents.keep = levels.use, sizes.highlight = 5, group.by = "dose", pt.size = 1.5, font.size = 12, raster = FALSE, order = levels.use, colors.use = my_colors)

> Error: The number of provided colors is lower than the unique values in the selected grouping variable (levels(object), group.by or split.by).

The original number of identities in my Seurat object is 208, but I want to keep those 5 only and give them my custom colors, which is also a vector of length 5. Since I get this error, I wonder if the color vector we need to pass to the function is actually for all the original identities or only for the ones that we select with idents.keep. And also order = levels.use is not working for me here :(

Thanks a lot for your help!
Cheers,
Fátima

from scpubr.

enblacar avatar enblacar commented on June 8, 2024

Hi @fatimasnc!

Happy to hear that it worked for you!

Nice catch on that bug. I will fix it for the next update. Right now, I am afraid you will have to provide the color vector you will normally use with the 208 identities (they will not be used in the plot, but it is still required by the function). But it makes more sense to only provide the colors for the identities to keep, so I will change it accordingly.

For the order, either you provide a single identity you want to be on top of all, or you provide a vector with all identities in the order you want them to be plotted. However, I am of the opinion of letting cells be randomly plotted with shuffle = TRUE, which is the default parameter.

Best,
Enrique

from scpubr.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.