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

Reduce time for single cell markers visualization #93

Open
lpantano opened this issue Mar 8, 2019 · 1 comment
Open

Reduce time for single cell markers visualization #93

lpantano opened this issue Mar 8, 2019 · 1 comment
Labels
category-research questions about analysis, how to do something in this particular case docs issues related to knowledgebase docs subcategory-scrnaseq single cell RNAseq pipeline

Comments

@lpantano
Copy link
Contributor

lpantano commented Mar 8, 2019

I have some code here to visualize a reduced version of the tSNE.

@marypiper, I don't use directly Seurat functions to plot, so I don't know how easy is to add that information to the tutorial, but here is the code:

reduce <- function(X, Y, resolution=30){
    # From iSEE bioconductor tool
    resolution <- max(resolution, 1L)
    resolution <- min(resolution, sqrt(.Machine$integer.max))
    resolution <- as.integer(resolution)
    
    # X and Y MUST be numeric.
    rangeX <- range(X)
    rangeY <- range(Y)
    
    binX <- (rangeX[2] - rangeX[1])/resolution
    xid <- (X - rangeX[1])/binX
    xid <- as.integer(xid)
    
    binY <- (rangeY[2] - rangeY[1])/resolution
    yid <- (Y - rangeY[1])/binY
    yid <- as.integer(yid)
    
    # Getting unique IDs, provided resolution^2 < .Machine$integer.max
    # We use fromLast=TRUE as the last points get plotted on top.
    id <- xid + yid * resolution 
    return(id)
}

# get data from Seurat of a list of genes
gene_data = FetchData(seurat, vars.all = c("tSNE_1", "tSNE_2",
                                           "condition", "ident",
                                           list_gene_name)) %>% 
    mutate(id_group=reduce(tSNE_1, tSNE_2)) %>% 
    gather(gene_id, counts, -tSNE_1,-tSNE_2,-condition,-ident, -id_group) %>%
    left_join(gene_mapping, by = "gene_id") # to get nice gene names

# plot all of this genes with ggplot summarizing the points first
group_by(gene_data,
             gene_id, ident, id_group, gene_name) %>% 
    summarise(tSNE_1=mean(tSNE_1), tSNE_2=mean(tSNE_2),
              value=median(counts)) %>% 
    ggplot(aes(tSNE_1, tSNE_2)) +
    geom_point(aes(color=value), alpha=0.8) +
    scale_color_gradient2(guide = FALSE, midpoint = 0,
                          mid = "grey90",
                          high = "#2c7fb8") +
    geom_text(data=tsne_label, aes(label=ident, x, y)) +
    # use gene_name if you mapped to nice gene names in the previous chunk of code
    facet_wrap(~gene_id) 

@channel, this is the code to plot tSNE with less points.

@lpantano lpantano added docs issues related to knowledgebase docs category-research questions about analysis, how to do something in this particular case subcategory-scrnaseq single cell RNAseq pipeline labels Mar 8, 2019
@roryk
Copy link
Collaborator

roryk commented Mar 8, 2019

Awesome!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
category-research questions about analysis, how to do something in this particular case docs issues related to knowledgebase docs subcategory-scrnaseq single cell RNAseq pipeline
Projects
None yet
Development

No branches or pull requests

2 participants