Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
73 changes: 38 additions & 35 deletions DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
Package: MFassign
Title: Molecular Formula Assignment for High Resolution metabolomics
Version: 0.7.8
Version: 0.7.9
Authors@R: person("Jasen", "Finch", email = "jsf9@aber.ac.uk", role = c("aut", "cre"))
Description: Molecular formula assignment for high resolution metabolomics.
Depends: R (>= 3.5.0),
Expand All @@ -24,46 +24,49 @@ Imports: CHNOSZ,
mzAnnotation,
metabolyseR,
ggrepel,
graphlayouts
graphlayouts,
future,
furrr
Remotes: jasenfinch/mzAnnotation,
jasenfinch/metabolyseR
License: GPL (>= 3)
Encoding: UTF-8
LazyData: true
RoxygenNote: 7.1.1
Collate:
'allGenerics.R'
'allClasses.R'
'prepCorrelations-method.R'
'addIsoAssign-method.R'
'transformationAssign-method.R'
'relationships-method.R'
'assignmentParameters.R'
'assignMFs.R'
'MFgen.R'
'MFscore.R'
'addIsoScore.R'
'addMFs.R'
'calcComponents.R'
'assignMethods.R'
'peakData.R'
'LCassignment.R'
'MFassign.R'
'FIEassignment.R'
'doAssignment-method.R'
'continueAssignment.R'
'show-method.R'
'access-methods.R'
'summariseAssignment-method.R'
'plotNetwork-method.R'
'networkComponents.R'
'addNames.R'
'eliminate.R'
'recalcComponents.R'
'plotAdductDist-method.R'
'plotFeatureSolutions.R'
'calcCorrelations-method.R'
'filterCorrelations-method.R'
'plotSpectrum-method.R'
allGenerics.R
allClasses.R
prepCorrelations-method.R
addIsoAssign-method.R
transformationAssign-method.R
relationships-method.R
assignmentParameters.R
assignMFs.R
MFgen.R
MFscore.R
addIsoScore.R
addMFs.R
calcComponents.R
assignMethods.R
peakData.R
LCassignment.R
MFassign.R
FIEassignment.R
doAssignment-method.R
continueAssignment.R
show-method.R
access-methods.R
summariseAssignment-method.R
plotNetwork-method.R
networkComponents.R
addNames.R
eliminate.R
recalcComponents.R
plotAdductDist-method.R
plotFeatureSolutions.R
calcCorrelations-method.R
filterCorrelations-method.R
plotSpectrum-method.R
reexports.R
Suggests: testthat,
covr
11 changes: 6 additions & 5 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ export(assignmentParameters)
export(continueAssignment)
export(edges)
export(nodes)
export(plan)
exportClasses(Assignment)
exportClasses(AssignmentParameters)
exportMethods(assignedData)
Expand Down Expand Up @@ -33,15 +34,19 @@ importFrom(dplyr,group_by)
importFrom(dplyr,inner_join)
importFrom(dplyr,left_join)
importFrom(dplyr,mutate)
importFrom(dplyr,mutate_at)
importFrom(dplyr,n)
importFrom(dplyr,rename)
importFrom(dplyr,rowwise)
importFrom(dplyr,select)
importFrom(dplyr,semi_join)
importFrom(dplyr,slice_sample)
importFrom(dplyr,summarise)
importFrom(dplyr,tbl_df)
importFrom(dplyr,ungroup)
importFrom(dplyr,vars)
importFrom(furrr,furrr_options)
importFrom(furrr,future_map)
importFrom(future,plan)
importFrom(ggplot2,aes)
importFrom(ggplot2,coord_fixed)
importFrom(ggplot2,element_blank)
Expand Down Expand Up @@ -100,10 +105,6 @@ importFrom(mzAnnotation,relationshipCalculator)
importFrom(mzAnnotation,transformMF)
importFrom(mzAnnotation,transformations)
importFrom(parallel,detectCores)
importFrom(parallel,makeCluster)
importFrom(parallel,parApply)
importFrom(parallel,parLapply)
importFrom(parallel,stopCluster)
importFrom(patchwork,plot_annotation)
importFrom(patchwork,wrap_plots)
importFrom(purrr,map)
Expand Down
28 changes: 10 additions & 18 deletions R/addIsoAssign-method.R
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
#' @importFrom stringr str_detect
#' @importFrom mzAnnotation calcM calcMZ ppmError
#' @importFrom igraph vertex.attributes V
#' @importFrom parallel parLapply
#' @importFrom furrr furrr_options

setMethod('addIsoAssign',signature = 'Assignment',
function(assignment){
Expand Down Expand Up @@ -30,21 +30,12 @@ setMethod('addIsoAssign',signature = 'Assignment',

nM <- nrow(M)

slaves <- nrow(M) / 100 * mean(M$M)/100
slaves <- ceiling(slaves)

if (slaves > parameters@nCores) {
slaves <- parameters@nCores
}

clus <- makeCluster(slaves,type = parameters@clusterType)

MF <- M %>%
ungroup() %>%
slice_sample(n = nM) %>%
split(1:nrow(.)) %>%
parLapply(cl = clus,function(x,parameters,M){
mf <- MFgen(x$M,x$mz,ppm = parameters@ppm)
future_map(~{
mf <- MFgen(.x$M,.x$mz,ppm = parameters@ppm)

if (nrow(mf) > 0) {
mf %>%
Expand All @@ -57,18 +48,19 @@ setMethod('addIsoAssign',signature = 'Assignment',
rowwise() %>%
mutate(Score = MFscore(MF),
`PPM Error` = abs(`PPM Error`),
AddIsoScore = addIsoScore(Adduct,Isotope,parameters@adducts,parameters@isotopes)) %>%
tbl_df() %>%
filter(Score == min(Score,na.rm = T)) %>%
AddIsoScore = addIsoScore(Adduct,
Isotope,
parameters@adducts,
parameters@isotopes)) %>%
ungroup() %>%
filter(Score == min(Score,na.rm = TRUE)) %>%
filter(Score < parameters@maxMFscore)
} else {
return(NULL)
}
},parameters = parameters,M = M) %>%
},.options = furrr_options(seed = 1234)) %>%
bind_rows()

stopCluster(clus)

rel <- rel %>%
addMFs(MF) %>%
filter(MF1 == MF2) %>%
Expand Down
38 changes: 33 additions & 5 deletions R/allClasses.R
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,7 @@
#' @slot adductRules tibble containing adduct formation rules as returned by mzAnnotation::adducts()
#' @slot isotopeRules tibble containing isotope rules as returned by mzAnnotation::isotopes()
#' @slot transformationRules tibble containing transformation rules as returned by mzAnnotation::transformations()
#' @slot nCores number of cores to use for parallisation
#' @slot clusterType cluster type to use for parallisation
#' @importFrom mzAnnotation transformations
#' @export

setClass('AssignmentParameters',
Expand All @@ -34,9 +33,26 @@ setClass('AssignmentParameters',
transformations = 'character',
adductRules = 'tbl_df',
isotopeRules = 'tbl_df',
transformationRules = 'tbl_df',
nCores = 'numeric',
clusterType = 'character'
transformationRules = 'tbl_df'
),
prototype = list(
technique = 'FIE',
correlations = list(method = 'pearson',pAdjustMethod = 'bonferroni',corPvalue = 0.05),
filter = list(rthresh = 0.7,n = 100000,rIncrement = 0.01,nIncrement = 20000),
maxM = 1000,
maxMFscore = 5,
ppm = 5,
limit = 0.001,
RTwindow = numeric(),
isotopes = c('13C','18O','13C2'),
adducts = list(n = c("[M-H]1-", "[M+Cl]1-", "[M+K-2H]1-",
"[M-2H]2-", "[M+Cl37]1-","[2M-H]1-"),
p = c('[M+H]1+','[M+K]1+','[M+Na]1+','[M+K41]1+',
'[M+NH4]1+','[M+2H]2+','[2M+H]1+')),
transformations = transformations()$`MF Change`,
adductRules = adducts(),
isotopeRules = isotopes(),
transformationRules = transformations()
))

#' Assignment
Expand Down Expand Up @@ -65,4 +81,16 @@ setClass('Assignment',
addIsoAssign = 'list',
transAssign = 'list',
assignments = 'tbl_df'
),
prototype = list(
log = list(date = date(),verbose = TRUE),
flags = character(),
parameters = new('AssignmentParameters'),
data = tibble(),
correlations = tibble(),
preparedCorrelations = tibble(),
relationships = tibble(),
addIsoAssign = list(),
transAssign = list(),
assignments = tibble()
))
14 changes: 3 additions & 11 deletions R/assignMFs.R
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@
#' @importFrom lubridate seconds_to_period
#' @importFrom utils capture.output
#' @examples
#' plan(future::sequential)
#' p <- assignmentParameters('FIE')
#' p@nCores <- 2
#'
#' assignment <- assignMFs(peakData,p)
#'
Expand All @@ -37,17 +37,9 @@ assignMFs <- function(dat,parameters,verbose = TRUE) {
}

assignment <- new('Assignment',
log = list(date = date(),verbose = verbose),
flags = character(),
parameters = parameters,
data = dat,
correlations = tibble(),
preparedCorrelations = tibble(),
relationships = tibble(),
addIsoAssign = list(),
transAssign = list(),
assignments = tibble()
)
parameters = parameters)
assignment@log$verbose <- verbose

assignment <- assignment %>%
doAssignment()
Expand Down
64 changes: 4 additions & 60 deletions R/assignmentParameters.R
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
#' @param technique technique to use for assignment. \code{NULL} prints available techniques
#' @importFrom parallel detectCores
#' @importFrom methods new
#' @importFrom mzAnnotation transformations
#' @export

assignmentParameters <- function(technique = NULL){
Expand All @@ -12,77 +11,22 @@ assignmentParameters <- function(technique = NULL){
cat('\nAvailable Techniques:',str_c('\n\t\t\t',str_c(availTechniques,collapse = '\n\t\t\t'),'\n'))
p <- NULL
} else {

if (technique == 'FIE') {
p <- new('AssignmentParameters',
technique = 'FIE',
correlations = list(method = 'pearson',pAdjustMethod = 'bonferroni',corPvalue = 0.05),
filter = list(rthresh = 0.7,n = 100000,rIncrement = 0.01,nIncrement = 20000),
maxM = 600,
maxMFscore = 5,
ppm = 5,
limit = 0.001,
RTwindow = numeric(),
isotopes = c('13C','18O','13C2'),
adducts = list(n = c("[M-H]1-", "[M+Cl]1-", "[M+K-2H]1-",
"[M-2H]2-", "[M+Cl37]1-","[2M-H]1-"),
p = c('[M+H]1+','[M+K]1+','[M+Na]1+','[M+K41]1+',
'[M+NH4]1+','[M+2H]2+','[2M+H]1+')),
transformations = transformations()$`MF Change`,
adductRules = adducts(),
isotopeRules = isotopes(),
transformationRules = transformations(),
nCores = detectCores(),
clusterType = 'FORK'
)
p <- new('AssignmentParameters')
}
if (technique == 'RP-LC') {
p <- new('AssignmentParameters',
technique = 'RP-LC',
correlations = list(method = 'pearson',pAdjustMethod = 'bonferroni',corPvalue = 0.05),
filter = list(rthresh = 0.7,n = 100000,rIncrement = 0.01,nIncrement = 20000),
maxM = 600,
maxMFscore = 5,
ppm = 5,
limit = 0.001,
RTwindow = 1/60,
isotopes = c('13C','18O','13C2'),
adducts = list(n = c("[M-H]1-", "[M+Cl]1-", "[M+K-2H]1-",
"[M-2H]2-", "[M+Cl37]1-","[2M-H]1-"),
p = c('[M+H]1+','[M+K]1+','[M+Na]1+','[M+K41]1+',
'[M+NH4]1+','[M+2H]2+','[2M+H]1+')),
transformations = transformations()$`MF Change`,
adductRules = adducts(),
isotopeRules = isotopes(),
transformationRules = transformations(),
nCores = detectCores(),
clusterType = 'FORK'
RTwindow = 1/60
)
}
if (technique == 'NP-LC') {
p <- new('AssignmentParameters',
technique = 'NP-LC',
correlations = list(method = 'pearson',pAdjustMethod = 'bonferroni',corPvalue = 0.05),
filter = list(rthresh = 0.7,n = 100000,rIncrement = 0.01,nIncrement = 20000),
maxM = 600,
maxMFscore = 5,
ppm = 5,
limit = 0.001,
RTwindow = 1/60,
isotopes = c('13C','18O','13C2'),
adducts = list(n = c('[M-H]1-','[M+Hac-H]1-','[M-2H]2-','[2M-H]1-'),
p = c('[M+H]1+','[M+NH4]1+','[M+H-H2O]1+','[M+ACN+H]1+','[M+Na]1+','[M+2H]2+','[2M+H]1+')),
transformations = transformations()$`MF Change`,
adductRules = adducts(),
isotopeRules = isotopes(),
transformationRules = transformations(),
nCores = detectCores(),
clusterType = 'FORK'
RTwindow = 1/60
)
}
if (.Platform$OS.type == 'windows') {
p@clusterType <- 'PSOCK'
}
p@nCores <- {detectCores() * 0.75} %>% round()
}
return(p)
}
Loading