Ka/Ks and dn/ds are usually considered to be the same thing (also named ω).
However, estimation of KaKs using seqinr and dnds using ape give different results. The two estimators refer to the same paper for details, although seqinr also refer to other papers.
Calling dnds function, we can browse the code and understand the computation:
dS <- (L[2] * A[2] + L[3] * A[3])/sum(L[2:3]) + B[3]
dN <- A[1] + (L[1] * B[1] + L[2] * B[2])/sum(L[1:2])
I wasn't able to dig anything useful in the code of kaks, but the help page illustrates how Ka and Ks are calculated:
Ks = (L2.A2 + L4.A4) / (L2 + L4) + B4
Ka = A0 + (L0.B0 + L2.B2) / (L0 + L2)
Were 0 is nonsynonymous, 2 is 2-fold synonymous and 4 is 4-fold synonymous. If we assume that in dnds they are saved in position 1, 2, and 3 respectively, the computation seems identical.
So, why even on a very simple sequence like the one I am linking here the results are different?
You can download the file, save it as "test.aln", and then run the code below and you should obtain 0.291 for Ka/Ks and 0.417 for dn/ds.
Am I doing something wrong? Any insight would be useful!
library(seqinr)
library(ape)
myal<-read.alignment("test.aln",format="clustal",forceToLower=FALSE)
myDNA<-as.DNAbin(myal)
mykaks<-kaks(myal)
dnds(myDNA)
mykaks$ka/mykaks$ks