Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 20 additions & 19 deletions code/pecotmr_integration/twas_ctwas.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -577,31 +577,32 @@
"\n",
"\n",
" # function to update method selection\n",
" update_twas_method <- function(twas_df){\n",
" update_twas_method <- function(twas_df, rsq_cutoff =${rsq_cutoff}, rsq_pval_cutoff = ${rsq_pval_cutoff}){\n",
" if (nrow(twas_df) == 0) { \n",
" return(twas_df)\n",
" } \n",
" twas_df$method_selected_original <- twas_df$is_selected_method\n",
" twas_df_colnames <- colnames(twas_df)\n",
" twas_df$gene_context <- paste0(twas_df$molecular_id, \"_\", twas_df$context)\n",
" rs_update <- do.call(rbind, lapply(unique(twas_df$gene_context), function(gene_context){\n",
" do.call(rbind, lapply(unique(twas_df$gwas_study), function(study){\n",
" df <- twas_df[twas_df$gene_context==gene_context & twas_df$gwas_study==study,,drop=FALSE]\n",
" df_colnames <- colnames(df)\n",
" all_imputable <- df[df$rsq_cv>=${rsq_cutoff} & df$pval_cv<=${rsq_pval_cutoff} & !is.na(df$twas_z),,drop=FALSE]\n",
" if (nrow(all_imputable)<=1 || all(is.na(all_imputable$twas_z))) {\n",
" return(df) # gene-context pair does not having any other imputable model available.\n",
" } else {\n",
" message (\"Updating method selection for \", gene_context, \"-\", study, \".\")\n",
" selected_method_update <- all_imputable[order(-all_imputable$rsq_cv),]$method[1]\n",
" df$selected_method_update <- df$method==selected_method_update\n",
" df$is_selected_method <- df$selected_method_update\n",
" df <- df[, df_colnames]\n",
" return(df)\n",
" }\n",
" }))\n",
" }))\n",
" return(rs_update[, twas_df_colnames])\n",
" fix_sel <- twas_df$is_selected_method & (is.na(twas_df$twas_z) | is.na(twas_df$twas_pval) | is.infinite(twas_df$twas_pval) |\n",
" is.null(twas_df$twas_z) | is.null(twas_df$twas_pval))\n",
" if (!any(fix_sel)) return(twas_df) # If nothing to fix, return fast\n",
" all_group <- paste(twas_df$gene_context, twas_df$gwas_study, sep = \" - \") #gene-context-study group\n",
" fix_groups <- unique(all_group[fix_sel])\n",
"\n",
" for (fixgroup in fix_groups) {\n",
" df_idxs <- which(all_group == fixgroup) # idx of gene-context-study group that needs replacement of method selected\n",
" if (length(df_idxs) == 1) next\n",
" candidate_idxs <- df_idxs[!is.na(twas_df$twas_z[df_idxs]) & !is.na(twas_df$twas_pval[df_idxs]) & !is.infinite(twas_df$twas_z[df_idxs]) & \n",
" !is.null(twas_df$twas_z[df_idxs]) & !is.null(twas_df$twas_pval[df_idxs]) &\n",
" twas_df$rsq_cv[df_idxs] >= rsq_cutoff & twas_df$pval_cv[df_idxs] <= rsq_pval_cutoff] \n",
" if (length(candidate_idxs) <= 0 || all(is.na(candidate_idxs))) next\n",
" best_i <- candidate_idxs[which.max(twas_df$rsq_cv[candidate_idxs])]\n",
" best_method <- twas_df$method[best_i]\n",
" twas_df$is_selected_method[df_idxs] <- (twas_df$method[df_idxs] == best_method)\n",
" message (\"Updating method selection for \", fixgroup, \".\")\n",
" }\n",
" return(twas_df[, twas_df_colnames])\n",
" }\n",
"\n",
" # Load metadata and configuration - let these fail if there are issues\n",
Expand Down
Loading