/cons_test/aa_funcs.R

http://github.com/sbotond/phylosim · R · 99 lines · 74 code · 15 blank · 10 comment · 8 complexity · d3dde47519c637bc6c1402d391d5c816 MD5 · raw file

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