## ----setup, include=FALSE-----------------------------------------------------
knitr::opts_chunk$set(
  collapse=TRUE,
  comment="#>",
  warning=FALSE,
  error=FALSE,
  eval=FALSE
)

## ----library, message=FALSE, warning=FALSE, error=FALSE-----------------------
# library(HPAanalyze)
# library(dplyr)
# library(xml2)

## -----------------------------------------------------------------------------
# CCNB1xml <- hpaXmlGet('ENSG00000134057')
# ## or, offline: CCNB1xml <- xml2::read_xml("data/ENSG00000134057.xml")

## -----------------------------------------------------------------------------
# CCNB1 <- hpaXmlParse(CCNB1xml)
# length(CCNB1)
# #> [1] 37
# 
# names(CCNB1)
# #>  [1] "antibody"                     "antibodyTargetWeights"
# #>  [3] "antibodyTargetWeights_weight" "assayImage"
# #>  [5] "blotLanes"                    "cellExpression"
# #>  [7] "data"                         "data_level"
# #>  [9] "data_location"                "entry"
# #> [11] "entry_synonym"                "identifier"
# #> [13] "identifier_xref"              "image"
# #> [15] "image_channel"                "image_imageUrl"
# #> [17] "lane"                         "lane_weight"
# #> [19] "patient"                      "patient_level"
# #> [21] "patient_location"             "proteinClasses"
# #> [23] "proteinClasses_proteinClass"  "proteinEvidence"
# #> [25] "proteinEvidence_evidence"     "rnaExpression"
# #> [27] "sample"                       "snomedParameters"
# #> [29] "snomedParameters_snomed"      "subAssay"
# #> [31] "tissueCell"                   "tissueCell_cellType"
# #> [33] "tissueCell_level"             "tissueCell_location"
# #> [35] "tissueExpression"             "tissueExpression_validation"
# #> [37] "westernBlot"

## -----------------------------------------------------------------------------
# CCNB1$entry
# #> # A tibble: 1 x 4
# #>   version url                                         entry_id name
# #>   <chr>   <chr>                                          <int> <chr>
# #> 1 18      http://v18.proteinatlas.org/ENSG00000134057        1 CCNB1

## -----------------------------------------------------------------------------
# CCNB1$data %>% select(data_id, tissueExpression_id, tissue) %>% head(3)
# #> # A tibble: 3 x 3
# #>   data_id tissueExpression_id tissue
# #>     <int>                <int> <chr>
# #> 1       1                    1 adrenal gland
# #> 2       2                    1 appendix
# #> 3       3                    1 bone marrow
# 
# CCNB1$tissueCell %>% head(3)
# #> # A tibble: 3 x 3
# #>   data_id tissueCell_id quantity
# #>     <int>         <int> <chr>
# #> 1       1             1 <NA>
# #> 2       2             2 <NA>
# #> 3       2             3 <NA>
# 
# CCNB1$tissueCell_level %>% head(3)
# #> # A tibble: 3 x 4
# #>   type       value        tissueCell_id count
# #>   <chr>      <chr>                <int> <chr>
# #> 1 expression not detected             1 <NA>
# #> 2 expression medium                   2 <NA>
# #> 3 expression low                      3 <NA>

## -----------------------------------------------------------------------------
# cellType <- CCNB1$tissueCell_cellType %>% rename(cellType = value)
# level    <- CCNB1$tissueCell_level    %>% rename(level = value)
# 
# CCNB1$data %>%
#     filter(tissue == "liver", !is.na(tissueExpression_id)) %>%
#     inner_join(CCNB1$tissueCell, by = "data_id") %>%
#     inner_join(cellType, by = "tissueCell_id") %>%
#     inner_join(level, by = "tissueCell_id") %>%
#     select(tissueExpression_id, tissue, cellType, level_type = type, level)
# 
# #> # A tibble: 6 x 5
# #>   tissueExpression_id tissue cellType        level_type level
# #>                  <int> <chr>  <chr>           <chr>      <chr>
# #> 1                    1 liver  bile duct cells expression not detected
# #> 2                    1 liver  hepatocytes     expression not detected
# #> 3                    2 liver  bile duct cells staining   not detected
# #> 4                    2 liver  bile duct cells intensity  Negative
# #> 5                    2 liver  hepatocytes     staining   not detected
# #> 6                    2 liver  hepatocytes     intensity  Negative

## -----------------------------------------------------------------------------
# saveRDS(CCNB1, "data/CCNB1_parsed_relational.rds")

