Skip to content

Commit 6babe79

Browse files
committed
Update code comments
1 parent f83b20b commit 6babe79

File tree

2 files changed

+17
-3
lines changed

2 files changed

+17
-3
lines changed

R/measure_polarization.R

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -186,6 +186,7 @@ MeasurePolar <- function(object,
186186
unpolarized_prob = FALSE,
187187
verbose = TRUE)
188188
{
189+
# Validate celltype
189190
if (!celltype %in% c(
190191
'B',
191192
'NK',
@@ -325,6 +326,7 @@ MeasurePolar <- function(object,
325326
df_polar <- data.frame(df_polar)
326327
rownames(df_polar) <- colnames(input_emb)
327328

329+
# Return the results as a dataframe or update the Seurat object
328330
if (return.df)
329331
return(df_polar)
330332

R/model_training.R

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ cosine_sim <- function(matrix)
66
return(cosine_similarity_matrix)
77
}
88

9-
# A helper function to calculate calibration scores for conformal prediction
9+
# A helper function to calculate calibration nonconformity scores for conformal prediction
1010
calculate_calibration_scores <- function(train_X,
1111
train_Y,
1212
classify_method='svr',
@@ -201,10 +201,13 @@ AssignStates <- function(object,
201201
embedding='uce',
202202
center_unpolar=TRUE)
203203
{
204+
# Label cells with 'PBS' in 'cyt_display' as the 'unpolarized' state
204205
object$polar <- 'else'
205206
object$polar[object$cyt_display == 'PBS'] <- 'unpolarized'
206207
states <- names(state_list)
207208
min_dist_state <- dist_thres
209+
210+
# Extract the embedding matrix based on the specified embedding
208211
if(embedding %in% Reductions(object))
209212
emb_matrix <- t(Embeddings(object, reduction=embedding))
210213
else if(embedding %in% Assays(object))
@@ -216,11 +219,14 @@ AssignStates <- function(object,
216219
)
217220
unpolar_center <- apply(emb_matrix[,object$cyt_display == 'PBS'],1,mean)
218221
all_center <- apply(emb_matrix,1,mean)
222+
223+
# Choose the center to use based on the center_unpolar paramter
219224
if(center_unpolar)
220225
using_center <- unpolar_center
221226
else
222227
using_center <- all_center
223228

229+
# Iterate over each state to assign states to cells
224230
for(state in states)
225231
{
226232
message(paste('Assigning state:', state))
@@ -241,6 +247,7 @@ AssignStates <- function(object,
241247
{
242248
min_dist_state <- pmin(mean_dist_state, min_dist_state)
243249
}
250+
# Filter cells with low correlation with others or low marker gene expression
244251
cell_state_final <- rownames(object@meta.data) %in% cell_state_filtered &
245252
mean_exp_marker > quantile(mean_exp_marker, marker_quantile)
246253
object$polar[cell_state_final] <- state
@@ -323,6 +330,7 @@ CalculateParams <- function(object,
323330
semi_supervised=FALSE,
324331
verbose=TRUE)
325332
{
333+
# Check if the embedding is in the reductions or assays of the object
326334
if(embedding %in% Reductions(object))
327335
embs <- t(Embeddings(object, reduction=embedding))
328336
else if(embedding %in% Assays(object))
@@ -335,12 +343,15 @@ CalculateParams <- function(object,
335343
emb_means <- rowMeans(embs)
336344
emb_sds <- apply(embs, 1, sd)
337345
emb_means_unpolar <- rowMeans(embs[,object$polar == 'unpolarized'])
346+
# Get PCs as input variables for machine learning models
338347
emb_loadings <- Loadings(object, reduction = 'pca')[rownames(embs), pc]
339348
state_train <- FetchData(object[,object$polar != 'else'],
340349
c(paste('PC',pc,sep='_'),'polar'))
341350
models <- list()
342351
unpolar_responses <- list()
343352
calibration_quantiles <- list()
353+
354+
# Iterate over each state to train machine learning models
344355
for(state in sort(unique(state_train$polar[state_train$polar != 'unpolarized'])))
345356
{
346357
if(verbose)
@@ -420,19 +431,20 @@ CalculateParams <- function(object,
420431
type='prob')[,'1']
421432
}
422433

434+
#Calculate the calibration nonconformity scores for conformal prediction
423435
cal_scores <- calculate_calibration_scores(train_X,
424436
train_Y,
425437
classify_method,
426438
calibration_fold)
427439

428440
models[[state]] <- model
429441
unpolar_responses[[state]] <- sort(unpolar_prediction)
430-
# Calculate 1~99 quantiles of calibration scores
442+
# Calculate 1~99 quantiles of calibration nonconformity scores
431443
calibration_quantiles[[state]] <- quantile(cal_scores,
432444
pmin(ceiling((1-seq(0.01,0.99,0.01)) *
433445
(length(cal_scores)+1)) / length(cal_scores), 1))
434446
}
435-
447+
# Create a list to store all calculated parameters
436448
object_saved_params <- list(mean=emb_means,
437449
sd=emb_sds,
438450
mean_unpolar=emb_means_unpolar,

0 commit comments

Comments
 (0)