-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathKINOMOSet-class.R
1433 lines (1178 loc) · 39 KB
/
KINOMOSet-class.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
#' @include KINOMOfit-class.R
#' @include heatmaps.R
NULL
#' isKINOMOfit(list(res, resm, 'not a result'), recursive=FALSE)
#'
isKINOMOfit <- function(object, recursive=TRUE){
res <- is(object, 'KINOMOfit') || is(object, 'KINOMOfitX')
# if the object is not a KINOMO result: apply to each element if a list (only in recursive mode)
if( !res && recursive && is.list(object) )
sapply(object, isKINOMOfit)
else
res
}
#'
setClass('KINOMOList'
, representation(
runtime='proc_time'
)
, contains='namedList'
, validity=function(object){
# the list must only contains KINOMOfit objects of the same dimensions
ok <- isKINOMOfit(object)
if( !is.logical(ok) )
return("Could not validate elements in list: input is probably a complex structure of lists.")
pb <- which(!ok)
if( length(pb) ){
return(paste("invalid class for element(s)"
, str_out(i)
, "of input list [all elements must be fitted KINOMO models]"))
}
}
)
#' Show method for objects of class \code{KINOMOList}
#' @export
setMethod('show', 'KINOMOList',
function(object)
{
cat("<Object of class:", class(object), ">\n")
cat("Length:", length(object), "\n")
if( length(object) > 0 ) cat("Method(s):", algorithm(object, string=TRUE), "\n")
# show totaltime if present
tt <- runtime(object)
if( length(tt) > 0 ){
cat("Total timing:\n"); show(tt);
}
}
)
#' This argument is forced to \code{TRUE} when \code{string=TRUE}.
#'
setMethod('algorithm', 'KINOMOList',
function(object, string=FALSE, unique=TRUE){
l <- length(object)
if( string ) unique <- TRUE
if( l == 0 ) NULL
else if( l == 1 ) algorithm(object[[1]])
else{
# build the vector of the algorithm names (with no repeat)
m <- sapply(object, algorithm)
if( unique ) m <- unique(m)
if( string ) m <- paste(m, collapse=', ')
m
}
}
)
.seqtime <- function(object){
if( length(object) == 0 ) return(NULL)
# sum up the time across the runs
t.mat <- sapply(object, function(x){
if( is(x, 'KINOMOfitXn') ) runtime.all(x)
else runtime(x)
})
res <- rowSums(t.mat)
class(res) <- 'proc_time'
res
}
#' It returns \code{NULL} on an empty object.
setMethod('seqtime', 'KINOMOList',
function(object){
if( length(object) == 0 ) return(NULL)
# sum up the time across the runs
.seqtime(object)
}
)
#' the fits in \code{object}.
setMethod('runtime', 'KINOMOList',
function(object, all=FALSE){
if( !all ){
t <- slot(object, 'runtime')
if( length(t)==0 ) seqtime(object) else t
}else
sapply(object, runtime)
}
)
as.KINOMOList <- function(..., unlist=FALSE){
arg.l <- list(...)
if( length(arg.l) == 1L && is.list(arg.l[[1]]) && !is(arg.l[[1]], 'KINOMOfitX') )
arg.l <- arg.l[[1]]
# unlist if required
if( unlist )
arg.l <- unlist(arg.l)
# create a KINOMOList object from the input list
new('KINOMOList', arg.l)
}
#' res
#'
#' # plot a heatmap of the consensus matrix
#' \dontrun{ consensusmap(res) }
#'
setClass('KINOMOfitX'
, representation(
runtime.all = 'proc_time' # running time to perform all the KINOMO runs
)
, contains='VIRTUAL'
)
#' Returns the CPU time required to compute all the KINOMO runs.
#' It returns \code{NULL} if no CPU data is available.
setMethod('runtime.all', 'KINOMOfitX',
function(object){
t <- slot(object, 'runtime.all')
if( length(t) > 0 ) t else NULL
}
)
#
' greater than one, while only the result of the best run is stored in
#' the object (cf. option \code{'k'} in method \code{\link{KINOMO}}).
setMethod('nrun', 'KINOMOfitX',
function(object){
stop("KINOMO::KINOMOfitX - missing definition for pure virtual method 'nrun' in class '", class(object), "'")
}
)
#' This method always returns 1, since an \code{KINOMOfit} object is obtained
#' from a single KINOMO run.
setMethod('nrun', 'KINOMOfit',
function(object){
1L
}
)
#' @rdname connectivity
#' @export
setGeneric('consensus', function(object, ...) standardGeneric('consensus') )
#' Pure virtual method defined to ensure \code{consensus} is defined for sub-classes of \code{KINOMOfitX}.
#' It throws an error if called.
setMethod('consensus', 'KINOMOfitX',
function(object, ...){
stop("KINOMO::KINOMOfitX - missing definition for pure virtual method 'consensus' in class '", class(object), "'")
}
)
#' This method is provided for completeness and is identical to
#' \code{\link{connectivity}}, and returns the connectivity matrix,
#' which, in the case of a single KINOMO model, is also the consensus matrix.
setMethod('consensus', 'KINOMO',
function(object, ...){
connectivity(object, ...)
}
)
#' @inline
#' @export
setGeneric('consensushc', function(object, ...) standardGeneric('consensushc'))
#' Default value is \code{TRUE}.
setMethod('consensushc', 'matrix',
function(object, method='average', dendrogram=TRUE){
# hierachical clustering based on the connectivity matrix
hc <- hclust(as.dist(1-object), method=method)
# convert into a dendrogram if requested
if( dendrogram ) as.dendrogram(hc)
else hc
}
)
#' Compute the hierarchical clustering on the connectivity matrix of \code{object}.
setMethod('consensushc', 'KINOMO',
function(object, ...){
# hierachical clustering based on the connectivity matrix
consensushc(connectivity(object), ...)
}
)
#'
setMethod('consensushc', 'KINOMOfitX',
function(object, what=c('consensus', 'fit'), ...){
what <- match.arg(what)
if( what == 'consensus' ){
# hierachical clustering on the consensus matrix
consensushc(consensus(object), ...)
}else if( what == 'fit' )
consensushc(fit(object), ...)
}
)
setMethod('predict', signature(object='KINOMOfitX'),
function(object, what=c('columns', 'rows', 'samples', 'features', 'consensus', 'chc'), dmatrix = FALSE, ...){
# determine which prediction to do
what <- match.arg(what)
res <- if( what %in% c('consensus', 'chc') ){
# build the tree from consensus matrix
h <- consensushc(object, what='consensus', dendrogram=FALSE)
# extract membership from the tree
cl <- cutree(h, k=nbasis(object))
# rename the cluster ids in the case of a consensus map
if( what != 'chc' ){
dr <- as.dendrogram(h)
o <- order.dendrogram(reorder(dr, rowMeans(consensus(object), na.rm=TRUE)))
cl <- setNames(match(cl, unique(cl[o])), names(cl))
}
res <- as.factor(cl)
# add dissimilarity matrix if requested
if( dmatrix ){
attr(res, 'dmatrix') <- 1 - consensus(object)
}
if( what != 'chc' ) attr(res, 'iOrd') <- o
# return
res
}
else predict(fit(object), what=what, ..., dmatrix = dmatrix)
attr(res, 'what') <- what
res
}
)
#' Returns the model object that achieves the lowest residual approximation
#' error across all the runs.
#'
#' It is a pure virtual method defined to ensure \code{fit} is defined
#' for sub-classes of \code{KINOMOfitX}, which throws an error if called.
setMethod('fit', 'KINOMOfitX',
function(object){
stop("KINOMO::KINOMOfitX - missing definition for pure virtual method 'fit' in class '", class(object), "'")
}
)
#' for sub-classes of \code{KINOMOfitX}, which throws an error if called.
setMethod('minfit', 'KINOMOfitX',
function(object){
stop("KINOMO::KINOMOfitX - missing definition for pure virtual method 'minfit' in class '", class(object), "'")
}
)
#' Show method for objects of class \code{KINOMOfitX}
#' @export
setMethod('show', 'KINOMOfitX',
function(object){
cat("<Object of class:", class(object), ">\n")
# name of the algorithm
cat(" Method:", algorithm(object), "\n")
# number of runs
cat(" Runs: ", nrun(object),"\n");
# initial state
cat(" RNG:\n ", RNGstr(getRNG1(object)),"\n");
if( nrun(object) > 0 ){
# show total timing
cat(" Total timing:\n"); show(runtime.all(object));
}
}
)
setGeneric('getRNG1', package='rngtools')
setMethod('getRNG1', signature(object='KINOMOfitX'),
function(object){
stop("KINOMO::getRNG1(", class(object), ") - Unimplemented pure virtual method: could not extract initial RNG settings.")
}
)
#' Compares two KINOMO models when at least one comes from multiple KINOMO runs.
setMethod('KINOMO.equal', signature(x='KINOMOfitX', y='KINOMO'),
function(x, y, ...){
KINOMO.equal(fit(x), y, ...)
}
)
#' Compares two KINOMO models when at least one comes from multiple KINOMO runs.
setMethod('KINOMO.equal', signature(x='KINOMO', y='KINOMOfitX'),
function(x, y, ...){
KINOMO.equal(x, fit(y), ...)
}
)
#' Returns the residuals achieved by the best fit object, i.e. the lowest
#' residual approximation error achieved across all KINOMO runs.
setMethod('residuals', signature(object='KINOMOfitX'),
function(object, ...){
residuals(minfit(object), ...)
}
)
#' Returns the deviance achieved by the best fit object, i.e. the lowest
#' deviance achieved across all KINOMO runs.
setMethod('deviance', signature(object='KINOMOfitX'),
function(object, ...){
deviance(minfit(object), ...)
}
)
#'
setClass('KINOMOfitX1'
, representation(
#fit = 'KINOMOfit' # holds the best fit from all the runs
consensus = 'matrix' # average connectivity matrix of all the KINOMO runs
, nrun = 'integer'
, rng1 = 'ANY'
)
, contains=c('KINOMOfitX', 'KINOMOfit')
, prototype=prototype(
consensus = matrix(as.numeric(NA),0,0)
, nrun = as.integer(0)
)
)
#' Show method for objects of class \code{KINOMOfitX1}
#' @export
setMethod('show', 'KINOMOfitX1',
function(object){
callNextMethod(object)
# show details of the best fit
#cat(" # Best fit:\n ")
#s <- capture.output(show(fit(object)))
#cat(s, sep="\n |")
}
)
#' Returns the number of KINOMO runs performed, amongst which \code{object} was
#' selected as the best fit.
setMethod('nrun', 'KINOMOfitX1',
function(object){
slot(object,'nrun')
}
)
#' The result is the matrix stored in slot \sQuote{consensus}.
#' This method returns \code{NULL} if the consensus matrix is empty.
setMethod('consensus', signature(object='KINOMOfitX1'),
function(object, no.attrib = FALSE){
C <- slot(object, 'consensus')
if( length(C) > 0 ){
if( !no.attrib ){
class(C) <- c(class(C), 'KINOMO.consensus')
attr(C, 'nrun') <- nrun(object)
attr(C, 'nbasis') <- nbasis(object)
}
C
}else NULL
}
)
#' Since \code{KINOMOfitX1} objects only hold the best fit, this method simply
#' returns \code{object} coerced into an \code{KINOMOfit} object.
setMethod('minfit', 'KINOMOfitX1',
function(object){
# coerce the object into a KINOMOfit object
as(object, 'KINOMOfit')
}
)
#' \sQuote{fit}.
setMethod('fit', signature(object='KINOMOfitX1'),
function(object){
slot(object, 'fit')
}
)
#' Returns the RNG settings used to compute the first of all KINOMO runs, amongst
#' which \code{object} was selected as the best fit.
setMethod('getRNG1', signature(object='KINOMOfitX1'),
function(object){
object@rng1
}
)
#' Compares the KINOMO models fitted by multiple runs, that only kept the best fits.
setMethod('KINOMO.equal', signature(x='KINOMOfitX1', y='KINOMOfitX1'),
function(x, y, ...){
KINOMO.equal(fit(x), fit(y), ...)
}
)
#'
setClass('KINOMOfitXn'
, contains=c('KINOMOfitX', 'list')
, validity=function(object){
# the list must only contains KINOMOfit objects of the same dimensions
ref.dim <- NULL
ref.algo <- NULL
for(i in seq_along(object)){
# check class of the element
item <- object[[i]]
if( !(is(item, 'KINOMOfit') && !is(item, 'KINOMOfitX')) )
return(paste("invalid class for element", i, "of input list [all elements must be a KINOMOfit object]"))
# check dimensions
if( is.null(ref.dim) ) ref.dim <- dim(item)
if( !identical(ref.dim, dim(item)) )
return(paste("invalid dimension for element", i, "of input list [all elements must have the same dimensions]"))
# check algorithm names
if( is.null(ref.algo) ) ref.algo <- algorithm(item)
if( !identical(ref.algo, algorithm(item)) )
return(paste("invalid algorithm for element", i, "of input list [all elements must result from the same algorithm]"))
}
}
)
#' Show method for objects of class \code{KINOMOfitXn}
#' @export
setMethod('show', 'KINOMOfitXn',
function(object){
callNextMethod(object)
# if the object is not empty and slot runtime.all is not null then show
# the sequential time, as it might be different from runtime.all
if( length(object) > 0 && !is.null(runtime.all(object, null=TRUE)) ){
# show total sequential timing
cat(" Sequential timing:\n"); show(seqtime(object));
}
}
)
#' This method returns \code{NULL} if the object is empty.
setMethod('nbasis', signature(x='KINOMOfitXn'),
function(x, ...){
if( length(x) == 0 ) return(NULL)
return( nbasis(x[[1]]) )
}
)
#' @rdname dims
setMethod('dim', signature(x='KINOMOfitXn'),
function(x){
if( length(x) == 0 ) return(NULL)
return( dim(x[[1L]]) )
}
)
#' Returns the coefficient matrix of the best fit amongst all the fits stored in
#' \code{object}.
#' It is a shortcut for \code{coef(fit(object))}.
setMethod('coef', signature(object='KINOMOfitXn'),
function(object, ...){
coef(fit(object), ...)
}
)
#' Returns the basis matrix of the best fit amongst all the fits stored in
#' \code{object}.
#' It is a shortcut for \code{basis(fit(object))}.
setMethod('basis', signature(object='KINOMOfitXn'),
function(object, ...){
basis(fit(object), ...)
}
)
#' Method for multiple KINOMO fit objects, which returns the indexes of fixed basis
#' terms from the best fitted model.
setMethod('ibterms', 'KINOMOfitX',
function(object){
ibterms(fit(object))
}
)
#' Method for multiple KINOMO fit objects, which returns the indexes of fixed
#' coefficient terms from the best fitted model.
setMethod('icterms', 'KINOMOfit',
function(object){
icterms(fit(object))
}
)
#' Returns the number of runs performed to compute the fits stored in the list
#' (i.e. the length of the list itself).
setMethod('nrun', 'KINOMOfitXn',
function(object){
length(object)
}
)
#' Returns the name of the common KINOMO algorithm used to compute all fits
#' stored in \code{object}
#'
#' Since all fits are computed with the same algorithm, this method returns the
#' name of algorithm that computed the first fit.
#' It returns \code{NULL} if the object is empty.
setMethod('algorithm', 'KINOMOfitXn',
function(object){
if( length(object) == 0 ) return(NULL)
return( algorithm(object[[1]]) )
}
)
#' Returns the name of the common seeding method used the computation of all fits
#' stored in \code{object}
#'
#' Since all fits are seeded using the same method, this method returns the
#' name of the seeding method used for the first fit.
#' It returns \code{NULL} if the object is empty.
setMethod('seeding', 'KINOMOfitXn',
function(object){
if( length(object) == 0 ) return(NULL)
return( seeding(object[[1]]) )
}
)
#' Returns the common type KINOMO model of all fits stored in \code{object}
#'
#' Since all fits are from the same KINOMO model, this method returns the
#' model type of the first fit.
#' It returns \code{NULL} if the object is empty.
setMethod('modelname', signature(object='KINOMOfitXn'),
function(object){
if( length(object) == 0 ) return(NULL)
return( modelname(object[[1]]) )
}
)
#' Returns the CPU time that would be required to sequentially compute all KINOMO
#' fits stored in \code{object}.
#'
#' This method calls the function \code{runtime} on each fit and sum up the
#' results.
#' It returns \code{NULL} on an empty object.
setMethod('seqtime', 'KINOMOfitXn',
function(object){
if( length(object) == 0 ) return(NULL)
# sum up the time across the runs
.seqtime(object)
}
)
#'
setMethod('runtime.all', 'KINOMOfitXn',
function(object, null=FALSE, warning=TRUE){
if( length(object) == 0 ) return(NULL)
stored.time <- slot(object, 'runtime.all')
# if there is some time stored, return it
if( length(stored.time) > 0 ) stored.time
else if( null ) NULL
else{
if( warning )
warning("KINOMOfitXn::runtime.all - computation time data not available [sequential time was used instead]")
seqtime(object) # otherwise total sequential time
}
}
)
#'
setMethod('minfit', 'KINOMOfitXn',
function(object){
b <- which.best(object, deviance)
# test for length 0
if( length(b) == 0 ) return(NULL)
# return the run with the lower
object[[ b ]]
}
)
#' @rdname advanced
which.best <- function(object, FUN=deviance, ...){
# test for length 0
if( length(object) == 0 )
return(integer())
# retrieve the measure for each run
e <- sapply(object, FUN, ...)
# return the run with the lower
which.min(e)
}
#' This method throws an error if the object is empty.
setMethod('getRNG1', signature(object='KINOMOfitXn'),
function(object){
if( length(object) == 0 )
stop("KINOMO::getRNG1 - Could not extract RNG data from empty object [class:", class(object), "]")
getRNG(object[[1]])
}
)
#' @inline
#' @rdname RNG
#' @export
setGeneric('.getRNG', package='rngtools')
#' Returns the RNG settings used for the best fit.
#'
#' This method throws an error if the object is empty.
setMethod('.getRNG', signature(object='KINOMOfitXn'),
function(object, ...){
if( length(object) == 0 )
stop("KINOMO::getRNG - Could not extract RNG data from empty object [class:", class(object), "]")
getRNG(minfit(object), ...)
}
)
#' Returns the best KINOMO fit object amongst all the fits stored in \code{object},
#' i.e. the fit that achieves the lowest estimation residuals.
setMethod('fit', signature(object='KINOMOfitXn'),
function(object){
fit( minfit(object) )
}
)
#' @inline
setMethod('KINOMO.equal', signature(x='list', y='list'),
function(x, y, ..., all=FALSE, vector=FALSE){
if( !all )
KINOMO.equal(x[[ which.best(x) ]], y[[ which.best(y) ]], ...)
else{
if( length(x) != length(y) )
FALSE
else
res <- mapply(function(a,b,...) isTRUE(KINOMO.equal(a,b,...)), x, y, MoreArgs=list(...))
if( !vector )
res <- all( res )
res
}
}
)
#' Compare all elements in \code{x} to \code{x[[1]]}.
setMethod('KINOMO.equal', signature(x='list', y='missing'),
function(x, y, ...){
if( length(x) == 0L ){
warning("Empty list argument `x`: returning NA")
return(NA)
}
if( length(x) == 1L ){
warning("Only one element in list argument `x`: returning TRUE")
return(TRUE)
}
for( a in x ){
if( !KINOMO.equal(x[[1]], a, ...) ) return(FALSE)
}
return(TRUE)
}
)
#' @aliases plot.KINOMO.consensus
setMethod('consensus', signature(object='KINOMOfitXn'),
function(object, ..., no.attrib = FALSE){
if( length(object) == 0 ) return(NULL)
# init empty consensus matrix
con <- matrix(0, ncol(object), ncol(object))
# name the rows and columns appropriately: use the sample names of the first fit
dimnames(con) <- list(colnames(object[[1]]), colnames(object[[1]]))
# compute mean connectivity matrix
sapply(object
, function(x, ...){
con <<- con + connectivity(x, ..., no.attrib = TRUE)
NULL
}
, ...
)
con <- con / nrun(object)
# return result
if( !no.attrib ){
class(con) <- c(class(con), 'KINOMO.consensus')
attr(con, 'nrun') <- nrun(object)
attr(con, 'nbasis') <- nbasis(object)
}
con
}
)
#' @method plot KINOMO.consensus
#' @export
plot.KINOMO.consensus <- function(x, ...){
consensusmap(x, ...)
}
#' @export
setGeneric('dispersion', function(object, ...) standardGeneric('dispersion') )
#' Workhorse method that computes the dispersion on a given matrix.
setMethod('dispersion', 'matrix',
function(object, ...){
stopifnot( nrow(object) == ncol(object) )
sum( 4 * (object-1/2)^2 ) / nrow(object)^2
}
)
#' Computes the dispersion on the consensus matrix obtained from multiple KINOMO
#' runs.
setMethod('dispersion', 'KINOMOfitX',
function(object, ...){
dispersion(consensus(object), ...)
}
)
#' @keywords internal
setGeneric('KINOMOfitX', function(object, ...) standardGeneric('KINOMOfitX') )
#' Create an \code{KINOMOfitX} object from a list of fits.
#'
setMethod('KINOMOfitX', 'list',
function(object, ..., .merge=FALSE){
if( length(object) == 0 )
return(new('KINOMOfitXn'))
else if( is(object, 'KINOMOfitXn') && !.merge)
return(object)
# retrieve the extra arguments
extra <- list(...)
# if runtime.all is provided: be sure it's of the right class
tt <- extra$runtime.all
compute.tt <- TRUE
if( !is.null(tt) ){
if( !is(tt, 'proc_time') ){
if( !is.numeric(tt) || length(tt) != 5 )
stop("KINOMO::KINOMOfitX - invalid value for 'runtime.all' [5-length numeric expected]")
class(extra$runtime.all) <- 'proc_time'
}
compute.tt <- FALSE
}else{
extra$runtime.all <- rep(0,5)
class(extra$runtime.all) <- 'proc_time'
}
# check validity and aggregate if required
ref.algo <- NULL
ref.class <- NULL
nrun <- 0
lapply( seq_along(object)
, function(i){
item <- object[[i]]
# check the type of each element
if( !(is(item, 'KINOMOfitX') || is(item, 'KINOMOfit')) )
stop("KINOMO::KINOMOfitX - invalid class for element ", i, " of input list [all elements must be KINOMOfit or KINOMOfitX objects]")
# check that all elements result from the same algorithm
if( is.null(ref.algo) ) ref.algo <<- algorithm(item)
if( !identical(algorithm(item), ref.algo) )
stop("KINOMO::KINOMOfitX - invalid algorithm for element ", i, " of input list [cannot join results from different algorithms]")
# check if simple join is possible: only Ok if all elements are from the same class (KINOMOfit or KINOMOfitXn)
if( length(ref.class) <= 1 ) ref.class <<- unique(c(ref.class, class(item)))
# sum up the number of runs
nrun <<- nrun + nrun(item)
# compute total running time if necessary
if( compute.tt )
extra$runtime.all <<- extra$runtime.all + runtime.all(item)
}
)
# force merging if the input list is hetergeneous or if it only contains KINOMOfitX1 objects
if( length(ref.class) > 1 || ref.class == 'KINOMOfitX1' ){
KINOMO.debug('KINOMOfitX', ".merge is forced to TRUE")
.merge <- TRUE
}
# unpack all the KINOMOfit objects
object.list <- unlist(object)
KINOMO.debug('KINOMOfitX', "Number of fits to join = ", length(object.list))
# one wants to keep only the best result
if( .merge ){
warning("KINOMO::KINOMOfitX - The method for merging lists is still in development")
# set the total number of runs
extra$nrun <- as.integer(nrun)
# consensus matrix
if( !is.null(extra$consensus) )
warning("KINOMO::KINOMOfitX - the value of 'consensus' was discarded as slot 'consensus' is computed internally")
extra$consensus <- NULL
consensus <- matrix(as.numeric(NA), 0, 0)
best.res <- Inf
best.fit <- NULL
sapply(object.list, function(x){
if( !is(x, 'KINOMOfit') )
stop("KINOMO::KINOMOfitX - all inner-elements of '",substitute(object),"' must inherit from class 'KINOMOfit'")
# merge consensus matrices
consensus <<- if( sum(dim(consensus)) == 0 ) nrun(x) * consensus(x)
else consensus + nrun(x) * consensus(x)
temp.res <- residuals(x)
if( temp.res < best.res ){
# keep best result
best.fit <<- minfit(x)
best.res <<- temp.res
}
})
# finalize consensus matrix
consensus <- consensus/extra$nrun
extra$consensus <- consensus
# return merged result
return( do.call(KINOMOfitX, c(list(best.fit), extra)) )
}
else{
# create a KINOMOfitXn object that holds the whole list
do.call('new', c(list('KINOMOfitXn', object.list), extra))
}
}
)
#' Creates an \code{KINOMOfitX1} object from a single fit.
#' This is used in \code{\link{KINOMO}} when only the best fit is kept in memory or
#' on disk.
#'
setMethod('KINOMOfitX', 'KINOMOfit',
function(object, ...){
extra <- list(...)
# default value for nrun is 1
if( is.null(extra$nrun) ) extra$nrun = as.integer(1)
# a consensus matrix is required (unless nrun is 1)
if( is.null(extra$consensus) ){
if( extra$nrun == 1 )
extra$consensus <- connectivity(object)
else
stop("Slot 'consensus' is required to create a 'KINOMOfitX1' object where nrun > 1")
}
# slot runtime.all is inferred if missing and nrun is 1
if( is.null(extra$runtime.all) && extra$nrun == 1 )
extra$runtime.all <- runtime(object)
# create the KINOMOfitX1 object
do.call('new', c(list('KINOMOfitX1', object), extra))
}
)
#' Provides a way to aggregate \code{KINOMOfitXn} objects into an \code{KINOMOfitX1}
#' object.
setMethod('KINOMOfitX', 'KINOMOfitX',
function(object, ...){
# nothing to do in the case of KINOMOfitX1 objects
if( is(object, 'KINOMOfitX1') ) return(object)
# retrieve extra arguments
extra <- list(...)
# take runtime.all from the object itself
if( !is.null(extra$runtime.all) )
warning("KINOMO::KINOMOfitX - argument 'runtime.all' was discarded as it is computed from argument 'object'")
extra$runtime.all <- runtime.all(object)
# create the KINOMOfitX1 object
f <- selectMethod(KINOMOfitX, 'list')
do.call(f, c(list(object), extra))
}
)
#' Computes the best or mean purity across all KINOMO fits stored in \code{x}.
#'
#' @param method a character string that specifies how the value is computed.
#' It may be either \code{'best'} or \code{'mean'} to compute the best or mean
#' purity respectively.
#'
#' @inline
setMethod('purity', signature(x='KINOMOfitXn', y='ANY'),
function(x, y, method='best', ...){
c <- sapply(x, purity, y=y, ...)
# aggregate the results if a method is provided
if( is.null(method) ) c
else aggregate.measure(c, method, decreasing=TRUE)
}
)
#' Computes the best or mean entropy across all KINOMO fits stored in \code{x}.
#'
#' @inline
setMethod('entropy', signature(x='KINOMOfitXn', y='ANY'),
function(x, y, method='best', ...){
c <- sapply(x, entropy, y=y, ...)
# aggregate the results if a method is provided
if( is.null(method) ) c
else aggregate.measure(c, method)
}
)
aggregate.measure <- function(measure, method=c('best', 'mean'), decreasing=FALSE){
# aggregate the results
method <- match.arg(method)
res <- switch(method
, mean = mean(measure)
, best = if( decreasing ) max(measure) else min(measure)
)
# set the name to
names(res) <- method
# return result
res
}
#'
setMethod('summary', signature(object='KINOMOfitX'),
function(object, ...){
# compute summary measures for the best fit
best.fit <- minfit(object)
s <- summary(best.fit, ...)
# get totaltime
t <- runtime.all(object)
# replace cpu.all and nrun in the result (as these are set by the summary method of class KINOMOfit)
s[c('cpu.all', 'nrun')] <- c(as.numeric(t['user.self']+t['user.child']), nrun(object))
# compute cophenetic correlation coeff and dispersion
C <- consensus(object)
s <- c(s, cophenetic=cophcor(C), dispersion=dispersion(C))