-
Notifications
You must be signed in to change notification settings - Fork 5
/
internal-functions-samr-adapted.R
2238 lines (2205 loc) · 70.4 KB
/
internal-functions-samr-adapted.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
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
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
# This script contains customized versions of functions found in the samr
# package. This is necessary because samr seems to have been abandoned, so an
# upstream collaboration doesn't seem possible at the time of writing.
# ATTENTION: The source code in this file is licensed under LGPL-3.
# ==============================================================================
# Constants
# ==============================================================================
samr.const.twoclass.unpaired.response <- "Two class unpaired"
samr.const.twoclass.paired.response <- "Two class paired"
samr.const.oneclass.response <- "One class"
samr.const.quantitative.response <- "Quantitative"
samr.const.multiclass.response <- "Multiclass"
samr.const.twoclass.unpaired.timecourse.response <-
"Two class unpaired timecourse"
samr.const.twoclass.paired.timecourse.response <- "Two class paired timecourse"
samr.const.oneclass.timecourse.response <- "One class timecourse"
samr.const.survival.response <- "Survival"
samr.const.patterndiscovery.response <- "Pattern discovery"
# ==============================================================================
# Functions
# ==============================================================================
#' @title Significance analysis of microarrays
#' @description This function is an adaptation of `samr::samr`
#' @param data Data object with components x- p by n matrix of features, one
#' observation per column (missing values allowed); y- n-vector of outcome
#' measurements; censoring.status- n-vector of censoring censoring.status
#' (1= died or event occurred, 0=survived, or event was censored), needed for a
#' censored survival outcome
#' @param resp.type Problem type: "Quantitative" for a continuous parameter
#' (Available for both array and sequencing data); "Two class unpaired" (for
#' both array and sequencing data); "Survival" for censored survival outcome
#' (for both array and sequencingdata); "Multiclass": more than 2 groups (for
#' both array and sequencing data); "One class" for a single group (only for
#' array data); "Two class paired" for two classes with paired observations
#' (for both array and sequencing data); "Two class unpaired timecourse" (only
#' for array data), "One class time course" (only for array data),
#' "Two class.paired timecourse" (only for array data), or "Pattern discovery"
#' (only for array data)
#' @param assay.type Assay type: "array" for microarray data, "seq" for counts
#' from sequencing
#' @param s0 Exchangeability factor for denominator of test statistic; Default
#' is automatic choice. Only used for array data.
#' @param s0.perc Percentile of standard deviation values to use for s0; default
#' is automatic choice; -1 means s0=0 (different from s0.perc=0, meaning
#' s0=zeroeth percentile of standard deviation values= min of sd values.
#' Only used for array data.
#' @param nperms Number of permutations used to estimate false discovery rates
#' @param center.arrays Should the data for each sample (array) be median
#' centered at the outset? Default =FALSE. Only used for array data.
#' @param testStatistic Test statistic to use in two class unpaired case.Either
#' "standard" (t-statistic) or ,"wilcoxon" (Two-sample wilcoxon or Mann-Whitney
#' test). Only used for array data.
#' @param time.summary.type Summary measure for each time course: "slope", or
#' "signed.area"). Only used for array data.
#' @param regression.method Regression method for quantitative case: "standard",
#' (linear least squares) or "ranks" (linear least squares on ranked data).
#' Only used for array data.
#' @param return.x Should the matrix of feature values be returned? Only useful
#' for time course data, where x contains summaries of the features over time.
#' Otherwise x is the same as the input data
#' @param knn.neighbors Number of nearest neighbors to use for imputation of
#' missing features values. Only used for array data.
#' @param random.seed Optional initial seed for random number generator
#' (integer)
#' @param nresamp For assay.type="seq", number of resamples used to construct
#' test statistic. Default 20. Only used for sequencing data.
#' @param nresamp.perm For assay.type="seq", number of resamples used to
#' construct test statistic for permutations. Default is equal to nresamp and it
#' must be at most nresamp. Only used for sequencing data.
#' @param xl.mode Used by Excel interface
#' @param xl.time Used by Excel interface
#' @param xl.prevfit Used by Excel interface
#' @importFrom impute impute.knn
sammy <- function(
data, resp.type = c(
"Quantitative", "Two class unpaired",
"Survival", "Multiclass", "One class", "Two class paired",
"Two class unpaired timecourse", "One class timecourse",
"Two class paired timecourse", "Pattern discovery"
), assay.type = c(
"array",
"seq"
), s0 = NULL, s0.perc = NULL, nperms = 100, center.arrays = FALSE,
testStatistic = c("standard", "wilcoxon"), time.summary.type = c(
"slope",
"signed.area"
), regression.method = c("standard", "ranks"),
return.x = FALSE, knn.neighbors = 10, random.seed = NULL,
nresamp = 20, nresamp.perm = NULL, xl.mode = c(
"regular",
"firsttime", "next20", "lasttime"
), xl.time = NULL, xl.prevfit = NULL) {
this.call <- match.call()
resp.type.arg <- match.arg(resp.type)
assay.type <- match.arg(assay.type)
xl.mode <- match.arg(xl.mode)
set.seed(random.seed)
if (is.null(nresamp.perm)) nresamp.perm <- nresamp
nresamp.perm <- min(nresamp, nresamp.perm)
if (xl.mode == "regular" | xl.mode == "firsttime") {
x <- NULL
xresamp <- NULL
ttstar0 <- NULL
evo <- NULL
ystar <- NULL
sdstar.keep <- NULL
censoring.status <- NULL
pi0 <- NULL
stand.contrasts <- NULL
stand.contrasts.star <- NULL
stand.contrasts.95 <- NULL
foldchange <- NULL
foldchange.star <- NULL
perms <- NULL
permsy <- NULL
eigengene <- NULL
eigengene.number <- NULL
testStatistic <- match.arg(testStatistic)
time.summary.type <- match.arg(time.summary.type)
regression.method <- match.arg(regression.method)
x <- data$x
y <- data$y
argy <- y
if (!is.null(data$eigengene.number)) {
eigengene.number <- data$eigengene.number
}
if (sum(is.na(x)) > 0) {
x <- impute.knn(x, k = knn.neighbors)
if (!is.matrix(x)) {
x <- x$data
}
}
are.blocks.specified <- FALSE
cond <- resp.type %in% c(
"One class", "Two class unpaired timecourse",
"One class unpaired timecourse", "Two class paired timecourse",
"Pattern discovery"
)
if (assay.type == "seq" & cond) {
stop(paste("Resp.type=", resp.type, " not allowed when assay.type='seq'"))
}
if (assay.type == "seq" & min(x) < 0) {
stop(paste("Negative values not allowed when assay.type='seq'"))
}
if (assay.type == "seq" & (sum(x %% 1 != 0) != 0)) {
stop("Non-integer values not alled when assay.type='seq'")
}
if (assay.type == "seq" & center.arrays) {
stop(paste("Centering not allowed when assay.type='seq'"))
}
if (assay.type == "seq" & regression.method == "ranks") {
stop(paste("regression.method==ranks not allowed when assay.type='seq'"))
}
if (center.arrays) {
x <- scale(x, center = apply(x, 2, median), scale = FALSE)
}
depth <- rep(NA, ncol(x))
if (assay.type == "seq") {
message("Estimating sequencing depths...")
depth <- samr.estimate.depth(x)
message("Resampling to get new data matrices...")
xresamp <- resa(x, depth, nresamp = nresamp)
}
if (resp.type == samr.const.twoclass.unpaired.response) {
if (substring(y[1], 2, 6) == "Block" | substring(
y[1],
2, 6
) == "block") {
junk <- parse.block.labels.for.2classes(y)
y <- junk$y
blocky <- junk$blocky
are.blocks.specified <- TRUE
}
}
if (resp.type == samr.const.twoclass.unpaired.response |
resp.type == samr.const.twoclass.paired.response |
resp.type == samr.const.oneclass.response |
resp.type == samr.const.quantitative.response |
resp.type == samr.const.multiclass.response
) {
y <- as.numeric(y)
}
sd.internal <- NULL
if (resp.type == samr.const.twoclass.unpaired.timecourse.response |
resp.type == samr.const.twoclass.paired.timecourse.response |
resp.type == samr.const.oneclass.timecourse.response) {
junk <- parse.time.labels.and.summarize.data(
x, y,
resp.type, time.summary.type
)
y <- junk$y
x <- junk$x
sd.internal <- sqrt(rowMeans(junk$sd^2))
if (min(table(y)) == 1) {
warning(
"Only one timecourse in one or more classes;\n",
"SAM plot and FDRs will be unreliable;",
"only gene scores are informative"
)
}
}
if (resp.type == samr.const.twoclass.unpaired.timecourse.response) {
resp.type <- samr.const.twoclass.unpaired.response
}
if (resp.type == samr.const.twoclass.paired.timecourse.response) {
resp.type <- samr.const.twoclass.paired.response
}
if (resp.type == samr.const.oneclass.timecourse.response) {
resp.type <- samr.const.oneclass.response
}
stand.contrasts <- NULL
stand.contrasts.95 <- NULL
if (resp.type == samr.const.survival.response) {
censoring.status <- data$censoring.status
}
check.format(
y,
resp.type = resp.type, censoring.status = censoring.status
)
if (resp.type == samr.const.quantitative.response & regression.method ==
"ranks") {
y <- rank(y)
x <- t(apply(x, 1, rank))
}
n <- nrow(x)
sd <- NULL
numer <- NULL
if (resp.type == samr.const.twoclass.unpaired.response &
testStatistic == "standard" & assay.type == "array") {
init.fit <- ttest.func(x, y, sd = sd.internal)
numer <- init.fit$numer
sd <- init.fit$sd
}
if (resp.type == samr.const.twoclass.unpaired.response &
testStatistic == "wilcoxon" & assay.type == "array") {
init.fit <- wilcoxon.func(x, y)
numer <- init.fit$numer
sd <- init.fit$sd
}
if (resp.type == samr.const.oneclass.response & assay.type ==
"array") {
init.fit <- onesample.ttest.func(x, y, sd = sd.internal)
numer <- init.fit$numer
sd <- init.fit$sd
}
if (resp.type == samr.const.twoclass.paired.response &
assay.type == "array") {
init.fit <- paired.ttest.func(x, y, sd = sd.internal)
numer <- init.fit$numer
sd <- init.fit$sd
}
if (resp.type == samr.const.survival.response & assay.type ==
"array") {
init.fit <- cox.func(x, y, censoring.status)
numer <- init.fit$numer
sd <- init.fit$sd
}
if (resp.type == samr.const.multiclass.response & assay.type ==
"array") {
init.fit <- multiclass.func(x, y)
numer <- init.fit$numer
sd <- init.fit$sd
}
if (resp.type == samr.const.quantitative.response & assay.type ==
"array") {
init.fit <- quantitative.func(x, y)
numer <- init.fit$numer
sd <- init.fit$sd
}
if (resp.type == samr.const.patterndiscovery.response &
assay.type == "array") {
init.fit <- patterndiscovery.func(x)
numer <- init.fit$numer
sd <- init.fit$sd
}
if ((resp.type == samr.const.quantitative.response &
(testStatistic == "wilcoxon" | regression.method ==
"ranks" & assay.type == "array") | resp.type ==
samr.const.patterndiscovery.response) | resp.type ==
samr.const.twoclass.unpaired.response & assay.type ==
"array" & testStatistic == "wilcoxon" | (nrow(x) <
500) & is.null(s0) & is.null(s0.perc)) {
s0 <- quantile(sd, 0.05)
s0.perc <- 0.05
}
if (is.null(s0) & assay.type == "array") {
if (!is.null(s0.perc)) {
if ((s0.perc != -1 & s0.perc < 0) | s0.perc >
100) {
stop(
"Illegal value for s0.perc: must be between 0 and 100, ",
"or equal\nto (-1) (meaning that s0 should be set to zero)"
)
}
if (s0.perc == -1) {
s0 <- 0
}
if (s0.perc >= 0) {
s0 <- quantile(init.fit$sd, s0.perc / 100)
}
}
if (is.null(s0.perc)) {
s0 <- est.s0(init.fit$tt, init.fit$sd)$s0.hat
s0.perc <- 100 * sum(init.fit$sd < s0) / length(init.fit$sd)
}
}
if (assay.type == "seq") {
s0 <- 0
s0.perc <- 0
}
if (resp.type == samr.const.twoclass.unpaired.response &
testStatistic == "standard" & assay.type == "array") {
tt <- ttest.func(x, y, s0 = s0, sd = sd.internal)$tt
}
if (resp.type == samr.const.twoclass.unpaired.response &
testStatistic == "wilcoxon" & assay.type == "array") {
tt <- wilcoxon.func(x, y, s0 = s0)$tt
}
if (resp.type == samr.const.oneclass.response & assay.type ==
"array") {
tt <- onesample.ttest.func(x, y, s0 = s0, sd = sd.internal)$tt
}
if (resp.type == samr.const.twoclass.paired.response &
assay.type == "array") {
tt <- paired.ttest.func(x, y, s0 = s0, sd = sd.internal)$tt
}
if (resp.type == samr.const.survival.response & assay.type ==
"array") {
tt <- cox.func(x, y, censoring.status, s0 = s0)$tt
}
if (resp.type == samr.const.multiclass.response & assay.type ==
"array") {
junk2 <- multiclass.func(x, y, s0 = s0)
tt <- junk2$tt
stand.contrasts <- junk2$stand.contrasts
}
if (resp.type == samr.const.quantitative.response & assay.type ==
"array") {
tt <- quantitative.func(x, y, s0 = s0)$tt
}
if (resp.type == samr.const.patterndiscovery.response &
assay.type == "array") {
junk <- patterndiscovery.func(
x, s0 = s0, eigengene.number = eigengene.number
)
tt <- junk$tt
eigengene <- junk$eigengene
}
if (resp.type == samr.const.twoclass.unpaired.response &
assay.type == "seq") {
junk <- wilcoxon.unpaired.seq.func(xresamp, y)
tt <- junk$tt
numer <- junk$numer
sd <- junk$sd
}
if (resp.type == samr.const.twoclass.paired.response &
assay.type == "seq") {
junk <- wilcoxon.paired.seq.func(xresamp, y)
tt <- junk$tt
numer <- junk$numer
sd <- junk$sd
}
if (resp.type == samr.const.quantitative.response & assay.type ==
"seq") {
junk <- quantitative.seq.func(xresamp, y)
tt <- junk$tt
numer <- junk$numer
sd <- junk$sd
}
if (resp.type == samr.const.survival.response & assay.type ==
"seq") {
junk <- cox.seq.func(xresamp, y, censoring.status)
tt <- junk$tt
numer <- junk$numer
sd <- junk$sd
}
if (resp.type == samr.const.multiclass.response & assay.type ==
"seq") {
junk2 <- multiclass.seq.func(xresamp, y)
tt <- junk2$tt
numer <- junk2$numer
sd <- junk2$sd
stand.contrasts <- junk2$stand.contrasts
}
if (
resp.type == samr.const.quantitative.response |
resp.type == samr.const.multiclass.response |
resp.type == samr.const.survival.response
) {
junk <- getperms(y, nperms)
perms <- junk$perms
all.perms.flag <- junk$all.perms.flag
nperms.act <- junk$nperms.act
}
if (resp.type == samr.const.twoclass.unpaired.response) {
if (are.blocks.specified) {
junk <- compute.block.perms(y, blocky, nperms)
permsy <- matrix(junk$permsy, ncol = length(y))
all.perms.flag <- junk$all.perms.flag
nperms.act <- junk$nperms.act
} else {
junk <- getperms(y, nperms)
permsy <- matrix(y[junk$perms], ncol = length(y))
all.perms.flag <- junk$all.perms.flag
nperms.act <- junk$nperms.act
}
}
if (resp.type == samr.const.oneclass.response) {
if ((length(y) * log(2)) < log(nperms)) {
allii <- 0:((2^length(y)) - 1)
nperms.act <- 2^length(y)
all.perms.flag <- 1
} else {
nperms.act <- nperms
all.perms.flag <- 0
}
permsy <- matrix(NA, nrow = nperms.act, ncol = length(y))
if (all.perms.flag == 1) {
k <- 0
for (i in allii) {
junk <- integer.base.b(i, b = 2)
if (length(junk) < length(y)) {
junk <- c(
rep(0, length(y) - length(junk)),
junk
)
}
k <- k + 1
permsy[k, ] <- y * (2 * junk - 1)
}
} else {
for (i in 1:nperms.act) {
permsy[i, ] <- sample(c(-1, 1),
size = length(y),
replace = TRUE
)
}
}
}
if (resp.type == samr.const.twoclass.paired.response) {
junk <- compute.block.perms(y, abs(y), nperms)
permsy <- junk$permsy
all.perms.flag <- junk$all.perms.flag
nperms.act <- junk$nperms.act
}
if (resp.type == samr.const.patterndiscovery.response) {
nperms.act <- nperms
perms <- NULL
permsy <- NULL
all.perms.flag <- FALSE
}
sdstar.keep <- NULL
if (assay.type != "seq") {
sdstar.keep <- matrix(0, ncol = nperms.act, nrow = nrow(x))
}
ttstar <- matrix(0, nrow = nrow(x), ncol = nperms.act)
foldchange.star <- NULL
if (resp.type == samr.const.twoclass.unpaired.response |
resp.type == samr.const.twoclass.paired.response) {
foldchange.star <- matrix(0, nrow = nrow(x), ncol = nperms.act)
}
if (resp.type == samr.const.multiclass.response) {
stand.contrasts.star <- array(NA, c(
nrow(x), length(table(y)),
nperms.act
))
}
}
if (xl.mode == "next20" | xl.mode == "lasttime") {
evo <- xl.prevfit$evo
tt <- xl.prevfit$tt
numer <- xl.prevfit$numer
eigengene <- xl.prevfit$eigengene
eigengene.number <- xl.prevfit$eigengene.number
sd <- xl.prevfit$sd - xl.prevfit$s0
sd.internal <- xl.prevfit$sd.internal
ttstar <- xl.prevfit$ttstar
ttstar0 <- xl.prevfit$ttstar0
n <- xl.prevfit$n
pi0 <- xl.prevfit$pi0
foldchange <- xl.prevfit$foldchange
y <- xl.prevfit$y
x <- xl.prevfit$x
xresamp <- xl.prevfit$xresamp
censoring.status <- xl.prevfit$censoring.status
argy <- xl.prevfit$argy
testStatistic <- xl.prevfit$testStatistic
foldchange.star <- xl.prevfit$foldchange.star
s0 <- xl.prevfit$s0
s0.perc <- xl.prevfit$s0.perc
resp.type <- xl.prevfit$resp.type
resp.type.arg <- xl.prevfit$resp.type.arg
assay.type <- xl.prevfit$assay.type
sdstar.keep <- xl.prevfit$sdstar.keep
resp.type <- xl.prevfit$resp.type
stand.contrasts <- xl.prevfit$stand.contrasts
stand.contrasts.star <- xl.prevfit$stand.contrasts.star
stand.contrasts.95 <- xl.prevfit$stand.contrasts.95
perms <- xl.prevfit$perms
permsy <- xl.prevfit$permsy
nperms <- xl.prevfit$nperms
nperms.act <- xl.prevfit$nperms.act
all.perms.flag <- xl.prevfit$all.perms.flag
depth <- xl.prevfit$depth
nresamp <- xl.prevfit$nresamp
nresamp.perm <- xl.prevfit$nresamp.perm
}
if (xl.mode == "regular") {
first <- 1
last <- nperms.act
}
if (xl.mode == "firsttime") {
first <- 1
last <- 1
}
if (xl.mode == "next20") {
first <- xl.time
last <- min(xl.time + 19, nperms.act - 1)
}
if (xl.mode == "lasttime") {
first <- nperms.act
last <- nperms.act
}
for (b in first:last) {
message(c("perm = ", b))
if (assay.type == "array") {
xstar <- x
}
if (assay.type == "seq") {
xstar <- xresamp[, , 1:nresamp.perm]
}
if (resp.type == samr.const.oneclass.response) {
ystar <- permsy[b, ]
if (testStatistic == "standard") {
ttstar[, b] <- onesample.ttest.func(xstar, ystar,
s0 = s0, sd = sd.internal
)$tt
}
}
if (resp.type == samr.const.twoclass.paired.response) {
ystar <- permsy[b, ]
if (assay.type == "array") {
ttstar[, b] <- paired.ttest.func(xstar, ystar,
s0 = s0, sd = sd.internal
)$tt
foldchange.star[, b] <- foldchange.paired(
xstar,
ystar, data$logged2
)
}
if (assay.type == "seq") {
ttstar[, b] <- wilcoxon.paired.seq.func(
xstar,
ystar
)$tt
foldchange.star[, b] <- foldchange.seq.twoclass.paired(
x,
ystar, depth
)
}
}
if (resp.type == samr.const.twoclass.unpaired.response) {
ystar <- permsy[b, ]
if (assay.type == "array") {
if (testStatistic == "standard") {
junk <- ttest.func(xstar, ystar, s0 = s0, sd = sd.internal)
}
if (testStatistic == "wilcoxon") {
junk <- wilcoxon.func(xstar, ystar, s0 = s0)
}
ttstar[, b] <- junk$tt
sdstar.keep[, b] <- junk$sd
foldchange.star[, b] <- foldchange.twoclass(
xstar,
ystar, data$logged2
)
}
if (assay.type == "seq") {
ttstar[, b] <- wilcoxon.unpaired.seq.func(
xstar,
ystar
)$tt
foldchange.star[, b] <- foldchange.seq.twoclass.unpaired(
x,
ystar, depth
)
}
}
if (resp.type == samr.const.survival.response) {
o <- perms[b, ]
if (assay.type == "array") {
ttstar[, b] <- cox.func(
xstar, y[o],
censoring.status = censoring.status[o], s0 = s0
)$tt
}
if (assay.type == "seq") {
ttstar[, b] <- cox.seq.func(
xstar, y[o],
censoring.status = censoring.status[o]
)$tt
}
}
if (resp.type == samr.const.multiclass.response) {
ystar <- y[perms[b, ]]
if (assay.type == "array") {
junk <- multiclass.func(xstar, ystar, s0 = s0)
ttstar[, b] <- junk$tt
sdstar.keep[, b] <- junk$sd
stand.contrasts.star[, , b] <- junk$stand.contrasts
}
if (assay.type == "seq") {
junk <- multiclass.seq.func(xstar, ystar)
ttstar[, b] <- junk$tt
stand.contrasts.star[, , b] <- junk$stand.contrasts
}
}
if (resp.type == samr.const.quantitative.response) {
ystar <- y[perms[b, ]]
if (assay.type == "array") {
junk <- quantitative.func(xstar, ystar, s0 = s0)
ttstar[, b] <- junk$tt
sdstar.keep[, b] <- junk$sd
}
if (assay.type == "seq") {
junk <- quantitative.seq.func(xstar, ystar)
ttstar[, b] <- junk$tt
}
}
if (resp.type == samr.const.patterndiscovery.response) {
xstar <- permute.rows(x)
junk <- patterndiscovery.func(
xstar,
s0 = s0, eigengene.number = eigengene.number
)
ttstar[, b] <- junk$tt
sdstar.keep[, b] <- junk$sd
}
}
if (xl.mode == "regular" | xl.mode == "lasttime") {
ttstar0 <- ttstar
for (j in seq_len(ncol(ttstar))) {
ttstar[, j] <- -1 * sort(-1 * ttstar[, j])
}
for (i in seq_len(nrow(ttstar))) {
ttstar[i, ] <- sort(ttstar[i, ])
}
evo <- apply(ttstar, 1, mean)
evo <- evo[seq(length(evo), 1)]
pi0 <- 1
if (resp.type != samr.const.multiclass.response) {
qq <- quantile(ttstar, c(0.25, 0.75))
}
if (resp.type == samr.const.multiclass.response) {
qq <- quantile(ttstar, c(0, 0.5))
}
pi0 <- sum(tt > qq[1] & tt < qq[2]) / (0.5 * length(tt))
foldchange <- NULL
if (resp.type == samr.const.twoclass.unpaired.response &
assay.type == "array") {
foldchange <- foldchange.twoclass(x, y, data$logged2)
}
if (resp.type == samr.const.twoclass.paired.response &
assay.type == "array") {
foldchange <- foldchange.paired(x, y, data$logged2)
}
if (resp.type == samr.const.oneclass.response & assay.type ==
"array") {
}
stand.contrasts.95 <- NULL
if (resp.type == samr.const.multiclass.response) {
stand.contrasts.95 <- quantile(
stand.contrasts.star,
c(0.025, 0.975)
)
}
if (resp.type == samr.const.twoclass.unpaired.response &
assay.type == "seq") {
foldchange <- foldchange.seq.twoclass.unpaired(
x,
y, depth
)
}
if (resp.type == samr.const.twoclass.paired.response &
assay.type == "seq") {
foldchange <- foldchange.seq.twoclass.paired(
x, y,
depth
)
}
if (return.x == FALSE) {
x <- NULL
}
}
return(
list(
n = n, x = x, xresamp = xresamp, y = y, argy = argy,
censoring.status = censoring.status, testStatistic = testStatistic,
nperms = nperms, nperms.act = nperms.act, tt = tt, numer = numer,
sd = sd + s0, sd.internal = sd.internal, s0 = s0, s0.perc = s0.perc,
evo = evo, perms = perms, permsy = permsy, nresamp = nresamp,
nresamp.perm = nresamp.perm, all.perms.flag = all.perms.flag,
ttstar = ttstar, ttstar0 = ttstar0, eigengene = eigengene,
eigengene.number = eigengene.number, pi0 = pi0, foldchange = foldchange,
foldchange.star = foldchange.star, sdstar.keep = sdstar.keep,
resp.type = resp.type, resp.type.arg = resp.type.arg,
assay.type = assay.type, stand.contrasts = stand.contrasts,
stand.contrasts.star = stand.contrasts.star,
stand.contrasts.95 = stand.contrasts.95,
depth = depth, call = this.call
)
)
}
#' @title Estimate sequencing depths
#' @param x data matrix. nrow=#gene, ncol=#sample
#' @return depth: estimated sequencing depth. a vector with len sample.
samr.estimate.depth <- function(x) {
iter <- 5
cmeans <- colSums(x) / sum(x)
for (i in 1:iter) {
n0 <- rowSums(x) %*% t(cmeans)
prop <- rowSums((x - n0)^2 / (n0 + 1e-08))
qs <- quantile(prop, c(0.25, 0.75))
keep <- (prop >= qs[1]) & (prop <= qs[2])
cmeans <- colMeans(x[keep, ])
cmeans <- cmeans / sum(cmeans)
}
depth <- cmeans / mean(cmeans)
return(depth)
}
#' @title Resampling
#' @param x data matrix. nrow=#gene, ncol=#sample
#' @param d estimated sequencing depth
#' @param nresamp number of resamplings
#' @return xresamp: an rank array with dim #gene*#sample*nresamp
#' @description Corresponds to `samr::resample`
#' @importFrom stats rpois runif
resa <- function(x, d, nresamp = 20) {
ng <- nrow(x)
ns <- ncol(x)
dbar <- exp(mean(log(d)))
xresamp <- array(0, dim = c(ng, ns, nresamp))
for (k in 1:nresamp) {
for (j in 1:ns) {
xresamp[, j, k] <- rpois(n = ng, lambda = (dbar / d[j]) *
x[, j]) + runif(ng) * 0.1
}
}
for (k in 1:nresamp) {
xresamp[, , k] <- t(rankcols(t(xresamp[, , k])))
}
return(xresamp)
}
#' @title Rank columns
#' @description Ranks the elements within each col of the matrix x and returns
#' these ranks in a matrix
#' @note this function is equivalent to `samr::rankcol`, but uses `apply` to
#' rank the colums instead of a compiled Fortran function which was causing our
#' DEGanalysis functions to freeze in large datasets.
#' @param x x
rankcols <- function(x) {
# ranks the elements within each col of the matrix x
# and returns these ranks in a matrix
n <- nrow(x)
p <- ncol(x)
mode(n) <- "integer"
mode(p) <- "integer"
mode(x) <- "double"
xr <- apply(x, 2, rank)
return(xr)
}
#' @title Check format
#' @param y y
#' @param resp.type resp type
#' @param censoring.status censoring status
check.format <- function(y, resp.type, censoring.status = NULL) {
# here i do some format checks for the input data$y
# note that checks for time course data are done in the
# parse function for time course;
# we then check the output from the parser in this function
if (resp.type == samr.const.twoclass.unpaired.response |
resp.type == samr.const.twoclass.unpaired.timecourse.response) {
if (sum(y == 1) + sum(y == 2) != length(y)) {
stop(paste(
"Error in input response data: response type ",
resp.type, " specified; values must be 1 or 2"
))
}
}
if (resp.type == samr.const.twoclass.paired.response | resp.type ==
samr.const.twoclass.paired.timecourse.response) {
if (sum(y) != 0) {
stop(paste(
"Error in input response data: response type ",
resp.type, " specified; values must be -1, 1, -2, 2, etc"
))
}
if (sum(table(y[y > 0]) != abs(table(y[y < 0])))) {
stop(paste(
"Error in input response data: response type ",
resp.type, " specified; values must be -1, 1, -2, 2, etc"
))
}
}
if (resp.type == samr.const.oneclass.response | resp.type ==
samr.const.oneclass.timecourse.response) {
if (sum(y == 1) != length(y)) {
stop(paste(
"Error in input response data: response type ",
resp.type, " specified; values must all be 1"
))
}
}
if (resp.type == samr.const.multiclass.response) {
tt <- table(y)
nc <- length(tt)
if (sum(y <= nc & y > 0) < length(y)) {
stop(
"Error in input response data: response type ", resp.type,
" specified; values must be 1,2, ... number of classes"
)
}
for (k in 1:nc) {
if (sum(y == k) < 2) {
stop(paste(
"Error in input response data: response type ",
resp.type, " specified; there must be >1 sample per class"
))
}
}
}
if (resp.type == samr.const.quantitative.response) {
if (!is.numeric(y)) {
stop(paste(
"Error in input response data: response type",
resp.type, " specified; values must be numeric"
))
}
}
if (resp.type == samr.const.survival.response) {
if (is.null(censoring.status)) {
stop(paste(
"Error in input response data: response type ",
resp.type, " specified; error in censoring indicator"
))
}
if (!is.numeric(y) | sum(y < 0) > 0) {
stop(
"Error in input response data: response type ", resp.type,
" specified; survival times must be numeric and nonnegative"
)
if (sum(censoring.status == 0) + sum(censoring.status ==
1) != length(censoring.status)) {
stop(
"Error in input response data: response type ", resp.type,
" specified; censoring indicators must be ",
"0 (censored) or 1 (failed)"
)
}
}
if (sum(censoring.status == 1) < 1) {
stop(paste(
"Error in input response data: response type ",
resp.type, " specified; there are no uncensored observations"
))
}
}
return()
}
#' @title Twoclass Wilcoxon statistics
#' @param xresamp an rank array with dim #gene*#sample*nresamp
#' @param y outcome vector of values 1 and 2
#' @return the statistic.
wilcoxon.unpaired.seq.func <- function(xresamp, y) {
tt <- rep(0, dim(xresamp)[1])
for (i in seq_len(dim(xresamp)[3])) {
tt <- tt + rowSums(xresamp[, y == 2, i]) - sum(y == 2) *
(length(y) + 1) / 2
}
tt <- tt / dim(xresamp)[3]
return(list(tt = tt, numer = tt, sd = rep(1, length(tt))))
}
wilcoxon.paired.seq.func <- function(xresamp, y) {
tt <- rep(0, dim(xresamp)[1])
for (i in seq_len(dim(xresamp)[3])) {
tt <- tt + rowSums(xresamp[, y > 0, i]) - sum(y > 0) *
(length(y) + 1) / 2
}
tt <- tt / dim(xresamp)[3]
return(list(tt = tt, numer = tt, sd = rep(1, length(tt))))
}
getperms <- function(y, nperms) {
total.perms <- factorial(length(y))
if (total.perms <= nperms) {
perms <- permute(seq_len(length(y)))
all.perms.flag <- 1
nperms.act <- total.perms
}
if (total.perms > nperms) {
perms <- matrix(NA, nrow = nperms, ncol = length(y))
for (i in 1:nperms) {
perms[i, ] <- sample(seq_len(length(y)), size = length(y))
}
all.perms.flag <- 0
nperms.act <- nperms
}
return(list(
perms = perms, all.perms.flag = all.perms.flag,
nperms.act = nperms.act
))
}
foldchange.twoclass <- function(x, y, logged2) {
m1 <- rowMeans(x[, y == 1, drop = FALSE])
m2 <- rowMeans(x[, y == 2, drop = FALSE])
if (!logged2) {
fc <- m2 / m1
}
if (logged2) {
fc <- 2^{
m2 - m1
}
}
return(fc)
}
#' @title Foldchange of twoclass unpaired sequencing data
#' @param x x
#' @param y y
#' @param depth depth
foldchange.seq.twoclass.unpaired <- function(x, y, depth) {
x.norm <- scale(x, center = FALSE, scale = depth) + 1e-08
fc <- apply(x.norm[, y == 2], 1, median) /
apply(x.norm[, y ==
1], 1, median)
return(fc)
}
foldchange.seq.twoclass.paired <- function(x, y, depth) {
nc <- ncol(x) / 2
o1 <- o2 <- rep(0, nc)
for (j in 1:nc) {
o1[j] <- which(y == -j)
o2[j] <- which(y == j)
}
x.norm <- scale(x, center = FALSE, scale = depth) + 1e-08
d <- x.norm[, o2, drop = FALSE] / x.norm[, o1, drop = FALSE]
fc <- lapply(d, 1, function(x) median(x, na.rm = TRUE))
return(fc)
}
permute <- function(elem) {
# generates all perms of the vector elem
if (!missing(elem)) {
if (length(elem) == 2) {
return(matrix(c(elem, elem[2], elem[1]), nrow = 2))
}
last.matrix <- permute(elem[-1])
dim.last <- dim(last.matrix)
new.matrix <- matrix(0, nrow = dim.last[1] * (dim.last[2] +
1), ncol = dim.last[2] + 1)
for (row in 1:(dim.last[1])) {
for (col in 1:(dim.last[2] + 1)) {
new.matrix[row + (col - 1) * dim.last[1], ] <-
insert.value(last.matrix[row, ], elem[1], col)
}
}
return(new.matrix)
} else {
message("Usage: permute(elem)\n\twhere elem is a vector")
}
}
insert.value <- function(vec, newval, pos) {
if (pos == 1) {
return(c(newval, vec))
}
lvec <- length(vec)
if (pos > lvec) {
return(c(vec, newval))
}
return(c(vec[1:pos - 1], newval, vec[pos:lvec]))
}
parse.block.labels.for.2classes <- function(y) {
# this only works for 2 class case- having form jBlockn,
# where j=1 or 2
n <- length(y)
y.act <- rep(NA, n)
blocky <- rep(NA, n)
for (i in 1:n) {
blocky[i] <- as.numeric(substring(y[i], 7, nchar(y[i])))
y.act[i] <- as.numeric(substring(y[i], 1, 1))
}
return(list(y.act = y.act, blocky = blocky))
}
parse.time.labels.and.summarize.data <- function(
x,