Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
XWcode11 committed Aug 8, 2024
0 parents commit 16f565f
Show file tree
Hide file tree
Showing 7 changed files with 141 additions and 0 deletions.
2 changes: 2 additions & 0 deletions .Rbuildignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
^CellTypeGPT\.Rproj$
^\.Rproj\.user$
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
.Rproj.user
22 changes: 22 additions & 0 deletions CellTypeGPT.Rproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
Version: 1.0

RestoreWorkspace: No
SaveWorkspace: No
AlwaysSaveHistory: Default

EnableCodeIndexing: Yes
UseSpacesForTab: Yes
NumSpacesForTab: 2
Encoding: UTF-8

RnwWeave: Sweave
LaTeX: pdfLaTeX

AutoAppendNewline: Yes
StripTrailingWhitespace: Yes
LineEndingConversion: Posix

BuildType: Package
PackageUseDevtools: Yes
PackageInstallArgs: --no-multiarch --with-keep.source
PackageRoxygenize: rd,collate,namespace
14 changes: 14 additions & 0 deletions DESCRIPTION
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
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
Encoding: UTF-8
Roxygen: list(markdown = TRUE)
RoxygenNote: 7.3.2
Imports:
httr2
3 changes: 3 additions & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# Generated by roxygen2: do not edit by hand

export(CellTypeGPT)
77 changes: 77 additions & 0 deletions R/CellTypeGPT.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@


#' Annotate Cell Types using Custom GPT
#'
#' This function annotates cell types based on input gene markers.
#'
#' @param input A list or data frame containing gene markers.
#' @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".
#' @return A character vector of annotated cell types.
#' @export
#' @name CellTypeGPT

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))
} 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(
"Authorization" = paste("Bearer", OPENAI_API_KEY),
"Content-Type" = "application/json"
) %>%
req_body_json(list(
model = model,
messages = list(list(
role = "user",
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]]

if (length(res) == length(id)) {
flag <- 1
}
}
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.')
return(gsub(',$', '', unlist(allres)))
}

22 changes: 22 additions & 0 deletions man/CellTypeGPT.Rd

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

0 comments on commit 16f565f

Please sign in to comment.