Skip to content

Commit

Permalink
Adds new end-points and improves to existing ones (#5)
Browse files Browse the repository at this point in the history
* adds new end-points and impv to previous ones

* adds pkgdown html links

* linting correction

* R-cmd-check in every push and PR

* minor correction to title

* updates doc

* chnages to snakecase for team_id and base_url

* error message() -> stop() which will exit with message.

* minor corrections

* local build and check

* --no-build-vignettes added for R-CMD-Check as it require api-key to build

* Attempt to fix tests (respect --no-build)

* Attempt to address #5 (comment)

* syntax fix in YAML

* vignette oved to README

* cleaning

* no else based on @cgpu 's review

Co-authored-by: cgpu <38183826+cgpu@users.noreply.github.com>
  • Loading branch information
sk-sahu and cgpu committed Feb 19, 2021
1 parent eafa760 commit dc0a98d
Show file tree
Hide file tree
Showing 46 changed files with 13,746 additions and 4,425 deletions.
2 changes: 2 additions & 0 deletions .Rbuildignore
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,5 @@
^docs$
^pkgdown$
^local*
README*
^check$
13 changes: 3 additions & 10 deletions .github/workflows/R-CMD-check.yaml
Original file line number Diff line number Diff line change
@@ -1,12 +1,5 @@
on:
push:
branches:
- master
pull_request:
branches:
- master

name: R-CMD-check
on: [push, pull_request]

jobs:
R-CMD-check:
Expand All @@ -26,7 +19,7 @@ jobs:
- {os: ubuntu-16.04, r: 'oldrel', rspm: "https://packagemanager.rstudio.com/cran/__linux__/xenial/latest"}
- {os: ubuntu-16.04, r: '3.5', rspm: "https://packagemanager.rstudio.com/cran/__linux__/xenial/latest"}
- {os: ubuntu-16.04, r: '3.4', rspm: "https://packagemanager.rstudio.com/cran/__linux__/xenial/latest"}
- {os: ubuntu-16.04, r: '3.3', rspm: "https://packagemanager.rstudio.com/cran/__linux__/xenial/latest"}
- {os: ubuntu-16.04, r: '3.3', rspm: "https://packagemanager.rstudio.com/cran/__linux__/xenial/latest", build_args: "--no-build-vignettes"}

env:
R_REMOTES_NO_ERRORS_FROM_WARNINGS: true
Expand Down Expand Up @@ -82,7 +75,7 @@ jobs:
- name: Check
env:
_R_CHECK_CRAN_INCOMING_: false
run: rcmdcheck::rcmdcheck(args = c("--no-manual", "--as-cran"), error_on = "warning", check_dir = "check")
run: rcmdcheck::rcmdcheck(args = c("--no-manual", "--as-cran", "--no-build-vignettes"), error_on = "warning", check_dir = "check", build_args = "--no-build-vignettes")
shell: Rscript {0}

- name: Show testthat output
Expand Down
4 changes: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
.Rproj.user
.Rhistory
.RData
local*
local*
inst/doc
check*
6 changes: 3 additions & 3 deletions DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
Package: cloudos
Title: R client library for CloudOS
Title: R Client Library for CloudOS
Version: 0.0.0.9000
Authors@R:
person(given = "Lifebit",
Expand All @@ -21,7 +21,7 @@ Suggests:
testthat,
lintr,
knitr,
kableExtra
kableExtra,
rmarkdown
URL: https://github.com/lifebit-ai/cloudos
BugReports: https://github.com/lifebit-ai/cloudos/issues
VignetteBuilder: knitr
2 changes: 2 additions & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
# Generated by roxygen2: do not edit by hand

export(create_cohort)
export(extract_genotypic_data)
export(extract_participants)
export(list_cohorts)
47 changes: 47 additions & 0 deletions R/create_cohort.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
#' @title Create Cohort
#'
#' @description Creates a new Cohort
#'
#' @param base_url Base URL of the CloudOS server. (Required)
#' @param auth An authentication method. (Required)
#' Example - Bearer token or API key.
#' @param team_id Team ID in CloudOS account. (Required)
#' @param cohort_name New cohort name to be created. (Required)
#' @param cohort_desc New cohort description to be created. (Required)
#' @param filters WIP - details will be added.
#'
#' @return A dataframe.
#'
#' @examples
#' \dontrun{
#' create_cohort(base_url= "https://cloudos.lifebit.ai",
#' auth = "Bearer ***token***",
#' team_id = "***team_id***",
#' cohort_name = "my cohort",
#' cohort_desc = "my cohort description")
#' }
#' @export
create_cohort <- function(base_url, auth, team_id,
cohort_name, cohort_desc, filters = "") {
url <- paste(base_url, "api/v1/cohort/", sep = "/")
r <- httr::POST(url,
httr::add_headers(.headers = c("Authorization" = auth,
"accept" = "application/json, text/plain, */*",
"content-type" = "application/json;charset=UTF-8")),
query = list("teamId" = team_id),
body = list("name" = cohort_name,
"description" = cohort_desc,
"moreFilters" = filters),
encode = "json"
)
if (!r$status_code == 200) {
stop("Something went wrong. Not able to create a cohort")
}
# parse the content
message("Cohort named ", cohort_name, " created successfully. Bellow are the details")
res <- httr::content(r)
# into a dataframe
res_df <- do.call(rbind, res)
colnames(res_df) <- "details"
return(res_df)
}
43 changes: 43 additions & 0 deletions R/extract_genotypic_data.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
#' @title Extract Genotypic Data
#'
#' @description Extract Genotypic Data with filter.
#'
#' @param base_url Base URL of the CloudOS server. (Required)
#' @param auth An authentication method. (Required)
#' Example - Bearer token or API key.
#' @param team_id Team ID in CloudOS account. (Required)
#' @param page_number Number of page. (Optional) Default - 0
#' @param page_size Number of entries in a page. (Optional) Default - 10
#' @param filters WIP - details will be added.
#'
#' @return A dataframe.
#'
#' @examples
#' \dontrun{
#' extract_genotypic_data(base_url= "https://cloudos.lifebit.ai",
#' auth = "Bearer ***token***",
#' team_id = "***team_id***")
#' }
#' @export
extract_genotypic_data <- function(base_url, auth, team_id,
page_number = 0,
page_size = 10,
filters = "") {
url <- paste(base_url, "api/v1/cohort/genotypic-data", sep = "/")
r <- httr::POST(url,
httr::add_headers(.headers = c("Authorization" = auth,
"accept" = "application/json, text/plain, */*",
"content-type" = "application/json;charset=UTF-8")),
query = list("teamId" = team_id),
body = list("pageNumber" = page_number,
"pageSize" = page_size,
"filters" = filters),
encode = "json"
)
if (!r$status_code == 200) {
stop("Something went wrong.")
}
# parse the content
res <- httr::content(r)
return(res$participants)
}
16 changes: 10 additions & 6 deletions R/extract_participants.R
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
#'
#' @description Extracts selected participants.
#'
#' @param baseurl Base URL of the CloudOS server. (Required)
#' @param base_url Base URL of the CloudOS server. (Required)
#' @param auth An authentication method. (Required)
#' Example - Bearer token or API key.
#' @param raw_data A JSON string for selected participants. (Required)
Expand All @@ -11,20 +11,24 @@
#'
#' @examples
#' \dontrun{
#' extract_participants(baseurl= "https://cloudos.lifebit.ai",
#' extract_participants(base_url= "https://cloudos.lifebit.ai",
#' auth = "Bearer ***token***",
#' raw_data = "a JSON string for selected participants")
#' }
#' @export
extract_participants <- function(baseurl, auth, raw_data) {
url <- paste(baseurl, "api/v1/cohort/participants/export", sep = "/")
extract_participants <- function(base_url, auth, raw_data) {
url <- paste(base_url, "api/v1/cohort/participants/export", sep = "/")
r <- httr::POST(url,
httr::add_headers(.headers = c("Authorization" = auth,
"accept" = "*/*",
"content-type" = "application/json")),
"accept" = "*/*",
"content-type" = "application/json")),
body = raw_data,
encode = "json"
)
if (!r$status_code == 200) {
stop("Something went wrong.")
}
# parse the content
res <- httr::content(r, as = "text")
df <- utils::read.csv(textConnection(res))
return(df)
Expand Down
63 changes: 32 additions & 31 deletions R/list_cohorts.R
Original file line number Diff line number Diff line change
@@ -1,56 +1,57 @@
#' @title Limited Cohort Data Extractor
#' @title List cohorts
#'
#' @description Extracts the data frame with limited cohort data columns.
#'
#' @param baseurl Base URL of the CloudOS server. (Required)
#' @param base_url Base URL of the CloudOS server. (Required)
#' @param auth An authentication method. (Required)
#' Example - Bearer token or API key.
#' @param teamid Team ID in CloudOS account. (Required)
#' @param team_id Team ID in CloudOS account. (Required)
#' @param page_number Number of page. (Optional) Default - 0
#' @param page_size Number of entries in a page. (Optional) Default - 10
#'
#' @return A data frame with available cohorts.
#'
#' @examples
#' \dontrun{
#' list_cohorts(baseurl= "https://cloudos.lifebit.ai",
#' list_cohorts(base_url= "https://cloudos.lifebit.ai",
#' auth = "Bearer ***token***",
#' teamid = "***teamid***")
#' team_id = "***team_id***")
#' }
#' @export
list_cohorts <- function(baseurl,
list_cohorts <- function(base_url,
auth,
teamid,
team_id,
page_number = 0,
page_size = 10) {
url <- paste(baseurl, "api/v1/cohort", sep = "/")
url <- paste(base_url, "api/v1/cohort", sep = "/")
r <- httr::GET(url,
httr::add_headers("Authorization" = auth),
query = list("teamId" = teamid,
query = list("teamId" = team_id,
"pageNumber" = page_number,
"pageSize" = page_size))
if (!r$status_code == 200) {
stop("No cohorts found. Or not able to connect with server.")
}
# parse the content
res <- httr::content(r)
if(length(res) == 0){
message("No cohorts found. Or not able to connect with server.")
} else {
message("Total number of cohorts found - ", res$total)
cohorts <- res$cohort
# make in to a list
cohorts_list <- list()
for(n in 1:res$total){
dta <- data.frame(id = cohorts[[n]]$`_id`,
name = cohorts[[n]]$`name`,
description = cohorts[[n]]$`description`,
number_of_participants = cohorts[[n]]$`numberOfParticipants`,
number_of_filters = cohorts[[n]]$`numberOfFilters`,
created_at = cohorts[[n]]$`createdAt`,
updated_at = cohorts[[n]]$`updatedAt`)
cohorts_list[[n]] <- dta
# filter
# cohorts[[1]]$`filters`
}
# make in to a dataframe
cohorts_df = do.call(rbind, cohorts_list)
return(cohorts_df)
message("Total number of cohorts found-", res$total,
". But here is 10. For more, change 'page_number' and 'page_size'")
cohorts <- res$cohorts
# make in to a list
cohorts_list <- list()
for (n in 1:page_size) {
dta <- data.frame(id = cohorts[[n]]$`_id`,
name = cohorts[[n]]$`name`,
description = cohorts[[n]]$`description`,
number_of_participants = cohorts[[n]]$`numberOfParticipants`,
number_of_filters = cohorts[[n]]$`numberOfFilters`,
created_at = cohorts[[n]]$`createdAt`,
updated_at = cohorts[[n]]$`updatedAt`)
cohorts_list[[n]] <- dta
# filter
# cohorts[[1]]$`filters`
}
# make in to a dataframe
cohorts_df <- do.call(rbind, cohorts_list)
return(cohorts_df)
}
94 changes: 94 additions & 0 deletions README.Rmd
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
---
output: github_document
---

<!-- README.md is generated from README.Rmd. Please edit that file -->

```{r setup, include = FALSE}
knitr::opts_chunk$set(
collapse = TRUE,
comment = "#>",
fig.path = "man/figures/README-",
out.width = "100%"
)
```

# cloudos

<!-- badges: start -->
[![Lifecycle: experimental](https://img.shields.io/badge/lifecycle-experimental-orange.svg)](https://www.tidyverse.org/lifecycle/#experimental)
[![R build status](https://github.com/lifebit-ai/cloudos/workflows/R-CMD-check/badge.svg)](https://github.com/lifebit-ai/cloudos/actions)
<!-- badges: end -->

The 'CloudOS' client library for R makes it easy to interact with CloudOS <https://cloudos.lifebit.ai/> in the R environment for analysis.

## Installation

You can install the released version of cloudos from [GitHub](https://github.com/lifebit-ai/cloudos/) at this moment. (Will be listed on [CRAN](https://CRAN.R-project.org) as well)

```{r, eval=FALSE}
if (!require(remotes)) { install.packages("remotes") }
remotes::install_github("lifebit-ai/cloudos")
```

## Usage

### Load the cloudos R-client library

```{r}
library(cloudos)
```

### Setup essentials

```{r}
cb_base_url <- "http://cohort-browser-766010452.eu-west-1.elb.amazonaws.com"
my_auth <- "*************************"
my_team_id <- "***************************"
```

```{r secrets, echo=FALSE}
my_auth <- Sys.getenv("cloudos_Bearer_token")
my_team_id <- Sys.getenv("cloudos_team_id")
```

### Create a cohort

```{r}
cloudos::create_cohort(base_url = cb_base_url,
auth = my_auth,
team_id = my_team_id,
cohort_name = "Cohort-R",
cohort_desc = "This cohort is for testing purpose, created from R.")
```

### List Cohorts

List available cohorts in a workspace.

```{r}
cohorts <- cloudos::list_cohorts(base_url = cb_base_url,
auth = my_auth,
team_id = my_team_id)
kableExtra::kable(cohorts)
```

### Extract participants

Create a RAW data string. This usually generates after selecting participants on UI. (more information will be added how to create this in R)

```{r}
new_raw_data <- '{"columns":[{"id":34,"instance":0,"array":{"type":"exact","value":0}},{"id":31,"instance":0,"array":{"type":"exact","value":0}},{"id":52,"instance":0,"array":{"type":"exact","value":0}},{"id":5984,"instance":0,"array":{"type":"avg"}},{"id":5984,"instance":0,"array":{"type":"min"}},{"id":5984,"instance":0,"array":{"type":"max"}},{"id":20001,"instance":0,"array":{"type":"exact","value":0}}],"ids":["5f185b92bf92ed4d3be9637d","5edbdd689d700db709af0c2f","5f185b91bf92ed4d3be9587e","5f185b91bf92ed4d3be95984","5edbdd689d700db709af0c3e","5edbdd689d700db709af0c2b","5edbdd689d700db709af0c2d","5f185b93bf92ed4d3be982e9","5edbdd689d700db709af0c2a","5edbdd689d700db709af0c4d"],"type":"csv","base_url":"http://cohort-browser-766010452.eu-west-1.elb.amazonaws.com"}'
```

Using this above raw data lets extract selected participants.

```{r}
df <- cloudos::extract_participants(base_url= cb_base_url,
auth = my_auth,
raw_data = new_raw_data)
kableExtra::kable(df)
```

Loading

0 comments on commit dc0a98d

Please sign in to comment.