-
Notifications
You must be signed in to change notification settings - Fork 0
/
1-2_TileR_2kb_raw_contactNumPerBaitPerSample_200404.R
548 lines (403 loc) · 14.8 KB
/
1-2_TileR_2kb_raw_contactNumPerBaitPerSample_200404.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
545
546
547
548
### TILED ANALYSIS ###
library(tidyverse)
library(DESeq2)
library(RColorBrewer)
library(gplot)
library(ggplot2)
library(stringr)
#### This script is to calculate interaction frequencies per 2kb window and plots them
## all done on RAW CONTACT MATRICES
# working 04 04 2020
###############
### OPTIONS ###
###############
options(max.print=50)
#################
### FUNCTIONS ###
#################
##############
### 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
###### NOTE HERE WE ARE USING ALL THE BAITS IN THE ENTIRE TILED REGION ######
baitFile <- paste0(base, "fragData_2kb.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 ###
###############
## DESeq directories
QC_dir <- paste0(base, "QC/")
dir.create(QC_dir, 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)
baits %<>%
mutate(baitName=V4)
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)
## alternatively moving on for plotting and normalisation #################
# join on bait and prey IDs
# only keeping the frags in format chr:start-stop
union <-
union %>%
full_join(fragData, by = "baitID") %>%
dplyr::rename(preyID=preyID.x) %>%
full_join(fragData, by = "preyID") %>%
dplyr::select(1,36:38,2,41:43,3:35) %>%
mutate(#"comboID"=paste0(baitID.x,"-",preyID),
"bait_frag"=paste0(prey_chr.x,":",prey_start.x,"-",prey_end.x),
"prey_frag"=paste0(prey_chr.y,":",prey_start.y,"-",prey_end.y)) %>%
dplyr::rename(baitID=baitID.x) %>%
dplyr::select(-matches("\\.x"),-matches("\\.y")) %>%
dplyr::select(baitID,preyID,bait_frag,prey_frag,3:35)
#union2=union
##########################
### BAIT SPECIFIC PART ###
##########################
# calcuating RAW interaction count per bait per replicate
# initialise empty variables for calculating interaction number per bait
# for raw and scaled normalised to read count
intNum <- data.frame()
normIntNum <- data.frame()
for (i in 1:nrow(baits)){ # working on each bait at a time
# get the bait_frag, baitName, and baitChrom we are working on
baitUsed = baits$bait_frag[i]
baitName = baits$baitName[i]
baitNum = baits$baitID[i]
#baitChrom =
# baits[i,] %>%
# separate(bait_frag, into=c("baitChrom", NA), sep=":", remove=T) %>%
# dplyr::select(baitChrom) %>%
# as.character()
cat("Now analysing bait", baits$baitName[i], "Tile-C version", "\n")
# filter the union table on just this bait
# note I am actually not really using the concept of bait and prey
# just looking for any fragment that interacts with this viewpoint!!
# grep on the combo of "baitID-preyID" wasn't working as grep(187) found 1871 etc
# also need to get a fragID column to plot the data over
# also excluding the baited bin
countData <-
union %>%
filter(baitNum==baitID | baitNum==preyID) %>%
mutate(fragID=((baitID+preyID)-baitNum)) %>%
filter(fragID!=baitNum) %>%
dplyr::select(-baitID,-preyID,-bait_frag,-prey_frag) %>%
tibble::column_to_rownames(var = "fragID")
#####################
### NORMALISATION ###
#####################
# make all columns of countData numeric to allow summing
for (i in 1:length(countData)){
countData[,i] <- as.numeric(countData[,i])
}
# exact normalisation method from CC stats pipeline
data.matrix <- data.matrix(countData)
column.totals <- apply(countData, 2, sum)
data.norm <- t(t(data.matrix) * 5e3 / column.totals)
column.totals.norm <- apply(data.norm, 2, sum)
# add the column totals to intNum
intNum <- rbind.data.frame(intNum,column.totals)
normIntNum <- rbind.data.frame(normIntNum,column.totals.norm)
}
#######################################
### EXPLORING INTERACTIONS PER BAIT ###
#######################################
# rename the outputted tables columns
colnames(intNum) <- samples$sample
colnames(normIntNum) <- samples$sample
# export wide format table
write.table(intNum,
file=paste0(QC_dir,"Tiled_2kb_raw_intNumPer2kbFragPerSamples_wide.txt"),
quote=F,
col.names=T,
row.names=F)
# make into long format
long <- gather(data = intNum,
key = "sample",
value = "contacts",
1:33,
factor_key = T)
# get the cellType_genoType group
long <-
long %>%
mutate(sample2=sample) %>%
separate(sample2, into=c("cellType", "genoType","clone","XP", "ID"),sep="_") %>%
mutate(genoType=gsub("-",".",genoType)) %>%
mutate(group=paste0(cellType,"_",genoType))
# convert to factor
long$group <- factor(long$group)
# get the numbers in each library
meanCont <- long %>%
group_by(sample) %>%
summarise(mean(contacts))
sdCont <- long %>%
group_by(sample) %>%
summarise(sd(contacts))
minCont <- long %>%
group_by(sample) %>%
summarise(min(contacts))
maxCont <- long %>%
group_by(sample) %>%
summarise(max(contacts))
# get the numbers in each merged group (cellType_genoType)
sumContCell <- long %>%
group_by(group) %>%
summarise(sum(contacts))
meanContCell <- long %>%
group_by(group) %>%
summarise(mean(contacts))
sdContCell <- long %>%
group_by(group) %>%
summarise(sd(contacts))
# get the numbers across all libraries
meanContAll <- long %>%
summarise(mean(contacts))
sdContAll <- long %>%
summarise(sd(contacts))
## quick plotting
box <-
ggplot(long, aes(x=sample, y=contacts, fill=cellType,colour=genoType)) +
geom_boxplot() +
scale_color_brewer(palette="Dark2")+
scale_fill_brewer(palette="Blues")+
xlab(label="Tiled-C Sample") +
ylab(label="Reporter counts per bin")+
ggtitle("Reporter counts per bin by Tiled-C library")+
theme_bw()+
theme(axis.text.x = element_text(angle = 90, hjust = 1,vjust=0.5,size=8))
ggsave(plot=box,
filename=paste0(QC_dir,"Average_Tiled-C_repCountsPerBin_2kb_raw_indiReps.pdf"),
device="pdf",
width=6,
height=5)
## Taking info for cis trans ratios from Jelenas pipeline in this file::::: COMBINED_allFinalCounts_table.txt
# used the following commands to automatically pull the data together and load it here
#cd /t1-home/molhaem6/dowens/bioinformatics16/CapC/Tiled/CTCF-KO/pipe
#mkdir cisTransRations
#tail -n +2 */D_analyseCapturesiteWise/COMBINED_allFinalCounts_table.txt | grep Runx1_mouse > table.txt
#ls -d * | grep ID > samples.txt
#paste samples.txt table.txt | column -s $'\t' -t > ./cisTransRations/allFinalCounts_table.txt
pipeData <- read.delim(file = paste0(QC_dir, "allFinalCounts_table2.txt"),sep=" ")
colnames(pipeData) =c("sample", "capturesite", "RepFragsTotal", "RepFragsCIS", "RepFragsTRANS")
pipeData <-
pipeData %>%
mutate(cisTransPerc=(RepFragsCIS/RepFragsTotal)*100,
sample2=sample) %>%
separate(sample, into=c("cellType", "genoType", "clone", "XP", "ID"),sep="_") %>%
mutate(group=paste0(cellType,"_",genoType))# %>%
#dplyr::select(cisTransPerc,sample,cellType)
# values are only visible as a line on the boxplot so better to show them numerically too
means <- signif(pipeData$cisTransPerc,3)
box3 <- ggplot(pipeData, aes(x=sample2, y=cisTransPerc, fill=cellType, colour=genoType)) +
geom_boxplot() +
scale_color_brewer(palette="Dark2")+
scale_fill_brewer(palette="Blues")+
xlab(label="Tiled-C Sample") +
ylab(label="Cis to trans interactions ratio")+
ggtitle("Cis to trans interactions ratio by Tiled-C library")+
#geom_text(aes(label = means, y = cisTransPerc -2), angle=90, color="black", size=3) +
scale_y_continuous(limits = c(95, 100)) +
theme_bw()+
theme(axis.text.x = element_text(angle = 90, hjust = 1,vjust=0.5,size=8))
ggsave(plot=box3,
filename=paste0(QC_dir,"Tiled-C_cis_to_trans_ratios_2kb_raw_indiReps.pdf"),
device="pdf",
width=6,
height=5)
# merge the replicates
box4 <- ggplot(pipeData, aes(x=sample2, y=cisTransPerc, fill=cellType, group=group, colour=genoType)) +
geom_boxplot() +
scale_color_brewer(palette="Dark2")+
scale_fill_brewer(palette="Blues")+
xlab(label="Tiled-C Sample") +
ylab(label="Cis to trans interactions ratio")+
ggtitle("Cis to trans interactions ratio by Tiled-C library")+
#geom_text(aes(label = means, y = cisTransPerc -5), angle=90, color="black", size=3) +
scale_y_continuous(limits = c(95, 100)) +
theme_bw()+
theme(axis.text.x = element_text(angle = 90, hjust = 1,vjust=0.5,size=8))
ggsave(plot=box4,
filename=paste0(QC_dir,"Tiled-C_cis_to_trans_ratios_2kb_raw_merged.pdf"),
device="pdf",
width=4.5,
height=5)