farrellja / URD

URD - Reconstruction of Branching Developmental Trajectories
GNU General Public License v3.0
117 stars 41 forks source link

scRNA profiles from multiple time points; Seurat to URD; clustering in Seurat #29

Open stephenchea opened 5 years ago

stephenchea commented 5 years ago

I have scRNA profiles of whole mouse embryos at multiple time points. I would like to cluster them using Seurat and then feed cluster identities into URD. Is this possible? If it is possible, where in the URD tutorial should I start after loading the data? And should I cluster cells from each timepoint separately or all together from all the time points at one time?

farrellja commented 5 years ago

There is a function seuratToURD that should work with Seurat v2. I have not yet updated it for Seurat v3, and I believe the structure of the object changed there. You would then need to do all steps beginning with Calculate Diffusion Map in the QuickStart tutorial. The method that URD uses to calculate variable genes is different from Seurat, so you may also want to explore URD's variable gene results and compare them to those from Seurat. Additionally, if you didn't calculate a tSNE representation in Seurat, you may want to calculate one in URD just for visualization and exploration. Clustering wise, I generally cluster at each stage individually, though you could try exploring both methods.

chiblyaa commented 5 years ago

Hi Stephen,

I updated Jeffrey's SeuratToURD function to import the Seurat (v3) object into URD. I'm just testing it now and haven't encountered any issues so far. You'll have to create a custom function - in this case I named it seuratToURD2 - and then use that one to import your seurat object. Here's the code:

seuratToURD2 <- function(seurat.object) {
  if (requireNamespace("Seurat", quietly = TRUE)) {
    # Create an empty URD object
    ds <- new("URD")

    # Copy over data
    ds@logupx.data <- as(as.matrix(seurat.object@assays$RNA@data), "dgCMatrix")
    if(!any(dim(seurat.object@assays$RNA@counts) == 0)) ds@count.data <- as(as.matrix(seurat.object@assays$RNA@counts[rownames(seurat.object@assays$RNA@data), colnames(seurat.object@assays$RNA@data)]), "dgCMatrix")

    # Copy over metadata
    ## TO DO - grab kmeans clustering info
    get.data <- NULL
    if (.hasSlot(seurat.object, "data.info")) { 
      get.data <- as.data.frame(seurat.object@assays$RNA@data.info)
    } else if (.hasSlot(seurat.object, "meta.data")) { 
      get.data <- as.data.frame(seurat.object@meta.data) 
    }
    if(!is.null(get.data)) {
      di <- colnames(get.data)
      m <- grep("res|cluster|Res|Cluster", di, value=T, invert = T) # Put as metadata if it's not the result of a clustering.
      discrete <- apply(get.data, 2, function(x) length(unique(x)) / length(x))
      gi <- di[which(discrete <= 0.015)]
      ds@meta <- get.data[,m,drop=F]
      ds@group.ids <- get.data[,gi,drop=F]
    }

    # Copy over var.genes
    if(length(seurat.object@assays$RNA@var.features > 0)) ds@var.genes <- seurat.object@assays$RNA@var.features

    # Move over tSNE projection
    if (.hasSlot(seurat.object, "tsne.rot")) {
      if(!any(dim(seurat.object@tsne.rot) == 0)) {
        ds@tsne.y <- as.data.frame(seurat.object@tsne.rot)
        colnames(ds@tsne.y) <- c("tSNE1", "tSNE2")
      }
    } else if (.hasSlot(seurat.object, "reductions")) {
      if(("tsne" %in% names(seurat.object@reductions)) && !any(dim(seurat.object@reductions$tsne) == 0)) {
        ds@tsne.y <- as.data.frame(seurat.object@reductions$tsne@cell.embeddings)
        colnames(ds@tsne.y) <- c("tSNE1", "tSNE2")
      }
    }

    # Move over PCA results
    if (.hasSlot(seurat.object, "pca.x")) {
      if(!any(dim(seurat.object@pca.x) == 0)) {
        ds@pca.load <- seurat.object@pca.x
        ds@pca.scores <- seurat.object@pca.rot
        warning("Need to set which PCs are significant in @pca.sig")
      }
      ## TO DO: Convert SVD to sdev
    } else if (.hasSlot(seurat.object, "reductions")) {
      if(("pca" %in% names(seurat.object@reductions)) && !any(dim(Loadings(seurat.object, reduction = "pca")) == 0)) {
        ds@pca.load <- as.data.frame(Loadings(seurat.object, reduction = "pca"))
        ds@pca.scores <- as.data.frame(seurat.object@reductions$pca@cell.embeddings)
        ds@pca.sdev <- seurat.object@reductions$pca@stdev
        ds@pca.sig <- pcaMarchenkoPastur(M=dim(ds@pca.scores)[1], N=dim(ds@pca.load)[1], pca.sdev=ds@pca.sdev)
      }
    }
    return(ds)
  } else {
    stop("Package Seurat is required for this function. To install: install.packages('Seurat')\n")
  }
}
farrellja commented 5 years ago

Thanks, Augustin! I will incorporate this into the code base soon & add an acknowledgment to you in the documentation & homepage.

CLambert7 commented 4 years ago

Thanks Stephen - really helpful!