Python for generating BEAST2 XML files

344 views
Skip to first unread message

Terry Jones

unread,
Mar 7, 2018, 9:25:10 PM3/7/18
to beast...@googlegroups.com
I just wrote some quick Python to generate BEAST2 XML files, either from the command line or for use in your own Python code.  See https://github.com/acorg/beast2-xml

It's currently very simplistic - there are many things that could be added. I'm not trying to replicate the whole of BEAUTi in Python, but am aiming to make it easy to programmatically (from the shell or from Python) generate BEAST2 config files without needing to use a GUI tool. The code currently has four simple templates to work from (based on clock model) if you don't provide one, all of which come straight from BEAUTi. It can make simple modifications to these. One workflow I imagine is that you set up a base template using BEAUTi, then you pass that to my code along with instructions for the modifications you want.

E.g., suppose you have a BEAST2 XML file (template.xml) you made with BEAUTi. You could write a (bash) shell loop like this:

$ for chainLength in 10000000 20000000 30000000
do
    for age in 10 20 30
    do
        for FASTA in file1 file2
        do
            beast2xml.py --templateFile template.xml --chainLength \
                $chainLength --age id1=$age --logFileBasename \
                ${chainLength}-${age}-${FASTA} \
                < $FASTA.fasta > config.xml
            beast-2 config.xml
            rm config.xml
        done
    done
done 

Or you can just use a default template, e.g., --clockModel relaxed-exponential.

There's more control if you're generating XML under Python mainly because you can use xml.etree.ElementTree (https://docs.python.org/3/library/xml.etree.elementtree.html) to manipulate the XML as much as you like.

Feedback welcome. I'd like to improve this (or, better, for you to improve it :-)), while hopefully keeping the code relatively simple.

Terry

Andrew Rambaut

unread,
Mar 8, 2018, 2:52:27 AM3/8/18
to beast...@googlegroups.com
Dear Terry,

I wrote a similar utility a while back (in Java). It is called ‘BEASTGen’ but actually can use templates for various phylogenetic input formats:


It might provide some ideas for this.

Best,
Andrew

--
You received this message because you are subscribed to the Google Groups "beast-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to beast-users...@googlegroups.com.
To post to this group, send email to beast...@googlegroups.com.
Visit this group at https://groups.google.com/group/beast-users.
For more options, visit https://groups.google.com/d/optout.

Terry Jones

unread,
Mar 8, 2018, 6:53:57 AM3/8/18
to beast...@googlegroups.com
Hi Andrew

Thanks for this - I just took a look.  I considered taking a route like that, but decided I didn't want to get the user to set up a config file to populate the template. That seemed like it was heading in the direction of just editing the XML directly (which after all is just a config file), and that I'd end up with an increasingly complicated config file (e.g., in JSON) that might come to resemble the XML in its complexity. I also took a very quick look at the BEAUTi code and saw the template files it has and its mechanism for indicating where things should be included. That might have been a better solution for me, but it also felt complicated (as someone who actually knows almost zero about the BEAST XML files). So I wrote something really simple, hoping that it wouldn't need to grow too much in complexity. The approach of letting the user make as complicated a base template as they want (using BEAUTi) and then making minor mods to it felt like a reasonable balance. It's a trade-off, of course, as you know :-)

Terry


To unsubscribe from this group and stop receiving emails from it, send an email to beast-users+unsubscribe@googlegroups.com.

To post to this group, send email to beast...@googlegroups.com.
Visit this group at https://groups.google.com/group/beast-users.
For more options, visit https://groups.google.com/d/optout.

--
You received this message because you are subscribed to the Google Groups "beast-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to beast-users+unsubscribe@googlegroups.com.

Remco Bouckaert

unread,
Mar 8, 2018, 2:18:53 PM3/8/18
to beast...@googlegroups.com
Hi Terry,

Thanks for your contribution. There have been other attempts to tackle the XML generating problem, all with a slightly different philosophy. This may be of interest:

http://www.beast2.org/2018/03/09/10-ways-to-generate-beast-xml.html

Cheers,

Remco


Terry Jones

unread,
Mar 8, 2018, 5:00:49 PM3/8/18
to beast...@googlegroups.com
Hi Remco

Thanks for adding the link :-)  It would be slightly more accurate to say it's "a Python class and command-line script to generate BEAST 2 XML files."  Also, there's a copy-paste error on the page: the BEASTShell also refers to BEAST2XML (though it links to the correct place).  Is there a link for Mixel?

Terry


Remco Bouckaert

unread,
Mar 8, 2018, 5:31:14 PM3/8/18
to beast...@googlegroups.com
Hi Terry,

Thanks for proofreading the post :-) I put in the fixes as you suggested.

I’ll check with Joseph, who wrote MiXeL whether the code is online somewhere.

Cheers,

Remco


To unsubscribe from this group and stop receiving emails from it, send an email to beast-users...@googlegroups.com.

Remco Bouckaert

unread,
Mar 14, 2018, 2:40:57 PM3/14/18
to beast...@googlegroups.com
Hi Terry, 


Cheers,

Remco

Terry Jones

unread,
Mar 15, 2018, 11:21:23 AM3/15/18
to beast...@googlegroups.com
Hi Remco - great, thanks a lot for chasing that down. I'll take a look.

Terry

Camila Duitama

unread,
Jun 6, 2025, 2:03:15 PM6/6/25
to beast-users

Hi!  

I am new to BEAST, and I will be conducting numerous experiments and replicates; therefore, I prefer not to use the GUI of BEAUti to produce the XML files. Is there a more standard method (since the last time this post was commented) to generate XML files for the latest version of BEAST (Beastv2.7.7) from the alignment files? I have tried Beast2XML (mentioned in this list: https://www.beast2.org/2018/03/09/10-ways-to-generate-beast-xml.html ) without success so far, as the format does not seem to be appropriate for the version of BEAST I am using.

Thanks,  

Camila
To unsubscribe from this group and stop receiving emails from it, send an email to beast-users...@googlegroups.com.

To post to this group, send email to beast...@googlegroups.com.
Visit this group at https://groups.google.com/group/beast-users.
For more options, visit https://groups.google.com/d/optout.

--
You received this message because you are subscribed to the Google Groups "beast-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to beast-users...@googlegroups.com.

To post to this group, send email to beast...@googlegroups.com.
Visit this group at https://groups.google.com/group/beast-users.
For more options, visit https://groups.google.com/d/optout.

--
You received this message because you are subscribed to the Google Groups "beast-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to beast-users...@googlegroups.com.

To post to this group, send email to beast...@googlegroups.com.
Visit this group at https://groups.google.com/group/beast-users.
For more options, visit https://groups.google.com/d/optout.

--
You received this message because you are subscribed to the Google Groups "beast-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to beast-users...@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages