---
title: "The HCABrowser Package"
author: "Daniel Van Twisk"
date: "`r format(Sys.Date(), '%A, %B %d, %Y')`"
always_allow_html: yes
output:
  BiocStyle::html_document:
    df_print: paged
    toc_float: true
abstract: >
  The [Human Cell Atlas] (HCA) (https://www.humancellatlas.org/) was created in
  order to create comprehensive reference maps of all human cells as a basis for
  both understanding human health and diagnosing, monitoring, and treating
  disease. The *HCABrowser* Biocondctor pacakge provides infrastructure for
  searching for, queerying, and accessing data help on the Human Cell Atlas's
  Data Coordination Platform (https://dss.data.humancellatlas.org/). Further
  changes to the package are planned to incorperate higer level functionality to
  upload user generated data to the the Human Cell Atlas platform.
vignette: >
  %\VignetteIndexEntry{Introduction to Accessing the HCABrowser using the
        HCABrowser package}
  %\VignetteEngine{knitr::knitr}
  %\VignetteIndexEntry{HCABrowser}
  %\VignetteEncoding{UTF-8}
---

```{r init, results='hide', echo=FALSE, warning=FALSE, message=FALSE}
library(knitr)
opts_chunk$set(warning=FALSE, message=FALSE)
BiocStyle::markdown()
```


# What is the Human Cell Atlas?

From the [Human Cell Atlas (HCA) website](https://www.humancellatlas.org/):

> The cell is the core unit of the human body—the key to understanding the
biology of health and the ways in which molecular dysfunction leads to disease.
Yet our characterization of the hundreds of types and subtypes of cells in the
human body is limited, based partly on techniques that have limited resolution
and classifications that do not always map neatly to each other. Genomics has
offered a systematic approach, but it has largely been applied in bulk to many
cell types at once—masking critical differences between cells—and in isolation
from other valuable sources of data.

> Recent advances in single-cell genomic analysis of cells and tissues have put
systematic, high-resolution and comprehensive reference maps of all human cells
within reach. In other words, we can now realistically envision a human cell
atlas to serve as a basis for both understanding human health and diagnosing,
monitoring, and treating disease.

> At its core, a cell atlas would be a collection of cellular reference maps,
characterizing each of the thousands of cell types in the human body and where
they are found. It would be an extremely valuable resource to empower the global
research community to systematically study the biological changes associated
with different diseases, understand where genes associated with disease are
active in our bodies, analyze the molecular  mechanisms that govern the
production and activity of different cell types, and sort out how different cell
types combine and work together to form tissues.

The Human Cell Atlas facilitates queries on it's [data coordination platform with
a RESTFUL API] (https://dss.data.humancellatlas.org/).

## Installation

To install this package, use Bioconductor's `BiocManager` package.

```{r install_bioc, eval=FALSE}
if (!require("BiocManager"))
    install.packages("BiocManager")
BiocManager::install('HCABrowser')
```

```{r libraries, message=FALSE}
library(HCABrowser)
```

## Connecting to the Human Cell Atlas

The `r Biocpkg("HCABrowser")` package relies on having network
connectivety. Also, the HCA's Data Coordination Platform (DCP) must
also be operational. This package is meant for users who have a working
knowledge of the HCA DCP's schema to provide the user with the
fill richness of the HCA's content. For a more simple-to-use way to access and
download data from The Human Cell Atlas, please use the `HCAExplorer` package.

The `HCABrowser` object serves as the representation of the Human Cell
Atlas. Upon creation, it will automatically peform a cursorary query and
display a small table showing the first few bundles of the entire HCA. This
intial table contains some columns that we have determined are most useful
to users. The output also displays the url of the instance of the HCA DCP being
used, the current query, whether bundles or files are being displayed, and the
number of bundles in the results

By default, ten bundles per page will be displayed in the result for "raw"
output and 100 will be displayed for "summary" output. The `host` argument
dictates the hca-dcp the client will be accessing while the `api_url` argument
dictates what schema the client will be accessing.  These three values can be
changed in the constructor. The default values are given below.

If the HCA cannot be reached, an error will be thrown displaying the status of
the request.

```{r createHCA}
hca <- HCABrowser(api_url='https://dss.data.humancellatlas.org/v1/swagger.json',
                  host = 'dss.data.humancellatlas.org/v1')
hca
```

Upon displaying the object, multiple fields can be seen:
- The class: `HCABrowser`
- The hca dcp host address that is currently being used.
- The current query (assigned using `filter()`)
- The current selection (assigned using `select()`)
    - You may notice that some columns are already selected. These columns are
      automatically selected to allow the user some initial view of the hca.

To change how many pages are being displayed, the `per_page()` method can be
used.
(Note the hca dcp had a maximum of 10 bundles per page to be shown at a time)

Since there are far more bundles in the HCA than can be shown, if `link` is
`True`, the next set of bundles can be obtained using the `nextResults` method.

```{r nextResults, eval=FALSE}
hca <- nextResults(hca)
hca
```

## Querying the HCABrowser

The HCA extends the functionality of the `r CRANpkg("dplyr")` package's `filter()`
and `select()` methods.

The `filter()` method allows the user to query the HCA by relating fields to
certain values. Character fields can be queried using the operators:
- `==`
- `!=`
- `%in%`
- `%startsWith%`
- `%endsWith%`
- `%contains%`

Numeric fields can be queried with the operators:
- `==`
- `!=`
- `%in%`
- `>`
- `<`
- `>=`
- `<=`

Queries can be encompassed by parenthesese
- `()`

Queries can be negated by placing the `!` symbol in front

Combination operators can be used to combine queries
- `&`
- `|`

Now we see that "brain" and "Brain" are available values. Since these values are
the result of input by other users, there may be errors or inconsistencies. To
be safe, both fields can be queried with the following query:

```{r firstFilter}
hca2 <- hca %>% filter('files.specimen_from_organism_json.organ.text' == c('Brain', 'brain'))
hca2 <- hca %>% filter('files.specimen_from_organism_json.organ.text' %in% c('Brain', 'brain'))
hca2 <- hca %>% filter('files.specimen_from_organism_json.organ.text' == Brain | 'files.specimen_from_organism_json.organ.text' == brain)
hca2
```

If we also wish to search for results based on the NCBI Taxon ID for mouse,
10090, as well as brain, we can perform this query in a variety of ways.
```{r multiFilter}
hca2 <- hca %>% filter('files.specimen_from_organism_json.organ.text' %in% c('Brain', 'brain')) %>%
                filter('specimen_from_organism_json.biomaterial_core.ncbi_taxon_id' == 10090)
hca2 <- hca %>% filter('files.specimen_from_organism_json.organ.text' %in% c('Brain', 'brain'),
                       'specimen_from_organism_json.biomaterial_core.ncbi_taxon_id' == 10090)
hca <- hca %>% filter('files.specimen_from_organism_json.organ.text' %in% c('Brain', 'brain') &
                      'specimen_from_organism_json.biomaterial_core.ncbi_taxon_id' == 10090)
hca
```

The `HCABrowser` package is able to handle arbitrarily complex queries on the
Human Cell Atlas.

```{r complexFilter}
hca2 <- hca %>% filter((!organ.text %in% c('Brain', 'blood')) & 
                       (files.specimen_from_organism_json.genus_species.text == "Homo sapiens" |
                        library_preparation_protocol_json.library_construction_approach.text == 'Smart-seq2')
                )
hca2
``` 

The `HCABrowser` object can undo the most recent queries run on it.

```{r undoQuery}
hca <- hca %>% filter('files.specimen_from_organism_json.organ.text' == heart)
hca <- hca %>% filter('files.specimen_from_organism_json.organ.text' != brain)
hca <- undoEsQuery(hca, n = 2)
hca
```

If one would want to start from a fresh query but retain the modifications made
to the `HCABrowser` object, the `resetEsQuery()` method can be used.

```
hca <- resetEsQuery(hca)
hca
```

Using `fields()`, we can find that the fields `paired_end` and
`organ.ontology` are availiable. These fields can be shown in our resulting
`HCABrowser` object using the `select()` method.

```{r select}
hca2 <- hca %>% select('paired_end', 'organ.ontology')
hca2 <- hca %>% select(c('paired_end', 'organ.ontology'))
hca2
```

# Use examples

The remainder of this vignette will review applicable uses for this package.

## Download a Bundle

```{r getBundle}
hca <- HCABrowser()

hca <- hca %>% filter('files.specimen_from_organism_json.organ.text' == "brain")
result <- searchBundles(hca, replica='aws', output_format='raw')
result <- parseToSearchResults(result)

bundles <- lapply(results(result), function(results) {
    uuid <- stringr::str_split(results[["bundle_fqid"]], '\\.')[[1]][[1]]
    getBundle(hca, uuid=uuid, replica='aws')
})

bundles
```

## Download a File

```{r getFile}
hca <- HCABrowser()

hca <- hca %>% filter('files.specimen_from_organism_json.organ.text' == "brain")
result <- searchBundles(hca, replica='aws', output_format='raw')
result <- parseToSearchResults(result)

bundles <- lapply(results(result), function(results) {
    uuid <- stringr::str_split(results[["bundle_fqid"]], '\\.')[[1]][[1]]

    bundle <- c()

    tryCatch({
    checkout_id <- httr::content(checkoutBundle(hca, uuid=uuid, replica='aws'))$checkout_job_id

    bundle_checkout_status <- httr::content(getBundleCheckout(hca, checkout_job_id=checkout_id, replica='aws'))$status

    bundle <- getBundle(uuid=uuid, replica='aws')
    httr::content(bundle)
    }, error = function(e) {
        message('Missing checkout request')
    }, finally = {
        NULL
    })
})

files <- lapply(bundles, function(bundle) {
    uuid <- bundle[['bundle']]['uuid']
    getFile(hca, uuid=uuid, replica='aws')
})

files
```

## Download an Expression Matrix

In addition to the method shown below, the HCAMatrixBrowser package can be
used itself to download expression matrix data.

```
# Add to filter (functions like json)
hca <- hca %>% filter('files.project_json.project_core.project_title' == 'Census of Immume Cells')

# Use ropenapi to access search function
ret <- searchBundles(hca, getEsQuery(hca), replica='aws', output_format='raw')

# Prepare SearchResult object
ret <- parseToSearchResults(ret)
res <- results(ret)

# Process SearchResult object, fetch file, save results file to BiocFileCache
for(bundles in res) {
    file <- bundles[['metadata']][['manifest']][['files']]
    if (endsWith('results', file)) {
        file <- searchBundles(hca, uuid=file, replica='aws')
        saveToBiocFileCache(file)
    }
}
```

## Download all fastq files in a project



```{r fastq}

```

# Developer notes

- The `S3` object-oriented programming paradigm is used.
- Methods from the `dplyr` package can be used to manipulate objects in the
`HCABrowser` package.
- In the future, we wish to expand the functionalit of this packages to cover
the remaining functionality of the hca dcp api.

