Skip to content

Commit 5e859b0

Browse files
authored
Merge pull request #44 from jasenfinch/devel
v0.7.9
2 parents 5b2df43 + 40aa369 commit 5e859b0

18 files changed

+169
-249
lines changed

DESCRIPTION

Lines changed: 38 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
Package: MFassign
22
Title: Molecular Formula Assignment for High Resolution metabolomics
3-
Version: 0.7.8
3+
Version: 0.7.9
44
Authors@R: person("Jasen", "Finch", email = "jsf9@aber.ac.uk", role = c("aut", "cre"))
55
Description: Molecular formula assignment for high resolution metabolomics.
66
Depends: R (>= 3.5.0),
@@ -24,46 +24,49 @@ Imports: CHNOSZ,
2424
mzAnnotation,
2525
metabolyseR,
2626
ggrepel,
27-
graphlayouts
27+
graphlayouts,
28+
future,
29+
furrr
2830
Remotes: jasenfinch/mzAnnotation,
2931
jasenfinch/metabolyseR
3032
License: GPL (>= 3)
3133
Encoding: UTF-8
3234
LazyData: true
3335
RoxygenNote: 7.1.1
3436
Collate:
35-
'allGenerics.R'
36-
'allClasses.R'
37-
'prepCorrelations-method.R'
38-
'addIsoAssign-method.R'
39-
'transformationAssign-method.R'
40-
'relationships-method.R'
41-
'assignmentParameters.R'
42-
'assignMFs.R'
43-
'MFgen.R'
44-
'MFscore.R'
45-
'addIsoScore.R'
46-
'addMFs.R'
47-
'calcComponents.R'
48-
'assignMethods.R'
49-
'peakData.R'
50-
'LCassignment.R'
51-
'MFassign.R'
52-
'FIEassignment.R'
53-
'doAssignment-method.R'
54-
'continueAssignment.R'
55-
'show-method.R'
56-
'access-methods.R'
57-
'summariseAssignment-method.R'
58-
'plotNetwork-method.R'
59-
'networkComponents.R'
60-
'addNames.R'
61-
'eliminate.R'
62-
'recalcComponents.R'
63-
'plotAdductDist-method.R'
64-
'plotFeatureSolutions.R'
65-
'calcCorrelations-method.R'
66-
'filterCorrelations-method.R'
67-
'plotSpectrum-method.R'
37+
allGenerics.R
38+
allClasses.R
39+
prepCorrelations-method.R
40+
addIsoAssign-method.R
41+
transformationAssign-method.R
42+
relationships-method.R
43+
assignmentParameters.R
44+
assignMFs.R
45+
MFgen.R
46+
MFscore.R
47+
addIsoScore.R
48+
addMFs.R
49+
calcComponents.R
50+
assignMethods.R
51+
peakData.R
52+
LCassignment.R
53+
MFassign.R
54+
FIEassignment.R
55+
doAssignment-method.R
56+
continueAssignment.R
57+
show-method.R
58+
access-methods.R
59+
summariseAssignment-method.R
60+
plotNetwork-method.R
61+
networkComponents.R
62+
addNames.R
63+
eliminate.R
64+
recalcComponents.R
65+
plotAdductDist-method.R
66+
plotFeatureSolutions.R
67+
calcCorrelations-method.R
68+
filterCorrelations-method.R
69+
plotSpectrum-method.R
70+
reexports.R
6871
Suggests: testthat,
6972
covr

NAMESPACE

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ export(assignmentParameters)
55
export(continueAssignment)
66
export(edges)
77
export(nodes)
8+
export(plan)
89
exportClasses(Assignment)
910
exportClasses(AssignmentParameters)
1011
exportMethods(assignedData)
@@ -33,15 +34,19 @@ importFrom(dplyr,group_by)
3334
importFrom(dplyr,inner_join)
3435
importFrom(dplyr,left_join)
3536
importFrom(dplyr,mutate)
37+
importFrom(dplyr,mutate_at)
3638
importFrom(dplyr,n)
3739
importFrom(dplyr,rename)
3840
importFrom(dplyr,rowwise)
3941
importFrom(dplyr,select)
4042
importFrom(dplyr,semi_join)
4143
importFrom(dplyr,slice_sample)
4244
importFrom(dplyr,summarise)
43-
importFrom(dplyr,tbl_df)
4445
importFrom(dplyr,ungroup)
46+
importFrom(dplyr,vars)
47+
importFrom(furrr,furrr_options)
48+
importFrom(furrr,future_map)
49+
importFrom(future,plan)
4550
importFrom(ggplot2,aes)
4651
importFrom(ggplot2,coord_fixed)
4752
importFrom(ggplot2,element_blank)
@@ -100,10 +105,6 @@ importFrom(mzAnnotation,relationshipCalculator)
100105
importFrom(mzAnnotation,transformMF)
101106
importFrom(mzAnnotation,transformations)
102107
importFrom(parallel,detectCores)
103-
importFrom(parallel,makeCluster)
104-
importFrom(parallel,parApply)
105-
importFrom(parallel,parLapply)
106-
importFrom(parallel,stopCluster)
107108
importFrom(patchwork,plot_annotation)
108109
importFrom(patchwork,wrap_plots)
109110
importFrom(purrr,map)

R/addIsoAssign-method.R

Lines changed: 10 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
#' @importFrom stringr str_detect
33
#' @importFrom mzAnnotation calcM calcMZ ppmError
44
#' @importFrom igraph vertex.attributes V
5-
#' @importFrom parallel parLapply
5+
#' @importFrom furrr furrr_options
66

77
setMethod('addIsoAssign',signature = 'Assignment',
88
function(assignment){
@@ -30,21 +30,12 @@ setMethod('addIsoAssign',signature = 'Assignment',
3030

3131
nM <- nrow(M)
3232

33-
slaves <- nrow(M) / 100 * mean(M$M)/100
34-
slaves <- ceiling(slaves)
35-
36-
if (slaves > parameters@nCores) {
37-
slaves <- parameters@nCores
38-
}
39-
40-
clus <- makeCluster(slaves,type = parameters@clusterType)
41-
4233
MF <- M %>%
4334
ungroup() %>%
4435
slice_sample(n = nM) %>%
4536
split(1:nrow(.)) %>%
46-
parLapply(cl = clus,function(x,parameters,M){
47-
mf <- MFgen(x$M,x$mz,ppm = parameters@ppm)
37+
future_map(~{
38+
mf <- MFgen(.x$M,.x$mz,ppm = parameters@ppm)
4839

4940
if (nrow(mf) > 0) {
5041
mf %>%
@@ -57,18 +48,19 @@ setMethod('addIsoAssign',signature = 'Assignment',
5748
rowwise() %>%
5849
mutate(Score = MFscore(MF),
5950
`PPM Error` = abs(`PPM Error`),
60-
AddIsoScore = addIsoScore(Adduct,Isotope,parameters@adducts,parameters@isotopes)) %>%
61-
tbl_df() %>%
62-
filter(Score == min(Score,na.rm = T)) %>%
51+
AddIsoScore = addIsoScore(Adduct,
52+
Isotope,
53+
parameters@adducts,
54+
parameters@isotopes)) %>%
55+
ungroup() %>%
56+
filter(Score == min(Score,na.rm = TRUE)) %>%
6357
filter(Score < parameters@maxMFscore)
6458
} else {
6559
return(NULL)
6660
}
67-
},parameters = parameters,M = M) %>%
61+
},.options = furrr_options(seed = 1234)) %>%
6862
bind_rows()
6963

70-
stopCluster(clus)
71-
7264
rel <- rel %>%
7365
addMFs(MF) %>%
7466
filter(MF1 == MF2) %>%

R/allClasses.R

Lines changed: 33 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,7 @@
1515
#' @slot adductRules tibble containing adduct formation rules as returned by mzAnnotation::adducts()
1616
#' @slot isotopeRules tibble containing isotope rules as returned by mzAnnotation::isotopes()
1717
#' @slot transformationRules tibble containing transformation rules as returned by mzAnnotation::transformations()
18-
#' @slot nCores number of cores to use for parallisation
19-
#' @slot clusterType cluster type to use for parallisation
18+
#' @importFrom mzAnnotation transformations
2019
#' @export
2120

2221
setClass('AssignmentParameters',
@@ -34,9 +33,26 @@ setClass('AssignmentParameters',
3433
transformations = 'character',
3534
adductRules = 'tbl_df',
3635
isotopeRules = 'tbl_df',
37-
transformationRules = 'tbl_df',
38-
nCores = 'numeric',
39-
clusterType = 'character'
36+
transformationRules = 'tbl_df'
37+
),
38+
prototype = list(
39+
technique = 'FIE',
40+
correlations = list(method = 'pearson',pAdjustMethod = 'bonferroni',corPvalue = 0.05),
41+
filter = list(rthresh = 0.7,n = 100000,rIncrement = 0.01,nIncrement = 20000),
42+
maxM = 1000,
43+
maxMFscore = 5,
44+
ppm = 5,
45+
limit = 0.001,
46+
RTwindow = numeric(),
47+
isotopes = c('13C','18O','13C2'),
48+
adducts = list(n = c("[M-H]1-", "[M+Cl]1-", "[M+K-2H]1-",
49+
"[M-2H]2-", "[M+Cl37]1-","[2M-H]1-"),
50+
p = c('[M+H]1+','[M+K]1+','[M+Na]1+','[M+K41]1+',
51+
'[M+NH4]1+','[M+2H]2+','[2M+H]1+')),
52+
transformations = transformations()$`MF Change`,
53+
adductRules = adducts(),
54+
isotopeRules = isotopes(),
55+
transformationRules = transformations()
4056
))
4157

4258
#' Assignment
@@ -65,4 +81,16 @@ setClass('Assignment',
6581
addIsoAssign = 'list',
6682
transAssign = 'list',
6783
assignments = 'tbl_df'
84+
),
85+
prototype = list(
86+
log = list(date = date(),verbose = TRUE),
87+
flags = character(),
88+
parameters = new('AssignmentParameters'),
89+
data = tibble(),
90+
correlations = tibble(),
91+
preparedCorrelations = tibble(),
92+
relationships = tibble(),
93+
addIsoAssign = list(),
94+
transAssign = list(),
95+
assignments = tibble()
6896
))

R/assignMFs.R

Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@
99
#' @importFrom lubridate seconds_to_period
1010
#' @importFrom utils capture.output
1111
#' @examples
12+
#' plan(future::sequential)
1213
#' p <- assignmentParameters('FIE')
13-
#' p@nCores <- 2
1414
#'
1515
#' assignment <- assignMFs(peakData,p)
1616
#'
@@ -37,17 +37,9 @@ assignMFs <- function(dat,parameters,verbose = TRUE) {
3737
}
3838

3939
assignment <- new('Assignment',
40-
log = list(date = date(),verbose = verbose),
41-
flags = character(),
42-
parameters = parameters,
4340
data = dat,
44-
correlations = tibble(),
45-
preparedCorrelations = tibble(),
46-
relationships = tibble(),
47-
addIsoAssign = list(),
48-
transAssign = list(),
49-
assignments = tibble()
50-
)
41+
parameters = parameters)
42+
assignment@log$verbose <- verbose
5143

5244
assignment <- assignment %>%
5345
doAssignment()

R/assignmentParameters.R

Lines changed: 4 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
#' @param technique technique to use for assignment. \code{NULL} prints available techniques
44
#' @importFrom parallel detectCores
55
#' @importFrom methods new
6-
#' @importFrom mzAnnotation transformations
76
#' @export
87

98
assignmentParameters <- function(technique = NULL){
@@ -12,77 +11,22 @@ assignmentParameters <- function(technique = NULL){
1211
cat('\nAvailable Techniques:',str_c('\n\t\t\t',str_c(availTechniques,collapse = '\n\t\t\t'),'\n'))
1312
p <- NULL
1413
} else {
14+
1515
if (technique == 'FIE') {
16-
p <- new('AssignmentParameters',
17-
technique = 'FIE',
18-
correlations = list(method = 'pearson',pAdjustMethod = 'bonferroni',corPvalue = 0.05),
19-
filter = list(rthresh = 0.7,n = 100000,rIncrement = 0.01,nIncrement = 20000),
20-
maxM = 600,
21-
maxMFscore = 5,
22-
ppm = 5,
23-
limit = 0.001,
24-
RTwindow = numeric(),
25-
isotopes = c('13C','18O','13C2'),
26-
adducts = list(n = c("[M-H]1-", "[M+Cl]1-", "[M+K-2H]1-",
27-
"[M-2H]2-", "[M+Cl37]1-","[2M-H]1-"),
28-
p = c('[M+H]1+','[M+K]1+','[M+Na]1+','[M+K41]1+',
29-
'[M+NH4]1+','[M+2H]2+','[2M+H]1+')),
30-
transformations = transformations()$`MF Change`,
31-
adductRules = adducts(),
32-
isotopeRules = isotopes(),
33-
transformationRules = transformations(),
34-
nCores = detectCores(),
35-
clusterType = 'FORK'
36-
)
16+
p <- new('AssignmentParameters')
3717
}
3818
if (technique == 'RP-LC') {
3919
p <- new('AssignmentParameters',
4020
technique = 'RP-LC',
41-
correlations = list(method = 'pearson',pAdjustMethod = 'bonferroni',corPvalue = 0.05),
42-
filter = list(rthresh = 0.7,n = 100000,rIncrement = 0.01,nIncrement = 20000),
43-
maxM = 600,
44-
maxMFscore = 5,
45-
ppm = 5,
46-
limit = 0.001,
47-
RTwindow = 1/60,
48-
isotopes = c('13C','18O','13C2'),
49-
adducts = list(n = c("[M-H]1-", "[M+Cl]1-", "[M+K-2H]1-",
50-
"[M-2H]2-", "[M+Cl37]1-","[2M-H]1-"),
51-
p = c('[M+H]1+','[M+K]1+','[M+Na]1+','[M+K41]1+',
52-
'[M+NH4]1+','[M+2H]2+','[2M+H]1+')),
53-
transformations = transformations()$`MF Change`,
54-
adductRules = adducts(),
55-
isotopeRules = isotopes(),
56-
transformationRules = transformations(),
57-
nCores = detectCores(),
58-
clusterType = 'FORK'
21+
RTwindow = 1/60
5922
)
6023
}
6124
if (technique == 'NP-LC') {
6225
p <- new('AssignmentParameters',
6326
technique = 'NP-LC',
64-
correlations = list(method = 'pearson',pAdjustMethod = 'bonferroni',corPvalue = 0.05),
65-
filter = list(rthresh = 0.7,n = 100000,rIncrement = 0.01,nIncrement = 20000),
66-
maxM = 600,
67-
maxMFscore = 5,
68-
ppm = 5,
69-
limit = 0.001,
70-
RTwindow = 1/60,
71-
isotopes = c('13C','18O','13C2'),
72-
adducts = list(n = c('[M-H]1-','[M+Hac-H]1-','[M-2H]2-','[2M-H]1-'),
73-
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+')),
74-
transformations = transformations()$`MF Change`,
75-
adductRules = adducts(),
76-
isotopeRules = isotopes(),
77-
transformationRules = transformations(),
78-
nCores = detectCores(),
79-
clusterType = 'FORK'
27+
RTwindow = 1/60
8028
)
8129
}
82-
if (.Platform$OS.type == 'windows') {
83-
p@clusterType <- 'PSOCK'
84-
}
85-
p@nCores <- {detectCores() * 0.75} %>% round()
8630
}
8731
return(p)
8832
}

0 commit comments

Comments
 (0)