Skip to content

Commit 110c4d3

Browse files
committed
Fix: Adding patch to address auto-type casting full rank issue, kvittingseerup/IsoformSwitchAnalyzeR#247
1 parent d8dc747 commit 110c4d3

1 file changed

Lines changed: 59 additions & 2 deletions

File tree

workflow/scripts/isoformswitchanalyzer.R

Lines changed: 59 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,70 @@
1010
# gene models, a two groups to compare, and an output
1111
# directory to store the results.
1212
########################################################
13-
14-
1513
# Load the IsoformSwitchAnalyzeR library
1614
suppressMessages(library(IsoformSwitchAnalyzeR)) # Bioconductor package
1715
suppressMessages(library(argparse)) # CRAN package
1816

1917

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+
2077
###################################
2178
# Helper functions
2279
###################################

0 commit comments

Comments
 (0)