Mercurial > repos > goeckslab > celesta
annotate celesta_assign_cells.R @ 3:283636dbfba5 draft default tip
planemo upload for repository https://github.com/goeckslab/tools-mti/tree/main/tools/celesta commit 36453551b1045adf20925d4eb6b5816c64475728
author | goeckslab |
---|---|
date | Thu, 19 Sep 2024 17:17:51 +0000 |
parents | 8001319743c0 |
children |
rev | line source |
---|---|
0
8001319743c0
planemo upload for repository https://github.com/goeckslab/tools-mti/tree/main/tools/celesta commit 0ec46718dfd00f37ccae4e2fa133fa8393fe6d92
goeckslab
parents:
diff
changeset
|
1 # --------------------------------------------------------------------------------- |
8001319743c0
planemo upload for repository https://github.com/goeckslab/tools-mti/tree/main/tools/celesta commit 0ec46718dfd00f37ccae4e2fa133fa8393fe6d92
goeckslab
parents:
diff
changeset
|
2 # The main algorithim for CELESTA cell type assignment |
8001319743c0
planemo upload for repository https://github.com/goeckslab/tools-mti/tree/main/tools/celesta commit 0ec46718dfd00f37ccae4e2fa133fa8393fe6d92
goeckslab
parents:
diff
changeset
|
3 # --------------------------------------------------------------------------------- |
8001319743c0
planemo upload for repository https://github.com/goeckslab/tools-mti/tree/main/tools/celesta commit 0ec46718dfd00f37ccae4e2fa133fa8393fe6d92
goeckslab
parents:
diff
changeset
|
4 |
8001319743c0
planemo upload for repository https://github.com/goeckslab/tools-mti/tree/main/tools/celesta commit 0ec46718dfd00f37ccae4e2fa133fa8393fe6d92
goeckslab
parents:
diff
changeset
|
5 suppressWarnings(suppressMessages(library(janitor))) |
8001319743c0
planemo upload for repository https://github.com/goeckslab/tools-mti/tree/main/tools/celesta commit 0ec46718dfd00f37ccae4e2fa133fa8393fe6d92
goeckslab
parents:
diff
changeset
|
6 suppressWarnings(suppressMessages(library(optparse))) |
8001319743c0
planemo upload for repository https://github.com/goeckslab/tools-mti/tree/main/tools/celesta commit 0ec46718dfd00f37ccae4e2fa133fa8393fe6d92
goeckslab
parents:
diff
changeset
|
7 suppressWarnings(suppressMessages(library(dplyr))) |
8001319743c0
planemo upload for repository https://github.com/goeckslab/tools-mti/tree/main/tools/celesta commit 0ec46718dfd00f37ccae4e2fa133fa8393fe6d92
goeckslab
parents:
diff
changeset
|
8 suppressWarnings(suppressMessages(library(anndataR))) |
8001319743c0
planemo upload for repository https://github.com/goeckslab/tools-mti/tree/main/tools/celesta commit 0ec46718dfd00f37ccae4e2fa133fa8393fe6d92
goeckslab
parents:
diff
changeset
|
9 suppressWarnings(suppressMessages(library(Rmixmod))) |
8001319743c0
planemo upload for repository https://github.com/goeckslab/tools-mti/tree/main/tools/celesta commit 0ec46718dfd00f37ccae4e2fa133fa8393fe6d92
goeckslab
parents:
diff
changeset
|
10 suppressWarnings(suppressMessages(library(spdep))) |
8001319743c0
planemo upload for repository https://github.com/goeckslab/tools-mti/tree/main/tools/celesta commit 0ec46718dfd00f37ccae4e2fa133fa8393fe6d92
goeckslab
parents:
diff
changeset
|
11 suppressWarnings(suppressMessages(library(ggplot2))) |
8001319743c0
planemo upload for repository https://github.com/goeckslab/tools-mti/tree/main/tools/celesta commit 0ec46718dfd00f37ccae4e2fa133fa8393fe6d92
goeckslab
parents:
diff
changeset
|
12 suppressWarnings(suppressMessages(library(reshape2))) |
8001319743c0
planemo upload for repository https://github.com/goeckslab/tools-mti/tree/main/tools/celesta commit 0ec46718dfd00f37ccae4e2fa133fa8393fe6d92
goeckslab
parents:
diff
changeset
|
13 suppressWarnings(suppressMessages(library(zeallot))) |
8001319743c0
planemo upload for repository https://github.com/goeckslab/tools-mti/tree/main/tools/celesta commit 0ec46718dfd00f37ccae4e2fa133fa8393fe6d92
goeckslab
parents:
diff
changeset
|
14 suppressWarnings(suppressMessages(library(CELESTA))) |
8001319743c0
planemo upload for repository https://github.com/goeckslab/tools-mti/tree/main/tools/celesta commit 0ec46718dfd00f37ccae4e2fa133fa8393fe6d92
goeckslab
parents:
diff
changeset
|
15 |
8001319743c0
planemo upload for repository https://github.com/goeckslab/tools-mti/tree/main/tools/celesta commit 0ec46718dfd00f37ccae4e2fa133fa8393fe6d92
goeckslab
parents:
diff
changeset
|
16 ### Define command line arguments |
8001319743c0
planemo upload for repository https://github.com/goeckslab/tools-mti/tree/main/tools/celesta commit 0ec46718dfd00f37ccae4e2fa133fa8393fe6d92
goeckslab
parents:
diff
changeset
|
17 option_list <- list( |
8001319743c0
planemo upload for repository https://github.com/goeckslab/tools-mti/tree/main/tools/celesta commit 0ec46718dfd00f37ccae4e2fa133fa8393fe6d92
goeckslab
parents:
diff
changeset
|
18 make_option(c("-i", "--imagingdata"), action = "store", default = NA, type = "character", |
8001319743c0
planemo upload for repository https://github.com/goeckslab/tools-mti/tree/main/tools/celesta commit 0ec46718dfd00f37ccae4e2fa133fa8393fe6d92
goeckslab
parents:
diff
changeset
|
19 help = "Path to imaging data"), |
8001319743c0
planemo upload for repository https://github.com/goeckslab/tools-mti/tree/main/tools/celesta commit 0ec46718dfd00f37ccae4e2fa133fa8393fe6d92
goeckslab
parents:
diff
changeset
|
20 make_option(c("-p", "--prior"), action = "store", default = NA, type = "character", |
8001319743c0
planemo upload for repository https://github.com/goeckslab/tools-mti/tree/main/tools/celesta commit 0ec46718dfd00f37ccae4e2fa133fa8393fe6d92
goeckslab
parents:
diff
changeset
|
21 help = "Path to prior marker info file"), |
8001319743c0
planemo upload for repository https://github.com/goeckslab/tools-mti/tree/main/tools/celesta commit 0ec46718dfd00f37ccae4e2fa133fa8393fe6d92
goeckslab
parents:
diff
changeset
|
22 make_option(c("-x", "--xcol"), action = "store", default = NA, type = "character", |
8001319743c0
planemo upload for repository https://github.com/goeckslab/tools-mti/tree/main/tools/celesta commit 0ec46718dfd00f37ccae4e2fa133fa8393fe6d92
goeckslab
parents:
diff
changeset
|
23 help = "Name of column in adata.obs containing X coordinate"), |
8001319743c0
planemo upload for repository https://github.com/goeckslab/tools-mti/tree/main/tools/celesta commit 0ec46718dfd00f37ccae4e2fa133fa8393fe6d92
goeckslab
parents:
diff
changeset
|
24 make_option(c("-y", "--ycol"), action = "store", default = NA, type = "character", |
8001319743c0
planemo upload for repository https://github.com/goeckslab/tools-mti/tree/main/tools/celesta commit 0ec46718dfd00f37ccae4e2fa133fa8393fe6d92
goeckslab
parents:
diff
changeset
|
25 help = "Name of column in adata.obs containing Y coordinate"), |
8001319743c0
planemo upload for repository https://github.com/goeckslab/tools-mti/tree/main/tools/celesta commit 0ec46718dfd00f37ccae4e2fa133fa8393fe6d92
goeckslab
parents:
diff
changeset
|
26 make_option(c("--filter"), action = "store_true", type = "logical", default = FALSE, |
8001319743c0
planemo upload for repository https://github.com/goeckslab/tools-mti/tree/main/tools/celesta commit 0ec46718dfd00f37ccae4e2fa133fa8393fe6d92
goeckslab
parents:
diff
changeset
|
27 help = "Boolean to filter cells or not (default: no filtering)"), |
8001319743c0
planemo upload for repository https://github.com/goeckslab/tools-mti/tree/main/tools/celesta commit 0ec46718dfd00f37ccae4e2fa133fa8393fe6d92
goeckslab
parents:
diff
changeset
|
28 make_option(c("--highfilter"), action = "store", default = 0.9, type = "double", |
8001319743c0
planemo upload for repository https://github.com/goeckslab/tools-mti/tree/main/tools/celesta commit 0ec46718dfd00f37ccae4e2fa133fa8393fe6d92
goeckslab
parents:
diff
changeset
|
29 help = "High marker threshold if filtering cells (default: 0.9)"), |
8001319743c0
planemo upload for repository https://github.com/goeckslab/tools-mti/tree/main/tools/celesta commit 0ec46718dfd00f37ccae4e2fa133fa8393fe6d92
goeckslab
parents:
diff
changeset
|
30 make_option(c("--lowfilter"), action = "store", default = 0.4, type = "double", |
8001319743c0
planemo upload for repository https://github.com/goeckslab/tools-mti/tree/main/tools/celesta commit 0ec46718dfd00f37ccae4e2fa133fa8393fe6d92
goeckslab
parents:
diff
changeset
|
31 help = "Low marker threshold if filtering cells (default: 0.4)"), |
8001319743c0
planemo upload for repository https://github.com/goeckslab/tools-mti/tree/main/tools/celesta commit 0ec46718dfd00f37ccae4e2fa133fa8393fe6d92
goeckslab
parents:
diff
changeset
|
32 make_option(c("--maxiteration"), action = "store", default = 10, type = "integer", |
8001319743c0
planemo upload for repository https://github.com/goeckslab/tools-mti/tree/main/tools/celesta commit 0ec46718dfd00f37ccae4e2fa133fa8393fe6d92
goeckslab
parents:
diff
changeset
|
33 help = "Maximum iterations allowed in the EM algorithm per round"), |
8001319743c0
planemo upload for repository https://github.com/goeckslab/tools-mti/tree/main/tools/celesta commit 0ec46718dfd00f37ccae4e2fa133fa8393fe6d92
goeckslab
parents:
diff
changeset
|
34 make_option(c("--changethresh"), action = "store", default = 0.01, type = "double", |
8001319743c0
planemo upload for repository https://github.com/goeckslab/tools-mti/tree/main/tools/celesta commit 0ec46718dfd00f37ccae4e2fa133fa8393fe6d92
goeckslab
parents:
diff
changeset
|
35 help = "Ending condition for the EM algorithm"), |
8001319743c0
planemo upload for repository https://github.com/goeckslab/tools-mti/tree/main/tools/celesta commit 0ec46718dfd00f37ccae4e2fa133fa8393fe6d92
goeckslab
parents:
diff
changeset
|
36 make_option(c("--highexpthresh"), action = "store", default = "default_high_thresholds", type = "character", |
8001319743c0
planemo upload for repository https://github.com/goeckslab/tools-mti/tree/main/tools/celesta commit 0ec46718dfd00f37ccae4e2fa133fa8393fe6d92
goeckslab
parents:
diff
changeset
|
37 help = "Path to file specifying high expression thresholds for anchor and index cells"), |
8001319743c0
planemo upload for repository https://github.com/goeckslab/tools-mti/tree/main/tools/celesta commit 0ec46718dfd00f37ccae4e2fa133fa8393fe6d92
goeckslab
parents:
diff
changeset
|
38 make_option(c("--lowexpthresh"), action = "store", default = "default_low_thresholds", type = "character", |
8001319743c0
planemo upload for repository https://github.com/goeckslab/tools-mti/tree/main/tools/celesta commit 0ec46718dfd00f37ccae4e2fa133fa8393fe6d92
goeckslab
parents:
diff
changeset
|
39 help = "Path to file specifying low expression thresholds for anchor and index cells") |
8001319743c0
planemo upload for repository https://github.com/goeckslab/tools-mti/tree/main/tools/celesta commit 0ec46718dfd00f37ccae4e2fa133fa8393fe6d92
goeckslab
parents:
diff
changeset
|
40 ) |
8001319743c0
planemo upload for repository https://github.com/goeckslab/tools-mti/tree/main/tools/celesta commit 0ec46718dfd00f37ccae4e2fa133fa8393fe6d92
goeckslab
parents:
diff
changeset
|
41 |
8001319743c0
planemo upload for repository https://github.com/goeckslab/tools-mti/tree/main/tools/celesta commit 0ec46718dfd00f37ccae4e2fa133fa8393fe6d92
goeckslab
parents:
diff
changeset
|
42 ### Functions |
8001319743c0
planemo upload for repository https://github.com/goeckslab/tools-mti/tree/main/tools/celesta commit 0ec46718dfd00f37ccae4e2fa133fa8393fe6d92
goeckslab
parents:
diff
changeset
|
43 anndata_to_celesta <- function(input_adata, x_col, y_col) { |
8001319743c0
planemo upload for repository https://github.com/goeckslab/tools-mti/tree/main/tools/celesta commit 0ec46718dfd00f37ccae4e2fa133fa8393fe6d92
goeckslab
parents:
diff
changeset
|
44 |
8001319743c0
planemo upload for repository https://github.com/goeckslab/tools-mti/tree/main/tools/celesta commit 0ec46718dfd00f37ccae4e2fa133fa8393fe6d92
goeckslab
parents:
diff
changeset
|
45 #' Function to convert anndata object to dataframe readable by CELESTA |
8001319743c0
planemo upload for repository https://github.com/goeckslab/tools-mti/tree/main/tools/celesta commit 0ec46718dfd00f37ccae4e2fa133fa8393fe6d92
goeckslab
parents:
diff
changeset
|
46 #' Coordinates columns in adata.obs are renamed to "X" and "Y", and placed at beginning of dataframe |
8001319743c0
planemo upload for repository https://github.com/goeckslab/tools-mti/tree/main/tools/celesta commit 0ec46718dfd00f37ccae4e2fa133fa8393fe6d92
goeckslab
parents:
diff
changeset
|
47 #' Marker intensities are concatted columnwise to the X and Y coords. cols: X,Y,Marker_1,...Marker N |
8001319743c0
planemo upload for repository https://github.com/goeckslab/tools-mti/tree/main/tools/celesta commit 0ec46718dfd00f37ccae4e2fa133fa8393fe6d92
goeckslab
parents:
diff
changeset
|
48 |
8001319743c0
planemo upload for repository https://github.com/goeckslab/tools-mti/tree/main/tools/celesta commit 0ec46718dfd00f37ccae4e2fa133fa8393fe6d92
goeckslab
parents:
diff
changeset
|
49 # initialize output as dataframe from adata.obs |
8001319743c0
planemo upload for repository https://github.com/goeckslab/tools-mti/tree/main/tools/celesta commit 0ec46718dfd00f37ccae4e2fa133fa8393fe6d92
goeckslab
parents:
diff
changeset
|
50 celesta_input_dataframe <- data.frame(input_adata$obs) |
8001319743c0
planemo upload for repository https://github.com/goeckslab/tools-mti/tree/main/tools/celesta commit 0ec46718dfd00f37ccae4e2fa133fa8393fe6d92
goeckslab
parents:
diff
changeset
|
51 |
8001319743c0
planemo upload for repository https://github.com/goeckslab/tools-mti/tree/main/tools/celesta commit 0ec46718dfd00f37ccae4e2fa133fa8393fe6d92
goeckslab
parents:
diff
changeset
|
52 # subset to X and Y coordinates from obs only |
8001319743c0
planemo upload for repository https://github.com/goeckslab/tools-mti/tree/main/tools/celesta commit 0ec46718dfd00f37ccae4e2fa133fa8393fe6d92
goeckslab
parents:
diff
changeset
|
53 celesta_input_dataframe <- celesta_input_dataframe %>% |
8001319743c0
planemo upload for repository https://github.com/goeckslab/tools-mti/tree/main/tools/celesta commit 0ec46718dfd00f37ccae4e2fa133fa8393fe6d92
goeckslab
parents:
diff
changeset
|
54 dplyr::select({{x_col}}, {{y_col}}) |
8001319743c0
planemo upload for repository https://github.com/goeckslab/tools-mti/tree/main/tools/celesta commit 0ec46718dfd00f37ccae4e2fa133fa8393fe6d92
goeckslab
parents:
diff
changeset
|
55 |
8001319743c0
planemo upload for repository https://github.com/goeckslab/tools-mti/tree/main/tools/celesta commit 0ec46718dfd00f37ccae4e2fa133fa8393fe6d92
goeckslab
parents:
diff
changeset
|
56 # rename X,Y column names to what CELESTA wants |
8001319743c0
planemo upload for repository https://github.com/goeckslab/tools-mti/tree/main/tools/celesta commit 0ec46718dfd00f37ccae4e2fa133fa8393fe6d92
goeckslab
parents:
diff
changeset
|
57 colnames(celesta_input_dataframe) <- c("X", "Y") |
8001319743c0
planemo upload for repository https://github.com/goeckslab/tools-mti/tree/main/tools/celesta commit 0ec46718dfd00f37ccae4e2fa133fa8393fe6d92
goeckslab
parents:
diff
changeset
|
58 |
8001319743c0
planemo upload for repository https://github.com/goeckslab/tools-mti/tree/main/tools/celesta commit 0ec46718dfd00f37ccae4e2fa133fa8393fe6d92
goeckslab
parents:
diff
changeset
|
59 # merge X,Y coords with marker intensities from adata.X |
8001319743c0
planemo upload for repository https://github.com/goeckslab/tools-mti/tree/main/tools/celesta commit 0ec46718dfd00f37ccae4e2fa133fa8393fe6d92
goeckslab
parents:
diff
changeset
|
60 x_df <- data.frame(input_adata$X) |
8001319743c0
planemo upload for repository https://github.com/goeckslab/tools-mti/tree/main/tools/celesta commit 0ec46718dfd00f37ccae4e2fa133fa8393fe6d92
goeckslab
parents:
diff
changeset
|
61 colnames(x_df) <- input_adata$var_names |
8001319743c0
planemo upload for repository https://github.com/goeckslab/tools-mti/tree/main/tools/celesta commit 0ec46718dfd00f37ccae4e2fa133fa8393fe6d92
goeckslab
parents:
diff
changeset
|
62 celesta_input_dataframe <- cbind(celesta_input_dataframe, x_df) |
8001319743c0
planemo upload for repository https://github.com/goeckslab/tools-mti/tree/main/tools/celesta commit 0ec46718dfd00f37ccae4e2fa133fa8393fe6d92
goeckslab
parents:
diff
changeset
|
63 |
8001319743c0
planemo upload for repository https://github.com/goeckslab/tools-mti/tree/main/tools/celesta commit 0ec46718dfd00f37ccae4e2fa133fa8393fe6d92
goeckslab
parents:
diff
changeset
|
64 return(celesta_input_dataframe) |
8001319743c0
planemo upload for repository https://github.com/goeckslab/tools-mti/tree/main/tools/celesta commit 0ec46718dfd00f37ccae4e2fa133fa8393fe6d92
goeckslab
parents:
diff
changeset
|
65 } |
8001319743c0
planemo upload for repository https://github.com/goeckslab/tools-mti/tree/main/tools/celesta commit 0ec46718dfd00f37ccae4e2fa133fa8393fe6d92
goeckslab
parents:
diff
changeset
|
66 |
8001319743c0
planemo upload for repository https://github.com/goeckslab/tools-mti/tree/main/tools/celesta commit 0ec46718dfd00f37ccae4e2fa133fa8393fe6d92
goeckslab
parents:
diff
changeset
|
67 ### Main |
8001319743c0
planemo upload for repository https://github.com/goeckslab/tools-mti/tree/main/tools/celesta commit 0ec46718dfd00f37ccae4e2fa133fa8393fe6d92
goeckslab
parents:
diff
changeset
|
68 # parse args |
8001319743c0
planemo upload for repository https://github.com/goeckslab/tools-mti/tree/main/tools/celesta commit 0ec46718dfd00f37ccae4e2fa133fa8393fe6d92
goeckslab
parents:
diff
changeset
|
69 opt <- parse_args(OptionParser(option_list = option_list)) |
8001319743c0
planemo upload for repository https://github.com/goeckslab/tools-mti/tree/main/tools/celesta commit 0ec46718dfd00f37ccae4e2fa133fa8393fe6d92
goeckslab
parents:
diff
changeset
|
70 |
8001319743c0
planemo upload for repository https://github.com/goeckslab/tools-mti/tree/main/tools/celesta commit 0ec46718dfd00f37ccae4e2fa133fa8393fe6d92
goeckslab
parents:
diff
changeset
|
71 # read anndata, convert to celesta format |
8001319743c0
planemo upload for repository https://github.com/goeckslab/tools-mti/tree/main/tools/celesta commit 0ec46718dfd00f37ccae4e2fa133fa8393fe6d92
goeckslab
parents:
diff
changeset
|
72 adata <- read_h5ad(opt$imagingdata) |
8001319743c0
planemo upload for repository https://github.com/goeckslab/tools-mti/tree/main/tools/celesta commit 0ec46718dfd00f37ccae4e2fa133fa8393fe6d92
goeckslab
parents:
diff
changeset
|
73 celesta_input_df <- anndata_to_celesta(adata, x_col = opt$xcol, y_col = opt$ycol) |
8001319743c0
planemo upload for repository https://github.com/goeckslab/tools-mti/tree/main/tools/celesta commit 0ec46718dfd00f37ccae4e2fa133fa8393fe6d92
goeckslab
parents:
diff
changeset
|
74 |
8001319743c0
planemo upload for repository https://github.com/goeckslab/tools-mti/tree/main/tools/celesta commit 0ec46718dfd00f37ccae4e2fa133fa8393fe6d92
goeckslab
parents:
diff
changeset
|
75 # read prior marker info |
8001319743c0
planemo upload for repository https://github.com/goeckslab/tools-mti/tree/main/tools/celesta commit 0ec46718dfd00f37ccae4e2fa133fa8393fe6d92
goeckslab
parents:
diff
changeset
|
76 prior <- read.csv(opt$prior, check.names = FALSE) |
8001319743c0
planemo upload for repository https://github.com/goeckslab/tools-mti/tree/main/tools/celesta commit 0ec46718dfd00f37ccae4e2fa133fa8393fe6d92
goeckslab
parents:
diff
changeset
|
77 |
8001319743c0
planemo upload for repository https://github.com/goeckslab/tools-mti/tree/main/tools/celesta commit 0ec46718dfd00f37ccae4e2fa133fa8393fe6d92
goeckslab
parents:
diff
changeset
|
78 # clean prior names and input dataframe names |
8001319743c0
planemo upload for repository https://github.com/goeckslab/tools-mti/tree/main/tools/celesta commit 0ec46718dfd00f37ccae4e2fa133fa8393fe6d92
goeckslab
parents:
diff
changeset
|
79 prior <- janitor::clean_names(prior, case = "all_caps") |
8001319743c0
planemo upload for repository https://github.com/goeckslab/tools-mti/tree/main/tools/celesta commit 0ec46718dfd00f37ccae4e2fa133fa8393fe6d92
goeckslab
parents:
diff
changeset
|
80 celesta_input_df <- janitor::clean_names(celesta_input_df, case = "all_caps") |
8001319743c0
planemo upload for repository https://github.com/goeckslab/tools-mti/tree/main/tools/celesta commit 0ec46718dfd00f37ccae4e2fa133fa8393fe6d92
goeckslab
parents:
diff
changeset
|
81 |
8001319743c0
planemo upload for repository https://github.com/goeckslab/tools-mti/tree/main/tools/celesta commit 0ec46718dfd00f37ccae4e2fa133fa8393fe6d92
goeckslab
parents:
diff
changeset
|
82 # instantiate celesta object |
8001319743c0
planemo upload for repository https://github.com/goeckslab/tools-mti/tree/main/tools/celesta commit 0ec46718dfd00f37ccae4e2fa133fa8393fe6d92
goeckslab
parents:
diff
changeset
|
83 CelestaObj <- CreateCelestaObject( |
8001319743c0
planemo upload for repository https://github.com/goeckslab/tools-mti/tree/main/tools/celesta commit 0ec46718dfd00f37ccae4e2fa133fa8393fe6d92
goeckslab
parents:
diff
changeset
|
84 project_title = "", |
8001319743c0
planemo upload for repository https://github.com/goeckslab/tools-mti/tree/main/tools/celesta commit 0ec46718dfd00f37ccae4e2fa133fa8393fe6d92
goeckslab
parents:
diff
changeset
|
85 prior_marker_info = prior, |
8001319743c0
planemo upload for repository https://github.com/goeckslab/tools-mti/tree/main/tools/celesta commit 0ec46718dfd00f37ccae4e2fa133fa8393fe6d92
goeckslab
parents:
diff
changeset
|
86 imaging_data_file = celesta_input_df |
8001319743c0
planemo upload for repository https://github.com/goeckslab/tools-mti/tree/main/tools/celesta commit 0ec46718dfd00f37ccae4e2fa133fa8393fe6d92
goeckslab
parents:
diff
changeset
|
87 ) |
8001319743c0
planemo upload for repository https://github.com/goeckslab/tools-mti/tree/main/tools/celesta commit 0ec46718dfd00f37ccae4e2fa133fa8393fe6d92
goeckslab
parents:
diff
changeset
|
88 |
8001319743c0
planemo upload for repository https://github.com/goeckslab/tools-mti/tree/main/tools/celesta commit 0ec46718dfd00f37ccae4e2fa133fa8393fe6d92
goeckslab
parents:
diff
changeset
|
89 # if filtering is specified, filter out cells outside high and low thresholds |
8001319743c0
planemo upload for repository https://github.com/goeckslab/tools-mti/tree/main/tools/celesta commit 0ec46718dfd00f37ccae4e2fa133fa8393fe6d92
goeckslab
parents:
diff
changeset
|
90 if (opt$filter) { |
8001319743c0
planemo upload for repository https://github.com/goeckslab/tools-mti/tree/main/tools/celesta commit 0ec46718dfd00f37ccae4e2fa133fa8393fe6d92
goeckslab
parents:
diff
changeset
|
91 print("filtering cells based on expression") |
8001319743c0
planemo upload for repository https://github.com/goeckslab/tools-mti/tree/main/tools/celesta commit 0ec46718dfd00f37ccae4e2fa133fa8393fe6d92
goeckslab
parents:
diff
changeset
|
92 CelestaObj <- FilterCells(CelestaObj, |
8001319743c0
planemo upload for repository https://github.com/goeckslab/tools-mti/tree/main/tools/celesta commit 0ec46718dfd00f37ccae4e2fa133fa8393fe6d92
goeckslab
parents:
diff
changeset
|
93 high_marker_threshold = opt$highfilter, |
8001319743c0
planemo upload for repository https://github.com/goeckslab/tools-mti/tree/main/tools/celesta commit 0ec46718dfd00f37ccae4e2fa133fa8393fe6d92
goeckslab
parents:
diff
changeset
|
94 low_marker_threshold = opt$lowfilter) |
8001319743c0
planemo upload for repository https://github.com/goeckslab/tools-mti/tree/main/tools/celesta commit 0ec46718dfd00f37ccae4e2fa133fa8393fe6d92
goeckslab
parents:
diff
changeset
|
95 } else { |
8001319743c0
planemo upload for repository https://github.com/goeckslab/tools-mti/tree/main/tools/celesta commit 0ec46718dfd00f37ccae4e2fa133fa8393fe6d92
goeckslab
parents:
diff
changeset
|
96 print("Proceeding to cell type assignment without cell filtering") |
8001319743c0
planemo upload for repository https://github.com/goeckslab/tools-mti/tree/main/tools/celesta commit 0ec46718dfd00f37ccae4e2fa133fa8393fe6d92
goeckslab
parents:
diff
changeset
|
97 } |
8001319743c0
planemo upload for repository https://github.com/goeckslab/tools-mti/tree/main/tools/celesta commit 0ec46718dfd00f37ccae4e2fa133fa8393fe6d92
goeckslab
parents:
diff
changeset
|
98 |
8001319743c0
planemo upload for repository https://github.com/goeckslab/tools-mti/tree/main/tools/celesta commit 0ec46718dfd00f37ccae4e2fa133fa8393fe6d92
goeckslab
parents:
diff
changeset
|
99 # check for non-default expression threshold files |
8001319743c0
planemo upload for repository https://github.com/goeckslab/tools-mti/tree/main/tools/celesta commit 0ec46718dfd00f37ccae4e2fa133fa8393fe6d92
goeckslab
parents:
diff
changeset
|
100 if (opt$highexpthresh != "default_high_thresholds") { |
8001319743c0
planemo upload for repository https://github.com/goeckslab/tools-mti/tree/main/tools/celesta commit 0ec46718dfd00f37ccae4e2fa133fa8393fe6d92
goeckslab
parents:
diff
changeset
|
101 # read high thresholds |
8001319743c0
planemo upload for repository https://github.com/goeckslab/tools-mti/tree/main/tools/celesta commit 0ec46718dfd00f37ccae4e2fa133fa8393fe6d92
goeckslab
parents:
diff
changeset
|
102 print("Using custom high expression thresholds") |
8001319743c0
planemo upload for repository https://github.com/goeckslab/tools-mti/tree/main/tools/celesta commit 0ec46718dfd00f37ccae4e2fa133fa8393fe6d92
goeckslab
parents:
diff
changeset
|
103 high_expression_thresholds <- read.csv(opt$highexpthresh) |
8001319743c0
planemo upload for repository https://github.com/goeckslab/tools-mti/tree/main/tools/celesta commit 0ec46718dfd00f37ccae4e2fa133fa8393fe6d92
goeckslab
parents:
diff
changeset
|
104 hi_exp_thresh_anchor <- high_expression_thresholds$anchor |
8001319743c0
planemo upload for repository https://github.com/goeckslab/tools-mti/tree/main/tools/celesta commit 0ec46718dfd00f37ccae4e2fa133fa8393fe6d92
goeckslab
parents:
diff
changeset
|
105 hi_exp_thresh_index <- high_expression_thresholds$index |
8001319743c0
planemo upload for repository https://github.com/goeckslab/tools-mti/tree/main/tools/celesta commit 0ec46718dfd00f37ccae4e2fa133fa8393fe6d92
goeckslab
parents:
diff
changeset
|
106 } else { |
8001319743c0
planemo upload for repository https://github.com/goeckslab/tools-mti/tree/main/tools/celesta commit 0ec46718dfd00f37ccae4e2fa133fa8393fe6d92
goeckslab
parents:
diff
changeset
|
107 print("Using default high expression thresholds -- this may need adjustment") |
8001319743c0
planemo upload for repository https://github.com/goeckslab/tools-mti/tree/main/tools/celesta commit 0ec46718dfd00f37ccae4e2fa133fa8393fe6d92
goeckslab
parents:
diff
changeset
|
108 hi_exp_thresh_anchor <- rep(0.7, length = 50) |
8001319743c0
planemo upload for repository https://github.com/goeckslab/tools-mti/tree/main/tools/celesta commit 0ec46718dfd00f37ccae4e2fa133fa8393fe6d92
goeckslab
parents:
diff
changeset
|
109 hi_exp_thresh_index <- rep(0.5, length = 50) |
8001319743c0
planemo upload for repository https://github.com/goeckslab/tools-mti/tree/main/tools/celesta commit 0ec46718dfd00f37ccae4e2fa133fa8393fe6d92
goeckslab
parents:
diff
changeset
|
110 } |
8001319743c0
planemo upload for repository https://github.com/goeckslab/tools-mti/tree/main/tools/celesta commit 0ec46718dfd00f37ccae4e2fa133fa8393fe6d92
goeckslab
parents:
diff
changeset
|
111 |
8001319743c0
planemo upload for repository https://github.com/goeckslab/tools-mti/tree/main/tools/celesta commit 0ec46718dfd00f37ccae4e2fa133fa8393fe6d92
goeckslab
parents:
diff
changeset
|
112 if (opt$lowexpthresh != "default_low_thresholds") { |
8001319743c0
planemo upload for repository https://github.com/goeckslab/tools-mti/tree/main/tools/celesta commit 0ec46718dfd00f37ccae4e2fa133fa8393fe6d92
goeckslab
parents:
diff
changeset
|
113 # read low thresholds |
8001319743c0
planemo upload for repository https://github.com/goeckslab/tools-mti/tree/main/tools/celesta commit 0ec46718dfd00f37ccae4e2fa133fa8393fe6d92
goeckslab
parents:
diff
changeset
|
114 print("Using custom low expression thresholds") |
8001319743c0
planemo upload for repository https://github.com/goeckslab/tools-mti/tree/main/tools/celesta commit 0ec46718dfd00f37ccae4e2fa133fa8393fe6d92
goeckslab
parents:
diff
changeset
|
115 low_expression_thresholds <- read.csv(opt$highexpthresh) |
8001319743c0
planemo upload for repository https://github.com/goeckslab/tools-mti/tree/main/tools/celesta commit 0ec46718dfd00f37ccae4e2fa133fa8393fe6d92
goeckslab
parents:
diff
changeset
|
116 low_exp_thresh_anchor <- low_expression_thresholds$anchor |
8001319743c0
planemo upload for repository https://github.com/goeckslab/tools-mti/tree/main/tools/celesta commit 0ec46718dfd00f37ccae4e2fa133fa8393fe6d92
goeckslab
parents:
diff
changeset
|
117 low_exp_thresh_index <- low_expression_thresholds$index |
8001319743c0
planemo upload for repository https://github.com/goeckslab/tools-mti/tree/main/tools/celesta commit 0ec46718dfd00f37ccae4e2fa133fa8393fe6d92
goeckslab
parents:
diff
changeset
|
118 } else { |
8001319743c0
planemo upload for repository https://github.com/goeckslab/tools-mti/tree/main/tools/celesta commit 0ec46718dfd00f37ccae4e2fa133fa8393fe6d92
goeckslab
parents:
diff
changeset
|
119 print("Using default low expression thresholds") |
8001319743c0
planemo upload for repository https://github.com/goeckslab/tools-mti/tree/main/tools/celesta commit 0ec46718dfd00f37ccae4e2fa133fa8393fe6d92
goeckslab
parents:
diff
changeset
|
120 low_exp_thresh_anchor <- rep(0.9, length = 50) |
8001319743c0
planemo upload for repository https://github.com/goeckslab/tools-mti/tree/main/tools/celesta commit 0ec46718dfd00f37ccae4e2fa133fa8393fe6d92
goeckslab
parents:
diff
changeset
|
121 low_exp_thresh_index <- rep(1, length = 50) |
8001319743c0
planemo upload for repository https://github.com/goeckslab/tools-mti/tree/main/tools/celesta commit 0ec46718dfd00f37ccae4e2fa133fa8393fe6d92
goeckslab
parents:
diff
changeset
|
122 } |
8001319743c0
planemo upload for repository https://github.com/goeckslab/tools-mti/tree/main/tools/celesta commit 0ec46718dfd00f37ccae4e2fa133fa8393fe6d92
goeckslab
parents:
diff
changeset
|
123 |
8001319743c0
planemo upload for repository https://github.com/goeckslab/tools-mti/tree/main/tools/celesta commit 0ec46718dfd00f37ccae4e2fa133fa8393fe6d92
goeckslab
parents:
diff
changeset
|
124 # run cell type assignment |
8001319743c0
planemo upload for repository https://github.com/goeckslab/tools-mti/tree/main/tools/celesta commit 0ec46718dfd00f37ccae4e2fa133fa8393fe6d92
goeckslab
parents:
diff
changeset
|
125 CelestaObj <- AssignCells(CelestaObj, |
8001319743c0
planemo upload for repository https://github.com/goeckslab/tools-mti/tree/main/tools/celesta commit 0ec46718dfd00f37ccae4e2fa133fa8393fe6d92
goeckslab
parents:
diff
changeset
|
126 max_iteration = opt$maxiteration, |
8001319743c0
planemo upload for repository https://github.com/goeckslab/tools-mti/tree/main/tools/celesta commit 0ec46718dfd00f37ccae4e2fa133fa8393fe6d92
goeckslab
parents:
diff
changeset
|
127 cell_change_threshold = opt$changethresh, |
8001319743c0
planemo upload for repository https://github.com/goeckslab/tools-mti/tree/main/tools/celesta commit 0ec46718dfd00f37ccae4e2fa133fa8393fe6d92
goeckslab
parents:
diff
changeset
|
128 high_expression_threshold_anchor = hi_exp_thresh_anchor, |
8001319743c0
planemo upload for repository https://github.com/goeckslab/tools-mti/tree/main/tools/celesta commit 0ec46718dfd00f37ccae4e2fa133fa8393fe6d92
goeckslab
parents:
diff
changeset
|
129 low_expression_threshold_anchor = low_exp_thresh_anchor, |
8001319743c0
planemo upload for repository https://github.com/goeckslab/tools-mti/tree/main/tools/celesta commit 0ec46718dfd00f37ccae4e2fa133fa8393fe6d92
goeckslab
parents:
diff
changeset
|
130 high_expression_threshold_index = hi_exp_thresh_index, |
8001319743c0
planemo upload for repository https://github.com/goeckslab/tools-mti/tree/main/tools/celesta commit 0ec46718dfd00f37ccae4e2fa133fa8393fe6d92
goeckslab
parents:
diff
changeset
|
131 low_expression_threshold_index = low_exp_thresh_index, |
8001319743c0
planemo upload for repository https://github.com/goeckslab/tools-mti/tree/main/tools/celesta commit 0ec46718dfd00f37ccae4e2fa133fa8393fe6d92
goeckslab
parents:
diff
changeset
|
132 save_result = FALSE) |
8001319743c0
planemo upload for repository https://github.com/goeckslab/tools-mti/tree/main/tools/celesta commit 0ec46718dfd00f37ccae4e2fa133fa8393fe6d92
goeckslab
parents:
diff
changeset
|
133 |
8001319743c0
planemo upload for repository https://github.com/goeckslab/tools-mti/tree/main/tools/celesta commit 0ec46718dfd00f37ccae4e2fa133fa8393fe6d92
goeckslab
parents:
diff
changeset
|
134 # save object as an RDS file for cell type plotting |
8001319743c0
planemo upload for repository https://github.com/goeckslab/tools-mti/tree/main/tools/celesta commit 0ec46718dfd00f37ccae4e2fa133fa8393fe6d92
goeckslab
parents:
diff
changeset
|
135 # for the time being, this is not exposed to Galaxy |
8001319743c0
planemo upload for repository https://github.com/goeckslab/tools-mti/tree/main/tools/celesta commit 0ec46718dfd00f37ccae4e2fa133fa8393fe6d92
goeckslab
parents:
diff
changeset
|
136 saveRDS(CelestaObj, file = "celestaobj.rds") |
8001319743c0
planemo upload for repository https://github.com/goeckslab/tools-mti/tree/main/tools/celesta commit 0ec46718dfd00f37ccae4e2fa133fa8393fe6d92
goeckslab
parents:
diff
changeset
|
137 |
8001319743c0
planemo upload for repository https://github.com/goeckslab/tools-mti/tree/main/tools/celesta commit 0ec46718dfd00f37ccae4e2fa133fa8393fe6d92
goeckslab
parents:
diff
changeset
|
138 # rename celesta assignment columns so they are obvious in output anndata |
8001319743c0
planemo upload for repository https://github.com/goeckslab/tools-mti/tree/main/tools/celesta commit 0ec46718dfd00f37ccae4e2fa133fa8393fe6d92
goeckslab
parents:
diff
changeset
|
139 celesta_assignments <- CelestaObj@final_cell_type_assignment |
8001319743c0
planemo upload for repository https://github.com/goeckslab/tools-mti/tree/main/tools/celesta commit 0ec46718dfd00f37ccae4e2fa133fa8393fe6d92
goeckslab
parents:
diff
changeset
|
140 celesta_assignments <- janitor::clean_names(celesta_assignments) |
8001319743c0
planemo upload for repository https://github.com/goeckslab/tools-mti/tree/main/tools/celesta commit 0ec46718dfd00f37ccae4e2fa133fa8393fe6d92
goeckslab
parents:
diff
changeset
|
141 colnames(celesta_assignments) <- paste0("celesta_", colnames(celesta_assignments)) |
8001319743c0
planemo upload for repository https://github.com/goeckslab/tools-mti/tree/main/tools/celesta commit 0ec46718dfd00f37ccae4e2fa133fa8393fe6d92
goeckslab
parents:
diff
changeset
|
142 |
8001319743c0
planemo upload for repository https://github.com/goeckslab/tools-mti/tree/main/tools/celesta commit 0ec46718dfd00f37ccae4e2fa133fa8393fe6d92
goeckslab
parents:
diff
changeset
|
143 # merge celesta assignments into anndata object |
8001319743c0
planemo upload for repository https://github.com/goeckslab/tools-mti/tree/main/tools/celesta commit 0ec46718dfd00f37ccae4e2fa133fa8393fe6d92
goeckslab
parents:
diff
changeset
|
144 adata$obs <- cbind(adata$obs, celesta_assignments) |
8001319743c0
planemo upload for repository https://github.com/goeckslab/tools-mti/tree/main/tools/celesta commit 0ec46718dfd00f37ccae4e2fa133fa8393fe6d92
goeckslab
parents:
diff
changeset
|
145 |
8001319743c0
planemo upload for repository https://github.com/goeckslab/tools-mti/tree/main/tools/celesta commit 0ec46718dfd00f37ccae4e2fa133fa8393fe6d92
goeckslab
parents:
diff
changeset
|
146 # print cell type value_counts to standard output |
8001319743c0
planemo upload for repository https://github.com/goeckslab/tools-mti/tree/main/tools/celesta commit 0ec46718dfd00f37ccae4e2fa133fa8393fe6d92
goeckslab
parents:
diff
changeset
|
147 print("----------------------------------------") |
8001319743c0
planemo upload for repository https://github.com/goeckslab/tools-mti/tree/main/tools/celesta commit 0ec46718dfd00f37ccae4e2fa133fa8393fe6d92
goeckslab
parents:
diff
changeset
|
148 print("Final cell type counts") |
8001319743c0
planemo upload for repository https://github.com/goeckslab/tools-mti/tree/main/tools/celesta commit 0ec46718dfd00f37ccae4e2fa133fa8393fe6d92
goeckslab
parents:
diff
changeset
|
149 print(adata$obs %>% dplyr::count(celesta_final_cell_type, sort = TRUE)) |
8001319743c0
planemo upload for repository https://github.com/goeckslab/tools-mti/tree/main/tools/celesta commit 0ec46718dfd00f37ccae4e2fa133fa8393fe6d92
goeckslab
parents:
diff
changeset
|
150 print("----------------------------------------") |
8001319743c0
planemo upload for repository https://github.com/goeckslab/tools-mti/tree/main/tools/celesta commit 0ec46718dfd00f37ccae4e2fa133fa8393fe6d92
goeckslab
parents:
diff
changeset
|
151 |
8001319743c0
planemo upload for repository https://github.com/goeckslab/tools-mti/tree/main/tools/celesta commit 0ec46718dfd00f37ccae4e2fa133fa8393fe6d92
goeckslab
parents:
diff
changeset
|
152 # write output anndata file |
8001319743c0
planemo upload for repository https://github.com/goeckslab/tools-mti/tree/main/tools/celesta commit 0ec46718dfd00f37ccae4e2fa133fa8393fe6d92
goeckslab
parents:
diff
changeset
|
153 write_h5ad(adata, "result.h5ad") |