|
10 | 10 | # gene models, a two groups to compare, and an output |
11 | 11 | # directory to store the results. |
12 | 12 | ######################################################## |
13 | | - |
14 | | - |
15 | 13 | # Load the IsoformSwitchAnalyzeR library |
16 | 14 | suppressMessages(library(IsoformSwitchAnalyzeR)) # Bioconductor package |
17 | 15 | suppressMessages(library(argparse)) # CRAN package |
18 | 16 |
|
19 | 17 |
|
| 18 | +################################### |
| 19 | +# Patching IsoformSwitchAnalyzeR |
| 20 | +################################### |
| 21 | +# This patch addresses an issue |
| 22 | +# within the importRdata function. |
| 23 | +# This patch prevents automatic |
| 24 | +# type casting of numeric/integer |
| 25 | +# columns within the design matrix. |
| 26 | +# This is important because auto- |
| 27 | +# casting can cause your design |
| 28 | +# matrix to not be full rank |
| 29 | +# after the conversion. Here is |
| 30 | +# the relevant GitHub issue: |
| 31 | +# https://github.com/kvittingseerup/IsoformSwitchAnalyzeR/issues/247 |
| 32 | +pkg <- "IsoformSwitchAnalyzeR" |
| 33 | +fn_name <- "importRdata" |
| 34 | +ns <- asNamespace(pkg) |
| 35 | +buggy_fn <- getFromNamespace(fn_name, pkg) |
| 36 | + |
| 37 | +# Turn the function into text |
| 38 | +before_txt <- paste(deparse(buggy_fn), collapse = "\n") |
| 39 | +# Regex that matches: |
| 40 | +# if( uniqueLength( localDesign[,i] ) * 2 <= length(localDesign[,i]) ) { |
| 41 | +# with flexible whitespace/newlines |
| 42 | +# We will replace this with FALSE |
| 43 | +# so the if statement never gets |
| 44 | +# evalulated. This is where the |
| 45 | +# auto-casting happens. |
| 46 | +pattern <- paste0( |
| 47 | + "if\\s*\\(\\s*uniqueLength\\s*\\(\\s*localDesign\\s*\\[\\s*,\\s*i\\s*\\]\\s*\\)", |
| 48 | + "\\s*\\*\\s*2\\s*<=\\s*length\\s*\\(\\s*localDesign\\s*\\[\\s*,\\s*i\\s*\\]\\s*\\)", |
| 49 | + "\\s*\\)\\s*\\{" |
| 50 | +) |
| 51 | + |
| 52 | +replacement <- "if(FALSE) { # patched: disables numeric to factor auto-casting" |
| 53 | +if (!grepl(pattern, before_txt, perl = TRUE)) { |
| 54 | + stop("Error: Patch target not found. Function text may differ in your installed version.") |
| 55 | +} |
| 56 | + |
| 57 | +after_txt <- sub(pattern, replacement, before_txt, perl = TRUE) |
| 58 | + |
| 59 | +# Recreate the function |
| 60 | +patched_fn <- eval(parse(text = after_txt), envir = ns) |
| 61 | + |
| 62 | +# Patches the namespace |
| 63 | +unlockBinding(fn_name, ns) |
| 64 | +assign(fn_name, patched_fn, envir = ns) |
| 65 | +lockBinding(fn_name, ns) |
| 66 | + |
| 67 | +# Patch any attached exports |
| 68 | +pkg_env_name <- paste0("package:", pkg) |
| 69 | +if (pkg_env_name %in% search()) { |
| 70 | + pkg_env <- as.environment(pkg_env_name) |
| 71 | + unlockBinding(fn_name, pkg_env) |
| 72 | + assign(fn_name, patched_fn, envir = pkg_env) |
| 73 | + lockBinding(fn_name, pkg_env) |
| 74 | +} |
| 75 | + |
| 76 | + |
20 | 77 | ################################### |
21 | 78 | # Helper functions |
22 | 79 | ################################### |
|
0 commit comments