Skip to content

Commit 450e139

Browse files
committed
update nrc
1 parent d8f74b6 commit 450e139

File tree

2 files changed

+35
-12
lines changed

2 files changed

+35
-12
lines changed

DESCRIPTION

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ Description: The 'vcfpp.h' (<https://github.com/Zilong-Li/vcfpp>) provides an ea
1212
Encoding: UTF-8
1313
Depends: R (>= 3.6.0)
1414
Roxygen: list(markdown = TRUE)
15-
RoxygenNote: 7.3.1
15+
RoxygenNote: 7.3.2
1616
Suggests:
1717
knitr,
1818
rmarkdown,

R/common.R

Lines changed: 34 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -104,27 +104,50 @@ F1 <- function(a, b) {
104104

105105
## follow GLIMPSE2_concordance
106106
## None Reference Concordnace = 1 - (e0 + e1 + e2) / (e0 + e1 + e2 + m1 + m2)
107+
## = (m1 + m2) / (sum(m) - m[ref,ref])
107108
## truth\imputed
108109
## 0 1 2
109110
## 0 ignore e0 e0
110111
## 1 e1 m1 e1
111112
## 2 e2 e2 m2
112113
## a <- c(1, 2, 0, 1,1)
113-
## b <- c(1, 2, 0, 0,1)
114+
## b <- c(1, 2, 1, 1,1)
114115
## NRC(a, b)
115-
NRC <- function(a, b) {
116-
o <- table(as.vector(a), as.vector(b))
117-
## make sure table is valid
118-
if( !((nrow(o) == ncol(o)) && (nrow(o) == 3)) ) {
119-
warning("NRC should be used only for a sample with genotypes of all types, hom ref(0), het(1) and hom alt(2)")
120-
return(NA)
116+
## a <- c(1, 2, 0, 1,1)
117+
## b <- c(0, 2, 1, 1,1)
118+
## NRC(a, b)
119+
## a <- c(1, 2, 1, 1,1)
120+
## b <- c(2, 1, 1, 1,1)
121+
## NRC(a, b)
122+
## a <- c(1, 2, 1, 1,1)
123+
## b <- c(1, 1, 1, 1,1)
124+
## NRC(a, b)
125+
NRC <- function(a, b, ref = 0) {
126+
# Ensure vectors are treated as factors with all common levels
127+
all_levels <- union(unique(a), unique(b))
128+
a_fct <- factor(a, levels = all_levels)
129+
b_fct <- factor(b, levels = all_levels)
130+
tbl <- table(a_fct, b_fct)
131+
ref_char <- as.character(ref)
132+
# Get reference-reference count
133+
if (ref_char %in% rownames(tbl) && ref_char %in% colnames(tbl)) {
134+
ref_ref <- tbl[ref_char, ref_char]
135+
} else {
136+
ref_ref <- 0
137+
}
138+
139+
total_concordant <- sum(diag(tbl))
140+
non_ref_concordant <- total_concordant - ref_ref
141+
total_non_ref <- sum(tbl) - ref_ref
142+
143+
if (total_non_ref == 0) {
144+
return(NA) # Handle case with no non-reference pairs
145+
} else {
146+
return(non_ref_concordant / total_non_ref)
121147
}
122-
mismatches <- sum(c(o[1,2:3], o[2,1], o[2,3], o[3,1:2]))
123-
matches <- sum(c(o[2,2], o[3,3]))
124-
res <- mismatches / (mismatches+matches)
125-
return(1-res)
126148
}
127149

150+
128151
calculate_pse_2ways <- function(test,
129152
truth,
130153
which_snps,

0 commit comments

Comments
 (0)