Skip to content
Draft
Changes from all commits
Commits
Show all changes
40 commits
Select commit Hold shift + click to select a range
046ac3a
Added print statements to rdtest
kjaisingh May 23, 2025
b4997e3
Updated to use t-test and mad/median
kjaisingh Jun 4, 2025
d9a6b14
Implemented ttest
kjaisingh Jun 5, 2025
806f878
Implemented median/MAD for singlesample test, Welch's t-test for twos…
kjaisingh Jun 10, 2025
ee18730
Reset other RD testing scripts to original versions
kjaisingh Jun 10, 2025
de90d27
Used wilcoxon rank-sum test for twosample case
kjaisingh Jun 11, 2025
d1cbd78
Implemented permutation test with n=1000
kjaisingh Jun 12, 2025
c2513e4
Implemented anderson-darling test
kjaisingh Jun 12, 2025
9c19c9c
Added ksamples to dockerfile
kjaisingh Jun 12, 2025
ade5bd4
Implemented Cramér-von Mises test
kjaisingh Jun 12, 2025
4cb02c9
Implemented robust welch's t-test with median/MAD
kjaisingh Jun 12, 2025
83c94c6
Revert to anderson-darling but import ksamples in RdTest now
kjaisingh Jun 12, 2025
b42ea0f
Use the cramer-von mises test implementation again, but with ksamples…
kjaisingh Jun 12, 2025
e08a8fd
Implemented robust perm test with median/mad in pclt method
kjaisingh Jun 13, 2025
8e0b27e
Correct double application of correction factor
kjaisingh Jun 13, 2025
c81eab2
Correct double application of correction factor pt2
kjaisingh Jun 13, 2025
2122624
Reverted to AD test
kjaisingh Jun 13, 2025
a209d83
Implemented CVM test
kjaisingh Jun 13, 2025
e988fcb
Used mad/median throughout instead of median/std
kjaisingh Jun 16, 2025
cd7407c
Use robust z-score V3
kjaisingh Jun 17, 2025
213f933
Reverted to original implementation + dockerfile
kjaisingh Jun 26, 2025
90e1ee7
Simple replacement of mean with median for group calculation
kjaisingh Jun 27, 2025
c3b143c
Include original code for reference
kjaisingh Jun 30, 2025
9a006e9
WIP
kjaisingh Jul 27, 2025
7dd4c1b
Merge branch 'main' into kj_rdtest_verbose
kjaisingh Jul 28, 2025
970da78
Merge branch 'main' into kj_rdtest_verbose
kjaisingh Jul 30, 2025
c8a6618
Stop tracking RdTestV0.R
kjaisingh Jul 30, 2025
4e89474
Add single sample robust z-score back to script
kjaisingh Jul 30, 2025
da5050f
Accidentally modified RdTestV2.R instead of main version
kjaisingh Jul 30, 2025
98a7b45
Remove permTS function itself, just keep twosample.pclt override
kjaisingh Jul 30, 2025
c1246cd
Minor formatting change + removed from gitignore
kjaisingh Aug 4, 2025
f3d1e2c
Removed redundant comment
kjaisingh Aug 4, 2025
c8b3729
Removed redundant comment
kjaisingh Aug 4, 2025
39341b2
Merge branch 'main' into kj_rdtest_recover_overlapped_cnvs
kjaisingh Aug 12, 2025
b4d49c9
Removed overwriting of robust correction
kjaisingh Aug 12, 2025
0164cbe
Reverted to OG implementation of perm test
kjaisingh Aug 12, 2025
cf54280
Implemented sampling of middle 80%
kjaisingh Aug 13, 2025
7235d2b
Implemented sampling of only those within 6 MAD
kjaisingh Aug 13, 2025
7d72f36
Implemented hard cutoffs
kjaisingh Aug 14, 2025
4f5b9d3
Log counts before/after dropping bg samples
kjaisingh Aug 14, 2025
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
69 changes: 64 additions & 5 deletions src/RdTest/RdTest.R
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,64 @@ for (i in RPackages)
}
}

pclt_kj <- function(scores, group) {
g <- group
bg_label <- 1
treat_label <- 0
n_control_before <- sum(g == bg_label, na.rm = TRUE)
n_treat_before <- sum(g == treat_label, na.rm = TRUE)

bg_idx <- g == bg_label
bg_scores <- scores[bg_idx]
cnvtype_opt <- getOption("rdtest_cnvtype", default = NA)
if (!is.na(cnvtype_opt) && cnvtype_opt == "DEL") {
keep_bg <- bg_scores <= 1.4
} else if (!is.na(cnvtype_opt) && cnvtype_opt == "DUP") {
keep_bg <- bg_scores >= 0.7
} else {
keep_bg <- rep(TRUE, length(bg_scores))
}
keep <- rep(TRUE, length(scores))
keep[bg_idx] <- keep_bg
if (sum(keep & bg_idx) >= 1) {
scores <- scores[keep]
group <- group[keep]
g <- g[keep]
}
n_control_after <- sum(g == bg_label, na.rm = TRUE)
n_treat_after <- sum(g == treat_label, na.rm = TRUE)

var_id <- getOption("rdtest_variant_id", default = NA)
var_start <- getOption("rdtest_variant_start", default = NA)
cat(paste0(
"RdTest permTS | CNVID=", var_id,
" | Control(before/after)=", n_control_before, "/", n_control_after,
" | Treat(before/after)=", n_treat_before, "/", n_treat_after,
"\n"
))

tab <- table(group, scores)
m <- sum(tab[2, ])
n <- length(scores)
Grp1 <- dimnames(tab)[[1]][2]
grp <- rep(0, n)
grp[group == Grp1] <- 1
T0 <- sum(scores * grp)
SSE.scores <- sum((scores - mean(scores))^2)
SSE.grp <- sum((grp - mean(grp))^2)
Z <- sqrt(n - 1) * (T0 - n * mean(scores) * mean(grp)) /
sqrt(SSE.scores * SSE.grp)
p.lte <- pnorm(Z)
p.gte <- 1 - pnorm(Z)
p.twosidedAbs <- 1 - pchisq(Z^2, 1)
p.values <- c(p.twosided = min(1, 2 * min(p.lte, p.gte)), p.lte = p.lte, p.gte = p.gte, p.twosidedAbs = p.twosidedAbs)
out <- list(p.values = p.values, Z = Z)
out
}

## Assign in namespace to trigger override
assignInNamespace("twosample.pclt", pclt_kj, ns="perm")

##build a list of command line options##
list <- structure(NA, class = "result")
"[<-.result" <- function(x, ..., value) {
Expand Down Expand Up @@ -647,9 +705,9 @@ onesamplezscore.median <- function(genotype_matrix,cnv_matrix,singlesample,cnvty
b<-create_groups(genotype_matrix, cnv_matrix)$b
##Calculate one-sided z score##
if (toupper(cnvtype) == "DEL") {
ztest.p <- pnorm((Treat - mean(Control)) / sd(Control))
ztest.p <- pnorm((Treat - median(Control)) / mad(Control, constant = 1.4826))
} else{
ztest.p <- pnorm((mean(Control) - Treat) / sd(Control))
ztest.p <- pnorm((median(Control) - Treat) / mad(Control, constant = 1.4826))
}
##Find the secondest worst p-value and record as an assement metric##
plist <- c()
Expand All @@ -661,9 +719,9 @@ onesamplezscore.median <- function(genotype_matrix,cnv_matrix,singlesample,cnvty
Treat2 <-
cnv_matrix[singlesample, column]
if (toupper(cnvtype) == "DEL") {
single.p <- pnorm((Treat2 - mean(Control2)) / sd(Control2))
single.p <- pnorm((Treat2 - median(Control2)) / mad(Control2, constant = 1.4826))
} else {
single.p <- pnorm((mean(Control2) - Treat2) / sd(Control2))
single.p <- pnorm((median(Control2) - Treat2) / mad(Control2, constant = 1.4826))
}
#store diffrent z p-value by column##
plist[i] <- single.p
Expand All @@ -689,6 +747,7 @@ twosamplezscore.median <- function(genotype_matrix,cnv_matrix,cnvtype)
Treat<-create_groups(genotype_matrix, cnv_matrix)$Treat
a<-create_groups(genotype_matrix, cnv_matrix)$a
b<-create_groups(genotype_matrix, cnv_matrix)$b
options(rdtest_cnvtype = toupper(cnvtype))
if (toupper(cnvtype) == "DEL") {
P_object <- permTS(Control, Treat, alternative = "greater", method = 'pclt')$p.value
} else{ P_object <- permTS(Control, Treat, alternative = "less", method = 'pclt')$p.value }
Expand Down Expand Up @@ -1405,4 +1464,4 @@ if (opt$denovo==FALSE) {
quote = FALSE,col.names = FALSE, row.names = FALSE,append=TRUE,sep= "\t")
}

cat("FINISHED\n")
cat("FINISHED\n")