/
process_counts_data.Rmd
390 lines (312 loc) · 11.7 KB
/
process_counts_data.Rmd
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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
---
title: "Estimate transcription rate and half-life from counts"
output:
html_notebook:
toc: true
number_sections: true
theme: readable
author: Marat Mufteev
date: "`r format(Sys.time(), 'Generated on %d %B, %Y')`"
---
Upload libraries
```{r}
library(DESeq2)
library(dplyr)
```
Upload drosophila normalized counts data
Column name format: Celltype_Genotype_Timepoint_Replicate
```{r}
normed_tbl = read.csv("./normed_counts.csv.gz",
header = T)
normed_avg_tbl = read.csv("./normed_avg_counts.csv.gz",
header = T)
```
## Estimate transcription rate (TR) log2 fold-change
Prepare matrix for DESeq2.
```{r}
include_pseudoreplicate = TRUE
# Depending on your experiment design, consider excluding a pseudoreplicate
if (include_pseudoreplicate) {
gene.counts = normed_tbl %>%
select(gene, contains("_0.5_"), contains("_1_")) %>%
group_by(gene) %>%
summarise_all(sum, na.rm = T) %>%
mutate_at(vars(contains("_1_")), function(x) {x/2})
} else {
gene.counts = normed_tbl %>%
select(gene, contains("_0.5_")) %>%
group_by(gene) %>%
summarise_all(sum, na.rm = T)
}
# Remove names of the genes
counts.data = gene.counts[,c(2:ncol(gene.counts))]
# Convert counts dataframe to matrix and append gene names
counts.data = as.matrix(counts.data)
row.names(counts.data) = gene.counts$gene
```
Run DESeq2
```{r}
# Choose cell types
cellA = "Neu_WT"
cellB = "Neu_RTT"
# Indices for subsets
A_indices = grepl(x = colnames(counts.data), pattern = cellA)
B_indices = grepl(x = colnames(counts.data), pattern = cellB)
# Select data for each assay and celltype
subset_A = counts.data[, A_indices]
subset_B = counts.data[, B_indices]
# [Optional] Quantile normalize ranges between replicates
columns_A = colnames(subset_A)
subset_A = preprocessCore::normalize.quantiles(as.matrix(subset_A))
subset_A = data.frame(subset_A)
colnames(subset_A) = columns_A
columns_B = colnames(subset_B)
subset_B = preprocessCore::normalize.quantiles(as.matrix(subset_B))
subset_B = data.frame(subset_B)
colnames(subset_B) = columns_B
# Merge Trap and Input data
# Convert data to counts.
subset = cbind(subset_A, subset_B) %>%
# Convert back to raw counts
# Not necessary, if you start with raw counts data
mutate_if(is.numeric, function(x) {as.integer(x/min(na_if(x, 0), na.rm = T))})
# Prepare samples for DESeq
samples = colnames(subset)
column.data = data.frame(samples,
celltype = gsub("_0.5|_1|_A|_B", "", samples),
row.names = samples)
# Create DESeqDataSet object from matrix with counts
dds = DESeq2::DESeqDataSetFromMatrix(countData = subset,
colData = column.data,
design =~ celltype)
# Run DESeq pipeline
dds = DESeq2::DESeq(dds)
# Prepare a dataframe with results
res = DESeq2::results(dds, contrast=c("celltype", cellB, cellA))
TR_shifts = data.frame(res) %>%
mutate(gene = row.names(counts.data)) %>%
select(gene, log2FoldChange, lfcSE, pvalue, padj)
# Rename columns
colnames(TR_shifts) = c("gene",
paste0(c("log2FC_", "lfcSE_", "pvalue_", "padj_"), "Neu_RTT_WT"))
# Save data
write.csv(TR_shifts, "./TR_log2FC_deseq2.csv",
quote = F, row.names = F)
```
## Estimate steady state (SS) log2 fold-change
Prepare matrix for DESeq2.
```{r}
gene.counts = normed_tbl %>%
select(gene, contains("SS")) %>%
group_by(gene) %>%
summarise_all(sum, na.rm = T)
# Remove names of the genes
counts.data = gene.counts[,c(2:ncol(gene.counts))] %>%
mutate_if(is.numeric, as.integer)
# Convert counts dataframe to matrix and append gene names
counts.data = as.matrix(counts.data)
row.names(counts.data) = gene.counts$gene
```
Run DESeq2
```{r}
# Choose cell types
cellA = "Neu_WT"
cellB = "Neu_RTT"
# Indices for subsets
A_indices = grepl(x = colnames(counts.data), pattern = cellA)
B_indices = grepl(x = colnames(counts.data), pattern = cellB)
# Select data for each assay and celltype
subset_A = counts.data[, A_indices]
subset_B = counts.data[, B_indices]
# Merge Trap and Input data
# Convert data to counts.
subset = cbind(subset_A, subset_B)
# Convert back to counts
# Not necessary, if you start with raw counts data
#as.data.frame() %>%
#mutate_if(is.numeric, function(x) {as.integer(x/min(na_if(x, 0), na.rm = T))})
# Prepare samples for DESeq
samples = colnames(subset)
column.data = data.frame(samples,
celltype = gsub("_SS|_A|_B", "", samples),
row.names = samples)
# Create DESeqDataSet object from matrix with counts
dds = DESeq2::DESeqDataSetFromMatrix(countData = subset,
colData = column.data,
design =~ celltype)
# Run DESeq pipeline
dds = DESeq2::DESeq(dds)
# Prepare a dataframe with results
res = DESeq2::results(dds, contrast=c("celltype", cellB, cellA))
SS_shifts = data.frame(res) %>%
mutate(gene = row.names(counts.data)) %>%
select(gene, log2FoldChange, lfcSE, pvalue, padj)
# Rename columns
colnames(SS_shifts) = c("gene",
paste0(c("log2FC_", "lfcSE_", "pvalue_", "padj_"), "Neu_RTT_WT"))
# Save data
write.csv(SS_shifts, "./SS_log2FC_deseq2.csv",
quote = F, row.names = F)
```
## Estimate half-life with a ratio method
Prepare matrix for DESeq2
```{r}
include_pseudoreplicate = FALSE
if (include_pseudoreplicate) {
gene.counts = normed_tbl %>%
select(gene, contains("SS"), contains("_0.5_"), contains("_1_")) %>%
group_by(gene) %>%
summarise_all(sum, na.rm = T) %>%
mutate_at(vars(contains("_1_")), function(x) {x/2})
} else {
gene.counts = normed_tbl %>%
select(gene, contains("SS"), contains("_0.5_")) %>%
group_by(gene) %>%
summarise_all(sum, na.rm = T)
}
# Remove names of the genes
counts.data = gene.counts[,c(2:ncol(gene.counts))]
# Convert counts dataframe to matrix and append gene names
counts.data = as.matrix(counts.data)
row.names(counts.data) = gene.counts$gene
```
Run DESeq2
```{r}
samples.list = list()
celltypes = c("Neu_WT", "Neu_RTT")
for (ind in c(1:2)) {
# Pick cell types
cell = celltypes[ind]
# Indices for subsets
indices = grepl(x = colnames(counts.data), pattern = cell)
# Select data for each celltype
subset = as.data.frame(counts.data[, indices])
subset = subset %>%
mutate_if(is.numeric, function(x) {as.integer(x/min(na_if(x, 0), na.rm = T))})
# Prepare samples for DESeq
samples = colnames(subset)
column.data = data.frame(samples,
assay = ifelse(grepl("SS", samples), "SS", "TR"),
celltype = gsub("_0.5|_1|_A|_B|_SS", "", samples),
row.names = samples) %>%
mutate(assay = factor(assay, levels = c("TR", "SS")))
# Create DESeqDataSet object from matrix with counts
dds = DESeq2::DESeqDataSetFromMatrix(countData = subset,
colData = column.data,
design =~ assay)
# Run DESeq pipeline
dds = DESeq2::DESeq(dds)
res = DESeq2::results(dds)
res = data.frame(res) %>%
mutate(gene = row.names(counts.data)) %>%
select(gene, log2FoldChange, lfcSE, pvalue, padj)
samples.list[[cell]] = res
}
# Combine tables for each transition
SS_TR_data = Reduce(x = samples.list, f = function(x, y) {full_join(x, y, by = "gene")})
colnames(SS_TR_data) = c("gene", unlist(lapply(X = names(samples.list),
FUN = function(x) paste0(c("log2_SS_TR_", "lfcSE_", "pvalue_", "padj_"), x))))
# Save table
output = "./SS_TR_deseq2.csv"
write.csv(SS_TR_data, output, row.names = F)
```
## Estimate half-life with saturation curve
Define function for fitting a saturation curve. This functions is slow. It's better
to run it on a cluster with multiple cores after adapting the code for parallel run in R.
```{r}
estimate_HL <- function(ind, tbl, celltype) {
# Subset counts table
columns = grep(celltype, colnames(tbl))
times = c(0.5, 1, 4, 8, 24)
if (celltype == "Neu_RTT") {
times = c(0.5, 4, 8, 24)
}
# Prepare counts data for fitting saturation curve
y = as.numeric(tbl[ind, columns])
t = times
# In a first pass fit curve roughly without correcting for
# dependence of variance on number of counts. Use rough fit to estimate
# "true" number of counts Y at each timepoint. Then, correct for
# increase in variance in second pass by setting weights W as follows:
# w = 1/variance ~ 1/Y
fit <- tryCatch(nls( y ~ C*( 1 - exp( k*t ) ),
start=c( C=max(y), k=-0.1 ),
lower=c(C=1,k=-5.0), upper=c(C=1e7,k=-0.01),
control=list(warnOnly=T) ),
error=function(e) NULL )
# If first pass works, create weights
if (is.null(fit)) {
output_vec = rep(NA, 19)
second_fit = F
} else {
second_fit = T
y_new = coef(fit)["C"]*( 1 - exp( coef(fit)["k"]*t ) )
w = 1/y_new
}
if (second_fit) {
# Correct for variance with weights in a second pass fit
fit <- tryCatch(nls( y ~ C*( 1 - exp( k*t ) ),
start=c( C=max(y), k=-0.1 ),
weights=w,
lower=c(C=1,k=-5.0), upper=c(C=1e7,k=-0.01),
control=list(warnOnly=T) ),
error=function(e) NULL )
if (is.null(fit)) {
output_vec = rep(NA, 19)
} else {
k = coef(fit)["k"]
C = coef(fit)["C"]
res_sum = sum((resid(fit))**2*fit$weights)
quality = res_sum/C**2
tot_sum = sum((y - mean(y))**2*fit$weights)
resids = resid(fit)
# Estimate confidence intervals based on likelihood profile method
CI_50 <- tryCatch(confint(fit, level = 0.5),
error=function(e) NULL )
CI_70 <- tryCatch(confint(fit, level = 0.7),
error=function(e) NULL )
if (is.null(CI_50)) {
CI_50_lower = NA
CI_50_upper = NA
} else {
CI_50_lower = CI_50[2,1]
CI_50_upper = CI_50[2,2]
}
if (is.null(CI_70)) {
CI_70_lower = NA
CI_70_upper = NA
} else {
CI_70_lower = CI_70[2,1]
CI_70_upper = CI_70[2,2]
}
# Construct output vector. Correct for missing 1H timepoint in Neu20.
if (celltype == "Neu20") {
resids = c(resids[1], NA, resids[2:4])
y = c(y[1], NA, y[2:4])
output_vec = c(-log(2)/k, C, quality, res_sum, tot_sum, resids, CI_50_lower, CI_50_upper, CI_70_lower, CI_70_upper, y)
} else {
output_vec = c(-log(2)/k, C, quality, res_sum, tot_sum, resids, CI_50_lower, CI_50_upper, CI_70_lower, CI_70_upper, y)
}
}
}
if (ind %% 100 == 0) {
print(ind)
}
# Give proper names to output
names(output_vec) = c("HL", "C", "quality", "rsqsum", "totsqsum",
"res_30m", "res_1H", "res_4H", "res_8H", "res_24H",
"CI_50_low", "CI_50_high", "CI_70_low", "CI_70_high",
"count_30m", "count_1H", "count_4H", "count_8H", "count_24H")
return(output_vec)
}
```
Fit the data to estimate half-life
```{r}
# Fit data for the first 100 genes of WT neurons
# Two main outputs are the first 2 columns of result_tbl
# The rest columns are auxiliary
res = sapply(X = 1:100, FUN = estimate_HL,
normed_avg_tbl %>% select(-contains("SS")), "Neu_WT")
result_tbl = data.frame(t(res))
```
#########