PharmAI / pbpk_engine /run_server.R.save
Hattie's picture
Initial PharmAI deployment
9316888
Raw
History Blame Contribute Delete
5.1 kB
# R Socket ์„œ๋ฒ„ - PBPK ์‹œ๋ฎฌ๋ ˆ์ด์…˜
# Python์œผ๋กœ๋ถ€ํ„ฐ ์•ฝ๋ฌผ ๋ฐ์ดํ„ฐ๋ฅผ ๋ฐ›์•„ PBPK ์‹œ๋ฎฌ๋ ˆ์ด์…˜ ์ˆ˜ํ–‰
# ํ•„์š”ํ•œ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ
if (!require("jsonlite", quietly = TRUE)) {
we install.packages("jsonlite", repos = "http://cran.r-project.org")
}
library(jsonlite)
# ์ž‘์—… ๋””๋ ‰ํ† ๋ฆฌ ์„ค์ •
#setwd("/workspace/bio_kmelloddy_all")
setwd("C:/Users/hjung/OneDrive/๋ฐ”ํƒ• ํ™”๋ฉด/์—ฐํ•ฉํ•™์Šต/K-Melloddy-master")
# PhysioSIm_run_webdemo.R์˜ run_pbpk_simulation ํ•จ์ˆ˜ ์‚ฌ์šฉ
# ํŒŒ์ผ ๋‚ด๋ถ€์˜ setwd() ํ˜ธ์ถœ์„ ๋ฌด์‹œํ•˜๊ธฐ ์œ„ํ•ด ํ™˜๊ฒฝ ๋ณ€์ˆ˜ ์„ค์ •
Sys.setenv(PBPK_WORKING_DIR = "/workspace/bio_kmelloddy_all/pbpk_ft")
source("pbpk_ft/PhysioSIm_run_webdemo.R")
# ์ „์—ญ ๋ณ€์ˆ˜ ์ดˆ๊ธฐํ™” (getPBPKParam, NCA_result_PBPK ๋“ฑ์˜ ํ•จ์ˆ˜๊ฐ€ ์ฐธ์กฐ)
cat("์ „์—ญ ๋ณ€์ˆ˜ ์ดˆ๊ธฐํ™” ์ค‘...\n")
species_select <- "Human"
param_organs <- select_param_organs(species_select)
param_ACAT <- select_param_ACAT(species_select)
param_general <- select_param_general(species_select)
param_diss <- select_param_diss(species_select)
cat("์ „์—ญ ๋ณ€์ˆ˜ ์ดˆ๊ธฐํ™” ์™„๋ฃŒ.\n\n")
start_pbpk_server <- function(host = "127.0.0.1", port = 7000, pid_file = ".pbpk_server.pid") {
#' PBPK ์‹œ๋ฎฌ๋ ˆ์ด์…˜ ์„œ๋ฒ„ ์‹œ์ž‘
cat("==========================================\n")
cat("PBPK ์„œ๋ฒ„ ์‹œ์ž‘\n")
cat("==========================================\n")
cat("ํฌํŠธ:", port, "\n")
cat("ํ˜ธ์ŠคํŠธ:", host, "\n")
cat("์ƒํƒœ: ํด๋ผ์ด์–ธํŠธ ์—ฐ๊ฒฐ ๋Œ€๊ธฐ ์ค‘...\n")
cat("==========================================\n")
# PID ํŒŒ์ผ ์ €์žฅ
pid <- Sys.getpid()
writeLines(as.character(pid), pid_file)
# ์„œ๋ฒ„ ์ข…๋ฃŒ ํ”Œ๋ž˜๊ทธ
should_shutdown <- FALSE
tryCatch({
# ๊ณ„์†ํ•ด์„œ ์ƒˆ๋กœ์šด ์—ฐ๊ฒฐ ๋Œ€๊ธฐ
while (!should_shutdown) {
# ๊ฐ ์š”์ฒญ๋งˆ๋‹ค ์ƒˆ๋กœ์šด ์†Œ์ผ“ ์—ฐ๊ฒฐ ์ƒ์„ฑ
server_socket <- tryCatch({
socketConnection(
host = host,
port = port,
server = TRUE,
blocking = TRUE,
open = "r+b",
timeout = 600 # PBPK ์‹œ๋ฎฌ๋ ˆ์ด์…˜์€ ์‹œ๊ฐ„์ด ๊ฑธ๋ฆด ์ˆ˜ ์žˆ์Œ
)
}, error = function(e) {
NULL
})
if (is.null(server_socket)) {
Sys.sleep(1)
next
}
# ํด๋ผ์ด์–ธํŠธ ์š”์ฒญ ์ฒ˜๋ฆฌ
tryCatch({
raw_data <- readLines(server_socket, n = 1, warn = FALSE)
if (length(raw_data) > 0) {
data <- fromJSON(raw_data)
# ์ข…๋ฃŒ ๋ช…๋ น ํ™•์ธ
if (!is.null(data$command) && data$command == "shutdown") {
response <- list(status = "success", message = "PBPK server shutting down")
writeLines(toJSON(response, auto_unbox = TRUE), server_socket)
flush(server_socket)
should_shutdown <- TRUE
}
# PBPK ์‹œ๋ฎฌ๋ ˆ์ด์…˜ ์š”์ฒญ ์ฒ˜๋ฆฌ
else if (!is.null(data$data)) {
drug_data <- data$data
cat("ํด๋ผ์ด์–ธํŠธ ์—ฐ๊ฒฐ๋จ. ์•ฝ๋ฌผ ๋ฐ์ดํ„ฐ ์ˆ˜์‹ :", drug_data$Drug.Name, "\n")
cat("PBPK ์‹œ๋ฎฌ๋ ˆ์ด์…˜ ์‹œ์ž‘...\n")
# PBPK ์‹œ๋ฎฌ๋ ˆ์ด์…˜ ์‹คํ–‰
pbpk_result <- tryCatch({
run_pbpk_simulation(drug_data_input = drug_data)
}, error = function(e) {
cat("PBPK ์‹œ๋ฎฌ๋ ˆ์ด์…˜ ์˜ค๋ฅ˜:", e$message, "\n")
list(error = TRUE, message = paste("PBPK error:", e$message))
})
if (!is.null(pbpk_result) && is.null(pbpk_result$error)) {
# ์„ฑ๊ณต ์‘๋‹ต - ๊ฐ„๋‹จํ•˜๊ฒŒ
cat("PBPK ์‹œ๋ฎฌ๋ ˆ์ด์…˜ ์™„๋ฃŒ:", drug_data$Drug.Name, "\n")
response <- list(status = "success", message = "PBPK prediction completed")
} else {
# ์—๋Ÿฌ ์‘๋‹ต
cat("PBPK ์‹œ๋ฎฌ๋ ˆ์ด์…˜ ์‹คํŒจ:", drug_data$Drug.Name, "\n")
response <- list(
status = "error",
message = ifelse(!is.null(pbpk_result$message),
pbpk_result$message,
"PBPK simulation failed")
)
}
writeLines(toJSON(response, auto_unbox = TRUE), server_socket)
flush(server_socket)
}
else {
# ์ž˜๋ชป๋œ ์š”์ฒญ
response <- list(status = "error", message = "Invalid request format")
writeLines(toJSON(response, auto_unbox = TRUE), server_socket)
flush(server_socket)
}
}
}, error = function(e) {
# ๊ฐœ๋ณ„ ์š”์ฒญ ์—๋Ÿฌ๋Š” ๋ฌด์‹œํ•˜๊ณ  ๊ณ„์† ์ง„ํ–‰
})
# ์—ฐ๊ฒฐ ์ข…๋ฃŒ
close(server_socket)
}
}, error = function(e) {
# ์ „์ฒด ์„œ๋ฒ„ ์—๋Ÿฌ
}, finally = {
# PID ํŒŒ์ผ ์‚ญ์ œ
if (file.exists(pid_file)) {
file.remove(pid_file)
}
})
}
# ์„œ๋ฒ„ ์‹œ์ž‘
if (!interactive()) {
cat("PBPK Simulation Server Starting...\n")
cat("Port: 7000\n")
cat("Working Directory:", getwd(), "\n\n")
start_pbpk_server(host = "127.0.0.1", port = 7000, pid_file = "pbpk_ft/.pbpk_server.pid")
}