Mercurial > repos > goeckslab > celesta
comparison celesta_plot_expression.R @ 0:8001319743c0 draft
planemo upload for repository https://github.com/goeckslab/tools-mti/tree/main/tools/celesta commit 0ec46718dfd00f37ccae4e2fa133fa8393fe6d92
| author | goeckslab |
|---|---|
| date | Wed, 28 Aug 2024 12:46:48 +0000 |
| parents | |
| children |
comparison
equal
deleted
inserted
replaced
| -1:000000000000 | 0:8001319743c0 |
|---|---|
| 1 # -------------------------------------------------------------------------------------------- | |
| 2 # Plot marker expression probabilities for cell assignment parameter optimization with CELESTA | |
| 3 # -------------------------------------------------------------------------------------------- | |
| 4 | |
| 5 suppressWarnings(suppressMessages(library(janitor))) | |
| 6 suppressWarnings(suppressMessages(library(optparse))) | |
| 7 suppressWarnings(suppressMessages(library(dplyr))) | |
| 8 suppressWarnings(suppressMessages(library(anndataR))) | |
| 9 suppressWarnings(suppressMessages(library(Rmixmod))) | |
| 10 suppressWarnings(suppressMessages(library(spdep))) | |
| 11 suppressWarnings(suppressMessages(library(ggplot2))) | |
| 12 suppressWarnings(suppressMessages(library(reshape2))) | |
| 13 suppressWarnings(suppressMessages(library(zeallot))) | |
| 14 suppressWarnings(suppressMessages(library(CELESTA))) | |
| 15 | |
| 16 ### Define command line arguments | |
| 17 option_list <- list( | |
| 18 make_option(c("-i", "--imagingdata"), action = "store", default = NA, type = "character", | |
| 19 help = "Path to imaging data"), | |
| 20 make_option(c("-p", "--prior"), action = "store", default = NA, type = "character", | |
| 21 help = "Path to prior marker info file"), | |
| 22 make_option(c("-x", "--xcol"), action = "store", default = NA, type = "character", | |
| 23 help = "Name of column in adata.obs containing X coordinate"), | |
| 24 make_option(c("-y", "--ycol"), action = "store", default = NA, type = "character", | |
| 25 help = "Name of column in adata.obs containing Y coordinate"), | |
| 26 make_option(c("--filter"), action = "store_true", type = "logical", default = FALSE, | |
| 27 help = "Boolean to filter cells or not (default: no filtering)"), | |
| 28 make_option(c("--highfilter"), action = "store", default = 0.9, type = "double", | |
| 29 help = "High marker threshold if filtering cells (default: 0.9)"), | |
| 30 make_option(c("--lowfilter"), action = "store", default = 0.4, type = "double", | |
| 31 help = "Low marker threshold if filtering cells (default: 0.4)"), | |
| 32 make_option(c("-s", "--size"), action = "store", default = 1, type = "double", | |
| 33 help = "Point size for plotting"), | |
| 34 make_option(c("--width"), action = "store", default = 5, type = "integer", | |
| 35 help = "Width of plot (inches)"), | |
| 36 make_option(c("--height"), action = "store", default = 4, type = "integer", | |
| 37 help = "Height of plot (inches)") | |
| 38 ) | |
| 39 | |
| 40 ### Functions | |
| 41 anndata_to_celesta <- function(input_adata, x_col, y_col) { | |
| 42 | |
| 43 #' Function to convert anndata object to dataframe readable by CELESTA | |
| 44 #' Coordinates columns in adata.obs are renamed to "X" and "Y", and placed at beginning of dataframe | |
| 45 #' Marker intensities are concatted columnwise to the X and Y coords. cols: X,Y,Marker_1,...Marker N | |
| 46 | |
| 47 # initialize output as dataframe from adata.obs | |
| 48 celesta_input_dataframe <- data.frame(input_adata$obs) | |
| 49 | |
| 50 # subset to X and Y coordinates from obs only | |
| 51 celesta_input_dataframe <- celesta_input_dataframe %>% | |
| 52 dplyr::select({{x_col}}, {{y_col}}) | |
| 53 | |
| 54 # rename X,Y column names to what CELESTA wants | |
| 55 colnames(celesta_input_dataframe) <- c("X", "Y") | |
| 56 | |
| 57 # merge X,Y coords with marker intensities from adata.X | |
| 58 x_df <- data.frame(input_adata$X) | |
| 59 colnames(x_df) <- input_adata$var_names | |
| 60 celesta_input_dataframe <- cbind(celesta_input_dataframe, x_df) | |
| 61 | |
| 62 return(celesta_input_dataframe) | |
| 63 } | |
| 64 | |
| 65 ### Main | |
| 66 # parse args | |
| 67 opt <- parse_args(OptionParser(option_list = option_list)) | |
| 68 | |
| 69 # read anndata, convert to celesta format | |
| 70 adata <- read_h5ad(opt$imagingdata) | |
| 71 celesta_input_df <- anndata_to_celesta(adata, x_col = opt$xcol, y_col = opt$ycol) | |
| 72 | |
| 73 # read prior marker info | |
| 74 prior <- read.csv(opt$prior, check.names = FALSE) | |
| 75 | |
| 76 # clean prior names, keeping a copy of originals for writing output | |
| 77 prior_original_names <- colnames(prior) | |
| 78 prior <- janitor::clean_names(prior, case = "all_caps") | |
| 79 | |
| 80 # clean input dataframe names | |
| 81 celesta_input_df <- janitor::clean_names(celesta_input_df, case = "all_caps") | |
| 82 | |
| 83 # instantiate celesta object | |
| 84 CelestaObj <- CreateCelestaObject( | |
| 85 project_title = "", | |
| 86 prior_marker_info = prior, | |
| 87 imaging_data_file = celesta_input_df | |
| 88 ) | |
| 89 | |
| 90 # if filtering is specified, filter out cells outside high and low thresholds | |
| 91 if (opt$filter) { | |
| 92 print("filtering cells based on expression") | |
| 93 CelestaObj <- FilterCells(CelestaObj, | |
| 94 high_marker_threshold = opt$highfilter, | |
| 95 low_marker_threshold = opt$lowfilter) | |
| 96 } else { | |
| 97 print("Proceeding to marker expression plotting without cell filtering") | |
| 98 } | |
| 99 | |
| 100 # create output directory if it does not already exist | |
| 101 dir.create("marker_exp_plots") | |
| 102 | |
| 103 # plot expression probability | |
| 104 PlotExpProb(coords = CelestaObj@coords, | |
| 105 marker_exp_prob = CelestaObj@marker_exp_prob, | |
| 106 prior_marker_info = CelestaObj@prior_info, | |
| 107 save_plot = TRUE, | |
| 108 output_dir = "./marker_exp_plots") |
