-
Notifications
You must be signed in to change notification settings - Fork 0
/
FigS21.r
141 lines (119 loc) · 4.9 KB
/
FigS21.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
#### Figure S21
### Noise analysis
### using i vs i+1 dif with and without intraread correlation broken
suppressMessages(library(GenomicRanges))
suppressMessages(library(tidyverse))
library(patchwork)
library(parallel)
RNGkind("L'Ecuyer-CMRG")
theme_set(theme_bw())
mypal <- c(paletteer::paletteer_d("ggthemes::Classic_20"),"grey40","black","gold","greenyellow")
`%+%` <- paste0
setwd("/Users/ll/work/RStudioProjects/NanoTiming")
path2fig <- "Figures_Article/Figures_pdf/"
path2data <- "Figures_Article/Figures_data/"
source("Figures_Article/rescaling_function.r")
ncores <- 8L
## load genomic informations
chrom_order <- c("chrI","chrII","chrIII","chrIV","chrV","chrVI","chrVII","chrVIII","chrIX","chrX","chrXI","chrXII","chrXIII","chrXIV","chrXV","chrXVI")
seqinf <- readRDS("Reference_Genome/seqinfBT1multiUra.rds")
seqlevels(seqinf)=seqlevels(seqinf)[1:16]
chrom_sizes <- as.data.frame(seqinf)
chrom_sizes$chrom <- rownames(chrom_sizes)
chrom_sizes <- as_tibble(chrom_sizes) %>%
select(chrom,seqlengths) %>%
mutate(chrom=factor(chrom,levels=chrom_order))
genome_size <- chrom_sizes %>% pull(seqlengths) %>% sum
### create a background to fill empty bins with NA
bs=1000
bingen <- tileGenome(seqinf,tilewidth=bs, cut.last.tile.in.chrom=T)
backgr <- as_tibble(bingen) %>% dplyr::rename(positions=start,chrom=seqnames)%>% mutate(y=NA) %>% select(chrom,positions,y)
# load data
alldata <- do.call(bind_rows,lapply(1:24, function(i) readRDS(path2data %+% "nanoT_WT_24rep"%+%i%+%"_alldata.rds")))
alldata2 <- alldata %>% mutate(readlength=end-start+1)
sum(alldata2$readlength)/genome_size
# [1] 3367.668
target_list <- c(1,3,10,20,30,50,70,100,200,300,600,1000,3000)
### generate data while breaking the intrareads correlation
set.seed(123)
bigres2 <- mclapply(1:10, function(i) {
totest <- alldata2 %>%
sample_frac() %>%
mutate(sumcov=cumsum(readlength)/genome_size)
totestb <- alldata2 %>%
sample_frac() %>%
mutate(sumcov=cumsum(readlength)/genome_size)
res <- lapply(target_list, function(target_cov) {
totest2 <- totest %>% filter(sumcov<=target_cov)
totest2b <- totestb %>% filter(sumcov<=target_cov)
nanot <- totest2 %>%
unnest(cols = c(signalr))%>%
filter(signal>0.02)%>%
group_by(chrom,positions)%>%
summarise(mean_br_bin=mean(signal),.groups="drop") %>%
ungroup %>%
mutate(mod=1+myscaling0(mean_br_bin,infq=0.005,supq=0.995)) %>%
select(-mean_br_bin)
nanotb <- totest2b %>%
unnest(cols = c(signalr))%>%
filter(signal>0.02)%>%
group_by(chrom,positions)%>%
summarise(mean_br_bin=mean(signal),.groups="drop") %>%
ungroup %>%
mutate(modb=1+myscaling0(mean_br_bin,infq=0.005,supq=0.995)) %>%
select(-mean_br_bin)
noise_totest <- full_join(nanot,nanotb,by = join_by(chrom, positions)) %>%
full_join(backgr,by = join_by(chrom, positions)) %>%
arrange(chrom,positions) %>%
select(-y) %>%
group_by(chrom) %>%
mutate(dif=mod-lead(modb)) %>%
ungroup %>%
mutate(gen_cov=target_cov)
noisevar <- noise_totest %>% pull(dif) %>% var(na.rm=T)
nbin <- noise_totest %>% reframe(nb=sum(!is.na(dif)))
resu <- tibble(gen_cov=target_cov,noisevar=noisevar,nbin=nbin,i=i)
return(resu)
})
},mc.cores=ncores,mc.set.seed=T)
bigres <- do.call(bind_rows,bigres2)
saveRDS(bigres,file=path2data %+% "Data_FigS21.rds")
#bigres <- readRDS(path2data %+% "Data_FigS21.rds")
### add sortseq/MFAseq data
## load data
SortSeqWT <- readRDS(path2data %+% "sortseq_WT.rds") %>%
mutate(mod=1+myscaling0(timing,infq=0.005,supq=0.995))%>%
mutate(chrom=factor(chrom,levels=chrom_order))
MFAseq <- readRDS(path2data %+% "MFAseq_WT.rds") %>%
mutate(mod=1+myscaling0(ratio,infq=0.005,supq=0.995)) %>%
mutate(chrom=factor(chrom,levels=chrom_order))
## noise
noise_sortseqWT <- SortSeqWT %>% select(-timing) %>%
full_join(backgr,by = join_by(chrom, positions)) %>%
arrange(chrom,positions) %>%
select(-y) %>%
group_by(chrom) %>%
mutate(dif=mod-lead(mod)) %>%
ungroup
noise_MFAseq <- MFAseq %>% select(-ratio) %>%
full_join(backgr,by = join_by(chrom, positions)) %>%
arrange(chrom,positions) %>%
select(-y) %>%
group_by(chrom) %>%
mutate(dif=mod-lead(mod)) %>%
ungroup
noisevarWT <- noise_sortseqWT %>% summarise(var=var(dif,na.rm=T)) %>% pull(var)
noisevarMFA <- noise_MFAseq %>% summarise(var=var(dif,na.rm=T)) %>% pull(var)
text_tb <- tibble(labl=factor(c("sort-seq","MFA-seq")),ypos=c(noisevarWT,noisevarMFA),gen_cov=c(1678,8740))
pl <- ggplot(bigres,aes(x=gen_cov,y=noisevar,group=gen_cov,col="Nanotiming")) +
stat_summary(fun=median,geom="line",aes(group=1),linewidth = 0.2)+
geom_boxplot(outlier.shape = NA,show.legend=F,linewidth = 0.2)+
geom_point(data=text_tb,aes(x=gen_cov,y=ypos,col=labl), shape=16,size=1,show.legend=F)+
scale_x_log10()+
xlab("Genome coverage (x)")+
ylab("Noise estimator")+
scale_color_manual("",values=mypal[c(3,1,7)])+
scale_y_log10()+
guides(color = guide_legend(override.aes = list(linewidth = 1)))+
ggtitle("Figure S21")
ggsave(plot=pl,file=path2fig %+% "FigS21.pdf",height=3,width=5)