-
Notifications
You must be signed in to change notification settings - Fork 28
/
Copy pathhmp_example_data.Rmd
139 lines (90 loc) · 2.51 KB
/
hmp_example_data.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
## Creating the HMP example dataset
### Sample data
```{r}
library(readr)
library(dplyr)
set.seed(1)
hmp_samples <- read_tsv("~/Downloads/v35_map_uniquebyPSN.txt")
hmp_otus <- read_tsv("~/Downloads/v35_psn_otu.genus.fixed.txt", skip = 1)
```
remove uneeded columns
```{r}
hmp_samples <- hmp_samples[, c("#SampleID", "sex", "HMPbodysubsite")]
```
rename columns
```{r}
colnames(hmp_samples) <- c("sample_id", "sex", "body_site")
```
subset treatments
```{r}
sites <- c("Saliva", "Throat", "Stool", "Right_Antecubital_fossa", "Anterior_nares")
hmp_samples <- hmp_samples[hmp_samples$body_site %in% sites, ]
```
rename treatments
```{r}
hmp_samples[hmp_samples$body_site == "Right_Antecubital_fossa", "body_site"] <- "Skin"
hmp_samples[hmp_samples$body_site == "Anterior_nares", "body_site"] <- "Nose"
```
remove samples not in abundance data
```{r}
hmp_samples <- hmp_samples[hmp_samples$sample_id %in% colnames(hmp_otus), ]
```
remove low count samples
```{r}
hmp_samples <- hmp_samples[colSums(hmp_otus[, as.character(hmp_samples$sample_id)]) >= 1000, ]
```
subsample sites
```{r}
hmp_samples <- hmp_samples %>%
group_by(body_site, sex) %>%
sample_n(size = 5)
```
convert sample IDs to character
```{r}
hmp_samples$sample_id <- as.character(hmp_samples$sample_id)
```
### Abundance matrix
Subset samples
```{r}
hmp_otus <- hmp_otus[, c("#OTU ID", "Consensus Lineage", hmp_samples$sample_id)]
```
rename columns
```{r}
colnames(hmp_otus)[1:2] <- c("otu_id", "lineage")
```
Remove OTUs with missing info
```{r}
hmp_otus <- hmp_otus[! endsWith(hmp_otus$lineage, "__"), ]
hmp_otus <- hmp_otus[! grepl(hmp_otus$lineage, pattern = "__;", fixed = TRUE), ]
```
Remove ambiguous OTUs
```{r}
hmp_otus <- hmp_otus[! grepl(hmp_otus$lineage, pattern = "IncertaeSedis"), ]
```
Remove singletons
```{r}
hmp_otus <- hmp_otus[rowSums(hmp_otus[, hmp_samples$sample_id]) > 1, ]
```
random subsample of OTUs
```{r}
hmp_otus <- hmp_otus %>% sample_n(1000, weight = rowSums(hmp_otus[, hmp_samples$sample_id]))
```
Add root rank
```{r}
hmp_otus$lineage <- paste0("r__", hmp_otus$lineage)
```
## Add to package
```{r}
devtools::use_data(hmp_otus, overwrite = TRUE)
devtools::use_data(hmp_samples, overwrite = TRUE)
```
## How to parse
```{r}
x = parse_tax_data(hmp_otus, class_cols = "lineage", class_sep = ";",
class_key = c(tax_rank = "info", tax_name = "taxon_name"),
class_regex = "^(.+)__(.+)$")
```
```{r}
library(metacoder)
heat_tree(x, node_size = n_obs, node_color = n_obs, node_label = taxon_names)
```