-
Notifications
You must be signed in to change notification settings - Fork 0
/
2-2_TileR_replicateAnalysis_DESeq_diffIntFrags_nonIced_matrices_justWT_200324.R
547 lines (389 loc) · 15.7 KB
/
2-2_TileR_replicateAnalysis_DESeq_diffIntFrags_nonIced_matrices_justWT_200324.R
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
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
### TILED ANALYSIS ###
library(tidyverse)
library(RColorBrewer)
library(gplots)
library(DESeq2)
#### taking raw interaction matrices calculated by Marieke's script
# from individual samples and I am data cleaning and putting through DESeq to:
# 1. cluster > PCA, heatmaps
# 2. find out differentially interacting fragments between genotypes and cell types
## all done on RAW CONTACT MATRICES
#################
### FUNCTIONS ###
#################
# making a function to perform pairwise tests on a combined dds object generated with a given design formula
pairTestDESeq <- function(dds,design,contrast,pair1,pair2,FDRLevel,outDirData,outDirMA){
# this function takes inputs:
# - dds: a dds object generated by the DESeq function
# - design: a character string containing the design used to generate the dds object (eg, "cellType+genoType")
# - contrast: a character string containing the name of the contrast to make (eg "cellType")
# - pair1: a character string for the first sample to include in the pairwise comparison (must match colData of dds!)
# - pair2: a character string for the second sample to include in the pairwise comparisonand
# - FDRLevel: an false discovery rate threshold to use
# - subset: a character string of the subset being tested (eg, if testing between genotypes, subset would be celltype, so subset="CD41")
# - outDirData: an output directory for the results tables
# - outDirMA: an output directory for the MA plots
# output is a results table for the pairwise comparison filtered on the FDRLevel threshold saved to the outDirData output directory
# perform the results function from DESeq
res <- results(dds,
contrast=c(contrast,
pair1,
pair2),
alpha=FDRLevel)
# get just the significant frags
res$logpvalue <- -log(res$pvalue)
res$logpadj <- -log(res$padj)
# filter only the FDR below chosen significance level
resData <- res %>% data.frame %>% rownames_to_column(var="frags")
diffInt <- resData %>% filter(padj<=FDRLevel)
# format the name of this comparison
compName <- paste(pair1,"vs",pair2,design,contrast,"FDR",FDRLevel,sep="_")
# write the table
write.table(diffInt,
file=paste0(outDirData,"DESeq2_", compName, ".txt"), eol="\n", sep="\t", row.names=F, quote=F)
# MA plot for this comparison
# Default DESeq2 MA plots
pdf(paste(outDirMA,paste("MA",design,contrast,pair1,"vs",pair2,FDRLevel,"qc-pca.pdf",sep="_")), 10,10)
MA<-DESeq2::plotMA(res,alpha=FDRLevel)
print(MA)
dev.off()
}
#### PLOTTING QC FUNCTION ####
plotting_QC <- function(dds,design,contrast,condition,outDirPCAb){
# inputs:
# - dds: a DESeq object
# - design: a character string of the design used to make the dds object (eg "cellType+genoType")
# - contrast: a character string of the contrast being done (eg "cellType")
# - condition: a factor variable used to perform DESeq, given to colData
# - outDirPCAb: a directory path to output the PCA plot
dat <- rlog(dds) # vst is a much faster transformation than rld!!!
## sample distance matrix
# Specify colours
mycols <- brewer.pal(8, "Set3")[1:length(unique(condition))]
# Create distance matrix
sampleDists <- as.matrix(dist(t(assay(dat))))
# Plot heatmap
pdf(paste0(outDirHeatmaps, design, "_qc-heatmap-samples.pdf"), 10,10)
hmap <- heatmap.2(as.matrix(sampleDists), key=F, trace="none", # Plot distance matrix
col=colorpanel(100, "black", "white"), # Distance coloured black-white
ColSideColors=mycols[condition], RowSideColors=mycols[condition], # Side annotations = Groups
margin=c(19, 19), main="Sample Distance Matrix")
leg <- legend("bottom",as.character(unique(condition)), col=mycols, pch=15)
leg2 <- legend("bottomleft", paste0("maxDist=",as.character(round(max(hmap$carpet)))))
print(hmap)
print(leg)
dev.off()
# Default DESeq2 PCA
pdf(paste(outDirPCAb,design,"_",contrast, "_justWT_small_qc-pca.pdf"), 3.3,3.3)
pca_b <- plotPCA(dat, intgroup=contrast)
print(pca_b)
dev.off()
}
##############
### INPUTS ###
##############
## public directory
publicDir <- "http://sara.molbiol.ox.ac.uk/public/dowens/CTCF-KO/Tiled/CTCF-KO_virtCapC/"
##
email <- "dominic.owens@imm.ox.ac.uk"
##
genome <- "mm9"
## the main directory
base = "C:/Users/Dominic/Desktop/Work/Paper/Bioinformatics/Tile-C/CTCF-KO/"
# the significance level to use for DESeq
FDRLevel <- 0.1
# are we using iced or raw matrices?
matrixType = "raw" # "raw"
#### relative to base folder, or could be specified elsewhere
## the bait file
baitFile <- paste0(base, "baitsRightIDs.txt")
## the fragments genome file
fragFile <- paste0(base, "fragData_2kb.txt")
## the data folder
if (matrixType=="iced"){
dataFolder <- paste0(base, "iced_matrix/")
} else if (matrixType=="raw") {
dataFolder <- paste0(base, "matrix/")
} else {
cat("Warning: matrixType must be set to either \"iced\" or \"raw\"")
}
###############
### OUTPUTS ###
###############
variablesFolder <- paste0(base, "savedVariables_", matrixType, "/")
dir.create(variablesFolder, showWarnings = F)
## DESeq directories
DESeqDir <- paste0(base, "DESeq_FDR_", FDRLevel, "_", matrixType, "/")
outDirData = paste0(DESeqDir, "/data_", matrixType, "/")
outDirPlots = paste0(DESeqDir, "/plots_", matrixType, "/")
outDirBeds = paste0(DESeqDir, "/bedFiles_", matrixType, "/")
outDirHeatmaps = paste0(outDirPlots, "heatmaps_", matrixType, "/")
outDirPCAb = paste0(outDirPlots, "PCA_basic_", matrixType, "/")
outDirPCAf = paste0(outDirPlots, "PCA_fancy_", matrixType, "/")
outDirMA = paste0(outDirPlots, "MA_plots_", matrixType, "/")
dir.create(DESeqDir, showWarnings=F)
dir.create(outDirPlots, showWarnings=F)
dir.create(outDirBeds, showWarnings=F)
dir.create(outDirData, showWarnings=F)
dir.create(outDirHeatmaps, showWarnings=F)
dir.create(outDirPCAb, showWarnings=F)
dir.create(outDirPCAf, showWarnings=F)
dir.create(outDirMA, showWarnings=F)
##########################
### LOAD THE VARIABLES ###
##########################
cat("Gathering the input files \n")
## load the bait file, get names of baits, and make into format useful for joining to later
## need to match the wrong DpnII bait IDs
baits = data.table::fread(baitFile)
colnames(baits) <- c("bait_chr", "bait_start", "bait_end", "baitID", "baitName")
baitNames <- baits$baitName
baits <-
baits %>%
mutate("bait_frag"=paste0(bait_chr,":",bait_start,"-",bait_end)) %>%
dplyr::select(bait_frag, baitID, baitName)
## load the frag file and make into format useful for joining to later
fragData = data.table::fread(fragFile)
colnames(fragData) <- c("prey_chr", "prey_start", "prey_end", "preyID")
fragData <-
fragData %>%
mutate("baitID"=preyID, "fragID"=preyID)
# dplyr::select(prey_frag, preyID)
# get the samples
samples <-
list.files(path=dataFolder) %>%
data.frame
colnames(samples) <- "sample"
# get the matrix location (sampleFIle) and add to the table
samples %<>%
mutate(sampleFiles=paste0(dataFolder,sample,"/raw/2000/tiled_",sample,"_2000.matrix"))
# get the matrix location (sampleFIle) and add to the table
# along with
# tissue types, genotypes, and clone names
samples %<>%
mutate(sampleFiles=paste0(dataFolder,sample,"/raw/2000/tiled_",sample,"_2000.matrix"),
sample2=sample) %>%
separate(sample2,into=c("tissue","genotype","clone"),sep="_")
# get the tissue types
tissues <- levels(factor(word(samples$sample, 1, sep="_")))
# geno types
genotypes <- levels(factor(word(samples$sample, 2, sep="_")))
# exchange the - for . to prevent problems with column naming
genotypes <- gsub("-", ".", genotypes)
# clone names (reps effectively)
clones <- levels(factor(word(samples$sample, 3, sep="_")))
###########################
#### DATA GATHER LOOP #####
###########################
#initialise empty data table
data <- NULL
# loop over directories and files to gather lots of files
for (i in 1:nrow(samples)){ # for samples loop
sampleFile = samples[i,2]
# get the variable for which sample
thisSample <- samples[i,1]
# load the data and add a column for the sample
interactions <-
data.table::fread(sampleFile, fill = TRUE) %>%
mutate(sample=thisSample)
# combine all in one df
data <- rbind(data,interactions)
} # for samples loop
colnames(data) <- c("baitID", "preyID", "reads", "sampleName")
cat("Total Usable Reporters:", sum(data$reads), "\n")
#######################
### DATA WRANGLING ###
#######################
# assign individual dfs for each sample
for (n in 1:nrow(samples)){
name=samples[n,1]
x <-
data %>%
filter(sampleName==name) %>%
mutate(combo=paste0(baitID,"-",preyID)) %>%
dplyr::select(-baitID,-preyID)
assign(paste0("df", n), x)
}
# now do a full join by bait and prey IDs combo
# so that any sample with a read for that bait-prey combo
# will be assigned to it in each row
union <-
df1 %>%
full_join(df2, by = "combo") %>%
full_join(df3, by = "combo") %>%
full_join(df4, by = "combo") %>%
full_join(df5, by = "combo") %>%
full_join(df6, by = "combo") %>%
full_join(df7, by = "combo") %>%
full_join(df8, by = "combo") %>%
full_join(df9, by = "combo") %>%
full_join(df10, by = "combo") %>%
full_join(df11, by = "combo") %>%
full_join(df12, by = "combo") %>%
full_join(df13, by = "combo") %>%
full_join(df14, by = "combo") %>%
full_join(df15, by = "combo") %>%
full_join(df16, by = "combo") %>%
full_join(df17, by = "combo") %>%
full_join(df18, by = "combo") %>%
full_join(df19, by = "combo") %>%
full_join(df20, by = "combo") %>%
full_join(df21, by = "combo") %>%
full_join(df22, by = "combo") %>%
full_join(df23, by = "combo") %>%
full_join(df24, by = "combo") %>%
full_join(df25, by = "combo") %>%
full_join(df26, by = "combo") %>%
full_join(df27, by = "combo") %>%
full_join(df28, by = "combo") %>%
full_join(df29, by = "combo") %>%
full_join(df30, by = "combo") %>%
full_join(df31, by = "combo") %>%
full_join(df32, by = "combo") %>%
full_join(df33, by = "combo")
# keep only the reads
union <-
union %>%
dplyr::select(-starts_with("sampleName")) %>%
dplyr::select(combo,starts_with("reads"))
# convert NAs to 0
union[is.na(union)] <- 0
# make sample names not a factor
samples$sample <- as.character(samples$sample)
# give proper names to the read columns
colnames(union) <- c("baitID-preyID",
samples[1,1],
samples[2,1],
samples[3,1],
samples[4,1],
samples[5,1],
samples[6,1],
samples[7,1],
samples[8,1],
samples[9,1],
samples[10,1],
samples[11,1],
samples[12,1],
samples[13,1],
samples[14,1],
samples[15,1],
samples[16,1],
samples[17,1],
samples[18,1],
samples[19,1],
samples[20,1],
samples[21,1],
samples[22,1],
samples[23,1],
samples[24,1],
samples[25,1],
samples[26,1],
samples[27,1],
samples[28,1],
samples[29,1],
samples[30,1],
samples[31,1],
samples[32,1],
samples[33,1])
## split up the bait and prey IDs
union <- separate(union, `baitID-preyID`, into=c("baitID","preyID"), sep = "-", remove = TRUE)
# make into integers to allow joining
union$baitID <- as.integer(union$baitID)
union$preyID <- as.integer(union$preyID)
fragData$baitID <- as.integer(fragData$baitID)
fragData$preyID <- as.integer(fragData$preyID)
## this variable is then taken into normalisation, or DESeq, for all BAITs
DESeqData <-
union %>%
mutate(combo=paste0(baitID,"-",preyID)) %>%
dplyr::select(-baitID,-preyID) %>%
tibble::column_to_rownames("combo")
## DESeqData, POSSIBLY USEFUL so saving
# save this table for use later
saveRDS(DESeqData, paste0(variablesFolder, "/DESeqData_", matrixType, ".rds"))
write.table(DESeqData, file=paste0(variablesFolder, "/DESeqData_", matrixType, ".txt"),
col.names = T,
row.names = F,
quote=F,
sep="\t")
##############
### DESeq ###
##############
## I am performing DEseq on ALL the interactions together, not just bait by bait!!!
cat("Moving onto statistical testing...", "\n")
## load data cleaned above
DESeqData <- readRDS(paste0(variablesFolder, "/DESeqData_", matrixType, ".rds"))
# found better clustering (PCA) with filtered DESeqData
# also much faster calculations at each step with filtered DESeqData
## fitType=local on filtered data gives most diff int frags
# filter DESeqData on greater than 1 read count
filtered <- DESeqData[!rowSums(DESeqData == 0) >= 1,]
# clean up colnames
colnames(filtered) <- gsub("-", ".", colnames(filtered))
## get just the wt samples before doing DESeq
filtered %<>%
dplyr::select(matches("WT"))
#### SIMPLE ONE VARIABLE TESTS ####
## working 24th March 2020
# good between cell types (within genotype) diff Int Frags
# not so good between genotypes (within cell types)
### setting up variables used to define the DESeq comparison
cond_df <-
data.frame(conditions=colnames(filtered)) %>%
separate(conditions, into=c("cellType","genoType",NA,"XP"),sep="_") %>%
mutate(combined=paste0(cellType,"_",genoType))
# set up the condition factor variable used in DESeq
condition = factor(cond_df$combined)
# set up the colData for DESeq
colData = data.frame(row.names = colnames(filtered),
combined = condition,
libType = "Tiled CapC")
# do the DESeq calculations
dds <- DESeqDataSetFromMatrix(countData = filtered,
colData = colData,
design = ~ combined)
dds <- DESeq(dds, fitType = "local")
# save this variable for use later
#saveRDS(dds, paste0(variablesFolder, "/Combined_filtered_local_dds.rds"))
## to load it back
#dds <- readRDS(paste0(variablesFolder, "/Combined_filtered_local_dds.rds"))
# use a custom function to plot PCA
plotting_QC(dds,"~combined_filtered_local","combined",condition,outDirPCAb)
#### some more checking
#plotDispEsts(dds)
#### PAIRWISE TESTS ####
# set up a table of conditions
tests <-
data.frame(test=levels(factor(cond_df$combined))) %>%
mutate(test2=test) %>%
separate(test2,into=c("Tissues","Genotypes"),sep="_")
#### testing within each tissue but between genotypes
for (tissue in tissues){
thisTest <-
tests %>%
filter(Tissues==tissue)
P1 <- as.character(thisTest$test[1])
P2 <- as.character(thisTest$test[2])
WT <- as.character(thisTest$test[3])
# test P1 vs P2
pairTestDESeq(dds,"~combined_filtered_local","combined",P1,P2,FDRLevel,outDirData,outDirMA)
# test P1 vs WT
pairTestDESeq(dds,"~combined_filtered_local","combined",P1,WT,FDRLevel,outDirData,outDirMA)
# test P2 vs WT
pairTestDESeq(dds,"~combined_filtered_local","combined",P2,WT,FDRLevel,outDirData,outDirMA)
}
#### testing within each genotype but between tissues
for (genotype in genotypes){
thisTest <-
tests %>%
filter(Genotypes==genotype)
CD41 <- as.character(thisTest$test[1])
Flk1 <- as.character(thisTest$test[2])
UN <- as.character(thisTest$test[3])
# test P1 vs P2
pairTestDESeq(dds,"~combined_filtered_local","combined",CD41,Flk1,FDRLevel,outDirData,outDirMA)
# test P1 vs WT
pairTestDESeq(dds,"~combined_filtered_local","combined",CD41,UN,FDRLevel,outDirData,outDirMA)
# test P2 vs WT
pairTestDESeq(dds,"~combined_filtered_local","combined",Flk1,UN,FDRLevel,outDirData,outDirMA)
}