Skip to content

Commit

Permalink
address lintr errors
Browse files Browse the repository at this point in the history
  • Loading branch information
CameronFRWatson committed Aug 27, 2024
1 parent cec7348 commit 31e93cd
Show file tree
Hide file tree
Showing 3 changed files with 153 additions and 153 deletions.
142 changes: 71 additions & 71 deletions tools/celesta/celesta_assign_cells.R
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# ---------------------------------------------------------------------------------
# The main algorithim for CELESTA cell type assignment
# The main algorithim for CELESTA cell type assignment
# ---------------------------------------------------------------------------------

suppressWarnings(suppressMessages(library(janitor)))
Expand All @@ -13,77 +13,77 @@ suppressWarnings(suppressMessages(library(reshape2)))
suppressWarnings(suppressMessages(library(zeallot)))
suppressWarnings(suppressMessages(library(CELESTA)))

### Define command line arguments
option_list = list(
make_option(c("-i", "--imagingdata"), action="store", default=NA, type='character',
help="Path to imaging data"),
make_option(c("-p", "--prior"), action="store", default=NA, type='character',
help="Path to prior marker info file"),
make_option(c("-x", "--xcol"), action="store", default=NA, type='character',
help="Name of column in adata.obs containing X coordinate"),
make_option(c("-y", "--ycol"), action="store", default=NA, type='character',
help="Name of column in adata.obs containing Y coordinate"),
make_option(c("--filter"), action="store_true", type='logical', default=FALSE,
### Define command line arguments
option_list <- list(
make_option(c("-i", "--imagingdata"), action = "store", default = NA, type = "character",
help = "Path to imaging data"),
make_option(c("-p", "--prior"), action = "store", default = NA, type = "character",
help = "Path to prior marker info file"),
make_option(c("-x", "--xcol"), action = "store", default = NA, type = "character",
help = "Name of column in adata.obs containing X coordinate"),
make_option(c("-y", "--ycol"), action = "store", default = NA, type = "character",
help = "Name of column in adata.obs containing Y coordinate"),
make_option(c("--filter"), action = "store_true", type = "logical", default = FALSE,
help="Boolean to filter cells or not (default: no filtering)"),
make_option(c("--highfilter"), action="store", default=0.9, type='double',
help="High marker threshold if filtering cells (default: 0.9)"),
make_option(c("--lowfilter"), action="store", default=0.4, type='double',
help="Low marker threshold if filtering cells (default: 0.4)"),
make_option(c("--maxiteration"), action="store", default=10, type='integer',
help="Maximum iterations allowed in the EM algorithm per round"),
make_option(c("--changethresh"), action="store", default=0.01, type='double',
help="Ending condition for the EM algorithm"),
make_option(c("--highexpthresh"), action="store", default='default_high_thresholds', type='character',
help="Path to file specifying high expression thresholds for anchor and index cells"),
make_option(c("--lowexpthresh"), action="store", default='default_low_thresholds', type='character',
help="Path to file specifying low expression thresholds for anchor and index cells")
make_option(c("--highfilter"), action = "store", default = 0.9, type = "double",
help = "High marker threshold if filtering cells (default: 0.9)"),
make_option(c("--lowfilter"), action = "store", default = 0.4, type = "double",
help = "Low marker threshold if filtering cells (default: 0.4)"),
make_option(c("--maxiteration"), action = "store", default = 10, type = "integer",
help = "Maximum iterations allowed in the EM algorithm per round"),
make_option(c("--changethresh"), action = "store", default = 0.01, type = "double",
help = "Ending condition for the EM algorithm"),
make_option(c("--highexpthresh"), action = "store", default = "default_high_thresholds", type = "character",
help = "Path to file specifying high expression thresholds for anchor and index cells"),
make_option(c("--lowexpthresh"), action = "store", default = "default_low_thresholds", type = "character",
help = "Path to file specifying low expression thresholds for anchor and index cells")
)

### Functions
anndata_to_celesta <- function(input_adata, x_col, y_col) {
### Functions
anndata_to_celesta <- function(input_adata, x_col, y_col) {

#' Function to convert anndata object to dataframe readable by CELESTA
#' Coordinates columns in adata.obs are renamed to 'X' and 'Y', and placed at beginning of dataframe
#' Coordinates columns in adata.obs are renamed to "X" and "Y", and placed at beginning of dataframe
#' Marker intensities are concatted columnwise to the X and Y coords. cols: X,Y,Marker_1,...Marker N

# initialize output as dataframe from adata.obs
celesta_input_dataframe <- data.frame(input_adata$obs)

# subset to X and Y coordinates from obs only
# subset to X and Y coordinates from obs only
celesta_input_dataframe <- celesta_input_dataframe %>%
dplyr::select({{x_col}},{{y_col}})
dplyr::select({{x_col}}, {{y_col}})

# rename X,Y column names to what CELESTA wants
colnames(celesta_input_dataframe) <- c('X','Y')
# rename X,Y column names to what CELESTA wants
colnames(celesta_input_dataframe) <- c("X", "Y")

# merge X,Y coords with marker intensities from adata.X
x_df <- data.frame(input_adata$X)
colnames(x_df) <- input_adata$var_names
celesta_input_dataframe <- cbind(celesta_input_dataframe,x_df)
celesta_input_dataframe <- cbind(celesta_input_dataframe, x_df)

return(celesta_input_dataframe)
}

### Main
# parse args
opt = parse_args(OptionParser(option_list=option_list))
### Main
# parse args
opt <- parse_args(OptionParser(option_list = option_list))

# read anndata, convert to celesta format
# read anndata, convert to celesta format
adata <- read_h5ad(opt$imagingdata)
celesta_input_df <- anndata_to_celesta(adata, x_col = opt$xcol, y_col = opt$ycol)

# read prior marker info
# read prior marker info
prior <- read.csv(opt$prior, check.names = FALSE)

# clean prior names, keeping a copy of originals for writing output
# clean prior names, keeping a copy of originals for writing output
prior_original_names <- colnames(prior)
prior <- janitor::clean_names(prior, case = "all_caps")

# clean input dataframe names, keeping a copy of originals for writing output
# clean input dataframe names, keeping a copy of originals for writing output
celesta_input_df_original_names <- colnames(celesta_input_df)
celesta_input_df <- janitor::clean_names(celesta_input_df, case = "all_caps")

# instantiate celesta object
# instantiate celesta object
CelestaObj <- CreateCelestaObject(
project_title = "",
prior_marker_info = prior,
Expand All @@ -101,58 +101,58 @@ if (opt$filter) {
print("Proceeding to cell type assignment without cell filtering")
}

# check for non-default expression threshold files
if (opt$highexpthresh != 'default_high_thresholds') {
# read high thresholds
# check for non-default expression threshold files
if (opt$highexpthresh != "default_high_thresholds") {
# read high thresholds
print("Using custom high expression thresholds")
high_expression_thresholds <- read.csv(opt$highexpthresh)
high_expression_threshold_anchor <- high_expression_thresholds$anchor
high_expression_threshold_index <- high_expression_thresholds$index
hi_exp_thresh_anchor <- high_expression_thresholds$anchor
hi_exp_thresh_index <- high_expression_thresholds$index
} else {
print("Using default high expression thresholds -- this may need adjustment")
high_expression_threshold_anchor <- rep(0.7,length = 50)
high_expression_threshold_index <- rep(0.5,length = 50)
hi_exp_thresh_anchor <- rep(0.7, length = 50)
hi_exp_thresh_index <- rep(0.5, length = 50)
}

if (opt$lowexpthresh != 'default_low_thresholds') {
# read low thresholds
if (opt$lowexpthresh != "default_low_thresholds") {
# read low thresholds
print("Using custom low expression thresholds")
low_expression_thresholds <- read.csv(opt$highexpthresh)
low_expression_threshold_anchor <- low_expression_thresholds$anchor
low_expression_threshold_index <- low_expression_thresholds$index
low_exp_thresh_anchor <- low_expression_thresholds$anchor
low_exp_thresh_index <- low_expression_thresholds$index
} else {
print("Using default low expression thresholds")
low_expression_threshold_anchor <- rep(0.9,length = 50)
low_expression_threshold_index <- rep(1,length = 50)
low_exp_thresh_anchor <- rep(0.9, length = 50)
low_exp_thresh_index <- rep(1, length = 50)
}

# run cell type assignment
# run cell type assignment
CelestaObj <- AssignCells(CelestaObj,
max_iteration = opt$maxiteration,
cell_change_threshold = opt$changethresh,
high_expression_threshold_anchor = high_expression_threshold_anchor,
low_expression_threshold_anchor = low_expression_threshold_anchor,
high_expression_threshold_index = high_expression_threshold_index,
low_expression_threshold_index = low_expression_threshold_index,
save_result = FALSE)

# save object as an RDS file for cell type plotting
# for the time being, this is not exposed to Galaxy
max_iteration = opt$maxiteration,
cell_change_threshold = opt$changethresh,
high_expression_threshold_anchor = hi_exp_thresh_anchor,
low_expression_threshold_anchor = low_exp_thresh_anchor,
high_expression_threshold_index = hi_exp_thresh_index,
low_expression_threshold_index = low_exp_thresh_index,
save_result = FALSE)

# save object as an RDS file for cell type plotting
# for the time being, this is not exposed to Galaxy
saveRDS(CelestaObj, file = "celestaobj.rds")

# rename celesta assignment columns so they're obvious in output anndata
# rename celesta assignment columns so they are obvious in output anndata
celesta_assignments <- CelestaObj@final_cell_type_assignment
celesta_assignments <- janitor::clean_names(celesta_assignments)
colnames(celesta_assignments) <- paste0("celesta_", colnames(celesta_assignments))

# merge celesta assignments into anndata object
adata$obs <- cbind(adata$obs,celesta_assignments)

# print cell type value_counts to standard output
print('----------------------------------------')
print('Final cell type counts')
# print cell type value_counts to standard output
print("----------------------------------------")
print("Final cell type counts")
print(adata$obs %>% dplyr::count(celesta_final_cell_type, sort = TRUE))
print('----------------------------------------')
print("----------------------------------------")

# write output anndata file
write_h5ad(adata, 'result.h5ad')
# write output anndata file
write_h5ad(adata, "result.h5ad")
82 changes: 41 additions & 41 deletions tools/celesta/celesta_plot_cells.R
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# ---------------------------------------------------------------------------------
# Plot assigned cell type combinations with CELESTA
# Plot assigned cell type combinations with CELESTA
# ---------------------------------------------------------------------------------

suppressWarnings(suppressMessages(library(janitor)))
Expand All @@ -13,64 +13,64 @@ suppressWarnings(suppressMessages(library(reshape2)))
suppressWarnings(suppressMessages(library(zeallot)))
suppressWarnings(suppressMessages(library(CELESTA)))

# define command line args
option_list = list(
make_option(c("-r", "--rds"), action="store", default='celestaobj.rds', type='character',
help="Path to CelestaObj RDS"),
make_option(c("-p", "--prior"), action="store", default=NA, type='character',
help="Path to prior marker info file"),
make_option(c("-c", "--celltypes"), action="store", default=NA, type='character',
help="Comma-separated list of cell types to plot"),
make_option(c("-s", "--size"), action="store", default=1, type='double',
help="Point size for plotting"),
make_option(c("--width"), action="store", default=12, type='integer',
help="Width of plot (inches)"),
make_option(c("--height"), action="store", default=12, type='integer',
help="Height of plot (inches)"),
make_option(c("--dpi"), action="store", default=300, type='integer',
help="DPI (dots per inch) of plot")
# define command line args
option_list <- list(
make_option(c("-r", "--rds"), action = "store", default = "celestaobj.rds", type = "character",
help = "Path to CelestaObj RDS"),
make_option(c("-p", "--prior"), action = "store", default = NA, type = "character",
help = "Path to prior marker info file"),
make_option(c("-c", "--celltypes"), action = "store", default = NA, type = "character",
help = "Comma-separated list of cell types to plot"),
make_option(c("-s", "--size"), action = "store", default = 1, type = "double",
help = "Point size for plotting"),
make_option(c("--width"), action = "store", default = 12, type = "integer",
help = "Width of plot (inches)"),
make_option(c("--height"), action = "store", default = 12, type = "integer",
help = "Height of plot (inches)"),
make_option(c("--dpi"), action = "store", default = 300, type = "integer",
help = "DPI (dots per inch) of plot")
)

# parse args
opt = parse_args(OptionParser(option_list=option_list))
# parse args
opt <- parse_args(OptionParser(option_list = option_list))

CelestaObj <- readRDS(opt$rds)
cell_types_to_plot <- strsplit(opt$celltypes, ",")[[1]]

# read prior marker info
prior <- read.csv(opt$prior,row.names = 1)
# read prior marker info
prior <- read.csv(opt$prior, row.names = 1)

# get indices of cell types to plot from the prior marker table
# get indices of cell types to plot from the prior marker table
cell_type_indices <- which(row.names(prior) %in% cell_types_to_plot)

print(cell_types_to_plot)
print(cell_type_indices)

print(row.names(prior))

# create output directory if it doesn't already exist
dir.create('cell_assign_plots')
# create output directory if it doesn"t already exist
dir.create("cell_assign_plots")

# create the cell type plot
# create the cell type plot
g <- PlotCellsAnyCombination(cell_type_assignment_to_plot=CelestaObj@final_cell_type_assignment[,(CelestaObj@total_rounds+1)],
coords = CelestaObj@coords,
prior_info = prior_marker_info,
cell_number_to_use=cell_type_indices,
test_size=1,
save_plot = FALSE)
coords = CelestaObj@coords,
prior_info = prior_marker_info,
cell_number_to_use=cell_type_indices,
test_size=1,
save_plot = FALSE)

# create a unique output name for the plot based on the input cell types
# create a unique output name for the plot based on the input cell types
cell_types_cleaned <- paste(make_clean_names(cell_types_to_plot), collapse = "")
output_name <- paste(c("plot_cells_",cell_types_cleaned,".png"), collapse = "")

# save to subdir
# FIXME: may want to expose plotting params to galaxy
# save to subdir
# FIXME: may want to expose plotting params to galaxy
ggsave(
path = 'cell_assign_plots',
filename = output_name,
plot = g,
width = opt$width,
height = opt$height,
units = "in",
dpi = opt$dpi
)
path = "cell_assign_plots",
filename = output_name,
plot = g,
width = opt$width,
height = opt$height,
units = "in",
dpi = opt$dpi
)
Loading

0 comments on commit 31e93cd

Please sign in to comment.