-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathupsetR.Rmd
197 lines (131 loc) · 5.19 KB
/
upsetR.Rmd
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
---
title: "upsetR"
author: "ruben"
date: "08/06/2022"
output: html_document
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```
## Librairies
```{r,message=F,warning=F}
library(dplyr)
library(readr)
library(data.table)
library(tidyr)
library(tibble)
library(tidyverse)
library(UpSetR)
source(file = "functions.R")
```
## import data
```{r,message=F,warning=F}
species <- c("Bifidobacterium adolescentis","Bifidobacterium animalis","Bifidobacterium bifidum","Bifidobacterium catenulatum","Bifidobacterium longum","Bifidobacterium pseudocatenulatum")
load("curated_v3_otu_tax.rda")
setwd("/home/tapju/storage/actibiome")
## MAGS annotations data, containing completeness, average distance according to a reference genome, etc
mags_tax_file = readr::read_tsv("data-raw/bif_mags/Bifidobacterium_annotation/annotations_url_metadata_opendata.tsv") %>%
filter(assigned_genus=="Bifidobacterium") %>%
filter(completeness > 80) %>%
select(genome_name,study,sample_name,assigned_species,completeness, average_distance)
## MAGS linked to functions ID's
mags_tax_gene_id_eggnog <- readr::read_csv2("data-raw/bif_mags/mags_tax_gene_id_eggnog.csv") %>%
select(-X1) %>%
## filter the species of interest
filter(grepl("adolescentis|animalis| longum|bifidum|dentium|pseudocatenulatum|catenulatum|animalis", assigned_species)) %>%
### eggnog is linked to several annotations KEGG, CAZy, so we link only 1 annotations for 1 eggnog
group_by(eggNOG_OGs) %>%
mutate(KEGG_ko = get_mode(KEGG_ko)) %>%
mutate(CAZy = get_mode(CAZy)) %>%
mutate(EC = get_mode(EC)) %>%
ungroup()
## number of mags per species
nb_mags_species <- mags_tax_file %>%
data.frame(.) %>%
group_by(assigned_species) %>%
summarise(n=n()) %>%
filter(grepl("adolescentis|animalis| longum|bifidum|dentium|pseudocatenulatum|catenulatum|animalis", assigned_species))
nb_mags_species$assigned_species <- gsub("Bifidobacterium","B.",nb_mags_species$assigned_species)
```
## mags _ tax file with selected species
```{r,message=F,warning=F}
sample <- sampleMetadata
## if one ppl has several samples, keep the sample with the highest nb of reads
sample <- unique(setDT(sampleMetadata)[order(subject_id, -number_reads)], by = "subject_id")
metadata <- sample %>%
## transform disease and westernized to good format
transform_disease(.) %>%
transform_westernized(.) %>%
select(sample_id, body_site, disease, age_category, gender, country, westernized, BMI, antibiotics_current_use)
## merge with mags_tax
mags_tax <- mags_tax_file %>%
merge(metadata, by.x = "sample_name", by.y = "sample_id") %>%
tibble::column_to_rownames("genome_name") %>%
filter(assigned_species %in% species)
```
## code block to add healthy/unhealthy clusters
```{r,message=F,warning=F}
metadata <- metadata
setwd("/home/ladeirru/GitHub/microbiome.actibiome")
bifido <- fread("enterotype_DMM") %>%
select(-V1) %>%
merge(metadata, by="sample_id") %>%
mutate(grp_healthy = case_when(
bifidotype %in% c(1,2,6) ~ "healthy",
TRUE ~ "unhealthy"
))
mags_tax <- mags_tax %>%
rownames_to_column("MAGS") %>%
merge(bifido %>% select(sample_id, grp_healthy), by.x = "sample_name", by.y="sample_id") %>%
column_to_rownames("MAGS")
```
## R Markdown
```{r, message=F,warning=F, fig.height=9, fig.width = 9}
functionality <- "eggNOG_OGs"
mags_ko_file_name <- "mags_ko_file.csv"
mags_ko_table <- mags_ko_table <- readr::read_csv2(mags_ko_file_name) %>%
merge(mags_tax_file %>% select(genome_name, assigned_species), by = "genome_name")
### heatmap with only 0 & 1
heat <- mags_ko_table %>%
mutate_at(vars(-c("assigned_species", "genome_name")),funs(case_when (. > 1 ~ 1,
TRUE ~ .)))
```
## import data
```{r,message=F,warning=F}
mags_tax_gene_id_eggnog %>%
select(assigned_species,EC) %>%
filter(EC!="-") %>%
unique() -> ec_assigned_species
ec_assigned_species %>%
group_by(assigned_species,EC) %>%
summarise(n=n()) %>%
reshape2::dcast(EC~assigned_species, value.var = "n", fill=0) %>%
UpSetR::upset()
readr::read_tsv("data-raw/cazy_dbcann.tsv") %>%
merge(mags_tax_gene_id_eggnog,., by.x="id_rep", by.y="Gene ID", all = FALSE) %>%
select(assigned_species, eggNOG_OGs, cazy_family, Description) %>%
group_by(assigned_species, cazy_family) %>%
summarise(n=1) %>%
reshape2::dcast(cazy_family~assigned_species, value.var = "n", fill=0) %>%
UpSetR::upset(nsets = 7)
```
```{r,message=F,warning=F}
#fig.height=4, fig.width =10
mags_tax_gene_id_eggnog_t <- mags_tax_gene_id_eggnog
# change to B.
mags_tax_gene_id_eggnog_t$assigned_species <- gsub("Bifidobacterium", "B.",mags_tax_gene_id_eggnog_t$assigned_species)
```
## upsetR on eggnogs
```{r,message=F,warning=F}
#fig.height=4, fig.width =10
mags_tax_gene_id_eggnog_t %>%
select(assigned_species, eggNOG_OGs) %>%
group_by(assigned_species, eggNOG_OGs) %>%
summarise(n=1) %>%
reshape2::dcast(eggNOG_OGs~assigned_species, value.var = "n", fill=0) %>%
UpSetR::upset(nsets = 7,
sets.x.label = "eggnog counts",
mainbar.y.label = "common eggnog counts",
number.angles = 30, point.size = 1, line.size = 0.5,
text.scale = c(1.5, 1, 1, 1, 1.5, 1))
```