Skip to content

Commit

Permalink
fix some bugs
Browse files Browse the repository at this point in the history
  • Loading branch information
XWcode11 committed Aug 11, 2024
1 parent 16f565f commit 6b1d56e
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 30 deletions.
12 changes: 12 additions & 0 deletions .Rhistory
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
devtools::document()
devtools::document()
devtools::document()
devtools::build()
devtools::check()
setwd("E:/CellTypeGPT-master")
devtools::document()
devtools::document()
build()
devtools::build()
devtools::check()
remotes::install_github("XWcode11/CellTypeGPT")
19 changes: 8 additions & 11 deletions DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,14 +1,11 @@
Package: CellTypeGPT
Title: What the Package Does (One Line, Title Case)
Version: 0.0.0.9000
Authors@R:
person("First", "Last", , "[email protected]", role = c("aut", "cre"),
comment = c(ORCID = "YOUR-ORCID-ID"))
Description: What the package does (one paragraph).
License: `use_mit_license()`, `use_gpl3_license()` or friends to pick a
license
Type: Package
Title: Annotate Cell Types using Custom GPT
Version: 0.1.0
Author: Your Name
Maintainer: Your Name <[email protected]>
Description: This package provides functions to annotate cell types using custom GPT models.
License: MIT
Imports: httr2
Encoding: UTF-8
Roxygen: list(markdown = TRUE)
RoxygenNote: 7.3.2
Imports:
httr2
2 changes: 1 addition & 1 deletion NAMESPACE
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
# Generated by roxygen2: do not edit by hand

export(CellTypeGPT)
export(annotate_cell_types)
30 changes: 15 additions & 15 deletions R/CellTypeGPT.R
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@


#' Annotate Cell Types using Custom GPT
#'
#' This function annotates cell types based on input gene markers.
Expand All @@ -8,41 +6,44 @@
#' @param tissuename A character string specifying the tissue name.
#' @param model A character string specifying the model to use.
#' @param topgenenumber An integer specifying the number of top genes to consider.
#' @param baseurl A character string specifying the base URL for the API. Default is "https://api.default.com/v1/chat/completions".
#' @param api_base A character string specifying the base URL for the API.
#' @return A character vector of annotated cell types.
#' @export
#' @name CellTypeGPT
#' @name annotate_cell_types
#' @examples
#' # Example usage:
#' # annotate_cell_types(input = my_gene_data, tissuename = "liver", model = "gpt-4o", topgenenumber = 10, api_base = "https://api.default.com/v1/chat/completions")

library(httr2)


annotate_cell_types <- function(input, tissuename = NULL, model = 'gpt-4o', topgenenumber = 10, api_base) {
OPENAI_API_KEY <- Sys.getenv("OPENAI_API_KEY")

if (OPENAI_API_KEY == "") {
stop("OpenAI API key not found. Please set your API key in the environment.")
}


if (class(input) == 'list') {
input <- sapply(input, paste, collapse = ',')
} else {
input <- input[input$avg_log2FC > 0, , drop = FALSE]
input <- tapply(input$gene, list(input$cluster), function(i) paste0(i[1:topgenenumber], collapse = ','))
}

cutnum <- ceiling(length(input) / 30)
if (cutnum > 1) {
cid <- as.numeric(cut(1:length(input), cutnum))
cid <- as.numeric(cut(1:length(input), cutnum))
} else {
cid <- rep(1, length(input))
}

allres <- sapply(1:cutnum, function(i) {
id <- which(cid == i)
flag <- 0
while (flag == 0) {

req <- request(api_base) %>%
req_method("POST") %>%
req_headers(
Expand All @@ -56,10 +57,10 @@ annotate_cell_types <- function(input, tissuename = NULL, model = 'gpt-4o', topg
content = paste0('You are an expert in cell biology. Identify cell types of ', tissuename, ' cells using the following markers separately for each\n row. Only provide the cell type name. Do not show numbers or additional text before the name.\n Some can be a mixture of multiple cell types.\n', paste(input[id], collapse = '\n'))
))
))


resp <- req_perform(req)

content <- resp_body_json(resp)
res <- strsplit(content$choices[[1]]$message$content, '\n')[[1]]

Expand All @@ -70,8 +71,7 @@ annotate_cell_types <- function(input, tissuename = NULL, model = 'gpt-4o', topg
names(res) <- names(input)[id]
res
}, simplify = FALSE)

print('Note: It is always recommended to check the results returned by GPT in case of\n AI hallucination, before going to downstream analysis.')
print('DONE.')
return(gsub(',$', '', unlist(allres)))
}

10 changes: 7 additions & 3 deletions man/CellTypeGPT.Rd → man/annotate_cell_types.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 6b1d56e

Please sign in to comment.