-
Notifications
You must be signed in to change notification settings - Fork 4
/
d_ratio_filter_class.R
187 lines (172 loc) · 5.98 KB
/
d_ratio_filter_class.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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
#' @eval get_description('dratio_filter')
#' @examples
#' D = MTBLS79_DatasetExperiment()
#' M = dratio_filter(threshold=20,qc_label='QC',factor_name='Class')
#' M = model_apply(M,D)
#' @export dratio_filter
dratio_filter = function(
threshold=20,
qc_label='QC',
factor_name,
method='ratio',
dispersion='sd',
...) {
out=struct::new_struct('dratio_filter',
threshold=threshold,
qc_label=qc_label,
factor_name=factor_name,
method=method,
dispersion=dispersion,
...)
return(out)
}
.dratio_filter<-setClass(
"dratio_filter",
contains = c('model'),
slots=c(threshold='entity',
qc_label='entity',
factor_name='entity',
filtered='entity',
flags='entity',
d_ratio='data.frame',
method='enum',
dispersion='enum'
),
prototype=list(name = 'Dispersion ratio filter',
description = paste0('The dispersion ratio (d-ratio) compares the ',
'standard deviation (or non-parametric equivalent) of the Quality ',
'Control (QC) samples relative to the standard deviation (or ',
'non-parametric equivalent) of the samples for each feature. ',
'If the d-ratio is greater than a predefined threshold then the ',
'observed sample variance could be due to technical variance and ',
'the feature is removed.'),
type = 'filter',
predicted = 'filtered',
.params=c('threshold','qc_label','factor_name','method','dispersion'),
.outputs=c('filtered','flags','d_ratio'),
citations=list(
bibentry(
bibtype='Article',
year = '2018',
month = 'May',
volume = 14,
number = 6,
author = as.person(
paste0('David Broadhurst, Royston Goodacre, ',
'Stacey N. Reinke, Julia Kuligowski, Ian D. Wilson, ',
'Matthew R. Lewis, Warwick B. Dunn')),
title = paste0('Guidelines and considerations for the use of ',
'system suitability and quality control samples in mass ',
'spectrometry assays applied in untargeted clinical ',
'metabolomic studies'),
journal = 'Metabolomics'
)
),
threshold=entity(name = 'Dispersion ratio threshold',
description = 'The threshold above which features are removed.',
value = 20,
type='numeric'),
qc_label=entity(name = 'QC label',
description = 'The label used to identify QC samples.',
value = 'QC',
type='character'),
factor_name=ents$factor_name,
filtered=entity(name = 'Filtered data',
description = 'A DatasetExperiment object containing the filtered data.',
type='DatasetExperiment',
value=DatasetExperiment()
),
flags=entity(name = 'Flags',
description = 'Flag indicating whether the feature was rejected by the filter or not.',
type='data.frame',
value=data.frame()
),
method=enum(
name='dratio method',
description = c(
'ratio' = paste0('Dispersion of the QCs divided by the ',
'dispersion of the samples. Corresponds to Eq 4 in ',
' Broadhurst et al (2018).'),
'euclidean' = paste0('Dispersion of the QCs divided by the ',
'euclidean length of the total dispersion. Total dispersion ',
'is estimated from the QC and Sample dispersion by assuming ',
'that they are orthogonal. Corresponds to Eq 5 in ',
'Broadhurst et al (2018)')),
allowed=c('ratio','euclidean'),
value='ratio',
type='character',
max_length = 1
),
dispersion=enum(
name='Dispersion method',
description = c(
'sd' = paste0('Dispersion is estimated using the ',
'standard deviation.'),
'mad' = paste0('Dispersion is estimated using the median ',
'absolute deviation.')),
allowed=c('sd','mad'),
value='sd',
type='character',
max_length = 1
)
)
)
#' @export
#' @template model_train
setMethod(f="model_train",
signature=c("dratio_filter","DatasetExperiment"),
definition=function(M,D)
{
# dispersion QC samples
QC = filter_smeta(
mode='include',
levels=M$qc_label,
factor_name=M$factor_name)
QC = model_apply(QC,D)
QC = predicted(QC)$data
if (M$dispersion=='mad') {
QC = apply(QC,2,mad,na.rm=TRUE)
} else {
QC = apply(QC,2,sd,na.rm=TRUE)
}
# dispersion (not QC) samples
S = filter_smeta(
mode='exclude',
levels=M$qc_label,
factor_name=M$factor_name)
S = model_apply(S,D)
S = predicted(S)$data
if (M$dispersion=='mad') {
S = apply(S,2,mad,na.rm=TRUE) # constant = 1.4826 default
} else {
S = apply(S,2,sd,na.rm=TRUE)
}
# dispersion ratio
if (M$method=='ratio') {
# eq 4
d_ratio=(QC/S)*100
} else {
# eq 5
d_ratio= (QC / sqrt((QC^2) + (S^2))) * 100
}
OUT=d_ratio>M$threshold
M$d_ratio=data.frame(d_ratio=d_ratio,row.names=colnames(D$data))
M$flags=data.frame(rejected=OUT,row.names = colnames(D$data))
return(M)
}
)
#' @export
#' @template model_predict
setMethod(f="model_predict",
signature=c("dratio_filter","DatasetExperiment"),
definition=function(M,D)
{
# get flags
OUT=M$flags$rejected
# remove flagged
D=D[,!OUT]
# store
M$filtered=D
return(M)
}
)