@@ -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) {
12791289merge_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 ))
0 commit comments