<?xml version="1.0" standalone="yes"?>

<!-- Generated by BEAUTi v1.10.4 Prerelease #bc6cbd9                         -->
<!--       by Alexei J. Drummond, Andrew Rambaut and Marc A. Suchard         -->
<!--       Department of Computer Science, University of Auckland and        -->
<!--       Institute of Evolutionary Biology, University of Edinburgh        -->
<!--       David Geffen School of Medicine, University of California, Los Angeles-->
<!--       http://beast.community/                                           -->
<beast version="1.10.4">
	

	<!-- The list of taxa to be analysed (can also include dates/ages).          -->
	<taxa id="taxa">
		{% for taxon in taxa %}
		<taxon id="{{ taxon.id }}">
			<date value="{{ taxon.date }}" direction="forwards" units="years"/>
		</taxon>
		{% endfor %}
	</taxa >
	
	<!-- Groups for FLC -->
	{% for group in groups %}
    <taxa id="{{ group }}">
		{% for taxon in taxa -%}
		{% if group is in taxon.id %}
		<taxon idref="{{ taxon.id }}"/>
		{% endif %}
		{%- endfor %}
    </taxa>
	{% endfor %}
	

	<!-- The sequence alignment (each sequence refers to a taxon above).         -->
	<alignment id="alignment" dataType="nucleotide">
		{% for taxon in taxa %}
		<sequence>
			<taxon idref="{{ taxon.id }}"/>
			{{ taxon.sequence }}
		</sequence>
		{% endfor %}
	</alignment>

	
	<!-- The unique patterns from 1 to end                                       -->
	<patterns id="patterns" from="1" strip="false">
		<alignment idref="alignment"/>
	</patterns>
	

	<!-- A prior assumption that the population size has grown exponentially     -->
	<!-- throughout the time spanned by the genealogy.                           -->
	<exponentialGrowth id="exponential" units="years">
		<populationSize>
			<parameter id="exponential.popSize" value="1.0" lower="0.0"/>
		</populationSize>
		<growthRate>
			<parameter id="exponential.growthRate" value="0.0"/>
		</growthRate>
	</exponentialGrowth>
	


	<!-- Generate a starting tree                                                 -->
	{% if fixedTree %}
	<fixedTree id="startingTree">
		<newick>{{fixedTree}}</newick>
	</fixedTree>
	{% else %}
	<coalescentSimulator id="startingTree">
		{% for group in groups %}
        <coalescentSimulator>
            <taxa idref="{{ group }}"/>
            <exponentialGrowth idref="exponential"/>
        </coalescentSimulator>
		{% endfor %}
        <taxa idref="taxa"/>
        <exponentialGrowth idref="exponential"/>
    </coalescentSimulator>
	{% endif %}
	

	<!-- Generate a tree model                                                   -->
	<treeModel id="treeModel">
		{% if fixedTree %}
		<newick idref="startingTree"/>
		{% else %}
		<coalescentTree idref="startingTree"/>
		{% endif %}
		<rootHeight>
			<parameter id="treeModel.rootHeight"/>
		</rootHeight>
		<nodeHeights internalNodes="true">
			<parameter id="treeModel.internalNodeHeights"/>
		</nodeHeights>
		<nodeHeights internalNodes="true" rootNode="true">
			<parameter id="treeModel.allInternalNodeHeights"/>
		</nodeHeights>
	</treeModel>

	<!-- Statistic for sum of the branch lengths of the tree (tree length)       -->
	<treeLengthStatistic id="treeLength">
		<treeModel idref="treeModel"/>
	</treeLengthStatistic>

	<!-- Statistic for time of most recent common ancestor of tree               -->
	<tmrcaStatistic id="age(root)" absolute="true">
		<treeModel idref="treeModel"/>
	</tmrcaStatistic>
	

	<!-- Taxon Sets                                                              -->
	{% for group in groups %}
	<tmrcaStatistic id="tmrca({{ group }})" absolute="false" includeStem="false">
		<mrca>
			<taxa idref="{{ group }}"/>
		</mrca>
		<treeModel idref="treeModel"/>
	</tmrcaStatistic>
	<tmrcaStatistic id="age({{ group }})" absolute="true" includeStem="false">
		<mrca>
			<taxa idref="{{ group }}"/>
		</mrca>
		<treeModel idref="treeModel"/>
	</tmrcaStatistic>
	<monophylyStatistic id="monophyly({{ group }})">
		<mrca>
			<taxa idref="{{ group }}"/>
		</mrca>
		<treeModel idref="treeModel"/>
	</monophylyStatistic>
	{% endfor %}

	<!-- Generate a coalescent likelihood                                        -->
	<coalescentLikelihood id="coalescent">
		<model>
			<exponentialGrowth idref="exponential"/>
		</model>
		<populationTree>
			<treeModel idref="treeModel"/>
		</populationTree>
	</coalescentLikelihood>


	{% if clock == 'strict' %}
	<!-- The strict clock (Uniform rates across branches)                        -->
	<strictClockBranchRates id="branchRates">
		<rate>
			<parameter id="clock.rate" value="1.0" lower="0.0"/>
		</rate>
	</strictClockBranchRates>
	{% elif clock == 'relaxed' %}
	<!-- The uncorrelated relaxed clock (Drummond, Ho, Phillips & Rambaut (2006) PLoS Biology 4, e88 )-->
	<discretizedBranchRates id="branchRates">
		<treeModel idref="treeModel"/>
		<distribution>
			<gammaDistributionModel>
				<mean>
					<parameter id="ucgd.mean" value="1.0" lower="0.0"/>
				</mean>
				<shape>
					<parameter id="ucgd.shape" value="0.3333333333333333" lower="0.0"/>
				</shape>
			</gammaDistributionModel>
		</distribution>
		<rateCategories>
			<parameter id="branchRates.categories"/>
		</rateCategories>
	</discretizedBranchRates>
	{% elif clock == 'flc-stem' %}
	<!-- The a priori local clock model (Yoder & Yang, 2000)                     -->
	<localClockModel id="branchRates">
		<treeModel idref="treeModel"/>
		<rate>
			<parameter id="clock.rate" value="1.0" lower="0.0"/>
		</rate>
		{% for group in groups %}
		<clade includeStem="true" excludeClade="true">
			<parameter id="{{ group }}.rate" value="1.0" lower="0.0"/>
			<taxa idref="{{ group }}"/>
		</clade>
		{% endfor %}
		<externalBranches>
			<parameter idref="clock.rate"/>
			{% for group in groups %}
			<taxa idref="{{ group }}"/>
			{% endfor %}
		</externalBranches>
	</localClockModel>
	{% endif %}

	{% macro branchRatesRef() %}
	{% if clock == 'strict' %}
	<strictClockBranchRates idref="branchRates"/>
	{% elif clock == 'relaxed' %}
	<discretizedBranchRates idref="branchRates"/>
	{% elif clock == 'flc-stem' %}
	<localClockModel idref="branchRates"/>
	{% endif %}
	{% endmacro %}
	
	<rateStatistic id="meanRate" name="meanRate" mode="mean" internal="true" external="true">
		<treeModel idref="treeModel"/>
		{{ branchRatesRef() }}
	</rateStatistic>
	
	<rateStatistic id="coefficientOfVariation" name="coefficientOfVariation" mode="coefficientOfVariation" internal="true" external="true">
		<treeModel idref="treeModel"/>
		{{ branchRatesRef() }}
	</rateStatistic>
	
	<rateCovarianceStatistic id="covariance" name="covariance">
		<treeModel idref="treeModel"/>
		{{ branchRatesRef() }}
	</rateCovarianceStatistic>
	

	<!-- The general time reversible (GTR) substitution model                    -->
	<gtrModel id="gtr">
		<frequencies>
			<frequencyModel dataType="nucleotide">
				<alignment idref="alignment"/>
				<frequencies>
					<parameter id="frequencies" dimension="4"/>
				</frequencies>
			</frequencyModel>
		</frequencies>
		<rates>
			<parameter id="gtr.rates" dimension="6" value="1.0" lower="0.0"/>
		</rates>
	</gtrModel>

	<!-- site model                                                              -->
	<siteModel id="siteModel">
		<substitutionModel>
			<gtrModel idref="gtr"/>
		</substitutionModel>
		<gammaShape gammaCategories="4">
			<parameter id="alpha" value="0.5" lower="0.0"/>
		</gammaShape>
	</siteModel>

	<!--                                                                         -->
	<statistic id="mu" name="mu">
		<siteModel idref="siteModel"/>
	</statistic>
	

	<!-- Likelihood for tree given sequence data                                 -->
	<treeDataLikelihood id="treeLikelihood" useAmbiguities="false">
		<partition>
			<patterns idref="patterns"/>
			<siteModel idref="siteModel"/>
		</partition>
		<treeModel idref="treeModel"/>
		{{ branchRatesRef() }}
	</treeDataLikelihood>
	

	<!-- Define operators                                                        -->
	<operators id="operators" optimizationSchedule="default">
		<deltaExchange delta="0.01" weight="1">
			<parameter idref="gtr.rates"/>
		</deltaExchange>
		<scaleOperator scaleFactor="0.75" weight="1">
			<parameter idref="alpha"/>
		</scaleOperator>
		{% if clock == 'strict' %}
		<scaleOperator scaleFactor="0.75" weight="3">
			<parameter idref="clock.rate"/>
		</scaleOperator>
		<upDownOperator scaleFactor="0.75" weight="3">
			<up>
				<parameter idref="treeModel.allInternalNodeHeights"/>
			</up>
			<down>
				<parameter idref="clock.rate"/>
			</down>
		</upDownOperator>
		{% endif %}
		{% if clock == 'relaxed' %}
		<scaleOperator scaleFactor="0.75" weight="3">
			<parameter idref="ucgd.mean"/>
		</scaleOperator>
		<scaleOperator scaleFactor="0.75" weight="3">
			<parameter idref="ucgd.shape"/>
		</scaleOperator>
		<upDownOperator scaleFactor="0.75" weight="3">
			<up>
				<parameter idref="treeModel.allInternalNodeHeights"/>
			</up>
			<down>
				<parameter idref="ucgd.mean"/>
			</down>
		</upDownOperator>
		<swapOperator size="1" weight="10" autoOptimize="false">
			<parameter idref="branchRates.categories"/>
		</swapOperator>
		<uniformIntegerOperator weight="10">
			<parameter idref="branchRates.categories"/>
		</uniformIntegerOperator>
		{% elif clock == 'flc-stem' %}
		<scaleOperator scaleFactor="0.75" weight="3">
			<parameter idref="clock.rate"/>
		</scaleOperator>
		{% for group in groups %}
		<scaleOperator scaleFactor="0.75" weight="3">
			<parameter idref="{{ group }}.rate"/>
		</scaleOperator>
		{% endfor %}
		{% endif %}
		{% if not fixedTree %}
		<subtreeSlide size="1.0" gaussian="true" weight="30">
			<treeModel idref="treeModel"/>
		</subtreeSlide>
		<narrowExchange weight="30">
			<treeModel idref="treeModel"/>
		</narrowExchange>
		<wideExchange weight="3">
			<treeModel idref="treeModel"/>
		</wideExchange>
		<wilsonBalding weight="3">
			<treeModel idref="treeModel"/>
		</wilsonBalding>
		{% endif %}
		<scaleOperator scaleFactor="0.75" weight="3">
			<parameter idref="treeModel.rootHeight"/>
		</scaleOperator>
		<uniformOperator weight="30">
			<parameter idref="treeModel.internalNodeHeights"/>
		</uniformOperator>
		<scaleOperator scaleFactor="0.75" weight="3">
			<parameter idref="exponential.popSize"/>
		</scaleOperator>
		<randomWalkOperator windowSize="1.0" weight="3">
			<parameter idref="exponential.growthRate"/>
		</randomWalkOperator>
	</operators>
	

	<!-- Define MCMC                                                             -->
	<mcmc id="mcmc" chainLength="{{ chainLength }}" autoOptimize="true">
		<joint id="joint">
			<prior id="prior">
				<booleanLikelihood>
					{% for group in groups %}
					<monophylyStatistic idref="monophyly({{ group }})"/>
					{% endfor %}
				</booleanLikelihood>
				<dirichletPrior alpha="1.0" sumsTo="6.0">
					<parameter idref="gtr.rates"/>
				</dirichletPrior>
				<exponentialPrior mean="0.5" offset="0.0">
					<parameter idref="alpha"/>
				</exponentialPrior>
				{% if clock == 'strict' %}
				<gammaPrior shape="{{ rateGammaPriorShape }}" scale="{{ rateGammaPriorScale }}" offset="0.0">
					<parameter idref="clock.rate"/>
				</gammaPrior>
				{% elif clock == 'relaxed' %}
				<gammaPrior shape="{{ rateGammaPriorShape }}" scale="{{ rateGammaPriorScale }}" offset="0.0">
					<parameter idref="ucgd.mean"/>
				</gammaPrior>
				<exponentialPrior mean="0.3333333333333333" offset="0.0">
					<parameter idref="ucgd.shape"/>
				</exponentialPrior>
				{% elif clock == 'flc-stem' %}
				<gammaPrior shape="{{ rateGammaPriorShape }}" scale="{{ rateGammaPriorScale }}" offset="0.0">
					<parameter idref="clock.rate"/>
				</gammaPrior>
				{% for group in groups %}
				<gammaPrior shape="{{ rateGammaPriorShape }}" scale="{{ rateGammaPriorScale }}" offset="0.0">
					<parameter idref="{{ group }}.rate"/>
				</gammaPrior>
				{% endfor %}
				{% endif %}
				<exponentialPrior mean="100.0" offset="0.0">
					<parameter idref="exponential.popSize"/>
				</exponentialPrior>
				<laplacePrior mean="0.0" scale="10.0">
					<parameter idref="exponential.growthRate"/>
				</laplacePrior>
				<coalescentLikelihood idref="coalescent"/>
				
				{{ branchRatesRef() }}
			</prior>
			<likelihood id="likelihood">
				<treeDataLikelihood idref="treeLikelihood"/>
			</likelihood>
		</joint>
		<operators idref="operators"/>

		<!-- write log to screen                                                     -->
		<log id="screenLog" logEvery="{{ screenLogEvery }}">
			<column label="Joint" dp="4" width="12">
				<joint idref="joint"/>
			</column>
			<column label="Prior" dp="4" width="12">
				<prior idref="prior"/>
			</column>
			<column label="Likelihood" dp="4" width="12">
				<likelihood idref="likelihood"/>
			</column>
			<column label="age(root)" sf="6" width="12">
				<tmrcaStatistic idref="age(root)"/>
			</column>
			{% if clock == 'strict' %}
			<column label="clock.rate" sf="6" width="12">
				<parameter idref="clock.rate"/>
			</column>
			{% elif clock == 'relaxed' %}
			<column label="ucgd.mean" sf="6" width="12">
				<parameter idref="ucgd.mean"/>
			</column>
			{% elif clock == 'flc-stem' %}
			<column label="clock.rate" sf="6" width="12">
				<parameter idref="clock.rate"/>
			</column>
			{% endif %}
		</log>

		{% if traceLog %}
		<!-- write log to file                                                       -->
		<log id="fileLog" logEvery="{{ traceLog.log_every }}" fileName="{{ traceLog.file_name }}" overwrite="false">
			<joint idref="joint"/>
			<prior idref="prior"/>
			<likelihood idref="likelihood"/>
			<parameter idref="treeModel.rootHeight"/>
			<tmrcaStatistic idref="age(root)"/>
			<treeLengthStatistic idref="treeLength"/>
			{% for group in groups %}
			<tmrcaStatistic idref="tmrca({{ group }})"/>
			<tmrcaStatistic idref="age({{ group }})"/>
			{% endfor %}
			<parameter idref="exponential.popSize"/>
			<parameter idref="exponential.growthRate"/>
			<parameter idref="gtr.rates"/>
			<parameter idref="alpha"/>
			{% if clock == 'strict' %}
			<parameter idref="clock.rate"/>
			{% elif clock == 'relaxed' %}
			<parameter idref="ucgd.mean"/>
			<parameter idref="ucgd.shape"/>
			{% elif clock == 'flc-stem' %}
			<parameter idref="clock.rate"/>
			{% for group in groups %}
			<parameter idref="{{ group }}.rate"/>
			{% endfor %}
			{% endif %}
			<rateStatistic idref="meanRate"/>
			<rateStatistic idref="coefficientOfVariation"/>
			{% if clock == 'relaxed' -%}
			<rateCovarianceStatistic idref="covariance"/>
			{%- endif %}
			<treeDataLikelihood idref="treeLikelihood"/>
			{{ branchRatesRef() }}
			<coalescentLikelihood idref="coalescent"/>
		</log>
		{% endif %}

		{% if treeLog %}
		<!-- write tree log to file                                                  -->
		<logTree id="treeFileLog" logEvery="{{ treeLog.log_every }}" nexusFormat="true" fileName="{{ treeLog.file_name }}" sortTranslationTable="true">
			<treeModel idref="treeModel"/>
			<trait name="rate" tag="rate">
				{{ branchRatesRef() }}
			</trait>
			<joint idref="joint"/>
		</logTree>
		{% endif %}
	</mcmc>
	
	{% if marginalLikelihoodEstimator %}
	<!-- START Marginal Likelihood Estimator                                     -->

	<!-- Define marginal likelihood estimator (PS/SS) settings                   -->
	<marginalLikelihoodEstimator chainLength="{{ marginalLikelihoodEstimator.chain_length }}" pathSteps="{{ marginalLikelihoodEstimator.path_steps }}" pathScheme="betaquantile" alpha="0.3">
		<samplers>
			<mcmc idref="mcmc"/>
		</samplers>
		<pathLikelihood id="pathLikelihood">
			<source>
				<joint idref="joint"/>
			</source>
			<destination>
				<prior idref="prior"/>
			</destination>
		</pathLikelihood>
		<log id="MLELog" logEvery="{{ marginalLikelihoodEstimator.log_every }}" fileName="{{ marginalLikelihoodEstimator.file_name }}">
			<pathLikelihood idref="pathLikelihood"/>
		</log>
	</marginalLikelihoodEstimator>

	<!-- Path sampling estimator from collected samples                          -->
	<pathSamplingAnalysis fileName="{{ marginalLikelihoodEstimator.file_name }}" resultsFileName="{{ marginalLikelihoodEstimator.results_file_name }}">
		<likelihoodColumn name="pathLikelihood.delta"/>
		<thetaColumn name="pathLikelihood.theta"/>
	</pathSamplingAnalysis>

	<!-- Stepping-stone sampling estimator from collected samples                -->
	<steppingStoneSamplingAnalysis fileName="{{ marginalLikelihoodEstimator.file_name }}" resultsFileName="{{ marginalLikelihoodEstimator.results_file_name }}">
		<likelihoodColumn name="pathLikelihood.delta"/>
		<thetaColumn name="pathLikelihood.theta"/>
	</steppingStoneSamplingAnalysis>

	<!-- END Marginal Likelihood Estimator                                       -->
	{% endif %}

	<report>
		<property name="timer">
			<mcmc idref="mcmc"/>
		</property>
	</report>
	
</beast>
