Skip to content

Commit c918028

Browse files
authored
Merge pull request #4 from computational-metabolomics/tk_workflow
Tk workflow
2 parents de32128 + d4dced2 commit c918028

File tree

88 files changed

+833
-355
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

88 files changed

+833
-355
lines changed

.travis.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
language: r
2-
r: bioc-devel
2+
r: bioc-release
33
sudo: false
44
cache: packages
55
warnings_are_errors: false

DESCRIPTION

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ Collate:
2424
'autoscale_class.R'
2525
'balanced_accuracy_class.R'
2626
'blank_filter_class.R'
27+
'bootstrap_class.R'
2728
'calculate_doc.R'
2829
'chart_plot_doc.R'
2930
'classical_lsq_class.R'
@@ -57,6 +58,7 @@ Collate:
5758
'mv_feature_filter_class.R'
5859
'mv_sample_filter_class.R'
5960
'pairs_filter_class.R'
61+
'permutation_test2_class.R'
6062
'permutation_test_class.R'
6163
'permute_sample_order_class.R'
6264
'pqn_norm_method_class.R'
@@ -73,7 +75,7 @@ Collate:
7375
'vec_norm_class.R'
7476
'wilcox_test_class.R'
7577
'zzz.R'
76-
Depends: struct
78+
Depends: struct (== 0.4.0)
7779
Imports: ggplot2,
7880
pmp,
7981
gridExtra,
@@ -101,7 +103,7 @@ Suggests:
101103
sbcms,
102104
Rtsne
103105
Remotes: computational-metabolomics/pmp,
104-
computational-metabolomics/struct,
106+
computational-metabolomics/struct@v0.4.0,
105107
computational-metabolomics/sbcms
106108
VignetteBuilder: knitr
107109
biocViews: WorkflowStep

NAMESPACE

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ export(autoscale)
1414
export(balanced_accuracy)
1515
export(blank_filter)
1616
export(blank_filter.hist)
17+
export(bootstrap)
1718
export(classical_lsq)
1819
export(compare_dist)
1920
export(confounders_clsq)
@@ -66,6 +67,7 @@ export(permutation_test.boxplot)
6667
export(permutation_test.hist)
6768
export(permutation_test.scatter)
6869
export(permutation_test.violin)
70+
export(permutation_test2)
6971
export(permute_sample_order)
7072
export(plsda_scores_plot)
7173
export(plsr_cook_dist)
@@ -89,7 +91,7 @@ export(wilcox_p_hist)
8991
export(wilcox_test)
9092
exportMethods(calculate)
9193
exportMethods(chart.plot)
92-
exportMethods(method.apply)
94+
exportMethods(model.apply)
9395
exportMethods(model.predict)
9496
exportMethods(model.reverse)
9597
exportMethods(model.train)

R/HSDEM_class.R

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
#' M = HSDEM()
1313
HSDEM<-setClass(
1414
"HSDEM",
15-
contains=c('method','stato'),
15+
contains=c('model','stato'),
1616
slots=c(
1717
# INPUTS
1818
params.alpha='entity.stato',
@@ -60,8 +60,8 @@ HSDEM<-setClass(
6060
)
6161

6262
#' @export
63-
#' @template method_apply
64-
setMethod(f="method.apply",
63+
#' @template model_apply
64+
setMethod(f="model.apply",
6565
signature=c("HSDEM",'dataset'),
6666
definition=function(M,D) {
6767
X=dataset.data(D)

R/HSD_class.R

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
#' M = HSD()
1212
HSD<-setClass(
1313
"HSD",
14-
contains=c('method','stato'),
14+
contains=c('model','stato'),
1515
slots=c(
1616
# INPUTS
1717
params.alpha='entity.stato',
@@ -70,8 +70,8 @@ HSD<-setClass(
7070
)
7171

7272
#' @export
73-
#' @template method_apply
74-
setMethod(f="method.apply",
73+
#' @template model_apply
74+
setMethod(f="model.apply",
7575
signature=c("HSD",'dataset'),
7676
definition=function(M,D) {
7777
X=dataset.data(D)

R/PLSFC_class.R

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,8 @@ PLSFC<-setClass(
2020
)
2121

2222
#' @export
23-
#' @template method_apply
24-
setMethod(f="method.apply",
23+
#' @template model_apply
24+
setMethod(f="model.apply",
2525
signature=c("PLSFC",'dataset'),
2626
definition=function(M,D) {
2727
# log transform
@@ -59,7 +59,7 @@ setMethod(f="method.apply",
5959
for (B in (A+1):(length(L))) {
6060
# filter groups to A and B
6161
FG=filter_smeta(factor_name=M$factor_name,mode='include',levels=L[c(A,B)])
62-
FG=method.apply(FG,D)
62+
FG=model.apply(FG,D)
6363
# change to ordered factor so that we make use of control group
6464
FG$filtered$sample_meta[[M$factor_name]]=ordered(FG$filtered$sample_meta[[M$factor_name]],levels=L[c(A,B)])
6565

R/anova_class.R

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,14 +14,14 @@
1414
#' @examples
1515
#' D = iris_dataset()
1616
#' M = ANOVA(formula=y~Species)
17-
#' M = method.apply(M,D)
17+
#' M = model.apply(M,D)
1818
#'
1919
#' @include entity_objects.R
2020
#'
2121
#' @export ANOVA
2222
ANOVA<-setClass(
2323
"ANOVA",
24-
contains=c('method','stato'),
24+
contains=c('model','stato'),
2525
slots=c(
2626
# INPUTS
2727
params.alpha='entity.stato',
@@ -58,8 +58,8 @@ ANOVA<-setClass(
5858

5959

6060
#' @export
61-
#' @template method_apply
62-
setMethod(f="method.apply",
61+
#' @template model_apply
62+
setMethod(f="model.apply",
6363
signature=c("ANOVA",'dataset'),
6464
definition=function(M,D)
6565
{

R/blank_filter_class.R

Lines changed: 24 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -20,12 +20,12 @@
2020
#' factor_name='Species',
2121
#' blank_label='setosa',
2222
#' qc_label='versicolor')
23-
#' M = method.apply(M,D)
23+
#' M = model.apply(M,D)
2424
#'
2525
#' @export blank_filter
2626
blank_filter<-setClass(
2727
"blank_filter",
28-
contains = c('method'),
28+
contains = c('model'),
2929
slots=c(params.fold_change='entity',
3030
params.blank_label='entity',
3131
params.qc_label='entity',
@@ -58,41 +58,49 @@ blank_filter<-setClass(
5858
)
5959

6060
#' @export
61-
#' @template method_apply
62-
setMethod(f="method.apply",
61+
#' @template model_train
62+
setMethod(f="model.train",
6363
signature=c("blank_filter","dataset"),
6464
definition=function(M,D)
6565
{
66-
opt=param.list(M)
6766
smeta=dataset.sample_meta(D)
6867
x=dataset.data(D)
6968
# remove = NULL does not remove blanks. ANY VALUE removes blanks.
70-
blank_filtered = pmp::filter_peaks_by_blank(t(x), fold_change=opt$fold_change,
71-
classes=smeta[,opt$factor_name],
72-
blank_label=opt$blank_label,
73-
qc_label=opt$qc_label,
69+
blank_filtered = pmp::filter_peaks_by_blank(t(x), fold_change=M$fold_change,
70+
classes=smeta[,M$factor_name],
71+
blank_label=M$blank_label,
72+
qc_label=M$qc_label,
7473
remove=FALSE,
75-
fraction_in_blank=opt$fraction
74+
fraction_in_blank=M$fraction
7675
)
77-
dataset.data(D) = as.data.frame(t(blank_filtered$df))
7876

77+
# store the flags
78+
flags=data.frame(blank_filtered$flags)
79+
output.value(M,'flags') = data.frame(blank_filtered$flags,stringsAsFactors = F)
80+
return(M)
81+
}
82+
)
83+
84+
#' @export
85+
#' @template model_predict
86+
setMethod(f="model.predict",signature=c("blank_filter","dataset"),
87+
definition=function(M,D) {
7988
# remove the blanks. do it this way because pmp doesnt remove from class labels.
80-
RB = filter_smeta(mode='exclude',levels=opt$blank_label,factor_name=opt$factor_name)
81-
RB=method.apply(RB,D)
89+
RB = filter_smeta(mode='exclude',levels=M$blank_label,factor_name=M$factor_name)
90+
RB=model.apply(RB,D)
8291
D=predicted(RB)
8392

84-
flags=data.frame(blank_filtered$flags)
93+
# get the flags
94+
flags=M$flags
8595
vmeta=dataset.variable_meta(D)
8696
vmeta=vmeta[flags$blank_flags==1,,drop=FALSE]
8797
dataset.variable_meta(D)=vmeta
8898

8999
output.value(M,'filtered') = D
90-
output.value(M,'flags') = data.frame(blank_filtered$flags,stringsAsFactors = F)
91100
return(M)
92101
}
93102
)
94103

95-
96104
##### plots
97105
#' plot for blank filter
98106
#'

R/bootstrap_class.R

Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
#' permutation test class
2+
#'
3+
#' Applies a permutation test to a model or model.seq()
4+
#' @examples
5+
#' I=bootstrap()
6+
#'
7+
#' @export bootstrap
8+
bootstrap<-setClass(
9+
"bootstrap",
10+
contains='resampler',
11+
slots=c(
12+
params.number_of_permutations='numeric',
13+
params.collect='character',
14+
outputs.results='data.frame',
15+
outputs.metric='data.frame',
16+
outputs.collected='entity'
17+
),
18+
prototype = list(name='permutation test',
19+
type='permutation',
20+
result='results',
21+
params.number_of_permutations=10,
22+
params.collect='vip',
23+
outputs.collected=entity(name='collected output',
24+
type=c('logical','list'),
25+
value=NA,max_length=Inf)
26+
)
27+
)
28+
29+
#' @export
30+
#' @template run
31+
setMethod(f="run",
32+
signature=c("bootstrap",'dataset','metric'),
33+
definition=function(I,D,MET=NULL)
34+
{
35+
36+
X=dataset.data(D)
37+
y=dataset.sample_meta(D)
38+
# get the WF
39+
WF=models(I)
40+
n=param.value(I,'number_of_permutations')
41+
42+
all_results=data.frame('actual'=rep(y[,1],n),'predicted'=rep(y[,1],n),'permutation'=0)
43+
44+
collected=list()
45+
46+
for (i in 1:n)
47+
{
48+
results=data.frame('actual'=y[,1],'predicted'=y[,1],'permutation'=i)
49+
50+
# generate a random sample order
51+
order=sample.int(nrow(X),replace = TRUE) # WITH REPLACEMENT
52+
53+
# permute
54+
Xp=X[order,,drop=FALSE]
55+
Yp=as.data.frame(y[order,,drop=FALSE])
56+
57+
# rebuild datasets
58+
Dp=D
59+
dataset.data(Dp)=Xp
60+
dataset.sample_meta(Dp)=Yp
61+
62+
if (is(WF,'model_OR_model.seq'))
63+
{
64+
## permuted labels
65+
# train
66+
WF=model.train(WF,Dp)
67+
# predict
68+
WF=model.predict(WF,Dp)
69+
p=predicted(WF)
70+
results[,2]=p[,1]
71+
all_results[((nrow(X)*(i-1))+1):(nrow(X)*i),]=results
72+
73+
if (!is.na(I$collect)) {
74+
if (is(WF,'model')) {
75+
collected=c(collected,list(output.value(WF,I$collect)))
76+
} else {
77+
# if sequence assume collecting from last index
78+
collected=c(collected,list(output.value(WF[length(WF)],I$collect)))
79+
}
80+
I$collected=collected
81+
}
82+
83+
84+
}
85+
86+
if (is(WF,'iterator'))
87+
{
88+
89+
stop('not implemented yet')
90+
}
91+
92+
}
93+
# store results
94+
output.value(I,'results')=all_results
95+
return(I)
96+
}
97+
)
98+
99+

R/classical_lsq_class.R

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,13 +17,13 @@
1717
#' @examples
1818
#' D = iris_dataset()
1919
#' M = classical_lsq(factor_names = 'Species')
20-
#' M = method.apply(M,D)
20+
#' M = model.apply(M,D)
2121
#'
2222
#' @export classical_lsq
2323

2424
classical_lsq<-setClass(
2525
"classical_lsq",
26-
contains='method',
26+
contains='model',
2727
slots=c(
2828
# INPUTS
2929
params.alpha='entity.stato',
@@ -75,8 +75,8 @@ classical_lsq<-setClass(
7575
)
7676

7777
#' @export
78-
#' @template method_apply
79-
setMethod(f="method.apply",
78+
#' @template model_apply
79+
setMethod(f="model.apply",
8080
signature=c("classical_lsq",'dataset'),
8181
definition=function(M,D)
8282
{

0 commit comments

Comments
 (0)