Download inputs/scRNAseq_preprocessing/hESC.Rmd from RoyalConceptEnterprise/webgenie: direct link, hf CLI and curl.
- Browser
- Download file 3.83 kB
-
https://huggingface.co/RoyalConceptEnterprise/webgenie/resolve/main/inputs/scRNAseq_preprocessing/hESC.Rmd
- Command line
-
hf download hf://RoyalConceptEnterprise/webgenie/inputs/scRNAseq_preprocessing/hESC.Rmd
-
curl -L -o hESC.Rmd https://huggingface.co/RoyalConceptEnterprise/webgenie/resolve/main/inputs/scRNAseq_preprocessing/hESC.Rmd
3.83 kB
| --- | |
| title: "Preprocessing script for Chu 2017" | |
| author: "Aditya Pratapa" | |
| date: "`r Sys.Date()`" | |
| output: | |
| BiocStyle::html_document: | |
| toc: true | |
| vignette: > | |
| --- | |
| Load necesdsary libraries | |
| ```{r warning=FALSE,message=FALSE} | |
| library(destiny) | |
| library(slingshot) | |
| library(plotly) | |
| library(gam) | |
| library(RColorBrewer) | |
| library(EBSeq) | |
| library(Rtsne) | |
| ``` | |
| Read input files. The input file is from https://www.ncbi.nlm.nih.gov/geo/query/acc.cgi?acc=GSE75748 | |
| ```{r warning=FALSE} | |
| Chu2017 <- read.table('GSE75748_sc_time_course_ec.csv', sep = ',', header = T, row.names = 1) | |
| ChuCellTypes <- data.frame() | |
| for (col in colnames(Chu2017)){ | |
| cType <- strsplit(strsplit(col,'[.]')[[1]][2],'b4s_|_')[[1]][1] | |
| ChuCellTypes[col,'Time'] <- cType | |
| } | |
| head(Chu2017) | |
| head(ChuCellTypes) | |
| ``` | |
| Compute diffusion map projection of the cells | |
| ```{r warning=FALSE, fig.height=10} | |
| # The scRNA-seq data were normalized by median-by-ratio normalization | |
| Sizes <- MedianNorm(Chu2017) | |
| if(is.na(Sizes[1])){ | |
| Sizes <- MedianNorm(Data, alternative=TRUE) | |
| message("alternative normalization method is applied") | |
| } | |
| Chu2017MedNorm <- GetNormalizedMat(Chu2017,Sizes) | |
| logExpression <- log2(Chu2017MedNorm+1) | |
| geneFiltered <- apply(Chu2017,1,function(x){ | |
| sum(x >= 1) >= 1 | |
| }) | |
| logExpressionFiltered <- logExpression[which(geneFiltered),] | |
| ``` | |
| ```{r} | |
| # Compute PCA to identify informative genes | |
| pcaRes <- prcomp(t(logExpressionFiltered), scale. = FALSE) | |
| dmapRes <- DiffusionMap(t(logExpressionFiltered), distance = "cosine", sigma =0.25, k = 100 ) | |
| ``` | |
| ```{r} | |
| rd1 <- as.data.frame(cbind(PC1 = pcaRes$x[,1], PC2 = pcaRes$x[,2], PC3 = pcaRes$x[,3])) | |
| rd2 <- as.data.frame(cbind(DC1 = dmapRes$DC1, DC2 = dmapRes$DC2)) | |
| plot_ly(as.data.frame(pcaRes$x), x=~PC1, y= ~PC2, color = ChuCellTypes$Time, colors = brewer.pal(6,"Set1")) | |
| plot_ly(rd2, x=~DC1, y= ~DC2, color = ChuCellTypes$Time, colors =brewer.pal(6,"Set1")) | |
| ``` | |
| Run slingshot | |
| ```{r} | |
| slingshotPT <- slingshot(rd2, reducedDim = rd2, | |
| clusterLabels = ChuCellTypes$Time, start.clus = '00h', end.clus = '96h') | |
| ``` | |
| ```{r} | |
| ssPT <- slingPseudotime(slingshotPT) | |
| ssPT <- as.data.frame(ssPT) | |
| plot_ly(rd2, x=~DC1, y= ~DC2, color = ssPT$curve1) | |
| plot_ly(rd1, x=~PC1, y= ~PC2, color = ssPT$curve1) | |
| ``` | |
| Find variable genes along the trajectory | |
| ```{r} | |
| t <- ssPT$curve1 | |
| # look at top variable gens | |
| Y <- logExpressionFiltered | |
| var1K <- names(sort(apply(Y,1,var),decreasing = TRUE)) | |
| Y <- Y[var1K,] | |
| # fit a GAM with a loess term for pseudotime | |
| gam.pval <- apply(Y,1,function(z){ | |
| d <- data.frame(z=z, t=t) | |
| suppressWarnings({ | |
| tmp <- gam(z ~ lo(t), data=d) | |
| }) | |
| p <- summary(tmp)[4][[1]][1,5] | |
| p | |
| }) | |
| ``` | |
| ```{r} | |
| topgenes <- names(sort(gam.pval, decreasing = FALSE))[1:15] | |
| heatdata <- logExpressionFiltered[topgenes, order(t, na.last = NA)] | |
| cTypes <- as.data.frame(ChuCellTypes$Time) | |
| rownames(cTypes) <- colnames(Chu2017) | |
| heatclus <- as.factor(cTypes[order(t, na.last = NA),]) | |
| heatmap(as.matrix(heatdata), Colv = NA, | |
| ColSideColors = brewer.pal(6,"Set1")[heatclus], labCol = FALSE) | |
| ``` | |
| ```{r} | |
| exprData <- logExpressionFiltered | |
| colnames(exprData) <- gsub(pattern = "[.]", replacement = '_', colnames(exprData)) | |
| ptData <- data.frame(ssPT$curve1) | |
| rownames(ptData) <- colnames(exprData) | |
| colnames(ptData) <- 'PseudoTime' | |
| geneData <- data.frame(sort(gam.pval, decreasing = FALSE)) | |
| colnames(geneData) <- 'VGAMpValue' | |
| geneData[,'Variance'] <- apply(logExpressionFiltered[rownames(geneData),],1,var) | |
| write.csv(x = exprData, file = 'ExpressionData.csv', quote = FALSE) | |
| write.csv(x = ptData, file = 'PseudoTime.csv', quote = FALSE) | |
| write.csv(x = geneData, file = 'GeneOrdering.csv', quote = FALSE) | |
| ``` | |
| Sanity check | |
| ```{r} | |
| rdDF <- as.data.frame(rd2) | |
| plot_ly(rdDF, x= ~DC1, y= ~DC2,color = ptData$PseudoTime) | |
| plot_ly(as.data.frame(rd2), x=~DC1, y= ~DC2, color = ssPT$curve1) | |
| ``` | |
| \ | |