Error with using mrbayes() wrapper from R package ips
1
1
Entering edit mode
7.8 years ago
ecolonaut ▴ 100

I am new to bioinformatics, but have been using R for some time. I'd like to use R to run some phylogentic analysis, specifically I want to call Mr. Bayes through the mrbayes() wrapper in the ips package written and maintained by Christoph Heibl to make reproducible results and easily manageable scripts (for me). I have downloaded the .exe and can work through/interpret tutorial examples with the Mr. Bayes interface. However, when I make block code in R and attempt to run the MCMCs I get an error. How can I get R to properly call Mr. Bayes and allow me to run the MCMCs from within R? See below for my attempt, mostly lifted from the CRAN ips vignette.

    # get some data included in the ips package
     data(ips.cox1)

    # truncate the seqences for a quick analysis
    ips <- ips.cox1[, 100:140]

    # set the working directory to the .exe location
    setwd("C:/Users/Gregory/Programs/MrBayes/")

    # set parameters, create the Mr. Bayes block code, and run the MCMCs
    mrbayes(ips, file = "", ngen = 100, run = TRUE)

    ###NEXUS

    ###[created by ips on Wed Feb 08 14:57:29 2017]

    ###begin data;
        ###dimensions ntax=26 nchar=41;
        ###format datatype=dna missing=N gap=-;
   ### matrix
    ###Ips_avulsus                ctcttagggtttgtagtttgagctcaccacatatttacagt
    ###Ips_confusus               cttctaggctttgtagtatgagcccaccatatattcacagt
    ###Ips_cribricollis           ttattaggatttgtagtatgagcccatcatatattcacagt
    ###Ips_borealis               cttctaggttttgtagtttgagctcatcatatatttacagt
    ###Ips_pilifrons              cttctaggttttgtagtttgagctcatcatatatttacagt
    ###Ips_nitidus                cttttaggatttgtagtatgagcccaccatatatttacagt
    ###Ips_sexdentatus            cttttaggttttgtagtttgagctcaccatatatttaccgt
    ###Ips_hoppingi               cttctaggctttgtagtatgagcccaccatatattcacagt
    ###Ips_perturbatus            cttctaggttttgtagtttgagctcaccatatatttacagt
    ###Ips_acuminatus             cttttaggatttgtggtctgagcacaccacatattcacagt
    ###Ips_apache                 ctacttggatttgtagtctgagctcaccatatattcacagt
    ###Ips_bonanseai              cttttaggatttgtcgtgtgagctcatcatatattcacagt
    ###Ips_cembrae                nnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnn
    ###Ips_duplicatus             agattttggcttcttcccccctctctagccctccttatcat
    ###Ips_typographus            cttttaggatttgtagtatgagctcaccatatatttacagt
    ###Ips_hunteri                cttctaggttttgtagtttgagctcaccatatatttacagt
    ###Ips_shangrila              ctcttaggtttcgtagtttgagctcatcatatatttacagt
    ###Ips_subelongatus           cttttaggcttcgttgtatgagcacatcatatatttacagt
    ###Ips_tridens                cttctaggttttgtagtttgagctcatcatatatttacagt
    ###Ips_amitinus               cttttaggatttgtagtatgagctcaccatatatttaccgt
    ###Orthotomicus_mannsfeldi    nnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnn
    ###Orthotomicus_suturalis     ttattaggatttgtagtttgagctcaccacatatttactat
    ###Orthotomicus_proximus      ctactaggcttcgtagtgtgagcccaccacatattcactgt
    ###Orthotomicus_erosus        cttctgggtttcgtggtgtgggctcaccacatattcaccgt
    ###Pityogenes_bistridentatus  cttctaggatttgtagtttgagcacatcatatatttactat
    ###Pityogenes_trepanatus      ttactaggatttgttgtttgagcccaccatatattcacaat
    ###;
    ###end;

    ###begin mrbayes;
        ###lset nst=6 rates=invgamma ngammacat=4;
        ###mcmc nruns=2 ngen=100 printfreq=100 samplefreq=10 nchains=4 savebrlens=yes temp=0.2;
        ###sumt filename= burnin=10 contype=allcompat;
    ###end;

   ###Error in file(file, "r") : cannot open the connection
    ###In addition: Warning messages:
    ###1: running command 'mrbayes .bayes' had status 127 
    ###2: In file(file, "r") :
     ### cannot open file '.con.tre': No such file or directory

I do not know how to interpret the last few lines here. The NEXUS file seems to be made correctly, with all parameters set mostly to default values. I'm not sure what the file "r" is nor what status 127 is. The consensus tree cannot be called because it hasn't been made presumably.

R MrBayes ips phylogenetics • 4.1k views
ADD COMMENT
2
Entering edit mode

Is mrbayes correctly installed = in your PATH variable?

ADD REPLY
0
Entering edit mode

I'm not sure - my suspicion is that R wasn't calling Mr. Bayes properly. How can a check this? And, perhaps more importantly, how can I fix this?

ADD REPLY
0
Entering edit mode

I know it is not an answer, but don't run MrBayes on your local machine, except if it is a beast (ho ho). Use CIPRES supercluster which runs MrBayes on 8 cores. It is much faster and cheaper. There is no reason to not use it. The support is excellent and answers within the hour, sometimes.

ADD REPLY
0
Entering edit mode

Those errors look like python code to me. "r" means you open the file for reading. The software cannot find a file .con.tre, which is a rather strange name. Are you sure that the file argument in this line: mrbayes(ips, file = "", ngen = 100, run = TRUE) has to be empty?

ADD REPLY
1
Entering edit mode

UPDATE: the file argument must be included if run = TRUE because the function calls the NEXUS file named via file = to run the MCMCs.

OLD: My understanding is that it does not not have to be empty, but it is empty in the vignette. If I include a file = name then the block code (parameters and data NEXUS file) are stored as that file name in my directory, which I can successfully view afterwards. However, after the file is written the same error occurs when attempting the MCMCs.

ADD REPLY
2
Entering edit mode
7.8 years ago
ddiez ★ 2.0k

This is part of the code at the end of mrbayes() function:

if (run) {
  if (.Platform$OS.type == "unix") {
    system(paste("mb > execute", file))
  }
  else {
    system(paste("mrbayes ", file, ".bayes", sep = ""))
  }
  tr <- read.nexus(paste(file, ".con.tre", sep = ""))
  tr
}

In other words, no special measures are taken to ensure the Mr. Bayes executable can be found. Also, unfortunately, you cannot pass the path to the executable to mrbayes(). Make sure you can run Mr. Bayes from the shell command line and add the path to the PATH environmental variable in order to fix it. Alternatively you could set it in R using Sys.setenv() and Sys.getenv():

Sys.setenv(PATH = paste(Sys.getenv("PATH"), "<path-to-mrbayes", sep = ":"))
ADD COMMENT
2
Entering edit mode

Okay, I think I'm getting closer. I've edited my PATH global environmental variable to include the proper path the the mrbayes.exe. Additionally, the function specifically calls for the executable to be called "mrbayes" but the newest .zip files comes with "mrbayes_x64" and "mrbayes_x86". I changed the name of "mrbayes_x64" to simply "mrbayes" and tried to run it again. The print is mostly the same as before except mrbayes is definitely called and some additional information is included in the error message:

>Sys.getenv("PATH")
[1] "C:\\Program Files\\R\\R-3.3.2\\bin\\x64;C:\\ProgramData\\Oracle\\Java\\javapath;C:\\WINDOWS\\system32;C:\\WINDOWS;C:\\WINDOWS\\System32\\Wbem;C:\\WINDOWS\\System32\\WindowsPowerShell\\v1.0\\;C:\\Program Files (x86)\\Skype\\Phone\\;C:\\Program Files\\MiKTeX 2.9\\miktex\\bin\\x64\\;C:\\Users\\Gregory\\AppData\\Local\\Microsoft\\WindowsApps;C:\\Users\\Gregory\\Programs\\MrBayes;"

>mrbayes(ips, file = "ips", ngen = 100, run = TRUE)

                            MrBayes v3.2.6 x64

                      (Bayesian Analysis of Phylogeny)

              Distributed under the GNU General Public License


               Type "help" or "help <command>" for information
                     on the commands that are available.

                   Type "about" for authorship and general
                       information about the program.



   Executing file "ips.bayes"
   Could not open file "ips.bayes"
   Error in command "Execute"
   Will exit with signal 1 (error) because quitonerror is set to yes
   If you want control to be returned to the command line on error,
   use 'mb -i <filename>' (i is for interactive) or use 'set quitonerror=no'

Error in file(file, "r") : cannot open the connection
In addition: Warning messages:
1: running command 'mrbayes ips.bayes' had status 1 
2: In file(file, "r") :
  cannot open file 'ips.con.tre': No such file or directory

The new error is because the file that is written is not named "ips.bayes" but instead is names simply "ips" when written. I manually edited the name of the file to be "ips.bayes" and the MCMCs ran and I got some summary statistics, but there are continued issues with how the written files are named and then called:

      Setting sumt filename and outputname to ips
      Setting urn-in to 10
      Setting sumt contype to Allcompat
      Summarizing trees in files "ips.run1.t" and "ips.run2.t"
      Using relative burnin ('relburnin=yes'), discarding the first 25 % of sampled trees
      Writing statistics to files ips.<parts|tstat|vstat|trprobs|con>
      Examining first file ...
      Could not open file "ips.run1.t"
      Error in command "Sumt"
      The error occurred when reading char. 47 on line 41
         in the file 'ips.bayes'
   Error in command "Execute"
   Will exit with signal 1 (error) because quitonerror is set to yes
   If you want control to be returned to the command line on error,
   use 'mb -i <filename>' (i is for interactive) or use 'set quitonerror=no'

Error in file(file, "r") : cannot open the connection
In addition: Warning messages:
1: running command 'mrbayes ips.bayes' had status 1 
2: In file(file, "r") :
  cannot open file 'ips.con.tre': No such file or directory

Specifically, the file named "ips.run1.t" cannot be opened because it isn't created, but a file named "ips.bayes.run1.t" is. Again, after manually editing the file names to be what mrbayes expects we can proceed to the end of the analysis without errors:

      Calculating tree probabilities...

      Credible sets of trees (18 trees sampled):
         50 % credible set contains 9 trees
         90 % credible set contains 17 trees
         95 % credible set contains 18 trees
         99 % credible set contains 18 trees

   Exiting mrbayes block
   Reached end of file

   Tasks completed, exiting program because mode is noninteractive
   To return control to the command line after completion of file processing, 
   set mode to interactive with 'mb -i <filename>' (i is for interactive)
   or use 'set mode=interactive'


Phylogenetic tree with 26 tips and 24 internal nodes.

Tip labels:
    Ips_avulsus, Ips_confusus, Ips_cribricollis, Ips_borealis, Ips_pilifrons, Ips_nitidus, ...

Unrooted; includes branch lengths.

I think the function naming conventions need to be updated so it can write and call files correctly. I've emailed Christoph Heibl regarding this.

ADD REPLY
1
Entering edit mode

Thanks for the detailed follow up. The ips package has not been updated since 2014 so it is possible that changes in Mr. Bayes caused all these issues. FYI, I just installed Mr. Bayes in my OSX computer and can run it using mrbayes() in your example without problem (except fixing the path to mb), so this might be a Windows issue. (NOTE: "without problem" means, it runs until the end. Not familiar with Mr. Bayes' output so can't really tell whether everything is screwed up or not).

ADD REPLY
1
Entering edit mode

UPDATE: I got the mrbayes() wrapper to work by updating the ips package to the newest version on Github, editing the .exe file name from "mrbayes_x86" to "mrbayes", and including the path to MrBayes in my PATH:

>library(devtools); install_github("heibl/ips")
>Sys.setenv(PATH = "C:\\Users\\Gregory\\Programs\\MrBayes;")
> sessionInfo()
###R version 3.3.2 (2016-10-31)
###Platform: x86_64-w64-mingw32/x64 (64-bit)
###Running under: Windows >= 8 x64 (build 9200)

###locale:
###[1] LC_COLLATE=English_United States.1252  LC_CTYPE=English_United States.1252   
###[3] LC_MONETARY=English_United States.1252 LC_NUMERIC=C                          
###[5] LC_TIME=English_United States.1252    

###attached base packages:
###[1] stats     graphics  grDevices utils     datasets  methods   base     

###other attached packages:
###[1] ips_0.0-9        devtools_1.12.0  XML_3.98-1.5     colorspace_1.3-2
###[5] ape_4.0
ADD REPLY
1
Entering edit mode

Great. FYI, I filed an issue in the github repository. Hopefully this will be easier to get working in the future.

ADD REPLY

Login before adding your answer.

Traffic: 1150 users visited in the last hour
Help About
FAQ
Access RSS
API
Stats

Use of this site constitutes acceptance of our User Agreement and Privacy Policy.

Powered by the version 2.3.6