article_figures.Rmd 13.2 KB
Newer Older
Marie Bourdon's avatar
Marie Bourdon committed
1
2
3
4
5
6
7
---
title: "article_figures.Rmd"
author: "Marie Bourdon"
date: "July 2021"
output: html_document
---

Marie Bourdon's avatar
Marie Bourdon committed
8
# Goal and raw data
Marie Bourdon's avatar
Marie Bourdon committed
9
10
11

The goal of this script is to produce figure for the stuart package manuscript.

Marie Bourdon's avatar
Marie Bourdon committed
12
This scripts uses data from the package, and other files found in the "files" directory.
Marie Bourdon's avatar
Marie Bourdon committed
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30

This script uses the qtl_plot() ggplot based function to plot QTL mapping results. This function is in the script "QTL_plot.R".

```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
library(tidyverse)
library(magrittr)
library(qtl)
library(cowplot)
library(grid)
library(gridExtra)
library(gridGraphics)

library(stuart)

source("files/QTL_plot.R")
```

Marie Bourdon's avatar
Marie Bourdon committed
31
# Data load and use of stuart functions
Marie Bourdon's avatar
Marie Bourdon committed
32

Marie Bourdon's avatar
Marie Bourdon committed
33
## genos dataframe
Marie Bourdon's avatar
Marie Bourdon committed
34
35
36
37
38
39

Data frame from stuart with genotypes of 176 F2 individuals and parental strains.
```{r genos}
data(genos)
summary(genos)
```
Marie Bourdon's avatar
Marie Bourdon committed
40
## phenos dataframe
Marie Bourdon's avatar
Marie Bourdon committed
41
42
43
44
45
46

Data frame from stuart with phenotypes of 176 F2 individuals for a quantitative trait.
```{r phenos}
data(phenos)
summary(phenos)
```
Marie Bourdon's avatar
Marie Bourdon committed
47
## annotation file
Marie Bourdon's avatar
Marie Bourdon committed
48

49
Annotation file from K.Broman: https://kbroman.org/MUGAarrays/mini_annotations.html
Marie Bourdon's avatar
Marie Bourdon committed
50
51
```{r annot}
annot_mini <- read.csv(url("https://raw.githubusercontent.com/kbroman/MUGAarrays/master/UWisc/mini_uwisc_v2.csv"))
Marie Bourdon's avatar
Marie Bourdon committed
52
summary(annot_mini)
Marie Bourdon's avatar
Marie Bourdon committed
53
```
Marie Bourdon's avatar
Marie Bourdon committed
54
## parental strains genotypes
Marie Bourdon's avatar
Marie Bourdon committed
55
56
57
58
59
60

2 genotypes tables will be used:

* One table with our genotypes of the strains used in the cross: "strains"

* One table with reference genotypes: "strns_ref"
Marie Bourdon's avatar
Marie Bourdon committed
61
62
63

```{r strains}
#our genotypes
Marie Bourdon's avatar
Marie Bourdon committed
64
65
66
67
68
69
70
71
72
73
74
75

#create tibble with individivual names
parental_strains <- tibble::tibble(parent1 = c("StrainsA_1","StrainsA_2"),
                                   parent2 = c("StrainsB_1","StrainsB_2"))


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

#remove genotypes of parental strains from genos data frame
Marie Bourdon's avatar
Marie Bourdon committed
76
77
genos %<>% filter(!Sample.ID %in% c("StrainsA_1","StrainsA_2","StrainsB_1","StrainsB_2"))

Marie Bourdon's avatar
Marie Bourdon committed
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
#summary
summary(strains)
```

```{r strns_ref}
#reference genotypes
#load parental strains genotype data from Neogen
strns_ref <- read.csv("files/ref_genotypes.csv")

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

#summary
summary(strns_ref)
```

Marie Bourdon's avatar
Marie Bourdon committed
94
## Use of stuart's functions
Marie Bourdon's avatar
Marie Bourdon committed
95
96
97
98
99
100
101
```{r}
data(stuart_tab)
summary(stuart_tab)

tab2 <- mark_match(stuart_tab,ref=strns_ref)
tab2 <- mark_poly(tab2)
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))
Marie Bourdon's avatar
Marie Bourdon committed
102
tab2 <- mark_allele(tab2,ref=strns_ref,cross="F2",par1="parent1",par2="parent2")
Marie Bourdon's avatar
Marie Bourdon committed
103

Marie Bourdon's avatar
Marie Bourdon committed
104
data(stuart_newmap)
mariefbourdon's avatar
mariefbourdon committed
105
tab2 <- mark_estmap(tab=tab2,map=stuart_newmap,annot=annot_mini)
Marie Bourdon's avatar
Marie Bourdon committed
106
107
```

Marie Bourdon's avatar
Marie Bourdon committed
108
109
110
111
112
113
114
# Graphs

## Grid mark_prop

### Graph missing genotypes

```{r graph_NA}
mariefbourdon's avatar
mariefbourdon committed
115
na_plot <- tab2 %>% mutate(prop_NA=n_NA/176) %>% ggplot(aes(x=prop_NA)) +
Marie Bourdon's avatar
Marie Bourdon committed
116
117
118
119
120
121
122
123
124
125
126
127
  geom_histogram() +
  scale_y_log10() +
  theme_bw() +
  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
```

mariefbourdon's avatar
mariefbourdon committed
128
129
130
131
132
133
Proportions of markers with more than 75% of missing genotypes:
```{r prop_missing}
tab2 %>% mutate(prop_NA=n_NA/176) %>% filter(prop_NA > 0.75) %>% summarise(p=n()/count(tab2)%>%pull())
```


Marie Bourdon's avatar
Marie Bourdon committed
134
135
136
### Graph proportion of genotypes

```{r graph_prop}
mariefbourdon's avatar
mariefbourdon committed
137
prop_plot <- tab2 %>% filter(n_NA<88) %>% filter(!chr %in% c("M","X","Y")) %>%
mariefbourdon's avatar
mariefbourdon committed
138
  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))) +
Marie Bourdon's avatar
Marie Bourdon committed
139
  geom_point() +
mariefbourdon's avatar
mariefbourdon committed
140
  scale_color_manual(values=c("#66bd63","#b2182b"),labels = c("Retained", "Excluded")) +
Marie Bourdon's avatar
Marie Bourdon committed
141
142
143
144
145
146
  #geom_text(aes(label=ifelse(exclude_prop=="1",SNP.Name,'')),hjust=0, vjust=0,size=2) +
  labs(title="Exclusion of markers with mark_prop()",
       x="Proportion of homozygous individuals AA",
       y="Proportion of homozygous individuals BB",
       color="Exclusion") +
  theme_classic() +
mariefbourdon's avatar
mariefbourdon committed
147
148
149
  theme(aspect.ratio=0.8,
        legend.position=c(0.8,0.8),
        legend.title = element_blank()) +
mariefbourdon's avatar
mariefbourdon committed
150
  theme(plot.title = element_text(hjust = 0.4,face="bold",size=14))
Marie Bourdon's avatar
Marie Bourdon committed
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
prop_plot
```
### Grid

Figure with graphs proportion of NA and exclusion depending on genotype proportions


```{r grid_na_prop}
grid <- plot_grid(na_plot,prop_plot,
                  ncol=1,
                  labels = c('A', 'B'), 
                  label_size = 20,
                  byrow = TRUE
                  )

grid

ggsave("figures/naprop.pdf",grid,width=5,height=8)

rm(na_plot,prop_plot)
```

## Table alleles different between parental strains and F2s

```{r allele}
#investigation of the role of mark_allele function
#prove that some marker with non corresponding alleles between parents and F2

#keep only markers that are exlcuded with mark_allele
allele <- tab2%>% 
  filter(exclude_allele==1&exclude_poly==0&exclude_prop==0)
strains_allele <- strns_ref %>% filter(marker %in% allele$marker)

#join with strains genotypes to have parental strains
allele <- left_join(allele,strains_allele,by=c("marker"="marker"))


#most of markers excluded with mark_allele that were not excluded with other functions have N/H as genotype for parents
#keep only those with non missing/heterozygous genotypes
allele %<>% filter(parent1 != "N" & parent2 != "N")
mariefbourdon's avatar
mariefbourdon committed
191
192
193
194
195
196
allele %<>% select(marker,parent1,parent2,allele_1,allele_2)

#number of markers in such situation
count(tab2%>% 
  filter(exclude_allele==1))

Marie Bourdon's avatar
Marie Bourdon committed
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
#keep only beggining of the table
allele <- allele[1:6,]

print(allele)

print(xtable::xtable(allele, type = "latex"), file = "tables/tab_alleles.tex",include.rownames=FALSE)
rm(allele,strains_allele)
```

## Graph number of markers kept after each function

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

barplot_df <- tibble(
  fct = c("none","match","poly","prop","allele"),
  markers = c(none, match, poly, prop, allele)
)

functions_plot <- barplot_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("allele", "prop", "poly","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))

functions_plot
ggsave("figures/functions.pdf",functions_plot,width=5,height=3)

rm(none,allele,match,poly,prop,barplot_df)
```

## Graph before after

### Before: creation of Rqtl csv file 

```{r cross_before}
# filter at minima: remove non polymorphic and NA>0.5 
tab_before <- mark_poly(stuart_tab)
tab_before <- mark_prop(tab_before,cross="F2",homo=0,hetero=0)

# create rqtl csv file
write_rqtl(geno=genos,pheno=phenos,tab=tab_before,ref=strns_ref,par1="parent1",par2="parent2",prefix="ind_",pos="cM_cox",path="files/cluster/cross_before.csv")
```

### Before: newmap and permutation

These objects were produced on our cluster with the following script: /files/cluster/before_after.R

### Before: plot estimated map

```{r before_map}
# import cross
cross_before <- read.cross(format="csv",file="files/cluster/cross_before.csv",
                              genotypes=c("0","1","2"),na.strings=c("NA"), convertXdata=TRUE)

# load rda with newmap
load("files/cluster/newmap_before.rda")

# plot
plotMap(cross_before,newmap_before,shift=TRUE)
plotmap_before <- ~plotMap(cross_before,newmap_before,shift=TRUE,main="Before stuart")
```

### Before: plot genome scan

```{r before_scan}
# load rda with perm
load("files/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")


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")
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) +
    theme(legend.position = "none") +
    ggtitle("")
pheno_before_plot
mariefbourdon's avatar
mariefbourdon committed
298
299
300
301
302
303
304
305
306
307
308
309

pheno_before_zoom <- 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",
         chrs = "13",
         size=0.6) +
    theme(legend.position = "none") +
    ggtitle("")
pheno_before_zoom

ggsave("figures/zoom_peak_13.pdf",pheno_before_zoom,width=3)
Marie Bourdon's avatar
Marie Bourdon committed
310
311
312
313
314
315
316
317
318
```


### After: creation of Rqtl csv file 

```{r cross_after}
# filter with stuart functions: use the good data for parental strains (strains df)
tab2 <- mark_match(stuart_tab,ref=strains)
tab2 <- mark_poly(tab2)
mariefbourdon's avatar
mariefbourdon committed
319
tab2 <- mark_na(tab2)
Marie Bourdon's avatar
Marie Bourdon committed
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
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="parent1",par2="parent2")


# create rqtl csv file
write_rqtl(geno=genos,pheno=phenos,tab=tab2,ref=strains,par1="parent1",par2="parent2",prefix="ind_",pos="cM_cox",path="files/cluster/cross_after.csv")
```

### After: newmap 

These objects were produced on our cluster with the following script: /files/cluster/before_after.R

### After: plot estimated map

```{r after_map}
# import cross
cross_after <- read.cross(format="csv",file="files/cluster/cross_after.csv",
                              genotypes=c("0","1","2"),na.strings=c("NA"), convertXdata=TRUE)

# load rda with newmap
load("files/cluster/newmap_after.rda")

# plot
plotMap(cross_after,newmap_after,shift=TRUE)
plotmap_after <- ~plotMap(cross_after,newmap_after,shift=TRUE,main="After stuart")
```
### Remove last problematic markers with mark_estmap

```{r after_estmap}
#filter with mark_estmap
350
tab2 <- mark_estmap(tab2,newmap_after,annot_mini)
Marie Bourdon's avatar
Marie Bourdon committed
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

# create rqtl csv file
write_rqtl(geno=genos,pheno=phenos,tab=tab2,ref=strains,par1="parent1",par2="parent2",prefix="ind_",pos="cM_cox",path="files/cluster2/cross_after.csv")
```

### After: plot estimated map 2

```{r after_map2}
# import cross
cross_after <- read.cross(format="csv",file="files/cluster2/cross_after.csv",
                              genotypes=c("0","1","2"),na.strings=c("NA"), convertXdata=TRUE)

# load rda with newmap
load("files/cluster2/newmap_after.rda")

# plot
plotMap(cross_after,newmap_after,shift=TRUE)
plotmap_after <- ~plotMap(cross_after,newmap_after,shift=TRUE,main="After stuart")
```

### After: plot genome scan

```{r after_scan}
# load rda with perm
load("files/cluster2/after_1000p.rda")
# load("files/after_1000p.rda")

# calculate thresholds
threshold_after <- summary(after_1000p,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_after, step=2.0, off.end=0.0, 
                                 error.prob=1.0E-4, map.function="haldane", stepwidth="fixed")


pheno_after <- scanone(cross=cross_after, 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")
summary(pheno_after)

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

```{r grid fig1, fig.height = 7, fig.width = 13, fig.align = "center"}
grid <- plot_grid(as_grob(plotmap_before),as_grob(plotmap_after),pheno_before_plot,pheno_after_plot,ncol=2,labels=c("A","B","C","D"),label_size=20)
grid

ggsave("figures/before_after.pdf",grid,width=11,height = 7)
```

# Differences between real and reference genotypes for parental strains

```{r dif_number}
dif <- full_join(strains,strns_ref,by=c("marker","chr","cM_cox")) %>% 
  mutate(dif=case_when((!parent1.x%in%c("N","H") & 
                          !parent1.y%in%c("N","H") & 
                          parent1.x!=parent1.y) ~ 1,
                       (!parent2.x%in%c("N","H") &
                          !parent2.y%in%c("N","H") & 
                          parent2.x!=parent2.y) ~ 1, 
                       T~0))

dif %>% filter(dif==1) %>% count()
```

```{r dif_table}
table_dif <- dif %>% filter(dif==1) %>% select(marker,parent1_ref=parent1.y,parent1_geno=parent1.x,parent2_ref=parent2.y,parent2_geno=parent2.x) %>% head()
knitr::kable(table_dif)
mariefbourdon's avatar
mariefbourdon committed
427
428
429
430
431
432
433
434
435
436
```

# Pheno data format
```{r pheno}
format_pheno <- phenos[1:6,]
print(xtable::xtable(format_pheno, type = "latex"), file = "tables/tab_alleles.tex",include.rownames=FALSE)


```