diff --git a/DESCRIPTION b/DESCRIPTION index 643caac..e5c8919 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -1,6 +1,6 @@ Package: metaboMisc Title: Miscellaneous Functions for Metabolomics Analyses -Version: 0.5.8 +Version: 0.5.9 Authors@R: person("Jasen", "Finch", email = "jsf9@aber.ac.uk", role = c("aut", "cre")) Description: Miscellaneous helper functions for metabolomics analyses that do not yet have a permanent home. URL: https://jasenfinch.github.io/metaboMisc diff --git a/NAMESPACE b/NAMESPACE index 0804971..7ec5ce9 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -59,6 +59,7 @@ importFrom(ggplot2,theme) importFrom(lubridate,seconds_to_period) importFrom(magrittr,"%>%") importFrom(magrittr,set_names) +importFrom(metabolyseR,"changeParameter<-") importFrom(metabolyseR,"dat<-") importFrom(metabolyseR,"parameters<-") importFrom(metabolyseR,"preTreated<-") diff --git a/NEWS.md b/NEWS.md index afb4bb1..a499960 100644 --- a/NEWS.md +++ b/NEWS.md @@ -1,4 +1,16 @@ -# metabMisc 0.5.8 +# metaboMisc 0.5.9 + +* Corrected the exported file name for mode-less processed data for the `MetaboProfile` class. + +* Fixed the character sanitation in `sanitiseTable()`. + +* Fixed extraneous console messages in `detectMissInjections()` and `detectBatchDiff()` methods for the `MetaboProfile` class. + +* Enabled unit tests for methods for the `MetaboProfile` class. + +* `detectPretreatmentParameters()` now detects the presence of QC samples. + +# metaboMisc 0.5.8 * Numbers of characters in strings are now limited by `sanitiseTable()`. diff --git a/R/detect.R b/R/detect.R index 4ad2530..1afa440 100644 --- a/R/detect.R +++ b/R/detect.R @@ -60,20 +60,27 @@ setMethod('detectMissInjections',signature = 'MetaboProfile', mi <- x %>% processedData() - if (str_detect(technique(x),'GCMS')) { - mi <- mi %>% - rowSums() %>% - tibble(value = .) %>% - bind_cols(i) - } else { - mi <- mi %>% - map(rowSums) %>% - bind_cols() %>% - rowSums() %>% - as_tibble() %>% - bind_cols(i) + if (!is.list(mi)){ + mi <- list(mi) + } + + mi <- mi %>% + map(rowSums) + + if (mi %>% + names() %>% + is.na() %>% + any()) { + names(mi) <- replace(names(mi), + is.na(names(mi)), + 'NA') } - mi %>% + + mi %>% + bind_cols() %>% + rowSums() %>% + as_tibble() %>% + bind_cols(i) %>% missInject(idx = idx) }) @@ -102,7 +109,7 @@ missInject <- function(TICdat,idx){ #' @details Analysis of Variance (ANOVA) is used to detect differences in total ion count (TIC) averages between batches/blocks. #' @examples #' ## Retrieve file paths and sample information for example data -#' files <- metaboData::filePaths('FIE-HRMS','BdistachyonEcotypes')[1:2] +#' files <- metaboData::filePaths('FIE-HRMS','BdistachyonEcotypes',ask = FALSE)[1:2] #' #' info <- metaboData::runinfo('FIE-HRMS','BdistachyonEcotypes')[1:2,] #' @@ -159,21 +166,30 @@ setMethod('detectBatchDiff',signature = "MetaboProfile", TICdat <- x %>% processedData() - if (str_detect(technique(x),'GCMS')) { - TICdat <- TICdat %>% - rowSums() %>% - {tibble(Sample = 1:length(.), - TIC = ., - batch = ri[,by] %>% deframe() %>% factor())} - } else { - TICdat <- TICdat %>% - map(rowSums) %>% - bind_cols() %>% - rowid_to_column(var = 'Sample') %>% - mutate(batch = ri[,by] %>% unlist() %>% factor()) %>% - gather('Mode','TIC',-batch,-Sample) + if (!is.list(TICdat)){ + TICdat <- list(TICdat) } + TICdat <- TICdat %>% + map(rowSums) + + if (TICdat %>% + names() %>% + is.na() %>% + any()) { + names(TICdat) <- replace(names(TICdat), + is.na(names(TICdat)), + 'NA') + } + + TICdat <- TICdat %>% + bind_cols() %>% + rowid_to_column(var = 'Sample') %>% + mutate(batch = ri[,by] %>% + unlist() %>% + factor()) %>% + gather('Mode','TIC',-batch,-Sample) + diff <- batchDiff(TICdat,pthresh) return(diff) }) @@ -242,6 +258,8 @@ batchDiff <- function(TICdat,pthresh = 0.05){ #' @rdname detectPretreatmentParameters #' @description Detect pre-treatment parameters for `Binalysis` or `MetaboProfile` class objects. #' @param x S4 object of class `Binalysis`, `MetaboProfile` or `AnalysisData` +#' @param cls the name of the sample information table column containing the sample class information +#' @param QCidx QC sample class label #' @return S4 object of class `AnalysisParameters` #' @examples #' ## Retreive example file paths and sample information @@ -263,22 +281,49 @@ batchDiff <- function(TICdat,pthresh = 0.05){ #' pp #' @export -setGeneric('detectPretreatmentParameters',function(x){ +setGeneric('detectPretreatmentParameters',function(x, + cls = 'class', + QCidx = 'QC'){ standardGeneric('detectPretreatmentParameters') }) #' @rdname detectPretreatmentParameters +#' @importFrom metabolyseR changeParameter<- setMethod('detectPretreatmentParameters',signature = 'Binalysis', - function(x){ - detectPretreatment(x) + function(x,cls = 'class',QCidx = 'QC'){ + pp <- detectPretreatment(x) + + sample_info <- binneR::sampleInfo(x) + + if (!detectQC(sample_info,cls,QCidx)){ + parameters(pp,'pre-treatment')$QC <- NULL + } else { + changeParameter(pp,'QCidx','pre-treatment') <- QCidx + } + + changeParameter(pp,'cls','pre-treatment') <- cls + + return(pp) }) #' @rdname detectPretreatmentParameters setMethod('detectPretreatmentParameters',signature = 'MetaboProfile', - function(x){ - detectPretreatment(x) + function(x,cls = 'class',QCidx = 'QC'){ + pp <- detectPretreatment(x) + + sample_info <- profilePro::sampleInfo(x) + + if (!detectQC(sample_info,cls,QCidx)){ + parameters(pp,'pre-treatment')$QC <- NULL + } else { + changeParameter(pp,'QCidx','pre-treatment') <- QCidx + } + + changeParameter(pp,'cls','pre-treatment') <- cls + + return(pp) }) #' @importFrom metabolyseR parameters<- parameters @@ -314,6 +359,10 @@ detectPretreatment <- function(x){ return(pre_treat_params) } +detectQC <- function(sample_info,cls,QCidx){ + any(str_detect(sample_info[[cls]],'QC')) +} + #' Detect modelling parameters #' @rdname detectModellingParameters #' @description Detect modelling parameters for `Binalysis`, `MetaboProfile` or `Analysis` S4 classes. @@ -366,17 +415,17 @@ setMethod('detectModellingParameters',signature = 'Binalysis', setMethod('detectModellingParameters',signature = 'MetaboProfile', function(x){ - idx <- x %>% - processingParameters() %>% - .$info %>% - .$cls - - sample_information <- x %>% - profilePro::sampleInfo() %>% - select(all_of(idx)) %>% - deframe() - - detectModelling(sample_information,idx) + idx <- x %>% + processingParameters() %>% + .$info %>% + .$cls + + sample_information <- x %>% + profilePro::sampleInfo() %>% + select(all_of(idx)) %>% + deframe() + + detectModelling(sample_information,idx) }) #' @rdname detectModellingParameters diff --git a/R/export.R b/R/export.R index bdd0b66..58c03b3 100644 --- a/R/export.R +++ b/R/export.R @@ -88,10 +88,20 @@ setMethod('exportData',signature = 'MetaboProfile', file_paths <- pd %>% names() %>% map_chr(~{ + prefix <- .x + + if (prefix != 'n' | prefix != 'p'){ + prefix <- '' + } else { + prefix <- switch(prefix, + n = 'negative_mode_', + p = 'positive_mode_') + } + bind_cols(pd[[.x]],i %>% select(name)) %>% gather('Feature','Intensity',-name) %>% spread(name,Intensity) %>% - exportCSV(str_c(outPath,'/',.x,'_mode_processed_data.csv')) + exportCSV(str_c(outPath,'/',prefix,'processed_data.csv')) }) return(file_paths) diff --git a/R/sanitise.R b/R/sanitise.R index dda734f..c649964 100644 --- a/R/sanitise.R +++ b/R/sanitise.R @@ -19,7 +19,7 @@ sanitiseTable <- function(x,maxRows = 5000,sigFig = 3,maxCharacters = 100){ x <- x[seq_len(maxRows),] } - a <- map_df(x,~{ + x <- map_df(x,~{ if (typeof(.x) == 'character'){ limit_characters <- .x %>% nchar() %>% diff --git a/R/summary.R b/R/summary.R index 05b174f..5385771 100644 --- a/R/summary.R +++ b/R/summary.R @@ -21,28 +21,48 @@ setGeneric('featureSummary',function(x) standardGeneric('featureSummary')) #' @rdname featureSummary -#' @importFrom dplyr n_distinct setMethod('featureSummary',signature = 'Binalysis', function(x){ x %>% - binnedData() %>% - map(~{{ - .x %>% - rowid_to_column(var = 'Sample') %>% - gather('Feature','Intensity',-Sample) - }}) %>% - bind_rows() %>% - mutate(Mode = str_sub(Feature,1,1)) %>% - group_by(Mode) %>% - summarise( - `Number of bins` = n_distinct(Feature), - `Missing Data (%)` = round(length(which(Intensity == 0)) / - length(Intensity) * 100,2), - .groups = 'drop') %>% - {{ - .$Mode[.$Mode == 'n'] <- 'Negative' - .$Mode[.$Mode == 'p'] <- 'Positive' - . - }} + binnedData() %>% + featSummary() }) + +#' @rdname featureSummary + +setMethod('featureSummary',signature = 'MetaboProfile', + function(x){ + x %>% + processedData() %>% + featSummary() + }) + +#' @importFrom dplyr n_distinct + +featSummary <- function(x){ + + if (!is.list(x)){ + x <- list(x) + } + + x %>% + map(~{{ + .x %>% + rowid_to_column(var = 'Sample') %>% + gather('Feature','Intensity',-Sample) + }}) %>% + bind_rows() %>% + mutate(Mode = str_sub(Feature,1,1)) %>% + mutate(Mode = replace(Mode, + Mode != 'n' & Mode != 'p', + NA)) %>% + group_by(Mode) %>% + summarise( + `Number of bins` = n_distinct(Feature), + `Missing Data (%)` = round(length(which(Intensity == 0)) / + length(Intensity) * 100,2), + .groups = 'drop') %>% + mutate(Mode = replace(Mode,Mode == 'n','Negative') %>% + replace(Mode == 'p','Positive')) +} \ No newline at end of file diff --git a/docs/404.html b/docs/404.html index 688cbb6..76ae6c3 100644 --- a/docs/404.html +++ b/docs/404.html @@ -1,66 +1,27 @@ - - -
- + + + + -R/pre-treat.R
+ Source: R/pre-treat.R
preTreatModes.Rd
Pre-treat both positive and negative ionisation modes for Binalysis
and MetaboProfile
classes.
preTreatModes(processed_data, parameters, verbose = TRUE) - -# S4 method for Binalysis -preTreatModes(processed_data, parameters, verbose = TRUE) +++++# S4 method for Binalysis +preTreatModes(processed_data, parameters, verbose = TRUE) -preTreatModes(processed_data, parameters, verbose = TRUE) -# S4 method for MetaboProfile -preTreatModes(processed_data, parameters, verbose = TRUE)
Arguments
--
- -- - -processed_data -- S4 object of class
Binalysis
orMetaboProfile
- -parameters -- object of class
AnalysisParameters
containing pre-treatment parameters- -verbose -- console output
Value
+# S4 method for MetaboProfile +preTreatModes(processed_data, parameters, verbose = TRUE)++Arguments
+
- processed_data
+- +
S4 object of class
Binalysis
orMetaboProfile
- parameters
+- +
object of class
AnalysisParameters
containing pre-treatment parameters- verbose
+- +
console output
+-Value
S4 object of class
+Analysis
Examples
-library(metabolyseR) -#> -#> Attaching package: ‘metabolyseR’ -#> The following object is masked from ‘package:stats’: -#> -#> anova -#> The following objects are masked from ‘package:base’: -#> -#> raw, split - -## Retrieve file paths and sample information for example data -files <- metaboData::filePaths('FIE-HRMS','BdistachyonEcotypes')[1:2] - -info <- metaboData::runinfo('FIE-HRMS','BdistachyonEcotypes')[1:2,] - -## Detect spectral binning parameters -bp <- binneR::detectParameters(files) - -## Perform spectral binning -analysis <- binneR::binneRlyse(files, - info, - parameters = bp) -#> binneR v2.6.2 Wed Nov 17 09:52:51 2021 -#> ________________________________________________________________________________ -#> Scans: 5:14 -#> ________________________________________________________________________________ -#> Reading raw data -#> Gathering bins -#> Removing single scan events -#> Averaging intensities across scans -#> Calculating bin metrics -#> Calculating accurate m/z -#> Building intensity matrix -#> Gathering file headers -#> -#> Completed! [2.5S] - -## Declare pre-treatment parameters -pre_treatment_parameters <- analysisParameters('pre-treatment') -parameters(pre_treatment_parameters, - 'pre-treatment') <- preTreatmentParameters( - list( - occupancyFilter = 'maximum', - impute = 'all', - transform = 'TICnorm' - ) -) -changeParameter(pre_treatment_parameters,'parallel') <- 'no' - -## Perform pre-treatment -pre_treated_data <- preTreatModes(analysis, - pre_treatment_parameters) -#> -#> metabolyseR v0.14.5 Wed Nov 17 09:52:53 2021 -#> ________________________________________________________________________________ -#> Parameters: -#> pre-treatment -#> occupancyFilter -#> maximum -#> cls = class -#> occupancy = 2/3 -#> impute -#> all -#> occupancy = 2/3 -#> parallel = no -#> seed = 1234 -#> transform -#> TICnorm -#> -#> ________________________________________________________________________________ -#> -#> Negative mode… Negative mode ✓ [1.1S] -#> Positive mode… Positive mode ✓ [0.9S] -#> ________________________________________________________________________________ -#> -#> Complete! [2.1S] -#> -++Examples
++library(metabolyseR) +#> +#> Attaching package: ‘metabolyseR’ +#> The following object is masked from ‘package:stats’: +#> +#> anova +#> The following objects are masked from ‘package:base’: +#> +#> raw, split + +## Retrieve file paths and sample information for example data +files <- metaboData::filePaths('FIE-HRMS','BdistachyonEcotypes')[1:2] + +info <- metaboData::runinfo('FIE-HRMS','BdistachyonEcotypes')[1:2,] + +## Detect spectral binning parameters +bp <- binneR::detectParameters(files) + +## Perform spectral binning +analysis <- binneR::binneRlyse(files, + info, + parameters = bp) +#> binneR v2.6.2 Thu Dec 16 13:30:46 2021 +#> ________________________________________________________________________________ +#> Scans: 5:14 +#> ________________________________________________________________________________ +#> Reading raw data +#> Gathering bins +#> Removing single scan events +#> Averaging intensities across scans +#> Calculating bin metrics +#> Calculating accurate m/z +#> Building intensity matrix +#> Gathering file headers +#> +#> Completed! [1.2S] + +## Declare pre-treatment parameters +pre_treatment_parameters <- analysisParameters('pre-treatment') +parameters(pre_treatment_parameters, + 'pre-treatment') <- preTreatmentParameters( + list( + occupancyFilter = 'maximum', + impute = 'all', + transform = 'TICnorm' + ) +) +changeParameter(pre_treatment_parameters,'parallel') <- 'no' + +## Perform pre-treatment +pre_treated_data <- preTreatModes(analysis, + pre_treatment_parameters) +#> +#> metabolyseR v0.14.6 Thu Dec 16 13:30:47 2021 +#> ________________________________________________________________________________ +#> Parameters: +#> pre-treatment +#> occupancyFilter +#> maximum +#> cls = class +#> occupancy = 2/3 +#> impute +#> all +#> occupancy = 2/3 +#> parallel = no +#> seed = 1234 +#> transform +#> TICnorm +#> +#> ________________________________________________________________________________ +#> +#> Negative mode… Negative mode ✔ [0.3S] +#> Positive mode… Positive mode ✔ [0.4S] +#> ________________________________________________________________________________ +#> +#> Complete! [0.7S] +#> +
R/reduce.R
+ Source: R/reduce.R
reduce.Rd
Reduce an analysis by removing isotopic, adduct or unknown features.
reduce(x, isotopes = TRUE, adducts = TRUE, unknowns = FALSE) - -# S4 method for Analysis -reduce(x, isotopes = TRUE, adducts = TRUE, unknowns = FALSE) - -# S4 method for AnalysisData -reduce(x, isotopes = TRUE, adducts = TRUE, unknowns = FALSE)- -
x | -S4 class of class |
-
---|---|
isotopes | -TRUE/FALSE remove isotopic features. |
-
adducts | -TRUE/FALSE remove multiple adduct features. |
-
unknowns | -TRUE/FALSE remove unassigned features. |
-
reduce(x, isotopes = TRUE, adducts = TRUE, unknowns = FALSE)
- S4 object of class Analysis
or AnalysisData
with features reduced
- Details
+# S4 method for Analysis
+reduce(x, isotopes = TRUE, adducts = TRUE, unknowns = FALSE)
+
+# S4 method for AnalysisData
+reduce(x, isotopes = TRUE, adducts = TRUE, unknowns = FALSE)
S4 class of class Analysis
or AnalysisData
TRUE/FALSE remove isotopic features.
TRUE/FALSE remove multiple adduct features.
TRUE/FALSE remove unassigned features.
S4 object of class Analysis
or AnalysisData
with features reduced
Isotope and adduct features are filtered based on the maximum intensity peak for each molecular formulas.
+library(MFassign) -#> Loading required package: ggraph -#> Loading required package: ggplot2 - -## Assign molecular formulas -p <- assignmentParameters('FIE') - -assignment <- assignMFs(peakData,p) -#> -#> MFassign v0.7.10 Wed Nov 17 09:52:56 2021 -#> ________________________________________________________________________________ -#> Assignment Parameters: -#> -#> Technique: FIE -#> Max M: 1000 -#> Max MF score: 5 -#> PPM threshold: 5 -#> Relationship limit: 0.001 -#> -#> Adducts: -#> n: [M-H]1-, [M+Cl]1-, [M+K-2H]1-, [M-2H]2-, [M+Cl37]1-, [2M-H]1- -#> p: [M+H]1+, [M+K]1+, [M+Na]1+, [M+K41]1+, [M+NH4]1+, [M+2H]2+, [2M+H]1+ -#> Isotopes: 13C, 18O, 13C2 -#> Transformations: M - [O] + [NH2], M - [OH] + [NH2], M + [H2], M - [H2] + [O], M - [H] + [CH3], M - [H] + [NH2], M - [H] + [OH], M + [H2O], M - [H3] + [H2O], M - [H] + [CHO2], M - [H] + [SO3], M - [H] + [PO3H2] -#> ________________________________________________________________________________ -#> No. m/z: 9 -#> Calculating correlations … -#> Calculating correlations ✓ [24 correlations] [0.1S] -#> Filtering correlations … -#> Filtering correlations ✓ [24 correlations] [0S] -#> Preparing correlations … -#> Preparing correlations ✓ [0S] -#> Calculating relationships … -#> Calculating relationships ✓ [12.4S] -#> Adduct & isotope assignment … -#> Adduct & isotope assignment ✓ [15.7S] -#> Transformation assignment iteration 1 … -#> Transformation assignment iteration 1 ✓ [0S] -#> ________________________________________________________________________________ -#> -#> Complete! [28.2S] - -## Retrieve assigned data -assigned_data <- metabolyseR::analysisData( - assignedData(assignment), - tibble::tibble(sample = seq_len(nrow(peakData))) - ) - -reduced_data <- metaboMisc::reduce(assigned_data) - -reduced_data -#> -#> AnalysisData object containing: -#> -#> Samples: 60 -#> Features: 1 -#> Info: 1 -#> -+
library(MFassign)
+#> Loading required package: ggraph
+#> Loading required package: ggplot2
+
+## Assign molecular formulas
+p <- assignmentParameters('FIE')
+
+assignment <- assignMFs(peakData,p)
+#>
+#> MFassign v0.7.10 Thu Dec 16 13:30:49 2021
+#> ________________________________________________________________________________
+#> Assignment Parameters:
+#>
+#> Technique: FIE
+#> Max M: 1000
+#> Max MF score: 5
+#> PPM threshold: 5
+#> Relationship limit: 0.001
+#>
+#> Adducts:
+#> n: [M-H]1-, [M+Cl]1-, [M+K-2H]1-, [M-2H]2-, [M+Cl37]1-, [2M-H]1-
+#> p: [M+H]1+, [M+K]1+, [M+Na]1+, [M+K41]1+, [M+NH4]1+, [M+2H]2+, [2M+H]1+
+#> Isotopes: 13C, 18O, 13C2
+#> Transformations: M - [O] + [NH2], M - [OH] + [NH2], M + [H2], M - [H2] + [O], M - [H] + [CH3], M - [H] + [NH2], M - [H] + [OH], M + [H2O], M - [H3] + [H2O], M - [H] + [CHO2], M - [H] + [SO3], M - [H] + [PO3H2]
+#> ________________________________________________________________________________
+#> No. m/z: 9
+#> Calculating correlations …
+#> Calculating correlations ✔ [24 correlations] [0S]
+#> Filtering correlations …
+#> Filtering correlations ✔ [24 correlations] [0S]
+#> Preparing correlations …
+#> Preparing correlations ✔ [0S]
+#> Calculating relationships …
+#> Calculating relationships ✔ [4.9S]
+#> Adduct & isotope assignment …
+#> Adduct & isotope assignment ✔ [8.3S]
+#> Transformation assignment iteration 1 …
+#> Transformation assignment iteration 1 ✔ [0S]
+#> ________________________________________________________________________________
+#>
+#> Complete! [13.4S]
+
+## Retrieve assigned data
+assigned_data <- metabolyseR::analysisData(
+ assignedData(assignment),
+ tibble::tibble(sample = seq_len(nrow(peakData)))
+ )
+
+reduced_data <- metaboMisc::reduce(assigned_data)
+
+reduced_data
+#>
+#> AnalysisData object containing:
+#>
+#> Samples: 60
+#> Features: 1
+#> Info: 1
+#>
+
These objects are imported from other packages. Follow the links below to see their documentation.
-%>%
%>%