-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patheasy_chi2.r
184 lines (147 loc) · 6.38 KB
/
easy_chi2.r
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
# setup environment
rm(list = ls())
library(data.table)
library(doParallel)
library(foreach)
source("easy_chi2_fun.r")
## set number of processor for parallel processing
numberOfProcesors <- 8
## set within group marking threshold
markThreshold <- 0.05
#set i/o ----
## input files
polySites <- readRDS("data/5feb_tem_chr1_avd.rds")
## input file names
### text output
txtOutputFile <- "data/output/5feb_tem_ezchi_c1.chi"
### r binary output
rawEzchiResults <- "data/output/5feb_tem_raw_ezchi_c1.rds"
rdsEzChiResults <- "data/output/5feb_tem_ezchi_c1.rds"
# program starts here ----
polySites$ref <- polySites$ref1
# remove extra columns ----
polySites$ref1 <- NULL
polySites$ref2 <- NULL
polySites$ref3 <- NULL
polySites$ref4 <- NULL
polySites$chrom1 <- NULL
polySites$chrom2 <- NULL
polySites$chrom3 <- NULL
polySites$chrom4 <- NULL
polySites$sumDepth1 <- NULL
polySites$sumDepth2 <- NULL
polySites$sumDepth3 <- NULL
polySites$sumDepth4 <- NULL
#dbug ----
#rowPosition <- which(polySites == 20423119, arr.ind=TRUE)[,"row"]
#polySites[6, ]
#polySite <- (polySites[1, ])
#print(dbug:on)
#dbug ----
# parallel ----
# parameters ----
nLines <- nrow(polySites)
cl <- parallel::makeCluster(numberOfProcesors)
doParallel::registerDoParallel(cl)
rawEzChiResults <- foreach( i = 1:nLines, .combine = 'rbind') %dopar% {
GetEasyChiEstimates(polySite = polySites[i, ])
}
stopCluster(cl)
saveRDS(rawEzChiResults, file = rawEzchiResults, compress = FALSE)
#let's make the matrix a data.table and make the nucleotide position a local key
ezChiResults <- as.data.table(rawEzChiResults, key = "nucPosition")
rm(rawEzChiResults)
rm(polySites)
#let's crate the x2 probs for the contingency tables
ezChiResults[ , totalProb := 1 - pchisq( q = totalChiSqr, df = totalDegFreedom ) ,
by = nucPosition ]
ezChiResults[ , group1Prob := 1 - pchisq( q = group1ChiSqr, df = group1DegFreedom ) ,
by = nucPosition ]
ezChiResults[ , group2Prob := 1 - pchisq( q = group2ChiSqr, df = group2DegFreedom ) ,
by = nucPosition ]
#estimate Benjamini
bhThreshold <- -log10( GetBenjaminiHochberThreshold(ezChiResults$totalProb))
#let's remove the sites with a global probability less than reject threshold
ezChiResults <- ezChiResults[ ezChiResults$lod > bhThreshold ]
# mark what is inconsistent within the group
ezChiResults[ , inconsistency := MarkInconsistency(chi1 = group1ChiSqr,
degFreedom1 = group1DegFreedom,
inconsistencyMark1 = "1*",
chi2 = group2ChiSqr ,
degFreedom2 = group2DegFreedom,
inconsistencyMark2 = "2*",
markThreshold = markThreshold),
by = nucPosition ]
#let's add the allele list the first character is the reference allele
ezChiResults[ , alleles := GetAllelesLabel(
nucPosition = nucPosition,
refNucleotide = refNuc,
As = As,
Cs = Cs,
Gs = Gs,
Ts = Ts,
Is = Is,
Ds = Ds),
by = nucPosition ]
saveRDS(ezChiResults, file = rdsEzChiResults, compress = FALSE)
nRemainingSites <- nrow(ezChiResults)
ezChiResults$alleles <- sprintf("%-6s", ezChiResults$alleles)
ezChiResults$alleles <- gsub(" ", "_", ezChiResults$alleles)
# Open a connection to the file
fileConn <- file(txtOutputFile, open = "wt")
# Header
header <- "SNPID,MUTATION,FREQ(ALIVE),FREQ(DEAD),LOD,HET(ALL),HET(ALIVE),HET(DEAD),FREQ(A),FREQ(C),FREQ(G),FREQ(T),FREQ(I),FREQ(D),CHISQ(ALL),CHISQ(ALIVE),CHISQ(DEAD),DF(ALL),DF(ALIVE),DF(DEAD)"
writeLines(header, fileConn)
printFormatTemp <- data.frame(nucPosition = "%10.0f",
alleles = "%6s",
group1AltAllFreq = "%8.5f",
group2AltAllFreq = "%8.5f",
lod = "%7.2f",
group1Heteroz = "%8.5f",
group2Heteroz = "%8.5f",
totalHeteroz = "%8.5f",
As = "%8.0f",
Cs = "%7.0f",
Gs = "%8.0f",
Ts = "%8.0f",
Is = "%8.0f",
Ds = "%8.0f",
group1ChiSqr = "%11.5f",
group2ChiSqr = "%10.5f",
totalChiSqr = "%10.5f",
group1DegFree = "%5.0f",
group2DegFree = "%5.0f",
totalDegFree = "%5.0f",
inconsistency = "%3s"
)
# Prepare format string
printFormat <- paste(printFormatTemp[, ], collapse = ",")
# Write each row to the file
for (i in 1:nRemainingSites) {
line <- sprintf(printFormat,
ezChiResults$nucPosition[i], #1
ezChiResults$alleles[i], #2
ezChiResults$group1AltAllFreq[i],#3
ezChiResults$group2AltAllFreq[i],#4
ezChiResults$lod[i], #5
ezChiResults$totalHeteroz[i], #6
ezChiResults$group1Heteroz[i], #7
ezChiResults$group2Heteroz[i], #8
ezChiResults$As[i], #9
ezChiResults$Cs[i], #10
ezChiResults$Gs[i], #11
ezChiResults$Ts[i], #12
ezChiResults$Is[i], #13
ezChiResults$Ds[i], #14
ezChiResults$group1ChiSqr[i], #15
ezChiResults$group2ChiSqr[i], #16
ezChiResults$totalChiSqr[i], #17
ezChiResults$group1DegFree[i], #18
ezChiResults$group2DegFree[i], #19
ezChiResults$totalDegFree[i], #20
ezChiResults$inconsistency[i] #21
)
writeLines(line, fileConn)
}
# Close the file connection
close(fileConn)