forked from matbonfanti/project-template
-
Notifications
You must be signed in to change notification settings - Fork 0
/
05_DE_edgeR.Rmd
164 lines (136 loc) · 5.64 KB
/
05_DE_edgeR.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
---
title: "Covidiamo Pseudobulk"
written by: "Alessandro Vitriolo"
modified by: "Emanuele Villa, Matteo Bonfanti"
date: 'Date: `r format(Sys.Date(), "%B %d, %Y")`'
output:
html_document:
toc: true
toc_float: true
theme: 'yeti'
highlight: 'tango'
code_folding: hide
params:
input_counts: '../Result/PseudoBulk/totalRaw.csv'
input_obs: '../Result/PseudoBulk/obsRaw.csv'
output_folder: '../Result/PseudoBulk/'
---
```{r load lib}
library(edgeR)
library(stringr)
```
# Read and prepare input
Load counts
```{r read counts}
metadata <- read.table(params$input_obs)
head(metadata)
```
```{r read obs}
counts.raw <- read.table(params$input_counts, head=T)
```
and filter expression (strong filter, as if it was a real bulk, you might want to play with minCount)
```{r filter matrix}
easyFilter <- function(counts,minCount,minSamples){
counts <- counts[which(apply(counts, 1, minCount = minCount,
minSamples = minSamples, function(x, minCount, minSamples) {
sum(x >= minCount)
}) >= minSamples), ]
}
counts.f <- easyFilter(counts.raw, minCount = 25, minSamples = 10)
```
# Remove control patient
```{r remove control}
countsRawNoCTRL <- counts.raw[,!grepl('crtl_control',names(counts.raw))]
metadataNoCTRL <- metadata[(metadata$time!='control'),]
countsFilter <- counts.f[,!grepl('crtl_control',names(counts.f))]
nrow(metadataNoCTRL); ncol(countsRawNoCTRL); ncol(countsFilter)
nrow(countsFilter)
nrow(countsRawNoCTRL)
```
# Differential expression and Enrichments on raw data
```{r define edgeR wrapper function}
edg2 <- function(e,mm,gt){
row.names(mm) <- colnames(e)
dds <- calcNormFactors(DGEList(e))
dds <- estimateGLMRobustDisp(dds,mm)
dds <- glmFit(dds, mm)
res <- as.data.frame(topTags(glmLRT(dds, gt), nrow(e)))
res
}
```
```{r differential expression for 1-2-3 pattern}
pattern=c(1,2,3)
patternLabel=paste(as.character(pattern), collapse = '_')
pattern=pattern/sum(pattern)
for (population in unique(metadataNoCTRL$cell_families)){
print(population)
popCounts <- countsFilter[,grep(paste0("_",population),names(countsFilter))]
popMeta <- metadataNoCTRL[(metadataNoCTRL$cell_families==population),]
ind <- popMeta$PatientTime
popMeta$time <- str_replace_all(popMeta$time,"admission",as.character(pattern[1]))
popMeta$time <- str_replace_all(popMeta$time,"discharge",as.character(pattern[2]))
popMeta$time <- str_replace_all(popMeta$time,"post-1mo",as.character(pattern[3]))
popMeta$time <- sapply(popMeta$time, as.numeric)
mm <- model.matrix(~0+popMeta$patient+popMeta$time)
colSums(mm)
popTag=edg2(popCounts,gt = 'popMeta$time',mm=mm)
write.csv(x=popTag,file=paste0(params$output_folder,"/cell_families_",population,"_",patternLabel,".csv"))
}
```
```{r differential expression for 1-2-2 pattern}
pattern=c(1,2,2)
patternLabel=paste(as.character(pattern), collapse = '_')
pattern=pattern/sum(pattern)
for (population in unique(metadataNoCTRL$cell_families)){
print(population)
popCounts <- countsFilter[,grep(paste0("_",population),names(countsFilter))]
popMeta <- metadataNoCTRL[(metadataNoCTRL$cell_families==population),]
ind <- popMeta$PatientTime
popMeta$time <- str_replace_all(popMeta$time,"admission",as.character(pattern[1]))
popMeta$time <- str_replace_all(popMeta$time,"discharge",as.character(pattern[2]))
popMeta$time <- str_replace_all(popMeta$time,"post-1mo",as.character(pattern[3]))
popMeta$time <- sapply(popMeta$time, as.numeric)
mm <- model.matrix(~0+popMeta$patient+popMeta$time)
colSums(mm)
popTag=edg2(popCounts,gt = 'popMeta$time',mm=mm)
write.csv(x=popTag,file=paste0(params$output_folder,"/cell_families_",population,"_",patternLabel,".csv"))
}
```
```{r differential expression for 1-1-2 pattern}
pattern=c(1,1,2)
patternLabel=paste(as.character(pattern), collapse = '_')
pattern=pattern/sum(pattern)
for (population in unique(metadataNoCTRL$cell_families)){
print(population)
popCounts <- countsFilter[,grep(paste0("_",population),names(countsFilter))]
popMeta <- metadataNoCTRL[(metadataNoCTRL$cell_families==population),]
ind <- popMeta$PatientTime
popMeta$time <- str_replace_all(popMeta$time,"admission",as.character(pattern[1]))
popMeta$time <- str_replace_all(popMeta$time,"discharge",as.character(pattern[2]))
popMeta$time <- str_replace_all(popMeta$time,"post-1mo",as.character(pattern[3]))
popMeta$time <- sapply(popMeta$time, as.numeric)
mm <- model.matrix(~0+popMeta$patient+popMeta$time)
colSums(mm)
popTag=edg2(popCounts,gt = 'popMeta$time',mm=mm)
write.csv(x=popTag,file=paste0(params$output_folder,"cell_families_",population,"_",patternLabel,".csv"))
}
```
```{r differential expression for 1-2-1 pattern}
pattern=c(1,2,1)
patternLabel=paste(as.character(pattern), collapse = '_')
pattern=pattern/sum(pattern)
for (population in unique(metadataNoCTRL$cell_families)){
print(population)
popCounts <- countsFilter[,grep(paste0("_",population),names(countsFilter))]
popMeta <- metadataNoCTRL[(metadataNoCTRL$cell_families==population),]
ind <- popMeta$PatientTime
popMeta$time <- str_replace_all(popMeta$time,"admission",as.character(pattern[1]))
popMeta$time <- str_replace_all(popMeta$time,"discharge",as.character(pattern[2]))
popMeta$time <- str_replace_all(popMeta$time,"post-1mo",as.character(pattern[3]))
popMeta$time <- sapply(popMeta$time, as.numeric)
mm <- model.matrix(~0+popMeta$patient+popMeta$time)
colSums(mm)
popTag=edg2(popCounts,gt = 'popMeta$time',mm=mm)
write.csv(x=popTag,file=paste0(params$output_folder,"cell_families_",population,"_",patternLabel,".csv"))
}
```