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

<!-- Generated by BEAUTi v1.10.4                                             -->
<!--       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/                                           -->

<#setting number_format="computer">

<#assign maxDate=0>
<#list taxa as taxon>
	<#if taxon.date?number gt maxDate>
		<#assign maxDate=taxon.date?number>
	</#if>
</#list>

<#assign cutOff=maxDate-2019.75> <#-- old max date - cutoff -->
<#assign gridPoints=((cutOff)*52)?round> <#-- assuming exactly 52 weeks a year -->

<beast version="1.10.4">
	

	<!-- The list of taxa to be analysed (can also include dates/ages).          -->
	<!-- ntax=${taxa?size}                                               -->
	<taxa id="taxa">
<#list taxa as taxon>
		<taxon id="${taxon.id}">
			<date value="${taxon.date}" direction="forwards" units="years" uncertainty="0.0"/>
		</taxon>
</#list> 
	</taxa>


	<newick id="startingTree" usingDates="true">
		${tree}
	</newick>

	
	<newick id="dataTree" usingDates="false" usingHeights="true">
		${tree}
	</newick>



	<constrainedTreeModel id = "treeModel">
		<tree idref="startingTree"/>
		<constraintsTree>
			<tree idref="dataTree"/>
		</constraintsTree>
	</constrainedTreeModel>
		<!-- Statistic for root height of the tree       -->
	<treeHeightStatistic id="treeModel.rootHeight">
	<treeModel idref="treeModel"/>
	</treeHeightStatistic>
			<!-- 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>

		<!-- Generate a coalescent likelihood                                        -->
	<!-- Generate a gmrfSkyGridLikelihood for the Bayesian SkyGrid process       -->
	<gmrfSkyGridLikelihood id="skygrid">
		<populationSizes>
	<!-- skygrid.logPopSize is in log units unlike other popSize                 -->
			<parameter id="skygrid.logPopSize" dimension="${gridPoints+1}" value="1.0"/>
		</populationSizes>
		<precisionParameter>
			<parameter id="skygrid.precision" value="0.1" lower="0.0"/>
		</precisionParameter>
		<numGridPoints>
			<parameter id="skygrid.numGridPoints" value="${gridPoints}.0"/>
		</numGridPoints>
		<cutOff>
			<parameter id="skygrid.cutOff" value="${cutOff}"/>
		</cutOff>
		
		<intervals>
			<bigFastTreeIntervals>
				<treeModel idref="treeModel"/>
			</bigFastTreeIntervals>
		</intervals>
		
	</gmrfSkyGridLikelihood>



	<thorneyTreeLikelihood id="treeLikelihood">
		<constrainedTreeModel idref="treeModel"/>
		
		<strictClockBranchLengthLikelihood id="branchLengthLikelihood" scale="29903.0">
			<parameter id="clock.rate" value="0.00075" lower="0.0"/>
		</strictClockBranchLengthLikelihood>
		
		<constrainedBranchLengthProvider scale="29903.0">
			<constrainedTreeModel idref="treeModel"/>
			<dataTree>
				<tree idref="dataTree"/>
			</dataTree>
		</constrainedBranchLengthProvider>
	</thorneyTreeLikelihood>

	

	<!-- Define operators                                                        -->
	<operators id="operators" optimizationSchedule="default">

		<scaleOperator scaleFactor="0.75" weight="3">
			<parameter idref="clock.rate"/>
		</scaleOperator>

		<nodeHeightOperator type="uniform" weight="${taxa?size/2}">
			<treeModel idref="treeModel"/>
		</nodeHeightOperator>
		
		<nodeHeightOperator type="scaleRoot" weight="${taxa?size/10}" scaleFactor="0.75" >
			<treeModel idref="treeModel"/>
		</nodeHeightOperator>
		
		<uniformSubtreePruneRegraft weight="${taxa?size/2}">
			<constrainedTreeModel idref="treeModel"/>
		</uniformSubtreePruneRegraft>
		
		<narrowExchange weight="${taxa?size/10}">
			<constrainedTreeModel idref="treeModel"/>
		</narrowExchange>
		
		<wideExchange weight="${taxa?size/10}">
			<constrainedTreeModel idref="treeModel"/>
		</wideExchange>
		
		<wilsonBalding weight="${taxa?size/10}">
			<constrainedTreeModel idref="treeModel"/>
		</wilsonBalding>
		<gmrfSkygridBlockUpdateOperator scaleFactor="1.1" weight="10">
			<gmrfSkygridLikelihood idref="skygrid"/>
		</gmrfSkygridBlockUpdateOperator>
		<scaleOperator scaleFactor="0.75" weight="1">
			<parameter idref="skygrid.precision"/>
		</scaleOperator>
		<randomWalkOperator windowSize="1.0" weight="1">
			<parameter idref="skygrid.logPopSize"/>
		</randomWalkOperator>
	</operators>
	

	<!-- Define MCMC                                                             -->
	<mcmc id="mcmc" chainLength="100000000" autoOptimize="true" operatorAnalysis="${outputStem}.ops">
		<joint id="joint">
			<prior id="prior">
				<gammaPrior id="clock.prior" shape="0.001" scale="1000" offset="0.0">
					<parameter idref="clock.rate"/>
				</gammaPrior>

				<gammaPrior id="skygrid.precision.prior" shape="0.001" scale="1000" offset="0.0">
					<parameter idref="skygrid.precision"/>
				</gammaPrior>
				<gmrfSkyGridLikelihood idref="skygrid"/>				
			
			</prior>
			<likelihood id="likelihood">
				<thorneyTreeLikelihood idref="treeLikelihood"/>
			</likelihood>
		</joint>
		<operators idref="operators"/>

		<!-- write log to screen                                                     -->
		<log id="screenLog" logEvery="10000">
			<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>
			<column label="clock.rate" sf="6" width="12">
				<parameter idref="clock.rate"/>
			</column>
		</log>

		<!-- write log to file                                                       -->
		<log id="fileLog" logEvery="10000" fileName="${outputStem}.log" overwrite="false">
			<joint idref="joint"/>
			<prior idref="prior"/>
			<likelihood idref="likelihood"/>
			<parameter idref="treeModel.rootHeight"/>
			<tmrcaStatistic idref="age(root)"/>
			<treeLengthStatistic idref="treeLength"/>
			<parameter idref="clock.rate"/>
			<parameter idref="skygrid.precision"/>
            <parameter idref="skygrid.logPopSize"/>
            <parameter idref="skygrid.cutOff"/>
            <gmrfSkyGridLikelihood idref="skygrid"/>
		
			<treeDataLikelihood idref="treeLikelihood"/>
			
		</log>

		<!-- write tree log to file                                                  -->
		<logTree id="treeFileLog" logEvery="100000" nexusFormat="true" fileName="${outputStem}.trees" sortTranslationTable="true">
			<treeModel idref="treeModel"/>

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