Skip to content

Commit 06f2b9b

Browse files
author
Jasen Finch
authored
Merge pull request #37 from jasenfinch/devel
v0.5.9
2 parents 73f5cef + c1838f3 commit 06f2b9b

40 files changed

+1561
-2846
lines changed

DESCRIPTION

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
Package: metaboMisc
22
Title: Miscellaneous Functions for Metabolomics Analyses
3-
Version: 0.5.8
3+
Version: 0.5.9
44
Authors@R: person("Jasen", "Finch", email = "jsf9@aber.ac.uk", role = c("aut", "cre"))
55
Description: Miscellaneous helper functions for metabolomics analyses that do not yet have a permanent home.
66
URL: https://jasenfinch.github.io/metaboMisc

NAMESPACE

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@ importFrom(ggplot2,theme)
5959
importFrom(lubridate,seconds_to_period)
6060
importFrom(magrittr,"%>%")
6161
importFrom(magrittr,set_names)
62+
importFrom(metabolyseR,"changeParameter<-")
6263
importFrom(metabolyseR,"dat<-")
6364
importFrom(metabolyseR,"parameters<-")
6465
importFrom(metabolyseR,"preTreated<-")

NEWS.md

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,16 @@
1-
# metabMisc 0.5.8
1+
# metaboMisc 0.5.9
2+
3+
* Corrected the exported file name for mode-less processed data for the `MetaboProfile` class.
4+
5+
* Fixed the character sanitation in `sanitiseTable()`.
6+
7+
* Fixed extraneous console messages in `detectMissInjections()` and `detectBatchDiff()` methods for the `MetaboProfile` class.
8+
9+
* Enabled unit tests for methods for the `MetaboProfile` class.
10+
11+
* `detectPretreatmentParameters()` now detects the presence of QC samples.
12+
13+
# metaboMisc 0.5.8
214

315
* Numbers of characters in strings are now limited by `sanitiseTable()`.
416

R/detect.R

Lines changed: 92 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -60,20 +60,27 @@ setMethod('detectMissInjections',signature = 'MetaboProfile',
6060
mi <- x %>%
6161
processedData()
6262

63-
if (str_detect(technique(x),'GCMS')) {
64-
mi <- mi %>%
65-
rowSums() %>%
66-
tibble(value = .) %>%
67-
bind_cols(i)
68-
} else {
69-
mi <- mi %>%
70-
map(rowSums) %>%
71-
bind_cols() %>%
72-
rowSums() %>%
73-
as_tibble() %>%
74-
bind_cols(i)
63+
if (!is.list(mi)){
64+
mi <- list(mi)
65+
}
66+
67+
mi <- mi %>%
68+
map(rowSums)
69+
70+
if (mi %>%
71+
names() %>%
72+
is.na() %>%
73+
any()) {
74+
names(mi) <- replace(names(mi),
75+
is.na(names(mi)),
76+
'NA')
7577
}
76-
mi %>%
78+
79+
mi %>%
80+
bind_cols() %>%
81+
rowSums() %>%
82+
as_tibble() %>%
83+
bind_cols(i) %>%
7784
missInject(idx = idx)
7885
})
7986

@@ -102,7 +109,7 @@ missInject <- function(TICdat,idx){
102109
#' @details Analysis of Variance (ANOVA) is used to detect differences in total ion count (TIC) averages between batches/blocks.
103110
#' @examples
104111
#' ## Retrieve file paths and sample information for example data
105-
#' files <- metaboData::filePaths('FIE-HRMS','BdistachyonEcotypes')[1:2]
112+
#' files <- metaboData::filePaths('FIE-HRMS','BdistachyonEcotypes',ask = FALSE)[1:2]
106113
#'
107114
#' info <- metaboData::runinfo('FIE-HRMS','BdistachyonEcotypes')[1:2,]
108115
#'
@@ -159,21 +166,30 @@ setMethod('detectBatchDiff',signature = "MetaboProfile",
159166
TICdat <- x %>%
160167
processedData()
161168

162-
if (str_detect(technique(x),'GCMS')) {
163-
TICdat <- TICdat %>%
164-
rowSums() %>%
165-
{tibble(Sample = 1:length(.),
166-
TIC = .,
167-
batch = ri[,by] %>% deframe() %>% factor())}
168-
} else {
169-
TICdat <- TICdat %>%
170-
map(rowSums) %>%
171-
bind_cols() %>%
172-
rowid_to_column(var = 'Sample') %>%
173-
mutate(batch = ri[,by] %>% unlist() %>% factor()) %>%
174-
gather('Mode','TIC',-batch,-Sample)
169+
if (!is.list(TICdat)){
170+
TICdat <- list(TICdat)
175171
}
176172

173+
TICdat <- TICdat %>%
174+
map(rowSums)
175+
176+
if (TICdat %>%
177+
names() %>%
178+
is.na() %>%
179+
any()) {
180+
names(TICdat) <- replace(names(TICdat),
181+
is.na(names(TICdat)),
182+
'NA')
183+
}
184+
185+
TICdat <- TICdat %>%
186+
bind_cols() %>%
187+
rowid_to_column(var = 'Sample') %>%
188+
mutate(batch = ri[,by] %>%
189+
unlist() %>%
190+
factor()) %>%
191+
gather('Mode','TIC',-batch,-Sample)
192+
177193
diff <- batchDiff(TICdat,pthresh)
178194
return(diff)
179195
})
@@ -242,6 +258,8 @@ batchDiff <- function(TICdat,pthresh = 0.05){
242258
#' @rdname detectPretreatmentParameters
243259
#' @description Detect pre-treatment parameters for `Binalysis` or `MetaboProfile` class objects.
244260
#' @param x S4 object of class `Binalysis`, `MetaboProfile` or `AnalysisData`
261+
#' @param cls the name of the sample information table column containing the sample class information
262+
#' @param QCidx QC sample class label
245263
#' @return S4 object of class `AnalysisParameters`
246264
#' @examples
247265
#' ## Retreive example file paths and sample information
@@ -263,22 +281,49 @@ batchDiff <- function(TICdat,pthresh = 0.05){
263281
#' pp
264282
#' @export
265283

266-
setGeneric('detectPretreatmentParameters',function(x){
284+
setGeneric('detectPretreatmentParameters',function(x,
285+
cls = 'class',
286+
QCidx = 'QC'){
267287
standardGeneric('detectPretreatmentParameters')
268288
})
269289

270290
#' @rdname detectPretreatmentParameters
291+
#' @importFrom metabolyseR changeParameter<-
271292

272293
setMethod('detectPretreatmentParameters',signature = 'Binalysis',
273-
function(x){
274-
detectPretreatment(x)
294+
function(x,cls = 'class',QCidx = 'QC'){
295+
pp <- detectPretreatment(x)
296+
297+
sample_info <- binneR::sampleInfo(x)
298+
299+
if (!detectQC(sample_info,cls,QCidx)){
300+
parameters(pp,'pre-treatment')$QC <- NULL
301+
} else {
302+
changeParameter(pp,'QCidx','pre-treatment') <- QCidx
303+
}
304+
305+
changeParameter(pp,'cls','pre-treatment') <- cls
306+
307+
return(pp)
275308
})
276309

277310
#' @rdname detectPretreatmentParameters
278311

279312
setMethod('detectPretreatmentParameters',signature = 'MetaboProfile',
280-
function(x){
281-
detectPretreatment(x)
313+
function(x,cls = 'class',QCidx = 'QC'){
314+
pp <- detectPretreatment(x)
315+
316+
sample_info <- profilePro::sampleInfo(x)
317+
318+
if (!detectQC(sample_info,cls,QCidx)){
319+
parameters(pp,'pre-treatment')$QC <- NULL
320+
} else {
321+
changeParameter(pp,'QCidx','pre-treatment') <- QCidx
322+
}
323+
324+
changeParameter(pp,'cls','pre-treatment') <- cls
325+
326+
return(pp)
282327
})
283328

284329
#' @importFrom metabolyseR parameters<- parameters
@@ -314,6 +359,10 @@ detectPretreatment <- function(x){
314359
return(pre_treat_params)
315360
}
316361

362+
detectQC <- function(sample_info,cls,QCidx){
363+
any(str_detect(sample_info[[cls]],'QC'))
364+
}
365+
317366
#' Detect modelling parameters
318367
#' @rdname detectModellingParameters
319368
#' @description Detect modelling parameters for `Binalysis`, `MetaboProfile` or `Analysis` S4 classes.
@@ -366,17 +415,17 @@ setMethod('detectModellingParameters',signature = 'Binalysis',
366415

367416
setMethod('detectModellingParameters',signature = 'MetaboProfile',
368417
function(x){
369-
idx <- x %>%
370-
processingParameters() %>%
371-
.$info %>%
372-
.$cls
373-
374-
sample_information <- x %>%
375-
profilePro::sampleInfo() %>%
376-
select(all_of(idx)) %>%
377-
deframe()
378-
379-
detectModelling(sample_information,idx)
418+
idx <- x %>%
419+
processingParameters() %>%
420+
.$info %>%
421+
.$cls
422+
423+
sample_information <- x %>%
424+
profilePro::sampleInfo() %>%
425+
select(all_of(idx)) %>%
426+
deframe()
427+
428+
detectModelling(sample_information,idx)
380429
})
381430

382431
#' @rdname detectModellingParameters

R/export.R

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,10 +88,20 @@ setMethod('exportData',signature = 'MetaboProfile',
8888
file_paths <- pd %>%
8989
names() %>%
9090
map_chr(~{
91+
prefix <- .x
92+
93+
if (prefix != 'n' | prefix != 'p'){
94+
prefix <- ''
95+
} else {
96+
prefix <- switch(prefix,
97+
n = 'negative_mode_',
98+
p = 'positive_mode_')
99+
}
100+
91101
bind_cols(pd[[.x]],i %>% select(name)) %>%
92102
gather('Feature','Intensity',-name) %>%
93103
spread(name,Intensity) %>%
94-
exportCSV(str_c(outPath,'/',.x,'_mode_processed_data.csv'))
104+
exportCSV(str_c(outPath,'/',prefix,'processed_data.csv'))
95105
})
96106

97107
return(file_paths)

R/sanitise.R

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ sanitiseTable <- function(x,maxRows = 5000,sigFig = 3,maxCharacters = 100){
1919
x <- x[seq_len(maxRows),]
2020
}
2121

22-
a <- map_df(x,~{
22+
x <- map_df(x,~{
2323
if (typeof(.x) == 'character'){
2424
limit_characters <- .x %>%
2525
nchar() %>%

R/summary.R

Lines changed: 40 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -21,28 +21,48 @@ setGeneric('featureSummary',function(x)
2121
standardGeneric('featureSummary'))
2222

2323
#' @rdname featureSummary
24-
#' @importFrom dplyr n_distinct
2524

2625
setMethod('featureSummary',signature = 'Binalysis',
2726
function(x){
2827
x %>%
29-
binnedData() %>%
30-
map(~{{
31-
.x %>%
32-
rowid_to_column(var = 'Sample') %>%
33-
gather('Feature','Intensity',-Sample)
34-
}}) %>%
35-
bind_rows() %>%
36-
mutate(Mode = str_sub(Feature,1,1)) %>%
37-
group_by(Mode) %>%
38-
summarise(
39-
`Number of bins` = n_distinct(Feature),
40-
`Missing Data (%)` = round(length(which(Intensity == 0)) /
41-
length(Intensity) * 100,2),
42-
.groups = 'drop') %>%
43-
{{
44-
.$Mode[.$Mode == 'n'] <- 'Negative'
45-
.$Mode[.$Mode == 'p'] <- 'Positive'
46-
.
47-
}}
28+
binnedData() %>%
29+
featSummary()
4830
})
31+
32+
#' @rdname featureSummary
33+
34+
setMethod('featureSummary',signature = 'MetaboProfile',
35+
function(x){
36+
x %>%
37+
processedData() %>%
38+
featSummary()
39+
})
40+
41+
#' @importFrom dplyr n_distinct
42+
43+
featSummary <- function(x){
44+
45+
if (!is.list(x)){
46+
x <- list(x)
47+
}
48+
49+
x %>%
50+
map(~{{
51+
.x %>%
52+
rowid_to_column(var = 'Sample') %>%
53+
gather('Feature','Intensity',-Sample)
54+
}}) %>%
55+
bind_rows() %>%
56+
mutate(Mode = str_sub(Feature,1,1)) %>%
57+
mutate(Mode = replace(Mode,
58+
Mode != 'n' & Mode != 'p',
59+
NA)) %>%
60+
group_by(Mode) %>%
61+
summarise(
62+
`Number of bins` = n_distinct(Feature),
63+
`Missing Data (%)` = round(length(which(Intensity == 0)) /
64+
length(Intensity) * 100,2),
65+
.groups = 'drop') %>%
66+
mutate(Mode = replace(Mode,Mode == 'n','Negative') %>%
67+
replace(Mode == 'p','Positive'))
68+
}

0 commit comments

Comments
 (0)