
<beast version='2.0'
       namespace='beast.app.beauti:beast.core:beast.evolution.branchratemodel:beast.evolution.speciation:beast.evolution.tree.coalescent:beast.core.util:beast.evolution.nuc:beast.evolution.operators:beast.evolution.sitemodel:beast.evolution.substitutionmodel:beast.evolution.likelihood:beast.evolution:beast.math.distributions'>


    <!-- Clock models -->
    <mergewith point='clockModelTemplates'>

        <!-- Strict clock -->
        <subtemplate id='StrictClock' class='beast.evolution.branchratemodel.StrictClockModel'
                     mainid='StrictClock.c:$(n)'>
            <![CDATA[
                <branchRateModel spec='StrictClockModel' id='StrictClock.c:$(n)'>
                    <clock.rate id='clockRate.c:$(n)' spec='parameter.RealParameter' value='1.0' estimate='false'/>
                </branchRateModel>

				<operator id='StrictClockRateScaler.c:$(n)' spec='ScaleOperator' scaleFactor="0.75" weight="3" parameter='@clockRate.c:$(n)'/>

				<upDownOperator id='strictClockUpDownOperator.c:$(n)' spec='UpDownOperator' scaleFactor="0.75" weight="3">
					<up idref="clockRate.c:$(n)"/>
					<down idref="Tree.t:$(n)"/>
				</upDownOperator>

		        <prior id='ClockPrior.c:$(n)' x='@clockRate.c:$(n)'><distr spec="beast.math.distributions.Uniform" upper='Infinity'/></prior>
]]>
            <connect srcID='clockRate.c:$(n)' targetID='state' inputName='stateNode'
                     if='inlikelihood(clockRate.c:$(n)) and clockRate.c:$(n)/estimate=true'/>

            <connect srcID='clockRate.c:$(n)' targetID='state' inputName='stateNode'
                     if='inlikelihood(clockRate.c:$(n)) and clockRate.c:$(n)/estimate=true'/>
            <connect srcID='ClockPrior.c:$(n)' targetID='prior' inputName='distribution'
                     if='inlikelihood(clockRate.c:$(n)) and clockRate.c:$(n)/estimate=true'>substitution rate of
                partition c:$(n)
            </connect>
            <connect srcID='StrictClockRateScaler.c:$(n)' targetID='mcmc' inputName='operator'
                     if='inlikelihood(clockRate.c:$(n)) and clockRate.c:$(n)/estimate=true'>Scale substitution rate of
                partition c:$(n)
            </connect>
            <connect srcID='strictClockUpDownOperator.c:$(n)' targetID='mcmc' inputName='operator'
                     if='nooperator(FixMeanRatesOperator) and inlikelihood(clockRate.c:$(n)) and inlikelihood(Tree.t:$(n)) and Tree.t:$(n)/estimate=true and clockRate.c:$(n)/estimate=true'>
                Scale up substitution rate c:$(n) and scale down tree t:($n)
            </connect>
        </subtemplate>

        <!-- Relaxed clock exponential -->
        <subtemplate id='RelaxedClockExponential' class='beast.evolution.branchratemodel.UCRelaxedClockModel'
                     mainid='ExponentialRelaxedClock.c:$(n)'
                     suppressInputs='beast.evolution.branchratemodel.UCRelaxedClockModel.distr,beast.evolution.branchratemodel.UCRelaxedClockModel.rateQuantiles'>
            <![CDATA[
        <plugin spec='UCRelaxedClockModel' id="ExponentialRelaxedClock.c:$(n)" tree='@Tree.t:$(n)'>
			<parameter name='clock.rate' id='ucedMean.c:$(n)' value='1.0' estimate='false'/>
            <distr id='Exponential.c:$(n)' name='distr' spec="beast.math.distributions.Exponential">
				<parameter id='UCExpLambda.c:$(n)' name='mean' value='1.0'/>
			</distr>
            <rateCategories spec='parameter.IntegerParameter' id='expRateCategories.c:$(n)' value="1" dimension='10' estimate='true'/>
        </plugin>

        <operator id='ucedMeanScaler.c:$(n)' spec='ScaleOperator' scaleAll='false' scaleFactor="0.5" weight="1" parameter="@ucedMean.c:$(n)"/>
        <operator id="ExpCategoriesRandomWalk.c:$(n)" spec="IntRandomWalkOperator" windowSize='1' weight="10" parameter="@expRateCategories.c:$(n)"/>
		<operator id='ExpCategoriesSwapOperator.c:$(n)' spec='SwapOperator' howMany="1" weight="10" intparameter='@expRateCategories.c:$(n)'/>
		<operator id='ExpCategoriesUniform.c:$(n)' spec='UniformOperator' weight="10" parameter='@expRateCategories.c:$(n)'/>

		<upDownOperator id='relaxedUpDownOperatorExp.c:$(n)' spec='UpDownOperator' scaleFactor="0.75" weight="3">
			<up idref="ucedMean.c:$(n)"/>
			<down idref="Tree.t:$(n)"/>
		</upDownOperator>

		<log id='rateStat.c:$(n)' spec='RateStatistic' tree='@Tree.t:$(n)' branchratemodel='@ExponentialRelaxedClock.c:$(n)'/>
        <prior id='UCMeanRatePrior.c:$(n)' x='@ucedMean.c:$(n)'><distr spec="beast.math.distributions.Uniform" upper='Infinity'/></prior>
]]>
            <connect srcID='ucedMean.c:$(n)' targetID='state' inputName='stateNode'
                     if='inlikelihood(ucedMean.c:$(n)) and ucedMean.c:$(n)/estimate=true'/>
            <connect srcID='expRateCategories.c:$(n)' targetID='state' inputName='stateNode'
                     if='inlikelihood(expRateCategories.c:$(n))'/>

            <connect srcID='UCMeanRatePrior.c:$(n)' targetID='prior' inputName='distribution'
                     if='inlikelihood(ucedMean.c:$(n)) and ucedMean.c:$(n)/estimate=true'>uncorrelated exponential
                relaxed clock mean of partition c:$(n)
            </connect>

            <connect srcID='ucedMeanScaler.c:$(n)' targetID='mcmc' inputName='operator'
                     if='inlikelihood(ucedMean.c:$(n)) and ucedMean.c:$(n)/estimate=true'>Scale clock rate of partition
                c:$(n)
            </connect>
            <connect srcID='ExpCategoriesRandomWalk.c:$(n)' targetID='mcmc' inputName='operator'
                     if='inlikelihood(expRateCategories.c:$(n)) and expRateCategories.c:$(n)/estimate=true'>Randomly change categories of partition c:$(n)
            </connect>
            <connect srcID='ExpCategoriesSwapOperator.c:$(n)' targetID='mcmc' inputName='operator'
                     if='inlikelihood(expRateCategories.c:$(n)) and expRateCategories.c:$(n)/estimate=true'>Swap categories of partition c:$(n)
            </connect>
            <connect srcID='ExpCategoriesUniform.c:$(n)' targetID='mcmc' inputName='operator'
                     if='inlikelihood(expRateCategories.c:$(n)) and expRateCategories.c:$(n)/estimate=true'>Uniformly draw categories of partition c:$(n)
            </connect>
            <connect srcID='relaxedUpDownOperatorExp.c:$(n)' targetID='mcmc' inputName='operator'
                     if='nooperator(FixMeanRatesOperator) and inlikelihood(ucedMean.c:$(n)) and inlikelihood(Tree.t:$(n)) and ucedMean.c:$(n)/estimate=true and Tree.t:$(n)/estimate=true'>
                Up/down scaler for mean rate and tree of partition c:$(n)
            </connect>
            <connect srcID='Tree.t:$(n)' targetID='ExponentialRelaxedClock.c:$(n)' inputName='tree' if='inlikelihood(ExponentialRelaxedClock.c:$(n))'/>
            <connect srcID='Tree.t:$(n)' targetID='rateStat.c:$(n)' inputName='tree' if='inlikelihood(ExponentialRelaxedClock.c:$(n))'/>

            <connect srcID='ucedMean.c:$(n)' targetID='tracelog' inputName='log'
                     if='inlikelihood(ucedMean.c:$(n)) and ucedMean.c:$(n)/estimate=true'/>
            <connect srcID='rateStat.c:$(n)' targetID='tracelog' inputName='log'
                     if='inposterior(ExponentialRelaxedClock.c:$(n))'/>

            <connect srcID='ExponentialRelaxedClock.c:$(n)' targetID='TreeWithMetaDataLogger.t:$(n)'
                     inputName='branchratemodel' if='inposterior(ExponentialRelaxedClock.c:$(n))'/>
        </subtemplate>


        <!-- Relaxed clock log normal -->
        <subtemplate id='RelaxedClockLogNormal' class='beast.evolution.branchratemodel.UCRelaxedClockModel'
                     mainid='RelaxedClock.c:$(n)'
                     suppressInputs='beast.evolution.branchratemodel.UCRelaxedClockModel.distr,beast.evolution.branchratemodel.UCRelaxedClockModel.rateQuantiles'>
            <![CDATA[
        <plugin spec='UCRelaxedClockModel' id="RelaxedClock.c:$(n)" tree='@Tree.t:$(n)'>
			<parameter name='clock.rate' id='ucldMean.c:$(n)' value='1.0'/>
            <distr id='LogNormalDistributionModel.c:$(n)' name='distr' spec="beast.math.distributions.LogNormalDistributionModel" meanInRealSpace='true'>
                <parameter name='M' value="1.0" estimate='false' lower='0' upper='1'/>
                <parameter name='S' id='ucldStdev.c:$(n)' value="0.1" lower="0" estimate='true'/>
            </distr>
            <rateCategories spec='parameter.IntegerParameter' id='rateCategories.c:$(n)' value="1" dimension='10' estimate='true'/>
        </plugin>

        <operator id='ucldMeanScaler.c:$(n)' spec='ScaleOperator' scaleAll='false' scaleFactor="0.5" weight="1" parameter="@ucldMean.c:$(n)"/>
        <operator id='ucldStdevScaler.c:$(n)' spec='ScaleOperator' scaleAll='false' scaleFactor="0.5" weight="3" parameter="@ucldStdev.c:$(n)"/>
        <operator id="CategoriesRandomWalk.c:$(n)" spec="IntRandomWalkOperator" windowSize='1' weight="10" parameter="@rateCategories.c:$(n)"/>
		<operator id='CategoriesSwapOperator.c:$(n)' spec='SwapOperator' howMany="1" weight="10" intparameter='@rateCategories.c:$(n)'/>
		<operator id='CategoriesUniform.c:$(n)' spec='UniformOperator' weight="10" parameter='@rateCategories.c:$(n)'/>

		<upDownOperator id='relaxedUpDownOperator.c:$(n)' spec='UpDownOperator' scaleFactor="0.75" weight="3">
			<up idref="ucldMean.c:$(n)"/>
			<down idref="Tree.t:$(n)"/>
		</upDownOperator>

        <prior id='ucldStdevPrior.c:$(n)' x='@ucldStdev.c:$(n)'>

            <!-- this prior has a median of 0.1 and 97.5% of its probability is below S=1 -->
            <distr spec="beast.math.distributions.Gamma">
                <parameter name='alpha' value="0.5396" estimate='false'/> <!-- shape -->
                <parameter name='beta' value="0.3819" estimate='false'/> <!-- scale -->
            </distr>

            <!-- <distr spec="beast.math.distributions.Exponential">
                <parameter name='mean' value="0.3333" estimate='false'/>
            </distr> -->
        </prior>


		<log id='rate.c:$(n)' spec='RateStatistic' tree='@Tree.t:$(n)' branchratemodel='@RelaxedClock.c:$(n)'/>
        <prior id='MeanRatePrior.c:$(n)' x='@ucldMean.c:$(n)'><distr spec="beast.math.distributions.Uniform" upper='Infinity'/></prior>
]]>
            <connect srcID='ucldMean.c:$(n)' targetID='state' inputName='stateNode'
                     if='inlikelihood(ucldMean.c:$(n)) and ucldMean.c:$(n)/estimate=true'/>
            <connect srcID='ucldStdev.c:$(n)' targetID='state' inputName='stateNode'
                     if='inlikelihood(ucldStdev.c:$(n)) and ucldStdev.c:$(n)/estimate=true'/>
            <connect srcID='rateCategories.c:$(n)' targetID='state' inputName='stateNode'
                     if='inlikelihood(rateCategories.c:$(n))'/>

            <connect srcID='ucldMeanScaler.c:$(n)' targetID='mcmc' inputName='operator'
                     if='inlikelihood(ucldMean.c:$(n)) and ucldMean.c:$(n)/estimate=true'>Scale clock rate of partition
                c:$(n)
            </connect>
            <connect srcID='ucldStdevScaler.c:$(n)' targetID='mcmc' inputName='operator'
                     if='inlikelihood(ucldStdev.c:$(n)) and ucldStdev.c:$(n)/estimate=true'>Scale stdev of rate of partition c:$(n)
            </connect>
            <connect srcID='CategoriesRandomWalk.c:$(n)' targetID='mcmc' inputName='operator'
                     if='inlikelihood(rateCategories.c:$(n)) and rateCategories.c:$(n)/estimate=true'>Randomly change categories of partition c:$(n)
            </connect>
            <connect srcID='CategoriesSwapOperator.c:$(n)' targetID='mcmc' inputName='operator'
                     if='inlikelihood(rateCategories.c:$(n)) and rateCategories.c:$(n)/estimate=true'>Swap categories of partition c:$(n)
            </connect>
            <connect srcID='CategoriesUniform.c:$(n)' targetID='mcmc' inputName='operator'
                     if='inlikelihood(rateCategories.c:$(n)) and rateCategories.c:$(n)/estimate=true'>Uniformly draw categories of partition c:$(n)
            </connect>
            <connect srcID='relaxedUpDownOperator.c:$(n)' targetID='mcmc' inputName='operator'
                     if='nooperator(FixMeanRatesOperator) and inlikelihood(ucldMean.c:$(n)) and ucldMean.c:$(n)/estimate=true and Tree.t:$(n)/estimate=true'>
                Up/down scaler for mean rate and tree of partition c:$(n)
            </connect>
            <connect srcID='Tree.t:$(n)' targetID='RelaxedClock.c:$(n)' inputName='tree' if='inlikelihood(RelaxedClock.c:$(n))'/>
            <connect srcID='Tree.t:$(n)' targetID='rate.c:$(n)' inputName='tree' if='inlikelihood(RelaxedClock.c:$(n))'/>

            <connect srcID='ucldMean.c:$(n)' targetID='tracelog' inputName='log'
                     if='inlikelihood(ucldMean.c:$(n)) and ucldMean.c:$(n)/estimate=true'/>
            <connect srcID='ucldStdev.c:$(n)' targetID='tracelog' inputName='log' if='inlikelihood(ucldStdev.c:$(n))'/>
            <connect srcID='rate.c:$(n)' targetID='tracelog' inputName='log' if='inposterior(RelaxedClock.c:$(n))'/>

            <connect srcID='ucldStdevPrior.c:$(n)' targetID='prior' inputName='distribution'
                     if='inlikelihood(ucldStdev.c:$(n))'>uncorrelated lognormal relaxed clock stdev of partition c:$(n)
            </connect>
            <connect srcID='MeanRatePrior.c:$(n)' targetID='prior' inputName='distribution'
                     if='inlikelihood(ucldMean.c:$(n)) and inlikelihood(Tree.t:$(n)) and ucldMean.c:$(n)/estimate=true'>
                uncorrelated lognormal relaxed clock mean of partition c:$(n)
            </connect>

            <connect srcID='RelaxedClock.c:$(n)' targetID='TreeWithMetaDataLogger.t:$(n)' inputName='branchratemodel'
                     if='inposterior(RelaxedClock.c:$(n))'/>
        </subtemplate>


        <!-- Random local clock -->
        <subtemplate id='RandomLocalClock' class='beast.evolution.branchratemodel.RandomLocalClockModel'
                     mainid='RandomLocalClock.c:$(n)'
                     suppressInputs='beast.evolution.branchratemodel.RandomLocalClockModel.rates'>
            <![CDATA[
        <input spec='RandomLocalClockModel' id="RandomLocalClock.c:$(n)" ratesAreMultipliers="false" tree='@Tree.t:$(n)'>
            <clock.rate id='meanClockRate.c:$(n)' spec='parameter.RealParameter' value='1.0' estimate='false'/>
            <parameter spec='parameter.BooleanParameter' name='indicators' id='Indicators.c:$(n)' value="0"/>
            <parameter name='rates' id='clockrates.c:$(n)' value="1"/>
        </input>

        <operator id="IndicatorsBitFlip.c:$(n)" spec="BitFlipOperator" weight="15" parameter="@Indicators.c:$(n)"/>
<!-- this should probably be a DeltaExchange instead of scale operator to keep mean rate to 1 -->
        <operator id='ClockRateScaler.c:$(n)' spec='ScaleOperator' scaleAll='false' scaleFactor="0.5" weight="15" parameter="@clockrates.c:$(n)"/>

        <operator id='randomClockScaler.c:$(n)' spec='ScaleOperator' scaleAll='false' scaleFactor="0.5" weight="1" parameter="@meanClockRate.c:$(n)"/>
		<upDownOperator id='randomClockUpDownOperator.c:$(n)' spec='UpDownOperator' scaleFactor="0.75" weight="3">
			<up idref="meanClockRate.c:$(n)"/>
			<down idref="Tree.t:$(n)"/>
		</upDownOperator>

		<distribution idref="prior">
			<if cond='inlikelihood(RandomLocalClock.c:$(n))'>
				<!-- prior on rates -->
			    <prior id='RRatesPrior.c:s$(n)' name="distribution" x='@clockrates.c:$(n)'>
			        <Gamma id="Gamma.0" name="distr">
			            <parameter estimate="false" name="alpha">0.5</parameter>
			            <parameter estimate="false" name="beta">2.0</parameter>
			        </Gamma>
				</prior>

				<!-- prior on number of changes -->
				<prior id="RRateChangesPrior.c:$(n)" name="distribution">
					<x id="RRateChanges.c:$(n)" spec="beast.core.util.Sum" arg="@Indicators.c:$(n)"/>
                    <distr spec='beast.math.distributions.Poisson'>
                          <lambda spec='parameter.RealParameter' estimate='false' value='0.6931471805599453'/>
                    </distr>
				</prior>
			</if>
		</distribution>

		<logger idref="tracelog">
			<if cond='inlikelihood(RandomLocalClock.c:$(n))'>
				<log idref="RRateChanges.c:$(n)"/>
			</if>
		</logger>

        <prior id='MeanRRatePrior.c:s$(n)' x='@meanClockRate.c:$(n)'><distr spec="beast.math.distributions.Uniform" upper='Infinity'/></prior>
]]>
            <connect srcID='Indicators.c:$(n)' targetID='state' inputName='stateNode'
                     if='inlikelihood(Indicators.c:$(n))'/>
            <connect srcID='meanClockRate.c:$(n)' targetID='state' inputName='stateNode'
                     if='inlikelihood(meanClockRate.c:$(n)) and meanClockRate.c:$(n)/estimate=true'/>
            <connect srcID='clockrates.c:$(n)' targetID='state' inputName='stateNode'
                     if='inlikelihood(clockrates.c:$(n))'/>


            <connect srcID='randomClockScaler.c:$(n)' targetID='mcmc' inputName='operator'
                     if='inlikelihood(meanClockRate.c:$(n)) and meanClockRate.c:$(n)/estimate=true'>Scale clock rate of
                partition c:$(n)
            </connect>
            <connect srcID='randomClockUpDownOperator.c:$(n)' targetID='mcmc' inputName='operator'
                     if='nooperator(FixMeanRatesOperator) and inlikelihood(meanClockRate.c:$(n)) and inlikelihood(Tree.t:$(n)) and meanClockRate.c:$(n)/estimate=true'>
                Up/down scaler for mean rate and tree of partition c:$(n)
            </connect>
            <connect srcID='IndicatorsBitFlip.c:$(n)' targetID='mcmc' inputName='operator'
                     if='inlikelihood(Indicators.c:$(n))'>Flip indicator bits for random clock of partition c:$(n)
            </connect>
            <connect srcID='ClockRateScaler.c:$(n)' targetID='mcmc' inputName='operator'
                     if='inlikelihood(clockrates.c:$(n))'>Scale random clock rates of partition c:$(n)
            </connect>
            <connect srcID='Tree.t:$(n)' targetID='RandomLocalClock.c:$(n)' inputName='tree' if='inlikelihood(RandomLocalClock.c:$(n))'/>

            <connect srcID='Indicators.c:$(n)' targetID='tracelog' inputName='log'
                     if='inlikelihood(Indicators.c:$(n))'/>
            <connect srcID='clockrates.c:$(n)' targetID='tracelog' inputName='log'
                     if='inlikelihood(clockrates.c:$(n))'/>
            <connect srcID='meanClockRate.c:$(n)' targetID='tracelog' inputName='log'
                     if='inposterior(RandomLocalClock.c:$(n)) and meanClockRate.c:$(n)/estimate=true'/>

            <connect srcID='MeanRRatePrior.c:$(n)' targetID='prior' inputName='distribution'
                     if='inlikelihood(meanClockRate.c:$(n)) and meanClockRate.c:$(n)/estimate=true'>substitution rate of
                partition c:$(n)
            </connect>

            <connect srcID='RandomLocalClock.c:$(n)' targetID='TreeWithMetaDataLogger.t:$(n)'
                     inputName='branchratemodel' if='inposterior(RandomLocalClock.c:$(n))'/>
        </subtemplate>

    </mergewith>

</beast>


