|
| 1 | +#' @title Annotates cell clusters in single-cell sequencing analysis. |
| 2 | + |
| 3 | +#' |
| 4 | + |
| 5 | +#' @description This package annotates cell clusters in single-cell sequencing analysis by using ERNIEBot or DashScope models. |
| 6 | + |
| 7 | +#' @param account list. Obtained from the website https://console.bce.baidu.com/qianfan/ais/console/applicationConsole/application?_=1720524456151. client_id is the API Key, and client_secret is the Secret Key. |
| 8 | +#' @param model character. Check from website https://console.bce.baidu.com/qianfan/ais/console/onlineService. the default value is "completions_pro". |
| 9 | +#' @param inputTable data.frame returned by FindAllMarkers. |
| 10 | +#' @param tissueName The tissue origin of single-cell sequencing data, such as "mouse brain". |
| 11 | +#' @param useGeneNumber numeric. The number of genes used for annotating cell types, with a default value of 10. |
| 12 | + |
| 13 | +#' @return character vector |
| 14 | + |
| 15 | +#' @export ERNIEBotCellType |
| 16 | +#' @name ERNIEBotCellType |
| 17 | + |
| 18 | +library(httr) |
| 19 | +library(jsonlite) |
| 20 | +getAccessToken_ERNIEBot = function(client_id, client_secret) { |
| 21 | + url = 'https://aip.baidubce.com/oauth/2.0/token' |
| 22 | + headers = c('Content-Type' = 'application/json') |
| 23 | + body = list(grant_type = 'client_credentials', |
| 24 | + client_id = client_id, |
| 25 | + client_secret = client_secret) |
| 26 | + response = POST(url, add_headers(headers), body = body) |
| 27 | + access_token = content(response)$access_token |
| 28 | + return(access_token) |
| 29 | +} |
| 30 | + |
| 31 | +ERNIEBotChat = function(access_token, contentText, model = "completions_pro") { |
| 32 | + url = paste0('https://aip.baidubce.com/rpc/2.0/ai_custom/v1/wenxinworkshop/chat/',model) |
| 33 | + full_url = paste0(url, "?access_token=", access_token) |
| 34 | + headers = c('Content-Type' = 'application/json') |
| 35 | + data = list( |
| 36 | + messages = list( |
| 37 | + list(role = "user", content = contentText) |
| 38 | + ), |
| 39 | + system = 'You are a tool used for annotating cell types in single-cell sequencing data, strictly executing annotation tasks according to the requirements!' |
| 40 | + ) |
| 41 | + json_data = toJSON(data, auto_unbox = TRUE) |
| 42 | + response = POST(full_url, add_headers(headers), body = json_data) |
| 43 | + response = content(response) |
| 44 | + prompt_tokens = response$usage$prompt_tokens |
| 45 | + completion_tokens = response$usage$completion_tokens |
| 46 | + total_tokens = response$usage$total_tokens |
| 47 | + tokenMessage = paste0('Tokens usage report: ', |
| 48 | + '\n\t-prompt tokens: ',prompt_tokens, |
| 49 | + '\n\t-completion tokens: ',completion_tokens, |
| 50 | + '\n\t-total tokens: ',total_tokens, '\n') |
| 51 | + cat(tokenMessage) |
| 52 | + return(response$result) |
| 53 | +} |
| 54 | + |
| 55 | +ERNIEBotCellType = function(account = list(client_id = NULL, client_secret = NULL), model = "completions_pro", inputTable, tissueName = NULL, useGeneNumber = 5) { |
| 56 | + if(is.null(account$client_id) || is.null(account$client_secret)) { |
| 57 | + print('client_id or client_secret is not available!') |
| 58 | + return() |
| 59 | + } |
| 60 | + access_token = getAccessToken_ERNIEBot(client_id = account$client_id, client_secret = account$client_secret) |
| 61 | + clusterNum = length(levels(factor(inputTable$cluster))) |
| 62 | + minCluster = as.numeric(levels(factor(inputTable$cluster))[1]) |
| 63 | + sectionSize = 30 |
| 64 | + sliceNum = ceiling(clusterNum/sectionSize) |
| 65 | + responseList = list() |
| 66 | + for (i in 1:sliceNum) { |
| 67 | + cat(paste0('----------section ',i,' start----------\n')) |
| 68 | + section = inputTable[inputTable$cluster<=sectionSize*i & inputTable$cluster>=minCluster+sectionSize*(i-1),] |
| 69 | + markedClusters = tapply(section$gene, list(section$cluster), function(i) paste0(i[1:useGeneNumber], collapse = ",")) |
| 70 | + message = paste0("Use the following markers to identify the cell types of ", tissueName, " tissue cells. Provide only the cell type names in English, formatted as number followed by a colon and the cell type name in English, with no additional description after the name and the end of the response.", |
| 71 | + "\n", paste0(names(markedClusters), ":", unlist(markedClusters), collapse = "\n")) |
| 72 | + response = ERNIEBotChat(access_token = access_token, content = message, model = model) |
| 73 | + responseList = append(responseList, response) |
| 74 | + } |
| 75 | + combinedRes = paste(unlist(responseList), collapse = "\n") |
| 76 | + clearedRes = gsub(pattern = "\n", replacement = "", x = combinedRes) |
| 77 | + clearedRes = strsplit(clearedRes, "\\d+:")[[1]] |
| 78 | + clearedRes = clearedRes[-1] |
| 79 | + cellTypes = setNames(clearedRes, levels(factor(inputTable$cluster))) |
| 80 | + return(cellTypes) |
| 81 | +} |
0 commit comments