Skip to content

Commit

Permalink
add back option for new output directory
Browse files Browse the repository at this point in the history
  • Loading branch information
ngreenwald committed Jul 5, 2022
1 parent ad6ccfc commit 942d067
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 7 deletions.
10 changes: 7 additions & 3 deletions ark/phenotyping/pixel_consensus_cluster.R
Original file line number Diff line number Diff line change
Expand Up @@ -19,17 +19,19 @@ library(foreach)
library(parallel)

# helper function to map a FOV to its consensus labels
mapConsensusLabels <- function(fov, pixelMatDir, som_to_meta_map) {
mapConsensusLabels <- function(fov, pixelMatDir, som_to_meta_map, pixelOutDir) {
# read in pixel data, we'll need the cluster column for mapping
fileName <- file.path(fov, "feather", fsep=".")
matPath <- file.path(pixelMatDir, fileName)
matOutPath <- file.path(pixelOutDir, fileName)

fovPixelData <- arrow::read_feather(matPath)

# assign hierarchical cluster labels
fovPixelData$pixel_meta_cluster <- som_to_meta_map[as.character(fovPixelData$pixel_som_cluster)]

# write data with consensus labels
arrow::write_feather(as.data.table(fovPixelData), matPath)
arrow::write_feather(as.data.table(fovPixelData), matOutPath)
}

# get the number of cores
Expand Down Expand Up @@ -66,6 +68,8 @@ batchSize <- strtoi(args[8])
seed <- strtoi(args[9])
set.seed(seed)

pixelOutDir <- args[10]

# read cluster averaged data
print("Reading cluster averaged data")
clusterAvgs <- as.data.frame(read.csv(clusterAvgPath, check.names=FALSE))
Expand Down Expand Up @@ -104,7 +108,7 @@ for (batchStart in seq(1, length(fovs), batchSize)) {
i=batchStart:batchEnd,
.combine='c'
) %dopar% {
mapConsensusLabels(fovs[i], pixelMatDir, som_to_meta_map)
mapConsensusLabels(fovs[i], pixelMatDir, som_to_meta_map, pixelOutDir)
}

# unregister the parallel cluster
Expand Down
10 changes: 7 additions & 3 deletions ark/phenotyping/run_pixel_som.R
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,10 @@ library(foreach)
library(parallel)

# helper function to map a FOV to its SOM labels
mapSOMLabels <- function(fov, somWeights, pixelMatDir) {
mapSOMLabels <- function(fov, somWeights, pixelMatDir, pixelOutDir) {
fileName <- paste0(fov, ".feather")
matPath <- file.path(pixelMatDir, fileName)
matOutPath <- file.path(pixelOutDir, fileName)
fovPixelData_all <- data.table(arrow::read_feather(matPath))

# 99.9% normalization
Expand All @@ -35,7 +36,7 @@ mapSOMLabels <- function(fov, somWeights, pixelMatDir) {
fovPixelData$pixel_som_cluster <- as.integer(clusters[,1])

# write to feather
arrow::write_feather(as.data.table(fovPixelData), matPath)
arrow::write_feather(as.data.table(fovPixelData), matOutPath)
}

# get the number of cores
Expand All @@ -59,6 +60,9 @@ pixelWeightsPath <- args[4]
# retrieve the batch size to determine number of threads to run in parallel
batchSize <- strtoi(args[5])

# get new output directory
pixelOutDir <- args[6]

# read the weights
somWeights <- as.matrix(arrow::read_feather(pixelWeightsPath))

Expand Down Expand Up @@ -91,7 +95,7 @@ for (batchStart in seq(1, length(fovs), batchSize)) {
i=batchStart:batchEnd,
.combine='c'
) %dopar% {
mapSOMLabels(fovs[i], somWeights, pixelMatDir)
mapSOMLabels(fovs[i], somWeights, pixelMatDir, pixelOutDir)
}

# unregister the parallel cluster
Expand Down
2 changes: 1 addition & 1 deletion ark/phenotyping/som_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -1210,7 +1210,7 @@ def cluster_pixels(fovs, channels, base_dir, data_dir='pixel_mat_data',

# run the trained SOM on the dataset, assigning clusters
process_args = ['Rscript', '/run_pixel_som.R', ','.join(fovs),
data_path, norm_vals_path, weights_path, str(batch_size)]
data_path, norm_vals_path, weights_path, str(batch_size), out_path]

process = subprocess.Popen(process_args, stdout=subprocess.PIPE, stderr=subprocess.STDOUT)

Expand Down

0 comments on commit 942d067

Please sign in to comment.