/cons_test/codon_funcs.R

http://github.com/sbotond/phylosim · R · 110 lines · 83 code · 15 blank · 12 comment · 13 complexity · 006265e0e1ba892410bfea894246da5c MD5 · raw file

  1. dir<-"aln_codon";
  2. system(paste("mkdir",dir));
  3. # construct substitution process object
  4. gy94<-GY94(kappa=gy94.true.kappa);
  5. construct_root_sequence<-function(len){
  6. # construct root sequence object
  7. seq<-CodonSequence(length=len);
  8. # attach process
  9. attachProcess(seq, gy94);
  10. return(seq);
  11. }
  12. # simulate nucleotide data
  13. simulate_codon<-function(phylo,seq,len,rep){
  14. # set all site states to NA
  15. clearStates(seq)
  16. # set omega
  17. omegaVarM0(seq,gy94,omega=gy94.true.omega);
  18. # sample states
  19. sampleStates(seq);
  20. # construct simulation object
  21. sim<-PhyloSim(
  22. phylo=phylo,
  23. root.seq=seq
  24. );
  25. # scale tree:
  26. scaleTree(sim,sf);
  27. # run simulation
  28. Simulate(sim,quiet=TRUE);
  29. # save alignment
  30. fname<-paste(dir,"/codonsim_",len,"_",rep,".fas",sep="");
  31. saveAlignment(sim,file=fname,skip.internal=TRUE);
  32. return(fname);
  33. }
  34. # estimate nucleotide model parameters
  35. estimate_codon<-function(phylo, len,rep){
  36. old.wd<-getwd();
  37. dir.name<-paste(paste("codonsim_",len,"_",rep,sep=""));
  38. dir.create(dir.name);
  39. setwd(dir.name);
  40. system(paste("(seqret -osf phylipnon -outseq sim.phy ../",dir,"/",dir.name,".fas 2>&1) >/dev/null",sep=""));
  41. system("codeml ../ctl/codeml_codon.ctl >/dev/null");
  42. lines<-file.slurp("mlc");
  43. res.tree<-NA;
  44. res.alpha<-NA;
  45. i<-1;
  46. while(i < length(lines)){
  47. if(length(grep(pattern="^tree length =",x=lines[[i]],perl=TRUE,value=FALSE)) > 0){
  48. res.tree<-lines[[i+4]];
  49. i<-i+4;
  50. }
  51. else if(length(grep(pattern="^kappa ",x=lines[[i]],perl=TRUE,value=FALSE)) > 0){
  52. res.kappa<-as.numeric(strsplit(lines[[i]],"=",fixed=TRUE)[[1]][[2]]);
  53. i<-i+1;
  54. }
  55. else if(length(grep(pattern="^omega ",x=lines[[i]],perl=TRUE,value=FALSE)) > 0){
  56. res.omega<-as.numeric(strsplit(lines[[i]],"=",fixed=TRUE)[[1]][[2]]);
  57. i<-i+1;
  58. } else {
  59. i<-i+1;
  60. }
  61. }
  62. cat(res.tree,file="res.nwk");
  63. res.tree<-reorder(read.tree("res.nwk"),"pruningwise");
  64. setwd(old.wd);
  65. system(paste("rm -fr",dir.name));
  66. tmp<-list();
  67. tmp$len=len;
  68. tmp$kappa=res.kappa;
  69. tmp$omega=res.omega;
  70. tmp$tree=res.tree;
  71. return(list(tmp));
  72. }
  73. plot_dframe<-function(d,p){
  74. t<-reorder(p,"pruningwise");
  75. for(col in names(d)){
  76. pl<-my_qplot(d,col);
  77. true<-NA;
  78. if(col=="len"){
  79. next();
  80. }
  81. else if (col=="kappa"){
  82. true<-gy94.true.kappa;
  83. }
  84. else if (col=="omega"){
  85. true<-gy94.true.omega;
  86. }
  87. else {
  88. edge.nr<-as.numeric(strsplit(col,"_")[[1]][[2]]);
  89. true<-t$edge.length[edge.nr];
  90. }
  91. pl<-hline(pl,true);
  92. print(pl);
  93. Sys.sleep(10);
  94. }
  95. }