Skip to content

Commit 2cbb287

Browse files
authored
Merge pull request #117 from pachterlab/devel
bug fix patches
2 parents 4d16c35 + 6f02774 commit 2cbb287

11 files changed

+56
-17
lines changed

R/bootstrap.R

+1
Original file line numberDiff line numberDiff line change
@@ -398,6 +398,7 @@ process_bootstrap <- function(i, samp_name, kal_path,
398398
if (read_bootstrap_tpm) {
399399
bs_quant_tpm <- aperm(apply(bs_mat, 1, counts_to_tpm,
400400
eff_len))
401+
colnames(bs_quant_tpm) <- colnames(bs_mat)
401402

402403
# gene level code is analogous here to below code
403404
if (gene_mode) {

R/measurement_error.R

+2-4
Original file line numberDiff line numberDiff line change
@@ -202,10 +202,10 @@ sleuth_wt <- function(obj, which_beta, which_model = 'full') {
202202
if ( length(beta_i) == 0 ) {
203203
stop(paste0("'", which_beta,
204204
"' doesn't appear in your design. Try one of the following:\n",
205-
colnames(d_matrix)))
205+
paste(colnames(d_matrix), collapse = ' ')))
206206
} else if ( length(beta_i) > 1 ) {
207207
stop(paste0("Sorry. '", which_beta, "' is ambiguous for columns: ",
208-
colnames(d_matrix[beta_i])))
208+
paste(colnames(d_matrix[beta_i]), collapse = ' ')))
209209
}
210210

211211
b <- sapply(obj$fits[[ which_model ]]$models,
@@ -237,8 +237,6 @@ sleuth_wt <- function(obj, which_beta, which_model = 'full') {
237237
qval = p.adjust(pval, method = 'BH')
238238
)
239239

240-
res <- dplyr::select(res, -x_group)
241-
242240
obj <- add_test(obj, res, which_beta, 'wt', which_model)
243241

244242
obj

R/sleuth.R

+20-10
Original file line numberDiff line numberDiff line change
@@ -201,11 +201,19 @@ 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

207215
msg('dropping unused factor levels')
208-
samples_to_covariates <- droplevels(sample_to_covariates)
216+
sample_to_covariates <- droplevels(sample_to_covariates)
209217

210218
nsamp <- 0
211219
# append sample column to data
@@ -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)
@@ -349,6 +357,7 @@ sleuth_prep <- function(
349357
# Get list of IDs to aggregate on (usually genes)
350358
# Also get the filtered list and update the "filter_df" and "filter_bool"
351359
# variables for the sleuth object
360+
target_mapping <- data.table::data.table(target_mapping)
352361
target_mapping[target_mapping[[aggregation_column]] == "",
353362
aggregation_column] <- NA
354363
agg_id <- unique(target_mapping[, aggregation_column, with = FALSE])
@@ -446,9 +455,10 @@ sleuth_prep <- function(
446455
})
447456

448457
# if mclapply results in an error (a warning is shown), then print error and stop
449-
if (is(bs_results[[1]], "try-error")) {
450-
print(attributes(bs_results[[1]])$condition)
451-
stop("mclapply had an error. See the above error message for more details.")
458+
error_status <- sapply(bs_results, function(x) is(x, "try-error"))
459+
if (any(error_status)) {
460+
print(attributes(bs_results[error_status])$condition)
461+
stop("At least one core from mclapply had an error. See the above error message(s) for more details.")
452462
}
453463

454464
# mclapply is expected to retun the bootstraps in order; this is a sanity check of that
@@ -471,10 +481,10 @@ sleuth_prep <- function(
471481
# This is the rest of the gene_summary code
472482
if (ret$gene_mode) {
473483
names(sigma_q_sq) <- which_agg_id
474-
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]
475485
} else {
476486
names(sigma_q_sq) <- which_target_id
477-
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]
478488
}
479489

480490
sigma_q_sq <- sigma_q_sq[order(names(sigma_q_sq))]
@@ -560,7 +570,7 @@ check_target_mapping <- function(t_id, target_mapping) {
560570
#' @export
561571
norm_factors <- function(mat) {
562572
nz <- apply(mat, 1, function(row) !any(round(row) == 0))
563-
mat_nz <- mat[nz, ]
573+
mat_nz <- mat[nz, , drop = FALSE]
564574
p <- ncol(mat)
565575
geo_means <- exp(apply(mat_nz, 1, function(row) mean(log(row))))
566576
s <- sweep(mat_nz, 1, geo_means, `/`)
@@ -716,7 +726,7 @@ obs_to_matrix <- function(obj, value_name) {
716726
rownames(obs_counts) <- obs_counts$target_id
717727
obs_counts$target_id <- NULL
718728
obs_counts <- as.matrix(obs_counts)
719-
obs_counts <- obs_counts[, obj$sample_to_covariates$sample]
729+
obs_counts <- obs_counts[, obj$sample_to_covariates$sample, drop = FALSE]
720730

721731
obs_counts
722732
}

tests/testthat/helper-setup.R

+6
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,12 @@ target_mapping <- read.table(file.path(data_path, 'target_mappings.txt'), header
1010
incomplete_mapping <- read.table(file.path(data_path, 'target_mappings_incomplete.txt'), header = TRUE,
1111
stringsAsFactors = FALSE, sep="\t", quote="")
1212

13+
small_study_map <- data.frame(sample = "small_sample", condition = "test",
14+
path = "small_test_data/kallisto.N",
15+
stringsAsFactors = F)
16+
small_target_mapping <- read.table('small_test_data/target_mapping.txt', header = TRUE,
17+
stringsAsFactors = FALSE, sep="\t", quote="")
18+
1319
study_mapping <- read.table(file.path(data_path, 'study_design.txt'), header = TRUE,
1420
stringsAsFactors = FALSE)
1521
study_mapping <- dplyr::select(study_mapping, sample = run, condition)
Binary file not shown.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
target_id gene_name
2+
NM_001168316 UGT3A2
3+
NM_174914 UGT3A2
4+
NR_031764 UGT3A2
5+
NM_004503 HOXC6
6+
NM_006897 HOXC9
7+
NM_014212 HOXC11
8+
NM_014620 HOXC4
9+
NM_017409 HOXC10
10+
NM_017410 HOXC13
11+
NM_018953 HOXC5
12+
NM_022658 HOXC8
13+
NM_153633 HOXC4
14+
NM_153693 HOXC6
15+
NM_173860 HOXC12
16+
NR_003084 HOXC5

tests/testthat/test-prep.R

+8
Original file line numberDiff line numberDiff line change
@@ -55,3 +55,11 @@ test_that("gene level", {
5555
target_mapping = incomplete_mapping,
5656
aggregation_column = "gene_name"))
5757
})
58+
59+
test_that(".N target mappings", {
60+
expect_warning(result.N <- sleuth_prep(small_study_map,
61+
target_mapping = small_target_mapping))
62+
expect_warning(result.N <- sleuth_prep(small_study_map,
63+
target_mapping = small_target_mapping,
64+
aggregation_column = "gene_name"))
65+
})

tests/testthat/test-read.R

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
context("reading")
22

33
test_that("get kallisto path", {
4-
dir_name <- "small_test_data"
4+
dir_name <- "small_test_data/kallisto"
55

66
# the standard case
77
file_name <- file.path(dir_name, "abundance.h5")
@@ -28,7 +28,7 @@ test_that("get kallisto path", {
2828
})
2929

3030
test_that("both read types", {
31-
dir_name <- "small_test_data"
31+
dir_name <- "small_test_data/kallisto"
3232

3333
h5_file_name <- file.path(dir_name, "abundance.h5")
3434
kal_h5 <- read_kallisto_h5(h5_file_name, read_bootstrap = FALSE)
@@ -42,7 +42,7 @@ test_that("both read types", {
4242
})
4343

4444
test_that("generalized read", {
45-
dir_name <- "small_test_data"
45+
dir_name <- "small_test_data/kallisto"
4646

4747
kal_dir <- read_kallisto(dir_name, read_bootstrap = TRUE)
4848
h5_file_name <- file.path(dir_name, "abundance.h5")

0 commit comments

Comments
 (0)