forked from Winnie09/GPTCelltype
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit 16f565f
Showing
7 changed files
with
141 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
^CellTypeGPT\.Rproj$ | ||
^\.Rproj\.user$ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
.Rproj.user |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
# Generated by roxygen2: do not edit by hand | ||
|
||
export(CellTypeGPT) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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))) | ||
} | ||
|
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.