-
Notifications
You must be signed in to change notification settings - Fork 13
/
CodonModels.R
1306 lines (1182 loc) · 28.8 KB
/
CodonModels.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.
##
##
## CodonUNREST
##
##########################################################################/**
#
# @RdocClass CodonUNREST
#
# @title "The CodonUNREST class"
#
# \description{
# This class implements a time-continuous Markov process acting on a state
# space defined by the symbol set of a CodonAlphabet object. The rate matrix of this model
# is unrestricted, so it can be used to implement empircal codon models or more
# restricted mechanistic models.
#
# @classhierarchy
# }
#
# @synopsis
#
# \arguments{
# \item{name}{The name of the object.}
# \item{table.id}{The identifier of the genetic code table (see \code{CodonAlphabet}).}
# \item{rate.list}{A list of unscaled substitution rates (see \code{setRateList.GeneralSubstitution}).}
# \item{equ.dist}{Equilibrium distribution.}
# \item{...}{Additional arguments.}
# }
#
# \section{Fields and Methods}{
# @allmethods
# }
#
# \examples{
# # create a CodonUNREST object
# p<-CodonUNREST(table.id=2)
# # get object summary
# summary(p)
# }
#
# @author
#
# \seealso{
# GeneralSubstitution GY94
# }
#
#*/###########################################################################
setConstructorS3(
"CodonUNREST",
function(
name="Anonymous", # name of the object
table.id=1, # the id of the genetic code table to use
rate.list=NA, # list of unscaled rates
equ.dist=NA, # equlibrium distribution
...
) {
got.rate.list<-!missing(rate.list);
got.equ.dist<-!missing(equ.dist);
this<-NA;
# Got rate list and equlibrium distribution:
if(got.rate.list & got.equ.dist){
this<-GeneralSubstitution(
name=name,
alphabet=CodonAlphabet(table.id=table.id),
rate.list=rate.list,
equ.dist=equ.dist
);
}
# Got rate list
else if(got.rate.list & !got.equ.dist){
this<-GeneralSubstitution(
name=name,
alphabet=CodonAlphabet(table.id=table.id),
rate.list=rate.list
);
}
# Got equlibrium distribution,
# we set it, but it will be owerwritten anyway.
else if(!got.rate.list & got.equ.dist){
this<-GeneralSubstitution(
name=name,
alphabet=CodonAlphabet(table.id=table.id),
equ.dist=equ.dist
);
}
# Got nothing:
else if(!got.rate.list & !got.equ.dist){
this<-GeneralSubstitution(
name=name,
alphabet=CodonAlphabet(table.id=table.id)
);
}
this<-extend(this, "CodonUNREST");
return(this);
},
enforceRCC=TRUE
);
##
## Method: checkConsistency
###########################################################################/**
#
# @RdocMethod checkConsistency
#
# @title "Check object consistency"
#
# \description{
# @get "title".
# }
#
# @synopsis
#
# \arguments{
# \item{this}{An 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="CodonUNREST",
function(
this,
...
){
wp<-this$writeProtected;
if (wp) {
this$writeProtected<-FALSE;
}
may.fail<-function(this) {
if(!inherits(this$alphabet, "CodonAlphabet")){
throw("This object must have as alphabet a CodonAlphabet object!\n");
}
}
tryCatch(may.fail(this),finally=this$writeProtected<-wp);
NextMethod();
},
private=FALSE,
protected=FALSE,
overwrite=FALSE,
conflict="warning",
validators=getOption("R.methodsS3:validators:setMethodS3")
);
##
# Method: is.CodonUNREST
##
###########################################################################/**
#
# @RdocDefault is.CodonUNREST
#
# @title "Check whether an object inherits from CodonUNREST"
#
# \description{
# @get "title".
# }
#
# @synopsis
#
# \arguments{
# \item{this}{An object.}
# \item{...}{Not used.}
#
# }
#
# \value{
# TRUE of FALSE.
# }
#
# \examples{
# # create some objects
# p<-CodonUNREST()
# pp<-GTR()
# # check if they inherit from CodonUNREST
# is.CodonUNREST(p)
# is.CodonUNREST(pp)
# }
#
# @author
#
#*/###########################################################################
setMethodS3(
"is.CodonUNREST",
class="default",
function(
this,
...
){
if(!is.GeneralSubstitution(this)) {return(FALSE)}
if ( inherits(this, "CodonUNREST")) {
return(TRUE);
} else {
return(FALSE)
}
},
private=FALSE,
protected=FALSE,
overwrite=FALSE,
conflict="warning",
validators=getOption("R.methodsS3:validators:setMethodS3")
);
##
## GY94
##
##########################################################################/**
#
# @RdocClass GY94
#
# @title "The GY94 class"
#
# \description{
# This class implements the codon substitution model of Goldman and Yang (1994).
# The transition/transversion rate ratio is stored in the \code{kappa} virtual field.
# The nonsynonymous/synonymous substitution rate ratio (\code{omega}) is a site-process specific parameter
# with a default value of one.
# Hence, after the attachment of the process the variation of omega ratios among sites follows
# the M0 model (see Yang et al. 2000).
#
# The rate matrix of the \code{\link{GY94}} model is scaled in a way that the expected number
# of potential substiutions per site is equal to one at equlibrium.
# The \emph{codeml} program from the PAML package scales the rate matrix in order to have
# the expected number of accepted substiutions per site equal to one. Use the
# \code{\link{getOmegaScalingFactor.GY94}} method to claculate a branch length scaling factor
# which allows to switch to a PAML-style scaling given an average omega.
#
# If the \code{scale.nuc} constructor argument is TRUE, the rates of the returned \code{Event} objects
# will be multiplied by \code{3} to obtain a process which has the expected number of nucleotide substitutions
# (not \code{codon} substitutions) equal to one at equilibrium. This is useful when simulating
# mixed sequences. This option doesn't affect the rate matrix in any way.
#
# The M1-M4 models are implemented in the \code{omegaVarM[1-4].CodonSeqeunce} methods.
# Simulations under more complex models (M5-M13) can be achieved by first discretizing them
# using the \code{M5-13} tool from the INDELible software
# package (\url{http://abacus.gene.ucl.ac.uk/software/indelible/}).
# After discretization, the M5-M13 models can be simulated through the M3 (discrete) model.
#
# @classhierarchy
# }
#
# \references{
# Goldman, N., Yang, Z. (1994) A codon-based model of nucleotide substitution for protein-coding DNA sequences - Mol Biol Evol 11(5):725-36 \url{http://bit.ly/aSVEoa}
#
# Yang, Z., Nielsen, R., Goldman, N., Pedersen Krabbe, A-M. (2000) Codon-Substitution Models for Heterogeneous Selection Pressure at Amino Acid Sites - Genetics 155:431-449 \url{http://bit.ly/bvjucn}
# }
#
# @synopsis
#
# \arguments{
# \item{name}{Name of the object.}
# \item{table.id}{The identifier of the genetic code table to use (1 by default).}
# \item{kappa}{The transition/transversion rate ratio (1 by default).}
# \item{omega.default}{The default value of the omega site-process specific parameter (1 by default).}
# \item{codon.freqs}{A vector of codon frequencies.}
# \item{scale.nuc}{Scale to nucleotide substitutions if TRUE (see above).}
# \item{...}{Additional arguments.}
# }
#
# \section{Fields and Methods}{
# @allmethods
# }
#
# \examples{
# # create a GY94 object
# p<-GY94(kappa=2)
# # check if inherits from GY94
# is.GY94(p)
# # get object summary
# summary(p)
# # display a bubble plot
# plot(p)
# # create a codon sequence, attach process
# s<-CodonSequence(length=5, processes=list(list(p)))
# # sample states
# sampleStates(s)
# # make first three positions invariable
# setRateMultipliers(s,p,0,1:3)
# # sample omega values from the M3 (discrete) model.
# omegaVarM3(s,p,omegas=c(0,1,2,3),probs=c(2/5,1/5,1/5,1/5))
# # get a histogram of omega values in s
# omegaHist(s,p,breaks=50)
# sim<-PhyloSim(root.seq=s,phylo=rcoal(2))
# # run simulation
# Simulate(sim)
# # get the list of recorded per-branch event counts
# getBranchEvents(sim)
# # export the number of synonymous substitutions as a phylo object
# syn.subst<-exportStatTree(sim,"nr.syn.subst")
# syn.subst
# # plot the exported phylo object
# plot(syn.subst)
# # print alignment
# sim$alignment
# }
#
# @author
#
# \seealso{
# CodonUNREST GeneralSubstitution CodonSequence GTR WAG
# }
#
#*/###########################################################################
setConstructorS3(
"GY94",
function(
name="Anonymous", # name of the object
table.id=1, # id of the genetic code table to use
kappa=1, # transition/transversion rate ratio
omega.default=1, # the default value of the omega site-process specific parameter
codon.freqs=NA, # codon frequencies
scale.nuc=FALSE, # scale Q matrix to nucleotide substitutions
...
) {
# Create a CodonUNREST object.
this<-CodonUNREST(table.id=table.id);
# Set codon frequencies to uniform if they are not provided:
if(missing(codon.freqs)){
codon.freqs<-rep((1/this$alphabet$size),this$alphabet$size);
}
# Extend:
this<-extend(
this,
"GY94",
.kappa=NA,
.is.ny98=TRUE,
.scale.const=as.double(1.0),
.syn.cache=NA
);
# Add the "omega" site-process specific parameter:
.addSiteSpecificParameter(
this,
id="omega",
name="Omega",
value=as.double(omega.default),
type="numeric"
);
# Set the codon frequencies/equilibrium distribution.
setEquDist(this,value=codon.freqs,force=TRUE);
# Set kappa:
this$kappa<-kappa;
# Scale to nucleotide if requested:
if(scale.nuc){
this$.scale.const<-as.double(3.0);
}
# Set object name:
this$name<-name;
# Force clearing id cache:
this$name<-this$name;
# create syn/nsyn matrix cache
# Get translation table:
trans.table<-this$.alphabet$.trans.table;
symbols<-this$.alphabet$symbols;
syn.cache<-matrix(nrow=this$.alphabet$size,ncol=this$.alphabet$size);
colnames(syn.cache)<-symbols;
rownames(syn.cache)<-symbols;
for(i in symbols){
for(j in symbols){
if(i == j) { next }
if( (trans.table[[i]]$aa) == (trans.table[[j]]$aa) ){
syn.cache[i,j]<-1;
}
else{
syn.cache[i,j]<-0;
}
}
}
this$.syn.cache<-syn.cache;
return(this);
},
enforceRCC=TRUE
);
##
## Method: is.GY94
##
###########################################################################/**
#
# @RdocDefault is.GY94
#
# @title "Check whether an object inherits from GY94"
#
# \description{
# @get "title".
# }
#
# @synopsis
#
# \arguments{
# \item{this}{An object.}
# \item{...}{Not used.}
# }
#
# \value{
# TRUE of FALSE.
# }
#
# \examples{
# # create some objects
# p<-CodonUNREST()
# pp<-GY94()
# # check if they inherit from CodonUNREST
# is.GY94(p)
# is.GY94(pp)
# }
#
# @author
#
#*/###########################################################################
setMethodS3(
"is.GY94",
class="default",
function(
this,
...
){
if(!is.PSRoot(this)) {return(FALSE)}
if(!is.null(this$.is.ny98)){return(TRUE)}
if ( inherits(this, "GY94")) {
this$.is.process<-TRUE;
return(TRUE);
} else {
return(FALSE)
}
},
private=FALSE,
protected=FALSE,
overwrite=FALSE,
conflict="warning",
validators=getOption("R.methodsS3:validators:setMethodS3")
);
##
## Method: getEventsAtSite.GY94
##
###########################################################################/**
#
# @RdocMethod getEventsAtSite
#
# @title "Generate the list of active Event objects for a given attached Site object"
#
# \description{
# @get "title".
#
# This method is almost an exact duplicate of the getEventsAtSite.GeneralSubstitution,
# with the exception of the portions dealing with the omega site-process specific parameter.
#
# }
#
# @synopsis
#
# \arguments{
# \item{this}{A GY94 object.}
# \item{target.site}{A Site object. The GY94 object must be attached to the Site object.}
# \item{...}{Not used.}
# }
#
# \value{
# A list of the active Event objects.
# }
#
#
# @author
#
# \seealso{
# getEventsAtSite.GeneralSubstitution GeneralSubstitution
# }
#
#*/###########################################################################
setMethodS3(
"getEventsAtSite",
class="GY94",
function(
this,
target.site,
...
){
if (!exists(x="PSIM_FAST")) {
if(!is.Site(target.site)) {
throw("Target site invalid!\n");
}
if(is.na(this$.q.matrix)){
throw("Cannot provide event objects because the rate matrix is not set!\n");
}
if(!is.numeric(this$.equ.dist)){
throw("Cannot provide event objects because the equilibrium frequencies are not defined!\n");
}
}
state<-as.character(target.site$.state);
# Just return an empty list if the state is NA:
if(is.na(state)){
return(list());
}
# Get rate matrix:
rate.matrix<-this$.q.matrix$.rate.matrix;
# Get translation table:
trans.table<-target.site$.alphabet$.trans.table;
# Get scaling constant:
scale.const<-this$.scale.const;
# get syn cache:
syn.cache<-this$.syn.cache;
symbols<-this$.alphabet$.symbols;
rest<-symbols[ which(symbols != state) ];
# Create the event objects:
events<-list();
# The rate of the event is the product of the general rate and the
# site-process specific rate multiplier:
rate.multiplier<-target.site$.processes[[this$.id]]$site.params[["rate.multiplier"]]$value;
# Return empty list if the rate multiplier is zero.
if(rate.multiplier == 0 ) {
return(list());
}
# Get the omega site-process specific parameter:
omega<-target.site$.processes[[this$.id]]$site.params[["omega"]]$value;
for(new.state in rest){
# Get the base rate:
base.rate<-rate.matrix[state,new.state];
# Skip event if base rate is zero:
if(base.rate == 0){
next;
}
name<-paste(state,new.state,sep="->");
# Clone the event template object:
event<-clone(this$.event.template);
# Set event name:
event$.name<-name;
# Set the generator process:
event$.process<-this;
# Set the target position passed in a temporary field,
# Event objects are not aware of their posiitions in general!
event$.position<-target.site$.position;
# Set the target site:
event$.site<-target.site;
# Figure out wether the event is a synonymous mutation ...
if( syn.cache[state,new.state] ){
# and ignore omega in that case
event$.rate<-(scale.const * rate.multiplier * base.rate);
# Mark substitution as synonymous.
event$.type<-"synonymous";
} else {
# incorporate omega otherwise
event$.rate<-(scale.const * rate.multiplier * omega * base.rate);
# Mark substitution as non-synonymous.
event$.type<-"non-synonymous";
}
# Set the handler for the substitution event:
event$.handler<-this$.handler.template;
# Add to events list:
events<-c(events, list(event));
}
return(events);
},
private=FALSE,
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{...}{Not used.}
# }
#
#
# \value{
# Returns an invisible TRUE if no inconsistencies found in the object, throws
# an error otherwise.
# }
#
# @author
#
# \seealso{
# @seeclass
# }
#
#*/###########################################################################
setMethodS3(
"checkConsistency",
class="GY94",
function(
this,
...
){
wp<-this$writeProtected;
if (wp) {
this$writeProtected<-FALSE;
}
may.fail<-function(this) {
# Check kappa:
if(!is.numeric(this$.kappa)){
throw("Kappa must be numeric!\n");
}
# Check codon.freqs
this$codonFreqs<-this$codonFreqs;
# Check rate sanity:
symbols<-this$alphabet$symbols;
alphabet<-this$alphabet;
for(from in symbols){
for(to in symbols){
# Skip diagonal elements:
if(from == to) {next()};
# Figure out codon differences:
diff<-sort(.codonDiff(alphabet, c(from,to)));
# Single transition:
if( all(diff == sort(c(0,0,"TI"))) ){
if( !PSRoot$my.all.equal ( this$.q.matrix$.orig.matrix[from, to], (this$.kappa * this$.equ.dist[1,to]) ) ){
throw("GY94 rate inconsistency. From:",from," To:",to,"!\n");
}
}
# Single transversion:
else if( all(diff == sort(c(0,0,"TV"))) ){
if( !PSRoot$my.all.equal ( this$.q.matrix$.orig.matrix[from, to], (this$.equ.dist[1,to]) ) ){
throw("GY94 rate inconsistency. From:",from," To:",to,"!\n");
}
}
# Multiple nucleotide substitution:
else {
if( this$.q.matrix$.orig.matrix[from, to] != 0.0 ){
throw("GY94 rate inconsistency. From:",from," To:",to,"!\n");
}
}
} #/for to
} #/for from
}
tryCatch(may.fail(this),finally=this$writeProtected<-wp);
NextMethod();
},
private=FALSE,
protected=FALSE,
overwrite=FALSE,
conflict="warning",
validators=getOption("R.methodsS3:validators:setMethodS3")
);
###########################################################################/**
#
# @RdocMethod getOmegaScalingFactor
#
# @title "Get the omega scaling factor"
#
# \description{
# @get "title".
#
# The rate matrix of the \code{\link{GY94}} model is scaled in a way that the expected number
# of potential substiutions per site is equal to one at equlibrium.
# The \emph{codeml} program from the PAML package scales the rate matrix in order to have
# the expected number of accepted substiutions per site equal to one.
#
# This method calculates the branch length multiplier needed for switching
# to PAML-style scaling given a fixed omega.
#
# }
#
# @synopsis
#
# \arguments{
# \item{this}{A GY94 object.}
# \item{omega}{The value of omega.}
# \item{...}{Not used.}
# }
#
# \value{
# A numeric vector of length one.
# }
#
# \examples{
# # construct a GY94 process object
# p<-GY94(kappa=4)
# # Calculate scaling factor for omega=2
# getOmegaScalingFactor(p,omega=2)
# }
#
# @author
#
# \seealso{
# @seeclass
# }
#
#*/###########################################################################
setMethodS3(
"getOmegaScalingFactor",
class="GY94",
function(
this,
omega,
...
){
if(missing(omega)){
throw("No omega provided!");
}
if(!is.numeric(omega)){
throw("Omega must be numeric!");
}
neutral.K<-0.0;
K <- 0.0;
# get the symbols:
symbols<-this$.alphabet$symbols;
# Get translation table and rate matrix:
trans.table<-this$.alphabet$.trans.table;
rate.matrix<-this$.q.matrix$.rate.matrix;
# For every symbol:
for (i in symbols) {
# Get the equlibrium probability:
i.equ<-this$.equ.dist[[ which(colnames(this$.equ.dist) == i) ]];
for(j in symbols){
if(i == j){next}
base.rate<-rate.matrix[i,j];
neutral.K<- neutral.K + (i.equ * base.rate);
if( (trans.table[[i]]$aa) == (trans.table[[j]]$aa) ){
K <- K + (i.equ * base.rate);
}
else {
K <- K + (i.equ * omega * base.rate);
}
}
}
return(neutral.K/K);
},
private=FALSE,
protected=FALSE,
overwrite=FALSE,
conflict="warning",
validators=getOption("R.methodsS3:validators:setMethodS3")
);
##
## Method: .buildGY94Rates
##
setMethodS3(
".buildGY94Rates",
class="GY94",
function(
this,
...
){
# Wiping out the rate matrices to prevent rescaling after
# modifying an individual rate. This could be more elegant.
# Wiping out the original rate matrix:
this$QMatrix$.orig.matrix[]<-NA;
# Wiping out the scaled rate matrix:
this$QMatrix$.rate.matrix[]<-NA;
alphabet<-this$.alphabet;
symbols<-alphabet$symbols;
purines<-c("A","G");
pyrimidines<-c("C","T");
for(i in symbols){
for(j in symbols){
# Split codons:
nuc.i<-strsplit(i,"",fixed=TRUE)[[1]];
nuc.j<-strsplit(j,"",fixed=TRUE)[[1]];
diff<-which( (nuc.i == nuc.j) == FALSE);
# Skip diagonal elements:
if( i == j) {
next;
}
else if( length( diff ) > 1){
# We have multiple nucleotiode substiutions:
this$.q.matrix$.orig.matrix[i,j]<-0;
}
else if (
length( intersect( purines, c(nuc.i[diff[1]],nuc.j[diff[1]])) ) == 2 |
length( intersect( pyrimidines, c(nuc.i[diff[1]],nuc.j[diff[1]])) ) == 2
){
# We have a single transition:
this$.q.matrix$.orig.matrix[i,j]<-(this$.kappa * this$.equ.dist[1,j]);
} else {
# The only possibility left is a single transversion:
this$.q.matrix$.orig.matrix[i,j]<-(this$.equ.dist[1,j]);
}
} # /for j
} # /for i
# Set the new diagonal element in the original rates matrix:
for(codon in symbols){
this$.q.matrix$.orig.matrix[codon, codon]<-.calculateDiagonal(this$.q.matrix, symbol=codon);
}
# Call rate rescaling, suppress equlibrium distribution guessing:
.callRateRescaling(this$.q.matrix,guess.equ=FALSE);
},
private=FALSE,
protected=FALSE,
overwrite=FALSE,
conflict="warning",
validators=getOption("R.methodsS3:validators:setMethodS3")
);
##
## Method: setRate
##
###########################################################################/**
#
# @RdocMethod setRate
#
# @title "Set an unscaled rate for an event from a GY94 object"
#
# \description{
# @get "title".
#
# See \code{\link{setRate.GeneralSubstitution}}.
# }
#
# @synopsis
#
# \arguments{
# \item{this}{A GeneralSubstitution object.}
# \item{name}{The name of the event.}
# \item{from}{The initial state.}
# \item{value}{The new value of the rate.}
# \item{to}{Target state.}
# \item{...}{Not used.}
# }
#
# \value{
# A Numeric vector of length one.
# }
#
#
# @author
#
# \seealso{
# @seeclass
# }
#
#*/###########################################################################
setMethodS3(
"setRate",
class="GY94",
function(
this,
name=NA,
value,
from=NA,
to=NA,
...
){
.checkWriteProtection(this);
# Setting unscaled rate:
if(!exists(x="PSIM_FAST")) {
if(!is.QMatrix(this$.q.matrix)){
throw("Cannot set rate as the rate matrix is undefined!\n");
}
}
if(!missing(name) & missing(from) & missing(to)){
return(setRate(this$.q.matrix, name=name, value=value,guess.equ=FALSE));
}
else if (missing(name) & !missing(from) & !missing(to)){
return(setRate(this$.q.matrix, from=from, to=to, value=value,guess.equ=FALSE));
}
},
private=FALSE,
protected=FALSE,
overwrite=FALSE,
conflict="warning",
validators=getOption("R.methodsS3:validators:setMethodS3")
);
##
## Method: getKappa
##
###########################################################################/**
#
# @RdocMethod getKappa
#
# @title "Get the transition/transversion rate ratio"
#
# \description{
# @get "title".
# }
#
# @synopsis
#
# \arguments{
# \item{this}{A GY94 object.}