[8e0848]: / common_scripts / featurematrix / compute.pairwise.R

Download this file

559 lines (431 with data), 20.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
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
regulon.feats=function(fm, genelist, cnv_annot=NULL, filter.val=3, filtertypes=NULL){
fmname=rownames(fm)
if(!is.null(filtertypes)){
fmname=fmname[!substr(fmname, 1,6)%in%filtertypes]
}
feature.gene=suppressWarnings(do.call(rbind, strsplit(fmname, ":"))[,3])
if(any(grepl(":", genelist)))genelist[grepl(":", genelist)]=do.call(rbind, strsplit(genelist[grepl(":", genelist)], ":"))[,3]
if(!is.null(cnv_annot)){
l.genes=strsplit(cnv_annot[,2], ",")
names(l.genes)=cnv_annot[,1]
st=stack(l.genes)
st=st[st[,1]%in%genelist,]
st[,2]=as.character(st[,2])
}
nested.features=function(gene, fmname, feature.gene){
direct=fmname[feature.gene%in%gene]
if(!is.null(cnv_annot))direct=c(direct, st[st[,1]%in%gene,2])
# order this to numeric, then to gexp-cnv-meth-gnab
n=unique(direct)
types=substr(n, 1, 6)
ord=c("N:GEXP","B:GEXP","B:GNAB", "N:CNVR", "B:CNVR","N:METH", "N:MIRN", "N:SAMP", "B:SAMP", "N:CLIN", "B:CLIN", "N:DRUG")
ord=ord[ord%in%types]
r=unlist(lapply(ord, function(o) n[types%in%o]))
return(r)
}
feats=lapply(genelist, nested.features, fmname, feature.gene)
# Filter if not enough data:
m=fm[rownames(fm)%in%unlist(feats),]
s=is.na(m)
m=m[!rowSums(s)==dim(m)[1]&rowSums(!s)>=filter.val, !colSums(s)==dim(m)[2]]
feats=lapply(feats, function(n)n[n%in%rownames(m)])
names(feats)=genelist
feats=feats[!unlist(lapply(feats, length))<1]
return(feats)
}
get.feat.within.region=function(INPUT, FEAT){
temp=tempfile()
temp2=tempfile()
# write out bed format temp file:
write.table(INPUT, temp, quote = F, row.names = F, col.names = F, sep="\t")
# intersect with features
system(paste0("bedtools intersect -a ",FEAT, " -b ",temp, " -wa -wb > ", temp2)) # genes in region
# read in R
d = read.delim(temp2, header=F, stringsAsFactors = F)
d[,4]=gsub(":.*.", "", d[,4])
d2=d[,c(10, 4)]
unlink(temp)
unlink(temp2)
return(d2)
}
fun.correlation.regulon=function(i, l.regulon.gene, fm, filter.val=3, method.cor="spearman"){
feats=l.regulon.gene[[i]]
genename=names(l.regulon.gene)[i]
x=data.matrix(t(as.matrix(fm[match(feats,rownames(fm)),,drop=F])))
class(x) <- "numeric"
if(dim(x)[2]<2)return(NULL) # this one did not have pairs to test
# filter if binary has less than n samples
if(any(grepl("^B:", colnames(x)))){
check=colSums(x[,grepl("^B:", colnames(x)),drop=F], na.rm = T)
x=x[,!colnames(x)%in%names(check[check<filter.val]),drop=F]
}
# filter if numeric has less than n non-zero samples
if(any(grepl("^N:", colnames(x)))){
check=colSums(x[,grepl("^N:", colnames(x)),drop=F]!=0, na.rm = T)
x=x[,!colnames(x)%in%names(check[check<filter.val]),drop=F]
}
if(dim(x)[2]<2)return(NULL) # this one did not have pairs to test
cor_rho=cor(x,use = "pairwise.complete.obs", method = method.cor)
cor_pval=cor.test.p.m(x, method = method.cor, use = "pairwise.complete.obs")
results=flat_cor_mat(cor_rho, cor_pval)
# respect original order:
results=do.call(rbind, lapply(colnames(x), function(n)results[results[,1]%in%n,]))
rownames(results)=NULL
# filter pairwise result:
results=results[!duplicated(t(apply(results[,1:2], 1, sort))),]
# add gene to cnvr:
# results[grepl(":CNVR:", results[,1]),1]=gsub(":CNVR:", paste0(":CNVR:",genename, "@"), results[grepl(":CNVR:", results[,1]),1])
# results[grepl(":CNVR:", results[,2]),2]=gsub(":CNVR:", paste0(":CNVR:",genename, "@"), results[grepl(":CNVR:", results[,2]),2])
return(results)
}
fun.correlation.extrafeatures=function(i, l.regulon.gene, fm, extrafeatures, filter.val=3, method.cor="spearman"){
feats=l.regulon.gene[[i]]
genename=names(l.regulon.gene)[i]
x=data.matrix(t(as.matrix(fm[match(feats,rownames(fm)),,drop=F])))
class(x) <- "numeric"
# filter if binary has less than n samples
if(any(grepl("^B:", colnames(x)))){
check=colSums(x[,grepl("^B:", colnames(x)),drop=F], na.rm = T)
x=x[,!colnames(x)%in%names(check[check<filter.val]),drop=F]
}
# filter if numeric has less than n non-zero samples
if(any(grepl("^N:", colnames(x)))){
check=colSums(x[,grepl("^N:", colnames(x)),drop=F]!=0, na.rm = T)
x=x[,!colnames(x)%in%names(check[check<filter.val]),drop=F]
}
# remove if in both lists
extrafeatures=extrafeatures[!extrafeatures%in%feats]
# Filter if not enough data (less than 3 not NA):
m2=fm[rownames(fm)%in%extrafeatures,]
s=is.na(m2)
m2=m2[!rowSums(s)==dim(m2)[2]&rowSums(!s)>=filter.val, ]
y=data.matrix(t(as.matrix(m2)))
class(y) <- "numeric"
y=cbind(x,y)
# filter if binary has less than n 3 samples
if(any(grepl("^B:", colnames(y)))){
check=colSums(y[,grepl("^B:", colnames(y)),drop=F], na.rm = T)
y=y[,!colnames(y)%in%names(check[check<filter.val])]
}
# filter if numeric has less than n non-zero samples
if(any(grepl("^N:", colnames(y)))){
check=colSums(y[,grepl("^N:", colnames(y)),drop=F]!=0, na.rm = T)
y=y[,!colnames(y)%in%names(check[check<filter.val]),drop=F]
}
if(dim(x)[2]==0|dim(y)[2]==0)return(NULL) # no pairs to test
cor_rho=cor(x,y,use = "pairwise.complete.obs", method = method.cor)
# compute cor pval
cor_pval=t(apply(x, 2, function(v1){
apply(y, 2, function(v2){
if(sum(complete.cases(cbind(v1, v2)))<filter.val)return(NA)
cor.test.p(v1, v2, method = method.cor, use = "pairwise.complete.obs")
})
}))
results=flat_cor_mat(data.matrix(cor_rho), data.matrix(cor_pval))
results=do.call(rbind, lapply(colnames(x), function(n)results[results[,1]%in%n,]))
rownames(results)=NULL
# remove regulonfeatures, they are coming from another test:
results=results[!(results[,1]%in%colnames(x)&results[,2]%in%colnames(x)),]
# filter pairwise result:
results=results[!duplicated(t(apply(results[,1:2], 1, sort))),]
# add gene to cnvr: BUG for wilcox
# results[grepl(":CNVR:", results[,1]),1]=gsub(":CNVR:", paste0(":CNVR:",genename, "@"), results[grepl(":CNVR:", results[,1]),1])
return(results)
}
p.adjust.datatype=function(results, adjust.method="BH", log10=F, prettyNumbers=T, orderdata=T){
# number of features tested:
datatypes=suppressWarnings(do.call(rbind, strsplit(results[,2], ":"))[,2]) # sometimes more than 3 columns
datatypes2=suppressWarnings(do.call(rbind, strsplit(results[,1], ":"))[,2])
datatypes3=apply(cbind(datatypes2,datatypes),1,paste, collapse=":")
datatypes=datatypes3
results=results[order(datatypes),]
datatypes=datatypes[order(datatypes)]
vals=seq(datatypes)
vals=unlist(lapply(unique(datatypes), function(d)p.adjust(results$p[datatypes%in%d], method = adjust.method)))
if(log10){
vals=abs(log10(vals))
}
if(prettyNumbers){
vals=prettyNum(signif(vals,2))
results[,3]=prettyNum(signif(results[,3],2))
results[,4]=prettyNum(signif(results[,4],2))
}else{
vals=as.numeric(vals)
results[,3]=as.numeric(results[,3])
results[,4]=as.numeric(results[,4])
}
results$adj.p=vals
results$signifCode=""
results$signifCode[as.numeric(vals)<0.1]="."
results$signifCode[as.numeric(vals)<0.05]="*"
results$signifCode[as.numeric(vals)<0.01]="**"
results$signifCode[as.numeric(vals)<0.001]="***"
results$signifCode[as.numeric(vals)<0.0001]="****"
results$datapairs=datatypes
if(orderdata)results=results[order(results[,1], datatypes, -as.numeric(results$cor)>0, as.numeric(results$adj.p), decreasing = F),]
return(results)
}
ft.alt <- function(a, b, c, d, alternative="greater") {
as.numeric(fisher.test(matrix(c(a, b, c, d), 2), alternative = alternative)$p.value)
}
fisher.test.pwpw=function(results, find, fm, fisher.alternative, cores){
results.o=results
# remove gene anno if CNV type, to match with FM
results.o[,1]=gsub("CNVR:.*.@", "CNVR:", results.o[,1])
results.o[,2]=gsub("CNVR:.*.@", "CNVR:", results.o[,2])
featsA=as.list(as.data.frame(t(fm[results.o[find,1],])))
featsB=as.list(as.data.frame(t(fm[results.o[find,2],])))
grid=data.frame(do.call(rbind, mclapply(seq(find), function(i)c(table(featsA[[i]], featsB[[i]])), mc.cores=cores)))
colnames(grid)=c("a", "b", "c", "d")
if(fisher.alternative=="two.sided"){
p.ft <- with(grid, HighSpeedStats::ultrafastfet(a, b, c, d))
results$p[find]=p.ft
}else{
p.ft <- with(grid, mapply(ft.alt, a, b, c, d, fisher.alternative))
results$p[find]=p.ft
}
results$test.method[find]=paste0("Fisher´s exact (", fisher.alternative, ")")
return(results)
}
# pairwise.correlation=function(l.regulon.gene, fm, extrafeatures=NULL, filter.val=3, cores=5, adjust.method="BH", method.cor="spearman", prettyNumbers=T, use.fisher=T, fisher.alternative="two.sided"){
#
# #****************************** Test within regulon ******************************
# r=parallel::mclapply(seq(l.regulon.gene),fun.correlation.regulon,l.regulon.gene, fm, filter.val=filter.val,method.cor=method.cor, mc.cores=cores)
# results=do.call(rbind, r)
# results=results[!is.na(results$p),,drop=F]
#
# if(!is.null(results)){
# results$test.method=paste(method.cor, "correlation")
# results$test.group="gene.features"
#
# # if any pairs are binary-binary, add fisher test p-value
# fa=substr(results[,1], 0, 1)=="B"
# fb=substr(results[,2], 0, 1)=="B"
# find=which(fa&fb)
# findl=fa&fb
#
# if(use.fisher&any(findl)){
# results=fisher.test.pwpw(results, find, fm, fisher.alternative, cores)
# results1=p.adjust.datatype(results[findl,], orderdata=T, adjust.method=adjust.method, prettyNumbers=prettyNumbers)
# results2=p.adjust.datatype(results[!findl,], orderdata=T, adjust.method=adjust.method, prettyNumbers=prettyNumbers)
# results=rbind(results1, results2)
# }else{
# results=p.adjust.datatype(results, orderdata=T, adjust.method=adjust.method, prettyNumbers=prettyNumbers)
# }
# }
# #*********************************************************************************
#
# # Test against extrafeatures if they exist:
# if(!is.null(extrafeatures)){
# r=parallel::mclapply(seq(l.regulon.gene),fun.correlation.extrafeatures, l.regulon.gene, fm, extrafeatures=extrafeatures, filter.val=filter.val,method.cor=method.cor, mc.cores=cores)
# results.e=do.call(rbind, r)
# results.e=results.e[!is.na(results.e$p),]
# results.e$test.method=paste(method.cor, "correlation")
# results.e$test.group="extra.features"
#
# # if any pairs are binary-binary, add fisher test p-value
# fa=substr(results.e[,1], 0, 1)=="B"
# fb=substr(results.e[,2], 0, 1)=="B"
# find=which(fa&fb)
# findl=fa&fb
#
# if(use.fisher&any(findl)){
# results.e=fisher.test.pwpw(results.e, find,fm, fisher.alternative, cores)
# results1=p.adjust.datatype(results.e[findl,], orderdata=T, adjust.method=adjust.method, prettyNumbers=prettyNumbers)
# if(sum(!findl)>0){
# results2=p.adjust.datatype(results.e[!findl,], orderdata=T, adjust.method=adjust.method, prettyNumbers=prettyNumbers)
# results.e=rbind(results1, results2)
# }else{
# results.e=results1
# }
# }else{
# results.e=p.adjust.datatype(results.e, orderdata=T, adjust.method=adjust.method, prettyNumbers=prettyNumbers)
# }
# results=rbind(results, results.e)
# }
#
# if(adjust.method=="BH")adj="FDR"
# if(adjust.method=="bonferroni")adj="FWER"
# colnames(results)[7]=adj
# results[grepl("Fisher", results$test.method),3]=""
#
# # order columnts:
# cols=c("featureA", "featureB", "cor", "p", adj, "signifCode", "test.method", "test.group", "datapairs")
# results=results[,cols]
# return(results)
# }
filter.pairwise.res=function(results, filter.table=NULL, FDR=0.1){
if(is.null(filter.table)){
filter.table=data.frame("pair"=unique(results$datapairs), "FDR"=rep(FDR, length(unique(results$datapairs))), stringsAsFactors = F)
type1=gsub(":.*.", "", filter.table[,1])
type2=gsub(".*.:", "", filter.table[,1])
# usually fewer features
filter.table$FDR[type1=="GNAB"|type2=="GNAB"]=0.25 # mutations can be interesting, high P-value kept
filter.table$FDR[type1=="SAMP"&type2=="SAMP"]=0.1 # these can be very different
filter.table$FDR[type1=="CLIN"&type2=="CLIN"]=0.1 # these can be very different
# exclude, not usually meaningful
filter.table$FDR[type1=="CNVR"&type2=="CNVR"]=0 # remove, highly correlated
filter.table$FDR[type1=="METH"&type2=="METH"]=0 # remove, highly correlated
# SAMP special rules:
filter.table$FDR[type1=="SAMP"&type2=="GEXP"]=1e-30
filter.table$FDR[type1=="SAMP"&type2=="MIRN"]=1e-30
filter.table$FDR[type1=="SAMP"&type2=="METH"]=1e-8
print("default table used: ")
print(filter.table)
}
pairs=results$datapairs
p=unique(pairs)
# make sure all pairs are found in filter.table
if(!all(p%in%filter.table[,1]))stop(paste("Not all pairs were found: ", p[!p%in%filter.table[,1],]))
filter.table=filter.table[match(p, filter.table[,1]),]
# p-value per pairs:
vals=do.call(rbind, lapply(seq(p), function(i){
r=results[pairs%in%p[i],]
filt=as.numeric(r[,5])<as.numeric(filter.table[i,2])
r=r[filt,]
}))
return(vals)
}
pairwise.correlation=function(l.regulon.gene, fm, extrafeatures=NULL, filter.val=3, cores=5, adjust.method="BH", method.cor="spearman", prettyNumbers=T, use.fisher=T, use.wilcox=T, fisher.alternative="two.sided", wilcox.alternative="two.sided"){
#****************************** Test within regulon ******************************
r=parallel::mclapply(seq(l.regulon.gene),fun.correlation.regulon,l.regulon.gene, fm, filter.val=filter.val,method.cor=method.cor, mc.cores=cores)
results=do.call(rbind, r)
results=results[!is.na(results$p),,drop=F]
if(!is.null(results)){
results$test.method=paste(method.cor, "correlation")
results$test.group="gene.features"
# if any pairs are binary-binary, add fisher test p-value
fa=substr(results[,1], 0, 1)=="B"
fb=substr(results[,2], 0, 1)=="B"
find=which(fa&fb)
findl=fa&fb
# if any pairs are binary-numeric, add wilcoxon test p-value
fc=substr(results[,1], 0, 1)=="N"
fd=substr(results[,2], 0, 1)=="B"
fe=substr(results[,1], 0, 1)=="B"
ff=substr(results[,2], 0, 1)=="N"
findw=fc&fd|fe&ff
# compute p-value depending on test type:
if(use.fisher&any(findl)){
# update B vs B tests:
results=fisher.test.pwpw(results, find, fm, fisher.alternative, cores)
}
# compute p-value depending on test type:
if(use.wilcox&any(findw)) {
# update B vs N tests:
results=test.feats.wilcox(features = results, rows = findw, data = fm, wilcox.alternative = wilcox.alternative, cores = cores)
}
# go through each test and adjust p-value per data type:
results=do.call(rbind, lapply(unique(results$test.method), function(type){
p.adjust.datatype(results[results$test.method%in%type,], orderdata=T, adjust.method=adjust.method, prettyNumbers=prettyNumbers)
}))
}
#*********************************************************************************
# Test against extrafeatures if they exist:
if(!is.null(extrafeatures)){
r=parallel::mclapply(seq(l.regulon.gene),fun.correlation.extrafeatures, l.regulon.gene, fm, extrafeatures=extrafeatures, filter.val=filter.val,method.cor=method.cor, mc.cores=cores)
results.e=do.call(rbind, r)
results.e=results.e[!is.na(results.e$p),]
results.e$test.method=paste(method.cor, "correlation")
results.e$test.group="extra.features"
# if any pairs are binary-binary, add fisher test p-value
fa=substr(results.e[,1], 0, 1)=="B"
fb=substr(results.e[,2], 0, 1)=="B"
find=which(fa&fb)
findl=fa&fb
# if any pairs are binary-numeric, add wilcoxon test p-value
fc=substr(results.e[,1], 0, 1)=="N"
fd=substr(results.e[,2], 0, 1)=="B"
fe=substr(results.e[,1], 0, 1)=="B"
ff=substr(results.e[,2], 0, 1)=="N"
findw=fc&fd|fe&ff
# compute p-value depending on test type:
if(use.fisher&any(findl)){
# update B vs B tests:
results.e=fisher.test.pwpw(results.e, find, fm, fisher.alternative, cores)
}
# compute p-value depending on test type:
if(use.wilcox&any(findw)) {
# update B vs N tests:
results.e=test.feats.wilcox(features = results.e, rows = findw, data = fm, wilcox.alternative = wilcox.alternative, cores = cores)
}
# go through each test and adjust p-value per data type
results.e=do.call(rbind, lapply(unique(results.e$test.method), function(type){
p.adjust.datatype(results.e[results.e$test.method%in%type,], orderdata=T, adjust.method=adjust.method, prettyNumbers=prettyNumbers)
}))
results=rbind(results, results.e)
}
if(adjust.method=="BH")adj="FDR"
if(adjust.method=="bonferroni")adj="FWER"
colnames(results)[7]=adj
results[grepl("Fisher", results$test.method),3]=""
# order columnts:
cols=c("featureA", "featureB", "cor", "p", adj, "signifCode", "test.method", "test.group", "datapairs")
results=results[,cols]
return(results)
}
filter.pairwise.res=function(results, filter.table=NULL, FDR=0.1){
if(is.null(filter.table)){
filter.table=data.frame("pair"=unique(results$datapairs), "FDR"=rep(FDR, length(unique(results$datapairs))), stringsAsFactors = F)
type1=gsub(":.*.", "", filter.table[,1])
type2=gsub(".*.:", "", filter.table[,1])
# usually fewer features
filter.table$FDR[type1=="GNAB"|type2=="GNAB"]=0.25 # mutations can be interesting, high P-value kept
filter.table$FDR[type1=="SAMP"&type2=="SAMP"]=0.1 # these can be very different
filter.table$FDR[type1=="CLIN"&type2=="CLIN"]=0.1 # these can be very different
# exclude, not usually meaningful
filter.table$FDR[type1=="CNVR"&type2=="CNVR"]=0 # remove, highly correlated
filter.table$FDR[type1=="METH"&type2=="METH"]=0 # remove, highly correlated
# SAMP special rules:
filter.table$FDR[type1=="SAMP"&type2=="GEXP"]=1e-30
filter.table$FDR[type1=="SAMP"&type2=="MIRN"]=1e-30
filter.table$FDR[type1=="SAMP"&type2=="METH"]=1e-8
print("default table used: ")
print(filter.table)
}
pairs=results$datapairs
p=unique(pairs)
# make sure all pairs are found in filter.table
if(!all(p%in%filter.table[,1]))stop(paste("Not all pairs were found: ", p[!p%in%filter.table[,1],]))
filter.table=filter.table[match(p, filter.table[,1]),]
# p-value per pairs:
vals=do.call(rbind, lapply(seq(p), function(i){
r=results[pairs%in%p[i],]
filt=as.numeric(r[,5])<as.numeric(filter.table[i,2])&!is.na(r[,5])
r=r[filt,]
}))
return(vals)
}
test.feats.wilcox <- function(features, rows=NA, data, wilcox.alternative="two.sided", cores=1) {
if(length(rows)>=1) {
pairs <- features[rows,]
} else pairs <- features
p=unlist(parallel::mclapply(1:nrow(pairs), function(i){
binary=substr(pairs[i,2], 0, 1)=="B"
if(binary) {
x <- as.numeric(data[pairs[i,1],])
y <- as.logical(as.numeric(data[pairs[i,2],]))
}else {
x <- as.numeric(data[pairs[i,2],])
y <- as.logical(as.numeric(data[pairs[i,1],]))
}
a <- vector()
b <- vector()
for(j in 1:length(y)) {
if(is.na(y[j])) next
if(y[j]) a <- c(a, x[j])
if(!y[j]) b <- c(b, x[j])
}
if((is.numeric(a) && is.numeric(b))&&(length(a)>0&&length(b>0))&&sum(is.na(a))!=length(a)&&sum(is.na(b))!=length(b)) {
p <- wilcox.test(a,b, alternative = wilcox.alternative)$p.value
} else {
p=NA # not enough data
}
return(p)
}, mc.cores=cores))
pairs$test.method=paste0("Wilcoxon test (", wilcox.alternative, ")")
pairs$p<- as.numeric(as.character(p))
# return original table but with updated p and test.method
features[rows,]=pairs
return(features)
}