---
title: "Reference database report"
author: "refdb"
date: "`r as.character(format(Sys.Date(), format = '%B %d, %Y'))`"
output: 
    html_document:
        toc: true
        toc_float: true
        df_print: paged
---

```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = FALSE, message = FALSE, warning = FALSE)
```


## Reference library overview

```{r}
refdb:::check_fields(x, c("sequence", "taxonomy"))
x_seq <- x[[attributes(x)$refdb$sequence]]
```


Total number of sequences: `r sum(!is.na(x_seq))`

Number of unique sequences: `r sum(!duplicated(x_seq[!is.na(x_seq)]))`

```{r, message=FALSE, warning=FALSE}
refdb_plot_tax_barplot(x)

refdb_plot_tax_treemap(x)

if(all(c("latitude", "longitude") %in%
       names(refdb_get_fields(x, silent = TRUE)))
   ){
refdb_plot_map(x)
}
```

## Check for spelling errors
```{r}
check_typo <- refdb_check_tax_typo(x)
check_typo <- dplyr::bind_rows(check_typo, .id = "Level")

DT::datatable(check_typo,
              rownames = FALSE,
              options = list(searching = FALSE,
                             pageLength = 10
                             )
)

```


## Check for taxonomic conflicts
```{r}
check_taxo <- refdb_check_tax_conflict(x)
check_taxo <- dplyr::bind_rows(check_taxo, .id = "Level")
check_taxo <- dplyr::group_by(check_taxo, Level, Taxon)



format_taxo <- function(y) {
  tax <- stringr::str_split(y, " > ")
  tax <- do.call("rbind", tax)
  tax_test <- apply(tax, 2, function(x) length(unique(x)) == 1)
  sel <- seq(min(which(tax_test == FALSE)) - 1, length(tax_test))
  res <- apply(tax[, sel], 1, paste, collapse = " > ")
  if(min(sel) > 1) {
    res <- paste("... >", res)
  }
  res <- paste(res, collapse = "<br/>")
  return(res)
}

if(nrow(check_taxo) > 0) {
  check_taxo <- dplyr::summarise(check_taxo, Taxonomy = format_taxo(Taxonomy))
}

DT::datatable(check_taxo,
              rownames = FALSE,
              escape = FALSE,
              options = list(searching = FALSE,
                             pageLength = 10
                             )
)

```


## Check for sequence conflicts
```{r}
check_seqs <- refdb_check_seq_conflict(x)

if(nrow(check_seqs) == 0) {
  cat("No sequence conflict detected.")
} else {
  check_seqs <- dplyr::group_by(check_seqs, level_conflict, sequence)
  check_seqs <- dplyr::summarise(check_seqs, taxonomy = format_taxo(taxonomy))
  check_seqs <- dplyr::select(check_seqs, sequence, level_conflict, taxonomy)
  
  check_seqs$sequence <- paste('<span style="width: 250px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; display:inline-block;">', check_seqs$sequence,'</span>')
  
  check_seqs <- setNames(check_seqs, c("Sequence", "Level", "Taxonomy"))
  
  DT::datatable(check_seqs,
                escape = FALSE,
                rownames = FALSE,
                options = list(searching = FALSE,
                               pageLength = 10))
  
}


```



