forked from bioinformatics-leibniz-hki/correlation-workshop
-
Notifications
You must be signed in to change notification settings - Fork 0
/
data_plan.R
164 lines (142 loc) · 4.95 KB
/
data_plan.R
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
#!/usr/bin/env Rscript
#
# Get data about feature abundance and samples
#
data_plan <- drake_plan(
# projects present in column dataset_name in table curatedMetagenomicData::combined_metadata
projects = c("NielsenHB_2014", "ZellerG_2014"),
# disease names present in column disease in table curatedMetagenomicData::combined_metadata
diseases = c("CRC", "healthy"),
# Works only if metaphlan data is available
feature_types = c("taxon", "pathway"),
# Subset: number of features to draw per subset (e.g. disease)
n_features = 20,
# Subset: number of samples to draw per subset (e.g. disease)
n_samples = 20,
# Min percentage of samples the taxon must be present to be included
min_prevalence_perc = 10,
# Sample meta data table
samples = {
curatedMetagenomicData::combined_metadata %>%
as_tibble() %>%
filter(
disease %in% diseases &
dataset_name %in% projects
) %>%
group_by(disease) %>%
sample_n(n_samples) %>%
ungroup() %>%
transmute(
sample_id = sprintf("S%02i", row_number()),
sample_alias = sampleID,
project = dataset_name,
body_site, disease, age, age_category, gender, country
) %>%
mutate(across(where(is.character), as.factor))
},
# Abundance table in long format
all_abundances = target(
{
curated_metagenomic_data_feature_type <-
feature_types %>% recode(taxon = "metaphlan_bugs_list", pathway = "pathabundance_relab")
raw_abundance <-
paste(projects, curated_metagenomic_data_feature_type, "stool()", sep = ".") %>%
map(~ eval(parse(text = .x))) %>%
pluck(1) %>%
exprs() %>%
as_tibble(rownames = "feature") %>%
gather(sample_alias, abundance, -feature) %>%
# subset and annotate samples
# inner join to only get subset of samples
inner_join(samples, by = "sample_alias") %>%
mutate(feature_type = feature_types)
# clean names
abundance <-
switch(feature_types,
"taxon" = {
raw_abundance %>%
# only keep metaphlan species counts
filter(
feature %>% str_detect("s__[A-Za-z_]+$")
) %>%
# cleaning names
mutate(
lineage = feature,
feature = feature %>%
str_extract("s__[A-Za-z_]+$") %>%
str_remove("^s__") %>%
str_replace_all("_", " ")
)
},
"pathway" = {
raw_abundance %>%
# discard species contribution of the pathway
filter(! feature %>% str_detect("\\|")) %>%
# discard unannotated pathways
filter(! feature %in% c("UNMAPPED", "UNINTEGRATED")) %>%
mutate(feature = feature %>% str_remove("^[A-z-+0-9]+: "))
}
)
# no nested table here, just one very long one
# e.g. just return the abundance tibble
# meta data is not required because the sample_id is unique
abundance
},
dynamic = cross(feature_types, projects)
),
lineages = {
all_abundances %>%
distinct(feature_type, feature, lineage) %>%
mutate(lineage = lineage %>% str_remove_all("[a-z]__") %>% str_replace_all("_", " ")) %>%
separate(
col = lineage,
sep = "\\|",
into = c("kingdom", "phylum", "order", "class", "family", "genus", "species")
)
},
subset_abundances = {
all_abundances %>%
group_by(feature_type, disease) %>%
nest() %>%
rename(abundance = data) %>%
mutate(
abundance = abundance %>% map(~ {
subset_samples <- .x$sample_id %>% unique()
prevalent_features <-
.x %>%
filter(abundance > 0) %>%
# count samples per feature
distinct(sample_id, feature) %>%
group_by(feature) %>%
count() %>%
# filter by prevalence
filter(n / length(subset_samples) * 100 >= min_prevalence_perc) %>%
arrange(-n) %>%
pull(feature)
if(length(prevalent_features) < n_features) {
str_glue(
"Not enough prevalent taxa! Found {length(prevalent_features)} ",
"but {n_features} were requested."
) %>%
stop()
}
selected_features <-
prevalent_features %>%
head(n_features)
.x %>% filter(feature %in% selected_features)
}
)
)
},
write_subsets = {
dir.create("data", showWarnings = FALSE)
all_abundances %>%
select(sample_id, feature, abundance) %>%
pivot_wider(names_from = feature, values_from = abundance) %>%
write_csv(file_out("data/features.csv"))
samples %>%
write_csv(file_out("data/samples.csv"))
lineages %>%
write_csv(file_out("data/lineages.csv"))
}
)