Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
175 changes: 87 additions & 88 deletions LinseedMetadata.R
Original file line number Diff line number Diff line change
Expand Up @@ -3,103 +3,102 @@ library(R6)
LinseedMetadata <- R6Class(
"LinseedMetadata",
public = list(
filtered_samples = NULL,
filtered_dataset = NULL,
V_row = NULL,
V_column = NULL,
raw_dataset = NULL,
path_ = NULL,
analysis_name = NULL,
dataset = NULL,
topGenes = NULL,
cell_types = NULL,
coef_der_X = NULL,
coef_der_Omega = NULL,
coef_hinge_H = NULL,
coef_hinge_W = NULL,
coef_pos_D_h = NULL,
coef_pos_D_w = NULL,
global_iterations = NULL,
init_count_neg_props = NULL,
init_count_neg_basis = NULL,
count_neg_props = NULL,
count_neg_basis = NULL,
filtered_samples = NULL,
filtered_dataset = NULL,
V_row = NULL,
V_column = NULL,
raw_dataset = NULL,
path_ = NULL,
analysis_name = NULL,
dataset = NULL,
topGenes = NULL,
cell_types = NULL,
coef_der_X = NULL,
coef_der_Omega = NULL,
coef_hinge_H = NULL,
coef_hinge_W = NULL,
coef_pos_D_h = NULL,
coef_pos_D_w = NULL,
global_iterations = NULL,
init_count_neg_props = NULL,
init_count_neg_basis = NULL,
count_neg_props = NULL,
count_neg_basis = NULL,

full_proportions = NULL,
full_basis = NULL,
orig_full_proportions = NULL,
orig_full_basis = NULL,
R = NULL,
S = NULL,

final_X = NULL,
final_Omega = NULL,
final_W_ = NULL,
final_H_ = NULL,
final_D_h = NULL,
final_D_w = NULL,

init_D_w = NULL,
init_D_h = NULL,
init_X = NULL,
init_H = NULL,
init_W = NULL,
init_Omega = NULL,
errors_statistics = NULL,
metric = NULL,
full_proportions = NULL,
full_basis = NULL,
orig_full_proportions = NULL,
orig_full_basis = NULL,
R = NULL,
S = NULL,

initialize = function(linseed_object) {
self$filtered_samples <- linseed_object$filtered_samples
self$filtered_dataset <- linseed_object$filtered_dataset
self$raw_dataset <- linseed_object$raw_dataset
self$V_row <- linseed_object$V_row
self$V_column <- linseed_object$V_column
final_X = NULL,
final_Omega = NULL,
final_W_ = NULL,
final_H_ = NULL,
final_D_h = NULL,
final_D_w = NULL,

self$dataset <- linseed_object$dataset
self$path_ <- linseed_object$path_
self$analysis_name <- linseed_object$analysis_name
self$topGenes <- linseed_object$topGenes
self$cell_types <- linseed_object$cell_types
self$coef_der_X <- linseed_object$coef_der_X
self$coef_der_Omega <- linseed_object$coef_der_Omega
self$coef_hinge_H <- linseed_object$coef_hinge_H
self$coef_hinge_W <- linseed_object$coef_hinge_W
self$coef_pos_D_h <- linseed_object$coef_pos_D_h
self$coef_pos_D_w <- linseed_object$coef_pos_D_w
self$global_iterations <- linseed_object$global_iterations
init_D_w = NULL,
init_D_h = NULL,
init_X = NULL,
init_H = NULL,
init_W = NULL,
init_Omega = NULL,
errors_statistics = NULL,
metric = NULL,

self$init_X <- linseed_object$init_X
self$init_Omega <- linseed_object$init_Omega
self$init_H <- linseed_object$init_H
self$init_W <- linseed_object$init_W
initialize = function(linseed_object) {
self$filtered_samples <- linseed_object$filtered_samples
self$filtered_dataset <- linseed_object$filtered_dataset
self$raw_dataset <- linseed_object$raw_dataset
self$V_row <- linseed_object$V_row
self$V_column <- linseed_object$V_column

self$final_X <- linseed_object$X
self$final_Omega <- linseed_object$Omega
self$final_H_ <- linseed_object$H_
self$final_W_ <- linseed_object$W_

self$errors_statistics <- linseed_object$errors_statistics
self$dataset <- linseed_object$dataset
self$path_ <- linseed_object$path_
self$analysis_name <- linseed_object$analysis_name
self$topGenes <- linseed_object$topGenes
self$cell_types <- linseed_object$cell_types
self$coef_der_X <- linseed_object$coef_der_X
self$coef_der_Omega <- linseed_object$coef_der_Omega
self$coef_hinge_H <- linseed_object$coef_hinge_H
self$coef_hinge_W <- linseed_object$coef_hinge_W
self$coef_pos_D_h <- linseed_object$coef_pos_D_h
self$coef_pos_D_w <- linseed_object$coef_pos_D_w
self$global_iterations <- linseed_object$global_iterations

self$R <- linseed_object$R
self$S <- linseed_object$S
self$init_X <- linseed_object$init_X
self$init_Omega <- linseed_object$init_Omega
self$init_H <- linseed_object$init_H
self$init_W <- linseed_object$init_W

self$init_D_h <- linseed_object$init_D_h
self$init_D_w <- linseed_object$init_D_w
self$final_D_h <- linseed_object$D_h
self$final_D_w <- linseed_object$D_w
self$final_X <- linseed_object$X
self$final_Omega <- linseed_object$Omega
self$final_H_ <- linseed_object$H_
self$final_W_ <- linseed_object$W_

self$init_count_neg_props <- linseed_object$init_count_neg_props
self$init_count_neg_basis <- linseed_object$init_count_neg_basis
self$count_neg_props <- linseed_object$count_neg_props
self$count_neg_basis <- linseed_object$count_neg_basis
self$errors_statistics <- linseed_object$errors_statistics

self$full_proportions <- linseed_object$full_proportions
self$orig_full_proportions <- linseed_object$orig_full_proportions
self$full_basis <- linseed_object$full_basis
self$orig_full_basis <- linseed_object$orig_full_basis
self$R <- linseed_object$R
self$S <- linseed_object$S

self$metric <- linseed_object$metric
self$init_D_h <- linseed_object$init_D_h
self$init_D_w <- linseed_object$init_D_w
self$final_D_h <- linseed_object$D_h
self$final_D_w <- linseed_object$D_w

}
)
self$init_count_neg_props <- linseed_object$init_count_neg_props
self$init_count_neg_basis <- linseed_object$init_count_neg_basis
self$count_neg_props <- linseed_object$count_neg_props
self$count_neg_basis <- linseed_object$count_neg_basis

self$full_proportions <- linseed_object$full_proportions
self$orig_full_proportions <- linseed_object$orig_full_proportions
self$full_basis <- linseed_object$full_basis
self$orig_full_basis <- linseed_object$orig_full_basis

self$metric <- linseed_object$metric
}
)
)
131 changes: 65 additions & 66 deletions RunDeconvolution.R
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,12 @@ sourceCpp('/app/pipeline.cpp')


option_list <- list(
make_option(c("-c", "--config"), action="store", default=NA, type='character',
help="YAML with configutation path"),
make_option(c("-i", "--init"), action="store", default=NA, type='integer',
help="Initialization number")
make_option(c("-c", "--config"), action = "store", default = NA, type = 'character',
help = "YAML with configutation path"),
make_option(c("-i", "--init"), action = "store", default = NA, type = 'integer',
help = "Initialization number")
)
opt <- parse_args(OptionParser(option_list=option_list))
opt <- parse_args(OptionParser(option_list = option_list))

if (is.na(opt$c)) {
stop("YAML Configuration file path is not specified")
Expand All @@ -30,7 +30,7 @@ dir.create(path_, recursive = T, showWarnings = F)

analysis_name <- obj[['analysis_name']]
if (!is.na(opt$i)) {
analysis_name <- paste0(analysis_name,"_",opt$i)
analysis_name <- paste0(analysis_name, "_", opt$i)
}

if (!is.null(obj[['coef_hinge_H']])) {
Expand Down Expand Up @@ -81,72 +81,71 @@ if (!is.null(obj[['scale_iterations']])) {
scale_iterations <- 20
}

cat(paste("\n",analysis_name,":",
"\nNumber of iterations:",global_iterations,
"\nCell types:",obj[['cell_types']],
"\nDataset:",obj[['dataset']],
"\nPath:",path_,
"\nX derivative coefficient:",coef_der_X,
"\nOmega derivative coefficient:",coef_der_Omega,
"\nSum-to-one D_h coefficient:",coef_pos_D_h,
"\nSum-to-one D_w coefficient:",coef_pos_D_w,
"\nPositive proportions coefficient:",coef_hinge_H,
"\nPositive basis coefficient:",coef_hinge_W,"\n"))
cat(paste("\n", analysis_name, ":",
"\nNumber of iterations:", global_iterations,
"\nCell types:", obj[['cell_types']],
"\nDataset:", obj[['dataset']],
"\nPath:", path_,
"\nX derivative coefficient:", coef_der_X,
"\nOmega derivative coefficient:", coef_der_Omega,
"\nSum-to-one D_h coefficient:", coef_pos_D_h,
"\nSum-to-one D_w coefficient:", coef_pos_D_w,
"\nPositive proportions coefficient:", coef_hinge_H,
"\nPositive basis coefficient:", coef_hinge_W, "\n"))


if (!is.null(obj[['data']])) {
data_ <- readRDS(obj[['data']][['path']])
tmp_snkhrn <- SinkhornNNLSLinseed$new(dataset = obj[['dataset']], path = path_, analysis_name = analysis_name,
cell_types = obj[['cell_types']],
global_iterations = global_iterations,
coef_der_X = coef_der_X, coef_der_Omega = coef_der_Omega,
coef_hinge_H = coef_hinge_H, coef_hinge_W = coef_hinge_W,
coef_pos_D_h = coef_pos_D_h, coef_pos_D_w = coef_pos_D_w,
data = data_)
cell_types = obj[['cell_types']],
global_iterations = global_iterations,
coef_der_X = coef_der_X, coef_der_Omega = coef_der_Omega,
coef_hinge_H = coef_hinge_H, coef_hinge_W = coef_hinge_W,
coef_pos_D_h = coef_pos_D_h, coef_pos_D_w = coef_pos_D_w,
data = data_)
} else {
tmp_snkhrn <- SinkhornNNLSLinseed$new(dataset = obj[['dataset']], path = path_, analysis_name = analysis_name,
cell_types = obj[['cell_types']],
global_iterations = global_iterations,
coef_der_X = coef_der_X, coef_der_Omega = coef_der_Omega,
coef_pos_D_h = coef_pos_D_h, coef_pos_D_w = coef_pos_D_w,
coef_hinge_H = coef_hinge_H, coef_hinge_W = coef_hinge_W)
cell_types = obj[['cell_types']],
global_iterations = global_iterations,
coef_der_X = coef_der_X, coef_der_Omega = coef_der_Omega,
coef_pos_D_h = coef_pos_D_h, coef_pos_D_w = coef_pos_D_w,
coef_hinge_H = coef_hinge_H, coef_hinge_W = coef_hinge_W)
}


tmp_snkhrn$selectTopGenes(min(obj[['top_genes']],nrow(tmp_snkhrn$filtered_data)))

if (!is.null(obj[['svd_k']])) {
k <- max(obj[['cell_types']],obj[['svd_k']])
} else {
k <- obj[['cell_types']]
}

print("Scaling dataset")
tmp_snkhrn$scaleDataset(iterations = scale_iterations)

if (!is.null(obj[['projections']])) {
print("Loading projections")
tmp_snkhrn$readProjections(file = file.path(obj[['projections']][['path']],paste0("ct",tmp_snkhrn$cell_types),paste0(tmp_snkhrn$dataset,"_",opt$i,".rds")))
} else {
print("Get S and R projections")
tmp_snkhrn$getSvdProjectionsNew(k=k)
}


if (!is.null(obj[['inits']])) {
print("Loading initial points")
tmp_snkhrn$readInitValues(file = file.path(obj[['inits']][['path']],paste0("ct",tmp_snkhrn$cell_types),paste0(tmp_snkhrn$dataset,"_",opt$i,".rds")))
} else {
print("Find initial points")
tmp_snkhrn$selectInitOmega()
}

print("Run optimization")
tmp_snkhrn$runOptimization()
print("Save results")
tmp_snkhrn$saveResults()
print("Deconvolution complete")

metadata_ <- LinseedMetadata$new(tmp_snkhrn)
save(metadata_,file=paste0(metadata_$path_,"/",metadata_$analysis_name,".meta"))


tmp_snkhrn$selectTopGenes(min(obj[['top_genes']], nrow(tmp_snkhrn$filtered_data)))

if (!is.null(obj[['svd_k']])) {
k <- max(obj[['cell_types']], obj[['svd_k']])
} else {
k <- obj[['cell_types']]
}

print("Scaling dataset")
tmp_snkhrn$scaleDataset(iterations = scale_iterations)

if (!is.null(obj[['projections']])) {
print("Loading projections")
tmp_snkhrn$readProjections(file = file.path(obj[['projections']][['path']], paste0("ct", tmp_snkhrn$cell_types), paste0(tmp_snkhrn$dataset, "_", opt$i, ".rds")))
} else {
print("Get S and R projections")
tmp_snkhrn$getSvdProjectionsNew(k = k)
}


if (!is.null(obj[['inits']])) {
print("Loading initial points")
tmp_snkhrn$readInitValues(file = file.path(obj[['inits']][['path']], paste0("ct", tmp_snkhrn$cell_types), paste0(tmp_snkhrn$dataset, "_", opt$i, ".rds")))
} else {
print("Find initial points")
tmp_snkhrn$selectInitOmega()
}

print("Run optimization")
tmp_snkhrn$runOptimization()
print("Save results")
tmp_snkhrn$saveResults()
print("Deconvolution complete")

metadata_ <- LinseedMetadata$new(tmp_snkhrn)
save(metadata_, file = paste0(metadata_$path_, "/", metadata_$analysis_name, ".meta"))
Loading