One of the functionalities of the SIAMCAT
package is the training of
statistical machine learning models on metagenomics data. In this vignette,
we demonstrate how such a model can be built on one dataset and then be applied
on another, similarly processed holdout dataset. This might be of interest
when comparing data from two different studies on the same disease.
In this vignette, we look at two datasets from studies on colorectal cancer (CRC). The first study from Zeller et al. investigated metagenomic markers for CRC in a population in France, while the second study from Yu et al. used samples from China for the same goal. Both datasets were profiled with the same taxonomic profiling tool, yielding the same taxonomic identifiers, which is required for holdout testing.
The datasets can be found on the web repository for public metagenomics datasets from the Zeller group.
library(SIAMCAT)
# this is data from Zeller et al., Mol. Syst. Biol. 2014
fn.feat.fr <-
'https://embl.de/download/zeller/FR-CRC/FR-CRC-N141_tax-ab-specI.tsv'
fn.meta.fr <-
'https://embl.de/download/zeller/FR-CRC/FR-CRC-N141_metadata.tsv'
# this is the external dataset from Yu et al., Gut 2017
fn.feat.cn <-
'https://embl.de/download/zeller/CN-CRC/CN-CRC-N128_tax-ab-specI.tsv'
fn.meta.cn <-
'https://embl.de/download/zeller/CN-CRC/CN-CRC-N128_metadata.tsv'
First of all, we build a SIAMCAT
object using the data from the French study
in the same way that we have seen before in the main SIAMCAT
vignette.
# features
# be vary of the defaults in R!!!
feat.fr <- read.table(fn.feat.fr, sep='\t', quote="",
check.names = FALSE, stringsAsFactors = FALSE)
# the features are counts, but we want to work with relative abundances
feat.fr.rel <- prop.table(as.matrix(feat.fr), 2)
# metadata
meta.fr <- read.table(fn.meta.fr, sep='\t', quote="",
check.names=FALSE, stringsAsFactors=FALSE)
# create SIAMCAT object
siamcat.fr <- siamcat(feat=feat.fr.rel, meta=meta.fr,
label='Group', case='CRC')
## + starting create.label
## Label used as case:
## CRC
## Label used as control:
## CTR
## + finished create.label.from.metadata in 0.058 s
## + starting validate.data
## +++ checking overlap between labels and features
## + Keeping labels of 141 sample(s).
## +++ checking sample number per class
## +++ checking overlap between samples and metadata
## + finished validate.data in 0.948 s
We can load the data from the Chinese study in a similar way and also create a
SIAMCAT
object for the holdout dataset.
# features
feat.cn <- read.table(fn.feat.cn, sep='\t', quote="",
check.names = FALSE)
feat.cn.rel <- prop.table(as.matrix(feat.cn), 2)
# metadata
meta.cn <- read.table(fn.meta.cn, sep='\t', quote="",
check.names=FALSE, stringsAsFactors = FALSE)
# SIAMCAT object
siamcat.cn <- siamcat(feat=feat.cn.rel, meta=meta.cn,
label='Group', case='CRC')
## + starting create.label
## Label used as case:
## CRC
## Label used as control:
## CTR
## + finished create.label.from.metadata in 0.002 s
## + starting validate.data
## +++ checking overlap between labels and features
## + Keeping labels of 128 sample(s).
## +++ checking sample number per class
## +++ checking overlap between samples and metadata
## + finished validate.data in 0.108 s
With the French dataset, we perform the complete process of model building
within SIAMCAT
, including data preprocessing steps like data validation,
filtering, and data normalization.
siamcat.fr <- filter.features(
siamcat.fr,
filter.method = 'abundance',
cutoff = 0.001,
rm.unmapped = TRUE,
verbose=2
)
## + starting filter.features
## +++ before filtering, the data have 1754 features
## +++ removed 1 features corresponding to UNMAPPED reads
## +++ removed 1538 features whose values did not exceed 0.001 in any sample (retaining 215)
## + finished filter.features in 0.032 s
siamcat.fr <- normalize.features(
siamcat.fr,
norm.method = "log.std",
norm.param = list(log.n0 = 1e-06, sd.min.q = 0.1),
verbose = 2
)
## + starting normalize.features
## +++ performing de novo normalization using the log.std method
## + feature sparsity before normalization: 46.05%
## +++ feature sparsity after normalization: 0 %
## + finished normalize.features in 0.033 s
Now, we can build the statistical model. We use the same parameters as in
the main SIAMCAT
vignette, where the process is explained in more detail.
siamcat.fr <- create.data.split(
siamcat.fr,
num.folds = 5,
num.resample = 2
)
## Features splitted for cross-validation successfully.
siamcat.fr <- train.model(
siamcat.fr,
method = "lasso"
)
##
## Trained lasso models successfully.
Finally, we can make predictions for each cross-validation fold and evaluate
the predictions as seen in the main SIAMCAT
vignette.
siamcat.fr <- make.predictions(siamcat.fr)
##
## Made predictions successfully.
siamcat.fr <- evaluate.predictions(siamcat.fr)
## Evaluated predictions successfully.
Now that we have successfully built the model for the French dataset, we can
apply it to the Chinese holdout dataset. First, we will normalize the Chinese
dataset with the same parameters that we used for the French dataset in order
to make the data comparable. For that step, we can use the frozen normalization
functionality in the normalize.features
function in SIAMCAT
. We supply to
the function all normalization parameters saved in the siamcat.fr
object,
which can be accessed using the norm_params
accessor.
siamcat.cn <- normalize.features(siamcat.cn,
norm.param=norm_params(siamcat.fr),
feature.type='original',
verbose = 2)
## + starting normalize.features
## + normalizing original features
## + performing frozen log.std normalization using the supplied parameters
## + feature sparsity before normalization: 49.77%
## + feature sparsity after normalization: 0%
## + finished normalize.features in 0.026 s
Next, we apply the trained model to predict the holdout dataset.
siamcat.cn <- make.predictions(
siamcat = siamcat.fr,
siamcat.holdout = siamcat.cn,
normalize.holdout = FALSE)
## Warning in make.predictions(siamcat = siamcat.fr, siamcat.holdout =
## siamcat.cn, : WARNING: holdout set is not being normalized!
##
## Made predictions successfully.
Note that the make.predictions
function can also take care of the
normalization of the holdout dataset.
## Alternative Code, not run here
siamcat.cn <- siamcat(feat=feat.cn.rel, meta=meta.cn,
label='Group', case='CRC')
siamcat.cn <- make.predictions(siamcat = siamcat.fr,
siamcat.holdout = siamcat.cn,
normalize.holdout = TRUE)
Again, we have to evaluate the predictions:
siamcat.cn <- evaluate.predictions(siamcat.cn)
Now, we can compare the performance of the classifier on the original and
the holdout dataset by using the model.evaluation.plot
function. Here,
we can supply several SIAMCAT
objects for which the model evaluation will be
plotted in the same plot. Note that we can supply the objects as named objects
in order to print the names in the legend.
model.evaluation.plot('FR-CRC'=siamcat.fr,
'CN-CRC'=siamcat.cn,
colours=c('dimgrey', 'orange'))
sessionInfo()
## R version 3.5.2 (2018-12-20)
## Platform: x86_64-pc-linux-gnu (64-bit)
## Running under: Ubuntu 16.04.5 LTS
##
## Matrix products: default
## BLAS: /home/biocbuild/bbs-3.8-bioc/R/lib/libRblas.so
## LAPACK: /home/biocbuild/bbs-3.8-bioc/R/lib/libRlapack.so
##
## locale:
## [1] LC_CTYPE=en_US.UTF-8 LC_NUMERIC=C
## [3] LC_TIME=en_US.UTF-8 LC_COLLATE=C
## [5] LC_MONETARY=en_US.UTF-8 LC_MESSAGES=en_US.UTF-8
## [7] LC_PAPER=en_US.UTF-8 LC_NAME=C
## [9] LC_ADDRESS=C LC_TELEPHONE=C
## [11] LC_MEASUREMENT=en_US.UTF-8 LC_IDENTIFICATION=C
##
## attached base packages:
## [1] stats graphics grDevices utils datasets methods base
##
## other attached packages:
## [1] SIAMCAT_1.2.1 phyloseq_1.26.1 mlr_2.13 ParamHelpers_1.11
## [5] BiocStyle_2.10.0
##
## loaded via a namespace (and not attached):
## [1] Biobase_2.42.0 jsonlite_1.6 splines_3.5.2
## [4] foreach_1.4.4 LiblineaR_2.10-8 PRROC_1.3.1
## [7] assertthat_0.2.0 BiocManager_1.30.4 stats4_3.5.2
## [10] yaml_2.2.0 corrplot_0.84 pillar_1.3.1
## [13] backports_1.1.3 lattice_0.20-38 glue_1.3.0
## [16] pROC_1.13.0 digest_0.6.18 RColorBrewer_1.1-2
## [19] XVector_0.22.0 checkmate_1.8.5 colorspace_1.3-2
## [22] htmltools_0.3.6 Matrix_1.2-15 plyr_1.8.4
## [25] XML_3.98-1.16 pkgconfig_2.0.2 bookdown_0.9
## [28] zlibbioc_1.28.0 purrr_0.2.5 scales_1.0.0
## [31] parallelMap_1.3 tibble_2.0.0 beanplot_1.2
## [34] mgcv_1.8-26 IRanges_2.16.0 ggplot2_3.1.0
## [37] infotheo_1.2.0 BiocGenerics_0.28.0 lazyeval_0.2.1
## [40] survival_2.43-3 magrittr_1.5 crayon_1.3.4
## [43] evaluate_0.12 nlme_3.1-137 MASS_7.3-51.1
## [46] vegan_2.5-3 tools_3.5.2 data.table_1.11.8
## [49] matrixStats_0.54.0 gridBase_0.4-7 BBmisc_1.11
## [52] stringr_1.3.1 Rhdf5lib_1.4.2 S4Vectors_0.20.1
## [55] munsell_0.5.0 glmnet_2.0-16 cluster_2.0.7-1
## [58] bindrcpp_0.2.2 Biostrings_2.50.2 ade4_1.7-13
## [61] compiler_3.5.2 rlang_0.3.0.1 rhdf5_2.26.2
## [64] grid_3.5.2 iterators_1.0.10 biomformat_1.10.1
## [67] igraph_1.2.2 rmarkdown_1.11 gtable_0.2.0
## [70] codetools_0.2-16 multtest_2.38.0 reshape2_1.4.3
## [73] R6_2.3.0 gridExtra_2.3 knitr_1.21
## [76] dplyr_0.7.8 bindr_0.1.1 fastmatch_1.1-0
## [79] permute_0.9-4 ape_5.2 stringi_1.2.4
## [82] parallel_3.5.2 Rcpp_1.0.0 tidyselect_0.2.5
## [85] xfun_0.4