real_samples.Rmd 8.72 KB
Newer Older
Olivier Rue's avatar
Olivier Rue committed
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
---
title: "Real samples analysis"
description: |
  METABARFOOD project.
author: Olivier Rué
date: '`r Sys.Date()`'
output:
  html_document:
    self_contained: false
    number_sections: FALSE
    code_folding: "show"
    toc: true
    toc_depth: 5
    toc_float: true
---

<link href="https://cdnjs.cloudflare.com/ajax/libs/noUiSlider/7.0.10/jquery.nouislider.min.css" rel="stylesheet"/>
<script src="https://cdnjs.cloudflare.com/ajax/libs/noUiSlider/7.0.10/jquery.nouislider.min.js"></script>
<link href="https://cdnjs.cloudflare.com/ajax/libs/selectize.js/0.12.0/css/selectize.bootstrap3.min.css" rel="stylesheet" />
<script src="https://cdnjs.cloudflare.com/ajax/libs/selectize.js/0.12.0/js/standalone/selectize.min.js"></script>

```{r setup, include=FALSE}
knitr::opts_chunk$set(eval=FALSE, echo =T, cache = FALSE, message = FALSE, warning = FALSE, cache.lazy = FALSE, fig.height = 3.5, fig.width = 10.5)
```

```{r, setup1, echo=FALSE, message = FALSE, warning = FALSE, eval=TRUE}
library(DT)
library(ggpubr)
library(kableExtra)
library(seqinr)
library(gridExtra)
library(phyloseq)
library(phyloseq.extended)
library(tidyverse)
library(plotly)
library(qiime2R)
library(tidyverse)
library(ComplexHeatmap)
library(phyloseqCompanion)
library(circlize)
```


```{r datatable global options, echo=FALSE, eval=TRUE}
options(DT.options = list(pageLength = 10,
                         scrollX = TRUE,
                         language = list(search = 'Filter:'),
                         dom = 'Bfrtip',
                         buttons = c('copy', 'csv', 'excel', 'pdf', 'print')))
```



```{bash, eval=F}
# Genologin
cd /work/frogsfungi/FungiPubli/Real_mock/METABARFOOD/REAL/scripts
sh dada2_frogs.sh
```

```{bash, eval=F}
scp orue@genologin.toulouse.inra.fr:/work/frogsfungi/FungiPubli/Real_mock/METABARFOOD/REAL/results/DADA2_FROGS/ITS1/frogs.biom REAL/ITS1.biom
scp orue@genologin.toulouse.inra.fr:/work/frogsfungi/FungiPubli/Real_mock/METABARFOOD/REAL/results/DADA2_FROGS/ITS2/frogs.biom REAL/ITS2.biom
scp orue@genologin.toulouse.inra.fr:/work/frogsfungi/FungiPubli/Real_mock/METABARFOOD/REAL/results/DADA2_FROGS/D1D2/frogs.biom REAL/D1D2.biom
scp orue@genologin.toulouse.inra.fr:/work/frogsfungi/FungiPubli/Real_mock/METABARFOOD/REAL/results/DADA2_FROGS/RPB2/frogs.biom REAL/RPB2.biom

scp orue@genologin.toulouse.inra.fr:/work/frogsfungi/FungiPubli/Real_mock/METABARFOOD/REAL/results/DADA2_FROGS/ITS1/frogs.tsv REAL/ITS1.tsv
scp orue@genologin.toulouse.inra.fr:/work/frogsfungi/FungiPubli/Real_mock/METABARFOOD/REAL/results/DADA2_FROGS/ITS2/frogs.tsv REAL/ITS2.tsv
scp orue@genologin.toulouse.inra.fr:/work/frogsfungi/FungiPubli/Real_mock/METABARFOOD/REAL/results/DADA2_FROGS/D1D2/frogs.tsv REAL/D1D2.tsv
scp orue@genologin.toulouse.inra.fr:/work/frogsfungi/FungiPubli/Real_mock/METABARFOOD/REAL/results/DADA2_FROGS/RPB2/frogs.tsv REAL/RPB2.tsv

scp orue@genologin.toulouse.inra.fr:/work/frogsfungi/FungiPubli/Real_mock/METABARFOOD/REAL/results/DADA2_FROGS/ITS1/multiaff.tsv REAL/ITS1_multiaff.tsv
scp orue@genologin.toulouse.inra.fr:/work/frogsfungi/FungiPubli/Real_mock/METABARFOOD/REAL/results/DADA2_FROGS/ITS2/multiaff.tsv REAL/ITS2_multiaff.tsv
scp orue@genologin.toulouse.inra.fr:/work/frogsfungi/FungiPubli/Real_mock/METABARFOOD/REAL/results/DADA2_FROGS/D1D2/multiaff.tsv REAL/D1D2_multiaff.tsv
scp orue@genologin.toulouse.inra.fr:/work/frogsfungi/FungiPubli/Real_mock/METABARFOOD/REAL/results/DADA2_FROGS/RPB2/multiaff.tsv REAL/RPB2_multiaff.tsv
```

```{r, eval=F}
if (!file.exists("REAL/ITS1.rds")) {
  biomfile <- "REAL/ITS1.biom"
  physeq_its1 <- import_frogs(biomfile, taxMethod = "blast")
  metadata <- read.table("REAL/metadata.tsv", row.names = 1, header = TRUE, sep = "\t", stringsAsFactors = FALSE)
  sample_data(physeq_its1) <- metadata
  sample_data(physeq_its1)$Marker <- "ITS1"
  saveRDS(physeq_its1,"REAL/ITS1.rds")
} else {
  physeq_its1 <- readRDS("REAL/ITS1.rds")
}

if (!file.exists("REAL/ITS2.rds")) {
  biomfile <- "REAL/ITS2.biom"
  physeq_its2 <- import_frogs(biomfile, taxMethod = "blast")
  metadata <- read.table("REAL/metadata.tsv", row.names = 1, header = TRUE, sep = "\t", stringsAsFactors = FALSE)
  sample_data(physeq_its2) <- metadata
  sample_data(physeq_its2)$Marker <- "ITS2"
  saveRDS(physeq_its2,"REAL/ITS2.rds")
} else {
  physeq_its2 <- readRDS("REAL/ITS2.rds")
}

if (!file.exists("REAL/D1D2.rds")) {
  biomfile <- "REAL/D1D2.biom"
  physeq_d1d2 <- import_frogs(biomfile, taxMethod = "blast")
  metadata <- read.table("REAL/metadata.tsv", row.names = 1, header = TRUE, sep = "\t", stringsAsFactors = FALSE)
  sample_data(physeq_d1d2) <- metadata
  sample_data(physeq_d1d2)$Marker <- "D1D2"
  saveRDS(physeq_d1d2,"REAL/D1D2.rds")
} else {
  physeq_d1d2 <- readRDS("REAL/D1D2.rds")
}

if (!file.exists("REAL/RPB2.rds")) {
  biomfile <- "REAL/RPB2.biom"
  physeq_rpb2 <- import_frogs(biomfile, taxMethod = "blast")
  metadata <- read.table("REAL/metadata.tsv", row.names = 1, header = TRUE, sep = "\t", stringsAsFactors = FALSE)
  sample_data(physeq_rpb2) <- metadata
  sample_data(physeq_rpb2)$Marker <- "RPB2"
  saveRDS(physeq_rpb2,"REAL/RPB2.rds")
} else {
  physeq_rpb2 <- readRDS("REAL/RPB2.rds")
}
```


```{r, eval=T}
create_frogs_object <- function(path, marker, type, file, multifile){
  biom <- file.path(path,file)
  physeq <- import_frogs(biom, taxMethod = "blast")
  metadata_file <- file.path(path,"metadata.tsv")
  metadata <- read.table(metadata_file, row.names=1, header=TRUE, sep="\t", stringsAsFactors = FALSE)

  sample_data(physeq) <- metadata
  multiaff_file <- file.path(path,multifile)
  physeq <- update_frogs_physeq_with_multi(physeq, multiaff_file)
  #physeq  = transform_sample_counts(physeq, function(x) x / sum(x) )
  sample_names(physeq) <- glue::glue(paste("{sample_names(physeq)}","DF",marker, sep="_"))
  sample_data(physeq)$method <- "DADA2_FROGS"
  sample_data(physeq)$Type <- type
  sample_data(physeq)$Marker <- marker
  physeq_aglom <- phyloseq.extended::fast_tax_glom(physeq, taxrank = "Species")
  #physeq_aglom <- prune_taxa(tax_table(physeq_aglom)[,7] %in% references, physeq_aglom)
  taxa_names(physeq_aglom) <- cbind(tax_table(physeq_aglom)[,7])
  return(physeq_aglom)
}

update_frogs_physeq_with_multi <- function(physeq, multiaff_file){

  infos <- readr::read_tsv(file = multiaff_file) %>%
    as_tibble() %>%
    distinct(`#observation_name`, .keep_all = T) %>%
    select(`#observation_name`, blast_taxonomy) %>%
    separate(blast_taxonomy, ";(?=[\\S])", into = c("Kingdom","Phylum","Class","Order","Family","Genus","Species")) %>%
    #mutate(across(Kingdom:Species, ~ stringr::str_remove(.x, ".:"))) %>%
    tibble::column_to_rownames(var = "#observation_name") %>%
    as("matrix")

  good <- tax_table(subset_taxa(physeq, Species != "Multi-affiliation" & Genus != "Multi-affiliation" & Family != "Multi-affiliation" & Order != "Multi-affiliation" & Class != "Multi-affiliation" & Phylum != "Multi-affiliation" & Kingdom != "Multi-affiliation" )) %>% as("matrix")
  #na <- tax_table(subset_taxa(physeq, is.na(Species))) %>%  as("matrix")
  all_infos <- unique(rbind(infos,good))#,na) # case where two species are identical but are multi-affiliated
  tax_table(physeq) <- all_infos

  return(physeq)
}


keep_only_interest_species <- function(physeq, true_species){
   tax_tab <- tax_table(physeq) %>% as("matrix")
   tax_tab <- cbind(tax_tab[ , 1:7],
                    Status = ifelse(tax_tab[ , "Species"] %in% true_species, "true species", "artefact"))
   tax_table(physeq) <- tax_tab
   return(physeq)
}
```


```{r, eval=T}
rpb2 <- create_frogs_object("REAL","RPB2", "DNA", "RPB2.biom", "RPB2_multiaff.tsv")
tax_table(rpb2)[, colnames(tax_table(rpb2))] <- gsub(tax_table(rpb2)[, colnames(tax_table(rpb2))],     pattern = "[a-z]:", replacement = "")
d1d2 <- create_frogs_object("REAL","D1D2", "DNA", "D1D2.biom", "D1D2_multiaff.tsv")
tax_table(d1d2)[, colnames(tax_table(d1d2))] <- gsub(tax_table(d1d2)[, colnames(tax_table(d1d2))],     pattern = "[a-z]:", replacement = "")
its1 <- create_frogs_object("REAL","ITS1", "DNA", "ITS1.biom", "ITS1_multiaff.tsv")
its2 <- create_frogs_object("REAL","ITS2", "DNA", "ITS2.biom", "ITS2_multiaff.tsv")

all <- merge_phyloseq(its1, its2, d1d2, rpb2)

sample_data(all)$Marker <- factor(sample_data(all)$Marker, levels=c("ITS1","ITS2","D1D2","RPB2"))
Method <- get_variable(all, "method")

saveRDS(all, "REAL/real_all.rds")



```


```{r, eval=T}
p <- plot_bar(all)
p + facet_grid(". ~ Marker", scales = "free_x", space = "free")
```

```{r, eval=T}
p <- plot_composition(physeq = all, taxaRank1 = "Kingdom", taxaSet1 = "Fungi", taxaRank2 = "Genus", numberOfTaxa = 22, x = "Sample")
p + facet_grid(". ~ Marker", scales = "free_x", space = "free")
```

```{r, eval=T}
p <- plot_composition(physeq = all, taxaRank1 = "Kingdom", taxaSet1 = "Fungi", taxaRank2 = "Species", numberOfTaxa = 22, x = "Sample")
p + facet_grid(". ~ Marker", scales = "free_x", space = "free")
```