Skip to content

Commit 68f7d05

Browse files
authored
Parallel process clusters and updates to cytetype v0.13.0 (#35)
* prod link update * Update .gitignore * n_parallel_clus doc * n_parallel_clus_validate * add ontology term name * update llm providers * fix parallel clus class * save job_details * Update cytetype.R * n_parallel_clus inputdata * Update schema.R * Update .gitignore * Update schema.R * Update cytetype.R * Update DESCRIPTION * Increment version number to 0.2.0
1 parent 91baa8c commit 68f7d05

7 files changed

Lines changed: 71 additions & 15 deletions

File tree

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,3 +9,5 @@
99
revdep/
1010
inst/doc
1111
CyteTypeR.Rproj
12+
query.json
13+
*.json

DESCRIPTION

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
Package: CyteTypeR
22
Title: CyteType for R
3-
Version: 0.1.5
3+
Version: 0.2.0
44
Description: CyteTypeR is the R version of CyteType python package.'
55
Authors@R:
66
person("Nygen Analytics AB", , ,"contact@nygen.io", role = c("aut", "cre"))

R/cytetype.R

Lines changed: 27 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -154,6 +154,8 @@ PrepareCyteTypeR <- function(obj,
154154
#' (in seconds). Default is 30.
155155
#' @param timeout_seconds Integer specifying maximum wait time for job completion
156156
#' (in seconds). Default is 1200 (20 minutes).
157+
#' @param n_parallel_clusters Number of parallel requests to make to the model. Maximum is 50. Note than high values can lead to rate limit errors.
158+
157159
#' @param api_url Optional character string specifying custom API URL.
158160
#' If `NULL`, uses default URL. Default is `NULL`.
159161
#' @param save_query Logical indicating whether to save the query as JSON file.
@@ -213,6 +215,7 @@ CyteTypeR <- function(obj,
213215
results_prefix = "cytetype",
214216
poll_interval_seconds = 30,
215217
timeout_seconds = 1200,
218+
n_parallel_clusters = 2,
216219
api_url = NULL,
217220
save_query = TRUE,
218221
query_filename = "query.json",
@@ -222,6 +225,7 @@ CyteTypeR <- function(obj,
222225
api_url <- api_url %||% .get_default_api_url()
223226
prepped_data$studyInfo <- study_context %||% ""
224227
prepped_data$infoTags <- metadata %||% list()
228+
prepped_data$nParallelClusters <- n_parallel_clusters
225229

226230
group_key <- prepped_data$group_key
227231
prepped_data$group_key <- NULL
@@ -245,21 +249,18 @@ CyteTypeR <- function(obj,
245249
llm_configs = llm_configs
246250
)
247251

248-
if (save_query){
249-
query_for_json <- .prepare_query_for_json(query_list)
250-
write_json(query_for_json, path = query_filename, auto_unbox = TRUE, pretty = TRUE)
251-
}
252+
# Prep query for submission
253+
query_for_json <- .prepare_query_for_json(query_list)
254+
252255
## NA value check on all data before submitting job
253256

254257

255258
# Job submission
256-
257259
job_id <- .submit_job(query_for_json, api_url, auth_token)
258260
if (is.na(job_id)) {
259261
stop("Job submission failed.")
260262
}
261263

262-
263264
# Save job details
264265
report_url <- file.path(api_url, 'report',job_id)
265266

@@ -270,6 +271,16 @@ CyteTypeR <- function(obj,
270271
auth_token = auth_token
271272
)
272273

274+
if (exists("prepped_data", envir = .GlobalEnv)){
275+
job_detail_fieldname <- paste0(results_prefix,'_jobDetails')
276+
prepped_data[[job_detail_fieldname]] <<- job_details
277+
}
278+
279+
# Save the query as json file if true
280+
if (save_query){
281+
write_json(query_for_json, path = query_filename, auto_unbox = TRUE, pretty = TRUE)
282+
}
283+
273284
# poll for results
274285
result <- .poll_for_results(
275286
job_id,
@@ -287,15 +298,19 @@ CyteTypeR <- function(obj,
287298
obj@misc$cytetype_results <- transformed_results
288299

289300
ann_colname <- paste(results_prefix, group_key, sep = "_" )
290-
onto_colname <- paste("cytetype_cell_ontology", group_key, sep = "_")
301+
onto_colname <- paste(results_prefix, "ontologyTerm", group_key, sep = "_")
302+
ontoID_colname <- paste(results_prefix, "ontologyID", group_key, sep = "_")
291303

292304
cluster_ann_map <- setNames(transformed_results$annotation,
293305
transformed_results$clusterId)
294306
cluster_onto_map <- setNames(transformed_results$ontologyTerm,
295307
transformed_results$clusterId)
308+
cluster_onto_id_map <- setNames(transformed_results$ontologyTermID,
309+
transformed_results$clusterId)
296310

297311
obj@meta.data[[ann_colname]] <- factor(cluster_ann_map[as.vector(obj@meta.data[[group_key]])])
298312
obj@meta.data[[onto_colname]] <- factor(cluster_onto_map[as.vector(obj@meta.data[[group_key]])])
313+
obj@meta.data[[ontoID_colname]] <- factor(cluster_onto_id_map[as.vector(obj@meta.data[[group_key]])])
299314

300315

301316
return(obj)
@@ -336,6 +351,11 @@ CyteTypeR <- function(obj,
336351
#' @importFrom jsonlite write_json
337352
#' @export
338353
GetResults <- function(job_id = NULL){
354+
355+
if (is.null(job_id)){
356+
stop("Please provide the job_id for your CyteType run.")
357+
}
358+
339359
tryCatch({
340360
# if (is.null(job_id)){
341361
# job_details <- fromJSON(paste0('job_details_', job_id, '.json'))

R/schema.R

Lines changed: 34 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,16 @@
11
# Valid LLM Provider Names
2-
LLM_PROVIDERS <- c("google", "openai", "anthropic", "groq", "mistral", "openrouter", "bedrock")
2+
LLM_PROVIDERS <- c("anthropic",
3+
"bedrock",
4+
"fireworks",
5+
"google",
6+
"groq",
7+
"huggingface",
8+
"mistral",
9+
"openai",
10+
"openrouter",
11+
"vertex",
12+
"xai"
13+
)
314

415
# Valid Agent Type Names
516
AGENT_TYPES <- c("contextualizer", "annotator", "reviewer", "summarizer", "clinician", "chat")
@@ -91,6 +102,13 @@ LLMModelConfig <- function(provider,
91102
obj_list[!sapply(obj_list, is.null)]
92103
})
93104
}
105+
106+
# Clear saved job_details from query for job submission
107+
mask_job_details <- !endsWith(names(query_list$input_data), 'jobDetails')
108+
109+
query_list$input_data <- query_list$input_data[mask_job_details]
110+
111+
94112
return(query_list)
95113
}
96114

@@ -127,7 +145,8 @@ InputData <- function(studyInfo = "",
127145
clusterMetadata = list(),
128146
markerGenes = list(),
129147
visualizationData = NULL,
130-
expressionData = list()) {
148+
expressionData = list(),
149+
nParallelClusters = numeric()) {
131150

132151
# Create the object
133152
obj <- list(
@@ -137,7 +156,8 @@ InputData <- function(studyInfo = "",
137156
clusterMetadata = clusterMetadata,
138157
markerGenes = markerGenes,
139158
visualizationData = visualizationData,
140-
expressionData = expressionData
159+
expressionData = expressionData,
160+
nParallelClusters = nParallelClusters
141161
)
142162

143163
# Set class
@@ -197,6 +217,13 @@ InputData <- function(studyInfo = "",
197217
stop("expressionData must be a named list")
198218
}
199219

220+
if (!is.numeric(obj$nParallelClusters) ||
221+
obj$nParallelClusters != round(obj$nParallelClusters) ||
222+
obj$nParallelClusters < 1 ||
223+
obj$nParallelClusters > 50) {
224+
stop("n_parallel_clusters must be an integer in range of 1 to 50")
225+
}
226+
200227
# Validate nested structure of clusterMetadata
201228
if (length(obj$clusterMetadata) > 0) {
202229
for (cluster_id in names(obj$clusterMetadata)) {
@@ -316,6 +343,9 @@ get_example_input_data <- function() {
316343
"Cluster2" = 2.8,
317344
"Cluster3" = 5.2
318345
)
319-
)
346+
),
347+
348+
nParallelClusters = 5
349+
320350
)
321351
}

R/seurat_helpers.R

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -224,7 +224,8 @@
224224
data.frame(
225225
clusterId = as.character(cluster_map[ann$clusterId] %||% ann$clusterId),
226226
annotation = as.character(ann$annotation %||% 'Unknown'),
227-
ontologyTerm = as.character(ann$cellOntologyTerm %||% 'Unknown'),
227+
ontologyTerm = as.character(ann$cellOntologyTermName %||% 'Unknown'),
228+
ontologyTermID = as.character(ann$cellOntologyTerm %||% 'Unknown'),
228229
granularAnnotation = as.character(ann$granularAnnotation %||% ''),
229230
cellState = as.character(ann$cellState %||% ''),
230231
justification = as.character(ann$justification %||% ''),

R/zzz.R

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
.onLoad <- function(libname, pkgname) {
33
# Set default configuration options
44
options(
5-
cytetype.default.api.url = "https://nygen-labs-prod--cytetype-api.modal.run",
5+
cytetype.default.api.url = "https://prod.cytetype.nygen.io",
66
cytetype.default.poll.interval = 10L,
77
cytetype.default.timeout = 7200L
88
)
@@ -14,7 +14,7 @@
1414

1515
# Get Default API URL
1616
.get_default_api_url <- function() {
17-
getOption("cytetype.default.api.url", "https://nygen-labs-prod--cytetype-api.modal.run")
17+
getOption("cytetype.default.api.url", "https://prod.cytetype.nygen.io")
1818
}
1919

2020
# Get Default Poll Interval

man/CyteTypeR.Rd

Lines changed: 3 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)