Skip to content

Commit 6f02774

Browse files
authored
Merge pull request #114 from warrenmcg/issue92
Misc bug fixes + Allow sleuth_prep to process just one sample
2 parents 41d0c87 + 71bf190 commit 6f02774

File tree

1 file changed

+14
-6
lines changed

1 file changed

+14
-6
lines changed

R/sleuth.R

+14-6
Original file line numberDiff line numberDiff line change
@@ -201,6 +201,14 @@ sleuth_prep <- function(
201201
msg('reading in kallisto results')
202202
sample_to_covariates$sample <- as.character(sample_to_covariates$sample)
203203

204+
if(nrow(sample_to_covariates) == 1 && !is.null(full_model)) {
205+
warning("There is only one sample present, but you also provided a model. ",
206+
"The model will be set to NULL to prevent downstream errors.\n",
207+
"The sample can be viewed using sleuth_live after preparation, ",
208+
"but you need more than one sample to run the other aspects of Sleuth.")
209+
full_model <- NULL
210+
}
211+
204212
kal_dirs <- sample_to_covariates$path
205213
sample_to_covariates$path <- NULL
206214

@@ -280,7 +288,7 @@ sleuth_prep <- function(
280288
filter_true <- filter_bool[filter_bool]
281289

282290
msg(paste0(sum(filter_bool), ' targets passed the filter'))
283-
est_counts_sf <- norm_fun_counts(est_counts_spread[filter_bool, ])
291+
est_counts_sf <- norm_fun_counts(est_counts_spread[filter_bool, , drop = FALSE])
284292

285293
filter_df <- adf(target_id = names(filter_true))
286294

@@ -298,7 +306,7 @@ sleuth_prep <- function(
298306
msg("normalizing tpm")
299307
tpm_spread <- spread_abundance_by(obs_raw, "tpm",
300308
sample_to_covariates$sample)
301-
tpm_sf <- norm_fun_tpm(tpm_spread[filter_bool, ])
309+
tpm_sf <- norm_fun_tpm(tpm_spread[filter_bool, , drop = FALSE])
302310
tpm_norm <- as_df(t(t(tpm_spread) / tpm_sf))
303311
tpm_norm$target_id <- rownames(tpm_norm)
304312
tpm_norm <- tidyr::gather(tpm_norm, sample, tpm, -target_id)
@@ -473,10 +481,10 @@ sleuth_prep <- function(
473481
# This is the rest of the gene_summary code
474482
if (ret$gene_mode) {
475483
names(sigma_q_sq) <- which_agg_id
476-
obs_counts <- obs_to_matrix(ret, "scaled_reads_per_base")[which_agg_id, ]
484+
obs_counts <- obs_to_matrix(ret, "scaled_reads_per_base")[which_agg_id, , drop = FALSE]
477485
} else {
478486
names(sigma_q_sq) <- which_target_id
479-
obs_counts <- obs_to_matrix(ret, "est_counts")[which_target_id, ]
487+
obs_counts <- obs_to_matrix(ret, "est_counts")[which_target_id, , drop = FALSE]
480488
}
481489

482490
sigma_q_sq <- sigma_q_sq[order(names(sigma_q_sq))]
@@ -562,7 +570,7 @@ check_target_mapping <- function(t_id, target_mapping) {
562570
#' @export
563571
norm_factors <- function(mat) {
564572
nz <- apply(mat, 1, function(row) !any(round(row) == 0))
565-
mat_nz <- mat[nz, ]
573+
mat_nz <- mat[nz, , drop = FALSE]
566574
p <- ncol(mat)
567575
geo_means <- exp(apply(mat_nz, 1, function(row) mean(log(row))))
568576
s <- sweep(mat_nz, 1, geo_means, `/`)
@@ -718,7 +726,7 @@ obs_to_matrix <- function(obj, value_name) {
718726
rownames(obs_counts) <- obs_counts$target_id
719727
obs_counts$target_id <- NULL
720728
obs_counts <- as.matrix(obs_counts)
721-
obs_counts <- obs_counts[, obj$sample_to_covariates$sample]
729+
obs_counts <- obs_counts[, obj$sample_to_covariates$sample, drop = FALSE]
722730

723731
obs_counts
724732
}

0 commit comments

Comments
 (0)