## ---- code=readLines(system.file('vignettes_inc.R', package='biodb')), echo=FALSE----
# Disable automatic loading of extra biodb packages
Sys.setenv(BIODB_AUTOLOAD_EXTRA_PKGS="FALSE")

biodbVignettes <- data.frame()
files <- Sys.glob('*.Rmd')
for (f in files) {
	name <- sub('^(.*)\\.Rmd', '\\1', f, perl=TRUE)
	firstLines <- readLines(f, n=20)
	title <- grep("^title:", firstLines, value=TRUE)
	title <- sub('^title: *"(.*)\\.?"$', '\\1', title, perl=TRUE)
	desc <- grep("%\\\\VignetteIndexEntry", firstLines, value=TRUE)
	desc <- sub('^.*VignetteIndexEntry{(.*)}.*$', '\\1', desc, perl=TRUE)
    html <- paste0(name, '.html')
    link <- paste0('[', title, '](', html, ')')
    biodbVignettes <- rbind(biodbVignettes, data.frame(name=name, title=title,
                                                       desc=desc, html=html,
                                                       link=link))
}

make_vignette_ref <- function(name) {
	cat(biodbVignettes[biodbVignettes$name == name, 'link', drop=TRUE])
}

insert_features_table <- function() {
    featuresFile <- system.file("features.tsv",
                                package='biodb')
    featuresDf <- read.table(featuresFile, sep="\t", header=TRUE, quote="",
                             stringsAsFactors=FALSE)
    knitr::kable(featuresDf, "pipe", label="features",
                 caption="*biodb* main features. These are generic features (i.e.: present at top-level of architecture or present in at least a group of connectors), unless specified otherwise.")
}

## -----------------------------------------------------------------------------
mybiodb <- biodb::BiodbMain$new()

## ---- echo=FALSE, results='asis'----------------------------------------------
make_vignette_ref('new_connector')

## ---- echo=FALSE, results='asis'----------------------------------------------
make_vignette_ref('new_connector')

## ---- echo=FALSE, results='asis'----------------------------------------------
make_vignette_ref('new_connector')

## -----------------------------------------------------------------------------
chebiexDefFile <- system.file("extdata", "chebi_ex.yml", package='biodb')
connClass <- system.file("extdata", "ChebiExConn.R", package='biodb')
entryClass <- system.file("extdata", "ChebiExEntry.R", package='biodb')
source(connClass)
source(entryClass)
mybiodb$loadDefinitions(chebiexDefFile)

## -----------------------------------------------------------------------------
conn <- mybiodb$getFactory()$createConn('chebi.ex')

## -----------------------------------------------------------------------------
entryIds <- c('17001', '40304', '64679')
entriesDf <- mybiodb$entriesToDataframe(conn$getEntry(entryIds))

## ----entriesChebiExTable, echo=FALSE, results='asis'--------------------------
# Prevent RMarkdown from interpreting @ character as a reference:
entriesDf$smiles <- vapply(entriesDf$smiles, function(s) paste0('`', s, '`'), FUN.VALUE='')
knitr::kable(head(entriesDf), "pipe", caption="Some entries from ChebiEx database. Their charge value is not visible become it was not parsed from database file.")

## -----------------------------------------------------------------------------
id <- entryIds[[1]]
id

## -----------------------------------------------------------------------------
conn$getEntryPageUrl(id)

## ---- echo=FALSE, results='asis'----------------------------------------------
cat("<", conn$getEntryPageUrl(id), ">", sep='')

## -----------------------------------------------------------------------------
conn$getCacheFile(id)

## -----------------------------------------------------------------------------
chargeParsingDefFile <- system.file("extdata", "chebi_ex_charge_parsing.yml", package='biodb')

## ---- eval=FALSE, highlight=FALSE, code=readLines(chargeParsingDefFile)-------
#  databases:
#    chebi.ex:
#      parsing.expr:
#        charge: //chebi:charge

## -----------------------------------------------------------------------------
mybiodb$loadDefinitions(chargeParsingDefFile)

## -----------------------------------------------------------------------------
mybiodb$getFactory()$deleteConn(conn)

## -----------------------------------------------------------------------------
conn <- mybiodb$getFactory()$createConn('chebi.ex')
entriesDf <- mybiodb$entriesToDataframe(conn$getEntry(entryIds))

## ----entriesChebiExWithChargeTable, echo=FALSE, results='asis'----------------
# Prevent RMarkdown from interpreting @ character as a reference:
entriesDf$smiles <- vapply(entriesDf$smiles, function(s) paste0('`', s, '`'), FUN.VALUE='')
knitr::kable(head(entriesDf), "pipe", caption="Some entries from ChebiEx database. Their charge value is now visible, since the parsing expression has been added to the connector.")

## ---- echo=FALSE, results='asis'----------------------------------------------
make_vignette_ref('in_house_compound_db')

## -----------------------------------------------------------------------------
fileUrl <- system.file("extdata", "chebi_extract_with_unknown_column.tsv", package='biodb')

## ----compDbTable, echo=FALSE, results='asis'----------------------------------
compDbDf <- read.table(fileUrl, sep="\t", header=TRUE, quote="")
# Prevent RMarkdown from interpreting @ character as a reference:
compDbDf$smiles <- vapply(compDbDf$smiles, function(s) paste0('`', s, '`'), FUN.VALUE='')
knitr::kable(head(compDbDf), "pipe", caption="First lines of the compound database file.")

## -----------------------------------------------------------------------------
conn <- mybiodb$getFactory()$createConn('comp.csv.file', url=fileUrl)

## -----------------------------------------------------------------------------
entriesDf <- mybiodb$entriesToDataframe(conn$getEntry(conn$getEntryIds()))

## ----entriesTable, echo=FALSE, results='asis'---------------------------------
# Prevent RMarkdown from interpreting @ character as a reference:
entriesDf$smiles <- vapply(entriesDf$smiles, function(s) paste0('`', s, '`'), FUN.VALUE='')
knitr::kable(head(entriesDf), "pipe", caption="Some entries from the compound database.")

## -----------------------------------------------------------------------------
conn$setField('charge', 'elecCharge')

## -----------------------------------------------------------------------------
conn$deleteAllEntriesFromVolatileCache()

## -----------------------------------------------------------------------------
entries2Df <- mybiodb$entriesToDataframe(conn$getEntry(conn$getEntryIds()))

## ----entries2Table, echo=FALSE, results='asis'--------------------------------
# Prevent RMarkdown from interpreting @ character as a reference:
entries2Df$smiles <- vapply(entries2Df$smiles, function(s) paste0('`', s, '`'), FUN.VALUE='')
knitr::kable(head(entries2Df), "pipe", caption="Some entries from the compound database. They now show the newly parsed \"charge\" field.")

## ---- echo=FALSE, results='asis'----------------------------------------------
make_vignette_ref('new_connector')

## -----------------------------------------------------------------------------
conn <- mybiodb$getFactory()$getConn('chebi.ex')
entryIds <- c('17001', '40304', '64679')
entriesDf <- mybiodb$entriesToDataframe(conn$getEntry(entryIds))

## ----entriesChebiEx2Table, echo=FALSE, results='asis'-------------------------
# Prevent RMarkdown from interpreting @ character as a reference:
entriesDf$smiles <- vapply(entriesDf$smiles, function(s) paste0('`', s, '`'), FUN.VALUE='')
knitr::kable(head(entriesDf), "pipe", caption="Some entries from ChebiEx database. There is no field that indicates the number of stars of an entry.")

## -----------------------------------------------------------------------------
nStarsDefFile <- system.file("extdata", "chebi_ex_stars_field.yml", package='biodb')

## ---- eval=FALSE, highlight=FALSE, code=readLines(nStarsDefFile)--------------
#  databases:
#    chebi.ex:
#      parsing.expr:
#        n_stars: //chebi:return/chebi:entityStar
#  
#  fields:
#    n_stars:
#      description: The ChEBI example stars indicator.
#      class: integer

## -----------------------------------------------------------------------------
mybiodb$loadDefinitions(nStarsDefFile)

## -----------------------------------------------------------------------------
mybiodb$getFactory()$deleteConn(conn)

## -----------------------------------------------------------------------------
conn <- mybiodb$getFactory()$createConn('chebi.ex')
entriesDf <- mybiodb$entriesToDataframe(conn$getEntry(entryIds))

## ----entriesChebiEx2WithNstarsTable, echo=FALSE, results='asis'---------------
# Prevent RMarkdown from interpreting @ character as a reference:
entriesDf$smiles <- vapply(entriesDf$smiles, function(s) paste0('`', s, '`'), FUN.VALUE='')
knitr::kable(head(entriesDf), "pipe", caption="Some entries from ChebiEx database. Now there is a n_stars no field that indicates the number of stars of an entry.")

## -----------------------------------------------------------------------------
mybiodb$terminate()

