An Introduction to the ImmuneSpaceR Package

Renan Sauteraud

2021-10-26

This package provides a thin wrapper around Rlabkey and connects to the ImmuneSpace database, making it easier to fetch datasets, including gene expression data, HAI, and so forth, from specific studies.

1 Configuration

In order to connect to ImmuneSpace, you will need a netrc file in your home directory.

Set up your netrc file now!

If you’re not familiar with the command-line interface, there is the interactive_netrc() function to set up your netrc file. See the interactive_netrc vignette.

Or create netrc file in the computer running R:

The following three lines must be included in the .netrc or _netrc file either separated by white space (spaces, tabs, or newlines) or commas.

machine www.immunespace.org
login myUser@mySite.com
password superSecretPassword

Multiple such blocks can exist in one file. Please ensure that the machine name in the netrc file contains the “www” prefix as that is how the package connects to immunespace by default. A mismatch will lead to connection failures.

See the official LabKey documentation for more information.

2 Instantiate a connection

We’ll be looking at study SDY269. If you want to use a different study, change that string. The connections have state, so you can instantiate multiple connections to different studies simultaneously.

library(ImmuneSpaceR)
sdy269 <- CreateConnection(study = "SDY269")
## Warning in matrix(data = unlist(curfld[resultCols]), nrow = 1, ncol =
## length(resultCols), : data length [4] is not a sub-multiple or multiple of the
## number of columns [7]
sdy269
## <ImmuneSpaceConnection>
##   Study: SDY269
##   URL: https://www.immunespace.org/Studies/SDY269
##   User: unknown_user at not_a_domain.com
##   9 Available Datasets
##     - cohort_membership
##     - demographics
##     - elisa
##     - elispot
##     - fcs_analyzed_result
##     - fcs_sample_files
##     - gene_expression_files
##     - hai
##     - pcr
##   2 Available Expression Matrices

The call to CreateConnection instantiates the connection. Printing the object shows where it’s connected, to what study, and the available data sets and gene expression matrices.

Note that when a script is running on ImmuneSpace, some variables set in the global environments will automatically indicate which study should be used and the study argument can be skipped.

3 Fetching datasets

We can grab any of the datasets listed in the connection.

sdy269$getDataset("hai")
##      participant_id age_reported gender  race          cohort
##   1:  SUB112829.269           26   Male White LAIV group 2008
##   2:  SUB112829.269           26   Male White LAIV group 2008
##   3:  SUB112829.269           26   Male White LAIV group 2008
##   4:  SUB112829.269           26   Male White LAIV group 2008
##   5:  SUB112829.269           26   Male White LAIV group 2008
##  ---                                                         
## 332:  SUB112888.269           34 Female White  TIV Group 2008
## 333:  SUB112888.269           34 Female White  TIV Group 2008
## 334:  SUB112888.269           34 Female White  TIV Group 2008
## 335:  SUB112888.269           34 Female White  TIV Group 2008
## 336:  SUB112888.269           34 Female White  TIV Group 2008
##      study_time_collected study_time_collected_unit                  virus
##   1:                    0                      Days A/South Dakota/06/2007
##   2:                    0                      Days     A/Uruguay/716/2007
##   3:                    0                      Days       B/Florida/4/2006
##   4:                   28                      Days A/South Dakota/06/2007
##   5:                   28                      Days     A/Uruguay/716/2007
##  ---                                                                      
## 332:                    0                      Days     A/Uruguay/716/2007
## 333:                    0                      Days     B/Brisbane/03/2007
## 334:                   28                      Days     A/Brisbane/59/2007
## 335:                   28                      Days     A/Uruguay/716/2007
## 336:                   28                      Days     B/Brisbane/03/2007
##      value_preferred
##   1:              40
##   2:              40
##   3:              20
##   4:              40
##   5:              40
##  ---                
## 332:               5
## 333:             320
## 334:              80
## 335:              40
## 336:              40

The sdy269 object is an R6 class, so it behaves like a true object. Methods (like getDataset) are members of the object, thus the $ semantics to access member functions.

The first time you retrieve a dataset, it will contact the database. The data is cached in the object, so the next time you call getDataset on the same dataset, it will retrieve the cached local copy. This is much faster.

To get only a subset of the data and speed up the download, filters can be passed to getDataset. The filters are created using the makeFilter function of the Rlabkey package.

library(Rlabkey)
myFilter <- makeFilter(c("gender", "EQUAL", "Female"))
hai <- sdy269$getDataset("hai", colFilter = myFilter)

See ?Rlabkey::makeFilter for more information on the syntax.

For more information about getDataset’s options, refer to the dedicated vignette.

4 Fetching expression matrices

We can also grab a gene expression matrix

sdy269$getGEMatrix("SDY269_PBMC_LAIV_Geo")
## Downloading matrix..
## Constructing ExpressionSet
## ExpressionSet (storageMode: lockedEnvironment)
## assayData: 16146 features, 83 samples 
##   element names: exprs 
## protocolData: none
## phenoData
##   sampleNames: BS586100 BS586156 ... BS586239 (83 total)
##   varLabels: participant_id study_time_collected ...
##     exposure_process_preferred (8 total)
##   varMetadata: labelDescription
## featureData
##   featureNames: DDR1 RFC2 ... NUS1P3 (16146 total)
##   fvarLabels: FeatureId gene_symbol
##   fvarMetadata: labelDescription
## experimentData: use 'experimentData(object)'
## Annotation:

The object contacts the database and downloads the matrix file. This is stored and cached locally as a data.table. The next time you access it, it will be much faster since it won’t need to contact the database again.

It is also possible to call this function using multiple matrix names. In this case, all the matrices are downloaded and combined into a single ExpressionSet.

sdy269$getGEMatrix(c("SDY269_PBMC_TIV_Geo", "SDY269_PBMC_LAIV_Geo"))
## Downloading matrix..
## Constructing ExpressionSet
## Returning SDY269_PBMC_LAIV_Geo_summary_latest_eset from cache
## Combining ExpressionSets
## ExpressionSet (storageMode: lockedEnvironment)
## assayData: 16146 features, 163 samples 
##   element names: exprs 
## protocolData: none
## phenoData
##   sampleNames: BS586128 BS586240 ... BS586239 (163 total)
##   varLabels: participant_id study_time_collected ...
##     exposure_process_preferred (8 total)
##   varMetadata: labelDescription
## featureData
##   featureNames: 1 2 ... 16146 (16146 total)
##   fvarLabels: FeatureId gene_symbol
##   fvarMetadata: labelDescription
## experimentData: use 'experimentData(object)'
## Annotation:

Finally, the summary argument will let you download the matrix with gene symbols in place of probe ids.

gs <- sdy269$getGEMatrix("SDY269_PBMC_TIV_Geo", outputType = "summary", annotation = "latest")
## Returning SDY269_PBMC_TIV_Geo_summary_latest_eset from cache

If the connection was created with verbose = TRUE, some methods will display additional informations such as the valid dataset names.

5 Plotting

A plot of a dataset can be generated using the plot method which automatically chooses the type of plot depending on the selected dataset.

sdy269$plot("hai")

sdy269$plot("elisa")

However, the type argument can be used to manually select from “boxplot”, “heatmap”, “violin” and “line”.

6 Cross study connections

To fetch data from multiple studies, simply create a connection at the project level.

con <- CreateConnection("")
## Warning in matrix(data = unlist(curfld[resultCols]), nrow = 1, ncol =
## length(resultCols), : data length [4] is not a sub-multiple or multiple of the
## number of columns [7]

This will instantiate a connection at the Studies level. Most functions work cross study connections just like they do on single studies.

You can get a list of datasets and gene expression matrices available accross all studies.

con
## <ImmuneSpaceConnection>
##   Study: Studies
##   URL: https://www.immunespace.org/Studies/
##   User: unknown_user at not_a_domain.com
##   13 Available Datasets
##     - cohort_membership
##     - demographics
##     - elisa
##     - elispot
##     - fcs_analyzed_result
##     - fcs_control_files
##     - fcs_sample_files
##     - gene_expression_files
##     - hai
##     - hla_typing
##     - mbaa
##     - neut_ab_titer
##     - pcr
##   123 Available Expression Matrices

In cross-study connections, getDataset and getGEMatrix will combine the requested datasets or expression matrices. See the dedicated vignettes for more information.

Likewise, plot will visualize accross studies. Note that in most cases the datasets will have too many cohorts/subjects, making the filtering of the data a necessity. The colFilter argument can be used here, as described in the getDataset section.

plotFilter <- makeFilter(
  c("cohort", "IN", "TIV 2010;TIV Group 2008"),
  c("study_time_collected", "EQUALS", "7")
)
con$plot("elispot", filter = plotFilter)

The figure above shows the ELISPOT results for two different years of TIV vaccine cohorts from two different studies.

7 Session info

sessionInfo()
## R version 4.1.1 (2021-08-10)
## Platform: x86_64-pc-linux-gnu (64-bit)
## Running under: Ubuntu 20.04.3 LTS
## 
## Matrix products: default
## BLAS:   /home/biocbuild/bbs-3.14-bioc/R/lib/libRblas.so
## LAPACK: /home/biocbuild/bbs-3.14-bioc/R/lib/libRlapack.so
## 
## locale:
##  [1] LC_CTYPE=en_US.UTF-8       LC_NUMERIC=C              
##  [3] LC_TIME=en_GB              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] Rlabkey_2.8.1       jsonlite_1.7.2      httr_1.4.2         
## [4] ImmuneSpaceR_1.22.0 rmarkdown_2.11      knitr_1.36         
## 
## loaded via a namespace (and not attached):
##  [1] ncdfFlow_2.40.0       bitops_1.0-7          matrixStats_0.61.0   
##  [4] webshot_0.5.2         RColorBrewer_1.1-2    Rgraphviz_2.38.0     
##  [7] tools_4.1.1           bslib_0.3.1           utf8_1.2.2           
## [10] R6_2.5.1              KernSmooth_2.23-20    DBI_1.1.1            
## [13] BiocGenerics_0.40.0   lazyeval_0.2.2        colorspace_2.0-2     
## [16] flowWorkspace_4.6.0   withr_2.4.2           tidyselect_1.1.1     
## [19] gridExtra_2.3         preprocessCore_1.56.0 curl_4.3.2           
## [22] compiler_4.1.1        graph_1.72.0          Biobase_2.54.0       
## [25] TSP_1.1-11            xml2_1.3.2            plotly_4.10.0        
## [28] labeling_0.4.2        sass_0.4.0            caTools_1.18.2       
## [31] scales_1.1.1          stringr_1.4.0         digest_0.6.28        
## [34] base64enc_0.1-3       jpeg_0.1-9            pkgconfig_2.0.3      
## [37] htmltools_0.5.2       highr_0.9             fastmap_1.1.0        
## [40] htmlwidgets_1.5.4     rlang_0.4.12          flowCore_2.6.0       
## [43] farver_2.1.0          jquerylib_0.1.4       generics_0.1.1       
## [46] gtools_3.9.2          dendextend_1.15.1     dplyr_1.0.7          
## [49] magrittr_2.0.1        RProtoBufLib_2.6.0    Rcpp_1.0.7           
## [52] munsell_0.5.0         S4Vectors_0.32.0      fansi_0.5.0          
## [55] viridis_0.6.2         lifecycle_1.0.1       stringi_1.7.5        
## [58] yaml_2.2.1            zlibbioc_1.40.0       gplots_3.1.1         
## [61] grid_4.1.1            crayon_1.4.1          lattice_0.20-45      
## [64] pillar_1.6.4          codetools_0.2-18      stats4_4.1.1         
## [67] XML_3.99-0.8          glue_1.4.2            evaluate_0.14        
## [70] latticeExtra_0.6-29   data.table_1.14.2     RcppParallel_5.1.4   
## [73] png_0.1-7             vctrs_0.3.8           foreach_1.5.1        
## [76] gtable_0.3.0          aws.s3_0.3.21         purrr_0.3.4          
## [79] tidyr_1.1.4           heatmaply_1.3.0       assertthat_0.2.1     
## [82] ggplot2_3.3.5         xfun_0.27             viridisLite_0.4.0    
## [85] pheatmap_1.0.12       seriation_1.3.1       tibble_3.1.5         
## [88] iterators_1.0.13      cytolib_2.6.0         aws.signature_0.6.0  
## [91] registry_0.5-1        ellipsis_0.3.2