<?xml version="1.0" standalone="yes"?>
<!--
  ~ testStarTreeTest.xml
  ~
  ~ Copyright (c) 2002-2011 Alexei Drummond, Andrew Rambaut and Marc Suchard
  ~
  ~ This file is part of BEAST.
  ~ See the NOTICE file distributed with this work for additional
  ~ information regarding copyright ownership and licensing.
  ~
  ~ BEAST is free software; you can redistribute it and/or modify
  ~ it under the terms of the GNU Lesser General Public License as
  ~ published by the Free Software Foundation; either version 2
  ~ of the License, or (at your option) any later version.
  ~
  ~  BEAST is distributed in the hope that it will be useful,
  ~  but WITHOUT ANY WARRANTY; without even the implied warranty of
  ~  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  ~  GNU Lesser General Public License for more details.
  ~
  ~ You should have received a copy of the GNU Lesser General Public
  ~ License along with BEAST; if not, write to the
  ~ Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
  ~ Boston, MA  02110-1301  USA
  -->

<!-- $Id: testMCMC.xml,v 1.2 2005/07/11 23:22:40 rambaut Exp $ -->
<!-- Tests the MCMC engine -->

<beast>

	<!-- The list of taxa analyse (can also include dates/ages).                 -->
	<!-- ntax=6                                                                  -->
	<taxa id="taxa">
		<taxon id="human"/>
		<taxon id="chimp"/>
		<taxon id="bonobo"/>
		<taxon id="gorilla"/>
		<taxon id="orangutan"/>
		<taxon id="siamang"/>
	</taxa>

	<!-- The sequence alignment (each sequence refers to a taxon above).         -->
	<!-- ntax=6 nchar=768                                                        -->
	<alignment id="alignment" dataType="nucleotide">
		<sequence>
			<taxon idref="human"/>
			AGAAATATGTCTGATAAAAGAGTTACTTTGATAGAGTAAATAATAGGAGCTTAAACCCCCTTATTTCTACTAGGACTATGAGAATCGAACCCATCCCTGAGAATCCAAAATTCTCCGTGCCACCTATCACACCCCATCCTAAGTAAGGTCAGCTAAATAAGCTATCGGGCCCATACCCCGAAAATGTTGGTTATACCCTTCCCGTACTAAGAAATTTAGGTTAAATACAGACCAAGAGCCTTCAAAGCCCTCAGTAAGTTG-CAATACTTAATTTCTGTAAGGACTGCAAAACCCCACTCTGCATCAACTGAACGCAAATCAGCCACTTTAATTAAGCTAAGCCCTTCTAGACCAATGGGACTTAAACCCACAAACACTTAGTTAACAGCTAAGCACCCTAATCAAC-TGGCTTCAATCTAAAGCCCCGGCAGG-TTTGAAGCTGCTTCTTCGAATTTGCAATTCAATATGAAAA-TCACCTCGGAGCTTGGTAAAAAGAGGCCTAACCCCTGTCTTTAGATTTACAGTCCAATGCTTCA-CTCAGCCATTTTACCACAAAAAAGGAAGGAATCGAACCCCCCAAAGCTGGTTTCAAGCCAACCCCATGGCCTCCATGACTTTTTCAAAAGGTATTAGAAAAACCATTTCATAACTTTGTCAAAGTTAAATTATAGGCT-AAATCCTATATATCTTA-CACTGTAAAGCTAACTTAGCATTAACCTTTTAAGTTAAAGATTAAGAGAACCAACACCTCTTTACAGTGA
		</sequence>
		<sequence>
			<taxon idref="chimp"/>
			AGAAATATGTCTGATAAAAGAATTACTTTGATAGAGTAAATAATAGGAGTTCAAATCCCCTTATTTCTACTAGGACTATAAGAATCGAACTCATCCCTGAGAATCCAAAATTCTCCGTGCCACCTATCACACCCCATCCTAAGTAAGGTCAGCTAAATAAGCTATCGGGCCCATACCCCGAAAATGTTGGTTACACCCTTCCCGTACTAAGAAATTTAGGTTAAGCACAGACCAAGAGCCTTCAAAGCCCTCAGCAAGTTA-CAATACTTAATTTCTGTAAGGACTGCAAAACCCCACTCTGCATCAACTGAACGCAAATCAGCCACTTTAATTAAGCTAAGCCCTTCTAGATTAATGGGACTTAAACCCACAAACATTTAGTTAACAGCTAAACACCCTAATCAAC-TGGCTTCAATCTAAAGCCCCGGCAGG-TTTGAAGCTGCTTCTTCGAATTTGCAATTCAATATGAAAA-TCACCTCAGAGCTTGGTAAAAAGAGGCTTAACCCCTGTCTTTAGATTTACAGTCCAATGCTTCA-CTCAGCCATTTTACCACAAAAAAGGAAGGAATCGAACCCCCTAAAGCTGGTTTCAAGCCAACCCCATGACCTCCATGACTTTTTCAAAAGATATTAGAAAAACTATTTCATAACTTTGTCAAAGTTAAATTACAGGTT-AACCCCCGTATATCTTA-CACTGTAAAGCTAACCTAGCATTAACCTTTTAAGTTAAAGATTAAGAGGACCGACACCTCTTTACAGTGA
		</sequence>
		<sequence>
			<taxon idref="bonobo"/>
			AGAAATATGTCTGATAAAAGAATTACTTTGATAGAGTAAATAATAGGAGTTTAAATCCCCTTATTTCTACTAGGACTATGAGAGTCGAACCCATCCCTGAGAATCCAAAATTCTCCGTGCCACCTATCACACCCCATCCTAAGTAAGGTCAGCTAAATAAGCTATCGGGCCCATACCCCGAAAATGTTGGTTATACCCTTCCCGTACTAAGAAATTTAGGTTAAACACAGACCAAGAGCCTTCAAAGCTCTCAGTAAGTTA-CAATACTTAATTTCTGTAAGGACTGCAAAACCCCACTCTGCATCAACTGAACGCAAATCAGCCACTTTAATTAAGCTAAGCCCTTCTAGATTAATGGGACTTAAACCCACAAACATTTAGTTAACAGCTAAACACCCTAATCAGC-TGGCTTCAATCTAAAGCCCCGGCAGG-TTTGAAGCTGCTTCTTTGAATTTGCAATTCAATATGAAAA-TCACCTCAGAGCTTGGTAAAAAGAGGCTTAACCCCTGTCTTTAGATTTACAGTCCAATGCTTCA-CTCAGCCATTTTACCACAAAAAAGGAAGGAATCGAACCCCCTAAAGCTGGTTTCAAGCCAACCCCATGACCCCCATGACTTTTTCAAAAGATATTAGAAAAACTATTTCATAACTTTGTCAAAGTTAAATTACAGGTT-AAACCCCGTATATCTTA-CACTGTAAAGCTAACCTAGCATTAACCTTTTAAGTTAAAGATTAAGAGGACCAACACCTCTTTACAGTGA
		</sequence>
		<sequence>
			<taxon idref="gorilla"/>
			AGAAATATGTCTGATAAAAGAGTTACTTTGATAGAGTAAATAATAGAGGTTTAAACCCCCTTATTTCTACTAGGACTATGAGAATTGAACCCATCCCTGAGAATCCAAAATTCTCCGTGCCACCTGTCACACCCCATCCTAAGTAAGGTCAGCTAAATAAGCTATCGGGCCCATACCCCGAAAATGTTGGTCACATCCTTCCCGTACTAAGAAATTTAGGTTAAACATAGACCAAGAGCCTTCAAAGCCCTTAGTAAGTTA-CAACACTTAATTTCTGTAAGGACTGCAAAACCCTACTCTGCATCAACTGAACGCAAATCAGCCACTTTAATTAAGCTAAGCCCTTCTAGATCAATGGGACTCAAACCCACAAACATTTAGTTAACAGCTAAACACCCTAGTCAAC-TGGCTTCAATCTAAAGCCCCGGCAGG-TTTGAAGCTGCTTCTTCGAATTTGCAATTCAATATGAAAT-TCACCTCGGAGCTTGGTAAAAAGAGGCCCAGCCTCTGTCTTTAGATTTACAGTCCAATGCCTTA-CTCAGCCATTTTACCACAAAAAAGGAAGGAATCGAACCCCCCAAAGCTGGTTTCAAGCCAACCCCATGACCTTCATGACTTTTTCAAAAGATATTAGAAAAACTATTTCATAACTTTGTCAAGGTTAAATTACGGGTT-AAACCCCGTATATCTTA-CACTGTAAAGCTAACCTAGCGTTAACCTTTTAAGTTAAAGATTAAGAGTATCGGCACCTCTTTGCAGTGA
		</sequence>
		<sequence>
			<taxon idref="orangutan"/>
			AGAAATATGTCTGACAAAAGAGTTACTTTGATAGAGTAAAAAATAGAGGTCTAAATCCCCTTATTTCTACTAGGACTATGGGAATTGAACCCACCCCTGAGAATCCAAAATTCTCCGTGCCACCCATCACACCCCATCCTAAGTAAGGTCAGCTAAATAAGCTATCGGGCCCATACCCCGAAAATGTTGGTTACACCCTTCCCGTACTAAGAAATTTAGGTTA--CACAGACCAAGAGCCTTCAAAGCCCTCAGCAAGTCA-CAGCACTTAATTTCTGTAAGGACTGCAAAACCCCACTTTGCATCAACTGAGCGCAAATCAGCCACTTTAATTAAGCTAAGCCCTCCTAGACCGATGGGACTTAAACCCACAAACATTTAGTTAACAGCTAAACACCCTAGTCAAT-TGGCTTCAGTCCAAAGCCCCGGCAGGCCTTAAAGCTGCTCCTTCGAATTTGCAATTCAACATGACAA-TCACCTCAGGGCTTGGTAAAAAGAGGTCTGACCCCTGTTCTTAGATTTACAGCCTAATGCCTTAACTCGGCCATTTTACCGCAAAAAAGGAAGGAATCGAACCTCCTAAAGCTGGTTTCAAGCCAACCCCATAACCCCCATGACTTTTTCAAAAGGTACTAGAAAAACCATTTCGTAACTTTGTCAAAGTTAAATTACAGGTC-AGACCCTGTGTATCTTA-CATTGCAAAGCTAACCTAGCATTAACCTTTTAAGTTAAAGACTAAGAGAACCAGCCTCTCTTTGCAATGA
		</sequence>
		<sequence>
			<taxon idref="siamang"/>
			AGAAATACGTCTGACGAAAGAGTTACTTTGATAGAGTAAATAACAGGGGTTTAAATCCCCTTATTTCTACTAGAACCATAGGAGTCGAACCCATCCTTGAGAATCCAAAACTCTCCGTGCCACCCGTCGCACCCTGTTCTAAGTAAGGTCAGCTAAATAAGCTATCGGGCCCATACCCCGAAAATGTTGGTTATACCCTTCCCATACTAAGAAATTTAGGTTAAACACAGACCAAGAGCCTTCAAAGCCCTCAGTAAGTTAACAAAACTTAATTTCTGCAAGGGCTGCAAAACCCTACTTTGCATCAACCGAACGCAAATCAGCCACTTTAATTAAGCTAAGCCCTTCTAGATCGATGGGACTTAAACCCATAAAAATTTAGTTAACAGCTAAACACCCTAAACAACCTGGCTTCAATCTAAAGCCCCGGCAGA-GTTGAAGCTGCTTCTTTGAACTTGCAATTCAACGTGAAAAATCACTTCGGAGCTTGGCAAAAAGAGGTTTCACCTCTGTCCTTAGATTTACAGTCTAATGCTTTA-CTCAGCCACTTTACCACAAAAAAGGAAGGAATCGAACCCTCTAAAACCGGTTTCAAGCCAGCCCCATAACCTTTATGACTTTTTCAAAAGATATTAGAAAAACTATTTCATAACTTTGTCAAAGTTAAATCACAGGTCCAAACCCCGTATATCTTATCACTGTAGAGCTAGACCAGCATTAACCTTTTAAGTTAAAGACTAAGAGAACTACCGCCTCTTTACAGTGA
		</sequence>	
<!--		<sequence>
			<taxon idref="human"/>
			A
	    </sequence>
		<sequence>
			<taxon idref="chimp"/>
			A
		</sequence>
		<sequence>
			<taxon idref="bonobo"/>
			A
		</sequence>
		<sequence>
			<taxon idref="gorilla"/>
			G
		</sequence>
		<sequence>
			<taxon idref="orangutan"/>
			G
		</sequence>
		<sequence>
			<taxon idref="siamang"/>
			A
		</sequence> -->
	</alignment>

	<!-- The unique patterns for all positions                                   -->
	<!-- npatterns=69                                                            -->
	<patterns id="patterns" from="1">
		<alignment idref="alignment"/>
	</patterns>

	<!-- This is a simple constant population size coalescent model              -->
	<!-- that is used to generate an initial tree for the chain.                 -->
	<constantSize id="initialDemo" units="substitutions">
		<populationSize>
			<parameter id="initialDemo.popSize" value="0.0001"/>
		</populationSize>
	</constantSize>

	<!-- Generate a random starting tree under the coalescent process            -->
	<coalescentTree id="startingTree" rootHeight="1">
		<taxa idref="taxa"/>
		<constantSize idref="initialDemo"/>
	</coalescentTree>

	<starTreeModel id="treeModel">
		<coalescentTree idref="startingTree"/>
		<rootHeight>
			<parameter id="treeModel.rootHeight"/>
		</rootHeight>
	</starTreeModel>

	<!-- The HKY substitution model (Hasegawa, Kishino & Yano, 1985)             -->
	<hkyModel id="hky">
		<frequencies>
			<frequencyModel dataType="nucleotide">
<!--				<alignment idref="alignment"/> -->
				<frequencies>
					<parameter id="hky.frequencies" value="0.25 0.25 0.25 0.25"/>
				</frequencies>
			</frequencyModel>
		</frequencies>
		<kappa>
			<parameter id="hky.kappa" value="1.0" lower="0.0"/>
		</kappa>
	</hkyModel>

	<!-- site model                                                              -->
	<siteModel id="siteModel">
		<substitutionModel>
			<hkyModel idref="hky"/>
		</substitutionModel>
		<mutationRate>
			<parameter id="siteModel.mu" value="1.0" lower="0.0"/>
		</mutationRate>
	</siteModel>

	<treeLikelihood id="treeLikelihood">
		<patterns idref="patterns"/>
		<treeModel idref="treeModel"/>
		<siteModel idref="siteModel"/>
	</treeLikelihood>
	
	<report>
	    <hkyModel idref="hky"/>
	    logLike = <starTreeLikelihood idref="treeLikelihood"/>
	</report>

	<operators id="operators">
		<scaleOperator scaleFactor="0.5" weight="1">
			<parameter idref="hky.kappa"/>
		</scaleOperator>
<!--        <scaleOperator scaleFactor="0.5" weight="1">
			<parameter idref="siteModel.mu"/>
		</scaleOperator>	-->	
		<scaleOperator scaleFactor="0.5" weight="1">
			<parameter idref="treeModel.rootHeight"/>
		</scaleOperator>
<!--		<uniformOperator weight="10">
			<parameter idref="treeModel.internalNodeHeights"/>
		</uniformOperator>
		<subtreeSlide weight="5" gaussian="true" size="1.0">
			<treeModel idref="treeModel"/>
		</subtreeSlide>
		<narrowExchange weight="1">
			<treeModel idref="treeModel"/>
		</narrowExchange>
		<wideExchange weight="1">
			<treeModel idref="treeModel"/>
		</wideExchange>
		<wilsonBalding weight="1">
			<treeModel idref="treeModel"/>
		</wilsonBalding> -->
	</operators>

	<mcmc id="mcmc" chainLength="10000000" autoOptimize="true">
		<compoundLikelihood id="likelihood">
			<treeLikelihood idref="treeLikelihood"/>
		</compoundLikelihood>
		<operators idref="operators"/>
		<log logEvery="10000">
			<column label="Likelihood" dp="4" width="12">
				<compoundLikelihood idref="likelihood"/>
			</column>
			<column label="Root Height" sf="6" width="12">
				<parameter idref="treeModel.rootHeight"/>
			</column>
			<column label="Rate" sf="6" width="12">
				<parameter idref="siteModel.mu"/>
			</column>			
			<column label="Kappa" sf="6" width="12">
				<parameter idref="hky.kappa"/>
			</column>
		</log>
		<log logEvery="1000" fileName="testMCMC.log">
			<compoundLikelihood idref="likelihood"/>
			<parameter idref="treeModel.rootHeight"/>
			<parameter idref="hky.kappa"/>
		</log>
		<logTree  logEvery="1000" nexusFormat="true" fileName="testMCMC.trees">
			<treeModel idref="treeModel"/>
		</logTree>
	</mcmc>

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

	<traceAnalysis fileName="testMCMC.log">
		<expectation name="likelihood" value="-1815.75"/>
		<expectation name="treeModel.rootHeight" value="6.42048E-2"/>
		<expectation name="hky.kappa" value="32.8941"/>
	</traceAnalysis>

	<treeTraceAnalysis fileName="testMCMC.trees"/>

</beast>

<!--

a = c(
    -3,  1,  1,  1,
     1, -3,  1,  1,
     1,  1, -3,  1,
     1,  1,  1, -3) / 3
     
A = matrix(a,nrow=4,byrow=T)     

q =  c(0.44769785366043535, 0.184100715397428, 0.1841007155447086, 0.184100715397428, 0.18410071554470858, 0.44769785351315483, 0.18410071554470858, 0.18410071539742803, 0.1841007155447086, 0.184100715397428, 0.44769785366043535, 0.184100715397428, 0.18410071554470858, 0.18410071539742803, 0.18410071554470858, 0.44769785351315483)
Q = matrix(q,nrow=4,byrow=T)

p1 = Q[1,1]^6 / 4 + Q[2,1]^6 / 4 +  Q[3,1]^6 / 4 +  Q[4,1]^6 / 4

p2 = Q[1,1]^4*Q[1,3]^2 / 4 + Q[2,1]^4*Q[2,3]^2 / 4 + 
     Q[3,1]^4*Q[3,3]^2 / 4 + Q[4,1]^4*Q[4,3]^2 / 4

log(p1)
log(p2)

-->
