# Downloading Datasets with getDataset

#### 2018-08-30

This vignette shows detailed examples for all functionalities of the `getDataset` method.

# 1 Create connections

As explained into the introductory vignette, datasets must be downloaded from `ImmuneSpaceConnection` objects. We must first instantiate a connection to the study or studies of interest. Throughout this vignette, we will use two connections, one to a single study, and one to to all available data.

    library(ImmuneSpaceR)
    sdy269 <- CreateConnection("SDY269")
    all <- CreateConnection("")

# 2 List the datasets

Now that the connections have been instantiated, we can start downloading from themm but we need to figure out which datasets are available within our chosen studies. Printing the connections will, among other information, list the datasets availables. The `listDatasets` method will display only the information we are looking for.

    sdy269$listDatasets()

    ## datasets
    ##  demographics
    ##  elispot
    ##  hai
    ##  fcs_analyzed_result
    ##  cohort_membership
    ##  pcr
    ##  elisa
    ##  gene_expression_files
    ##  fcs_sample_files
    ## Expression Matrices
    ##  TIV_2008
    ##  LAIV_2008

    all$listDatasets()

    ## datasets
    ##  neut_ab_titer
    ##  fcs_sample_files
    ##  elisa
    ##  fcs_analyzed_result
    ##  pcr
    ##  demographics
    ##  hai
    ##  elispot
    ##  hla_typing
    ##  cohort_membership
    ##  fcs_control_files
    ##  gene_expression_files
    ##  mbaa
    ## Expression Matrices
    ##  SDY300_dendriticCell_dcMonoFlu2011
    ##  SDY300_otherCell_dcMonoFlu2011
    ##  SDY67_HealthyAdults
    ##  SDY67_Batch2_HealthyAdult
    ##  SDY1328_healthy_adults
    ##  SDY1324_nonBCG_Vaccinated
    ##  SDY1324_Latent_TB
    ##  SDY1324_BCG_Vaccinated
    ##  SDY1293_Vaccinated
    ##  SDY1293_Infectivity_Control
    ##  SDY1289_Montreal_adult
    ##  SDY1289_Lausanne_cohort
    ##  SDY1289_Emory_adult
    ##  SDY1276_Validation_Cohort
    ##  SDY1276_Discovery
    ##  SDY1264_Trial_2
    ##  SDY1264_Trial_1
    ##  SDY1260_MPSV4_Cohort
    ##  SDY1260_MCV4_Cohort
    ##  SDY984_Young_cohort
    ##  SDY984_elderly_cohort
    ##  SDY888_denvNeg_endemicAreaTrue
    ##  SDY888_DenvNeg_endemicAreaFalse
    ##  SDY888_DenvPos_endemicAreaTrue
    ##  SDY690_WholeBlood_EnergixB
    ##  SDY690_PBMC_EnergixB
    ##  SDY667_HIPC_PSOR_PPP
    ##  SDY597_Cellsubs_Invitro
    ##  SDY387_NCH
    ##  SDY372_JDM_2012_2013
    ##  SDY368_NCH_2013_2014
    ##  SDY364_NCH_2012_2013
    ##  SDY315_GroupC
    ##  SDY315_GroupB
    ##  SDY315_GroupA
    ##  SDY312_GroupC_Cohort
    ##  SDY312_GroupB_Cohort
    ##  SDY312_GroupA_Cohort
    ##  SDY305_TIVGroup
    ##  SDY305_IDTIVGroup
    ##  SDY299_HEPISLAV_Cohort
    ##  SDY112_GroupC
    ##  SDY112_GroupB
    ##  SDY112_GroupA
    ##  SDY34_InfluenzaVaccine
    ##  SDY34_HealthyControls
    ##  SDY28_Dryvax_Recipients
    ##  SDY180_group2_saline
    ##  SDY180_group2_pneunomax
    ##  SDY180_group2_fluzone
    ##  SDY180_group1_saline
    ##  SDY180_group1_pneunomax
    ##  SDY180_group1_fluzone
    ##  SDY301_AIRFV
    ##  SDY296_AIRFV
    ##  SDY162_Macrophage_VLplus
    ##  SDY162_PBMC_VLplus
    ##  SDY162_Macrophage_VLminus
    ##  SDY162_PBMC_VLminus
    ##  SDY522_LAIV_Vaccine
    ##  SDY212_Cohort2_Older
    ##  SDY212_Cohort1_Young
    ##  SDY400_PBMC_Young
    ##  SDY400_PBMC_Older
    ##  SDY404_PBMC_Young
    ##  SDY404_PBMC_Older
    ##  SDY63_PBMC_Older
    ##  SDY63_PBMC_Young
    ##  SDY144_TIV2011
    ##  TIV_older
    ##  TIV_young
    ##  pH1N1_2009
    ##  TIV_2010
    ##  TIV_2007
    ##  TIV_2008
    ##  LAIV_2008

Naturally, `all` contains every dataset available on ImmuneSpace as it combines all available studies. Additionaly, when creating connection object with `verbose = TRUE`, a call to the `getDataset` method with an invalid dataset name will return the list of valid datasets.

# 3 Download

Calling `getDataset` returns a selected dataset as it is displayed on ImmuneSpace.

    hai_269 <- sdy269$getDataset("hai")
    hai_all <- all$getDataset("hai")

    head(hai_269)

    ##    participant_id age_reported gender  race          cohort
    ## 1:  SUB112868.269           37   Male White LAIV group 2008
    ## 2:  SUB112869.269           41   Male White LAIV group 2008
    ## 3:  SUB112841.269           28 Female White  TIV Group 2008
    ## 4:  SUB112836.269           28 Female White LAIV group 2008
    ## 5:  SUB112844.269           29 Female White LAIV group 2008
    ## 6:  SUB112857.269           33   Male White  TIV Group 2008
    ##    study_time_collected study_time_collected_unit                  virus
    ## 1:                   28                      Days     A/Uruguay/716/2007
    ## 2:                   28                      Days       B/Florida/4/2006
    ## 3:                    0                      Days     A/Uruguay/716/2007
    ## 4:                   28                      Days A/South Dakota/06/2007
    ## 5:                   28                      Days     A/Uruguay/716/2007
    ## 6:                    0                      Days     A/Uruguay/716/2007
    ##    value_preferred
    ## 1:               5
    ## 2:               5
    ## 3:               5
    ## 4:               5
    ## 5:              20
    ## 6:               5

Because some datasets such as flow cytometry results can contain a large number of rows, the method returns `data.table` objects to improve performance. This is especially important with multi-study connections.

# 4 Filters

The datasets can be filtered before download. Filters should be created using `Rlabkey`’s `makeFilter` function.

Each filter is composed of three part: * A column name or column label * An operator * A value or array of values separated by a semi-colon

    library(Rlabkey)

    # Get participants under age of 30
    young_filter <- makeFilter(c("age_reported", "LESS_THAN", 30))

    # Get a specific list of two participants
    pid_filter <- makeFilter(c("participantid", "IN", "SUB112841.269;SUB112834.269"))

For a list of available operators, see `?Rlabkey::makeFilter`.

    # HAI data for participants of study SDY269 under age of 30
    hai_young <- sdy269$getDataset("hai", colFilter = young_filter)

    # List of participants under age 30
    demo_young <- all$getDataset("demographics", colFilter = young_filter)

    # ELISPOT assay results for two participants
    mbaa_pid2 <- all$getDataset("elispot", colFilter = pid_filter)

Note that filtering is done before download. When performance is a concern, it is faster to do the filtering via the `colfFilter` argument than on the returned table.

# 5 Views

Any dataset grid on ImmuneSpace offers the possibility to switch views between ‘Default’ and ‘Full’. The Default view contains information that is directly relevant to the user. Sample description and results are joined with basic demographic. However, this is not the way data is organized in the database. The ‘Full’ view is a representation of the data as it is stored on ImmPort. The accession columns are used under the hood for join operations. They will be useful to developers and user writing reports to be displayed in ImmuneSpace studies.

Screen capture of the button bar of a dataset grid on ImmuneSpace.

The `original_view` argument decides which view is downloaded. If set to `TRUE`, the ‘Full’ view is returned.

    full_hai <- sdy269$getDataset("hai", original_view = TRUE)

    colnames(full_hai)

    ##  [1] "participant_id"            "arm_accession"
    ##  [3] "biosample_accession"       "expsample_accession"
    ##  [5] "experiment_accession"      "study_accession"
    ##  [7] "study_time_collected"      "study_time_collected_unit"
    ##  [9] "virus"                     "value_reported"
    ## [11] "value_preferred"           "unit_reported"
    ## [13] "unit_preferred"

For additional information, refer to the ‘Working with tabular data’ video tutorial.

# 6 Caching

As explained in the introductory guide, the `ImmuneSpaceConnection` class is a `R6` class. It means its objects have fields accessed by reference. As a consequence, they can be modified without making a copy of the entire object. ImmuneSpaceR uses this feature to store downloaded datasets and expression matrices. Subsequent calls to `getDataset` with the same input will be faster.

See `?R6::R6Class` for more information about R6 class system.

We can see the data currently cached using the `cache` field. This is not intended to be used for data manipulation and only displayed here to explain what gets cached.

    pcr <- sdy269$getDataset("pcr")
    names(sdy269$cache)

    ## [1] "GE_matrices"

Different views are saved separately.

    pcr_ori <- sdy269$getDataset("pcr", original_view = TRUE)
    names(sdy269$cache)

    ## [1] "GE_matrices"

Because of the infinite number of filters and combinations of filters, we do not cache filtered datasets.

If, for any reason, a specific dataset needs to be redownloaded, the `reload` argument will clear the cache for that specific `getDataset` call and download the table again.

    hai_269 <- sdy269$getDataset("hai", reload = TRUE)

Finally, it is possible to clear every cached dataset (and expression matrix).

    sdy269$clearCache()
    names(sdy269$cache)

    ## [1] "GE_matrices"

Again, the `cache` field should never be modified manually. When in doubt, simply reload the dataset.

# 7 Session info

    sessionInfo()

    ## R version 3.5.1 Patched (2018-07-24 r75005)
    ## Platform: x86_64-w64-mingw32/x64 (64-bit)
    ## Running under: Windows Server 2012 R2 x64 (build 9600)
    ##
    ## Matrix products: default
    ##
    ## locale:
    ## [1] LC_COLLATE=C
    ## [2] LC_CTYPE=English_United States.1252
    ## [3] LC_MONETARY=English_United States.1252
    ## [4] LC_NUMERIC=C
    ## [5] LC_TIME=English_United States.1252
    ##
    ## attached base packages:
    ## [1] stats     graphics  grDevices utils     datasets  methods   base
    ##
    ## other attached packages:
    ## [1] Rlabkey_2.2.2      jsonlite_1.5       httr_1.3.1
    ## [4] ImmuneSpaceR_1.8.2 rmarkdown_1.10     knitr_1.20
    ##
    ## loaded via a namespace (and not attached):
    ##  [1] Biobase_2.40.0        viridis_0.5.1         tidyr_0.8.1
    ##  [4] viridisLite_0.3.0     foreach_1.4.4         gtools_3.8.1
    ##  [7] assertthat_0.2.0      stats4_3.5.1          yaml_2.2.0
    ## [10] robustbase_0.93-2     pillar_1.3.0          backports_1.1.2
    ## [13] lattice_0.20-35       glue_1.3.0            digest_0.6.16
    ## [16] RColorBrewer_1.1-2    colorspace_1.3-2      preprocessCore_1.42.0
    ## [19] htmltools_0.3.6       plyr_1.8.4            pkgconfig_2.0.2
    ## [22] pheatmap_1.0.10       purrr_0.2.5           mvtnorm_1.0-8
    ## [25] scales_1.0.0          webshot_0.5.0         gdata_2.18.0
    ## [28] whisker_0.3-2         tibble_1.4.2          ggplot2_3.0.0
    ## [31] nnet_7.3-12           BiocGenerics_0.26.0   lazyeval_0.2.1
    ## [34] magrittr_1.5          crayon_1.3.4          mclust_5.4.1
    ## [37] heatmaply_0.15.2      evaluate_0.11         MASS_7.3-50
    ## [40] gplots_3.0.1          class_7.3-14          registry_0.5
    ## [43] tools_3.5.1           data.table_1.11.4     trimcluster_0.1-2.1
    ## [46] stringr_1.3.1         plotly_4.8.0          kernlab_0.9-27
    ## [49] munsell_0.5.0         cluster_2.0.7-1       fpc_2.1-11.1
    ## [52] bindrcpp_0.2.2        compiler_3.5.1        caTools_1.17.1.1
    ## [55] rlang_0.2.2           grid_3.5.1            iterators_1.0.10
    ## [58] rjson_0.2.20          htmlwidgets_1.2       labeling_0.3
    ## [61] bitops_1.0-6          codetools_0.2-15      gtable_0.2.0
    ## [64] flexmix_2.3-14        curl_3.2              reshape2_1.4.3
    ## [67] TSP_1.1-6             R6_2.2.2              seriation_1.2-3
    ## [70] gridExtra_2.3         prabclus_2.2-6        dplyr_0.7.6
    ## [73] bindr_0.1.1           rprojroot_1.3-2       KernSmooth_2.23-15
    ## [76] dendextend_1.8.0      modeltools_0.2-22     stringi_1.2.4
    ## [79] parallel_3.5.1        Rcpp_0.12.18          gclus_1.3.1
    ## [82] DEoptimR_1.0-8        tidyselect_0.2.4      diptest_0.75-7