-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfilter_variable_outlier.Rmd
More file actions
executable file
·249 lines (199 loc) · 6.86 KB
/
filter_variable_outlier.Rmd
File metadata and controls
executable file
·249 lines (199 loc) · 6.86 KB
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
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
---
title: "Filter noisy variables and outlier samples"
author:
- name: Xiaotao Shen (https://www.shenxt.info/)
date: "Created on 2020-04-01 and updated on `r Sys.Date()`"
output:
html_document:
df_print: paged
toc: no
pdf_document:
toc: no
vignette: >
%\VignetteIndexEntry{filter_variable_outlier}
%\VignettePackage{masscleaner}
% \VignetteEngine{knitr::rmarkdown}
% \usepackage[utf8]{inputenc}
%\VignetteEncoding{UTF-8}
---
```{r, include = FALSE, echo=FALSE}
knitr::opts_chunk$set(
collapse = TRUE,
comment = "#>",
warning = FALSE,
message = TRUE,
out.width = "100%"
)
```
# **Introduction**
We can use `masscleaner` to remove noisy features and outlier samples.
First, we need to prepare samples for `masscleaner`.
Download the demo data: [peak tables](https://drive.google.com/file/d/1mXljBn8lbEMlN4tMHaSFLNsJRmZwKPhU/view?usp=sharing) and uncompress.

Download the demo data: [sample information](https://drive.google.com/file/d/1yyJeOMUhuMSTmPWfzKuFTCRfZ3zdhO6q/view?usp=sharing) and uncompress.

# **Data preparation**
Here we only use the positive mode as an example.
```{r,eval=TRUE,warning=FALSE, R.options="", message=TRUE, cache=TRUE}
library(masscleaner)
library(tidyverse)
```
```{r,eval=TRUE,warning=FALSE, R.options="", message=TRUE, cache=TRUE}
peak_table_pos = readr::read_csv("peak_tables/POS/Peak_table_for_cleaning.csv") %>%
as.data.frame()
sample_info_pos = readr::read_csv("sample_info/sample_info_pos.csv") %>%
as.data.frame()
```
```{r,eval=TRUE,warning=FALSE, R.options="", message=TRUE, cache=TRUE}
expression_data_pos <-
peak_table_pos %>%
dplyr::select(-c(variable_id, mz, rt))
variable_info_pos <-
peak_table_pos %>%
dplyr::select(c(variable_id, mz, rt))
rownames(expression_data_pos) = variable_info_pos$variable_id
dim(expression_data_pos)
dim(sample_info_pos)
colnames(expression_data_pos) == sample_info_pos$sample_id
```
```{r,eval=TRUE,warning=FALSE, R.options="", message=TRUE, cache=TRUE}
expression_data_pos =
expression_data_pos[,sample_info_pos$sample_id]
sum(colnames(expression_data_pos) == sample_info_pos$sample_id)
```
```{r,eval=TRUE,warning=FALSE, R.options="", message=TRUE, cache=TRUE}
object <-
create_mass_dataset(expression_data = expression_data_pos,
sample_info = sample_info_pos,
variable_info = variable_info_pos)
```
```{r,eval=TRUE,warning=FALSE, R.options="", message=TRUE, cache=TRUE}
object
```
Summary information.
```{r,eval=TRUE,warning=FALSE, R.options="", message=TRUE, cache=TRUE}
get_mv_number(object)
massdataset::get_mv_number(object, by = "sample") %>%
head()
head(massdataset::get_mv_number(object, by = "variable"))
massdataset::get_mv_number(object, by = "sample", show_by = "percentage") %>%
head()
head(massdataset::get_mv_number(object, by = "variable"), show_by = "percentage")
```
# **Filter noisy features**
Remove variables who have mv in more than 20% QC samples and in at lest 50% samples in control group or case group.
```{r,eval=TRUE,warning=FALSE, R.options="", message=TRUE, cache=TRUE}
object %>%
activate_mass_dataset(what = "sample_info") %>%
dplyr::count(group)
```
```{r,eval=TRUE,warning=FALSE, R.options="", message=TRUE, cache=TRUE}
show_variable_missing_values(object = object,
percentage = TRUE) +
scale_size_continuous(range = c(0.01, 2))
```
```{r,eval=TRUE,warning=FALSE, R.options="", message=TRUE, cache=TRUE}
qc_id =
object %>%
activate_mass_dataset(what = "sample_info") %>%
filter(class == "QC") %>%
pull(sample_id)
control_id =
object %>%
activate_mass_dataset(what = "sample_info") %>%
filter(group == "Control") %>%
pull(sample_id)
case_id =
object %>%
activate_mass_dataset(what = "sample_info") %>%
filter(group == "Case") %>%
pull(sample_id)
object =
object %>%
mutate_variable_na_freq(according_to_samples = qc_id) %>%
mutate_variable_na_freq(according_to_samples = control_id) %>%
mutate_variable_na_freq(according_to_samples = case_id)
head(extract_variable_info(object))
```
```{r,eval=TRUE,warning=FALSE, R.options="", message=TRUE, cache=TRUE}
object <-
object %>%
activate_mass_dataset(what = "variable_info") %>%
filter(na_freq < 0.2 & (na_freq.1 < 0.5 | na_freq.2 < 0.5))
object
```
```{r,eval=TRUE,warning=FALSE, R.options="", message=TRUE, cache=TRUE}
show_variable_missing_values(object = object[,qc_id],
percentage = TRUE)
```
```{r,eval=TRUE,warning=FALSE, R.options="", message=TRUE, cache=TRUE}
show_variable_missing_values(object = object[,control_id],
percentage = TRUE)
```
```{r,eval=TRUE,warning=FALSE, R.options="", message=TRUE, cache=TRUE}
show_variable_missing_values(object = object[,case_id],
percentage = TRUE)
```
```{r,eval=TRUE,warning=FALSE, R.options="", message=TRUE, cache=TRUE}
object %>%
activate_mass_dataset(what = "variable_info") %>%
dplyr::filter(na_freq.1 > 0.5) %>%
extract_variable_info() %>%
ggplot(aes(na_freq.1, na_freq.2)) +
geom_point() +
scale_x_continuous(limits = c(0,1)) +
scale_y_continuous(limits = c(0,1))
```
```{r,eval=TRUE,warning=FALSE, R.options="", message=TRUE, cache=TRUE}
object %>%
activate_mass_dataset(what = "variable_info") %>%
dplyr::filter(na_freq.2 > 0.5) %>%
extract_variable_info() %>%
ggplot(aes(na_freq.1, na_freq.2)) +
geom_point() +
scale_x_continuous(limits = c(0,1)) +
scale_y_continuous(limits = c(0,1))
```
# **Filter outlier samples**
We can use the `detect_outlier()` to find the outlier samples.
More information about how to detect outlier samples can be found [here](https://privefl.github.io/blog/detecting-outlier-samples-in-pca/).
```{r,eval=TRUE,warning=FALSE, R.options="", message=TRUE, cache=TRUE}
massdataset::show_sample_missing_values(object = object,
color_by = "group",
order_by = "injection.order",
percentage = TRUE) +
ggsci::scale_color_aaas()
```
Detect outlier samples.
```{r,eval=TRUE,warning=FALSE, R.options="", message=TRUE, cache=TRUE}
outlier_samples =
object %>%
`+`(1) %>%
log(2) %>%
scale() %>%
detect_outlier()
outlier_samples
```
```{r,eval=TRUE,warning=FALSE, R.options="", message=TRUE, cache=TRUE}
outlier_table <-
extract_outlier_table(outlier_samples)
outlier_table %>%
head()
```
```{r,eval=TRUE,warning=FALSE, R.options="", message=TRUE, cache=TRUE}
outlier_table %>%
apply(1, function(x){
sum(x)
}) %>%
`>`(0) %>%
which()
```
No outlier samples.
Save data for next analysis.
```{r,eval=TRUE,warning=FALSE, R.options="", message=TRUE, cache=TRUE}
save(object, file = "peak_tables/POS/object")
```
# **Session information**
```{r,eval=TRUE,warning=FALSE, R.options="", message=TRUE, cache=TRUE}
sessionInfo()
```