## ----include=FALSE-------------------------------------------------------
knitr::opts_chunk$set(eval = FALSE)

## ------------------------------------------------------------------------
#  library(sevenbridges)
#  a <- Auth(token = "8c3329a4de664c35bb657499bb2f335c", url = "https://api.sbgenomics.com/1.1/")
#  ## Get projects: GET /1.1/project
#  a$api(path = "project")
#  ## higher level doesn't work for v1
#  ## a$project() won't work

## ------------------------------------------------------------------------
#  source("http://bioconductor.org/biocLite.R")
#  useDevel(devel = TRUE)
#  biocLite("sevenbridges")

## ------------------------------------------------------------------------
#  # install.packages("devtools") if devtools was not installed
#  ## Install from github for development version
#  source("http://bioconductor.org/biocLite.R")
#  # install.packages("devtools") if devtools was not installed
#  library(devtools)
#  install_github("sbg/sevenbridges-r", build_vignettes=TRUE,
#    repos=BiocInstaller::biocinstallRepos(),
#    dependencies=TRUE)

## ------------------------------------------------------------------------
#  system.file("docker", "sevenbridges/Dockerfile", package = "sevenbridges")

## ------------------------------------------------------------------------
#  library(sevenbridges)
#  ## direct setup
#  a <- Auth(token = "<fake_token>",
#            url = "https://cgc-api.sbgenomics.com/v2/")
#  ## or load default from config file (autoloaded into options)
#  library(sevenbridges)
#  a <- Auth(platform = "us", username = "tengfei")

## ------------------------------------------------------------------------
#  a$user()

## ------------------------------------------------------------------------
#  a$user(username = "tengfei")

## ------------------------------------------------------------------------
#  a$rate_limit()

## ------------------------------------------------------------------------
#  ## check your billing info
#  a$billing()
#  a$invoice()

## ------------------------------------------------------------------------
#  a$billing(id = "your_billing_id", breakdown = TRUE)

## ------------------------------------------------------------------------
#  ## get billing group id
#  bid <- a$billing()$id
#  ## create new project
#  (p <- a$project_new(name = "api testing", bid, description = "Just a testing"))

## ---- eval = TRUE--------------------------------------------------------
## Add an CWL file to your project
fl.runif <- system.file("docker", "sevenbridges/rabix/runif.json", package = "sevenbridges")

## ----comment='', eval = TRUE---------------------------------------------
cat(readLines(fl.runif), sep = '\n')

## ------------------------------------------------------------------------
#  ## name your app
#  (aid <- p$app_add("runif", fl.runif)$id)

## ----comment='', eval = TRUE---------------------------------------------
fl <- system.file("docker", "sevenbridges/rabix/generator.R", package = "sevenbridges")
cat(readLines(fl), sep = '\n')

## ------------------------------------------------------------------------
#  ## rbx is the object returned by Tool function
#  (aid <- p$app_add("runif", rbx)$id)

## ------------------------------------------------------------------------
#  (tsk <- p$task_add(name = "randome number 5",
#             description = "Generate some random number",
#             app = aid,
#             inputs = list(min = 1, max = 10, number = 1, seed = 4)))
#  
#  ## confirm, show all task status is draft
#  p$task(status = "draft")
#  
#  ## or just list the task
#  tsk$update()

## ------------------------------------------------------------------------
#  ## not run
#  ## tsk$delete()

## ------------------------------------------------------------------------
#  tsk$getInputs()
#  ## missing number input, only update number
#  tsk$update(inputs = list(number = 500, seed = 1))
#  ## double check
#  tsk$getInputs()

## ------------------------------------------------------------------------
#  ## Run your task
#  tsk$run()

## ------------------------------------------------------------------------
#  tsk$update()

## ------------------------------------------------------------------------
#  ## Monitor your task (skip this part)
#  ## tsk$monitor()

## ------------------------------------------------------------------------
#  tsk$download("~/Downloads")

## ------------------------------------------------------------------------
#  setTaskHook("completed", function(){
#      tsk$download("~/Downloads")
#  })
#  tsk$monitor()

## ------------------------------------------------------------------------
#  ## Create Auth object from config file
#  a <- Auth(username = "yintengfei", platform = "us")
#  ## show all
#  getToken()
#  ## show all pre-set user token for platform
#  getToken("cgc")
#  ## show individual token for a user
#  getToken(platform = "cgc", username = "tengfei")

## ------------------------------------------------------------------------
#  library(sevenbridges)
#  ## direct setup
#  a <- Auth(token = "1c0e6e202b544030870ccc147092c257",
#            url = "https://cgc-api.sbgenomics.com/v2/")

## ------------------------------------------------------------------------
#  a$api()

## ------------------------------------------------------------------------
#  getOption("sevenbridges")$offset
#  getOption("sevenbridges")$limit

## ------------------------------------------------------------------------
#  ## first, search by id is fast
#  x <- a$app(visibility = "public", id = "djordje_klisic/public-apps-by-seven-bridges/sbg-ucsc-b37-bed-converter/0")
#  
#  ## show 100 items from public
#  x <-  a$app(visibility = "public")
#  length(x) ## 100
#  
#  
#  x <- a$app(visibility = "public", complete = TRUE)
#  length(x) ## 211 by March, 2016
#  ## this return nothing, because it's not in the first 100
#  a$app(visibility = "public", name = "bed converter")
#  ## this return an app, because it pulls all apps and did serach.
#  a$app(visibility = "public", name = "bed converter", complete = TRUE)

## ------------------------------------------------------------------------
#  a$rate_limit()

## ------------------------------------------------------------------------
#  ## return your information
#  a$user()
#  ## return user 'tengfei''s information
#  a$user("tengfei")

## ------------------------------------------------------------------------
#  ## return a BillingList object
#  (b <- a$billing())
#  a$billing(id = b$id, breakdown = TRUE)

## ------------------------------------------------------------------------
#  a$invoice()
#  a$invoice(id = "fake_id")

## ------------------------------------------------------------------------
#  a$project()

## ------------------------------------------------------------------------
#  a$project(owner = "tengfei")
#  a$project(owner = "yintengfei")

## ------------------------------------------------------------------------
#  a$project(detail = TRUE)

## ------------------------------------------------------------------------
#  ## want to return a project called
#  a$project("hello")

## ------------------------------------------------------------------------
#  a$project_new("api_testing_tcga", b$id,
#                description = "Test for API")

## ------------------------------------------------------------------------
#  a$project_new("controlled_project", b$id,
#                description = "Test for API", tags = list("tcga"))

## ------------------------------------------------------------------------
#  ## remove it, not run
#  a$project("api_testing")$delete()
#  ## check
#  ## will delete all projects matcht the name
#  delete(a$project("api_testing_donnot_delete_me"))

## ------------------------------------------------------------------------
#  a$project(id = "tengfei/helloworld")
#  a$project(id = "tengfei/helloworld")$update(name = "Hello World Update",
#                                                      description = "Update description")
#  

## ------------------------------------------------------------------------
#  a$project(id = "tengfei/demo-project")$member()

## ------------------------------------------------------------------------
#  m <- a$project(id = "tengfei/demo-project")$member_add(username = "yintengfei")

## ------------------------------------------------------------------------
#  m <- a$project(id = "tengfei/demo-project")$
#         member(username = "yintengfei")
#  m$update(copy = TRUE)

## ------------------------------------------------------------------------
#  m$delete()
#  ## confirm
#  a$project(id = "tengfei/demo-project")$member()

## ------------------------------------------------------------------------
#  p <- a$project(id = "tengfei/demo-project")
#  p$file()

## ------------------------------------------------------------------------
#  a$file(project = p$id)
#  a$file("omni", project = p$id, detail = TRUE)

## ------------------------------------------------------------------------
#  p$file()

## ------------------------------------------------------------------------
#  ## need to check
#  p$file(detail = TRUE)

## ------------------------------------------------------------------------
#  ## 1000G_omni2.5.b37.vcf
#  fid <- "561e1b33e4b0aa6ec48167d7"
#  fid2 <- "561e1b33e4b0aa6ec48167d3"
#  pid <- a$project("demo")$id
#  a$copyFile(c(fid, fid2), project = pid)
#  a$project(id = pid)$file()

## ------------------------------------------------------------------------
#  a$project("hello")$file(id = fid)$copyTo(pid)

## ------------------------------------------------------------------------
#  a$project("demo")$file()[[1]]$delete()
#  ## confirm the deletion
#  a$project("demo")$file()

## ------------------------------------------------------------------------
#  ## return 5 files
#  a$project("demo")$file("phase1")
#  ## delete all of them
#  delete(a$project("demo")$file("phase1"))
#  a$project("demo")$file("phase1")

## ------------------------------------------------------------------------
#  a$project("demo")$file()[[1]]$download_url()

## ------------------------------------------------------------------------
#  fid <- a$project("demo")$file()[[1]]$id
#  a$project("demo")$file(id = fid3)$download("~/Downloads/")

## ------------------------------------------------------------------------
#  fls <- a$project("demo")$file()
#  download(fls, "~/Downloads/")

## ------------------------------------------------------------------------
#  a$project("demo")$download("~/Downloads")

## ------------------------------------------------------------------------
#  a <- Auth(username = "tengfei", platform = "cgc")
#  fl <- system.file("extdata", "sample1.fastq", package = "sevenbridges")
#  
#  (p <- a$project(id = "tengfei/quickstart"))
#  
#  ## by default load .meta for the file
#  p$upload(fl, overwrite = TRUE)
#  ## pass metadata
#  p$upload(fl, overwrite = TRUE, metadata = list(library_id = "testid2", platform = "Illumina x11"))
#  ## rename
#  p$upload(fl, overwrite = TRUE, name = "sample_new_name.fastq",
#           metadata = list(library_id = "new_id"))
#  
#  

## ------------------------------------------------------------------------
#  dir.ext <- system.file("extdata", package = "sevenbridges")
#  list.files(dir.ext)
#  p$upload(dir.ext, overwrite = TRUE)

## ------------------------------------------------------------------------
#  dir.ext <- system.file("extdata", package = "sevenbridges")
#  ## enable full name
#  fls <- list.files(dir.ext, recursive = TRUE, full.names = TRUE)
#  p$upload(fls, overwrite = TRUE)
#  p$upload("~/Documents/Data/sbgtest/1000G_phase1.snps.high_confidence.b37.vcf")

## ------------------------------------------------------------------------
#  ## list all outputs file from a task id
#  a$task(id = "53020538-6936-422f-80de-02fa65ae4b39")$file()
#  
#  ## alternative way to list files under specific project
#  a$file(project = "tengfei/re",
#         origin.task = "53020538-6936-422f-80de-02fa65ae4b39")
#  
#  
#  ## you can filter by metadata as well
#  a$file(project = "tengfei/re",
#         origin.task = "53020538-6936-422f-80de-02fa65ae4b39",
#         metadata = list(experimental_strategy = "RNA-Seq"))

## ------------------------------------------------------------------------
#  a$file(project = "admin/sbg-public-data")

## ------------------------------------------------------------------------
#  (fl <- a$project(id = "tengfei/demo-project")$file(name = "sample.fastq"))

## ------------------------------------------------------------------------
#  ## show metadata
#  fl$meta()

## ------------------------------------------------------------------------
#  fl$update(name = "sample.fastq", metadata = list(new_item1 = "item1", new_item2 = "item2", file_extension = "fastq"))
#  ## check it out
#  fl$meta()

## ------------------------------------------------------------------------
#  ## meta is pulling the latest information via API
#  fl$meta()
#  ## field meta data saved the previous saved one
#  fl$metadata

## ------------------------------------------------------------------------
#  fl$setMeta(new_item3 = "item3")
#  fl
#  ## oops it removed rest of the meta
#  fl$setMeta(new_item4 = "item4", overwrite = TRUE)
#  fl

## ------------------------------------------------------------------------
#  ## check which schema we have
#  Metadata()$show(full = TRUE)
#  ## check details for each, play with it
#  platform()
#  paired_end()
#  quality_scale()

## ------------------------------------------------------------------------
#  Metadata(platform  = "Affymetrix SNP Array 6.0",
#           paired_end = 1,
#           quality_scale = "sanger",
#           new_item = "new test")

## ------------------------------------------------------------------------
#  a$app()
#  ## or show details
#  a$app(detail = TRUE)

## ------------------------------------------------------------------------
#  ## pattern match
#  a$app(name = "STAR")
#  ## unique id
#  aid <- a$app()[[1]]$id
#  aid
#  a$app(id = aid)
#  ## get a specific revision from an app
#  a$app(id = aid, revision = 0)

## ------------------------------------------------------------------------
#  ## my favorite, always
#  a$project("demo")$app()
#  
#  ## or alternatviely
#  pid <- a$project("demo")$id
#  a$app(project = pid)

## ------------------------------------------------------------------------
#  ## show 100 items from public
#  x = a$app(visibility = "public")
#  length(x)
#  x = a$app(visibility = "public", complete = TRUE)
#  length(x)
#  x = a$app(project = "tengfei/helloworld", complete = TRUE)
#  length(x)
#  a$app(visibility = "public", limit = 5, offset = 150)

## ------------------------------------------------------------------------
#  a$app("STAR", visibility = "public", complete = TRUE)

## ------------------------------------------------------------------------
#  aid <- a$app(visibility = "public")[[1]]$id
#  a$copyApp(aid, project = pid, name = "copy-rename-test")
#  ## check it is copied
#  a$app(project = pid)

## ------------------------------------------------------------------------
#  ap <- a$app(visibility = "public")[[1]]
#  a$project("demo")$app("index")
#  ## get a specific revision
#  a$project("demo")$app("index", revision = 0)
#  

## ------------------------------------------------------------------------
#  cwl.fl <- system.file("extdata", "bam_index.json", package = "sevenbridges")
#  a$project("demo")$app_add(short_name = "new_bam_index_app", filename = cwl.fl)
#  a$project("demo")$app_add(short_name = "new_bam_index_app", revision = 2, filename = cwl.fl)

## ------------------------------------------------------------------------
#  ## all tasks
#  a$task()
#  ## filter
#  a$task(status = "completed")
#  a$task(status = "running")

## ------------------------------------------------------------------------
#  ## better way
#  a$project("demo")$task()
#  
#  ## alternatively
#  pid <- a$project("demo")$id
#  pid
#  a$task(project = pid)

## ------------------------------------------------------------------------
#  ## push an app first
#  fl.runif <- system.file("extdata", "runif.json", package = "sbgr")
#  a$project("demo")$app_add("runif_draft", fl.runif)
#  runif_id <- "tengfei/demo-project/runif_draft"
#  ## create a draft task
#  a$project("demo")$task_add(name = "Draft runif 3",
#                             description = "Description for runif 3",
#                             app = runif_id,
#                             inputs = list(min = 1, max = 10))
#  ## confirm
#  a$project("demo")$task(status = "draft")

## ------------------------------------------------------------------------
#  ## get the single task you want to update
#  tsk <- a$project("demo")$task("Draft runif 3")
#  tsk
#  tsk$update(name = "Draft runif update", description = "draft 2",
#             inputs = list(max = 100))
#  ## alternative way to check all inputs
#  tsk$getInputs()

## ------------------------------------------------------------------------
#  tsk$run()
#  ## run update without information just return latest information
#  tsk$update()

## ------------------------------------------------------------------------
#  tsk$monitor()

## ------------------------------------------------------------------------
#  getTaskHook("completed")
#  getTaskHook("draft")
#  setTaskHook("draft", function(){message("never happens"); return(TRUE)})
#  getTaskHook("draft")

## ------------------------------------------------------------------------
#  ## abort
#  tsk$abort()
#  ## check
#  tsk$update()

## ------------------------------------------------------------------------
#  tsklst <- a$task(status = "draft")
#  ## delete a single task
#  tsklst[[1]]$delete()
#  ## confirm
#  a$task(status = "draft")
#  ## delete a list of tasks
#  delete(tsklst)

## ------------------------------------------------------------------------
#  tsk$download("~/Downloads")

## ------------------------------------------------------------------------
#  ## batch by items
#  (tsk <- p$task_add(name = "RNA DE report new batch 2",
#                     description = "RNA DE analysis report",
#                     app = rna.app$id,
#                     batch = batch(input = "bamfiles"),
#                     inputs = list(bamfiles = bamfiles.in,
#                                   design = design.in,
#                                   gtffile = gtf.in)))
#  
#  ## batch by metadata, input files has to have metadata fields specified
#  (tsk <- p$task_add(name = "RNA DE report new batch 3",
#                     description = "RNA DE analysis report",
#                     app = rna.app$id,
#                     batch = batch(input = "fastq",
#                                   c("metadata.sample_id", "metadata.library_id")),
#                     inputs = list(bamfiles = bamfiles.in,
#                                   design = design.in,
#                                   gtffile = gtf.in)))

## ------------------------------------------------------------------------
#  ## Authentification
#  getToken()
#  a <- Auth(token = token)
#  a <- Auth(token = token,
#            url = "https://cgc-api.sbgenomics.com/v2/")
#  a <- Auth(platform = "us", username = "tengfei")
#  
#  ## list API
#  a$api()
#  
#  ## Rate limits
#  a$rate_limit()
#  
#  ## Users
#  a$user()
#  a$user("tengfei")
#  
#  ## billing
#  a$billing()
#  a$billing(id = , breakdown = TRUE)
#  a$invoice()
#  a$invoice(id = "fake_id")
#  
#  ## Project
#  ### create new project
#  a$project_new(name = , billing_group_id = , description = )
#  ### list all project owned by you
#  a$project()
#  a$project(owner = "yintengfei")
#  ### partial match
#  p <-  a$project(name = , id = , exact = TRUE)
#  ### delete
#  p$delete()
#  ### update
#  p$update(name = , description = )
#  ### members
#  p$member()
#  p$member_add(username = )
#  p$member(username = )$update(write = , copy = , execute = )
#  p$memeber(usrname = )$delete()
#  
#  ## file
#  ### list all files in this project
#  p$file()
#  ### list all public files
#  a$file(visibility = "public")
#  ### copy
#  a$copyFile(c(fid, fid2), project = pid)
#  ### delete
#  p$file(id = fid)$delete()
#  ### download
#  p$file()[[1]]$download_url()
#  p$file(id = fid3)$download("~/Downloads/")
#  ### download all
#  download(p$file())
#  ### update a file
#  fl$update(name = , metadata = list(a =  ,b = , ...))
#  ### meta
#  fl$meta()
#  fl$setMeta()
#  fl$setMeta(..., overwrite = TRUE)
#  
#  ## App
#  a$app()
#  ### apps in a project
#  p$app()
#  p$app(name, id, revision = )
#  a$copyApp(aid, project = pid, name = )
#  ### add
#  p$app_add(short_name = , filename =)
#  
#  ## Task
#  a$task()
#  a$task(name = , id = )
#  a$task(status = )
#  
#  p$task()
#  p$task(name = , id = )
#  p$task(status = )
#  
#  tsk <- p$task(name = , id = )
#  tsk$update()
#  tsk$abort()
#  tsk$run()
#  tsk$download()
#  tsk$detele()
#  tsk$getInputs()
#  tsk$monitor()
#  
#  getTaskHook()
#  setTaskHook(statis = , fun  =)