Skip to content

Commit fe1f4c1

Browse files
authored
Merge pull request #124 from xueweic/main
minor fix error for merging ucos and ucos_from_cos
2 parents 17f9a2c + 2a6cc70 commit fe1f4c1

2 files changed

Lines changed: 61 additions & 34 deletions

File tree

R/colocboost_output.R

Lines changed: 55 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -363,25 +363,38 @@ get_robust_colocalization <- function(cb_output,
363363
# remove CoS only with one trait
364364
n_outcome <- sapply(cos_details$cos_outcomes$outcome_index, length)
365365
single <- which(n_outcome == 1)
366-
if (length(single) == length(n_outcome)) {
367-
# - all remaining the single outcome
368-
cb_output$cos_details <- cb_output$vcp <- NULL
369-
cb_output <- c(cb_output, list(vcp = NULL, cos_details = NULL))
370-
} else if (length(single) != 0 & length(single) != length(n_outcome)) {
371-
# - partial remaining the single outcome
366+
if (length(single)!=0){
367+
# organize results
372368
ucos_outcomes_npc <- data.frame(
373-
outcome = unlist(cos_details$cos_outcomes$outcome_name[single]),
374-
outcomes_index = unlist(cos_details$cos_outcomes$outcome_index[single]),
375-
relative_logLR = sapply(single, function(ss){
376-
cos_details$cos_outcomes_npc[[ss]]$relative_logLR[1]
377-
}),
378-
npc_outcome = sapply(single, function(ss){
379-
cos_details$cos_outcomes_npc[[ss]]$npc_outcome[1]
380-
})
369+
outcome = unlist(cos_details$cos_outcomes$outcome_name[single]),
370+
outcomes_index = unlist(cos_details$cos_outcomes$outcome_index[single]),
371+
relative_logLR = sapply(single, function(ss){
372+
cos_details$cos_outcomes_npc[[ss]]$relative_logLR[1]
373+
}),
374+
npc_outcome = sapply(single, function(ss){
375+
cos_details$cos_outcomes_npc[[ss]]$npc_outcome[1]
376+
})
377+
)
378+
rownames(ucos_outcomes_npc) <- names(cos_details$cos$cos_index[single])
379+
ww <- cos_details$cos_weights[single]
380+
names(ww) <- names(cos_details$cos$cos_index[single])
381+
382+
if (length(single) == length(n_outcome)){
383+
# - all remaining the single outcome
384+
cos_ucos_purity = list(
385+
"min_abs_cor" = NULL,
386+
"median_abs_cor" = NULL,
387+
"max_abs_cor" = NULL
381388
)
382-
rownames(ucos_outcomes_npc) <- names(cos_details$cos$cos_index[single])
383-
ww <- cos_details$cos_weights[single]
384-
names(ww) <- names(cos_details$cos$cos_index[single])
389+
} else {
390+
# - partial remaining the single outcome
391+
cos_ucos_purity = list(
392+
"min_abs_cor" = as.matrix(cos_details$cos_purity$min_abs_cor)[-single, single, drop = FALSE],
393+
"median_abs_cor" = as.matrix(cos_details$cos_purity$median_abs_cor)[-single, single, drop = FALSE],
394+
"max_abs_cor" = as.matrix(cos_details$cos_purity$max_abs_cor)[-single, single, drop = FALSE]
395+
)
396+
}
397+
385398
ucos_from_cos <- list(
386399
"ucos" = list(
387400
"ucos_index" = cos_details$cos$cos_index[single],
@@ -398,29 +411,26 @@ get_robust_colocalization <- function(cb_output,
398411
"median_abs_cor" = as.matrix(cos_details$cos_purity$median_abs_cor)[single, single, drop = FALSE],
399412
"max_abs_cor" = as.matrix(cos_details$cos_purity$max_abs_cor)[single, single, drop = FALSE]
400413
),
401-
"cos_ucos_purity" = list(
402-
"min_abs_cor" = as.matrix(cos_details$cos_purity$min_abs_cor)[-single, single, drop = FALSE],
403-
"median_abs_cor" = as.matrix(cos_details$cos_purity$median_abs_cor)[-single, single, drop = FALSE],
404-
"max_abs_cor" = as.matrix(cos_details$cos_purity$max_abs_cor)[-single, single, drop = FALSE]
405-
),
414+
"cos_ucos_purity" = cos_ucos_purity,
406415
"ucos_outcomes_npc" = ucos_outcomes_npc
407416
)
408417
cb_output <- remove_cos(cb_output, remove_idx = single)
409-
410418
# merge ucos_from_cos to ucos_details if appliable
411419
message("There are ", length(single), " uCoS generated after filtering the robust colocalization.")
412420
if (!("ucos_details" %in% names(cb_output))) {
413421
cb_output$ucos_details <- ucos_from_cos
414422
} else {
415-
cb_output$ucos_details <- merge_ucos_details(cb_output$ucos_details, ucos_from_cos)
423+
if (is.null(cb_output$ucos_details)){
424+
cb_output$ucos_details <- ucos_from_cos
425+
} else {
426+
cb_output$ucos_details <- merge_ucos_details(cb_output$ucos_details, ucos_from_cos)
427+
}
416428
}
417429
}
418-
419430

420431
# remove CoS does not pass cos_npc_cutoff
421432
remove <- which(cb_output$cos_details$cos_npc < cos_npc_cutoff)
422433
cb_output <- remove_cos(cb_output, remove_idx = remove)
423-
cos_details <- cb_output$cos_details
424434

425435
# - refine and output
426436
class(cb_output) <- "colocboost"
@@ -1279,21 +1289,34 @@ get_cos_purity <- function(cos, X = NULL, Xcorr = NULL, n_purity = 100) {
12791289
merge_ucos_details <- function(ucos_details, ucos_from_cos) {
12801290

12811291
get_cos_ucos_purity <- function(from_ucos, from_cos){
1282-
cos <- intersect(rownames(from_ucos), rownames(from_ucos))
1283-
tmp_from_ucos <- from_ucos[match(cos, rownames(from_ucos)), , drop = FALSE]
1284-
tmp_from_cos <- from_cos[match(cos, rownames(from_cos)), , drop = FALSE]
1285-
cbind(tmp_from_ucos, tmp_from_cos)
1292+
if (is.null(from_cos)){
1293+
return(from_ucos)
1294+
} else {
1295+
cos <- intersect(rownames(from_ucos), rownames(from_cos))
1296+
tmp_from_ucos <- from_ucos[match(cos, rownames(from_ucos)), , drop = FALSE]
1297+
tmp_from_cos <- from_cos[match(cos, rownames(from_cos)), , drop = FALSE]
1298+
cbind(tmp_from_ucos, tmp_from_cos)
1299+
}
12861300
}
12871301

12881302
get_ucos_purity <- function(from_ucos, from_cos, cross_from_ucos, cross_from_cos) {
1303+
1304+
from_ucos = ucos_details$ucos_purity$min_abs_cor
1305+
from_cos = ucos_from_cos$ucos_purity$min_abs_cor
1306+
cross_from_ucos = ucos_details$cos_ucos_purity$min_abs_cor
1307+
cross_from_cos = ucos_from_cos$cos_ucos_purity$min_abs_cor
1308+
1309+
12891310
for (id in unique(sub(":.*", "", rownames(from_cos)))) {
12901311
old <- grep(paste0("^", id, ":"), rownames(cross_from_ucos), value = TRUE)[1]
12911312
new <- grep(paste0("^", id, ":"), rownames(from_cos), value = TRUE)[1]
12921313
if (!is.na(old) && !is.na(new)) {
12931314
rownames(cross_from_ucos) <- sub(old, new, rownames(cross_from_ucos), fixed = TRUE)
12941315
colnames(cross_from_ucos) <- sub(old, new, colnames(cross_from_ucos), fixed = TRUE)
1295-
rownames(cross_from_cos) <- sub(old, new, rownames(cross_from_cos), fixed = TRUE)
1296-
colnames(cross_from_cos) <- sub(old, new, colnames(cross_from_cos), fixed = TRUE)
1316+
if (!is.null(cross_from_cos)){
1317+
rownames(cross_from_cos) <- sub(old, new, rownames(cross_from_cos), fixed = TRUE)
1318+
colnames(cross_from_cos) <- sub(old, new, colnames(cross_from_cos), fixed = TRUE)
1319+
}
12971320
}
12981321
}
12991322
all_ucos <- c(rownames(from_ucos), rownames(from_cos))

R/colocboost_utils.R

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1038,8 +1038,12 @@ get_full_output <- function(cb_obj, past_out = NULL, variables = NULL, cb_output
10381038
}
10391039
names(specific_cs_purity) <- c("min_abs_cor", "max_abs_cor", "median_abs_cor")
10401040
} else {
1041-
specific_cs_purity <- out_ucos$purity_each
1042-
rownames(specific_cs_purity) <- specific_cs_names
1041+
specific_cs_purity <- lapply(1:3, function(ii) {
1042+
mm <- as.matrix(out_ucos$purity_each[,ii])
1043+
rownames(mm) <- colnames(mm) <- specific_cs_names
1044+
return(mm)
1045+
})
1046+
names(specific_cs_purity) <- c("min_abs_cor", "max_abs_cor", "median_abs_cor")
10431047
}
10441048

10451049
# - cos&ucos purity

0 commit comments

Comments
 (0)