-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathParallel_NANUQ.R
More file actions
228 lines (221 loc) · 7.1 KB
/
Parallel_NANUQ.R
File metadata and controls
228 lines (221 loc) · 7.1 KB
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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
Parallel_quartetTable_byTree<-function (trees, taxonnames = NULL,
epsilon = 0, random = 0,
parallel=FALSE,RAM_Gigs=1.0) {
if (random < 0)
stop("Parameter 'random' must be non-negative.")
random = round(random)
if (is.null(taxonnames)) {
taxonnames = trees[[1]]$tip.label
message("Using taxa that appear on first gene tree.")
}
taxonnames = sort(taxonnames)
nt = length(trees)
N = length(taxonnames)
if (random > 0)
M = random
else M = choose(N, 4)
Q = matrix(0, M, N + 4)
qnames = c("12|34", "13|24", "14|23", "1234")
colnames(Q) = c(taxonnames, qnames)
warnMissing = 0
if (random == 0) {
m = 0
for (i in 1:(N - 3)) {
for (j in (i + 1):(N - 2)) {
for (k in (j + 1):(N - 1)) {
for (l in (k + 1):N) {
m = m + 1
Q[m, c(i, j, k, l)] = 1
}
}
}
}
}
else {
i = 1
while (i <= random) {
q = sample(N, size = 4, replace = FALSE)
row = integer(N)
row[q] = 1
j = 1
while (j < i) {
if (identical(Q[j, 1:N], row))
j = i + 1
else j = j + 1
}
if (j == i) {
Q[i, 1:N] = row
i = i + 1
}
}
}
message("Counting occurrences of displayed quartets for ",
M, " four-taxon subsets of ", N, " taxa across ", nt,
" gene trees.")
start_time <- Sys.time()
if(parallel==TRUE){
options(future.globals.maxSize = RAM_Gigs * 1024^3) #1024^3 = ca. 1G RAM
fopts <- options()[c("future.globals.maxSize")]
array.out<-future_sapply(1:nt,FUN=function(numt,Q) {
## Set 'fopts' options (here fopts is a global variable)
options(fopts)
## Show that those options are set
cat(sprintf("test = %s\n", getOption("test")))
t = trees[[numt]]
if (is.null(t$edge.length)) {
t = compute.brlen(t, 1)
}
else {
if (sum(t$edge.length < 0) > 0)
stop("Error: Negative branch length in tree")
}
zeros = which(t$edge.length <= epsilon)
t$edge.length[] = 1
t$edge.length[zeros] = 0
d = cophenetic.phylo(t)
for (m in 1:M) {
tax = as.character(taxonnames[which(Q[m, 1:N] ==
1)])
if (all(tax %in% colnames(d))) {
a = d[tax[1], tax[2]] + d[tax[3], tax[4]]
b = d[tax[1], tax[3]] + d[tax[2], tax[4]]
c = d[tax[1], tax[4]] + d[tax[2], tax[3]]
z = sort(c(a, b, c))
if (z[1] == z[2]) {
Q[m, "1234"] = Q[m, "1234"] + 1
}
else {
if (z[1] == a) {
Q[m, "12|34"] = Q[m, "12|34"] + 1
}
else {
if (z[1] == b) {
Q[m, "13|24"] = Q[m, "13|24"] + 1
}
else {
Q[m, "14|23"] = Q[m, "14|23"] + 1
}
}
}
}
else warnMissing = 1
}
return(Q[,c("12|34", "13|24", "14|23", "1234")])
# return(Q)
},Q=Q,simplify = "array")
}else{
array.out<-sapply(1:nt,FUN=function(numt,Q) {
t = trees[[numt]]
if (is.null(t$edge.length)) {
t = compute.brlen(t, 1)
}
else {
if (sum(t$edge.length < 0) > 0)
stop("Error: Negative branch length in tree")
}
zeros = which(t$edge.length <= epsilon)
t$edge.length[] = 1
t$edge.length[zeros] = 0
d = cophenetic.phylo(t)
for (m in 1:M) {
tax = as.character(taxonnames[which(Q[m, 1:N] ==
1)])
if (all(tax %in% colnames(d))) {
a = d[tax[1], tax[2]] + d[tax[3], tax[4]]
b = d[tax[1], tax[3]] + d[tax[2], tax[4]]
c = d[tax[1], tax[4]] + d[tax[2], tax[3]]
z = sort(c(a, b, c))
if (z[1] == z[2]) {
Q[m, "1234"] = Q[m, "1234"] + 1
}
else {
if (z[1] == a) {
Q[m, "12|34"] = Q[m, "12|34"] + 1
}
else {
if (z[1] == b) {
Q[m, "13|24"] = Q[m, "13|24"] + 1
}
else {
Q[m, "14|23"] = Q[m, "14|23"] + 1
}
}
}
}
else warnMissing = 1
}
return(Q[,c("12|34", "13|24", "14|23", "1234")])
# return(Q)
},Q=Q,simplify = "array")
}
array.summed<-rowSums(array.out, dims = 2)
Q<-cbind(Q[,-which(names(as.data.frame(Q))%in%qnames)],array.summed)
# Q<-array.out
if (warnMissing == 1)
warning("Some taxa missing from some trees.")
if ((N > 4) && (sum(rowSums(Q[, qnames, drop = FALSE]) ==
0) > 0))
warning("Some 4-taxon set not present on any tree.")
if (sum(Q[, "1234"]) > 0)
warning("Some quartets unresolved.")
current_time = Sys.time()
elapsedTime = as.numeric(difftime(current_time, start_time,
units = "mins"))
# if (elapsedTime > 15) {
message("Time to process quartets on gene trees was ",
elapsedTime, " minutes.")
# }
return(Q)
}
### implement parallel quartetTable in NANUQ
Parallel_NANUQ<-function (genedata, outfile = "NANUQdist", alpha = 0.05, beta = 0.95,
taxanames = NULL, plot = TRUE, RAM_Gigs = 1.0)
{
if (!(is.numeric(alpha) && is.numeric(beta))) {
stop("Critical values alpha and beta must be numeric.")
}
if ("matrix" %in% class(genedata)) {
pTable = genedata
if (!is.null(taxanames)) {
message("Ignoring argument taxanames since genedata supplied as quartet table.")
}
}
else {
if ("multiPhylo" %in% class(genedata)) {
genetrees = genedata
}
else {
if ("character" %in% class(genedata)) {
genetrees <- read.tree(genedata)
message(paste("Read", length(genetrees), "gene trees from file."))
}
else {
stop("Data must be supplied as an object of type multiPhylo, character, or matrix.")
}
}
if (is.null(taxanames)) {
taxanames = genetrees[[1]]$tip.label
}
taxanames = sort(taxanames)
if (length(taxanames) <= 25) {
namelist = paste0(taxanames, collapse = ", ")
}
else {
namelist = paste0(paste0(taxanames[1:25], collapse = ", "),
",...(see output table for full list)")
}
message("Analyzing ", length(taxanames), " taxa: ",
namelist)
pTable = Parallel_quartetTable_byTree(genetrees, taxonnames=taxanames,
parallel=TRUE,RAM_Gigs = RAM_Gigs)
pTable = quartetTableResolved(pTable, omit = FALSE)
}
if (!("p_T3" %in% colnames(pTable))) {
pTable = quartetTreeTestInd(pTable, model = "T3", lambda = 0)
}
if (!("p_star" %in% colnames(pTable))) {
pTable = quartetStarTestInd(pTable)
}
NANUQdist(pTable, outfile, alpha, beta, plot)
invisible(pTable)
}