-
Notifications
You must be signed in to change notification settings - Fork 13
/
QMatrix.R
2303 lines (2149 loc) · 47.4 KB
/
QMatrix.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
##
## Copyright 2009 Botond Sipos
## See the package description for licensing information.
##
##########################################################################/**
#
# @RdocClass QMatrix
#
# @title "The QMatrix class"
#
# \description{
# This is the class representing rate matrices. The QMatrix objects store two rate matrices: one contains
# the rates provided by the user (unscaled rate matrix), the other matrix (scaled rate matrix) is rescaled so the
# expected number of subsitutions per unit time is equal to one when the process is at equlibrium.
# The scaled rate matrices, along with the site-process-specific rate multiplier
# parameters define the rates of the Event objects generated by the associated GeneralSubstitution objects.
#
# The QMatrix objects have a bidirectonal relationship with the GeneralSubstitution object as they store a reference
# to the associated process objects. QMatrix objects do not have a write-protection field, but they use the one
# from the associated GeneralSubstitution object.
#
# Usually there is no need to interact with the QMatrix objects directly, so this class
# is mainly used internally.
#
# @classhierarchy
# }
#
# @synopsis
#
# \arguments{
# \item{name}{The name of the QMatrix object (a character vector of length one).}
# \item{alphabet}{An Alphabet object.}
# \item{rate.list}{A list of unscaled event rates and the associated event names. It will be passed to the \code{setRateList.QMatrix} method.}
# \item{process}{A GeneralSubstitution object to be associated with the QMatrix object.}
# \item{...}{Not used.}
# }
#
# \section{Fields and Methods}{
# @allmethods
# }
#
# \examples{
# # create a QMatrix object by providing an Alphabet object and rates
# m<-QMatrix(name="Susie Q", alphabet=BinaryAlphabet(), rate.list=list("1->0"=2,"0->1"=3))
# # get object summary
# summary(m)
# # create a GeneralSubstitution object by
# # providing an Alphabet object and the rates
# p<-GeneralSubstitution(alphabet=BinaryAlphabet(), rate.list=list("1->0"=1,"0->1"=3))
# # get the QMatrix object from p
# m<-p$QMatrix
# # get various object properties
# m
# is.QMatrix(m)
# m$name
# m$id
# m$alphabet
# # get the associated process
# m$process
# # get the unscaled rate of "0->1"
# getRate(m,"0->1")
# # get the scaled rate of "0->1"
# getEventRate(m,"0->1")
# # get the list of unscaled rates
# m$rateList
# # get unscaled rate matrix
# m$matrix
# # get scaled rate matrix
# m$scaledMatrix
# }
#
# @author
#
# \seealso{
# GeneralSubstitution Alphabet Process
# }
#
#*/###########################################################################
setConstructorS3(
"QMatrix",
function(
name="Anonymous",
alphabet=NA,
rate.list=NA,
process=NA,
...
) {
this<-PSRoot();
this<-extend(
this,
"QMatrix",
.name=NA,
.alphabet=NA,
.rate.matrix=NA,
.orig.matrix=NA,
.norm.const=NA,
.process=NA,
.is.q.matrix=TRUE
);
this$name<-name;
if(!missing(process)){
this$process<-process;
}
if(!missing(alphabet)){
this$alphabet<-alphabet;
}
if(!missing(rate.list)){
if(missing(alphabet)){
throw("Cannot set rates because the alphabet is not specified!\n")
}
this$rateList<-rate.list;
}
return(this);
},
enforceRCC=TRUE
);
##
## Method: .buildRateMatrix
##
setMethodS3(
".buildRateMatrix",
class="QMatrix",
function(
this,
...
){
size<-this$alphabet$size;
symbols<-this$alphabet$symbols;
# Setting the dimension names
# for the original rates matrix:
if(!isEmpty(this$.alphabet)){
this$.orig.matrix<-matrix(nrow=size,ncol=size);
colnames(this$.orig.matrix)<-symbols;
rownames(this$.orig.matrix)<-symbols;
}
# Copy to the scaled matrix:
this$.rate.matrix<-this$.orig.matrix;
},
private=TRUE,
protected=FALSE,
overwrite=FALSE,
conflict="warning",
validators=getOption("R.methodsS3:validators:setMethodS3")
);
##
## Method: checkConsistency
##
###########################################################################/**
#
# @RdocMethod checkConsistency
#
# @title "Check object consistency"
#
# \description{
# @get "title".
# }
#
# @synopsis
#
# \arguments{
# \item{this}{An object.}
# \item{check.process}{Check the associated process object.}
# \item{...}{Not used.}
# }
#
#
# \value{
# Returns an invisible TRUE if no inconsistencies found in the object, throws
# an error otherwise.
# }
#
# @author
#
# \seealso{
# @seeclass
# }
#
#*/###########################################################################
setMethodS3(
"checkConsistency",
class="QMatrix",
function(
this,
check.process=TRUE,
...
){
if(is.Process(this$.process)){
wp<-this$.process$.write.protected;
if (wp) {
# Cannot use virtual field here because of
# some obscure errror:
this$.process$.write.protected<-FALSE;
}
}
may.fail<-function(this) {
# Reassing name:
this$name<-this$name;
# Do not reassign alphabet as that will wipe out the rates!
if(!is.na(this$.alphabet) & !is.Alphabet(this$.alphabet)){
throw("QMatrix alphabet is invalid!\n");
}
# Check if the original rate matrix is a matrix or NA.
if(!is.matrix(this$.orig.matrix) & !all(is.na(this$.orig.matrix))){
throw("The original rates matrix is invalid!\n");
}
# Check original rates matrix size:
else if(!all(dim(this$.orig.matix) != c(this$.alphabet$.size, this$.alphabet$.size))) {
throw("The original rates matrix is of wrong size!");
}
# Check if the rescaled rate matrix is a matrix or NA.
if(!is.matrix(this$.rate.matrix) & !all(is.na(this$.rate.matrix))){
throw("The rescaled rates matrix is invalid!\n");
}
# Check rescaled rates matrix size:
else if(!all(dim(this$.rates.matix) != c(this$.alphabet$.size, this$.alphabet$.size))) {
throw("The original rates matrix is of wrong size!");
}
# Flag for not having NA-as in the matrices.
COMPLETE<-TRUE;
if(is.matrix(this$.orig.matrix) ){
if(any(is.na(this$.orig.matrix))){
warning("Some rates are undefined!\n");
COMPLETE<-FALSE;
}
# Watch out for the operator precedence here!
else if ( (!all(is.numeric(this$.orig.matrix))) & (!all(is.na(this$.orig.matrix))) ){
print(this$.orig.matrix)
throw("The original rates matrix has non-numeric elements!\n");
}
}
if(is.matrix(this$.orig.matrix) ){
if( any(is.na(this$.orig.matrix)) & COMPLETE ){
COMPLETE<-FALSE;
throw("The original rates matrix is complete, but the rescaled matrix has undefined elements!\n");
}
# Watch out for the operator precedence here!
else if ( (!all(is.numeric(this$.orig.matrix)) & (!all(is.na(this$.orig.matrix)))) ){
throw("The original rates matrix has non-numeric elements!\n");
}
}
# Check the normalizing constant:
if( length(this$.norm.const) != 1 | (!is.na(this$.norm.const) & !is.numeric(this$.norm.const)) ){
throw("Normalizing constant is invalid!\n");
}
# Check the normalization:
if(is.matrix(this$.orig.matrix) & is.matrix(this$.rate.matrix) & COMPLETE ){
if(!PSRoot$my.all.equal(this$.rate.matrix, (this$.norm.const * this$.orig.matrix)) ){
throw("The scaled matrix is inconsistent with the original matrix and the normalizing constant!\n");
}
}
# Check the process:
if(check.process==TRUE){
if(is.Process(this$.process)){
# Check for alphabet compatibility:
if(this$.alphabet != this$.process$alphabet){
throw("Process/QMatrix alphabet mismatch!\n");
}
# Check if the parent process QMatrix is this object:
if(!equals(this$.process$.q.matrix, this) ){
throw("Parent process QMatrix is not identical with self!\n");
}
}
else if(!is.na(this$.process)){
throw("Process entry is invalid!\n");
}
}
}
tryCatch(may.fail(this),finally={if(is.Process(this$.process)){this$.process$.write.protected<-wp}});
return(invisible(TRUE));
},
private=FALSE,
protected=FALSE,
overwrite=FALSE,
conflict="warning",
validators=getOption("R.methodsS3:validators:setMethodS3")
);
##
## Method: .callRateRescaling
##
setMethodS3(
".callRateRescaling",
class="QMatrix",
function(
this,
guess.equ=TRUE,
...
){
# Usually called when the rate matrix chenges.
# If the Q matrix has a parent process with a valid equilibrium distribution:
if(is.Process(this$.process) & !any(is.na(as.vector(this$.orig.matrix))) ){
if(guess.equ){
# Try to guess the new equlibrium distribution:
if(!.setEquDistFromGuess(this$.process)){
# Fill with NA-s if failed with guessing:
.initEquDist(this$.process);
}
}
# Rescale if the equilibrium distribution was succesfully set:
if(all(!is.na(this$.process$equDist))){
rescaleQMatrix(this$.process);
}
}
},
private=TRUE,
protected=FALSE,
overwrite=FALSE,
conflict="warning",
validators=getOption("R.methodsS3:validators:setMethodS3")
);
##
## Method: getName
##
###########################################################################/**
#
# @RdocMethod getName
#
# @title "Get the name of a QMatrix object"
#
# \description{
# @get "title".
# }
#
# @synopsis
#
# \arguments{
# \item{this}{A QMatrix object.}
# \item{...}{Not used.}
# }
#
# \value{
# A charcter vector of length one.
# }
#
# \examples{
# # create a QMatrix object
# m<-QMatrix()
# # set/get name
# setName(m,"Susie Q")
# getName(m)
# # set/get name via virtual field
# m$name<-"Q"
# m$name
# }
#
# @author
#
# \seealso{
# @seeclass
# }
#
#*/###########################################################################
setMethodS3(
"getName",
class="QMatrix",
function(
this,
...
){
this$.name;
},
private=FALSE,
protected=FALSE,
overwrite=FALSE,
conflict="warning",
validators=getOption("R.methodsS3:validators:setMethodS3")
);
##
## Method: setName
##
###########################################################################/**
#
# @RdocMethod setName
#
# @title "Set the name of a QMatrix object"
#
# \description{
# @get "title".
# }
#
# @synopsis
#
# \arguments{
# \item{this}{A QMatrix object.}
# \item{value}{A character vector of length one.}
# \item{...}{Not used.}
# }
#
# \value{
# The new object name.
# }
#
# \examples{
# # create a QMatrix object
# m<-QMatrix()
# # set/get name
# setName(m,"Susie Q")
# getName(m)
# # set/get name via virtual field
# m$name<-"Q"
# m$name
# }
#
# @author
#
# \seealso{
# @seeclass
# }
#
#*/###########################################################################
setMethodS3(
"setName",
class="QMatrix",
function(
this,
value,
...
){
.checkWriteProtection(this);
if(missing(value)){
throw("No new value provided!\n");
}
else{
value<-as.character(value);
if(stringLength(value) == 0){
throw("Cannot set empty name!");
} else {
this$.name<-value;
}
}
return(this$.name);
},
private=FALSE,
protected=FALSE,
overwrite=FALSE,
conflict="warning",
validators=getOption("R.methodsS3:validators:setMethodS3")
);
##
## Method: getProcess
##
###########################################################################/**
#
# @RdocMethod getProcess
#
# @title "Get the process object associated with a QMatrix object"
#
# \description{
# @get "title".
# }
#
# @synopsis
#
# \arguments{
# \item{this}{A QMatrix object.}
# \item{...}{Not used.}
# }
#
# \value{
# A process object, most likely one which inherits from GeneralSubstitution.
# }
#
# \examples{
# # Create a GeneralSubstitution object
# p<-GeneralSubstitution(alphabet=BinaryAlphabet())
# p
# # get the associated QMatrix object from p
# m<-p$qMatrix
# summary(m)
# # get the associated process from m
# m$process
# # clone p
# pp<-clone(p)
# # assotiate m with pp
# pp$qMatrix<-m
# # assotiate pp with m
# m$process<-pp
# m$process
# }
#
# @author
#
# \seealso{
# @seeclass
# }
#
#*/###########################################################################
setMethodS3(
"getProcess",
class="QMatrix",
function(
this,
...
){
this$.process;
},
private=FALSE,
protected=FALSE,
overwrite=FALSE,
conflict="warning",
validators=getOption("R.methodsS3:validators:setMethodS3")
);
##
## Method: setProcess
##
###########################################################################/**
#
# @RdocMethod setProcess
#
# @title "Assotiate a process object with a QMatrix object"
#
# \description{
# @get "title".
# }
#
# @synopsis
#
# \arguments{
# \item{this}{A QMatrix object.}
# \item{value}{A Process object.}
# \item{...}{Not used.}
# }
#
# \value{
# The Process object.
# }
#
# \examples{
# # Create a GeneralSubstitution object
# p<-GeneralSubstitution(alphabet=BinaryAlphabet())
# p
# # get the associated QMatrix object from p
# m<-p$qMatrix
# summary(m)
# # get the associated process from m
# m$process
# # clone p
# pp<-clone(p)
# # assotiate m with pp
# pp$qMatrix<-m
# # assotiate pp with m
# m$process<-pp
# m$process
# }
#
# @author
#
# \seealso{
# @seeclass
# }
#
#*/###########################################################################
setMethodS3(
"setProcess",
class="QMatrix",
function(
this,
value,
...
){
.checkWriteProtection(this);
if(missing(value)){
throw("No new value provided!\n");
}
else if (!is.Process(value)){
throw("Process object invalid!\n");
}
else {
this$.process<-value;
}
},
private=FALSE,
protected=FALSE,
overwrite=FALSE,
conflict="warning",
validators=getOption("R.methodsS3:validators:setMethodS3")
);
##
## Method: getAlphabet
##
###########################################################################/**
#
# @RdocMethod getAlphabet
#
# @title "Get the Alphabet object associated with a QMatrix object"
#
# \description{
# @get "title".
# }
#
# @synopsis
#
# \arguments{
# \item{this}{A QMatrix object.}
# \item{...}{Not used.}
# }
#
# \value{
# An Alphabet object.
# }
#
# \examples{
# # create a QMatrix object
# m<-QMatrix()
# # set the alphabet
# setAlphabet(m,NucleotideAlphabet())
# # get the alphabet
# getAlphabet(m)
# # set alphabet via virtual field
# m$alphabet<-BinaryAlphabet()
# summary(m)
# }
#
# @author
#
# \seealso{
# @seeclass
# }
#
#*/###########################################################################
setMethodS3(
"getAlphabet",
class="QMatrix",
function(
this,
...
){
this$.alphabet;
},
private=FALSE,
protected=FALSE,
overwrite=FALSE,
conflict="warning",
validators=getOption("R.methodsS3:validators:setMethodS3")
);
##
## Method: setAlphabet
##
###########################################################################/**
#
# @RdocMethod setAlphabet
#
# @title "Set the Alphabet object for a QMatrix object"
#
# \description{
# @get "title".
#
# This method rebuilds the scaled and unscaled rate matrices and so sets all rates to NA.
# }
#
# @synopsis
#
# \arguments{
# \item{this}{A QMatrix object.}
# \item{value}{An Alphabet object.}
# \item{...}{Not used.}
# }
#
# \value{
# The Alphabet object.
# }
#
# \examples{
# # create a QMatrix object
# m<-QMatrix()
# # set the alphabet
# setAlphabet(m,NucleotideAlphabet())
# # get the alphabet
# getAlphabet(m)
# # set alphabet via virtual field
# m$alphabet<-BinaryAlphabet()
# summary(m)
# }
#
# @author
#
# \seealso{
# @seeclass
# }
#
#*/###########################################################################
setMethodS3(
"setAlphabet",
class="QMatrix",
function(
this,
value,
...
){
.checkWriteProtection(this);
if(!exists(x="PSIM_FAST")){
if(missing(value)){
throw("No new value provided!\n");
}
else if(!is.Alphabet(value)) {
throw("Alphabet object invalid!\n");
}
else if(is.Process(this$.process)){
if(value != this$.process$alphabet){
throw("The new alphabet should match with the one from the subsitution process!\n");
}
}
if(is.matrix(this$.rate.matrix)){
warning("Be aware that setting a new alphabet wipes out completely the rate matrix!\n");
}
}
this$.alphabet<-value;
.buildRateMatrix(this);
return(this$.alphabet);
},
private=FALSE,
protected=FALSE,
overwrite=FALSE,
conflict="warning",
validators=getOption("R.methodsS3:validators:setMethodS3")
);
##
## Method: .nameToDim
##
setMethodS3(
".nameToDim",
class="QMatrix",
function(
this,
name,
...
){
# split the name
substitution<-rbind(strsplit(name,split="->",fixed=TRUE)[[1]]);
if(length(substitution) != 2 ) {
throw("Substitution event name was invalid!");
}
# Check if symbols are valid:
if(!hasSymbols(this$.alphabet, substitution)){
throw("All symbols must be in the alphabet!\n");
}
# Return a vector with the DIMNAMES:
colnames(substitution)<-c("from","to");
rownames(substitution)<-c("Substitution");
substitution;
},
private=FALSE,
protected=FALSE,
overwrite=FALSE,
conflict="warning",
validators=getOption("R.methodsS3:validators:setMethodS3")
);
##
## Method: .nameToDimFast
##
setMethodS3(
".nameToDimFast",
class="QMatrix",
function(
this,
name,
...
){
substitution<-rbind(strsplit(name,split="->",fixed=TRUE)[[1]]);
colnames(substitution)<-c("from","to");
rownames(substitution)<-c("Substitution");
substitution;
},
private=FALSE,
protected=FALSE,
overwrite=FALSE,
conflict="warning",
validators=getOption("R.methodsS3:validators:setMethodS3")
);
##
## Method: .dimToName
##
setMethodS3(
".dimToName",
class="QMatrix",
function(
this,
dim,
...
){
paste(dim,collapse="->");
},
private=FALSE,
protected=FALSE,
overwrite=FALSE,
conflict="warning",
validators=getOption("R.methodsS3:validators:setMethodS3")
);
##
## Method: getEventRate
##
###########################################################################/**
#
# @RdocMethod getEventRate
#
# @title "Get the unscaled rate of an event from a QMatrix object"
#
# \description{
# @get "title".
#
# This method return the element corresponding to a given event from the scaled rate matrix stored in a QMatrix object.
# The event can be specified by the inital and target states ("from" and "to" arguments), or by the
# event name ("from->to"). The event name takes precedence over the "from" and "to" arguments.
#
# This method returns NA if the resacling of the rates was not performed.
# The scaling is performed by the \code{rescaleQMatrix.GeneralSubstitution} method.
# }
#
# @synopsis
#
# \arguments{
# \item{this}{A QMatrix object.}
# \item{name}{The name of the event.}
# \item{from}{The initial state.}
# \item{to}{Target state.}
# \item{...}{Not used.}
# }
#
# \value{
# A Numeric vector of length one.
# }
#
# \examples{
# # create a QMatrix object
# # provide an Alphabet object and the rates
# m<-QMatrix(alphabet=BinaryAlphabet(), rate.list=list("0->1"=1,"1->0"=1))
# # get the unscaled rate of "0->1" by name
# getEventRate(m,"0->1") # retruns NA
# # get the unscaled rate of "0->1" by states
# getEventRate(m,from="0",to="1") # returns NA
# }
#
# @author
#
# \seealso{
# @seeclass
# }
#
#*/###########################################################################
setMethodS3(
"getEventRate",
class="QMatrix",
function(
this,
name=NA,
from=NA,
to=NA,
...
){
# Event specified by name:
if(!missing(name) & missing(from) & missing(to)){
# convert to dimnames
tmp<-.nameToDim(this, name);
# Return the rate from the rescaled matrix:
return(this$.rate.matrix[tmp[1],tmp[2]]);
}
# Event specified by from= and to=
else if(missing(name) & !missing(from) & !missing(to)){
# Check symbols:
if(!hasSymbols(this$.alphabet, c(from,to))){
throw("All symbols must be in the alphabet!\n")
}
else{
# Get the rate from the rescaled matrix:
return(this$.rate.matrix[as.character(from),as.character(to)]);
}
}
else {
throw("The substitution should be specified by name or by the \"from\" and \"to\" arguments!\n");
}
},
private=FALSE,
protected=FALSE,
overwrite=FALSE,
conflict="warning",
validators=getOption("R.methodsS3:validators:setMethodS3")
);
##
## Method: .getEventRateFast
##
setMethodS3(
".getEventRateFast",
class="QMatrix",
function(
this,
name=NA,
...
){
# convert to dimnames
tmp<-.nameToDimFast(this, name);
# Return the rate from the rescaled matrix:
return(this$.rate.matrix[tmp[1],tmp[2]]);
},
private=TRUE,
protected=FALSE,
overwrite=FALSE,
conflict="warning",
validators=getOption("R.methodsS3:validators:setMethodS3")
);
##
## Method: getRate
##
###########################################################################/**
#
# @RdocMethod getRate
#
# @title "Get an unscaled rate of an event from a QMatrix object"
#
# \description{
# @get "title".
#
# This method gets the element corresponding to a given event form the \emph{unscaled} rate matrix.
# a given event. The event can be specified by the inital and target states ("from" and "to" arguments), or by the
# event name ("from->to"). The event name takes precedence over the "from" and "to" arguments.
#
# }
#
# @synopsis
#
# \arguments{
# \item{this}{A QMatrix object.}
# \item{name}{The name of the event.}
# \item{from}{The initial state.}
# \item{to}{Target state.}
# \item{...}{Not used.}
# }
#
# \value{
# A Numeric vector of length one.
# }
#
# \examples{
# # create a QMatrix object
# # provide an Alphabet object and the rates
# m<-QMatrix(alphabet=BinaryAlphabet(), rate.list=list("1->0"=1,"0->1"=1))