| |
| |
|
|
| |
| if (!require("jsonlite", quietly = TRUE)) { |
| we install.packages("jsonlite", repos = "http://cran.r-project.org") |
| } |
| library(jsonlite) |
|
|
| |
| |
| setwd("C:/Users/hjung/OneDrive/๋ฐํ ํ๋ฉด/์ฐํฉํ์ต/K-Melloddy-master") |
|
|
| |
| |
| Sys.setenv(PBPK_WORKING_DIR = "/workspace/bio_kmelloddy_all/pbpk_ft") |
| source("pbpk_ft/PhysioSIm_run_webdemo.R") |
|
|
| |
| 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") { |
| |
| |
| cat("==========================================\n") |
| cat("PBPK ์๋ฒ ์์\n") |
| cat("==========================================\n") |
| cat("ํฌํธ:", port, "\n") |
| cat("ํธ์คํธ:", host, "\n") |
| cat("์ํ: ํด๋ผ์ด์ธํธ ์ฐ๊ฒฐ ๋๊ธฐ ์ค...\n") |
| cat("==========================================\n") |
| |
| |
| 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 |
| ) |
| }, 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 |
| } |
| |
| else if (!is.null(data$data)) { |
| drug_data <- data$data |
| cat("ํด๋ผ์ด์ธํธ ์ฐ๊ฒฐ๋จ. ์ฝ๋ฌผ ๋ฐ์ดํฐ ์์ :", drug_data$Drug.Name, "\n") |
| cat("PBPK ์๋ฎฌ๋ ์ด์
์์...\n") |
| |
| |
| 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 = { |
| |
| 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") |
| } |
|
|
|
|