@@ -45,13 +45,6 @@ test_that("do_adjustRtime_peakGroups works", {
45
45
})
46
46
47
47
test_that(" applyRtAdjustment works" , {
48
- skip_on_os(os = " windows" , arch = " i386" )
49
-
50
- xs <- faahko
51
- # # group em.
52
- # # xsg <- group(xs)
53
- # # ## align em.
54
- # # xsa <- retcor(xsg, method = "peakgroups")
55
48
pksAdj <- .applyRtAdjToChromPeaks(chromPeaks(xod_xg ),
56
49
rtraw = rtime(xod_xg , bySample = TRUE ),
57
50
rtadj = rtime(xod_xgr , bySample = TRUE ))
@@ -73,16 +66,86 @@ test_that("applyRtAdjustment works", {
73
66
# # Artificial examples.
74
67
a_raw <- c(1 , 2 , 3 , 5 , 6 , 7 , 8 , 10 , 12 , 13 , 14 , 16 )
75
68
a_adj <- a_raw + 2 # shift by 2
76
- b <- .applyRtAdjustment(a_raw , a_raw , a_adj )
69
+ b <- .applyRtAdjustment(a_raw , a_raw , a_adj , method = " approxfun " )
77
70
expect_equal(a_adj , b )
78
71
b_2 <- .applyRtAdjustment(a_raw , a_raw [4 : 8 ], a_adj [4 : 8 ])
79
72
expect_equal(b , b_2 )
73
+ x <- c(2 , 3 , 5 , 6 , 8 )
74
+ res <- .applyRtAdjustment(x , a_raw , a_adj , method = " approxfun" )
75
+ expect_equal(res , x + 2 )
76
+ res <- .applyRtAdjustment(x , a_raw , a_adj , method = " stepfun" )
77
+ expect_equal(res , x + 2 )
80
78
81
79
a_adj <- a_raw - 2
82
80
b <- .applyRtAdjustment(a_raw , a_raw , a_adj )
83
81
expect_equal(a_adj , b )
84
82
b_2 <- .applyRtAdjustment(a_raw , a_raw [4 : 8 ], a_adj [4 : 8 ])
85
83
expect_equal(b , b_2 )
84
+
85
+ # # Difference between stepfun (old default) and approxfun.
86
+ a_raw <- seq(1 , 100 , by = 0.3 )
87
+ a_adj <- a_raw + 0.2
88
+ x <- seq(4 , 20 , by = 0.3 )
89
+ res <- .applyRtAdjustment(x , a_raw , a_adj , method = " stepfun" )
90
+ expect_equal(res , x + 0.2 )
91
+ res <- .applyRtAdjustment(x , a_raw , a_adj , method = " approxfun" )
92
+ expect_equal(res , x + 0.2 )
93
+
94
+ x <- seq(1.4 , 20 , by = 0.3 )
95
+ res <- .applyRtAdjustment(x , a_raw , a_adj , method = " stepfun" )
96
+ # # expect_equal(res, x + 0.2) # error!
97
+ res <- .applyRtAdjustment(x , a_raw , a_adj , method = " approxfun" )
98
+ expect_equal(res , x + 0.2 )
99
+
100
+ a_adj <- a_raw + 1.3
101
+ res <- .applyRtAdjustment(x , a_raw , a_adj , method = " stepfun" )
102
+ # # expect_equal(res, x + 1.3) # error!
103
+ res <- .applyRtAdjustment(x , a_raw , a_adj , method = " approxfun" )
104
+ expect_equal(res , x + 1.3 )
105
+
106
+ # # small increments
107
+ a_raw <- seq(0.1 , 100 , by = 0.03 )
108
+ a_adj <- sort(a_raw + 0.2 )
109
+
110
+ res <- .applyRtAdjustment(a_raw , a_raw , a_adj , method = " stepfun" )
111
+ expect_equal(a_adj , res )
112
+ expect_equal(quantile(diff(a_adj )), quantile(diff(res )))
113
+
114
+ set.seed(123 )
115
+ x <- seq(2.02 , 90.02 , by = 0.03 )
116
+ x <- sort(x + rnorm(length(x ), 0 , 0.001 ))
117
+ res <- .applyRtAdjustment(x , a_raw , a_adj , method = " stepfun" )
118
+ # # expect_equal(res, x + 0.2)
119
+ # # plot(res, res - x, type = "l")
120
+
121
+ res <- .applyRtAdjustment(x , a_raw , a_adj , method = " approxfun" )
122
+ expect_equal(quantile(diff(res )), quantile(diff(x )))
123
+ expect_equal(res , x + 0.2 )
124
+ # # plot(res, res - x, type = "l")
125
+
126
+ # # deviation is smaller than diff
127
+ a_raw <- seq(0.1 , 100 , by = 1.2 )
128
+ a_adj <- sort(a_raw + 0.2 )
129
+
130
+ res <- .applyRtAdjustment(a_raw , a_raw , a_adj , method = " stepfun" )
131
+ expect_equal(a_adj , res )
132
+ expect_equal(diff(a_adj ), diff(res ))
133
+
134
+ # # Now, that's an issue.
135
+ # # we should! have a constant shift by 0.2
136
+ x <- seq(2 , 90 , by = 1.2 )
137
+ x <- sort(x + rnorm(length(x ), mean = 0 , sd = 0.001 ))
138
+ res <- .applyRtAdjustment(x , a_raw , a_adj , method = " stepfun" )
139
+ # # plot(res, res - x, type = "l")
140
+ # # For a constant shift we expect the difference between consecutive values
141
+ # # to stay the same, but the test below fails.
142
+ # # expect_equal(diff(x), diff(res))
143
+
144
+ res <- .applyRtAdjustment(x , a_raw , a_adj , method = " approxfun" )
145
+ expect_equal(diff(x ), diff(res ))
146
+ expect_equal(mean(res - x ), 0.2 )
147
+ expect_equal(res , x + 0.2 )
148
+ # # plot(res, res - x, type = "l")
86
149
})
87
150
88
151
test_that(" .get_closest_index works" , {
@@ -136,42 +199,65 @@ test_that(".match_trim_vectors and index works", {
136
199
})
137
200
138
201
test_that(" adjustRtimeSubset works" , {
139
- skip_on_os(os = " windows" , arch = " i386" )
140
-
141
202
rt_raw <- rtime(xod_xgr , adjusted = FALSE , bySample = TRUE )
142
203
rt_adj <- rtime(xod_xgr , adjusted = TRUE , bySample = TRUE )
143
204
144
- res <- adjustRtimeSubset(rt_raw , rt_adj , subset = c(1 , 3 ),
145
- method = " previous" )
205
+ res <- xcms ::: adjustRtimeSubset(rt_raw , rt_adj , subset = c(1 , 3 ),
206
+ method = " previous" , adjFun = " stepfun " )
146
207
expect_equal(res [[1 ]], rt_adj [[1 ]])
147
208
expect_equal(res [[3 ]], rt_adj [[3 ]])
148
209
expect_true(all(res [[2 ]] != rt_adj [[2 ]]))
149
210
expect_equal(names(res [[2 ]]), names(rt_adj [[2 ]]))
150
211
expect_equal(unname(res [[2 ]]), unname(rt_adj [[1 ]]))
151
212
152
- a <- res [[1 ]] - rt_raw [[1 ]]
153
- b <- res [[2 ]] - rt_raw [[2 ]]
154
- c <- res [[3 ]] - rt_raw [[3 ]]
155
- plot(res [[1 ]], a , type = " l" , col = " #ff000040" , lty = 2 ,
156
- ylim = range(a , b , c ))
157
- points(res [[2 ]], b , type = " l" , col = " #00ff0060" , lty = 1 )
158
- points(res [[3 ]], c , type = " l" , col = " #0000ff40" , lty = 2 )
213
+ res <- xcms ::: adjustRtimeSubset(rt_raw , rt_adj , subset = c(1 , 3 ),
214
+ method = " previous" , adjFun = " approxfun" )
215
+ expect_equal(res [[1 ]], rt_adj [[1 ]])
216
+ expect_equal(res [[3 ]], rt_adj [[3 ]])
217
+ expect_true(all(res [[2 ]] != rt_adj [[2 ]]))
218
+ # # Values are no longer IDENTICAL, but highly similar:
219
+ expect_true(median(res [[2 ]] - rt_adj [[1 ]]) == 0 )
220
+ expect_true(max(abs(res [[2 ]] - rt_adj [[1 ]])) < 0.002 )
221
+
222
+ # # a <- res[[1]] - rt_raw[[1]]
223
+ # # b <- res[[2]] - rt_raw[[2]]
224
+ # # c <- res[[3]] - rt_raw[[3]]
225
+ # # plot(res[[1]], a, type = "l", col = "#ff000040", lty = 2,
226
+ # # ylim = range(a, b, c))
227
+ # # points(res[[2]], b, type = "l", col = "#00ff0060", lty = 1)
228
+ # # points(res[[3]], c, type = "l", col = "#0000ff40", lty = 2)
229
+
230
+ res <- xcms ::: adjustRtimeSubset(rt_raw , rt_adj , subset = c(1 , 3 ),
231
+ method = " average" , adjFun = " stepfun" )
232
+ expect_equal(res [[1 ]], rt_adj [[1 ]])
233
+ expect_equal(res [[3 ]], rt_adj [[3 ]])
234
+ expect_true(all(res [[2 ]] != rt_adj [[2 ]]))
235
+ expect_true(all(res [[2 ]] != rt_adj [[1 ]]))
236
+ expect_true(all(res [[2 ]] != rt_adj [[3 ]]))
237
+
238
+ # # a <- res[[1]] - rt_raw[[1]]
239
+ # # b <- res[[2]] - rt_raw[[2]]
240
+ # # c <- res[[3]] - rt_raw[[3]]
241
+ # # plot(res[[1]], a, type = "l", col = "#ff000040", lty = 2,
242
+ # # ylim = range(a, b, c))
243
+ # # points(res[[2]], b, type = "l", col = "#00ff0060", lty = 1)
244
+ # # points(res[[3]], c, type = "l", col = "#0000ff40", lty = 2)
159
245
160
- res <- adjustRtimeSubset(rt_raw , rt_adj , subset = c(1 , 3 ),
161
- method = " average" )
246
+ res <- xcms ::: adjustRtimeSubset(rt_raw , rt_adj , subset = c(1 , 3 ),
247
+ method = " average" , adjFun = " approxfun " )
162
248
expect_equal(res [[1 ]], rt_adj [[1 ]])
163
249
expect_equal(res [[3 ]], rt_adj [[3 ]])
164
250
expect_true(all(res [[2 ]] != rt_adj [[2 ]]))
165
251
expect_true(all(res [[2 ]] != rt_adj [[1 ]]))
166
252
expect_true(all(res [[2 ]] != rt_adj [[3 ]]))
167
253
168
- a <- res [[1 ]] - rt_raw [[1 ]]
169
- b <- res [[2 ]] - rt_raw [[2 ]]
170
- c <- res [[3 ]] - rt_raw [[3 ]]
171
- plot(res [[1 ]], a , type = " l" , col = " #ff000040" , lty = 2 ,
172
- ylim = range(a , b , c ))
173
- points(res [[2 ]], b , type = " l" , col = " #00ff0060" , lty = 1 )
174
- points(res [[3 ]], c , type = " l" , col = " #0000ff40" , lty = 2 )
254
+ # # a <- res[[1]] - rt_raw[[1]]
255
+ # # b <- res[[2]] - rt_raw[[2]]
256
+ # # c <- res[[3]] - rt_raw[[3]]
257
+ # # plot(res[[1]], a, type = "l", col = "#ff000040", lty = 2,
258
+ # # ylim = range(a, b, c))
259
+ # # points(res[[2]], b, type = "l", col = "#00ff0060", lty = 1)
260
+ # # points(res[[3]], c, type = "l", col = "#0000ff40", lty = 2)
175
261
})
176
262
177
263
test_that(" .adjustRtime_peakGroupsMatrix works" , {
0 commit comments