---
title: "TCGAbiolinks: Clinical data"
date: "`r BiocStyle::doc_date()`"
vignette: >
  %\VignetteIndexEntry{"4. Clinical data"}
  %\VignetteEngine{knitr::rmarkdown}
  %\VignetteEncoding{UTF-8}
---

```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
knitr::opts_knit$set(progress = FALSE)
```

```{r message=FALSE, warning=FALSE, include=FALSE}
library(TCGAbiolinks)
library(SummarizedExperiment)
library(dplyr)
library(DT)
```


**TCGAbiolinks** has provided a few functions to search, download and parse clinical data.
This section starts by explaining the different sources for clinical information in GDC, followed by the necessary
function to access these sources and it finishes by showing the insconsistencies between those sources.


---
# Useful information


<div class="panel panel-info">
<div class="panel-heading">Different sources</div>
<div class="panel-body">

In GDC database the clinical data can be retrieved from two sources:

- indexed clinical: a refined clinical data that is created using the XML files.
- XML files

There are two main differences:

- XML has more information: radiation, drugs information, follow-ups, biospecimen, etc. So the indexed one is only a subset of the XML files
- The indexed data contains the updated data with the follow up informaiton. 
  For example: if the patient is alive in the first time clinical data was collect and the in the next follow-up he is dead, 
  the indexed data will show dead. The XML will have two fields, one for the first time saying he is alive (in the clinical part) and the follow-up saying he is dead. You can see this case here: 

</div>
</div>


# Get clinical indexed data

In this example we will fetch clinical indexed data.

```{r results='hide', echo=TRUE, message=FALSE, warning=FALSE}
clinical <- GDCquery_clinic(project = "TCGA-LUAD", type = "clinical")
```
```{r  echo=TRUE, message=FALSE, warning=FALSE}
datatable(clinical, filter = 'top', 
          options = list(scrollX = TRUE, keys = TRUE, pageLength = 5),  
          rownames = FALSE)
```


# Parse XML clinical data

The process to get data directly from the XML are:
1. Use `GDCquery` and `GDCDownload` functions to search/download either biospecimen or clinical XML files
2. Use `GDCprepare_clinic` function to parse the XML files.

The relation between one patient and other clinical information are 1:n, 
one patient could have several radiation treatments. For that reason, we only give the option
to parse individual tables (only drug information, only radiation informtaion,...)
The selection of the tabel is done by the argument `clinical.info`.

<div class="panel panel-info">
<div class="panel-heading">clinical.info options to parse information for each data category </div>
<div class="panel-body">
| data.category | clinical.info |
|------------|--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| Clinical | drug |
| Clinical | admin |
| Clinical | follow_up |
| Clinical | radiation |
| Clinical | patient |
| Clinical | stage_event |
| Clinical | new_tumor_event |
| Biospecimen | sample |
| Biospecimen | bio_patient |
| Biospecimen | analyte |
| Biospecimen | aliquot |
| Biospecimen | protocol |
| Biospecimen | portion |
| Biospecimen | slide |
| Other | msi |
</div>
</div>

Below are several examples fetching clinical data directly from the clinical XML files.

```{r results = 'hide', echo=TRUE, message=FALSE, warning=FALSE}
query <- GDCquery(project = "TCGA-COAD", 
                  data.category = "Clinical", 
                  barcode = c("TCGA-RU-A8FL","TCGA-AA-3972"))
GDCdownload(query)
clinical <- GDCprepare_clinic(query, clinical.info = "patient")
```
```{r  echo = TRUE, message = FALSE, warning = FALSE}
datatable(clinical, options = list(scrollX = TRUE, keys = TRUE), rownames = FALSE)
```
```{r results = 'hide', echo=TRUE, message=FALSE, warning=FALSE}
clinical.drug <- GDCprepare_clinic(query, clinical.info = "drug")
```
```{r  echo = TRUE, message = FALSE, warning = FALSE}
datatable(clinical.drug, options = list(scrollX = TRUE, keys = TRUE), rownames = FALSE)
```
```{r results = 'hide', echo=TRUE, message=FALSE, warning=FALSE}
clinical.radiation <- GDCprepare_clinic(query, clinical.info = "radiation")
```
```{r  echo = TRUE, message = FALSE, warning = FALSE}
datatable(clinical.radiation, options = list(scrollX = TRUE,  keys = TRUE), rownames = FALSE)
```
```{r results = 'hide', echo=TRUE, message=FALSE, warning=FALSE}
clinical.admin <- GDCprepare_clinic(query, clinical.info = "admin")
```
```{r  echo = TRUE, message = FALSE, warning = FALSE}
datatable(clinical.admin, options = list(scrollX = TRUE, keys = TRUE), rownames = FALSE)
```


## Microsatellite data


MSI-Mono-Dinucleotide Assay is performed to test a panel of four mononucleotide repeat loci (polyadenine tracts BAT25, BAT26, BAT40, and transforming growth factor receptor type II) and three dinucleotide repeat loci (CA repeats in D2S123, D5S346, and D17S250). Two additional pentanucleotide loci (Penta D and Penta E) are included in this assay to evaluate sample identity. Multiplex fluorescent-labeled PCR and capillary electrophoresis were used to identify MSI if a variation in the number of microsatellite repeats was detected between tumor and matched non-neoplastic tissue or mononuclear blood cells. Equivocal or failed markers were re-evaluated by singleplex PCR.

classifications: microsatellite-stable (MSS), low level MSI (MSI-L) if less than 40% of markers were altered and high level MSI (MSI-H) if greater than 40% of markers were altered.

Reference: [TCGA wiki](https://wiki.nci.nih.gov/display/TCGA/Microsatellite+data)

Level 3 data is included in BCR clinical-based submissions and can be downloaded as follows:

```{r results = 'hide', echo=TRUE, message=FALSE, warning=FALSE}

query <- GDCquery(project = "TCGA-COAD", 
                  data.category = "Other",
                  legacy = TRUE,access = "open",
                  data.type = "Auxiliary test",
                   barcode = c("TCGA-AD-A5EJ","TCGA-DM-A0X9"))  
GDCdownload(query)
msi_results <- GDCprepare_clinic(query, "msi")
```
```{r  echo=TRUE, message=FALSE, warning=FALSE}
datatable(msi_results, options = list(scrollX = TRUE, keys = TRUE))
```


# Get legacy clinical data

The clincal data types available in legacy database are:

* Biospecimen data (Biotab format)
* Tissue slide image (SVS format)
* Clinical Supplement (XML format)
* Pathology report (PDF)
* Clinical data (Biotab format)

```{r results = 'hide', echo=TRUE, message=FALSE, warning=FALSE}
# Tissue slide image files
query <- GDCquery(project = "TCGA-COAD", 
                  data.category = "Clinical", 
                  data.type = "Tissue slide image",
                  legacy = TRUE,
                  barcode = c("TCGA-RU-A8FL","TCGA-AA-3972")) 
```

```{r  echo=TRUE, message=FALSE, warning=FALSE}
query %>% getResults %>% datatable(options = list(scrollX = TRUE, keys = TRUE))
```

```{r results = 'hide', echo=TRUE, message=FALSE, warning=FALSE}
# Pathology report
query <- GDCquery(project = "TCGA-COAD", 
                  data.category = "Clinical", 
                  data.type = "Pathology report",
                  legacy = TRUE,
                  barcode = c("TCGA-RU-A8FL","TCGA-AA-3972"))  
```

```{r  echo=TRUE, message=FALSE, warning=FALSE}
query %>% getResults %>% datatable(options = list(scrollX = TRUE, keys = TRUE))
```

```{r results = 'hide', echo=TRUE, message=FALSE, warning=FALSE}
# Tissue slide image
query <- GDCquery(project = "TCGA-COAD", 
                  data.category = "Clinical", 
                  data.type = "Tissue slide image",
                  legacy = TRUE,
                  barcode = c("TCGA-RU-A8FL","TCGA-AA-3972")) 
```

```{r  echo=TRUE, message=FALSE, warning=FALSE}
query %>% getResults %>% datatable(options = list(scrollX = TRUE, keys = TRUE))
```

```{r results = 'hide', echo=TRUE, message=FALSE, warning=FALSE}
# Clinical Supplement
query <- GDCquery(project = "TCGA-COAD", 
                  data.category = "Clinical", 
                  data.type = "Clinical Supplement",
                  legacy = TRUE,
                  barcode = c("TCGA-RU-A8FL","TCGA-AA-3972")) 
```

```{r  echo=TRUE, message=FALSE, warning=FALSE}
query %>% getResults %>% datatable(options = list(scrollX = TRUE, keys = TRUE))
```

```{r results = 'hide', echo=TRUE, message=FALSE, warning=FALSE}
# Clinical data
query <- GDCquery(project = "TCGA-COAD", 
                  data.category = "Clinical", 
                  data.type = "Clinical data",
                  legacy = TRUE,
                  file.type = "txt")  
```

```{r  echo=TRUE, message=FALSE, warning=FALSE}
query %>% getResults %>% select(-matches("cases"))%>% datatable(options = list(scrollX = TRUE, keys = TRUE))
```

```{r results = 'hide', echo=TRUE, message=FALSE, warning=FALSE}
GDCdownload(query)
clinical.biotab <- GDCprepare(query)
```

```{r  echo=TRUE, message=FALSE, warning=FALSE}
names(clinical.biotab)
datatable(clinical.biotab$clinical_radiation_coad, options = list(scrollX = TRUE, keys = TRUE))
```


# Clinical data inconsistencies

<div class="panel panel-danger">
<div class="panel-heading">Clinical data inconsistencies</div>
<div class="panel-body">

Some inconsisentecies have been found in the indexed clinical data and are being
investigated by the GDC team. These inconsistencies are:

- ***Vital status*** field is not correctly updated 
- ***Tumor Grade*** field is not being filled
- ***Progression or Recurrence*** field is not being filled

</div>
</div>

## Vital status inconsistancie

```{r results = 'hide', echo=TRUE, message=FALSE, warning=FALSE}
# Get XML files and parse them
clin.query <- GDCquery(project = "TCGA-READ", data.category = "Clinical", barcode = "TCGA-F5-6702")
GDCdownload(clin.query)
clinical.patient <- GDCprepare_clinic(clin.query, clinical.info = "patient")
clinical.patient.followup <- GDCprepare_clinic(clin.query, clinical.info = "follow_up")

# Get indexed data
clinical.index <- GDCquery_clinic("TCGA-READ")
```
```{r  echo = TRUE, message = FALSE, warning = FALSE}
select(clinical.patient,vital_status,days_to_death,days_to_last_followup) %>% datatable
select(clinical.patient.followup, vital_status,days_to_death,days_to_last_followup) %>% datatable
# Vital status should be the same in the follow up table 
filter(clinical.index,submitter_id == "TCGA-F5-6702") %>% select(vital_status,days_to_death,days_to_last_follow_up) %>% datatable
```

## Progression or Recurrence and Grande inconsistancie

```{r results = 'hide', echo=TRUE, message=FALSE, warning=FALSE}
# Get XML files and parse them
recurrent.samples <- GDCquery(project = "TCGA-LIHC",
                             data.category = "Transcriptome Profiling",
                             data.type = "Gene Expression Quantification", 
                             workflow.type = "HTSeq - Counts",
                             sample.type = 	"Recurrent Solid Tumor")$results[[1]] %>% select(cases)
recurrent.patients <- unique(substr(recurrent.samples$cases,1,12))
clin.query <- GDCquery(project = "TCGA-LIHC", data.category = "Clinical", barcode = recurrent.patients)
GDCdownload(clin.query)
clinical.patient <- GDCprepare_clinic(clin.query, clinical.info = "patient") 
```
```{r  echo = TRUE, message = FALSE, warning = FALSE}
# Get indexed data
GDCquery_clinic("TCGA-LIHC") %>% filter(submitter_id %in% recurrent.patients) %>% 
    select(progression_or_recurrence,days_to_recurrence,tumor_grade) %>% datatable

# XML data
clinical.patient %>% select(bcr_patient_barcode,neoplasm_histologic_grade) %>% datatable

```


