-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathspatialExample.Rmd
More file actions
148 lines (130 loc) · 3.84 KB
/
spatialExample.Rmd
File metadata and controls
148 lines (130 loc) · 3.84 KB
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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
---
title: "spatialExample"
author:
- Gaurav Sharma
- Loyal Goff
- Genevieve Stein-O'Brien
date: "7/31/2019"
output:
html_document: default
pdf_document: default
---
```{r setup, warning=FALSE, message=FALSE}
library(rgl)
library(projectR)
library(psych)
library(fields)
library(CoGAPS)
library(magick)
library(BiocParallel)
source('./utils.R')
# Fix rgl crashing issue
Sys.setenv(LIBGL_ALWAYS_SOFTWARE=1)
```
# Overview
## Data Import
```{r dataImport}
# Read Raw data
raw.data = read.csv("dge_raw.txt",sep = "\t",header = F)
raw.data.genes = raw.data$V1
raw.data$V1 = NULL
print(grep("'",raw.data.genes,value = T,fixed = T))
raw.data.genes = gsub("'","",raw.data.genes,fixed = T)
raw.data = as.matrix(raw.data)
rownames(raw.data) = raw.data.genes
# Read Normalized data
normalized.data = read.csv("dge_normalized.txt", sep = "\t")
print(grep("'",rownames(normalized.data),value = T,fixed = T))
normalized.data.genes<-gsub("'","",rownames(normalized.data),fixed = T)
normalized.data <- as.matrix(normalized.data)
rownames(normalized.data) <- normalized.data.genes
# In situ data
insitu.matrix = read.csv("binarized_bdtnp.csv",check.names=F)
insitu.genes_orig <- colnames(insitu.matrix)
missingGenes = insitu.genes_orig[which(!insitu.genes_orig %in% normalized.data.genes)]
print(missingGenes)
insitu.genes = gsub(".","-",insitu.genes_orig,fixed = T)
insitu.genes = gsub("-spl-","(spl)",insitu.genes,fixed = T)
stopifnot(all(insitu.genes %in% raw.data.genes))
stopifnot(all(insitu.genes %in% normalized.data.genes))
insitu.matrix = as.matrix(insitu.matrix)
colnames(insitu.matrix) = insitu.genes
```
```{r geometry}
# Geometry data
geometry <- read.csv("geometry.txt",sep = " ")
geometry.inv<-geometry
geometry.inv$ycoord<--(geometry.inv$ycoord)
full.geometry<-rbind(geometry,geometry.inv)
xlim<-c(-max(abs(geometry)),max(abs(geometry)))
ylim<-xlim
zlim<-ylim
#Visualize the geometry
plot3d(full.geometry,xlim=xlim,ylim=ylim,zlim=zlim)
```
## CoGAPS on Gene expression data
```{r eval=FALSE}
#This can take few hours to run depending on the machine
nPat <- 20
runParams <- new("CogapsParams", nPatterns = nPat, seed = 123, nIterations = 50000)
dataCogaps <- CoGAPS(normalized.data, params = runParams, nThreads = 10)
```
## projectR on the cogapsResults
```{r}
nPat <- 20
#importing the pre-computed cogaps result
dataCogaps <- readRDS('drosophilaNormalizedData20result2.rds')
projPosCgps <- projectR(t(insitu.matrix),loadings = dataCogaps, full = T)
```
## Visualize and save the patterns
Remove eval = FALSE to visualize all patterns
```{r}
for(x in 1:nPat) {
i <- x
pp.plot <- c(projPosCgps[[1]][i, ], projPosCgps[[1]][i, ])
par3d(windowRect = c(0, 50, 800, 800))
plot3d(
full.geometry,
xlim = xlim,
ylim = ylim,
zlim = zlim,
col = myColorRamp(palette = inferno(100), pp.plot),
size = 10,
box = F,
axes = F,
xlab = "",
ylab = "",
zlab = "",
aspect = T
)
bgplot3d(suppressWarnings (
image.plot(
legend.only = TRUE,
nlevel = 100,
zlim = c(min(pp.plot), max(pp.plot)),
legend.args = list(text = 'Projected pattern'),
col = inferno(100)
)
))
snapshot3d(
filename = paste0(
"./plots/png/cpgs",
as.character(nPat),
"Pattern",
as.character(i),
".png"
),
fmt = "png"
)
movie3d(
spin3d(axis = c(1, 1, 1), rpm = 5),
duration = 10,
movie = paste0("cgps", as.character(nPat), "Pattern", as.character(i)),
dir = "./plots",
convert = TRUE
)
while (rgl.cur() > 0) {
rgl.close()
}
}
```