forked from pluto-biosciences/pluto-sdk-R
-
Notifications
You must be signed in to change notification settings - Fork 0
/
pluto.R
152 lines (115 loc) · 4.84 KB
/
pluto.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
library(httr)
library(jsonlite)
library(rjson)
# Helper function to format JSON -> df
pluto_api_response_to_df <- function(response){
json_obj = prettify(httr::content(response, as = 'text', encoding = 'UTF-8'))
json_list = fromJSON(json_obj)
column_headers = unlist(json_list$headers)
#df = as.data.frame(json_list$items)
max_row = length(json_list$items)
df = as.data.frame(matrix(nrow = max_row, ncol = length(column_headers)))
names(df) = column_headers
for (i in 1:max_row){
col = json_list$items[[i]]
for (ii in 1:length(col)){
if (is.character(unlist(col[ii])) | is.numeric(unlist(col[ii]))){
df[i, ii] = col[ii]
}
}
}
return(df)
}
# Function to read sample and assay data
pluto_read <- function(experiment_id, data_type, limit=NULL, plot_id=NULL){
pagination_step_size = 10000
access_token = Sys.getenv('PLUTO_API_TOKEN')
if (is.null(access_token)){
stop("Missing access token. Have you set the PLUTO_API_TOKEN environment variable?")
}
if (data_type == 'sample'){
endpoint = '/sample-data/'
} else if (data_type == 'assay'){
endpoint = '/assay-data/'
} else if (data_type == 'results'){
if (is.null(plot_id)){
stop("plot_id param must be provided to fetch results")
} else{
endpoint = paste0('/plots/', plot_id, '/data/')
}
} else{
stop("Unsupported data_type. Supported types are 'sample', 'assay', and 'results'.")
}
if (!is.null(limit)){
path = paste0('https://api.pluto.bio/lab/experiments/',
experiment_id, endpoint, '?limit=', limit)
} else{
path = paste0('https://api.pluto.bio/lab/experiments/',
experiment_id, endpoint, '?limit=', pagination_step_size)
}
response = GET(path,
add_headers(Authorization = paste0('Token ',
access_token)))
if (response$status_code == 200){
# Calculate whether we need to paginate
response_obj = fromJSON(prettify(httr::content(response, as = 'text', encoding = 'UTF-8')))
total_count = response_obj$count
# Temp until count is added to the data/ endpoint
if (is.null(total_count)){
total_count = length(response_obj$items)
}
if (!is.null(limit)){
if (total_count <= limit){
message('All ', total_count, ' rows of the ', data_type, ' data were fetched.')
} else{
message('Note: Data has ', total_count, ' rows but only ', limit,
' will be fetched due to the "limit" parameter.',
'\nIncrease or remove the "limit" parameter if more rows are desired.')
}
final_df = pluto_api_response_to_df(response)
} else{
if (total_count <= pagination_step_size){
message('All ', total_count, ' rows of the ', data_type, ' data were fetched.')
final_df = pluto_api_response_to_df(response)
} else{
message('Paginating API calls to retrieve all ', total_count,
' rows in the ', data_type, ' data in batches of ',
pagination_step_size, ' rows...')
final_df = pluto_api_response_to_df(response)
offsets = as.numeric(unlist(lapply(split(1:total_count,
ceiling(seq_along(1:total_count)/pagination_step_size))[-1],
function (l){
l[[1]]
})))
for (offset_num in offsets){
message('Fetching rows ', offset_num, ' to ', offset_num + pagination_step_size, '...')
path = paste0('https://api.pluto.bio/lab/experiments/',
experiment_id, endpoint,
'?limit=', min(c(pagination_step_size,
total_count - nrow(final_df))),
'&offset=', offset_num)
response = GET(path,
add_headers(Authorization = paste0('Token ',
access_token)))
if (response$status_code == 200){
final_df = rbind(final_df,
pluto_api_response_to_df(response))
} else{
stop(paste0('Response: ', response$status_code))
}
}
}
}
if (data_type == 'sample'){
names(final_df) = tolower(names(final_df))
}
if (data_type == 'assay'){
names(final_df)[1] = tolower(names(final_df)[1])
}
return(final_df)
} else if (response$status_code == 401){
stop('Unauthorized: User does not have permission to view experiment')
} else {
stop(paste0('Response: ', response$status_code))
}
}