data3.Rmd 21.1 KB
Newer Older
mariefbourdon's avatar
mariefbourdon committed
1
2
3
---
title: "data3.Rmd"
author: "Marie Bourdon"
mariefbourdon's avatar
mariefbourdon committed
4
date: '2022-06-08'
mariefbourdon's avatar
mariefbourdon committed
5
6
7
8
9
10
11
12
13
14
output: html_document
---

```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
library(stuart)
library(magrittr)
library(readr)
library(stringr)
library(qtl)
mariefbourdon's avatar
mariefbourdon committed
15
source("../files/QTL_plot.R")
mariefbourdon's avatar
mariefbourdon committed
16
17
18
19
```

## Load
```{r}
mariefbourdon's avatar
mariefbourdon committed
20
genos <- read_csv("genos_data3.csv",show_col_types = FALSE) #genotypes of F2s
mariefbourdon's avatar
mariefbourdon committed
21
annot_mini <- read.csv(url("https://raw.githubusercontent.com/kbroman/MUGAarrays/master/UWisc/mini_uwisc_v2.csv")) #annotation file for miniMUGA
mariefbourdon's avatar
mariefbourdon committed
22
23
phenos <- read_csv("phenos_data3.csv",show_col_types = FALSE) #phenotypes of F2s
parents <- read_csv("parents_data3.csv",show_col_types = FALSE) #genotypes of parental strains (genotyped with F2s)
mariefbourdon's avatar
mariefbourdon committed
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
strns_ref <- read_csv("ref_geno_data3.csv",show_col_types = FALSE) #reference genotypes of parental strains
```

```{r}
tab <- tab_mark(genos,annot_mini,"cM_cox")
```

## Before: creation of Rqtl csv file 

```{r cross_before}
# filter at minima: remove non polymorphic and NA>0.5 
tab_before <- mark_na(tab)
tab_before <- mark_poly(tab_before)

#join with annotation file from miniMUGA
strns_ref <- strns_ref %>% select(name,StrainA,StrainB) %>% right_join(annot_mini,.,by=c("marker"="name")) %>% select(c(marker,chr,cM_cox,StrainA,StrainB))

# create rqtl csv file
mariefbourdon's avatar
mariefbourdon committed
42
write_rqtl(geno=genos,pheno=phenos,tab=tab_before,ref=strns_ref,par1="StrainA",par2="StrainB",prefix=" ",pos="cM_cox",path="cluster/cross_before.csv")
mariefbourdon's avatar
mariefbourdon committed
43
44
45

# import cross
cross_before <- read.cross(format="csv",file="cluster/cross_before.csv",
mariefbourdon's avatar
mariefbourdon committed
46
                              genotypes=c("0","1","2"),na.strings=c("NA"), convertXdata=TRUE)
mariefbourdon's avatar
mariefbourdon committed
47
48


mariefbourdon's avatar
mariefbourdon committed
49
50
load("cluster/newmap_before.rda")
plotMap(cross_before,newmap_before,shift=TRUE)
mariefbourdon's avatar
mariefbourdon committed
51
52
53

cross_before_data3 <- cross_before
newmap_before_data3 <- newmap_before
mariefbourdon's avatar
mariefbourdon committed
54
plotmap_before_data3 <- ~plotMap(cross_before_data3,newmap_before_data3,shift=TRUE,main="", ylab='')
mariefbourdon's avatar
mariefbourdon committed
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
```

### Before: plot genome scan

```{r before_scan}
# load rda with perm
load("cluster/before_1000p.rda")
# load("files/before_1000p.rda")


# calculate thresholds
threshold_before <- summary(before_1000p,alpha=c(0.05,0.1,0.63)) #donne lod score pour risque 0.05, 0.1, 0.63

# scanone
cross_before <- calc.genoprob(cross_before, step=2.0, off.end=0.0,
                                 error.prob=1.0E-4, map.function="haldane", stepwidth="fixed")


mariefbourdon's avatar
mariefbourdon committed
73
pheno_before <- scanone(cross=cross_before, chr=c("1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12", "13", "14", "15", "16", "17", "18", "19", "X", "Y"), pheno.col="Pheno", model="normal", method="em")
mariefbourdon's avatar
mariefbourdon committed
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
summary(pheno_before)

# Plot
pheno_before_plot <- qtl_plot(pheno_before,lod=data.frame(group = c("alpha=0.05", "alpha=0.1","alpha=0.63"),
                 lod = threshold_before[1:3]),
         ylab="LOD score",
         title="QTL mapping",
         x.label = element_blank(),
         size=0.6,
         strip.pos="bottom") +
    theme(legend.position = "none") +
    ggtitle("")
pheno_before_plot
```

mariefbourdon's avatar
mariefbourdon committed
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
### Distribution of calculated distance between markers

```{r}
## BEFORE
#initialize variables
mark <- c()
chr <- c()
pos <- c()
place <- c()
previous <- c()
follow <- c()
kn_previous <- c()
kn_follow <- c()

#get information in newmap
for(i in names(newmap_before)){
  mark <- c(mark,names(newmap_before[[i]]))
  chr <- c(chr,rep(i,times=length(newmap_before[[i]])))
  pos <- c(pos,unname(newmap_before[[i]]))
  place <- c(place,"first",rep("middle",times=(length(newmap_before[[i]])-2)),"last")
  prev <- c(NA,unname(newmap_before[[i]])[1:length(newmap_before[[i]])-1])
  previous <- c(previous,prev)
  fol <- c(unname(newmap_before[[i]])[2:length(newmap_before[[i]])],NA)
  follow <- c(follow,fol)
}

annot <- annot_mini %>% filter(marker %in% mark)
kn_pos <- annot$cM_cox
kn_prev <- c(NA, annot[1:(nrow(annot) - 1), "cM_cox"])
kn_previous <- c(kn_previous, kn_prev)
kn_fol <- c(annot[2:nrow(annot), "cM_cox"], NA)
kn_follow <- c(kn_follow, kn_fol)

#create tab with positions
rec_ratios <- tibble(marker = mark,
                  chr = chr,
                  place = place,
                  pos = pos,
                  previous = previous,
                  prev_dif = pos-previous,
                  kn_pos = kn_pos,
                  kn_previous = kn_previous,
                  kn_prev_dif = kn_pos - kn_previous)

rec_ratios <- rec_ratios %>% mutate(kn_prev_dif = case_when(is.na(previous) == TRUE ~ NA_real_, T ~ kn_prev_dif))


rec_ratios <- rec_ratios %>% mutate(rat_prev = prev_dif/kn_prev_dif)


#remove if dist < 1cM
rec_ratios %<>% filter(!prev_dif<1 & !kn_prev_dif<1) 

#mean sd
rec_ratios %>% summarise(mean=mean(rat_prev,na.rm=TRUE),
                       sd=sd(rat_prev,na.rm=TRUE),
                       max=max(rat_prev,na.rm=TRUE)) 


rec_ratios_before_data3 <- rec_ratios %>% ggplot(aes(x=rat_prev)) +
  geom_histogram(binwidth = .1,alpha=0.4, position="identity",fill="#990000") +
mariefbourdon's avatar
mariefbourdon committed
150
  scale_x_log10(limits=c(0.2,1000)) +
mariefbourdon's avatar
mariefbourdon committed
151
152
  labs(x="Ratio between the calculated and the known distances",
       y="Marker pairs",
mariefbourdon's avatar
mariefbourdon committed
153
       fill="",
mariefbourdon's avatar
mariefbourdon committed
154
       title="Distances between adjacent markers") +
mariefbourdon's avatar
mariefbourdon committed
155
  geom_vline(xintercept = 5,linetype="dashed") +
mariefbourdon's avatar
mariefbourdon committed
156
157
  ggpubr::theme_classic2() +
  theme(plot.title = element_text(hjust=0.5,size=14)) 
mariefbourdon's avatar
mariefbourdon committed
158
159
160

rec_ratios_before_data3

mariefbourdon's avatar
mariefbourdon committed
161
162
163
164
165
#percentage of markers in each group
rec_ratios %>% mutate(group_rat=case_when(rat_prev<5 ~ 0,
                                          rat_prev >=5 ~ 1)) %>%
  group_by(group_rat) %>% summarise(n=n(),p=n()/nrow(rec_ratios))

mariefbourdon's avatar
mariefbourdon committed
166
167
168
rm(mark,chr,pos,place,previous,follow,kn_previous,kn_follow,fol,kn_fol,kn_pos,kn_prev,i,prev)
```

mariefbourdon's avatar
mariefbourdon committed
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
## other peaks

chr1 : max lod 8.054 on pseudomarker next to marker with non mendelian proportions

```{r}
qtl_plot(pheno_before,lod=data.frame(group = c("alpha=0.05", "alpha=0.1","alpha=0.63"),
                 lod = threshold_before[1:3]),
         ylab="LOD score",
         chrs = "1",
         size=0.6,
         rug = TRUE) +
    theme(legend.position = "none",
          strip.background = element_blank(),
          strip.text.x = element_blank()) +
    theme(plot.title = element_text(face="plain",size=14)) 
```
chr13 : max lod 8.236 on pseudomarker and its adjacent marker with non mendelian proportions

```{r}
qtl_plot(pheno_before,lod=data.frame(group = c("alpha=0.05", "alpha=0.1","alpha=0.63"),
                 lod = threshold_before[1:3]),
         ylab="LOD score",
         chrs = "13",
         size=0.6,
         rug = TRUE) +
    theme(legend.position = "none",
          strip.background = element_blank(),
          strip.text.x = element_blank()) +
    theme(plot.title = element_text(face="plain",size=14)) 
```
mariefbourdon's avatar
mariefbourdon committed
199
200
201
202
203

```{r}
#our genotypes

#create tibble with individivual names
mariefbourdon's avatar
mariefbourdon committed
204
parental_strains <- tibble::tibble(StrainA = "StrainA",
mariefbourdon's avatar
mariefbourdon committed
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
                                   StrainB = c("StrainB_1","StrainB_2"))


#create data frame with geno_strains
strains <- geno_strains(annot=annot_mini,geno=parents,
                        strn=parental_strains,cols=c("chr","cM_cox"))
rm(parental_strains)

#summary
summary(strains)
```

## Use of stuart's functions

```{r}
tab2 <- mark_match(tab,ref=strains)
tab2 <- mark_poly(tab2)
tab2 <- mark_na(tab2)
mariefbourdon's avatar
mariefbourdon committed
223
224
tab2 <- mark_prop(tab2,cross="F2",homo=0.1,hetero=0.1,homo1X=c(0.1,1),homo2X=c(0.1,1),heteroX=c(0.1,1))
tab2 <- mark_allele(tab2,ref=strains,cross="F2",par1="StrainA",par2="StrainB")
mariefbourdon's avatar
mariefbourdon committed
225
226
227
228
229
230
231
232
233
234
```

### estmap

```{r}
# create rqtl csv file
write_rqtl(geno=genos,pheno=phenos,tab=tab2,ref=strains,par1="StrainA",par2="StrainB",prefix=" ",pos="cM_cox",path="cluster/cross_after.csv")

# import cross
cross_after <- read.cross(format="csv",file="cluster/cross_after.csv",
mariefbourdon's avatar
mariefbourdon committed
235
                              genotypes=c("0","1","2"),na.strings=c("NA"), convertXdata=TRUE)
mariefbourdon's avatar
mariefbourdon committed
236

mariefbourdon's avatar
mariefbourdon committed
237

mariefbourdon's avatar
mariefbourdon committed
238
load("cluster/newmap_after.rda")
mariefbourdon's avatar
mariefbourdon committed
239
240
241
242
243
244
245
plotMap(cross_after,newmap_after,shift=TRUE)

tab2 <- mark_estmap(tab=tab2,map=newmap_after,annot=annot_mini)

# create new rqtl csv file
write_rqtl(geno=genos,pheno=phenos,tab=tab2,ref=strains,par1="StrainA",par2="StrainB",prefix="F2-",pos="cM_cox",path="cluster2/cross_after2.csv")
```
mariefbourdon's avatar
mariefbourdon committed
246
247


mariefbourdon's avatar
mariefbourdon committed
248
249
250
251
### After: plot estimated map 2

```{r after_map2}
# import cross
mariefbourdon's avatar
data3-4    
mariefbourdon committed
252
cross_after2 <- read.cross(format="csv",file="cluster2/cross_after2.csv",
mariefbourdon's avatar
mariefbourdon committed
253
                              genotypes=c("0","1","2"),na.strings=c("NA"), convertXdata=TRUE)
mariefbourdon's avatar
mariefbourdon committed
254

mariefbourdon's avatar
mariefbourdon committed
255
256
257
258
cross_after2 <- calc.genoprob(cross_after2, step=2.0, off.end=0.0, 
                             error.prob=1.0E-4, map.function="haldane", stepwidth="fixed")

newmap_after2 <- est.map(cross=cross_after2,error.prob=0.01)
mariefbourdon's avatar
mariefbourdon committed
259
260
261

# plot
plotMap(cross_after2,newmap_after2,shift=TRUE)
mariefbourdon's avatar
mariefbourdon committed
262
263
264
265


cross_after_data3 <- cross_after2
newmap_after_data3 <- newmap_after2
mariefbourdon's avatar
mariefbourdon committed
266
plotmap_after_data3 <- ~plotMap(cross_after_data3,newmap_after_data3,shift=TRUE,main="")
mariefbourdon's avatar
mariefbourdon committed
267
268
```

mariefbourdon's avatar
mariefbourdon committed
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
### map length

```{r}
#calc map
length_calc <- newmap_after2[["1"]][[length(newmap_after2[["1"]])]] + newmap_after2[["2"]][[length(newmap_after2[["2"]])]] +
  newmap_after2[["3"]][[length(newmap_after2[["3"]])]] + newmap_after2[["4"]][[length(newmap_after2[["4"]])]] +
  newmap_after2[["5"]][[length(newmap_after2[["5"]])]] + newmap_after2[["6"]][[length(newmap_after2[["6"]])]] +
  newmap_after2[["7"]][[length(newmap_after2[["7"]])]] + newmap_after2[["8"]][[length(newmap_after2[["8"]])]] +
  newmap_after2[["9"]][[length(newmap_after2[["9"]])]] + newmap_after2[["10"]][[length(newmap_after2[["10"]])]] +
  newmap_after2[["11"]][[length(newmap_after2[["11"]])]] + newmap_after2[["12"]][[length(newmap_after2[["12"]])]] +
  newmap_after2[["13"]][[length(newmap_after2[["13"]])]] + newmap_after2[["14"]][[length(newmap_after2[["14"]])]] +
  newmap_after2[["15"]][[length(newmap_after2[["15"]])]] + newmap_after2[["16"]][[length(newmap_after2[["16"]])]] +
  newmap_after2[["17"]][[length(newmap_after2[["17"]])]] + newmap_after2[["18"]][[length(newmap_after2[["18"]])]] +
  newmap_after2[["19"]][[length(newmap_after2[["19"]])]] + newmap_after2[["X"]][[length(newmap_after2[["X"]])]]

#known map
lengh_known <- cross_after2[["geno"]][["1"]][["map"]][[length(cross_after2[["geno"]][["1"]][["map"]])]] +
  cross_after2[["geno"]][["2"]][["map"]][[length(cross_after2[["geno"]][["2"]][["map"]])]] +
  cross_after2[["geno"]][["3"]][["map"]][[length(cross_after2[["geno"]][["3"]][["map"]])]] + 
  cross_after2[["geno"]][["4"]][["map"]][[length(cross_after2[["geno"]][["4"]][["map"]])]] +
  cross_after2[["geno"]][["5"]][["map"]][[length(cross_after2[["geno"]][["5"]][["map"]])]] + 
  cross_after2[["geno"]][["6"]][["map"]][[length(cross_after2[["geno"]][["6"]][["map"]])]] +
  cross_after2[["geno"]][["7"]][["map"]][[length(cross_after2[["geno"]][["7"]][["map"]])]] + 
  cross_after2[["geno"]][["8"]][["map"]][[length(cross_after2[["geno"]][["8"]][["map"]])]] +
  cross_after2[["geno"]][["9"]][["map"]][[length(cross_after2[["geno"]][["9"]][["map"]])]] + 
  cross_after2[["geno"]][["10"]][["map"]][[length(cross_after2[["geno"]][["10"]][["map"]])]] +
  cross_after2[["geno"]][["11"]][["map"]][[length(cross_after2[["geno"]][["11"]][["map"]])]] + 
  cross_after2[["geno"]][["12"]][["map"]][[length(cross_after2[["geno"]][["12"]][["map"]])]] +
  cross_after2[["geno"]][["13"]][["map"]][[length(cross_after2[["geno"]][["13"]][["map"]])]] + 
  cross_after2[["geno"]][["14"]][["map"]][[length(cross_after2[["geno"]][["14"]][["map"]])]] +
  cross_after2[["geno"]][["15"]][["map"]][[length(cross_after2[["geno"]][["15"]][["map"]])]] + 
  cross_after2[["geno"]][["16"]][["map"]][[length(cross_after2[["geno"]][["16"]][["map"]])]] +
  cross_after2[["geno"]][["17"]][["map"]][[length(cross_after2[["geno"]][["17"]][["map"]])]] + 
  cross_after2[["geno"]][["18"]][["map"]][[length(cross_after2[["geno"]][["18"]][["map"]])]] +
  cross_after2[["geno"]][["19"]][["map"]][[length(cross_after2[["geno"]][["19"]][["map"]])]] + 
  cross_after2[["geno"]][["X"]][["map"]][[length(cross_after2[["geno"]][["X"]][["map"]])]]

#ratio 
length_rat <- length_calc/lengh_known
length_rat
```

mariefbourdon's avatar
mariefbourdon committed
311
312
```{r after_scan}
# load rda with perm
mariefbourdon's avatar
mariefbourdon committed
313
314
315
after_1000p2 <- scanone(cross=cross_after2, 
                        chr=c("1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12", "13", "14", "15", "16", "17", "18", "19", "X"), 
                        pheno.col="Pheno", model="normal", method="em", n.perm=1000, perm.Xsp=FALSE, verbose=FALSE) 
mariefbourdon's avatar
mariefbourdon committed
316
317
318
319
320
321
322
323
324

# calculate thresholds
threshold_after <- summary(after_1000p2,alpha=c(0.05,0.1,0.63)) #donne lod score pour risque 0.05, 0.1, 0.63

# scanone
cross_after <- calc.genoprob(cross_after2, step=2.0, off.end=0.0, 
                                 error.prob=1.0E-4, map.function="haldane", stepwidth="fixed")


mariefbourdon's avatar
mariefbourdon committed
325
pheno_after <- scanone(cross=cross_after2, chr=c("1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12", "13", "14", "15", "16", "17", "18", "19", "X", "Y"), pheno.col="Pheno", model="normal", method="em")
mariefbourdon's avatar
mariefbourdon committed
326
327
328
summary(pheno_after)

# Plot
mariefbourdon's avatar
mariefbourdon committed
329
pheno_after_plot_data3 <- qtl_plot(pheno_after,lod=data.frame(group = c("alpha=0.05", "alpha=0.1","alpha=0.63"),
mariefbourdon's avatar
mariefbourdon committed
330
331
332
333
                 lod = threshold_after[1:3]),
         ylab="LOD score",
         title="QTL mapping",
         x.label = element_blank(),
mariefbourdon's avatar
mariefbourdon committed
334
335
         size=0.6,
         strip.pos="bottom") +
mariefbourdon's avatar
mariefbourdon committed
336
    theme(legend.position = "none") +
mariefbourdon's avatar
mariefbourdon committed
337
338
    ggtitle("Dataset 3: genome scan") +
   theme(plot.title = element_text(face="plain",size=14,hjust=0.5)) 
mariefbourdon's avatar
mariefbourdon committed
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
pheno_after_plot_data3
```

### Distribution of calculated distance between markers

```{r}
## AFTER
#initialize variables
mark <- c()
chr <- c()
pos <- c()
place <- c()
previous <- c()
follow <- c()
kn_previous <- c()
kn_follow <- c()

#get information in newmap
for(i in names(newmap_after2)){
  mark <- c(mark,names(newmap_after2[[i]]))
  chr <- c(chr,rep(i,times=length(newmap_after2[[i]])))
  pos <- c(pos,unname(newmap_after2[[i]]))
  place <- c(place,"first",rep("middle",times=(length(newmap_after2[[i]])-2)),"last")
  prev <- c(NA,unname(newmap_after2[[i]])[1:length(newmap_after2[[i]])-1])
  previous <- c(previous,prev)
  fol <- c(unname(newmap_after2[[i]])[2:length(newmap_after2[[i]])],NA)
  follow <- c(follow,fol)
}

annot <- annot_mini %>% filter(marker %in% mark)
kn_pos <- annot$cM_cox
kn_prev <- c(NA, annot[1:(nrow(annot) - 1), "cM_cox"])
kn_previous <- c(kn_previous, kn_prev)
kn_fol <- c(annot[2:nrow(annot), "cM_cox"], NA)
kn_follow <- c(kn_follow, kn_fol)

#create tab with positions
rec_ratios <- tibble(marker = mark,
                  chr = chr,
                  place = place,
                  pos = pos,
                  previous = previous,
                  prev_dif = pos-previous,
                  kn_pos = kn_pos,
                  kn_previous = kn_previous,
                  kn_prev_dif = kn_pos - kn_previous)

rec_ratios <- rec_ratios %>% mutate(kn_prev_dif = case_when(is.na(previous) == TRUE ~ NA_real_, T ~ kn_prev_dif))

rec_ratios <- rec_ratios %>% mutate(rat_prev = prev_dif/kn_prev_dif)

#remove if dist < 1cM
rec_ratios %<>% filter(!prev_dif<1 & !kn_prev_dif<1) 

#mean sd
rec_ratios %>% summarise(mean=mean(rat_prev,na.rm=TRUE),
                       sd=sd(rat_prev,na.rm=TRUE)) 


rec_ratios_after_data3 <- rec_ratios %>% ggplot(aes(x=rat_prev)) +
  geom_histogram(binwidth = .1,alpha=0.4, position="identity",fill="#2171b5") +
mariefbourdon's avatar
mariefbourdon committed
400
  scale_x_log10(limits=c(0.2,1000)) +
mariefbourdon's avatar
mariefbourdon committed
401
402
  labs(x="Ratio between the calculated and the known distances",
       y="Marker pairs",
mariefbourdon's avatar
mariefbourdon committed
403
       fill="",
mariefbourdon's avatar
mariefbourdon committed
404
       title="Distances between adjacent markers") +
mariefbourdon's avatar
mariefbourdon committed
405
406
  ggpubr::theme_classic2() +
  theme(plot.title = element_text(hjust=0.5,size=14)) 
mariefbourdon's avatar
mariefbourdon committed
407
408
409
410

rec_ratios_after_data3

rm(mark,chr,pos,place,previous,follow,kn_previous,kn_follow,fol,kn_fol,kn_pos,kn_prev,i,prev)
mariefbourdon's avatar
mariefbourdon committed
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
```

## Number of markers kept after each function

```{r barplot}
none <- tab2 %>% nrow()
match <- tab2 %>% filter(exclude_match==0) %>% nrow()
allele <- tab2 %>% filter(exclude_match==0&exclude_allele==0) %>% nrow()
naf <- tab2 %>% filter(exclude_match==0&exclude_allele==0&exclude_na==0) %>% nrow()
poly <- tab2 %>% filter(exclude_match==0&exclude_allele==0&exclude_na==0&exclude_poly==0) %>% nrow()
prop <- tab2 %>% filter(exclude_match==0&exclude_allele==0&exclude_na==0&exclude_poly==0&exclude_prop==0) %>% nrow()
estmap <- tab2 %>% filter(exclude_match==0&exclude_allele==0&exclude_na==0&exclude_poly==0&exclude_prop==0&exclude_estmap==0) %>% nrow()

functions_df <- tibble(fct=c("none","match","allele","na","poly","prop","estmap"),
                       markers=c(none,match,allele,naf,poly,prop,estmap))

functions_plot <- functions_df %>% ggplot(aes(x=markers,y=fct)) +
  geom_bar(stat="identity",width=0.6) +
  geom_text(aes(label=markers), hjust=1.3, color="white", size=3.5) +
  scale_y_discrete(limits=c("estmap","prop","poly", "na", "allele","match","none")) +
  theme(aspect.ratio=0.7) +
  labs(title="Number of markers kept after each step",
       x="Number of markers",
       y="Function used") +
  theme_classic() +
  theme(plot.title = element_text(hjust = 0.4,face="bold",size=14))
mariefbourdon's avatar
mariefbourdon committed
437

mariefbourdon's avatar
mariefbourdon committed
438
functions_plot
mariefbourdon's avatar
mariefbourdon committed
439
rm(none,allele,match,poly,prop)
mariefbourdon's avatar
mariefbourdon committed
440
441
442
```


mariefbourdon's avatar
mariefbourdon committed
443
444
445
446
447
448
449
## Narrow peaks

```{r before_ann}
chrs <- c("1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12", "13", "14", "15", "16", "17", "18", "19", "X")
ann_dat_text<-data.frame(
    chr=factor(chrs,
               levels=chrs),
mariefbourdon's avatar
mariefbourdon committed
450
451
452
    lod=c(NA,NA,NA,NA,11,NA,NA,NA,NA,NA,NA,NA,NA,NA,NA,NA,NA,NA,NA,NA),
    label=c(NA,NA,NA,NA,"p4",NA,NA,NA,NA,NA,NA,NA,NA,NA,NA,NA,NA,NA,NA,NA),
    x=c(NA,NA,NA,NA,20,NA,NA,NA,NA,NA,NA,NA,NA,NA,NA,NA,NA,NA,NA,NA)
mariefbourdon's avatar
mariefbourdon committed
453
454
455
456
457
458
459
460
461
)

pheno_before_annot_data3 <- pheno_before_plot +  geom_text(
    # the new dataframe for annotating text
    data = ann_dat_text,
    mapping = aes(x = x,
                  y = lod,
                  label = label,
                  color="blue")
mariefbourdon's avatar
mariefbourdon committed
462
  ) +
mariefbourdon's avatar
mariefbourdon committed
463
464
  ggtitle("Dataset 3: genome scan") +
  theme(plot.title = element_text(face="plain",size=14,hjust=0.5)) 
mariefbourdon's avatar
mariefbourdon committed
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
pheno_before_annot_data3
```

### Peak 4

```{r peak4_zoom}
peak4 <- qtl_plot(pheno_before,lod=data.frame(group = c("alpha=0.05", "alpha=0.1","alpha=0.63"),
                 lod = threshold_before[1:3]),
         ylab="LOD score",
         title="QTL mapping",
         x.label = element_blank(),
         size=0.6,
         strip.pos="bottom",
         chr="5",
         rug=TRUE) +
    theme(legend.position = "none",
          strip.background = element_blank(),
          strip.text.x = element_blank()) +
    xlab("Position (cM)") +
    xlim(c(12,32)) +
    ggtitle("")
peak4
```

1 peak on chromosome 5 on 3 pseudomarkers : c5.loc16, c5.loc18, c5.loc20 in a region with very few markers, postionned between S6J050685107 and mUNC050096588.

Here are the infos on genotype counts for these markers:

```{r summary_geno_peak4}
tab_before %>% filter(marker %in% c("S6J050685107","mUNC050096588")) %>% select(marker:n_NA)
```


For mUNC050096588, all individuals heterozygous so this marker should be removed. The proportions for S6J050685107 seem correct.

Graph:

```{r geno_plot_peak4}
phenotypes <- cross_before[["pheno"]]
map <- cross_before[["geno"]][["5"]][["map"]] 
map <- tibble(marker=names(map),pos=map)
genotypes <- cross_before[["geno"]][["5"]][["data"]]
genotypes <- as_tibble(genotypes)
phenogeno <- cbind(phenotypes,genotypes)
phenogeno %<>% pivot_longer(mUNC050013072:gUNC10448854,names_to="marker",values_to="genotype")
pgmap <- full_join(phenogeno,map,by="marker")

geno_plot4 <- pgmap %>% filter(pos > 1 & pos < 30) %>%
  filter(id %in% sample(phenotypes$id,10)) %>%
  ggplot(aes(x=pos,y=as.factor(id))) +
  geom_point(aes(color=as.factor(genotype))) +
  coord_cartesian(ylim = c(1, 10), expand = TRUE, clip = "off") +
  annotate(geom="text",y=-1,size=3,
           x = map %>% filter(pos > 1 & pos < 30) %>% pull(pos),
           label = map %>% filter(pos > 1 & pos < 30) %>% pull(marker),
           angle=90) +
  labs(x="Position (cM)",y="Individual",color="Genotype") +
  theme_bw() +
  theme(plot.margin = unit(c(1, 1, 1, 1), "lines"),
        axis.title.x = element_text(margin = margin(t = 50)))
geno_plot4
```

## Phenotype distributions

```{r pheno_distrib}
pheno_data3 <- phenos %>% ggplot(aes(x=Pheno)) +
  geom_histogram(binwidth=0.2) +
  ggpubr::theme_classic2() +
mariefbourdon's avatar
mariefbourdon committed
534
  labs(y="F2 individuals", x="Quantitative phenotype",title="Dataset 3") +
mariefbourdon's avatar
mariefbourdon committed
535
  theme(plot.title = element_text(hjust=0.5,size=14)) 
mariefbourdon's avatar
mariefbourdon committed
536
537
538
pheno_data3
```

mariefbourdon's avatar
mariefbourdon committed
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
## Missing genotypes

```{r}
na_plot <- tab2 %>% mutate(prop_NA=n_NA/176) %>% ggplot(aes(x=prop_NA)) +
  geom_histogram() +
  scale_y_log10() +
  theme_classic() +
  labs(title="Proportion of missing genotyped",
       x="Proportion of NA",y="Number of markers") +
  theme(
    aspect.ratio=0.8,
    plot.title = element_text(hjust = 0.4,face="bold",size=14))

na_plot
```

## Genotype proportions

```{r}
prop_plot <- tab2 %>% filter(n_NA<88) %>% filter(!chr %in% c("M","X","Y")) %>%
  ggplot(aes(x=n_HM1/(n_HM1+n_HM2+n_HT),y=n_HM2/(n_HM1+n_HM2+n_HT),color=as.factor(exclude_prop))) +
  geom_point() +
  scale_color_manual(values=c("#66bd63","#b2182b"),labels = c("Retained", "Excluded")) +
  geom_hline(yintercept = 0.1,linetype="dashed",size=.3) +
  geom_vline(xintercept = 0.1,linetype="dashed",size=.3) +
  geom_abline(intercept = 0.9, slope=-1,linetype="dashed",size=.3) +
  labs(title="Exclusion of markers with mark_prop()",
       x="Proportion of homozygous individuals AA",
       y="Proportion of homozygous individuals BB",
       color="Exclusion") +
  theme_classic() +
  theme(aspect.ratio=0.8,
        legend.position=c(0.8,0.8),
        legend.title = element_blank()) +
  theme(plot.title = element_text(hjust = 0.4,face="bold",size=14))

prop_plot
```
mariefbourdon's avatar
mariefbourdon committed
577

mariefbourdon's avatar
mariefbourdon committed
578
```{r}
mariefbourdon's avatar
mariefbourdon committed
579
tab2_data3 <- tab2
mariefbourdon's avatar
mariefbourdon committed
580
save(pheno_before_annot_data3,pheno_data3,tab2_data3,
mariefbourdon's avatar
mariefbourdon committed
581
582
583
584
585
     rec_ratios_before_data3,rec_ratios_after_data3,
     plotmap_before_data3,plotmap_after_data3,
     cross_before_data3,newmap_before_data3,cross_after_data3,newmap_after_data3,
     pheno_after_plot_data3,
     file="data3.rda")
mariefbourdon's avatar
mariefbourdon committed
586
rm(tab2_data3)
mariefbourdon's avatar
mariefbourdon committed
587
```
mariefbourdon's avatar
mariefbourdon committed
588