File size: 4,318 Bytes
eb028df | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 | ---
title: "Preprocessing script for Nestorowa 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)
```
Read input files. Both the input .txt files are from http://blood.stemcells.cam.ac.uk/data/
```{r warning=FALSE}
Nestorowa2017 <- read.table('normalisedCountsVariableGenes.txt', header = T, row.names = 1)
NestorowaCellTypes <- read.table('all_cell_types.txt', header = T, row.names = 1)
dim(Nestorowa2017)
head(NestorowaCellTypes)
```
Compute diffusion map projection of the cells
```{r warning=FALSE, fig.height=10}
logExpression <- log2(Nestorowa2017 + 1)
dm <- DiffusionMap(t(logExpression), distance = "cosine", sigma = .16)
rd2 <- cbind(DC1 = dm$DC1, DC2 = dm$DC2, DC3 = dm$DC3)
plot_ly(as.data.frame(rd2), type='scatter3d', x= ~DC1, y= ~DC2, z= ~DC3, size = 5, color = "grey")
```
Map the cell types identified using narrow gating in the original publication to the lower dimenstional representation.
```{r warning=False}
# Process the cell type file, use only the following columns instead of all the broad categories
cellTypes <- NestorowaCellTypes[,c('ESLAM','LTHSC_broad','LMPP_broad','MPP_broad','CMP_broad','MEP_broad','GMP_broad','MPP1_broad','MPP2_broad','MPP3_broad','STHSC_broad')]
# assign a cluster label to each cell type
cellTypeDict <- DataFrame()
for(cellName in colnames(Nestorowa2017)){
cTypeName <- colnames(cellTypes)[which(cellTypes[cellName[1],] == 1)][1]
if(is.na(cTypeName)){
cTypeName <- 'Unknown'
}
cellTypeDict[cellName,'Type'] <- cTypeName
}
plot_ly(as.data.frame(rd2), type='scatter3d', x= ~DC1, y= ~DC2, z= ~DC3, size = 5, color = cellTypeDict$Type, colors = 'Set2')
```
Run slingshot
```{r}
slingshotPT <- slingshot(rd2, reducedDim = rd2,clusterLabels = cellTypeDict$Type, start.clus = 'ESLAM', end.clus = c('MEP_broad','GMP_broad','LMPP_broad'))
```
```{r}
ssPT <- slingPseudotime(slingshotPT)
ssPT <- as.data.frame(ssPT)
plot_ly(as.data.frame(rd2), type='scatter3d', x= ~DC1, y= ~DC2, z= ~DC3, size = 5, color = ssPT$curve1)
plot_ly(as.data.frame(rd2), type='scatter3d', x= ~DC1, y= ~DC2, z= ~DC3, size = 5, color = ssPT$curve2)
plot_ly(as.data.frame(rd2), type='scatter3d', x= ~DC1, y= ~DC2, z= ~DC3, size = 5, color = ssPT$curve3)
```
According to the paper the three lineages above are as follows: curve1 is Granulocyte-Macrophage (GM), curve2 is Erythroid (E), and curve3 is Lymphoid (L).
Find variable genes along the GM trajectory
```{r}
lineages <- c('GM','E','L')
count <- 1
for (col in colnames(ssPT)){
t <- ssPT[,col]
Y <- logExpression
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
})
topgenes <- names(sort(gam.pval, decreasing = FALSE))[1:10]
heatdata <- logExpression[topgenes, order(t, na.last = NA)]
heatclus <- as.factor(cellTypeDict[order(t, na.last = NA),])
heatmap(as.matrix(heatdata), Colv = NA,
ColSideColors = brewer.pal(9,"Set1")[heatclus], labCol = FALSE)
exprData <- logExpression[,which(!is.na(t))]
colnames(exprData) <- gsub(pattern = "[.]", replacement = '_', colnames(exprData))
ptData <- data.frame(ssPT[which(!is.na(t)), col])
rownames(ptData) <- colnames(exprData)
colnames(ptData) <- 'PseudoTime'
geneData <- data.frame(sort(gam.pval, decreasing = FALSE))
colnames(geneData) <- 'VGAMpValue'
geneData[,'Variance'] <- apply(exprData[rownames(geneData),],1,var)
write.csv(x = exprData, file = paste0('ExpressionData-',lineages[count],'.csv'), quote = FALSE)
write.csv(x = ptData, file = paste0('PseudoTime-',lineages[count],'.csv'), quote = FALSE)
write.csv(x = geneData, file = paste0('GeneOrdering-',lineages[count],'.csv'), quote = FALSE)
count <- count +1
}
```
Sanity check
```{r}
rdDF <- as.data.frame(rd2)
rdDF <- rdDF[which(!is.na(ssPT$curve3)),]
plot_ly(rdDF, type='scatter3d', x= ~DC1, y= ~DC2, z= ~DC3,color = ptData$PseudoTime)
plot_ly(as.data.frame(rd2), type='scatter3d', x= ~DC1, y= ~DC2, z= ~DC3, size = 5, color = ssPT$curve3)
``` |