Skip to content

Reduce time for single cell markers visualization #93

@lpantano

Description

@lpantano

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    category-researchquestions about analysis, how to do something in this particular casedocsissues related to knowledgebase docssubcategory-scrnaseqsingle cell RNAseq pipeline

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions