Skip to content

Commit 4cf9bfb

Browse files
committed
add warning message for duplicated names of region_data
1 parent 2f3bf31 commit 4cf9bfb

File tree

3 files changed

+25
-14
lines changed

3 files changed

+25
-14
lines changed

R/ctwas_est_parameters.R

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@
3434
#'
3535
#' @param ... Additional arguments of \code{susie_rss}.
3636
#'
37-
#' @importFrom logging addHandler loginfo writeToFile
37+
#' @importFrom logging addHandler loginfo logwarn writeToFile
3838
#'
3939
#' @return a list with estimated parameters
4040
#'
@@ -65,9 +65,11 @@ est_param <- function(
6565
# check inputs
6666
group_prior_var_structure <- match.arg(group_prior_var_structure)
6767

68-
if (!inherits(region_data,"list")){
68+
if (!inherits(region_data,"list"))
6969
stop("'region_data' should be a list.")
70-
}
70+
71+
if (anyDuplicated(names(region_data)))
72+
logwarn("Duplicated names of region_data found! Please use unique names for region_data!")
7173

7274
# extract thin value from region_data
7375
thin <- unique(sapply(region_data, "[[", "thin"))
@@ -103,16 +105,16 @@ est_param <- function(
103105
skip_region_ids <- region_ids[(n_sids + n_gids) < min_var]
104106
if (length(skip_region_ids) > 0){
105107
loginfo("Skip %d regions with number of variables < %d.", length(skip_region_ids), min_var)
106-
region_data[skip_region_ids] <- NULL
108+
region_data <- region_data[!names(region_data) %in% skip_region_ids]
107109
}
108110
}
109111

110112
# skip regions with fewer than min_gene genes
111113
if (min_gene > 0) {
112114
skip_region_ids <- region_ids[n_gids < min_gene]
113115
if (length(skip_region_ids) > 0){
114-
loginfo("Remove %d regions with number of genes < %d.", length(skip_region_ids), min_gene)
115-
region_data[skip_region_ids] <- NULL
116+
loginfo("Skip %d regions with number of genes < %d.", length(skip_region_ids), min_gene)
117+
region_data <- region_data[!names(region_data) %in% skip_region_ids]
116118
}
117119
}
118120

R/ctwas_finemapping.R

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,9 @@ finemap_regions <- function(region_data,
9090
if (!inherits(region_data,"list"))
9191
stop("'region_data' should be a list.")
9292

93+
if (anyDuplicated(names(region_data)))
94+
logwarn("Duplicated names of region_data found! Please use unique names for region_data!")
95+
9396
if (missing(LD_map))
9497
stop("'LD_map' is required when running with LD!")
9598

@@ -130,7 +133,6 @@ finemap_regions <- function(region_data,
130133
}
131134

132135
region_ids <- names(region_data)
133-
134136
res <- mclapply_check(region_ids, function(region_id){
135137
finemap_single_region(region_data = region_data,
136138
region_id = region_id,
@@ -196,7 +198,7 @@ finemap_regions <- function(region_data,
196198
#'
197199
#' @return a data frame of cTWAS finemapping results.
198200
#'
199-
#' @importFrom logging addHandler loginfo writeToFile
201+
#' @importFrom logging addHandler loginfo logwarn writeToFile
200202
#' @importFrom parallel mclapply
201203
#'
202204
#' @export
@@ -221,6 +223,9 @@ finemap_regions_noLD <- function(region_data,
221223
if (!inherits(region_data,"list"))
222224
stop("'region_data' should be a list!")
223225

226+
if (anyDuplicated(names(region_data)))
227+
logwarn("Duplicated names of region_data found! Please use unique names for region_data!")
228+
224229
if (verbose) {
225230
if (is.null(group_prior)) {
226231
loginfo("Use uniform prior.")
@@ -231,7 +236,6 @@ finemap_regions_noLD <- function(region_data,
231236
}
232237

233238
region_ids <- names(region_data)
234-
235239
res <- mclapply_check(region_ids, function(region_id){
236240
finemap_single_region_noLD(region_data = region_data,
237241
region_id = region_id,
@@ -592,7 +596,8 @@ fast_finemap_single_region_L1_noLD <- function(region_data,
592596
rm(regiondata)
593597

594598
if (length(z) < 2) {
595-
stop(paste(length(z), "variables in the region. At least two variables in a region are needed to run susie"))
599+
stop(paste(length(z), "variables in the region", region_id, "\n",
600+
"At least two variables in a region are needed to run susie"))
596601
}
597602

598603
# update priors, prior variances and null_weight

R/ctwas_screen_regions.R

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -232,7 +232,7 @@ screen_regions <- function(region_data,
232232
#' @return a list, containing a data frame of selected region data,
233233
#' and a data frame of screening summary for all regions.
234234
#'
235-
#' @importFrom logging addHandler loginfo writeToFile
235+
#' @importFrom logging addHandler loginfo logwarn writeToFile
236236
#'
237237
#' @export
238238
#'
@@ -267,13 +267,17 @@ screen_regions_noLD <- function(region_data,
267267
if (!inherits(region_data,"list"))
268268
stop("'region_data' should be a list.")
269269

270+
if (anyDuplicated(names(region_data)))
271+
logwarn("Duplicated names of region_data found! Please use unique names for region_data!")
272+
270273
# adjust group_prior to account for thin argument
271274
if (!is.null(group_prior)){
272275
group_prior["SNP"] <- group_prior["SNP"]/thin
273276
}
274277

275278
# create a data frame for screening summary
276279
region_ids <- names(region_data)
280+
277281
n_gids <- sapply(region_data, function(x){length(x$gid)})
278282
n_sids <- sapply(region_data, function(x){length(x$sid)})
279283
screen_summary <- data.frame(region_id = region_ids,
@@ -287,16 +291,16 @@ screen_regions_noLD <- function(region_data,
287291
skip_region_ids <- region_ids[(n_sids + n_gids) < min_var]
288292
if (length(skip_region_ids) > 0){
289293
loginfo("Skip %d regions with number of variables < %d.", length(skip_region_ids), min_var)
290-
region_data[skip_region_ids] <- NULL
294+
region_data <- region_data[!names(region_data) %in% skip_region_ids]
291295
}
292296
}
293297

294298
# skip regions with fewer than min_gene genes
295299
if (min_gene > 0) {
296300
skip_region_ids <- region_ids[n_gids < min_gene]
297301
if (length(skip_region_ids) > 0){
298-
loginfo("Remove %d regions with number of genes < %d.", length(skip_region_ids), min_gene)
299-
region_data[skip_region_ids] <- NULL
302+
loginfo("Skip %d regions with number of genes < %d.", length(skip_region_ids), min_gene)
303+
region_data <- region_data[!names(region_data) %in% skip_region_ids]
300304
}
301305
}
302306

0 commit comments

Comments
 (0)