<?xml version="1.0"?>
<feed xmlns="http://www.w3.org/2005/Atom" xml:lang="en">
	<id>https://teaching.healthtech.dtu.dk/22115/api.php?action=feedcontributions&amp;feedformat=atom&amp;user=Gorm</id>
	<title>22115 - User contributions [en]</title>
	<link rel="self" type="application/atom+xml" href="https://teaching.healthtech.dtu.dk/22115/api.php?action=feedcontributions&amp;feedformat=atom&amp;user=Gorm"/>
	<link rel="alternate" type="text/html" href="https://teaching.healthtech.dtu.dk/22115/index.php/Special:Contributions/Gorm"/>
	<updated>2026-05-03T09:52:43Z</updated>
	<subtitle>User contributions</subtitle>
	<generator>MediaWiki 1.41.0</generator>
	<entry>
		<id>https://teaching.healthtech.dtu.dk/22115/index.php?title=Bayesian_phylogenetics:_clock_models&amp;diff=293</id>
		<title>Bayesian phylogenetics: clock models</title>
		<link rel="alternate" type="text/html" href="https://teaching.healthtech.dtu.dk/22115/index.php?title=Bayesian_phylogenetics:_clock_models&amp;diff=293"/>
		<updated>2026-04-22T13:43:44Z</updated>

		<summary type="html">&lt;p&gt;Gorm: /* Questions */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;This exercise is part of the course  [[22115_-_Computational_Molecular_Evolution|Computational Molecular Evolution (22115)]].&lt;br /&gt;
&lt;br /&gt;
== Overview ==&lt;br /&gt;
&lt;br /&gt;
In this exercise we will use the software package BEASTX to infer phylogenies under molecular-clock models.&lt;br /&gt;
&lt;br /&gt;
In previous exercises, branch lengths were measured only in expected numbers of substitutions per site. In a clock-based analysis, genetic change is instead related to calendar time through a model of evolutionary rates. If temporal information is available, for example in the form of known sampling times for rapidly evolving sequences, this can be used to estimate both the rate of evolution and the times of internal nodes in the tree.&lt;br /&gt;
&lt;br /&gt;
In this exercise we will focus on so-called heterochronous data, i.e., sequence data where the individual sequences were sampled at different known times. When evolution is sufficiently rapid, the amount of sequence change observed over these sampling times contains information about the evolutionary rate and about the timing of common ancestors.&lt;br /&gt;
&lt;br /&gt;
The main purpose of the exercise is:&lt;br /&gt;
:* to become familiar with the BEASTX workflow&lt;br /&gt;
:* to set up and run a clock-based Bayesian phylogenetic analysis&lt;br /&gt;
:* to inspect MCMC output in Tracer&lt;br /&gt;
:* to summarize posterior trees using TreeAnnotator&lt;br /&gt;
:* to visualize and interpret a dated tree in FigTree&lt;br /&gt;
:* to compare a strict-clock analysis with a relaxed-clock analysis&lt;br /&gt;
&lt;br /&gt;
:* In the exercise below, you should follow the instructions on the tutorial page.&lt;br /&gt;
:* Depending on your operating system and how you installed the software, you can start the relevant programs either from the command line or by double clicking an app. The executables that you may need are:&lt;br /&gt;
:** beauti&lt;br /&gt;
:** beast&lt;br /&gt;
:** tracer&lt;br /&gt;
:** treeannotator&lt;br /&gt;
:** figtree&lt;br /&gt;
&lt;br /&gt;
== BEASTX tutorial ==&lt;br /&gt;
&lt;br /&gt;
:* Open this link in a new tab: [https://beast.community/workshop_rates_and_dates Estimating rates and dates from time-stamped sequences]&lt;br /&gt;
&lt;br /&gt;
Answer the questions below and hand in the report. Include a small number of screendumps showing relevant output from the tools you are using.&lt;br /&gt;
&lt;br /&gt;
== Questions ==&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Question 1&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
Explain what the temporal information is in this analysis. How does BEAST obtain information about the sampling times of the sequences, and why is that information needed in order to estimate dates in calendar time?&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Question 2&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
In the first analysis, the tutorial uses a strict molecular clock. What is the assumption behind this model? Explain what is being assumed about evolutionary rates on different branches, and why this means that expected branch length depends on branch duration and a single shared substitution rate. Also describe a pattern in the data that would suggest this assumption may be unrealistic.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Question 3&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
After the first BEAST run, inspect the output in Tracer. What indications are there that the initial run is not yet satisfactory? In your answer, mention burn-in, trace behaviour, and ESS, and include at least one relevant screendump from Tracer.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Question 4&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
Why does increasing the MCMC chain length help in this case? Explain the difference between increasing chain length and discarding a larger burn-in.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Question 5&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
TreeAnnotator is used to summarize the posterior sample of trees into a single representative tree. Compared with an ordinary phylogram or a simple consensus tree, what additional information does this summary tree contain? Mention at least two specific annotations visible in this tutorial, and explain briefly why each is useful.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Question 6&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
Inspect the summarized tree in FigTree. How do the virus samples from the Americas cluster relative to the African samples? What does the inferred timescale suggest about the origin and history of yellow fever virus in the Americas?&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Question 7&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
The tutorial then repeats the analysis using a relaxed lognormal clock. What is the difference between a strict clock and this relaxed-clock model? What extra biological possibility is the relaxed-clock model allowing for?&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Question 8&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
Based on the relaxed-clock analysis, is there evidence for substantial rate variation among lineages? In your answer, state which parameter in Tracer you inspected to assess this, and explain what kind of result would indicate little versus substantial rate variation. Also comment on whether the main biological conclusion about introduction of yellow fever virus into the Americas changes or remains similar under the relaxed-clock model.&lt;/div&gt;</summary>
		<author><name>Gorm</name></author>
	</entry>
	<entry>
		<id>https://teaching.healthtech.dtu.dk/22115/index.php?title=Bayesian_phylogenetics:_clock_models&amp;diff=292</id>
		<title>Bayesian phylogenetics: clock models</title>
		<link rel="alternate" type="text/html" href="https://teaching.healthtech.dtu.dk/22115/index.php?title=Bayesian_phylogenetics:_clock_models&amp;diff=292"/>
		<updated>2026-04-22T10:50:43Z</updated>

		<summary type="html">&lt;p&gt;Gorm: /* Questions */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;This exercise is part of the course  [[22115_-_Computational_Molecular_Evolution|Computational Molecular Evolution (22115)]].&lt;br /&gt;
&lt;br /&gt;
== Overview ==&lt;br /&gt;
&lt;br /&gt;
In this exercise we will use the software package BEASTX to infer phylogenies under molecular-clock models.&lt;br /&gt;
&lt;br /&gt;
In previous exercises, branch lengths were measured only in expected numbers of substitutions per site. In a clock-based analysis, genetic change is instead related to calendar time through a model of evolutionary rates. If temporal information is available, for example in the form of known sampling times for rapidly evolving sequences, this can be used to estimate both the rate of evolution and the times of internal nodes in the tree.&lt;br /&gt;
&lt;br /&gt;
In this exercise we will focus on so-called heterochronous data, i.e., sequence data where the individual sequences were sampled at different known times. When evolution is sufficiently rapid, the amount of sequence change observed over these sampling times contains information about the evolutionary rate and about the timing of common ancestors.&lt;br /&gt;
&lt;br /&gt;
The main purpose of the exercise is:&lt;br /&gt;
:* to become familiar with the BEASTX workflow&lt;br /&gt;
:* to set up and run a clock-based Bayesian phylogenetic analysis&lt;br /&gt;
:* to inspect MCMC output in Tracer&lt;br /&gt;
:* to summarize posterior trees using TreeAnnotator&lt;br /&gt;
:* to visualize and interpret a dated tree in FigTree&lt;br /&gt;
:* to compare a strict-clock analysis with a relaxed-clock analysis&lt;br /&gt;
&lt;br /&gt;
:* In the exercise below, you should follow the instructions on the tutorial page.&lt;br /&gt;
:* Depending on your operating system and how you installed the software, you can start the relevant programs either from the command line or by double clicking an app. The executables that you may need are:&lt;br /&gt;
:** beauti&lt;br /&gt;
:** beast&lt;br /&gt;
:** tracer&lt;br /&gt;
:** treeannotator&lt;br /&gt;
:** figtree&lt;br /&gt;
&lt;br /&gt;
== BEASTX tutorial ==&lt;br /&gt;
&lt;br /&gt;
:* Open this link in a new tab: [https://beast.community/workshop_rates_and_dates Estimating rates and dates from time-stamped sequences]&lt;br /&gt;
&lt;br /&gt;
Answer the questions below and hand in the report. Include a small number of screendumps showing relevant output from the tools you are using.&lt;br /&gt;
&lt;br /&gt;
== Questions ==&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Question 1&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
Explain what the temporal information is in this analysis. How does BEAST obtain information about the sampling times of the sequences, and why is that information needed in order to estimate dates in calendar time?&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Question 2&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
In the first analysis, the tutorial uses a strict molecular clock. What is the assumption behind this model? Explain what is being assumed about evolutionary rates on different branches, and why this means that expected branch length depends on branch duration and a single shared substitution rate. Also describe a pattern in the data that would suggest this assumption may be unrealistic.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Question 3&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
After the first BEAST run, inspect the output in Tracer.&lt;br /&gt;
&lt;br /&gt;
After the first BEAST run, inspect the output in Tracer. What indications are there that the initial run is not yet satisfactory? In your answer, mention burn-in, trace behaviour, and ESS, and include at least one relevant screendump from Tracer.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Question 4&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
Why does increasing the MCMC chain length help in this case? Explain the difference between increasing chain length and discarding a larger burn-in.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Question 5&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
TreeAnnotator is used to summarize the posterior sample of trees into a single representative tree. Compared with an ordinary phylogram or a simple consensus tree, what additional information does this summary tree contain? Mention at least two specific annotations visible in this tutorial, and explain briefly why each is useful.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Question 6&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
Inspect the summarized tree in FigTree. How do the virus samples from the Americas cluster relative to the African samples? What does the inferred timescale suggest about the origin and history of yellow fever virus in the Americas?&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Question 7&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
The tutorial then repeats the analysis using a relaxed lognormal clock. What is the difference between a strict clock and this relaxed-clock model? What extra biological possibility is the relaxed-clock model allowing for?&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Question 8&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
Based on the relaxed-clock analysis, is there evidence for substantial rate variation among lineages? In your answer, state which parameter in Tracer you inspected to assess this, and explain what kind of result would indicate little versus substantial rate variation. Also comment on whether the main biological conclusion about introduction of yellow fever virus into the Americas changes or remains similar under the relaxed-clock model.&lt;/div&gt;</summary>
		<author><name>Gorm</name></author>
	</entry>
	<entry>
		<id>https://teaching.healthtech.dtu.dk/22115/index.php?title=Bayesian_phylogenetics:_clock_models&amp;diff=291</id>
		<title>Bayesian phylogenetics: clock models</title>
		<link rel="alternate" type="text/html" href="https://teaching.healthtech.dtu.dk/22115/index.php?title=Bayesian_phylogenetics:_clock_models&amp;diff=291"/>
		<updated>2026-04-22T10:39:12Z</updated>

		<summary type="html">&lt;p&gt;Gorm: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;This exercise is part of the course  [[22115_-_Computational_Molecular_Evolution|Computational Molecular Evolution (22115)]].&lt;br /&gt;
&lt;br /&gt;
== Overview ==&lt;br /&gt;
&lt;br /&gt;
In this exercise we will use the software package BEASTX to infer phylogenies under molecular-clock models.&lt;br /&gt;
&lt;br /&gt;
In previous exercises, branch lengths were measured only in expected numbers of substitutions per site. In a clock-based analysis, genetic change is instead related to calendar time through a model of evolutionary rates. If temporal information is available, for example in the form of known sampling times for rapidly evolving sequences, this can be used to estimate both the rate of evolution and the times of internal nodes in the tree.&lt;br /&gt;
&lt;br /&gt;
In this exercise we will focus on so-called heterochronous data, i.e., sequence data where the individual sequences were sampled at different known times. When evolution is sufficiently rapid, the amount of sequence change observed over these sampling times contains information about the evolutionary rate and about the timing of common ancestors.&lt;br /&gt;
&lt;br /&gt;
The main purpose of the exercise is:&lt;br /&gt;
:* to become familiar with the BEASTX workflow&lt;br /&gt;
:* to set up and run a clock-based Bayesian phylogenetic analysis&lt;br /&gt;
:* to inspect MCMC output in Tracer&lt;br /&gt;
:* to summarize posterior trees using TreeAnnotator&lt;br /&gt;
:* to visualize and interpret a dated tree in FigTree&lt;br /&gt;
:* to compare a strict-clock analysis with a relaxed-clock analysis&lt;br /&gt;
&lt;br /&gt;
:* In the exercise below, you should follow the instructions on the tutorial page.&lt;br /&gt;
:* Depending on your operating system and how you installed the software, you can start the relevant programs either from the command line or by double clicking an app. The executables that you may need are:&lt;br /&gt;
:** beauti&lt;br /&gt;
:** beast&lt;br /&gt;
:** tracer&lt;br /&gt;
:** treeannotator&lt;br /&gt;
:** figtree&lt;br /&gt;
&lt;br /&gt;
== BEASTX tutorial ==&lt;br /&gt;
&lt;br /&gt;
:* Open this link in a new tab: [https://beast.community/workshop_rates_and_dates Estimating rates and dates from time-stamped sequences]&lt;br /&gt;
&lt;br /&gt;
Answer the questions below and hand in the report. Include a small number of screendumps showing relevant output from the tools you are using.&lt;br /&gt;
&lt;br /&gt;
== Questions ==&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Question 1&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
Explain what the temporal information is in this analysis. How does BEAST obtain information about the sampling times of the sequences, and why is that information needed in order to estimate dates in calendar time?&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Question 2&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
In the first analysis, the tutorial uses a strict molecular clock. What does this assumption mean biologically and statistically? Why might this be a reasonable first model to try, and what kinds of evolutionary patterns would violate this assumption?&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Question 3&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
After the first BEAST run, inspect the output in Tracer.&lt;br /&gt;
&lt;br /&gt;
After the first BEAST run, inspect the output in Tracer. What indications are there that the initial run is not yet satisfactory? In your answer, mention burn-in, trace behaviour, and ESS, and include at least one relevant screendump from Tracer.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Question 4&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
Why does increasing the MCMC chain length help in this case? Explain the difference between increasing chain length and discarding a larger burn-in.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Question 5&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
TreeAnnotator is used to summarize the posterior sample of trees into a single representative tree. Compared with an ordinary phylogram or consensus tree, what additional information does this summary tree contain? Mention at least two specific annotations and explain briefly why each is useful.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Question 6&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
Inspect the summarized tree in FigTree. How do the virus samples from the Americas cluster relative to the African samples? What does the inferred timescale suggest about the origin and history of yellow fever virus in the Americas?&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Question 7&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
The tutorial then repeats the analysis using a relaxed lognormal clock. What is the difference between a strict clock and this relaxed-clock model? What extra biological possibility is the relaxed-clock model allowing for?&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Question 8&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
Based on the relaxed-clock analysis, is there evidence for substantial rate variation among lineages? In your answer, state what output you used to assess this. Also comment on whether the main biological conclusion about introduction of yellow fever virus into the Americas changes or remains similar under the relaxed-clock model.&lt;/div&gt;</summary>
		<author><name>Gorm</name></author>
	</entry>
	<entry>
		<id>https://teaching.healthtech.dtu.dk/22115/index.php?title=Bayesian_phylogenetics:_clock_models&amp;diff=290</id>
		<title>Bayesian phylogenetics: clock models</title>
		<link rel="alternate" type="text/html" href="https://teaching.healthtech.dtu.dk/22115/index.php?title=Bayesian_phylogenetics:_clock_models&amp;diff=290"/>
		<updated>2026-04-22T09:33:52Z</updated>

		<summary type="html">&lt;p&gt;Gorm: /* Introduction to BEAST2 */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;This exercise is part of the course  [[22115_-_Computational_Molecular_Evolution|Computational Molecular Evolution (22115)]].&lt;br /&gt;
&lt;br /&gt;
== Overview ==&lt;br /&gt;
&lt;br /&gt;
In this exercise we will explore how to use the software tool BEASTX to construct phylogenies based on molecular-clock models. In previous exercises we have worked with phylogenies where we did not have information about how fast sequences were evolving, and we therefore used the number of substitutions as branch lengths. When there &#039;&#039;is&#039;&#039; temporal information (e.g., fossils that can be used to date an internal node, or information about sampling-time for rapidly evolving sequences) we can instead use clock-based models. These models assume that sequences are evolving at a more or less constant rate, branch lengths are expressed in terms of time, and we can estimate times for internal nodes. Apart from being useful when the focus is on dating evolutionary events, time trees are also useful in that the clock model itself can lead to better inference of the phylogeny (essentially because it adds prior information to the problem, such that we dont have to infer all branch lengths only from limited amounts of sequence variation).&lt;br /&gt;
&lt;br /&gt;
The main purpose with this exercise is to make you acquainted with BEASTX and to learn how to fit clock-models using either fossil data (by setting a prior on the date for internal nodes) or using so-called heterochronous data, i.e., sequences where the individual leaves have been sampled at different, known times, and where evolution is sufficiently rapid that we can estimate the parameters in a clock-model by seeing how much change has happened over time.&lt;br /&gt;
&lt;br /&gt;
For these tutorials you only need to report minimally: make a small report with a handful of uncommented screendumps showing your progress through the exercise. The important thing is that you get to be a bit familiar with the use of the program, such that you can use it in the mini project later.&lt;br /&gt;
&lt;br /&gt;
:* In the exercises below, you should simply follow the instructions on the tutorial pages. &lt;br /&gt;
:* Depending on your operating system and on how you installed the software, you can start required programs either from the command line or by double clicking an app. The names of the executables that you will need for this exercise are:&lt;br /&gt;
:** beauti&lt;br /&gt;
:** beast&lt;br /&gt;
:** tracer&lt;br /&gt;
:** treeannotator&lt;br /&gt;
:** figtree&lt;br /&gt;
&lt;br /&gt;
== Introduction to BEASTX ==&lt;br /&gt;
&lt;br /&gt;
:* Create a new directory for storing the results of this exercise:&lt;br /&gt;
 cd /path/to/molevol&lt;br /&gt;
 mkdir bayes2&lt;br /&gt;
 cd bayes2&lt;br /&gt;
:* Open this link in a new tab: [https://taming-the-beast.org/tutorials/Introduction-to-BEAST2/ Introduction to BEAST2]&lt;br /&gt;
:* Follow instructions down to the optional part.&lt;br /&gt;
:* &#039;&#039;&#039;Note:&#039;&#039;&#039; If you are running the BEASTX programs from the command line (not starting them by double clicking an app), then to get the graphical interface for BEASTX shown in figure 11 in the tutorial, you should start the program as follows:&lt;br /&gt;
 beast -options&lt;br /&gt;
&lt;br /&gt;
== Prior selection and clock calibration using Influenza A data ==&lt;br /&gt;
&lt;br /&gt;
:* Open this link in a new tab: [https://taming-the-beast.org/tutorials/Prior-selection/ Prior selection and clock calibration using Influenza A data]&lt;br /&gt;
:* &#039;&#039;&#039;NOTE:&#039;&#039;&#039; Only do the part about &#039;&#039;&#039;heterochronous&#039;&#039;&#039; data (not the homochronous part, although you can if you want to)&lt;/div&gt;</summary>
		<author><name>Gorm</name></author>
	</entry>
	<entry>
		<id>https://teaching.healthtech.dtu.dk/22115/index.php?title=Bayesian_phylogenetics:_clock_models&amp;diff=289</id>
		<title>Bayesian phylogenetics: clock models</title>
		<link rel="alternate" type="text/html" href="https://teaching.healthtech.dtu.dk/22115/index.php?title=Bayesian_phylogenetics:_clock_models&amp;diff=289"/>
		<updated>2026-04-22T09:33:17Z</updated>

		<summary type="html">&lt;p&gt;Gorm: /* Overview */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;This exercise is part of the course  [[22115_-_Computational_Molecular_Evolution|Computational Molecular Evolution (22115)]].&lt;br /&gt;
&lt;br /&gt;
== Overview ==&lt;br /&gt;
&lt;br /&gt;
In this exercise we will explore how to use the software tool BEASTX to construct phylogenies based on molecular-clock models. In previous exercises we have worked with phylogenies where we did not have information about how fast sequences were evolving, and we therefore used the number of substitutions as branch lengths. When there &#039;&#039;is&#039;&#039; temporal information (e.g., fossils that can be used to date an internal node, or information about sampling-time for rapidly evolving sequences) we can instead use clock-based models. These models assume that sequences are evolving at a more or less constant rate, branch lengths are expressed in terms of time, and we can estimate times for internal nodes. Apart from being useful when the focus is on dating evolutionary events, time trees are also useful in that the clock model itself can lead to better inference of the phylogeny (essentially because it adds prior information to the problem, such that we dont have to infer all branch lengths only from limited amounts of sequence variation).&lt;br /&gt;
&lt;br /&gt;
The main purpose with this exercise is to make you acquainted with BEASTX and to learn how to fit clock-models using either fossil data (by setting a prior on the date for internal nodes) or using so-called heterochronous data, i.e., sequences where the individual leaves have been sampled at different, known times, and where evolution is sufficiently rapid that we can estimate the parameters in a clock-model by seeing how much change has happened over time.&lt;br /&gt;
&lt;br /&gt;
For these tutorials you only need to report minimally: make a small report with a handful of uncommented screendumps showing your progress through the exercise. The important thing is that you get to be a bit familiar with the use of the program, such that you can use it in the mini project later.&lt;br /&gt;
&lt;br /&gt;
:* In the exercises below, you should simply follow the instructions on the tutorial pages. &lt;br /&gt;
:* Depending on your operating system and on how you installed the software, you can start required programs either from the command line or by double clicking an app. The names of the executables that you will need for this exercise are:&lt;br /&gt;
:** beauti&lt;br /&gt;
:** beast&lt;br /&gt;
:** tracer&lt;br /&gt;
:** treeannotator&lt;br /&gt;
:** figtree&lt;br /&gt;
&lt;br /&gt;
== Introduction to BEAST2 ==&lt;br /&gt;
&lt;br /&gt;
:* Create a new directory for storing the results of this exercise:&lt;br /&gt;
 cd /path/to/molevol&lt;br /&gt;
 mkdir bayes2&lt;br /&gt;
 cd bayes2&lt;br /&gt;
:* Open this link in a new tab: [https://taming-the-beast.org/tutorials/Introduction-to-BEAST2/ Introduction to BEAST2]&lt;br /&gt;
:* Follow instructions down to the optional part.&lt;br /&gt;
:* &#039;&#039;&#039;Note:&#039;&#039;&#039; If you are running the BEAST2 programs from the command line (not starting them by double clicking an app), then to get the graphical interface for BEAST2 shown in figure 11 in the tutorial, you should start the program as follows:&lt;br /&gt;
 beast -options&lt;br /&gt;
&lt;br /&gt;
== Prior selection and clock calibration using Influenza A data ==&lt;br /&gt;
&lt;br /&gt;
:* Open this link in a new tab: [https://taming-the-beast.org/tutorials/Prior-selection/ Prior selection and clock calibration using Influenza A data]&lt;br /&gt;
:* &#039;&#039;&#039;NOTE:&#039;&#039;&#039; Only do the part about &#039;&#039;&#039;heterochronous&#039;&#039;&#039; data (not the homochronous part, although you can if you want to)&lt;/div&gt;</summary>
		<author><name>Gorm</name></author>
	</entry>
	<entry>
		<id>https://teaching.healthtech.dtu.dk/22115/index.php?title=Bayesian_phylogenetics:_checking_convergence&amp;diff=288</id>
		<title>Bayesian phylogenetics: checking convergence</title>
		<link rel="alternate" type="text/html" href="https://teaching.healthtech.dtu.dk/22115/index.php?title=Bayesian_phylogenetics:_checking_convergence&amp;diff=288"/>
		<updated>2026-04-22T09:31:17Z</updated>

		<summary type="html">&lt;p&gt;Gorm: /* Check convergence using Tracer */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;This exercise is part of the course  [[22115_-_Computational_Molecular_Evolution|Computational Molecular Evolution (22115)]].&lt;br /&gt;
&lt;br /&gt;
== Check convergence using Tracer ==&lt;br /&gt;
&lt;br /&gt;
In this exercise you will be briefly introduced to how to check if an MCMC run has converged using the program Tracer from the BEASTX package. You will do this by re-examining the output from the Bayesian analysis you did in the week 7 exercise.&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Question 1&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
: Issue this command to start the Tracer program:&lt;br /&gt;
 tracer&lt;br /&gt;
&lt;br /&gt;
: Now import the two MCMC sample files from the MrBayes run you did in week 7 for the hcvsmall data set:&lt;br /&gt;
:* File -&amp;gt; Import Trace File (or use the + under the trace file pane)&lt;br /&gt;
:* In the import dialog: find the &amp;quot;bayes&amp;quot; directory and select &amp;quot;All files&amp;quot; under &amp;quot;files of type&amp;quot;. This should give you a list of the output files from the MrBayes run&lt;br /&gt;
:* Select the file &amp;quot;hcvsmall.nexus.run1.p&amp;quot; and open it.&lt;br /&gt;
:* Repeat process for second log file (suffix &amp;quot;.run2.p&amp;quot;)&lt;br /&gt;
&lt;br /&gt;
:::::[[File:Tracer fileload2.png |800px]]&lt;br /&gt;
&lt;br /&gt;
: You can now use Tracer to explore the results of the Bayesian analysis. The first thing you want to check is that the two independent runs have resulted in similar posteriors for the different parameters. This is investigated as follows:&lt;br /&gt;
:* Select both trace files by shift-clicking on their names in the &amp;quot;Trace files&amp;quot; pane (upper left of the Tracer window)&lt;br /&gt;
:* Select the &amp;quot;Marginal Density&amp;quot; tab in the window on the right.&lt;br /&gt;
:* Check different parameters by choosing them in the &amp;quot;Traces&amp;quot; pane on the left (while making sure you still have both trace files selected). This will show the two posteriors for the chosen parameter (see example below). If a run has converged then the two posteriors should mostly be placed right on top of each other. &lt;br /&gt;
:* Note that Tracer by default uses a burnin of 10% of the total number of generations. You can change that by double-clicking in the Burn-in field of the trace file pane (you need to change it separately for each file). Typically we would use a burn-in of 25% or 50%.&lt;br /&gt;
&lt;br /&gt;
:::::[[File:Tracer marginals overlap.png|800px]]&lt;br /&gt;
&lt;br /&gt;
:* The plot below shows an example where convergence has not occurred yet:&lt;br /&gt;
&lt;br /&gt;
:::::[[File:Tracer lousy convergence.png|800px]]&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Question: &#039;&#039;&#039;Select both trace files in the “Trace Files” pane and inspect the Marginal Density plots for the parameters r(A&amp;lt;-&amp;gt;G){all} and m{2}.&lt;br /&gt;
&lt;br /&gt;
Include the marginal posterior plots for these two parameters in your report. For each parameter, describe whether the posterior distributions from the two independent runs overlap closely, and explain what this indicates about convergence.&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Question 2&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
:* Another thing to check is how the trace looks as a function of the iteration number: Optimally you would want a trace that looks like a &amp;quot;hairy caterpillar&amp;quot;, with random jumps up and down on a mostly constant level (see example below). &lt;br /&gt;
:* Select the &amp;quot;Trace&amp;quot; tab in the window on the right to see trace plots (still with one or both trace files selected in the Trace File pane).&lt;br /&gt;
:* Related to this: The ESS column gives the &amp;quot;Effective Sample Size&amp;quot; for each parameter. As a rule of thumb we want this to be at least 200 (and Tracer flags smaller values by colouring the ESS values). &lt;br /&gt;
:** Briefly, the problem here is that consecutive samples from MCMC are correlated (they are not independent). This is due to the use of a Markov chain for sampling: the new position in parameter space depends on the previous location (and the proposal distribution). &lt;br /&gt;
:** The degree of non-indepence can be quantified by the auto-correlation for different lags: The autocorrelation for lag k is found by computing the Pearson correlation between the vector of all samples, and the values in the same vector shifted k positions. &lt;br /&gt;
:** Based on computation of auto-correlation at different lags (&amp;lt;math&amp;gt;k = [1, 2, 3, ...]&amp;lt;/math&amp;gt;) Tracer determines the Auto-Correlation Time (ACT), which is the number of generations in the MCMC chain that two samples have to be separated by for them to be uncorrelated. The ACT for a parameter can be seen in the Estimates tab in Tracer.&lt;br /&gt;
:** Tracer also estimates the Effective Sample Size (ESS), which is the number of independent samples that the trace is equivalent to. This is essentially the chain length (excluding the burn-in) divided by the ACT.&lt;br /&gt;
:* Note how the highlighted parameter corresponding to the hairy caterpillar trace also has a high ESS in the example below.&lt;br /&gt;
&lt;br /&gt;
:::::[[File:Tracer hairy caterpillar.png|800px]]&lt;br /&gt;
&lt;br /&gt;
:* Trace plots where there are clearly visible dips and rises (see example below) indicates that there is auto correlation among the samples we have included - the samples are not independent of each other (and therefore provide less information about the posterior). This is referred to as &amp;quot;poor mixing&amp;quot;. One solution to such a problem is to increase the number of iterations (and perhaps write samples less frequently). It might also be an indication that the model fits poorly, and that you could get a better convergence by changing the substitution model, or setting more informative priors.&lt;br /&gt;
:* Note how the poorly mixing parameter in the example below also has a low ESS.&lt;br /&gt;
&lt;br /&gt;
:::::[[File:Tracer ugly caterpillar.png|800px]]&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Question: &#039;&#039;&#039;Now inspect the Trace plots for the same two parameters: r(A&amp;lt;-&amp;gt;G){all} and m{2}.&lt;br /&gt;
&lt;br /&gt;
Include the trace plots for these two parameters in your report, and state the ESS for each of them. For each parameter, describe whether the trace looks well mixed, and compare the two parameters in terms of sampling efficiency. Does one of them appear to have stronger autocorrelation than the other?&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Question 3&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Question: &#039;&#039;&#039;Using both the marginal density plots and the trace plots/ESS values, assess whether r(A&amp;lt;-&amp;gt;G){all} and m{2} appear to have converged. Explain what pattern in either the marginal densities or the trace/ESS values would have made you doubt convergence.&lt;/div&gt;</summary>
		<author><name>Gorm</name></author>
	</entry>
	<entry>
		<id>https://teaching.healthtech.dtu.dk/22115/index.php?title=Bayesian_phylogenetics:_checking_convergence&amp;diff=287</id>
		<title>Bayesian phylogenetics: checking convergence</title>
		<link rel="alternate" type="text/html" href="https://teaching.healthtech.dtu.dk/22115/index.php?title=Bayesian_phylogenetics:_checking_convergence&amp;diff=287"/>
		<updated>2026-04-22T09:09:23Z</updated>

		<summary type="html">&lt;p&gt;Gorm: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;This exercise is part of the course  [[22115_-_Computational_Molecular_Evolution|Computational Molecular Evolution (22115)]].&lt;br /&gt;
&lt;br /&gt;
== Check convergence using Tracer ==&lt;br /&gt;
&lt;br /&gt;
In this exercise you will be briefly introduced to how to check if an MCMC run has converged using the program Tracer from the BEASTX package. You will do this by re-examining the output from the Bayesian analysis you did in the week 7 exercise.&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Question 1&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
: Issue this command to start the Tracer program:&lt;br /&gt;
 tracer&lt;br /&gt;
&lt;br /&gt;
: Now import the two MCMC sample files from the MrBayes run you did in week 7 for the hcvsmall data set:&lt;br /&gt;
:* File -&amp;gt; Import Trace File (or use the + under the trace file pane)&lt;br /&gt;
:* In the import dialog: find the &amp;quot;bayes&amp;quot; directory and select &amp;quot;All files&amp;quot; under &amp;quot;files of type&amp;quot;. This should give you a list of the output files from the MrBayes run&lt;br /&gt;
:* Select the file &amp;quot;hcvsmall.nexus.run1.p&amp;quot; and open it.&lt;br /&gt;
:* Repeat process for second log file (suffix &amp;quot;.run2.p&amp;quot;)&lt;br /&gt;
&lt;br /&gt;
:::::[[File:Tracer fileload2.png |800px]]&lt;br /&gt;
&lt;br /&gt;
: You can now use Tracer to explore the results of the Bayesian analysis. The first thing you want to check is that the two independent runs have resulted in similar posteriors for the different parameters. This is investigated as follows:&lt;br /&gt;
:* Select both trace files by shift-clicking on their names in the &amp;quot;Trace files&amp;quot; pane (upper left of the Tracer window)&lt;br /&gt;
:* Select the &amp;quot;Marginal Density&amp;quot; tab in the window on the right.&lt;br /&gt;
:* Check different parameters by choosing them in the &amp;quot;Traces&amp;quot; pane on the left (while making sure you still have both trace files selected). This will show the two posteriors for the chosen parameter (see example below). If a run has converged then the two posteriors should mostly be placed right on top of each other. &lt;br /&gt;
:* Note that Tracer by default uses a burnin of 10% of the total number of generations. You can change that by double-clicking in the Burn-in field of the trace file pane (you need to change it separately for each file). Typically we would use a burn-in of 25% or 50%.&lt;br /&gt;
&lt;br /&gt;
:::::[[File:Tracer marginals overlap.png|800px]]&lt;br /&gt;
&lt;br /&gt;
:* The plot below shows an example where convergence has not occurred yet:&lt;br /&gt;
&lt;br /&gt;
:::::[[File:Tracer lousy convergence.png|800px]]&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Question: &#039;&#039;&#039;Take screen dumps of the marginal posterior plots for the following parameters and include them in your report: m{1} and piA{all}&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Question 2&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
:* Another thing to check is how the trace looks as a function of the iteration number: Optimally you would want a trace that looks like a &amp;quot;hairy caterpillar&amp;quot;, with random jumps up and down on a mostly constant level (see example below). &lt;br /&gt;
:* Select the &amp;quot;Trace&amp;quot; tab in the window on the right to see trace plots (still with one or both trace files selected in the Trace File pane).&lt;br /&gt;
:* Related to this: The ESS column gives the &amp;quot;Effective Sample Size&amp;quot; for each parameter. As a rule of thumb we want this to be at least 200 (and Tracer flags smaller values by colouring the ESS values). &lt;br /&gt;
:** Briefly, the problem here is that consecutive samples from MCMC are correlated (they are not independent). This is due to the use of a Markov chain for sampling: the new position in parameter space depends on the previous location (and the proposal distribution). &lt;br /&gt;
:** The degree of non-indepence can be quantified by the auto-correlation for different lags: The autocorrelation for lag k is found by computing the Pearson correlation between the vector of all samples, and the values in the same vector shifted k positions. &lt;br /&gt;
:** Based on computation of auto-correlation at different lags (&amp;lt;math&amp;gt;k = [1, 2, 3, ...]&amp;lt;/math&amp;gt;) Tracer determines the Auto-Correlation Time (ACT), which is the number of generations in the MCMC chain that two samples have to be separated by for them to be uncorrelated. The ACT for a parameter can be seen in the Estimates tab in Tracer.&lt;br /&gt;
:** Tracer also estimates the Effective Sample Size (ESS), which is the number of independent samples that the trace is equivalent to. This is essentially the chain length (excluding the burn-in) divided by the ACT.&lt;br /&gt;
:* Note how the highlighted parameter corresponding to the hairy caterpillar trace also has a high ESS in the example below.&lt;br /&gt;
&lt;br /&gt;
:::::[[File:Tracer hairy caterpillar.png|800px]]&lt;br /&gt;
&lt;br /&gt;
:* Trace plots where there are clearly visible dips and rises (see example below) indicates that there is auto correlation among the samples we have included - the samples are not independent of each other (and therefore provide less information about the posterior). This is referred to as &amp;quot;poor mixing&amp;quot;. One solution to such a problem is to increase the number of iterations (and perhaps write samples less frequently). It might also be an indication that the model fits poorly, and that you could get a better convergence by changing the substitution model, or setting more informative priors.&lt;br /&gt;
:* Note how the poorly mixing parameter in the example below also has a low ESS.&lt;br /&gt;
&lt;br /&gt;
:::::[[File:Tracer ugly caterpillar.png|800px]]&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Question: &#039;&#039;&#039;Take screen dumps of the trace plots for the following parameters and include them in your report: m{1} and piA{all}. What is the ESS for these parameters?&lt;/div&gt;</summary>
		<author><name>Gorm</name></author>
	</entry>
	<entry>
		<id>https://teaching.healthtech.dtu.dk/22115/index.php?title=Bayesian_phylogenetics:_checking_convergence&amp;diff=286</id>
		<title>Bayesian phylogenetics: checking convergence</title>
		<link rel="alternate" type="text/html" href="https://teaching.healthtech.dtu.dk/22115/index.php?title=Bayesian_phylogenetics:_checking_convergence&amp;diff=286"/>
		<updated>2026-04-22T09:03:06Z</updated>

		<summary type="html">&lt;p&gt;Gorm: /* Check convergence using Tracer */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;This exercise is part of the course  [[22115_-_Computational_Molecular_Evolution|Computational Molecular Evolution (22115)]].&lt;br /&gt;
&lt;br /&gt;
== Check convergence using Tracer ==&lt;br /&gt;
&lt;br /&gt;
In this exercise you will be briefly introduced to how to check if an MCMC run has converged using the program Tracer from the BEASTX package. You will do this by re-examining the output from the Bayesian analysis you did in the week 7 exercise.&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Question 1&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
: Issue this command to start the Tracer program:&lt;br /&gt;
 tracer&lt;br /&gt;
&lt;br /&gt;
: Now import the two MCMC sample files from the MrBayes run you did in week 7 for the hcvsmall data set:&lt;br /&gt;
:* File -&amp;gt; Import Trace File (or use the + under the trace file pane)&lt;br /&gt;
:* In the import dialog: find the &amp;quot;bayes&amp;quot; directory and select &amp;quot;All files&amp;quot; under &amp;quot;files of type&amp;quot;. This should give you a list of the output files from the MrBayes run&lt;br /&gt;
:* Select the file &amp;quot;hcvsmall.nexus.run1.p&amp;quot; and open it.&lt;br /&gt;
:* Repeat process for second log file (suffix &amp;quot;.run2.p&amp;quot;)&lt;br /&gt;
&lt;br /&gt;
:::::[[File:Tracer fileload2.png |800px]]&lt;br /&gt;
&lt;br /&gt;
: You can now use Tracer to explore the results of the Bayesian analysis. The first thing you want to check is that the two independent runs have resulted in similar posteriors for the different parameters. This is investigated as follows:&lt;br /&gt;
:* Select both trace files by shift-clicking on their names in the &amp;quot;Trace files&amp;quot; pane (upper left of the Tracer window)&lt;br /&gt;
:* Select the &amp;quot;Marginal Density&amp;quot; tab in the window on the right.&lt;br /&gt;
:* Check different parameters by choosing them in the &amp;quot;Traces&amp;quot; pane on the left (while making sure you still have both trace files selected). This will show the two posteriors for the chosen parameter (see example below). If a run has converged then the two posteriors should mostly be placed right on top of each other. &lt;br /&gt;
:* Note that Tracer by default uses a burnin of 10% of the total number of generations. You can change that by double-clicking in the Burn-in field of the trace file pane (you need to change it separately for each file). Typically we would use a burn-in of 25% or 50%.&lt;br /&gt;
&lt;br /&gt;
:::::[[File:Tracer marginals overlap.png|800px]]&lt;br /&gt;
&lt;br /&gt;
:* The plot below shows an example where convergence has not occurred yet:&lt;br /&gt;
&lt;br /&gt;
:::::[[File:Tracer lousy convergence.png|800px]]&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Question: &#039;&#039;&#039;Take screen dumps of the marginal posterior plots for the following parameters and include them in your report: m{1} and piA{all}&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Question 2&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
:* Another thing to check is how the trace looks as a function of the iteration number: Optimally you would want a trace that looks like a &amp;quot;hairy caterpillar&amp;quot;, with random jumps up and down on a mostly constant level (see example below). &lt;br /&gt;
:* Select the &amp;quot;Trace&amp;quot; tab in the window on the right to see trace plots (still with one or both trace files selected in the Trace File pane).&lt;br /&gt;
:* Related to this: The ESS column gives the &amp;quot;Effective Sample Size&amp;quot; for each parameter. As a rule of thumb we want this to be at least 200 (and Tracer flags smaller values by colouring the ESS values). &lt;br /&gt;
:** Briefly, the problem here is that consecutive samples from MCMC are correlated (they are not independent). This is due to the use of a Markov chain for sampling: the new position in parameter space depends on the previous location (and the proposal distribution). &lt;br /&gt;
:** The degree of non-indepence can be quantified by the auto-correlation for different lags: The autocorrelation for lag k is found by computing the Pearson correlation between all samples, and the samples k generations later. &lt;br /&gt;
:** Based on computation of auto-correlation at different lags (&amp;lt;math&amp;gt;k = [1, 2, 3, ...]&amp;lt;/math&amp;gt;) Tracer determines the Auto-Correlation Time (ACT), which is the number of generations in the MCMC chain that two samples have to be separated by for them to be uncorrelated. The ACT for a parameter can be seen in the Estimates tab in Tracer.&lt;br /&gt;
:** Tracer also estimates the Effective Sample Size (ESS), which is the number of independent samples that the trace is equivalent to. This is essentially the chain length (excluding the burn-in) divided by the ACT.&lt;br /&gt;
:* Note how the highlighted parameter corresponding to the hairy caterpillar trace also has a high ESS in the example below.&lt;br /&gt;
&lt;br /&gt;
:::::[[File:Tracer hairy caterpillar.png|800px]]&lt;br /&gt;
&lt;br /&gt;
:* Trace plots where there are clearly visible dips and rises (see example below) indicates that there is auto correlation among the samples we have included - the samples are not independent of each other (and therefore provide less information about the posterior). This is referred to as &amp;quot;poor mixing&amp;quot;. One solution to such a problem is to increase the number of iterations (and perhaps write samples less frequently). It might also be an indication that the model fits poorly, and that you could get a better convergence by changing the substitution model, or setting more informative priors.&lt;br /&gt;
:* Note how the poorly mixing parameter in the example below also has a low ESS.&lt;br /&gt;
&lt;br /&gt;
:::::[[File:Tracer ugly caterpillar.png|800px]]&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Question: &#039;&#039;&#039;Take screen dumps of the trace plots for the following parameters and include them in your report: m{1} and piA{all}. What is the ESS for these parameters?&lt;/div&gt;</summary>
		<author><name>Gorm</name></author>
	</entry>
	<entry>
		<id>https://teaching.healthtech.dtu.dk/22115/index.php?title=Model_selection&amp;diff=285</id>
		<title>Model selection</title>
		<link rel="alternate" type="text/html" href="https://teaching.healthtech.dtu.dk/22115/index.php?title=Model_selection&amp;diff=285"/>
		<updated>2026-04-15T18:27:05Z</updated>

		<summary type="html">&lt;p&gt;Gorm: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;This exercise is part of the course  [[22115_-_Computational_Molecular_Evolution|Computational Molecular Evolution (22115)]].&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Overview ==&lt;br /&gt;
&lt;br /&gt;
: In this exercise you are going to investigate features of HIV-1 evolution. You will do this by analyzing a large set of env-genes from HIV-1, subtype B. specifically, the DNA sequences analyzed here correspond to a region surrounding the hypervariable V3 region of the gp120 protein.&lt;br /&gt;
&lt;br /&gt;
: Like other retroviruses, particles of HIV are made up of 2 copies of a single-stranded RNA genome packaged inside a protein core, or capsid. The core particle also contains viral proteins that are essential for the early steps of the virus life cycle, such as reverse transcription and integration. A lipid envelope, derived from the infected cell, surrounds the core particle. Embedded in this envelope are the surface glycoproteins of HIV: gp120 and gp41. The gp120 protein is crucial for binding of the virus particle to target cells, while gp41 is important for the subsequent fusion event. It is the specific affinity of gp120 for the CD4 protein that targets HIV to those cells of the immune system that express CD4 on their surface (e.g., T-helper lymphocytes, monocytes, and macrophages).&lt;br /&gt;
&lt;br /&gt;
: The role gp120 plays in infection and the fact that it is situated on the surface of the HIV particle, means it is an obvious target for the immune response. That means that there may be a considerable selective pressure on gp120 for creating immune-escape mutants, where amino acids in the gp120 epitopes have been substituted. In this exercise you will construct a maximum likelihood tree that we will subsequently use to investigate whether you can detect such a selective pressure on parts of gp120, again using maximum likelihood methods.&lt;br /&gt;
&lt;br /&gt;
: One major goal with the exercise is to introduce you to statistically based methods for assessing the strength of evidence for a set of alternative hypotheses about some biological system of interest. The model selection method we will use is AIC (Akaike Information Criterion), based on which you will compute Akaike weights, which can be interpreted as relative support for the models, or approximately as relative model probabilities within the candidate set. A second goal is to make you aware that phylogenetic analysis is not only about constructing trees, but that it is also a useful framework for analyzing biological questions more generally.&lt;br /&gt;
&lt;br /&gt;
: Specifically, you will&lt;br /&gt;
&lt;br /&gt;
:# perform a multiple alignment of gp120 DNA sequences taking protein-level information into account (using revtrans).&lt;br /&gt;
:# select a suitable nucleotide substitution model (using jmodeltest2)&lt;br /&gt;
:# construct a phylogenetic tree (using PAUP).&lt;br /&gt;
:# try to detect positively selected sites in gp120 (using PAML).&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
== Recipe for computing AIC values and Akaike weights (interpretable as relative model probabilities) ==&lt;br /&gt;
&lt;br /&gt;
: Later in today’s exercise you will be asked to compute AIC values and Akaike weights. Return to this section and follow the instructions when you need to do so.&lt;br /&gt;
&lt;br /&gt;
:# Fit a set of models to your data, note the maximized log likelihoods (lnL) and the number of free parameters (K) for each model in the investigated set. The models you fit should represent a plausible and comprehensive set of hypotheses about your data.&lt;br /&gt;
:# Compute AIC for each of the models: &#039;&#039;&#039;AIC = -2 x lnL + 2K&#039;&#039;&#039;.  &amp;lt;br&amp;gt;For example: a model with lnL = -2010 and K = 5 will have AIC = -2 x -2010 + 2 x 5 = 4030.&lt;br /&gt;
:# Identify the model with the smallest AIC (this is the best model in the set). We will call the AIC for this model &#039;&#039;&#039;&amp;quot;AICmin&amp;quot;&#039;&#039;&#039;.&lt;br /&gt;
:# Compute the &amp;quot;ΔAIC&amp;quot; values for each model: &#039;&#039;&#039;ΔAIC = AIC - AICmin&#039;&#039;&#039; &amp;lt;br&amp;gt;For each model subtract the minimum AIC value. The best model will have a ΔAIC of zero. The rest of the models will have positive ΔAICs.&lt;br /&gt;
:# For each model compute the following quantity: &#039;&#039;&#039;numerator = exp(-0.5 x ΔAIC)&#039;&#039;&#039; &amp;lt;br&amp;gt;For example, a model with ΔAIC=4.2 will have numerator = exp(-0.5 x 4.2) = exp(-2.1) = 0.1225. Also compute the &#039;&#039;&#039;sum&#039;&#039;&#039; of the numerator values for all models.&lt;br /&gt;
:# Finally, the Akaike weight for each model is found as:&amp;lt;br&amp;gt; &#039;&#039;&#039;weight(model) = numerator / sum&#039;&#039;&#039; &amp;lt;br&amp;gt;For example, if sum = 3.75 and a model has numerator = 1.3, then it has P(model) = 1.3 / 3.75 = 0.35&lt;br /&gt;
&lt;br /&gt;
: You may want to keep track of the computations by constructing a table along the following lines:&lt;br /&gt;
[[File:Molevol-Downloads-aictable.png|700px]]&lt;br /&gt;
&lt;br /&gt;
: Note that model probabilities can also be computed using Bayesian methods. One advantage of Bayesian methods over AIC is that instead of relying on a point estimate, uncertainty about parameter values is accounted for by integrating over all possible values (typically using MCMC).&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
== Getting started ==&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Create working directory, copy files&#039;&#039;&#039;&lt;br /&gt;
: In the command below: Instead of /path/to/molevol enter the path to the directory where you have placed your course files (for instance cd /Users/bob/Documents/molevol, or cd /home/student/molevol).&lt;br /&gt;
 cd /path/to/molevol&lt;br /&gt;
 mkdir modelselect&lt;br /&gt;
 cd modelselect&lt;br /&gt;
 cp ../data/gp120.fasta ./gp120.fasta&lt;br /&gt;
 cp ../data/codeml.ctl ./codeml.ctl&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Have a look at the DNA data file:&#039;&#039;&#039;&lt;br /&gt;
 nedit gp120.fasta &amp;amp;&lt;br /&gt;
: The file contains several DNA sequences from HIV-1, subtype B. The sequences are approximately 500 bp long, and correspond to a region surrounding the hypervariable V3 region in the gene encoding gp120. Close the nedit window when you&#039;ve had a look.&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
== Protein-guided alignment of coding DNA ==&lt;br /&gt;
&lt;br /&gt;
: There are three reasons why it is preferable to align coding DNA at the amino acid level rather than directly as nucleotides. First, the DNA alphabet has only four letters compared to 20 amino acids, which means that even unrelated DNA sequences will share roughly 25% identity by chance alone, making it much harder to distinguish true homology from noise. Second, protein alignments benefit from empirical substitution matrices such as BLOSUM-62, which capture the fact that amino acid replacements tend to be conservative. Equivalent matrices for DNA are far less informative because nucleotide substitution patterns vary strongly between genes and organisms. Third, synonymous substitutions accumulate faster than non-synonymous ones, so DNA sequences diverge more rapidly than the proteins they encode. Taken together, these factors mean that the phylogenetic signal in a DNA alignment erodes much faster than in the corresponding protein alignment.&lt;br /&gt;
&lt;br /&gt;
: However, while protein-level alignment gives us the most reliable homology signal, it discards exactly the information we need for studying selection: the synonymous substitutions. Estimating the ratio of non-synonymous to synonymous substitution rates (dN/dS) requires a codon-level alignment where analogous codon positions are properly lined up. We would therefore like to construct a multiple alignment at the DNA level, but using information at the protein level, and the RevTrans server does exactly that.&lt;br /&gt;
&lt;br /&gt;
: &#039;&#039;&#039;RevTrans&#039;&#039;&#039; takes as input an unaligned set of DNA sequences, automatically translates them to the equivalent amino acid sequences, constructs a multiple alignment of the protein sequences, and finally uses the protein alignment as a template for constructing a multiple DNA alignment. Because each amino acid maps back to its original codon, gaps are always inserted in groups of three and codon boundaries are preserved throughout the alignment. This is essential for the dN/dS analyses we will carry out in this exercise: if codon positions are misaligned, synonymous and non-synonymous changes cannot be correctly distinguished.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Construct RevTrans alignment&#039;&#039;&#039;&lt;br /&gt;
:* Open RevTrans server page: https://services.healthtech.dtu.dk/services/RevTrans-2.0/&lt;br /&gt;
:* On the RevTrans page: Choose the file gp120.fasta as input (or copy and paste the sequence into the sequence window)&lt;br /&gt;
:* Click the &amp;quot;Submit query&amp;quot; button&lt;br /&gt;
:* When the alignment is done you may have to click link named &amp;quot;here&amp;quot; to go to results page&lt;br /&gt;
:* Download DNA alignment, by right-clicking the link for &amp;quot;Download alignment in FASTA format&amp;quot;, and choosing &amp;quot;Save link as...&amp;quot; (save file under the name gp120align.fasta and make sure to save the file in the directory modelselect).&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Convert alignment to NEXUS format&#039;&#039;&#039;&lt;br /&gt;
: Convert the fasta file to NEXUS format and save file in the modelselect directory under the name gp120.nexus&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
== Model selection using AIC and jModelTest2 ==&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Question 1&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
: As part of the present analysis we are going to build a phylogenetic tree based on the DNA alignment constructed above. We will construct the tree using maximum likelihood, but to do that we first have to decide which substitution model we want to use. Specifically, we are interested in selecting a model that achieves a good balance between fit to the data and number of parameters. We will investigate this issue by fitting a set of 56 different models to our data and then comparing them using AIC.&lt;br /&gt;
&lt;br /&gt;
: In the first part of the exercise, however, you will only compare three closely related models by hand. The purpose of this restricted comparison is not primarily to identify the globally best model, but to understand how AIC, ΔAIC, and Akaike weights are computed and interpreted. Because only three models are included in this manual comparison, the weights you compute will describe relative support &#039;&#039;&#039;within this restricted set only&#039;&#039;&#039;.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Start jmodeltest2&#039;&#039;&#039;&lt;br /&gt;
 jmodeltest&lt;br /&gt;
&#039;&#039;&#039;Load data set&#039;&#039;&#039;&lt;br /&gt;
:* File -&amp;gt; Load DNA alignment -&amp;gt; File Format -&amp;gt; Select &amp;quot;All files&amp;quot;&lt;br /&gt;
:* Navigate to gp120.nexus and load it&lt;br /&gt;
&#039;&#039;&#039;Fit 56 models&#039;&#039;&#039;&lt;br /&gt;
:* Analysis -&amp;gt; Compute likelihood scores&lt;br /&gt;
:* Select &amp;quot;7&amp;quot; under &amp;quot;Number of substitution schemes&amp;quot;&lt;br /&gt;
:* Select &amp;quot;Fixed BIONJ-JC&amp;quot; under &amp;quot;Base tree for likelihood calculations&amp;quot;&lt;br /&gt;
:* Click &amp;quot;Compute likelihoods&amp;quot;&lt;br /&gt;
&lt;br /&gt;
: This causes jmodeltest2 to perform the following actions: first a neighbor joining tree is constructed using the Jukes and Cantor model. Then the tree is fixed and used as the basis for fitting a set of 56 different models to the data. For each model, the estimated model parameters and the negative log-likelihood are recorded. In addition to varying sets of substitution rate parameters (JC, K2P, ...), some of these models also include extra parameters that take into account the presence of different rates between sites. This is done in two ways: (1) by fitting a gamma distribution of rates (&amp;quot;+G&amp;quot;), and (2) by allowing for a proportion of constant (&amp;quot;invariable&amp;quot;) sites (&amp;quot;+I&amp;quot;).&lt;br /&gt;
&lt;br /&gt;
: Wait until jmodeltest2 is done fitting all 56 models (this will take a little while depending on your computer).&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Inspect result&#039;&#039;&#039;&lt;br /&gt;
:* Results -&amp;gt; Show results table&lt;br /&gt;
: For each model this table lists the negative log-likelihood (&amp;quot;-lnL&amp;quot;), the number of parameters (&amp;quot;p&amp;quot; - note this includes branch lengths), and estimates of all model parameters (excluding branch lengths).&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Manually compute Akaike weights for three substitution models&#039;&#039;&#039;&lt;br /&gt;
: Use AIC-based model comparison to investigate which of the following three substitution models are best supported as descriptions of how the sequences have evolved:&lt;br /&gt;
:* Jukes and Cantor with fraction of invariant sites (JC+I)&lt;br /&gt;
:* Jukes and Cantor with gamma-distributed rates over sites (JC+G)&lt;br /&gt;
:* Jukes and Cantor with invariant sites and gamma-distributed rates (JC+I+G)&lt;br /&gt;
: Before you can do the computation you need to know the log likelihood and the number of parameters for each model. Locate these values in the table for the JC+I, JC+G, and JC+I+G models, and write them down. Close the window with the result table when you are done.&lt;br /&gt;
&lt;br /&gt;
: Make sure to get the signs right: the values reported in the table are -lnL values, so you will need to reverse the sign to get the lnL (the lnL values you write down should be negative).&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Question: &#039;&#039;&#039;Use the recipe above to compute AIC, ΔAIC, and Akaike weight for the three models. Report the results in a table similar to the one shown above, and verify that the three weights sum to 1 (up to rounding error).&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Question 2&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
: Based on the Akaike weights, which of the three models has the strongest support? Is the support strongly concentrated on one model, or is there still substantial support for one or both of the others?&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Question 3&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
: What you just did manually for JC+I, JC+G and JC+I+G, jmodeltest2 can do automatically for the full set of 56 fitted models. Specifically, it uses the list of negative log likelihoods and parameter counts in the table to compute AIC values and Akaike weights for the full candidate set:&lt;br /&gt;
:* Analysis -&amp;gt; Do AIC calculations -&amp;gt;&lt;br /&gt;
:* Select &amp;quot;Write PAUP* block&amp;quot;&lt;br /&gt;
:* click &amp;quot;Do AIC calculations&amp;quot;&lt;br /&gt;
:* Results -&amp;gt; Show results table&lt;br /&gt;
:* Select &amp;quot;AIC&amp;quot; tab&lt;br /&gt;
:* &#039;&#039;&#039;IMPORTANT:&#039;&#039;&#039; SHIFT+click on the header of the &amp;quot;weight&amp;quot; column. This sorts the rows according to model weight, in descending order.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Question: &#039;&#039;&#039;What model was selected by modeltest based on the AIC values? (This will be the model with the highest weight - and lowest AIC - and will be in the first row of the results table after SHIFT-clicking the &amp;quot;weight&amp;quot; header).&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Question 4&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
: In the same AIC results table, now click (not SHIFT+click) the header of the &amp;quot;-lnL&amp;quot; column. In this table, a normal click sorts in ascending order, whereas SHIFT+click sorts in descending order. The model at the top will therefore be the one with the smallest value of &amp;quot;-lnL&amp;quot; (equivalently: the highest likelihood). Compare this model to the one with the highest Akaike weight.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Question: &#039;&#039;&#039;Is the model with the highest likelihood also the model with the highest Akaike weight? If not, explain briefly why these two rankings can differ.&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
== Phylogenetic tree inference ==&lt;br /&gt;
&lt;br /&gt;
=== Tree inference using the model selected by jModelTest2 (PAUP) ===&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Question 5&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
: Above, you used jModelTest2 to select a substitution model based on AIC. You will now use that model to construct a maximum likelihood tree in &#039;&#039;&#039;PAUP&#039;&#039;&#039;. The purpose of doing this in PAUP is mainly pedagogical: it makes the individual steps of model-based phylogenetic inference more explicit. In practice, one would often use a program such as IQ-TREE 3 (see next section).&lt;br /&gt;
&lt;br /&gt;
: Close the results table. In the main window you should now scroll up to the lines giving PAUP commands that implement the selected model. The command is enclosed between &amp;quot;BEGIN PAUP&amp;quot; and &amp;quot;END;&amp;quot; and should look something like this:&lt;br /&gt;
 Lset Base=(0.4064, [...]&lt;br /&gt;
: You will need to copy this command to a PAUP session in the next step.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Start PAUP&#039;&#039;&#039;&lt;br /&gt;
 paup&lt;br /&gt;
: Above, you used jModelTest2 to select the most suitable substitution model for the present data set. You will now use this model to construct a maximum likelihood tree. You will use PAUP for this purpose. (Note: it is possible to create a maximum likelihood tree, or even a model-averaged tree, directly from jModelTest2, but we will instead do it in PAUP in order to more clearly see each step that is taken.)&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Load alignment:&#039;&#039;&#039;&lt;br /&gt;
 execute gp120.nexus&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Set tree-building criterion to maximum likelihood&#039;&#039;&#039;&lt;br /&gt;
 set criterion=likelihood&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Set model parameters to winning estimates&#039;&#039;&#039;&lt;br /&gt;
: Above, you located a set of lines in the jModelTest2 output giving a PAUP command that sets the model parameters to the estimates found for the winning model. Copy and paste this lset command (without the BEGIN and END parts) into the window where PAUP is running.&lt;br /&gt;
&lt;br /&gt;
 PASTE LSET COMMAND FROM MODELTEST RUN HERE&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Find best tree using selected model&#039;&#039;&#039;&lt;br /&gt;
: Still in the PAUP window, enter the following command:&lt;br /&gt;
 hsearch swap=tbr start=nj&lt;br /&gt;
: This command causes PAUP to perform a heuristic search for the best maximum likelihood tree. Once an initial tree has been constructed, the heuristic search proceeds by rearrangements of the &amp;quot;tree bisection and reconnection&amp;quot; type (TBR). We are using the model selected by modeltest, and the parameter estimates found by modeltest for that model. You could also choose to estimate all the model parameters as part of this step (i.e., at the same time as finding the best tree), but fixing them improves speed tremendously. Finding the best tree should take at most a few minutes.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Save best tree to file&#039;&#039;&#039;&lt;br /&gt;
 savetrees format=newick brlens=yes file=gp120tree_paup.phy from=1 to=1&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Quit program&#039;&#039;&#039;&lt;br /&gt;
 quit&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Have a look at the tree:&#039;&#039;&#039;&lt;br /&gt;
: You have now produced an unrooted tree of the HIV sequences and saved it in the file gp120tree_paup.phy. Note that in this exercise we will not be interested in the tree as such - our focus is instead on finding positive selection at a subset of codon positions, and the tree is just something we need in order to fit the different codon models to the data. If you want to see the tree, you can do so with the following command:&lt;br /&gt;
 figtree gp120tree_paup.phy &amp;amp;&lt;br /&gt;
: There is no meaningful root placed on this tree, so you may want to choose the unrooted view (the third icon in the Layout section of the FigTree window). Close the FigTree window when you have had a look.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Question: &#039;&#039;&#039;Which substitution model did you use for the PAUP tree search, and what was the negative log likelihood of the best tree found under this model?&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
=== One-step model selection and tree inference using IQ-TREE 3 ===&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Question 6&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
: In real-life phylogenetic analysis one would often use a modern program such as &#039;&#039;&#039;IQ-TREE 3&#039;&#039;&#039;, which can carry out model selection and maximum likelihood tree reconstruction in a single analysis. You will now repeat the analysis using this more integrated approach.&lt;br /&gt;
&lt;br /&gt;
: Run IQ-TREE 3 on the alignment using the following command:&lt;br /&gt;
 iqtree3 -s gp120.nexus -m MFP&lt;br /&gt;
&lt;br /&gt;
: Here, &amp;quot;-m MFP&amp;quot; tells IQ-TREE 3 to perform automatic model selection using &amp;quot;ModelFinder Plus&amp;quot; before reconstructing the tree.&lt;br /&gt;
&lt;br /&gt;
: When the run has finished, inspect the output written to the screen and to the file ending in &amp;quot;.iqtree&amp;quot;. Note that IQ-TREE 3 reports several model-selection criteria in the output. The line near the top of the ModelFinder section states which criterion was used to choose the final model. Locate the substitution model selected by IQ-TREE 3 and the log likelihood of the best tree.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Question: &#039;&#039;&#039;Which substitution model was selected by IQ-TREE 3, and according to which information criterion was it selected? What was the log likelihood of the best tree? Compare this to the earlier jModelTest2 + PAUP analysis. Is the selected model exactly the same? Is the likelihood exactly the same? Briefly suggest one reason why the two analyses might not give identical results.&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
== Detection of positively selected sites in gp120 ==&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Question 7&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
: There is much more to phylogenetic analyses than merely reconstructing trees. One interesting result of probabilistic methods is that the parameters of a model are estimated as part of the optimization procedure. This means that once such a model has been fitted to the data, it is possible to investigate these estimated parameter values to learn features about the evolutionary history of the sequences under investigation. In the present example we will focus on whether we can detect positively selected sites in our data set, defined as sites where the dN/dS ratio is larger than 1. We do that by using a codon substitution model where the dN/dS ratio is one of its parameters.&lt;br /&gt;
&lt;br /&gt;
: A further strength of the probabilistic approach is that you get a probabilistic measure of how well any model fits the data. This means you can use a stringent approach to determine which model fits the data best. In this framework one uses likelihoods (probabilities of data given model) to determine which model fits the data best. As you saw above, it is for instance possible to compute AIC values and weights (model probabilities) from the likelihood values of fitted models. Since each model essentially corresponds to a hypothesis about the evolutionary history of the data, we can thus use a stringent statistical approach to decide which hypothesis best describes our data.&lt;br /&gt;
&lt;br /&gt;
: In outline, you will now use the following steps to investigate whether there is any evidence for positively selected codons in your data set:&lt;br /&gt;
&lt;br /&gt;
:* Fit model M1, which assumes there are two classes of codons in the sequence: some with dN/dS &amp;lt; 1, some with dN/dS = 1.&lt;br /&gt;
:* Fit model M2, which assumes 3 distinct classes of codons: two with dN/dS ratios as for M1, and one extra class with dN/dS &amp;gt; 1.&lt;br /&gt;
:* Assess the strength of evidence for the two models using AIC-based model probabilities&lt;br /&gt;
:* If M2 is better: identify the positively selected codons&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Edit the parameter file&#039;&#039;&#039;&lt;br /&gt;
 nedit codeml.ctl &amp;amp;&lt;br /&gt;
: The file &amp;quot;codeml.ctl&amp;quot; contains several settings that control how &#039;&#039;&#039;codeml&#039;&#039;&#039; is run. Before continuing, edit the file and save it so that the following settings have these values. Any text appearing after an asterisk (*) on a line is only a comment, and you do not need to change it.&lt;br /&gt;
&lt;br /&gt;
 &#039;&#039;&#039;seqfile =  gp120align.fasta&#039;&#039;&#039;&lt;br /&gt;
: name of alignment file&lt;br /&gt;
&lt;br /&gt;
 &#039;&#039;&#039;treefile =  gp120.nexus.treefile&#039;&#039;&#039;&lt;br /&gt;
: name of tree file produced by IQ-TREE 3&lt;br /&gt;
&lt;br /&gt;
 &#039;&#039;&#039;seqtype = 1&#039;&#039;&#039;&lt;br /&gt;
: tells the program that the data consist of coding DNA&lt;br /&gt;
&lt;br /&gt;
 &#039;&#039;&#039;NSsites = 1 2&#039;&#039;&#039;&lt;br /&gt;
: tells the program to analyze models M1 and M2&lt;br /&gt;
&lt;br /&gt;
 &#039;&#039;&#039;cleandata = 0&#039;&#039;&#039;&lt;br /&gt;
: tells the program to keep positions with gaps&lt;br /&gt;
&lt;br /&gt;
: &#039;&#039;&#039;Important:&#039;&#039;&#039; codeml requires a phylogenetic tree as input. The tree supplies the topology for the codon-model analysis, while codeml estimates the branch lengths and the remaining model parameters itself. In this exercise, we use the tree produced by IQ-TREE 3 (gp120.nexus.treefile) as input.&lt;br /&gt;
&lt;br /&gt;
: The settings entered by us will cause codeml to analyze two hypotheses about dN/dS ratios. M1 says there are two classes of codons with different dN/dS ratios in the sequence: one class with dN/dS &amp;lt; 1 (codons under purifying or negative selection), and one class with dN/dS = 1 (no selection - neutrally evolving sites). M2 says there are 3 distinct dN/dS ratios for different sites in the sequence: one class with dN/dS &amp;lt; 1, one class with dN/dS = 1 (these are the same type of classes as for M1), and one class with dN/dS &amp;gt; 1 (corresponding to sites under positive selection). The value of the dN/dS ratios (for those classes that have dN/dS &amp;lt; 1 or dN/dS &amp;gt; 1), the fraction of sites belonging to each class, and the position of sites belonging to each class, are unknown at first and will be determined during the analysis.&lt;br /&gt;
&lt;br /&gt;
: (Regarding &amp;quot;cleandata&amp;quot; setting: &amp;quot;cleandata = 1&amp;quot; would cause codeml to discard any alignment columns with gaps or ambiguity symbols. For alignments with many sequences this can lead to discarding the majority of sites and I would therefore recommend not using that setting. At the same time you do want to be wary of columns where most sequences have gaps, since inference of selection for these will be very uncertain. It might be better to discard such columns from an alignment before doing the codeml analysis.)&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Start the analysis&#039;&#039;&#039;&lt;br /&gt;
 codeml&lt;br /&gt;
: This will start the codeml program using the settings in the file codeml.ctl. Depending on your computer, this will take some minutes to finish. (You may be able to see how the optimization procedure results in progressively better fits: the likelihood increases, meaning that negative log-likelihood decreases, as the fit improves.)&lt;br /&gt;
&lt;br /&gt;
: &#039;&#039;&#039;Note:&#039;&#039;&#039; On some systems the current version of codeml (4.10.10) may print an error message near the end of the run, even though the relevant results have already been written to &#039;&#039;&#039;selection.results&#039;&#039;&#039;. If this happens, inspect &#039;&#039;&#039;selection.results&#039;&#039;&#039; before assuming that the run failed completely.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Inspect result file:&#039;&#039;&#039;&lt;br /&gt;
: Wait for the run to finish, and then look at the result file:&lt;br /&gt;
 nedit selection.results &amp;amp;&lt;br /&gt;
: This file contains a wealth of information concerning your analysis. You may want to turn off line-wrapping to more clearly see the structure of the file (there will be some long lines that will otherwise wrap around). The top part of the file gives an overview of your sequences, codon usage and nucleotide frequencies. You can ignore this information for now, and move on to the interesting part, namely the model likelihoods and parameter values.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Find likelihood, and number of free parameters for model M1&#039;&#039;&#039;&lt;br /&gt;
 Search ==&amp;gt; Find... ==&amp;gt; enter &amp;quot;Model 1&amp;quot; and click Find&lt;br /&gt;
: You are now in the region of the result file where the model likelihoods and parameter estimates are noted. Now locate a line that looks a bit like the one shown below:&lt;br /&gt;
 lnL(ntime: 72  np: 74):  -4242.470345     +0.000000&lt;br /&gt;
: Identify the number of &amp;quot;free parameters&amp;quot;, K, used in model M1: this is indicated by &amp;quot;np&amp;quot;, and is 74 in the example shown above (most of these parameters are branch lengths in the tree; specifically, the number of branch length parameters is indicated by &amp;quot;ntime&amp;quot;, and is 72 in this example). Also note the log-likelihood of the fitted model. This is the number right after the parenthesis, and is -4242.470345 in the example here.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Question: &#039;&#039;&#039;What are the values of K and lnL for model M1?&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Question 8&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Find dN/dS ratios and codon class proportions for model M1&#039;&#039;&#039;&lt;br /&gt;
: Scroll down a few lines until you get to a small table similar to this:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
      dN/dS for site classes (K=2)&lt;br /&gt;
      p:   0.75111  0.24889&lt;br /&gt;
      w:   0.06583  1.00000&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
: This gives a summary of the dN/dS ratios that were found in the data set. The line starting &amp;quot;w:&amp;quot; lists the two dN/dS ratios that were found (in this case 0.06583 and 1.00000 - the last one was pre-specified by us as part of the model and was therefore not a free parameter). In this context &amp;quot;w&amp;quot; means &amp;quot;omega&amp;quot;, which is the symbol typically used to represent the dN/dS rate ratio. The line starting &amp;quot;p:&amp;quot; gives the proportion of codon sites belonging to each of the dN/dS ratio classes (in the example above approximately 75% belong to the first class, while 25% of all sites belong to the class having dN/dS = 1.00000).&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Question: &#039;&#039;&#039;What are the dN/dS value (w) and proportion (p) of sites for both classes? Report the following values: p(class1), w(class1), p(class2), w(class2)&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Question 9&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Find likelihood, and K for model M2&#039;&#039;&#039;&lt;br /&gt;
: Scroll past the M1 output until you get to the results for model M2.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Question: &#039;&#039;&#039;What are the values of K and lnL for model M2?&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Question 10&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Find dN/dS ratios and codon class proportions for model M2&#039;&#039;&#039;&lt;br /&gt;
: Now scroll down a few lines until you get to a small table similar to the one you examined for M1 before. For this model there are 3 separate classes of codons.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Question: &#039;&#039;&#039;What are the dN/dS value (w) and proportion (p) of sites for all three classes? Report these values: p(class1), w(class1), p(class2), w(class2), p(class3), w(class3)&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Question 11&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Assess strength of evidence for models M1 and M2&#039;&#039;&#039;&lt;br /&gt;
: M2 will always have a better (higher) log-likelihood than model M1 because M2 has more free parameters, and M1 is nested within M2. You should now use the recipe given above to compute AIC values and Akaike weights for M1 and M2.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Question: &#039;&#039;&#039;Report: AIC, ΔAIC, weight (model probability) for M1 and M2&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Question 12: &#039;&#039;&#039;Is M2 better than M1? Based on this comparison, is there evidence that some codon sites in gp120 have dN/dS &amp;gt; 1?&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Question 13&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Examine list of positively selected sites&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
: If your M2 is clearly better than M1, then you have evidence for the existence of positively selected sites in the gp120 gene. Now scroll down to the end of the result file and locate the &amp;quot;Bayes Empirical Bayes (BEB) analysis&amp;quot; table. Note: use the BEB table, not the Naive Empirical Bayes (NEB) table. (It is not important what the distinction is in this context, but if you are interested: NEB ignores the fact that there is uncertainty about maximum likelihood estimates, especially for smaller data sets - for instance w for some codon is perhaps not exactly 3.046, but could lie in a region around that value - whereas [https://pubmed.ncbi.nlm.nih.gov/15689528/ BEB accounts for that uncertainty].)&lt;br /&gt;
&lt;br /&gt;
: This table lists codon positions, the posterior probability that each site belongs to the positively selected class (that is, the class with dN/dS &amp;gt; 1), and the estimated mean value of w for the site.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Question: &#039;&#039;&#039;List all sites having more than 95% probability of belonging to the positively selected class.&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Question 14&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
: Consider your results from Questions 10–13. In model M2, only a fraction of sites belong to the positively selected class, while many sites belong to classes with dN/dS ≤ 1.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Question: &#039;&#039;&#039;What does this suggest about the overall pattern of selection acting on gp120? Explain briefly why one might expect a mixture of negatively selected, neutral, and positively selected sites in this gene.&lt;/div&gt;</summary>
		<author><name>Gorm</name></author>
	</entry>
	<entry>
		<id>https://teaching.healthtech.dtu.dk/22115/index.php?title=Model_selection&amp;diff=284</id>
		<title>Model selection</title>
		<link rel="alternate" type="text/html" href="https://teaching.healthtech.dtu.dk/22115/index.php?title=Model_selection&amp;diff=284"/>
		<updated>2026-04-15T15:09:47Z</updated>

		<summary type="html">&lt;p&gt;Gorm: /* Detection of positively selected sites in gp120 */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;This exercise is part of the course  [[22115_-_Computational_Molecular_Evolution|Computational Molecular Evolution (22115)]].&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Overview ==&lt;br /&gt;
&lt;br /&gt;
: In this exercise you are going to investigate features of HIV-1 evolution. You will do this by analyzing a large set of env-genes from HIV-1, subtype B. specifically, the DNA sequences analyzed here correspond to a region surrounding the hypervariable V3 region of the gp120 protein.&lt;br /&gt;
&lt;br /&gt;
: Like other retroviruses, particles of HIV are made up of 2 copies of a single-stranded RNA genome packaged inside a protein core, or capsid. The core particle also contains viral proteins that are essential for the early steps of the virus life cycle, such as reverse transcription and integration. A lipid envelope, derived from the infected cell, surrounds the core particle. Embedded in this envelope are the surface glycoproteins of HIV: gp120 and gp41. The gp120 protein is crucial for binding of the virus particle to target cells, while gp41 is important for the subsequent fusion event. It is the specific affinity of gp120 for the CD4 protein that targets HIV to those cells of the immune system that express CD4 on their surface (e.g., T-helper lymphocytes, monocytes, and macrophages).&lt;br /&gt;
&lt;br /&gt;
: The role gp120 plays in infection and the fact that it is situated on the surface of the HIV particle, means it is an obvious target for the immune response. That means that there may be a considerable selective pressure on gp120 for creating immune-escape mutants, where amino acids in the gp120 epitopes have been substituted. In this exercise you will construct a maximum likelihood tree that we will subsequently use to investigate whether you can detect such a selective pressure on parts of gp120, again using maximum likelihood methods.&lt;br /&gt;
&lt;br /&gt;
: One major goal with the exercise is to introduce you to statistically based methods for assessing the strength of evidence for a set of alternative hypotheses about some biological system of interest. The model selection method we will use is AIC (Akaike Information Criterion), based on which you will compute model probabilities. A second goal is to make you aware that phylogenetic analysis is not only about constructing trees, but that it is also a useful framework for analyzing biological questions more generally.&lt;br /&gt;
&lt;br /&gt;
: Specifically, you will&lt;br /&gt;
&lt;br /&gt;
:# perform a multiple alignment of gp120 DNA sequences taking protein-level information into account (using revtrans).&lt;br /&gt;
:# select a suitable nucleotide substitution model (using jmodeltest2)&lt;br /&gt;
:# construct a phylogenetic tree (using PAUP).&lt;br /&gt;
:# try to detect positively selected sites in gp120 (using PAML).&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
== Recipe for computing AIC values and model probabilities ==&lt;br /&gt;
&lt;br /&gt;
: Later in today&#039;s exercise you will be asked to compute AIC values and model probabilities. Return to this section and follow the instructions when you need to do so.&lt;br /&gt;
&lt;br /&gt;
:# Fit a set of models to your data, note the maximized log likelihoods (lnL) and the number of free parameters (K) for each model in the investigated set. The models you fit should represent a plausible and comprehensive set of hypotheses about your data.&lt;br /&gt;
:# Compute AIC for each of the models: &#039;&#039;&#039;AIC = -2 x lnL + 2K&#039;&#039;&#039;.  &amp;lt;br&amp;gt;For example: a model with lnL = -2010 and K = 5 will have AIC = -2 x -2010 + 2 x 5 = 4030.&lt;br /&gt;
:# Identify the model with the smallest AIC (this is the best model in the set). We will call the AIC for this model &#039;&#039;&#039;&amp;quot;AICmin&amp;quot;&#039;&#039;&#039;.&lt;br /&gt;
:# Compute the &amp;quot;ΔAIC&amp;quot; values for each model: &#039;&#039;&#039;ΔAIC = AIC - AICmin&#039;&#039;&#039; &amp;lt;br&amp;gt;For each model subtract the minimum AIC value. The best model will have a ΔAIC of zero. The rest of the models will have positive ΔAICs.&lt;br /&gt;
:# For each model compute the following quantity: &#039;&#039;&#039;numerator = exp(-0.5 x ΔAIC)&#039;&#039;&#039; &amp;lt;br&amp;gt;For example, a model with ΔAIC=4.2 will have numerator = exp(-0.5 x 4.2) = exp(-2.1) = 0.1225. Also compute the &#039;&#039;&#039;sum&#039;&#039;&#039; of the numerator values for all models.&lt;br /&gt;
:# Finally, the model probabilities (also called model weights) for each model are found as:&amp;lt;br&amp;gt; &#039;&#039;&#039;P(model) = numerator / sum&#039;&#039;&#039; &amp;lt;br&amp;gt;For example, if sum = 3.75 and a model has numerator = 1.3, then it has P(model) = 1.3 / 3.75 = 0.35&lt;br /&gt;
&lt;br /&gt;
: You may want to keep track of the computations by constructing a table along the following lines:&lt;br /&gt;
[[File:Molevol-Downloads-aictable.png|700px]]&lt;br /&gt;
&lt;br /&gt;
: Note that model probabilities can also be computed using Bayesian methods. One advantage of Bayesian methods over AIC is that instead of relying on a point estimate, uncertainty about parameter values is accounted for by integrating over all possible values (typically using MCMC).&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
== Getting started ==&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Create working directory, copy files&#039;&#039;&#039;&lt;br /&gt;
: In the command below: Instead of /path/to/molevol enter the path to the directory where you have placed your course files (for instance cd /Users/bob/Documents/molevol, or cd /home/student/molevol).&lt;br /&gt;
 cd /path/to/molevol&lt;br /&gt;
 mkdir modelselect&lt;br /&gt;
 cd modelselect&lt;br /&gt;
 cp ../data/gp120.fasta ./gp120.fasta&lt;br /&gt;
 cp ../data/codeml.ctl ./codeml.ctl&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Have a look at the DNA data file:&#039;&#039;&#039;&lt;br /&gt;
 nedit gp120.fasta &amp;amp;&lt;br /&gt;
: The file contains several DNA sequences from HIV-1, subtype B. The sequences are approximately 500 bp long, and correspond to a region surrounding the hypervariable V3 region in the gene encoding gp120. Close the nedit window when you&#039;ve had a look.&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
== Protein-guided alignment of coding DNA ==&lt;br /&gt;
&lt;br /&gt;
: There are three reasons why it is preferable to align coding DNA at the amino acid level rather than directly as nucleotides. First, the DNA alphabet has only four letters compared to 20 amino acids, which means that even unrelated DNA sequences will share roughly 25% identity by chance alone, making it much harder to distinguish true homology from noise. Second, protein alignments benefit from empirical substitution matrices such as BLOSUM-62, which capture the fact that amino acid replacements tend to be conservative. Equivalent matrices for DNA are far less informative because nucleotide substitution patterns vary strongly between genes and organisms. Third, synonymous substitutions accumulate faster than non-synonymous ones, so DNA sequences diverge more rapidly than the proteins they encode. Taken together, these factors mean that the phylogenetic signal in a DNA alignment erodes much faster than in the corresponding protein alignment.&lt;br /&gt;
&lt;br /&gt;
: However, while protein-level alignment gives us the most reliable homology signal, it discards exactly the information we need for studying selection: the synonymous substitutions. Estimating the ratio of non-synonymous to synonymous substitution rates (dN/dS) requires a codon-level alignment where analogous codon positions are properly lined up. We would therefore like to construct a multiple alignment at the DNA level, but using information at the protein level, and the RevTrans server does exactly that.&lt;br /&gt;
&lt;br /&gt;
: &#039;&#039;&#039;RevTrans&#039;&#039;&#039; takes as input an unaligned set of DNA sequences, automatically translates them to the equivalent amino acid sequences, constructs a multiple alignment of the protein sequences, and finally uses the protein alignment as a template for constructing a multiple DNA alignment. Because each amino acid maps back to its original codon, gaps are always inserted in groups of three and codon boundaries are preserved throughout the alignment. This is essential for the dN/dS analyses we will carry out in this exercise: if codon positions are misaligned, synonymous and non-synonymous changes cannot be correctly distinguished.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Construct RevTrans alignment&#039;&#039;&#039;&lt;br /&gt;
:* Open RevTrans server page: https://services.healthtech.dtu.dk/services/RevTrans-2.0/&lt;br /&gt;
:* On the RevTrans page: Choose the file gp120.fasta as input (or copy and paste the sequence into the sequence window)&lt;br /&gt;
:* Click the &amp;quot;Submit query&amp;quot; button&lt;br /&gt;
:* When the alignment is done you may have to click link named &amp;quot;here&amp;quot; to go to results page&lt;br /&gt;
:* Download DNA alignment, by right-clicking the link for &amp;quot;Download alignment in FASTA format&amp;quot;, and choosing &amp;quot;Save link as...&amp;quot; (save file under the name gp120align.fasta and make sure to save the file in the directory modelselect).&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Convert alignment to NEXUS format&#039;&#039;&#039;&lt;br /&gt;
: Convert the fasta file to NEXUS format and save file in the modelselect directory under the name gp120.nexus&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
== Model selection using AIC and jModelTest2 ==&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Question 1&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
: As part of the present analysis we are going to build a phylogenetic tree based on the DNA alignment constructed above. We will construct the tree using maximum likelihood, but to do that we first have to decide which substitution model we want to use. Specifically, we are interested in selecting a model that achieves a good balance between fit to the data and number of parameters. We will investigate this issue by fitting a set of 56 different models to our data and then comparing them using AIC.&lt;br /&gt;
&lt;br /&gt;
: In the first part of the exercise, however, you will only compare three closely related models by hand. The purpose of this restricted comparison is not primarily to identify the globally best model, but to understand how AIC, ΔAIC, and Akaike weights are computed and interpreted. Because only three models are included in this manual comparison, the weights you compute will describe relative support &#039;&#039;&#039;within this restricted set only&#039;&#039;&#039;.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Start jmodeltest2&#039;&#039;&#039;&lt;br /&gt;
 jmodeltest&lt;br /&gt;
&#039;&#039;&#039;Load data set&#039;&#039;&#039;&lt;br /&gt;
:* File -&amp;gt; Load DNA alignment -&amp;gt; File Format -&amp;gt; Select &amp;quot;All files&amp;quot;&lt;br /&gt;
:* Navigate to gp120.nexus and load it&lt;br /&gt;
&#039;&#039;&#039;Fit 56 models&#039;&#039;&#039;&lt;br /&gt;
:* Analysis -&amp;gt; Compute likelihood scores&lt;br /&gt;
:* Select &amp;quot;7&amp;quot; under &amp;quot;Number of substitution schemes&amp;quot;&lt;br /&gt;
:* Select &amp;quot;Fixed BIONJ-JC&amp;quot; under &amp;quot;Base tree for likelihood calculations&amp;quot;&lt;br /&gt;
:* Click &amp;quot;Compute likelihoods&amp;quot;&lt;br /&gt;
&lt;br /&gt;
: This causes jmodeltest2 to perform the following actions: first a neighbor joining tree is constructed using the Jukes and Cantor model. Then the tree is fixed and used as the basis for fitting a set of 56 different models to the data. For each model, the estimated model parameters and the negative log-likelihood are recorded. In addition to varying sets of substitution rate parameters (JC, K2P, ...), some of these models also include extra parameters that take into account the presence of different rates between sites. This is done in two ways: (1) by fitting a gamma distribution of rates (&amp;quot;+G&amp;quot;), and (2) by allowing for a proportion of constant (&amp;quot;invariable&amp;quot;) sites (&amp;quot;+I&amp;quot;).&lt;br /&gt;
&lt;br /&gt;
: Wait until jmodeltest2 is done fitting all 56 models (this will take a little while depending on your computer).&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Inspect result&#039;&#039;&#039;&lt;br /&gt;
:* Results -&amp;gt; Show results table&lt;br /&gt;
: For each model this table lists the negative log-likelihood (&amp;quot;-lnL&amp;quot;), the number of parameters (&amp;quot;p&amp;quot; - note this includes branch lengths), and estimates of all model parameters (excluding branch lengths).&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Manually compute Akaike weights for three substitution models&#039;&#039;&#039;&lt;br /&gt;
: Use AIC-based model comparison to investigate which of the following three substitution models are best supported as descriptions of how the sequences have evolved:&lt;br /&gt;
:* Jukes and Cantor with fraction of invariant sites (JC+I)&lt;br /&gt;
:* Jukes and Cantor with gamma-distributed rates over sites (JC+G)&lt;br /&gt;
:* Jukes and Cantor with invariant sites and gamma-distributed rates (JC+I+G)&lt;br /&gt;
: Before you can do the computation you need to know the log likelihood and the number of parameters for each model. Locate these values in the table for the JC+I, JC+G, and JC+I+G models, and write them down. Close the window with the result table when you are done.&lt;br /&gt;
&lt;br /&gt;
: Make sure to get the signs right: the values reported in the table are -lnL values, so you will need to reverse the sign to get the lnL (the lnL values you write down should be negative).&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Question: &#039;&#039;&#039;Use the recipe above to compute AIC, ΔAIC, and Akaike weight for the three models. Report the results in a table similar to the one shown above, and verify that the three weights sum to 1 (up to rounding error).&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Question 2&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
: Based on the Akaike weights, which of the three models has the strongest support? Is the support strongly concentrated on one model, or is there still substantial support for one or both of the others?&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Question 3&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
: What you just did manually for JC+I, JC+G and JC+I+G, jmodeltest2 can do automatically for the full set of 56 fitted models. Specifically, it uses the list of negative log likelihoods and parameter counts in the table to compute AIC values and Akaike weights for the full candidate set:&lt;br /&gt;
:* Analysis -&amp;gt; Do AIC calculations -&amp;gt;&lt;br /&gt;
:* Select &amp;quot;Write PAUP* block&amp;quot;&lt;br /&gt;
:* click &amp;quot;Do AIC calculations&amp;quot;&lt;br /&gt;
:* Results -&amp;gt; Show results table&lt;br /&gt;
:* Select &amp;quot;AIC&amp;quot; tab&lt;br /&gt;
:* &#039;&#039;&#039;IMPORTANT:&#039;&#039;&#039; SHIFT+click on the header of the &amp;quot;weight&amp;quot; column. This sorts the rows according to model weight, in descending order.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Question: &#039;&#039;&#039;What model was selected by modeltest based on the AIC values? (This will be the model with the highest weight - and lowest AIC - and will be in the first row of the results table after SHIFT-clicking the &amp;quot;weight&amp;quot; header).&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Question 4&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
: In the same AIC results table, now click (not SHIFT+click) the header of the &amp;quot;-lnL&amp;quot; column. In this table, a normal click sorts in ascending order, whereas SHIFT+click sorts in descending order. The model at the top will therefore be the one with the smallest value of &amp;quot;-lnL&amp;quot; (equivalently: the highest likelihood). Compare this model to the one with the highest Akaike weight.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Question: &#039;&#039;&#039;Is the model with the highest likelihood also the model with the highest Akaike weight? If not, explain briefly why these two rankings can differ.&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
== Phylogenetic tree inference ==&lt;br /&gt;
&lt;br /&gt;
=== Tree inference using the model selected by jModelTest2 (PAUP) ===&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Question 5&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
: Above, you used jModelTest2 to select a substitution model based on AIC. You will now use that model to construct a maximum likelihood tree in &#039;&#039;&#039;PAUP&#039;&#039;&#039;. The purpose of doing this in PAUP is mainly pedagogical: it makes the individual steps of model-based phylogenetic inference more explicit. In practice, one would often use a program such as IQ-TREE 3 (see next section).&lt;br /&gt;
&lt;br /&gt;
: Close the results table. In the main window you should now scroll up to the lines giving PAUP commands that implement the selected model. The command is enclosed between &amp;quot;BEGIN PAUP&amp;quot; and &amp;quot;END;&amp;quot; and should look something like this:&lt;br /&gt;
 Lset Base=(0.4064, [...]&lt;br /&gt;
: You will need to copy this command to a PAUP session in the next step.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Start PAUP&#039;&#039;&#039;&lt;br /&gt;
 paup&lt;br /&gt;
: Above, you used jModelTest2 to select the most suitable substitution model for the present data set. You will now use this model to construct a maximum likelihood tree. You will use PAUP for this purpose. (Note: it is possible to create a maximum likelihood tree, or even a model-averaged tree, directly from jModelTest2, but we will instead do it in PAUP in order to more clearly see each step that is taken.)&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Load alignment:&#039;&#039;&#039;&lt;br /&gt;
 execute gp120.nexus&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Set tree-building criterion to maximum likelihood&#039;&#039;&#039;&lt;br /&gt;
 set criterion=likelihood&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Set model parameters to winning estimates&#039;&#039;&#039;&lt;br /&gt;
: Above, you located a set of lines in the jModelTest2 output giving a PAUP command that sets the model parameters to the estimates found for the winning model. Copy and paste this lset command (without the BEGIN and END parts) into the window where PAUP is running.&lt;br /&gt;
&lt;br /&gt;
 PASTE LSET COMMAND FROM MODELTEST RUN HERE&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Find best tree using selected model&#039;&#039;&#039;&lt;br /&gt;
: Still in the PAUP window, enter the following command:&lt;br /&gt;
 hsearch swap=tbr start=nj&lt;br /&gt;
: This command causes PAUP to perform a heuristic search for the best maximum likelihood tree. Once an initial tree has been constructed, the heuristic search proceeds by rearrangements of the &amp;quot;tree bisection and reconnection&amp;quot; type (TBR). We are using the model selected by modeltest, and the parameter estimates found by modeltest for that model. You could also choose to estimate all the model parameters as part of this step (i.e., at the same time as finding the best tree), but fixing them improves speed tremendously. Finding the best tree should take at most a few minutes.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Save best tree to file&#039;&#039;&#039;&lt;br /&gt;
 savetrees format=newick brlens=yes file=gp120tree_paup.phy from=1 to=1&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Quit program&#039;&#039;&#039;&lt;br /&gt;
 quit&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Have a look at the tree:&#039;&#039;&#039;&lt;br /&gt;
: You have now produced an unrooted tree of the HIV sequences and saved it in the file gp120tree_paup.phy. Note that in this exercise we will not be interested in the tree as such - our focus is instead on finding positive selection at a subset of codon positions, and the tree is just something we need in order to fit the different codon models to the data. If you want to see the tree, you can do so with the following command:&lt;br /&gt;
 figtree gp120tree_paup.phy &amp;amp;&lt;br /&gt;
: There is no meaningful root placed on this tree, so you may want to choose the unrooted view (the third icon in the Layout section of the FigTree window). Close the FigTree window when you have had a look.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Question: &#039;&#039;&#039;Which substitution model did you use for the PAUP tree search, and what was the negative log likelihood of the best tree found under this model?&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
=== One-step model selection and tree inference using IQ-TREE 3 ===&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Question 6&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
: In real-life phylogenetic analysis one would often use a modern program such as &#039;&#039;&#039;IQ-TREE 3&#039;&#039;&#039;, which can carry out model selection and maximum likelihood tree reconstruction in a single analysis. You will now repeat the analysis using this more integrated approach.&lt;br /&gt;
&lt;br /&gt;
: Run IQ-TREE 3 on the alignment using the following command:&lt;br /&gt;
 iqtree3 -s gp120.nexus -m MFP&lt;br /&gt;
&lt;br /&gt;
: Here, &amp;quot;-m MFP&amp;quot; tells IQ-TREE 3 to perform automatic model selection using &amp;quot;ModelFinder Plus&amp;quot; before reconstructing the tree.&lt;br /&gt;
&lt;br /&gt;
: When the run has finished, inspect the output written to the screen and to the file ending in &amp;quot;.iqtree&amp;quot;. Note that IQ-TREE 3 reports several model-selection criteria in the output. The line near the top of the ModelFinder section states which criterion was used to choose the final model. Locate the substitution model selected by IQ-TREE 3 and the log likelihood of the best tree.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Question: &#039;&#039;&#039;Which substitution model was selected by IQ-TREE 3, and according to which information criterion was it selected? What was the log likelihood of the best tree? Compare this to the earlier jModelTest2 + PAUP analysis. Is the selected model exactly the same? Is the likelihood exactly the same? Briefly suggest one reason why the two analyses might not give identical results.&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
== Detection of positively selected sites in gp120 ==&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Question 7&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
: There is much more to phylogenetic analyses than merely reconstructing trees. One interesting result of probabilistic methods is that the parameters of a model are estimated as part of the optimization procedure. This means that once such a model has been fitted to the data, it is possible to investigate these estimated parameter values to learn features about the evolutionary history of the sequences under investigation. In the present example we will focus on whether we can detect positively selected sites in our data set, defined as sites where the dN/dS ratio is larger than 1. We do that by using a codon substitution model where the dN/dS ratio is one of its parameters.&lt;br /&gt;
&lt;br /&gt;
: A further strength of the probabilistic approach is that you get a probabilistic measure of how well any model fits the data. This means you can use a stringent approach to determine which model fits the data best. In this framework one uses likelihoods (probabilities of data given model) to determine which model fits the data best. As you saw above, it is for instance possible to compute AIC values and weights (model probabilities) from the likelihood values of fitted models. Since each model essentially corresponds to a hypothesis about the evolutionary history of the data, we can thus use a stringent statistical approach to decide which hypothesis best describes our data.&lt;br /&gt;
&lt;br /&gt;
: In outline, you will now use the following steps to investigate whether there is any evidence for positively selected codons in your data set:&lt;br /&gt;
&lt;br /&gt;
:* Fit model M1, which assumes there are two classes of codons in the sequence: some with dN/dS &amp;lt; 1, some with dN/dS = 1.&lt;br /&gt;
:* Fit model M2, which assumes 3 distinct classes of codons: two with dN/dS ratios as for M1, and one extra class with dN/dS &amp;gt; 1.&lt;br /&gt;
:* Assess the strength of evidence for the two models using AIC-based model probabilities&lt;br /&gt;
:* If M2 is better: identify the positively selected codons&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Edit the parameter file&#039;&#039;&#039;&lt;br /&gt;
 nedit codeml.ctl &amp;amp;&lt;br /&gt;
: The file &amp;quot;codeml.ctl&amp;quot; contains several settings that control how &#039;&#039;&#039;codeml&#039;&#039;&#039; is run. Before continuing, edit the file and save it so that the following settings have these values. Any text appearing after an asterisk (*) on a line is only a comment, and you do not need to change it.&lt;br /&gt;
&lt;br /&gt;
 &#039;&#039;&#039;seqfile =  gp120align.fasta&#039;&#039;&#039;&lt;br /&gt;
: name of alignment file&lt;br /&gt;
&lt;br /&gt;
 &#039;&#039;&#039;treefile =  gp120.nexus.treefile&#039;&#039;&#039;&lt;br /&gt;
: name of tree file produced by IQ-TREE 3&lt;br /&gt;
&lt;br /&gt;
 &#039;&#039;&#039;seqtype = 1&#039;&#039;&#039;&lt;br /&gt;
: tells the program that the data consist of coding DNA&lt;br /&gt;
&lt;br /&gt;
 &#039;&#039;&#039;NSsites = 1 2&#039;&#039;&#039;&lt;br /&gt;
: tells the program to analyze models M1 and M2&lt;br /&gt;
&lt;br /&gt;
 &#039;&#039;&#039;cleandata = 0&#039;&#039;&#039;&lt;br /&gt;
: tells the program to keep positions with gaps&lt;br /&gt;
&lt;br /&gt;
: &#039;&#039;&#039;Important:&#039;&#039;&#039; codeml requires a phylogenetic tree as input. The tree supplies the topology for the codon-model analysis, while codeml estimates the branch lengths and the remaining model parameters itself. In this exercise, we use the tree produced by IQ-TREE 3 (gp120.nexus.treefile) as input.&lt;br /&gt;
&lt;br /&gt;
: The settings entered by us will cause codeml to analyze two hypotheses about dN/dS ratios. M1 says there are two classes of codons with different dN/dS ratios in the sequence: one class with dN/dS &amp;lt; 1 (codons under purifying or negative selection), and one class with dN/dS = 1 (no selection - neutrally evolving sites). M2 says there are 3 distinct dN/dS ratios for different sites in the sequence: one class with dN/dS &amp;lt; 1, one class with dN/dS = 1 (these are the same type of classes as for M1), and one class with dN/dS &amp;gt; 1 (corresponding to sites under positive selection). The value of the dN/dS ratios (for those classes that have dN/dS &amp;lt; 1 or dN/dS &amp;gt; 1), the fraction of sites belonging to each class, and the position of sites belonging to each class, are unknown at first and will be determined during the analysis.&lt;br /&gt;
&lt;br /&gt;
: (Regarding &amp;quot;cleandata&amp;quot; setting: &amp;quot;cleandata = 1&amp;quot; would cause codeml to discard any alignment columns with gaps or ambiguity symbols. For alignments with many sequences this can lead to discarding the majority of sites and I would therefore recommend not using that setting. At the same time you do want to be wary of columns where most sequences have gaps, since inference of selection for these will be very uncertain. It might be better to discard such columns from an alignment before doing the codeml analysis.)&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Start the analysis&#039;&#039;&#039;&lt;br /&gt;
 codeml&lt;br /&gt;
: This will start the codeml program using the settings in the file codeml.ctl. Depending on your computer, this will take some minutes to finish. (You may be able to see how the optimization procedure results in progressively better fits: the likelihood increases, meaning that negative log-likelihood decreases, as the fit improves.)&lt;br /&gt;
&lt;br /&gt;
: &#039;&#039;&#039;Note:&#039;&#039;&#039; On some systems the current version of codeml (4.10.10) may print an error message near the end of the run, even though the relevant results have already been written to &#039;&#039;&#039;selection.results&#039;&#039;&#039;. If this happens, inspect &#039;&#039;&#039;selection.results&#039;&#039;&#039; before assuming that the run failed completely.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Inspect result file:&#039;&#039;&#039;&lt;br /&gt;
: Wait for the run to finish, and then look at the result file:&lt;br /&gt;
 nedit selection.results &amp;amp;&lt;br /&gt;
: This file contains a wealth of information concerning your analysis. You may want to turn off line-wrapping to more clearly see the structure of the file (there will be some long lines that will otherwise wrap around). The top part of the file gives an overview of your sequences, codon usage and nucleotide frequencies. You can ignore this information for now, and move on to the interesting part, namely the model likelihoods and parameter values.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Find likelihood, and number of free parameters for model M1&#039;&#039;&#039;&lt;br /&gt;
 Search ==&amp;gt; Find... ==&amp;gt; enter &amp;quot;Model 1&amp;quot; and click Find&lt;br /&gt;
: You are now in the region of the result file where the model likelihoods and parameter estimates are noted. Now locate a line that looks a bit like the one shown below:&lt;br /&gt;
 lnL(ntime: 72  np: 74):  -4242.470345     +0.000000&lt;br /&gt;
: Identify the number of &amp;quot;free parameters&amp;quot;, K, used in model M1: this is indicated by &amp;quot;np&amp;quot;, and is 74 in the example shown above (most of these parameters are branch lengths in the tree; specifically, the number of branch length parameters is indicated by &amp;quot;ntime&amp;quot;, and is 72 in this example). Also note the log-likelihood of the fitted model. This is the number right after the parenthesis, and is -4242.470345 in the example here.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Question: &#039;&#039;&#039;What are the values of K and lnL for model M1?&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Question 8&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Find dN/dS ratios and codon class proportions for model M1&#039;&#039;&#039;&lt;br /&gt;
: Scroll down a few lines until you get to a small table similar to this:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
      dN/dS for site classes (K=2)&lt;br /&gt;
      p:   0.75111  0.24889&lt;br /&gt;
      w:   0.06583  1.00000&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
: This gives a summary of the dN/dS ratios that were found in the data set. The line starting &amp;quot;w:&amp;quot; lists the two dN/dS ratios that were found (in this case 0.06583 and 1.00000 - the last one was pre-specified by us as part of the model and was therefore not a free parameter). In this context &amp;quot;w&amp;quot; means &amp;quot;omega&amp;quot;, which is the symbol typically used to represent the dN/dS rate ratio. The line starting &amp;quot;p:&amp;quot; gives the proportion of codon sites belonging to each of the dN/dS ratio classes (in the example above approximately 75% belong to the first class, while 25% of all sites belong to the class having dN/dS = 1.00000).&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Question: &#039;&#039;&#039;What are the dN/dS value (w) and proportion (p) of sites for both classes? Report the following values: p(class1), w(class1), p(class2), w(class2)&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Question 9&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Find likelihood, and K for model M2&#039;&#039;&#039;&lt;br /&gt;
: Scroll past the M1 output until you get to the results for model M2.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Question: &#039;&#039;&#039;What are the values of K and lnL for model M2?&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Question 10&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Find dN/dS ratios and codon class proportions for model M2&#039;&#039;&#039;&lt;br /&gt;
: Now scroll down a few lines until you get to a small table similar to the one you examined for M1 before. For this model there are 3 separate classes of codons.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Question: &#039;&#039;&#039;What are the dN/dS value (w) and proportion (p) of sites for all three classes? Report these values: p(class1), w(class1), p(class2), w(class2), p(class3), w(class3)&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Question 11&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Assess strength of evidence for models M1 and M2&#039;&#039;&#039;&lt;br /&gt;
: M2 will always have a better (higher) log-likelihood than model M1 because M2 has more free parameters, and M1 is nested within M2. You should now use the recipe given above to compute AIC values and model probabilities for M1 and M2.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Question: &#039;&#039;&#039;Report: AIC, ΔAIC, weight (model probability) for M1 and M2&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Question 12: &#039;&#039;&#039;Is M2 better than M1? Based on this comparison, is there evidence that some codon sites in gp120 have dN/dS &amp;gt; 1?&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Question 13&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Examine list of positively selected sites&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
: If your M2 is clearly better than M1, then you have evidence for the existence of positively selected sites in the gp120 gene. Now scroll down to the end of the result file and locate the &amp;quot;Bayes Empirical Bayes (BEB) analysis&amp;quot; table. Note: use the BEB table, not the Naive Empirical Bayes (NEB) table. (It is not important what the distinction is in this context, but if you are interested: NEB ignores the fact that there is uncertainty about maximum likelihood estimates, especially for smaller data sets - for instance w for some codon is perhaps not exactly 3.046, but could lie in a region around that value - whereas [https://pubmed.ncbi.nlm.nih.gov/15689528/ BEB accounts for that uncertainty].)&lt;br /&gt;
&lt;br /&gt;
: This table lists codon positions, the posterior probability that each site belongs to the positively selected class (that is, the class with dN/dS &amp;gt; 1), and the estimated mean value of w for the site.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Question: &#039;&#039;&#039;List all sites having more than 95% probability of belonging to the positively selected class.&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Question 14&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
: Consider your results from Questions 10–13. In model M2, only a fraction of sites belong to the positively selected class, while many sites belong to classes with dN/dS ≤ 1.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Question: &#039;&#039;&#039;What does this suggest about the overall pattern of selection acting on gp120? Explain briefly why one might expect a mixture of negatively selected, neutral, and positively selected sites in this gene.&lt;/div&gt;</summary>
		<author><name>Gorm</name></author>
	</entry>
	<entry>
		<id>https://teaching.healthtech.dtu.dk/22115/index.php?title=Model_selection&amp;diff=283</id>
		<title>Model selection</title>
		<link rel="alternate" type="text/html" href="https://teaching.healthtech.dtu.dk/22115/index.php?title=Model_selection&amp;diff=283"/>
		<updated>2026-04-15T15:08:58Z</updated>

		<summary type="html">&lt;p&gt;Gorm: /* Detection of positively selected sites in gp120 */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;This exercise is part of the course  [[22115_-_Computational_Molecular_Evolution|Computational Molecular Evolution (22115)]].&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Overview ==&lt;br /&gt;
&lt;br /&gt;
: In this exercise you are going to investigate features of HIV-1 evolution. You will do this by analyzing a large set of env-genes from HIV-1, subtype B. specifically, the DNA sequences analyzed here correspond to a region surrounding the hypervariable V3 region of the gp120 protein.&lt;br /&gt;
&lt;br /&gt;
: Like other retroviruses, particles of HIV are made up of 2 copies of a single-stranded RNA genome packaged inside a protein core, or capsid. The core particle also contains viral proteins that are essential for the early steps of the virus life cycle, such as reverse transcription and integration. A lipid envelope, derived from the infected cell, surrounds the core particle. Embedded in this envelope are the surface glycoproteins of HIV: gp120 and gp41. The gp120 protein is crucial for binding of the virus particle to target cells, while gp41 is important for the subsequent fusion event. It is the specific affinity of gp120 for the CD4 protein that targets HIV to those cells of the immune system that express CD4 on their surface (e.g., T-helper lymphocytes, monocytes, and macrophages).&lt;br /&gt;
&lt;br /&gt;
: The role gp120 plays in infection and the fact that it is situated on the surface of the HIV particle, means it is an obvious target for the immune response. That means that there may be a considerable selective pressure on gp120 for creating immune-escape mutants, where amino acids in the gp120 epitopes have been substituted. In this exercise you will construct a maximum likelihood tree that we will subsequently use to investigate whether you can detect such a selective pressure on parts of gp120, again using maximum likelihood methods.&lt;br /&gt;
&lt;br /&gt;
: One major goal with the exercise is to introduce you to statistically based methods for assessing the strength of evidence for a set of alternative hypotheses about some biological system of interest. The model selection method we will use is AIC (Akaike Information Criterion), based on which you will compute model probabilities. A second goal is to make you aware that phylogenetic analysis is not only about constructing trees, but that it is also a useful framework for analyzing biological questions more generally.&lt;br /&gt;
&lt;br /&gt;
: Specifically, you will&lt;br /&gt;
&lt;br /&gt;
:# perform a multiple alignment of gp120 DNA sequences taking protein-level information into account (using revtrans).&lt;br /&gt;
:# select a suitable nucleotide substitution model (using jmodeltest2)&lt;br /&gt;
:# construct a phylogenetic tree (using PAUP).&lt;br /&gt;
:# try to detect positively selected sites in gp120 (using PAML).&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
== Recipe for computing AIC values and model probabilities ==&lt;br /&gt;
&lt;br /&gt;
: Later in today&#039;s exercise you will be asked to compute AIC values and model probabilities. Return to this section and follow the instructions when you need to do so.&lt;br /&gt;
&lt;br /&gt;
:# Fit a set of models to your data, note the maximized log likelihoods (lnL) and the number of free parameters (K) for each model in the investigated set. The models you fit should represent a plausible and comprehensive set of hypotheses about your data.&lt;br /&gt;
:# Compute AIC for each of the models: &#039;&#039;&#039;AIC = -2 x lnL + 2K&#039;&#039;&#039;.  &amp;lt;br&amp;gt;For example: a model with lnL = -2010 and K = 5 will have AIC = -2 x -2010 + 2 x 5 = 4030.&lt;br /&gt;
:# Identify the model with the smallest AIC (this is the best model in the set). We will call the AIC for this model &#039;&#039;&#039;&amp;quot;AICmin&amp;quot;&#039;&#039;&#039;.&lt;br /&gt;
:# Compute the &amp;quot;ΔAIC&amp;quot; values for each model: &#039;&#039;&#039;ΔAIC = AIC - AICmin&#039;&#039;&#039; &amp;lt;br&amp;gt;For each model subtract the minimum AIC value. The best model will have a ΔAIC of zero. The rest of the models will have positive ΔAICs.&lt;br /&gt;
:# For each model compute the following quantity: &#039;&#039;&#039;numerator = exp(-0.5 x ΔAIC)&#039;&#039;&#039; &amp;lt;br&amp;gt;For example, a model with ΔAIC=4.2 will have numerator = exp(-0.5 x 4.2) = exp(-2.1) = 0.1225. Also compute the &#039;&#039;&#039;sum&#039;&#039;&#039; of the numerator values for all models.&lt;br /&gt;
:# Finally, the model probabilities (also called model weights) for each model are found as:&amp;lt;br&amp;gt; &#039;&#039;&#039;P(model) = numerator / sum&#039;&#039;&#039; &amp;lt;br&amp;gt;For example, if sum = 3.75 and a model has numerator = 1.3, then it has P(model) = 1.3 / 3.75 = 0.35&lt;br /&gt;
&lt;br /&gt;
: You may want to keep track of the computations by constructing a table along the following lines:&lt;br /&gt;
[[File:Molevol-Downloads-aictable.png|700px]]&lt;br /&gt;
&lt;br /&gt;
: Note that model probabilities can also be computed using Bayesian methods. One advantage of Bayesian methods over AIC is that instead of relying on a point estimate, uncertainty about parameter values is accounted for by integrating over all possible values (typically using MCMC).&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
== Getting started ==&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Create working directory, copy files&#039;&#039;&#039;&lt;br /&gt;
: In the command below: Instead of /path/to/molevol enter the path to the directory where you have placed your course files (for instance cd /Users/bob/Documents/molevol, or cd /home/student/molevol).&lt;br /&gt;
 cd /path/to/molevol&lt;br /&gt;
 mkdir modelselect&lt;br /&gt;
 cd modelselect&lt;br /&gt;
 cp ../data/gp120.fasta ./gp120.fasta&lt;br /&gt;
 cp ../data/codeml.ctl ./codeml.ctl&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Have a look at the DNA data file:&#039;&#039;&#039;&lt;br /&gt;
 nedit gp120.fasta &amp;amp;&lt;br /&gt;
: The file contains several DNA sequences from HIV-1, subtype B. The sequences are approximately 500 bp long, and correspond to a region surrounding the hypervariable V3 region in the gene encoding gp120. Close the nedit window when you&#039;ve had a look.&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
== Protein-guided alignment of coding DNA ==&lt;br /&gt;
&lt;br /&gt;
: There are three reasons why it is preferable to align coding DNA at the amino acid level rather than directly as nucleotides. First, the DNA alphabet has only four letters compared to 20 amino acids, which means that even unrelated DNA sequences will share roughly 25% identity by chance alone, making it much harder to distinguish true homology from noise. Second, protein alignments benefit from empirical substitution matrices such as BLOSUM-62, which capture the fact that amino acid replacements tend to be conservative. Equivalent matrices for DNA are far less informative because nucleotide substitution patterns vary strongly between genes and organisms. Third, synonymous substitutions accumulate faster than non-synonymous ones, so DNA sequences diverge more rapidly than the proteins they encode. Taken together, these factors mean that the phylogenetic signal in a DNA alignment erodes much faster than in the corresponding protein alignment.&lt;br /&gt;
&lt;br /&gt;
: However, while protein-level alignment gives us the most reliable homology signal, it discards exactly the information we need for studying selection: the synonymous substitutions. Estimating the ratio of non-synonymous to synonymous substitution rates (dN/dS) requires a codon-level alignment where analogous codon positions are properly lined up. We would therefore like to construct a multiple alignment at the DNA level, but using information at the protein level, and the RevTrans server does exactly that.&lt;br /&gt;
&lt;br /&gt;
: &#039;&#039;&#039;RevTrans&#039;&#039;&#039; takes as input an unaligned set of DNA sequences, automatically translates them to the equivalent amino acid sequences, constructs a multiple alignment of the protein sequences, and finally uses the protein alignment as a template for constructing a multiple DNA alignment. Because each amino acid maps back to its original codon, gaps are always inserted in groups of three and codon boundaries are preserved throughout the alignment. This is essential for the dN/dS analyses we will carry out in this exercise: if codon positions are misaligned, synonymous and non-synonymous changes cannot be correctly distinguished.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Construct RevTrans alignment&#039;&#039;&#039;&lt;br /&gt;
:* Open RevTrans server page: https://services.healthtech.dtu.dk/services/RevTrans-2.0/&lt;br /&gt;
:* On the RevTrans page: Choose the file gp120.fasta as input (or copy and paste the sequence into the sequence window)&lt;br /&gt;
:* Click the &amp;quot;Submit query&amp;quot; button&lt;br /&gt;
:* When the alignment is done you may have to click link named &amp;quot;here&amp;quot; to go to results page&lt;br /&gt;
:* Download DNA alignment, by right-clicking the link for &amp;quot;Download alignment in FASTA format&amp;quot;, and choosing &amp;quot;Save link as...&amp;quot; (save file under the name gp120align.fasta and make sure to save the file in the directory modelselect).&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Convert alignment to NEXUS format&#039;&#039;&#039;&lt;br /&gt;
: Convert the fasta file to NEXUS format and save file in the modelselect directory under the name gp120.nexus&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
== Model selection using AIC and jModelTest2 ==&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Question 1&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
: As part of the present analysis we are going to build a phylogenetic tree based on the DNA alignment constructed above. We will construct the tree using maximum likelihood, but to do that we first have to decide which substitution model we want to use. Specifically, we are interested in selecting a model that achieves a good balance between fit to the data and number of parameters. We will investigate this issue by fitting a set of 56 different models to our data and then comparing them using AIC.&lt;br /&gt;
&lt;br /&gt;
: In the first part of the exercise, however, you will only compare three closely related models by hand. The purpose of this restricted comparison is not primarily to identify the globally best model, but to understand how AIC, ΔAIC, and Akaike weights are computed and interpreted. Because only three models are included in this manual comparison, the weights you compute will describe relative support &#039;&#039;&#039;within this restricted set only&#039;&#039;&#039;.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Start jmodeltest2&#039;&#039;&#039;&lt;br /&gt;
 jmodeltest&lt;br /&gt;
&#039;&#039;&#039;Load data set&#039;&#039;&#039;&lt;br /&gt;
:* File -&amp;gt; Load DNA alignment -&amp;gt; File Format -&amp;gt; Select &amp;quot;All files&amp;quot;&lt;br /&gt;
:* Navigate to gp120.nexus and load it&lt;br /&gt;
&#039;&#039;&#039;Fit 56 models&#039;&#039;&#039;&lt;br /&gt;
:* Analysis -&amp;gt; Compute likelihood scores&lt;br /&gt;
:* Select &amp;quot;7&amp;quot; under &amp;quot;Number of substitution schemes&amp;quot;&lt;br /&gt;
:* Select &amp;quot;Fixed BIONJ-JC&amp;quot; under &amp;quot;Base tree for likelihood calculations&amp;quot;&lt;br /&gt;
:* Click &amp;quot;Compute likelihoods&amp;quot;&lt;br /&gt;
&lt;br /&gt;
: This causes jmodeltest2 to perform the following actions: first a neighbor joining tree is constructed using the Jukes and Cantor model. Then the tree is fixed and used as the basis for fitting a set of 56 different models to the data. For each model, the estimated model parameters and the negative log-likelihood are recorded. In addition to varying sets of substitution rate parameters (JC, K2P, ...), some of these models also include extra parameters that take into account the presence of different rates between sites. This is done in two ways: (1) by fitting a gamma distribution of rates (&amp;quot;+G&amp;quot;), and (2) by allowing for a proportion of constant (&amp;quot;invariable&amp;quot;) sites (&amp;quot;+I&amp;quot;).&lt;br /&gt;
&lt;br /&gt;
: Wait until jmodeltest2 is done fitting all 56 models (this will take a little while depending on your computer).&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Inspect result&#039;&#039;&#039;&lt;br /&gt;
:* Results -&amp;gt; Show results table&lt;br /&gt;
: For each model this table lists the negative log-likelihood (&amp;quot;-lnL&amp;quot;), the number of parameters (&amp;quot;p&amp;quot; - note this includes branch lengths), and estimates of all model parameters (excluding branch lengths).&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Manually compute Akaike weights for three substitution models&#039;&#039;&#039;&lt;br /&gt;
: Use AIC-based model comparison to investigate which of the following three substitution models are best supported as descriptions of how the sequences have evolved:&lt;br /&gt;
:* Jukes and Cantor with fraction of invariant sites (JC+I)&lt;br /&gt;
:* Jukes and Cantor with gamma-distributed rates over sites (JC+G)&lt;br /&gt;
:* Jukes and Cantor with invariant sites and gamma-distributed rates (JC+I+G)&lt;br /&gt;
: Before you can do the computation you need to know the log likelihood and the number of parameters for each model. Locate these values in the table for the JC+I, JC+G, and JC+I+G models, and write them down. Close the window with the result table when you are done.&lt;br /&gt;
&lt;br /&gt;
: Make sure to get the signs right: the values reported in the table are -lnL values, so you will need to reverse the sign to get the lnL (the lnL values you write down should be negative).&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Question: &#039;&#039;&#039;Use the recipe above to compute AIC, ΔAIC, and Akaike weight for the three models. Report the results in a table similar to the one shown above, and verify that the three weights sum to 1 (up to rounding error).&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Question 2&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
: Based on the Akaike weights, which of the three models has the strongest support? Is the support strongly concentrated on one model, or is there still substantial support for one or both of the others?&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Question 3&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
: What you just did manually for JC+I, JC+G and JC+I+G, jmodeltest2 can do automatically for the full set of 56 fitted models. Specifically, it uses the list of negative log likelihoods and parameter counts in the table to compute AIC values and Akaike weights for the full candidate set:&lt;br /&gt;
:* Analysis -&amp;gt; Do AIC calculations -&amp;gt;&lt;br /&gt;
:* Select &amp;quot;Write PAUP* block&amp;quot;&lt;br /&gt;
:* click &amp;quot;Do AIC calculations&amp;quot;&lt;br /&gt;
:* Results -&amp;gt; Show results table&lt;br /&gt;
:* Select &amp;quot;AIC&amp;quot; tab&lt;br /&gt;
:* &#039;&#039;&#039;IMPORTANT:&#039;&#039;&#039; SHIFT+click on the header of the &amp;quot;weight&amp;quot; column. This sorts the rows according to model weight, in descending order.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Question: &#039;&#039;&#039;What model was selected by modeltest based on the AIC values? (This will be the model with the highest weight - and lowest AIC - and will be in the first row of the results table after SHIFT-clicking the &amp;quot;weight&amp;quot; header).&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Question 4&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
: In the same AIC results table, now click (not SHIFT+click) the header of the &amp;quot;-lnL&amp;quot; column. In this table, a normal click sorts in ascending order, whereas SHIFT+click sorts in descending order. The model at the top will therefore be the one with the smallest value of &amp;quot;-lnL&amp;quot; (equivalently: the highest likelihood). Compare this model to the one with the highest Akaike weight.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Question: &#039;&#039;&#039;Is the model with the highest likelihood also the model with the highest Akaike weight? If not, explain briefly why these two rankings can differ.&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
== Phylogenetic tree inference ==&lt;br /&gt;
&lt;br /&gt;
=== Tree inference using the model selected by jModelTest2 (PAUP) ===&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Question 5&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
: Above, you used jModelTest2 to select a substitution model based on AIC. You will now use that model to construct a maximum likelihood tree in &#039;&#039;&#039;PAUP&#039;&#039;&#039;. The purpose of doing this in PAUP is mainly pedagogical: it makes the individual steps of model-based phylogenetic inference more explicit. In practice, one would often use a program such as IQ-TREE 3 (see next section).&lt;br /&gt;
&lt;br /&gt;
: Close the results table. In the main window you should now scroll up to the lines giving PAUP commands that implement the selected model. The command is enclosed between &amp;quot;BEGIN PAUP&amp;quot; and &amp;quot;END;&amp;quot; and should look something like this:&lt;br /&gt;
 Lset Base=(0.4064, [...]&lt;br /&gt;
: You will need to copy this command to a PAUP session in the next step.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Start PAUP&#039;&#039;&#039;&lt;br /&gt;
 paup&lt;br /&gt;
: Above, you used jModelTest2 to select the most suitable substitution model for the present data set. You will now use this model to construct a maximum likelihood tree. You will use PAUP for this purpose. (Note: it is possible to create a maximum likelihood tree, or even a model-averaged tree, directly from jModelTest2, but we will instead do it in PAUP in order to more clearly see each step that is taken.)&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Load alignment:&#039;&#039;&#039;&lt;br /&gt;
 execute gp120.nexus&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Set tree-building criterion to maximum likelihood&#039;&#039;&#039;&lt;br /&gt;
 set criterion=likelihood&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Set model parameters to winning estimates&#039;&#039;&#039;&lt;br /&gt;
: Above, you located a set of lines in the jModelTest2 output giving a PAUP command that sets the model parameters to the estimates found for the winning model. Copy and paste this lset command (without the BEGIN and END parts) into the window where PAUP is running.&lt;br /&gt;
&lt;br /&gt;
 PASTE LSET COMMAND FROM MODELTEST RUN HERE&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Find best tree using selected model&#039;&#039;&#039;&lt;br /&gt;
: Still in the PAUP window, enter the following command:&lt;br /&gt;
 hsearch swap=tbr start=nj&lt;br /&gt;
: This command causes PAUP to perform a heuristic search for the best maximum likelihood tree. Once an initial tree has been constructed, the heuristic search proceeds by rearrangements of the &amp;quot;tree bisection and reconnection&amp;quot; type (TBR). We are using the model selected by modeltest, and the parameter estimates found by modeltest for that model. You could also choose to estimate all the model parameters as part of this step (i.e., at the same time as finding the best tree), but fixing them improves speed tremendously. Finding the best tree should take at most a few minutes.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Save best tree to file&#039;&#039;&#039;&lt;br /&gt;
 savetrees format=newick brlens=yes file=gp120tree_paup.phy from=1 to=1&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Quit program&#039;&#039;&#039;&lt;br /&gt;
 quit&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Have a look at the tree:&#039;&#039;&#039;&lt;br /&gt;
: You have now produced an unrooted tree of the HIV sequences and saved it in the file gp120tree_paup.phy. Note that in this exercise we will not be interested in the tree as such - our focus is instead on finding positive selection at a subset of codon positions, and the tree is just something we need in order to fit the different codon models to the data. If you want to see the tree, you can do so with the following command:&lt;br /&gt;
 figtree gp120tree_paup.phy &amp;amp;&lt;br /&gt;
: There is no meaningful root placed on this tree, so you may want to choose the unrooted view (the third icon in the Layout section of the FigTree window). Close the FigTree window when you have had a look.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Question: &#039;&#039;&#039;Which substitution model did you use for the PAUP tree search, and what was the negative log likelihood of the best tree found under this model?&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
=== One-step model selection and tree inference using IQ-TREE 3 ===&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Question 6&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
: In real-life phylogenetic analysis one would often use a modern program such as &#039;&#039;&#039;IQ-TREE 3&#039;&#039;&#039;, which can carry out model selection and maximum likelihood tree reconstruction in a single analysis. You will now repeat the analysis using this more integrated approach.&lt;br /&gt;
&lt;br /&gt;
: Run IQ-TREE 3 on the alignment using the following command:&lt;br /&gt;
 iqtree3 -s gp120.nexus -m MFP&lt;br /&gt;
&lt;br /&gt;
: Here, &amp;quot;-m MFP&amp;quot; tells IQ-TREE 3 to perform automatic model selection using &amp;quot;ModelFinder Plus&amp;quot; before reconstructing the tree.&lt;br /&gt;
&lt;br /&gt;
: When the run has finished, inspect the output written to the screen and to the file ending in &amp;quot;.iqtree&amp;quot;. Note that IQ-TREE 3 reports several model-selection criteria in the output. The line near the top of the ModelFinder section states which criterion was used to choose the final model. Locate the substitution model selected by IQ-TREE 3 and the log likelihood of the best tree.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Question: &#039;&#039;&#039;Which substitution model was selected by IQ-TREE 3, and according to which information criterion was it selected? What was the log likelihood of the best tree? Compare this to the earlier jModelTest2 + PAUP analysis. Is the selected model exactly the same? Is the likelihood exactly the same? Briefly suggest one reason why the two analyses might not give identical results.&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
== Detection of positively selected sites in gp120 ==&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Question 7&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
: There is much more to phylogenetic analyses than merely reconstructing trees. One interesting result of probabilistic methods is that the parameters of a model are estimated as part of the optimization procedure. This means that once such a model has been fitted to the data, it is possible to investigate these estimated parameter values to learn features about the evolutionary history of the sequences under investigation. In the present example we will focus on whether we can detect positively selected sites in our data set, defined as sites where the dN/dS ratio is larger than 1. We do that by using a codon substitution model where the dN/dS ratio is one of its parameters.&lt;br /&gt;
&lt;br /&gt;
: A further strength of the probabilistic approach is that you get a probabilistic measure of how well any model fits the data. This means you can use a stringent approach to determine which model fits the data best. In this framework one uses likelihoods (probabilities of data given model) to determine which model fits the data best. As you saw above, it is for instance possible to compute AIC values and weights (model probabilities) from the likelihood values of fitted models. Since each model essentially corresponds to a hypothesis about the evolutionary history of the data, we can thus use a stringent statistical approach to decide which hypothesis best describes our data.&lt;br /&gt;
&lt;br /&gt;
: In outline, you will now use the following steps to investigate whether there is any evidence for positively selected codons in your data set:&lt;br /&gt;
&lt;br /&gt;
:* Fit model M1, which assumes there are two classes of codons in the sequence: some with dN/dS &amp;lt; 1, some with dN/dS = 1.&lt;br /&gt;
:* Fit model M2, which assumes 3 distinct classes of codons: two with dN/dS ratios as for M1, and one extra class with dN/dS &amp;gt; 1.&lt;br /&gt;
:* Assess the strength of evidence for the two models using AIC-based model probabilities&lt;br /&gt;
:* If M2 is better: identify the positively selected codons&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Edit the parameter file&#039;&#039;&#039;&lt;br /&gt;
 nedit codeml.ctl &amp;amp;&lt;br /&gt;
: The file &amp;quot;codeml.ctl&amp;quot; contains several settings that control how &#039;&#039;&#039;codeml&#039;&#039;&#039; is run. Before continuing, edit the file and save it so that the following settings have these values. Any text appearing after an asterisk (*) on a line is only a comment, and you do not need to change it.&lt;br /&gt;
&lt;br /&gt;
 &#039;&#039;&#039;seqfile =  gp120align.fasta&#039;&#039;&#039;&lt;br /&gt;
: name of alignment file&lt;br /&gt;
&lt;br /&gt;
 &#039;&#039;&#039;treefile =  gp120.nexus.treefile&#039;&#039;&#039;&lt;br /&gt;
: name of tree file produced by IQ-TREE 3&lt;br /&gt;
&lt;br /&gt;
 &#039;&#039;&#039;seqtype = 1&#039;&#039;&#039;&lt;br /&gt;
: tells the program that the data consist of coding DNA&lt;br /&gt;
&lt;br /&gt;
 &#039;&#039;&#039;NSsites = 1 2&#039;&#039;&#039;&lt;br /&gt;
: tells the program to analyze models M1 and M2&lt;br /&gt;
&lt;br /&gt;
 &#039;&#039;&#039;cleandata = 0&#039;&#039;&#039;&lt;br /&gt;
: tells the program to keep positions with gaps&lt;br /&gt;
&lt;br /&gt;
: &#039;&#039;&#039;Important:&#039;&#039;&#039; codeml requires a phylogenetic tree as input. The tree supplies the topology for the codon-model analysis, while codeml estimates the branch lengths and the remaining model parameters itself. In this exercise, we use the tree produced by &#039;&#039;&#039;IQ-TREE 3&#039;&#039;&#039; (&#039;&#039;&#039;gp120.nexus.treefile&#039;&#039;&#039;) as input.&lt;br /&gt;
&lt;br /&gt;
: The settings entered by us will cause codeml to analyze two hypotheses about dN/dS ratios. M1 says there are two classes of codons with different dN/dS ratios in the sequence: one class with dN/dS &amp;lt; 1 (codons under purifying or negative selection), and one class with dN/dS = 1 (no selection - neutrally evolving sites). M2 says there are 3 distinct dN/dS ratios for different sites in the sequence: one class with dN/dS &amp;lt; 1, one class with dN/dS = 1 (these are the same type of classes as for M1), and one class with dN/dS &amp;gt; 1 (corresponding to sites under positive selection). The value of the dN/dS ratios (for those classes that have dN/dS &amp;lt; 1 or dN/dS &amp;gt; 1), the fraction of sites belonging to each class, and the position of sites belonging to each class, are unknown at first and will be determined during the analysis.&lt;br /&gt;
&lt;br /&gt;
: (Regarding &amp;quot;cleandata&amp;quot; setting: &amp;quot;cleandata = 1&amp;quot; would cause codeml to discard any alignment columns with gaps or ambiguity symbols. For alignments with many sequences this can lead to discarding the majority of sites and I would therefore recommend not using that setting. At the same time you do want to be wary of columns where most sequences have gaps, since inference of selection for these will be very uncertain. It might be better to discard such columns from an alignment before doing the codeml analysis.)&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Start the analysis&#039;&#039;&#039;&lt;br /&gt;
 codeml&lt;br /&gt;
: This will start the codeml program using the settings in the file codeml.ctl. Depending on your computer, this will take some minutes to finish. (You may be able to see how the optimization procedure results in progressively better fits: the likelihood increases, meaning that negative log-likelihood decreases, as the fit improves.)&lt;br /&gt;
&lt;br /&gt;
: &#039;&#039;&#039;Note:&#039;&#039;&#039; On some systems the current version of codeml (4.10.10) may print an error message near the end of the run, even though the relevant results have already been written to &#039;&#039;&#039;selection.results&#039;&#039;&#039;. If this happens, inspect &#039;&#039;&#039;selection.results&#039;&#039;&#039; before assuming that the run failed completely.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Inspect result file:&#039;&#039;&#039;&lt;br /&gt;
: Wait for the run to finish, and then look at the result file:&lt;br /&gt;
 nedit selection.results &amp;amp;&lt;br /&gt;
: This file contains a wealth of information concerning your analysis. You may want to turn off line-wrapping to more clearly see the structure of the file (there will be some long lines that will otherwise wrap around). The top part of the file gives an overview of your sequences, codon usage and nucleotide frequencies. You can ignore this information for now, and move on to the interesting part, namely the model likelihoods and parameter values.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Find likelihood, and number of free parameters for model M1&#039;&#039;&#039;&lt;br /&gt;
 Search ==&amp;gt; Find... ==&amp;gt; enter &amp;quot;Model 1&amp;quot; and click Find&lt;br /&gt;
: You are now in the region of the result file where the model likelihoods and parameter estimates are noted. Now locate a line that looks a bit like the one shown below:&lt;br /&gt;
 lnL(ntime: 72  np: 74):  -4242.470345     +0.000000&lt;br /&gt;
: Identify the number of &amp;quot;free parameters&amp;quot;, K, used in model M1: this is indicated by &amp;quot;np&amp;quot;, and is 74 in the example shown above (most of these parameters are branch lengths in the tree; specifically, the number of branch length parameters is indicated by &amp;quot;ntime&amp;quot;, and is 72 in this example). Also note the log-likelihood of the fitted model. This is the number right after the parenthesis, and is -4242.470345 in the example here.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Question: &#039;&#039;&#039;What are the values of K and lnL for model M1?&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Question 8&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Find dN/dS ratios and codon class proportions for model M1&#039;&#039;&#039;&lt;br /&gt;
: Scroll down a few lines until you get to a small table similar to this:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
      dN/dS for site classes (K=2)&lt;br /&gt;
      p:   0.75111  0.24889&lt;br /&gt;
      w:   0.06583  1.00000&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
: This gives a summary of the dN/dS ratios that were found in the data set. The line starting &amp;quot;w:&amp;quot; lists the two dN/dS ratios that were found (in this case 0.06583 and 1.00000 - the last one was pre-specified by us as part of the model and was therefore not a free parameter). In this context &amp;quot;w&amp;quot; means &amp;quot;omega&amp;quot;, which is the symbol typically used to represent the dN/dS rate ratio. The line starting &amp;quot;p:&amp;quot; gives the proportion of codon sites belonging to each of the dN/dS ratio classes (in the example above approximately 75% belong to the first class, while 25% of all sites belong to the class having dN/dS = 1.00000).&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Question: &#039;&#039;&#039;What are the dN/dS value (w) and proportion (p) of sites for both classes? Report the following values: p(class1), w(class1), p(class2), w(class2)&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Question 9&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Find likelihood, and K for model M2&#039;&#039;&#039;&lt;br /&gt;
: Scroll past the M1 output until you get to the results for model M2.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Question: &#039;&#039;&#039;What are the values of K and lnL for model M2?&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Question 10&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Find dN/dS ratios and codon class proportions for model M2&#039;&#039;&#039;&lt;br /&gt;
: Now scroll down a few lines until you get to a small table similar to the one you examined for M1 before. For this model there are 3 separate classes of codons.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Question: &#039;&#039;&#039;What are the dN/dS value (w) and proportion (p) of sites for all three classes? Report these values: p(class1), w(class1), p(class2), w(class2), p(class3), w(class3)&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Question 11&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Assess strength of evidence for models M1 and M2&#039;&#039;&#039;&lt;br /&gt;
: M2 will always have a better (higher) log-likelihood than model M1 because M2 has more free parameters, and M1 is nested within M2. You should now use the recipe given above to compute AIC values and model probabilities for M1 and M2.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Question: &#039;&#039;&#039;Report: AIC, ΔAIC, weight (model probability) for M1 and M2&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Question 12: &#039;&#039;&#039;Is M2 better than M1? Based on this comparison, is there evidence that some codon sites in gp120 have dN/dS &amp;gt; 1?&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Question 13&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Examine list of positively selected sites&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
: If your M2 is clearly better than M1, then you have evidence for the existence of positively selected sites in the gp120 gene. Now scroll down to the end of the result file and locate the &amp;quot;Bayes Empirical Bayes (BEB) analysis&amp;quot; table. Note: use the BEB table, not the Naive Empirical Bayes (NEB) table. (It is not important what the distinction is in this context, but if you are interested: NEB ignores the fact that there is uncertainty about maximum likelihood estimates, especially for smaller data sets - for instance w for some codon is perhaps not exactly 3.046, but could lie in a region around that value - whereas [https://pubmed.ncbi.nlm.nih.gov/15689528/ BEB accounts for that uncertainty].)&lt;br /&gt;
&lt;br /&gt;
: This table lists codon positions, the posterior probability that each site belongs to the positively selected class (that is, the class with dN/dS &amp;gt; 1), and the estimated mean value of w for the site.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Question: &#039;&#039;&#039;List all sites having more than 95% probability of belonging to the positively selected class.&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Question 14&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
: Consider your results from Questions 10–13. In model M2, only a fraction of sites belong to the positively selected class, while many sites belong to classes with dN/dS ≤ 1.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Question: &#039;&#039;&#039;What does this suggest about the overall pattern of selection acting on gp120? Explain briefly why one might expect a mixture of negatively selected, neutral, and positively selected sites in this gene.&lt;/div&gt;</summary>
		<author><name>Gorm</name></author>
	</entry>
	<entry>
		<id>https://teaching.healthtech.dtu.dk/22115/index.php?title=Model_selection&amp;diff=282</id>
		<title>Model selection</title>
		<link rel="alternate" type="text/html" href="https://teaching.healthtech.dtu.dk/22115/index.php?title=Model_selection&amp;diff=282"/>
		<updated>2026-04-15T10:08:22Z</updated>

		<summary type="html">&lt;p&gt;Gorm: /* Detection of positively selected sites in gp120 */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;This exercise is part of the course  [[22115_-_Computational_Molecular_Evolution|Computational Molecular Evolution (22115)]].&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Overview ==&lt;br /&gt;
&lt;br /&gt;
: In this exercise you are going to investigate features of HIV-1 evolution. You will do this by analyzing a large set of env-genes from HIV-1, subtype B. specifically, the DNA sequences analyzed here correspond to a region surrounding the hypervariable V3 region of the gp120 protein.&lt;br /&gt;
&lt;br /&gt;
: Like other retroviruses, particles of HIV are made up of 2 copies of a single-stranded RNA genome packaged inside a protein core, or capsid. The core particle also contains viral proteins that are essential for the early steps of the virus life cycle, such as reverse transcription and integration. A lipid envelope, derived from the infected cell, surrounds the core particle. Embedded in this envelope are the surface glycoproteins of HIV: gp120 and gp41. The gp120 protein is crucial for binding of the virus particle to target cells, while gp41 is important for the subsequent fusion event. It is the specific affinity of gp120 for the CD4 protein that targets HIV to those cells of the immune system that express CD4 on their surface (e.g., T-helper lymphocytes, monocytes, and macrophages).&lt;br /&gt;
&lt;br /&gt;
: The role gp120 plays in infection and the fact that it is situated on the surface of the HIV particle, means it is an obvious target for the immune response. That means that there may be a considerable selective pressure on gp120 for creating immune-escape mutants, where amino acids in the gp120 epitopes have been substituted. In this exercise you will construct a maximum likelihood tree that we will subsequently use to investigate whether you can detect such a selective pressure on parts of gp120, again using maximum likelihood methods.&lt;br /&gt;
&lt;br /&gt;
: One major goal with the exercise is to introduce you to statistically based methods for assessing the strength of evidence for a set of alternative hypotheses about some biological system of interest. The model selection method we will use is AIC (Akaike Information Criterion), based on which you will compute model probabilities. A second goal is to make you aware that phylogenetic analysis is not only about constructing trees, but that it is also a useful framework for analyzing biological questions more generally.&lt;br /&gt;
&lt;br /&gt;
: Specifically, you will&lt;br /&gt;
&lt;br /&gt;
:# perform a multiple alignment of gp120 DNA sequences taking protein-level information into account (using revtrans).&lt;br /&gt;
:# select a suitable nucleotide substitution model (using jmodeltest2)&lt;br /&gt;
:# construct a phylogenetic tree (using PAUP).&lt;br /&gt;
:# try to detect positively selected sites in gp120 (using PAML).&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
== Recipe for computing AIC values and model probabilities ==&lt;br /&gt;
&lt;br /&gt;
: Later in today&#039;s exercise you will be asked to compute AIC values and model probabilities. Return to this section and follow the instructions when you need to do so.&lt;br /&gt;
&lt;br /&gt;
:# Fit a set of models to your data, note the maximized log likelihoods (lnL) and the number of free parameters (K) for each model in the investigated set. The models you fit should represent a plausible and comprehensive set of hypotheses about your data.&lt;br /&gt;
:# Compute AIC for each of the models: &#039;&#039;&#039;AIC = -2 x lnL + 2K&#039;&#039;&#039;.  &amp;lt;br&amp;gt;For example: a model with lnL = -2010 and K = 5 will have AIC = -2 x -2010 + 2 x 5 = 4030.&lt;br /&gt;
:# Identify the model with the smallest AIC (this is the best model in the set). We will call the AIC for this model &#039;&#039;&#039;&amp;quot;AICmin&amp;quot;&#039;&#039;&#039;.&lt;br /&gt;
:# Compute the &amp;quot;ΔAIC&amp;quot; values for each model: &#039;&#039;&#039;ΔAIC = AIC - AICmin&#039;&#039;&#039; &amp;lt;br&amp;gt;For each model subtract the minimum AIC value. The best model will have a ΔAIC of zero. The rest of the models will have positive ΔAICs.&lt;br /&gt;
:# For each model compute the following quantity: &#039;&#039;&#039;numerator = exp(-0.5 x ΔAIC)&#039;&#039;&#039; &amp;lt;br&amp;gt;For example, a model with ΔAIC=4.2 will have numerator = exp(-0.5 x 4.2) = exp(-2.1) = 0.1225. Also compute the &#039;&#039;&#039;sum&#039;&#039;&#039; of the numerator values for all models.&lt;br /&gt;
:# Finally, the model probabilities (also called model weights) for each model are found as:&amp;lt;br&amp;gt; &#039;&#039;&#039;P(model) = numerator / sum&#039;&#039;&#039; &amp;lt;br&amp;gt;For example, if sum = 3.75 and a model has numerator = 1.3, then it has P(model) = 1.3 / 3.75 = 0.35&lt;br /&gt;
&lt;br /&gt;
: You may want to keep track of the computations by constructing a table along the following lines:&lt;br /&gt;
[[File:Molevol-Downloads-aictable.png|700px]]&lt;br /&gt;
&lt;br /&gt;
: Note that model probabilities can also be computed using Bayesian methods. One advantage of Bayesian methods over AIC is that instead of relying on a point estimate, uncertainty about parameter values is accounted for by integrating over all possible values (typically using MCMC).&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
== Getting started ==&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Create working directory, copy files&#039;&#039;&#039;&lt;br /&gt;
: In the command below: Instead of /path/to/molevol enter the path to the directory where you have placed your course files (for instance cd /Users/bob/Documents/molevol, or cd /home/student/molevol).&lt;br /&gt;
 cd /path/to/molevol&lt;br /&gt;
 mkdir modelselect&lt;br /&gt;
 cd modelselect&lt;br /&gt;
 cp ../data/gp120.fasta ./gp120.fasta&lt;br /&gt;
 cp ../data/codeml.ctl ./codeml.ctl&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Have a look at the DNA data file:&#039;&#039;&#039;&lt;br /&gt;
 nedit gp120.fasta &amp;amp;&lt;br /&gt;
: The file contains several DNA sequences from HIV-1, subtype B. The sequences are approximately 500 bp long, and correspond to a region surrounding the hypervariable V3 region in the gene encoding gp120. Close the nedit window when you&#039;ve had a look.&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
== Protein-guided alignment of coding DNA ==&lt;br /&gt;
&lt;br /&gt;
: There are three reasons why it is preferable to align coding DNA at the amino acid level rather than directly as nucleotides. First, the DNA alphabet has only four letters compared to 20 amino acids, which means that even unrelated DNA sequences will share roughly 25% identity by chance alone, making it much harder to distinguish true homology from noise. Second, protein alignments benefit from empirical substitution matrices such as BLOSUM-62, which capture the fact that amino acid replacements tend to be conservative. Equivalent matrices for DNA are far less informative because nucleotide substitution patterns vary strongly between genes and organisms. Third, synonymous substitutions accumulate faster than non-synonymous ones, so DNA sequences diverge more rapidly than the proteins they encode. Taken together, these factors mean that the phylogenetic signal in a DNA alignment erodes much faster than in the corresponding protein alignment.&lt;br /&gt;
&lt;br /&gt;
: However, while protein-level alignment gives us the most reliable homology signal, it discards exactly the information we need for studying selection: the synonymous substitutions. Estimating the ratio of non-synonymous to synonymous substitution rates (dN/dS) requires a codon-level alignment where analogous codon positions are properly lined up. We would therefore like to construct a multiple alignment at the DNA level, but using information at the protein level, and the RevTrans server does exactly that.&lt;br /&gt;
&lt;br /&gt;
: &#039;&#039;&#039;RevTrans&#039;&#039;&#039; takes as input an unaligned set of DNA sequences, automatically translates them to the equivalent amino acid sequences, constructs a multiple alignment of the protein sequences, and finally uses the protein alignment as a template for constructing a multiple DNA alignment. Because each amino acid maps back to its original codon, gaps are always inserted in groups of three and codon boundaries are preserved throughout the alignment. This is essential for the dN/dS analyses we will carry out in this exercise: if codon positions are misaligned, synonymous and non-synonymous changes cannot be correctly distinguished.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Construct RevTrans alignment&#039;&#039;&#039;&lt;br /&gt;
:* Open RevTrans server page: https://services.healthtech.dtu.dk/services/RevTrans-2.0/&lt;br /&gt;
:* On the RevTrans page: Choose the file gp120.fasta as input (or copy and paste the sequence into the sequence window)&lt;br /&gt;
:* Click the &amp;quot;Submit query&amp;quot; button&lt;br /&gt;
:* When the alignment is done you may have to click link named &amp;quot;here&amp;quot; to go to results page&lt;br /&gt;
:* Download DNA alignment, by right-clicking the link for &amp;quot;Download alignment in FASTA format&amp;quot;, and choosing &amp;quot;Save link as...&amp;quot; (save file under the name gp120align.fasta and make sure to save the file in the directory modelselect).&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Convert alignment to NEXUS format&#039;&#039;&#039;&lt;br /&gt;
: Convert the fasta file to NEXUS format and save file in the modelselect directory under the name gp120.nexus&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
== Model selection using AIC and jModelTest2 ==&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Question 1&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
: As part of the present analysis we are going to build a phylogenetic tree based on the DNA alignment constructed above. We will construct the tree using maximum likelihood, but to do that we first have to decide which substitution model we want to use. Specifically, we are interested in selecting a model that achieves a good balance between fit to the data and number of parameters. We will investigate this issue by fitting a set of 56 different models to our data and then comparing them using AIC.&lt;br /&gt;
&lt;br /&gt;
: In the first part of the exercise, however, you will only compare three closely related models by hand. The purpose of this restricted comparison is not primarily to identify the globally best model, but to understand how AIC, ΔAIC, and Akaike weights are computed and interpreted. Because only three models are included in this manual comparison, the weights you compute will describe relative support &#039;&#039;&#039;within this restricted set only&#039;&#039;&#039;.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Start jmodeltest2&#039;&#039;&#039;&lt;br /&gt;
 jmodeltest&lt;br /&gt;
&#039;&#039;&#039;Load data set&#039;&#039;&#039;&lt;br /&gt;
:* File -&amp;gt; Load DNA alignment -&amp;gt; File Format -&amp;gt; Select &amp;quot;All files&amp;quot;&lt;br /&gt;
:* Navigate to gp120.nexus and load it&lt;br /&gt;
&#039;&#039;&#039;Fit 56 models&#039;&#039;&#039;&lt;br /&gt;
:* Analysis -&amp;gt; Compute likelihood scores&lt;br /&gt;
:* Select &amp;quot;7&amp;quot; under &amp;quot;Number of substitution schemes&amp;quot;&lt;br /&gt;
:* Select &amp;quot;Fixed BIONJ-JC&amp;quot; under &amp;quot;Base tree for likelihood calculations&amp;quot;&lt;br /&gt;
:* Click &amp;quot;Compute likelihoods&amp;quot;&lt;br /&gt;
&lt;br /&gt;
: This causes jmodeltest2 to perform the following actions: first a neighbor joining tree is constructed using the Jukes and Cantor model. Then the tree is fixed and used as the basis for fitting a set of 56 different models to the data. For each model, the estimated model parameters and the negative log-likelihood are recorded. In addition to varying sets of substitution rate parameters (JC, K2P, ...), some of these models also include extra parameters that take into account the presence of different rates between sites. This is done in two ways: (1) by fitting a gamma distribution of rates (&amp;quot;+G&amp;quot;), and (2) by allowing for a proportion of constant (&amp;quot;invariable&amp;quot;) sites (&amp;quot;+I&amp;quot;).&lt;br /&gt;
&lt;br /&gt;
: Wait until jmodeltest2 is done fitting all 56 models (this will take a little while depending on your computer).&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Inspect result&#039;&#039;&#039;&lt;br /&gt;
:* Results -&amp;gt; Show results table&lt;br /&gt;
: For each model this table lists the negative log-likelihood (&amp;quot;-lnL&amp;quot;), the number of parameters (&amp;quot;p&amp;quot; - note this includes branch lengths), and estimates of all model parameters (excluding branch lengths).&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Manually compute Akaike weights for three substitution models&#039;&#039;&#039;&lt;br /&gt;
: Use AIC-based model comparison to investigate which of the following three substitution models are best supported as descriptions of how the sequences have evolved:&lt;br /&gt;
:* Jukes and Cantor with fraction of invariant sites (JC+I)&lt;br /&gt;
:* Jukes and Cantor with gamma-distributed rates over sites (JC+G)&lt;br /&gt;
:* Jukes and Cantor with invariant sites and gamma-distributed rates (JC+I+G)&lt;br /&gt;
: Before you can do the computation you need to know the log likelihood and the number of parameters for each model. Locate these values in the table for the JC+I, JC+G, and JC+I+G models, and write them down. Close the window with the result table when you are done.&lt;br /&gt;
&lt;br /&gt;
: Make sure to get the signs right: the values reported in the table are -lnL values, so you will need to reverse the sign to get the lnL (the lnL values you write down should be negative).&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Question: &#039;&#039;&#039;Use the recipe above to compute AIC, ΔAIC, and Akaike weight for the three models. Report the results in a table similar to the one shown above, and verify that the three weights sum to 1 (up to rounding error).&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Question 2&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
: Based on the Akaike weights, which of the three models has the strongest support? Is the support strongly concentrated on one model, or is there still substantial support for one or both of the others?&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Question 3&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
: What you just did manually for JC+I, JC+G and JC+I+G, jmodeltest2 can do automatically for the full set of 56 fitted models. Specifically, it uses the list of negative log likelihoods and parameter counts in the table to compute AIC values and Akaike weights for the full candidate set:&lt;br /&gt;
:* Analysis -&amp;gt; Do AIC calculations -&amp;gt;&lt;br /&gt;
:* Select &amp;quot;Write PAUP* block&amp;quot;&lt;br /&gt;
:* click &amp;quot;Do AIC calculations&amp;quot;&lt;br /&gt;
:* Results -&amp;gt; Show results table&lt;br /&gt;
:* Select &amp;quot;AIC&amp;quot; tab&lt;br /&gt;
:* &#039;&#039;&#039;IMPORTANT:&#039;&#039;&#039; SHIFT+click on the header of the &amp;quot;weight&amp;quot; column. This sorts the rows according to model weight, in descending order.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Question: &#039;&#039;&#039;What model was selected by modeltest based on the AIC values? (This will be the model with the highest weight - and lowest AIC - and will be in the first row of the results table after SHIFT-clicking the &amp;quot;weight&amp;quot; header).&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Question 4&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
: In the same AIC results table, now click (not SHIFT+click) the header of the &amp;quot;-lnL&amp;quot; column. In this table, a normal click sorts in ascending order, whereas SHIFT+click sorts in descending order. The model at the top will therefore be the one with the smallest value of &amp;quot;-lnL&amp;quot; (equivalently: the highest likelihood). Compare this model to the one with the highest Akaike weight.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Question: &#039;&#039;&#039;Is the model with the highest likelihood also the model with the highest Akaike weight? If not, explain briefly why these two rankings can differ.&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
== Phylogenetic tree inference ==&lt;br /&gt;
&lt;br /&gt;
=== Tree inference using the model selected by jModelTest2 (PAUP) ===&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Question 5&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
: Above, you used jModelTest2 to select a substitution model based on AIC. You will now use that model to construct a maximum likelihood tree in &#039;&#039;&#039;PAUP&#039;&#039;&#039;. The purpose of doing this in PAUP is mainly pedagogical: it makes the individual steps of model-based phylogenetic inference more explicit. In practice, one would often use a program such as IQ-TREE 3 (see next section).&lt;br /&gt;
&lt;br /&gt;
: Close the results table. In the main window you should now scroll up to the lines giving PAUP commands that implement the selected model. The command is enclosed between &amp;quot;BEGIN PAUP&amp;quot; and &amp;quot;END;&amp;quot; and should look something like this:&lt;br /&gt;
 Lset Base=(0.4064, [...]&lt;br /&gt;
: You will need to copy this command to a PAUP session in the next step.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Start PAUP&#039;&#039;&#039;&lt;br /&gt;
 paup&lt;br /&gt;
: Above, you used jModelTest2 to select the most suitable substitution model for the present data set. You will now use this model to construct a maximum likelihood tree. You will use PAUP for this purpose. (Note: it is possible to create a maximum likelihood tree, or even a model-averaged tree, directly from jModelTest2, but we will instead do it in PAUP in order to more clearly see each step that is taken.)&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Load alignment:&#039;&#039;&#039;&lt;br /&gt;
 execute gp120.nexus&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Set tree-building criterion to maximum likelihood&#039;&#039;&#039;&lt;br /&gt;
 set criterion=likelihood&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Set model parameters to winning estimates&#039;&#039;&#039;&lt;br /&gt;
: Above, you located a set of lines in the jModelTest2 output giving a PAUP command that sets the model parameters to the estimates found for the winning model. Copy and paste this lset command (without the BEGIN and END parts) into the window where PAUP is running.&lt;br /&gt;
&lt;br /&gt;
 PASTE LSET COMMAND FROM MODELTEST RUN HERE&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Find best tree using selected model&#039;&#039;&#039;&lt;br /&gt;
: Still in the PAUP window, enter the following command:&lt;br /&gt;
 hsearch swap=tbr start=nj&lt;br /&gt;
: This command causes PAUP to perform a heuristic search for the best maximum likelihood tree. Once an initial tree has been constructed, the heuristic search proceeds by rearrangements of the &amp;quot;tree bisection and reconnection&amp;quot; type (TBR). We are using the model selected by modeltest, and the parameter estimates found by modeltest for that model. You could also choose to estimate all the model parameters as part of this step (i.e., at the same time as finding the best tree), but fixing them improves speed tremendously. Finding the best tree should take at most a few minutes.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Save best tree to file&#039;&#039;&#039;&lt;br /&gt;
 savetrees format=newick brlens=yes file=gp120tree_paup.phy from=1 to=1&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Quit program&#039;&#039;&#039;&lt;br /&gt;
 quit&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Have a look at the tree:&#039;&#039;&#039;&lt;br /&gt;
: You have now produced an unrooted tree of the HIV sequences and saved it in the file gp120tree_paup.phy. Note that in this exercise we will not be interested in the tree as such - our focus is instead on finding positive selection at a subset of codon positions, and the tree is just something we need in order to fit the different codon models to the data. If you want to see the tree, you can do so with the following command:&lt;br /&gt;
 figtree gp120tree_paup.phy &amp;amp;&lt;br /&gt;
: There is no meaningful root placed on this tree, so you may want to choose the unrooted view (the third icon in the Layout section of the FigTree window). Close the FigTree window when you have had a look.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Question: &#039;&#039;&#039;Which substitution model did you use for the PAUP tree search, and what was the negative log likelihood of the best tree found under this model?&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
=== One-step model selection and tree inference using IQ-TREE 3 ===&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Question 6&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
: In real-life phylogenetic analysis one would often use a modern program such as &#039;&#039;&#039;IQ-TREE 3&#039;&#039;&#039;, which can carry out model selection and maximum likelihood tree reconstruction in a single analysis. You will now repeat the analysis using this more integrated approach.&lt;br /&gt;
&lt;br /&gt;
: Run IQ-TREE 3 on the alignment using the following command:&lt;br /&gt;
 iqtree3 -s gp120.nexus -m MFP&lt;br /&gt;
&lt;br /&gt;
: Here, &amp;quot;-m MFP&amp;quot; tells IQ-TREE 3 to perform automatic model selection using &amp;quot;ModelFinder Plus&amp;quot; before reconstructing the tree.&lt;br /&gt;
&lt;br /&gt;
: When the run has finished, inspect the output written to the screen and to the file ending in &amp;quot;.iqtree&amp;quot;. Note that IQ-TREE 3 reports several model-selection criteria in the output. The line near the top of the ModelFinder section states which criterion was used to choose the final model. Locate the substitution model selected by IQ-TREE 3 and the log likelihood of the best tree.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Question: &#039;&#039;&#039;Which substitution model was selected by IQ-TREE 3, and according to which information criterion was it selected? What was the log likelihood of the best tree? Compare this to the earlier jModelTest2 + PAUP analysis. Is the selected model exactly the same? Is the likelihood exactly the same? Briefly suggest one reason why the two analyses might not give identical results.&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
== Detection of positively selected sites in gp120 ==&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Question 7&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
: There is much more to phylogenetic analyses than merely reconstructing trees. One interesting result of probabilistic methods is that the parameters of a model are estimated as part of the optimization procedure. This means that once such a model has been fitted to the data, it is possible to investigate these estimated parameter values to learn features about the evolutionary history of the sequences under investigation. In the present example we will focus on whether we can detect positively selected sites in our data set, defined as sites where the dN/dS ratio is larger than 1. We do that by using a codon substitution model where the dN/dS ratio is one of its parameters.&lt;br /&gt;
&lt;br /&gt;
: A further strength of the probabilistic approach is that you get a probabilistic measure of how well any model fits the data. This means you can use a stringent approach to determine which model fits the data best. In this framework one uses likelihoods (probabilities of data given model) to determine which model fits the data best. As you saw above, it is for instance possible to compute AIC values and weights (model probabilities) from the likelihood values of fitted models. Since each model essentially corresponds to a hypothesis about the evolutionary history of the data, we can thus use a stringent statistical approach to decide which hypothesis best describes our data.&lt;br /&gt;
&lt;br /&gt;
: In outline, you will now use the following steps to investigate whether there is any evidence for positively selected codons in your data set:&lt;br /&gt;
&lt;br /&gt;
:* Fit model M1, which assumes there are two classes of codons in the sequence: some with dN/dS &amp;lt; 1, some with dN/dS = 1.&lt;br /&gt;
:* Fit model M2, which assumes 3 distinct classes of codons: two with dN/dS ratios as for M1, and one extra class with dN/dS &amp;gt; 1.&lt;br /&gt;
:* Assess the strength of evidence for the two models using AIC-based model probabilities&lt;br /&gt;
:* If M2 is better: identify the positively selected codons&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Edit the parameter file&#039;&#039;&#039;&lt;br /&gt;
 nedit codeml.ctl &amp;amp;&lt;br /&gt;
: The file &amp;quot;codeml.ctl&amp;quot; contains several settings that control how &#039;&#039;&#039;codeml&#039;&#039;&#039; is run. Before continuing, edit the file and save it so that the following settings have these values. Any text appearing after an asterisk (*) on a line is only a comment, and you do not need to change it.&lt;br /&gt;
&lt;br /&gt;
 &#039;&#039;&#039;seqfile =  gp120align.fasta&#039;&#039;&#039;&lt;br /&gt;
: name of alignment file&lt;br /&gt;
&lt;br /&gt;
 &#039;&#039;&#039;treefile =  gp120.nexus.treefile&#039;&#039;&#039;&lt;br /&gt;
: name of tree file produced by IQ-TREE 3&lt;br /&gt;
&lt;br /&gt;
 &#039;&#039;&#039;seqtype = 1&#039;&#039;&#039;&lt;br /&gt;
: tells the program that the data consist of coding DNA&lt;br /&gt;
&lt;br /&gt;
 &#039;&#039;&#039;NSsites = 1 2&#039;&#039;&#039;&lt;br /&gt;
: tells the program to analyze models M1 and M2&lt;br /&gt;
&lt;br /&gt;
 &#039;&#039;&#039;cleandata = 0&#039;&#039;&#039;&lt;br /&gt;
: tells the program to keep positions with gaps&lt;br /&gt;
&lt;br /&gt;
: &#039;&#039;&#039;Important:&#039;&#039;&#039; codeml requires a phylogenetic tree as input. The tree supplies the topology for the codon-model analysis, while codeml estimates the branch lengths and the remaining model parameters itself. In this exercise, use the tree produced by &#039;&#039;&#039;IQ-TREE 3&#039;&#039;&#039; (&#039;&#039;&#039;gp120.nexus.treefile&#039;&#039;&#039;) as input.&lt;br /&gt;
&lt;br /&gt;
: The settings entered by us will cause codeml to analyze two hypotheses about dN/dS ratios. M1 says there are two classes of codons with different dN/dS ratios in the sequence: one class with dN/dS &amp;lt; 1 (codons under purifying or negative selection), and one class with dN/dS = 1 (no selection - neutrally evolving sites). M2 says there are 3 distinct dN/dS ratios for different sites in the sequence: one class with dN/dS &amp;lt; 1, one class with dN/dS = 1 (these are the same type of classes as for M1), and one class with dN/dS &amp;gt; 1 (corresponding to sites under positive selection). The value of the dN/dS ratios (for those classes that have dN/dS &amp;lt; 1 or dN/dS &amp;gt; 1), the fraction of sites belonging to each class, and the position of sites belonging to each class, are unknown at first and will be determined during the analysis.&lt;br /&gt;
&lt;br /&gt;
: (Regarding &amp;quot;cleandata&amp;quot; setting: &amp;quot;cleandata = 1&amp;quot; would cause codeml to discard any alignment columns with gaps or ambiguity symbols. For alignments with many sequences this can lead to discarding the majority of sites and I would therefore recommend not using that setting. At the same time you do want to be wary of columns where most sequences have gaps, since inference of selection for these will be very uncertain. It might be better to discard such columns from an alignment before doing the codeml analysis.)&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Start the analysis&#039;&#039;&#039;&lt;br /&gt;
 codeml&lt;br /&gt;
: This will start the codeml program using the settings in the file codeml.ctl. Depending on your computer, this will take some minutes to finish. (You may be able to see how the optimization procedure results in progressively better fits: the likelihood increases, meaning that negative log-likelihood decreases, as the fit improves.)&lt;br /&gt;
&lt;br /&gt;
: &#039;&#039;&#039;Note:&#039;&#039;&#039; On some systems the current version of codeml (4.10.10) may print an error message near the end of the run, even though the relevant results have already been written to &#039;&#039;&#039;selection.results&#039;&#039;&#039;. If this happens, inspect &#039;&#039;&#039;selection.results&#039;&#039;&#039; before assuming that the run failed completely.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Inspect result file:&#039;&#039;&#039;&lt;br /&gt;
: Wait for the run to finish, and then look at the result file:&lt;br /&gt;
 nedit selection.results &amp;amp;&lt;br /&gt;
: This file contains a wealth of information concerning your analysis. You may want to turn off line-wrapping to more clearly see the structure of the file (there will be some long lines that will otherwise wrap around). The top part of the file gives an overview of your sequences, codon usage and nucleotide frequencies. You can ignore this information for now, and move on to the interesting part, namely the model likelihoods and parameter values.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Find likelihood, and number of free parameters for model M1&#039;&#039;&#039;&lt;br /&gt;
 Search ==&amp;gt; Find... ==&amp;gt; enter &amp;quot;Model 1&amp;quot; and click Find&lt;br /&gt;
: You are now in the region of the result file where the model likelihoods and parameter estimates are noted. Now locate a line that looks a bit like the one shown below:&lt;br /&gt;
 lnL(ntime: 72  np: 74):  -4242.470345     +0.000000&lt;br /&gt;
: Identify the number of &amp;quot;free parameters&amp;quot;, K, used in model M1: this is indicated by &amp;quot;np&amp;quot;, and is 74 in the example shown above (most of these parameters are branch lengths in the tree; specifically, the number of branch length parameters is indicated by &amp;quot;ntime&amp;quot;, and is 72 in this example). Also note the log-likelihood of the fitted model. This is the number right after the parenthesis, and is -4242.470345 in the example here.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Question: &#039;&#039;&#039;What are the values of K and lnL for model M1?&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Question 8&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Find dN/dS ratios and codon class proportions for model M1&#039;&#039;&#039;&lt;br /&gt;
: Scroll down a few lines until you get to a small table similar to this:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
      dN/dS for site classes (K=2)&lt;br /&gt;
      p:   0.75111  0.24889&lt;br /&gt;
      w:   0.06583  1.00000&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
: This gives a summary of the dN/dS ratios that were found in the data set. The line starting &amp;quot;w:&amp;quot; lists the two dN/dS ratios that were found (in this case 0.06583 and 1.00000 - the last one was pre-specified by us as part of the model and was therefore not a free parameter). In this context &amp;quot;w&amp;quot; means &amp;quot;omega&amp;quot;, which is the symbol typically used to represent the dN/dS rate ratio. The line starting &amp;quot;p:&amp;quot; gives the proportion of codon sites belonging to each of the dN/dS ratio classes (in the example above approximately 75% belong to the first class, while 25% of all sites belong to the class having dN/dS = 1.00000).&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Question: &#039;&#039;&#039;What are the dN/dS value (w) and proportion (p) of sites for both classes? Report the following values: p(class1), w(class1), p(class2), w(class2)&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Question 9&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Find likelihood, and K for model M2&#039;&#039;&#039;&lt;br /&gt;
: Scroll past the M1 output until you get to the results for model M2.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Question: &#039;&#039;&#039;What are the values of K and lnL for model M2?&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Question 10&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Find dN/dS ratios and codon class proportions for model M2&#039;&#039;&#039;&lt;br /&gt;
: Now scroll down a few lines until you get to a small table similar to the one you examined for M1 before. For this model there are 3 separate classes of codons.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Question: &#039;&#039;&#039;What are the dN/dS value (w) and proportion (p) of sites for all three classes? Report these values: p(class1), w(class1), p(class2), w(class2), p(class3), w(class3)&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Question 11&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Assess strength of evidence for models M1 and M2&#039;&#039;&#039;&lt;br /&gt;
: M2 will always have a better (higher) log-likelihood than model M1 because M2 has more free parameters, and M1 is nested within M2. You should now use the recipe given above to compute AIC values and model probabilities for M1 and M2.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Question: &#039;&#039;&#039;Report: AIC, ΔAIC, weight (model probability) for M1 and M2&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Question 12: &#039;&#039;&#039;Is M2 better than M1? Based on this comparison, is there evidence that some codon sites in gp120 have dN/dS &amp;gt; 1?&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Question 13&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Examine list of positively selected sites&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
: If your M2 is clearly better than M1, then you have evidence for the existence of positively selected sites in the gp120 gene. Now scroll down to the end of the result file and locate the &amp;quot;Bayes Empirical Bayes (BEB) analysis&amp;quot; table. Note: use the BEB table, not the Naive Empirical Bayes (NEB) table. (It is not important what the distinction is in this context, but if you are interested: NEB ignores the fact that there is uncertainty about maximum likelihood estimates, especially for smaller data sets - for instance w for some codon is perhaps not exactly 3.046, but could lie in a region around that value - whereas [https://pubmed.ncbi.nlm.nih.gov/15689528/ BEB accounts for that uncertainty].)&lt;br /&gt;
&lt;br /&gt;
: This table lists codon positions, the posterior probability that each site belongs to the positively selected class (that is, the class with dN/dS &amp;gt; 1), and the estimated mean value of w for the site.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Question: &#039;&#039;&#039;List all sites having more than 95% probability of belonging to the positively selected class.&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Question 14&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
: Consider your results from Questions 10–13. In model M2, only a fraction of sites belong to the positively selected class, while many sites belong to classes with dN/dS ≤ 1.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Question: &#039;&#039;&#039;What does this suggest about the overall pattern of selection acting on gp120? Explain briefly why one might expect a mixture of negatively selected, neutral, and positively selected sites in this gene.&lt;/div&gt;</summary>
		<author><name>Gorm</name></author>
	</entry>
	<entry>
		<id>https://teaching.healthtech.dtu.dk/22115/index.php?title=Model_selection&amp;diff=281</id>
		<title>Model selection</title>
		<link rel="alternate" type="text/html" href="https://teaching.healthtech.dtu.dk/22115/index.php?title=Model_selection&amp;diff=281"/>
		<updated>2026-04-15T09:48:45Z</updated>

		<summary type="html">&lt;p&gt;Gorm: /* Detection of positively selected sites in gp120 */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;This exercise is part of the course  [[22115_-_Computational_Molecular_Evolution|Computational Molecular Evolution (22115)]].&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Overview ==&lt;br /&gt;
&lt;br /&gt;
: In this exercise you are going to investigate features of HIV-1 evolution. You will do this by analyzing a large set of env-genes from HIV-1, subtype B. specifically, the DNA sequences analyzed here correspond to a region surrounding the hypervariable V3 region of the gp120 protein.&lt;br /&gt;
&lt;br /&gt;
: Like other retroviruses, particles of HIV are made up of 2 copies of a single-stranded RNA genome packaged inside a protein core, or capsid. The core particle also contains viral proteins that are essential for the early steps of the virus life cycle, such as reverse transcription and integration. A lipid envelope, derived from the infected cell, surrounds the core particle. Embedded in this envelope are the surface glycoproteins of HIV: gp120 and gp41. The gp120 protein is crucial for binding of the virus particle to target cells, while gp41 is important for the subsequent fusion event. It is the specific affinity of gp120 for the CD4 protein that targets HIV to those cells of the immune system that express CD4 on their surface (e.g., T-helper lymphocytes, monocytes, and macrophages).&lt;br /&gt;
&lt;br /&gt;
: The role gp120 plays in infection and the fact that it is situated on the surface of the HIV particle, means it is an obvious target for the immune response. That means that there may be a considerable selective pressure on gp120 for creating immune-escape mutants, where amino acids in the gp120 epitopes have been substituted. In this exercise you will construct a maximum likelihood tree that we will subsequently use to investigate whether you can detect such a selective pressure on parts of gp120, again using maximum likelihood methods.&lt;br /&gt;
&lt;br /&gt;
: One major goal with the exercise is to introduce you to statistically based methods for assessing the strength of evidence for a set of alternative hypotheses about some biological system of interest. The model selection method we will use is AIC (Akaike Information Criterion), based on which you will compute model probabilities. A second goal is to make you aware that phylogenetic analysis is not only about constructing trees, but that it is also a useful framework for analyzing biological questions more generally.&lt;br /&gt;
&lt;br /&gt;
: Specifically, you will&lt;br /&gt;
&lt;br /&gt;
:# perform a multiple alignment of gp120 DNA sequences taking protein-level information into account (using revtrans).&lt;br /&gt;
:# select a suitable nucleotide substitution model (using jmodeltest2)&lt;br /&gt;
:# construct a phylogenetic tree (using PAUP).&lt;br /&gt;
:# try to detect positively selected sites in gp120 (using PAML).&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
== Recipe for computing AIC values and model probabilities ==&lt;br /&gt;
&lt;br /&gt;
: Later in today&#039;s exercise you will be asked to compute AIC values and model probabilities. Return to this section and follow the instructions when you need to do so.&lt;br /&gt;
&lt;br /&gt;
:# Fit a set of models to your data, note the maximized log likelihoods (lnL) and the number of free parameters (K) for each model in the investigated set. The models you fit should represent a plausible and comprehensive set of hypotheses about your data.&lt;br /&gt;
:# Compute AIC for each of the models: &#039;&#039;&#039;AIC = -2 x lnL + 2K&#039;&#039;&#039;.  &amp;lt;br&amp;gt;For example: a model with lnL = -2010 and K = 5 will have AIC = -2 x -2010 + 2 x 5 = 4030.&lt;br /&gt;
:# Identify the model with the smallest AIC (this is the best model in the set). We will call the AIC for this model &#039;&#039;&#039;&amp;quot;AICmin&amp;quot;&#039;&#039;&#039;.&lt;br /&gt;
:# Compute the &amp;quot;ΔAIC&amp;quot; values for each model: &#039;&#039;&#039;ΔAIC = AIC - AICmin&#039;&#039;&#039; &amp;lt;br&amp;gt;For each model subtract the minimum AIC value. The best model will have a ΔAIC of zero. The rest of the models will have positive ΔAICs.&lt;br /&gt;
:# For each model compute the following quantity: &#039;&#039;&#039;numerator = exp(-0.5 x ΔAIC)&#039;&#039;&#039; &amp;lt;br&amp;gt;For example, a model with ΔAIC=4.2 will have numerator = exp(-0.5 x 4.2) = exp(-2.1) = 0.1225. Also compute the &#039;&#039;&#039;sum&#039;&#039;&#039; of the numerator values for all models.&lt;br /&gt;
:# Finally, the model probabilities (also called model weights) for each model are found as:&amp;lt;br&amp;gt; &#039;&#039;&#039;P(model) = numerator / sum&#039;&#039;&#039; &amp;lt;br&amp;gt;For example, if sum = 3.75 and a model has numerator = 1.3, then it has P(model) = 1.3 / 3.75 = 0.35&lt;br /&gt;
&lt;br /&gt;
: You may want to keep track of the computations by constructing a table along the following lines:&lt;br /&gt;
[[File:Molevol-Downloads-aictable.png|700px]]&lt;br /&gt;
&lt;br /&gt;
: Note that model probabilities can also be computed using Bayesian methods. One advantage of Bayesian methods over AIC is that instead of relying on a point estimate, uncertainty about parameter values is accounted for by integrating over all possible values (typically using MCMC).&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
== Getting started ==&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Create working directory, copy files&#039;&#039;&#039;&lt;br /&gt;
: In the command below: Instead of /path/to/molevol enter the path to the directory where you have placed your course files (for instance cd /Users/bob/Documents/molevol, or cd /home/student/molevol).&lt;br /&gt;
 cd /path/to/molevol&lt;br /&gt;
 mkdir modelselect&lt;br /&gt;
 cd modelselect&lt;br /&gt;
 cp ../data/gp120.fasta ./gp120.fasta&lt;br /&gt;
 cp ../data/codeml.ctl ./codeml.ctl&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Have a look at the DNA data file:&#039;&#039;&#039;&lt;br /&gt;
 nedit gp120.fasta &amp;amp;&lt;br /&gt;
: The file contains several DNA sequences from HIV-1, subtype B. The sequences are approximately 500 bp long, and correspond to a region surrounding the hypervariable V3 region in the gene encoding gp120. Close the nedit window when you&#039;ve had a look.&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
== Protein-guided alignment of coding DNA ==&lt;br /&gt;
&lt;br /&gt;
: There are three reasons why it is preferable to align coding DNA at the amino acid level rather than directly as nucleotides. First, the DNA alphabet has only four letters compared to 20 amino acids, which means that even unrelated DNA sequences will share roughly 25% identity by chance alone, making it much harder to distinguish true homology from noise. Second, protein alignments benefit from empirical substitution matrices such as BLOSUM-62, which capture the fact that amino acid replacements tend to be conservative. Equivalent matrices for DNA are far less informative because nucleotide substitution patterns vary strongly between genes and organisms. Third, synonymous substitutions accumulate faster than non-synonymous ones, so DNA sequences diverge more rapidly than the proteins they encode. Taken together, these factors mean that the phylogenetic signal in a DNA alignment erodes much faster than in the corresponding protein alignment.&lt;br /&gt;
&lt;br /&gt;
: However, while protein-level alignment gives us the most reliable homology signal, it discards exactly the information we need for studying selection: the synonymous substitutions. Estimating the ratio of non-synonymous to synonymous substitution rates (dN/dS) requires a codon-level alignment where analogous codon positions are properly lined up. We would therefore like to construct a multiple alignment at the DNA level, but using information at the protein level, and the RevTrans server does exactly that.&lt;br /&gt;
&lt;br /&gt;
: &#039;&#039;&#039;RevTrans&#039;&#039;&#039; takes as input an unaligned set of DNA sequences, automatically translates them to the equivalent amino acid sequences, constructs a multiple alignment of the protein sequences, and finally uses the protein alignment as a template for constructing a multiple DNA alignment. Because each amino acid maps back to its original codon, gaps are always inserted in groups of three and codon boundaries are preserved throughout the alignment. This is essential for the dN/dS analyses we will carry out in this exercise: if codon positions are misaligned, synonymous and non-synonymous changes cannot be correctly distinguished.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Construct RevTrans alignment&#039;&#039;&#039;&lt;br /&gt;
:* Open RevTrans server page: https://services.healthtech.dtu.dk/services/RevTrans-2.0/&lt;br /&gt;
:* On the RevTrans page: Choose the file gp120.fasta as input (or copy and paste the sequence into the sequence window)&lt;br /&gt;
:* Click the &amp;quot;Submit query&amp;quot; button&lt;br /&gt;
:* When the alignment is done you may have to click link named &amp;quot;here&amp;quot; to go to results page&lt;br /&gt;
:* Download DNA alignment, by right-clicking the link for &amp;quot;Download alignment in FASTA format&amp;quot;, and choosing &amp;quot;Save link as...&amp;quot; (save file under the name gp120align.fasta and make sure to save the file in the directory modelselect).&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Convert alignment to NEXUS format&#039;&#039;&#039;&lt;br /&gt;
: Convert the fasta file to NEXUS format and save file in the modelselect directory under the name gp120.nexus&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
== Model selection using AIC and jModelTest2 ==&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Question 1&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
: As part of the present analysis we are going to build a phylogenetic tree based on the DNA alignment constructed above. We will construct the tree using maximum likelihood, but to do that we first have to decide which substitution model we want to use. Specifically, we are interested in selecting a model that achieves a good balance between fit to the data and number of parameters. We will investigate this issue by fitting a set of 56 different models to our data and then comparing them using AIC.&lt;br /&gt;
&lt;br /&gt;
: In the first part of the exercise, however, you will only compare three closely related models by hand. The purpose of this restricted comparison is not primarily to identify the globally best model, but to understand how AIC, ΔAIC, and Akaike weights are computed and interpreted. Because only three models are included in this manual comparison, the weights you compute will describe relative support &#039;&#039;&#039;within this restricted set only&#039;&#039;&#039;.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Start jmodeltest2&#039;&#039;&#039;&lt;br /&gt;
 jmodeltest&lt;br /&gt;
&#039;&#039;&#039;Load data set&#039;&#039;&#039;&lt;br /&gt;
:* File -&amp;gt; Load DNA alignment -&amp;gt; File Format -&amp;gt; Select &amp;quot;All files&amp;quot;&lt;br /&gt;
:* Navigate to gp120.nexus and load it&lt;br /&gt;
&#039;&#039;&#039;Fit 56 models&#039;&#039;&#039;&lt;br /&gt;
:* Analysis -&amp;gt; Compute likelihood scores&lt;br /&gt;
:* Select &amp;quot;7&amp;quot; under &amp;quot;Number of substitution schemes&amp;quot;&lt;br /&gt;
:* Select &amp;quot;Fixed BIONJ-JC&amp;quot; under &amp;quot;Base tree for likelihood calculations&amp;quot;&lt;br /&gt;
:* Click &amp;quot;Compute likelihoods&amp;quot;&lt;br /&gt;
&lt;br /&gt;
: This causes jmodeltest2 to perform the following actions: first a neighbor joining tree is constructed using the Jukes and Cantor model. Then the tree is fixed and used as the basis for fitting a set of 56 different models to the data. For each model, the estimated model parameters and the negative log-likelihood are recorded. In addition to varying sets of substitution rate parameters (JC, K2P, ...), some of these models also include extra parameters that take into account the presence of different rates between sites. This is done in two ways: (1) by fitting a gamma distribution of rates (&amp;quot;+G&amp;quot;), and (2) by allowing for a proportion of constant (&amp;quot;invariable&amp;quot;) sites (&amp;quot;+I&amp;quot;).&lt;br /&gt;
&lt;br /&gt;
: Wait until jmodeltest2 is done fitting all 56 models (this will take a little while depending on your computer).&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Inspect result&#039;&#039;&#039;&lt;br /&gt;
:* Results -&amp;gt; Show results table&lt;br /&gt;
: For each model this table lists the negative log-likelihood (&amp;quot;-lnL&amp;quot;), the number of parameters (&amp;quot;p&amp;quot; - note this includes branch lengths), and estimates of all model parameters (excluding branch lengths).&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Manually compute Akaike weights for three substitution models&#039;&#039;&#039;&lt;br /&gt;
: Use AIC-based model comparison to investigate which of the following three substitution models are best supported as descriptions of how the sequences have evolved:&lt;br /&gt;
:* Jukes and Cantor with fraction of invariant sites (JC+I)&lt;br /&gt;
:* Jukes and Cantor with gamma-distributed rates over sites (JC+G)&lt;br /&gt;
:* Jukes and Cantor with invariant sites and gamma-distributed rates (JC+I+G)&lt;br /&gt;
: Before you can do the computation you need to know the log likelihood and the number of parameters for each model. Locate these values in the table for the JC+I, JC+G, and JC+I+G models, and write them down. Close the window with the result table when you are done.&lt;br /&gt;
&lt;br /&gt;
: Make sure to get the signs right: the values reported in the table are -lnL values, so you will need to reverse the sign to get the lnL (the lnL values you write down should be negative).&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Question: &#039;&#039;&#039;Use the recipe above to compute AIC, ΔAIC, and Akaike weight for the three models. Report the results in a table similar to the one shown above, and verify that the three weights sum to 1 (up to rounding error).&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Question 2&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
: Based on the Akaike weights, which of the three models has the strongest support? Is the support strongly concentrated on one model, or is there still substantial support for one or both of the others?&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Question 3&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
: What you just did manually for JC+I, JC+G and JC+I+G, jmodeltest2 can do automatically for the full set of 56 fitted models. Specifically, it uses the list of negative log likelihoods and parameter counts in the table to compute AIC values and Akaike weights for the full candidate set:&lt;br /&gt;
:* Analysis -&amp;gt; Do AIC calculations -&amp;gt;&lt;br /&gt;
:* Select &amp;quot;Write PAUP* block&amp;quot;&lt;br /&gt;
:* click &amp;quot;Do AIC calculations&amp;quot;&lt;br /&gt;
:* Results -&amp;gt; Show results table&lt;br /&gt;
:* Select &amp;quot;AIC&amp;quot; tab&lt;br /&gt;
:* &#039;&#039;&#039;IMPORTANT:&#039;&#039;&#039; SHIFT+click on the header of the &amp;quot;weight&amp;quot; column. This sorts the rows according to model weight, in descending order.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Question: &#039;&#039;&#039;What model was selected by modeltest based on the AIC values? (This will be the model with the highest weight - and lowest AIC - and will be in the first row of the results table after SHIFT-clicking the &amp;quot;weight&amp;quot; header).&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Question 4&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
: In the same AIC results table, now click (not SHIFT+click) the header of the &amp;quot;-lnL&amp;quot; column. In this table, a normal click sorts in ascending order, whereas SHIFT+click sorts in descending order. The model at the top will therefore be the one with the smallest value of &amp;quot;-lnL&amp;quot; (equivalently: the highest likelihood). Compare this model to the one with the highest Akaike weight.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Question: &#039;&#039;&#039;Is the model with the highest likelihood also the model with the highest Akaike weight? If not, explain briefly why these two rankings can differ.&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
== Phylogenetic tree inference ==&lt;br /&gt;
&lt;br /&gt;
=== Tree inference using the model selected by jModelTest2 (PAUP) ===&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Question 5&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
: Above, you used jModelTest2 to select a substitution model based on AIC. You will now use that model to construct a maximum likelihood tree in &#039;&#039;&#039;PAUP&#039;&#039;&#039;. The purpose of doing this in PAUP is mainly pedagogical: it makes the individual steps of model-based phylogenetic inference more explicit. In practice, one would often use a program such as IQ-TREE 3 (see next section).&lt;br /&gt;
&lt;br /&gt;
: Close the results table. In the main window you should now scroll up to the lines giving PAUP commands that implement the selected model. The command is enclosed between &amp;quot;BEGIN PAUP&amp;quot; and &amp;quot;END;&amp;quot; and should look something like this:&lt;br /&gt;
 Lset Base=(0.4064, [...]&lt;br /&gt;
: You will need to copy this command to a PAUP session in the next step.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Start PAUP&#039;&#039;&#039;&lt;br /&gt;
 paup&lt;br /&gt;
: Above, you used jModelTest2 to select the most suitable substitution model for the present data set. You will now use this model to construct a maximum likelihood tree. You will use PAUP for this purpose. (Note: it is possible to create a maximum likelihood tree, or even a model-averaged tree, directly from jModelTest2, but we will instead do it in PAUP in order to more clearly see each step that is taken.)&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Load alignment:&#039;&#039;&#039;&lt;br /&gt;
 execute gp120.nexus&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Set tree-building criterion to maximum likelihood&#039;&#039;&#039;&lt;br /&gt;
 set criterion=likelihood&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Set model parameters to winning estimates&#039;&#039;&#039;&lt;br /&gt;
: Above, you located a set of lines in the jModelTest2 output giving a PAUP command that sets the model parameters to the estimates found for the winning model. Copy and paste this lset command (without the BEGIN and END parts) into the window where PAUP is running.&lt;br /&gt;
&lt;br /&gt;
 PASTE LSET COMMAND FROM MODELTEST RUN HERE&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Find best tree using selected model&#039;&#039;&#039;&lt;br /&gt;
: Still in the PAUP window, enter the following command:&lt;br /&gt;
 hsearch swap=tbr start=nj&lt;br /&gt;
: This command causes PAUP to perform a heuristic search for the best maximum likelihood tree. Once an initial tree has been constructed, the heuristic search proceeds by rearrangements of the &amp;quot;tree bisection and reconnection&amp;quot; type (TBR). We are using the model selected by modeltest, and the parameter estimates found by modeltest for that model. You could also choose to estimate all the model parameters as part of this step (i.e., at the same time as finding the best tree), but fixing them improves speed tremendously. Finding the best tree should take at most a few minutes.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Save best tree to file&#039;&#039;&#039;&lt;br /&gt;
 savetrees format=newick brlens=yes file=gp120tree_paup.phy from=1 to=1&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Quit program&#039;&#039;&#039;&lt;br /&gt;
 quit&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Have a look at the tree:&#039;&#039;&#039;&lt;br /&gt;
: You have now produced an unrooted tree of the HIV sequences and saved it in the file gp120tree_paup.phy. Note that in this exercise we will not be interested in the tree as such - our focus is instead on finding positive selection at a subset of codon positions, and the tree is just something we need in order to fit the different codon models to the data. If you want to see the tree, you can do so with the following command:&lt;br /&gt;
 figtree gp120tree_paup.phy &amp;amp;&lt;br /&gt;
: There is no meaningful root placed on this tree, so you may want to choose the unrooted view (the third icon in the Layout section of the FigTree window). Close the FigTree window when you have had a look.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Question: &#039;&#039;&#039;Which substitution model did you use for the PAUP tree search, and what was the negative log likelihood of the best tree found under this model?&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
=== One-step model selection and tree inference using IQ-TREE 3 ===&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Question 6&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
: In real-life phylogenetic analysis one would often use a modern program such as &#039;&#039;&#039;IQ-TREE 3&#039;&#039;&#039;, which can carry out model selection and maximum likelihood tree reconstruction in a single analysis. You will now repeat the analysis using this more integrated approach.&lt;br /&gt;
&lt;br /&gt;
: Run IQ-TREE 3 on the alignment using the following command:&lt;br /&gt;
 iqtree3 -s gp120.nexus -m MFP&lt;br /&gt;
&lt;br /&gt;
: Here, &amp;quot;-m MFP&amp;quot; tells IQ-TREE 3 to perform automatic model selection using &amp;quot;ModelFinder Plus&amp;quot; before reconstructing the tree.&lt;br /&gt;
&lt;br /&gt;
: When the run has finished, inspect the output written to the screen and to the file ending in &amp;quot;.iqtree&amp;quot;. Note that IQ-TREE 3 reports several model-selection criteria in the output. The line near the top of the ModelFinder section states which criterion was used to choose the final model. Locate the substitution model selected by IQ-TREE 3 and the log likelihood of the best tree.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Question: &#039;&#039;&#039;Which substitution model was selected by IQ-TREE 3, and according to which information criterion was it selected? What was the log likelihood of the best tree? Compare this to the earlier jModelTest2 + PAUP analysis. Is the selected model exactly the same? Is the likelihood exactly the same? Briefly suggest one reason why the two analyses might not give identical results.&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
== Detection of positively selected sites in gp120 ==&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Question 7&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
: There is much more to phylogenetic analyses than merely reconstructing trees. One interesting result of probabilistic methods is that the parameters of a model are estimated as part of the optimization procedure. This means that once such a model has been fitted to the data, it is possible to investigate these estimated parameter values to learn features about the evolutionary history of the sequences under investigation. In the present example we will focus on whether we can detect positively selected sites in our data set, defined as sites where the dN/dS ratio is larger than 1. We do that by using a codon substitution model where the dN/dS ratio is one of its parameters.&lt;br /&gt;
&lt;br /&gt;
: A further strength of the probabilistic approach is that you get a probabilistic measure of how well any model fits the data. This means you can use a stringent approach to determine which model fits the data best. In this framework one uses likelihoods (probabilities of data given model) to determine which model fits the data best. As you saw above, it is for instance possible to compute AIC values and weights (model probabilities) from the likelihood values of fitted models. Since each model essentially corresponds to a hypothesis about the evolutionary history of the data, we can thus use a stringent statistical approach to decide which hypothesis best describes our data.&lt;br /&gt;
&lt;br /&gt;
: In outline, you will now use the following steps to investigate whether there is any evidence for positively selected codons in your data set:&lt;br /&gt;
&lt;br /&gt;
:* Fit model M1, which assumes there are two classes of codons in the sequence: some with dN/dS &amp;lt; 1, some with dN/dS = 1.&lt;br /&gt;
:* Fit model M2, which assumes 3 distinct classes of codons: two with dN/dS ratios as for M1, and one extra class with dN/dS &amp;gt; 1.&lt;br /&gt;
:* Assess the strength of evidence for the two models using AIC-based model probabilities&lt;br /&gt;
:* If M2 is better: identify the positively selected codons&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Edit the parameter file&#039;&#039;&#039;&lt;br /&gt;
 nedit codeml.ctl &amp;amp;&lt;br /&gt;
: The file &amp;quot;codeml.ctl&amp;quot; contains several settings that control how &#039;&#039;&#039;codeml&#039;&#039;&#039; is run. Before continuing, edit the file and save it so that the following settings have these values. Any text appearing after an asterisk (*) on a line is only a comment, and you do not need to change it.&lt;br /&gt;
&lt;br /&gt;
 &#039;&#039;&#039;seqfile =  gp120align.fasta&#039;&#039;&#039;&lt;br /&gt;
: name of alignment file&lt;br /&gt;
&lt;br /&gt;
 &#039;&#039;&#039;treefile =  gp120.nexus.treefile&#039;&#039;&#039;&lt;br /&gt;
: name of tree file produced by IQ-TREE 3&lt;br /&gt;
&lt;br /&gt;
 &#039;&#039;&#039;seqtype = 1&#039;&#039;&#039;&lt;br /&gt;
: tells the program that the data consist of coding DNA&lt;br /&gt;
&lt;br /&gt;
 &#039;&#039;&#039;NSsites = 1 2&#039;&#039;&#039;&lt;br /&gt;
: tells the program to analyze models M1 and M2&lt;br /&gt;
&lt;br /&gt;
 &#039;&#039;&#039;cleandata = 0&#039;&#039;&#039;&lt;br /&gt;
: tells the program to keep positions with gaps&lt;br /&gt;
&lt;br /&gt;
: &#039;&#039;&#039;Important:&#039;&#039;&#039; codeml requires a phylogenetic tree as input. The tree supplies the topology for the codon-model analysis, while codeml estimates the branch lengths and the remaining model parameters itself. In this exercise, use the tree produced by &#039;&#039;&#039;IQ-TREE 3&#039;&#039;&#039; (&#039;&#039;&#039;gp120.nexus.treefile&#039;&#039;&#039;) as input.&lt;br /&gt;
&lt;br /&gt;
: The settings entered by us will cause codeml to analyze two hypotheses about dN/dS ratios. M1 says there are two classes of codons with different dN/dS ratios in the sequence: one class with dN/dS &amp;lt; 1 (codons under purifying or negative selection), and one class with dN/dS = 1 (no selection - neutrally evolving sites). M2 says there are 3 distinct dN/dS ratios for different sites in the sequence: one class with dN/dS &amp;lt; 1, one class with dN/dS = 1 (these are the same type of classes as for M1), and one class with dN/dS &amp;gt; 1 (corresponding to sites under positive selection). The value of the dN/dS ratios (for those classes that have dN/dS &amp;lt; 1 or dN/dS &amp;gt; 1), the fraction of sites belonging to each class, and the position of sites belonging to each class, are unknown at first and will be determined during the analysis.&lt;br /&gt;
&lt;br /&gt;
: (Regarding &amp;quot;cleandata&amp;quot; setting: &amp;quot;cleandata = 1&amp;quot; would cause codeml to discard any alignment columns with gaps or ambiguity symbols. For alignments with many sequences this can lead to discarding the majority of sites and I would therefore recommend not using that setting. At the same time you do want to be wary of columns where most sequences have gaps, since inference of selection for these will be very uncertain. It might be better to discard such columns from an alignment before doing the codeml analysis.)&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Start the analysis&#039;&#039;&#039;&lt;br /&gt;
 codeml&lt;br /&gt;
: This will start the codeml program using the settings in the file codeml.ctl. Depending on your computer, this will take some minutes to finish. (You may be able to see how the optimization procedure results in progressively better fits: the likelihood increases, meaning that negative log-likelihood decreases, as the fit improves.)&lt;br /&gt;
&lt;br /&gt;
: &#039;&#039;&#039;Note:&#039;&#039;&#039; On some systems the current version of codeml (4.10.10) may print an error message near the end of the run, even though the relevant results have already been written to &#039;&#039;&#039;selection.results&#039;&#039;&#039;. If this happens, inspect &#039;&#039;&#039;selection.results&#039;&#039;&#039; before assuming that the run failed completely.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Inspect result file:&#039;&#039;&#039;&lt;br /&gt;
: Wait for the run to finish, and then look at the result file:&lt;br /&gt;
 nedit selection.results &amp;amp;&lt;br /&gt;
: This file contains a wealth of information concerning your analysis. You may want to turn off line-wrapping to more clearly see the structure of the file (there will be some long lines that will otherwise wrap around). The top part of the file gives an overview of your sequences, codon usage and nucleotide frequencies. You can ignore this information for now, and move on to the interesting part, namely the model likelihoods and parameter values.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Find likelihood, and number of free parameters for model M1&#039;&#039;&#039;&lt;br /&gt;
 Search ==&amp;gt; Find... ==&amp;gt; enter &amp;quot;Model 1&amp;quot; and click Find&lt;br /&gt;
: You are now in the region of the result file where the model likelihoods and parameter estimates are noted. Now locate a line that looks a bit like the one shown below:&lt;br /&gt;
 lnL(ntime: 72  np: 74):  -4242.470345     +0.000000&lt;br /&gt;
: Identify the number of &amp;quot;free parameters&amp;quot;, K, used in model M1: this is indicated by &amp;quot;np&amp;quot;, and is 74 in the example shown above (most of these parameters are branch lengths in the tree; specifically, the number of branch length parameters is indicated by &amp;quot;ntime&amp;quot;, and is 72 in this example). Also note the log-likelihood of the fitted model. This is the number right after the parenthesis, and is -4242.470345 in the example here.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Question: &#039;&#039;&#039;What are the values of K and lnL for model M1?&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Question 8&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Find dN/dS ratios and codon class proportions for model M1&#039;&#039;&#039;&lt;br /&gt;
: Scroll down a few lines until you get to a small table similar to this:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
      dN/dS for site classes (K=2)&lt;br /&gt;
      p:   0.75111  0.24889&lt;br /&gt;
      w:   0.06583  1.00000&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
: This gives a summary of the dN/dS ratios that were found in the data set. The line starting &amp;quot;w:&amp;quot; lists the two dN/dS ratios that were found (in this case 0.06583 and 1.00000 - the last one was pre-specified by us as part of the model and was therefore not a free parameter). In this context &amp;quot;w&amp;quot; means &amp;quot;omega&amp;quot;, which is the symbol typically used to represent the dN/dS rate ratio. The line starting &amp;quot;p:&amp;quot; gives the proportion of codon sites belonging to each of the dN/dS ratio classes (in the example above approximately 75% belong to the first class, while 25% of all sites belong to the class having dN/dS = 1.00000).&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Question: &#039;&#039;&#039;What are the dN/dS value (w) and proportion (p) of sites for both classes? Report the following values: p(class1), w(class1), p(class2), w(class2)&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Question 9&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Find likelihood, and K for model M2&#039;&#039;&#039;&lt;br /&gt;
: Scroll past the M1 output until you get to the results for model M2.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Question: &#039;&#039;&#039;What are the values of K and lnL for model M2?&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Question 10&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Find dN/dS ratios and codon class proportions for model M2&#039;&#039;&#039;&lt;br /&gt;
: Now scroll down a few lines until you get to a small table similar to the one you examined for M1 before. For this model there are 3 separate classes of codons.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Question: &#039;&#039;&#039;What are the dN/dS value (w) and proportion (p) of sites for all three classes? Report these values: p(class1), w(class1), p(class2), w(class2), p(class3), w(class3)&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Question 11&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Assess strength of evidence for models M1 and M2&#039;&#039;&#039;&lt;br /&gt;
: M2 will always have a better (higher) log-likelihood than model M1 because M2 has more free parameters, and M1 is nested within M2. You should now use the recipe given above to compute AIC values and model probabilities for M1 and M2.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Question: &#039;&#039;&#039;Report: AIC, ΔAIC, weight (model probability) for M1 and M2&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Question 12: &#039;&#039;&#039;Is M2 better than M1?&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Question 13&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Examine list of positively selected sites&#039;&#039;&#039;&lt;br /&gt;
: If your M2 is clearly better than M1, then you have evidence for the existence of positively selected sites in the gp120 gene. Now scroll down to the end of the result file and locate a list similar to the one below. Note: this is the &amp;quot;Bayes Empirical Bayes&amp;quot; table, not the &amp;quot;Naive Empirical Bayes&amp;quot; table.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Bayes Empirical Bayes (BEB) analysis&lt;br /&gt;
Positively selected sites&lt;br /&gt;
&lt;br /&gt;
         Prob(w&amp;gt;1)     mean w&lt;br /&gt;
&lt;br /&gt;
    25 A 0.959*        3.133 +- 0.769&lt;br /&gt;
    27 P 0.906         2.990 +- 0.877&lt;br /&gt;
    56 K 0.987*        3.197 +- 0.687&lt;br /&gt;
    59 V 0.915         3.032 +- 0.873&lt;br /&gt;
    78 R 0.637         2.351 +- 1.129&lt;br /&gt;
    88 K 0.573         2.148 +- 1.077&lt;br /&gt;
    95 V 0.925         3.046 +- 0.843&lt;br /&gt;
    ...&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
: It is not important what the distinction is in this context, but very briefly: NEB ignores the fact that there is uncertainty about maximum likelihood estimates, especially for smaller data sets (for instance w for some codon is perhaps not exactly 3.046, but could lie in a region around that value), whereas [https://pubmed.ncbi.nlm.nih.gov/15689528/ BEB accounts for that uncertainty].&lt;br /&gt;
&lt;br /&gt;
: This gives you a list of which residues (if any) were found to belong to the positively selected dN/dS class. Also listed is the probability that the site really is in the codon class where dN/dS &amp;gt; 1, and a weighted average of the w at the site.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Question: &#039;&#039;&#039;List all sites having more than 95% probability of belonging to the positively selected class.&lt;/div&gt;</summary>
		<author><name>Gorm</name></author>
	</entry>
	<entry>
		<id>https://teaching.healthtech.dtu.dk/22115/index.php?title=Model_selection&amp;diff=280</id>
		<title>Model selection</title>
		<link rel="alternate" type="text/html" href="https://teaching.healthtech.dtu.dk/22115/index.php?title=Model_selection&amp;diff=280"/>
		<updated>2026-04-15T09:42:36Z</updated>

		<summary type="html">&lt;p&gt;Gorm: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;This exercise is part of the course  [[22115_-_Computational_Molecular_Evolution|Computational Molecular Evolution (22115)]].&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Overview ==&lt;br /&gt;
&lt;br /&gt;
: In this exercise you are going to investigate features of HIV-1 evolution. You will do this by analyzing a large set of env-genes from HIV-1, subtype B. specifically, the DNA sequences analyzed here correspond to a region surrounding the hypervariable V3 region of the gp120 protein.&lt;br /&gt;
&lt;br /&gt;
: Like other retroviruses, particles of HIV are made up of 2 copies of a single-stranded RNA genome packaged inside a protein core, or capsid. The core particle also contains viral proteins that are essential for the early steps of the virus life cycle, such as reverse transcription and integration. A lipid envelope, derived from the infected cell, surrounds the core particle. Embedded in this envelope are the surface glycoproteins of HIV: gp120 and gp41. The gp120 protein is crucial for binding of the virus particle to target cells, while gp41 is important for the subsequent fusion event. It is the specific affinity of gp120 for the CD4 protein that targets HIV to those cells of the immune system that express CD4 on their surface (e.g., T-helper lymphocytes, monocytes, and macrophages).&lt;br /&gt;
&lt;br /&gt;
: The role gp120 plays in infection and the fact that it is situated on the surface of the HIV particle, means it is an obvious target for the immune response. That means that there may be a considerable selective pressure on gp120 for creating immune-escape mutants, where amino acids in the gp120 epitopes have been substituted. In this exercise you will construct a maximum likelihood tree that we will subsequently use to investigate whether you can detect such a selective pressure on parts of gp120, again using maximum likelihood methods.&lt;br /&gt;
&lt;br /&gt;
: One major goal with the exercise is to introduce you to statistically based methods for assessing the strength of evidence for a set of alternative hypotheses about some biological system of interest. The model selection method we will use is AIC (Akaike Information Criterion), based on which you will compute model probabilities. A second goal is to make you aware that phylogenetic analysis is not only about constructing trees, but that it is also a useful framework for analyzing biological questions more generally.&lt;br /&gt;
&lt;br /&gt;
: Specifically, you will&lt;br /&gt;
&lt;br /&gt;
:# perform a multiple alignment of gp120 DNA sequences taking protein-level information into account (using revtrans).&lt;br /&gt;
:# select a suitable nucleotide substitution model (using jmodeltest2)&lt;br /&gt;
:# construct a phylogenetic tree (using PAUP).&lt;br /&gt;
:# try to detect positively selected sites in gp120 (using PAML).&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
== Recipe for computing AIC values and model probabilities ==&lt;br /&gt;
&lt;br /&gt;
: Later in today&#039;s exercise you will be asked to compute AIC values and model probabilities. Return to this section and follow the instructions when you need to do so.&lt;br /&gt;
&lt;br /&gt;
:# Fit a set of models to your data, note the maximized log likelihoods (lnL) and the number of free parameters (K) for each model in the investigated set. The models you fit should represent a plausible and comprehensive set of hypotheses about your data.&lt;br /&gt;
:# Compute AIC for each of the models: &#039;&#039;&#039;AIC = -2 x lnL + 2K&#039;&#039;&#039;.  &amp;lt;br&amp;gt;For example: a model with lnL = -2010 and K = 5 will have AIC = -2 x -2010 + 2 x 5 = 4030.&lt;br /&gt;
:# Identify the model with the smallest AIC (this is the best model in the set). We will call the AIC for this model &#039;&#039;&#039;&amp;quot;AICmin&amp;quot;&#039;&#039;&#039;.&lt;br /&gt;
:# Compute the &amp;quot;ΔAIC&amp;quot; values for each model: &#039;&#039;&#039;ΔAIC = AIC - AICmin&#039;&#039;&#039; &amp;lt;br&amp;gt;For each model subtract the minimum AIC value. The best model will have a ΔAIC of zero. The rest of the models will have positive ΔAICs.&lt;br /&gt;
:# For each model compute the following quantity: &#039;&#039;&#039;numerator = exp(-0.5 x ΔAIC)&#039;&#039;&#039; &amp;lt;br&amp;gt;For example, a model with ΔAIC=4.2 will have numerator = exp(-0.5 x 4.2) = exp(-2.1) = 0.1225. Also compute the &#039;&#039;&#039;sum&#039;&#039;&#039; of the numerator values for all models.&lt;br /&gt;
:# Finally, the model probabilities (also called model weights) for each model are found as:&amp;lt;br&amp;gt; &#039;&#039;&#039;P(model) = numerator / sum&#039;&#039;&#039; &amp;lt;br&amp;gt;For example, if sum = 3.75 and a model has numerator = 1.3, then it has P(model) = 1.3 / 3.75 = 0.35&lt;br /&gt;
&lt;br /&gt;
: You may want to keep track of the computations by constructing a table along the following lines:&lt;br /&gt;
[[File:Molevol-Downloads-aictable.png|700px]]&lt;br /&gt;
&lt;br /&gt;
: Note that model probabilities can also be computed using Bayesian methods. One advantage of Bayesian methods over AIC is that instead of relying on a point estimate, uncertainty about parameter values is accounted for by integrating over all possible values (typically using MCMC).&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
== Getting started ==&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Create working directory, copy files&#039;&#039;&#039;&lt;br /&gt;
: In the command below: Instead of /path/to/molevol enter the path to the directory where you have placed your course files (for instance cd /Users/bob/Documents/molevol, or cd /home/student/molevol).&lt;br /&gt;
 cd /path/to/molevol&lt;br /&gt;
 mkdir modelselect&lt;br /&gt;
 cd modelselect&lt;br /&gt;
 cp ../data/gp120.fasta ./gp120.fasta&lt;br /&gt;
 cp ../data/codeml.ctl ./codeml.ctl&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Have a look at the DNA data file:&#039;&#039;&#039;&lt;br /&gt;
 nedit gp120.fasta &amp;amp;&lt;br /&gt;
: The file contains several DNA sequences from HIV-1, subtype B. The sequences are approximately 500 bp long, and correspond to a region surrounding the hypervariable V3 region in the gene encoding gp120. Close the nedit window when you&#039;ve had a look.&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
== Protein-guided alignment of coding DNA ==&lt;br /&gt;
&lt;br /&gt;
: There are three reasons why it is preferable to align coding DNA at the amino acid level rather than directly as nucleotides. First, the DNA alphabet has only four letters compared to 20 amino acids, which means that even unrelated DNA sequences will share roughly 25% identity by chance alone, making it much harder to distinguish true homology from noise. Second, protein alignments benefit from empirical substitution matrices such as BLOSUM-62, which capture the fact that amino acid replacements tend to be conservative. Equivalent matrices for DNA are far less informative because nucleotide substitution patterns vary strongly between genes and organisms. Third, synonymous substitutions accumulate faster than non-synonymous ones, so DNA sequences diverge more rapidly than the proteins they encode. Taken together, these factors mean that the phylogenetic signal in a DNA alignment erodes much faster than in the corresponding protein alignment.&lt;br /&gt;
&lt;br /&gt;
: However, while protein-level alignment gives us the most reliable homology signal, it discards exactly the information we need for studying selection: the synonymous substitutions. Estimating the ratio of non-synonymous to synonymous substitution rates (dN/dS) requires a codon-level alignment where analogous codon positions are properly lined up. We would therefore like to construct a multiple alignment at the DNA level, but using information at the protein level, and the RevTrans server does exactly that.&lt;br /&gt;
&lt;br /&gt;
: &#039;&#039;&#039;RevTrans&#039;&#039;&#039; takes as input an unaligned set of DNA sequences, automatically translates them to the equivalent amino acid sequences, constructs a multiple alignment of the protein sequences, and finally uses the protein alignment as a template for constructing a multiple DNA alignment. Because each amino acid maps back to its original codon, gaps are always inserted in groups of three and codon boundaries are preserved throughout the alignment. This is essential for the dN/dS analyses we will carry out in this exercise: if codon positions are misaligned, synonymous and non-synonymous changes cannot be correctly distinguished.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Construct RevTrans alignment&#039;&#039;&#039;&lt;br /&gt;
:* Open RevTrans server page: https://services.healthtech.dtu.dk/services/RevTrans-2.0/&lt;br /&gt;
:* On the RevTrans page: Choose the file gp120.fasta as input (or copy and paste the sequence into the sequence window)&lt;br /&gt;
:* Click the &amp;quot;Submit query&amp;quot; button&lt;br /&gt;
:* When the alignment is done you may have to click link named &amp;quot;here&amp;quot; to go to results page&lt;br /&gt;
:* Download DNA alignment, by right-clicking the link for &amp;quot;Download alignment in FASTA format&amp;quot;, and choosing &amp;quot;Save link as...&amp;quot; (save file under the name gp120align.fasta and make sure to save the file in the directory modelselect).&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Convert alignment to NEXUS format&#039;&#039;&#039;&lt;br /&gt;
: Convert the fasta file to NEXUS format and save file in the modelselect directory under the name gp120.nexus&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
== Model selection using AIC and jModelTest2 ==&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Question 1&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
: As part of the present analysis we are going to build a phylogenetic tree based on the DNA alignment constructed above. We will construct the tree using maximum likelihood, but to do that we first have to decide which substitution model we want to use. Specifically, we are interested in selecting a model that achieves a good balance between fit to the data and number of parameters. We will investigate this issue by fitting a set of 56 different models to our data and then comparing them using AIC.&lt;br /&gt;
&lt;br /&gt;
: In the first part of the exercise, however, you will only compare three closely related models by hand. The purpose of this restricted comparison is not primarily to identify the globally best model, but to understand how AIC, ΔAIC, and Akaike weights are computed and interpreted. Because only three models are included in this manual comparison, the weights you compute will describe relative support &#039;&#039;&#039;within this restricted set only&#039;&#039;&#039;.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Start jmodeltest2&#039;&#039;&#039;&lt;br /&gt;
 jmodeltest&lt;br /&gt;
&#039;&#039;&#039;Load data set&#039;&#039;&#039;&lt;br /&gt;
:* File -&amp;gt; Load DNA alignment -&amp;gt; File Format -&amp;gt; Select &amp;quot;All files&amp;quot;&lt;br /&gt;
:* Navigate to gp120.nexus and load it&lt;br /&gt;
&#039;&#039;&#039;Fit 56 models&#039;&#039;&#039;&lt;br /&gt;
:* Analysis -&amp;gt; Compute likelihood scores&lt;br /&gt;
:* Select &amp;quot;7&amp;quot; under &amp;quot;Number of substitution schemes&amp;quot;&lt;br /&gt;
:* Select &amp;quot;Fixed BIONJ-JC&amp;quot; under &amp;quot;Base tree for likelihood calculations&amp;quot;&lt;br /&gt;
:* Click &amp;quot;Compute likelihoods&amp;quot;&lt;br /&gt;
&lt;br /&gt;
: This causes jmodeltest2 to perform the following actions: first a neighbor joining tree is constructed using the Jukes and Cantor model. Then the tree is fixed and used as the basis for fitting a set of 56 different models to the data. For each model, the estimated model parameters and the negative log-likelihood are recorded. In addition to varying sets of substitution rate parameters (JC, K2P, ...), some of these models also include extra parameters that take into account the presence of different rates between sites. This is done in two ways: (1) by fitting a gamma distribution of rates (&amp;quot;+G&amp;quot;), and (2) by allowing for a proportion of constant (&amp;quot;invariable&amp;quot;) sites (&amp;quot;+I&amp;quot;).&lt;br /&gt;
&lt;br /&gt;
: Wait until jmodeltest2 is done fitting all 56 models (this will take a little while depending on your computer).&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Inspect result&#039;&#039;&#039;&lt;br /&gt;
:* Results -&amp;gt; Show results table&lt;br /&gt;
: For each model this table lists the negative log-likelihood (&amp;quot;-lnL&amp;quot;), the number of parameters (&amp;quot;p&amp;quot; - note this includes branch lengths), and estimates of all model parameters (excluding branch lengths).&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Manually compute Akaike weights for three substitution models&#039;&#039;&#039;&lt;br /&gt;
: Use AIC-based model comparison to investigate which of the following three substitution models are best supported as descriptions of how the sequences have evolved:&lt;br /&gt;
:* Jukes and Cantor with fraction of invariant sites (JC+I)&lt;br /&gt;
:* Jukes and Cantor with gamma-distributed rates over sites (JC+G)&lt;br /&gt;
:* Jukes and Cantor with invariant sites and gamma-distributed rates (JC+I+G)&lt;br /&gt;
: Before you can do the computation you need to know the log likelihood and the number of parameters for each model. Locate these values in the table for the JC+I, JC+G, and JC+I+G models, and write them down. Close the window with the result table when you are done.&lt;br /&gt;
&lt;br /&gt;
: Make sure to get the signs right: the values reported in the table are -lnL values, so you will need to reverse the sign to get the lnL (the lnL values you write down should be negative).&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Question: &#039;&#039;&#039;Use the recipe above to compute AIC, ΔAIC, and Akaike weight for the three models. Report the results in a table similar to the one shown above, and verify that the three weights sum to 1 (up to rounding error).&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Question 2&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
: Based on the Akaike weights, which of the three models has the strongest support? Is the support strongly concentrated on one model, or is there still substantial support for one or both of the others?&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Question 3&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
: What you just did manually for JC+I, JC+G and JC+I+G, jmodeltest2 can do automatically for the full set of 56 fitted models. Specifically, it uses the list of negative log likelihoods and parameter counts in the table to compute AIC values and Akaike weights for the full candidate set:&lt;br /&gt;
:* Analysis -&amp;gt; Do AIC calculations -&amp;gt;&lt;br /&gt;
:* Select &amp;quot;Write PAUP* block&amp;quot;&lt;br /&gt;
:* click &amp;quot;Do AIC calculations&amp;quot;&lt;br /&gt;
:* Results -&amp;gt; Show results table&lt;br /&gt;
:* Select &amp;quot;AIC&amp;quot; tab&lt;br /&gt;
:* &#039;&#039;&#039;IMPORTANT:&#039;&#039;&#039; SHIFT+click on the header of the &amp;quot;weight&amp;quot; column. This sorts the rows according to model weight, in descending order.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Question: &#039;&#039;&#039;What model was selected by modeltest based on the AIC values? (This will be the model with the highest weight - and lowest AIC - and will be in the first row of the results table after SHIFT-clicking the &amp;quot;weight&amp;quot; header).&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Question 4&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
: In the same AIC results table, now click (not SHIFT+click) the header of the &amp;quot;-lnL&amp;quot; column. In this table, a normal click sorts in ascending order, whereas SHIFT+click sorts in descending order. The model at the top will therefore be the one with the smallest value of &amp;quot;-lnL&amp;quot; (equivalently: the highest likelihood). Compare this model to the one with the highest Akaike weight.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Question: &#039;&#039;&#039;Is the model with the highest likelihood also the model with the highest Akaike weight? If not, explain briefly why these two rankings can differ.&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
== Phylogenetic tree inference ==&lt;br /&gt;
&lt;br /&gt;
=== Tree inference using the model selected by jModelTest2 (PAUP) ===&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Question 5&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
: Above, you used jModelTest2 to select a substitution model based on AIC. You will now use that model to construct a maximum likelihood tree in &#039;&#039;&#039;PAUP&#039;&#039;&#039;. The purpose of doing this in PAUP is mainly pedagogical: it makes the individual steps of model-based phylogenetic inference more explicit. In practice, one would often use a program such as IQ-TREE 3 (see next section).&lt;br /&gt;
&lt;br /&gt;
: Close the results table. In the main window you should now scroll up to the lines giving PAUP commands that implement the selected model. The command is enclosed between &amp;quot;BEGIN PAUP&amp;quot; and &amp;quot;END;&amp;quot; and should look something like this:&lt;br /&gt;
 Lset Base=(0.4064, [...]&lt;br /&gt;
: You will need to copy this command to a PAUP session in the next step.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Start PAUP&#039;&#039;&#039;&lt;br /&gt;
 paup&lt;br /&gt;
: Above, you used jModelTest2 to select the most suitable substitution model for the present data set. You will now use this model to construct a maximum likelihood tree. You will use PAUP for this purpose. (Note: it is possible to create a maximum likelihood tree, or even a model-averaged tree, directly from jModelTest2, but we will instead do it in PAUP in order to more clearly see each step that is taken.)&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Load alignment:&#039;&#039;&#039;&lt;br /&gt;
 execute gp120.nexus&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Set tree-building criterion to maximum likelihood&#039;&#039;&#039;&lt;br /&gt;
 set criterion=likelihood&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Set model parameters to winning estimates&#039;&#039;&#039;&lt;br /&gt;
: Above, you located a set of lines in the jModelTest2 output giving a PAUP command that sets the model parameters to the estimates found for the winning model. Copy and paste this lset command (without the BEGIN and END parts) into the window where PAUP is running.&lt;br /&gt;
&lt;br /&gt;
 PASTE LSET COMMAND FROM MODELTEST RUN HERE&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Find best tree using selected model&#039;&#039;&#039;&lt;br /&gt;
: Still in the PAUP window, enter the following command:&lt;br /&gt;
 hsearch swap=tbr start=nj&lt;br /&gt;
: This command causes PAUP to perform a heuristic search for the best maximum likelihood tree. Once an initial tree has been constructed, the heuristic search proceeds by rearrangements of the &amp;quot;tree bisection and reconnection&amp;quot; type (TBR). We are using the model selected by modeltest, and the parameter estimates found by modeltest for that model. You could also choose to estimate all the model parameters as part of this step (i.e., at the same time as finding the best tree), but fixing them improves speed tremendously. Finding the best tree should take at most a few minutes.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Save best tree to file&#039;&#039;&#039;&lt;br /&gt;
 savetrees format=newick brlens=yes file=gp120tree_paup.phy from=1 to=1&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Quit program&#039;&#039;&#039;&lt;br /&gt;
 quit&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Have a look at the tree:&#039;&#039;&#039;&lt;br /&gt;
: You have now produced an unrooted tree of the HIV sequences and saved it in the file gp120tree_paup.phy. Note that in this exercise we will not be interested in the tree as such - our focus is instead on finding positive selection at a subset of codon positions, and the tree is just something we need in order to fit the different codon models to the data. If you want to see the tree, you can do so with the following command:&lt;br /&gt;
 figtree gp120tree_paup.phy &amp;amp;&lt;br /&gt;
: There is no meaningful root placed on this tree, so you may want to choose the unrooted view (the third icon in the Layout section of the FigTree window). Close the FigTree window when you have had a look.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Question: &#039;&#039;&#039;Which substitution model did you use for the PAUP tree search, and what was the negative log likelihood of the best tree found under this model?&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
=== One-step model selection and tree inference using IQ-TREE 3 ===&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Question 6&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
: In real-life phylogenetic analysis one would often use a modern program such as &#039;&#039;&#039;IQ-TREE 3&#039;&#039;&#039;, which can carry out model selection and maximum likelihood tree reconstruction in a single analysis. You will now repeat the analysis using this more integrated approach.&lt;br /&gt;
&lt;br /&gt;
: Run IQ-TREE 3 on the alignment using the following command:&lt;br /&gt;
 iqtree3 -s gp120.nexus -m MFP&lt;br /&gt;
&lt;br /&gt;
: Here, &amp;quot;-m MFP&amp;quot; tells IQ-TREE 3 to perform automatic model selection using &amp;quot;ModelFinder Plus&amp;quot; before reconstructing the tree.&lt;br /&gt;
&lt;br /&gt;
: When the run has finished, inspect the output written to the screen and to the file ending in &amp;quot;.iqtree&amp;quot;. Note that IQ-TREE 3 reports several model-selection criteria in the output. The line near the top of the ModelFinder section states which criterion was used to choose the final model. Locate the substitution model selected by IQ-TREE 3 and the log likelihood of the best tree.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Question: &#039;&#039;&#039;Which substitution model was selected by IQ-TREE 3, and according to which information criterion was it selected? What was the log likelihood of the best tree? Compare this to the earlier jModelTest2 + PAUP analysis. Is the selected model exactly the same? Is the likelihood exactly the same? Briefly suggest one reason why the two analyses might not give identical results.&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
== Detection of positively selected sites in gp120 ==&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Question 7&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
: There is much more to phylogenetic analyses than merely reconstructing trees. One interesting result of probabilistic methods is that the parameters of a model are estimated as part of the optimization procedure. This means that once such a model has been fitted to the data, it is possible to investigate these estimated parameter values to learn features about the evolutionary history of the sequences under investigation. In the present example we will focus on whether we can detect positively selected sites in our data set, defined as sites where the dN/dS ratio is larger than 1. We do that by using a codon substitution model where the dN/dS ratio is one of its parameters.&lt;br /&gt;
&lt;br /&gt;
: A further strength of the probabilistic approach is that you get a probabilistic measure of how well any model fits the data. This means you can use a stringent approach to determine which model fits the data best. In this framework one uses likelihoods (probabilities of data given model) to determine which model fits the data best. As you saw above, it is for instance possible to compute AIC values and weights (model probabilities) from the likelihood values of fitted models. Since each model essentially corresponds to a hypothesis about the evolutionary history of the data, we can thus use a stringent statistical approach to decide which hypothesis best describes our data.&lt;br /&gt;
&lt;br /&gt;
: In outline, you will now use the following steps to investigate whether there is any evidence for positively selected codons in your data set:&lt;br /&gt;
&lt;br /&gt;
:* Fit model M1, which assumes there are two classes of codons in the sequence: some with dN/dS &amp;lt; 1, some with dN/dS = 1.&lt;br /&gt;
:* Fit model M2, which assumes 3 distinct classes of codons: two with dN/dS ratios as for M1, and one extra class with dN/dS &amp;gt; 1.&lt;br /&gt;
:* Assess the strength of evidence for the two models using AIC-based model probabilities&lt;br /&gt;
:* If M2 is better: identify the positively selected codons&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Edit the parameter file&#039;&#039;&#039;&lt;br /&gt;
 nedit codeml.ctl &amp;amp;&lt;br /&gt;
: The file &amp;quot;codeml.ctl&amp;quot; contains several settings that control how &#039;&#039;&#039;codeml&#039;&#039;&#039; is run. Before continuing, edit the file and save it so that the following settings have these values. Any text appearing after an asterisk (*) on a line is only a comment, and you do not need to change it.&lt;br /&gt;
&lt;br /&gt;
 &#039;&#039;&#039;seqfile =  gp120align.fasta&#039;&#039;&#039;&lt;br /&gt;
: name of alignment file&lt;br /&gt;
&lt;br /&gt;
 &#039;&#039;&#039;treefile =  gp120.nexus.treefile&#039;&#039;&#039;&lt;br /&gt;
: name of tree file produced by IQ-TREE 3&lt;br /&gt;
&lt;br /&gt;
 &#039;&#039;&#039;seqtype = 1&#039;&#039;&#039;&lt;br /&gt;
: tells the program that the data consist of coding DNA&lt;br /&gt;
&lt;br /&gt;
 &#039;&#039;&#039;NSsites = 1 2&#039;&#039;&#039;&lt;br /&gt;
: tells the program to analyze models M1 and M2&lt;br /&gt;
&lt;br /&gt;
 &#039;&#039;&#039;cleandata = 0&#039;&#039;&#039;&lt;br /&gt;
: tells the program to keep positions with gaps&lt;br /&gt;
&lt;br /&gt;
: &#039;&#039;&#039;Important:&#039;&#039;&#039; codeml requires a phylogenetic tree as input. The tree supplies the topology for the codon-model analysis, while codeml estimates the branch lengths and the remaining model parameters itself. In this exercise, use the tree produced by &#039;&#039;&#039;IQ-TREE 3&#039;&#039;&#039; (&#039;&#039;&#039;gp120.nexus.treefile&#039;&#039;&#039;) as input.&lt;br /&gt;
&lt;br /&gt;
: The settings entered by us will cause codeml to analyze two hypotheses about dN/dS ratios. M1 says there are two classes of codons with different dN/dS ratios in the sequence: one class with dN/dS &amp;lt; 1 (codons under purifying or negative selection), and one class with dN/dS = 1 (no selection - neutrally evolving sites). M2 says there are 3 distinct dN/dS ratios for different sites in the sequence: one class with dN/dS &amp;lt; 1, one class with dN/dS = 1 (these are the same type of classes as for M1), and one class with dN/dS &amp;gt; 1 (corresponding to sites under positive selection). The value of the dN/dS ratios (for those classes that have dN/dS &amp;lt; 1 or dN/dS &amp;gt; 1), the fraction of sites belonging to each class, and the position of sites belonging to each class, are unknown at first and will be determined during the analysis.&lt;br /&gt;
&lt;br /&gt;
: (Regarding &amp;quot;cleandata&amp;quot; setting: &amp;quot;cleandata = 1&amp;quot; would cause codeml to discard any alignment columns with gaps or ambiguity symbols. For alignments with many sequences this can lead to discarding the majority of sites and I would therefore recommend not using that setting. At the same time you do want to be wary of columns where most sequences have gaps, since inference of selection for these will be very uncertain. It might be better to discard such columns from an alignment before doing the codeml analysis.)&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Start the analysis&#039;&#039;&#039;&lt;br /&gt;
 codeml&lt;br /&gt;
: This will start the codeml program using the settings in the file codeml.ctl. Depending on your computer, this will take some minutes to finish. (You may be able to see how the optimization procedure results in progressively better fits: the likelihood increases, meaning that negative log-likelihood decreases, as the fit improves.)&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Inspect result file:&#039;&#039;&#039;&lt;br /&gt;
: Wait for the run to finish, and then look at the result file:&lt;br /&gt;
 nedit selection.results &amp;amp;&lt;br /&gt;
: This file contains a wealth of information concerning your analysis. You may want to turn off line-wrapping to more clearly see the structure of the file (there will be some long lines that will otherwise wrap around). The top part of the file gives an overview of your sequences, codon usage and nucleotide frequencies. You can ignore this information for now, and move on to the interesting part, namely the model likelihoods and parameter values.&lt;br /&gt;
&lt;br /&gt;
: &#039;&#039;&#039;Note:&#039;&#039;&#039; On some systems codeml may print an error message near the end of the run, for instance during the BEB step, even though the relevant results have already been written to &#039;&#039;&#039;selection.results&#039;&#039;&#039;. If this happens, inspect &#039;&#039;&#039;selection.results&#039;&#039;&#039; before assuming that the run failed completely.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Find likelihood, and number of free parameters for model M1&#039;&#039;&#039;&lt;br /&gt;
 Search ==&amp;gt; Find... ==&amp;gt; enter &amp;quot;Model 1&amp;quot; and click Find&lt;br /&gt;
: You are now in the region of the result file where the model likelihoods and parameter estimates are noted. Now locate a line that looks a bit like the one shown below:&lt;br /&gt;
 lnL(ntime: 72  np: 74):  -4242.470345     +0.000000&lt;br /&gt;
: Identify the number of &amp;quot;free parameters&amp;quot;, K, used in model M1: this is indicated by &amp;quot;np&amp;quot;, and is 74 in the example shown above (most of these parameters are branch lengths in the tree; specifically, the number of branch length parameters is indicated by &amp;quot;ntime&amp;quot;, and is 72 in this example). Also note the log-likelihood of the fitted model. This is the number right after the parenthesis, and is -4242.470345 in the example here.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Question: &#039;&#039;&#039;What are the values of K and lnL for model M1?&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Question 8&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Find dN/dS ratios and codon class proportions for model M1&#039;&#039;&#039;&lt;br /&gt;
: Scroll down a few lines until you get to a small table similar to this:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
      dN/dS for site classes (K=2)&lt;br /&gt;
      p:   0.75111  0.24889&lt;br /&gt;
      w:   0.06583  1.00000&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
: This gives a summary of the dN/dS ratios that were found in the data set. The line starting &amp;quot;w:&amp;quot; lists the two dN/dS ratios that were found (in this case 0.06583 and 1.00000 - the last one was pre-specified by us as part of the model and was therefore not a free parameter). In this context &amp;quot;w&amp;quot; means &amp;quot;omega&amp;quot;, which is the symbol typically used to represent the dN/dS rate ratio. The line starting &amp;quot;p:&amp;quot; gives the proportion of codon sites belonging to each of the dN/dS ratio classes (in the example above approximately 75% belong to the first class, while 25% of all sites belong to the class having dN/dS = 1.00000).&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Question: &#039;&#039;&#039;What are the dN/dS value (w) and proportion (p) of sites for both classes? Report the following values: p(class1), w(class1), p(class2), w(class2)&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Question 9&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Find likelihood, and K for model M2&#039;&#039;&#039;&lt;br /&gt;
: Scroll past the M1 output until you get to the results for model M2.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Question: &#039;&#039;&#039;What are the values of K and lnL for model M2?&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Question 10&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Find dN/dS ratios and codon class proportions for model M2&#039;&#039;&#039;&lt;br /&gt;
: Now scroll down a few lines until you get to a small table similar to the one you examined for M1 before. For this model there are 3 separate classes of codons.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Question: &#039;&#039;&#039;What are the dN/dS value (w) and proportion (p) of sites for all three classes? Report these values: p(class1), w(class1), p(class2), w(class2), p(class3), w(class3)&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Question 11&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Assess strength of evidence for models M1 and M2&#039;&#039;&#039;&lt;br /&gt;
: M2 will always have a better (higher) log-likelihood than model M1 because M2 has more free parameters, and M1 is nested within M2. You should now use the recipe given above to compute AIC values and model probabilities for M1 and M2.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Question: &#039;&#039;&#039;Report: AIC, ΔAIC, weight (model probability) for M1 and M2&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Question 12: &#039;&#039;&#039;Is M2 better than M1?&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Question 13&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Examine list of positively selected sites&#039;&#039;&#039;&lt;br /&gt;
: If your M2 is clearly better than M1, then you have evidence for the existence of positively selected sites in the gp120 gene. Now scroll down to the end of the result file and locate a list similar to the one below. Note: this is the &amp;quot;Bayes Empirical Bayes&amp;quot; table, not the &amp;quot;Naive Empirical Bayes&amp;quot; table.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Bayes Empirical Bayes (BEB) analysis&lt;br /&gt;
Positively selected sites&lt;br /&gt;
&lt;br /&gt;
         Prob(w&amp;gt;1)     mean w&lt;br /&gt;
&lt;br /&gt;
    25 A 0.959*        3.133 +- 0.769&lt;br /&gt;
    27 P 0.906         2.990 +- 0.877&lt;br /&gt;
    56 K 0.987*        3.197 +- 0.687&lt;br /&gt;
    59 V 0.915         3.032 +- 0.873&lt;br /&gt;
    78 R 0.637         2.351 +- 1.129&lt;br /&gt;
    88 K 0.573         2.148 +- 1.077&lt;br /&gt;
    95 V 0.925         3.046 +- 0.843&lt;br /&gt;
    ...&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
: It is not important what the distinction is in this context, but very briefly: NEB ignores the fact that there is uncertainty about maximum likelihood estimates, especially for smaller data sets (for instance w for some codon is perhaps not exactly 3.046, but could lie in a region around that value), whereas [https://pubmed.ncbi.nlm.nih.gov/15689528/ BEB accounts for that uncertainty].&lt;br /&gt;
&lt;br /&gt;
: This gives you a list of which residues (if any) were found to belong to the positively selected dN/dS class. Also listed is the probability that the site really is in the codon class where dN/dS &amp;gt; 1, and a weighted average of the w at the site.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Question: &#039;&#039;&#039;List all sites having more than 95% probability of belonging to the positively selected class.&lt;/div&gt;</summary>
		<author><name>Gorm</name></author>
	</entry>
	<entry>
		<id>https://teaching.healthtech.dtu.dk/22115/index.php?title=Model_selection&amp;diff=279</id>
		<title>Model selection</title>
		<link rel="alternate" type="text/html" href="https://teaching.healthtech.dtu.dk/22115/index.php?title=Model_selection&amp;diff=279"/>
		<updated>2026-04-15T09:28:30Z</updated>

		<summary type="html">&lt;p&gt;Gorm: /* Detection of positively selected sites in gp120 */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;This exercise is part of the course  [[22115_-_Computational_Molecular_Evolution|Computational Molecular Evolution (22115)]].&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Overview ==&lt;br /&gt;
&lt;br /&gt;
: In this exercise you are going to investigate features of HIV-1 evolution. You will do this by analyzing a large set of env-genes from HIV-1, subtype B. specifically, the DNA sequences analyzed here correspond to a region surrounding the hypervariable V3 region of the gp120 protein.&lt;br /&gt;
&lt;br /&gt;
: Like other retroviruses, particles of HIV are made up of 2 copies of a single-stranded RNA genome packaged inside a protein core, or capsid. The core particle also contains viral proteins that are essential for the early steps of the virus life cycle, such as reverse transcription and integration. A lipid envelope, derived from the infected cell, surrounds the core particle. Embedded in this envelope are the surface glycoproteins of HIV: gp120 and gp41. The gp120 protein is crucial for binding of the virus particle to target cells, while gp41 is important for the subsequent fusion event. It is the specific affinity of gp120 for the CD4 protein that targets HIV to those cells of the immune system that express CD4 on their surface (e.g., T-helper lymphocytes, monocytes, and macrophages).&lt;br /&gt;
&lt;br /&gt;
: The role gp120 plays in infection and the fact that it is situated on the surface of the HIV particle, means it is an obvious target for the immune response. That means that there may be a considerable selective pressure on gp120 for creating immune-escape mutants, where amino acids in the gp120 epitopes have been substituted. In this exercise you will construct a maximum likelihood tree that we will subsequently use to investigate whether you can detect such a selective pressure on parts of gp120, again using maximum likelihood methods.&lt;br /&gt;
&lt;br /&gt;
: One major goal with the exercise is to introduce you to statistically based methods for assessing the strength of evidence for a set of alternative hypotheses about some biological system of interest. The model selection method we will use is AIC (Akaike Information Criterion), based on which you will compute model probabilities. A second goal is to make you aware that phylogenetic analysis is not only about constructing trees, but that it is also a useful framework for analyzing biological questions more generally.&lt;br /&gt;
&lt;br /&gt;
: Specifically, you will&lt;br /&gt;
&lt;br /&gt;
:# perform a multiple alignment of gp120 DNA sequences taking protein-level information into account (using revtrans).&lt;br /&gt;
:# select a suitable nucleotide substitution model (using jmodeltest2)&lt;br /&gt;
:# construct a phylogenetic tree (using PAUP).&lt;br /&gt;
:# try to detect positively selected sites in gp120 (using PAML).&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
== Recipe for computing AIC values and model probabilities ==&lt;br /&gt;
&lt;br /&gt;
: Later in today&#039;s exercise you will be asked to compute AIC values and model probabilities. Return to this section and follow the instructions when you need to do so.&lt;br /&gt;
&lt;br /&gt;
:# Fit a set of models to your data, note the maximized log likelihoods (lnL) and the number of free parameters (K) for each model in the investigated set. The models you fit should represent a plausible and comprehensive set of hypotheses about your data.&lt;br /&gt;
:# Compute AIC for each of the models: &#039;&#039;&#039;AIC = -2 x lnL + 2K&#039;&#039;&#039;.  &amp;lt;br&amp;gt;For example: a model with lnL = -2010 and K = 5 will have AIC = -2 x -2010 + 2 x 5 = 4030.&lt;br /&gt;
:# Identify the model with the smallest AIC (this is the best model in the set). We will call the AIC for this model &#039;&#039;&#039;&amp;quot;AICmin&amp;quot;&#039;&#039;&#039;.&lt;br /&gt;
:# Compute the &amp;quot;ΔAIC&amp;quot; values for each model: &#039;&#039;&#039;ΔAIC = AIC - AICmin&#039;&#039;&#039; &amp;lt;br&amp;gt;For each model subtract the minimum AIC value. The best model will have a ΔAIC of zero. The rest of the models will have positive ΔAICs.&lt;br /&gt;
:# For each model compute the following quantity: &#039;&#039;&#039;numerator = exp(-0.5 x ΔAIC)&#039;&#039;&#039; &amp;lt;br&amp;gt;For example, a model with ΔAIC=4.2 will have numerator = exp(-0.5 x 4.2) = exp(-2.1) = 0.1225. Also compute the &#039;&#039;&#039;sum&#039;&#039;&#039; of the numerator values for all models.&lt;br /&gt;
:# Finally, the model probabilities (also called model weights) for each model are found as:&amp;lt;br&amp;gt; &#039;&#039;&#039;P(model) = numerator / sum&#039;&#039;&#039; &amp;lt;br&amp;gt;For example, if sum = 3.75 and a model has numerator = 1.3, then it has P(model) = 1.3 / 3.75 = 0.35&lt;br /&gt;
&lt;br /&gt;
: You may want to keep track of the computations by constructing a table along the following lines:&lt;br /&gt;
[[File:Molevol-Downloads-aictable.png|700px]]&lt;br /&gt;
&lt;br /&gt;
: Note that model probabilities can also be computed using Bayesian methods. One advantage of Bayesian methods over AIC is that instead of relying on a point estimate, uncertainty about parameter values is accounted for by integrating over all possible values (typically using MCMC).&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
== Getting started ==&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Create working directory, copy files&#039;&#039;&#039;&lt;br /&gt;
: In the command below: Instead of /path/to/molevol enter the path to the directory where you have placed your course files (for instance cd /Users/bob/Documents/molevol, or cd /home/student/molevol).&lt;br /&gt;
 cd /path/to/molevol&lt;br /&gt;
 mkdir modelselect&lt;br /&gt;
 cd modelselect&lt;br /&gt;
 cp ../data/gp120.fasta ./gp120.fasta&lt;br /&gt;
 cp ../data/codeml.ctl ./codeml.ctl&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Have a look at the DNA data file:&#039;&#039;&#039;&lt;br /&gt;
 nedit gp120.fasta &amp;amp;&lt;br /&gt;
: The file contains several DNA sequences from HIV-1, subtype B. The sequences are approximately 500 bp long, and correspond to a region surrounding the hypervariable V3 region in the gene encoding gp120. Close the nedit window when you&#039;ve had a look.&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
== Protein-guided alignment of coding DNA ==&lt;br /&gt;
&lt;br /&gt;
: There are three reasons why it is preferable to align coding DNA at the amino acid level rather than directly as nucleotides. First, the DNA alphabet has only four letters compared to 20 amino acids, which means that even unrelated DNA sequences will share roughly 25% identity by chance alone, making it much harder to distinguish true homology from noise. Second, protein alignments benefit from empirical substitution matrices such as BLOSUM-62, which capture the fact that amino acid replacements tend to be conservative. Equivalent matrices for DNA are far less informative because nucleotide substitution patterns vary strongly between genes and organisms. Third, synonymous substitutions accumulate faster than non-synonymous ones, so DNA sequences diverge more rapidly than the proteins they encode. Taken together, these factors mean that the phylogenetic signal in a DNA alignment erodes much faster than in the corresponding protein alignment.&lt;br /&gt;
&lt;br /&gt;
: However, while protein-level alignment gives us the most reliable homology signal, it discards exactly the information we need for studying selection: the synonymous substitutions. Estimating the ratio of non-synonymous to synonymous substitution rates (dN/dS) requires a codon-level alignment where analogous codon positions are properly lined up. We would therefore like to construct a multiple alignment at the DNA level, but using information at the protein level, and the RevTrans server does exactly that.&lt;br /&gt;
&lt;br /&gt;
: &#039;&#039;&#039;RevTrans&#039;&#039;&#039; takes as input an unaligned set of DNA sequences, automatically translates them to the equivalent amino acid sequences, constructs a multiple alignment of the protein sequences, and finally uses the protein alignment as a template for constructing a multiple DNA alignment. Because each amino acid maps back to its original codon, gaps are always inserted in groups of three and codon boundaries are preserved throughout the alignment. This is essential for the dN/dS analyses we will carry out in this exercise: if codon positions are misaligned, synonymous and non-synonymous changes cannot be correctly distinguished.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Construct RevTrans alignment&#039;&#039;&#039;&lt;br /&gt;
:* Open RevTrans server page: https://services.healthtech.dtu.dk/services/RevTrans-2.0/&lt;br /&gt;
:* On the RevTrans page: Choose the file gp120.fasta as input (or copy and paste the sequence into the sequence window)&lt;br /&gt;
:* Click the &amp;quot;Submit query&amp;quot; button&lt;br /&gt;
:* When the alignment is done you may have to click link named &amp;quot;here&amp;quot; to go to results page&lt;br /&gt;
:* Download DNA alignment, by right-clicking the link for &amp;quot;Download alignment in FASTA format&amp;quot;, and choosing &amp;quot;Save link as...&amp;quot; (save file under the name gp120align.fasta and make sure to save the file in the directory modelselect).&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Convert alignment to NEXUS format&#039;&#039;&#039;&lt;br /&gt;
: Convert the fasta file to NEXUS format and save file in the modelselect directory under the name gp120.nexus&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
== Model selection using AIC and jModelTest2 ==&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Question 1&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
: As part of the present analysis we are going to build a phylogenetic tree based on the DNA alignment constructed above. We will construct the tree using maximum likelihood, but to do that we first have to decide which substitution model we want to use. Specifically, we are interested in selecting a model that achieves a good balance between fit to the data and number of parameters. We will investigate this issue by fitting a set of 56 different models to our data and then comparing them using AIC.&lt;br /&gt;
&lt;br /&gt;
: In the first part of the exercise, however, you will only compare three closely related models by hand. The purpose of this restricted comparison is not primarily to identify the globally best model, but to understand how AIC, ΔAIC, and Akaike weights are computed and interpreted. Because only three models are included in this manual comparison, the weights you compute will describe relative support &#039;&#039;&#039;within this restricted set only&#039;&#039;&#039;.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Start jmodeltest2&#039;&#039;&#039;&lt;br /&gt;
 jmodeltest&lt;br /&gt;
&#039;&#039;&#039;Load data set&#039;&#039;&#039;&lt;br /&gt;
:* File -&amp;gt; Load DNA alignment -&amp;gt; File Format -&amp;gt; Select &amp;quot;All files&amp;quot;&lt;br /&gt;
:* Navigate to gp120.nexus and load it&lt;br /&gt;
&#039;&#039;&#039;Fit 56 models&#039;&#039;&#039;&lt;br /&gt;
:* Analysis -&amp;gt; Compute likelihood scores&lt;br /&gt;
:* Select &amp;quot;7&amp;quot; under &amp;quot;Number of substitution schemes&amp;quot;&lt;br /&gt;
:* Select &amp;quot;Fixed BIONJ-JC&amp;quot; under &amp;quot;Base tree for likelihood calculations&amp;quot;&lt;br /&gt;
:* Click &amp;quot;Compute likelihoods&amp;quot;&lt;br /&gt;
&lt;br /&gt;
: This causes jmodeltest2 to perform the following actions: first a neighbor joining tree is constructed using the Jukes and Cantor model. Then the tree is fixed and used as the basis for fitting a set of 56 different models to the data. For each model, the estimated model parameters and the negative log-likelihood are recorded. In addition to varying sets of substitution rate parameters (JC, K2P, ...), some of these models also include extra parameters that take into account the presence of different rates between sites. This is done in two ways: (1) by fitting a gamma distribution of rates (&amp;quot;+G&amp;quot;), and (2) by allowing for a proportion of constant (&amp;quot;invariable&amp;quot;) sites (&amp;quot;+I&amp;quot;).&lt;br /&gt;
&lt;br /&gt;
: Wait until jmodeltest2 is done fitting all 56 models (this will take a little while depending on your computer).&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Inspect result&#039;&#039;&#039;&lt;br /&gt;
:* Results -&amp;gt; Show results table&lt;br /&gt;
: For each model this table lists the negative log-likelihood (&amp;quot;-lnL&amp;quot;), the number of parameters (&amp;quot;p&amp;quot; - note this includes branch lengths), and estimates of all model parameters (excluding branch lengths).&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Manually compute Akaike weights for three substitution models&#039;&#039;&#039;&lt;br /&gt;
: Use AIC-based model comparison to investigate which of the following three substitution models are best supported as descriptions of how the sequences have evolved:&lt;br /&gt;
:* Jukes and Cantor with fraction of invariant sites (JC+I)&lt;br /&gt;
:* Jukes and Cantor with gamma-distributed rates over sites (JC+G)&lt;br /&gt;
:* Jukes and Cantor with invariant sites and gamma-distributed rates (JC+I+G)&lt;br /&gt;
: Before you can do the computation you need to know the log likelihood and the number of parameters for each model. Locate these values in the table for the JC+I, JC+G, and JC+I+G models, and write them down. Close the window with the result table when you are done.&lt;br /&gt;
&lt;br /&gt;
: Make sure to get the signs right: the values reported in the table are -lnL values, so you will need to reverse the sign to get the lnL (the lnL values you write down should be negative).&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Question: &#039;&#039;&#039;Use the recipe above to compute AIC, ΔAIC, and Akaike weight for the three models. Report the results in a table similar to the one shown above, and verify that the three weights sum to 1 (up to rounding error).&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Question 2&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
: Based on the Akaike weights, which of the three models has the strongest support? Is the support strongly concentrated on one model, or is there still substantial support for one or both of the others?&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Question 3&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
: What you just did manually for JC+I, JC+G and JC+I+G, jmodeltest2 can do automatically for the full set of 56 fitted models. Specifically, it uses the list of negative log likelihoods and parameter counts in the table to compute AIC values and Akaike weights for the full candidate set:&lt;br /&gt;
:* Analysis -&amp;gt; Do AIC calculations -&amp;gt;&lt;br /&gt;
:* Select &amp;quot;Write PAUP* block&amp;quot;&lt;br /&gt;
:* click &amp;quot;Do AIC calculations&amp;quot;&lt;br /&gt;
:* Results -&amp;gt; Show results table&lt;br /&gt;
:* Select &amp;quot;AIC&amp;quot; tab&lt;br /&gt;
:* &#039;&#039;&#039;IMPORTANT:&#039;&#039;&#039; SHIFT+click on the header of the &amp;quot;weight&amp;quot; column. This sorts the rows according to model weight, in descending order.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Question: &#039;&#039;&#039;What model was selected by modeltest based on the AIC values? (This will be the model with the highest weight - and lowest AIC - and will be in the first row of the results table after SHIFT-clicking the &amp;quot;weight&amp;quot; header).&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Question 4&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
: In the same AIC results table, now click (not SHIFT+click) the header of the &amp;quot;-lnL&amp;quot; column. In this table, a normal click sorts in ascending order, whereas SHIFT+click sorts in descending order. The model at the top will therefore be the one with the smallest value of &amp;quot;-lnL&amp;quot; (equivalently: the highest likelihood). Compare this model to the one with the highest Akaike weight.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Question: &#039;&#039;&#039;Is the model with the highest likelihood also the model with the highest Akaike weight? If not, explain briefly why these two rankings can differ.&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
== Phylogenetic tree inference ==&lt;br /&gt;
&lt;br /&gt;
=== Tree inference using the model selected by jModelTest2 (PAUP) ===&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Question 5&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
: Above, you used jModelTest2 to select a substitution model based on AIC. You will now use that model to construct a maximum likelihood tree in &#039;&#039;&#039;PAUP&#039;&#039;&#039;. The purpose of doing this in PAUP is mainly pedagogical: it makes the individual steps of model-based phylogenetic inference more explicit. In practice, one would often use a program such as IQ-TREE 3 (see next section).&lt;br /&gt;
&lt;br /&gt;
: Close the results table. In the main window you should now scroll up to the lines giving PAUP commands that implement the selected model. The command is enclosed between &amp;quot;BEGIN PAUP&amp;quot; and &amp;quot;END;&amp;quot; and should look something like this:&lt;br /&gt;
 Lset Base=(0.4064, [...]&lt;br /&gt;
: You will need to copy this command to a PAUP session in the next step.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Start PAUP&#039;&#039;&#039;&lt;br /&gt;
 paup&lt;br /&gt;
: Above, you used jModelTest2 to select the most suitable substitution model for the present data set. You will now use this model to construct a maximum likelihood tree. You will use PAUP for this purpose. (Note: it is possible to create a maximum likelihood tree, or even a model-averaged tree, directly from jModelTest2, but we will instead do it in PAUP in order to more clearly see each step that is taken.)&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Load alignment:&#039;&#039;&#039;&lt;br /&gt;
 execute gp120.nexus&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Set tree-building criterion to maximum likelihood&#039;&#039;&#039;&lt;br /&gt;
 set criterion=likelihood&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Set model parameters to winning estimates&#039;&#039;&#039;&lt;br /&gt;
: Above, you located a set of lines in the jModelTest2 output giving a PAUP command that sets the model parameters to the estimates found for the winning model. Copy and paste this lset command (without the BEGIN and END parts) into the window where PAUP is running.&lt;br /&gt;
&lt;br /&gt;
 PASTE LSET COMMAND FROM MODELTEST RUN HERE&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Find best tree using selected model&#039;&#039;&#039;&lt;br /&gt;
: Still in the PAUP window, enter the following command:&lt;br /&gt;
 hsearch swap=tbr start=nj&lt;br /&gt;
: This command causes PAUP to perform a heuristic search for the best maximum likelihood tree. Once an initial tree has been constructed, the heuristic search proceeds by rearrangements of the &amp;quot;tree bisection and reconnection&amp;quot; type (TBR). We are using the model selected by modeltest, and the parameter estimates found by modeltest for that model. You could also choose to estimate all the model parameters as part of this step (i.e., at the same time as finding the best tree), but fixing them improves speed tremendously. Finding the best tree should take at most a few minutes.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Save best tree to file&#039;&#039;&#039;&lt;br /&gt;
 savetrees format=newick brlens=yes file=gp120tree_paup.phy from=1 to=1&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Quit program&#039;&#039;&#039;&lt;br /&gt;
 quit&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Have a look at the tree:&#039;&#039;&#039;&lt;br /&gt;
: You have now produced an unrooted tree of the HIV sequences and saved it in the file gp120tree_paup.phy. Note that in this exercise we will not be interested in the tree as such - our focus is instead on finding positive selection at a subset of codon positions, and the tree is just something we need in order to fit the different codon models to the data. If you want to see the tree, you can do so with the following command:&lt;br /&gt;
 figtree gp120tree_paup.phy &amp;amp;&lt;br /&gt;
: There is no meaningful root placed on this tree, so you may want to choose the unrooted view (the third icon in the Layout section of the FigTree window). Close the FigTree window when you have had a look.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Question: &#039;&#039;&#039;Which substitution model did you use for the PAUP tree search, and what was the negative log likelihood of the best tree found under this model?&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
=== One-step model selection and tree inference using IQ-TREE 3 ===&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Question 6&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
: In real-life phylogenetic analysis one would often use a modern program such as &#039;&#039;&#039;IQ-TREE 3&#039;&#039;&#039;, which can carry out model selection and maximum likelihood tree reconstruction in a single analysis. You will now repeat the analysis using this more integrated approach.&lt;br /&gt;
&lt;br /&gt;
: Run IQ-TREE 3 on the alignment using the following command:&lt;br /&gt;
 iqtree3 -s gp120.nexus -m MFP&lt;br /&gt;
&lt;br /&gt;
: Here, &amp;quot;-m MFP&amp;quot; tells IQ-TREE 3 to perform automatic model selection using &amp;quot;ModelFinder Plus&amp;quot; before reconstructing the tree.&lt;br /&gt;
&lt;br /&gt;
: When the run has finished, inspect the output written to the screen and to the file ending in &amp;quot;.iqtree&amp;quot;. Note that IQ-TREE 3 reports several model-selection criteria in the output. The line near the top of the ModelFinder section states which criterion was used to choose the final model. Locate the substitution model selected by IQ-TREE 3 and the log likelihood of the best tree.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Question: &#039;&#039;&#039;Which substitution model was selected by IQ-TREE 3, and according to which information criterion was it selected? What was the log likelihood of the best tree? Compare this to the earlier jModelTest2 + PAUP analysis. Is the selected model exactly the same? Is the likelihood exactly the same? Briefly suggest one reason why the two analyses might not give identical results.&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
== Detection of positively selected sites in gp120 ==&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Question 7&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
: There is much more to phylogenetic analyses than merely reconstructing trees. One interesting result of probabilistic methods is that the parameters of a model are estimated as part of the optimization procedure. This means that once such a model has been fitted to the data, it is possible to investigate these estimated parameter values to learn features about the evolutionary history of the sequences under investigation. In the present example we will focus on whether we can detect positively selected sites in our data set, defined as sites where the dN/dS ratio is larger than 1. We do that by using a codon substitution model where the dN/dS ratio is one of its parameters.&lt;br /&gt;
&lt;br /&gt;
: A further strength of the probabilistic approach is that you get a probabilistic measure of how well any model fits the data. This means you can use a stringent approach to determine which model fits the data best. In this framework one uses likelihoods (probabilities of data given model) to determine which model fits the data best. As you saw above, it is for instance possible to compute AIC values and weights (model probabilities) from the likelihood values of fitted models. Since each model essentially corresponds to a hypothesis about the evolutionary history of the data, we can thus use a stringent statistical approach to decide which hypothesis best describes our data.&lt;br /&gt;
&lt;br /&gt;
: In outline, you will now use the following steps to investigate whether there is any evidence for positively selected codons in your data set:&lt;br /&gt;
&lt;br /&gt;
:* Fit model M1, which assumes there are two classes of codons in the sequence: some with dN/dS &amp;lt; 1, some with dN/dS = 1.&lt;br /&gt;
:* Fit model M2, which assumes 3 distinct classes of codons: two with dN/dS ratios as for M1, and one extra class with dN/dS &amp;gt; 1.&lt;br /&gt;
:* Assess the strength of evidence for the two models using AIC-based model probabilities&lt;br /&gt;
:* If M2 is better: identify the positively selected codons&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Edit the parameter file&#039;&#039;&#039;&lt;br /&gt;
 nedit codeml.ctl &amp;amp;&lt;br /&gt;
: The file &amp;quot;codeml.ctl&amp;quot; contains several settings that control how &#039;&#039;&#039;codeml&#039;&#039;&#039; is run. Before continuing, edit the file and save it so that the following settings have these values. Any text appearing after an asterisk (*) on a line is only a comment, and you do not need to change it.&lt;br /&gt;
&lt;br /&gt;
 &#039;&#039;&#039;seqfile =  gp120align.fasta&#039;&#039;&#039;&lt;br /&gt;
: name of alignment file&lt;br /&gt;
&lt;br /&gt;
 &#039;&#039;&#039;treefile =  gp120.nexus.treefile&#039;&#039;&#039;&lt;br /&gt;
: name of tree file produced by IQ-TREE 3&lt;br /&gt;
&lt;br /&gt;
 &#039;&#039;&#039;seqtype = 1&#039;&#039;&#039;&lt;br /&gt;
: tells the program that the data consist of coding DNA&lt;br /&gt;
&lt;br /&gt;
 &#039;&#039;&#039;NSsites = 1 2&#039;&#039;&#039;&lt;br /&gt;
: tells the program to analyze models M1 and M2&lt;br /&gt;
&lt;br /&gt;
 &#039;&#039;&#039;cleandata = 0&#039;&#039;&#039;&lt;br /&gt;
: tells the program to keep positions with gaps&lt;br /&gt;
&lt;br /&gt;
: &#039;&#039;&#039;Important note:&#039;&#039;&#039; in this exercise, use the tree from &#039;&#039;&#039;IQ-TREE 3&#039;&#039;&#039; for the codeml analysis. On some systems, codeml may have problems with the PAUP tree file or may stop with a confusing error message even though the downstream results are still written to the output file. Using the IQ-TREE tree avoids this issue.&lt;br /&gt;
&lt;br /&gt;
: The settings entered by us will cause codeml to analyze two hypotheses about dN/dS ratios. M1 says there are two classes of codons with different dN/dS ratios in the sequence: one class with dN/dS &amp;lt; 1 (codons under purifying or negative selection), and one class with dN/dS = 1 (no selection - neutrally evolving sites). M2 says there are 3 distinct dN/dS ratios for different sites in the sequence: one class with dN/dS &amp;lt; 1, one class with dN/dS = 1 (these are the same type of classes as for M1), and one class with dN/dS &amp;gt; 1 (corresponding to sites under positive selection). The value of the dN/dS ratios (for those classes that have dN/dS &amp;lt; 1 or dN/dS &amp;gt; 1), the fraction of sites belonging to each class, and the position of sites belonging to each class, are unknown at first and will be determined during the analysis.&lt;br /&gt;
&lt;br /&gt;
: (Regarding &amp;quot;cleandata&amp;quot; setting: &amp;quot;cleandata = 1&amp;quot; would cause codeml to discard any alignment columns with gaps or ambiguity symbols. For alignments with many sequences this can lead to discarding the majority of sites and I would therefore recommend not using that setting. At the same time you do want to be wary of columns where most sequences have gaps, since inference of selection for these will be very uncertain. It might be better to discard such columns from an alignment before doing the codeml analysis.)&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Start the analysis&#039;&#039;&#039;&lt;br /&gt;
 codeml&lt;br /&gt;
: This will start the codeml program using the settings in the file codeml.ctl. Depending on your computer, this will take some minutes to finish. (You may be able to see how the optimization procedure results in progressively better fits: the likelihood increases, meaning that negative log-likelihood decreases, as the fit improves.)&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Inspect result file:&#039;&#039;&#039;&lt;br /&gt;
: Wait for the run to finish, and then look at the result file:&lt;br /&gt;
 nedit selection.results &amp;amp;&lt;br /&gt;
: This file contains a wealth of information concerning your analysis. You may want to turn off line-wrapping to more clearly see the structure of the file (there will be some long lines that will otherwise wrap around). The top part of the file gives an overview of your sequences, codon usage and nucleotide frequencies. You can ignore this information for now, and move on to the interesting part, namely the model likelihoods and parameter values.&lt;br /&gt;
&lt;br /&gt;
: &#039;&#039;&#039;Note:&#039;&#039;&#039; On some systems codeml may print an error message near the end of the run, for instance during the BEB step, even though the relevant results have already been written to &#039;&#039;&#039;selection.results&#039;&#039;&#039;. If this happens, inspect &#039;&#039;&#039;selection.results&#039;&#039;&#039; before assuming that the run failed completely.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Find likelihood, and number of free parameters for model M1&#039;&#039;&#039;&lt;br /&gt;
 Search ==&amp;gt; Find... ==&amp;gt; enter &amp;quot;Model 1&amp;quot; and click Find&lt;br /&gt;
: You are now in the region of the result file where the model likelihoods and parameter estimates are noted. Now locate a line that looks a bit like the one shown below:&lt;br /&gt;
 lnL(ntime: 72  np: 74):  -4242.470345     +0.000000&lt;br /&gt;
: Identify the number of &amp;quot;free parameters&amp;quot;, K, used in model M1: this is indicated by &amp;quot;np&amp;quot;, and is 74 in the example shown above (most of these parameters are branch lengths in the tree; specifically, the number of branch length parameters is indicated by &amp;quot;ntime&amp;quot;, and is 72 in this example). Also note the log-likelihood of the fitted model. This is the number right after the parenthesis, and is -4242.470345 in the example here.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Question: &#039;&#039;&#039;What are the values of K and lnL for model M1?&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Question 8&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Find dN/dS ratios and codon class proportions for model M1&#039;&#039;&#039;&lt;br /&gt;
: Scroll down a few lines until you get to a small table similar to this:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
      dN/dS for site classes (K=2)&lt;br /&gt;
      p:   0.75111  0.24889&lt;br /&gt;
      w:   0.06583  1.00000&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
: This gives a summary of the dN/dS ratios that were found in the data set. The line starting &amp;quot;w:&amp;quot; lists the two dN/dS ratios that were found (in this case 0.06583 and 1.00000 - the last one was pre-specified by us as part of the model and was therefore not a free parameter). In this context &amp;quot;w&amp;quot; means &amp;quot;omega&amp;quot;, which is the symbol typically used to represent the dN/dS rate ratio. The line starting &amp;quot;p:&amp;quot; gives the proportion of codon sites belonging to each of the dN/dS ratio classes (in the example above approximately 75% belong to the first class, while 25% of all sites belong to the class having dN/dS = 1.00000).&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Question: &#039;&#039;&#039;What are the dN/dS value (w) and proportion (p) of sites for both classes? Report the following values: p(class1), w(class1), p(class2), w(class2)&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Question 9&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Find likelihood, and K for model M2&#039;&#039;&#039;&lt;br /&gt;
: Scroll past the M1 output until you get to the results for model M2.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Question: &#039;&#039;&#039;What are the values of K and lnL for model M2?&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Question 10&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Find dN/dS ratios and codon class proportions for model M2&#039;&#039;&#039;&lt;br /&gt;
: Now scroll down a few lines until you get to a small table similar to the one you examined for M1 before. For this model there are 3 separate classes of codons.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Question: &#039;&#039;&#039;What are the dN/dS value (w) and proportion (p) of sites for all three classes? Report these values: p(class1), w(class1), p(class2), w(class2), p(class3), w(class3)&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Question 11&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Assess strength of evidence for models M1 and M2&#039;&#039;&#039;&lt;br /&gt;
: M2 will always have a better (higher) log-likelihood than model M1 because M2 has more free parameters, and M1 is nested within M2. You should now use the recipe given above to compute AIC values and model probabilities for M1 and M2.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Question: &#039;&#039;&#039;Report: AIC, ΔAIC, weight (model probability) for M1 and M2&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Question 12: &#039;&#039;&#039;Is M2 better than M1?&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Question 13&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Examine list of positively selected sites&#039;&#039;&#039;&lt;br /&gt;
: If your M2 is clearly better than M1, then you have evidence for the existence of positively selected sites in the gp120 gene. Now scroll down to the end of the result file and locate a list similar to the one below. Note: this is the &amp;quot;Bayes Empirical Bayes&amp;quot; table, not the &amp;quot;Naive Empirical Bayes&amp;quot; table.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Bayes Empirical Bayes (BEB) analysis&lt;br /&gt;
Positively selected sites&lt;br /&gt;
&lt;br /&gt;
         Prob(w&amp;gt;1)     mean w&lt;br /&gt;
&lt;br /&gt;
    25 A 0.959*        3.133 +- 0.769&lt;br /&gt;
    27 P 0.906         2.990 +- 0.877&lt;br /&gt;
    56 K 0.987*        3.197 +- 0.687&lt;br /&gt;
    59 V 0.915         3.032 +- 0.873&lt;br /&gt;
    78 R 0.637         2.351 +- 1.129&lt;br /&gt;
    88 K 0.573         2.148 +- 1.077&lt;br /&gt;
    95 V 0.925         3.046 +- 0.843&lt;br /&gt;
    ...&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
: It is not important what the distinction is in this context, but very briefly: NEB ignores the fact that there is uncertainty about maximum likelihood estimates, especially for smaller data sets (for instance w for some codon is perhaps not exactly 3.046, but could lie in a region around that value), whereas [https://pubmed.ncbi.nlm.nih.gov/15689528/ BEB accounts for that uncertainty].&lt;br /&gt;
&lt;br /&gt;
: This gives you a list of which residues (if any) were found to belong to the positively selected dN/dS class. Also listed is the probability that the site really is in the codon class where dN/dS &amp;gt; 1, and a weighted average of the w at the site.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Question: &#039;&#039;&#039;List all sites having more than 95% probability of belonging to the positively selected class.&lt;/div&gt;</summary>
		<author><name>Gorm</name></author>
	</entry>
	<entry>
		<id>https://teaching.healthtech.dtu.dk/22115/index.php?title=Model_selection&amp;diff=278</id>
		<title>Model selection</title>
		<link rel="alternate" type="text/html" href="https://teaching.healthtech.dtu.dk/22115/index.php?title=Model_selection&amp;diff=278"/>
		<updated>2026-04-15T09:25:14Z</updated>

		<summary type="html">&lt;p&gt;Gorm: /* Detection of positively selected sites in gp120 */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;This exercise is part of the course  [[22115_-_Computational_Molecular_Evolution|Computational Molecular Evolution (22115)]].&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Overview ==&lt;br /&gt;
&lt;br /&gt;
: In this exercise you are going to investigate features of HIV-1 evolution. You will do this by analyzing a large set of env-genes from HIV-1, subtype B. specifically, the DNA sequences analyzed here correspond to a region surrounding the hypervariable V3 region of the gp120 protein.&lt;br /&gt;
&lt;br /&gt;
: Like other retroviruses, particles of HIV are made up of 2 copies of a single-stranded RNA genome packaged inside a protein core, or capsid. The core particle also contains viral proteins that are essential for the early steps of the virus life cycle, such as reverse transcription and integration. A lipid envelope, derived from the infected cell, surrounds the core particle. Embedded in this envelope are the surface glycoproteins of HIV: gp120 and gp41. The gp120 protein is crucial for binding of the virus particle to target cells, while gp41 is important for the subsequent fusion event. It is the specific affinity of gp120 for the CD4 protein that targets HIV to those cells of the immune system that express CD4 on their surface (e.g., T-helper lymphocytes, monocytes, and macrophages).&lt;br /&gt;
&lt;br /&gt;
: The role gp120 plays in infection and the fact that it is situated on the surface of the HIV particle, means it is an obvious target for the immune response. That means that there may be a considerable selective pressure on gp120 for creating immune-escape mutants, where amino acids in the gp120 epitopes have been substituted. In this exercise you will construct a maximum likelihood tree that we will subsequently use to investigate whether you can detect such a selective pressure on parts of gp120, again using maximum likelihood methods.&lt;br /&gt;
&lt;br /&gt;
: One major goal with the exercise is to introduce you to statistically based methods for assessing the strength of evidence for a set of alternative hypotheses about some biological system of interest. The model selection method we will use is AIC (Akaike Information Criterion), based on which you will compute model probabilities. A second goal is to make you aware that phylogenetic analysis is not only about constructing trees, but that it is also a useful framework for analyzing biological questions more generally.&lt;br /&gt;
&lt;br /&gt;
: Specifically, you will&lt;br /&gt;
&lt;br /&gt;
:# perform a multiple alignment of gp120 DNA sequences taking protein-level information into account (using revtrans).&lt;br /&gt;
:# select a suitable nucleotide substitution model (using jmodeltest2)&lt;br /&gt;
:# construct a phylogenetic tree (using PAUP).&lt;br /&gt;
:# try to detect positively selected sites in gp120 (using PAML).&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
== Recipe for computing AIC values and model probabilities ==&lt;br /&gt;
&lt;br /&gt;
: Later in today&#039;s exercise you will be asked to compute AIC values and model probabilities. Return to this section and follow the instructions when you need to do so.&lt;br /&gt;
&lt;br /&gt;
:# Fit a set of models to your data, note the maximized log likelihoods (lnL) and the number of free parameters (K) for each model in the investigated set. The models you fit should represent a plausible and comprehensive set of hypotheses about your data.&lt;br /&gt;
:# Compute AIC for each of the models: &#039;&#039;&#039;AIC = -2 x lnL + 2K&#039;&#039;&#039;.  &amp;lt;br&amp;gt;For example: a model with lnL = -2010 and K = 5 will have AIC = -2 x -2010 + 2 x 5 = 4030.&lt;br /&gt;
:# Identify the model with the smallest AIC (this is the best model in the set). We will call the AIC for this model &#039;&#039;&#039;&amp;quot;AICmin&amp;quot;&#039;&#039;&#039;.&lt;br /&gt;
:# Compute the &amp;quot;ΔAIC&amp;quot; values for each model: &#039;&#039;&#039;ΔAIC = AIC - AICmin&#039;&#039;&#039; &amp;lt;br&amp;gt;For each model subtract the minimum AIC value. The best model will have a ΔAIC of zero. The rest of the models will have positive ΔAICs.&lt;br /&gt;
:# For each model compute the following quantity: &#039;&#039;&#039;numerator = exp(-0.5 x ΔAIC)&#039;&#039;&#039; &amp;lt;br&amp;gt;For example, a model with ΔAIC=4.2 will have numerator = exp(-0.5 x 4.2) = exp(-2.1) = 0.1225. Also compute the &#039;&#039;&#039;sum&#039;&#039;&#039; of the numerator values for all models.&lt;br /&gt;
:# Finally, the model probabilities (also called model weights) for each model are found as:&amp;lt;br&amp;gt; &#039;&#039;&#039;P(model) = numerator / sum&#039;&#039;&#039; &amp;lt;br&amp;gt;For example, if sum = 3.75 and a model has numerator = 1.3, then it has P(model) = 1.3 / 3.75 = 0.35&lt;br /&gt;
&lt;br /&gt;
: You may want to keep track of the computations by constructing a table along the following lines:&lt;br /&gt;
[[File:Molevol-Downloads-aictable.png|700px]]&lt;br /&gt;
&lt;br /&gt;
: Note that model probabilities can also be computed using Bayesian methods. One advantage of Bayesian methods over AIC is that instead of relying on a point estimate, uncertainty about parameter values is accounted for by integrating over all possible values (typically using MCMC).&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
== Getting started ==&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Create working directory, copy files&#039;&#039;&#039;&lt;br /&gt;
: In the command below: Instead of /path/to/molevol enter the path to the directory where you have placed your course files (for instance cd /Users/bob/Documents/molevol, or cd /home/student/molevol).&lt;br /&gt;
 cd /path/to/molevol&lt;br /&gt;
 mkdir modelselect&lt;br /&gt;
 cd modelselect&lt;br /&gt;
 cp ../data/gp120.fasta ./gp120.fasta&lt;br /&gt;
 cp ../data/codeml.ctl ./codeml.ctl&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Have a look at the DNA data file:&#039;&#039;&#039;&lt;br /&gt;
 nedit gp120.fasta &amp;amp;&lt;br /&gt;
: The file contains several DNA sequences from HIV-1, subtype B. The sequences are approximately 500 bp long, and correspond to a region surrounding the hypervariable V3 region in the gene encoding gp120. Close the nedit window when you&#039;ve had a look.&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
== Protein-guided alignment of coding DNA ==&lt;br /&gt;
&lt;br /&gt;
: There are three reasons why it is preferable to align coding DNA at the amino acid level rather than directly as nucleotides. First, the DNA alphabet has only four letters compared to 20 amino acids, which means that even unrelated DNA sequences will share roughly 25% identity by chance alone, making it much harder to distinguish true homology from noise. Second, protein alignments benefit from empirical substitution matrices such as BLOSUM-62, which capture the fact that amino acid replacements tend to be conservative. Equivalent matrices for DNA are far less informative because nucleotide substitution patterns vary strongly between genes and organisms. Third, synonymous substitutions accumulate faster than non-synonymous ones, so DNA sequences diverge more rapidly than the proteins they encode. Taken together, these factors mean that the phylogenetic signal in a DNA alignment erodes much faster than in the corresponding protein alignment.&lt;br /&gt;
&lt;br /&gt;
: However, while protein-level alignment gives us the most reliable homology signal, it discards exactly the information we need for studying selection: the synonymous substitutions. Estimating the ratio of non-synonymous to synonymous substitution rates (dN/dS) requires a codon-level alignment where analogous codon positions are properly lined up. We would therefore like to construct a multiple alignment at the DNA level, but using information at the protein level, and the RevTrans server does exactly that.&lt;br /&gt;
&lt;br /&gt;
: &#039;&#039;&#039;RevTrans&#039;&#039;&#039; takes as input an unaligned set of DNA sequences, automatically translates them to the equivalent amino acid sequences, constructs a multiple alignment of the protein sequences, and finally uses the protein alignment as a template for constructing a multiple DNA alignment. Because each amino acid maps back to its original codon, gaps are always inserted in groups of three and codon boundaries are preserved throughout the alignment. This is essential for the dN/dS analyses we will carry out in this exercise: if codon positions are misaligned, synonymous and non-synonymous changes cannot be correctly distinguished.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Construct RevTrans alignment&#039;&#039;&#039;&lt;br /&gt;
:* Open RevTrans server page: https://services.healthtech.dtu.dk/services/RevTrans-2.0/&lt;br /&gt;
:* On the RevTrans page: Choose the file gp120.fasta as input (or copy and paste the sequence into the sequence window)&lt;br /&gt;
:* Click the &amp;quot;Submit query&amp;quot; button&lt;br /&gt;
:* When the alignment is done you may have to click link named &amp;quot;here&amp;quot; to go to results page&lt;br /&gt;
:* Download DNA alignment, by right-clicking the link for &amp;quot;Download alignment in FASTA format&amp;quot;, and choosing &amp;quot;Save link as...&amp;quot; (save file under the name gp120align.fasta and make sure to save the file in the directory modelselect).&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Convert alignment to NEXUS format&#039;&#039;&#039;&lt;br /&gt;
: Convert the fasta file to NEXUS format and save file in the modelselect directory under the name gp120.nexus&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
== Model selection using AIC and jModelTest2 ==&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Question 1&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
: As part of the present analysis we are going to build a phylogenetic tree based on the DNA alignment constructed above. We will construct the tree using maximum likelihood, but to do that we first have to decide which substitution model we want to use. Specifically, we are interested in selecting a model that achieves a good balance between fit to the data and number of parameters. We will investigate this issue by fitting a set of 56 different models to our data and then comparing them using AIC.&lt;br /&gt;
&lt;br /&gt;
: In the first part of the exercise, however, you will only compare three closely related models by hand. The purpose of this restricted comparison is not primarily to identify the globally best model, but to understand how AIC, ΔAIC, and Akaike weights are computed and interpreted. Because only three models are included in this manual comparison, the weights you compute will describe relative support &#039;&#039;&#039;within this restricted set only&#039;&#039;&#039;.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Start jmodeltest2&#039;&#039;&#039;&lt;br /&gt;
 jmodeltest&lt;br /&gt;
&#039;&#039;&#039;Load data set&#039;&#039;&#039;&lt;br /&gt;
:* File -&amp;gt; Load DNA alignment -&amp;gt; File Format -&amp;gt; Select &amp;quot;All files&amp;quot;&lt;br /&gt;
:* Navigate to gp120.nexus and load it&lt;br /&gt;
&#039;&#039;&#039;Fit 56 models&#039;&#039;&#039;&lt;br /&gt;
:* Analysis -&amp;gt; Compute likelihood scores&lt;br /&gt;
:* Select &amp;quot;7&amp;quot; under &amp;quot;Number of substitution schemes&amp;quot;&lt;br /&gt;
:* Select &amp;quot;Fixed BIONJ-JC&amp;quot; under &amp;quot;Base tree for likelihood calculations&amp;quot;&lt;br /&gt;
:* Click &amp;quot;Compute likelihoods&amp;quot;&lt;br /&gt;
&lt;br /&gt;
: This causes jmodeltest2 to perform the following actions: first a neighbor joining tree is constructed using the Jukes and Cantor model. Then the tree is fixed and used as the basis for fitting a set of 56 different models to the data. For each model, the estimated model parameters and the negative log-likelihood are recorded. In addition to varying sets of substitution rate parameters (JC, K2P, ...), some of these models also include extra parameters that take into account the presence of different rates between sites. This is done in two ways: (1) by fitting a gamma distribution of rates (&amp;quot;+G&amp;quot;), and (2) by allowing for a proportion of constant (&amp;quot;invariable&amp;quot;) sites (&amp;quot;+I&amp;quot;).&lt;br /&gt;
&lt;br /&gt;
: Wait until jmodeltest2 is done fitting all 56 models (this will take a little while depending on your computer).&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Inspect result&#039;&#039;&#039;&lt;br /&gt;
:* Results -&amp;gt; Show results table&lt;br /&gt;
: For each model this table lists the negative log-likelihood (&amp;quot;-lnL&amp;quot;), the number of parameters (&amp;quot;p&amp;quot; - note this includes branch lengths), and estimates of all model parameters (excluding branch lengths).&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Manually compute Akaike weights for three substitution models&#039;&#039;&#039;&lt;br /&gt;
: Use AIC-based model comparison to investigate which of the following three substitution models are best supported as descriptions of how the sequences have evolved:&lt;br /&gt;
:* Jukes and Cantor with fraction of invariant sites (JC+I)&lt;br /&gt;
:* Jukes and Cantor with gamma-distributed rates over sites (JC+G)&lt;br /&gt;
:* Jukes and Cantor with invariant sites and gamma-distributed rates (JC+I+G)&lt;br /&gt;
: Before you can do the computation you need to know the log likelihood and the number of parameters for each model. Locate these values in the table for the JC+I, JC+G, and JC+I+G models, and write them down. Close the window with the result table when you are done.&lt;br /&gt;
&lt;br /&gt;
: Make sure to get the signs right: the values reported in the table are -lnL values, so you will need to reverse the sign to get the lnL (the lnL values you write down should be negative).&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Question: &#039;&#039;&#039;Use the recipe above to compute AIC, ΔAIC, and Akaike weight for the three models. Report the results in a table similar to the one shown above, and verify that the three weights sum to 1 (up to rounding error).&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Question 2&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
: Based on the Akaike weights, which of the three models has the strongest support? Is the support strongly concentrated on one model, or is there still substantial support for one or both of the others?&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Question 3&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
: What you just did manually for JC+I, JC+G and JC+I+G, jmodeltest2 can do automatically for the full set of 56 fitted models. Specifically, it uses the list of negative log likelihoods and parameter counts in the table to compute AIC values and Akaike weights for the full candidate set:&lt;br /&gt;
:* Analysis -&amp;gt; Do AIC calculations -&amp;gt;&lt;br /&gt;
:* Select &amp;quot;Write PAUP* block&amp;quot;&lt;br /&gt;
:* click &amp;quot;Do AIC calculations&amp;quot;&lt;br /&gt;
:* Results -&amp;gt; Show results table&lt;br /&gt;
:* Select &amp;quot;AIC&amp;quot; tab&lt;br /&gt;
:* &#039;&#039;&#039;IMPORTANT:&#039;&#039;&#039; SHIFT+click on the header of the &amp;quot;weight&amp;quot; column. This sorts the rows according to model weight, in descending order.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Question: &#039;&#039;&#039;What model was selected by modeltest based on the AIC values? (This will be the model with the highest weight - and lowest AIC - and will be in the first row of the results table after SHIFT-clicking the &amp;quot;weight&amp;quot; header).&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Question 4&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
: In the same AIC results table, now click (not SHIFT+click) the header of the &amp;quot;-lnL&amp;quot; column. In this table, a normal click sorts in ascending order, whereas SHIFT+click sorts in descending order. The model at the top will therefore be the one with the smallest value of &amp;quot;-lnL&amp;quot; (equivalently: the highest likelihood). Compare this model to the one with the highest Akaike weight.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Question: &#039;&#039;&#039;Is the model with the highest likelihood also the model with the highest Akaike weight? If not, explain briefly why these two rankings can differ.&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
== Phylogenetic tree inference ==&lt;br /&gt;
&lt;br /&gt;
=== Tree inference using the model selected by jModelTest2 (PAUP) ===&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Question 5&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
: Above, you used jModelTest2 to select a substitution model based on AIC. You will now use that model to construct a maximum likelihood tree in &#039;&#039;&#039;PAUP&#039;&#039;&#039;. The purpose of doing this in PAUP is mainly pedagogical: it makes the individual steps of model-based phylogenetic inference more explicit. In practice, one would often use a program such as IQ-TREE 3 (see next section).&lt;br /&gt;
&lt;br /&gt;
: Close the results table. In the main window you should now scroll up to the lines giving PAUP commands that implement the selected model. The command is enclosed between &amp;quot;BEGIN PAUP&amp;quot; and &amp;quot;END;&amp;quot; and should look something like this:&lt;br /&gt;
 Lset Base=(0.4064, [...]&lt;br /&gt;
: You will need to copy this command to a PAUP session in the next step.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Start PAUP&#039;&#039;&#039;&lt;br /&gt;
 paup&lt;br /&gt;
: Above, you used jModelTest2 to select the most suitable substitution model for the present data set. You will now use this model to construct a maximum likelihood tree. You will use PAUP for this purpose. (Note: it is possible to create a maximum likelihood tree, or even a model-averaged tree, directly from jModelTest2, but we will instead do it in PAUP in order to more clearly see each step that is taken.)&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Load alignment:&#039;&#039;&#039;&lt;br /&gt;
 execute gp120.nexus&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Set tree-building criterion to maximum likelihood&#039;&#039;&#039;&lt;br /&gt;
 set criterion=likelihood&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Set model parameters to winning estimates&#039;&#039;&#039;&lt;br /&gt;
: Above, you located a set of lines in the jModelTest2 output giving a PAUP command that sets the model parameters to the estimates found for the winning model. Copy and paste this lset command (without the BEGIN and END parts) into the window where PAUP is running.&lt;br /&gt;
&lt;br /&gt;
 PASTE LSET COMMAND FROM MODELTEST RUN HERE&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Find best tree using selected model&#039;&#039;&#039;&lt;br /&gt;
: Still in the PAUP window, enter the following command:&lt;br /&gt;
 hsearch swap=tbr start=nj&lt;br /&gt;
: This command causes PAUP to perform a heuristic search for the best maximum likelihood tree. Once an initial tree has been constructed, the heuristic search proceeds by rearrangements of the &amp;quot;tree bisection and reconnection&amp;quot; type (TBR). We are using the model selected by modeltest, and the parameter estimates found by modeltest for that model. You could also choose to estimate all the model parameters as part of this step (i.e., at the same time as finding the best tree), but fixing them improves speed tremendously. Finding the best tree should take at most a few minutes.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Save best tree to file&#039;&#039;&#039;&lt;br /&gt;
 savetrees format=newick brlens=yes file=gp120tree_paup.phy from=1 to=1&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Quit program&#039;&#039;&#039;&lt;br /&gt;
 quit&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Have a look at the tree:&#039;&#039;&#039;&lt;br /&gt;
: You have now produced an unrooted tree of the HIV sequences and saved it in the file gp120tree_paup.phy. Note that in this exercise we will not be interested in the tree as such - our focus is instead on finding positive selection at a subset of codon positions, and the tree is just something we need in order to fit the different codon models to the data. If you want to see the tree, you can do so with the following command:&lt;br /&gt;
 figtree gp120tree_paup.phy &amp;amp;&lt;br /&gt;
: There is no meaningful root placed on this tree, so you may want to choose the unrooted view (the third icon in the Layout section of the FigTree window). Close the FigTree window when you have had a look.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Question: &#039;&#039;&#039;Which substitution model did you use for the PAUP tree search, and what was the negative log likelihood of the best tree found under this model?&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
=== One-step model selection and tree inference using IQ-TREE 3 ===&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Question 6&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
: In real-life phylogenetic analysis one would often use a modern program such as &#039;&#039;&#039;IQ-TREE 3&#039;&#039;&#039;, which can carry out model selection and maximum likelihood tree reconstruction in a single analysis. You will now repeat the analysis using this more integrated approach.&lt;br /&gt;
&lt;br /&gt;
: Run IQ-TREE 3 on the alignment using the following command:&lt;br /&gt;
 iqtree3 -s gp120.nexus -m MFP&lt;br /&gt;
&lt;br /&gt;
: Here, &amp;quot;-m MFP&amp;quot; tells IQ-TREE 3 to perform automatic model selection using &amp;quot;ModelFinder Plus&amp;quot; before reconstructing the tree.&lt;br /&gt;
&lt;br /&gt;
: When the run has finished, inspect the output written to the screen and to the file ending in &amp;quot;.iqtree&amp;quot;. Note that IQ-TREE 3 reports several model-selection criteria in the output. The line near the top of the ModelFinder section states which criterion was used to choose the final model. Locate the substitution model selected by IQ-TREE 3 and the log likelihood of the best tree.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Question: &#039;&#039;&#039;Which substitution model was selected by IQ-TREE 3, and according to which information criterion was it selected? What was the log likelihood of the best tree? Compare this to the earlier jModelTest2 + PAUP analysis. Is the selected model exactly the same? Is the likelihood exactly the same? Briefly suggest one reason why the two analyses might not give identical results.&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
== Detection of positively selected sites in gp120 ==&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Question 7&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
: There is much more to phylogenetic analyses than merely reconstructing trees. One interesting result of probabilistic methods is that the parameters of a model are estimated as part of the optimization procedure. This means that once such a model has been fitted to the data, it is possible to investigate these estimated parameter values to learn features about the evolutionary history of the sequences under investigation. In the present example we will focus on whether we can detect positively selected sites in our data set, defined as sites where the dN/dS ratio is larger than 1. We do that by using a codon substitution model where the dN/dS ratio is one of its parameters.&lt;br /&gt;
&lt;br /&gt;
: A further strength of the probabilistic approach is that you get a probabilistic measure of how well any model fits the data. This means you can use a stringent approach to determine which model fits the data best. In this framework one uses likelihoods (probabilities of data given model) to determine which model fits the data best. As you saw above, it is for instance possible to compute AIC values and weights (model probabilities) from the likelihood values of fitted models. Since each model essentially corresponds to a hypothesis about the evolutionary history of the data, we can thus use a stringent statistical approach to decide which hypothesis best describes our data.&lt;br /&gt;
&lt;br /&gt;
: In outline, you will now use the following steps to investigate whether there is any evidence for positively selected codons in your data set:&lt;br /&gt;
&lt;br /&gt;
:* Fit model M1, which assumes there are two classes of codons in the sequence: some with dN/dS &amp;lt; 1, some with dN/dS = 1.&lt;br /&gt;
:* Fit model M2, which assumes 3 distinct classes of codons: two with dN/dS ratios as for M1, and one extra class with dN/dS &amp;gt; 1.&lt;br /&gt;
:* Assess the strength of evidence for the two models using AIC-based model probabilities&lt;br /&gt;
:* If M2 is better: identify the positively selected codons&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Edit the parameter file&#039;&#039;&#039;&lt;br /&gt;
 nedit codeml.ctl &amp;amp;&lt;br /&gt;
: The file &amp;quot;codeml.ctl&amp;quot; contains several settings that control how &#039;&#039;&#039;codeml&#039;&#039;&#039; is run. Before continuing, edit the file and save it so that the following arguments have exactly these values:&lt;br /&gt;
 &#039;&#039;&#039;seqfile =  gp120align.fasta&#039;&#039;&#039;: name of alignment file&lt;br /&gt;
 &#039;&#039;&#039;treefile =  gp120.nexus.treefile&#039;&#039;&#039;: name of tree file produced by IQ-TREE 3&lt;br /&gt;
 &#039;&#039;&#039;seqtype = 1&#039;&#039;&#039;: tells the program that our data consists of coding DNA&lt;br /&gt;
 &#039;&#039;&#039;NSsites = 1 2&#039;&#039;&#039;: tells the program to analyze models M1 and M2&lt;br /&gt;
 &#039;&#039;&#039;cleandata = 0&#039;&#039;&#039;: tells the program to keep positions with gaps&lt;br /&gt;
&lt;br /&gt;
: &#039;&#039;&#039;Important note:&#039;&#039;&#039; in this exercise, use the tree from &#039;&#039;&#039;IQ-TREE 3&#039;&#039;&#039; for the codeml analysis. On some systems, codeml may have problems with the PAUP tree file or may stop with a confusing error message even though the downstream results are still written to the output file. Using the IQ-TREE tree avoids this issue.&lt;br /&gt;
&lt;br /&gt;
: The settings entered by us will cause codeml to analyze two hypotheses about dN/dS ratios. M1 says there are two classes of codons with different dN/dS ratios in the sequence: one class with dN/dS &amp;lt; 1 (codons under purifying or negative selection), and one class with dN/dS = 1 (no selection - neutrally evolving sites). M2 says there are 3 distinct dN/dS ratios for different sites in the sequence: one class with dN/dS &amp;lt; 1, one class with dN/dS = 1 (these are the same type of classes as for M1), and one class with dN/dS &amp;gt; 1 (corresponding to sites under positive selection). The value of the dN/dS ratios (for those classes that have dN/dS &amp;lt; 1 or dN/dS &amp;gt; 1), the fraction of sites belonging to each class, and the position of sites belonging to each class, are unknown at first and will be determined during the analysis.&lt;br /&gt;
&lt;br /&gt;
: (Regarding &amp;quot;cleandata&amp;quot; setting: &amp;quot;cleandata = 1&amp;quot; would cause codeml to discard any alignment columns with gaps or ambiguity symbols. For alignments with many sequences this can lead to discarding the majority of sites and I would therefore recommend not using that setting. At the same time you do want to be wary of columns where most sequences have gaps, since inference of selection for these will be very uncertain. It might be better to discard such columns from an alignment before doing the codeml analysis.)&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Start the analysis&#039;&#039;&#039;&lt;br /&gt;
 codeml&lt;br /&gt;
: This will start the codeml program using the settings in the file codeml.ctl. Depending on your computer, this will take some minutes to finish. (You may be able to see how the optimization procedure results in progressively better fits: the likelihood increases, meaning that negative log-likelihood decreases, as the fit improves.)&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Inspect result file:&#039;&#039;&#039;&lt;br /&gt;
: Wait for the run to finish, and then look at the result file:&lt;br /&gt;
 nedit selection.results &amp;amp;&lt;br /&gt;
: This file contains a wealth of information concerning your analysis. You may want to turn off line-wrapping to more clearly see the structure of the file (there will be some long lines that will otherwise wrap around). The top part of the file gives an overview of your sequences, codon usage and nucleotide frequencies. You can ignore this information for now, and move on to the interesting part, namely the model likelihoods and parameter values.&lt;br /&gt;
&lt;br /&gt;
: &#039;&#039;&#039;Note:&#039;&#039;&#039; On some systems codeml may print an error message near the end of the run, for instance during the BEB step, even though the relevant results have already been written to &#039;&#039;&#039;selection.results&#039;&#039;&#039;. If this happens, inspect &#039;&#039;&#039;selection.results&#039;&#039;&#039; before assuming that the run failed completely.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Find likelihood, and number of free parameters for model M1&#039;&#039;&#039;&lt;br /&gt;
 Search ==&amp;gt; Find... ==&amp;gt; enter &amp;quot;Model 1&amp;quot; and click Find&lt;br /&gt;
: You are now in the region of the result file where the model likelihoods and parameter estimates are noted. Now locate a line that looks a bit like the one shown below:&lt;br /&gt;
 lnL(ntime: 72  np: 74):  -4242.470345     +0.000000&lt;br /&gt;
: Identify the number of &amp;quot;free parameters&amp;quot;, K, used in model M1: this is indicated by &amp;quot;np&amp;quot;, and is 74 in the example shown above (most of these parameters are branch lengths in the tree; specifically, the number of branch length parameters is indicated by &amp;quot;ntime&amp;quot;, and is 72 in this example). Also note the log-likelihood of the fitted model. This is the number right after the parenthesis, and is -4242.470345 in the example here.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Question: &#039;&#039;&#039;What are the values of K and lnL for model M1?&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Question 8&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Find dN/dS ratios and codon class proportions for model M1&#039;&#039;&#039;&lt;br /&gt;
: Scroll down a few lines until you get to a small table similar to this:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
      dN/dS for site classes (K=2)&lt;br /&gt;
      p:   0.75111  0.24889&lt;br /&gt;
      w:   0.06583  1.00000&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
: This gives a summary of the dN/dS ratios that were found in the data set. The line starting &amp;quot;w:&amp;quot; lists the two dN/dS ratios that were found (in this case 0.06583 and 1.00000 - the last one was pre-specified by us as part of the model and was therefore not a free parameter). In this context &amp;quot;w&amp;quot; means &amp;quot;omega&amp;quot;, which is the symbol typically used to represent the dN/dS rate ratio. The line starting &amp;quot;p:&amp;quot; gives the proportion of codon sites belonging to each of the dN/dS ratio classes (in the example above approximately 75% belong to the first class, while 25% of all sites belong to the class having dN/dS = 1.00000).&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Question: &#039;&#039;&#039;What are the dN/dS value (w) and proportion (p) of sites for both classes? Report the following values: p(class1), w(class1), p(class2), w(class2)&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Question 9&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Find likelihood, and K for model M2&#039;&#039;&#039;&lt;br /&gt;
: Scroll past the M1 output until you get to the results for model M2.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Question: &#039;&#039;&#039;What are the values of K and lnL for model M2?&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Question 10&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Find dN/dS ratios and codon class proportions for model M2&#039;&#039;&#039;&lt;br /&gt;
: Now scroll down a few lines until you get to a small table similar to the one you examined for M1 before. For this model there are 3 separate classes of codons.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Question: &#039;&#039;&#039;What are the dN/dS value (w) and proportion (p) of sites for all three classes? Report these values: p(class1), w(class1), p(class2), w(class2), p(class3), w(class3)&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Question 11&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Assess strength of evidence for models M1 and M2&#039;&#039;&#039;&lt;br /&gt;
: M2 will always have a better (higher) log-likelihood than model M1 because M2 has more free parameters, and M1 is nested within M2. You should now use the recipe given above to compute AIC values and model probabilities for M1 and M2.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Question: &#039;&#039;&#039;Report: AIC, ΔAIC, weight (model probability) for M1 and M2&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Question 12: &#039;&#039;&#039;Is M2 better than M1?&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Question 13&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Examine list of positively selected sites&#039;&#039;&#039;&lt;br /&gt;
: If your M2 is clearly better than M1, then you have evidence for the existence of positively selected sites in the gp120 gene. Now scroll down to the end of the result file and locate a list similar to the one below. Note: this is the &amp;quot;Bayes Empirical Bayes&amp;quot; table, not the &amp;quot;Naive Empirical Bayes&amp;quot; table.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Bayes Empirical Bayes (BEB) analysis&lt;br /&gt;
Positively selected sites&lt;br /&gt;
&lt;br /&gt;
         Prob(w&amp;gt;1)     mean w&lt;br /&gt;
&lt;br /&gt;
    25 A 0.959*        3.133 +- 0.769&lt;br /&gt;
    27 P 0.906         2.990 +- 0.877&lt;br /&gt;
    56 K 0.987*        3.197 +- 0.687&lt;br /&gt;
    59 V 0.915         3.032 +- 0.873&lt;br /&gt;
    78 R 0.637         2.351 +- 1.129&lt;br /&gt;
    88 K 0.573         2.148 +- 1.077&lt;br /&gt;
    95 V 0.925         3.046 +- 0.843&lt;br /&gt;
    ...&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
: It is not important what the distinction is in this context, but very briefly: NEB ignores the fact that there is uncertainty about maximum likelihood estimates, especially for smaller data sets (for instance w for some codon is perhaps not exactly 3.046, but could lie in a region around that value), whereas [https://pubmed.ncbi.nlm.nih.gov/15689528/ BEB accounts for that uncertainty].&lt;br /&gt;
&lt;br /&gt;
: This gives you a list of which residues (if any) were found to belong to the positively selected dN/dS class. Also listed is the probability that the site really is in the codon class where dN/dS &amp;gt; 1, and a weighted average of the w at the site.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Question: &#039;&#039;&#039;List all sites having more than 95% probability of belonging to the positively selected class.&lt;/div&gt;</summary>
		<author><name>Gorm</name></author>
	</entry>
	<entry>
		<id>https://teaching.healthtech.dtu.dk/22115/index.php?title=Model_selection&amp;diff=277</id>
		<title>Model selection</title>
		<link rel="alternate" type="text/html" href="https://teaching.healthtech.dtu.dk/22115/index.php?title=Model_selection&amp;diff=277"/>
		<updated>2026-04-15T09:18:50Z</updated>

		<summary type="html">&lt;p&gt;Gorm: /* Tree inference using the model selected by jModelTest2 (PAUP) */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;This exercise is part of the course  [[22115_-_Computational_Molecular_Evolution|Computational Molecular Evolution (22115)]].&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Overview ==&lt;br /&gt;
&lt;br /&gt;
: In this exercise you are going to investigate features of HIV-1 evolution. You will do this by analyzing a large set of env-genes from HIV-1, subtype B. specifically, the DNA sequences analyzed here correspond to a region surrounding the hypervariable V3 region of the gp120 protein.&lt;br /&gt;
&lt;br /&gt;
: Like other retroviruses, particles of HIV are made up of 2 copies of a single-stranded RNA genome packaged inside a protein core, or capsid. The core particle also contains viral proteins that are essential for the early steps of the virus life cycle, such as reverse transcription and integration. A lipid envelope, derived from the infected cell, surrounds the core particle. Embedded in this envelope are the surface glycoproteins of HIV: gp120 and gp41. The gp120 protein is crucial for binding of the virus particle to target cells, while gp41 is important for the subsequent fusion event. It is the specific affinity of gp120 for the CD4 protein that targets HIV to those cells of the immune system that express CD4 on their surface (e.g., T-helper lymphocytes, monocytes, and macrophages).&lt;br /&gt;
&lt;br /&gt;
: The role gp120 plays in infection and the fact that it is situated on the surface of the HIV particle, means it is an obvious target for the immune response. That means that there may be a considerable selective pressure on gp120 for creating immune-escape mutants, where amino acids in the gp120 epitopes have been substituted. In this exercise you will construct a maximum likelihood tree that we will subsequently use to investigate whether you can detect such a selective pressure on parts of gp120, again using maximum likelihood methods.&lt;br /&gt;
&lt;br /&gt;
: One major goal with the exercise is to introduce you to statistically based methods for assessing the strength of evidence for a set of alternative hypotheses about some biological system of interest. The model selection method we will use is AIC (Akaike Information Criterion), based on which you will compute model probabilities. A second goal is to make you aware that phylogenetic analysis is not only about constructing trees, but that it is also a useful framework for analyzing biological questions more generally.&lt;br /&gt;
&lt;br /&gt;
: Specifically, you will&lt;br /&gt;
&lt;br /&gt;
:# perform a multiple alignment of gp120 DNA sequences taking protein-level information into account (using revtrans).&lt;br /&gt;
:# select a suitable nucleotide substitution model (using jmodeltest2)&lt;br /&gt;
:# construct a phylogenetic tree (using PAUP).&lt;br /&gt;
:# try to detect positively selected sites in gp120 (using PAML).&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
== Recipe for computing AIC values and model probabilities ==&lt;br /&gt;
&lt;br /&gt;
: Later in today&#039;s exercise you will be asked to compute AIC values and model probabilities. Return to this section and follow the instructions when you need to do so.&lt;br /&gt;
&lt;br /&gt;
:# Fit a set of models to your data, note the maximized log likelihoods (lnL) and the number of free parameters (K) for each model in the investigated set. The models you fit should represent a plausible and comprehensive set of hypotheses about your data.&lt;br /&gt;
:# Compute AIC for each of the models: &#039;&#039;&#039;AIC = -2 x lnL + 2K&#039;&#039;&#039;.  &amp;lt;br&amp;gt;For example: a model with lnL = -2010 and K = 5 will have AIC = -2 x -2010 + 2 x 5 = 4030.&lt;br /&gt;
:# Identify the model with the smallest AIC (this is the best model in the set). We will call the AIC for this model &#039;&#039;&#039;&amp;quot;AICmin&amp;quot;&#039;&#039;&#039;.&lt;br /&gt;
:# Compute the &amp;quot;ΔAIC&amp;quot; values for each model: &#039;&#039;&#039;ΔAIC = AIC - AICmin&#039;&#039;&#039; &amp;lt;br&amp;gt;For each model subtract the minimum AIC value. The best model will have a ΔAIC of zero. The rest of the models will have positive ΔAICs.&lt;br /&gt;
:# For each model compute the following quantity: &#039;&#039;&#039;numerator = exp(-0.5 x ΔAIC)&#039;&#039;&#039; &amp;lt;br&amp;gt;For example, a model with ΔAIC=4.2 will have numerator = exp(-0.5 x 4.2) = exp(-2.1) = 0.1225. Also compute the &#039;&#039;&#039;sum&#039;&#039;&#039; of the numerator values for all models.&lt;br /&gt;
:# Finally, the model probabilities (also called model weights) for each model are found as:&amp;lt;br&amp;gt; &#039;&#039;&#039;P(model) = numerator / sum&#039;&#039;&#039; &amp;lt;br&amp;gt;For example, if sum = 3.75 and a model has numerator = 1.3, then it has P(model) = 1.3 / 3.75 = 0.35&lt;br /&gt;
&lt;br /&gt;
: You may want to keep track of the computations by constructing a table along the following lines:&lt;br /&gt;
[[File:Molevol-Downloads-aictable.png|700px]]&lt;br /&gt;
&lt;br /&gt;
: Note that model probabilities can also be computed using Bayesian methods. One advantage of Bayesian methods over AIC is that instead of relying on a point estimate, uncertainty about parameter values is accounted for by integrating over all possible values (typically using MCMC).&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
== Getting started ==&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Create working directory, copy files&#039;&#039;&#039;&lt;br /&gt;
: In the command below: Instead of /path/to/molevol enter the path to the directory where you have placed your course files (for instance cd /Users/bob/Documents/molevol, or cd /home/student/molevol).&lt;br /&gt;
 cd /path/to/molevol&lt;br /&gt;
 mkdir modelselect&lt;br /&gt;
 cd modelselect&lt;br /&gt;
 cp ../data/gp120.fasta ./gp120.fasta&lt;br /&gt;
 cp ../data/codeml.ctl ./codeml.ctl&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Have a look at the DNA data file:&#039;&#039;&#039;&lt;br /&gt;
 nedit gp120.fasta &amp;amp;&lt;br /&gt;
: The file contains several DNA sequences from HIV-1, subtype B. The sequences are approximately 500 bp long, and correspond to a region surrounding the hypervariable V3 region in the gene encoding gp120. Close the nedit window when you&#039;ve had a look.&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
== Protein-guided alignment of coding DNA ==&lt;br /&gt;
&lt;br /&gt;
: There are three reasons why it is preferable to align coding DNA at the amino acid level rather than directly as nucleotides. First, the DNA alphabet has only four letters compared to 20 amino acids, which means that even unrelated DNA sequences will share roughly 25% identity by chance alone, making it much harder to distinguish true homology from noise. Second, protein alignments benefit from empirical substitution matrices such as BLOSUM-62, which capture the fact that amino acid replacements tend to be conservative. Equivalent matrices for DNA are far less informative because nucleotide substitution patterns vary strongly between genes and organisms. Third, synonymous substitutions accumulate faster than non-synonymous ones, so DNA sequences diverge more rapidly than the proteins they encode. Taken together, these factors mean that the phylogenetic signal in a DNA alignment erodes much faster than in the corresponding protein alignment.&lt;br /&gt;
&lt;br /&gt;
: However, while protein-level alignment gives us the most reliable homology signal, it discards exactly the information we need for studying selection: the synonymous substitutions. Estimating the ratio of non-synonymous to synonymous substitution rates (dN/dS) requires a codon-level alignment where analogous codon positions are properly lined up. We would therefore like to construct a multiple alignment at the DNA level, but using information at the protein level, and the RevTrans server does exactly that.&lt;br /&gt;
&lt;br /&gt;
: &#039;&#039;&#039;RevTrans&#039;&#039;&#039; takes as input an unaligned set of DNA sequences, automatically translates them to the equivalent amino acid sequences, constructs a multiple alignment of the protein sequences, and finally uses the protein alignment as a template for constructing a multiple DNA alignment. Because each amino acid maps back to its original codon, gaps are always inserted in groups of three and codon boundaries are preserved throughout the alignment. This is essential for the dN/dS analyses we will carry out in this exercise: if codon positions are misaligned, synonymous and non-synonymous changes cannot be correctly distinguished.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Construct RevTrans alignment&#039;&#039;&#039;&lt;br /&gt;
:* Open RevTrans server page: https://services.healthtech.dtu.dk/services/RevTrans-2.0/&lt;br /&gt;
:* On the RevTrans page: Choose the file gp120.fasta as input (or copy and paste the sequence into the sequence window)&lt;br /&gt;
:* Click the &amp;quot;Submit query&amp;quot; button&lt;br /&gt;
:* When the alignment is done you may have to click link named &amp;quot;here&amp;quot; to go to results page&lt;br /&gt;
:* Download DNA alignment, by right-clicking the link for &amp;quot;Download alignment in FASTA format&amp;quot;, and choosing &amp;quot;Save link as...&amp;quot; (save file under the name gp120align.fasta and make sure to save the file in the directory modelselect).&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Convert alignment to NEXUS format&#039;&#039;&#039;&lt;br /&gt;
: Convert the fasta file to NEXUS format and save file in the modelselect directory under the name gp120.nexus&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
== Model selection using AIC and jModelTest2 ==&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Question 1&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
: As part of the present analysis we are going to build a phylogenetic tree based on the DNA alignment constructed above. We will construct the tree using maximum likelihood, but to do that we first have to decide which substitution model we want to use. Specifically, we are interested in selecting a model that achieves a good balance between fit to the data and number of parameters. We will investigate this issue by fitting a set of 56 different models to our data and then comparing them using AIC.&lt;br /&gt;
&lt;br /&gt;
: In the first part of the exercise, however, you will only compare three closely related models by hand. The purpose of this restricted comparison is not primarily to identify the globally best model, but to understand how AIC, ΔAIC, and Akaike weights are computed and interpreted. Because only three models are included in this manual comparison, the weights you compute will describe relative support &#039;&#039;&#039;within this restricted set only&#039;&#039;&#039;.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Start jmodeltest2&#039;&#039;&#039;&lt;br /&gt;
 jmodeltest&lt;br /&gt;
&#039;&#039;&#039;Load data set&#039;&#039;&#039;&lt;br /&gt;
:* File -&amp;gt; Load DNA alignment -&amp;gt; File Format -&amp;gt; Select &amp;quot;All files&amp;quot;&lt;br /&gt;
:* Navigate to gp120.nexus and load it&lt;br /&gt;
&#039;&#039;&#039;Fit 56 models&#039;&#039;&#039;&lt;br /&gt;
:* Analysis -&amp;gt; Compute likelihood scores&lt;br /&gt;
:* Select &amp;quot;7&amp;quot; under &amp;quot;Number of substitution schemes&amp;quot;&lt;br /&gt;
:* Select &amp;quot;Fixed BIONJ-JC&amp;quot; under &amp;quot;Base tree for likelihood calculations&amp;quot;&lt;br /&gt;
:* Click &amp;quot;Compute likelihoods&amp;quot;&lt;br /&gt;
&lt;br /&gt;
: This causes jmodeltest2 to perform the following actions: first a neighbor joining tree is constructed using the Jukes and Cantor model. Then the tree is fixed and used as the basis for fitting a set of 56 different models to the data. For each model, the estimated model parameters and the negative log-likelihood are recorded. In addition to varying sets of substitution rate parameters (JC, K2P, ...), some of these models also include extra parameters that take into account the presence of different rates between sites. This is done in two ways: (1) by fitting a gamma distribution of rates (&amp;quot;+G&amp;quot;), and (2) by allowing for a proportion of constant (&amp;quot;invariable&amp;quot;) sites (&amp;quot;+I&amp;quot;).&lt;br /&gt;
&lt;br /&gt;
: Wait until jmodeltest2 is done fitting all 56 models (this will take a little while depending on your computer).&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Inspect result&#039;&#039;&#039;&lt;br /&gt;
:* Results -&amp;gt; Show results table&lt;br /&gt;
: For each model this table lists the negative log-likelihood (&amp;quot;-lnL&amp;quot;), the number of parameters (&amp;quot;p&amp;quot; - note this includes branch lengths), and estimates of all model parameters (excluding branch lengths).&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Manually compute Akaike weights for three substitution models&#039;&#039;&#039;&lt;br /&gt;
: Use AIC-based model comparison to investigate which of the following three substitution models are best supported as descriptions of how the sequences have evolved:&lt;br /&gt;
:* Jukes and Cantor with fraction of invariant sites (JC+I)&lt;br /&gt;
:* Jukes and Cantor with gamma-distributed rates over sites (JC+G)&lt;br /&gt;
:* Jukes and Cantor with invariant sites and gamma-distributed rates (JC+I+G)&lt;br /&gt;
: Before you can do the computation you need to know the log likelihood and the number of parameters for each model. Locate these values in the table for the JC+I, JC+G, and JC+I+G models, and write them down. Close the window with the result table when you are done.&lt;br /&gt;
&lt;br /&gt;
: Make sure to get the signs right: the values reported in the table are -lnL values, so you will need to reverse the sign to get the lnL (the lnL values you write down should be negative).&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Question: &#039;&#039;&#039;Use the recipe above to compute AIC, ΔAIC, and Akaike weight for the three models. Report the results in a table similar to the one shown above, and verify that the three weights sum to 1 (up to rounding error).&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Question 2&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
: Based on the Akaike weights, which of the three models has the strongest support? Is the support strongly concentrated on one model, or is there still substantial support for one or both of the others?&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Question 3&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
: What you just did manually for JC+I, JC+G and JC+I+G, jmodeltest2 can do automatically for the full set of 56 fitted models. Specifically, it uses the list of negative log likelihoods and parameter counts in the table to compute AIC values and Akaike weights for the full candidate set:&lt;br /&gt;
:* Analysis -&amp;gt; Do AIC calculations -&amp;gt;&lt;br /&gt;
:* Select &amp;quot;Write PAUP* block&amp;quot;&lt;br /&gt;
:* click &amp;quot;Do AIC calculations&amp;quot;&lt;br /&gt;
:* Results -&amp;gt; Show results table&lt;br /&gt;
:* Select &amp;quot;AIC&amp;quot; tab&lt;br /&gt;
:* &#039;&#039;&#039;IMPORTANT:&#039;&#039;&#039; SHIFT+click on the header of the &amp;quot;weight&amp;quot; column. This sorts the rows according to model weight, in descending order.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Question: &#039;&#039;&#039;What model was selected by modeltest based on the AIC values? (This will be the model with the highest weight - and lowest AIC - and will be in the first row of the results table after SHIFT-clicking the &amp;quot;weight&amp;quot; header).&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Question 4&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
: In the same AIC results table, now click (not SHIFT+click) the header of the &amp;quot;-lnL&amp;quot; column. In this table, a normal click sorts in ascending order, whereas SHIFT+click sorts in descending order. The model at the top will therefore be the one with the smallest value of &amp;quot;-lnL&amp;quot; (equivalently: the highest likelihood). Compare this model to the one with the highest Akaike weight.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Question: &#039;&#039;&#039;Is the model with the highest likelihood also the model with the highest Akaike weight? If not, explain briefly why these two rankings can differ.&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
== Phylogenetic tree inference ==&lt;br /&gt;
&lt;br /&gt;
=== Tree inference using the model selected by jModelTest2 (PAUP) ===&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Question 5&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
: Above, you used jModelTest2 to select a substitution model based on AIC. You will now use that model to construct a maximum likelihood tree in &#039;&#039;&#039;PAUP&#039;&#039;&#039;. The purpose of doing this in PAUP is mainly pedagogical: it makes the individual steps of model-based phylogenetic inference more explicit. In practice, one would often use a program such as IQ-TREE 3 (see next section).&lt;br /&gt;
&lt;br /&gt;
: Close the results table. In the main window you should now scroll up to the lines giving PAUP commands that implement the selected model. The command is enclosed between &amp;quot;BEGIN PAUP&amp;quot; and &amp;quot;END;&amp;quot; and should look something like this:&lt;br /&gt;
 Lset Base=(0.4064, [...]&lt;br /&gt;
: You will need to copy this command to a PAUP session in the next step.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Start PAUP&#039;&#039;&#039;&lt;br /&gt;
 paup&lt;br /&gt;
: Above, you used jModelTest2 to select the most suitable substitution model for the present data set. You will now use this model to construct a maximum likelihood tree. You will use PAUP for this purpose. (Note: it is possible to create a maximum likelihood tree, or even a model-averaged tree, directly from jModelTest2, but we will instead do it in PAUP in order to more clearly see each step that is taken.)&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Load alignment:&#039;&#039;&#039;&lt;br /&gt;
 execute gp120.nexus&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Set tree-building criterion to maximum likelihood&#039;&#039;&#039;&lt;br /&gt;
 set criterion=likelihood&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Set model parameters to winning estimates&#039;&#039;&#039;&lt;br /&gt;
: Above, you located a set of lines in the jModelTest2 output giving a PAUP command that sets the model parameters to the estimates found for the winning model. Copy and paste this lset command (without the BEGIN and END parts) into the window where PAUP is running.&lt;br /&gt;
&lt;br /&gt;
 PASTE LSET COMMAND FROM MODELTEST RUN HERE&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Find best tree using selected model&#039;&#039;&#039;&lt;br /&gt;
: Still in the PAUP window, enter the following command:&lt;br /&gt;
 hsearch swap=tbr start=nj&lt;br /&gt;
: This command causes PAUP to perform a heuristic search for the best maximum likelihood tree. Once an initial tree has been constructed, the heuristic search proceeds by rearrangements of the &amp;quot;tree bisection and reconnection&amp;quot; type (TBR). We are using the model selected by modeltest, and the parameter estimates found by modeltest for that model. You could also choose to estimate all the model parameters as part of this step (i.e., at the same time as finding the best tree), but fixing them improves speed tremendously. Finding the best tree should take at most a few minutes.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Save best tree to file&#039;&#039;&#039;&lt;br /&gt;
 savetrees format=newick brlens=yes file=gp120tree_paup.phy from=1 to=1&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Quit program&#039;&#039;&#039;&lt;br /&gt;
 quit&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Have a look at the tree:&#039;&#039;&#039;&lt;br /&gt;
: You have now produced an unrooted tree of the HIV sequences and saved it in the file gp120tree_paup.phy. Note that in this exercise we will not be interested in the tree as such - our focus is instead on finding positive selection at a subset of codon positions, and the tree is just something we need in order to fit the different codon models to the data. If you want to see the tree, you can do so with the following command:&lt;br /&gt;
 figtree gp120tree_paup.phy &amp;amp;&lt;br /&gt;
: There is no meaningful root placed on this tree, so you may want to choose the unrooted view (the third icon in the Layout section of the FigTree window). Close the FigTree window when you have had a look.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Question: &#039;&#039;&#039;Which substitution model did you use for the PAUP tree search, and what was the negative log likelihood of the best tree found under this model?&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
=== One-step model selection and tree inference using IQ-TREE 3 ===&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Question 6&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
: In real-life phylogenetic analysis one would often use a modern program such as &#039;&#039;&#039;IQ-TREE 3&#039;&#039;&#039;, which can carry out model selection and maximum likelihood tree reconstruction in a single analysis. You will now repeat the analysis using this more integrated approach.&lt;br /&gt;
&lt;br /&gt;
: Run IQ-TREE 3 on the alignment using the following command:&lt;br /&gt;
 iqtree3 -s gp120.nexus -m MFP&lt;br /&gt;
&lt;br /&gt;
: Here, &amp;quot;-m MFP&amp;quot; tells IQ-TREE 3 to perform automatic model selection using &amp;quot;ModelFinder Plus&amp;quot; before reconstructing the tree.&lt;br /&gt;
&lt;br /&gt;
: When the run has finished, inspect the output written to the screen and to the file ending in &amp;quot;.iqtree&amp;quot;. Note that IQ-TREE 3 reports several model-selection criteria in the output. The line near the top of the ModelFinder section states which criterion was used to choose the final model. Locate the substitution model selected by IQ-TREE 3 and the log likelihood of the best tree.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Question: &#039;&#039;&#039;Which substitution model was selected by IQ-TREE 3, and according to which information criterion was it selected? What was the log likelihood of the best tree? Compare this to the earlier jModelTest2 + PAUP analysis. Is the selected model exactly the same? Is the likelihood exactly the same? Briefly suggest one reason why the two analyses might not give identical results.&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
== Detection of positively selected sites in gp120 ==&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Question 7&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
: There is much more to phylogenetic analyses than merely reconstructing trees. One interesting result of probabilistic methods is that the parameters of a model are estimated as part of the optimization procedure. This means that once such a model has been fitted to the data, it is possible to investigate these estimated parameter values to learn features about the evolutionary history of the sequences under investigation. In the present example we will focus on whether we can detect positively selected sites in our data set, defined as sites where the dN/dS ratio is larger than 1. We do that by using a codon substitution model where the dN/dS ratio is one of its parameters.&lt;br /&gt;
&lt;br /&gt;
: A further strength of the probabilistic approach is that you get a probabilistic measure of how well any model fits the data. This means you can use a stringent approach to determine which model fits the data best. In this framework one uses likelihoods (probabilities of data given model) to determine which model fits the data best. As you saw above, it is for instance possible to compute AIC values and weights (model probabilities) from the likelihood values of fitted models. Since each model essentially corresponds to a hypothesis about the evolutionary history of the data, we can thus use a stringent statistical approach to decide which hypothesis best describes our data.&lt;br /&gt;
&lt;br /&gt;
: In outline, you will now use the following steps to investigate whether there is any evidence for positively selected codons in your data set:&lt;br /&gt;
&lt;br /&gt;
:* Fit model M1, which assumes there are two classes of codons in the sequence: some with dN/dS &amp;lt; 1, some with dN/dS = 1.&lt;br /&gt;
:* Fit model M2, which assumes 3 distinct classes of codons: two with dN/dS ratios as for M1, and one extra class with dN/dS &amp;gt; 1.&lt;br /&gt;
:* Assess the strength of evidence for the two models using AIC-based model probabilities&lt;br /&gt;
:* If M2 is better: identify the positively selected codons&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Inspect the parameter file&#039;&#039;&#039;&lt;br /&gt;
 nedit codeml.ctl &amp;amp;&lt;br /&gt;
: The file &amp;quot;codeml.ctl&amp;quot; contains several settings that are relevant for running the program &#039;&#039;&#039;codeml&#039;&#039;&#039;. Find the following lines and ensure that the file contains these values:&lt;br /&gt;
 &#039;&#039;&#039;seqfile =  gp120align.fasta&#039;&#039;&#039;: name of alignment file&lt;br /&gt;
 &#039;&#039;&#039;treefile =  gp120.nexus.treefile&#039;&#039;&#039;: name of tree file produced by IQ-TREE 3&lt;br /&gt;
 &#039;&#039;&#039;seqtype = 1&#039;&#039;&#039;: tells the program that our data consists of coding DNA&lt;br /&gt;
 &#039;&#039;&#039;NSsites = 1 2&#039;&#039;&#039;: tells the program to analyze models M1 and M2&lt;br /&gt;
 &#039;&#039;&#039;cleandata = 0&#039;&#039;&#039;: tells the program to keep positions with gaps&lt;br /&gt;
&lt;br /&gt;
: &#039;&#039;&#039;Important note:&#039;&#039;&#039; in this exercise, use the tree from &#039;&#039;&#039;IQ-TREE 3&#039;&#039;&#039; for the codeml analysis. On some systems, codeml may have problems with the PAUP tree file or may stop with a confusing error message even though the downstream results are still written to the output file. Using the IQ-TREE tree avoids this issue.&lt;br /&gt;
&lt;br /&gt;
: The settings entered by us will cause codeml to analyze two hypotheses about dN/dS ratios. M1 says there are two classes of codons with different dN/dS ratios in the sequence: one class with dN/dS &amp;lt; 1 (codons under purifying or negative selection), and one class with dN/dS = 1 (no selection - neutrally evolving sites). M2 says there are 3 distinct dN/dS ratios for different sites in the sequence: one class with dN/dS &amp;lt; 1, one class with dN/dS = 1 (these are the same type of classes as for M1), and one class with dN/dS &amp;gt; 1 (corresponding to sites under positive selection). The value of the dN/dS ratios (for those classes that have dN/dS &amp;lt; 1 or dN/dS &amp;gt; 1), the fraction of sites belonging to each class, and the position of sites belonging to each class, are unknown at first and will be determined during the analysis.&lt;br /&gt;
&lt;br /&gt;
: (Regarding &amp;quot;cleandata&amp;quot; setting: &amp;quot;cleandata = 1&amp;quot; would cause codeml to discard any alignment columns with gaps or ambiguity symbols. For alignments with many sequences this can lead to discarding the majority of sites and I would therefore recommend not using that setting. At the same time you do want to be wary of columns where most sequences have gaps, since inference of selection for these will be very uncertain. It might be better to discard such columns from an alignment before doing the codeml analysis.)&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Start the analysis&#039;&#039;&#039;&lt;br /&gt;
 codeml&lt;br /&gt;
: This will start the codeml program using the settings in the file codeml.ctl. Depending on your computer, this will take some minutes to finish. (You may be able to see how the optimization procedure results in progressively better fits: the likelihood increases, meaning that negative log-likelihood decreases, as the fit improves.)&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Inspect result file:&#039;&#039;&#039;&lt;br /&gt;
: Wait for the run to finish, and then look at the result file:&lt;br /&gt;
 nedit selection.results &amp;amp;&lt;br /&gt;
: This file contains a wealth of information concerning your analysis. You may want to turn off line-wrapping to more clearly see the structure of the file (there will be some long lines that will otherwise wrap around). The top part of the file gives an overview of your sequences, codon usage and nucleotide frequencies. You can ignore this information for now, and move on to the interesting part, namely the model likelihoods and parameter values.&lt;br /&gt;
&lt;br /&gt;
: &#039;&#039;&#039;Note:&#039;&#039;&#039; On some systems codeml may print an error message near the end of the run, for instance during the BEB step, even though the relevant results have already been written to &#039;&#039;&#039;selection.results&#039;&#039;&#039;. If this happens, inspect &#039;&#039;&#039;selection.results&#039;&#039;&#039; before assuming that the run failed completely.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Find likelihood, and number of free parameters for model M1&#039;&#039;&#039;&lt;br /&gt;
 Search ==&amp;gt; Find... ==&amp;gt; enter &amp;quot;Model 1&amp;quot; and click Find&lt;br /&gt;
: You are now in the region of the result file where the model likelihoods and parameter estimates are noted. Now locate a line that looks a bit like the one shown below:&lt;br /&gt;
 lnL(ntime: 72  np: 74):  -4242.470345     +0.000000&lt;br /&gt;
: Identify the number of &amp;quot;free parameters&amp;quot;, K, used in model M1: this is indicated by &amp;quot;np&amp;quot;, and is 74 in the example shown above (most of these parameters are branch lengths in the tree; specifically, the number of branch length parameters is indicated by &amp;quot;ntime&amp;quot;, and is 72 in this example). Also note the log-likelihood of the fitted model. This is the number right after the parenthesis, and is -4242.470345 in the example here.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Question: &#039;&#039;&#039;What are the values of K and lnL for model M1?&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Question 8&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Find dN/dS ratios and codon class proportions for model M1&#039;&#039;&#039;&lt;br /&gt;
: Scroll down a few lines until you get to a small table similar to this:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
      dN/dS for site classes (K=2)&lt;br /&gt;
      p:   0.75111  0.24889&lt;br /&gt;
      w:   0.06583  1.00000&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
: This gives a summary of the dN/dS ratios that were found in the data set. The line starting &amp;quot;w:&amp;quot; lists the two dN/dS ratios that were found (in this case 0.06583 and 1.00000 - the last one was pre-specified by us as part of the model and was therefore not a free parameter). In this context &amp;quot;w&amp;quot; means &amp;quot;omega&amp;quot;, which is the symbol typically used to represent the dN/dS rate ratio. The line starting &amp;quot;p:&amp;quot; gives the proportion of codon sites belonging to each of the dN/dS ratio classes (in the example above approximately 75% belong to the first class, while 25% of all sites belong to the class having dN/dS = 1.00000).&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Question: &#039;&#039;&#039;What are the dN/dS value (w) and proportion (p) of sites for both classes? Report the following values: p(class1), w(class1), p(class2), w(class2)&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Question 9&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Find likelihood, and K for model M2&#039;&#039;&#039;&lt;br /&gt;
: Scroll past the M1 output until you get to the results for model M2.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Question: &#039;&#039;&#039;What are the values of K and lnL for model M2?&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Question 10&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Find dN/dS ratios and codon class proportions for model M2&#039;&#039;&#039;&lt;br /&gt;
: Now scroll down a few lines until you get to a small table similar to the one you examined for M1 before. For this model there are 3 separate classes of codons.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Question: &#039;&#039;&#039;What are the dN/dS value (w) and proportion (p) of sites for all three classes? Report these values: p(class1), w(class1), p(class2), w(class2), p(class3), w(class3)&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Question 11&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Assess strength of evidence for models M1 and M2&#039;&#039;&#039;&lt;br /&gt;
: M2 will always have a better (higher) log-likelihood than model M1 because M2 has more free parameters, and M1 is nested within M2. You should now use the recipe given above to compute AIC values and model probabilities for M1 and M2.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Question: &#039;&#039;&#039;Report: AIC, ΔAIC, weight (model probability) for M1 and M2&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Question 12: &#039;&#039;&#039;Is M2 better than M1?&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Question 13&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Examine list of positively selected sites&#039;&#039;&#039;&lt;br /&gt;
: If your M2 is clearly better than M1, then you have evidence for the existence of positively selected sites in the gp120 gene. Now scroll down to the end of the result file and locate a list similar to the one below. Note: this is the &amp;quot;Bayes Empirical Bayes&amp;quot; table, not the &amp;quot;Naive Empirical Bayes&amp;quot; table.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Bayes Empirical Bayes (BEB) analysis&lt;br /&gt;
Positively selected sites&lt;br /&gt;
&lt;br /&gt;
         Prob(w&amp;gt;1)     mean w&lt;br /&gt;
&lt;br /&gt;
    25 A 0.959*        3.133 +- 0.769&lt;br /&gt;
    27 P 0.906         2.990 +- 0.877&lt;br /&gt;
    56 K 0.987*        3.197 +- 0.687&lt;br /&gt;
    59 V 0.915         3.032 +- 0.873&lt;br /&gt;
    78 R 0.637         2.351 +- 1.129&lt;br /&gt;
    88 K 0.573         2.148 +- 1.077&lt;br /&gt;
    95 V 0.925         3.046 +- 0.843&lt;br /&gt;
    ...&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
: It is not important what the distinction is in this context, but very briefly: NEB ignores the fact that there is uncertainty about maximum likelihood estimates, especially for smaller data sets (for instance w for some codon is perhaps not exactly 3.046, but could lie in a region around that value), whereas [https://pubmed.ncbi.nlm.nih.gov/15689528/ BEB accounts for that uncertainty].&lt;br /&gt;
&lt;br /&gt;
: This gives you a list of which residues (if any) were found to belong to the positively selected dN/dS class. Also listed is the probability that the site really is in the codon class where dN/dS &amp;gt; 1, and a weighted average of the w at the site.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Question: &#039;&#039;&#039;List all sites having more than 95% probability of belonging to the positively selected class.&lt;/div&gt;</summary>
		<author><name>Gorm</name></author>
	</entry>
	<entry>
		<id>https://teaching.healthtech.dtu.dk/22115/index.php?title=Model_selection&amp;diff=276</id>
		<title>Model selection</title>
		<link rel="alternate" type="text/html" href="https://teaching.healthtech.dtu.dk/22115/index.php?title=Model_selection&amp;diff=276"/>
		<updated>2026-04-15T09:18:00Z</updated>

		<summary type="html">&lt;p&gt;Gorm: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;This exercise is part of the course  [[22115_-_Computational_Molecular_Evolution|Computational Molecular Evolution (22115)]].&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Overview ==&lt;br /&gt;
&lt;br /&gt;
: In this exercise you are going to investigate features of HIV-1 evolution. You will do this by analyzing a large set of env-genes from HIV-1, subtype B. specifically, the DNA sequences analyzed here correspond to a region surrounding the hypervariable V3 region of the gp120 protein.&lt;br /&gt;
&lt;br /&gt;
: Like other retroviruses, particles of HIV are made up of 2 copies of a single-stranded RNA genome packaged inside a protein core, or capsid. The core particle also contains viral proteins that are essential for the early steps of the virus life cycle, such as reverse transcription and integration. A lipid envelope, derived from the infected cell, surrounds the core particle. Embedded in this envelope are the surface glycoproteins of HIV: gp120 and gp41. The gp120 protein is crucial for binding of the virus particle to target cells, while gp41 is important for the subsequent fusion event. It is the specific affinity of gp120 for the CD4 protein that targets HIV to those cells of the immune system that express CD4 on their surface (e.g., T-helper lymphocytes, monocytes, and macrophages).&lt;br /&gt;
&lt;br /&gt;
: The role gp120 plays in infection and the fact that it is situated on the surface of the HIV particle, means it is an obvious target for the immune response. That means that there may be a considerable selective pressure on gp120 for creating immune-escape mutants, where amino acids in the gp120 epitopes have been substituted. In this exercise you will construct a maximum likelihood tree that we will subsequently use to investigate whether you can detect such a selective pressure on parts of gp120, again using maximum likelihood methods.&lt;br /&gt;
&lt;br /&gt;
: One major goal with the exercise is to introduce you to statistically based methods for assessing the strength of evidence for a set of alternative hypotheses about some biological system of interest. The model selection method we will use is AIC (Akaike Information Criterion), based on which you will compute model probabilities. A second goal is to make you aware that phylogenetic analysis is not only about constructing trees, but that it is also a useful framework for analyzing biological questions more generally.&lt;br /&gt;
&lt;br /&gt;
: Specifically, you will&lt;br /&gt;
&lt;br /&gt;
:# perform a multiple alignment of gp120 DNA sequences taking protein-level information into account (using revtrans).&lt;br /&gt;
:# select a suitable nucleotide substitution model (using jmodeltest2)&lt;br /&gt;
:# construct a phylogenetic tree (using PAUP).&lt;br /&gt;
:# try to detect positively selected sites in gp120 (using PAML).&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
== Recipe for computing AIC values and model probabilities ==&lt;br /&gt;
&lt;br /&gt;
: Later in today&#039;s exercise you will be asked to compute AIC values and model probabilities. Return to this section and follow the instructions when you need to do so.&lt;br /&gt;
&lt;br /&gt;
:# Fit a set of models to your data, note the maximized log likelihoods (lnL) and the number of free parameters (K) for each model in the investigated set. The models you fit should represent a plausible and comprehensive set of hypotheses about your data.&lt;br /&gt;
:# Compute AIC for each of the models: &#039;&#039;&#039;AIC = -2 x lnL + 2K&#039;&#039;&#039;.  &amp;lt;br&amp;gt;For example: a model with lnL = -2010 and K = 5 will have AIC = -2 x -2010 + 2 x 5 = 4030.&lt;br /&gt;
:# Identify the model with the smallest AIC (this is the best model in the set). We will call the AIC for this model &#039;&#039;&#039;&amp;quot;AICmin&amp;quot;&#039;&#039;&#039;.&lt;br /&gt;
:# Compute the &amp;quot;ΔAIC&amp;quot; values for each model: &#039;&#039;&#039;ΔAIC = AIC - AICmin&#039;&#039;&#039; &amp;lt;br&amp;gt;For each model subtract the minimum AIC value. The best model will have a ΔAIC of zero. The rest of the models will have positive ΔAICs.&lt;br /&gt;
:# For each model compute the following quantity: &#039;&#039;&#039;numerator = exp(-0.5 x ΔAIC)&#039;&#039;&#039; &amp;lt;br&amp;gt;For example, a model with ΔAIC=4.2 will have numerator = exp(-0.5 x 4.2) = exp(-2.1) = 0.1225. Also compute the &#039;&#039;&#039;sum&#039;&#039;&#039; of the numerator values for all models.&lt;br /&gt;
:# Finally, the model probabilities (also called model weights) for each model are found as:&amp;lt;br&amp;gt; &#039;&#039;&#039;P(model) = numerator / sum&#039;&#039;&#039; &amp;lt;br&amp;gt;For example, if sum = 3.75 and a model has numerator = 1.3, then it has P(model) = 1.3 / 3.75 = 0.35&lt;br /&gt;
&lt;br /&gt;
: You may want to keep track of the computations by constructing a table along the following lines:&lt;br /&gt;
[[File:Molevol-Downloads-aictable.png|700px]]&lt;br /&gt;
&lt;br /&gt;
: Note that model probabilities can also be computed using Bayesian methods. One advantage of Bayesian methods over AIC is that instead of relying on a point estimate, uncertainty about parameter values is accounted for by integrating over all possible values (typically using MCMC).&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
== Getting started ==&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Create working directory, copy files&#039;&#039;&#039;&lt;br /&gt;
: In the command below: Instead of /path/to/molevol enter the path to the directory where you have placed your course files (for instance cd /Users/bob/Documents/molevol, or cd /home/student/molevol).&lt;br /&gt;
 cd /path/to/molevol&lt;br /&gt;
 mkdir modelselect&lt;br /&gt;
 cd modelselect&lt;br /&gt;
 cp ../data/gp120.fasta ./gp120.fasta&lt;br /&gt;
 cp ../data/codeml.ctl ./codeml.ctl&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Have a look at the DNA data file:&#039;&#039;&#039;&lt;br /&gt;
 nedit gp120.fasta &amp;amp;&lt;br /&gt;
: The file contains several DNA sequences from HIV-1, subtype B. The sequences are approximately 500 bp long, and correspond to a region surrounding the hypervariable V3 region in the gene encoding gp120. Close the nedit window when you&#039;ve had a look.&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
== Protein-guided alignment of coding DNA ==&lt;br /&gt;
&lt;br /&gt;
: There are three reasons why it is preferable to align coding DNA at the amino acid level rather than directly as nucleotides. First, the DNA alphabet has only four letters compared to 20 amino acids, which means that even unrelated DNA sequences will share roughly 25% identity by chance alone, making it much harder to distinguish true homology from noise. Second, protein alignments benefit from empirical substitution matrices such as BLOSUM-62, which capture the fact that amino acid replacements tend to be conservative. Equivalent matrices for DNA are far less informative because nucleotide substitution patterns vary strongly between genes and organisms. Third, synonymous substitutions accumulate faster than non-synonymous ones, so DNA sequences diverge more rapidly than the proteins they encode. Taken together, these factors mean that the phylogenetic signal in a DNA alignment erodes much faster than in the corresponding protein alignment.&lt;br /&gt;
&lt;br /&gt;
: However, while protein-level alignment gives us the most reliable homology signal, it discards exactly the information we need for studying selection: the synonymous substitutions. Estimating the ratio of non-synonymous to synonymous substitution rates (dN/dS) requires a codon-level alignment where analogous codon positions are properly lined up. We would therefore like to construct a multiple alignment at the DNA level, but using information at the protein level, and the RevTrans server does exactly that.&lt;br /&gt;
&lt;br /&gt;
: &#039;&#039;&#039;RevTrans&#039;&#039;&#039; takes as input an unaligned set of DNA sequences, automatically translates them to the equivalent amino acid sequences, constructs a multiple alignment of the protein sequences, and finally uses the protein alignment as a template for constructing a multiple DNA alignment. Because each amino acid maps back to its original codon, gaps are always inserted in groups of three and codon boundaries are preserved throughout the alignment. This is essential for the dN/dS analyses we will carry out in this exercise: if codon positions are misaligned, synonymous and non-synonymous changes cannot be correctly distinguished.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Construct RevTrans alignment&#039;&#039;&#039;&lt;br /&gt;
:* Open RevTrans server page: https://services.healthtech.dtu.dk/services/RevTrans-2.0/&lt;br /&gt;
:* On the RevTrans page: Choose the file gp120.fasta as input (or copy and paste the sequence into the sequence window)&lt;br /&gt;
:* Click the &amp;quot;Submit query&amp;quot; button&lt;br /&gt;
:* When the alignment is done you may have to click link named &amp;quot;here&amp;quot; to go to results page&lt;br /&gt;
:* Download DNA alignment, by right-clicking the link for &amp;quot;Download alignment in FASTA format&amp;quot;, and choosing &amp;quot;Save link as...&amp;quot; (save file under the name gp120align.fasta and make sure to save the file in the directory modelselect).&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Convert alignment to NEXUS format&#039;&#039;&#039;&lt;br /&gt;
: Convert the fasta file to NEXUS format and save file in the modelselect directory under the name gp120.nexus&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
== Model selection using AIC and jModelTest2 ==&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Question 1&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
: As part of the present analysis we are going to build a phylogenetic tree based on the DNA alignment constructed above. We will construct the tree using maximum likelihood, but to do that we first have to decide which substitution model we want to use. Specifically, we are interested in selecting a model that achieves a good balance between fit to the data and number of parameters. We will investigate this issue by fitting a set of 56 different models to our data and then comparing them using AIC.&lt;br /&gt;
&lt;br /&gt;
: In the first part of the exercise, however, you will only compare three closely related models by hand. The purpose of this restricted comparison is not primarily to identify the globally best model, but to understand how AIC, ΔAIC, and Akaike weights are computed and interpreted. Because only three models are included in this manual comparison, the weights you compute will describe relative support &#039;&#039;&#039;within this restricted set only&#039;&#039;&#039;.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Start jmodeltest2&#039;&#039;&#039;&lt;br /&gt;
 jmodeltest&lt;br /&gt;
&#039;&#039;&#039;Load data set&#039;&#039;&#039;&lt;br /&gt;
:* File -&amp;gt; Load DNA alignment -&amp;gt; File Format -&amp;gt; Select &amp;quot;All files&amp;quot;&lt;br /&gt;
:* Navigate to gp120.nexus and load it&lt;br /&gt;
&#039;&#039;&#039;Fit 56 models&#039;&#039;&#039;&lt;br /&gt;
:* Analysis -&amp;gt; Compute likelihood scores&lt;br /&gt;
:* Select &amp;quot;7&amp;quot; under &amp;quot;Number of substitution schemes&amp;quot;&lt;br /&gt;
:* Select &amp;quot;Fixed BIONJ-JC&amp;quot; under &amp;quot;Base tree for likelihood calculations&amp;quot;&lt;br /&gt;
:* Click &amp;quot;Compute likelihoods&amp;quot;&lt;br /&gt;
&lt;br /&gt;
: This causes jmodeltest2 to perform the following actions: first a neighbor joining tree is constructed using the Jukes and Cantor model. Then the tree is fixed and used as the basis for fitting a set of 56 different models to the data. For each model, the estimated model parameters and the negative log-likelihood are recorded. In addition to varying sets of substitution rate parameters (JC, K2P, ...), some of these models also include extra parameters that take into account the presence of different rates between sites. This is done in two ways: (1) by fitting a gamma distribution of rates (&amp;quot;+G&amp;quot;), and (2) by allowing for a proportion of constant (&amp;quot;invariable&amp;quot;) sites (&amp;quot;+I&amp;quot;).&lt;br /&gt;
&lt;br /&gt;
: Wait until jmodeltest2 is done fitting all 56 models (this will take a little while depending on your computer).&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Inspect result&#039;&#039;&#039;&lt;br /&gt;
:* Results -&amp;gt; Show results table&lt;br /&gt;
: For each model this table lists the negative log-likelihood (&amp;quot;-lnL&amp;quot;), the number of parameters (&amp;quot;p&amp;quot; - note this includes branch lengths), and estimates of all model parameters (excluding branch lengths).&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Manually compute Akaike weights for three substitution models&#039;&#039;&#039;&lt;br /&gt;
: Use AIC-based model comparison to investigate which of the following three substitution models are best supported as descriptions of how the sequences have evolved:&lt;br /&gt;
:* Jukes and Cantor with fraction of invariant sites (JC+I)&lt;br /&gt;
:* Jukes and Cantor with gamma-distributed rates over sites (JC+G)&lt;br /&gt;
:* Jukes and Cantor with invariant sites and gamma-distributed rates (JC+I+G)&lt;br /&gt;
: Before you can do the computation you need to know the log likelihood and the number of parameters for each model. Locate these values in the table for the JC+I, JC+G, and JC+I+G models, and write them down. Close the window with the result table when you are done.&lt;br /&gt;
&lt;br /&gt;
: Make sure to get the signs right: the values reported in the table are -lnL values, so you will need to reverse the sign to get the lnL (the lnL values you write down should be negative).&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Question: &#039;&#039;&#039;Use the recipe above to compute AIC, ΔAIC, and Akaike weight for the three models. Report the results in a table similar to the one shown above, and verify that the three weights sum to 1 (up to rounding error).&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Question 2&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
: Based on the Akaike weights, which of the three models has the strongest support? Is the support strongly concentrated on one model, or is there still substantial support for one or both of the others?&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Question 3&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
: What you just did manually for JC+I, JC+G and JC+I+G, jmodeltest2 can do automatically for the full set of 56 fitted models. Specifically, it uses the list of negative log likelihoods and parameter counts in the table to compute AIC values and Akaike weights for the full candidate set:&lt;br /&gt;
:* Analysis -&amp;gt; Do AIC calculations -&amp;gt;&lt;br /&gt;
:* Select &amp;quot;Write PAUP* block&amp;quot;&lt;br /&gt;
:* click &amp;quot;Do AIC calculations&amp;quot;&lt;br /&gt;
:* Results -&amp;gt; Show results table&lt;br /&gt;
:* Select &amp;quot;AIC&amp;quot; tab&lt;br /&gt;
:* &#039;&#039;&#039;IMPORTANT:&#039;&#039;&#039; SHIFT+click on the header of the &amp;quot;weight&amp;quot; column. This sorts the rows according to model weight, in descending order.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Question: &#039;&#039;&#039;What model was selected by modeltest based on the AIC values? (This will be the model with the highest weight - and lowest AIC - and will be in the first row of the results table after SHIFT-clicking the &amp;quot;weight&amp;quot; header).&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Question 4&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
: In the same AIC results table, now click (not SHIFT+click) the header of the &amp;quot;-lnL&amp;quot; column. In this table, a normal click sorts in ascending order, whereas SHIFT+click sorts in descending order. The model at the top will therefore be the one with the smallest value of &amp;quot;-lnL&amp;quot; (equivalently: the highest likelihood). Compare this model to the one with the highest Akaike weight.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Question: &#039;&#039;&#039;Is the model with the highest likelihood also the model with the highest Akaike weight? If not, explain briefly why these two rankings can differ.&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
== Phylogenetic tree inference ==&lt;br /&gt;
&lt;br /&gt;
=== Tree inference using the model selected by jModelTest2 (PAUP) ===&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Question 5&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
: Above, you used jModelTest2 to select a substitution model based on AIC. You will now use that model to construct a maximum likelihood tree in &#039;&#039;&#039;PAUP&#039;&#039;&#039;. The purpose of doing this in PAUP is mainly pedagogical: it makes the individual steps of model-based phylogenetic inference more explicit. In practice, one would often use a more integrated program such as IQ-TREE 3 (see next section).&lt;br /&gt;
&lt;br /&gt;
: Close the results table. In the main window you should now scroll up to the lines giving PAUP commands that implement the selected model. The command is enclosed between &amp;quot;BEGIN PAUP&amp;quot; and &amp;quot;END;&amp;quot; and should look something like this:&lt;br /&gt;
 Lset Base=(0.4064, [...]&lt;br /&gt;
: You will need to copy this command to a PAUP session in the next step.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Start PAUP&#039;&#039;&#039;&lt;br /&gt;
 paup&lt;br /&gt;
: Above, you used jModelTest2 to select the most suitable substitution model for the present data set. You will now use this model to construct a maximum likelihood tree. You will use PAUP for this purpose. (Note: it is possible to create a maximum likelihood tree, or even a model-averaged tree, directly from jModelTest2, but we will instead do it in PAUP in order to more clearly see each step that is taken.)&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Load alignment:&#039;&#039;&#039;&lt;br /&gt;
 execute gp120.nexus&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Set tree-building criterion to maximum likelihood&#039;&#039;&#039;&lt;br /&gt;
 set criterion=likelihood&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Set model parameters to winning estimates&#039;&#039;&#039;&lt;br /&gt;
: Above, you located a set of lines in the jModelTest2 output giving a PAUP command that sets the model parameters to the estimates found for the winning model. Copy and paste this lset command (without the BEGIN and END parts) into the window where PAUP is running.&lt;br /&gt;
&lt;br /&gt;
 PASTE LSET COMMAND FROM MODELTEST RUN HERE&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Find best tree using selected model&#039;&#039;&#039;&lt;br /&gt;
: Still in the PAUP window, enter the following command:&lt;br /&gt;
 hsearch swap=tbr start=nj&lt;br /&gt;
: This command causes PAUP to perform a heuristic search for the best maximum likelihood tree. Once an initial tree has been constructed, the heuristic search proceeds by rearrangements of the &amp;quot;tree bisection and reconnection&amp;quot; type (TBR). We are using the model selected by modeltest, and the parameter estimates found by modeltest for that model. You could also choose to estimate all the model parameters as part of this step (i.e., at the same time as finding the best tree), but fixing them improves speed tremendously. Finding the best tree should take at most a few minutes.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Save best tree to file&#039;&#039;&#039;&lt;br /&gt;
 savetrees format=newick brlens=yes file=gp120tree_paup.phy from=1 to=1&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Quit program&#039;&#039;&#039;&lt;br /&gt;
 quit&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Have a look at the tree:&#039;&#039;&#039;&lt;br /&gt;
: You have now produced an unrooted tree of the HIV sequences and saved it in the file gp120tree_paup.phy. Note that in this exercise we will not be interested in the tree as such - our focus is instead on finding positive selection at a subset of codon positions, and the tree is just something we need in order to fit the different codon models to the data. If you want to see the tree, you can do so with the following command:&lt;br /&gt;
 figtree gp120tree_paup.phy &amp;amp;&lt;br /&gt;
: There is no meaningful root placed on this tree, so you may want to choose the unrooted view (the third icon in the Layout section of the FigTree window). Close the FigTree window when you have had a look.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Question: &#039;&#039;&#039;Which substitution model did you use for the PAUP tree search, and what was the negative log likelihood of the best tree found under this model?&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
=== One-step model selection and tree inference using IQ-TREE 3 ===&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Question 6&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
: In real-life phylogenetic analysis one would often use a modern program such as &#039;&#039;&#039;IQ-TREE 3&#039;&#039;&#039;, which can carry out model selection and maximum likelihood tree reconstruction in a single analysis. You will now repeat the analysis using this more integrated approach.&lt;br /&gt;
&lt;br /&gt;
: Run IQ-TREE 3 on the alignment using the following command:&lt;br /&gt;
 iqtree3 -s gp120.nexus -m MFP&lt;br /&gt;
&lt;br /&gt;
: Here, &amp;quot;-m MFP&amp;quot; tells IQ-TREE 3 to perform automatic model selection using &amp;quot;ModelFinder Plus&amp;quot; before reconstructing the tree.&lt;br /&gt;
&lt;br /&gt;
: When the run has finished, inspect the output written to the screen and to the file ending in &amp;quot;.iqtree&amp;quot;. Note that IQ-TREE 3 reports several model-selection criteria in the output. The line near the top of the ModelFinder section states which criterion was used to choose the final model. Locate the substitution model selected by IQ-TREE 3 and the log likelihood of the best tree.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Question: &#039;&#039;&#039;Which substitution model was selected by IQ-TREE 3, and according to which information criterion was it selected? What was the log likelihood of the best tree? Compare this to the earlier jModelTest2 + PAUP analysis. Is the selected model exactly the same? Is the likelihood exactly the same? Briefly suggest one reason why the two analyses might not give identical results.&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
== Detection of positively selected sites in gp120 ==&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Question 7&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
: There is much more to phylogenetic analyses than merely reconstructing trees. One interesting result of probabilistic methods is that the parameters of a model are estimated as part of the optimization procedure. This means that once such a model has been fitted to the data, it is possible to investigate these estimated parameter values to learn features about the evolutionary history of the sequences under investigation. In the present example we will focus on whether we can detect positively selected sites in our data set, defined as sites where the dN/dS ratio is larger than 1. We do that by using a codon substitution model where the dN/dS ratio is one of its parameters.&lt;br /&gt;
&lt;br /&gt;
: A further strength of the probabilistic approach is that you get a probabilistic measure of how well any model fits the data. This means you can use a stringent approach to determine which model fits the data best. In this framework one uses likelihoods (probabilities of data given model) to determine which model fits the data best. As you saw above, it is for instance possible to compute AIC values and weights (model probabilities) from the likelihood values of fitted models. Since each model essentially corresponds to a hypothesis about the evolutionary history of the data, we can thus use a stringent statistical approach to decide which hypothesis best describes our data.&lt;br /&gt;
&lt;br /&gt;
: In outline, you will now use the following steps to investigate whether there is any evidence for positively selected codons in your data set:&lt;br /&gt;
&lt;br /&gt;
:* Fit model M1, which assumes there are two classes of codons in the sequence: some with dN/dS &amp;lt; 1, some with dN/dS = 1.&lt;br /&gt;
:* Fit model M2, which assumes 3 distinct classes of codons: two with dN/dS ratios as for M1, and one extra class with dN/dS &amp;gt; 1.&lt;br /&gt;
:* Assess the strength of evidence for the two models using AIC-based model probabilities&lt;br /&gt;
:* If M2 is better: identify the positively selected codons&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Inspect the parameter file&#039;&#039;&#039;&lt;br /&gt;
 nedit codeml.ctl &amp;amp;&lt;br /&gt;
: The file &amp;quot;codeml.ctl&amp;quot; contains several settings that are relevant for running the program &#039;&#039;&#039;codeml&#039;&#039;&#039;. Find the following lines and ensure that the file contains these values:&lt;br /&gt;
 &#039;&#039;&#039;seqfile =  gp120align.fasta&#039;&#039;&#039;: name of alignment file&lt;br /&gt;
 &#039;&#039;&#039;treefile =  gp120.nexus.treefile&#039;&#039;&#039;: name of tree file produced by IQ-TREE 3&lt;br /&gt;
 &#039;&#039;&#039;seqtype = 1&#039;&#039;&#039;: tells the program that our data consists of coding DNA&lt;br /&gt;
 &#039;&#039;&#039;NSsites = 1 2&#039;&#039;&#039;: tells the program to analyze models M1 and M2&lt;br /&gt;
 &#039;&#039;&#039;cleandata = 0&#039;&#039;&#039;: tells the program to keep positions with gaps&lt;br /&gt;
&lt;br /&gt;
: &#039;&#039;&#039;Important note:&#039;&#039;&#039; in this exercise, use the tree from &#039;&#039;&#039;IQ-TREE 3&#039;&#039;&#039; for the codeml analysis. On some systems, codeml may have problems with the PAUP tree file or may stop with a confusing error message even though the downstream results are still written to the output file. Using the IQ-TREE tree avoids this issue.&lt;br /&gt;
&lt;br /&gt;
: The settings entered by us will cause codeml to analyze two hypotheses about dN/dS ratios. M1 says there are two classes of codons with different dN/dS ratios in the sequence: one class with dN/dS &amp;lt; 1 (codons under purifying or negative selection), and one class with dN/dS = 1 (no selection - neutrally evolving sites). M2 says there are 3 distinct dN/dS ratios for different sites in the sequence: one class with dN/dS &amp;lt; 1, one class with dN/dS = 1 (these are the same type of classes as for M1), and one class with dN/dS &amp;gt; 1 (corresponding to sites under positive selection). The value of the dN/dS ratios (for those classes that have dN/dS &amp;lt; 1 or dN/dS &amp;gt; 1), the fraction of sites belonging to each class, and the position of sites belonging to each class, are unknown at first and will be determined during the analysis.&lt;br /&gt;
&lt;br /&gt;
: (Regarding &amp;quot;cleandata&amp;quot; setting: &amp;quot;cleandata = 1&amp;quot; would cause codeml to discard any alignment columns with gaps or ambiguity symbols. For alignments with many sequences this can lead to discarding the majority of sites and I would therefore recommend not using that setting. At the same time you do want to be wary of columns where most sequences have gaps, since inference of selection for these will be very uncertain. It might be better to discard such columns from an alignment before doing the codeml analysis.)&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Start the analysis&#039;&#039;&#039;&lt;br /&gt;
 codeml&lt;br /&gt;
: This will start the codeml program using the settings in the file codeml.ctl. Depending on your computer, this will take some minutes to finish. (You may be able to see how the optimization procedure results in progressively better fits: the likelihood increases, meaning that negative log-likelihood decreases, as the fit improves.)&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Inspect result file:&#039;&#039;&#039;&lt;br /&gt;
: Wait for the run to finish, and then look at the result file:&lt;br /&gt;
 nedit selection.results &amp;amp;&lt;br /&gt;
: This file contains a wealth of information concerning your analysis. You may want to turn off line-wrapping to more clearly see the structure of the file (there will be some long lines that will otherwise wrap around). The top part of the file gives an overview of your sequences, codon usage and nucleotide frequencies. You can ignore this information for now, and move on to the interesting part, namely the model likelihoods and parameter values.&lt;br /&gt;
&lt;br /&gt;
: &#039;&#039;&#039;Note:&#039;&#039;&#039; On some systems codeml may print an error message near the end of the run, for instance during the BEB step, even though the relevant results have already been written to &#039;&#039;&#039;selection.results&#039;&#039;&#039;. If this happens, inspect &#039;&#039;&#039;selection.results&#039;&#039;&#039; before assuming that the run failed completely.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Find likelihood, and number of free parameters for model M1&#039;&#039;&#039;&lt;br /&gt;
 Search ==&amp;gt; Find... ==&amp;gt; enter &amp;quot;Model 1&amp;quot; and click Find&lt;br /&gt;
: You are now in the region of the result file where the model likelihoods and parameter estimates are noted. Now locate a line that looks a bit like the one shown below:&lt;br /&gt;
 lnL(ntime: 72  np: 74):  -4242.470345     +0.000000&lt;br /&gt;
: Identify the number of &amp;quot;free parameters&amp;quot;, K, used in model M1: this is indicated by &amp;quot;np&amp;quot;, and is 74 in the example shown above (most of these parameters are branch lengths in the tree; specifically, the number of branch length parameters is indicated by &amp;quot;ntime&amp;quot;, and is 72 in this example). Also note the log-likelihood of the fitted model. This is the number right after the parenthesis, and is -4242.470345 in the example here.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Question: &#039;&#039;&#039;What are the values of K and lnL for model M1?&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Question 8&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Find dN/dS ratios and codon class proportions for model M1&#039;&#039;&#039;&lt;br /&gt;
: Scroll down a few lines until you get to a small table similar to this:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
      dN/dS for site classes (K=2)&lt;br /&gt;
      p:   0.75111  0.24889&lt;br /&gt;
      w:   0.06583  1.00000&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
: This gives a summary of the dN/dS ratios that were found in the data set. The line starting &amp;quot;w:&amp;quot; lists the two dN/dS ratios that were found (in this case 0.06583 and 1.00000 - the last one was pre-specified by us as part of the model and was therefore not a free parameter). In this context &amp;quot;w&amp;quot; means &amp;quot;omega&amp;quot;, which is the symbol typically used to represent the dN/dS rate ratio. The line starting &amp;quot;p:&amp;quot; gives the proportion of codon sites belonging to each of the dN/dS ratio classes (in the example above approximately 75% belong to the first class, while 25% of all sites belong to the class having dN/dS = 1.00000).&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Question: &#039;&#039;&#039;What are the dN/dS value (w) and proportion (p) of sites for both classes? Report the following values: p(class1), w(class1), p(class2), w(class2)&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Question 9&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Find likelihood, and K for model M2&#039;&#039;&#039;&lt;br /&gt;
: Scroll past the M1 output until you get to the results for model M2.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Question: &#039;&#039;&#039;What are the values of K and lnL for model M2?&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Question 10&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Find dN/dS ratios and codon class proportions for model M2&#039;&#039;&#039;&lt;br /&gt;
: Now scroll down a few lines until you get to a small table similar to the one you examined for M1 before. For this model there are 3 separate classes of codons.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Question: &#039;&#039;&#039;What are the dN/dS value (w) and proportion (p) of sites for all three classes? Report these values: p(class1), w(class1), p(class2), w(class2), p(class3), w(class3)&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Question 11&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Assess strength of evidence for models M1 and M2&#039;&#039;&#039;&lt;br /&gt;
: M2 will always have a better (higher) log-likelihood than model M1 because M2 has more free parameters, and M1 is nested within M2. You should now use the recipe given above to compute AIC values and model probabilities for M1 and M2.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Question: &#039;&#039;&#039;Report: AIC, ΔAIC, weight (model probability) for M1 and M2&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Question 12: &#039;&#039;&#039;Is M2 better than M1?&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Question 13&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Examine list of positively selected sites&#039;&#039;&#039;&lt;br /&gt;
: If your M2 is clearly better than M1, then you have evidence for the existence of positively selected sites in the gp120 gene. Now scroll down to the end of the result file and locate a list similar to the one below. Note: this is the &amp;quot;Bayes Empirical Bayes&amp;quot; table, not the &amp;quot;Naive Empirical Bayes&amp;quot; table.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Bayes Empirical Bayes (BEB) analysis&lt;br /&gt;
Positively selected sites&lt;br /&gt;
&lt;br /&gt;
         Prob(w&amp;gt;1)     mean w&lt;br /&gt;
&lt;br /&gt;
    25 A 0.959*        3.133 +- 0.769&lt;br /&gt;
    27 P 0.906         2.990 +- 0.877&lt;br /&gt;
    56 K 0.987*        3.197 +- 0.687&lt;br /&gt;
    59 V 0.915         3.032 +- 0.873&lt;br /&gt;
    78 R 0.637         2.351 +- 1.129&lt;br /&gt;
    88 K 0.573         2.148 +- 1.077&lt;br /&gt;
    95 V 0.925         3.046 +- 0.843&lt;br /&gt;
    ...&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
: It is not important what the distinction is in this context, but very briefly: NEB ignores the fact that there is uncertainty about maximum likelihood estimates, especially for smaller data sets (for instance w for some codon is perhaps not exactly 3.046, but could lie in a region around that value), whereas [https://pubmed.ncbi.nlm.nih.gov/15689528/ BEB accounts for that uncertainty].&lt;br /&gt;
&lt;br /&gt;
: This gives you a list of which residues (if any) were found to belong to the positively selected dN/dS class. Also listed is the probability that the site really is in the codon class where dN/dS &amp;gt; 1, and a weighted average of the w at the site.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Question: &#039;&#039;&#039;List all sites having more than 95% probability of belonging to the positively selected class.&lt;/div&gt;</summary>
		<author><name>Gorm</name></author>
	</entry>
	<entry>
		<id>https://teaching.healthtech.dtu.dk/22115/index.php?title=Model_selection&amp;diff=275</id>
		<title>Model selection</title>
		<link rel="alternate" type="text/html" href="https://teaching.healthtech.dtu.dk/22115/index.php?title=Model_selection&amp;diff=275"/>
		<updated>2026-04-15T08:29:34Z</updated>

		<summary type="html">&lt;p&gt;Gorm: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;This exercise is part of the course  [[22115_-_Computational_Molecular_Evolution|Computational Molecular Evolution (22115)]].&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Overview ==&lt;br /&gt;
&lt;br /&gt;
: In this exercise you are going to investigate features of HIV-1 evolution. You will do this by analyzing a large set of env-genes from HIV-1, subtype B. specifically, the DNA sequences analyzed here correspond to a region surrounding the hypervariable V3 region of the gp120 protein.&lt;br /&gt;
&lt;br /&gt;
: Like other retroviruses, particles of HIV are made up of 2 copies of a single-stranded RNA genome packaged inside a protein core, or capsid. The core particle also contains viral proteins that are essential for the early steps of the virus life cycle, such as reverse transcription and integration. A lipid envelope, derived from the infected cell, surrounds the core particle. Embedded in this envelope are the surface glycoproteins of HIV: gp120 and gp41. The gp120 protein is crucial for binding of the virus particle to target cells, while gp41 is important for the subsequent fusion event. It is the specific affinity of gp120 for the CD4 protein that targets HIV to those cells of the immune system that express CD4 on their surface (e.g., T-helper lymphocytes, monocytes, and macrophages).&lt;br /&gt;
&lt;br /&gt;
: The role gp120 plays in infection and the fact that it is situated on the surface of the HIV particle, means it is an obvious target for the immune response. That means that there may be a considerable selective pressure on gp120 for creating immune-escape mutants, where amino acids in the gp120 epitopes have been substituted. In this exercise you will construct a maximum likelihood tree that we will subsequently use to investigate whether you can detect such a selective pressure on parts of gp120, again using maximum likelihood methods.&lt;br /&gt;
&lt;br /&gt;
: One major goal with the exercise is to introduce you to statistically based methods for assessing the strength of evidence for a set of alternative hypotheses about some biological system of interest. The model selection method we will use is AIC (Akaike Information Criterion), based on which you will compute model probabilities. A second goal is to make you aware that phylogenetic analysis is not only about constructing trees, but that it is also a useful framework for analyzing biological questions more generally.&lt;br /&gt;
&lt;br /&gt;
: Specifically, you will&lt;br /&gt;
&lt;br /&gt;
:# perform a multiple alignment of gp120 DNA sequences taking protein-level information into account (using revtrans).&lt;br /&gt;
:# select a suitable nucleotide substitution model (using jmodeltest2)&lt;br /&gt;
:# construct a phylogenetic tree (using PAUP).&lt;br /&gt;
:# try to detect positively selected sites in gp120 (using PAML).&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
== Recipe for computing AIC values and model probabilities ==&lt;br /&gt;
&lt;br /&gt;
: Later in today&#039;s exercise you will be asked to compute AIC values and model probabilities. Return to this section and follow the instructions when you need to do so.&lt;br /&gt;
&lt;br /&gt;
:# Fit a set of models to your data, note the maximized log likelihoods (lnL) and the number of free parameters (K) for each model in the investigated set. The models you fit should represent a plausible and comprehensive set of hypotheses about your data.&lt;br /&gt;
:# Compute AIC for each of the models: &#039;&#039;&#039;AIC = -2 x lnL + 2K&#039;&#039;&#039;.  &amp;lt;br&amp;gt;For example: a model with lnL = -2010 and K = 5 will have AIC = -2 x -2010 + 2 x 5 = 4030.&lt;br /&gt;
:# Identify the model with the smallest AIC (this is the best model in the set). We will call the AIC for this model &#039;&#039;&#039;&amp;quot;AICmin&amp;quot;&#039;&#039;&#039;.&lt;br /&gt;
:# Compute the &amp;quot;ΔAIC&amp;quot; values for each model: &#039;&#039;&#039;ΔAIC = AIC - AICmin&#039;&#039;&#039; &amp;lt;br&amp;gt;For each model subtract the minimum AIC value. The best model will have a ΔAIC of zero. The rest of the models will have positive ΔAICs.&lt;br /&gt;
:# For each model compute the following quantity: &#039;&#039;&#039;numerator = exp(-0.5 x ΔAIC)&#039;&#039;&#039; &amp;lt;br&amp;gt;For example, a model with ΔAIC=4.2 will have numerator = exp(-0.5 x 4.2) = exp(-2.1) = 0.1225. Also compute the &#039;&#039;&#039;sum&#039;&#039;&#039; of the numerator values for all models.&lt;br /&gt;
:# Finally, the model probabilities (also called model weights) for each model are found as:&amp;lt;br&amp;gt; &#039;&#039;&#039;P(model) = numerator / sum&#039;&#039;&#039; &amp;lt;br&amp;gt;For example, if sum = 3.75 and a model has numerator = 1.3, then it has P(model) = 1.3 / 3.75 = 0.35&lt;br /&gt;
&lt;br /&gt;
: You may want to keep track of the computations by constructing a table along the following lines:&lt;br /&gt;
[[File:Molevol-Downloads-aictable.png|700px]]&lt;br /&gt;
&lt;br /&gt;
: Note that model probabilities can also be computed using Bayesian methods. One advantage of Bayesian methods over AIC is that instead of relying on a point estimate, uncertainty about parameter values is accounted for by integrating over all possible values (typically using MCMC).&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
== Getting started ==&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Create working directory, copy files&#039;&#039;&#039;&lt;br /&gt;
: In the command below: Instead of /path/to/molevol enter the path to the directory where you have placed your course files (for instance cd /Users/bob/Documents/molevol, or cd /home/student/molevol).&lt;br /&gt;
 cd /path/to/molevol&lt;br /&gt;
 mkdir modelselect&lt;br /&gt;
 cd modelselect&lt;br /&gt;
 cp ../data/gp120.fasta ./gp120.fasta&lt;br /&gt;
 cp ../data/codeml.ctl ./codeml.ctl&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Have a look at the DNA data file:&#039;&#039;&#039;&lt;br /&gt;
 nedit gp120.fasta &amp;amp;&lt;br /&gt;
: The file contains several DNA sequences from HIV-1, subtype B. The sequences are approximately 500 bp long, and correspond to a region surrounding the hypervariable V3 region in the gene encoding gp120. Close the nedit window when you&#039;ve had a look.&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
== Protein-guided alignment of coding DNA ==&lt;br /&gt;
&lt;br /&gt;
: There are three reasons why it is preferable to align coding DNA at the amino acid level rather than directly as nucleotides. First, the DNA alphabet has only four letters compared to 20 amino acids, which means that even unrelated DNA sequences will share roughly 25% identity by chance alone, making it much harder to distinguish true homology from noise. Second, protein alignments benefit from empirical substitution matrices such as BLOSUM-62, which capture the fact that amino acid replacements tend to be conservative. Equivalent matrices for DNA are far less informative because nucleotide substitution patterns vary strongly between genes and organisms. Third, synonymous substitutions accumulate faster than non-synonymous ones, so DNA sequences diverge more rapidly than the proteins they encode. Taken together, these factors mean that the phylogenetic signal in a DNA alignment erodes much faster than in the corresponding protein alignment.&lt;br /&gt;
&lt;br /&gt;
: However, while protein-level alignment gives us the most reliable homology signal, it discards exactly the information we need for studying selection: the synonymous substitutions. Estimating the ratio of non-synonymous to synonymous substitution rates (dN/dS) requires a codon-level alignment where analogous codon positions are properly lined up. We would therefore like to construct a multiple alignment at the DNA level, but using information at the protein level, and the RevTrans server does exactly that.&lt;br /&gt;
&lt;br /&gt;
: &#039;&#039;&#039;RevTrans&#039;&#039;&#039; takes as input an unaligned set of DNA sequences, automatically translates them to the equivalent amino acid sequences, constructs a multiple alignment of the protein sequences, and finally uses the protein alignment as a template for constructing a multiple DNA alignment. Because each amino acid maps back to its original codon, gaps are always inserted in groups of three and codon boundaries are preserved throughout the alignment. This is essential for the dN/dS analyses we will carry out in this exercise: if codon positions are misaligned, synonymous and non-synonymous changes cannot be correctly distinguished.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Construct RevTrans alignment&#039;&#039;&#039;&lt;br /&gt;
:* Open RevTrans server page: https://services.healthtech.dtu.dk/services/RevTrans-2.0/&lt;br /&gt;
:* On the RevTrans page: Choose the file gp120.fasta as input (or copy and paste the sequence into the sequence window)&lt;br /&gt;
:* Click the &amp;quot;Submit query&amp;quot; button&lt;br /&gt;
:* When the alignment is done you may have to click link named &amp;quot;here&amp;quot; to go to results page&lt;br /&gt;
:* Download DNA alignment, by right-clicking the link for &amp;quot;Download alignment in FASTA format&amp;quot;, and choosing &amp;quot;Save link as...&amp;quot; (save file under the name gp120align.fasta and make sure to save the file in the directory modelselect).&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Convert alignment to NEXUS format&#039;&#039;&#039;&lt;br /&gt;
: Convert the fasta file to NEXUS format and save file in the modelselect directory under the name gp120.nexus&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
== Model selection using AIC and jModelTest2 ==&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Question 1&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
: As part of the present analysis we are going to build a phylogenetic tree based on the DNA alignment constructed above. We will construct the tree using maximum likelihood, but to do that we first have to decide which substitution model we want to use. Specifically, we are interested in selecting a model that achieves a good balance between fit to the data and number of parameters. We will investigate this issue by fitting a set of 56 different models to our data and then comparing them using AIC.&lt;br /&gt;
&lt;br /&gt;
: In the first part of the exercise, however, you will only compare three closely related models by hand. The purpose of this restricted comparison is not primarily to identify the globally best model, but to understand how AIC, ΔAIC, and Akaike weights are computed and interpreted. Because only three models are included in this manual comparison, the weights you compute will describe relative support &#039;&#039;&#039;within this restricted set only&#039;&#039;&#039;.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Start jmodeltest2&#039;&#039;&#039;&lt;br /&gt;
 jmodeltest&lt;br /&gt;
&#039;&#039;&#039;Load data set&#039;&#039;&#039;&lt;br /&gt;
:* File -&amp;gt; Load DNA alignment -&amp;gt; File Format -&amp;gt; Select &amp;quot;All files&amp;quot;&lt;br /&gt;
:* Navigate to gp120.nexus and load it&lt;br /&gt;
&#039;&#039;&#039;Fit 56 models&#039;&#039;&#039;&lt;br /&gt;
:* Analysis -&amp;gt; Compute likelihood scores&lt;br /&gt;
:* Select &amp;quot;7&amp;quot; under &amp;quot;Number of substitution schemes&amp;quot;&lt;br /&gt;
:* Select &amp;quot;Fixed BIONJ-JC&amp;quot; under &amp;quot;Base tree for likelihood calculations&amp;quot;&lt;br /&gt;
:* Click &amp;quot;Compute likelihoods&amp;quot;&lt;br /&gt;
&lt;br /&gt;
: This causes jmodeltest2 to perform the following actions: first a neighbor joining tree is constructed using the Jukes and Cantor model. Then the tree is fixed and used as the basis for fitting a set of 56 different models to the data. For each model, the estimated model parameters and the negative log-likelihood are recorded. In addition to varying sets of substitution rate parameters (JC, K2P, ...), some of these models also include extra parameters that take into account the presence of different rates between sites. This is done in two ways: (1) by fitting a gamma distribution of rates (&amp;quot;+G&amp;quot;), and (2) by allowing for a proportion of constant (&amp;quot;invariable&amp;quot;) sites (&amp;quot;+I&amp;quot;).&lt;br /&gt;
&lt;br /&gt;
: Wait until jmodeltest2 is done fitting all 56 models (this will take a little while depending on your computer).&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Inspect result&#039;&#039;&#039;&lt;br /&gt;
:* Results -&amp;gt; Show results table&lt;br /&gt;
: For each model this table lists the negative log-likelihood (&amp;quot;-lnL&amp;quot;), the number of parameters (&amp;quot;p&amp;quot; - note this includes branch lengths), and estimates of all model parameters (excluding branch lengths).&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Manually compute Akaike weights for three substitution models&#039;&#039;&#039;&lt;br /&gt;
: Use AIC-based model comparison to investigate which of the following three substitution models are best supported as descriptions of how the sequences have evolved:&lt;br /&gt;
:* Jukes and Cantor with fraction of invariant sites (JC+I)&lt;br /&gt;
:* Jukes and Cantor with gamma-distributed rates over sites (JC+G)&lt;br /&gt;
:* Jukes and Cantor with invariant sites and gamma-distributed rates (JC+I+G)&lt;br /&gt;
: Before you can do the computation you need to know the log likelihood and the number of parameters for each model. Locate these values in the table for the JC+I, JC+G, and JC+I+G models, and write them down. Close the window with the result table when you are done.&lt;br /&gt;
&lt;br /&gt;
: Make sure to get the signs right: the values reported in the table are -lnL values, so you will need to reverse the sign to get the lnL (the lnL values you write down should be negative).&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Question: &#039;&#039;&#039;Use the recipe above to compute AIC, ΔAIC, and Akaike weight for the three models. Report the results in a table similar to the one shown above, and verify that the three weights sum to 1 (up to rounding error).&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Question 2&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
: Based on the Akaike weights, which of the three models has the strongest support? Is the support strongly concentrated on one model, or is there still substantial support for one or both of the others?&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Question 3&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
: What you just did manually for JC+I, JC+G and JC+I+G, jmodeltest2 can do automatically for the full set of 56 fitted models. Specifically, it uses the list of negative log likelihoods and parameter counts in the table to compute AIC values and Akaike weights for the full candidate set:&lt;br /&gt;
:* Analysis -&amp;gt; Do AIC calculations -&amp;gt;&lt;br /&gt;
:* Select &amp;quot;Write PAUP* block&amp;quot;&lt;br /&gt;
:* click &amp;quot;Do AIC calculations&amp;quot;&lt;br /&gt;
:* Results -&amp;gt; Show results table&lt;br /&gt;
:* Select &amp;quot;AIC&amp;quot; tab&lt;br /&gt;
:* &#039;&#039;&#039;IMPORTANT:&#039;&#039;&#039; SHIFT+click on the header of the &amp;quot;weight&amp;quot; column. This sorts the rows according to model weight, in descending order.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Question: &#039;&#039;&#039;What model was selected by modeltest based on the AIC values? (This will be the model with the highest weight - and lowest AIC - and will be in the first row of the results table after SHIFT-clicking the &amp;quot;weight&amp;quot; header).&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Question 4&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
: In the same AIC results table, now click (not SHIFT+click) the header of the &amp;quot;-lnL&amp;quot; column. In this table, a normal click sorts in ascending order, whereas SHIFT+click sorts in descending order. The model at the top will therefore be the one with the smallest value of &amp;quot;-lnL&amp;quot; (equivalently: the highest likelihood). Compare this model to the one with the highest Akaike weight.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Question: &#039;&#039;&#039;Is the model with the highest likelihood also the model with the highest Akaike weight? If not, explain briefly why these two rankings can differ.&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
== Construction of phylogenetic tree using PAUP ==&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Question 5&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
: Close the results table. In the main window you should now scroll up to the lines giving PAUP commands that implement the selected model. The command is enclosed between &amp;quot;BEGIN PAUP&amp;quot; and &amp;quot;END;&amp;quot; and should look something like this:&lt;br /&gt;
 Lset Base=(0.4064, [...]&lt;br /&gt;
: You will need to copy this command to a PAUP session in the next step.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Start PAUP&#039;&#039;&#039;&lt;br /&gt;
 paup&lt;br /&gt;
: Above, you used jmodeltest2 to select the most suitable substitution model for the present data set. You will now use this model to construct a maximum likelihood tree. You will use PAUP for this purpose. (Note: it is possible to create a maximum likelihood tree, or even a model-averaged tree, directly from jmodeltest2, but we will instead do it in PAUP in order to more clearly see each step that is taken.)&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Load alignment:&#039;&#039;&#039;&lt;br /&gt;
 execute gp120.nexus&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Set tree-building criterion to maximum likelihood&#039;&#039;&#039;&lt;br /&gt;
 set criterion=likelihood&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Set model parameters to winning estimates&#039;&#039;&#039;&lt;br /&gt;
: Above, you located a set of lines in the jmodeltest2 output giving a PAUP command that sets the model parameters to the estimates found for the winning model. Copy and paste this lset command (without the BEGIN and END parts) into the window where PAUP is running.&lt;br /&gt;
&lt;br /&gt;
 PASTE LSET COMMAND FROM MODELTEST RUN HERE&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Find best tree using selected model&#039;&#039;&#039;&lt;br /&gt;
: Still in the PAUP window, enter the following command:&lt;br /&gt;
 hsearch swap=tbr start=nj&lt;br /&gt;
: This command causes PAUP to perform a heuristic search for the best maximum likelihood tree. Once an initial tree has been constructed, the heuristic search proceeds by rearrangements of the &amp;quot;tree bisection and reconnection&amp;quot; type (TBR). We are using the model selected by modeltest, and the parameter estimates found by modeltest for that model. You could also choose to estimate all the model parameters as part of this step (i.e., at the same time as finding the best tree), but fixing them improves speed tremendously. Finding the best tree should take at most a few minutes.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Save best tree to file&#039;&#039;&#039;&lt;br /&gt;
 savetrees format=newick brlens=yes file=gp120tree.phy from=1 to=1&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Quit program&#039;&#039;&#039;&lt;br /&gt;
 quit&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Have a look at the tree:&#039;&#039;&#039;&lt;br /&gt;
: You have now produced an unrooted tree of the HIV sequences and saved it in the file gp120tree.phy. Note that in this exercise we will not be interested in the tree as such - our focus is instead on finding positive selection at a subset of codon positions, and the tree is just something we need in order to fit the different codon models to the data. If you want to see the tree, you can do so with the following command:&lt;br /&gt;
 figtree gp120tree.phy &amp;amp;&lt;br /&gt;
: There is no meaningful root placed on this tree, so you may want to choose the unrooted view (the third icon in the Layout section of the FigTree window). Close the FigTree window when you have had a look.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Question: &#039;&#039;&#039;Which substitution model did you use for the PAUP tree search, and what was the negative log likelihood of the best tree found under this model?&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
== Model selection and tree reconstruction in one step using IQ-TREE 3 ==&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Question 6&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
: In real-life phylogenetic analysis one would often use a modern program such as &#039;&#039;&#039;IQ-TREE 3&#039;&#039;&#039;, which can carry out model selection and maximum likelihood tree reconstruction in a single analysis.&lt;br /&gt;
&lt;br /&gt;
: Run IQ-TREE 3 on the alignment using the following command:&lt;br /&gt;
 iqtree3 -s gp120.nexus -m MFP&lt;br /&gt;
&lt;br /&gt;
: Here, &amp;quot;-m MFP&amp;quot; tells IQ-TREE 3 to perform automatic model selection using &amp;quot;ModelFinder Plus&amp;quot; before reconstructing the tree.&lt;br /&gt;
&lt;br /&gt;
: When the run has finished, inspect the output written to the screen and to the file ending in &amp;quot;.iqtree&amp;quot;. Note that IQ-TREE 3 reports several model-selection criteria in the output. The line near the top of the ModelFinder section states which criterion was used to choose the final model. Locate the substitution model selected by IQ-TREE 3 and the log likelihood of the best tree.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Question: &#039;&#039;&#039; Which substitution model was selected by IQ-TREE 3, and according to which information criterion was it selected? What was the log likelihood of the best tree? Compare this to the earlier jModelTest2 + PAUP analysis. Is the selected model exactly the same? Is the likelihood exactly the same? Briefly suggest one reason why the two analyses might not give identical results.&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
== Detection of positively selected sites in gp120 ==&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Question 7&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
: There is much more to phylogenetic analyses than merely reconstructing trees. One interesting result of probabilistic methods, is that the parameters of a model will have their values estimated as part of the optimization procedure. This means that once such a model has been fitted to the data, it is possible to investigate these estimated parameter values to learn features about the evolutionary history of the sequences under investigation. In the present example we will focus on investigating whether we can find positively selected sites in our data set, defined as sites where the dN/dS ratio is larger than 1. We do that by using a codon substitution model where the dN/dS ratio is one of its parameters.&lt;br /&gt;
&lt;br /&gt;
: A further strength of the probabilistic approach is that you get a probabilistic measure of how well any model fits the data. This means you can use a stringent approach to determine which model fits the data best. In this framework one uses likelihoods (probabilities of data given model) to determine which model fits the data best. As you saw above, it is for instance possible to compute AIC values and weights (model probabilities) from the likelihood values of fitted models, Since each model essentially corresponds to a hypothesis about the evolutionary history of the data, we can thus use a stringent statistical approach to decide which hypothesis best describes our data.&lt;br /&gt;
&lt;br /&gt;
: In outline, you will now use the following steps to investigate whether there is any evidence for positively selected codons in your data set:&lt;br /&gt;
&lt;br /&gt;
:* Fit model M1, which assumes there are two classes of codons in the sequence: some with dN/dS &amp;lt; 1, some with dN/dS=1.&lt;br /&gt;
:* Fit model M2, which assumes 3 distinct classes of codons: two with dN/dS ratios as for M1, and one extra class with dN/dS &amp;gt; 1.&lt;br /&gt;
:* Assess the strength of evidence for the two models using AIC-based model probabilities&lt;br /&gt;
:* If M2 is better: identify the positively selected codons&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Inspect the parameter file&#039;&#039;&#039;&lt;br /&gt;
 nedit codeml.ctl &amp;amp;&lt;br /&gt;
: The file &amp;quot;codeml.ctl&amp;quot; contains several settings that are relevant for running the program &#039;&#039;&#039;codeml&#039;&#039;&#039;. Find the following lines and ensure that the file contains these values:&lt;br /&gt;
 &#039;&#039;&#039;seqfile =  gp120align.fasta&#039;&#039;&#039;:  name of alignment file&lt;br /&gt;
 &#039;&#039;&#039;treefile =  gp120tree.phy&#039;&#039;&#039;: name of tree file&lt;br /&gt;
 &#039;&#039;&#039;seqtype = 1&#039;&#039;&#039;: tells the program that our data consists of coding DNA.&lt;br /&gt;
 &#039;&#039;&#039;NSsites = 1 2&#039;&#039;&#039; : tells the program to analyze models M1 and M2.&lt;br /&gt;
 &#039;&#039;&#039;cleandata = 0&#039;&#039;&#039;: tells the program to keep positions with gaps.&lt;br /&gt;
&lt;br /&gt;
: The settings entered by us will cause codeml to analyze two hypotheses about dN/dS ratios. M1 says there are two classes of codons with different dN/dS ratios in the sequence: one class with dN/dS &amp;lt; 1 (codons under purifying or negative selection), and one class with dN/dS=1 (no selection - neutrally evolving sites). M2 says there are 3 distinct dN/dS ratios for different sites in the sequence: one class with dN/dS &amp;lt; 1, one class with dN/dS=1 (these are the same type of classes as for M1), and one class with dN/dS &amp;gt; 1 (corresponding to sites under positive selection). The value of the dN/dS ratios (for those classes that have dN/dS &amp;lt; 1 or dN/dS &amp;gt; 1), the fraction of sites belonging to each class, and the position of sites belonging to each class, are unknown at first and will be determined during the analysis.&lt;br /&gt;
&lt;br /&gt;
: (Regarding &amp;quot;cleandata&amp;quot; setting: &amp;quot;cleandata = 1&amp;quot; would cause codeml to discard any alignment columns with gaps or ambiguity symbols. For alignments with many sequences this can lead to discarding the majority of sites and I would therefore recommend not using that setting. At the same time you do want to be wary of columns where most sequences have gaps, since inference of selection for these will be very uncertain. It might be better to discard such columns from an alignment before doing the codeml analysis).&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Start the analysis&#039;&#039;&#039;&lt;br /&gt;
 codeml&lt;br /&gt;
: This will start the codeml program using the settings in the file codeml.ctl. Depending on your computer, this will take some minutes to finish. (You may be able to see how the optimization procedure results in progressively better fits: the likelihood increases, meaning that negative log-likelihood decreases, as the fit improves).&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Inspect result file:&#039;&#039;&#039;&lt;br /&gt;
: Wait for the run to finish, and then look at the result file:&lt;br /&gt;
 nedit selection.results &amp;amp;&lt;br /&gt;
: This file contains a wealth of information concerning your analysis. You may want to turn off line-wrapping to more clearly see the structure of the file (there will be some long lines that will otherwise wrap around). The top part of the file gives an overview of your sequences, codon usage and nucleotide frequencies. You can ignore this information for now, and move on to the interesting part, namely the model likelihoods and parameter values:&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Find likelihood, and number of free parameters for model M1&#039;&#039;&#039;&lt;br /&gt;
 Search ==&amp;gt; Find... ==&amp;gt; enter &amp;quot;Model 1&amp;quot; and click Find&lt;br /&gt;
: You are now in the region of the result file where the model likelihoods and parameter estimates are noted. Now, locate a line that looks a bit like the one shown below:&lt;br /&gt;
 lnL(ntime: 72  np: 74):  -4242.470345     +0.000000&lt;br /&gt;
: Identify the number of &amp;quot;free parameters&amp;quot;, K, used in model M1: This is indicated by &amp;quot;np&amp;quot;, and is 74 in the example shown above (most of these parameters are branch lengths in the tree; specifically, the number of branch length parameters is indicated by &amp;quot;ntime&amp;quot;, and is 72 in this example). Also note the log-likelihood of the fitted model. This is the number right after the parenthesis, and is -4242.470345 in the example here.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Question: &#039;&#039;&#039;What are the values of K and lnL for model M1?&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Question 8&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Find dn/dS ratios and codon class proportions for model M1:&#039;&#039;&#039;&lt;br /&gt;
: Scroll down a few lines until you get to a small table similar to this:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
      dN/dS for site classes (K=2)&lt;br /&gt;
      p:   0.75111  0.24889&lt;br /&gt;
      w:   0.06583  1.00000&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
: This gives a summary of the dN/dS ratios that were found in the data set. The line starting &amp;quot;w:&amp;quot; lists the two dN/dS ratios that were found (in this case 0.06583 and 1.00000 - the last one was pre-specified by us as part of the model and was therefore not a free parameter). In this context &amp;quot;w&amp;quot; means &amp;quot;omega&amp;quot;, which is the symbol typically used to represent the dN/dS rate ratio. The line starting p: gives the proportion of codon sites belonging to each of the dN/dS ratio classes (in the example above approximately 75% belong to the first class , while 25% of all sites belong to the class having dN/dS=1.00000).&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Question: &#039;&#039;&#039;What are the dN/dS value (w) and proportion (p) of sites for both classes. Report the following values: p(class1), w(class1), p(class2), w(class2)&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Question 9&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Find likelihood, and K for model M2&#039;&#039;&#039;&lt;br /&gt;
: Scroll past the M1 output until you get to the results for model M2.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Question: &#039;&#039;&#039;What are the values of K and lnL for model M2?&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Question 10&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Find dn/dS ratios and codon class proportions for model M2:&#039;&#039;&#039;&lt;br /&gt;
: Now, scroll down a few lines until you get to a small table similar to the one you examined for M1 before. For this model there are 3 separate classes of codons.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Question: &#039;&#039;&#039;What are the dN/dS value (w) and proportion (p) of sites for all three classes? Report these values: p(class1), w(class1), p(class2), w(class2), p(class3), w(class3)&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Question 11&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Assess strength of evidence for models M1 and M2:&#039;&#039;&#039;&lt;br /&gt;
: M2 will always have a better (higher) log-likelihood than model M1 because M2 has more free parameters, and M1 is nested within M2. You should now use the recipe given above to compute AIC values and model probabilities for M1 and M2.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Question: &#039;&#039;&#039; Report: AIC, ΔAIC, weight (model probability) for M1 and M2&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Question 12: &#039;&#039;&#039; Is M2 better than M1?&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Question 13&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Examine list of positively selected sites&#039;&#039;&#039;&lt;br /&gt;
: If your M2 is clearly better than M1 (I firmly believe it should be if you did things according to instructions...), then you have evidence for the existence of positively selected sites in the gp120 gene. Now, scroll down to the end of the result file and locate a list similar to the one below. Note: This is the &amp;quot;Bayes Empirical Bayes&amp;quot; table, not the &amp;quot;Naive Empirical Bayes&amp;quot; table. &lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Bayes Empirical Bayes (BEB) analysis&lt;br /&gt;
Positively selected sites&lt;br /&gt;
&lt;br /&gt;
         Prob(w&amp;gt;1)     mean w&lt;br /&gt;
&lt;br /&gt;
    25 A 0.959*        3.133 +- 0.769&lt;br /&gt;
    27 P 0.906         2.990 +- 0.877&lt;br /&gt;
    56 K 0.987*        3.197 +- 0.687&lt;br /&gt;
    59 V 0.915         3.032 +- 0.873&lt;br /&gt;
    78 R 0.637         2.351 +- 1.129&lt;br /&gt;
    88 K 0.573         2.148 +- 1.077&lt;br /&gt;
    95 V 0.925         3.046 +- 0.843&lt;br /&gt;
    ...&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
: It is not important what the distinction is in this context, but very briefly NEB ignores the fact that there is uncertainty about  maximum likelihood estimates, especially for smaller data sets (for instance w for some codon is perhaps not exactly 3.046, but could be in a region around that value), while [https://pubmed.ncbi.nlm.nih.gov/15689528/ BEB accounts for that uncertainty].&lt;br /&gt;
: This gives you a list of which residues (if any) that were found to belong to the positively selected dN/dS-class. Also listed is the probability that the site really is in the codon class where dN/dS &amp;gt; 1, and a weighted average of the w at the site. Using only DNA sequences you have now identified likely epitopes on the gp120 protein.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Question: &#039;&#039;&#039;List all sites having more than 95% probability of belonging to the positively selected class&lt;/div&gt;</summary>
		<author><name>Gorm</name></author>
	</entry>
	<entry>
		<id>https://teaching.healthtech.dtu.dk/22115/index.php?title=Model_selection&amp;diff=274</id>
		<title>Model selection</title>
		<link rel="alternate" type="text/html" href="https://teaching.healthtech.dtu.dk/22115/index.php?title=Model_selection&amp;diff=274"/>
		<updated>2026-04-15T08:29:03Z</updated>

		<summary type="html">&lt;p&gt;Gorm: /* Model selection and tree reconstruction in one step using IQ-TREE 3 */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;This exercise is part of the course  [[22115_-_Computational_Molecular_Evolution|Computational Molecular Evolution (22115)]].&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Overview ==&lt;br /&gt;
&lt;br /&gt;
: In this exercise you are going to investigate features of HIV-1 evolution. You will do this by analyzing a large set of env-genes from HIV-1, subtype B. specifically, the DNA sequences analyzed here correspond to a region surrounding the hypervariable V3 region of the gp120 protein.&lt;br /&gt;
&lt;br /&gt;
: Like other retroviruses, particles of HIV are made up of 2 copies of a single-stranded RNA genome packaged inside a protein core, or capsid. The core particle also contains viral proteins that are essential for the early steps of the virus life cycle, such as reverse transcription and integration. A lipid envelope, derived from the infected cell, surrounds the core particle. Embedded in this envelope are the surface glycoproteins of HIV: gp120 and gp41. The gp120 protein is crucial for binding of the virus particle to target cells, while gp41 is important for the subsequent fusion event. It is the specific affinity of gp120 for the CD4 protein that targets HIV to those cells of the immune system that express CD4 on their surface (e.g., T-helper lymphocytes, monocytes, and macrophages).&lt;br /&gt;
&lt;br /&gt;
: The role gp120 plays in infection and the fact that it is situated on the surface of the HIV particle, means it is an obvious target for the immune response. That means that there may be a considerable selective pressure on gp120 for creating immune-escape mutants, where amino acids in the gp120 epitopes have been substituted. In this exercise you will construct a maximum likelihood tree that we will subsequently use to investigate whether you can detect such a selective pressure on parts of gp120, again using maximum likelihood methods.&lt;br /&gt;
&lt;br /&gt;
: One major goal with the exercise is to introduce you to statistically based methods for assessing the strength of evidence for a set of alternative hypotheses about some biological system of interest. The model selection method we will use is AIC (Akaike Information Criterion), based on which you will compute model probabilities. A second goal is to make you aware that phylogenetic analysis is not only about constructing trees, but that it is also a useful framework for analyzing biological questions more generally.&lt;br /&gt;
&lt;br /&gt;
: Specifically, you will&lt;br /&gt;
&lt;br /&gt;
:# perform a multiple alignment of gp120 DNA sequences taking protein-level information into account (using revtrans).&lt;br /&gt;
:# select a suitable nucleotide substitution model (using jmodeltest2)&lt;br /&gt;
:# construct a phylogenetic tree (using PAUP).&lt;br /&gt;
:# try to detect positively selected sites in gp120 (using PAML).&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
== Recipe for computing AIC values and model probabilities ==&lt;br /&gt;
&lt;br /&gt;
: Later in today&#039;s exercise you will be asked to compute AIC values and model probabilities. Return to this section and follow the instructions when you need to do so.&lt;br /&gt;
&lt;br /&gt;
:# Fit a set of models to your data, note the maximized log likelihoods (lnL) and the number of free parameters (K) for each model in the investigated set. The models you fit should represent a plausible and comprehensive set of hypotheses about your data.&lt;br /&gt;
:# Compute AIC for each of the models: &#039;&#039;&#039;AIC = -2 x lnL + 2K&#039;&#039;&#039;.  &amp;lt;br&amp;gt;For example: a model with lnL = -2010 and K = 5 will have AIC = -2 x -2010 + 2 x 5 = 4030.&lt;br /&gt;
:# Identify the model with the smallest AIC (this is the best model in the set). We will call the AIC for this model &#039;&#039;&#039;&amp;quot;AICmin&amp;quot;&#039;&#039;&#039;.&lt;br /&gt;
:# Compute the &amp;quot;ΔAIC&amp;quot; values for each model: &#039;&#039;&#039;ΔAIC = AIC - AICmin&#039;&#039;&#039; &amp;lt;br&amp;gt;For each model subtract the minimum AIC value. The best model will have a ΔAIC of zero. The rest of the models will have positive ΔAICs.&lt;br /&gt;
:# For each model compute the following quantity: &#039;&#039;&#039;numerator = exp(-0.5 x ΔAIC)&#039;&#039;&#039; &amp;lt;br&amp;gt;For example, a model with ΔAIC=4.2 will have numerator = exp(-0.5 x 4.2) = exp(-2.1) = 0.1225. Also compute the &#039;&#039;&#039;sum&#039;&#039;&#039; of the numerator values for all models.&lt;br /&gt;
:# Finally, the model probabilities (also called model weights) for each model are found as:&amp;lt;br&amp;gt; &#039;&#039;&#039;P(model) = numerator / sum&#039;&#039;&#039; &amp;lt;br&amp;gt;For example, if sum = 3.75 and a model has numerator = 1.3, then it has P(model) = 1.3 / 3.75 = 0.35&lt;br /&gt;
&lt;br /&gt;
: You may want to keep track of the computations by constructing a table along the following lines:&lt;br /&gt;
[[File:Molevol-Downloads-aictable.png|700px]]&lt;br /&gt;
&lt;br /&gt;
: Note that model probabilities can also be computed using Bayesian methods. One advantage of Bayesian methods over AIC is that instead of relying on a point estimate, uncertainty about parameter values is accounted for by integrating over all possible values (typically using MCMC).&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
== Getting started ==&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Create working directory, copy files&#039;&#039;&#039;&lt;br /&gt;
: In the command below: Instead of /path/to/molevol enter the path to the directory where you have placed your course files (for instance cd /Users/bob/Documents/molevol, or cd /home/student/molevol).&lt;br /&gt;
 cd /path/to/molevol&lt;br /&gt;
 mkdir modelselect&lt;br /&gt;
 cd modelselect&lt;br /&gt;
 cp ../data/gp120.fasta ./gp120.fasta&lt;br /&gt;
 cp ../data/codeml.ctl ./codeml.ctl&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Have a look at the DNA data file:&#039;&#039;&#039;&lt;br /&gt;
 nedit gp120.fasta &amp;amp;&lt;br /&gt;
: The file contains several DNA sequences from HIV-1, subtype B. The sequences are approximately 500 bp long, and correspond to a region surrounding the hypervariable V3 region in the gene encoding gp120. Close the nedit window when you&#039;ve had a look.&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
== Protein-guided alignment of coding DNA ==&lt;br /&gt;
&lt;br /&gt;
: There are three reasons why it is preferable to align coding DNA at the amino acid level rather than directly as nucleotides. First, the DNA alphabet has only four letters compared to 20 amino acids, which means that even unrelated DNA sequences will share roughly 25% identity by chance alone, making it much harder to distinguish true homology from noise. Second, protein alignments benefit from empirical substitution matrices such as BLOSUM-62, which capture the fact that amino acid replacements tend to be conservative. Equivalent matrices for DNA are far less informative because nucleotide substitution patterns vary strongly between genes and organisms. Third, synonymous substitutions accumulate faster than non-synonymous ones, so DNA sequences diverge more rapidly than the proteins they encode. Taken together, these factors mean that the phylogenetic signal in a DNA alignment erodes much faster than in the corresponding protein alignment.&lt;br /&gt;
&lt;br /&gt;
: However, while protein-level alignment gives us the most reliable homology signal, it discards exactly the information we need for studying selection: the synonymous substitutions. Estimating the ratio of non-synonymous to synonymous substitution rates (dN/dS) requires a codon-level alignment where analogous codon positions are properly lined up. We would therefore like to construct a multiple alignment at the DNA level, but using information at the protein level, and the RevTrans server does exactly that.&lt;br /&gt;
&lt;br /&gt;
: &#039;&#039;&#039;RevTrans&#039;&#039;&#039; takes as input an unaligned set of DNA sequences, automatically translates them to the equivalent amino acid sequences, constructs a multiple alignment of the protein sequences, and finally uses the protein alignment as a template for constructing a multiple DNA alignment. Because each amino acid maps back to its original codon, gaps are always inserted in groups of three and codon boundaries are preserved throughout the alignment. This is essential for the dN/dS analyses we will carry out in this exercise: if codon positions are misaligned, synonymous and non-synonymous changes cannot be correctly distinguished.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Construct RevTrans alignment&#039;&#039;&#039;&lt;br /&gt;
:* Open RevTrans server page: https://services.healthtech.dtu.dk/services/RevTrans-2.0/&lt;br /&gt;
:* On the RevTrans page: Choose the file gp120.fasta as input (or copy and paste the sequence into the sequence window)&lt;br /&gt;
:* Click the &amp;quot;Submit query&amp;quot; button&lt;br /&gt;
:* When the alignment is done you may have to click link named &amp;quot;here&amp;quot; to go to results page&lt;br /&gt;
:* Download DNA alignment, by right-clicking the link for &amp;quot;Download alignment in FASTA format&amp;quot;, and choosing &amp;quot;Save link as...&amp;quot; (save file under the name gp120align.fasta and make sure to save the file in the directory modelselect).&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Convert alignment to NEXUS format&#039;&#039;&#039;&lt;br /&gt;
: Convert the fasta file to NEXUS format and save file in the modelselect directory under the name gp120.nexus&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
== Model selection using AIC and jModelTest2 ==&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Question 1&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
: As part of the present analysis we are going to build a phylogenetic tree based on the DNA alignment constructed above. We will construct the tree using maximum likelihood, but to do that we first have to decide which substitution model we want to use. Specifically, we are interested in selecting a model that achieves a good balance between fit to the data and number of parameters. We will investigate this issue by fitting a set of 56 different models to our data and then comparing them using AIC.&lt;br /&gt;
&lt;br /&gt;
: In the first part of the exercise, however, you will only compare three closely related models by hand. The purpose of this restricted comparison is not primarily to identify the globally best model, but to understand how AIC, ΔAIC, and Akaike weights are computed and interpreted. Because only three models are included in this manual comparison, the weights you compute will describe relative support &#039;&#039;&#039;within this restricted set only&#039;&#039;&#039;.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Start jmodeltest2&#039;&#039;&#039;&lt;br /&gt;
 jmodeltest&lt;br /&gt;
&#039;&#039;&#039;Load data set&#039;&#039;&#039;&lt;br /&gt;
:* File -&amp;gt; Load DNA alignment -&amp;gt; File Format -&amp;gt; Select &amp;quot;All files&amp;quot;&lt;br /&gt;
:* Navigate to gp120.nexus and load it&lt;br /&gt;
&#039;&#039;&#039;Fit 56 models&#039;&#039;&#039;&lt;br /&gt;
:* Analysis -&amp;gt; Compute likelihood scores&lt;br /&gt;
:* Select &amp;quot;7&amp;quot; under &amp;quot;Number of substitution schemes&amp;quot;&lt;br /&gt;
:* Select &amp;quot;Fixed BIONJ-JC&amp;quot; under &amp;quot;Base tree for likelihood calculations&amp;quot;&lt;br /&gt;
:* Click &amp;quot;Compute likelihoods&amp;quot;&lt;br /&gt;
&lt;br /&gt;
: This causes jmodeltest2 to perform the following actions: first a neighbor joining tree is constructed using the Jukes and Cantor model. Then the tree is fixed and used as the basis for fitting a set of 56 different models to the data. For each model, the estimated model parameters and the negative log-likelihood are recorded. In addition to varying sets of substitution rate parameters (JC, K2P, ...), some of these models also include extra parameters that take into account the presence of different rates between sites. This is done in two ways: (1) by fitting a gamma distribution of rates (&amp;quot;+G&amp;quot;), and (2) by allowing for a proportion of constant (&amp;quot;invariable&amp;quot;) sites (&amp;quot;+I&amp;quot;).&lt;br /&gt;
&lt;br /&gt;
: Wait until jmodeltest2 is done fitting all 56 models (this will take a little while depending on your computer).&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Inspect result&#039;&#039;&#039;&lt;br /&gt;
:* Results -&amp;gt; Show results table&lt;br /&gt;
: For each model this table lists the negative log-likelihood (&amp;quot;-lnL&amp;quot;), the number of parameters (&amp;quot;p&amp;quot; - note this includes branch lengths), and estimates of all model parameters (excluding branch lengths).&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Manually compute Akaike weights for three substitution models&#039;&#039;&#039;&lt;br /&gt;
: Use AIC-based model comparison to investigate which of the following three substitution models are best supported as descriptions of how the sequences have evolved:&lt;br /&gt;
:* Jukes and Cantor with fraction of invariant sites (JC+I)&lt;br /&gt;
:* Jukes and Cantor with gamma-distributed rates over sites (JC+G)&lt;br /&gt;
:* Jukes and Cantor with invariant sites and gamma-distributed rates (JC+I+G)&lt;br /&gt;
: Before you can do the computation you need to know the log likelihood and the number of parameters for each model. Locate these values in the table for the JC+I, JC+G, and JC+I+G models, and write them down. Close the window with the result table when you are done.&lt;br /&gt;
&lt;br /&gt;
: Make sure to get the signs right: the values reported in the table are -lnL values, so you will need to reverse the sign to get the lnL (the lnL values you write down should be negative).&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Question: &#039;&#039;&#039;Use the recipe above to compute AIC, ΔAIC, and Akaike weight for the three models. Report the results in a table similar to the one shown above, and verify that the three weights sum to 1 (up to rounding error).&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Question 2&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
: Based on the Akaike weights, which of the three models has the strongest support? Is the support strongly concentrated on one model, or is there still substantial support for one or both of the others?&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Question 3&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
: What you just did manually for JC+I, JC+G and JC+I+G, jmodeltest2 can do automatically for the full set of 56 fitted models. Specifically, it uses the list of negative log likelihoods and parameter counts in the table to compute AIC values and Akaike weights for the full candidate set:&lt;br /&gt;
:* Analysis -&amp;gt; Do AIC calculations -&amp;gt;&lt;br /&gt;
:* Select &amp;quot;Write PAUP* block&amp;quot;&lt;br /&gt;
:* click &amp;quot;Do AIC calculations&amp;quot;&lt;br /&gt;
:* Results -&amp;gt; Show results table&lt;br /&gt;
:* Select &amp;quot;AIC&amp;quot; tab&lt;br /&gt;
:* &#039;&#039;&#039;IMPORTANT:&#039;&#039;&#039; SHIFT+click on the header of the &amp;quot;weight&amp;quot; column. This sorts the rows according to model weight, in descending order.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Question: &#039;&#039;&#039;What model was selected by modeltest based on the AIC values? (This will be the model with the highest weight - and lowest AIC - and will be in the first row of the results table after SHIFT-clicking the &amp;quot;weight&amp;quot; header).&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Question 4&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
: In the same AIC results table, now click (not SHIFT+click) the header of the &amp;quot;-lnL&amp;quot; column. In this table, a normal click sorts in ascending order, whereas SHIFT+click sorts in descending order. The model at the top will therefore be the one with the smallest value of &amp;quot;-lnL&amp;quot; (equivalently: the highest likelihood). Compare this model to the one with the highest Akaike weight.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Question: &#039;&#039;&#039;Is the model with the highest likelihood also the model with the highest Akaike weight? If not, explain briefly why these two rankings can differ.&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
== Construction of phylogenetic tree using PAUP ==&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Question 5&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
: Close the results table. In the main window you should now scroll up to the lines giving PAUP commands that implement the selected model. The command is enclosed between &amp;quot;BEGIN PAUP&amp;quot; and &amp;quot;END;&amp;quot; and should look something like this:&lt;br /&gt;
 Lset Base=(0.4064, [...]&lt;br /&gt;
: You will need to copy this command to a PAUP session in the next step.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Start PAUP&#039;&#039;&#039;&lt;br /&gt;
 paup&lt;br /&gt;
: Above, you used jmodeltest2 to select the most suitable substitution model for the present data set. You will now use this model to construct a maximum likelihood tree. You will use PAUP for this purpose. (Note: it is possible to create a maximum likelihood tree, or even a model-averaged tree, directly from jmodeltest2, but we will instead do it in PAUP in order to more clearly see each step that is taken.)&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Load alignment:&#039;&#039;&#039;&lt;br /&gt;
 execute gp120.nexus&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Set tree-building criterion to maximum likelihood&#039;&#039;&#039;&lt;br /&gt;
 set criterion=likelihood&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Set model parameters to winning estimates&#039;&#039;&#039;&lt;br /&gt;
: Above, you located a set of lines in the jmodeltest2 output giving a PAUP command that sets the model parameters to the estimates found for the winning model. Copy and paste this lset command (without the BEGIN and END parts) into the window where PAUP is running.&lt;br /&gt;
&lt;br /&gt;
 PASTE LSET COMMAND FROM MODELTEST RUN HERE&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Find best tree using selected model&#039;&#039;&#039;&lt;br /&gt;
: Still in the PAUP window, enter the following command:&lt;br /&gt;
 hsearch swap=tbr start=nj&lt;br /&gt;
: This command causes PAUP to perform a heuristic search for the best maximum likelihood tree. Once an initial tree has been constructed, the heuristic search proceeds by rearrangements of the &amp;quot;tree bisection and reconnection&amp;quot; type (TBR). We are using the model selected by modeltest, and the parameter estimates found by modeltest for that model. You could also choose to estimate all the model parameters as part of this step (i.e., at the same time as finding the best tree), but fixing them improves speed tremendously. Finding the best tree should take at most a few minutes.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Save best tree to file&#039;&#039;&#039;&lt;br /&gt;
 savetrees format=newick brlens=yes file=gp120tree.phy from=1 to=1&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Quit program&#039;&#039;&#039;&lt;br /&gt;
 quit&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Have a look at the tree:&#039;&#039;&#039;&lt;br /&gt;
: You have now produced an unrooted tree of the HIV sequences and saved it in the file gp120tree.phy. Note that in this exercise we will not be interested in the tree as such - our focus is instead on finding positive selection at a subset of codon positions, and the tree is just something we need in order to fit the different codon models to the data. If you want to see the tree, you can do so with the following command:&lt;br /&gt;
 figtree gp120tree.phy &amp;amp;&lt;br /&gt;
: There is no meaningful root placed on this tree, so you may want to choose the unrooted view (the third icon in the Layout section of the FigTree window). Close the FigTree window when you have had a look.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Question: &#039;&#039;&#039;Which substitution model did you use for the PAUP tree search, and what was the negative log likelihood of the best tree found under this model?&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
== Model selection and tree reconstruction in one step using IQ-TREE 3 ==&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Question 6&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
: In real-life phylogenetic analysis one would often use a modern program such as &#039;&#039;&#039;IQ-TREE 3&#039;&#039;&#039;, which can carry out model selection and maximum likelihood tree reconstruction in a single analysis.&lt;br /&gt;
&lt;br /&gt;
: Run IQ-TREE 3 on the alignment using the following command:&lt;br /&gt;
 iqtree3 -s gp120.nexus -m MFP&lt;br /&gt;
&lt;br /&gt;
: Here, &amp;quot;-m MFP&amp;quot; tells IQ-TREE 3 to perform automatic model selection using &amp;quot;ModelFinder Plus&amp;quot; before reconstructing the tree.&lt;br /&gt;
&lt;br /&gt;
: When the run has finished, inspect the output written to the screen and to the file ending in &amp;quot;.iqtree&amp;quot;. Note that IQ-TREE 3 reports several model-selection criteria in the output. The line near the top of the ModelFinder section states which criterion was used to choose the final model. Locate the substitution model selected by IQ-TREE 3 and the log likelihood of the best tree.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Question: &#039;&#039;&#039; Which substitution model was selected by IQ-TREE 3, and according to which information criterion was it selected? What was the log likelihood of the best tree? Compare this to the earlier jModelTest2 + PAUP analysis. Is the selected model exactly the same? Is the likelihood exactly the same? Briefly suggest one reason why the two analyses might not give identical results.&lt;br /&gt;
&lt;br /&gt;
== Detection of positively selected sites in gp120 ==&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Question 7&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
: There is much more to phylogenetic analyses than merely reconstructing trees. One interesting result of probabilistic methods, is that the parameters of a model will have their values estimated as part of the optimization procedure. This means that once such a model has been fitted to the data, it is possible to investigate these estimated parameter values to learn features about the evolutionary history of the sequences under investigation. In the present example we will focus on investigating whether we can find positively selected sites in our data set, defined as sites where the dN/dS ratio is larger than 1. We do that by using a codon substitution model where the dN/dS ratio is one of its parameters.&lt;br /&gt;
&lt;br /&gt;
: A further strength of the probabilistic approach is that you get a probabilistic measure of how well any model fits the data. This means you can use a stringent approach to determine which model fits the data best. In this framework one uses likelihoods (probabilities of data given model) to determine which model fits the data best. As you saw above, it is for instance possible to compute AIC values and weights (model probabilities) from the likelihood values of fitted models, Since each model essentially corresponds to a hypothesis about the evolutionary history of the data, we can thus use a stringent statistical approach to decide which hypothesis best describes our data.&lt;br /&gt;
&lt;br /&gt;
: In outline, you will now use the following steps to investigate whether there is any evidence for positively selected codons in your data set:&lt;br /&gt;
&lt;br /&gt;
:* Fit model M1, which assumes there are two classes of codons in the sequence: some with dN/dS &amp;lt; 1, some with dN/dS=1.&lt;br /&gt;
:* Fit model M2, which assumes 3 distinct classes of codons: two with dN/dS ratios as for M1, and one extra class with dN/dS &amp;gt; 1.&lt;br /&gt;
:* Assess the strength of evidence for the two models using AIC-based model probabilities&lt;br /&gt;
:* If M2 is better: identify the positively selected codons&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Inspect the parameter file&#039;&#039;&#039;&lt;br /&gt;
 nedit codeml.ctl &amp;amp;&lt;br /&gt;
: The file &amp;quot;codeml.ctl&amp;quot; contains several settings that are relevant for running the program &#039;&#039;&#039;codeml&#039;&#039;&#039;. Find the following lines and ensure that the file contains these values:&lt;br /&gt;
 &#039;&#039;&#039;seqfile =  gp120align.fasta&#039;&#039;&#039;:  name of alignment file&lt;br /&gt;
 &#039;&#039;&#039;treefile =  gp120tree.phy&#039;&#039;&#039;: name of tree file&lt;br /&gt;
 &#039;&#039;&#039;seqtype = 1&#039;&#039;&#039;: tells the program that our data consists of coding DNA.&lt;br /&gt;
 &#039;&#039;&#039;NSsites = 1 2&#039;&#039;&#039; : tells the program to analyze models M1 and M2.&lt;br /&gt;
 &#039;&#039;&#039;cleandata = 0&#039;&#039;&#039;: tells the program to keep positions with gaps.&lt;br /&gt;
&lt;br /&gt;
: The settings entered by us will cause codeml to analyze two hypotheses about dN/dS ratios. M1 says there are two classes of codons with different dN/dS ratios in the sequence: one class with dN/dS &amp;lt; 1 (codons under purifying or negative selection), and one class with dN/dS=1 (no selection - neutrally evolving sites). M2 says there are 3 distinct dN/dS ratios for different sites in the sequence: one class with dN/dS &amp;lt; 1, one class with dN/dS=1 (these are the same type of classes as for M1), and one class with dN/dS &amp;gt; 1 (corresponding to sites under positive selection). The value of the dN/dS ratios (for those classes that have dN/dS &amp;lt; 1 or dN/dS &amp;gt; 1), the fraction of sites belonging to each class, and the position of sites belonging to each class, are unknown at first and will be determined during the analysis.&lt;br /&gt;
&lt;br /&gt;
: (Regarding &amp;quot;cleandata&amp;quot; setting: &amp;quot;cleandata = 1&amp;quot; would cause codeml to discard any alignment columns with gaps or ambiguity symbols. For alignments with many sequences this can lead to discarding the majority of sites and I would therefore recommend not using that setting. At the same time you do want to be wary of columns where most sequences have gaps, since inference of selection for these will be very uncertain. It might be better to discard such columns from an alignment before doing the codeml analysis).&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Start the analysis&#039;&#039;&#039;&lt;br /&gt;
 codeml&lt;br /&gt;
: This will start the codeml program using the settings in the file codeml.ctl. Depending on your computer, this will take some minutes to finish. (You may be able to see how the optimization procedure results in progressively better fits: the likelihood increases, meaning that negative log-likelihood decreases, as the fit improves).&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Inspect result file:&#039;&#039;&#039;&lt;br /&gt;
: Wait for the run to finish, and then look at the result file:&lt;br /&gt;
 nedit selection.results &amp;amp;&lt;br /&gt;
: This file contains a wealth of information concerning your analysis. You may want to turn off line-wrapping to more clearly see the structure of the file (there will be some long lines that will otherwise wrap around). The top part of the file gives an overview of your sequences, codon usage and nucleotide frequencies. You can ignore this information for now, and move on to the interesting part, namely the model likelihoods and parameter values:&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Find likelihood, and number of free parameters for model M1&#039;&#039;&#039;&lt;br /&gt;
 Search ==&amp;gt; Find... ==&amp;gt; enter &amp;quot;Model 1&amp;quot; and click Find&lt;br /&gt;
: You are now in the region of the result file where the model likelihoods and parameter estimates are noted. Now, locate a line that looks a bit like the one shown below:&lt;br /&gt;
 lnL(ntime: 72  np: 74):  -4242.470345     +0.000000&lt;br /&gt;
: Identify the number of &amp;quot;free parameters&amp;quot;, K, used in model M1: This is indicated by &amp;quot;np&amp;quot;, and is 74 in the example shown above (most of these parameters are branch lengths in the tree; specifically, the number of branch length parameters is indicated by &amp;quot;ntime&amp;quot;, and is 72 in this example). Also note the log-likelihood of the fitted model. This is the number right after the parenthesis, and is -4242.470345 in the example here.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Question: &#039;&#039;&#039;What are the values of K and lnL for model M1?&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Question 8&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Find dn/dS ratios and codon class proportions for model M1:&#039;&#039;&#039;&lt;br /&gt;
: Scroll down a few lines until you get to a small table similar to this:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
      dN/dS for site classes (K=2)&lt;br /&gt;
      p:   0.75111  0.24889&lt;br /&gt;
      w:   0.06583  1.00000&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
: This gives a summary of the dN/dS ratios that were found in the data set. The line starting &amp;quot;w:&amp;quot; lists the two dN/dS ratios that were found (in this case 0.06583 and 1.00000 - the last one was pre-specified by us as part of the model and was therefore not a free parameter). In this context &amp;quot;w&amp;quot; means &amp;quot;omega&amp;quot;, which is the symbol typically used to represent the dN/dS rate ratio. The line starting p: gives the proportion of codon sites belonging to each of the dN/dS ratio classes (in the example above approximately 75% belong to the first class , while 25% of all sites belong to the class having dN/dS=1.00000).&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Question: &#039;&#039;&#039;What are the dN/dS value (w) and proportion (p) of sites for both classes. Report the following values: p(class1), w(class1), p(class2), w(class2)&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Question 9&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Find likelihood, and K for model M2&#039;&#039;&#039;&lt;br /&gt;
: Scroll past the M1 output until you get to the results for model M2.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Question: &#039;&#039;&#039;What are the values of K and lnL for model M2?&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Question 10&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Find dn/dS ratios and codon class proportions for model M2:&#039;&#039;&#039;&lt;br /&gt;
: Now, scroll down a few lines until you get to a small table similar to the one you examined for M1 before. For this model there are 3 separate classes of codons.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Question: &#039;&#039;&#039;What are the dN/dS value (w) and proportion (p) of sites for all three classes? Report these values: p(class1), w(class1), p(class2), w(class2), p(class3), w(class3)&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Question 11&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Assess strength of evidence for models M1 and M2:&#039;&#039;&#039;&lt;br /&gt;
: M2 will always have a better (higher) log-likelihood than model M1 because M2 has more free parameters, and M1 is nested within M2. You should now use the recipe given above to compute AIC values and model probabilities for M1 and M2.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Question: &#039;&#039;&#039; Report: AIC, ΔAIC, weight (model probability) for M1 and M2&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Question 12: &#039;&#039;&#039; Is M2 better than M1?&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Question 13&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Examine list of positively selected sites&#039;&#039;&#039;&lt;br /&gt;
: If your M2 is clearly better than M1 (I firmly believe it should be if you did things according to instructions...), then you have evidence for the existence of positively selected sites in the gp120 gene. Now, scroll down to the end of the result file and locate a list similar to the one below. Note: This is the &amp;quot;Bayes Empirical Bayes&amp;quot; table, not the &amp;quot;Naive Empirical Bayes&amp;quot; table. &lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Bayes Empirical Bayes (BEB) analysis&lt;br /&gt;
Positively selected sites&lt;br /&gt;
&lt;br /&gt;
         Prob(w&amp;gt;1)     mean w&lt;br /&gt;
&lt;br /&gt;
    25 A 0.959*        3.133 +- 0.769&lt;br /&gt;
    27 P 0.906         2.990 +- 0.877&lt;br /&gt;
    56 K 0.987*        3.197 +- 0.687&lt;br /&gt;
    59 V 0.915         3.032 +- 0.873&lt;br /&gt;
    78 R 0.637         2.351 +- 1.129&lt;br /&gt;
    88 K 0.573         2.148 +- 1.077&lt;br /&gt;
    95 V 0.925         3.046 +- 0.843&lt;br /&gt;
    ...&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
: It is not important what the distinction is in this context, but very briefly NEB ignores the fact that there is uncertainty about  maximum likelihood estimates, especially for smaller data sets (for instance w for some codon is perhaps not exactly 3.046, but could be in a region around that value), while [https://pubmed.ncbi.nlm.nih.gov/15689528/ BEB accounts for that uncertainty].&lt;br /&gt;
: This gives you a list of which residues (if any) that were found to belong to the positively selected dN/dS-class. Also listed is the probability that the site really is in the codon class where dN/dS &amp;gt; 1, and a weighted average of the w at the site. Using only DNA sequences you have now identified likely epitopes on the gp120 protein.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Question: &#039;&#039;&#039;List all sites having more than 95% probability of belonging to the positively selected class&lt;/div&gt;</summary>
		<author><name>Gorm</name></author>
	</entry>
	<entry>
		<id>https://teaching.healthtech.dtu.dk/22115/index.php?title=Model_selection&amp;diff=273</id>
		<title>Model selection</title>
		<link rel="alternate" type="text/html" href="https://teaching.healthtech.dtu.dk/22115/index.php?title=Model_selection&amp;diff=273"/>
		<updated>2026-04-15T08:28:29Z</updated>

		<summary type="html">&lt;p&gt;Gorm: /* Model selection and tree reconstruction in one step using IQ-TREE 3 */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;This exercise is part of the course  [[22115_-_Computational_Molecular_Evolution|Computational Molecular Evolution (22115)]].&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Overview ==&lt;br /&gt;
&lt;br /&gt;
: In this exercise you are going to investigate features of HIV-1 evolution. You will do this by analyzing a large set of env-genes from HIV-1, subtype B. specifically, the DNA sequences analyzed here correspond to a region surrounding the hypervariable V3 region of the gp120 protein.&lt;br /&gt;
&lt;br /&gt;
: Like other retroviruses, particles of HIV are made up of 2 copies of a single-stranded RNA genome packaged inside a protein core, or capsid. The core particle also contains viral proteins that are essential for the early steps of the virus life cycle, such as reverse transcription and integration. A lipid envelope, derived from the infected cell, surrounds the core particle. Embedded in this envelope are the surface glycoproteins of HIV: gp120 and gp41. The gp120 protein is crucial for binding of the virus particle to target cells, while gp41 is important for the subsequent fusion event. It is the specific affinity of gp120 for the CD4 protein that targets HIV to those cells of the immune system that express CD4 on their surface (e.g., T-helper lymphocytes, monocytes, and macrophages).&lt;br /&gt;
&lt;br /&gt;
: The role gp120 plays in infection and the fact that it is situated on the surface of the HIV particle, means it is an obvious target for the immune response. That means that there may be a considerable selective pressure on gp120 for creating immune-escape mutants, where amino acids in the gp120 epitopes have been substituted. In this exercise you will construct a maximum likelihood tree that we will subsequently use to investigate whether you can detect such a selective pressure on parts of gp120, again using maximum likelihood methods.&lt;br /&gt;
&lt;br /&gt;
: One major goal with the exercise is to introduce you to statistically based methods for assessing the strength of evidence for a set of alternative hypotheses about some biological system of interest. The model selection method we will use is AIC (Akaike Information Criterion), based on which you will compute model probabilities. A second goal is to make you aware that phylogenetic analysis is not only about constructing trees, but that it is also a useful framework for analyzing biological questions more generally.&lt;br /&gt;
&lt;br /&gt;
: Specifically, you will&lt;br /&gt;
&lt;br /&gt;
:# perform a multiple alignment of gp120 DNA sequences taking protein-level information into account (using revtrans).&lt;br /&gt;
:# select a suitable nucleotide substitution model (using jmodeltest2)&lt;br /&gt;
:# construct a phylogenetic tree (using PAUP).&lt;br /&gt;
:# try to detect positively selected sites in gp120 (using PAML).&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
== Recipe for computing AIC values and model probabilities ==&lt;br /&gt;
&lt;br /&gt;
: Later in today&#039;s exercise you will be asked to compute AIC values and model probabilities. Return to this section and follow the instructions when you need to do so.&lt;br /&gt;
&lt;br /&gt;
:# Fit a set of models to your data, note the maximized log likelihoods (lnL) and the number of free parameters (K) for each model in the investigated set. The models you fit should represent a plausible and comprehensive set of hypotheses about your data.&lt;br /&gt;
:# Compute AIC for each of the models: &#039;&#039;&#039;AIC = -2 x lnL + 2K&#039;&#039;&#039;.  &amp;lt;br&amp;gt;For example: a model with lnL = -2010 and K = 5 will have AIC = -2 x -2010 + 2 x 5 = 4030.&lt;br /&gt;
:# Identify the model with the smallest AIC (this is the best model in the set). We will call the AIC for this model &#039;&#039;&#039;&amp;quot;AICmin&amp;quot;&#039;&#039;&#039;.&lt;br /&gt;
:# Compute the &amp;quot;ΔAIC&amp;quot; values for each model: &#039;&#039;&#039;ΔAIC = AIC - AICmin&#039;&#039;&#039; &amp;lt;br&amp;gt;For each model subtract the minimum AIC value. The best model will have a ΔAIC of zero. The rest of the models will have positive ΔAICs.&lt;br /&gt;
:# For each model compute the following quantity: &#039;&#039;&#039;numerator = exp(-0.5 x ΔAIC)&#039;&#039;&#039; &amp;lt;br&amp;gt;For example, a model with ΔAIC=4.2 will have numerator = exp(-0.5 x 4.2) = exp(-2.1) = 0.1225. Also compute the &#039;&#039;&#039;sum&#039;&#039;&#039; of the numerator values for all models.&lt;br /&gt;
:# Finally, the model probabilities (also called model weights) for each model are found as:&amp;lt;br&amp;gt; &#039;&#039;&#039;P(model) = numerator / sum&#039;&#039;&#039; &amp;lt;br&amp;gt;For example, if sum = 3.75 and a model has numerator = 1.3, then it has P(model) = 1.3 / 3.75 = 0.35&lt;br /&gt;
&lt;br /&gt;
: You may want to keep track of the computations by constructing a table along the following lines:&lt;br /&gt;
[[File:Molevol-Downloads-aictable.png|700px]]&lt;br /&gt;
&lt;br /&gt;
: Note that model probabilities can also be computed using Bayesian methods. One advantage of Bayesian methods over AIC is that instead of relying on a point estimate, uncertainty about parameter values is accounted for by integrating over all possible values (typically using MCMC).&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
== Getting started ==&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Create working directory, copy files&#039;&#039;&#039;&lt;br /&gt;
: In the command below: Instead of /path/to/molevol enter the path to the directory where you have placed your course files (for instance cd /Users/bob/Documents/molevol, or cd /home/student/molevol).&lt;br /&gt;
 cd /path/to/molevol&lt;br /&gt;
 mkdir modelselect&lt;br /&gt;
 cd modelselect&lt;br /&gt;
 cp ../data/gp120.fasta ./gp120.fasta&lt;br /&gt;
 cp ../data/codeml.ctl ./codeml.ctl&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Have a look at the DNA data file:&#039;&#039;&#039;&lt;br /&gt;
 nedit gp120.fasta &amp;amp;&lt;br /&gt;
: The file contains several DNA sequences from HIV-1, subtype B. The sequences are approximately 500 bp long, and correspond to a region surrounding the hypervariable V3 region in the gene encoding gp120. Close the nedit window when you&#039;ve had a look.&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
== Protein-guided alignment of coding DNA ==&lt;br /&gt;
&lt;br /&gt;
: There are three reasons why it is preferable to align coding DNA at the amino acid level rather than directly as nucleotides. First, the DNA alphabet has only four letters compared to 20 amino acids, which means that even unrelated DNA sequences will share roughly 25% identity by chance alone, making it much harder to distinguish true homology from noise. Second, protein alignments benefit from empirical substitution matrices such as BLOSUM-62, which capture the fact that amino acid replacements tend to be conservative. Equivalent matrices for DNA are far less informative because nucleotide substitution patterns vary strongly between genes and organisms. Third, synonymous substitutions accumulate faster than non-synonymous ones, so DNA sequences diverge more rapidly than the proteins they encode. Taken together, these factors mean that the phylogenetic signal in a DNA alignment erodes much faster than in the corresponding protein alignment.&lt;br /&gt;
&lt;br /&gt;
: However, while protein-level alignment gives us the most reliable homology signal, it discards exactly the information we need for studying selection: the synonymous substitutions. Estimating the ratio of non-synonymous to synonymous substitution rates (dN/dS) requires a codon-level alignment where analogous codon positions are properly lined up. We would therefore like to construct a multiple alignment at the DNA level, but using information at the protein level, and the RevTrans server does exactly that.&lt;br /&gt;
&lt;br /&gt;
: &#039;&#039;&#039;RevTrans&#039;&#039;&#039; takes as input an unaligned set of DNA sequences, automatically translates them to the equivalent amino acid sequences, constructs a multiple alignment of the protein sequences, and finally uses the protein alignment as a template for constructing a multiple DNA alignment. Because each amino acid maps back to its original codon, gaps are always inserted in groups of three and codon boundaries are preserved throughout the alignment. This is essential for the dN/dS analyses we will carry out in this exercise: if codon positions are misaligned, synonymous and non-synonymous changes cannot be correctly distinguished.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Construct RevTrans alignment&#039;&#039;&#039;&lt;br /&gt;
:* Open RevTrans server page: https://services.healthtech.dtu.dk/services/RevTrans-2.0/&lt;br /&gt;
:* On the RevTrans page: Choose the file gp120.fasta as input (or copy and paste the sequence into the sequence window)&lt;br /&gt;
:* Click the &amp;quot;Submit query&amp;quot; button&lt;br /&gt;
:* When the alignment is done you may have to click link named &amp;quot;here&amp;quot; to go to results page&lt;br /&gt;
:* Download DNA alignment, by right-clicking the link for &amp;quot;Download alignment in FASTA format&amp;quot;, and choosing &amp;quot;Save link as...&amp;quot; (save file under the name gp120align.fasta and make sure to save the file in the directory modelselect).&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Convert alignment to NEXUS format&#039;&#039;&#039;&lt;br /&gt;
: Convert the fasta file to NEXUS format and save file in the modelselect directory under the name gp120.nexus&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
== Model selection using AIC and jModelTest2 ==&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Question 1&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
: As part of the present analysis we are going to build a phylogenetic tree based on the DNA alignment constructed above. We will construct the tree using maximum likelihood, but to do that we first have to decide which substitution model we want to use. Specifically, we are interested in selecting a model that achieves a good balance between fit to the data and number of parameters. We will investigate this issue by fitting a set of 56 different models to our data and then comparing them using AIC.&lt;br /&gt;
&lt;br /&gt;
: In the first part of the exercise, however, you will only compare three closely related models by hand. The purpose of this restricted comparison is not primarily to identify the globally best model, but to understand how AIC, ΔAIC, and Akaike weights are computed and interpreted. Because only three models are included in this manual comparison, the weights you compute will describe relative support &#039;&#039;&#039;within this restricted set only&#039;&#039;&#039;.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Start jmodeltest2&#039;&#039;&#039;&lt;br /&gt;
 jmodeltest&lt;br /&gt;
&#039;&#039;&#039;Load data set&#039;&#039;&#039;&lt;br /&gt;
:* File -&amp;gt; Load DNA alignment -&amp;gt; File Format -&amp;gt; Select &amp;quot;All files&amp;quot;&lt;br /&gt;
:* Navigate to gp120.nexus and load it&lt;br /&gt;
&#039;&#039;&#039;Fit 56 models&#039;&#039;&#039;&lt;br /&gt;
:* Analysis -&amp;gt; Compute likelihood scores&lt;br /&gt;
:* Select &amp;quot;7&amp;quot; under &amp;quot;Number of substitution schemes&amp;quot;&lt;br /&gt;
:* Select &amp;quot;Fixed BIONJ-JC&amp;quot; under &amp;quot;Base tree for likelihood calculations&amp;quot;&lt;br /&gt;
:* Click &amp;quot;Compute likelihoods&amp;quot;&lt;br /&gt;
&lt;br /&gt;
: This causes jmodeltest2 to perform the following actions: first a neighbor joining tree is constructed using the Jukes and Cantor model. Then the tree is fixed and used as the basis for fitting a set of 56 different models to the data. For each model, the estimated model parameters and the negative log-likelihood are recorded. In addition to varying sets of substitution rate parameters (JC, K2P, ...), some of these models also include extra parameters that take into account the presence of different rates between sites. This is done in two ways: (1) by fitting a gamma distribution of rates (&amp;quot;+G&amp;quot;), and (2) by allowing for a proportion of constant (&amp;quot;invariable&amp;quot;) sites (&amp;quot;+I&amp;quot;).&lt;br /&gt;
&lt;br /&gt;
: Wait until jmodeltest2 is done fitting all 56 models (this will take a little while depending on your computer).&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Inspect result&#039;&#039;&#039;&lt;br /&gt;
:* Results -&amp;gt; Show results table&lt;br /&gt;
: For each model this table lists the negative log-likelihood (&amp;quot;-lnL&amp;quot;), the number of parameters (&amp;quot;p&amp;quot; - note this includes branch lengths), and estimates of all model parameters (excluding branch lengths).&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Manually compute Akaike weights for three substitution models&#039;&#039;&#039;&lt;br /&gt;
: Use AIC-based model comparison to investigate which of the following three substitution models are best supported as descriptions of how the sequences have evolved:&lt;br /&gt;
:* Jukes and Cantor with fraction of invariant sites (JC+I)&lt;br /&gt;
:* Jukes and Cantor with gamma-distributed rates over sites (JC+G)&lt;br /&gt;
:* Jukes and Cantor with invariant sites and gamma-distributed rates (JC+I+G)&lt;br /&gt;
: Before you can do the computation you need to know the log likelihood and the number of parameters for each model. Locate these values in the table for the JC+I, JC+G, and JC+I+G models, and write them down. Close the window with the result table when you are done.&lt;br /&gt;
&lt;br /&gt;
: Make sure to get the signs right: the values reported in the table are -lnL values, so you will need to reverse the sign to get the lnL (the lnL values you write down should be negative).&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Question: &#039;&#039;&#039;Use the recipe above to compute AIC, ΔAIC, and Akaike weight for the three models. Report the results in a table similar to the one shown above, and verify that the three weights sum to 1 (up to rounding error).&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Question 2&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
: Based on the Akaike weights, which of the three models has the strongest support? Is the support strongly concentrated on one model, or is there still substantial support for one or both of the others?&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Question 3&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
: What you just did manually for JC+I, JC+G and JC+I+G, jmodeltest2 can do automatically for the full set of 56 fitted models. Specifically, it uses the list of negative log likelihoods and parameter counts in the table to compute AIC values and Akaike weights for the full candidate set:&lt;br /&gt;
:* Analysis -&amp;gt; Do AIC calculations -&amp;gt;&lt;br /&gt;
:* Select &amp;quot;Write PAUP* block&amp;quot;&lt;br /&gt;
:* click &amp;quot;Do AIC calculations&amp;quot;&lt;br /&gt;
:* Results -&amp;gt; Show results table&lt;br /&gt;
:* Select &amp;quot;AIC&amp;quot; tab&lt;br /&gt;
:* &#039;&#039;&#039;IMPORTANT:&#039;&#039;&#039; SHIFT+click on the header of the &amp;quot;weight&amp;quot; column. This sorts the rows according to model weight, in descending order.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Question: &#039;&#039;&#039;What model was selected by modeltest based on the AIC values? (This will be the model with the highest weight - and lowest AIC - and will be in the first row of the results table after SHIFT-clicking the &amp;quot;weight&amp;quot; header).&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Question 4&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
: In the same AIC results table, now click (not SHIFT+click) the header of the &amp;quot;-lnL&amp;quot; column. In this table, a normal click sorts in ascending order, whereas SHIFT+click sorts in descending order. The model at the top will therefore be the one with the smallest value of &amp;quot;-lnL&amp;quot; (equivalently: the highest likelihood). Compare this model to the one with the highest Akaike weight.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Question: &#039;&#039;&#039;Is the model with the highest likelihood also the model with the highest Akaike weight? If not, explain briefly why these two rankings can differ.&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
== Construction of phylogenetic tree using PAUP ==&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Question 5&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
: Close the results table. In the main window you should now scroll up to the lines giving PAUP commands that implement the selected model. The command is enclosed between &amp;quot;BEGIN PAUP&amp;quot; and &amp;quot;END;&amp;quot; and should look something like this:&lt;br /&gt;
 Lset Base=(0.4064, [...]&lt;br /&gt;
: You will need to copy this command to a PAUP session in the next step.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Start PAUP&#039;&#039;&#039;&lt;br /&gt;
 paup&lt;br /&gt;
: Above, you used jmodeltest2 to select the most suitable substitution model for the present data set. You will now use this model to construct a maximum likelihood tree. You will use PAUP for this purpose. (Note: it is possible to create a maximum likelihood tree, or even a model-averaged tree, directly from jmodeltest2, but we will instead do it in PAUP in order to more clearly see each step that is taken.)&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Load alignment:&#039;&#039;&#039;&lt;br /&gt;
 execute gp120.nexus&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Set tree-building criterion to maximum likelihood&#039;&#039;&#039;&lt;br /&gt;
 set criterion=likelihood&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Set model parameters to winning estimates&#039;&#039;&#039;&lt;br /&gt;
: Above, you located a set of lines in the jmodeltest2 output giving a PAUP command that sets the model parameters to the estimates found for the winning model. Copy and paste this lset command (without the BEGIN and END parts) into the window where PAUP is running.&lt;br /&gt;
&lt;br /&gt;
 PASTE LSET COMMAND FROM MODELTEST RUN HERE&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Find best tree using selected model&#039;&#039;&#039;&lt;br /&gt;
: Still in the PAUP window, enter the following command:&lt;br /&gt;
 hsearch swap=tbr start=nj&lt;br /&gt;
: This command causes PAUP to perform a heuristic search for the best maximum likelihood tree. Once an initial tree has been constructed, the heuristic search proceeds by rearrangements of the &amp;quot;tree bisection and reconnection&amp;quot; type (TBR). We are using the model selected by modeltest, and the parameter estimates found by modeltest for that model. You could also choose to estimate all the model parameters as part of this step (i.e., at the same time as finding the best tree), but fixing them improves speed tremendously. Finding the best tree should take at most a few minutes.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Save best tree to file&#039;&#039;&#039;&lt;br /&gt;
 savetrees format=newick brlens=yes file=gp120tree.phy from=1 to=1&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Quit program&#039;&#039;&#039;&lt;br /&gt;
 quit&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Have a look at the tree:&#039;&#039;&#039;&lt;br /&gt;
: You have now produced an unrooted tree of the HIV sequences and saved it in the file gp120tree.phy. Note that in this exercise we will not be interested in the tree as such - our focus is instead on finding positive selection at a subset of codon positions, and the tree is just something we need in order to fit the different codon models to the data. If you want to see the tree, you can do so with the following command:&lt;br /&gt;
 figtree gp120tree.phy &amp;amp;&lt;br /&gt;
: There is no meaningful root placed on this tree, so you may want to choose the unrooted view (the third icon in the Layout section of the FigTree window). Close the FigTree window when you have had a look.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Question: &#039;&#039;&#039;Which substitution model did you use for the PAUP tree search, and what was the negative log likelihood of the best tree found under this model?&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
== Model selection and tree reconstruction in one step using IQ-TREE 3 ==&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Question 6&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
: In real-life phylogenetic analysis one would often use a modern program such as &#039;&#039;&#039;IQ-TREE 3&#039;&#039;&#039;, which can carry out model selection and maximum likelihood tree reconstruction in a single analysis.&lt;br /&gt;
&lt;br /&gt;
: Run IQ-TREE 3 on the alignment using the following command:&lt;br /&gt;
 iqtree3 -s gp120.nexus -m MFP&lt;br /&gt;
&lt;br /&gt;
: Here, &amp;quot;-m MFP&amp;quot; tells IQ-TREE 3 to perform automatic model selection using &amp;quot;ModelFinder Plus&amp;quot; before reconstructing the tree.&lt;br /&gt;
&lt;br /&gt;
: When the run has finished, inspect the output written to the screen and to the file ending in &amp;quot;.iqtree&amp;quot;. Note that IQ-TREE 3 reports several model-selection criteria in the output. The line near the top of the ModelFinder section states which criterion was used to choose the final model. Locate the substitution model selected by IQ-TREE 3 and the log likelihood of the best tree.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Question: &#039;&#039;&#039; Which substitution model was selected by IQ-TREE 3, and according to which information criterion was it selected? What was the log likelihood of the best tree? Compare this to the earlier jModelTest2 + PAUP analysis. Is the selected model exactly the same? Is the likelihood exactly the same? Briefly suggest one reason why the two analyses might not give identical results.&lt;br /&gt;
&lt;br /&gt;
---&lt;br /&gt;
&lt;br /&gt;
== Detection of positively selected sites in gp120 ==&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Question 7&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
: There is much more to phylogenetic analyses than merely reconstructing trees. One interesting result of probabilistic methods, is that the parameters of a model will have their values estimated as part of the optimization procedure. This means that once such a model has been fitted to the data, it is possible to investigate these estimated parameter values to learn features about the evolutionary history of the sequences under investigation. In the present example we will focus on investigating whether we can find positively selected sites in our data set, defined as sites where the dN/dS ratio is larger than 1. We do that by using a codon substitution model where the dN/dS ratio is one of its parameters.&lt;br /&gt;
&lt;br /&gt;
: A further strength of the probabilistic approach is that you get a probabilistic measure of how well any model fits the data. This means you can use a stringent approach to determine which model fits the data best. In this framework one uses likelihoods (probabilities of data given model) to determine which model fits the data best. As you saw above, it is for instance possible to compute AIC values and weights (model probabilities) from the likelihood values of fitted models, Since each model essentially corresponds to a hypothesis about the evolutionary history of the data, we can thus use a stringent statistical approach to decide which hypothesis best describes our data.&lt;br /&gt;
&lt;br /&gt;
: In outline, you will now use the following steps to investigate whether there is any evidence for positively selected codons in your data set:&lt;br /&gt;
&lt;br /&gt;
:* Fit model M1, which assumes there are two classes of codons in the sequence: some with dN/dS &amp;lt; 1, some with dN/dS=1.&lt;br /&gt;
:* Fit model M2, which assumes 3 distinct classes of codons: two with dN/dS ratios as for M1, and one extra class with dN/dS &amp;gt; 1.&lt;br /&gt;
:* Assess the strength of evidence for the two models using AIC-based model probabilities&lt;br /&gt;
:* If M2 is better: identify the positively selected codons&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Inspect the parameter file&#039;&#039;&#039;&lt;br /&gt;
 nedit codeml.ctl &amp;amp;&lt;br /&gt;
: The file &amp;quot;codeml.ctl&amp;quot; contains several settings that are relevant for running the program &#039;&#039;&#039;codeml&#039;&#039;&#039;. Find the following lines and ensure that the file contains these values:&lt;br /&gt;
 &#039;&#039;&#039;seqfile =  gp120align.fasta&#039;&#039;&#039;:  name of alignment file&lt;br /&gt;
 &#039;&#039;&#039;treefile =  gp120tree.phy&#039;&#039;&#039;: name of tree file&lt;br /&gt;
 &#039;&#039;&#039;seqtype = 1&#039;&#039;&#039;: tells the program that our data consists of coding DNA.&lt;br /&gt;
 &#039;&#039;&#039;NSsites = 1 2&#039;&#039;&#039; : tells the program to analyze models M1 and M2.&lt;br /&gt;
 &#039;&#039;&#039;cleandata = 0&#039;&#039;&#039;: tells the program to keep positions with gaps.&lt;br /&gt;
&lt;br /&gt;
: The settings entered by us will cause codeml to analyze two hypotheses about dN/dS ratios. M1 says there are two classes of codons with different dN/dS ratios in the sequence: one class with dN/dS &amp;lt; 1 (codons under purifying or negative selection), and one class with dN/dS=1 (no selection - neutrally evolving sites). M2 says there are 3 distinct dN/dS ratios for different sites in the sequence: one class with dN/dS &amp;lt; 1, one class with dN/dS=1 (these are the same type of classes as for M1), and one class with dN/dS &amp;gt; 1 (corresponding to sites under positive selection). The value of the dN/dS ratios (for those classes that have dN/dS &amp;lt; 1 or dN/dS &amp;gt; 1), the fraction of sites belonging to each class, and the position of sites belonging to each class, are unknown at first and will be determined during the analysis.&lt;br /&gt;
&lt;br /&gt;
: (Regarding &amp;quot;cleandata&amp;quot; setting: &amp;quot;cleandata = 1&amp;quot; would cause codeml to discard any alignment columns with gaps or ambiguity symbols. For alignments with many sequences this can lead to discarding the majority of sites and I would therefore recommend not using that setting. At the same time you do want to be wary of columns where most sequences have gaps, since inference of selection for these will be very uncertain. It might be better to discard such columns from an alignment before doing the codeml analysis).&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Start the analysis&#039;&#039;&#039;&lt;br /&gt;
 codeml&lt;br /&gt;
: This will start the codeml program using the settings in the file codeml.ctl. Depending on your computer, this will take some minutes to finish. (You may be able to see how the optimization procedure results in progressively better fits: the likelihood increases, meaning that negative log-likelihood decreases, as the fit improves).&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Inspect result file:&#039;&#039;&#039;&lt;br /&gt;
: Wait for the run to finish, and then look at the result file:&lt;br /&gt;
 nedit selection.results &amp;amp;&lt;br /&gt;
: This file contains a wealth of information concerning your analysis. You may want to turn off line-wrapping to more clearly see the structure of the file (there will be some long lines that will otherwise wrap around). The top part of the file gives an overview of your sequences, codon usage and nucleotide frequencies. You can ignore this information for now, and move on to the interesting part, namely the model likelihoods and parameter values:&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Find likelihood, and number of free parameters for model M1&#039;&#039;&#039;&lt;br /&gt;
 Search ==&amp;gt; Find... ==&amp;gt; enter &amp;quot;Model 1&amp;quot; and click Find&lt;br /&gt;
: You are now in the region of the result file where the model likelihoods and parameter estimates are noted. Now, locate a line that looks a bit like the one shown below:&lt;br /&gt;
 lnL(ntime: 72  np: 74):  -4242.470345     +0.000000&lt;br /&gt;
: Identify the number of &amp;quot;free parameters&amp;quot;, K, used in model M1: This is indicated by &amp;quot;np&amp;quot;, and is 74 in the example shown above (most of these parameters are branch lengths in the tree; specifically, the number of branch length parameters is indicated by &amp;quot;ntime&amp;quot;, and is 72 in this example). Also note the log-likelihood of the fitted model. This is the number right after the parenthesis, and is -4242.470345 in the example here.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Question: &#039;&#039;&#039;What are the values of K and lnL for model M1?&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Question 8&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Find dn/dS ratios and codon class proportions for model M1:&#039;&#039;&#039;&lt;br /&gt;
: Scroll down a few lines until you get to a small table similar to this:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
      dN/dS for site classes (K=2)&lt;br /&gt;
      p:   0.75111  0.24889&lt;br /&gt;
      w:   0.06583  1.00000&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
: This gives a summary of the dN/dS ratios that were found in the data set. The line starting &amp;quot;w:&amp;quot; lists the two dN/dS ratios that were found (in this case 0.06583 and 1.00000 - the last one was pre-specified by us as part of the model and was therefore not a free parameter). In this context &amp;quot;w&amp;quot; means &amp;quot;omega&amp;quot;, which is the symbol typically used to represent the dN/dS rate ratio. The line starting p: gives the proportion of codon sites belonging to each of the dN/dS ratio classes (in the example above approximately 75% belong to the first class , while 25% of all sites belong to the class having dN/dS=1.00000).&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Question: &#039;&#039;&#039;What are the dN/dS value (w) and proportion (p) of sites for both classes. Report the following values: p(class1), w(class1), p(class2), w(class2)&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Question 9&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Find likelihood, and K for model M2&#039;&#039;&#039;&lt;br /&gt;
: Scroll past the M1 output until you get to the results for model M2.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Question: &#039;&#039;&#039;What are the values of K and lnL for model M2?&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Question 10&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Find dn/dS ratios and codon class proportions for model M2:&#039;&#039;&#039;&lt;br /&gt;
: Now, scroll down a few lines until you get to a small table similar to the one you examined for M1 before. For this model there are 3 separate classes of codons.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Question: &#039;&#039;&#039;What are the dN/dS value (w) and proportion (p) of sites for all three classes? Report these values: p(class1), w(class1), p(class2), w(class2), p(class3), w(class3)&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Question 11&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Assess strength of evidence for models M1 and M2:&#039;&#039;&#039;&lt;br /&gt;
: M2 will always have a better (higher) log-likelihood than model M1 because M2 has more free parameters, and M1 is nested within M2. You should now use the recipe given above to compute AIC values and model probabilities for M1 and M2.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Question: &#039;&#039;&#039; Report: AIC, ΔAIC, weight (model probability) for M1 and M2&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Question 12: &#039;&#039;&#039; Is M2 better than M1?&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Question 13&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Examine list of positively selected sites&#039;&#039;&#039;&lt;br /&gt;
: If your M2 is clearly better than M1 (I firmly believe it should be if you did things according to instructions...), then you have evidence for the existence of positively selected sites in the gp120 gene. Now, scroll down to the end of the result file and locate a list similar to the one below. Note: This is the &amp;quot;Bayes Empirical Bayes&amp;quot; table, not the &amp;quot;Naive Empirical Bayes&amp;quot; table. &lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Bayes Empirical Bayes (BEB) analysis&lt;br /&gt;
Positively selected sites&lt;br /&gt;
&lt;br /&gt;
         Prob(w&amp;gt;1)     mean w&lt;br /&gt;
&lt;br /&gt;
    25 A 0.959*        3.133 +- 0.769&lt;br /&gt;
    27 P 0.906         2.990 +- 0.877&lt;br /&gt;
    56 K 0.987*        3.197 +- 0.687&lt;br /&gt;
    59 V 0.915         3.032 +- 0.873&lt;br /&gt;
    78 R 0.637         2.351 +- 1.129&lt;br /&gt;
    88 K 0.573         2.148 +- 1.077&lt;br /&gt;
    95 V 0.925         3.046 +- 0.843&lt;br /&gt;
    ...&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
: It is not important what the distinction is in this context, but very briefly NEB ignores the fact that there is uncertainty about  maximum likelihood estimates, especially for smaller data sets (for instance w for some codon is perhaps not exactly 3.046, but could be in a region around that value), while [https://pubmed.ncbi.nlm.nih.gov/15689528/ BEB accounts for that uncertainty].&lt;br /&gt;
: This gives you a list of which residues (if any) that were found to belong to the positively selected dN/dS-class. Also listed is the probability that the site really is in the codon class where dN/dS &amp;gt; 1, and a weighted average of the w at the site. Using only DNA sequences you have now identified likely epitopes on the gp120 protein.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Question: &#039;&#039;&#039;List all sites having more than 95% probability of belonging to the positively selected class&lt;/div&gt;</summary>
		<author><name>Gorm</name></author>
	</entry>
	<entry>
		<id>https://teaching.healthtech.dtu.dk/22115/index.php?title=Model_selection&amp;diff=272</id>
		<title>Model selection</title>
		<link rel="alternate" type="text/html" href="https://teaching.healthtech.dtu.dk/22115/index.php?title=Model_selection&amp;diff=272"/>
		<updated>2026-04-15T08:28:00Z</updated>

		<summary type="html">&lt;p&gt;Gorm: /* Model selection and tree reconstruction in one step using IQ-TREE 3 */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;This exercise is part of the course  [[22115_-_Computational_Molecular_Evolution|Computational Molecular Evolution (22115)]].&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Overview ==&lt;br /&gt;
&lt;br /&gt;
: In this exercise you are going to investigate features of HIV-1 evolution. You will do this by analyzing a large set of env-genes from HIV-1, subtype B. specifically, the DNA sequences analyzed here correspond to a region surrounding the hypervariable V3 region of the gp120 protein.&lt;br /&gt;
&lt;br /&gt;
: Like other retroviruses, particles of HIV are made up of 2 copies of a single-stranded RNA genome packaged inside a protein core, or capsid. The core particle also contains viral proteins that are essential for the early steps of the virus life cycle, such as reverse transcription and integration. A lipid envelope, derived from the infected cell, surrounds the core particle. Embedded in this envelope are the surface glycoproteins of HIV: gp120 and gp41. The gp120 protein is crucial for binding of the virus particle to target cells, while gp41 is important for the subsequent fusion event. It is the specific affinity of gp120 for the CD4 protein that targets HIV to those cells of the immune system that express CD4 on their surface (e.g., T-helper lymphocytes, monocytes, and macrophages).&lt;br /&gt;
&lt;br /&gt;
: The role gp120 plays in infection and the fact that it is situated on the surface of the HIV particle, means it is an obvious target for the immune response. That means that there may be a considerable selective pressure on gp120 for creating immune-escape mutants, where amino acids in the gp120 epitopes have been substituted. In this exercise you will construct a maximum likelihood tree that we will subsequently use to investigate whether you can detect such a selective pressure on parts of gp120, again using maximum likelihood methods.&lt;br /&gt;
&lt;br /&gt;
: One major goal with the exercise is to introduce you to statistically based methods for assessing the strength of evidence for a set of alternative hypotheses about some biological system of interest. The model selection method we will use is AIC (Akaike Information Criterion), based on which you will compute model probabilities. A second goal is to make you aware that phylogenetic analysis is not only about constructing trees, but that it is also a useful framework for analyzing biological questions more generally.&lt;br /&gt;
&lt;br /&gt;
: Specifically, you will&lt;br /&gt;
&lt;br /&gt;
:# perform a multiple alignment of gp120 DNA sequences taking protein-level information into account (using revtrans).&lt;br /&gt;
:# select a suitable nucleotide substitution model (using jmodeltest2)&lt;br /&gt;
:# construct a phylogenetic tree (using PAUP).&lt;br /&gt;
:# try to detect positively selected sites in gp120 (using PAML).&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
== Recipe for computing AIC values and model probabilities ==&lt;br /&gt;
&lt;br /&gt;
: Later in today&#039;s exercise you will be asked to compute AIC values and model probabilities. Return to this section and follow the instructions when you need to do so.&lt;br /&gt;
&lt;br /&gt;
:# Fit a set of models to your data, note the maximized log likelihoods (lnL) and the number of free parameters (K) for each model in the investigated set. The models you fit should represent a plausible and comprehensive set of hypotheses about your data.&lt;br /&gt;
:# Compute AIC for each of the models: &#039;&#039;&#039;AIC = -2 x lnL + 2K&#039;&#039;&#039;.  &amp;lt;br&amp;gt;For example: a model with lnL = -2010 and K = 5 will have AIC = -2 x -2010 + 2 x 5 = 4030.&lt;br /&gt;
:# Identify the model with the smallest AIC (this is the best model in the set). We will call the AIC for this model &#039;&#039;&#039;&amp;quot;AICmin&amp;quot;&#039;&#039;&#039;.&lt;br /&gt;
:# Compute the &amp;quot;ΔAIC&amp;quot; values for each model: &#039;&#039;&#039;ΔAIC = AIC - AICmin&#039;&#039;&#039; &amp;lt;br&amp;gt;For each model subtract the minimum AIC value. The best model will have a ΔAIC of zero. The rest of the models will have positive ΔAICs.&lt;br /&gt;
:# For each model compute the following quantity: &#039;&#039;&#039;numerator = exp(-0.5 x ΔAIC)&#039;&#039;&#039; &amp;lt;br&amp;gt;For example, a model with ΔAIC=4.2 will have numerator = exp(-0.5 x 4.2) = exp(-2.1) = 0.1225. Also compute the &#039;&#039;&#039;sum&#039;&#039;&#039; of the numerator values for all models.&lt;br /&gt;
:# Finally, the model probabilities (also called model weights) for each model are found as:&amp;lt;br&amp;gt; &#039;&#039;&#039;P(model) = numerator / sum&#039;&#039;&#039; &amp;lt;br&amp;gt;For example, if sum = 3.75 and a model has numerator = 1.3, then it has P(model) = 1.3 / 3.75 = 0.35&lt;br /&gt;
&lt;br /&gt;
: You may want to keep track of the computations by constructing a table along the following lines:&lt;br /&gt;
[[File:Molevol-Downloads-aictable.png|700px]]&lt;br /&gt;
&lt;br /&gt;
: Note that model probabilities can also be computed using Bayesian methods. One advantage of Bayesian methods over AIC is that instead of relying on a point estimate, uncertainty about parameter values is accounted for by integrating over all possible values (typically using MCMC).&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
== Getting started ==&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Create working directory, copy files&#039;&#039;&#039;&lt;br /&gt;
: In the command below: Instead of /path/to/molevol enter the path to the directory where you have placed your course files (for instance cd /Users/bob/Documents/molevol, or cd /home/student/molevol).&lt;br /&gt;
 cd /path/to/molevol&lt;br /&gt;
 mkdir modelselect&lt;br /&gt;
 cd modelselect&lt;br /&gt;
 cp ../data/gp120.fasta ./gp120.fasta&lt;br /&gt;
 cp ../data/codeml.ctl ./codeml.ctl&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Have a look at the DNA data file:&#039;&#039;&#039;&lt;br /&gt;
 nedit gp120.fasta &amp;amp;&lt;br /&gt;
: The file contains several DNA sequences from HIV-1, subtype B. The sequences are approximately 500 bp long, and correspond to a region surrounding the hypervariable V3 region in the gene encoding gp120. Close the nedit window when you&#039;ve had a look.&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
== Protein-guided alignment of coding DNA ==&lt;br /&gt;
&lt;br /&gt;
: There are three reasons why it is preferable to align coding DNA at the amino acid level rather than directly as nucleotides. First, the DNA alphabet has only four letters compared to 20 amino acids, which means that even unrelated DNA sequences will share roughly 25% identity by chance alone, making it much harder to distinguish true homology from noise. Second, protein alignments benefit from empirical substitution matrices such as BLOSUM-62, which capture the fact that amino acid replacements tend to be conservative. Equivalent matrices for DNA are far less informative because nucleotide substitution patterns vary strongly between genes and organisms. Third, synonymous substitutions accumulate faster than non-synonymous ones, so DNA sequences diverge more rapidly than the proteins they encode. Taken together, these factors mean that the phylogenetic signal in a DNA alignment erodes much faster than in the corresponding protein alignment.&lt;br /&gt;
&lt;br /&gt;
: However, while protein-level alignment gives us the most reliable homology signal, it discards exactly the information we need for studying selection: the synonymous substitutions. Estimating the ratio of non-synonymous to synonymous substitution rates (dN/dS) requires a codon-level alignment where analogous codon positions are properly lined up. We would therefore like to construct a multiple alignment at the DNA level, but using information at the protein level, and the RevTrans server does exactly that.&lt;br /&gt;
&lt;br /&gt;
: &#039;&#039;&#039;RevTrans&#039;&#039;&#039; takes as input an unaligned set of DNA sequences, automatically translates them to the equivalent amino acid sequences, constructs a multiple alignment of the protein sequences, and finally uses the protein alignment as a template for constructing a multiple DNA alignment. Because each amino acid maps back to its original codon, gaps are always inserted in groups of three and codon boundaries are preserved throughout the alignment. This is essential for the dN/dS analyses we will carry out in this exercise: if codon positions are misaligned, synonymous and non-synonymous changes cannot be correctly distinguished.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Construct RevTrans alignment&#039;&#039;&#039;&lt;br /&gt;
:* Open RevTrans server page: https://services.healthtech.dtu.dk/services/RevTrans-2.0/&lt;br /&gt;
:* On the RevTrans page: Choose the file gp120.fasta as input (or copy and paste the sequence into the sequence window)&lt;br /&gt;
:* Click the &amp;quot;Submit query&amp;quot; button&lt;br /&gt;
:* When the alignment is done you may have to click link named &amp;quot;here&amp;quot; to go to results page&lt;br /&gt;
:* Download DNA alignment, by right-clicking the link for &amp;quot;Download alignment in FASTA format&amp;quot;, and choosing &amp;quot;Save link as...&amp;quot; (save file under the name gp120align.fasta and make sure to save the file in the directory modelselect).&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Convert alignment to NEXUS format&#039;&#039;&#039;&lt;br /&gt;
: Convert the fasta file to NEXUS format and save file in the modelselect directory under the name gp120.nexus&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
== Model selection using AIC and jModelTest2 ==&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Question 1&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
: As part of the present analysis we are going to build a phylogenetic tree based on the DNA alignment constructed above. We will construct the tree using maximum likelihood, but to do that we first have to decide which substitution model we want to use. Specifically, we are interested in selecting a model that achieves a good balance between fit to the data and number of parameters. We will investigate this issue by fitting a set of 56 different models to our data and then comparing them using AIC.&lt;br /&gt;
&lt;br /&gt;
: In the first part of the exercise, however, you will only compare three closely related models by hand. The purpose of this restricted comparison is not primarily to identify the globally best model, but to understand how AIC, ΔAIC, and Akaike weights are computed and interpreted. Because only three models are included in this manual comparison, the weights you compute will describe relative support &#039;&#039;&#039;within this restricted set only&#039;&#039;&#039;.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Start jmodeltest2&#039;&#039;&#039;&lt;br /&gt;
 jmodeltest&lt;br /&gt;
&#039;&#039;&#039;Load data set&#039;&#039;&#039;&lt;br /&gt;
:* File -&amp;gt; Load DNA alignment -&amp;gt; File Format -&amp;gt; Select &amp;quot;All files&amp;quot;&lt;br /&gt;
:* Navigate to gp120.nexus and load it&lt;br /&gt;
&#039;&#039;&#039;Fit 56 models&#039;&#039;&#039;&lt;br /&gt;
:* Analysis -&amp;gt; Compute likelihood scores&lt;br /&gt;
:* Select &amp;quot;7&amp;quot; under &amp;quot;Number of substitution schemes&amp;quot;&lt;br /&gt;
:* Select &amp;quot;Fixed BIONJ-JC&amp;quot; under &amp;quot;Base tree for likelihood calculations&amp;quot;&lt;br /&gt;
:* Click &amp;quot;Compute likelihoods&amp;quot;&lt;br /&gt;
&lt;br /&gt;
: This causes jmodeltest2 to perform the following actions: first a neighbor joining tree is constructed using the Jukes and Cantor model. Then the tree is fixed and used as the basis for fitting a set of 56 different models to the data. For each model, the estimated model parameters and the negative log-likelihood are recorded. In addition to varying sets of substitution rate parameters (JC, K2P, ...), some of these models also include extra parameters that take into account the presence of different rates between sites. This is done in two ways: (1) by fitting a gamma distribution of rates (&amp;quot;+G&amp;quot;), and (2) by allowing for a proportion of constant (&amp;quot;invariable&amp;quot;) sites (&amp;quot;+I&amp;quot;).&lt;br /&gt;
&lt;br /&gt;
: Wait until jmodeltest2 is done fitting all 56 models (this will take a little while depending on your computer).&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Inspect result&#039;&#039;&#039;&lt;br /&gt;
:* Results -&amp;gt; Show results table&lt;br /&gt;
: For each model this table lists the negative log-likelihood (&amp;quot;-lnL&amp;quot;), the number of parameters (&amp;quot;p&amp;quot; - note this includes branch lengths), and estimates of all model parameters (excluding branch lengths).&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Manually compute Akaike weights for three substitution models&#039;&#039;&#039;&lt;br /&gt;
: Use AIC-based model comparison to investigate which of the following three substitution models are best supported as descriptions of how the sequences have evolved:&lt;br /&gt;
:* Jukes and Cantor with fraction of invariant sites (JC+I)&lt;br /&gt;
:* Jukes and Cantor with gamma-distributed rates over sites (JC+G)&lt;br /&gt;
:* Jukes and Cantor with invariant sites and gamma-distributed rates (JC+I+G)&lt;br /&gt;
: Before you can do the computation you need to know the log likelihood and the number of parameters for each model. Locate these values in the table for the JC+I, JC+G, and JC+I+G models, and write them down. Close the window with the result table when you are done.&lt;br /&gt;
&lt;br /&gt;
: Make sure to get the signs right: the values reported in the table are -lnL values, so you will need to reverse the sign to get the lnL (the lnL values you write down should be negative).&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Question: &#039;&#039;&#039;Use the recipe above to compute AIC, ΔAIC, and Akaike weight for the three models. Report the results in a table similar to the one shown above, and verify that the three weights sum to 1 (up to rounding error).&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Question 2&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
: Based on the Akaike weights, which of the three models has the strongest support? Is the support strongly concentrated on one model, or is there still substantial support for one or both of the others?&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Question 3&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
: What you just did manually for JC+I, JC+G and JC+I+G, jmodeltest2 can do automatically for the full set of 56 fitted models. Specifically, it uses the list of negative log likelihoods and parameter counts in the table to compute AIC values and Akaike weights for the full candidate set:&lt;br /&gt;
:* Analysis -&amp;gt; Do AIC calculations -&amp;gt;&lt;br /&gt;
:* Select &amp;quot;Write PAUP* block&amp;quot;&lt;br /&gt;
:* click &amp;quot;Do AIC calculations&amp;quot;&lt;br /&gt;
:* Results -&amp;gt; Show results table&lt;br /&gt;
:* Select &amp;quot;AIC&amp;quot; tab&lt;br /&gt;
:* &#039;&#039;&#039;IMPORTANT:&#039;&#039;&#039; SHIFT+click on the header of the &amp;quot;weight&amp;quot; column. This sorts the rows according to model weight, in descending order.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Question: &#039;&#039;&#039;What model was selected by modeltest based on the AIC values? (This will be the model with the highest weight - and lowest AIC - and will be in the first row of the results table after SHIFT-clicking the &amp;quot;weight&amp;quot; header).&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Question 4&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
: In the same AIC results table, now click (not SHIFT+click) the header of the &amp;quot;-lnL&amp;quot; column. In this table, a normal click sorts in ascending order, whereas SHIFT+click sorts in descending order. The model at the top will therefore be the one with the smallest value of &amp;quot;-lnL&amp;quot; (equivalently: the highest likelihood). Compare this model to the one with the highest Akaike weight.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Question: &#039;&#039;&#039;Is the model with the highest likelihood also the model with the highest Akaike weight? If not, explain briefly why these two rankings can differ.&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
== Construction of phylogenetic tree using PAUP ==&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Question 5&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
: Close the results table. In the main window you should now scroll up to the lines giving PAUP commands that implement the selected model. The command is enclosed between &amp;quot;BEGIN PAUP&amp;quot; and &amp;quot;END;&amp;quot; and should look something like this:&lt;br /&gt;
 Lset Base=(0.4064, [...]&lt;br /&gt;
: You will need to copy this command to a PAUP session in the next step.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Start PAUP&#039;&#039;&#039;&lt;br /&gt;
 paup&lt;br /&gt;
: Above, you used jmodeltest2 to select the most suitable substitution model for the present data set. You will now use this model to construct a maximum likelihood tree. You will use PAUP for this purpose. (Note: it is possible to create a maximum likelihood tree, or even a model-averaged tree, directly from jmodeltest2, but we will instead do it in PAUP in order to more clearly see each step that is taken.)&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Load alignment:&#039;&#039;&#039;&lt;br /&gt;
 execute gp120.nexus&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Set tree-building criterion to maximum likelihood&#039;&#039;&#039;&lt;br /&gt;
 set criterion=likelihood&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Set model parameters to winning estimates&#039;&#039;&#039;&lt;br /&gt;
: Above, you located a set of lines in the jmodeltest2 output giving a PAUP command that sets the model parameters to the estimates found for the winning model. Copy and paste this lset command (without the BEGIN and END parts) into the window where PAUP is running.&lt;br /&gt;
&lt;br /&gt;
 PASTE LSET COMMAND FROM MODELTEST RUN HERE&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Find best tree using selected model&#039;&#039;&#039;&lt;br /&gt;
: Still in the PAUP window, enter the following command:&lt;br /&gt;
 hsearch swap=tbr start=nj&lt;br /&gt;
: This command causes PAUP to perform a heuristic search for the best maximum likelihood tree. Once an initial tree has been constructed, the heuristic search proceeds by rearrangements of the &amp;quot;tree bisection and reconnection&amp;quot; type (TBR). We are using the model selected by modeltest, and the parameter estimates found by modeltest for that model. You could also choose to estimate all the model parameters as part of this step (i.e., at the same time as finding the best tree), but fixing them improves speed tremendously. Finding the best tree should take at most a few minutes.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Save best tree to file&#039;&#039;&#039;&lt;br /&gt;
 savetrees format=newick brlens=yes file=gp120tree.phy from=1 to=1&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Quit program&#039;&#039;&#039;&lt;br /&gt;
 quit&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Have a look at the tree:&#039;&#039;&#039;&lt;br /&gt;
: You have now produced an unrooted tree of the HIV sequences and saved it in the file gp120tree.phy. Note that in this exercise we will not be interested in the tree as such - our focus is instead on finding positive selection at a subset of codon positions, and the tree is just something we need in order to fit the different codon models to the data. If you want to see the tree, you can do so with the following command:&lt;br /&gt;
 figtree gp120tree.phy &amp;amp;&lt;br /&gt;
: There is no meaningful root placed on this tree, so you may want to choose the unrooted view (the third icon in the Layout section of the FigTree window). Close the FigTree window when you have had a look.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Question: &#039;&#039;&#039;Which substitution model did you use for the PAUP tree search, and what was the negative log likelihood of the best tree found under this model?&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
== Model selection and tree reconstruction in one step using IQ-TREE 3 ==&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Question 6&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
: In real-life phylogenetic analysis one would often use a modern program such as &#039;&#039;&#039;IQ-TREE 3&#039;&#039;&#039;, which can carry out model selection and maximum likelihood tree reconstruction in a single analysis.&lt;br /&gt;
&lt;br /&gt;
: Run IQ-TREE 3 on the alignment using the following command:&lt;br /&gt;
 iqtree3 -s gp120.nexus -m MFP&lt;br /&gt;
&lt;br /&gt;
: Here, &amp;quot;-m MFP&amp;quot; tells IQ-TREE 3 to perform automatic model selection using &amp;quot;ModelFinder Plus&amp;quot; before reconstructing the tree.&lt;br /&gt;
&lt;br /&gt;
: When the run has finished, inspect the output written to the screen and to the file ending in &amp;quot;.iqtree&amp;quot;. : Note that IQ-TREE 3 reports several model-selection criteria in the output. The line near the top of the ModelFinder section states which criterion was used to choose the final model. Locate the substitution model selected by IQ-TREE 3 and the log likelihood of the best tree.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Question: &#039;&#039;&#039; Which substitution model was selected by IQ-TREE 3, and according to which information criterion was it selected? What was the log likelihood of the best tree? Compare this to the earlier jModelTest2 + PAUP analysis. Is the selected model exactly the same? Is the likelihood exactly the same? Briefly suggest one reason why the two analyses might not give identical results.&lt;br /&gt;
&lt;br /&gt;
---&lt;br /&gt;
&lt;br /&gt;
== Detection of positively selected sites in gp120 ==&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Question 7&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
: There is much more to phylogenetic analyses than merely reconstructing trees. One interesting result of probabilistic methods, is that the parameters of a model will have their values estimated as part of the optimization procedure. This means that once such a model has been fitted to the data, it is possible to investigate these estimated parameter values to learn features about the evolutionary history of the sequences under investigation. In the present example we will focus on investigating whether we can find positively selected sites in our data set, defined as sites where the dN/dS ratio is larger than 1. We do that by using a codon substitution model where the dN/dS ratio is one of its parameters.&lt;br /&gt;
&lt;br /&gt;
: A further strength of the probabilistic approach is that you get a probabilistic measure of how well any model fits the data. This means you can use a stringent approach to determine which model fits the data best. In this framework one uses likelihoods (probabilities of data given model) to determine which model fits the data best. As you saw above, it is for instance possible to compute AIC values and weights (model probabilities) from the likelihood values of fitted models, Since each model essentially corresponds to a hypothesis about the evolutionary history of the data, we can thus use a stringent statistical approach to decide which hypothesis best describes our data.&lt;br /&gt;
&lt;br /&gt;
: In outline, you will now use the following steps to investigate whether there is any evidence for positively selected codons in your data set:&lt;br /&gt;
&lt;br /&gt;
:* Fit model M1, which assumes there are two classes of codons in the sequence: some with dN/dS &amp;lt; 1, some with dN/dS=1.&lt;br /&gt;
:* Fit model M2, which assumes 3 distinct classes of codons: two with dN/dS ratios as for M1, and one extra class with dN/dS &amp;gt; 1.&lt;br /&gt;
:* Assess the strength of evidence for the two models using AIC-based model probabilities&lt;br /&gt;
:* If M2 is better: identify the positively selected codons&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Inspect the parameter file&#039;&#039;&#039;&lt;br /&gt;
 nedit codeml.ctl &amp;amp;&lt;br /&gt;
: The file &amp;quot;codeml.ctl&amp;quot; contains several settings that are relevant for running the program &#039;&#039;&#039;codeml&#039;&#039;&#039;. Find the following lines and ensure that the file contains these values:&lt;br /&gt;
 &#039;&#039;&#039;seqfile =  gp120align.fasta&#039;&#039;&#039;:  name of alignment file&lt;br /&gt;
 &#039;&#039;&#039;treefile =  gp120tree.phy&#039;&#039;&#039;: name of tree file&lt;br /&gt;
 &#039;&#039;&#039;seqtype = 1&#039;&#039;&#039;: tells the program that our data consists of coding DNA.&lt;br /&gt;
 &#039;&#039;&#039;NSsites = 1 2&#039;&#039;&#039; : tells the program to analyze models M1 and M2.&lt;br /&gt;
 &#039;&#039;&#039;cleandata = 0&#039;&#039;&#039;: tells the program to keep positions with gaps.&lt;br /&gt;
&lt;br /&gt;
: The settings entered by us will cause codeml to analyze two hypotheses about dN/dS ratios. M1 says there are two classes of codons with different dN/dS ratios in the sequence: one class with dN/dS &amp;lt; 1 (codons under purifying or negative selection), and one class with dN/dS=1 (no selection - neutrally evolving sites). M2 says there are 3 distinct dN/dS ratios for different sites in the sequence: one class with dN/dS &amp;lt; 1, one class with dN/dS=1 (these are the same type of classes as for M1), and one class with dN/dS &amp;gt; 1 (corresponding to sites under positive selection). The value of the dN/dS ratios (for those classes that have dN/dS &amp;lt; 1 or dN/dS &amp;gt; 1), the fraction of sites belonging to each class, and the position of sites belonging to each class, are unknown at first and will be determined during the analysis.&lt;br /&gt;
&lt;br /&gt;
: (Regarding &amp;quot;cleandata&amp;quot; setting: &amp;quot;cleandata = 1&amp;quot; would cause codeml to discard any alignment columns with gaps or ambiguity symbols. For alignments with many sequences this can lead to discarding the majority of sites and I would therefore recommend not using that setting. At the same time you do want to be wary of columns where most sequences have gaps, since inference of selection for these will be very uncertain. It might be better to discard such columns from an alignment before doing the codeml analysis).&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Start the analysis&#039;&#039;&#039;&lt;br /&gt;
 codeml&lt;br /&gt;
: This will start the codeml program using the settings in the file codeml.ctl. Depending on your computer, this will take some minutes to finish. (You may be able to see how the optimization procedure results in progressively better fits: the likelihood increases, meaning that negative log-likelihood decreases, as the fit improves).&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Inspect result file:&#039;&#039;&#039;&lt;br /&gt;
: Wait for the run to finish, and then look at the result file:&lt;br /&gt;
 nedit selection.results &amp;amp;&lt;br /&gt;
: This file contains a wealth of information concerning your analysis. You may want to turn off line-wrapping to more clearly see the structure of the file (there will be some long lines that will otherwise wrap around). The top part of the file gives an overview of your sequences, codon usage and nucleotide frequencies. You can ignore this information for now, and move on to the interesting part, namely the model likelihoods and parameter values:&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Find likelihood, and number of free parameters for model M1&#039;&#039;&#039;&lt;br /&gt;
 Search ==&amp;gt; Find... ==&amp;gt; enter &amp;quot;Model 1&amp;quot; and click Find&lt;br /&gt;
: You are now in the region of the result file where the model likelihoods and parameter estimates are noted. Now, locate a line that looks a bit like the one shown below:&lt;br /&gt;
 lnL(ntime: 72  np: 74):  -4242.470345     +0.000000&lt;br /&gt;
: Identify the number of &amp;quot;free parameters&amp;quot;, K, used in model M1: This is indicated by &amp;quot;np&amp;quot;, and is 74 in the example shown above (most of these parameters are branch lengths in the tree; specifically, the number of branch length parameters is indicated by &amp;quot;ntime&amp;quot;, and is 72 in this example). Also note the log-likelihood of the fitted model. This is the number right after the parenthesis, and is -4242.470345 in the example here.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Question: &#039;&#039;&#039;What are the values of K and lnL for model M1?&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Question 8&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Find dn/dS ratios and codon class proportions for model M1:&#039;&#039;&#039;&lt;br /&gt;
: Scroll down a few lines until you get to a small table similar to this:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
      dN/dS for site classes (K=2)&lt;br /&gt;
      p:   0.75111  0.24889&lt;br /&gt;
      w:   0.06583  1.00000&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
: This gives a summary of the dN/dS ratios that were found in the data set. The line starting &amp;quot;w:&amp;quot; lists the two dN/dS ratios that were found (in this case 0.06583 and 1.00000 - the last one was pre-specified by us as part of the model and was therefore not a free parameter). In this context &amp;quot;w&amp;quot; means &amp;quot;omega&amp;quot;, which is the symbol typically used to represent the dN/dS rate ratio. The line starting p: gives the proportion of codon sites belonging to each of the dN/dS ratio classes (in the example above approximately 75% belong to the first class , while 25% of all sites belong to the class having dN/dS=1.00000).&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Question: &#039;&#039;&#039;What are the dN/dS value (w) and proportion (p) of sites for both classes. Report the following values: p(class1), w(class1), p(class2), w(class2)&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Question 9&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Find likelihood, and K for model M2&#039;&#039;&#039;&lt;br /&gt;
: Scroll past the M1 output until you get to the results for model M2.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Question: &#039;&#039;&#039;What are the values of K and lnL for model M2?&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Question 10&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Find dn/dS ratios and codon class proportions for model M2:&#039;&#039;&#039;&lt;br /&gt;
: Now, scroll down a few lines until you get to a small table similar to the one you examined for M1 before. For this model there are 3 separate classes of codons.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Question: &#039;&#039;&#039;What are the dN/dS value (w) and proportion (p) of sites for all three classes? Report these values: p(class1), w(class1), p(class2), w(class2), p(class3), w(class3)&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Question 11&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Assess strength of evidence for models M1 and M2:&#039;&#039;&#039;&lt;br /&gt;
: M2 will always have a better (higher) log-likelihood than model M1 because M2 has more free parameters, and M1 is nested within M2. You should now use the recipe given above to compute AIC values and model probabilities for M1 and M2.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Question: &#039;&#039;&#039; Report: AIC, ΔAIC, weight (model probability) for M1 and M2&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Question 12: &#039;&#039;&#039; Is M2 better than M1?&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Question 13&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Examine list of positively selected sites&#039;&#039;&#039;&lt;br /&gt;
: If your M2 is clearly better than M1 (I firmly believe it should be if you did things according to instructions...), then you have evidence for the existence of positively selected sites in the gp120 gene. Now, scroll down to the end of the result file and locate a list similar to the one below. Note: This is the &amp;quot;Bayes Empirical Bayes&amp;quot; table, not the &amp;quot;Naive Empirical Bayes&amp;quot; table. &lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Bayes Empirical Bayes (BEB) analysis&lt;br /&gt;
Positively selected sites&lt;br /&gt;
&lt;br /&gt;
         Prob(w&amp;gt;1)     mean w&lt;br /&gt;
&lt;br /&gt;
    25 A 0.959*        3.133 +- 0.769&lt;br /&gt;
    27 P 0.906         2.990 +- 0.877&lt;br /&gt;
    56 K 0.987*        3.197 +- 0.687&lt;br /&gt;
    59 V 0.915         3.032 +- 0.873&lt;br /&gt;
    78 R 0.637         2.351 +- 1.129&lt;br /&gt;
    88 K 0.573         2.148 +- 1.077&lt;br /&gt;
    95 V 0.925         3.046 +- 0.843&lt;br /&gt;
    ...&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
: It is not important what the distinction is in this context, but very briefly NEB ignores the fact that there is uncertainty about  maximum likelihood estimates, especially for smaller data sets (for instance w for some codon is perhaps not exactly 3.046, but could be in a region around that value), while [https://pubmed.ncbi.nlm.nih.gov/15689528/ BEB accounts for that uncertainty].&lt;br /&gt;
: This gives you a list of which residues (if any) that were found to belong to the positively selected dN/dS-class. Also listed is the probability that the site really is in the codon class where dN/dS &amp;gt; 1, and a weighted average of the w at the site. Using only DNA sequences you have now identified likely epitopes on the gp120 protein.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Question: &#039;&#039;&#039;List all sites having more than 95% probability of belonging to the positively selected class&lt;/div&gt;</summary>
		<author><name>Gorm</name></author>
	</entry>
	<entry>
		<id>https://teaching.healthtech.dtu.dk/22115/index.php?title=Model_selection&amp;diff=271</id>
		<title>Model selection</title>
		<link rel="alternate" type="text/html" href="https://teaching.healthtech.dtu.dk/22115/index.php?title=Model_selection&amp;diff=271"/>
		<updated>2026-04-15T08:27:29Z</updated>

		<summary type="html">&lt;p&gt;Gorm: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;This exercise is part of the course  [[22115_-_Computational_Molecular_Evolution|Computational Molecular Evolution (22115)]].&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Overview ==&lt;br /&gt;
&lt;br /&gt;
: In this exercise you are going to investigate features of HIV-1 evolution. You will do this by analyzing a large set of env-genes from HIV-1, subtype B. specifically, the DNA sequences analyzed here correspond to a region surrounding the hypervariable V3 region of the gp120 protein.&lt;br /&gt;
&lt;br /&gt;
: Like other retroviruses, particles of HIV are made up of 2 copies of a single-stranded RNA genome packaged inside a protein core, or capsid. The core particle also contains viral proteins that are essential for the early steps of the virus life cycle, such as reverse transcription and integration. A lipid envelope, derived from the infected cell, surrounds the core particle. Embedded in this envelope are the surface glycoproteins of HIV: gp120 and gp41. The gp120 protein is crucial for binding of the virus particle to target cells, while gp41 is important for the subsequent fusion event. It is the specific affinity of gp120 for the CD4 protein that targets HIV to those cells of the immune system that express CD4 on their surface (e.g., T-helper lymphocytes, monocytes, and macrophages).&lt;br /&gt;
&lt;br /&gt;
: The role gp120 plays in infection and the fact that it is situated on the surface of the HIV particle, means it is an obvious target for the immune response. That means that there may be a considerable selective pressure on gp120 for creating immune-escape mutants, where amino acids in the gp120 epitopes have been substituted. In this exercise you will construct a maximum likelihood tree that we will subsequently use to investigate whether you can detect such a selective pressure on parts of gp120, again using maximum likelihood methods.&lt;br /&gt;
&lt;br /&gt;
: One major goal with the exercise is to introduce you to statistically based methods for assessing the strength of evidence for a set of alternative hypotheses about some biological system of interest. The model selection method we will use is AIC (Akaike Information Criterion), based on which you will compute model probabilities. A second goal is to make you aware that phylogenetic analysis is not only about constructing trees, but that it is also a useful framework for analyzing biological questions more generally.&lt;br /&gt;
&lt;br /&gt;
: Specifically, you will&lt;br /&gt;
&lt;br /&gt;
:# perform a multiple alignment of gp120 DNA sequences taking protein-level information into account (using revtrans).&lt;br /&gt;
:# select a suitable nucleotide substitution model (using jmodeltest2)&lt;br /&gt;
:# construct a phylogenetic tree (using PAUP).&lt;br /&gt;
:# try to detect positively selected sites in gp120 (using PAML).&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
== Recipe for computing AIC values and model probabilities ==&lt;br /&gt;
&lt;br /&gt;
: Later in today&#039;s exercise you will be asked to compute AIC values and model probabilities. Return to this section and follow the instructions when you need to do so.&lt;br /&gt;
&lt;br /&gt;
:# Fit a set of models to your data, note the maximized log likelihoods (lnL) and the number of free parameters (K) for each model in the investigated set. The models you fit should represent a plausible and comprehensive set of hypotheses about your data.&lt;br /&gt;
:# Compute AIC for each of the models: &#039;&#039;&#039;AIC = -2 x lnL + 2K&#039;&#039;&#039;.  &amp;lt;br&amp;gt;For example: a model with lnL = -2010 and K = 5 will have AIC = -2 x -2010 + 2 x 5 = 4030.&lt;br /&gt;
:# Identify the model with the smallest AIC (this is the best model in the set). We will call the AIC for this model &#039;&#039;&#039;&amp;quot;AICmin&amp;quot;&#039;&#039;&#039;.&lt;br /&gt;
:# Compute the &amp;quot;ΔAIC&amp;quot; values for each model: &#039;&#039;&#039;ΔAIC = AIC - AICmin&#039;&#039;&#039; &amp;lt;br&amp;gt;For each model subtract the minimum AIC value. The best model will have a ΔAIC of zero. The rest of the models will have positive ΔAICs.&lt;br /&gt;
:# For each model compute the following quantity: &#039;&#039;&#039;numerator = exp(-0.5 x ΔAIC)&#039;&#039;&#039; &amp;lt;br&amp;gt;For example, a model with ΔAIC=4.2 will have numerator = exp(-0.5 x 4.2) = exp(-2.1) = 0.1225. Also compute the &#039;&#039;&#039;sum&#039;&#039;&#039; of the numerator values for all models.&lt;br /&gt;
:# Finally, the model probabilities (also called model weights) for each model are found as:&amp;lt;br&amp;gt; &#039;&#039;&#039;P(model) = numerator / sum&#039;&#039;&#039; &amp;lt;br&amp;gt;For example, if sum = 3.75 and a model has numerator = 1.3, then it has P(model) = 1.3 / 3.75 = 0.35&lt;br /&gt;
&lt;br /&gt;
: You may want to keep track of the computations by constructing a table along the following lines:&lt;br /&gt;
[[File:Molevol-Downloads-aictable.png|700px]]&lt;br /&gt;
&lt;br /&gt;
: Note that model probabilities can also be computed using Bayesian methods. One advantage of Bayesian methods over AIC is that instead of relying on a point estimate, uncertainty about parameter values is accounted for by integrating over all possible values (typically using MCMC).&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
== Getting started ==&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Create working directory, copy files&#039;&#039;&#039;&lt;br /&gt;
: In the command below: Instead of /path/to/molevol enter the path to the directory where you have placed your course files (for instance cd /Users/bob/Documents/molevol, or cd /home/student/molevol).&lt;br /&gt;
 cd /path/to/molevol&lt;br /&gt;
 mkdir modelselect&lt;br /&gt;
 cd modelselect&lt;br /&gt;
 cp ../data/gp120.fasta ./gp120.fasta&lt;br /&gt;
 cp ../data/codeml.ctl ./codeml.ctl&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Have a look at the DNA data file:&#039;&#039;&#039;&lt;br /&gt;
 nedit gp120.fasta &amp;amp;&lt;br /&gt;
: The file contains several DNA sequences from HIV-1, subtype B. The sequences are approximately 500 bp long, and correspond to a region surrounding the hypervariable V3 region in the gene encoding gp120. Close the nedit window when you&#039;ve had a look.&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
== Protein-guided alignment of coding DNA ==&lt;br /&gt;
&lt;br /&gt;
: There are three reasons why it is preferable to align coding DNA at the amino acid level rather than directly as nucleotides. First, the DNA alphabet has only four letters compared to 20 amino acids, which means that even unrelated DNA sequences will share roughly 25% identity by chance alone, making it much harder to distinguish true homology from noise. Second, protein alignments benefit from empirical substitution matrices such as BLOSUM-62, which capture the fact that amino acid replacements tend to be conservative. Equivalent matrices for DNA are far less informative because nucleotide substitution patterns vary strongly between genes and organisms. Third, synonymous substitutions accumulate faster than non-synonymous ones, so DNA sequences diverge more rapidly than the proteins they encode. Taken together, these factors mean that the phylogenetic signal in a DNA alignment erodes much faster than in the corresponding protein alignment.&lt;br /&gt;
&lt;br /&gt;
: However, while protein-level alignment gives us the most reliable homology signal, it discards exactly the information we need for studying selection: the synonymous substitutions. Estimating the ratio of non-synonymous to synonymous substitution rates (dN/dS) requires a codon-level alignment where analogous codon positions are properly lined up. We would therefore like to construct a multiple alignment at the DNA level, but using information at the protein level, and the RevTrans server does exactly that.&lt;br /&gt;
&lt;br /&gt;
: &#039;&#039;&#039;RevTrans&#039;&#039;&#039; takes as input an unaligned set of DNA sequences, automatically translates them to the equivalent amino acid sequences, constructs a multiple alignment of the protein sequences, and finally uses the protein alignment as a template for constructing a multiple DNA alignment. Because each amino acid maps back to its original codon, gaps are always inserted in groups of three and codon boundaries are preserved throughout the alignment. This is essential for the dN/dS analyses we will carry out in this exercise: if codon positions are misaligned, synonymous and non-synonymous changes cannot be correctly distinguished.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Construct RevTrans alignment&#039;&#039;&#039;&lt;br /&gt;
:* Open RevTrans server page: https://services.healthtech.dtu.dk/services/RevTrans-2.0/&lt;br /&gt;
:* On the RevTrans page: Choose the file gp120.fasta as input (or copy and paste the sequence into the sequence window)&lt;br /&gt;
:* Click the &amp;quot;Submit query&amp;quot; button&lt;br /&gt;
:* When the alignment is done you may have to click link named &amp;quot;here&amp;quot; to go to results page&lt;br /&gt;
:* Download DNA alignment, by right-clicking the link for &amp;quot;Download alignment in FASTA format&amp;quot;, and choosing &amp;quot;Save link as...&amp;quot; (save file under the name gp120align.fasta and make sure to save the file in the directory modelselect).&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Convert alignment to NEXUS format&#039;&#039;&#039;&lt;br /&gt;
: Convert the fasta file to NEXUS format and save file in the modelselect directory under the name gp120.nexus&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
== Model selection using AIC and jModelTest2 ==&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Question 1&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
: As part of the present analysis we are going to build a phylogenetic tree based on the DNA alignment constructed above. We will construct the tree using maximum likelihood, but to do that we first have to decide which substitution model we want to use. Specifically, we are interested in selecting a model that achieves a good balance between fit to the data and number of parameters. We will investigate this issue by fitting a set of 56 different models to our data and then comparing them using AIC.&lt;br /&gt;
&lt;br /&gt;
: In the first part of the exercise, however, you will only compare three closely related models by hand. The purpose of this restricted comparison is not primarily to identify the globally best model, but to understand how AIC, ΔAIC, and Akaike weights are computed and interpreted. Because only three models are included in this manual comparison, the weights you compute will describe relative support &#039;&#039;&#039;within this restricted set only&#039;&#039;&#039;.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Start jmodeltest2&#039;&#039;&#039;&lt;br /&gt;
 jmodeltest&lt;br /&gt;
&#039;&#039;&#039;Load data set&#039;&#039;&#039;&lt;br /&gt;
:* File -&amp;gt; Load DNA alignment -&amp;gt; File Format -&amp;gt; Select &amp;quot;All files&amp;quot;&lt;br /&gt;
:* Navigate to gp120.nexus and load it&lt;br /&gt;
&#039;&#039;&#039;Fit 56 models&#039;&#039;&#039;&lt;br /&gt;
:* Analysis -&amp;gt; Compute likelihood scores&lt;br /&gt;
:* Select &amp;quot;7&amp;quot; under &amp;quot;Number of substitution schemes&amp;quot;&lt;br /&gt;
:* Select &amp;quot;Fixed BIONJ-JC&amp;quot; under &amp;quot;Base tree for likelihood calculations&amp;quot;&lt;br /&gt;
:* Click &amp;quot;Compute likelihoods&amp;quot;&lt;br /&gt;
&lt;br /&gt;
: This causes jmodeltest2 to perform the following actions: first a neighbor joining tree is constructed using the Jukes and Cantor model. Then the tree is fixed and used as the basis for fitting a set of 56 different models to the data. For each model, the estimated model parameters and the negative log-likelihood are recorded. In addition to varying sets of substitution rate parameters (JC, K2P, ...), some of these models also include extra parameters that take into account the presence of different rates between sites. This is done in two ways: (1) by fitting a gamma distribution of rates (&amp;quot;+G&amp;quot;), and (2) by allowing for a proportion of constant (&amp;quot;invariable&amp;quot;) sites (&amp;quot;+I&amp;quot;).&lt;br /&gt;
&lt;br /&gt;
: Wait until jmodeltest2 is done fitting all 56 models (this will take a little while depending on your computer).&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Inspect result&#039;&#039;&#039;&lt;br /&gt;
:* Results -&amp;gt; Show results table&lt;br /&gt;
: For each model this table lists the negative log-likelihood (&amp;quot;-lnL&amp;quot;), the number of parameters (&amp;quot;p&amp;quot; - note this includes branch lengths), and estimates of all model parameters (excluding branch lengths).&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Manually compute Akaike weights for three substitution models&#039;&#039;&#039;&lt;br /&gt;
: Use AIC-based model comparison to investigate which of the following three substitution models are best supported as descriptions of how the sequences have evolved:&lt;br /&gt;
:* Jukes and Cantor with fraction of invariant sites (JC+I)&lt;br /&gt;
:* Jukes and Cantor with gamma-distributed rates over sites (JC+G)&lt;br /&gt;
:* Jukes and Cantor with invariant sites and gamma-distributed rates (JC+I+G)&lt;br /&gt;
: Before you can do the computation you need to know the log likelihood and the number of parameters for each model. Locate these values in the table for the JC+I, JC+G, and JC+I+G models, and write them down. Close the window with the result table when you are done.&lt;br /&gt;
&lt;br /&gt;
: Make sure to get the signs right: the values reported in the table are -lnL values, so you will need to reverse the sign to get the lnL (the lnL values you write down should be negative).&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Question: &#039;&#039;&#039;Use the recipe above to compute AIC, ΔAIC, and Akaike weight for the three models. Report the results in a table similar to the one shown above, and verify that the three weights sum to 1 (up to rounding error).&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Question 2&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
: Based on the Akaike weights, which of the three models has the strongest support? Is the support strongly concentrated on one model, or is there still substantial support for one or both of the others?&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Question 3&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
: What you just did manually for JC+I, JC+G and JC+I+G, jmodeltest2 can do automatically for the full set of 56 fitted models. Specifically, it uses the list of negative log likelihoods and parameter counts in the table to compute AIC values and Akaike weights for the full candidate set:&lt;br /&gt;
:* Analysis -&amp;gt; Do AIC calculations -&amp;gt;&lt;br /&gt;
:* Select &amp;quot;Write PAUP* block&amp;quot;&lt;br /&gt;
:* click &amp;quot;Do AIC calculations&amp;quot;&lt;br /&gt;
:* Results -&amp;gt; Show results table&lt;br /&gt;
:* Select &amp;quot;AIC&amp;quot; tab&lt;br /&gt;
:* &#039;&#039;&#039;IMPORTANT:&#039;&#039;&#039; SHIFT+click on the header of the &amp;quot;weight&amp;quot; column. This sorts the rows according to model weight, in descending order.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Question: &#039;&#039;&#039;What model was selected by modeltest based on the AIC values? (This will be the model with the highest weight - and lowest AIC - and will be in the first row of the results table after SHIFT-clicking the &amp;quot;weight&amp;quot; header).&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Question 4&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
: In the same AIC results table, now click (not SHIFT+click) the header of the &amp;quot;-lnL&amp;quot; column. In this table, a normal click sorts in ascending order, whereas SHIFT+click sorts in descending order. The model at the top will therefore be the one with the smallest value of &amp;quot;-lnL&amp;quot; (equivalently: the highest likelihood). Compare this model to the one with the highest Akaike weight.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Question: &#039;&#039;&#039;Is the model with the highest likelihood also the model with the highest Akaike weight? If not, explain briefly why these two rankings can differ.&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
== Construction of phylogenetic tree using PAUP ==&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Question 5&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
: Close the results table. In the main window you should now scroll up to the lines giving PAUP commands that implement the selected model. The command is enclosed between &amp;quot;BEGIN PAUP&amp;quot; and &amp;quot;END;&amp;quot; and should look something like this:&lt;br /&gt;
 Lset Base=(0.4064, [...]&lt;br /&gt;
: You will need to copy this command to a PAUP session in the next step.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Start PAUP&#039;&#039;&#039;&lt;br /&gt;
 paup&lt;br /&gt;
: Above, you used jmodeltest2 to select the most suitable substitution model for the present data set. You will now use this model to construct a maximum likelihood tree. You will use PAUP for this purpose. (Note: it is possible to create a maximum likelihood tree, or even a model-averaged tree, directly from jmodeltest2, but we will instead do it in PAUP in order to more clearly see each step that is taken.)&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Load alignment:&#039;&#039;&#039;&lt;br /&gt;
 execute gp120.nexus&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Set tree-building criterion to maximum likelihood&#039;&#039;&#039;&lt;br /&gt;
 set criterion=likelihood&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Set model parameters to winning estimates&#039;&#039;&#039;&lt;br /&gt;
: Above, you located a set of lines in the jmodeltest2 output giving a PAUP command that sets the model parameters to the estimates found for the winning model. Copy and paste this lset command (without the BEGIN and END parts) into the window where PAUP is running.&lt;br /&gt;
&lt;br /&gt;
 PASTE LSET COMMAND FROM MODELTEST RUN HERE&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Find best tree using selected model&#039;&#039;&#039;&lt;br /&gt;
: Still in the PAUP window, enter the following command:&lt;br /&gt;
 hsearch swap=tbr start=nj&lt;br /&gt;
: This command causes PAUP to perform a heuristic search for the best maximum likelihood tree. Once an initial tree has been constructed, the heuristic search proceeds by rearrangements of the &amp;quot;tree bisection and reconnection&amp;quot; type (TBR). We are using the model selected by modeltest, and the parameter estimates found by modeltest for that model. You could also choose to estimate all the model parameters as part of this step (i.e., at the same time as finding the best tree), but fixing them improves speed tremendously. Finding the best tree should take at most a few minutes.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Save best tree to file&#039;&#039;&#039;&lt;br /&gt;
 savetrees format=newick brlens=yes file=gp120tree.phy from=1 to=1&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Quit program&#039;&#039;&#039;&lt;br /&gt;
 quit&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Have a look at the tree:&#039;&#039;&#039;&lt;br /&gt;
: You have now produced an unrooted tree of the HIV sequences and saved it in the file gp120tree.phy. Note that in this exercise we will not be interested in the tree as such - our focus is instead on finding positive selection at a subset of codon positions, and the tree is just something we need in order to fit the different codon models to the data. If you want to see the tree, you can do so with the following command:&lt;br /&gt;
 figtree gp120tree.phy &amp;amp;&lt;br /&gt;
: There is no meaningful root placed on this tree, so you may want to choose the unrooted view (the third icon in the Layout section of the FigTree window). Close the FigTree window when you have had a look.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Question: &#039;&#039;&#039;Which substitution model did you use for the PAUP tree search, and what was the negative log likelihood of the best tree found under this model?&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
== Model selection and tree reconstruction in one step using IQ-TREE 3 ==&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Question 6&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
: In real-life phylogenetic analysis one would often use a modern program such as &#039;&#039;&#039;IQ-TREE 3&#039;&#039;&#039;, which can carry out model selection and maximum likelihood tree reconstruction in a single analysis.&lt;br /&gt;
&lt;br /&gt;
: Run IQ-TREE 3 on the alignment using the following command:&lt;br /&gt;
 iqtree3 -s gp120.nexus -m MFP -nt AUTO&lt;br /&gt;
&lt;br /&gt;
: Here, &amp;quot;-m MFP&amp;quot; tells IQ-TREE 3 to perform automatic model selection using &amp;quot;ModelFinder Plus&amp;quot; before reconstructing the tree.&lt;br /&gt;
&lt;br /&gt;
: When the run has finished, inspect the output written to the screen and to the file ending in &amp;quot;.iqtree&amp;quot;. : Note that IQ-TREE 3 reports several model-selection criteria in the output. The line near the top of the ModelFinder section states which criterion was used to choose the final model. Locate the substitution model selected by IQ-TREE 3 and the log likelihood of the best tree.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Question: &#039;&#039;&#039; Which substitution model was selected by IQ-TREE 3, and according to which information criterion was it selected? What was the log likelihood of the best tree? Compare this to the earlier jModelTest2 + PAUP analysis. Is the selected model exactly the same? Is the likelihood exactly the same? Briefly suggest one reason why the two analyses might not give identical results.&lt;br /&gt;
&lt;br /&gt;
---&lt;br /&gt;
&lt;br /&gt;
== Detection of positively selected sites in gp120 ==&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Question 7&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
: There is much more to phylogenetic analyses than merely reconstructing trees. One interesting result of probabilistic methods, is that the parameters of a model will have their values estimated as part of the optimization procedure. This means that once such a model has been fitted to the data, it is possible to investigate these estimated parameter values to learn features about the evolutionary history of the sequences under investigation. In the present example we will focus on investigating whether we can find positively selected sites in our data set, defined as sites where the dN/dS ratio is larger than 1. We do that by using a codon substitution model where the dN/dS ratio is one of its parameters.&lt;br /&gt;
&lt;br /&gt;
: A further strength of the probabilistic approach is that you get a probabilistic measure of how well any model fits the data. This means you can use a stringent approach to determine which model fits the data best. In this framework one uses likelihoods (probabilities of data given model) to determine which model fits the data best. As you saw above, it is for instance possible to compute AIC values and weights (model probabilities) from the likelihood values of fitted models, Since each model essentially corresponds to a hypothesis about the evolutionary history of the data, we can thus use a stringent statistical approach to decide which hypothesis best describes our data.&lt;br /&gt;
&lt;br /&gt;
: In outline, you will now use the following steps to investigate whether there is any evidence for positively selected codons in your data set:&lt;br /&gt;
&lt;br /&gt;
:* Fit model M1, which assumes there are two classes of codons in the sequence: some with dN/dS &amp;lt; 1, some with dN/dS=1.&lt;br /&gt;
:* Fit model M2, which assumes 3 distinct classes of codons: two with dN/dS ratios as for M1, and one extra class with dN/dS &amp;gt; 1.&lt;br /&gt;
:* Assess the strength of evidence for the two models using AIC-based model probabilities&lt;br /&gt;
:* If M2 is better: identify the positively selected codons&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Inspect the parameter file&#039;&#039;&#039;&lt;br /&gt;
 nedit codeml.ctl &amp;amp;&lt;br /&gt;
: The file &amp;quot;codeml.ctl&amp;quot; contains several settings that are relevant for running the program &#039;&#039;&#039;codeml&#039;&#039;&#039;. Find the following lines and ensure that the file contains these values:&lt;br /&gt;
 &#039;&#039;&#039;seqfile =  gp120align.fasta&#039;&#039;&#039;:  name of alignment file&lt;br /&gt;
 &#039;&#039;&#039;treefile =  gp120tree.phy&#039;&#039;&#039;: name of tree file&lt;br /&gt;
 &#039;&#039;&#039;seqtype = 1&#039;&#039;&#039;: tells the program that our data consists of coding DNA.&lt;br /&gt;
 &#039;&#039;&#039;NSsites = 1 2&#039;&#039;&#039; : tells the program to analyze models M1 and M2.&lt;br /&gt;
 &#039;&#039;&#039;cleandata = 0&#039;&#039;&#039;: tells the program to keep positions with gaps.&lt;br /&gt;
&lt;br /&gt;
: The settings entered by us will cause codeml to analyze two hypotheses about dN/dS ratios. M1 says there are two classes of codons with different dN/dS ratios in the sequence: one class with dN/dS &amp;lt; 1 (codons under purifying or negative selection), and one class with dN/dS=1 (no selection - neutrally evolving sites). M2 says there are 3 distinct dN/dS ratios for different sites in the sequence: one class with dN/dS &amp;lt; 1, one class with dN/dS=1 (these are the same type of classes as for M1), and one class with dN/dS &amp;gt; 1 (corresponding to sites under positive selection). The value of the dN/dS ratios (for those classes that have dN/dS &amp;lt; 1 or dN/dS &amp;gt; 1), the fraction of sites belonging to each class, and the position of sites belonging to each class, are unknown at first and will be determined during the analysis.&lt;br /&gt;
&lt;br /&gt;
: (Regarding &amp;quot;cleandata&amp;quot; setting: &amp;quot;cleandata = 1&amp;quot; would cause codeml to discard any alignment columns with gaps or ambiguity symbols. For alignments with many sequences this can lead to discarding the majority of sites and I would therefore recommend not using that setting. At the same time you do want to be wary of columns where most sequences have gaps, since inference of selection for these will be very uncertain. It might be better to discard such columns from an alignment before doing the codeml analysis).&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Start the analysis&#039;&#039;&#039;&lt;br /&gt;
 codeml&lt;br /&gt;
: This will start the codeml program using the settings in the file codeml.ctl. Depending on your computer, this will take some minutes to finish. (You may be able to see how the optimization procedure results in progressively better fits: the likelihood increases, meaning that negative log-likelihood decreases, as the fit improves).&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Inspect result file:&#039;&#039;&#039;&lt;br /&gt;
: Wait for the run to finish, and then look at the result file:&lt;br /&gt;
 nedit selection.results &amp;amp;&lt;br /&gt;
: This file contains a wealth of information concerning your analysis. You may want to turn off line-wrapping to more clearly see the structure of the file (there will be some long lines that will otherwise wrap around). The top part of the file gives an overview of your sequences, codon usage and nucleotide frequencies. You can ignore this information for now, and move on to the interesting part, namely the model likelihoods and parameter values:&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Find likelihood, and number of free parameters for model M1&#039;&#039;&#039;&lt;br /&gt;
 Search ==&amp;gt; Find... ==&amp;gt; enter &amp;quot;Model 1&amp;quot; and click Find&lt;br /&gt;
: You are now in the region of the result file where the model likelihoods and parameter estimates are noted. Now, locate a line that looks a bit like the one shown below:&lt;br /&gt;
 lnL(ntime: 72  np: 74):  -4242.470345     +0.000000&lt;br /&gt;
: Identify the number of &amp;quot;free parameters&amp;quot;, K, used in model M1: This is indicated by &amp;quot;np&amp;quot;, and is 74 in the example shown above (most of these parameters are branch lengths in the tree; specifically, the number of branch length parameters is indicated by &amp;quot;ntime&amp;quot;, and is 72 in this example). Also note the log-likelihood of the fitted model. This is the number right after the parenthesis, and is -4242.470345 in the example here.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Question: &#039;&#039;&#039;What are the values of K and lnL for model M1?&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Question 8&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Find dn/dS ratios and codon class proportions for model M1:&#039;&#039;&#039;&lt;br /&gt;
: Scroll down a few lines until you get to a small table similar to this:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
      dN/dS for site classes (K=2)&lt;br /&gt;
      p:   0.75111  0.24889&lt;br /&gt;
      w:   0.06583  1.00000&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
: This gives a summary of the dN/dS ratios that were found in the data set. The line starting &amp;quot;w:&amp;quot; lists the two dN/dS ratios that were found (in this case 0.06583 and 1.00000 - the last one was pre-specified by us as part of the model and was therefore not a free parameter). In this context &amp;quot;w&amp;quot; means &amp;quot;omega&amp;quot;, which is the symbol typically used to represent the dN/dS rate ratio. The line starting p: gives the proportion of codon sites belonging to each of the dN/dS ratio classes (in the example above approximately 75% belong to the first class , while 25% of all sites belong to the class having dN/dS=1.00000).&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Question: &#039;&#039;&#039;What are the dN/dS value (w) and proportion (p) of sites for both classes. Report the following values: p(class1), w(class1), p(class2), w(class2)&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Question 9&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Find likelihood, and K for model M2&#039;&#039;&#039;&lt;br /&gt;
: Scroll past the M1 output until you get to the results for model M2.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Question: &#039;&#039;&#039;What are the values of K and lnL for model M2?&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Question 10&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Find dn/dS ratios and codon class proportions for model M2:&#039;&#039;&#039;&lt;br /&gt;
: Now, scroll down a few lines until you get to a small table similar to the one you examined for M1 before. For this model there are 3 separate classes of codons.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Question: &#039;&#039;&#039;What are the dN/dS value (w) and proportion (p) of sites for all three classes? Report these values: p(class1), w(class1), p(class2), w(class2), p(class3), w(class3)&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Question 11&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Assess strength of evidence for models M1 and M2:&#039;&#039;&#039;&lt;br /&gt;
: M2 will always have a better (higher) log-likelihood than model M1 because M2 has more free parameters, and M1 is nested within M2. You should now use the recipe given above to compute AIC values and model probabilities for M1 and M2.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Question: &#039;&#039;&#039; Report: AIC, ΔAIC, weight (model probability) for M1 and M2&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Question 12: &#039;&#039;&#039; Is M2 better than M1?&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Question 13&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Examine list of positively selected sites&#039;&#039;&#039;&lt;br /&gt;
: If your M2 is clearly better than M1 (I firmly believe it should be if you did things according to instructions...), then you have evidence for the existence of positively selected sites in the gp120 gene. Now, scroll down to the end of the result file and locate a list similar to the one below. Note: This is the &amp;quot;Bayes Empirical Bayes&amp;quot; table, not the &amp;quot;Naive Empirical Bayes&amp;quot; table. &lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Bayes Empirical Bayes (BEB) analysis&lt;br /&gt;
Positively selected sites&lt;br /&gt;
&lt;br /&gt;
         Prob(w&amp;gt;1)     mean w&lt;br /&gt;
&lt;br /&gt;
    25 A 0.959*        3.133 +- 0.769&lt;br /&gt;
    27 P 0.906         2.990 +- 0.877&lt;br /&gt;
    56 K 0.987*        3.197 +- 0.687&lt;br /&gt;
    59 V 0.915         3.032 +- 0.873&lt;br /&gt;
    78 R 0.637         2.351 +- 1.129&lt;br /&gt;
    88 K 0.573         2.148 +- 1.077&lt;br /&gt;
    95 V 0.925         3.046 +- 0.843&lt;br /&gt;
    ...&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
: It is not important what the distinction is in this context, but very briefly NEB ignores the fact that there is uncertainty about  maximum likelihood estimates, especially for smaller data sets (for instance w for some codon is perhaps not exactly 3.046, but could be in a region around that value), while [https://pubmed.ncbi.nlm.nih.gov/15689528/ BEB accounts for that uncertainty].&lt;br /&gt;
: This gives you a list of which residues (if any) that were found to belong to the positively selected dN/dS-class. Also listed is the probability that the site really is in the codon class where dN/dS &amp;gt; 1, and a weighted average of the w at the site. Using only DNA sequences you have now identified likely epitopes on the gp120 protein.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Question: &#039;&#039;&#039;List all sites having more than 95% probability of belonging to the positively selected class&lt;/div&gt;</summary>
		<author><name>Gorm</name></author>
	</entry>
	<entry>
		<id>https://teaching.healthtech.dtu.dk/22115/index.php?title=Model_selection&amp;diff=270</id>
		<title>Model selection</title>
		<link rel="alternate" type="text/html" href="https://teaching.healthtech.dtu.dk/22115/index.php?title=Model_selection&amp;diff=270"/>
		<updated>2026-04-15T08:25:27Z</updated>

		<summary type="html">&lt;p&gt;Gorm: /* Model selection and tree reconstruction in one step using IQ-TREE 3 */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;This exercise is part of the course  [[22115_-_Computational_Molecular_Evolution|Computational Molecular Evolution (22115)]].&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Overview ==&lt;br /&gt;
&lt;br /&gt;
: In this exercise you are going to investigate features of HIV-1 evolution. You will do this by analyzing a large set of env-genes from HIV-1, subtype B. specifically, the DNA sequences analyzed here correspond to a region surrounding the hypervariable V3 region of the gp120 protein.&lt;br /&gt;
&lt;br /&gt;
: Like other retroviruses, particles of HIV are made up of 2 copies of a single-stranded RNA genome packaged inside a protein core, or capsid. The core particle also contains viral proteins that are essential for the early steps of the virus life cycle, such as reverse transcription and integration. A lipid envelope, derived from the infected cell, surrounds the core particle. Embedded in this envelope are the surface glycoproteins of HIV: gp120 and gp41. The gp120 protein is crucial for binding of the virus particle to target cells, while gp41 is important for the subsequent fusion event. It is the specific affinity of gp120 for the CD4 protein that targets HIV to those cells of the immune system that express CD4 on their surface (e.g., T-helper lymphocytes, monocytes, and macrophages).&lt;br /&gt;
&lt;br /&gt;
: The role gp120 plays in infection and the fact that it is situated on the surface of the HIV particle, means it is an obvious target for the immune response. That means that there may be a considerable selective pressure on gp120 for creating immune-escape mutants, where amino acids in the gp120 epitopes have been substituted. In this exercise you will construct a maximum likelihood tree that we will subsequently use to investigate whether you can detect such a selective pressure on parts of gp120, again using maximum likelihood methods.&lt;br /&gt;
&lt;br /&gt;
: One major goal with the exercise is to introduce you to statistically based methods for assessing the strength of evidence for a set of alternative hypotheses about some biological system of interest. The model selection method we will use is AIC (Akaike Information Criterion), based on which you will compute model probabilities. A second goal is to make you aware that phylogenetic analysis is not only about constructing trees, but that it is also a useful framework for analyzing biological questions more generally.&lt;br /&gt;
&lt;br /&gt;
: Specifically, you will&lt;br /&gt;
&lt;br /&gt;
:# perform a multiple alignment of gp120 DNA sequences taking protein-level information into account (using revtrans).&lt;br /&gt;
:# select a suitable nucleotide substitution model (using jmodeltest2)&lt;br /&gt;
:# construct a phylogenetic tree (using PAUP).&lt;br /&gt;
:# try to detect positively selected sites in gp120 (using PAML).&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
== Recipe for computing AIC values and model probabilities ==&lt;br /&gt;
&lt;br /&gt;
: Later in today&#039;s exercise you will be asked to compute AIC values and model probabilities. Return to this section and follow the instructions when you need to do so.&lt;br /&gt;
&lt;br /&gt;
:# Fit a set of models to your data, note the maximized log likelihoods (lnL) and the number of free parameters (K) for each model in the investigated set. The models you fit should represent a plausible and comprehensive set of hypotheses about your data.&lt;br /&gt;
:# Compute AIC for each of the models: &#039;&#039;&#039;AIC = -2 x lnL + 2K&#039;&#039;&#039;.  &amp;lt;br&amp;gt;For example: a model with lnL = -2010 and K = 5 will have AIC = -2 x -2010 + 2 x 5 = 4030.&lt;br /&gt;
:# Identify the model with the smallest AIC (this is the best model in the set). We will call the AIC for this model &#039;&#039;&#039;&amp;quot;AICmin&amp;quot;&#039;&#039;&#039;.&lt;br /&gt;
:# Compute the &amp;quot;ΔAIC&amp;quot; values for each model: &#039;&#039;&#039;ΔAIC = AIC - AICmin&#039;&#039;&#039; &amp;lt;br&amp;gt;For each model subtract the minimum AIC value. The best model will have a ΔAIC of zero. The rest of the models will have positive ΔAICs.&lt;br /&gt;
:# For each model compute the following quantity: &#039;&#039;&#039;numerator = exp(-0.5 x ΔAIC)&#039;&#039;&#039; &amp;lt;br&amp;gt;For example, a model with ΔAIC=4.2 will have numerator = exp(-0.5 x 4.2) = exp(-2.1) = 0.1225. Also compute the &#039;&#039;&#039;sum&#039;&#039;&#039; of the numerator values for all models.&lt;br /&gt;
:# Finally, the model probabilities (also called model weights) for each model are found as:&amp;lt;br&amp;gt; &#039;&#039;&#039;P(model) = numerator / sum&#039;&#039;&#039; &amp;lt;br&amp;gt;For example, if sum = 3.75 and a model has numerator = 1.3, then it has P(model) = 1.3 / 3.75 = 0.35&lt;br /&gt;
&lt;br /&gt;
: You may want to keep track of the computations by constructing a table along the following lines:&lt;br /&gt;
[[File:Molevol-Downloads-aictable.png|700px]]&lt;br /&gt;
&lt;br /&gt;
: Note that model probabilities can also be computed using Bayesian methods. One advantage of Bayesian methods over AIC is that instead of relying on a point estimate, uncertainty about parameter values is accounted for by integrating over all possible values (typically using MCMC).&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
== Getting started ==&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Create working directory, copy files&#039;&#039;&#039;&lt;br /&gt;
: In the command below: Instead of /path/to/molevol enter the path to the directory where you have placed your course files (for instance cd /Users/bob/Documents/molevol, or cd /home/student/molevol).&lt;br /&gt;
 cd /path/to/molevol&lt;br /&gt;
 mkdir modelselect&lt;br /&gt;
 cd modelselect&lt;br /&gt;
 cp ../data/gp120.fasta ./gp120.fasta&lt;br /&gt;
 cp ../data/codeml.ctl ./codeml.ctl&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Have a look at the DNA data file:&#039;&#039;&#039;&lt;br /&gt;
 nedit gp120.fasta &amp;amp;&lt;br /&gt;
: The file contains several DNA sequences from HIV-1, subtype B. The sequences are approximately 500 bp long, and correspond to a region surrounding the hypervariable V3 region in the gene encoding gp120. Close the nedit window when you&#039;ve had a look.&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
== Protein-guided alignment of coding DNA ==&lt;br /&gt;
&lt;br /&gt;
: There are three reasons why it is preferable to align coding DNA at the amino acid level rather than directly as nucleotides. First, the DNA alphabet has only four letters compared to 20 amino acids, which means that even unrelated DNA sequences will share roughly 25% identity by chance alone, making it much harder to distinguish true homology from noise. Second, protein alignments benefit from empirical substitution matrices such as BLOSUM-62, which capture the fact that amino acid replacements tend to be conservative. Equivalent matrices for DNA are far less informative because nucleotide substitution patterns vary strongly between genes and organisms. Third, synonymous substitutions accumulate faster than non-synonymous ones, so DNA sequences diverge more rapidly than the proteins they encode. Taken together, these factors mean that the phylogenetic signal in a DNA alignment erodes much faster than in the corresponding protein alignment.&lt;br /&gt;
&lt;br /&gt;
: However, while protein-level alignment gives us the most reliable homology signal, it discards exactly the information we need for studying selection: the synonymous substitutions. Estimating the ratio of non-synonymous to synonymous substitution rates (dN/dS) requires a codon-level alignment where analogous codon positions are properly lined up. We would therefore like to construct a multiple alignment at the DNA level, but using information at the protein level, and the RevTrans server does exactly that.&lt;br /&gt;
&lt;br /&gt;
: &#039;&#039;&#039;RevTrans&#039;&#039;&#039; takes as input an unaligned set of DNA sequences, automatically translates them to the equivalent amino acid sequences, constructs a multiple alignment of the protein sequences, and finally uses the protein alignment as a template for constructing a multiple DNA alignment. Because each amino acid maps back to its original codon, gaps are always inserted in groups of three and codon boundaries are preserved throughout the alignment. This is essential for the dN/dS analyses we will carry out in this exercise: if codon positions are misaligned, synonymous and non-synonymous changes cannot be correctly distinguished.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Construct RevTrans alignment&#039;&#039;&#039;&lt;br /&gt;
:* Open RevTrans server page: https://services.healthtech.dtu.dk/services/RevTrans-2.0/&lt;br /&gt;
:* On the RevTrans page: Choose the file gp120.fasta as input (or copy and paste the sequence into the sequence window)&lt;br /&gt;
:* Click the &amp;quot;Submit query&amp;quot; button&lt;br /&gt;
:* When the alignment is done you may have to click link named &amp;quot;here&amp;quot; to go to results page&lt;br /&gt;
:* Download DNA alignment, by right-clicking the link for &amp;quot;Download alignment in FASTA format&amp;quot;, and choosing &amp;quot;Save link as...&amp;quot; (save file under the name gp120align.fasta and make sure to save the file in the directory modelselect).&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Convert alignment to NEXUS format&#039;&#039;&#039;&lt;br /&gt;
: Convert the fasta file to NEXUS format and save file in the modelselect directory under the name gp120.nexus&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
== Model selection using AIC and jModelTest2 ==&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Question 1&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
: As part of the present analysis we are going to build a phylogenetic tree based on the DNA alignment constructed above. We will construct the tree using maximum likelihood, but to do that we first have to decide which substitution model we want to use. Specifically, we are interested in selecting a model that achieves a good balance between fit to the data and number of parameters. We will investigate this issue by fitting a set of 56 different models to our data and then comparing them using AIC.&lt;br /&gt;
&lt;br /&gt;
: In the first part of the exercise, however, you will only compare three closely related models by hand. The purpose of this restricted comparison is not primarily to identify the globally best model, but to understand how AIC, ΔAIC, and Akaike weights are computed and interpreted. Because only three models are included in this manual comparison, the weights you compute will describe relative support &#039;&#039;&#039;within this restricted set only&#039;&#039;&#039;.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Start jmodeltest2&#039;&#039;&#039;&lt;br /&gt;
 jmodeltest&lt;br /&gt;
&#039;&#039;&#039;Load data set&#039;&#039;&#039;&lt;br /&gt;
:* File -&amp;gt; Load DNA alignment -&amp;gt; File Format -&amp;gt; Select &amp;quot;All files&amp;quot;&lt;br /&gt;
:* Navigate to gp120.nexus and load it&lt;br /&gt;
&#039;&#039;&#039;Fit 56 models&#039;&#039;&#039;&lt;br /&gt;
:* Analysis -&amp;gt; Compute likelihood scores&lt;br /&gt;
:* Select &amp;quot;7&amp;quot; under &amp;quot;Number of substitution schemes&amp;quot;&lt;br /&gt;
:* Select &amp;quot;Fixed BIONJ-JC&amp;quot; under &amp;quot;Base tree for likelihood calculations&amp;quot;&lt;br /&gt;
:* Click &amp;quot;Compute likelihoods&amp;quot;&lt;br /&gt;
&lt;br /&gt;
: This causes jmodeltest2 to perform the following actions: first a neighbor joining tree is constructed using the Jukes and Cantor model. Then the tree is fixed and used as the basis for fitting a set of 56 different models to the data. For each model, the estimated model parameters and the negative log-likelihood are recorded. In addition to varying sets of substitution rate parameters (JC, K2P, ...), some of these models also include extra parameters that take into account the presence of different rates between sites. This is done in two ways: (1) by fitting a gamma distribution of rates (&amp;quot;+G&amp;quot;), and (2) by allowing for a proportion of constant (&amp;quot;invariable&amp;quot;) sites (&amp;quot;+I&amp;quot;).&lt;br /&gt;
&lt;br /&gt;
: Wait until jmodeltest2 is done fitting all 56 models (this will take a little while depending on your computer).&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Inspect result&#039;&#039;&#039;&lt;br /&gt;
:* Results -&amp;gt; Show results table&lt;br /&gt;
: For each model this table lists the negative log-likelihood (&amp;quot;-lnL&amp;quot;), the number of parameters (&amp;quot;p&amp;quot; - note this includes branch lengths), and estimates of all model parameters (excluding branch lengths).&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Manually compute Akaike weights for three substitution models&#039;&#039;&#039;&lt;br /&gt;
: Use AIC-based model comparison to investigate which of the following three substitution models are best supported as descriptions of how the sequences have evolved:&lt;br /&gt;
:* Jukes and Cantor with fraction of invariant sites (JC+I)&lt;br /&gt;
:* Jukes and Cantor with gamma-distributed rates over sites (JC+G)&lt;br /&gt;
:* Jukes and Cantor with invariant sites and gamma-distributed rates (JC+I+G)&lt;br /&gt;
: Before you can do the computation you need to know the log likelihood and the number of parameters for each model. Locate these values in the table for the JC+I, JC+G, and JC+I+G models, and write them down. Close the window with the result table when you are done.&lt;br /&gt;
&lt;br /&gt;
: Make sure to get the signs right: the values reported in the table are -lnL values, so you will need to reverse the sign to get the lnL (the lnL values you write down should be negative).&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Question: &#039;&#039;&#039;Use the recipe above to compute AIC, ΔAIC, and Akaike weight for the three models. Report the results in a table similar to the one shown above, and verify that the three weights sum to 1 (up to rounding error).&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Question 2&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
: Based on the Akaike weights, which of the three models has the strongest support? Is the support strongly concentrated on one model, or is there still substantial support for one or both of the others?&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Question 3&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
: What you just did manually for JC+I, JC+G and JC+I+G, jmodeltest2 can do automatically for the full set of 56 fitted models. Specifically, it uses the list of negative log likelihoods and parameter counts in the table to compute AIC values and Akaike weights for the full candidate set:&lt;br /&gt;
:* Analysis -&amp;gt; Do AIC calculations -&amp;gt;&lt;br /&gt;
:* Select &amp;quot;Write PAUP* block&amp;quot;&lt;br /&gt;
:* click &amp;quot;Do AIC calculations&amp;quot;&lt;br /&gt;
:* Results -&amp;gt; Show results table&lt;br /&gt;
:* Select &amp;quot;AIC&amp;quot; tab&lt;br /&gt;
:* &#039;&#039;&#039;IMPORTANT:&#039;&#039;&#039; SHIFT+click on the header of the &amp;quot;weight&amp;quot; column. This sorts the rows according to model weight, in descending order.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Question: &#039;&#039;&#039;What model was selected by modeltest based on the AIC values? (This will be the model with the highest weight - and lowest AIC - and will be in the first row of the results table after SHIFT-clicking the &amp;quot;weight&amp;quot; header).&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Question 4&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
: In the same AIC results table, now click (not SHIFT+click) the header of the &amp;quot;-lnL&amp;quot; column. In this table, a normal click sorts in ascending order, whereas SHIFT+click sorts in descending order. The model at the top will therefore be the one with the smallest value of &amp;quot;-lnL&amp;quot; (equivalently: the highest likelihood). Compare this model to the one with the highest Akaike weight.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Question: &#039;&#039;&#039;Is the model with the highest likelihood also the model with the highest Akaike weight? If not, explain briefly why these two rankings can differ.&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
== Construction of phylogenetic tree using PAUP ==&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Question 5&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
: Close the results table. In the main window you should now scroll up to the lines giving PAUP commands that implement the selected model. The command is enclosed between &amp;quot;BEGIN PAUP&amp;quot; and &amp;quot;END;&amp;quot; and should look something like this:&lt;br /&gt;
 Lset Base=(0.4064, [...]&lt;br /&gt;
: You will need to copy this command to a PAUP session in the next step.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Start PAUP&#039;&#039;&#039;&lt;br /&gt;
 paup&lt;br /&gt;
: Above, you used jmodeltest2 to select the most suitable substitution model for the present data set. You will now use this model to construct a maximum likelihood tree. You will use PAUP for this purpose. (Note: it is possible to create a maximum likelihood tree, or even a model-averaged tree, directly from jmodeltest2, but we will instead do it in PAUP in order to more clearly see each step that is taken.)&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Load alignment:&#039;&#039;&#039;&lt;br /&gt;
 execute gp120.nexus&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Set tree-building criterion to maximum likelihood&#039;&#039;&#039;&lt;br /&gt;
 set criterion=likelihood&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Set model parameters to winning estimates&#039;&#039;&#039;&lt;br /&gt;
: Above, you located a set of lines in the jmodeltest2 output giving a PAUP command that sets the model parameters to the estimates found for the winning model. Copy and paste this lset command (without the BEGIN and END parts) into the window where PAUP is running.&lt;br /&gt;
&lt;br /&gt;
 PASTE LSET COMMAND FROM MODELTEST RUN HERE&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Find best tree using selected model&#039;&#039;&#039;&lt;br /&gt;
: Still in the PAUP window, enter the following command:&lt;br /&gt;
 hsearch swap=tbr start=nj&lt;br /&gt;
: This command causes PAUP to perform a heuristic search for the best maximum likelihood tree. Once an initial tree has been constructed, the heuristic search proceeds by rearrangements of the &amp;quot;tree bisection and reconnection&amp;quot; type (TBR). We are using the model selected by modeltest, and the parameter estimates found by modeltest for that model. You could also choose to estimate all the model parameters as part of this step (i.e., at the same time as finding the best tree), but fixing them improves speed tremendously. Finding the best tree should take at most a few minutes.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Save best tree to file&#039;&#039;&#039;&lt;br /&gt;
 savetrees format=newick brlens=yes file=gp120tree.phy from=1 to=1&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Quit program&#039;&#039;&#039;&lt;br /&gt;
 quit&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Have a look at the tree:&#039;&#039;&#039;&lt;br /&gt;
: You have now produced an unrooted tree of the HIV sequences and saved it in the file gp120tree.phy. Note that in this exercise we will not be interested in the tree as such - our focus is instead on finding positive selection at a subset of codon positions, and the tree is just something we need in order to fit the different codon models to the data. If you want to see the tree, you can do so with the following command:&lt;br /&gt;
 figtree gp120tree.phy &amp;amp;&lt;br /&gt;
: There is no meaningful root placed on this tree, so you may want to choose the unrooted view (the third icon in the Layout section of the FigTree window). Close the FigTree window when you have had a look.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Question: &#039;&#039;&#039;Which substitution model did you use for the PAUP tree search, and what was the negative log likelihood of the best tree found under this model?&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
== Model selection and tree reconstruction in one step using IQ-TREE 3 ==&lt;br /&gt;
&lt;br /&gt;
: In real-life phylogenetic analysis one would often use a modern program such as &#039;&#039;&#039;IQ-TREE 3&#039;&#039;&#039;, which can carry out model selection and maximum likelihood tree reconstruction in a single analysis.&lt;br /&gt;
&lt;br /&gt;
: Run IQ-TREE 3 on the alignment using the following command:&lt;br /&gt;
 iqtree3 -s gp120.nexus -m MFP -nt AUTO&lt;br /&gt;
&lt;br /&gt;
: Here, &amp;quot;-m MFP&amp;quot; tells IQ-TREE 3 to perform automatic model selection using &amp;quot;ModelFinder Plus&amp;quot; before reconstructing the tree.&lt;br /&gt;
&lt;br /&gt;
: When the run has finished, inspect the output written to the screen and to the file ending in &amp;quot;.iqtree&amp;quot;. : Note that IQ-TREE 3 reports several model-selection criteria in the output. The line near the top of the ModelFinder section states which criterion was used to choose the final model. Locate the substitution model selected by IQ-TREE 3 and the log likelihood of the best tree.&lt;br /&gt;
&lt;br /&gt;
: Which substitution model was selected by IQ-TREE 3, and according to which information criterion was it selected? What was the log likelihood of the best tree? Compare this to the earlier jModelTest2 + PAUP analysis. Is the selected model exactly the same? Is the likelihood exactly the same? Briefly suggest one reason why the two analyses might not give identical results.&lt;br /&gt;
&lt;br /&gt;
---&lt;br /&gt;
&lt;br /&gt;
== Detection of positively selected sites in gp120 ==&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Question 7&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
: There is much more to phylogenetic analyses than merely reconstructing trees. One interesting result of probabilistic methods, is that the parameters of a model will have their values estimated as part of the optimization procedure. This means that once such a model has been fitted to the data, it is possible to investigate these estimated parameter values to learn features about the evolutionary history of the sequences under investigation. In the present example we will focus on investigating whether we can find positively selected sites in our data set, defined as sites where the dN/dS ratio is larger than 1. We do that by using a codon substitution model where the dN/dS ratio is one of its parameters.&lt;br /&gt;
&lt;br /&gt;
: A further strength of the probabilistic approach is that you get a probabilistic measure of how well any model fits the data. This means you can use a stringent approach to determine which model fits the data best. In this framework one uses likelihoods (probabilities of data given model) to determine which model fits the data best. As you saw above, it is for instance possible to compute AIC values and weights (model probabilities) from the likelihood values of fitted models, Since each model essentially corresponds to a hypothesis about the evolutionary history of the data, we can thus use a stringent statistical approach to decide which hypothesis best describes our data.&lt;br /&gt;
&lt;br /&gt;
: In outline, you will now use the following steps to investigate whether there is any evidence for positively selected codons in your data set:&lt;br /&gt;
&lt;br /&gt;
:* Fit model M1, which assumes there are two classes of codons in the sequence: some with dN/dS &amp;lt; 1, some with dN/dS=1.&lt;br /&gt;
:* Fit model M2, which assumes 3 distinct classes of codons: two with dN/dS ratios as for M1, and one extra class with dN/dS &amp;gt; 1.&lt;br /&gt;
:* Assess the strength of evidence for the two models using AIC-based model probabilities&lt;br /&gt;
:* If M2 is better: identify the positively selected codons&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Inspect the parameter file&#039;&#039;&#039;&lt;br /&gt;
 nedit codeml.ctl &amp;amp;&lt;br /&gt;
: The file &amp;quot;codeml.ctl&amp;quot; contains several settings that are relevant for running the program &#039;&#039;&#039;codeml&#039;&#039;&#039;. Find the following lines and ensure that the file contains these values:&lt;br /&gt;
 &#039;&#039;&#039;seqfile =  gp120align.fasta&#039;&#039;&#039;:  name of alignment file&lt;br /&gt;
 &#039;&#039;&#039;treefile =  gp120tree.phy&#039;&#039;&#039;: name of tree file&lt;br /&gt;
 &#039;&#039;&#039;seqtype = 1&#039;&#039;&#039;: tells the program that our data consists of coding DNA.&lt;br /&gt;
 &#039;&#039;&#039;NSsites = 1 2&#039;&#039;&#039; : tells the program to analyze models M1 and M2.&lt;br /&gt;
 &#039;&#039;&#039;cleandata = 0&#039;&#039;&#039;: tells the program to keep positions with gaps.&lt;br /&gt;
&lt;br /&gt;
: The settings entered by us will cause codeml to analyze two hypotheses about dN/dS ratios. M1 says there are two classes of codons with different dN/dS ratios in the sequence: one class with dN/dS &amp;lt; 1 (codons under purifying or negative selection), and one class with dN/dS=1 (no selection - neutrally evolving sites). M2 says there are 3 distinct dN/dS ratios for different sites in the sequence: one class with dN/dS &amp;lt; 1, one class with dN/dS=1 (these are the same type of classes as for M1), and one class with dN/dS &amp;gt; 1 (corresponding to sites under positive selection). The value of the dN/dS ratios (for those classes that have dN/dS &amp;lt; 1 or dN/dS &amp;gt; 1), the fraction of sites belonging to each class, and the position of sites belonging to each class, are unknown at first and will be determined during the analysis.&lt;br /&gt;
&lt;br /&gt;
: (Regarding &amp;quot;cleandata&amp;quot; setting: &amp;quot;cleandata = 1&amp;quot; would cause codeml to discard any alignment columns with gaps or ambiguity symbols. For alignments with many sequences this can lead to discarding the majority of sites and I would therefore recommend not using that setting. At the same time you do want to be wary of columns where most sequences have gaps, since inference of selection for these will be very uncertain. It might be better to discard such columns from an alignment before doing the codeml analysis).&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Start the analysis&#039;&#039;&#039;&lt;br /&gt;
 codeml&lt;br /&gt;
: This will start the codeml program using the settings in the file codeml.ctl. Depending on your computer, this will take some minutes to finish. (You may be able to see how the optimization procedure results in progressively better fits: the likelihood increases, meaning that negative log-likelihood decreases, as the fit improves).&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Inspect result file:&#039;&#039;&#039;&lt;br /&gt;
: Wait for the run to finish, and then look at the result file:&lt;br /&gt;
 nedit selection.results &amp;amp;&lt;br /&gt;
: This file contains a wealth of information concerning your analysis. You may want to turn off line-wrapping to more clearly see the structure of the file (there will be some long lines that will otherwise wrap around). The top part of the file gives an overview of your sequences, codon usage and nucleotide frequencies. You can ignore this information for now, and move on to the interesting part, namely the model likelihoods and parameter values:&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Find likelihood, and number of free parameters for model M1&#039;&#039;&#039;&lt;br /&gt;
 Search ==&amp;gt; Find... ==&amp;gt; enter &amp;quot;Model 1&amp;quot; and click Find&lt;br /&gt;
: You are now in the region of the result file where the model likelihoods and parameter estimates are noted. Now, locate a line that looks a bit like the one shown below:&lt;br /&gt;
 lnL(ntime: 72  np: 74):  -4242.470345     +0.000000&lt;br /&gt;
: Identify the number of &amp;quot;free parameters&amp;quot;, K, used in model M1: This is indicated by &amp;quot;np&amp;quot;, and is 74 in the example shown above (most of these parameters are branch lengths in the tree; specifically, the number of branch length parameters is indicated by &amp;quot;ntime&amp;quot;, and is 72 in this example). Also note the log-likelihood of the fitted model. This is the number right after the parenthesis, and is -4242.470345 in the example here.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Question: &#039;&#039;&#039;What are the values of K and lnL for model M1?&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Question 8&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Find dn/dS ratios and codon class proportions for model M1:&#039;&#039;&#039;&lt;br /&gt;
: Scroll down a few lines until you get to a small table similar to this:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
      dN/dS for site classes (K=2)&lt;br /&gt;
      p:   0.75111  0.24889&lt;br /&gt;
      w:   0.06583  1.00000&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
: This gives a summary of the dN/dS ratios that were found in the data set. The line starting &amp;quot;w:&amp;quot; lists the two dN/dS ratios that were found (in this case 0.06583 and 1.00000 - the last one was pre-specified by us as part of the model and was therefore not a free parameter). In this context &amp;quot;w&amp;quot; means &amp;quot;omega&amp;quot;, which is the symbol typically used to represent the dN/dS rate ratio. The line starting p: gives the proportion of codon sites belonging to each of the dN/dS ratio classes (in the example above approximately 75% belong to the first class , while 25% of all sites belong to the class having dN/dS=1.00000).&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Question: &#039;&#039;&#039;What are the dN/dS value (w) and proportion (p) of sites for both classes. Report the following values: p(class1), w(class1), p(class2), w(class2)&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Question 9&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Find likelihood, and K for model M2&#039;&#039;&#039;&lt;br /&gt;
: Scroll past the M1 output until you get to the results for model M2.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Question: &#039;&#039;&#039;What are the values of K and lnL for model M2?&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Question 10&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Find dn/dS ratios and codon class proportions for model M2:&#039;&#039;&#039;&lt;br /&gt;
: Now, scroll down a few lines until you get to a small table similar to the one you examined for M1 before. For this model there are 3 separate classes of codons.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Question: &#039;&#039;&#039;What are the dN/dS value (w) and proportion (p) of sites for all three classes? Report these values: p(class1), w(class1), p(class2), w(class2), p(class3), w(class3)&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Question 11&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Assess strength of evidence for models M1 and M2:&#039;&#039;&#039;&lt;br /&gt;
: M2 will always have a better (higher) log-likelihood than model M1 because M2 has more free parameters, and M1 is nested within M2. You should now use the recipe given above to compute AIC values and model probabilities for M1 and M2.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Question: &#039;&#039;&#039; Report: AIC, ΔAIC, weight (model probability) for M1 and M2&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Question 12: &#039;&#039;&#039; Is M2 better than M1?&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Question 13&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Examine list of positively selected sites&#039;&#039;&#039;&lt;br /&gt;
: If your M2 is clearly better than M1 (I firmly believe it should be if you did things according to instructions...), then you have evidence for the existence of positively selected sites in the gp120 gene. Now, scroll down to the end of the result file and locate a list similar to the one below. Note: This is the &amp;quot;Bayes Empirical Bayes&amp;quot; table, not the &amp;quot;Naive Empirical Bayes&amp;quot; table. &lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Bayes Empirical Bayes (BEB) analysis&lt;br /&gt;
Positively selected sites&lt;br /&gt;
&lt;br /&gt;
         Prob(w&amp;gt;1)     mean w&lt;br /&gt;
&lt;br /&gt;
    25 A 0.959*        3.133 +- 0.769&lt;br /&gt;
    27 P 0.906         2.990 +- 0.877&lt;br /&gt;
    56 K 0.987*        3.197 +- 0.687&lt;br /&gt;
    59 V 0.915         3.032 +- 0.873&lt;br /&gt;
    78 R 0.637         2.351 +- 1.129&lt;br /&gt;
    88 K 0.573         2.148 +- 1.077&lt;br /&gt;
    95 V 0.925         3.046 +- 0.843&lt;br /&gt;
    ...&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
: It is not important what the distinction is in this context, but very briefly NEB ignores the fact that there is uncertainty about  maximum likelihood estimates, especially for smaller data sets (for instance w for some codon is perhaps not exactly 3.046, but could be in a region around that value), while [https://pubmed.ncbi.nlm.nih.gov/15689528/ BEB accounts for that uncertainty].&lt;br /&gt;
: This gives you a list of which residues (if any) that were found to belong to the positively selected dN/dS-class. Also listed is the probability that the site really is in the codon class where dN/dS &amp;gt; 1, and a weighted average of the w at the site. Using only DNA sequences you have now identified likely epitopes on the gp120 protein.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Question: &#039;&#039;&#039;List all sites having more than 95% probability of belonging to the positively selected class&lt;/div&gt;</summary>
		<author><name>Gorm</name></author>
	</entry>
	<entry>
		<id>https://teaching.healthtech.dtu.dk/22115/index.php?title=Model_selection&amp;diff=269</id>
		<title>Model selection</title>
		<link rel="alternate" type="text/html" href="https://teaching.healthtech.dtu.dk/22115/index.php?title=Model_selection&amp;diff=269"/>
		<updated>2026-04-15T08:20:42Z</updated>

		<summary type="html">&lt;p&gt;Gorm: /* Model selection and tree reconstruction in one step using IQ-TREE 3 */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;This exercise is part of the course  [[22115_-_Computational_Molecular_Evolution|Computational Molecular Evolution (22115)]].&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Overview ==&lt;br /&gt;
&lt;br /&gt;
: In this exercise you are going to investigate features of HIV-1 evolution. You will do this by analyzing a large set of env-genes from HIV-1, subtype B. specifically, the DNA sequences analyzed here correspond to a region surrounding the hypervariable V3 region of the gp120 protein.&lt;br /&gt;
&lt;br /&gt;
: Like other retroviruses, particles of HIV are made up of 2 copies of a single-stranded RNA genome packaged inside a protein core, or capsid. The core particle also contains viral proteins that are essential for the early steps of the virus life cycle, such as reverse transcription and integration. A lipid envelope, derived from the infected cell, surrounds the core particle. Embedded in this envelope are the surface glycoproteins of HIV: gp120 and gp41. The gp120 protein is crucial for binding of the virus particle to target cells, while gp41 is important for the subsequent fusion event. It is the specific affinity of gp120 for the CD4 protein that targets HIV to those cells of the immune system that express CD4 on their surface (e.g., T-helper lymphocytes, monocytes, and macrophages).&lt;br /&gt;
&lt;br /&gt;
: The role gp120 plays in infection and the fact that it is situated on the surface of the HIV particle, means it is an obvious target for the immune response. That means that there may be a considerable selective pressure on gp120 for creating immune-escape mutants, where amino acids in the gp120 epitopes have been substituted. In this exercise you will construct a maximum likelihood tree that we will subsequently use to investigate whether you can detect such a selective pressure on parts of gp120, again using maximum likelihood methods.&lt;br /&gt;
&lt;br /&gt;
: One major goal with the exercise is to introduce you to statistically based methods for assessing the strength of evidence for a set of alternative hypotheses about some biological system of interest. The model selection method we will use is AIC (Akaike Information Criterion), based on which you will compute model probabilities. A second goal is to make you aware that phylogenetic analysis is not only about constructing trees, but that it is also a useful framework for analyzing biological questions more generally.&lt;br /&gt;
&lt;br /&gt;
: Specifically, you will&lt;br /&gt;
&lt;br /&gt;
:# perform a multiple alignment of gp120 DNA sequences taking protein-level information into account (using revtrans).&lt;br /&gt;
:# select a suitable nucleotide substitution model (using jmodeltest2)&lt;br /&gt;
:# construct a phylogenetic tree (using PAUP).&lt;br /&gt;
:# try to detect positively selected sites in gp120 (using PAML).&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
== Recipe for computing AIC values and model probabilities ==&lt;br /&gt;
&lt;br /&gt;
: Later in today&#039;s exercise you will be asked to compute AIC values and model probabilities. Return to this section and follow the instructions when you need to do so.&lt;br /&gt;
&lt;br /&gt;
:# Fit a set of models to your data, note the maximized log likelihoods (lnL) and the number of free parameters (K) for each model in the investigated set. The models you fit should represent a plausible and comprehensive set of hypotheses about your data.&lt;br /&gt;
:# Compute AIC for each of the models: &#039;&#039;&#039;AIC = -2 x lnL + 2K&#039;&#039;&#039;.  &amp;lt;br&amp;gt;For example: a model with lnL = -2010 and K = 5 will have AIC = -2 x -2010 + 2 x 5 = 4030.&lt;br /&gt;
:# Identify the model with the smallest AIC (this is the best model in the set). We will call the AIC for this model &#039;&#039;&#039;&amp;quot;AICmin&amp;quot;&#039;&#039;&#039;.&lt;br /&gt;
:# Compute the &amp;quot;ΔAIC&amp;quot; values for each model: &#039;&#039;&#039;ΔAIC = AIC - AICmin&#039;&#039;&#039; &amp;lt;br&amp;gt;For each model subtract the minimum AIC value. The best model will have a ΔAIC of zero. The rest of the models will have positive ΔAICs.&lt;br /&gt;
:# For each model compute the following quantity: &#039;&#039;&#039;numerator = exp(-0.5 x ΔAIC)&#039;&#039;&#039; &amp;lt;br&amp;gt;For example, a model with ΔAIC=4.2 will have numerator = exp(-0.5 x 4.2) = exp(-2.1) = 0.1225. Also compute the &#039;&#039;&#039;sum&#039;&#039;&#039; of the numerator values for all models.&lt;br /&gt;
:# Finally, the model probabilities (also called model weights) for each model are found as:&amp;lt;br&amp;gt; &#039;&#039;&#039;P(model) = numerator / sum&#039;&#039;&#039; &amp;lt;br&amp;gt;For example, if sum = 3.75 and a model has numerator = 1.3, then it has P(model) = 1.3 / 3.75 = 0.35&lt;br /&gt;
&lt;br /&gt;
: You may want to keep track of the computations by constructing a table along the following lines:&lt;br /&gt;
[[File:Molevol-Downloads-aictable.png|700px]]&lt;br /&gt;
&lt;br /&gt;
: Note that model probabilities can also be computed using Bayesian methods. One advantage of Bayesian methods over AIC is that instead of relying on a point estimate, uncertainty about parameter values is accounted for by integrating over all possible values (typically using MCMC).&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
== Getting started ==&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Create working directory, copy files&#039;&#039;&#039;&lt;br /&gt;
: In the command below: Instead of /path/to/molevol enter the path to the directory where you have placed your course files (for instance cd /Users/bob/Documents/molevol, or cd /home/student/molevol).&lt;br /&gt;
 cd /path/to/molevol&lt;br /&gt;
 mkdir modelselect&lt;br /&gt;
 cd modelselect&lt;br /&gt;
 cp ../data/gp120.fasta ./gp120.fasta&lt;br /&gt;
 cp ../data/codeml.ctl ./codeml.ctl&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Have a look at the DNA data file:&#039;&#039;&#039;&lt;br /&gt;
 nedit gp120.fasta &amp;amp;&lt;br /&gt;
: The file contains several DNA sequences from HIV-1, subtype B. The sequences are approximately 500 bp long, and correspond to a region surrounding the hypervariable V3 region in the gene encoding gp120. Close the nedit window when you&#039;ve had a look.&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
== Protein-guided alignment of coding DNA ==&lt;br /&gt;
&lt;br /&gt;
: There are three reasons why it is preferable to align coding DNA at the amino acid level rather than directly as nucleotides. First, the DNA alphabet has only four letters compared to 20 amino acids, which means that even unrelated DNA sequences will share roughly 25% identity by chance alone, making it much harder to distinguish true homology from noise. Second, protein alignments benefit from empirical substitution matrices such as BLOSUM-62, which capture the fact that amino acid replacements tend to be conservative. Equivalent matrices for DNA are far less informative because nucleotide substitution patterns vary strongly between genes and organisms. Third, synonymous substitutions accumulate faster than non-synonymous ones, so DNA sequences diverge more rapidly than the proteins they encode. Taken together, these factors mean that the phylogenetic signal in a DNA alignment erodes much faster than in the corresponding protein alignment.&lt;br /&gt;
&lt;br /&gt;
: However, while protein-level alignment gives us the most reliable homology signal, it discards exactly the information we need for studying selection: the synonymous substitutions. Estimating the ratio of non-synonymous to synonymous substitution rates (dN/dS) requires a codon-level alignment where analogous codon positions are properly lined up. We would therefore like to construct a multiple alignment at the DNA level, but using information at the protein level, and the RevTrans server does exactly that.&lt;br /&gt;
&lt;br /&gt;
: &#039;&#039;&#039;RevTrans&#039;&#039;&#039; takes as input an unaligned set of DNA sequences, automatically translates them to the equivalent amino acid sequences, constructs a multiple alignment of the protein sequences, and finally uses the protein alignment as a template for constructing a multiple DNA alignment. Because each amino acid maps back to its original codon, gaps are always inserted in groups of three and codon boundaries are preserved throughout the alignment. This is essential for the dN/dS analyses we will carry out in this exercise: if codon positions are misaligned, synonymous and non-synonymous changes cannot be correctly distinguished.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Construct RevTrans alignment&#039;&#039;&#039;&lt;br /&gt;
:* Open RevTrans server page: https://services.healthtech.dtu.dk/services/RevTrans-2.0/&lt;br /&gt;
:* On the RevTrans page: Choose the file gp120.fasta as input (or copy and paste the sequence into the sequence window)&lt;br /&gt;
:* Click the &amp;quot;Submit query&amp;quot; button&lt;br /&gt;
:* When the alignment is done you may have to click link named &amp;quot;here&amp;quot; to go to results page&lt;br /&gt;
:* Download DNA alignment, by right-clicking the link for &amp;quot;Download alignment in FASTA format&amp;quot;, and choosing &amp;quot;Save link as...&amp;quot; (save file under the name gp120align.fasta and make sure to save the file in the directory modelselect).&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Convert alignment to NEXUS format&#039;&#039;&#039;&lt;br /&gt;
: Convert the fasta file to NEXUS format and save file in the modelselect directory under the name gp120.nexus&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
== Model selection using AIC and jModelTest2 ==&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Question 1&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
: As part of the present analysis we are going to build a phylogenetic tree based on the DNA alignment constructed above. We will construct the tree using maximum likelihood, but to do that we first have to decide which substitution model we want to use. Specifically, we are interested in selecting a model that achieves a good balance between fit to the data and number of parameters. We will investigate this issue by fitting a set of 56 different models to our data and then comparing them using AIC.&lt;br /&gt;
&lt;br /&gt;
: In the first part of the exercise, however, you will only compare three closely related models by hand. The purpose of this restricted comparison is not primarily to identify the globally best model, but to understand how AIC, ΔAIC, and Akaike weights are computed and interpreted. Because only three models are included in this manual comparison, the weights you compute will describe relative support &#039;&#039;&#039;within this restricted set only&#039;&#039;&#039;.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Start jmodeltest2&#039;&#039;&#039;&lt;br /&gt;
 jmodeltest&lt;br /&gt;
&#039;&#039;&#039;Load data set&#039;&#039;&#039;&lt;br /&gt;
:* File -&amp;gt; Load DNA alignment -&amp;gt; File Format -&amp;gt; Select &amp;quot;All files&amp;quot;&lt;br /&gt;
:* Navigate to gp120.nexus and load it&lt;br /&gt;
&#039;&#039;&#039;Fit 56 models&#039;&#039;&#039;&lt;br /&gt;
:* Analysis -&amp;gt; Compute likelihood scores&lt;br /&gt;
:* Select &amp;quot;7&amp;quot; under &amp;quot;Number of substitution schemes&amp;quot;&lt;br /&gt;
:* Select &amp;quot;Fixed BIONJ-JC&amp;quot; under &amp;quot;Base tree for likelihood calculations&amp;quot;&lt;br /&gt;
:* Click &amp;quot;Compute likelihoods&amp;quot;&lt;br /&gt;
&lt;br /&gt;
: This causes jmodeltest2 to perform the following actions: first a neighbor joining tree is constructed using the Jukes and Cantor model. Then the tree is fixed and used as the basis for fitting a set of 56 different models to the data. For each model, the estimated model parameters and the negative log-likelihood are recorded. In addition to varying sets of substitution rate parameters (JC, K2P, ...), some of these models also include extra parameters that take into account the presence of different rates between sites. This is done in two ways: (1) by fitting a gamma distribution of rates (&amp;quot;+G&amp;quot;), and (2) by allowing for a proportion of constant (&amp;quot;invariable&amp;quot;) sites (&amp;quot;+I&amp;quot;).&lt;br /&gt;
&lt;br /&gt;
: Wait until jmodeltest2 is done fitting all 56 models (this will take a little while depending on your computer).&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Inspect result&#039;&#039;&#039;&lt;br /&gt;
:* Results -&amp;gt; Show results table&lt;br /&gt;
: For each model this table lists the negative log-likelihood (&amp;quot;-lnL&amp;quot;), the number of parameters (&amp;quot;p&amp;quot; - note this includes branch lengths), and estimates of all model parameters (excluding branch lengths).&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Manually compute Akaike weights for three substitution models&#039;&#039;&#039;&lt;br /&gt;
: Use AIC-based model comparison to investigate which of the following three substitution models are best supported as descriptions of how the sequences have evolved:&lt;br /&gt;
:* Jukes and Cantor with fraction of invariant sites (JC+I)&lt;br /&gt;
:* Jukes and Cantor with gamma-distributed rates over sites (JC+G)&lt;br /&gt;
:* Jukes and Cantor with invariant sites and gamma-distributed rates (JC+I+G)&lt;br /&gt;
: Before you can do the computation you need to know the log likelihood and the number of parameters for each model. Locate these values in the table for the JC+I, JC+G, and JC+I+G models, and write them down. Close the window with the result table when you are done.&lt;br /&gt;
&lt;br /&gt;
: Make sure to get the signs right: the values reported in the table are -lnL values, so you will need to reverse the sign to get the lnL (the lnL values you write down should be negative).&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Question: &#039;&#039;&#039;Use the recipe above to compute AIC, ΔAIC, and Akaike weight for the three models. Report the results in a table similar to the one shown above, and verify that the three weights sum to 1 (up to rounding error).&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Question 2&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
: Based on the Akaike weights, which of the three models has the strongest support? Is the support strongly concentrated on one model, or is there still substantial support for one or both of the others?&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Question 3&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
: What you just did manually for JC+I, JC+G and JC+I+G, jmodeltest2 can do automatically for the full set of 56 fitted models. Specifically, it uses the list of negative log likelihoods and parameter counts in the table to compute AIC values and Akaike weights for the full candidate set:&lt;br /&gt;
:* Analysis -&amp;gt; Do AIC calculations -&amp;gt;&lt;br /&gt;
:* Select &amp;quot;Write PAUP* block&amp;quot;&lt;br /&gt;
:* click &amp;quot;Do AIC calculations&amp;quot;&lt;br /&gt;
:* Results -&amp;gt; Show results table&lt;br /&gt;
:* Select &amp;quot;AIC&amp;quot; tab&lt;br /&gt;
:* &#039;&#039;&#039;IMPORTANT:&#039;&#039;&#039; SHIFT+click on the header of the &amp;quot;weight&amp;quot; column. This sorts the rows according to model weight, in descending order.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Question: &#039;&#039;&#039;What model was selected by modeltest based on the AIC values? (This will be the model with the highest weight - and lowest AIC - and will be in the first row of the results table after SHIFT-clicking the &amp;quot;weight&amp;quot; header).&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Question 4&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
: In the same AIC results table, now click (not SHIFT+click) the header of the &amp;quot;-lnL&amp;quot; column. In this table, a normal click sorts in ascending order, whereas SHIFT+click sorts in descending order. The model at the top will therefore be the one with the smallest value of &amp;quot;-lnL&amp;quot; (equivalently: the highest likelihood). Compare this model to the one with the highest Akaike weight.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Question: &#039;&#039;&#039;Is the model with the highest likelihood also the model with the highest Akaike weight? If not, explain briefly why these two rankings can differ.&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
== Construction of phylogenetic tree using PAUP ==&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Question 5&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
: Close the results table. In the main window you should now scroll up to the lines giving PAUP commands that implement the selected model. The command is enclosed between &amp;quot;BEGIN PAUP&amp;quot; and &amp;quot;END;&amp;quot; and should look something like this:&lt;br /&gt;
 Lset Base=(0.4064, [...]&lt;br /&gt;
: You will need to copy this command to a PAUP session in the next step.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Start PAUP&#039;&#039;&#039;&lt;br /&gt;
 paup&lt;br /&gt;
: Above, you used jmodeltest2 to select the most suitable substitution model for the present data set. You will now use this model to construct a maximum likelihood tree. You will use PAUP for this purpose. (Note: it is possible to create a maximum likelihood tree, or even a model-averaged tree, directly from jmodeltest2, but we will instead do it in PAUP in order to more clearly see each step that is taken.)&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Load alignment:&#039;&#039;&#039;&lt;br /&gt;
 execute gp120.nexus&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Set tree-building criterion to maximum likelihood&#039;&#039;&#039;&lt;br /&gt;
 set criterion=likelihood&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Set model parameters to winning estimates&#039;&#039;&#039;&lt;br /&gt;
: Above, you located a set of lines in the jmodeltest2 output giving a PAUP command that sets the model parameters to the estimates found for the winning model. Copy and paste this lset command (without the BEGIN and END parts) into the window where PAUP is running.&lt;br /&gt;
&lt;br /&gt;
 PASTE LSET COMMAND FROM MODELTEST RUN HERE&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Find best tree using selected model&#039;&#039;&#039;&lt;br /&gt;
: Still in the PAUP window, enter the following command:&lt;br /&gt;
 hsearch swap=tbr start=nj&lt;br /&gt;
: This command causes PAUP to perform a heuristic search for the best maximum likelihood tree. Once an initial tree has been constructed, the heuristic search proceeds by rearrangements of the &amp;quot;tree bisection and reconnection&amp;quot; type (TBR). We are using the model selected by modeltest, and the parameter estimates found by modeltest for that model. You could also choose to estimate all the model parameters as part of this step (i.e., at the same time as finding the best tree), but fixing them improves speed tremendously. Finding the best tree should take at most a few minutes.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Save best tree to file&#039;&#039;&#039;&lt;br /&gt;
 savetrees format=newick brlens=yes file=gp120tree.phy from=1 to=1&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Quit program&#039;&#039;&#039;&lt;br /&gt;
 quit&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Have a look at the tree:&#039;&#039;&#039;&lt;br /&gt;
: You have now produced an unrooted tree of the HIV sequences and saved it in the file gp120tree.phy. Note that in this exercise we will not be interested in the tree as such - our focus is instead on finding positive selection at a subset of codon positions, and the tree is just something we need in order to fit the different codon models to the data. If you want to see the tree, you can do so with the following command:&lt;br /&gt;
 figtree gp120tree.phy &amp;amp;&lt;br /&gt;
: There is no meaningful root placed on this tree, so you may want to choose the unrooted view (the third icon in the Layout section of the FigTree window). Close the FigTree window when you have had a look.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Question: &#039;&#039;&#039;Which substitution model did you use for the PAUP tree search, and what was the negative log likelihood of the best tree found under this model?&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
== Model selection and tree reconstruction in one step using IQ-TREE 3 ==&lt;br /&gt;
&lt;br /&gt;
: In real-life phylogenetic analysis one would often use a modern program such as &#039;&#039;&#039;IQ-TREE 3&#039;&#039;&#039;, which can carry out model selection and maximum likelihood tree reconstruction in a single analysis.&lt;br /&gt;
&lt;br /&gt;
: Run IQ-TREE 3 on the alignment using the following command:&lt;br /&gt;
 iqtree3 -s gp120.nexus -m MFP -nt AUTO&lt;br /&gt;
&lt;br /&gt;
: Here, &amp;quot;-m MFP&amp;quot; tells IQ-TREE 3 to perform automatic model selection using &amp;quot;ModelFinder Plus&amp;quot; before reconstructing the tree.&lt;br /&gt;
&lt;br /&gt;
: When the run has finished, inspect the output written to the screen and to the file ending in &amp;quot;.iqtree&amp;quot;. Locate the substitution model selected by IQ-TREE 3 and the log likelihood of the best tree.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Question 6&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
: Which substitution model was selected by IQ-TREE 3, and what was the log likelihood of the best tree? Compare this to the earlier jModelTest2 + PAUP analysis. Is the selected model exactly the same? Is the likelihood exactly the same? Briefly suggest one reason why the two analyses might not give identical results.&lt;br /&gt;
&lt;br /&gt;
---&lt;br /&gt;
&lt;br /&gt;
== Detection of positively selected sites in gp120 ==&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Question 7&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
: There is much more to phylogenetic analyses than merely reconstructing trees. One interesting result of probabilistic methods, is that the parameters of a model will have their values estimated as part of the optimization procedure. This means that once such a model has been fitted to the data, it is possible to investigate these estimated parameter values to learn features about the evolutionary history of the sequences under investigation. In the present example we will focus on investigating whether we can find positively selected sites in our data set, defined as sites where the dN/dS ratio is larger than 1. We do that by using a codon substitution model where the dN/dS ratio is one of its parameters.&lt;br /&gt;
&lt;br /&gt;
: A further strength of the probabilistic approach is that you get a probabilistic measure of how well any model fits the data. This means you can use a stringent approach to determine which model fits the data best. In this framework one uses likelihoods (probabilities of data given model) to determine which model fits the data best. As you saw above, it is for instance possible to compute AIC values and weights (model probabilities) from the likelihood values of fitted models, Since each model essentially corresponds to a hypothesis about the evolutionary history of the data, we can thus use a stringent statistical approach to decide which hypothesis best describes our data.&lt;br /&gt;
&lt;br /&gt;
: In outline, you will now use the following steps to investigate whether there is any evidence for positively selected codons in your data set:&lt;br /&gt;
&lt;br /&gt;
:* Fit model M1, which assumes there are two classes of codons in the sequence: some with dN/dS &amp;lt; 1, some with dN/dS=1.&lt;br /&gt;
:* Fit model M2, which assumes 3 distinct classes of codons: two with dN/dS ratios as for M1, and one extra class with dN/dS &amp;gt; 1.&lt;br /&gt;
:* Assess the strength of evidence for the two models using AIC-based model probabilities&lt;br /&gt;
:* If M2 is better: identify the positively selected codons&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Inspect the parameter file&#039;&#039;&#039;&lt;br /&gt;
 nedit codeml.ctl &amp;amp;&lt;br /&gt;
: The file &amp;quot;codeml.ctl&amp;quot; contains several settings that are relevant for running the program &#039;&#039;&#039;codeml&#039;&#039;&#039;. Find the following lines and ensure that the file contains these values:&lt;br /&gt;
 &#039;&#039;&#039;seqfile =  gp120align.fasta&#039;&#039;&#039;:  name of alignment file&lt;br /&gt;
 &#039;&#039;&#039;treefile =  gp120tree.phy&#039;&#039;&#039;: name of tree file&lt;br /&gt;
 &#039;&#039;&#039;seqtype = 1&#039;&#039;&#039;: tells the program that our data consists of coding DNA.&lt;br /&gt;
 &#039;&#039;&#039;NSsites = 1 2&#039;&#039;&#039; : tells the program to analyze models M1 and M2.&lt;br /&gt;
 &#039;&#039;&#039;cleandata = 0&#039;&#039;&#039;: tells the program to keep positions with gaps.&lt;br /&gt;
&lt;br /&gt;
: The settings entered by us will cause codeml to analyze two hypotheses about dN/dS ratios. M1 says there are two classes of codons with different dN/dS ratios in the sequence: one class with dN/dS &amp;lt; 1 (codons under purifying or negative selection), and one class with dN/dS=1 (no selection - neutrally evolving sites). M2 says there are 3 distinct dN/dS ratios for different sites in the sequence: one class with dN/dS &amp;lt; 1, one class with dN/dS=1 (these are the same type of classes as for M1), and one class with dN/dS &amp;gt; 1 (corresponding to sites under positive selection). The value of the dN/dS ratios (for those classes that have dN/dS &amp;lt; 1 or dN/dS &amp;gt; 1), the fraction of sites belonging to each class, and the position of sites belonging to each class, are unknown at first and will be determined during the analysis.&lt;br /&gt;
&lt;br /&gt;
: (Regarding &amp;quot;cleandata&amp;quot; setting: &amp;quot;cleandata = 1&amp;quot; would cause codeml to discard any alignment columns with gaps or ambiguity symbols. For alignments with many sequences this can lead to discarding the majority of sites and I would therefore recommend not using that setting. At the same time you do want to be wary of columns where most sequences have gaps, since inference of selection for these will be very uncertain. It might be better to discard such columns from an alignment before doing the codeml analysis).&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Start the analysis&#039;&#039;&#039;&lt;br /&gt;
 codeml&lt;br /&gt;
: This will start the codeml program using the settings in the file codeml.ctl. Depending on your computer, this will take some minutes to finish. (You may be able to see how the optimization procedure results in progressively better fits: the likelihood increases, meaning that negative log-likelihood decreases, as the fit improves).&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Inspect result file:&#039;&#039;&#039;&lt;br /&gt;
: Wait for the run to finish, and then look at the result file:&lt;br /&gt;
 nedit selection.results &amp;amp;&lt;br /&gt;
: This file contains a wealth of information concerning your analysis. You may want to turn off line-wrapping to more clearly see the structure of the file (there will be some long lines that will otherwise wrap around). The top part of the file gives an overview of your sequences, codon usage and nucleotide frequencies. You can ignore this information for now, and move on to the interesting part, namely the model likelihoods and parameter values:&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Find likelihood, and number of free parameters for model M1&#039;&#039;&#039;&lt;br /&gt;
 Search ==&amp;gt; Find... ==&amp;gt; enter &amp;quot;Model 1&amp;quot; and click Find&lt;br /&gt;
: You are now in the region of the result file where the model likelihoods and parameter estimates are noted. Now, locate a line that looks a bit like the one shown below:&lt;br /&gt;
 lnL(ntime: 72  np: 74):  -4242.470345     +0.000000&lt;br /&gt;
: Identify the number of &amp;quot;free parameters&amp;quot;, K, used in model M1: This is indicated by &amp;quot;np&amp;quot;, and is 74 in the example shown above (most of these parameters are branch lengths in the tree; specifically, the number of branch length parameters is indicated by &amp;quot;ntime&amp;quot;, and is 72 in this example). Also note the log-likelihood of the fitted model. This is the number right after the parenthesis, and is -4242.470345 in the example here.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Question: &#039;&#039;&#039;What are the values of K and lnL for model M1?&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Question 8&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Find dn/dS ratios and codon class proportions for model M1:&#039;&#039;&#039;&lt;br /&gt;
: Scroll down a few lines until you get to a small table similar to this:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
      dN/dS for site classes (K=2)&lt;br /&gt;
      p:   0.75111  0.24889&lt;br /&gt;
      w:   0.06583  1.00000&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
: This gives a summary of the dN/dS ratios that were found in the data set. The line starting &amp;quot;w:&amp;quot; lists the two dN/dS ratios that were found (in this case 0.06583 and 1.00000 - the last one was pre-specified by us as part of the model and was therefore not a free parameter). In this context &amp;quot;w&amp;quot; means &amp;quot;omega&amp;quot;, which is the symbol typically used to represent the dN/dS rate ratio. The line starting p: gives the proportion of codon sites belonging to each of the dN/dS ratio classes (in the example above approximately 75% belong to the first class , while 25% of all sites belong to the class having dN/dS=1.00000).&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Question: &#039;&#039;&#039;What are the dN/dS value (w) and proportion (p) of sites for both classes. Report the following values: p(class1), w(class1), p(class2), w(class2)&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Question 9&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Find likelihood, and K for model M2&#039;&#039;&#039;&lt;br /&gt;
: Scroll past the M1 output until you get to the results for model M2.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Question: &#039;&#039;&#039;What are the values of K and lnL for model M2?&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Question 10&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Find dn/dS ratios and codon class proportions for model M2:&#039;&#039;&#039;&lt;br /&gt;
: Now, scroll down a few lines until you get to a small table similar to the one you examined for M1 before. For this model there are 3 separate classes of codons.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Question: &#039;&#039;&#039;What are the dN/dS value (w) and proportion (p) of sites for all three classes? Report these values: p(class1), w(class1), p(class2), w(class2), p(class3), w(class3)&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Question 11&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Assess strength of evidence for models M1 and M2:&#039;&#039;&#039;&lt;br /&gt;
: M2 will always have a better (higher) log-likelihood than model M1 because M2 has more free parameters, and M1 is nested within M2. You should now use the recipe given above to compute AIC values and model probabilities for M1 and M2.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Question: &#039;&#039;&#039; Report: AIC, ΔAIC, weight (model probability) for M1 and M2&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Question 12: &#039;&#039;&#039; Is M2 better than M1?&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Question 13&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Examine list of positively selected sites&#039;&#039;&#039;&lt;br /&gt;
: If your M2 is clearly better than M1 (I firmly believe it should be if you did things according to instructions...), then you have evidence for the existence of positively selected sites in the gp120 gene. Now, scroll down to the end of the result file and locate a list similar to the one below. Note: This is the &amp;quot;Bayes Empirical Bayes&amp;quot; table, not the &amp;quot;Naive Empirical Bayes&amp;quot; table. &lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Bayes Empirical Bayes (BEB) analysis&lt;br /&gt;
Positively selected sites&lt;br /&gt;
&lt;br /&gt;
         Prob(w&amp;gt;1)     mean w&lt;br /&gt;
&lt;br /&gt;
    25 A 0.959*        3.133 +- 0.769&lt;br /&gt;
    27 P 0.906         2.990 +- 0.877&lt;br /&gt;
    56 K 0.987*        3.197 +- 0.687&lt;br /&gt;
    59 V 0.915         3.032 +- 0.873&lt;br /&gt;
    78 R 0.637         2.351 +- 1.129&lt;br /&gt;
    88 K 0.573         2.148 +- 1.077&lt;br /&gt;
    95 V 0.925         3.046 +- 0.843&lt;br /&gt;
    ...&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
: It is not important what the distinction is in this context, but very briefly NEB ignores the fact that there is uncertainty about  maximum likelihood estimates, especially for smaller data sets (for instance w for some codon is perhaps not exactly 3.046, but could be in a region around that value), while [https://pubmed.ncbi.nlm.nih.gov/15689528/ BEB accounts for that uncertainty].&lt;br /&gt;
: This gives you a list of which residues (if any) that were found to belong to the positively selected dN/dS-class. Also listed is the probability that the site really is in the codon class where dN/dS &amp;gt; 1, and a weighted average of the w at the site. Using only DNA sequences you have now identified likely epitopes on the gp120 protein.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Question: &#039;&#039;&#039;List all sites having more than 95% probability of belonging to the positively selected class&lt;/div&gt;</summary>
		<author><name>Gorm</name></author>
	</entry>
	<entry>
		<id>https://teaching.healthtech.dtu.dk/22115/index.php?title=Model_selection&amp;diff=268</id>
		<title>Model selection</title>
		<link rel="alternate" type="text/html" href="https://teaching.healthtech.dtu.dk/22115/index.php?title=Model_selection&amp;diff=268"/>
		<updated>2026-04-15T08:13:51Z</updated>

		<summary type="html">&lt;p&gt;Gorm: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;This exercise is part of the course  [[22115_-_Computational_Molecular_Evolution|Computational Molecular Evolution (22115)]].&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Overview ==&lt;br /&gt;
&lt;br /&gt;
: In this exercise you are going to investigate features of HIV-1 evolution. You will do this by analyzing a large set of env-genes from HIV-1, subtype B. specifically, the DNA sequences analyzed here correspond to a region surrounding the hypervariable V3 region of the gp120 protein.&lt;br /&gt;
&lt;br /&gt;
: Like other retroviruses, particles of HIV are made up of 2 copies of a single-stranded RNA genome packaged inside a protein core, or capsid. The core particle also contains viral proteins that are essential for the early steps of the virus life cycle, such as reverse transcription and integration. A lipid envelope, derived from the infected cell, surrounds the core particle. Embedded in this envelope are the surface glycoproteins of HIV: gp120 and gp41. The gp120 protein is crucial for binding of the virus particle to target cells, while gp41 is important for the subsequent fusion event. It is the specific affinity of gp120 for the CD4 protein that targets HIV to those cells of the immune system that express CD4 on their surface (e.g., T-helper lymphocytes, monocytes, and macrophages).&lt;br /&gt;
&lt;br /&gt;
: The role gp120 plays in infection and the fact that it is situated on the surface of the HIV particle, means it is an obvious target for the immune response. That means that there may be a considerable selective pressure on gp120 for creating immune-escape mutants, where amino acids in the gp120 epitopes have been substituted. In this exercise you will construct a maximum likelihood tree that we will subsequently use to investigate whether you can detect such a selective pressure on parts of gp120, again using maximum likelihood methods.&lt;br /&gt;
&lt;br /&gt;
: One major goal with the exercise is to introduce you to statistically based methods for assessing the strength of evidence for a set of alternative hypotheses about some biological system of interest. The model selection method we will use is AIC (Akaike Information Criterion), based on which you will compute model probabilities. A second goal is to make you aware that phylogenetic analysis is not only about constructing trees, but that it is also a useful framework for analyzing biological questions more generally.&lt;br /&gt;
&lt;br /&gt;
: Specifically, you will&lt;br /&gt;
&lt;br /&gt;
:# perform a multiple alignment of gp120 DNA sequences taking protein-level information into account (using revtrans).&lt;br /&gt;
:# select a suitable nucleotide substitution model (using jmodeltest2)&lt;br /&gt;
:# construct a phylogenetic tree (using PAUP).&lt;br /&gt;
:# try to detect positively selected sites in gp120 (using PAML).&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
== Recipe for computing AIC values and model probabilities ==&lt;br /&gt;
&lt;br /&gt;
: Later in today&#039;s exercise you will be asked to compute AIC values and model probabilities. Return to this section and follow the instructions when you need to do so.&lt;br /&gt;
&lt;br /&gt;
:# Fit a set of models to your data, note the maximized log likelihoods (lnL) and the number of free parameters (K) for each model in the investigated set. The models you fit should represent a plausible and comprehensive set of hypotheses about your data.&lt;br /&gt;
:# Compute AIC for each of the models: &#039;&#039;&#039;AIC = -2 x lnL + 2K&#039;&#039;&#039;.  &amp;lt;br&amp;gt;For example: a model with lnL = -2010 and K = 5 will have AIC = -2 x -2010 + 2 x 5 = 4030.&lt;br /&gt;
:# Identify the model with the smallest AIC (this is the best model in the set). We will call the AIC for this model &#039;&#039;&#039;&amp;quot;AICmin&amp;quot;&#039;&#039;&#039;.&lt;br /&gt;
:# Compute the &amp;quot;ΔAIC&amp;quot; values for each model: &#039;&#039;&#039;ΔAIC = AIC - AICmin&#039;&#039;&#039; &amp;lt;br&amp;gt;For each model subtract the minimum AIC value. The best model will have a ΔAIC of zero. The rest of the models will have positive ΔAICs.&lt;br /&gt;
:# For each model compute the following quantity: &#039;&#039;&#039;numerator = exp(-0.5 x ΔAIC)&#039;&#039;&#039; &amp;lt;br&amp;gt;For example, a model with ΔAIC=4.2 will have numerator = exp(-0.5 x 4.2) = exp(-2.1) = 0.1225. Also compute the &#039;&#039;&#039;sum&#039;&#039;&#039; of the numerator values for all models.&lt;br /&gt;
:# Finally, the model probabilities (also called model weights) for each model are found as:&amp;lt;br&amp;gt; &#039;&#039;&#039;P(model) = numerator / sum&#039;&#039;&#039; &amp;lt;br&amp;gt;For example, if sum = 3.75 and a model has numerator = 1.3, then it has P(model) = 1.3 / 3.75 = 0.35&lt;br /&gt;
&lt;br /&gt;
: You may want to keep track of the computations by constructing a table along the following lines:&lt;br /&gt;
[[File:Molevol-Downloads-aictable.png|700px]]&lt;br /&gt;
&lt;br /&gt;
: Note that model probabilities can also be computed using Bayesian methods. One advantage of Bayesian methods over AIC is that instead of relying on a point estimate, uncertainty about parameter values is accounted for by integrating over all possible values (typically using MCMC).&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
== Getting started ==&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Create working directory, copy files&#039;&#039;&#039;&lt;br /&gt;
: In the command below: Instead of /path/to/molevol enter the path to the directory where you have placed your course files (for instance cd /Users/bob/Documents/molevol, or cd /home/student/molevol).&lt;br /&gt;
 cd /path/to/molevol&lt;br /&gt;
 mkdir modelselect&lt;br /&gt;
 cd modelselect&lt;br /&gt;
 cp ../data/gp120.fasta ./gp120.fasta&lt;br /&gt;
 cp ../data/codeml.ctl ./codeml.ctl&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Have a look at the DNA data file:&#039;&#039;&#039;&lt;br /&gt;
 nedit gp120.fasta &amp;amp;&lt;br /&gt;
: The file contains several DNA sequences from HIV-1, subtype B. The sequences are approximately 500 bp long, and correspond to a region surrounding the hypervariable V3 region in the gene encoding gp120. Close the nedit window when you&#039;ve had a look.&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
== Protein-guided alignment of coding DNA ==&lt;br /&gt;
&lt;br /&gt;
: There are three reasons why it is preferable to align coding DNA at the amino acid level rather than directly as nucleotides. First, the DNA alphabet has only four letters compared to 20 amino acids, which means that even unrelated DNA sequences will share roughly 25% identity by chance alone, making it much harder to distinguish true homology from noise. Second, protein alignments benefit from empirical substitution matrices such as BLOSUM-62, which capture the fact that amino acid replacements tend to be conservative. Equivalent matrices for DNA are far less informative because nucleotide substitution patterns vary strongly between genes and organisms. Third, synonymous substitutions accumulate faster than non-synonymous ones, so DNA sequences diverge more rapidly than the proteins they encode. Taken together, these factors mean that the phylogenetic signal in a DNA alignment erodes much faster than in the corresponding protein alignment.&lt;br /&gt;
&lt;br /&gt;
: However, while protein-level alignment gives us the most reliable homology signal, it discards exactly the information we need for studying selection: the synonymous substitutions. Estimating the ratio of non-synonymous to synonymous substitution rates (dN/dS) requires a codon-level alignment where analogous codon positions are properly lined up. We would therefore like to construct a multiple alignment at the DNA level, but using information at the protein level, and the RevTrans server does exactly that.&lt;br /&gt;
&lt;br /&gt;
: &#039;&#039;&#039;RevTrans&#039;&#039;&#039; takes as input an unaligned set of DNA sequences, automatically translates them to the equivalent amino acid sequences, constructs a multiple alignment of the protein sequences, and finally uses the protein alignment as a template for constructing a multiple DNA alignment. Because each amino acid maps back to its original codon, gaps are always inserted in groups of three and codon boundaries are preserved throughout the alignment. This is essential for the dN/dS analyses we will carry out in this exercise: if codon positions are misaligned, synonymous and non-synonymous changes cannot be correctly distinguished.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Construct RevTrans alignment&#039;&#039;&#039;&lt;br /&gt;
:* Open RevTrans server page: https://services.healthtech.dtu.dk/services/RevTrans-2.0/&lt;br /&gt;
:* On the RevTrans page: Choose the file gp120.fasta as input (or copy and paste the sequence into the sequence window)&lt;br /&gt;
:* Click the &amp;quot;Submit query&amp;quot; button&lt;br /&gt;
:* When the alignment is done you may have to click link named &amp;quot;here&amp;quot; to go to results page&lt;br /&gt;
:* Download DNA alignment, by right-clicking the link for &amp;quot;Download alignment in FASTA format&amp;quot;, and choosing &amp;quot;Save link as...&amp;quot; (save file under the name gp120align.fasta and make sure to save the file in the directory modelselect).&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Convert alignment to NEXUS format&#039;&#039;&#039;&lt;br /&gt;
: Convert the fasta file to NEXUS format and save file in the modelselect directory under the name gp120.nexus&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
== Model selection using AIC and jModelTest2 ==&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Question 1&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
: As part of the present analysis we are going to build a phylogenetic tree based on the DNA alignment constructed above. We will construct the tree using maximum likelihood, but to do that we first have to decide which substitution model we want to use. Specifically, we are interested in selecting a model that achieves a good balance between fit to the data and number of parameters. We will investigate this issue by fitting a set of 56 different models to our data and then comparing them using AIC.&lt;br /&gt;
&lt;br /&gt;
: In the first part of the exercise, however, you will only compare three closely related models by hand. The purpose of this restricted comparison is not primarily to identify the globally best model, but to understand how AIC, ΔAIC, and Akaike weights are computed and interpreted. Because only three models are included in this manual comparison, the weights you compute will describe relative support &#039;&#039;&#039;within this restricted set only&#039;&#039;&#039;.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Start jmodeltest2&#039;&#039;&#039;&lt;br /&gt;
 jmodeltest&lt;br /&gt;
&#039;&#039;&#039;Load data set&#039;&#039;&#039;&lt;br /&gt;
:* File -&amp;gt; Load DNA alignment -&amp;gt; File Format -&amp;gt; Select &amp;quot;All files&amp;quot;&lt;br /&gt;
:* Navigate to gp120.nexus and load it&lt;br /&gt;
&#039;&#039;&#039;Fit 56 models&#039;&#039;&#039;&lt;br /&gt;
:* Analysis -&amp;gt; Compute likelihood scores&lt;br /&gt;
:* Select &amp;quot;7&amp;quot; under &amp;quot;Number of substitution schemes&amp;quot;&lt;br /&gt;
:* Select &amp;quot;Fixed BIONJ-JC&amp;quot; under &amp;quot;Base tree for likelihood calculations&amp;quot;&lt;br /&gt;
:* Click &amp;quot;Compute likelihoods&amp;quot;&lt;br /&gt;
&lt;br /&gt;
: This causes jmodeltest2 to perform the following actions: first a neighbor joining tree is constructed using the Jukes and Cantor model. Then the tree is fixed and used as the basis for fitting a set of 56 different models to the data. For each model, the estimated model parameters and the negative log-likelihood are recorded. In addition to varying sets of substitution rate parameters (JC, K2P, ...), some of these models also include extra parameters that take into account the presence of different rates between sites. This is done in two ways: (1) by fitting a gamma distribution of rates (&amp;quot;+G&amp;quot;), and (2) by allowing for a proportion of constant (&amp;quot;invariable&amp;quot;) sites (&amp;quot;+I&amp;quot;).&lt;br /&gt;
&lt;br /&gt;
: Wait until jmodeltest2 is done fitting all 56 models (this will take a little while depending on your computer).&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Inspect result&#039;&#039;&#039;&lt;br /&gt;
:* Results -&amp;gt; Show results table&lt;br /&gt;
: For each model this table lists the negative log-likelihood (&amp;quot;-lnL&amp;quot;), the number of parameters (&amp;quot;p&amp;quot; - note this includes branch lengths), and estimates of all model parameters (excluding branch lengths).&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Manually compute Akaike weights for three substitution models&#039;&#039;&#039;&lt;br /&gt;
: Use AIC-based model comparison to investigate which of the following three substitution models are best supported as descriptions of how the sequences have evolved:&lt;br /&gt;
:* Jukes and Cantor with fraction of invariant sites (JC+I)&lt;br /&gt;
:* Jukes and Cantor with gamma-distributed rates over sites (JC+G)&lt;br /&gt;
:* Jukes and Cantor with invariant sites and gamma-distributed rates (JC+I+G)&lt;br /&gt;
: Before you can do the computation you need to know the log likelihood and the number of parameters for each model. Locate these values in the table for the JC+I, JC+G, and JC+I+G models, and write them down. Close the window with the result table when you are done.&lt;br /&gt;
&lt;br /&gt;
: Make sure to get the signs right: the values reported in the table are -lnL values, so you will need to reverse the sign to get the lnL (the lnL values you write down should be negative).&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Question: &#039;&#039;&#039;Use the recipe above to compute AIC, ΔAIC, and Akaike weight for the three models. Report the results in a table similar to the one shown above, and verify that the three weights sum to 1 (up to rounding error).&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Question 2&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
: Based on the Akaike weights, which of the three models has the strongest support? Is the support strongly concentrated on one model, or is there still substantial support for one or both of the others?&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Question 3&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
: What you just did manually for JC+I, JC+G and JC+I+G, jmodeltest2 can do automatically for the full set of 56 fitted models. Specifically, it uses the list of negative log likelihoods and parameter counts in the table to compute AIC values and Akaike weights for the full candidate set:&lt;br /&gt;
:* Analysis -&amp;gt; Do AIC calculations -&amp;gt;&lt;br /&gt;
:* Select &amp;quot;Write PAUP* block&amp;quot;&lt;br /&gt;
:* click &amp;quot;Do AIC calculations&amp;quot;&lt;br /&gt;
:* Results -&amp;gt; Show results table&lt;br /&gt;
:* Select &amp;quot;AIC&amp;quot; tab&lt;br /&gt;
:* &#039;&#039;&#039;IMPORTANT:&#039;&#039;&#039; SHIFT+click on the header of the &amp;quot;weight&amp;quot; column. This sorts the rows according to model weight, in descending order.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Question: &#039;&#039;&#039;What model was selected by modeltest based on the AIC values? (This will be the model with the highest weight - and lowest AIC - and will be in the first row of the results table after SHIFT-clicking the &amp;quot;weight&amp;quot; header).&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Question 4&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
: In the same AIC results table, now click (not SHIFT+click) the header of the &amp;quot;-lnL&amp;quot; column. In this table, a normal click sorts in ascending order, whereas SHIFT+click sorts in descending order. The model at the top will therefore be the one with the smallest value of &amp;quot;-lnL&amp;quot; (equivalently: the highest likelihood). Compare this model to the one with the highest Akaike weight.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Question: &#039;&#039;&#039;Is the model with the highest likelihood also the model with the highest Akaike weight? If not, explain briefly why these two rankings can differ.&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
== Construction of phylogenetic tree using PAUP ==&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Question 5&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
: Close the results table. In the main window you should now scroll up to the lines giving PAUP commands that implement the selected model. The command is enclosed between &amp;quot;BEGIN PAUP&amp;quot; and &amp;quot;END;&amp;quot; and should look something like this:&lt;br /&gt;
 Lset Base=(0.4064, [...]&lt;br /&gt;
: You will need to copy this command to a PAUP session in the next step.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Start PAUP&#039;&#039;&#039;&lt;br /&gt;
 paup&lt;br /&gt;
: Above, you used jmodeltest2 to select the most suitable substitution model for the present data set. You will now use this model to construct a maximum likelihood tree. You will use PAUP for this purpose. (Note: it is possible to create a maximum likelihood tree, or even a model-averaged tree, directly from jmodeltest2, but we will instead do it in PAUP in order to more clearly see each step that is taken.)&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Load alignment:&#039;&#039;&#039;&lt;br /&gt;
 execute gp120.nexus&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Set tree-building criterion to maximum likelihood&#039;&#039;&#039;&lt;br /&gt;
 set criterion=likelihood&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Set model parameters to winning estimates&#039;&#039;&#039;&lt;br /&gt;
: Above, you located a set of lines in the jmodeltest2 output giving a PAUP command that sets the model parameters to the estimates found for the winning model. Copy and paste this lset command (without the BEGIN and END parts) into the window where PAUP is running.&lt;br /&gt;
&lt;br /&gt;
 PASTE LSET COMMAND FROM MODELTEST RUN HERE&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Find best tree using selected model&#039;&#039;&#039;&lt;br /&gt;
: Still in the PAUP window, enter the following command:&lt;br /&gt;
 hsearch swap=tbr start=nj&lt;br /&gt;
: This command causes PAUP to perform a heuristic search for the best maximum likelihood tree. Once an initial tree has been constructed, the heuristic search proceeds by rearrangements of the &amp;quot;tree bisection and reconnection&amp;quot; type (TBR). We are using the model selected by modeltest, and the parameter estimates found by modeltest for that model. You could also choose to estimate all the model parameters as part of this step (i.e., at the same time as finding the best tree), but fixing them improves speed tremendously. Finding the best tree should take at most a few minutes.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Save best tree to file&#039;&#039;&#039;&lt;br /&gt;
 savetrees format=newick brlens=yes file=gp120tree.phy from=1 to=1&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Quit program&#039;&#039;&#039;&lt;br /&gt;
 quit&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Have a look at the tree:&#039;&#039;&#039;&lt;br /&gt;
: You have now produced an unrooted tree of the HIV sequences and saved it in the file gp120tree.phy. Note that in this exercise we will not be interested in the tree as such - our focus is instead on finding positive selection at a subset of codon positions, and the tree is just something we need in order to fit the different codon models to the data. If you want to see the tree, you can do so with the following command:&lt;br /&gt;
 figtree gp120tree.phy &amp;amp;&lt;br /&gt;
: There is no meaningful root placed on this tree, so you may want to choose the unrooted view (the third icon in the Layout section of the FigTree window). Close the FigTree window when you have had a look.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Question: &#039;&#039;&#039;Which substitution model did you use for the PAUP tree search, and what was the negative log likelihood of the best tree found under this model?&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
== Model selection and tree reconstruction in one step using IQ-TREE 3 ==&lt;br /&gt;
&lt;br /&gt;
: In real-life phylogenetic analysis one would often use a modern program such as &#039;&#039;&#039;IQ-TREE 3&#039;&#039;&#039;, which can carry out model selection and maximum likelihood tree reconstruction in a single analysis.&lt;br /&gt;
&lt;br /&gt;
: Run IQ-TREE 3 on the alignment using the following command:&lt;br /&gt;
 iqtree3 -s gp120.nexus -m MFP -nt AUTO&lt;br /&gt;
&lt;br /&gt;
: When the run has finished, inspect the output written to the screen and to the file ending in &amp;quot;.iqtree&amp;quot;. Locate the substitution model selected by IQ-TREE 3 and the log likelihood of the best tree.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Question 6&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
: Which substitution model was selected by IQ-TREE 3, and what was the log likelihood of the best tree? Compare this to the earlier jModelTest2 + PAUP analysis. Is the selected model exactly the same? Is the likelihood exactly the same? Briefly suggest one reason why the two analyses might not give identical results.&lt;br /&gt;
&lt;br /&gt;
---&lt;br /&gt;
&lt;br /&gt;
== Detection of positively selected sites in gp120 ==&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Question 7&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
: There is much more to phylogenetic analyses than merely reconstructing trees. One interesting result of probabilistic methods, is that the parameters of a model will have their values estimated as part of the optimization procedure. This means that once such a model has been fitted to the data, it is possible to investigate these estimated parameter values to learn features about the evolutionary history of the sequences under investigation. In the present example we will focus on investigating whether we can find positively selected sites in our data set, defined as sites where the dN/dS ratio is larger than 1. We do that by using a codon substitution model where the dN/dS ratio is one of its parameters.&lt;br /&gt;
&lt;br /&gt;
: A further strength of the probabilistic approach is that you get a probabilistic measure of how well any model fits the data. This means you can use a stringent approach to determine which model fits the data best. In this framework one uses likelihoods (probabilities of data given model) to determine which model fits the data best. As you saw above, it is for instance possible to compute AIC values and weights (model probabilities) from the likelihood values of fitted models, Since each model essentially corresponds to a hypothesis about the evolutionary history of the data, we can thus use a stringent statistical approach to decide which hypothesis best describes our data.&lt;br /&gt;
&lt;br /&gt;
: In outline, you will now use the following steps to investigate whether there is any evidence for positively selected codons in your data set:&lt;br /&gt;
&lt;br /&gt;
:* Fit model M1, which assumes there are two classes of codons in the sequence: some with dN/dS &amp;lt; 1, some with dN/dS=1.&lt;br /&gt;
:* Fit model M2, which assumes 3 distinct classes of codons: two with dN/dS ratios as for M1, and one extra class with dN/dS &amp;gt; 1.&lt;br /&gt;
:* Assess the strength of evidence for the two models using AIC-based model probabilities&lt;br /&gt;
:* If M2 is better: identify the positively selected codons&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Inspect the parameter file&#039;&#039;&#039;&lt;br /&gt;
 nedit codeml.ctl &amp;amp;&lt;br /&gt;
: The file &amp;quot;codeml.ctl&amp;quot; contains several settings that are relevant for running the program &#039;&#039;&#039;codeml&#039;&#039;&#039;. Find the following lines and ensure that the file contains these values:&lt;br /&gt;
 &#039;&#039;&#039;seqfile =  gp120align.fasta&#039;&#039;&#039;:  name of alignment file&lt;br /&gt;
 &#039;&#039;&#039;treefile =  gp120tree.phy&#039;&#039;&#039;: name of tree file&lt;br /&gt;
 &#039;&#039;&#039;seqtype = 1&#039;&#039;&#039;: tells the program that our data consists of coding DNA.&lt;br /&gt;
 &#039;&#039;&#039;NSsites = 1 2&#039;&#039;&#039; : tells the program to analyze models M1 and M2.&lt;br /&gt;
 &#039;&#039;&#039;cleandata = 0&#039;&#039;&#039;: tells the program to keep positions with gaps.&lt;br /&gt;
&lt;br /&gt;
: The settings entered by us will cause codeml to analyze two hypotheses about dN/dS ratios. M1 says there are two classes of codons with different dN/dS ratios in the sequence: one class with dN/dS &amp;lt; 1 (codons under purifying or negative selection), and one class with dN/dS=1 (no selection - neutrally evolving sites). M2 says there are 3 distinct dN/dS ratios for different sites in the sequence: one class with dN/dS &amp;lt; 1, one class with dN/dS=1 (these are the same type of classes as for M1), and one class with dN/dS &amp;gt; 1 (corresponding to sites under positive selection). The value of the dN/dS ratios (for those classes that have dN/dS &amp;lt; 1 or dN/dS &amp;gt; 1), the fraction of sites belonging to each class, and the position of sites belonging to each class, are unknown at first and will be determined during the analysis.&lt;br /&gt;
&lt;br /&gt;
: (Regarding &amp;quot;cleandata&amp;quot; setting: &amp;quot;cleandata = 1&amp;quot; would cause codeml to discard any alignment columns with gaps or ambiguity symbols. For alignments with many sequences this can lead to discarding the majority of sites and I would therefore recommend not using that setting. At the same time you do want to be wary of columns where most sequences have gaps, since inference of selection for these will be very uncertain. It might be better to discard such columns from an alignment before doing the codeml analysis).&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Start the analysis&#039;&#039;&#039;&lt;br /&gt;
 codeml&lt;br /&gt;
: This will start the codeml program using the settings in the file codeml.ctl. Depending on your computer, this will take some minutes to finish. (You may be able to see how the optimization procedure results in progressively better fits: the likelihood increases, meaning that negative log-likelihood decreases, as the fit improves).&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Inspect result file:&#039;&#039;&#039;&lt;br /&gt;
: Wait for the run to finish, and then look at the result file:&lt;br /&gt;
 nedit selection.results &amp;amp;&lt;br /&gt;
: This file contains a wealth of information concerning your analysis. You may want to turn off line-wrapping to more clearly see the structure of the file (there will be some long lines that will otherwise wrap around). The top part of the file gives an overview of your sequences, codon usage and nucleotide frequencies. You can ignore this information for now, and move on to the interesting part, namely the model likelihoods and parameter values:&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Find likelihood, and number of free parameters for model M1&#039;&#039;&#039;&lt;br /&gt;
 Search ==&amp;gt; Find... ==&amp;gt; enter &amp;quot;Model 1&amp;quot; and click Find&lt;br /&gt;
: You are now in the region of the result file where the model likelihoods and parameter estimates are noted. Now, locate a line that looks a bit like the one shown below:&lt;br /&gt;
 lnL(ntime: 72  np: 74):  -4242.470345     +0.000000&lt;br /&gt;
: Identify the number of &amp;quot;free parameters&amp;quot;, K, used in model M1: This is indicated by &amp;quot;np&amp;quot;, and is 74 in the example shown above (most of these parameters are branch lengths in the tree; specifically, the number of branch length parameters is indicated by &amp;quot;ntime&amp;quot;, and is 72 in this example). Also note the log-likelihood of the fitted model. This is the number right after the parenthesis, and is -4242.470345 in the example here.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Question: &#039;&#039;&#039;What are the values of K and lnL for model M1?&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Question 8&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Find dn/dS ratios and codon class proportions for model M1:&#039;&#039;&#039;&lt;br /&gt;
: Scroll down a few lines until you get to a small table similar to this:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
      dN/dS for site classes (K=2)&lt;br /&gt;
      p:   0.75111  0.24889&lt;br /&gt;
      w:   0.06583  1.00000&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
: This gives a summary of the dN/dS ratios that were found in the data set. The line starting &amp;quot;w:&amp;quot; lists the two dN/dS ratios that were found (in this case 0.06583 and 1.00000 - the last one was pre-specified by us as part of the model and was therefore not a free parameter). In this context &amp;quot;w&amp;quot; means &amp;quot;omega&amp;quot;, which is the symbol typically used to represent the dN/dS rate ratio. The line starting p: gives the proportion of codon sites belonging to each of the dN/dS ratio classes (in the example above approximately 75% belong to the first class , while 25% of all sites belong to the class having dN/dS=1.00000).&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Question: &#039;&#039;&#039;What are the dN/dS value (w) and proportion (p) of sites for both classes. Report the following values: p(class1), w(class1), p(class2), w(class2)&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Question 9&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Find likelihood, and K for model M2&#039;&#039;&#039;&lt;br /&gt;
: Scroll past the M1 output until you get to the results for model M2.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Question: &#039;&#039;&#039;What are the values of K and lnL for model M2?&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Question 10&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Find dn/dS ratios and codon class proportions for model M2:&#039;&#039;&#039;&lt;br /&gt;
: Now, scroll down a few lines until you get to a small table similar to the one you examined for M1 before. For this model there are 3 separate classes of codons.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Question: &#039;&#039;&#039;What are the dN/dS value (w) and proportion (p) of sites for all three classes? Report these values: p(class1), w(class1), p(class2), w(class2), p(class3), w(class3)&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Question 11&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Assess strength of evidence for models M1 and M2:&#039;&#039;&#039;&lt;br /&gt;
: M2 will always have a better (higher) log-likelihood than model M1 because M2 has more free parameters, and M1 is nested within M2. You should now use the recipe given above to compute AIC values and model probabilities for M1 and M2.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Question: &#039;&#039;&#039; Report: AIC, ΔAIC, weight (model probability) for M1 and M2&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Question 12: &#039;&#039;&#039; Is M2 better than M1?&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Question 13&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Examine list of positively selected sites&#039;&#039;&#039;&lt;br /&gt;
: If your M2 is clearly better than M1 (I firmly believe it should be if you did things according to instructions...), then you have evidence for the existence of positively selected sites in the gp120 gene. Now, scroll down to the end of the result file and locate a list similar to the one below. Note: This is the &amp;quot;Bayes Empirical Bayes&amp;quot; table, not the &amp;quot;Naive Empirical Bayes&amp;quot; table. &lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Bayes Empirical Bayes (BEB) analysis&lt;br /&gt;
Positively selected sites&lt;br /&gt;
&lt;br /&gt;
         Prob(w&amp;gt;1)     mean w&lt;br /&gt;
&lt;br /&gt;
    25 A 0.959*        3.133 +- 0.769&lt;br /&gt;
    27 P 0.906         2.990 +- 0.877&lt;br /&gt;
    56 K 0.987*        3.197 +- 0.687&lt;br /&gt;
    59 V 0.915         3.032 +- 0.873&lt;br /&gt;
    78 R 0.637         2.351 +- 1.129&lt;br /&gt;
    88 K 0.573         2.148 +- 1.077&lt;br /&gt;
    95 V 0.925         3.046 +- 0.843&lt;br /&gt;
    ...&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
: It is not important what the distinction is in this context, but very briefly NEB ignores the fact that there is uncertainty about  maximum likelihood estimates, especially for smaller data sets (for instance w for some codon is perhaps not exactly 3.046, but could be in a region around that value), while [https://pubmed.ncbi.nlm.nih.gov/15689528/ BEB accounts for that uncertainty].&lt;br /&gt;
: This gives you a list of which residues (if any) that were found to belong to the positively selected dN/dS-class. Also listed is the probability that the site really is in the codon class where dN/dS &amp;gt; 1, and a weighted average of the w at the site. Using only DNA sequences you have now identified likely epitopes on the gp120 protein.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Question: &#039;&#039;&#039;List all sites having more than 95% probability of belonging to the positively selected class&lt;/div&gt;</summary>
		<author><name>Gorm</name></author>
	</entry>
	<entry>
		<id>https://teaching.healthtech.dtu.dk/22115/index.php?title=Model_selection&amp;diff=267</id>
		<title>Model selection</title>
		<link rel="alternate" type="text/html" href="https://teaching.healthtech.dtu.dk/22115/index.php?title=Model_selection&amp;diff=267"/>
		<updated>2026-04-15T07:01:03Z</updated>

		<summary type="html">&lt;p&gt;Gorm: /* Construction of phylogenetic tree using PAUP */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;This exercise is part of the course  [[22115_-_Computational_Molecular_Evolution|Computational Molecular Evolution (22115)]].&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Overview ==&lt;br /&gt;
&lt;br /&gt;
: In this exercise you are going to investigate features of HIV-1 evolution. You will do this by analyzing a large set of env-genes from HIV-1, subtype B. specifically, the DNA sequences analyzed here correspond to a region surrounding the hypervariable V3 region of the gp120 protein.&lt;br /&gt;
&lt;br /&gt;
: Like other retroviruses, particles of HIV are made up of 2 copies of a single-stranded RNA genome packaged inside a protein core, or capsid. The core particle also contains viral proteins that are essential for the early steps of the virus life cycle, such as reverse transcription and integration. A lipid envelope, derived from the infected cell, surrounds the core particle. Embedded in this envelope are the surface glycoproteins of HIV: gp120 and gp41. The gp120 protein is crucial for binding of the virus particle to target cells, while gp41 is important for the subsequent fusion event. It is the specific affinity of gp120 for the CD4 protein that targets HIV to those cells of the immune system that express CD4 on their surface (e.g., T-helper lymphocytes, monocytes, and macrophages).&lt;br /&gt;
&lt;br /&gt;
: The role gp120 plays in infection and the fact that it is situated on the surface of the HIV particle, means it is an obvious target for the immune response. That means that there may be a considerable selective pressure on gp120 for creating immune-escape mutants, where amino acids in the gp120 epitopes have been substituted. In this exercise you will construct a maximum likelihood tree that we will subsequently use to investigate whether you can detect such a selective pressure on parts of gp120, again using maximum likelihood methods.&lt;br /&gt;
&lt;br /&gt;
: One major goal with the exercise is to introduce you to statistically based methods for assessing the strength of evidence for a set of alternative hypotheses about some biological system of interest. The model selection method we will use is AIC (Akaike Information Criterion), based on which you will compute model probabilities. A second goal is to make you aware that phylogenetic analysis is not only about constructing trees, but that it is also a useful framework for analyzing biological questions more generally.&lt;br /&gt;
&lt;br /&gt;
: Specifically, you will&lt;br /&gt;
&lt;br /&gt;
:# perform a multiple alignment of gp120 DNA sequences taking protein-level information into account (using revtrans).&lt;br /&gt;
:# select a suitable nucleotide substitution model (using jmodeltest2)&lt;br /&gt;
:# construct a phylogenetic tree (using PAUP).&lt;br /&gt;
:# try to detect positively selected sites in gp120 (using PAML).&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
== Recipe for computing AIC values and model probabilities ==&lt;br /&gt;
&lt;br /&gt;
: Later in today&#039;s exercise you will be asked to compute AIC values and model probabilities. Return to this section and follow the instructions when you need to do so.&lt;br /&gt;
&lt;br /&gt;
:# Fit a set of models to your data, note the maximized log likelihoods (lnL) and the number of free parameters (K) for each model in the investigated set. The models you fit should represent a plausible and comprehensive set of hypotheses about your data.&lt;br /&gt;
:# Compute AIC for each of the models: &#039;&#039;&#039;AIC = -2 x lnL + 2K&#039;&#039;&#039;.  &amp;lt;br&amp;gt;For example: a model with lnL = -2010 and K = 5 will have AIC = -2 x -2010 + 2 x 5 = 4030.&lt;br /&gt;
:# Identify the model with the smallest AIC (this is the best model in the set). We will call the AIC for this model &#039;&#039;&#039;&amp;quot;AICmin&amp;quot;&#039;&#039;&#039;.&lt;br /&gt;
:# Compute the &amp;quot;ΔAIC&amp;quot; values for each model: &#039;&#039;&#039;ΔAIC = AIC - AICmin&#039;&#039;&#039; &amp;lt;br&amp;gt;For each model subtract the minimum AIC value. The best model will have a ΔAIC of zero. The rest of the models will have positive ΔAICs.&lt;br /&gt;
:# For each model compute the following quantity: &#039;&#039;&#039;numerator = exp(-0.5 x ΔAIC)&#039;&#039;&#039; &amp;lt;br&amp;gt;For example, a model with ΔAIC=4.2 will have numerator = exp(-0.5 x 4.2) = exp(-2.1) = 0.1225. Also compute the &#039;&#039;&#039;sum&#039;&#039;&#039; of the numerator values for all models.&lt;br /&gt;
:# Finally, the model probabilities (also called model weights) for each model are found as:&amp;lt;br&amp;gt; &#039;&#039;&#039;P(model) = numerator / sum&#039;&#039;&#039; &amp;lt;br&amp;gt;For example, if sum = 3.75 and a model has numerator = 1.3, then it has P(model) = 1.3 / 3.75 = 0.35&lt;br /&gt;
&lt;br /&gt;
: You may want to keep track of the computations by constructing a table along the following lines:&lt;br /&gt;
[[File:Molevol-Downloads-aictable.png|700px]]&lt;br /&gt;
&lt;br /&gt;
: Note that model probabilities can also be computed using Bayesian methods. One advantage of Bayesian methods over AIC is that instead of relying on a point estimate, uncertainty about parameter values is accounted for by integrating over all possible values (typically using MCMC).&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
== Getting started ==&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Create working directory, copy files&#039;&#039;&#039;&lt;br /&gt;
: In the command below: Instead of /path/to/molevol enter the path to the directory where you have placed your course files (for instance cd /Users/bob/Documents/molevol, or cd /home/student/molevol).&lt;br /&gt;
 cd /path/to/molevol&lt;br /&gt;
 mkdir modelselect&lt;br /&gt;
 cd modelselect&lt;br /&gt;
 cp ../data/gp120.fasta ./gp120.fasta&lt;br /&gt;
 cp ../data/codeml.ctl ./codeml.ctl&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Have a look at the DNA data file:&#039;&#039;&#039;&lt;br /&gt;
 nedit gp120.fasta &amp;amp;&lt;br /&gt;
: The file contains several DNA sequences from HIV-1, subtype B. The sequences are approximately 500 bp long, and correspond to a region surrounding the hypervariable V3 region in the gene encoding gp120. Close the nedit window when you&#039;ve had a look.&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
== Protein-guided alignment of coding DNA ==&lt;br /&gt;
&lt;br /&gt;
: There are three reasons why it is preferable to align coding DNA at the amino acid level rather than directly as nucleotides. First, the DNA alphabet has only four letters compared to 20 amino acids, which means that even unrelated DNA sequences will share roughly 25% identity by chance alone, making it much harder to distinguish true homology from noise. Second, protein alignments benefit from empirical substitution matrices such as BLOSUM-62, which capture the fact that amino acid replacements tend to be conservative. Equivalent matrices for DNA are far less informative because nucleotide substitution patterns vary strongly between genes and organisms. Third, synonymous substitutions accumulate faster than non-synonymous ones, so DNA sequences diverge more rapidly than the proteins they encode. Taken together, these factors mean that the phylogenetic signal in a DNA alignment erodes much faster than in the corresponding protein alignment.&lt;br /&gt;
&lt;br /&gt;
: However, while protein-level alignment gives us the most reliable homology signal, it discards exactly the information we need for studying selection: the synonymous substitutions. Estimating the ratio of non-synonymous to synonymous substitution rates (dN/dS) requires a codon-level alignment where analogous codon positions are properly lined up. We would therefore like to construct a multiple alignment at the DNA level, but using information at the protein level, and the RevTrans server does exactly that.&lt;br /&gt;
&lt;br /&gt;
: &#039;&#039;&#039;RevTrans&#039;&#039;&#039; takes as input an unaligned set of DNA sequences, automatically translates them to the equivalent amino acid sequences, constructs a multiple alignment of the protein sequences, and finally uses the protein alignment as a template for constructing a multiple DNA alignment. Because each amino acid maps back to its original codon, gaps are always inserted in groups of three and codon boundaries are preserved throughout the alignment. This is essential for the dN/dS analyses we will carry out in this exercise: if codon positions are misaligned, synonymous and non-synonymous changes cannot be correctly distinguished.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Construct RevTrans alignment&#039;&#039;&#039;&lt;br /&gt;
:* Open RevTrans server page: https://services.healthtech.dtu.dk/services/RevTrans-2.0/&lt;br /&gt;
:* On the RevTrans page: Choose the file gp120.fasta as input (or copy and paste the sequence into the sequence window)&lt;br /&gt;
:* Click the &amp;quot;Submit query&amp;quot; button&lt;br /&gt;
:* When the alignment is done you may have to click link named &amp;quot;here&amp;quot; to go to results page&lt;br /&gt;
:* Download DNA alignment, by right-clicking the link for &amp;quot;Download alignment in FASTA format&amp;quot;, and choosing &amp;quot;Save link as...&amp;quot; (save file under the name gp120align.fasta and make sure to save the file in the directory modelselect).&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Convert alignment to NEXUS format&#039;&#039;&#039;&lt;br /&gt;
: Convert the fasta file to NEXUS format and save file in the modelselect directory under the name gp120.nexus&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
== Model selection using AIC and jModelTest2 ==&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Question 1&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
: As part of the present analysis we are going to build a phylogenetic tree based on the DNA alignment constructed above. We will construct the tree using maximum likelihood, but to do that we first have to decide which substitution model we want to use. Specifically, we are interested in selecting a model that achieves a good balance between fit to the data and number of parameters. We will investigate this issue by fitting a set of 56 different models to our data and then comparing them using AIC.&lt;br /&gt;
&lt;br /&gt;
: In the first part of the exercise, however, you will only compare three closely related models by hand. The purpose of this restricted comparison is not primarily to identify the globally best model, but to understand how AIC, ΔAIC, and Akaike weights are computed and interpreted. Because only three models are included in this manual comparison, the weights you compute will describe relative support &#039;&#039;&#039;within this restricted set only&#039;&#039;&#039;.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Start jmodeltest2&#039;&#039;&#039;&lt;br /&gt;
 jmodeltest&lt;br /&gt;
&#039;&#039;&#039;Load data set&#039;&#039;&#039;&lt;br /&gt;
:* File -&amp;gt; Load DNA alignment -&amp;gt; File Format -&amp;gt; Select &amp;quot;All files&amp;quot;&lt;br /&gt;
:* Navigate to gp120.nexus and load it&lt;br /&gt;
&#039;&#039;&#039;Fit 56 models&#039;&#039;&#039;&lt;br /&gt;
:* Analysis -&amp;gt; Compute likelihood scores&lt;br /&gt;
:* Select &amp;quot;7&amp;quot; under &amp;quot;Number of substitution schemes&amp;quot;&lt;br /&gt;
:* Select &amp;quot;Fixed BIONJ-JC&amp;quot; under &amp;quot;Base tree for likelihood calculations&amp;quot;&lt;br /&gt;
:* Click &amp;quot;Compute likelihoods&amp;quot;&lt;br /&gt;
&lt;br /&gt;
: This causes jmodeltest2 to perform the following actions: first a neighbor joining tree is constructed using the Jukes and Cantor model. Then the tree is fixed and used as the basis for fitting a set of 56 different models to the data. For each model, the estimated model parameters and the negative log-likelihood are recorded. In addition to varying sets of substitution rate parameters (JC, K2P, ...), some of these models also include extra parameters that take into account the presence of different rates between sites. This is done in two ways: (1) by fitting a gamma distribution of rates (&amp;quot;+G&amp;quot;), and (2) by allowing for a proportion of constant (&amp;quot;invariable&amp;quot;) sites (&amp;quot;+I&amp;quot;).&lt;br /&gt;
&lt;br /&gt;
: Wait until jmodeltest2 is done fitting all 56 models (this will take a little while depending on your computer).&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Inspect result&#039;&#039;&#039;&lt;br /&gt;
:* Results -&amp;gt; Show results table&lt;br /&gt;
: For each model this table lists the negative log-likelihood (&amp;quot;-lnL&amp;quot;), the number of parameters (&amp;quot;p&amp;quot; - note this includes branch lengths), and estimates of all model parameters (excluding branch lengths).&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Manually compute Akaike weights for three substitution models&#039;&#039;&#039;&lt;br /&gt;
: Use AIC-based model comparison to investigate which of the following three substitution models are best supported as descriptions of how the sequences have evolved:&lt;br /&gt;
:* Jukes and Cantor with fraction of invariant sites (JC+I)&lt;br /&gt;
:* Jukes and Cantor with gamma-distributed rates over sites (JC+G)&lt;br /&gt;
:* Jukes and Cantor with invariant sites and gamma-distributed rates (JC+I+G)&lt;br /&gt;
: Before you can do the computation you need to know the log likelihood and the number of parameters for each model. Locate these values in the table for the JC+I, JC+G, and JC+I+G models, and write them down. Close the window with the result table when you are done.&lt;br /&gt;
&lt;br /&gt;
: Make sure to get the signs right: the values reported in the table are -lnL values, so you will need to reverse the sign to get the lnL (the lnL values you write down should be negative).&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Question: &#039;&#039;&#039;Use the recipe above to compute AIC, ΔAIC, and Akaike weight for the three models. Report the results in a table similar to the one shown above, and verify that the three weights sum to 1 (up to rounding error).&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Question 2&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
: Based on the Akaike weights, which of the three models has the strongest support? Is the support strongly concentrated on one model, or is there still substantial support for one or both of the others?&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Question 3&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
: What you just did manually for JC+I, JC+G and JC+I+G, jmodeltest2 can do automatically for the full set of 56 fitted models. Specifically, it uses the list of negative log likelihoods and parameter counts in the table to compute AIC values and Akaike weights for the full candidate set:&lt;br /&gt;
:* Analysis -&amp;gt; Do AIC calculations -&amp;gt;&lt;br /&gt;
:* Select &amp;quot;Write PAUP* block&amp;quot;&lt;br /&gt;
:* click &amp;quot;Do AIC calculations&amp;quot;&lt;br /&gt;
:* Results -&amp;gt; Show results table&lt;br /&gt;
:* Select &amp;quot;AIC&amp;quot; tab&lt;br /&gt;
:* &#039;&#039;&#039;IMPORTANT:&#039;&#039;&#039; SHIFT+click on the header of the &amp;quot;weight&amp;quot; column. This sorts the rows according to model weight, in descending order.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Question: &#039;&#039;&#039;What model was selected by modeltest based on the AIC values? (This will be the model with the highest weight - and lowest AIC - and will be in the first row of the results table after SHIFT-clicking the &amp;quot;weight&amp;quot; header).&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Question 4&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
: In the same AIC results table, now click (not SHIFT+click) the header of the &amp;quot;-lnL&amp;quot; column. In this table, a normal click sorts in ascending order, whereas SHIFT+click sorts in descending order. The model at the top will therefore be the one with the smallest value of &amp;quot;-lnL&amp;quot; (equivalently: the highest likelihood). Compare this model to the one with the highest Akaike weight.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Question: &#039;&#039;&#039;Is the model with the highest likelihood also the model with the highest Akaike weight? If not, explain briefly why these two rankings can differ.&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
== Construction of phylogenetic tree using PAUP ==&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Question 5&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
: Close the results table. In the main window you should now scroll up to the lines giving PAUP commands that will implement the selected model. The command is enclosed between &amp;quot;BEGIN PAUP&amp;quot; and &amp;quot;END;&amp;quot; and should look something like this:&lt;br /&gt;
 Lset Base=(0.4064, [...]&lt;br /&gt;
: You will need to copy this command to a PAUP session in the next step.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Start PAUP&#039;&#039;&#039;&lt;br /&gt;
 paup&lt;br /&gt;
: Above you used jmodeltest2 to select the most suitable substitution model for the present data set. You will now use this model to construct a maximum likelihood tree. You will use PAUP for this purpose. (note: it is possible to create a maximum likelihood or a model-averaged tree directly from the jmodeltest2 program, but we will instead do it in PAUP in order to more clearly see each step that is taken).&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Load alignment:&#039;&#039;&#039;&lt;br /&gt;
 execute gp120.nexus&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Set tree-building criterion to maximum likelihood&#039;&#039;&#039;&lt;br /&gt;
 set criterion=likelihood&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Set model parameters to winning estimates&#039;&#039;&#039;&lt;br /&gt;
: Above you located a set of lines in the jmodeltest2 output giving a PAUP command that sets the model parameters to the estimates that were found using the winning model. Copy and paste this lset command (without the BEGIN and END parts) into the window where PAUP is running.&lt;br /&gt;
&lt;br /&gt;
 PASTE LSET COMMAND FROM MODELTEST RUN HERE&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Find best tree using selected model&#039;&#039;&#039;&lt;br /&gt;
: Still in the PAUP-window, enter the following command&lt;br /&gt;
 hsearch swap=tbr start=nj&lt;br /&gt;
: This command causes PAUP to perform a heuristic search for the best maximum likelihood tree. Once an initial tree has been constructed, the heuristic search proceeds by rearrangements of the &amp;quot;tree bisection and reconnection&amp;quot; type (TBR). We are using the model selected by modeltest, AND the parameter estimates found by modeltest on that model. You could also have chosen to simply estimate all the model parameters as part of this step (i.e., at the same time as finding the best tree), but fixing them improves speed tremendously. Finding the best tree should take a few minutes at most.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Save best tree to file&#039;&#039;&#039;&lt;br /&gt;
 savetrees format=newick brlens=yes file=gp120tree.phy from=1 to=1&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Quit program&#039;&#039;&#039;&lt;br /&gt;
 quit&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Have a look at the tree:&#039;&#039;&#039;&lt;br /&gt;
: You have now produced an unrooted tree of the HIV sequences and saved it in the file gp120tree.phy. Note that in this exercise we will not be interested in the tree as such - our focus is instead on finding positive selection on a subset of codon positions and the tree is just something we need in order to be able to fit the different codon models to the data. If you want to see the tree, you can do so with the following command:&lt;br /&gt;
 figtree gp120tree.phy &amp;amp;&lt;br /&gt;
: There is no meaningful root placed in this tree, so you may want to choose the unrooted view (the third icon in the Layout section of the figtree window). Close the figtree window when you have had a look&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Question: &#039;&#039;&#039;What is the negative log likelihood of the tree you just found?&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
== Detection of positively selected sites in gp120 ==&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Question 6&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
: There is much more to phylogenetic analyses than merely reconstructing trees. One interesting result of probabilistic methods, is that the parameters of a model will have their values estimated as part of the optimization procedure. This means that once such a model has been fitted to the data, it is possible to investigate these estimated parameter values to learn features about the evolutionary history of the sequences under investigation. In the present example we will focus on investigating whether we can find positively selected sites in our data set, defined as sites where the dN/dS ratio is larger than 1. We do that by using a codon substitution model where the dN/dS ratio is one of its parameters.&lt;br /&gt;
&lt;br /&gt;
: A further strength of the probabilistic approach is that you get a probabilistic measure of how well any model fits the data. This means you can use a stringent approach to determine which model fits the data best. In this framework one uses likelihoods (probabilities of data given model) to determine which model fits the data best. As you saw above, it is for instance possible to compute AIC values and weights (model probabilities) from the likelihood values of fitted models, Since each model essentially corresponds to a hypothesis about the evolutionary history of the data, we can thus use a stringent statistical approach to decide which hypothesis best describes our data.&lt;br /&gt;
&lt;br /&gt;
: In outline, you will now use the following steps to investigate whether there is any evidence for positively selected codons in your data set:&lt;br /&gt;
&lt;br /&gt;
:* Fit model M1, which assumes there are two classes of codons in the sequence: some with dN/dS &amp;lt; 1, some with dN/dS=1.&lt;br /&gt;
:* Fit model M2, which assumes 3 distinct classes of codons: two with dN/dS ratios as for M1, and one extra class with dN/dS &amp;gt; 1.&lt;br /&gt;
:* Assess the strength of evidence for the two models using AIC-based model probabilities&lt;br /&gt;
:* If M2 is better: identify the positively selected codons&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Inspect the parameter file&#039;&#039;&#039;&lt;br /&gt;
 nedit codeml.ctl &amp;amp;&lt;br /&gt;
: The file &amp;quot;codeml.ctl&amp;quot; contains several settings that are relevant for running the program &#039;&#039;&#039;codeml&#039;&#039;&#039;. Find the following lines and ensure that the file contains these values:&lt;br /&gt;
 &#039;&#039;&#039;seqfile =  gp120align.fasta&#039;&#039;&#039;:  name of alignment file&lt;br /&gt;
 &#039;&#039;&#039;treefile =  gp120tree.phy&#039;&#039;&#039;: name of tree file&lt;br /&gt;
 &#039;&#039;&#039;seqtype = 1&#039;&#039;&#039;: tells the program that our data consists of coding DNA.&lt;br /&gt;
 &#039;&#039;&#039;NSsites = 1 2&#039;&#039;&#039; : tells the program to analyze models M1 and M2.&lt;br /&gt;
 &#039;&#039;&#039;cleandata = 0&#039;&#039;&#039;: tells the program to keep positions with gaps.&lt;br /&gt;
&lt;br /&gt;
: The settings entered by us will cause codeml to analyze two hypotheses about dN/dS ratios. M1 says there are two classes of codons with different dN/dS ratios in the sequence: one class with dN/dS &amp;lt; 1 (codons under purifying or negative selection), and one class with dN/dS=1 (no selection - neutrally evolving sites). M2 says there are 3 distinct dN/dS ratios for different sites in the sequence: one class with dN/dS &amp;lt; 1, one class with dN/dS=1 (these are the same type of classes as for M1), and one class with dN/dS &amp;gt; 1 (corresponding to sites under positive selection). The value of the dN/dS ratios (for those classes that have dN/dS &amp;lt; 1 or dN/dS &amp;gt; 1), the fraction of sites belonging to each class, and the position of sites belonging to each class, are unknown at first and will be determined during the analysis.&lt;br /&gt;
&lt;br /&gt;
: (Regarding &amp;quot;cleandata&amp;quot; setting: &amp;quot;cleandata = 1&amp;quot; would cause codeml to discard any alignment columns with gaps or ambiguity symbols. For alignments with many sequences this can lead to discarding the majority of sites and I would therefore recommend not using that setting. At the same time you do want to be wary of columns where most sequences have gaps, since inference of selection for these will be very uncertain. It might be better to discard such columns from an alignment before doing the codeml analysis).&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Start the analysis&#039;&#039;&#039;&lt;br /&gt;
 codeml&lt;br /&gt;
: This will start the codeml program using the settings in the file codeml.ctl. Depending on your computer, this will take some minutes to finish. (You may be able to see how the optimization procedure results in progressively better fits: the likelihood increases, meaning that negative log-likelihood decreases, as the fit improves).&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Inspect result file:&#039;&#039;&#039;&lt;br /&gt;
: Wait for the run to finish, and then look at the result file:&lt;br /&gt;
 nedit selection.results &amp;amp;&lt;br /&gt;
: This file contains a wealth of information concerning your analysis. You may want to turn off line-wrapping to more clearly see the structure of the file (there will be some long lines that will otherwise wrap around). The top part of the file gives an overview of your sequences, codon usage and nucleotide frequencies. You can ignore this information for now, and move on to the interesting part, namely the model likelihoods and parameter values:&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Find likelihood, and number of free parameters for model M1&#039;&#039;&#039;&lt;br /&gt;
 Search ==&amp;gt; Find... ==&amp;gt; enter &amp;quot;Model 1&amp;quot; and click Find&lt;br /&gt;
: You are now in the region of the result file where the model likelihoods and parameter estimates are noted. Now, locate a line that looks a bit like the one shown below:&lt;br /&gt;
 lnL(ntime: 72  np: 74):  -4242.470345     +0.000000&lt;br /&gt;
: Identify the number of &amp;quot;free parameters&amp;quot;, K, used in model M1: This is indicated by &amp;quot;np&amp;quot;, and is 74 in the example shown above (most of these parameters are branch lengths in the tree; specifically, the number of branch length parameters is indicated by &amp;quot;ntime&amp;quot;, and is 72 in this example). Also note the log-likelihood of the fitted model. This is the number right after the parenthesis, and is -4242.470345 in the example here.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Question: &#039;&#039;&#039;What are the values of K and lnL for model M1?&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Question 7&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Find dn/dS ratios and codon class proportions for model M1:&#039;&#039;&#039;&lt;br /&gt;
: Scroll down a few lines until you get to a small table similar to this:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
      dN/dS for site classes (K=2)&lt;br /&gt;
      p:   0.75111  0.24889&lt;br /&gt;
      w:   0.06583  1.00000&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
: This gives a summary of the dN/dS ratios that were found in the data set. The line starting &amp;quot;w:&amp;quot; lists the two dN/dS ratios that were found (in this case 0.06583 and 1.00000 - the last one was pre-specified by us as part of the model and was therefore not a free parameter). In this context &amp;quot;w&amp;quot; means &amp;quot;omega&amp;quot;, which is the symbol typically used to represent the dN/dS rate ratio. The line starting p: gives the proportion of codon sites belonging to each of the dN/dS ratio classes (in the example above approximately 75% belong to the first class , while 25% of all sites belong to the class having dN/dS=1.00000).&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Question: &#039;&#039;&#039;What are the dN/dS value (w) and proportion (p) of sites for both classes. Report the following values: p(class1), w(class1), p(class2), w(class2)&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Question 8&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Find likelihood, and K for model M2&#039;&#039;&#039;&lt;br /&gt;
: Scroll past the M1 output until you get to the results for model M2.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Question: &#039;&#039;&#039;What are the values of K and lnL for model M2?&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Question 9&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Find dn/dS ratios and codon class proportions for model M2:&#039;&#039;&#039;&lt;br /&gt;
: Now, scroll down a few lines until you get to a small table similar to the one you examined for M1 before. For this model there are 3 separate classes of codons.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Question: &#039;&#039;&#039;What are the dN/dS value (w) and proportion (p) of sites for all three classes? Report these values: p(class1), w(class1), p(class2), w(class2), p(class3), w(class3)&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Question 10&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Assess strength of evidence for models M1 and M2:&#039;&#039;&#039;&lt;br /&gt;
: M2 will always have a better (higher) log-likelihood than model M1 because M2 has more free parameters, and M1 is nested within M2. You should now use the recipe given above to compute AIC values and model probabilities for M1 and M2.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Question: &#039;&#039;&#039; Report: AIC, ΔAIC, weight (model probability) for M1 and M2&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Question 11: &#039;&#039;&#039; Is M2 better than M1?&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Question 12&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Examine list of positively selected sites&#039;&#039;&#039;&lt;br /&gt;
: If your M2 is clearly better than M1 (I firmly believe it should be if you did things according to instructions...), then you have evidence for the existence of positively selected sites in the gp120 gene. Now, scroll down to the end of the result file and locate a list similar to the one below. Note: This is the &amp;quot;Bayes Empirical Bayes&amp;quot; table, not the &amp;quot;Naive Empirical Bayes&amp;quot; table. &lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Bayes Empirical Bayes (BEB) analysis&lt;br /&gt;
Positively selected sites&lt;br /&gt;
&lt;br /&gt;
         Prob(w&amp;gt;1)     mean w&lt;br /&gt;
&lt;br /&gt;
    25 A 0.959*        3.133 +- 0.769&lt;br /&gt;
    27 P 0.906         2.990 +- 0.877&lt;br /&gt;
    56 K 0.987*        3.197 +- 0.687&lt;br /&gt;
    59 V 0.915         3.032 +- 0.873&lt;br /&gt;
    78 R 0.637         2.351 +- 1.129&lt;br /&gt;
    88 K 0.573         2.148 +- 1.077&lt;br /&gt;
    95 V 0.925         3.046 +- 0.843&lt;br /&gt;
    ...&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
: It is not important what the distinction is in this context, but very briefly NEB ignores the fact that there is uncertainty about  maximum likelihood estimates, especially for smaller data sets (for instance w for some codon is perhaps not exactly 3.046, but could be in a region around that value), while [https://pubmed.ncbi.nlm.nih.gov/15689528/ BEB accounts for that uncertainty].&lt;br /&gt;
: This gives you a list of which residues (if any) that were found to belong to the positively selected dN/dS-class. Also listed is the probability that the site really is in the codon class where dN/dS &amp;gt; 1, and a weighted average of the w at the site. Using only DNA sequences you have now identified likely epitopes on the gp120 protein.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Question: &#039;&#039;&#039;List all sites having more than 95% probability of belonging to the positively selected class&lt;/div&gt;</summary>
		<author><name>Gorm</name></author>
	</entry>
	<entry>
		<id>https://teaching.healthtech.dtu.dk/22115/index.php?title=Model_selection&amp;diff=266</id>
		<title>Model selection</title>
		<link rel="alternate" type="text/html" href="https://teaching.healthtech.dtu.dk/22115/index.php?title=Model_selection&amp;diff=266"/>
		<updated>2026-04-15T06:56:05Z</updated>

		<summary type="html">&lt;p&gt;Gorm: /* Model selection using AIC and jModelTest2 */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;This exercise is part of the course  [[22115_-_Computational_Molecular_Evolution|Computational Molecular Evolution (22115)]].&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Overview ==&lt;br /&gt;
&lt;br /&gt;
: In this exercise you are going to investigate features of HIV-1 evolution. You will do this by analyzing a large set of env-genes from HIV-1, subtype B. specifically, the DNA sequences analyzed here correspond to a region surrounding the hypervariable V3 region of the gp120 protein.&lt;br /&gt;
&lt;br /&gt;
: Like other retroviruses, particles of HIV are made up of 2 copies of a single-stranded RNA genome packaged inside a protein core, or capsid. The core particle also contains viral proteins that are essential for the early steps of the virus life cycle, such as reverse transcription and integration. A lipid envelope, derived from the infected cell, surrounds the core particle. Embedded in this envelope are the surface glycoproteins of HIV: gp120 and gp41. The gp120 protein is crucial for binding of the virus particle to target cells, while gp41 is important for the subsequent fusion event. It is the specific affinity of gp120 for the CD4 protein that targets HIV to those cells of the immune system that express CD4 on their surface (e.g., T-helper lymphocytes, monocytes, and macrophages).&lt;br /&gt;
&lt;br /&gt;
: The role gp120 plays in infection and the fact that it is situated on the surface of the HIV particle, means it is an obvious target for the immune response. That means that there may be a considerable selective pressure on gp120 for creating immune-escape mutants, where amino acids in the gp120 epitopes have been substituted. In this exercise you will construct a maximum likelihood tree that we will subsequently use to investigate whether you can detect such a selective pressure on parts of gp120, again using maximum likelihood methods.&lt;br /&gt;
&lt;br /&gt;
: One major goal with the exercise is to introduce you to statistically based methods for assessing the strength of evidence for a set of alternative hypotheses about some biological system of interest. The model selection method we will use is AIC (Akaike Information Criterion), based on which you will compute model probabilities. A second goal is to make you aware that phylogenetic analysis is not only about constructing trees, but that it is also a useful framework for analyzing biological questions more generally.&lt;br /&gt;
&lt;br /&gt;
: Specifically, you will&lt;br /&gt;
&lt;br /&gt;
:# perform a multiple alignment of gp120 DNA sequences taking protein-level information into account (using revtrans).&lt;br /&gt;
:# select a suitable nucleotide substitution model (using jmodeltest2)&lt;br /&gt;
:# construct a phylogenetic tree (using PAUP).&lt;br /&gt;
:# try to detect positively selected sites in gp120 (using PAML).&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
== Recipe for computing AIC values and model probabilities ==&lt;br /&gt;
&lt;br /&gt;
: Later in today&#039;s exercise you will be asked to compute AIC values and model probabilities. Return to this section and follow the instructions when you need to do so.&lt;br /&gt;
&lt;br /&gt;
:# Fit a set of models to your data, note the maximized log likelihoods (lnL) and the number of free parameters (K) for each model in the investigated set. The models you fit should represent a plausible and comprehensive set of hypotheses about your data.&lt;br /&gt;
:# Compute AIC for each of the models: &#039;&#039;&#039;AIC = -2 x lnL + 2K&#039;&#039;&#039;.  &amp;lt;br&amp;gt;For example: a model with lnL = -2010 and K = 5 will have AIC = -2 x -2010 + 2 x 5 = 4030.&lt;br /&gt;
:# Identify the model with the smallest AIC (this is the best model in the set). We will call the AIC for this model &#039;&#039;&#039;&amp;quot;AICmin&amp;quot;&#039;&#039;&#039;.&lt;br /&gt;
:# Compute the &amp;quot;ΔAIC&amp;quot; values for each model: &#039;&#039;&#039;ΔAIC = AIC - AICmin&#039;&#039;&#039; &amp;lt;br&amp;gt;For each model subtract the minimum AIC value. The best model will have a ΔAIC of zero. The rest of the models will have positive ΔAICs.&lt;br /&gt;
:# For each model compute the following quantity: &#039;&#039;&#039;numerator = exp(-0.5 x ΔAIC)&#039;&#039;&#039; &amp;lt;br&amp;gt;For example, a model with ΔAIC=4.2 will have numerator = exp(-0.5 x 4.2) = exp(-2.1) = 0.1225. Also compute the &#039;&#039;&#039;sum&#039;&#039;&#039; of the numerator values for all models.&lt;br /&gt;
:# Finally, the model probabilities (also called model weights) for each model are found as:&amp;lt;br&amp;gt; &#039;&#039;&#039;P(model) = numerator / sum&#039;&#039;&#039; &amp;lt;br&amp;gt;For example, if sum = 3.75 and a model has numerator = 1.3, then it has P(model) = 1.3 / 3.75 = 0.35&lt;br /&gt;
&lt;br /&gt;
: You may want to keep track of the computations by constructing a table along the following lines:&lt;br /&gt;
[[File:Molevol-Downloads-aictable.png|700px]]&lt;br /&gt;
&lt;br /&gt;
: Note that model probabilities can also be computed using Bayesian methods. One advantage of Bayesian methods over AIC is that instead of relying on a point estimate, uncertainty about parameter values is accounted for by integrating over all possible values (typically using MCMC).&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
== Getting started ==&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Create working directory, copy files&#039;&#039;&#039;&lt;br /&gt;
: In the command below: Instead of /path/to/molevol enter the path to the directory where you have placed your course files (for instance cd /Users/bob/Documents/molevol, or cd /home/student/molevol).&lt;br /&gt;
 cd /path/to/molevol&lt;br /&gt;
 mkdir modelselect&lt;br /&gt;
 cd modelselect&lt;br /&gt;
 cp ../data/gp120.fasta ./gp120.fasta&lt;br /&gt;
 cp ../data/codeml.ctl ./codeml.ctl&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Have a look at the DNA data file:&#039;&#039;&#039;&lt;br /&gt;
 nedit gp120.fasta &amp;amp;&lt;br /&gt;
: The file contains several DNA sequences from HIV-1, subtype B. The sequences are approximately 500 bp long, and correspond to a region surrounding the hypervariable V3 region in the gene encoding gp120. Close the nedit window when you&#039;ve had a look.&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
== Protein-guided alignment of coding DNA ==&lt;br /&gt;
&lt;br /&gt;
: There are three reasons why it is preferable to align coding DNA at the amino acid level rather than directly as nucleotides. First, the DNA alphabet has only four letters compared to 20 amino acids, which means that even unrelated DNA sequences will share roughly 25% identity by chance alone, making it much harder to distinguish true homology from noise. Second, protein alignments benefit from empirical substitution matrices such as BLOSUM-62, which capture the fact that amino acid replacements tend to be conservative. Equivalent matrices for DNA are far less informative because nucleotide substitution patterns vary strongly between genes and organisms. Third, synonymous substitutions accumulate faster than non-synonymous ones, so DNA sequences diverge more rapidly than the proteins they encode. Taken together, these factors mean that the phylogenetic signal in a DNA alignment erodes much faster than in the corresponding protein alignment.&lt;br /&gt;
&lt;br /&gt;
: However, while protein-level alignment gives us the most reliable homology signal, it discards exactly the information we need for studying selection: the synonymous substitutions. Estimating the ratio of non-synonymous to synonymous substitution rates (dN/dS) requires a codon-level alignment where analogous codon positions are properly lined up. We would therefore like to construct a multiple alignment at the DNA level, but using information at the protein level, and the RevTrans server does exactly that.&lt;br /&gt;
&lt;br /&gt;
: &#039;&#039;&#039;RevTrans&#039;&#039;&#039; takes as input an unaligned set of DNA sequences, automatically translates them to the equivalent amino acid sequences, constructs a multiple alignment of the protein sequences, and finally uses the protein alignment as a template for constructing a multiple DNA alignment. Because each amino acid maps back to its original codon, gaps are always inserted in groups of three and codon boundaries are preserved throughout the alignment. This is essential for the dN/dS analyses we will carry out in this exercise: if codon positions are misaligned, synonymous and non-synonymous changes cannot be correctly distinguished.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Construct RevTrans alignment&#039;&#039;&#039;&lt;br /&gt;
:* Open RevTrans server page: https://services.healthtech.dtu.dk/services/RevTrans-2.0/&lt;br /&gt;
:* On the RevTrans page: Choose the file gp120.fasta as input (or copy and paste the sequence into the sequence window)&lt;br /&gt;
:* Click the &amp;quot;Submit query&amp;quot; button&lt;br /&gt;
:* When the alignment is done you may have to click link named &amp;quot;here&amp;quot; to go to results page&lt;br /&gt;
:* Download DNA alignment, by right-clicking the link for &amp;quot;Download alignment in FASTA format&amp;quot;, and choosing &amp;quot;Save link as...&amp;quot; (save file under the name gp120align.fasta and make sure to save the file in the directory modelselect).&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Convert alignment to NEXUS format&#039;&#039;&#039;&lt;br /&gt;
: Convert the fasta file to NEXUS format and save file in the modelselect directory under the name gp120.nexus&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
== Model selection using AIC and jModelTest2 ==&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Question 1&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
: As part of the present analysis we are going to build a phylogenetic tree based on the DNA alignment constructed above. We will construct the tree using maximum likelihood, but to do that we first have to decide which substitution model we want to use. Specifically, we are interested in selecting a model that achieves a good balance between fit to the data and number of parameters. We will investigate this issue by fitting a set of 56 different models to our data and then comparing them using AIC.&lt;br /&gt;
&lt;br /&gt;
: In the first part of the exercise, however, you will only compare three closely related models by hand. The purpose of this restricted comparison is not primarily to identify the globally best model, but to understand how AIC, ΔAIC, and Akaike weights are computed and interpreted. Because only three models are included in this manual comparison, the weights you compute will describe relative support &#039;&#039;&#039;within this restricted set only&#039;&#039;&#039;.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Start jmodeltest2&#039;&#039;&#039;&lt;br /&gt;
 jmodeltest&lt;br /&gt;
&#039;&#039;&#039;Load data set&#039;&#039;&#039;&lt;br /&gt;
:* File -&amp;gt; Load DNA alignment -&amp;gt; File Format -&amp;gt; Select &amp;quot;All files&amp;quot;&lt;br /&gt;
:* Navigate to gp120.nexus and load it&lt;br /&gt;
&#039;&#039;&#039;Fit 56 models&#039;&#039;&#039;&lt;br /&gt;
:* Analysis -&amp;gt; Compute likelihood scores&lt;br /&gt;
:* Select &amp;quot;7&amp;quot; under &amp;quot;Number of substitution schemes&amp;quot;&lt;br /&gt;
:* Select &amp;quot;Fixed BIONJ-JC&amp;quot; under &amp;quot;Base tree for likelihood calculations&amp;quot;&lt;br /&gt;
:* Click &amp;quot;Compute likelihoods&amp;quot;&lt;br /&gt;
&lt;br /&gt;
: This causes jmodeltest2 to perform the following actions: first a neighbor joining tree is constructed using the Jukes and Cantor model. Then the tree is fixed and used as the basis for fitting a set of 56 different models to the data. For each model, the estimated model parameters and the negative log-likelihood are recorded. In addition to varying sets of substitution rate parameters (JC, K2P, ...), some of these models also include extra parameters that take into account the presence of different rates between sites. This is done in two ways: (1) by fitting a gamma distribution of rates (&amp;quot;+G&amp;quot;), and (2) by allowing for a proportion of constant (&amp;quot;invariable&amp;quot;) sites (&amp;quot;+I&amp;quot;).&lt;br /&gt;
&lt;br /&gt;
: Wait until jmodeltest2 is done fitting all 56 models (this will take a little while depending on your computer).&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Inspect result&#039;&#039;&#039;&lt;br /&gt;
:* Results -&amp;gt; Show results table&lt;br /&gt;
: For each model this table lists the negative log-likelihood (&amp;quot;-lnL&amp;quot;), the number of parameters (&amp;quot;p&amp;quot; - note this includes branch lengths), and estimates of all model parameters (excluding branch lengths).&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Manually compute Akaike weights for three substitution models&#039;&#039;&#039;&lt;br /&gt;
: Use AIC-based model comparison to investigate which of the following three substitution models are best supported as descriptions of how the sequences have evolved:&lt;br /&gt;
:* Jukes and Cantor with fraction of invariant sites (JC+I)&lt;br /&gt;
:* Jukes and Cantor with gamma-distributed rates over sites (JC+G)&lt;br /&gt;
:* Jukes and Cantor with invariant sites and gamma-distributed rates (JC+I+G)&lt;br /&gt;
: Before you can do the computation you need to know the log likelihood and the number of parameters for each model. Locate these values in the table for the JC+I, JC+G, and JC+I+G models, and write them down. Close the window with the result table when you are done.&lt;br /&gt;
&lt;br /&gt;
: Make sure to get the signs right: the values reported in the table are -lnL values, so you will need to reverse the sign to get the lnL (the lnL values you write down should be negative).&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Question: &#039;&#039;&#039;Use the recipe above to compute AIC, ΔAIC, and Akaike weight for the three models. Report the results in a table similar to the one shown above, and verify that the three weights sum to 1 (up to rounding error).&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Question 2&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
: Based on the Akaike weights, which of the three models has the strongest support? Is the support strongly concentrated on one model, or is there still substantial support for one or both of the others?&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Question 3&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
: What you just did manually for JC+I, JC+G and JC+I+G, jmodeltest2 can do automatically for the full set of 56 fitted models. Specifically, it uses the list of negative log likelihoods and parameter counts in the table to compute AIC values and Akaike weights for the full candidate set:&lt;br /&gt;
:* Analysis -&amp;gt; Do AIC calculations -&amp;gt;&lt;br /&gt;
:* Select &amp;quot;Write PAUP* block&amp;quot;&lt;br /&gt;
:* click &amp;quot;Do AIC calculations&amp;quot;&lt;br /&gt;
:* Results -&amp;gt; Show results table&lt;br /&gt;
:* Select &amp;quot;AIC&amp;quot; tab&lt;br /&gt;
:* &#039;&#039;&#039;IMPORTANT:&#039;&#039;&#039; SHIFT+click on the header of the &amp;quot;weight&amp;quot; column. This sorts the rows according to model weight, in descending order.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Question: &#039;&#039;&#039;What model was selected by modeltest based on the AIC values? (This will be the model with the highest weight - and lowest AIC - and will be in the first row of the results table after SHIFT-clicking the &amp;quot;weight&amp;quot; header).&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Question 4&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
: In the same AIC results table, now click (not SHIFT+click) the header of the &amp;quot;-lnL&amp;quot; column. In this table, a normal click sorts in ascending order, whereas SHIFT+click sorts in descending order. The model at the top will therefore be the one with the smallest value of &amp;quot;-lnL&amp;quot; (equivalently: the highest likelihood). Compare this model to the one with the highest Akaike weight.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Question: &#039;&#039;&#039;Is the model with the highest likelihood also the model with the highest Akaike weight? If not, explain briefly why these two rankings can differ.&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
== Construction of phylogenetic tree using PAUP ==&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Question 5&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
: Close the results table. In the main window you should now scroll up to the lines giving PAUP commands that will implement the selected model. The command is enclosed between &amp;quot;BEGIN PAUP&amp;quot; and &amp;quot;END;&amp;quot; and should look something like this:&lt;br /&gt;
 Lset Base=(0.4064, [...]&lt;br /&gt;
: You will need to copy this command to a PAUP session in the next step.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Start PAUP&#039;&#039;&#039;&lt;br /&gt;
 paup&lt;br /&gt;
: Above you used jmodeltest2 to select the most suitable substitution model for the present data set. You will now use this model to construct a maximum likelihood tree. You will use PAUP for this purpose. (note: it is possible to create a maximum likelihood or a model-averaged tree directly from the jmodeltest2 program, but we will instead do it in PAUP in order to more clearly see each step that is taken).&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Load alignment:&#039;&#039;&#039;&lt;br /&gt;
 execute gp120.nexus&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Set tree-building criterion to maximum likelihood&#039;&#039;&#039;&lt;br /&gt;
 set criterion=likelihood&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Set model parameters to winning estimates&#039;&#039;&#039;&lt;br /&gt;
: Above you located a set of lines in the jmodeltest2 output giving a PAUP command that sets the model parameters to the estimates that were found using the winning model. Copy and paste this lset command (without the BEGIN and END parts) into the window where PAUP is running.&lt;br /&gt;
&lt;br /&gt;
 PASTE LSET COMMAND FROM MODELTEST RUN HERE&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Find best tree using selected model&#039;&#039;&#039;&lt;br /&gt;
: Still in the PAUP-window, enter the following command&lt;br /&gt;
 hsearch swap=tbr start=nj&lt;br /&gt;
: This command causes PAUP to perform a heuristic search for the best maximum likelihood tree. Once an initial tree has been constructed, the heuristic search proceeds by rearrangements of the &amp;quot;tree bisection and reconnection&amp;quot; type (TBR). We are using the model selected by modeltest, AND the parameter estimates found by modeltest on that model. You could also have chosen to simply estimate all the model parameters as part of this step (i.e., at the same time as finding the best tree), but fixing them improves speed tremendously. Findind the best tree should take a few minutes at most.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Save best tree to file&#039;&#039;&#039;&lt;br /&gt;
 savetrees format=newick brlens=yes file=gp120tree.phy from=1 to=1&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Quit program&#039;&#039;&#039;&lt;br /&gt;
 quit&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Have a look at the tree:&#039;&#039;&#039;&lt;br /&gt;
: You have now produced an unrooted tree of the HIV sequences and saved it in the file gp120tree.phy. Note that in this exercise we will not be interested in the tree as such - our focus is instead on finding positive selection on a subset of codon positions and the tree is just something we need in order to be able to fit the different codon models to the data. If you want to see the tree, you can do so with the following command:&lt;br /&gt;
 figtree gp120tree.phy &amp;amp;&lt;br /&gt;
: There is no meaningful root placed in this tree, so you may want to choose the unrooted view (the third icon in the Layout section of the figtree window). Close the figtree window when you have had a look&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Question: &#039;&#039;&#039;What is the negative log likelihood of the tree you just found?&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
== Detection of positively selected sites in gp120 ==&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Question 6&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
: There is much more to phylogenetic analyses than merely reconstructing trees. One interesting result of probabilistic methods, is that the parameters of a model will have their values estimated as part of the optimization procedure. This means that once such a model has been fitted to the data, it is possible to investigate these estimated parameter values to learn features about the evolutionary history of the sequences under investigation. In the present example we will focus on investigating whether we can find positively selected sites in our data set, defined as sites where the dN/dS ratio is larger than 1. We do that by using a codon substitution model where the dN/dS ratio is one of its parameters.&lt;br /&gt;
&lt;br /&gt;
: A further strength of the probabilistic approach is that you get a probabilistic measure of how well any model fits the data. This means you can use a stringent approach to determine which model fits the data best. In this framework one uses likelihoods (probabilities of data given model) to determine which model fits the data best. As you saw above, it is for instance possible to compute AIC values and weights (model probabilities) from the likelihood values of fitted models, Since each model essentially corresponds to a hypothesis about the evolutionary history of the data, we can thus use a stringent statistical approach to decide which hypothesis best describes our data.&lt;br /&gt;
&lt;br /&gt;
: In outline, you will now use the following steps to investigate whether there is any evidence for positively selected codons in your data set:&lt;br /&gt;
&lt;br /&gt;
:* Fit model M1, which assumes there are two classes of codons in the sequence: some with dN/dS &amp;lt; 1, some with dN/dS=1.&lt;br /&gt;
:* Fit model M2, which assumes 3 distinct classes of codons: two with dN/dS ratios as for M1, and one extra class with dN/dS &amp;gt; 1.&lt;br /&gt;
:* Assess the strength of evidence for the two models using AIC-based model probabilities&lt;br /&gt;
:* If M2 is better: identify the positively selected codons&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Inspect the parameter file&#039;&#039;&#039;&lt;br /&gt;
 nedit codeml.ctl &amp;amp;&lt;br /&gt;
: The file &amp;quot;codeml.ctl&amp;quot; contains several settings that are relevant for running the program &#039;&#039;&#039;codeml&#039;&#039;&#039;. Find the following lines and ensure that the file contains these values:&lt;br /&gt;
 &#039;&#039;&#039;seqfile =  gp120align.fasta&#039;&#039;&#039;:  name of alignment file&lt;br /&gt;
 &#039;&#039;&#039;treefile =  gp120tree.phy&#039;&#039;&#039;: name of tree file&lt;br /&gt;
 &#039;&#039;&#039;seqtype = 1&#039;&#039;&#039;: tells the program that our data consists of coding DNA.&lt;br /&gt;
 &#039;&#039;&#039;NSsites = 1 2&#039;&#039;&#039; : tells the program to analyze models M1 and M2.&lt;br /&gt;
 &#039;&#039;&#039;cleandata = 0&#039;&#039;&#039;: tells the program to keep positions with gaps.&lt;br /&gt;
&lt;br /&gt;
: The settings entered by us will cause codeml to analyze two hypotheses about dN/dS ratios. M1 says there are two classes of codons with different dN/dS ratios in the sequence: one class with dN/dS &amp;lt; 1 (codons under purifying or negative selection), and one class with dN/dS=1 (no selection - neutrally evolving sites). M2 says there are 3 distinct dN/dS ratios for different sites in the sequence: one class with dN/dS &amp;lt; 1, one class with dN/dS=1 (these are the same type of classes as for M1), and one class with dN/dS &amp;gt; 1 (corresponding to sites under positive selection). The value of the dN/dS ratios (for those classes that have dN/dS &amp;lt; 1 or dN/dS &amp;gt; 1), the fraction of sites belonging to each class, and the position of sites belonging to each class, are unknown at first and will be determined during the analysis.&lt;br /&gt;
&lt;br /&gt;
: (Regarding &amp;quot;cleandata&amp;quot; setting: &amp;quot;cleandata = 1&amp;quot; would cause codeml to discard any alignment columns with gaps or ambiguity symbols. For alignments with many sequences this can lead to discarding the majority of sites and I would therefore recommend not using that setting. At the same time you do want to be wary of columns where most sequences have gaps, since inference of selection for these will be very uncertain. It might be better to discard such columns from an alignment before doing the codeml analysis).&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Start the analysis&#039;&#039;&#039;&lt;br /&gt;
 codeml&lt;br /&gt;
: This will start the codeml program using the settings in the file codeml.ctl. Depending on your computer, this will take some minutes to finish. (You may be able to see how the optimization procedure results in progressively better fits: the likelihood increases, meaning that negative log-likelihood decreases, as the fit improves).&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Inspect result file:&#039;&#039;&#039;&lt;br /&gt;
: Wait for the run to finish, and then look at the result file:&lt;br /&gt;
 nedit selection.results &amp;amp;&lt;br /&gt;
: This file contains a wealth of information concerning your analysis. You may want to turn off line-wrapping to more clearly see the structure of the file (there will be some long lines that will otherwise wrap around). The top part of the file gives an overview of your sequences, codon usage and nucleotide frequencies. You can ignore this information for now, and move on to the interesting part, namely the model likelihoods and parameter values:&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Find likelihood, and number of free parameters for model M1&#039;&#039;&#039;&lt;br /&gt;
 Search ==&amp;gt; Find... ==&amp;gt; enter &amp;quot;Model 1&amp;quot; and click Find&lt;br /&gt;
: You are now in the region of the result file where the model likelihoods and parameter estimates are noted. Now, locate a line that looks a bit like the one shown below:&lt;br /&gt;
 lnL(ntime: 72  np: 74):  -4242.470345     +0.000000&lt;br /&gt;
: Identify the number of &amp;quot;free parameters&amp;quot;, K, used in model M1: This is indicated by &amp;quot;np&amp;quot;, and is 74 in the example shown above (most of these parameters are branch lengths in the tree; specifically, the number of branch length parameters is indicated by &amp;quot;ntime&amp;quot;, and is 72 in this example). Also note the log-likelihood of the fitted model. This is the number right after the parenthesis, and is -4242.470345 in the example here.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Question: &#039;&#039;&#039;What are the values of K and lnL for model M1?&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Question 7&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Find dn/dS ratios and codon class proportions for model M1:&#039;&#039;&#039;&lt;br /&gt;
: Scroll down a few lines until you get to a small table similar to this:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
      dN/dS for site classes (K=2)&lt;br /&gt;
      p:   0.75111  0.24889&lt;br /&gt;
      w:   0.06583  1.00000&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
: This gives a summary of the dN/dS ratios that were found in the data set. The line starting &amp;quot;w:&amp;quot; lists the two dN/dS ratios that were found (in this case 0.06583 and 1.00000 - the last one was pre-specified by us as part of the model and was therefore not a free parameter). In this context &amp;quot;w&amp;quot; means &amp;quot;omega&amp;quot;, which is the symbol typically used to represent the dN/dS rate ratio. The line starting p: gives the proportion of codon sites belonging to each of the dN/dS ratio classes (in the example above approximately 75% belong to the first class , while 25% of all sites belong to the class having dN/dS=1.00000).&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Question: &#039;&#039;&#039;What are the dN/dS value (w) and proportion (p) of sites for both classes. Report the following values: p(class1), w(class1), p(class2), w(class2)&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Question 8&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Find likelihood, and K for model M2&#039;&#039;&#039;&lt;br /&gt;
: Scroll past the M1 output until you get to the results for model M2.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Question: &#039;&#039;&#039;What are the values of K and lnL for model M2?&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Question 9&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Find dn/dS ratios and codon class proportions for model M2:&#039;&#039;&#039;&lt;br /&gt;
: Now, scroll down a few lines until you get to a small table similar to the one you examined for M1 before. For this model there are 3 separate classes of codons.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Question: &#039;&#039;&#039;What are the dN/dS value (w) and proportion (p) of sites for all three classes? Report these values: p(class1), w(class1), p(class2), w(class2), p(class3), w(class3)&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Question 10&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Assess strength of evidence for models M1 and M2:&#039;&#039;&#039;&lt;br /&gt;
: M2 will always have a better (higher) log-likelihood than model M1 because M2 has more free parameters, and M1 is nested within M2. You should now use the recipe given above to compute AIC values and model probabilities for M1 and M2.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Question: &#039;&#039;&#039; Report: AIC, ΔAIC, weight (model probability) for M1 and M2&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Question 11: &#039;&#039;&#039; Is M2 better than M1?&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Question 12&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Examine list of positively selected sites&#039;&#039;&#039;&lt;br /&gt;
: If your M2 is clearly better than M1 (I firmly believe it should be if you did things according to instructions...), then you have evidence for the existence of positively selected sites in the gp120 gene. Now, scroll down to the end of the result file and locate a list similar to the one below. Note: This is the &amp;quot;Bayes Empirical Bayes&amp;quot; table, not the &amp;quot;Naive Empirical Bayes&amp;quot; table. &lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Bayes Empirical Bayes (BEB) analysis&lt;br /&gt;
Positively selected sites&lt;br /&gt;
&lt;br /&gt;
         Prob(w&amp;gt;1)     mean w&lt;br /&gt;
&lt;br /&gt;
    25 A 0.959*        3.133 +- 0.769&lt;br /&gt;
    27 P 0.906         2.990 +- 0.877&lt;br /&gt;
    56 K 0.987*        3.197 +- 0.687&lt;br /&gt;
    59 V 0.915         3.032 +- 0.873&lt;br /&gt;
    78 R 0.637         2.351 +- 1.129&lt;br /&gt;
    88 K 0.573         2.148 +- 1.077&lt;br /&gt;
    95 V 0.925         3.046 +- 0.843&lt;br /&gt;
    ...&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
: It is not important what the distinction is in this context, but very briefly NEB ignores the fact that there is uncertainty about  maximum likelihood estimates, especially for smaller data sets (for instance w for some codon is perhaps not exactly 3.046, but could be in a region around that value), while [https://pubmed.ncbi.nlm.nih.gov/15689528/ BEB accounts for that uncertainty].&lt;br /&gt;
: This gives you a list of which residues (if any) that were found to belong to the positively selected dN/dS-class. Also listed is the probability that the site really is in the codon class where dN/dS &amp;gt; 1, and a weighted average of the w at the site. Using only DNA sequences you have now identified likely epitopes on the gp120 protein.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Question: &#039;&#039;&#039;List all sites having more than 95% probability of belonging to the positively selected class&lt;/div&gt;</summary>
		<author><name>Gorm</name></author>
	</entry>
	<entry>
		<id>https://teaching.healthtech.dtu.dk/22115/index.php?title=Model_selection&amp;diff=265</id>
		<title>Model selection</title>
		<link rel="alternate" type="text/html" href="https://teaching.healthtech.dtu.dk/22115/index.php?title=Model_selection&amp;diff=265"/>
		<updated>2026-04-15T06:55:26Z</updated>

		<summary type="html">&lt;p&gt;Gorm: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;This exercise is part of the course  [[22115_-_Computational_Molecular_Evolution|Computational Molecular Evolution (22115)]].&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Overview ==&lt;br /&gt;
&lt;br /&gt;
: In this exercise you are going to investigate features of HIV-1 evolution. You will do this by analyzing a large set of env-genes from HIV-1, subtype B. specifically, the DNA sequences analyzed here correspond to a region surrounding the hypervariable V3 region of the gp120 protein.&lt;br /&gt;
&lt;br /&gt;
: Like other retroviruses, particles of HIV are made up of 2 copies of a single-stranded RNA genome packaged inside a protein core, or capsid. The core particle also contains viral proteins that are essential for the early steps of the virus life cycle, such as reverse transcription and integration. A lipid envelope, derived from the infected cell, surrounds the core particle. Embedded in this envelope are the surface glycoproteins of HIV: gp120 and gp41. The gp120 protein is crucial for binding of the virus particle to target cells, while gp41 is important for the subsequent fusion event. It is the specific affinity of gp120 for the CD4 protein that targets HIV to those cells of the immune system that express CD4 on their surface (e.g., T-helper lymphocytes, monocytes, and macrophages).&lt;br /&gt;
&lt;br /&gt;
: The role gp120 plays in infection and the fact that it is situated on the surface of the HIV particle, means it is an obvious target for the immune response. That means that there may be a considerable selective pressure on gp120 for creating immune-escape mutants, where amino acids in the gp120 epitopes have been substituted. In this exercise you will construct a maximum likelihood tree that we will subsequently use to investigate whether you can detect such a selective pressure on parts of gp120, again using maximum likelihood methods.&lt;br /&gt;
&lt;br /&gt;
: One major goal with the exercise is to introduce you to statistically based methods for assessing the strength of evidence for a set of alternative hypotheses about some biological system of interest. The model selection method we will use is AIC (Akaike Information Criterion), based on which you will compute model probabilities. A second goal is to make you aware that phylogenetic analysis is not only about constructing trees, but that it is also a useful framework for analyzing biological questions more generally.&lt;br /&gt;
&lt;br /&gt;
: Specifically, you will&lt;br /&gt;
&lt;br /&gt;
:# perform a multiple alignment of gp120 DNA sequences taking protein-level information into account (using revtrans).&lt;br /&gt;
:# select a suitable nucleotide substitution model (using jmodeltest2)&lt;br /&gt;
:# construct a phylogenetic tree (using PAUP).&lt;br /&gt;
:# try to detect positively selected sites in gp120 (using PAML).&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
== Recipe for computing AIC values and model probabilities ==&lt;br /&gt;
&lt;br /&gt;
: Later in today&#039;s exercise you will be asked to compute AIC values and model probabilities. Return to this section and follow the instructions when you need to do so.&lt;br /&gt;
&lt;br /&gt;
:# Fit a set of models to your data, note the maximized log likelihoods (lnL) and the number of free parameters (K) for each model in the investigated set. The models you fit should represent a plausible and comprehensive set of hypotheses about your data.&lt;br /&gt;
:# Compute AIC for each of the models: &#039;&#039;&#039;AIC = -2 x lnL + 2K&#039;&#039;&#039;.  &amp;lt;br&amp;gt;For example: a model with lnL = -2010 and K = 5 will have AIC = -2 x -2010 + 2 x 5 = 4030.&lt;br /&gt;
:# Identify the model with the smallest AIC (this is the best model in the set). We will call the AIC for this model &#039;&#039;&#039;&amp;quot;AICmin&amp;quot;&#039;&#039;&#039;.&lt;br /&gt;
:# Compute the &amp;quot;ΔAIC&amp;quot; values for each model: &#039;&#039;&#039;ΔAIC = AIC - AICmin&#039;&#039;&#039; &amp;lt;br&amp;gt;For each model subtract the minimum AIC value. The best model will have a ΔAIC of zero. The rest of the models will have positive ΔAICs.&lt;br /&gt;
:# For each model compute the following quantity: &#039;&#039;&#039;numerator = exp(-0.5 x ΔAIC)&#039;&#039;&#039; &amp;lt;br&amp;gt;For example, a model with ΔAIC=4.2 will have numerator = exp(-0.5 x 4.2) = exp(-2.1) = 0.1225. Also compute the &#039;&#039;&#039;sum&#039;&#039;&#039; of the numerator values for all models.&lt;br /&gt;
:# Finally, the model probabilities (also called model weights) for each model are found as:&amp;lt;br&amp;gt; &#039;&#039;&#039;P(model) = numerator / sum&#039;&#039;&#039; &amp;lt;br&amp;gt;For example, if sum = 3.75 and a model has numerator = 1.3, then it has P(model) = 1.3 / 3.75 = 0.35&lt;br /&gt;
&lt;br /&gt;
: You may want to keep track of the computations by constructing a table along the following lines:&lt;br /&gt;
[[File:Molevol-Downloads-aictable.png|700px]]&lt;br /&gt;
&lt;br /&gt;
: Note that model probabilities can also be computed using Bayesian methods. One advantage of Bayesian methods over AIC is that instead of relying on a point estimate, uncertainty about parameter values is accounted for by integrating over all possible values (typically using MCMC).&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
== Getting started ==&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Create working directory, copy files&#039;&#039;&#039;&lt;br /&gt;
: In the command below: Instead of /path/to/molevol enter the path to the directory where you have placed your course files (for instance cd /Users/bob/Documents/molevol, or cd /home/student/molevol).&lt;br /&gt;
 cd /path/to/molevol&lt;br /&gt;
 mkdir modelselect&lt;br /&gt;
 cd modelselect&lt;br /&gt;
 cp ../data/gp120.fasta ./gp120.fasta&lt;br /&gt;
 cp ../data/codeml.ctl ./codeml.ctl&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Have a look at the DNA data file:&#039;&#039;&#039;&lt;br /&gt;
 nedit gp120.fasta &amp;amp;&lt;br /&gt;
: The file contains several DNA sequences from HIV-1, subtype B. The sequences are approximately 500 bp long, and correspond to a region surrounding the hypervariable V3 region in the gene encoding gp120. Close the nedit window when you&#039;ve had a look.&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
== Protein-guided alignment of coding DNA ==&lt;br /&gt;
&lt;br /&gt;
: There are three reasons why it is preferable to align coding DNA at the amino acid level rather than directly as nucleotides. First, the DNA alphabet has only four letters compared to 20 amino acids, which means that even unrelated DNA sequences will share roughly 25% identity by chance alone, making it much harder to distinguish true homology from noise. Second, protein alignments benefit from empirical substitution matrices such as BLOSUM-62, which capture the fact that amino acid replacements tend to be conservative. Equivalent matrices for DNA are far less informative because nucleotide substitution patterns vary strongly between genes and organisms. Third, synonymous substitutions accumulate faster than non-synonymous ones, so DNA sequences diverge more rapidly than the proteins they encode. Taken together, these factors mean that the phylogenetic signal in a DNA alignment erodes much faster than in the corresponding protein alignment.&lt;br /&gt;
&lt;br /&gt;
: However, while protein-level alignment gives us the most reliable homology signal, it discards exactly the information we need for studying selection: the synonymous substitutions. Estimating the ratio of non-synonymous to synonymous substitution rates (dN/dS) requires a codon-level alignment where analogous codon positions are properly lined up. We would therefore like to construct a multiple alignment at the DNA level, but using information at the protein level, and the RevTrans server does exactly that.&lt;br /&gt;
&lt;br /&gt;
: &#039;&#039;&#039;RevTrans&#039;&#039;&#039; takes as input an unaligned set of DNA sequences, automatically translates them to the equivalent amino acid sequences, constructs a multiple alignment of the protein sequences, and finally uses the protein alignment as a template for constructing a multiple DNA alignment. Because each amino acid maps back to its original codon, gaps are always inserted in groups of three and codon boundaries are preserved throughout the alignment. This is essential for the dN/dS analyses we will carry out in this exercise: if codon positions are misaligned, synonymous and non-synonymous changes cannot be correctly distinguished.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Construct RevTrans alignment&#039;&#039;&#039;&lt;br /&gt;
:* Open RevTrans server page: https://services.healthtech.dtu.dk/services/RevTrans-2.0/&lt;br /&gt;
:* On the RevTrans page: Choose the file gp120.fasta as input (or copy and paste the sequence into the sequence window)&lt;br /&gt;
:* Click the &amp;quot;Submit query&amp;quot; button&lt;br /&gt;
:* When the alignment is done you may have to click link named &amp;quot;here&amp;quot; to go to results page&lt;br /&gt;
:* Download DNA alignment, by right-clicking the link for &amp;quot;Download alignment in FASTA format&amp;quot;, and choosing &amp;quot;Save link as...&amp;quot; (save file under the name gp120align.fasta and make sure to save the file in the directory modelselect).&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Convert alignment to NEXUS format&#039;&#039;&#039;&lt;br /&gt;
: Convert the fasta file to NEXUS format and save file in the modelselect directory under the name gp120.nexus&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
== Model selection using AIC and jModelTest2 ==&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Question 1&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
: As part of the present analysis we are going to build a phylogenetic tree based on the DNA alignment constructed above. We will construct the tree using maximum likelihood, but to do that we first have to decide which substitution model we want to use. Specifically, we are interested in selecting a model that achieves a good balance between fit to the data and number of parameters. We will investigate this issue by fitting a set of 56 different models to our data and then comparing them using AIC.&lt;br /&gt;
&lt;br /&gt;
: In the first part of the exercise, however, you will only compare three closely related models by hand. The purpose of this restricted comparison is not primarily to identify the globally best model, but to understand how AIC, ΔAIC, and Akaike weights are computed and interpreted. Because only three models are included in this manual comparison, the weights you compute will describe relative support &#039;&#039;&#039;within this restricted set only&#039;&#039;&#039;.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Start jmodeltest2&#039;&#039;&#039;&lt;br /&gt;
 jmodeltest&lt;br /&gt;
&#039;&#039;&#039;Load data set&#039;&#039;&#039;&lt;br /&gt;
:* File -&amp;gt; Load DNA alignment -&amp;gt; File Format -&amp;gt; Select &amp;quot;All files&amp;quot;&lt;br /&gt;
:* Navigate to gp120.nexus and load it&lt;br /&gt;
&#039;&#039;&#039;Fit 56 models&#039;&#039;&#039;&lt;br /&gt;
:* Analysis -&amp;gt; Compute likelihood scores&lt;br /&gt;
:* Select &amp;quot;7&amp;quot; under &amp;quot;Number of substitution schemes&amp;quot;&lt;br /&gt;
:* Select &amp;quot;Fixed BIONJ-JC&amp;quot; under &amp;quot;Base tree for likelihood calculations&amp;quot;&lt;br /&gt;
:* Click &amp;quot;Compute likelihoods&amp;quot;&lt;br /&gt;
&lt;br /&gt;
: This causes jmodeltest2 to perform the following actions: first a neighbor joining tree is constructed using the Jukes and Cantor model. Then the tree is fixed and used as the basis for fitting a set of 56 different models to the data. For each model, the estimated model parameters and the negative log-likelihood are recorded. In addition to varying sets of substitution rate parameters (JC, K2P, ...), some of these models also include extra parameters that take into account the presence of different rates between sites. This is done in two ways: (1) by fitting a gamma distribution of rates (&amp;quot;+G&amp;quot;), and (2) by allowing for a proportion of constant (&amp;quot;invariable&amp;quot;) sites (&amp;quot;+I&amp;quot;).&lt;br /&gt;
&lt;br /&gt;
: Wait until jmodeltest2 is done fitting all 56 models (this will take a little while depending on your computer).&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Inspect result&#039;&#039;&#039;&lt;br /&gt;
:* Results -&amp;gt; Show results table&lt;br /&gt;
: For each model this table lists the negative log-likelihood (&amp;quot;-lnL&amp;quot;), the number of parameters (&amp;quot;p&amp;quot; - note this includes branch lengths), and estimates of all model parameters (excluding branch lengths).&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Manually compute Akaike weights for three substitution models&#039;&#039;&#039;&lt;br /&gt;
: Use AIC-based model comparison to investigate which of the following three substitution models are best supported as descriptions of how the sequences have evolved:&lt;br /&gt;
:* Jukes and Cantor with fraction of invariant sites (JC+I)&lt;br /&gt;
:* Jukes and Cantor with gamma-distributed rates over sites (JC+G)&lt;br /&gt;
:* Jukes and Cantor with invariant sites and gamma-distributed rates (JC+I+G)&lt;br /&gt;
: Before you can do the computation you need to know the log likelihood and the number of parameters for each model. Locate these values in the table for the JC+I, JC+G, and JC+I+G models, and write them down. Close the window with the result table when you are done.&lt;br /&gt;
&lt;br /&gt;
: Make sure to get the signs right: the values reported in the table are -lnL values, so you will need to reverse the sign to get the lnL (the lnL values you write down should be negative).&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Question: &#039;&#039;&#039;Use the recipe above to compute AIC, ΔAIC, and Akaike weight for the three models. Report the results in a table similar to the one shown above, and verify that the three weights sum to 1 (up to rounding error).&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Question 2&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
: Based on the Akaike weights, which of the three models has the strongest support? Is the support strongly concentrated on one model, or is there still substantial support for one or both of the others?&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Question 3&#039;&#039;&#039;&lt;br /&gt;
&#039;&#039;&#039;Use modeltest program to select best model&#039;&#039;&#039;&lt;br /&gt;
: What you just did manually for JC+I, JC+G and JC+I+G, jmodeltest2 can do automatically for the full set of 56 fitted models. Specifically, it uses the list of negative log likelihoods and parameter counts in the table to compute AIC values and Akaike weights for the full candidate set:&lt;br /&gt;
:* Analysis -&amp;gt; Do AIC calculations -&amp;gt;&lt;br /&gt;
:* Select &amp;quot;Write PAUP* block&amp;quot;&lt;br /&gt;
:* click &amp;quot;Do AIC calculations&amp;quot;&lt;br /&gt;
:* Results -&amp;gt; Show results table&lt;br /&gt;
:* Select &amp;quot;AIC&amp;quot; tab&lt;br /&gt;
:* &#039;&#039;&#039;IMPORTANT:&#039;&#039;&#039; SHIFT+click on the header of the &amp;quot;weight&amp;quot; column. This sorts the rows according to model weight, in descending order.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Question: &#039;&#039;&#039;What model was selected by modeltest based on the AIC values? (This will be the model with the highest weight - and lowest AIC - and will be in the first row of the results table after SHIFT-clicking the &amp;quot;weight&amp;quot; header).&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Question 4&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
: In the same AIC results table, now click (not SHIFT+click) the header of the &amp;quot;-lnL&amp;quot; column. In this table, a normal click sorts in ascending order, whereas SHIFT+click sorts in descending order. The model at the top will therefore be the one with the smallest value of &amp;quot;-lnL&amp;quot; (equivalently: the highest likelihood). Compare this model to the one with the highest Akaike weight.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Question: &#039;&#039;&#039;Is the model with the highest likelihood also the model with the highest Akaike weight? If not, explain briefly why these two rankings can differ.&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
== Construction of phylogenetic tree using PAUP ==&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Question 5&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
: Close the results table. In the main window you should now scroll up to the lines giving PAUP commands that will implement the selected model. The command is enclosed between &amp;quot;BEGIN PAUP&amp;quot; and &amp;quot;END;&amp;quot; and should look something like this:&lt;br /&gt;
 Lset Base=(0.4064, [...]&lt;br /&gt;
: You will need to copy this command to a PAUP session in the next step.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Start PAUP&#039;&#039;&#039;&lt;br /&gt;
 paup&lt;br /&gt;
: Above you used jmodeltest2 to select the most suitable substitution model for the present data set. You will now use this model to construct a maximum likelihood tree. You will use PAUP for this purpose. (note: it is possible to create a maximum likelihood or a model-averaged tree directly from the jmodeltest2 program, but we will instead do it in PAUP in order to more clearly see each step that is taken).&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Load alignment:&#039;&#039;&#039;&lt;br /&gt;
 execute gp120.nexus&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Set tree-building criterion to maximum likelihood&#039;&#039;&#039;&lt;br /&gt;
 set criterion=likelihood&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Set model parameters to winning estimates&#039;&#039;&#039;&lt;br /&gt;
: Above you located a set of lines in the jmodeltest2 output giving a PAUP command that sets the model parameters to the estimates that were found using the winning model. Copy and paste this lset command (without the BEGIN and END parts) into the window where PAUP is running.&lt;br /&gt;
&lt;br /&gt;
 PASTE LSET COMMAND FROM MODELTEST RUN HERE&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Find best tree using selected model&#039;&#039;&#039;&lt;br /&gt;
: Still in the PAUP-window, enter the following command&lt;br /&gt;
 hsearch swap=tbr start=nj&lt;br /&gt;
: This command causes PAUP to perform a heuristic search for the best maximum likelihood tree. Once an initial tree has been constructed, the heuristic search proceeds by rearrangements of the &amp;quot;tree bisection and reconnection&amp;quot; type (TBR). We are using the model selected by modeltest, AND the parameter estimates found by modeltest on that model. You could also have chosen to simply estimate all the model parameters as part of this step (i.e., at the same time as finding the best tree), but fixing them improves speed tremendously. Findind the best tree should take a few minutes at most.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Save best tree to file&#039;&#039;&#039;&lt;br /&gt;
 savetrees format=newick brlens=yes file=gp120tree.phy from=1 to=1&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Quit program&#039;&#039;&#039;&lt;br /&gt;
 quit&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Have a look at the tree:&#039;&#039;&#039;&lt;br /&gt;
: You have now produced an unrooted tree of the HIV sequences and saved it in the file gp120tree.phy. Note that in this exercise we will not be interested in the tree as such - our focus is instead on finding positive selection on a subset of codon positions and the tree is just something we need in order to be able to fit the different codon models to the data. If you want to see the tree, you can do so with the following command:&lt;br /&gt;
 figtree gp120tree.phy &amp;amp;&lt;br /&gt;
: There is no meaningful root placed in this tree, so you may want to choose the unrooted view (the third icon in the Layout section of the figtree window). Close the figtree window when you have had a look&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Question: &#039;&#039;&#039;What is the negative log likelihood of the tree you just found?&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
== Detection of positively selected sites in gp120 ==&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Question 6&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
: There is much more to phylogenetic analyses than merely reconstructing trees. One interesting result of probabilistic methods, is that the parameters of a model will have their values estimated as part of the optimization procedure. This means that once such a model has been fitted to the data, it is possible to investigate these estimated parameter values to learn features about the evolutionary history of the sequences under investigation. In the present example we will focus on investigating whether we can find positively selected sites in our data set, defined as sites where the dN/dS ratio is larger than 1. We do that by using a codon substitution model where the dN/dS ratio is one of its parameters.&lt;br /&gt;
&lt;br /&gt;
: A further strength of the probabilistic approach is that you get a probabilistic measure of how well any model fits the data. This means you can use a stringent approach to determine which model fits the data best. In this framework one uses likelihoods (probabilities of data given model) to determine which model fits the data best. As you saw above, it is for instance possible to compute AIC values and weights (model probabilities) from the likelihood values of fitted models, Since each model essentially corresponds to a hypothesis about the evolutionary history of the data, we can thus use a stringent statistical approach to decide which hypothesis best describes our data.&lt;br /&gt;
&lt;br /&gt;
: In outline, you will now use the following steps to investigate whether there is any evidence for positively selected codons in your data set:&lt;br /&gt;
&lt;br /&gt;
:* Fit model M1, which assumes there are two classes of codons in the sequence: some with dN/dS &amp;lt; 1, some with dN/dS=1.&lt;br /&gt;
:* Fit model M2, which assumes 3 distinct classes of codons: two with dN/dS ratios as for M1, and one extra class with dN/dS &amp;gt; 1.&lt;br /&gt;
:* Assess the strength of evidence for the two models using AIC-based model probabilities&lt;br /&gt;
:* If M2 is better: identify the positively selected codons&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Inspect the parameter file&#039;&#039;&#039;&lt;br /&gt;
 nedit codeml.ctl &amp;amp;&lt;br /&gt;
: The file &amp;quot;codeml.ctl&amp;quot; contains several settings that are relevant for running the program &#039;&#039;&#039;codeml&#039;&#039;&#039;. Find the following lines and ensure that the file contains these values:&lt;br /&gt;
 &#039;&#039;&#039;seqfile =  gp120align.fasta&#039;&#039;&#039;:  name of alignment file&lt;br /&gt;
 &#039;&#039;&#039;treefile =  gp120tree.phy&#039;&#039;&#039;: name of tree file&lt;br /&gt;
 &#039;&#039;&#039;seqtype = 1&#039;&#039;&#039;: tells the program that our data consists of coding DNA.&lt;br /&gt;
 &#039;&#039;&#039;NSsites = 1 2&#039;&#039;&#039; : tells the program to analyze models M1 and M2.&lt;br /&gt;
 &#039;&#039;&#039;cleandata = 0&#039;&#039;&#039;: tells the program to keep positions with gaps.&lt;br /&gt;
&lt;br /&gt;
: The settings entered by us will cause codeml to analyze two hypotheses about dN/dS ratios. M1 says there are two classes of codons with different dN/dS ratios in the sequence: one class with dN/dS &amp;lt; 1 (codons under purifying or negative selection), and one class with dN/dS=1 (no selection - neutrally evolving sites). M2 says there are 3 distinct dN/dS ratios for different sites in the sequence: one class with dN/dS &amp;lt; 1, one class with dN/dS=1 (these are the same type of classes as for M1), and one class with dN/dS &amp;gt; 1 (corresponding to sites under positive selection). The value of the dN/dS ratios (for those classes that have dN/dS &amp;lt; 1 or dN/dS &amp;gt; 1), the fraction of sites belonging to each class, and the position of sites belonging to each class, are unknown at first and will be determined during the analysis.&lt;br /&gt;
&lt;br /&gt;
: (Regarding &amp;quot;cleandata&amp;quot; setting: &amp;quot;cleandata = 1&amp;quot; would cause codeml to discard any alignment columns with gaps or ambiguity symbols. For alignments with many sequences this can lead to discarding the majority of sites and I would therefore recommend not using that setting. At the same time you do want to be wary of columns where most sequences have gaps, since inference of selection for these will be very uncertain. It might be better to discard such columns from an alignment before doing the codeml analysis).&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Start the analysis&#039;&#039;&#039;&lt;br /&gt;
 codeml&lt;br /&gt;
: This will start the codeml program using the settings in the file codeml.ctl. Depending on your computer, this will take some minutes to finish. (You may be able to see how the optimization procedure results in progressively better fits: the likelihood increases, meaning that negative log-likelihood decreases, as the fit improves).&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Inspect result file:&#039;&#039;&#039;&lt;br /&gt;
: Wait for the run to finish, and then look at the result file:&lt;br /&gt;
 nedit selection.results &amp;amp;&lt;br /&gt;
: This file contains a wealth of information concerning your analysis. You may want to turn off line-wrapping to more clearly see the structure of the file (there will be some long lines that will otherwise wrap around). The top part of the file gives an overview of your sequences, codon usage and nucleotide frequencies. You can ignore this information for now, and move on to the interesting part, namely the model likelihoods and parameter values:&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Find likelihood, and number of free parameters for model M1&#039;&#039;&#039;&lt;br /&gt;
 Search ==&amp;gt; Find... ==&amp;gt; enter &amp;quot;Model 1&amp;quot; and click Find&lt;br /&gt;
: You are now in the region of the result file where the model likelihoods and parameter estimates are noted. Now, locate a line that looks a bit like the one shown below:&lt;br /&gt;
 lnL(ntime: 72  np: 74):  -4242.470345     +0.000000&lt;br /&gt;
: Identify the number of &amp;quot;free parameters&amp;quot;, K, used in model M1: This is indicated by &amp;quot;np&amp;quot;, and is 74 in the example shown above (most of these parameters are branch lengths in the tree; specifically, the number of branch length parameters is indicated by &amp;quot;ntime&amp;quot;, and is 72 in this example). Also note the log-likelihood of the fitted model. This is the number right after the parenthesis, and is -4242.470345 in the example here.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Question: &#039;&#039;&#039;What are the values of K and lnL for model M1?&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Question 7&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Find dn/dS ratios and codon class proportions for model M1:&#039;&#039;&#039;&lt;br /&gt;
: Scroll down a few lines until you get to a small table similar to this:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
      dN/dS for site classes (K=2)&lt;br /&gt;
      p:   0.75111  0.24889&lt;br /&gt;
      w:   0.06583  1.00000&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
: This gives a summary of the dN/dS ratios that were found in the data set. The line starting &amp;quot;w:&amp;quot; lists the two dN/dS ratios that were found (in this case 0.06583 and 1.00000 - the last one was pre-specified by us as part of the model and was therefore not a free parameter). In this context &amp;quot;w&amp;quot; means &amp;quot;omega&amp;quot;, which is the symbol typically used to represent the dN/dS rate ratio. The line starting p: gives the proportion of codon sites belonging to each of the dN/dS ratio classes (in the example above approximately 75% belong to the first class , while 25% of all sites belong to the class having dN/dS=1.00000).&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Question: &#039;&#039;&#039;What are the dN/dS value (w) and proportion (p) of sites for both classes. Report the following values: p(class1), w(class1), p(class2), w(class2)&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Question 8&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Find likelihood, and K for model M2&#039;&#039;&#039;&lt;br /&gt;
: Scroll past the M1 output until you get to the results for model M2.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Question: &#039;&#039;&#039;What are the values of K and lnL for model M2?&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Question 9&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Find dn/dS ratios and codon class proportions for model M2:&#039;&#039;&#039;&lt;br /&gt;
: Now, scroll down a few lines until you get to a small table similar to the one you examined for M1 before. For this model there are 3 separate classes of codons.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Question: &#039;&#039;&#039;What are the dN/dS value (w) and proportion (p) of sites for all three classes? Report these values: p(class1), w(class1), p(class2), w(class2), p(class3), w(class3)&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Question 10&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Assess strength of evidence for models M1 and M2:&#039;&#039;&#039;&lt;br /&gt;
: M2 will always have a better (higher) log-likelihood than model M1 because M2 has more free parameters, and M1 is nested within M2. You should now use the recipe given above to compute AIC values and model probabilities for M1 and M2.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Question: &#039;&#039;&#039; Report: AIC, ΔAIC, weight (model probability) for M1 and M2&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Question 11: &#039;&#039;&#039; Is M2 better than M1?&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Question 12&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Examine list of positively selected sites&#039;&#039;&#039;&lt;br /&gt;
: If your M2 is clearly better than M1 (I firmly believe it should be if you did things according to instructions...), then you have evidence for the existence of positively selected sites in the gp120 gene. Now, scroll down to the end of the result file and locate a list similar to the one below. Note: This is the &amp;quot;Bayes Empirical Bayes&amp;quot; table, not the &amp;quot;Naive Empirical Bayes&amp;quot; table. &lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Bayes Empirical Bayes (BEB) analysis&lt;br /&gt;
Positively selected sites&lt;br /&gt;
&lt;br /&gt;
         Prob(w&amp;gt;1)     mean w&lt;br /&gt;
&lt;br /&gt;
    25 A 0.959*        3.133 +- 0.769&lt;br /&gt;
    27 P 0.906         2.990 +- 0.877&lt;br /&gt;
    56 K 0.987*        3.197 +- 0.687&lt;br /&gt;
    59 V 0.915         3.032 +- 0.873&lt;br /&gt;
    78 R 0.637         2.351 +- 1.129&lt;br /&gt;
    88 K 0.573         2.148 +- 1.077&lt;br /&gt;
    95 V 0.925         3.046 +- 0.843&lt;br /&gt;
    ...&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
: It is not important what the distinction is in this context, but very briefly NEB ignores the fact that there is uncertainty about  maximum likelihood estimates, especially for smaller data sets (for instance w for some codon is perhaps not exactly 3.046, but could be in a region around that value), while [https://pubmed.ncbi.nlm.nih.gov/15689528/ BEB accounts for that uncertainty].&lt;br /&gt;
: This gives you a list of which residues (if any) that were found to belong to the positively selected dN/dS-class. Also listed is the probability that the site really is in the codon class where dN/dS &amp;gt; 1, and a weighted average of the w at the site. Using only DNA sequences you have now identified likely epitopes on the gp120 protein.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Question: &#039;&#039;&#039;List all sites having more than 95% probability of belonging to the positively selected class&lt;/div&gt;</summary>
		<author><name>Gorm</name></author>
	</entry>
	<entry>
		<id>https://teaching.healthtech.dtu.dk/22115/index.php?title=Model_selection&amp;diff=264</id>
		<title>Model selection</title>
		<link rel="alternate" type="text/html" href="https://teaching.healthtech.dtu.dk/22115/index.php?title=Model_selection&amp;diff=264"/>
		<updated>2026-04-15T06:36:14Z</updated>

		<summary type="html">&lt;p&gt;Gorm: /* Detection of positively selected sites in gp120 */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;This exercise is part of the course  [[22115_-_Computational_Molecular_Evolution|Computational Molecular Evolution (22115)]].&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Overview ==&lt;br /&gt;
&lt;br /&gt;
: In this exercise you are going to investigate features of HIV-1 evolution. You will do this by analyzing a large set of env-genes from HIV-1, subtype B. specifically, the DNA sequences analyzed here correspond to a region surrounding the hypervariable V3 region of the gp120 protein.&lt;br /&gt;
&lt;br /&gt;
: Like other retroviruses, particles of HIV are made up of 2 copies of a single-stranded RNA genome packaged inside a protein core, or capsid. The core particle also contains viral proteins that are essential for the early steps of the virus life cycle, such as reverse transcription and integration. A lipid envelope, derived from the infected cell, surrounds the core particle. Embedded in this envelope are the surface glycoproteins of HIV: gp120 and gp41. The gp120 protein is crucial for binding of the virus particle to target cells, while gp41 is important for the subsequent fusion event. It is the specific affinity of gp120 for the CD4 protein that targets HIV to those cells of the immune system that express CD4 on their surface (e.g., T-helper lymphocytes, monocytes, and macrophages).&lt;br /&gt;
&lt;br /&gt;
: The role gp120 plays in infection and the fact that it is situated on the surface of the HIV particle, means it is an obvious target for the immune response. That means that there may be a considerable selective pressure on gp120 for creating immune-escape mutants, where amino acids in the gp120 epitopes have been substituted. In this exercise you will construct a maximum likelihood tree that we will subsequently use to investigate whether you can detect such a selective pressure on parts of gp120, again using maximum likelihood methods.&lt;br /&gt;
&lt;br /&gt;
: One major goal with the exercise is to introduce you to statistically based methods for assessing the strength of evidence for a set of alternative hypotheses about some biological system of interest. The model selection method we will use is AIC (Akaike Information Criterion), based on which you will compute model probabilities. A second goal is to make you aware that phylogenetic analysis is not only about constructing trees, but that it is also a useful framework for analyzing biological questions more generally.&lt;br /&gt;
&lt;br /&gt;
: Specifically, you will&lt;br /&gt;
&lt;br /&gt;
:# perform a multiple alignment of gp120 DNA sequences taking protein-level information into account (using revtrans).&lt;br /&gt;
:# select a suitable nucleotide substitution model (using jmodeltest2)&lt;br /&gt;
:# construct a phylogenetic tree (using PAUP).&lt;br /&gt;
:# try to detect positively selected sites in gp120 (using PAML).&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
== Recipe for computing AIC values and model probabilities ==&lt;br /&gt;
&lt;br /&gt;
: Later in today&#039;s exercise you will be asked to compute AIC values and model probabilities. Return to this section and follow the instructions when you need to do so.&lt;br /&gt;
&lt;br /&gt;
:# Fit a set of models to your data, note the maximized log likelihoods (lnL) and the number of free parameters (K) for each model in the investigated set. The models you fit should represent a plausible and comprehensive set of hypotheses about your data.&lt;br /&gt;
:# Compute AIC for each of the models: &#039;&#039;&#039;AIC = -2 x lnL + 2K&#039;&#039;&#039;.  &amp;lt;br&amp;gt;For example: a model with lnL = -2010 and K = 5 will have AIC = -2 x -2010 + 2 x 5 = 4030.&lt;br /&gt;
:# Identify the model with the smallest AIC (this is the best model in the set). We will call the AIC for this model &#039;&#039;&#039;&amp;quot;AICmin&amp;quot;&#039;&#039;&#039;.&lt;br /&gt;
:# Compute the &amp;quot;ΔAIC&amp;quot; values for each model: &#039;&#039;&#039;ΔAIC = AIC - AICmin&#039;&#039;&#039; &amp;lt;br&amp;gt;For each model subtract the minimum AIC value. The best model will have a ΔAIC of zero. The rest of the models will have positive ΔAICs.&lt;br /&gt;
:# For each model compute the following quantity: &#039;&#039;&#039;numerator = exp(-0.5 x ΔAIC)&#039;&#039;&#039; &amp;lt;br&amp;gt;For example, a model with ΔAIC=4.2 will have numerator = exp(-0.5 x 4.2) = exp(-2.1) = 0.1225. Also compute the &#039;&#039;&#039;sum&#039;&#039;&#039; of the numerator values for all models.&lt;br /&gt;
:# Finally, the model probabilities (also called model weights) for each model are found as:&amp;lt;br&amp;gt; &#039;&#039;&#039;P(model) = numerator / sum&#039;&#039;&#039; &amp;lt;br&amp;gt;For example, if sum = 3.75 and a model has numerator = 1.3, then it has P(model) = 1.3 / 3.75 = 0.35&lt;br /&gt;
&lt;br /&gt;
: You may want to keep track of the computations by constructing a table along the following lines:&lt;br /&gt;
[[File:Molevol-Downloads-aictable.png|700px]]&lt;br /&gt;
&lt;br /&gt;
: Note that model probabilities can also be computed using Bayesian methods. One advantage of Bayesian methods over AIC is that instead of relying on a point estimate, uncertainty about parameter values is accounted for by integrating over all possible values (typically using MCMC).&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
== Getting started ==&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Create working directory, copy files&#039;&#039;&#039;&lt;br /&gt;
: In the command below: Instead of /path/to/molevol enter the path to the directory where you have placed your course files (for instance cd /Users/bob/Documents/molevol, or cd /home/student/molevol).&lt;br /&gt;
 cd /path/to/molevol&lt;br /&gt;
 mkdir modelselect&lt;br /&gt;
 cd modelselect&lt;br /&gt;
 cp ../data/gp120.fasta ./gp120.fasta&lt;br /&gt;
 cp ../data/codeml.ctl ./codeml.ctl&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Have a look at the DNA data file:&#039;&#039;&#039;&lt;br /&gt;
 nedit gp120.fasta &amp;amp;&lt;br /&gt;
: The file contains several DNA sequences from HIV-1, subtype B. The sequences are approximately 500 bp long, and correspond to a region surrounding the hypervariable V3 region in the gene encoding gp120. Close the nedit window when you&#039;ve had a look.&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
== Analysis of viral data set: alignment of coding DNA ==&lt;br /&gt;
&lt;br /&gt;
: There are three reasons why it is preferable to align coding DNA at the amino acid level rather than directly as nucleotides. First, the DNA alphabet has only four letters compared to 20 amino acids, which means that even unrelated DNA sequences will share roughly 25% identity by chance alone, making it much harder to distinguish true homology from noise. Second, protein alignments benefit from empirical substitution matrices such as BLOSUM-62, which capture the fact that amino acid replacements tend to be conservative. Equivalent matrices for DNA are far less informative because nucleotide substitution patterns vary strongly between genes and organisms. Third, synonymous substitutions accumulate faster than non-synonymous ones, so DNA sequences diverge more rapidly than the proteins they encode. Taken together, these factors mean that the phylogenetic signal in a DNA alignment erodes much faster than in the corresponding protein alignment.&lt;br /&gt;
&lt;br /&gt;
: However, while protein-level alignment gives us the most reliable homology signal, it discards exactly the information we need for studying selection: the synonymous substitutions. Estimating the ratio of non-synonymous to synonymous substitution rates (dN/dS) requires a codon-level alignment where analogous codon positions are properly lined up. We would therefore like to construct a multiple alignment at the DNA level, but using information at the protein level, and the RevTrans server does exactly that.&lt;br /&gt;
&lt;br /&gt;
: &#039;&#039;&#039;RevTrans&#039;&#039;&#039; takes as input an unaligned set of DNA sequences, automatically translates them to the equivalent amino acid sequences, constructs a multiple alignment of the protein sequences, and finally uses the protein alignment as a template for constructing a multiple DNA alignment. Because each amino acid maps back to its original codon, gaps are always inserted in groups of three and codon boundaries are preserved throughout the alignment. This is essential for the dN/dS analyses we will carry out in this exercise: if codon positions are misaligned, synonymous and non-synonymous changes cannot be correctly distinguished.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Construct RevTrans alignment&#039;&#039;&#039;&lt;br /&gt;
:* Open RevTrans server page: https://services.healthtech.dtu.dk/services/RevTrans-2.0/&lt;br /&gt;
:* On the RevTrans page: Choose the file gp120.fasta as input (or copy and paste the sequence into the sequence window)&lt;br /&gt;
:* Click the &amp;quot;Submit query&amp;quot; button&lt;br /&gt;
:* When the alignment is done you may have to click link named &amp;quot;here&amp;quot; to go to results page&lt;br /&gt;
:* Download DNA alignment, by right-clicking the link for &amp;quot;Download alignment in FASTA format&amp;quot;, and choosing &amp;quot;Save link as...&amp;quot; (save file under the name gp120align.fasta and make sure to save the file in the directory modelselect).&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Convert alignment to NEXUS format&#039;&#039;&#039;&lt;br /&gt;
: Convert the fasta file to NEXUS format and save file in the modelselect directory under the name gp120.nexus&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Question 1&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
: As part of the present analysis we are going to build a phylogenetic tree based on the DNA alignment constructed above. We will construct the tree using maximum likelihood, but to do that we first have to decide which substitution model we want to use. Specifically, we are interested in selecting a model that achieves a good balance between fit to the data and number of parameters. We will investigate this issue by fitting a set of 56 different models to our data and then comparing them using AIC.&lt;br /&gt;
&lt;br /&gt;
: In the first part of the exercise, however, you will only compare three closely related models by hand. The purpose of this restricted comparison is not primarily to identify the globally best model, but to understand how AIC, ΔAIC, and Akaike weights are computed and interpreted. Because only three models are included in this manual comparison, the weights you compute will describe relative support &#039;&#039;&#039;within this restricted set only&#039;&#039;&#039;.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Start jmodeltest2&#039;&#039;&#039;&lt;br /&gt;
 jmodeltest&lt;br /&gt;
&#039;&#039;&#039;Load data set&#039;&#039;&#039;&lt;br /&gt;
:* File -&amp;gt; Load DNA alignment -&amp;gt; File Format -&amp;gt; Select &amp;quot;All files&amp;quot;&lt;br /&gt;
:* Navigate to gp120.nexus and load it&lt;br /&gt;
&#039;&#039;&#039;Fit 56 models&#039;&#039;&#039;&lt;br /&gt;
:* Analysis -&amp;gt; Compute likelihood scores&lt;br /&gt;
:* Select &amp;quot;7&amp;quot; under &amp;quot;Number of substitution schemes&amp;quot;&lt;br /&gt;
:* Select &amp;quot;Fixed BIONJ-JC&amp;quot; under &amp;quot;Base tree for likelihood calculations&amp;quot;&lt;br /&gt;
:* Click &amp;quot;Compute likelihoods&amp;quot;&lt;br /&gt;
&lt;br /&gt;
: This causes jmodeltest2 to perform the following actions: first a neighbor joining tree is constructed using the Jukes and Cantor model. Then the tree is fixed and used as the basis for fitting a set of 56 different models to the data. For each model, the estimated model parameters and the negative log-likelihood are recorded. In addition to varying sets of substitution rate parameters (JC, K2P, ...), some of these models also include extra parameters that take into account the presence of different rates between sites. This is done in two ways: (1) by fitting a gamma distribution of rates (&amp;quot;+G&amp;quot;), and (2) by allowing for a proportion of constant (&amp;quot;invariable&amp;quot;) sites (&amp;quot;+I&amp;quot;).&lt;br /&gt;
&lt;br /&gt;
: Wait until jmodeltest2 is done fitting all 56 models (this will take a little while depending on your computer).&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Inspect result&#039;&#039;&#039;&lt;br /&gt;
:* Results -&amp;gt; Show results table&lt;br /&gt;
: For each model this table lists the negative log-likelihood (&amp;quot;-lnL&amp;quot;), the number of parameters (&amp;quot;p&amp;quot; - note this includes branch lengths), and estimates of all model parameters (excluding branch lengths).&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Manually compute Akaike weights for three substitution models&#039;&#039;&#039;&lt;br /&gt;
: Use AIC-based model comparison to investigate which of the following three substitution models are best supported as descriptions of how the sequences have evolved:&lt;br /&gt;
:* Jukes and Cantor with fraction of invariant sites (JC+I)&lt;br /&gt;
:* Jukes and Cantor with gamma-distributed rates over sites (JC+G)&lt;br /&gt;
:* Jukes and Cantor with invariant sites and gamma-distributed rates (JC+I+G)&lt;br /&gt;
: Before you can do the computation you need to know the log likelihood and the number of parameters for each model. Locate these values in the table for the JC+I, JC+G, and JC+I+G models, and write them down. Close the window with the result table when you are done.&lt;br /&gt;
&lt;br /&gt;
: Make sure to get the signs right: the values reported in the table are -lnL values, so you will need to reverse the sign to get the lnL (the lnL values you write down should be negative).&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Question: &#039;&#039;&#039;Use the recipe above to compute AIC, ΔAIC, and Akaike weight for the three models. Report the results in a table similar to the one shown above, and verify that the three weights sum to 1 (up to rounding error).&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Question 2&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
: Based on the Akaike weights, which of the three models has the strongest support? Is the support strongly concentrated on one model, or is there still substantial support for one or both of the others?&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Question 3&#039;&#039;&#039;&lt;br /&gt;
&#039;&#039;&#039;Use modeltest program to select best model&#039;&#039;&#039;&lt;br /&gt;
: What you just did manually for JC+I, JC+G and JC+I+G, jmodeltest2 can do automatically for the full set of 56 fitted models. Specifically, it uses the list of negative log likelihoods and parameter counts in the table to compute AIC values and Akaike weights for the full candidate set:&lt;br /&gt;
:* Analysis -&amp;gt; Do AIC calculations -&amp;gt;&lt;br /&gt;
:* Select &amp;quot;Write PAUP* block&amp;quot;&lt;br /&gt;
:* click &amp;quot;Do AIC calculations&amp;quot;&lt;br /&gt;
:* Results -&amp;gt; Show results table&lt;br /&gt;
:* Select &amp;quot;AIC&amp;quot; tab&lt;br /&gt;
:* &#039;&#039;&#039;IMPORTANT:&#039;&#039;&#039; SHIFT+click on the header of the &amp;quot;weight&amp;quot; column. This sorts the rows according to model weight, in descending order.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Question: &#039;&#039;&#039;What model was selected by modeltest based on the AIC values? (This will be the model with the highest weight - and lowest AIC - and will be in the first row of the results table after SHIFT-clicking the &amp;quot;weight&amp;quot; header).&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Question 4&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
: In the same AIC results table, now click (not SHIFT+click) the header of the &amp;quot;-lnL&amp;quot; column. In this table, a normal click sorts in ascending order, whereas SHIFT+click sorts in descending order. The model at the top will therefore be the one with the smallest value of &amp;quot;-lnL&amp;quot; (equivalently: the highest likelihood). Compare this model to the one with the highest Akaike weight.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Question: &#039;&#039;&#039;Is the model with the highest likelihood also the model with the highest Akaike weight? If not, explain briefly why these two rankings can differ.&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
== Construction of phylogenetic tree using PAUP ==&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Question 5&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
: Close the results table. In the main window you should now scroll up to the lines giving PAUP commands that will implement the selected model. The command is enclosed between &amp;quot;BEGIN PAUP&amp;quot; and &amp;quot;END;&amp;quot; and should look something like this:&lt;br /&gt;
 Lset Base=(0.4064, [...]&lt;br /&gt;
: You will need to copy this command to a PAUP session in the next step.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Start PAUP&#039;&#039;&#039;&lt;br /&gt;
 paup&lt;br /&gt;
: Above you used jmodeltest2 to select the most suitable substitution model for the present data set. You will now use this model to construct a maximum likelihood tree. You will use PAUP for this purpose. (note: it is possible to create a maximum likelihood or a model-averaged tree directly from the jmodeltest2 program, but we will instead do it in PAUP in order to more clearly see each step that is taken).&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Load alignment:&#039;&#039;&#039;&lt;br /&gt;
 execute gp120.nexus&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Set tree-building criterion to maximum likelihood&#039;&#039;&#039;&lt;br /&gt;
 set criterion=likelihood&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Set model parameters to winning estimates&#039;&#039;&#039;&lt;br /&gt;
: Above you located a set of lines in the jmodeltest2 output giving a PAUP command that sets the model parameters to the estimates that were found using the winning model. Copy and paste this lset command (without the BEGIN and END parts) into the window where PAUP is running.&lt;br /&gt;
&lt;br /&gt;
 PASTE LSET COMMAND FROM MODELTEST RUN HERE&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Find best tree using selected model&#039;&#039;&#039;&lt;br /&gt;
: Still in the PAUP-window, enter the following command&lt;br /&gt;
 hsearch swap=tbr start=nj&lt;br /&gt;
: This command causes PAUP to perform a heuristic search for the best maximum likelihood tree. Once an initial tree has been constructed, the heuristic search proceeds by rearrangements of the &amp;quot;tree bisection and reconnection&amp;quot; type (TBR). We are using the model selected by modeltest, AND the parameter estimates found by modeltest on that model. You could also have chosen to simply estimate all the model parameters as part of this step (i.e., at the same time as finding the best tree), but fixing them improves speed tremendously. Findind the best tree should take a few minutes at most.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Save best tree to file&#039;&#039;&#039;&lt;br /&gt;
 savetrees format=newick brlens=yes file=gp120tree.phy from=1 to=1&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Quit program&#039;&#039;&#039;&lt;br /&gt;
 quit&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Have a look at the tree:&#039;&#039;&#039;&lt;br /&gt;
: You have now produced an unrooted tree of the HIV sequences and saved it in the file gp120tree.phy. Note that in this exercise we will not be interested in the tree as such - our focus is instead on finding positive selection on a subset of codon positions and the tree is just something we need in order to be able to fit the different codon models to the data. If you want to see the tree, you can do so with the following command:&lt;br /&gt;
 figtree gp120tree.phy &amp;amp;&lt;br /&gt;
: There is no meaningful root placed in this tree, so you may want to choose the unrooted view (the third icon in the Layout section of the figtree window). Close the figtree window when you have had a look&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Question: &#039;&#039;&#039;What is the negative log likelihood of the tree you just found?&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
== Detection of positively selected sites in gp120 ==&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Question 6&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
: There is much more to phylogenetic analyses than merely reconstructing trees. One interesting result of probabilistic methods, is that the parameters of a model will have their values estimated as part of the optimization procedure. This means that once such a model has been fitted to the data, it is possible to investigate these estimated parameter values to learn features about the evolutionary history of the sequences under investigation. In the present example we will focus on investigating whether we can find positively selected sites in our data set, defined as sites where the dN/dS ratio is larger than 1. We do that by using a codon substitution model where the dN/dS ratio is one of its parameters.&lt;br /&gt;
&lt;br /&gt;
: A further strength of the probabilistic approach is that you get a probabilistic measure of how well any model fits the data. This means you can use a stringent approach to determine which model fits the data best. In this framework one uses likelihoods (probabilities of data given model) to determine which model fits the data best. As you saw above, it is for instance possible to compute AIC values and weights (model probabilities) from the likelihood values of fitted models, Since each model essentially corresponds to a hypothesis about the evolutionary history of the data, we can thus use a stringent statistical approach to decide which hypothesis best describes our data.&lt;br /&gt;
&lt;br /&gt;
: In outline, you will now use the following steps to investigate whether there is any evidence for positively selected codons in your data set:&lt;br /&gt;
&lt;br /&gt;
:* Fit model M1, which assumes there are two classes of codons in the sequence: some with dN/dS &amp;lt; 1, some with dN/dS=1.&lt;br /&gt;
:* Fit model M2, which assumes 3 distinct classes of codons: two with dN/dS ratios as for M1, and one extra class with dN/dS &amp;gt; 1.&lt;br /&gt;
:* Assess the strength of evidence for the two models using AIC-based model probabilities&lt;br /&gt;
:* If M2 is better: identify the positively selected codons&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Inspect the parameter file&#039;&#039;&#039;&lt;br /&gt;
 nedit codeml.ctl &amp;amp;&lt;br /&gt;
: The file &amp;quot;codeml.ctl&amp;quot; contains several settings that are relevant for running the program &#039;&#039;&#039;codeml&#039;&#039;&#039;. Find the following lines and ensure that the file contains these values:&lt;br /&gt;
 &#039;&#039;&#039;seqfile =  gp120align.fasta&#039;&#039;&#039;:  name of alignment file&lt;br /&gt;
 &#039;&#039;&#039;treefile =  gp120tree.phy&#039;&#039;&#039;: name of tree file&lt;br /&gt;
 &#039;&#039;&#039;seqtype = 1&#039;&#039;&#039;: tells the program that our data consists of coding DNA.&lt;br /&gt;
 &#039;&#039;&#039;NSsites = 1 2&#039;&#039;&#039; : tells the program to analyze models M1 and M2.&lt;br /&gt;
 &#039;&#039;&#039;cleandata = 0&#039;&#039;&#039;: tells the program to keep positions with gaps.&lt;br /&gt;
&lt;br /&gt;
: The settings entered by us will cause codeml to analyze two hypotheses about dN/dS ratios. M1 says there are two classes of codons with different dN/dS ratios in the sequence: one class with dN/dS &amp;lt; 1 (codons under purifying or negative selection), and one class with dN/dS=1 (no selection - neutrally evolving sites). M2 says there are 3 distinct dN/dS ratios for different sites in the sequence: one class with dN/dS &amp;lt; 1, one class with dN/dS=1 (these are the same type of classes as for M1), and one class with dN/dS &amp;gt; 1 (corresponding to sites under positive selection). The value of the dN/dS ratios (for those classes that have dN/dS &amp;lt; 1 or dN/dS &amp;gt; 1), the fraction of sites belonging to each class, and the position of sites belonging to each class, are unknown at first and will be determined during the analysis.&lt;br /&gt;
&lt;br /&gt;
: (Regarding &amp;quot;cleandata&amp;quot; setting: &amp;quot;cleandata = 1&amp;quot; would cause codeml to discard any alignment columns with gaps or ambiguity symbols. For alignments with many sequences this can lead to discarding the majority of sites and I would therefore recommend not using that setting. At the same time you do want to be wary of columns where most sequences have gaps, since inference of selection for these will be very uncertain. It might be better to discard such columns from an alignment before doing the codeml analysis).&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Start the analysis&#039;&#039;&#039;&lt;br /&gt;
 codeml&lt;br /&gt;
: This will start the codeml program using the settings in the file codeml.ctl. Depending on your computer, this will take some minutes to finish. (You may be able to see how the optimization procedure results in progressively better fits: the likelihood increases, meaning that negative log-likelihood decreases, as the fit improves).&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Inspect result file:&#039;&#039;&#039;&lt;br /&gt;
: Wait for the run to finish, and then look at the result file:&lt;br /&gt;
 nedit selection.results &amp;amp;&lt;br /&gt;
: This file contains a wealth of information concerning your analysis. You may want to turn off line-wrapping to more clearly see the structure of the file (there will be some long lines that will otherwise wrap around). The top part of the file gives an overview of your sequences, codon usage and nucleotide frequencies. You can ignore this information for now, and move on to the interesting part, namely the model likelihoods and parameter values:&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Find likelihood, and number of free parameters for model M1&#039;&#039;&#039;&lt;br /&gt;
 Search ==&amp;gt; Find... ==&amp;gt; enter &amp;quot;Model 1&amp;quot; and click Find&lt;br /&gt;
: You are now in the region of the result file where the model likelihoods and parameter estimates are noted. Now, locate a line that looks a bit like the one shown below:&lt;br /&gt;
 lnL(ntime: 72  np: 74):  -4242.470345     +0.000000&lt;br /&gt;
: Identify the number of &amp;quot;free parameters&amp;quot;, K, used in model M1: This is indicated by &amp;quot;np&amp;quot;, and is 74 in the example shown above (most of these parameters are branch lengths in the tree; specifically, the number of branch length parameters is indicated by &amp;quot;ntime&amp;quot;, and is 72 in this example). Also note the log-likelihood of the fitted model. This is the number right after the parenthesis, and is -4242.470345 in the example here.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Question: &#039;&#039;&#039;What are the values of K and lnL for model M1?&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Question 7&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Find dn/dS ratios and codon class proportions for model M1:&#039;&#039;&#039;&lt;br /&gt;
: Scroll down a few lines until you get to a small table similar to this:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
      dN/dS for site classes (K=2)&lt;br /&gt;
      p:   0.75111  0.24889&lt;br /&gt;
      w:   0.06583  1.00000&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
: This gives a summary of the dN/dS ratios that were found in the data set. The line starting &amp;quot;w:&amp;quot; lists the two dN/dS ratios that were found (in this case 0.06583 and 1.00000 - the last one was pre-specified by us as part of the model and was therefore not a free parameter). In this context &amp;quot;w&amp;quot; means &amp;quot;omega&amp;quot;, which is the symbol typically used to represent the dN/dS rate ratio. The line starting p: gives the proportion of codon sites belonging to each of the dN/dS ratio classes (in the example above approximately 75% belong to the first class , while 25% of all sites belong to the class having dN/dS=1.00000).&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Question: &#039;&#039;&#039;What are the dN/dS value (w) and proportion (p) of sites for both classes. Report the following values: p(class1), w(class1), p(class2), w(class2)&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Question 8&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Find likelihood, and K for model M2&#039;&#039;&#039;&lt;br /&gt;
: Scroll past the M1 output until you get to the results for model M2.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Question: &#039;&#039;&#039;What are the values of K and lnL for model M2?&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Question 9&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Find dn/dS ratios and codon class proportions for model M2:&#039;&#039;&#039;&lt;br /&gt;
: Now, scroll down a few lines until you get to a small table similar to the one you examined for M1 before. For this model there are 3 separate classes of codons.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Question: &#039;&#039;&#039;What are the dN/dS value (w) and proportion (p) of sites for all three classes? Report these values: p(class1), w(class1), p(class2), w(class2), p(class3), w(class3)&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Question 10&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Assess strength of evidence for models M1 and M2:&#039;&#039;&#039;&lt;br /&gt;
: M2 will always have a better (higher) log-likelihood than model M1 because M2 has more free parameters, and M1 is nested within M2. You should now use the recipe given above to compute AIC values and model probabilities for M1 and M2.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Question: &#039;&#039;&#039; Report: AIC, ΔAIC, weight (model probability) for M1 and M2&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Question 11: &#039;&#039;&#039; Is M2 better than M1?&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Question 12&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Examine list of positively selected sites&#039;&#039;&#039;&lt;br /&gt;
: If your M2 is clearly better than M1 (I firmly believe it should be if you did things according to instructions...), then you have evidence for the existence of positively selected sites in the gp120 gene. Now, scroll down to the end of the result file and locate a list similar to the one below. Note: This is the &amp;quot;Bayes Empirical Bayes&amp;quot; table, not the &amp;quot;Naive Empirical Bayes&amp;quot; table. &lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Bayes Empirical Bayes (BEB) analysis&lt;br /&gt;
Positively selected sites&lt;br /&gt;
&lt;br /&gt;
         Prob(w&amp;gt;1)     mean w&lt;br /&gt;
&lt;br /&gt;
    25 A 0.959*        3.133 +- 0.769&lt;br /&gt;
    27 P 0.906         2.990 +- 0.877&lt;br /&gt;
    56 K 0.987*        3.197 +- 0.687&lt;br /&gt;
    59 V 0.915         3.032 +- 0.873&lt;br /&gt;
    78 R 0.637         2.351 +- 1.129&lt;br /&gt;
    88 K 0.573         2.148 +- 1.077&lt;br /&gt;
    95 V 0.925         3.046 +- 0.843&lt;br /&gt;
    ...&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
: It is not important what the distinction is in this context, but very briefly NEB ignores the fact that there is uncertainty about  maximum likelihood estimates, especially for smaller data sets (for instance w for some codon is perhaps not exactly 3.046, but could be in a region around that value), while [https://pubmed.ncbi.nlm.nih.gov/15689528/ BEB accounts for that uncertainty].&lt;br /&gt;
: This gives you a list of which residues (if any) that were found to belong to the positively selected dN/dS-class. Also listed is the probability that the site really is in the codon class where dN/dS &amp;gt; 1, and a weighted average of the w at the site. Using only DNA sequences you have now identified likely epitopes on the gp120 protein.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Question: &#039;&#039;&#039;List all sites having more than 95% probability of belonging to the positively selected class&lt;/div&gt;</summary>
		<author><name>Gorm</name></author>
	</entry>
	<entry>
		<id>https://teaching.healthtech.dtu.dk/22115/index.php?title=Model_selection&amp;diff=263</id>
		<title>Model selection</title>
		<link rel="alternate" type="text/html" href="https://teaching.healthtech.dtu.dk/22115/index.php?title=Model_selection&amp;diff=263"/>
		<updated>2026-04-15T06:35:40Z</updated>

		<summary type="html">&lt;p&gt;Gorm: /* Construction of phylogenetic tree using PAUP */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;This exercise is part of the course  [[22115_-_Computational_Molecular_Evolution|Computational Molecular Evolution (22115)]].&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Overview ==&lt;br /&gt;
&lt;br /&gt;
: In this exercise you are going to investigate features of HIV-1 evolution. You will do this by analyzing a large set of env-genes from HIV-1, subtype B. specifically, the DNA sequences analyzed here correspond to a region surrounding the hypervariable V3 region of the gp120 protein.&lt;br /&gt;
&lt;br /&gt;
: Like other retroviruses, particles of HIV are made up of 2 copies of a single-stranded RNA genome packaged inside a protein core, or capsid. The core particle also contains viral proteins that are essential for the early steps of the virus life cycle, such as reverse transcription and integration. A lipid envelope, derived from the infected cell, surrounds the core particle. Embedded in this envelope are the surface glycoproteins of HIV: gp120 and gp41. The gp120 protein is crucial for binding of the virus particle to target cells, while gp41 is important for the subsequent fusion event. It is the specific affinity of gp120 for the CD4 protein that targets HIV to those cells of the immune system that express CD4 on their surface (e.g., T-helper lymphocytes, monocytes, and macrophages).&lt;br /&gt;
&lt;br /&gt;
: The role gp120 plays in infection and the fact that it is situated on the surface of the HIV particle, means it is an obvious target for the immune response. That means that there may be a considerable selective pressure on gp120 for creating immune-escape mutants, where amino acids in the gp120 epitopes have been substituted. In this exercise you will construct a maximum likelihood tree that we will subsequently use to investigate whether you can detect such a selective pressure on parts of gp120, again using maximum likelihood methods.&lt;br /&gt;
&lt;br /&gt;
: One major goal with the exercise is to introduce you to statistically based methods for assessing the strength of evidence for a set of alternative hypotheses about some biological system of interest. The model selection method we will use is AIC (Akaike Information Criterion), based on which you will compute model probabilities. A second goal is to make you aware that phylogenetic analysis is not only about constructing trees, but that it is also a useful framework for analyzing biological questions more generally.&lt;br /&gt;
&lt;br /&gt;
: Specifically, you will&lt;br /&gt;
&lt;br /&gt;
:# perform a multiple alignment of gp120 DNA sequences taking protein-level information into account (using revtrans).&lt;br /&gt;
:# select a suitable nucleotide substitution model (using jmodeltest2)&lt;br /&gt;
:# construct a phylogenetic tree (using PAUP).&lt;br /&gt;
:# try to detect positively selected sites in gp120 (using PAML).&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
== Recipe for computing AIC values and model probabilities ==&lt;br /&gt;
&lt;br /&gt;
: Later in today&#039;s exercise you will be asked to compute AIC values and model probabilities. Return to this section and follow the instructions when you need to do so.&lt;br /&gt;
&lt;br /&gt;
:# Fit a set of models to your data, note the maximized log likelihoods (lnL) and the number of free parameters (K) for each model in the investigated set. The models you fit should represent a plausible and comprehensive set of hypotheses about your data.&lt;br /&gt;
:# Compute AIC for each of the models: &#039;&#039;&#039;AIC = -2 x lnL + 2K&#039;&#039;&#039;.  &amp;lt;br&amp;gt;For example: a model with lnL = -2010 and K = 5 will have AIC = -2 x -2010 + 2 x 5 = 4030.&lt;br /&gt;
:# Identify the model with the smallest AIC (this is the best model in the set). We will call the AIC for this model &#039;&#039;&#039;&amp;quot;AICmin&amp;quot;&#039;&#039;&#039;.&lt;br /&gt;
:# Compute the &amp;quot;ΔAIC&amp;quot; values for each model: &#039;&#039;&#039;ΔAIC = AIC - AICmin&#039;&#039;&#039; &amp;lt;br&amp;gt;For each model subtract the minimum AIC value. The best model will have a ΔAIC of zero. The rest of the models will have positive ΔAICs.&lt;br /&gt;
:# For each model compute the following quantity: &#039;&#039;&#039;numerator = exp(-0.5 x ΔAIC)&#039;&#039;&#039; &amp;lt;br&amp;gt;For example, a model with ΔAIC=4.2 will have numerator = exp(-0.5 x 4.2) = exp(-2.1) = 0.1225. Also compute the &#039;&#039;&#039;sum&#039;&#039;&#039; of the numerator values for all models.&lt;br /&gt;
:# Finally, the model probabilities (also called model weights) for each model are found as:&amp;lt;br&amp;gt; &#039;&#039;&#039;P(model) = numerator / sum&#039;&#039;&#039; &amp;lt;br&amp;gt;For example, if sum = 3.75 and a model has numerator = 1.3, then it has P(model) = 1.3 / 3.75 = 0.35&lt;br /&gt;
&lt;br /&gt;
: You may want to keep track of the computations by constructing a table along the following lines:&lt;br /&gt;
[[File:Molevol-Downloads-aictable.png|700px]]&lt;br /&gt;
&lt;br /&gt;
: Note that model probabilities can also be computed using Bayesian methods. One advantage of Bayesian methods over AIC is that instead of relying on a point estimate, uncertainty about parameter values is accounted for by integrating over all possible values (typically using MCMC).&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
== Getting started ==&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Create working directory, copy files&#039;&#039;&#039;&lt;br /&gt;
: In the command below: Instead of /path/to/molevol enter the path to the directory where you have placed your course files (for instance cd /Users/bob/Documents/molevol, or cd /home/student/molevol).&lt;br /&gt;
 cd /path/to/molevol&lt;br /&gt;
 mkdir modelselect&lt;br /&gt;
 cd modelselect&lt;br /&gt;
 cp ../data/gp120.fasta ./gp120.fasta&lt;br /&gt;
 cp ../data/codeml.ctl ./codeml.ctl&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Have a look at the DNA data file:&#039;&#039;&#039;&lt;br /&gt;
 nedit gp120.fasta &amp;amp;&lt;br /&gt;
: The file contains several DNA sequences from HIV-1, subtype B. The sequences are approximately 500 bp long, and correspond to a region surrounding the hypervariable V3 region in the gene encoding gp120. Close the nedit window when you&#039;ve had a look.&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
== Analysis of viral data set: alignment of coding DNA ==&lt;br /&gt;
&lt;br /&gt;
: There are three reasons why it is preferable to align coding DNA at the amino acid level rather than directly as nucleotides. First, the DNA alphabet has only four letters compared to 20 amino acids, which means that even unrelated DNA sequences will share roughly 25% identity by chance alone, making it much harder to distinguish true homology from noise. Second, protein alignments benefit from empirical substitution matrices such as BLOSUM-62, which capture the fact that amino acid replacements tend to be conservative. Equivalent matrices for DNA are far less informative because nucleotide substitution patterns vary strongly between genes and organisms. Third, synonymous substitutions accumulate faster than non-synonymous ones, so DNA sequences diverge more rapidly than the proteins they encode. Taken together, these factors mean that the phylogenetic signal in a DNA alignment erodes much faster than in the corresponding protein alignment.&lt;br /&gt;
&lt;br /&gt;
: However, while protein-level alignment gives us the most reliable homology signal, it discards exactly the information we need for studying selection: the synonymous substitutions. Estimating the ratio of non-synonymous to synonymous substitution rates (dN/dS) requires a codon-level alignment where analogous codon positions are properly lined up. We would therefore like to construct a multiple alignment at the DNA level, but using information at the protein level, and the RevTrans server does exactly that.&lt;br /&gt;
&lt;br /&gt;
: &#039;&#039;&#039;RevTrans&#039;&#039;&#039; takes as input an unaligned set of DNA sequences, automatically translates them to the equivalent amino acid sequences, constructs a multiple alignment of the protein sequences, and finally uses the protein alignment as a template for constructing a multiple DNA alignment. Because each amino acid maps back to its original codon, gaps are always inserted in groups of three and codon boundaries are preserved throughout the alignment. This is essential for the dN/dS analyses we will carry out in this exercise: if codon positions are misaligned, synonymous and non-synonymous changes cannot be correctly distinguished.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Construct RevTrans alignment&#039;&#039;&#039;&lt;br /&gt;
:* Open RevTrans server page: https://services.healthtech.dtu.dk/services/RevTrans-2.0/&lt;br /&gt;
:* On the RevTrans page: Choose the file gp120.fasta as input (or copy and paste the sequence into the sequence window)&lt;br /&gt;
:* Click the &amp;quot;Submit query&amp;quot; button&lt;br /&gt;
:* When the alignment is done you may have to click link named &amp;quot;here&amp;quot; to go to results page&lt;br /&gt;
:* Download DNA alignment, by right-clicking the link for &amp;quot;Download alignment in FASTA format&amp;quot;, and choosing &amp;quot;Save link as...&amp;quot; (save file under the name gp120align.fasta and make sure to save the file in the directory modelselect).&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Convert alignment to NEXUS format&#039;&#039;&#039;&lt;br /&gt;
: Convert the fasta file to NEXUS format and save file in the modelselect directory under the name gp120.nexus&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Question 1&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
: As part of the present analysis we are going to build a phylogenetic tree based on the DNA alignment constructed above. We will construct the tree using maximum likelihood, but to do that we first have to decide which substitution model we want to use. Specifically, we are interested in selecting a model that achieves a good balance between fit to the data and number of parameters. We will investigate this issue by fitting a set of 56 different models to our data and then comparing them using AIC.&lt;br /&gt;
&lt;br /&gt;
: In the first part of the exercise, however, you will only compare three closely related models by hand. The purpose of this restricted comparison is not primarily to identify the globally best model, but to understand how AIC, ΔAIC, and Akaike weights are computed and interpreted. Because only three models are included in this manual comparison, the weights you compute will describe relative support &#039;&#039;&#039;within this restricted set only&#039;&#039;&#039;.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Start jmodeltest2&#039;&#039;&#039;&lt;br /&gt;
 jmodeltest&lt;br /&gt;
&#039;&#039;&#039;Load data set&#039;&#039;&#039;&lt;br /&gt;
:* File -&amp;gt; Load DNA alignment -&amp;gt; File Format -&amp;gt; Select &amp;quot;All files&amp;quot;&lt;br /&gt;
:* Navigate to gp120.nexus and load it&lt;br /&gt;
&#039;&#039;&#039;Fit 56 models&#039;&#039;&#039;&lt;br /&gt;
:* Analysis -&amp;gt; Compute likelihood scores&lt;br /&gt;
:* Select &amp;quot;7&amp;quot; under &amp;quot;Number of substitution schemes&amp;quot;&lt;br /&gt;
:* Select &amp;quot;Fixed BIONJ-JC&amp;quot; under &amp;quot;Base tree for likelihood calculations&amp;quot;&lt;br /&gt;
:* Click &amp;quot;Compute likelihoods&amp;quot;&lt;br /&gt;
&lt;br /&gt;
: This causes jmodeltest2 to perform the following actions: first a neighbor joining tree is constructed using the Jukes and Cantor model. Then the tree is fixed and used as the basis for fitting a set of 56 different models to the data. For each model, the estimated model parameters and the negative log-likelihood are recorded. In addition to varying sets of substitution rate parameters (JC, K2P, ...), some of these models also include extra parameters that take into account the presence of different rates between sites. This is done in two ways: (1) by fitting a gamma distribution of rates (&amp;quot;+G&amp;quot;), and (2) by allowing for a proportion of constant (&amp;quot;invariable&amp;quot;) sites (&amp;quot;+I&amp;quot;).&lt;br /&gt;
&lt;br /&gt;
: Wait until jmodeltest2 is done fitting all 56 models (this will take a little while depending on your computer).&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Inspect result&#039;&#039;&#039;&lt;br /&gt;
:* Results -&amp;gt; Show results table&lt;br /&gt;
: For each model this table lists the negative log-likelihood (&amp;quot;-lnL&amp;quot;), the number of parameters (&amp;quot;p&amp;quot; - note this includes branch lengths), and estimates of all model parameters (excluding branch lengths).&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Manually compute Akaike weights for three substitution models&#039;&#039;&#039;&lt;br /&gt;
: Use AIC-based model comparison to investigate which of the following three substitution models are best supported as descriptions of how the sequences have evolved:&lt;br /&gt;
:* Jukes and Cantor with fraction of invariant sites (JC+I)&lt;br /&gt;
:* Jukes and Cantor with gamma-distributed rates over sites (JC+G)&lt;br /&gt;
:* Jukes and Cantor with invariant sites and gamma-distributed rates (JC+I+G)&lt;br /&gt;
: Before you can do the computation you need to know the log likelihood and the number of parameters for each model. Locate these values in the table for the JC+I, JC+G, and JC+I+G models, and write them down. Close the window with the result table when you are done.&lt;br /&gt;
&lt;br /&gt;
: Make sure to get the signs right: the values reported in the table are -lnL values, so you will need to reverse the sign to get the lnL (the lnL values you write down should be negative).&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Question: &#039;&#039;&#039;Use the recipe above to compute AIC, ΔAIC, and Akaike weight for the three models. Report the results in a table similar to the one shown above, and verify that the three weights sum to 1 (up to rounding error).&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Question 2&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
: Based on the Akaike weights, which of the three models has the strongest support? Is the support strongly concentrated on one model, or is there still substantial support for one or both of the others?&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Question 3&#039;&#039;&#039;&lt;br /&gt;
&#039;&#039;&#039;Use modeltest program to select best model&#039;&#039;&#039;&lt;br /&gt;
: What you just did manually for JC+I, JC+G and JC+I+G, jmodeltest2 can do automatically for the full set of 56 fitted models. Specifically, it uses the list of negative log likelihoods and parameter counts in the table to compute AIC values and Akaike weights for the full candidate set:&lt;br /&gt;
:* Analysis -&amp;gt; Do AIC calculations -&amp;gt;&lt;br /&gt;
:* Select &amp;quot;Write PAUP* block&amp;quot;&lt;br /&gt;
:* click &amp;quot;Do AIC calculations&amp;quot;&lt;br /&gt;
:* Results -&amp;gt; Show results table&lt;br /&gt;
:* Select &amp;quot;AIC&amp;quot; tab&lt;br /&gt;
:* &#039;&#039;&#039;IMPORTANT:&#039;&#039;&#039; SHIFT+click on the header of the &amp;quot;weight&amp;quot; column. This sorts the rows according to model weight, in descending order.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Question: &#039;&#039;&#039;What model was selected by modeltest based on the AIC values? (This will be the model with the highest weight - and lowest AIC - and will be in the first row of the results table after SHIFT-clicking the &amp;quot;weight&amp;quot; header).&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Question 4&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
: In the same AIC results table, now click (not SHIFT+click) the header of the &amp;quot;-lnL&amp;quot; column. In this table, a normal click sorts in ascending order, whereas SHIFT+click sorts in descending order. The model at the top will therefore be the one with the smallest value of &amp;quot;-lnL&amp;quot; (equivalently: the highest likelihood). Compare this model to the one with the highest Akaike weight.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Question: &#039;&#039;&#039;Is the model with the highest likelihood also the model with the highest Akaike weight? If not, explain briefly why these two rankings can differ.&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
== Construction of phylogenetic tree using PAUP ==&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Question 5&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
: Close the results table. In the main window you should now scroll up to the lines giving PAUP commands that will implement the selected model. The command is enclosed between &amp;quot;BEGIN PAUP&amp;quot; and &amp;quot;END;&amp;quot; and should look something like this:&lt;br /&gt;
 Lset Base=(0.4064, [...]&lt;br /&gt;
: You will need to copy this command to a PAUP session in the next step.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Start PAUP&#039;&#039;&#039;&lt;br /&gt;
 paup&lt;br /&gt;
: Above you used jmodeltest2 to select the most suitable substitution model for the present data set. You will now use this model to construct a maximum likelihood tree. You will use PAUP for this purpose. (note: it is possible to create a maximum likelihood or a model-averaged tree directly from the jmodeltest2 program, but we will instead do it in PAUP in order to more clearly see each step that is taken).&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Load alignment:&#039;&#039;&#039;&lt;br /&gt;
 execute gp120.nexus&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Set tree-building criterion to maximum likelihood&#039;&#039;&#039;&lt;br /&gt;
 set criterion=likelihood&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Set model parameters to winning estimates&#039;&#039;&#039;&lt;br /&gt;
: Above you located a set of lines in the jmodeltest2 output giving a PAUP command that sets the model parameters to the estimates that were found using the winning model. Copy and paste this lset command (without the BEGIN and END parts) into the window where PAUP is running.&lt;br /&gt;
&lt;br /&gt;
 PASTE LSET COMMAND FROM MODELTEST RUN HERE&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Find best tree using selected model&#039;&#039;&#039;&lt;br /&gt;
: Still in the PAUP-window, enter the following command&lt;br /&gt;
 hsearch swap=tbr start=nj&lt;br /&gt;
: This command causes PAUP to perform a heuristic search for the best maximum likelihood tree. Once an initial tree has been constructed, the heuristic search proceeds by rearrangements of the &amp;quot;tree bisection and reconnection&amp;quot; type (TBR). We are using the model selected by modeltest, AND the parameter estimates found by modeltest on that model. You could also have chosen to simply estimate all the model parameters as part of this step (i.e., at the same time as finding the best tree), but fixing them improves speed tremendously. Findind the best tree should take a few minutes at most.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Save best tree to file&#039;&#039;&#039;&lt;br /&gt;
 savetrees format=newick brlens=yes file=gp120tree.phy from=1 to=1&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Quit program&#039;&#039;&#039;&lt;br /&gt;
 quit&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Have a look at the tree:&#039;&#039;&#039;&lt;br /&gt;
: You have now produced an unrooted tree of the HIV sequences and saved it in the file gp120tree.phy. Note that in this exercise we will not be interested in the tree as such - our focus is instead on finding positive selection on a subset of codon positions and the tree is just something we need in order to be able to fit the different codon models to the data. If you want to see the tree, you can do so with the following command:&lt;br /&gt;
 figtree gp120tree.phy &amp;amp;&lt;br /&gt;
: There is no meaningful root placed in this tree, so you may want to choose the unrooted view (the third icon in the Layout section of the figtree window). Close the figtree window when you have had a look&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Question: &#039;&#039;&#039;What is the negative log likelihood of the tree you just found?&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
== Detection of positively selected sites in gp120 ==&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Question 5&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
: There is much more to phylogenetic analyses than merely reconstructing trees. One interesting result of probabilistic methods, is that the parameters of a model will have their values estimated as part of the optimization procedure. This means that once such a model has been fitted to the data, it is possible to investigate these estimated parameter values to learn features about the evolutionary history of the sequences under investigation. In the present example we will focus on investigating whether we can find positively selected sites in our data set, defined as sites where the dN/dS ratio is larger than 1. We do that by using a codon substitution model where the dN/dS ratio is one of its parameters.&lt;br /&gt;
&lt;br /&gt;
: A further strength of the probabilistic approach is that you get a probabilistic measure of how well any model fits the data. This means you can use a stringent approach to determine which model fits the data best. In this framework one uses likelihoods (probabilities of data given model) to determine which model fits the data best. As you saw above, it is for instance possible to compute AIC values and weights (model probabilities) from the likelihood values of fitted models, Since each model essentially corresponds to a hypothesis about the evolutionary history of the data, we can thus use a stringent statistical approach to decide which hypothesis best describes our data.&lt;br /&gt;
&lt;br /&gt;
: In outline, you will now use the following steps to investigate whether there is any evidence for positively selected codons in your data set:&lt;br /&gt;
&lt;br /&gt;
:* Fit model M1, which assumes there are two classes of codons in the sequence: some with dN/dS &amp;lt; 1, some with dN/dS=1.&lt;br /&gt;
:* Fit model M2, which assumes 3 distinct classes of codons: two with dN/dS ratios as for M1, and one extra class with dN/dS &amp;gt; 1.&lt;br /&gt;
:* Assess the strength of evidence for the two models using AIC-based model probabilities&lt;br /&gt;
:* If M2 is better: identify the positively selected codons&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Inspect the parameter file&#039;&#039;&#039;&lt;br /&gt;
 nedit codeml.ctl &amp;amp;&lt;br /&gt;
: The file &amp;quot;codeml.ctl&amp;quot; contains several settings that are relevant for running the program &#039;&#039;&#039;codeml&#039;&#039;&#039;. Find the following lines and ensure that the file contains these values:&lt;br /&gt;
 &#039;&#039;&#039;seqfile =  gp120align.fasta&#039;&#039;&#039;:  name of alignment file&lt;br /&gt;
 &#039;&#039;&#039;treefile =  gp120tree.phy&#039;&#039;&#039;: name of tree file&lt;br /&gt;
 &#039;&#039;&#039;seqtype = 1&#039;&#039;&#039;: tells the program that our data consists of coding DNA.&lt;br /&gt;
 &#039;&#039;&#039;NSsites = 1 2&#039;&#039;&#039; : tells the program to analyze models M1 and M2.&lt;br /&gt;
 &#039;&#039;&#039;cleandata = 0&#039;&#039;&#039;: tells the program to keep positions with gaps.&lt;br /&gt;
&lt;br /&gt;
: The settings entered by us will cause codeml to analyze two hypotheses about dN/dS ratios. M1 says there are two classes of codons with different dN/dS ratios in the sequence: one class with dN/dS &amp;lt; 1 (codons under purifying or negative selection), and one class with dN/dS=1 (no selection - neutrally evolving sites). M2 says there are 3 distinct dN/dS ratios for different sites in the sequence: one class with dN/dS &amp;lt; 1, one class with dN/dS=1 (these are the same type of classes as for M1), and one class with dN/dS &amp;gt; 1 (corresponding to sites under positive selection). The value of the dN/dS ratios (for those classes that have dN/dS &amp;lt; 1 or dN/dS &amp;gt; 1), the fraction of sites belonging to each class, and the position of sites belonging to each class, are unknown at first and will be determined during the analysis.&lt;br /&gt;
&lt;br /&gt;
: (Regarding &amp;quot;cleandata&amp;quot; setting: &amp;quot;cleandata = 1&amp;quot; would cause codeml to discard any alignment columns with gaps or ambiguity symbols. For alignments with many sequences this can lead to discarding the majority of sites and I would therefore recommend not using that setting. At the same time you do want to be wary of columns where most sequences have gaps, since inference of selection for these will be very uncertain. It might be better to discard such columns from an alignment before doing the codeml analysis).&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Start the analysis&#039;&#039;&#039;&lt;br /&gt;
 codeml&lt;br /&gt;
: This will start the codeml program using the settings in the file codeml.ctl. Depending on your computer, this will take some minutes to finish. (You may be able to see how the optimization procedure results in progressively better fits: the likelihood increases, meaning that negative log-likelihood decreases, as the fit improves).&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Inspect result file:&#039;&#039;&#039;&lt;br /&gt;
: Wait for the run to finish, and then look at the result file:&lt;br /&gt;
 nedit selection.results &amp;amp;&lt;br /&gt;
: This file contains a wealth of information concerning your analysis. You may want to turn off line-wrapping to more clearly see the structure of the file (there will be some long lines that will otherwise wrap around). The top part of the file gives an overview of your sequences, codon usage and nucleotide frequencies. You can ignore this information for now, and move on to the interesting part, namely the model likelihoods and parameter values:&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Find likelihood, and number of free parameters for model M1&#039;&#039;&#039;&lt;br /&gt;
 Search ==&amp;gt; Find... ==&amp;gt; enter &amp;quot;Model 1&amp;quot; and click Find&lt;br /&gt;
: You are now in the region of the result file where the model likelihoods and parameter estimates are noted. Now, locate a line that looks a bit like the one shown below:&lt;br /&gt;
 lnL(ntime: 72  np: 74):  -4242.470345     +0.000000&lt;br /&gt;
: Identify the number of &amp;quot;free parameters&amp;quot;, K, used in model M1: This is indicated by &amp;quot;np&amp;quot;, and is 74 in the example shown above (most of these parameters are branch lengths in the tree; specifically, the number of branch length parameters is indicated by &amp;quot;ntime&amp;quot;, and is 72 in this example). Also note the log-likelihood of the fitted model. This is the number right after the parenthesis, and is -4242.470345 in the example here.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Question: &#039;&#039;&#039;What are the values of K and lnL for model M1?&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Question 6&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Find dn/dS ratios and codon class proportions for model M1:&#039;&#039;&#039;&lt;br /&gt;
: Scroll down a few lines until you get to a small table similar to this:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
      dN/dS for site classes (K=2)&lt;br /&gt;
      p:   0.75111  0.24889&lt;br /&gt;
      w:   0.06583  1.00000&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
: This gives a summary of the dN/dS ratios that were found in the data set. The line starting &amp;quot;w:&amp;quot; lists the two dN/dS ratios that were found (in this case 0.06583 and 1.00000 - the last one was pre-specified by us as part of the model and was therefore not a free parameter). In this context &amp;quot;w&amp;quot; means &amp;quot;omega&amp;quot;, which is the symbol typically used to represent the dN/dS rate ratio. The line starting p: gives the proportion of codon sites belonging to each of the dN/dS ratio classes (in the example above approximately 75% belong to the first class , while 25% of all sites belong to the class having dN/dS=1.00000).&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Question: &#039;&#039;&#039;What are the dN/dS value (w) and proportion (p) of sites for both classes. Report the following values: p(class1), w(class1), p(class2), w(class2)&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Question 7&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Find likelihood, and K for model M2&#039;&#039;&#039;&lt;br /&gt;
: Scroll past the M1 output until you get to the results for model M2.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Question: &#039;&#039;&#039;What are the values of K and lnL for model M2?&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Question 8&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Find dn/dS ratios and codon class proportions for model M2:&#039;&#039;&#039;&lt;br /&gt;
: Now, scroll down a few lines until you get to a small table similar to the one you examined for M1 before. For this model there are 3 separate classes of codons.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Question: &#039;&#039;&#039;What are the dN/dS value (w) and proportion (p) of sites for all three classes? Report these values: p(class1), w(class1), p(class2), w(class2), p(class3), w(class3)&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Question 9&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Assess strength of evidence for models M1 and M2:&#039;&#039;&#039;&lt;br /&gt;
: M2 will always have a better (higher) log-likelihood than model M1 because M2 has more free parameters, and M1 is nested within M2. You should now use the recipe given above to compute AIC values and model probabilities for M1 and M2.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Question: &#039;&#039;&#039; Report: AIC, ΔAIC, weight (model probability) for M1 and M2&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Question 10: &#039;&#039;&#039; Is M2 better than M1?&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Question 11&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Examine list of positively selected sites&#039;&#039;&#039;&lt;br /&gt;
: If your M2 is clearly better than M1 (I firmly believe it should be if you did things according to instructions...), then you have evidence for the existence of positively selected sites in the gp120 gene. Now, scroll down to the end of the result file and locate a list similar to the one below. Note: This is the &amp;quot;Bayes Empirical Bayes&amp;quot; table, not the &amp;quot;Naive Empirical Bayes&amp;quot; table. &lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Bayes Empirical Bayes (BEB) analysis&lt;br /&gt;
Positively selected sites&lt;br /&gt;
&lt;br /&gt;
         Prob(w&amp;gt;1)     mean w&lt;br /&gt;
&lt;br /&gt;
    25 A 0.959*        3.133 +- 0.769&lt;br /&gt;
    27 P 0.906         2.990 +- 0.877&lt;br /&gt;
    56 K 0.987*        3.197 +- 0.687&lt;br /&gt;
    59 V 0.915         3.032 +- 0.873&lt;br /&gt;
    78 R 0.637         2.351 +- 1.129&lt;br /&gt;
    88 K 0.573         2.148 +- 1.077&lt;br /&gt;
    95 V 0.925         3.046 +- 0.843&lt;br /&gt;
    ...&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
: It is not important what the distinction is in this context, but very briefly NEB ignores the fact that there is uncertainty about  maximum likelihood estimates, especially for smaller data sets (for instance w for some codon is perhaps not exactly 3.046, but could be in a region around that value), while [https://pubmed.ncbi.nlm.nih.gov/15689528/ BEB accounts for that uncertainty].&lt;br /&gt;
: This gives you a list of which residues (if any) that were found to belong to the positively selected dN/dS-class. Also listed is the probability that the site really is in the codon class where dN/dS &amp;gt; 1, and a weighted average of the w at the site. Using only DNA sequences you have now identified likely epitopes on the gp120 protein.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Question: &#039;&#039;&#039;List all sites having more than 95% probability of belonging to the positively selected class&lt;/div&gt;</summary>
		<author><name>Gorm</name></author>
	</entry>
	<entry>
		<id>https://teaching.healthtech.dtu.dk/22115/index.php?title=Model_selection&amp;diff=262</id>
		<title>Model selection</title>
		<link rel="alternate" type="text/html" href="https://teaching.healthtech.dtu.dk/22115/index.php?title=Model_selection&amp;diff=262"/>
		<updated>2026-04-15T06:34:30Z</updated>

		<summary type="html">&lt;p&gt;Gorm: /* Selection of substitution model using jmodeltest2 */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;This exercise is part of the course  [[22115_-_Computational_Molecular_Evolution|Computational Molecular Evolution (22115)]].&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Overview ==&lt;br /&gt;
&lt;br /&gt;
: In this exercise you are going to investigate features of HIV-1 evolution. You will do this by analyzing a large set of env-genes from HIV-1, subtype B. specifically, the DNA sequences analyzed here correspond to a region surrounding the hypervariable V3 region of the gp120 protein.&lt;br /&gt;
&lt;br /&gt;
: Like other retroviruses, particles of HIV are made up of 2 copies of a single-stranded RNA genome packaged inside a protein core, or capsid. The core particle also contains viral proteins that are essential for the early steps of the virus life cycle, such as reverse transcription and integration. A lipid envelope, derived from the infected cell, surrounds the core particle. Embedded in this envelope are the surface glycoproteins of HIV: gp120 and gp41. The gp120 protein is crucial for binding of the virus particle to target cells, while gp41 is important for the subsequent fusion event. It is the specific affinity of gp120 for the CD4 protein that targets HIV to those cells of the immune system that express CD4 on their surface (e.g., T-helper lymphocytes, monocytes, and macrophages).&lt;br /&gt;
&lt;br /&gt;
: The role gp120 plays in infection and the fact that it is situated on the surface of the HIV particle, means it is an obvious target for the immune response. That means that there may be a considerable selective pressure on gp120 for creating immune-escape mutants, where amino acids in the gp120 epitopes have been substituted. In this exercise you will construct a maximum likelihood tree that we will subsequently use to investigate whether you can detect such a selective pressure on parts of gp120, again using maximum likelihood methods.&lt;br /&gt;
&lt;br /&gt;
: One major goal with the exercise is to introduce you to statistically based methods for assessing the strength of evidence for a set of alternative hypotheses about some biological system of interest. The model selection method we will use is AIC (Akaike Information Criterion), based on which you will compute model probabilities. A second goal is to make you aware that phylogenetic analysis is not only about constructing trees, but that it is also a useful framework for analyzing biological questions more generally.&lt;br /&gt;
&lt;br /&gt;
: Specifically, you will&lt;br /&gt;
&lt;br /&gt;
:# perform a multiple alignment of gp120 DNA sequences taking protein-level information into account (using revtrans).&lt;br /&gt;
:# select a suitable nucleotide substitution model (using jmodeltest2)&lt;br /&gt;
:# construct a phylogenetic tree (using PAUP).&lt;br /&gt;
:# try to detect positively selected sites in gp120 (using PAML).&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
== Recipe for computing AIC values and model probabilities ==&lt;br /&gt;
&lt;br /&gt;
: Later in today&#039;s exercise you will be asked to compute AIC values and model probabilities. Return to this section and follow the instructions when you need to do so.&lt;br /&gt;
&lt;br /&gt;
:# Fit a set of models to your data, note the maximized log likelihoods (lnL) and the number of free parameters (K) for each model in the investigated set. The models you fit should represent a plausible and comprehensive set of hypotheses about your data.&lt;br /&gt;
:# Compute AIC for each of the models: &#039;&#039;&#039;AIC = -2 x lnL + 2K&#039;&#039;&#039;.  &amp;lt;br&amp;gt;For example: a model with lnL = -2010 and K = 5 will have AIC = -2 x -2010 + 2 x 5 = 4030.&lt;br /&gt;
:# Identify the model with the smallest AIC (this is the best model in the set). We will call the AIC for this model &#039;&#039;&#039;&amp;quot;AICmin&amp;quot;&#039;&#039;&#039;.&lt;br /&gt;
:# Compute the &amp;quot;ΔAIC&amp;quot; values for each model: &#039;&#039;&#039;ΔAIC = AIC - AICmin&#039;&#039;&#039; &amp;lt;br&amp;gt;For each model subtract the minimum AIC value. The best model will have a ΔAIC of zero. The rest of the models will have positive ΔAICs.&lt;br /&gt;
:# For each model compute the following quantity: &#039;&#039;&#039;numerator = exp(-0.5 x ΔAIC)&#039;&#039;&#039; &amp;lt;br&amp;gt;For example, a model with ΔAIC=4.2 will have numerator = exp(-0.5 x 4.2) = exp(-2.1) = 0.1225. Also compute the &#039;&#039;&#039;sum&#039;&#039;&#039; of the numerator values for all models.&lt;br /&gt;
:# Finally, the model probabilities (also called model weights) for each model are found as:&amp;lt;br&amp;gt; &#039;&#039;&#039;P(model) = numerator / sum&#039;&#039;&#039; &amp;lt;br&amp;gt;For example, if sum = 3.75 and a model has numerator = 1.3, then it has P(model) = 1.3 / 3.75 = 0.35&lt;br /&gt;
&lt;br /&gt;
: You may want to keep track of the computations by constructing a table along the following lines:&lt;br /&gt;
[[File:Molevol-Downloads-aictable.png|700px]]&lt;br /&gt;
&lt;br /&gt;
: Note that model probabilities can also be computed using Bayesian methods. One advantage of Bayesian methods over AIC is that instead of relying on a point estimate, uncertainty about parameter values is accounted for by integrating over all possible values (typically using MCMC).&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
== Getting started ==&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Create working directory, copy files&#039;&#039;&#039;&lt;br /&gt;
: In the command below: Instead of /path/to/molevol enter the path to the directory where you have placed your course files (for instance cd /Users/bob/Documents/molevol, or cd /home/student/molevol).&lt;br /&gt;
 cd /path/to/molevol&lt;br /&gt;
 mkdir modelselect&lt;br /&gt;
 cd modelselect&lt;br /&gt;
 cp ../data/gp120.fasta ./gp120.fasta&lt;br /&gt;
 cp ../data/codeml.ctl ./codeml.ctl&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Have a look at the DNA data file:&#039;&#039;&#039;&lt;br /&gt;
 nedit gp120.fasta &amp;amp;&lt;br /&gt;
: The file contains several DNA sequences from HIV-1, subtype B. The sequences are approximately 500 bp long, and correspond to a region surrounding the hypervariable V3 region in the gene encoding gp120. Close the nedit window when you&#039;ve had a look.&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
== Analysis of viral data set: alignment of coding DNA ==&lt;br /&gt;
&lt;br /&gt;
: There are three reasons why it is preferable to align coding DNA at the amino acid level rather than directly as nucleotides. First, the DNA alphabet has only four letters compared to 20 amino acids, which means that even unrelated DNA sequences will share roughly 25% identity by chance alone, making it much harder to distinguish true homology from noise. Second, protein alignments benefit from empirical substitution matrices such as BLOSUM-62, which capture the fact that amino acid replacements tend to be conservative. Equivalent matrices for DNA are far less informative because nucleotide substitution patterns vary strongly between genes and organisms. Third, synonymous substitutions accumulate faster than non-synonymous ones, so DNA sequences diverge more rapidly than the proteins they encode. Taken together, these factors mean that the phylogenetic signal in a DNA alignment erodes much faster than in the corresponding protein alignment.&lt;br /&gt;
&lt;br /&gt;
: However, while protein-level alignment gives us the most reliable homology signal, it discards exactly the information we need for studying selection: the synonymous substitutions. Estimating the ratio of non-synonymous to synonymous substitution rates (dN/dS) requires a codon-level alignment where analogous codon positions are properly lined up. We would therefore like to construct a multiple alignment at the DNA level, but using information at the protein level, and the RevTrans server does exactly that.&lt;br /&gt;
&lt;br /&gt;
: &#039;&#039;&#039;RevTrans&#039;&#039;&#039; takes as input an unaligned set of DNA sequences, automatically translates them to the equivalent amino acid sequences, constructs a multiple alignment of the protein sequences, and finally uses the protein alignment as a template for constructing a multiple DNA alignment. Because each amino acid maps back to its original codon, gaps are always inserted in groups of three and codon boundaries are preserved throughout the alignment. This is essential for the dN/dS analyses we will carry out in this exercise: if codon positions are misaligned, synonymous and non-synonymous changes cannot be correctly distinguished.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Construct RevTrans alignment&#039;&#039;&#039;&lt;br /&gt;
:* Open RevTrans server page: https://services.healthtech.dtu.dk/services/RevTrans-2.0/&lt;br /&gt;
:* On the RevTrans page: Choose the file gp120.fasta as input (or copy and paste the sequence into the sequence window)&lt;br /&gt;
:* Click the &amp;quot;Submit query&amp;quot; button&lt;br /&gt;
:* When the alignment is done you may have to click link named &amp;quot;here&amp;quot; to go to results page&lt;br /&gt;
:* Download DNA alignment, by right-clicking the link for &amp;quot;Download alignment in FASTA format&amp;quot;, and choosing &amp;quot;Save link as...&amp;quot; (save file under the name gp120align.fasta and make sure to save the file in the directory modelselect).&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Convert alignment to NEXUS format&#039;&#039;&#039;&lt;br /&gt;
: Convert the fasta file to NEXUS format and save file in the modelselect directory under the name gp120.nexus&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Question 1&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
: As part of the present analysis we are going to build a phylogenetic tree based on the DNA alignment constructed above. We will construct the tree using maximum likelihood, but to do that we first have to decide which substitution model we want to use. Specifically, we are interested in selecting a model that achieves a good balance between fit to the data and number of parameters. We will investigate this issue by fitting a set of 56 different models to our data and then comparing them using AIC.&lt;br /&gt;
&lt;br /&gt;
: In the first part of the exercise, however, you will only compare three closely related models by hand. The purpose of this restricted comparison is not primarily to identify the globally best model, but to understand how AIC, ΔAIC, and Akaike weights are computed and interpreted. Because only three models are included in this manual comparison, the weights you compute will describe relative support &#039;&#039;&#039;within this restricted set only&#039;&#039;&#039;.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Start jmodeltest2&#039;&#039;&#039;&lt;br /&gt;
 jmodeltest&lt;br /&gt;
&#039;&#039;&#039;Load data set&#039;&#039;&#039;&lt;br /&gt;
:* File -&amp;gt; Load DNA alignment -&amp;gt; File Format -&amp;gt; Select &amp;quot;All files&amp;quot;&lt;br /&gt;
:* Navigate to gp120.nexus and load it&lt;br /&gt;
&#039;&#039;&#039;Fit 56 models&#039;&#039;&#039;&lt;br /&gt;
:* Analysis -&amp;gt; Compute likelihood scores&lt;br /&gt;
:* Select &amp;quot;7&amp;quot; under &amp;quot;Number of substitution schemes&amp;quot;&lt;br /&gt;
:* Select &amp;quot;Fixed BIONJ-JC&amp;quot; under &amp;quot;Base tree for likelihood calculations&amp;quot;&lt;br /&gt;
:* Click &amp;quot;Compute likelihoods&amp;quot;&lt;br /&gt;
&lt;br /&gt;
: This causes jmodeltest2 to perform the following actions: first a neighbor joining tree is constructed using the Jukes and Cantor model. Then the tree is fixed and used as the basis for fitting a set of 56 different models to the data. For each model, the estimated model parameters and the negative log-likelihood are recorded. In addition to varying sets of substitution rate parameters (JC, K2P, ...), some of these models also include extra parameters that take into account the presence of different rates between sites. This is done in two ways: (1) by fitting a gamma distribution of rates (&amp;quot;+G&amp;quot;), and (2) by allowing for a proportion of constant (&amp;quot;invariable&amp;quot;) sites (&amp;quot;+I&amp;quot;).&lt;br /&gt;
&lt;br /&gt;
: Wait until jmodeltest2 is done fitting all 56 models (this will take a little while depending on your computer).&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Inspect result&#039;&#039;&#039;&lt;br /&gt;
:* Results -&amp;gt; Show results table&lt;br /&gt;
: For each model this table lists the negative log-likelihood (&amp;quot;-lnL&amp;quot;), the number of parameters (&amp;quot;p&amp;quot; - note this includes branch lengths), and estimates of all model parameters (excluding branch lengths).&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Manually compute Akaike weights for three substitution models&#039;&#039;&#039;&lt;br /&gt;
: Use AIC-based model comparison to investigate which of the following three substitution models are best supported as descriptions of how the sequences have evolved:&lt;br /&gt;
:* Jukes and Cantor with fraction of invariant sites (JC+I)&lt;br /&gt;
:* Jukes and Cantor with gamma-distributed rates over sites (JC+G)&lt;br /&gt;
:* Jukes and Cantor with invariant sites and gamma-distributed rates (JC+I+G)&lt;br /&gt;
: Before you can do the computation you need to know the log likelihood and the number of parameters for each model. Locate these values in the table for the JC+I, JC+G, and JC+I+G models, and write them down. Close the window with the result table when you are done.&lt;br /&gt;
&lt;br /&gt;
: Make sure to get the signs right: the values reported in the table are -lnL values, so you will need to reverse the sign to get the lnL (the lnL values you write down should be negative).&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Question: &#039;&#039;&#039;Use the recipe above to compute AIC, ΔAIC, and Akaike weight for the three models. Report the results in a table similar to the one shown above, and verify that the three weights sum to 1 (up to rounding error).&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Question 2&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
: Based on the Akaike weights, which of the three models has the strongest support? Is the support strongly concentrated on one model, or is there still substantial support for one or both of the others?&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Question 3&#039;&#039;&#039;&lt;br /&gt;
&#039;&#039;&#039;Use modeltest program to select best model&#039;&#039;&#039;&lt;br /&gt;
: What you just did manually for JC+I, JC+G and JC+I+G, jmodeltest2 can do automatically for the full set of 56 fitted models. Specifically, it uses the list of negative log likelihoods and parameter counts in the table to compute AIC values and Akaike weights for the full candidate set:&lt;br /&gt;
:* Analysis -&amp;gt; Do AIC calculations -&amp;gt;&lt;br /&gt;
:* Select &amp;quot;Write PAUP* block&amp;quot;&lt;br /&gt;
:* click &amp;quot;Do AIC calculations&amp;quot;&lt;br /&gt;
:* Results -&amp;gt; Show results table&lt;br /&gt;
:* Select &amp;quot;AIC&amp;quot; tab&lt;br /&gt;
:* &#039;&#039;&#039;IMPORTANT:&#039;&#039;&#039; SHIFT+click on the header of the &amp;quot;weight&amp;quot; column. This sorts the rows according to model weight, in descending order.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Question: &#039;&#039;&#039;What model was selected by modeltest based on the AIC values? (This will be the model with the highest weight - and lowest AIC - and will be in the first row of the results table after SHIFT-clicking the &amp;quot;weight&amp;quot; header).&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Question 4&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
: In the same AIC results table, now click (not SHIFT+click) the header of the &amp;quot;-lnL&amp;quot; column. In this table, a normal click sorts in ascending order, whereas SHIFT+click sorts in descending order. The model at the top will therefore be the one with the smallest value of &amp;quot;-lnL&amp;quot; (equivalently: the highest likelihood). Compare this model to the one with the highest Akaike weight.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Question: &#039;&#039;&#039;Is the model with the highest likelihood also the model with the highest Akaike weight? If not, explain briefly why these two rankings can differ.&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
== Construction of phylogenetic tree using PAUP ==&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Question 4&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
: Close the results table. In the main window you should now scroll up to the lines giving PAUP commands that will implement the selected model. The command is enclosed between &amp;quot;BEGIN PAUP&amp;quot; and &amp;quot;END;&amp;quot; and should look something like this:&lt;br /&gt;
 Lset Base=(0.4064, [...]&lt;br /&gt;
: You will need to copy this command to a PAUP session in the next step.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Start PAUP&#039;&#039;&#039;&lt;br /&gt;
 paup&lt;br /&gt;
: Above you used jmodeltest2 to select the most suitable substitution model for the present data set. You will now use this model to construct a maximum likelihood tree. You will use PAUP for this purpose. (note: it is possible to create a maximum likelihood or a model-averaged tree directly from the jmodeltest2 program, but we will instead do it in PAUP in order to more clearly see each step that is taken).&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Load alignment:&#039;&#039;&#039;&lt;br /&gt;
 execute gp120.nexus&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Set tree-building criterion to maximum likelihood&#039;&#039;&#039;&lt;br /&gt;
 set criterion=likelihood&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Set model parameters to winning estimates&#039;&#039;&#039;&lt;br /&gt;
: Above you located a set of lines in the jmodeltest2 output giving a PAUP command that sets the model parameters to the estimates that were found using the winning model. Copy and paste this lset command (without the BEGIN and END parts) into the window where PAUP is running.&lt;br /&gt;
&lt;br /&gt;
 PASTE LSET COMMAND FROM MODELTEST RUN HERE&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Find best tree using selected model&#039;&#039;&#039;&lt;br /&gt;
: Still in the PAUP-window, enter the following command&lt;br /&gt;
 hsearch swap=tbr start=nj&lt;br /&gt;
: This command causes PAUP to perform a heuristic search for the best maximum likelihood tree. Once an initial tree has been constructed, the heuristic search proceeds by rearrangements of the &amp;quot;tree bisection and reconnection&amp;quot; type (TBR). We are using the model selected by modeltest, AND the parameter estimates found by modeltest on that model. You could also have chosen to simply estimate all the model parameters as part of this step (i.e., at the same time as finding the best tree), but fixing them improves speed tremendously. Findind the best tree should take a few minutes at most.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Save best tree to file&#039;&#039;&#039;&lt;br /&gt;
 savetrees format=newick brlens=yes file=gp120tree.phy from=1 to=1&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Quit program&#039;&#039;&#039;&lt;br /&gt;
 quit&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Have a look at the tree:&#039;&#039;&#039;&lt;br /&gt;
: You have now produced an unrooted tree of the HIV sequences and saved it in the file gp120tree.phy. Note that in this exercise we will not be interested in the tree as such - our focus is instead on finding positive selection on a subset of codon positions and the tree is just something we need in order to be able to fit the different codon models to the data. If you want to see the tree, you can do so with the following command:&lt;br /&gt;
 figtree gp120tree.phy &amp;amp;&lt;br /&gt;
: There is no meaningful root placed in this tree, so you may want to choose the unrooted view (the third icon in the Layout section of the figtree window). Close the figtree window when you have had a look&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Question: &#039;&#039;&#039;What is the negative log likelihood of the tree you just found?&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
== Detection of positively selected sites in gp120 ==&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Question 5&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
: There is much more to phylogenetic analyses than merely reconstructing trees. One interesting result of probabilistic methods, is that the parameters of a model will have their values estimated as part of the optimization procedure. This means that once such a model has been fitted to the data, it is possible to investigate these estimated parameter values to learn features about the evolutionary history of the sequences under investigation. In the present example we will focus on investigating whether we can find positively selected sites in our data set, defined as sites where the dN/dS ratio is larger than 1. We do that by using a codon substitution model where the dN/dS ratio is one of its parameters.&lt;br /&gt;
&lt;br /&gt;
: A further strength of the probabilistic approach is that you get a probabilistic measure of how well any model fits the data. This means you can use a stringent approach to determine which model fits the data best. In this framework one uses likelihoods (probabilities of data given model) to determine which model fits the data best. As you saw above, it is for instance possible to compute AIC values and weights (model probabilities) from the likelihood values of fitted models, Since each model essentially corresponds to a hypothesis about the evolutionary history of the data, we can thus use a stringent statistical approach to decide which hypothesis best describes our data.&lt;br /&gt;
&lt;br /&gt;
: In outline, you will now use the following steps to investigate whether there is any evidence for positively selected codons in your data set:&lt;br /&gt;
&lt;br /&gt;
:* Fit model M1, which assumes there are two classes of codons in the sequence: some with dN/dS &amp;lt; 1, some with dN/dS=1.&lt;br /&gt;
:* Fit model M2, which assumes 3 distinct classes of codons: two with dN/dS ratios as for M1, and one extra class with dN/dS &amp;gt; 1.&lt;br /&gt;
:* Assess the strength of evidence for the two models using AIC-based model probabilities&lt;br /&gt;
:* If M2 is better: identify the positively selected codons&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Inspect the parameter file&#039;&#039;&#039;&lt;br /&gt;
 nedit codeml.ctl &amp;amp;&lt;br /&gt;
: The file &amp;quot;codeml.ctl&amp;quot; contains several settings that are relevant for running the program &#039;&#039;&#039;codeml&#039;&#039;&#039;. Find the following lines and ensure that the file contains these values:&lt;br /&gt;
 &#039;&#039;&#039;seqfile =  gp120align.fasta&#039;&#039;&#039;:  name of alignment file&lt;br /&gt;
 &#039;&#039;&#039;treefile =  gp120tree.phy&#039;&#039;&#039;: name of tree file&lt;br /&gt;
 &#039;&#039;&#039;seqtype = 1&#039;&#039;&#039;: tells the program that our data consists of coding DNA.&lt;br /&gt;
 &#039;&#039;&#039;NSsites = 1 2&#039;&#039;&#039; : tells the program to analyze models M1 and M2.&lt;br /&gt;
 &#039;&#039;&#039;cleandata = 0&#039;&#039;&#039;: tells the program to keep positions with gaps.&lt;br /&gt;
&lt;br /&gt;
: The settings entered by us will cause codeml to analyze two hypotheses about dN/dS ratios. M1 says there are two classes of codons with different dN/dS ratios in the sequence: one class with dN/dS &amp;lt; 1 (codons under purifying or negative selection), and one class with dN/dS=1 (no selection - neutrally evolving sites). M2 says there are 3 distinct dN/dS ratios for different sites in the sequence: one class with dN/dS &amp;lt; 1, one class with dN/dS=1 (these are the same type of classes as for M1), and one class with dN/dS &amp;gt; 1 (corresponding to sites under positive selection). The value of the dN/dS ratios (for those classes that have dN/dS &amp;lt; 1 or dN/dS &amp;gt; 1), the fraction of sites belonging to each class, and the position of sites belonging to each class, are unknown at first and will be determined during the analysis.&lt;br /&gt;
&lt;br /&gt;
: (Regarding &amp;quot;cleandata&amp;quot; setting: &amp;quot;cleandata = 1&amp;quot; would cause codeml to discard any alignment columns with gaps or ambiguity symbols. For alignments with many sequences this can lead to discarding the majority of sites and I would therefore recommend not using that setting. At the same time you do want to be wary of columns where most sequences have gaps, since inference of selection for these will be very uncertain. It might be better to discard such columns from an alignment before doing the codeml analysis).&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Start the analysis&#039;&#039;&#039;&lt;br /&gt;
 codeml&lt;br /&gt;
: This will start the codeml program using the settings in the file codeml.ctl. Depending on your computer, this will take some minutes to finish. (You may be able to see how the optimization procedure results in progressively better fits: the likelihood increases, meaning that negative log-likelihood decreases, as the fit improves).&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Inspect result file:&#039;&#039;&#039;&lt;br /&gt;
: Wait for the run to finish, and then look at the result file:&lt;br /&gt;
 nedit selection.results &amp;amp;&lt;br /&gt;
: This file contains a wealth of information concerning your analysis. You may want to turn off line-wrapping to more clearly see the structure of the file (there will be some long lines that will otherwise wrap around). The top part of the file gives an overview of your sequences, codon usage and nucleotide frequencies. You can ignore this information for now, and move on to the interesting part, namely the model likelihoods and parameter values:&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Find likelihood, and number of free parameters for model M1&#039;&#039;&#039;&lt;br /&gt;
 Search ==&amp;gt; Find... ==&amp;gt; enter &amp;quot;Model 1&amp;quot; and click Find&lt;br /&gt;
: You are now in the region of the result file where the model likelihoods and parameter estimates are noted. Now, locate a line that looks a bit like the one shown below:&lt;br /&gt;
 lnL(ntime: 72  np: 74):  -4242.470345     +0.000000&lt;br /&gt;
: Identify the number of &amp;quot;free parameters&amp;quot;, K, used in model M1: This is indicated by &amp;quot;np&amp;quot;, and is 74 in the example shown above (most of these parameters are branch lengths in the tree; specifically, the number of branch length parameters is indicated by &amp;quot;ntime&amp;quot;, and is 72 in this example). Also note the log-likelihood of the fitted model. This is the number right after the parenthesis, and is -4242.470345 in the example here.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Question: &#039;&#039;&#039;What are the values of K and lnL for model M1?&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Question 6&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Find dn/dS ratios and codon class proportions for model M1:&#039;&#039;&#039;&lt;br /&gt;
: Scroll down a few lines until you get to a small table similar to this:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
      dN/dS for site classes (K=2)&lt;br /&gt;
      p:   0.75111  0.24889&lt;br /&gt;
      w:   0.06583  1.00000&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
: This gives a summary of the dN/dS ratios that were found in the data set. The line starting &amp;quot;w:&amp;quot; lists the two dN/dS ratios that were found (in this case 0.06583 and 1.00000 - the last one was pre-specified by us as part of the model and was therefore not a free parameter). In this context &amp;quot;w&amp;quot; means &amp;quot;omega&amp;quot;, which is the symbol typically used to represent the dN/dS rate ratio. The line starting p: gives the proportion of codon sites belonging to each of the dN/dS ratio classes (in the example above approximately 75% belong to the first class , while 25% of all sites belong to the class having dN/dS=1.00000).&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Question: &#039;&#039;&#039;What are the dN/dS value (w) and proportion (p) of sites for both classes. Report the following values: p(class1), w(class1), p(class2), w(class2)&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Question 7&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Find likelihood, and K for model M2&#039;&#039;&#039;&lt;br /&gt;
: Scroll past the M1 output until you get to the results for model M2.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Question: &#039;&#039;&#039;What are the values of K and lnL for model M2?&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Question 8&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Find dn/dS ratios and codon class proportions for model M2:&#039;&#039;&#039;&lt;br /&gt;
: Now, scroll down a few lines until you get to a small table similar to the one you examined for M1 before. For this model there are 3 separate classes of codons.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Question: &#039;&#039;&#039;What are the dN/dS value (w) and proportion (p) of sites for all three classes? Report these values: p(class1), w(class1), p(class2), w(class2), p(class3), w(class3)&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Question 9&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Assess strength of evidence for models M1 and M2:&#039;&#039;&#039;&lt;br /&gt;
: M2 will always have a better (higher) log-likelihood than model M1 because M2 has more free parameters, and M1 is nested within M2. You should now use the recipe given above to compute AIC values and model probabilities for M1 and M2.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Question: &#039;&#039;&#039; Report: AIC, ΔAIC, weight (model probability) for M1 and M2&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Question 10: &#039;&#039;&#039; Is M2 better than M1?&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Question 11&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Examine list of positively selected sites&#039;&#039;&#039;&lt;br /&gt;
: If your M2 is clearly better than M1 (I firmly believe it should be if you did things according to instructions...), then you have evidence for the existence of positively selected sites in the gp120 gene. Now, scroll down to the end of the result file and locate a list similar to the one below. Note: This is the &amp;quot;Bayes Empirical Bayes&amp;quot; table, not the &amp;quot;Naive Empirical Bayes&amp;quot; table. &lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Bayes Empirical Bayes (BEB) analysis&lt;br /&gt;
Positively selected sites&lt;br /&gt;
&lt;br /&gt;
         Prob(w&amp;gt;1)     mean w&lt;br /&gt;
&lt;br /&gt;
    25 A 0.959*        3.133 +- 0.769&lt;br /&gt;
    27 P 0.906         2.990 +- 0.877&lt;br /&gt;
    56 K 0.987*        3.197 +- 0.687&lt;br /&gt;
    59 V 0.915         3.032 +- 0.873&lt;br /&gt;
    78 R 0.637         2.351 +- 1.129&lt;br /&gt;
    88 K 0.573         2.148 +- 1.077&lt;br /&gt;
    95 V 0.925         3.046 +- 0.843&lt;br /&gt;
    ...&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
: It is not important what the distinction is in this context, but very briefly NEB ignores the fact that there is uncertainty about  maximum likelihood estimates, especially for smaller data sets (for instance w for some codon is perhaps not exactly 3.046, but could be in a region around that value), while [https://pubmed.ncbi.nlm.nih.gov/15689528/ BEB accounts for that uncertainty].&lt;br /&gt;
: This gives you a list of which residues (if any) that were found to belong to the positively selected dN/dS-class. Also listed is the probability that the site really is in the codon class where dN/dS &amp;gt; 1, and a weighted average of the w at the site. Using only DNA sequences you have now identified likely epitopes on the gp120 protein.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Question: &#039;&#039;&#039;List all sites having more than 95% probability of belonging to the positively selected class&lt;/div&gt;</summary>
		<author><name>Gorm</name></author>
	</entry>
	<entry>
		<id>https://teaching.healthtech.dtu.dk/22115/index.php?title=Model_selection&amp;diff=261</id>
		<title>Model selection</title>
		<link rel="alternate" type="text/html" href="https://teaching.healthtech.dtu.dk/22115/index.php?title=Model_selection&amp;diff=261"/>
		<updated>2026-04-15T05:52:04Z</updated>

		<summary type="html">&lt;p&gt;Gorm: /* Recipe for computing AIC values and model probabilities */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;This exercise is part of the course  [[22115_-_Computational_Molecular_Evolution|Computational Molecular Evolution (22115)]].&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Overview ==&lt;br /&gt;
&lt;br /&gt;
: In this exercise you are going to investigate features of HIV-1 evolution. You will do this by analyzing a large set of env-genes from HIV-1, subtype B. specifically, the DNA sequences analyzed here correspond to a region surrounding the hypervariable V3 region of the gp120 protein.&lt;br /&gt;
&lt;br /&gt;
: Like other retroviruses, particles of HIV are made up of 2 copies of a single-stranded RNA genome packaged inside a protein core, or capsid. The core particle also contains viral proteins that are essential for the early steps of the virus life cycle, such as reverse transcription and integration. A lipid envelope, derived from the infected cell, surrounds the core particle. Embedded in this envelope are the surface glycoproteins of HIV: gp120 and gp41. The gp120 protein is crucial for binding of the virus particle to target cells, while gp41 is important for the subsequent fusion event. It is the specific affinity of gp120 for the CD4 protein that targets HIV to those cells of the immune system that express CD4 on their surface (e.g., T-helper lymphocytes, monocytes, and macrophages).&lt;br /&gt;
&lt;br /&gt;
: The role gp120 plays in infection and the fact that it is situated on the surface of the HIV particle, means it is an obvious target for the immune response. That means that there may be a considerable selective pressure on gp120 for creating immune-escape mutants, where amino acids in the gp120 epitopes have been substituted. In this exercise you will construct a maximum likelihood tree that we will subsequently use to investigate whether you can detect such a selective pressure on parts of gp120, again using maximum likelihood methods.&lt;br /&gt;
&lt;br /&gt;
: One major goal with the exercise is to introduce you to statistically based methods for assessing the strength of evidence for a set of alternative hypotheses about some biological system of interest. The model selection method we will use is AIC (Akaike Information Criterion), based on which you will compute model probabilities. A second goal is to make you aware that phylogenetic analysis is not only about constructing trees, but that it is also a useful framework for analyzing biological questions more generally.&lt;br /&gt;
&lt;br /&gt;
: Specifically, you will&lt;br /&gt;
&lt;br /&gt;
:# perform a multiple alignment of gp120 DNA sequences taking protein-level information into account (using revtrans).&lt;br /&gt;
:# select a suitable nucleotide substitution model (using jmodeltest2)&lt;br /&gt;
:# construct a phylogenetic tree (using PAUP).&lt;br /&gt;
:# try to detect positively selected sites in gp120 (using PAML).&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
== Recipe for computing AIC values and model probabilities ==&lt;br /&gt;
&lt;br /&gt;
: Later in today&#039;s exercise you will be asked to compute AIC values and model probabilities. Return to this section and follow the instructions when you need to do so.&lt;br /&gt;
&lt;br /&gt;
:# Fit a set of models to your data, note the maximized log likelihoods (lnL) and the number of free parameters (K) for each model in the investigated set. The models you fit should represent a plausible and comprehensive set of hypotheses about your data.&lt;br /&gt;
:# Compute AIC for each of the models: &#039;&#039;&#039;AIC = -2 x lnL + 2K&#039;&#039;&#039;.  &amp;lt;br&amp;gt;For example: a model with lnL = -2010 and K = 5 will have AIC = -2 x -2010 + 2 x 5 = 4030.&lt;br /&gt;
:# Identify the model with the smallest AIC (this is the best model in the set). We will call the AIC for this model &#039;&#039;&#039;&amp;quot;AICmin&amp;quot;&#039;&#039;&#039;.&lt;br /&gt;
:# Compute the &amp;quot;ΔAIC&amp;quot; values for each model: &#039;&#039;&#039;ΔAIC = AIC - AICmin&#039;&#039;&#039; &amp;lt;br&amp;gt;For each model subtract the minimum AIC value. The best model will have a ΔAIC of zero. The rest of the models will have positive ΔAICs.&lt;br /&gt;
:# For each model compute the following quantity: &#039;&#039;&#039;numerator = exp(-0.5 x ΔAIC)&#039;&#039;&#039; &amp;lt;br&amp;gt;For example, a model with ΔAIC=4.2 will have numerator = exp(-0.5 x 4.2) = exp(-2.1) = 0.1225. Also compute the &#039;&#039;&#039;sum&#039;&#039;&#039; of the numerator values for all models.&lt;br /&gt;
:# Finally, the model probabilities (also called model weights) for each model are found as:&amp;lt;br&amp;gt; &#039;&#039;&#039;P(model) = numerator / sum&#039;&#039;&#039; &amp;lt;br&amp;gt;For example, if sum = 3.75 and a model has numerator = 1.3, then it has P(model) = 1.3 / 3.75 = 0.35&lt;br /&gt;
&lt;br /&gt;
: You may want to keep track of the computations by constructing a table along the following lines:&lt;br /&gt;
[[File:Molevol-Downloads-aictable.png|700px]]&lt;br /&gt;
&lt;br /&gt;
: Note that model probabilities can also be computed using Bayesian methods. One advantage of Bayesian methods over AIC is that instead of relying on a point estimate, uncertainty about parameter values is accounted for by integrating over all possible values (typically using MCMC).&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
== Getting started ==&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Create working directory, copy files&#039;&#039;&#039;&lt;br /&gt;
: In the command below: Instead of /path/to/molevol enter the path to the directory where you have placed your course files (for instance cd /Users/bob/Documents/molevol, or cd /home/student/molevol).&lt;br /&gt;
 cd /path/to/molevol&lt;br /&gt;
 mkdir modelselect&lt;br /&gt;
 cd modelselect&lt;br /&gt;
 cp ../data/gp120.fasta ./gp120.fasta&lt;br /&gt;
 cp ../data/codeml.ctl ./codeml.ctl&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Have a look at the DNA data file:&#039;&#039;&#039;&lt;br /&gt;
 nedit gp120.fasta &amp;amp;&lt;br /&gt;
: The file contains several DNA sequences from HIV-1, subtype B. The sequences are approximately 500 bp long, and correspond to a region surrounding the hypervariable V3 region in the gene encoding gp120. Close the nedit window when you&#039;ve had a look.&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
== Analysis of viral data set: alignment of coding DNA ==&lt;br /&gt;
&lt;br /&gt;
: There are three reasons why it is preferable to align coding DNA at the amino acid level rather than directly as nucleotides. First, the DNA alphabet has only four letters compared to 20 amino acids, which means that even unrelated DNA sequences will share roughly 25% identity by chance alone, making it much harder to distinguish true homology from noise. Second, protein alignments benefit from empirical substitution matrices such as BLOSUM-62, which capture the fact that amino acid replacements tend to be conservative. Equivalent matrices for DNA are far less informative because nucleotide substitution patterns vary strongly between genes and organisms. Third, synonymous substitutions accumulate faster than non-synonymous ones, so DNA sequences diverge more rapidly than the proteins they encode. Taken together, these factors mean that the phylogenetic signal in a DNA alignment erodes much faster than in the corresponding protein alignment.&lt;br /&gt;
&lt;br /&gt;
: However, while protein-level alignment gives us the most reliable homology signal, it discards exactly the information we need for studying selection: the synonymous substitutions. Estimating the ratio of non-synonymous to synonymous substitution rates (dN/dS) requires a codon-level alignment where analogous codon positions are properly lined up. We would therefore like to construct a multiple alignment at the DNA level, but using information at the protein level, and the RevTrans server does exactly that.&lt;br /&gt;
&lt;br /&gt;
: &#039;&#039;&#039;RevTrans&#039;&#039;&#039; takes as input an unaligned set of DNA sequences, automatically translates them to the equivalent amino acid sequences, constructs a multiple alignment of the protein sequences, and finally uses the protein alignment as a template for constructing a multiple DNA alignment. Because each amino acid maps back to its original codon, gaps are always inserted in groups of three and codon boundaries are preserved throughout the alignment. This is essential for the dN/dS analyses we will carry out in this exercise: if codon positions are misaligned, synonymous and non-synonymous changes cannot be correctly distinguished.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Construct RevTrans alignment&#039;&#039;&#039;&lt;br /&gt;
:* Open RevTrans server page: https://services.healthtech.dtu.dk/services/RevTrans-2.0/&lt;br /&gt;
:* On the RevTrans page: Choose the file gp120.fasta as input (or copy and paste the sequence into the sequence window)&lt;br /&gt;
:* Click the &amp;quot;Submit query&amp;quot; button&lt;br /&gt;
:* When the alignment is done you may have to click link named &amp;quot;here&amp;quot; to go to results page&lt;br /&gt;
:* Download DNA alignment, by right-clicking the link for &amp;quot;Download alignment in FASTA format&amp;quot;, and choosing &amp;quot;Save link as...&amp;quot; (save file under the name gp120align.fasta and make sure to save the file in the directory modelselect).&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Convert alignment to NEXUS format&#039;&#039;&#039;&lt;br /&gt;
: Convert the fasta file to NEXUS format and save file in the modelselect directory under the name gp120.nexus&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
== Selection of substitution model using jmodeltest2 ==&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Question 1&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
: As part of the present analysis we are going to build a phylogenetic tree based on the DNA alignment constructed above. We will construct the tree using maximum likelihood, but to do that we first have to decide which substitution model we want to use. Specifically, we are interested in using the model that best describes our data without having more parameters than strictly necessary (thus avoiding overfitting). We will investigate this issue by fitting a set of 56 different models to our data and then selecting one with a reasonable balance between model complexity and data fit.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Start jmodeltest2&#039;&#039;&#039;&lt;br /&gt;
 jmodeltest&lt;br /&gt;
&#039;&#039;&#039;Load data set&#039;&#039;&#039;&lt;br /&gt;
:* File -&amp;gt; Load DNA alignment -&amp;gt; File Format -&amp;gt; Select &amp;quot;All files&amp;quot;&lt;br /&gt;
:* Navigate to gp120.nexus and load it&lt;br /&gt;
&#039;&#039;&#039;Fit 56 models&#039;&#039;&#039;&lt;br /&gt;
:* Analysis -&amp;gt; Compute likelihood scores&lt;br /&gt;
:* Select &amp;quot;7&amp;quot; under &amp;quot;Number of substitution schemes&amp;quot;&lt;br /&gt;
:* Select &amp;quot;Fixed BIONJ-JC&amp;quot; under &amp;quot;Base tree for likelihood calculations&amp;quot;&lt;br /&gt;
:* Click &amp;quot;Compute likelihoods&amp;quot;&lt;br /&gt;
&lt;br /&gt;
: This causes jmodeltest2 to perform the following actions: first a neighbor joining tree is constructed using the Jukes and Cantor model. Then the tree is fixed and used as the basis for fitting a set of 56 different models to the data. For each model, the estimated model parameters and the negative log-likelihood are recorded. In addition to varying sets of substitution rate parameters (JC, K2P, ...), some of these models also include extra parameters that take into account the presence of different rates between sites. This is done in two ways: (1) by fitting a gamma distribution of rates (&amp;quot;+G&amp;quot;), and (2) by allowing for a proportion of constant (&amp;quot;invariable&amp;quot;) sites (&amp;quot;+I&amp;quot;).&lt;br /&gt;
&lt;br /&gt;
: Wait until jmodeltest2 is done fitting all 56 models (this will take a little while depending on your computer).&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Inspect result&#039;&#039;&#039;&lt;br /&gt;
:* Results -&amp;gt; Show results table&lt;br /&gt;
: For each model this table lists the negative log-likelihood (&amp;quot;-lnL&amp;quot;), the number of parameters (&amp;quot;p&amp;quot; - note this includes branch lengths), and estimates of all model parameters (excluding branch lengths).&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Manually compute model probabilities for three substitution models&#039;&#039;&#039;&lt;br /&gt;
: Use AIC-based model probabilities to investigate which of the following three substitution models are best at describing how the sequences have evolved:&lt;br /&gt;
:* Jukes and Cantor with fraction of invariant sites (JC+I)&lt;br /&gt;
:* Jukes and Cantor with gamma-distributed rates over sites (JC+G)&lt;br /&gt;
:* Jukes and Cantor with invariant sites and gamma-distributed rates (JC+I+G)&lt;br /&gt;
: Before you can do the computation you need to know the log likelihood and the number of parameters for each model. Locate these values in the table for the JC+I, JC+G, and JC+I+G models, and write them down. Close the window with the result table when you are done.&lt;br /&gt;
&lt;br /&gt;
:Make sure to get the signs right: the values reported in the table are -lnL values, so you will need to reverse the sign to get the lnL (the lnL values you write down should be negative). &lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Question: &#039;&#039;&#039; Use the recipe above to compute AIC values and model probabilities. Report the results in a table similar to the one shown above&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Question 2&#039;&#039;&#039; Based on the model probabilities: wich model has more support?&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Question 3&#039;&#039;&#039;&lt;br /&gt;
&#039;&#039;&#039;Use modeltest program to select best model&#039;&#039;&#039;&lt;br /&gt;
: What you just did manually for JC+I, JC+G and JC+I+G, jmodeltest2 can do automatically for the full set of 56 fitted models. Specifically, it uses the list of negative log likelihoods and parameter counts in the table to compute AIC and model probabilities, and uses this to select the model that best fits the sequence data:&lt;br /&gt;
:* Analysis -&amp;gt; Do AIC calculations -&amp;gt; &lt;br /&gt;
:* Select &amp;quot;Write PAUP* block&amp;quot;&lt;br /&gt;
:* click &amp;quot;Do AIC calculations&amp;quot;&lt;br /&gt;
:* Results -&amp;gt; Show results table&lt;br /&gt;
:* Select &amp;quot;AIC&amp;quot; tab&lt;br /&gt;
:* &#039;&#039;&#039;IMPORTANT:&#039;&#039;&#039; SHIFT+click on the header of the &amp;quot;weight&amp;quot; column. This sorts the rows according to model weight, in descending order.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Question: &#039;&#039;&#039;What model was selected by modeltest based on the AIC values? (This will be the model with the highest weight - and lowest AIC - and will be in the first row of the results table after SHIFT-clicking the &amp;quot;weight&amp;quot; header).&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
== Construction of phylogenetic tree using PAUP ==&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Question 4&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
: Close the results table. In the main window you should now scroll up to the lines giving PAUP commands that will implement the selected model. The command is enclosed between &amp;quot;BEGIN PAUP&amp;quot; and &amp;quot;END;&amp;quot; and should look something like this:&lt;br /&gt;
 Lset Base=(0.4064, [...]&lt;br /&gt;
: You will need to copy this command to a PAUP session in the next step.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Start PAUP&#039;&#039;&#039;&lt;br /&gt;
 paup&lt;br /&gt;
: Above you used jmodeltest2 to select the most suitable substitution model for the present data set. You will now use this model to construct a maximum likelihood tree. You will use PAUP for this purpose. (note: it is possible to create a maximum likelihood or a model-averaged tree directly from the jmodeltest2 program, but we will instead do it in PAUP in order to more clearly see each step that is taken).&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Load alignment:&#039;&#039;&#039;&lt;br /&gt;
 execute gp120.nexus&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Set tree-building criterion to maximum likelihood&#039;&#039;&#039;&lt;br /&gt;
 set criterion=likelihood&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Set model parameters to winning estimates&#039;&#039;&#039;&lt;br /&gt;
: Above you located a set of lines in the jmodeltest2 output giving a PAUP command that sets the model parameters to the estimates that were found using the winning model. Copy and paste this lset command (without the BEGIN and END parts) into the window where PAUP is running.&lt;br /&gt;
&lt;br /&gt;
 PASTE LSET COMMAND FROM MODELTEST RUN HERE&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Find best tree using selected model&#039;&#039;&#039;&lt;br /&gt;
: Still in the PAUP-window, enter the following command&lt;br /&gt;
 hsearch swap=tbr start=nj&lt;br /&gt;
: This command causes PAUP to perform a heuristic search for the best maximum likelihood tree. Once an initial tree has been constructed, the heuristic search proceeds by rearrangements of the &amp;quot;tree bisection and reconnection&amp;quot; type (TBR). We are using the model selected by modeltest, AND the parameter estimates found by modeltest on that model. You could also have chosen to simply estimate all the model parameters as part of this step (i.e., at the same time as finding the best tree), but fixing them improves speed tremendously. Findind the best tree should take a few minutes at most.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Save best tree to file&#039;&#039;&#039;&lt;br /&gt;
 savetrees format=newick brlens=yes file=gp120tree.phy from=1 to=1&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Quit program&#039;&#039;&#039;&lt;br /&gt;
 quit&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Have a look at the tree:&#039;&#039;&#039;&lt;br /&gt;
: You have now produced an unrooted tree of the HIV sequences and saved it in the file gp120tree.phy. Note that in this exercise we will not be interested in the tree as such - our focus is instead on finding positive selection on a subset of codon positions and the tree is just something we need in order to be able to fit the different codon models to the data. If you want to see the tree, you can do so with the following command:&lt;br /&gt;
 figtree gp120tree.phy &amp;amp;&lt;br /&gt;
: There is no meaningful root placed in this tree, so you may want to choose the unrooted view (the third icon in the Layout section of the figtree window). Close the figtree window when you have had a look&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Question: &#039;&#039;&#039;What is the negative log likelihood of the tree you just found?&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
== Detection of positively selected sites in gp120 ==&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Question 5&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
: There is much more to phylogenetic analyses than merely reconstructing trees. One interesting result of probabilistic methods, is that the parameters of a model will have their values estimated as part of the optimization procedure. This means that once such a model has been fitted to the data, it is possible to investigate these estimated parameter values to learn features about the evolutionary history of the sequences under investigation. In the present example we will focus on investigating whether we can find positively selected sites in our data set, defined as sites where the dN/dS ratio is larger than 1. We do that by using a codon substitution model where the dN/dS ratio is one of its parameters.&lt;br /&gt;
&lt;br /&gt;
: A further strength of the probabilistic approach is that you get a probabilistic measure of how well any model fits the data. This means you can use a stringent approach to determine which model fits the data best. In this framework one uses likelihoods (probabilities of data given model) to determine which model fits the data best. As you saw above, it is for instance possible to compute AIC values and weights (model probabilities) from the likelihood values of fitted models, Since each model essentially corresponds to a hypothesis about the evolutionary history of the data, we can thus use a stringent statistical approach to decide which hypothesis best describes our data.&lt;br /&gt;
&lt;br /&gt;
: In outline, you will now use the following steps to investigate whether there is any evidence for positively selected codons in your data set:&lt;br /&gt;
&lt;br /&gt;
:* Fit model M1, which assumes there are two classes of codons in the sequence: some with dN/dS &amp;lt; 1, some with dN/dS=1.&lt;br /&gt;
:* Fit model M2, which assumes 3 distinct classes of codons: two with dN/dS ratios as for M1, and one extra class with dN/dS &amp;gt; 1.&lt;br /&gt;
:* Assess the strength of evidence for the two models using AIC-based model probabilities&lt;br /&gt;
:* If M2 is better: identify the positively selected codons&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Inspect the parameter file&#039;&#039;&#039;&lt;br /&gt;
 nedit codeml.ctl &amp;amp;&lt;br /&gt;
: The file &amp;quot;codeml.ctl&amp;quot; contains several settings that are relevant for running the program &#039;&#039;&#039;codeml&#039;&#039;&#039;. Find the following lines and ensure that the file contains these values:&lt;br /&gt;
 &#039;&#039;&#039;seqfile =  gp120align.fasta&#039;&#039;&#039;:  name of alignment file&lt;br /&gt;
 &#039;&#039;&#039;treefile =  gp120tree.phy&#039;&#039;&#039;: name of tree file&lt;br /&gt;
 &#039;&#039;&#039;seqtype = 1&#039;&#039;&#039;: tells the program that our data consists of coding DNA.&lt;br /&gt;
 &#039;&#039;&#039;NSsites = 1 2&#039;&#039;&#039; : tells the program to analyze models M1 and M2.&lt;br /&gt;
 &#039;&#039;&#039;cleandata = 0&#039;&#039;&#039;: tells the program to keep positions with gaps.&lt;br /&gt;
&lt;br /&gt;
: The settings entered by us will cause codeml to analyze two hypotheses about dN/dS ratios. M1 says there are two classes of codons with different dN/dS ratios in the sequence: one class with dN/dS &amp;lt; 1 (codons under purifying or negative selection), and one class with dN/dS=1 (no selection - neutrally evolving sites). M2 says there are 3 distinct dN/dS ratios for different sites in the sequence: one class with dN/dS &amp;lt; 1, one class with dN/dS=1 (these are the same type of classes as for M1), and one class with dN/dS &amp;gt; 1 (corresponding to sites under positive selection). The value of the dN/dS ratios (for those classes that have dN/dS &amp;lt; 1 or dN/dS &amp;gt; 1), the fraction of sites belonging to each class, and the position of sites belonging to each class, are unknown at first and will be determined during the analysis.&lt;br /&gt;
&lt;br /&gt;
: (Regarding &amp;quot;cleandata&amp;quot; setting: &amp;quot;cleandata = 1&amp;quot; would cause codeml to discard any alignment columns with gaps or ambiguity symbols. For alignments with many sequences this can lead to discarding the majority of sites and I would therefore recommend not using that setting. At the same time you do want to be wary of columns where most sequences have gaps, since inference of selection for these will be very uncertain. It might be better to discard such columns from an alignment before doing the codeml analysis).&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Start the analysis&#039;&#039;&#039;&lt;br /&gt;
 codeml&lt;br /&gt;
: This will start the codeml program using the settings in the file codeml.ctl. Depending on your computer, this will take some minutes to finish. (You may be able to see how the optimization procedure results in progressively better fits: the likelihood increases, meaning that negative log-likelihood decreases, as the fit improves).&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Inspect result file:&#039;&#039;&#039;&lt;br /&gt;
: Wait for the run to finish, and then look at the result file:&lt;br /&gt;
 nedit selection.results &amp;amp;&lt;br /&gt;
: This file contains a wealth of information concerning your analysis. You may want to turn off line-wrapping to more clearly see the structure of the file (there will be some long lines that will otherwise wrap around). The top part of the file gives an overview of your sequences, codon usage and nucleotide frequencies. You can ignore this information for now, and move on to the interesting part, namely the model likelihoods and parameter values:&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Find likelihood, and number of free parameters for model M1&#039;&#039;&#039;&lt;br /&gt;
 Search ==&amp;gt; Find... ==&amp;gt; enter &amp;quot;Model 1&amp;quot; and click Find&lt;br /&gt;
: You are now in the region of the result file where the model likelihoods and parameter estimates are noted. Now, locate a line that looks a bit like the one shown below:&lt;br /&gt;
 lnL(ntime: 72  np: 74):  -4242.470345     +0.000000&lt;br /&gt;
: Identify the number of &amp;quot;free parameters&amp;quot;, K, used in model M1: This is indicated by &amp;quot;np&amp;quot;, and is 74 in the example shown above (most of these parameters are branch lengths in the tree; specifically, the number of branch length parameters is indicated by &amp;quot;ntime&amp;quot;, and is 72 in this example). Also note the log-likelihood of the fitted model. This is the number right after the parenthesis, and is -4242.470345 in the example here.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Question: &#039;&#039;&#039;What are the values of K and lnL for model M1?&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Question 6&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Find dn/dS ratios and codon class proportions for model M1:&#039;&#039;&#039;&lt;br /&gt;
: Scroll down a few lines until you get to a small table similar to this:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
      dN/dS for site classes (K=2)&lt;br /&gt;
      p:   0.75111  0.24889&lt;br /&gt;
      w:   0.06583  1.00000&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
: This gives a summary of the dN/dS ratios that were found in the data set. The line starting &amp;quot;w:&amp;quot; lists the two dN/dS ratios that were found (in this case 0.06583 and 1.00000 - the last one was pre-specified by us as part of the model and was therefore not a free parameter). In this context &amp;quot;w&amp;quot; means &amp;quot;omega&amp;quot;, which is the symbol typically used to represent the dN/dS rate ratio. The line starting p: gives the proportion of codon sites belonging to each of the dN/dS ratio classes (in the example above approximately 75% belong to the first class , while 25% of all sites belong to the class having dN/dS=1.00000).&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Question: &#039;&#039;&#039;What are the dN/dS value (w) and proportion (p) of sites for both classes. Report the following values: p(class1), w(class1), p(class2), w(class2)&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Question 7&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Find likelihood, and K for model M2&#039;&#039;&#039;&lt;br /&gt;
: Scroll past the M1 output until you get to the results for model M2.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Question: &#039;&#039;&#039;What are the values of K and lnL for model M2?&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Question 8&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Find dn/dS ratios and codon class proportions for model M2:&#039;&#039;&#039;&lt;br /&gt;
: Now, scroll down a few lines until you get to a small table similar to the one you examined for M1 before. For this model there are 3 separate classes of codons.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Question: &#039;&#039;&#039;What are the dN/dS value (w) and proportion (p) of sites for all three classes? Report these values: p(class1), w(class1), p(class2), w(class2), p(class3), w(class3)&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Question 9&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Assess strength of evidence for models M1 and M2:&#039;&#039;&#039;&lt;br /&gt;
: M2 will always have a better (higher) log-likelihood than model M1 because M2 has more free parameters, and M1 is nested within M2. You should now use the recipe given above to compute AIC values and model probabilities for M1 and M2.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Question: &#039;&#039;&#039; Report: AIC, ΔAIC, weight (model probability) for M1 and M2&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Question 10: &#039;&#039;&#039; Is M2 better than M1?&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Question 11&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Examine list of positively selected sites&#039;&#039;&#039;&lt;br /&gt;
: If your M2 is clearly better than M1 (I firmly believe it should be if you did things according to instructions...), then you have evidence for the existence of positively selected sites in the gp120 gene. Now, scroll down to the end of the result file and locate a list similar to the one below. Note: This is the &amp;quot;Bayes Empirical Bayes&amp;quot; table, not the &amp;quot;Naive Empirical Bayes&amp;quot; table. &lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Bayes Empirical Bayes (BEB) analysis&lt;br /&gt;
Positively selected sites&lt;br /&gt;
&lt;br /&gt;
         Prob(w&amp;gt;1)     mean w&lt;br /&gt;
&lt;br /&gt;
    25 A 0.959*        3.133 +- 0.769&lt;br /&gt;
    27 P 0.906         2.990 +- 0.877&lt;br /&gt;
    56 K 0.987*        3.197 +- 0.687&lt;br /&gt;
    59 V 0.915         3.032 +- 0.873&lt;br /&gt;
    78 R 0.637         2.351 +- 1.129&lt;br /&gt;
    88 K 0.573         2.148 +- 1.077&lt;br /&gt;
    95 V 0.925         3.046 +- 0.843&lt;br /&gt;
    ...&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
: It is not important what the distinction is in this context, but very briefly NEB ignores the fact that there is uncertainty about  maximum likelihood estimates, especially for smaller data sets (for instance w for some codon is perhaps not exactly 3.046, but could be in a region around that value), while [https://pubmed.ncbi.nlm.nih.gov/15689528/ BEB accounts for that uncertainty].&lt;br /&gt;
: This gives you a list of which residues (if any) that were found to belong to the positively selected dN/dS-class. Also listed is the probability that the site really is in the codon class where dN/dS &amp;gt; 1, and a weighted average of the w at the site. Using only DNA sequences you have now identified likely epitopes on the gp120 protein.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Question: &#039;&#039;&#039;List all sites having more than 95% probability of belonging to the positively selected class&lt;/div&gt;</summary>
		<author><name>Gorm</name></author>
	</entry>
	<entry>
		<id>https://teaching.healthtech.dtu.dk/22115/index.php?title=Model_selection&amp;diff=260</id>
		<title>Model selection</title>
		<link rel="alternate" type="text/html" href="https://teaching.healthtech.dtu.dk/22115/index.php?title=Model_selection&amp;diff=260"/>
		<updated>2026-04-15T05:51:20Z</updated>

		<summary type="html">&lt;p&gt;Gorm: /* Recipe for computing AIC values and model probabilities */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;This exercise is part of the course  [[22115_-_Computational_Molecular_Evolution|Computational Molecular Evolution (22115)]].&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Overview ==&lt;br /&gt;
&lt;br /&gt;
: In this exercise you are going to investigate features of HIV-1 evolution. You will do this by analyzing a large set of env-genes from HIV-1, subtype B. specifically, the DNA sequences analyzed here correspond to a region surrounding the hypervariable V3 region of the gp120 protein.&lt;br /&gt;
&lt;br /&gt;
: Like other retroviruses, particles of HIV are made up of 2 copies of a single-stranded RNA genome packaged inside a protein core, or capsid. The core particle also contains viral proteins that are essential for the early steps of the virus life cycle, such as reverse transcription and integration. A lipid envelope, derived from the infected cell, surrounds the core particle. Embedded in this envelope are the surface glycoproteins of HIV: gp120 and gp41. The gp120 protein is crucial for binding of the virus particle to target cells, while gp41 is important for the subsequent fusion event. It is the specific affinity of gp120 for the CD4 protein that targets HIV to those cells of the immune system that express CD4 on their surface (e.g., T-helper lymphocytes, monocytes, and macrophages).&lt;br /&gt;
&lt;br /&gt;
: The role gp120 plays in infection and the fact that it is situated on the surface of the HIV particle, means it is an obvious target for the immune response. That means that there may be a considerable selective pressure on gp120 for creating immune-escape mutants, where amino acids in the gp120 epitopes have been substituted. In this exercise you will construct a maximum likelihood tree that we will subsequently use to investigate whether you can detect such a selective pressure on parts of gp120, again using maximum likelihood methods.&lt;br /&gt;
&lt;br /&gt;
: One major goal with the exercise is to introduce you to statistically based methods for assessing the strength of evidence for a set of alternative hypotheses about some biological system of interest. The model selection method we will use is AIC (Akaike Information Criterion), based on which you will compute model probabilities. A second goal is to make you aware that phylogenetic analysis is not only about constructing trees, but that it is also a useful framework for analyzing biological questions more generally.&lt;br /&gt;
&lt;br /&gt;
: Specifically, you will&lt;br /&gt;
&lt;br /&gt;
:# perform a multiple alignment of gp120 DNA sequences taking protein-level information into account (using revtrans).&lt;br /&gt;
:# select a suitable nucleotide substitution model (using jmodeltest2)&lt;br /&gt;
:# construct a phylogenetic tree (using PAUP).&lt;br /&gt;
:# try to detect positively selected sites in gp120 (using PAML).&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
== Recipe for computing AIC values and model probabilities ==&lt;br /&gt;
&lt;br /&gt;
: Later in today&#039;s exercise you will be asked to compute AIC values and model probabilities. Return to this section and follow the instructions when you need to do so.&lt;br /&gt;
&lt;br /&gt;
:# Fit a set of models to your data, note the maximized log likelihoods (lnL) and the number of free parameters (K) for each model in the investigated set. The models you fit should represent a plausible and comprehensive set of hypotheses about your data.&lt;br /&gt;
:# Compute AIC for each of the models: &#039;&#039;&#039;AIC = -2 x lnL + 2K&#039;&#039;&#039;.  &amp;lt;br&amp;gt;For example: a model with lnL = -2010 and K = 5 will have AIC = -2 x -2010 + 2 x 5 = 4030.&lt;br /&gt;
:# Identify the model with the smallest AIC (this is the best model in the set). We will call the AIC for this model &#039;&#039;&#039;&amp;quot;AICmin&amp;quot;&#039;&#039;&#039;.&lt;br /&gt;
:# Compute the &amp;quot;ΔAIC&amp;quot; values for each model: &#039;&#039;&#039;ΔAIC = AIC - AICmin&#039;&#039;&#039; &amp;lt;br&amp;gt;For each model subtract the minimum AIC value. The best model will have a ΔAIC of zero. The rest of the models will have positive ΔAICs.&lt;br /&gt;
:# For each model compute the following quantity: &#039;&#039;&#039;numerator = exp(-0.5 x ΔAIC)&#039;&#039;&#039; &amp;lt;br&amp;gt;For example, a model with ΔAIC=4.2 will have numerator = exp(-0.5 x 4.2) = exp(-2.1) = 0.1225. Also compute the &#039;&#039;&#039;sum&#039;&#039;&#039; of the numerator values for all models.&lt;br /&gt;
:# Finally, the model probabilities (also called model weights) for each model are found as: &#039;&#039;&#039;P(model) = numerator / sum&#039;&#039;&#039; &amp;lt;br&amp;gt;For example, if sum = 3.75 and a model has numerator = 1.3, then it has P(model) = 1.3 / 3.75 = 0.35&lt;br /&gt;
&lt;br /&gt;
: You may want to keep track of the computations by constructing a table along the following lines:&lt;br /&gt;
[[File:Molevol-Downloads-aictable.png|700px]]&lt;br /&gt;
&lt;br /&gt;
: Note that model probabilities can also be computed using Bayesian methods. One advantage of Bayesian methods over AIC is that instead of relying on a point estimate, uncertainty about parameter values is accounted for by integrating over all possible values (typically using MCMC).&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
== Getting started ==&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Create working directory, copy files&#039;&#039;&#039;&lt;br /&gt;
: In the command below: Instead of /path/to/molevol enter the path to the directory where you have placed your course files (for instance cd /Users/bob/Documents/molevol, or cd /home/student/molevol).&lt;br /&gt;
 cd /path/to/molevol&lt;br /&gt;
 mkdir modelselect&lt;br /&gt;
 cd modelselect&lt;br /&gt;
 cp ../data/gp120.fasta ./gp120.fasta&lt;br /&gt;
 cp ../data/codeml.ctl ./codeml.ctl&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Have a look at the DNA data file:&#039;&#039;&#039;&lt;br /&gt;
 nedit gp120.fasta &amp;amp;&lt;br /&gt;
: The file contains several DNA sequences from HIV-1, subtype B. The sequences are approximately 500 bp long, and correspond to a region surrounding the hypervariable V3 region in the gene encoding gp120. Close the nedit window when you&#039;ve had a look.&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
== Analysis of viral data set: alignment of coding DNA ==&lt;br /&gt;
&lt;br /&gt;
: There are three reasons why it is preferable to align coding DNA at the amino acid level rather than directly as nucleotides. First, the DNA alphabet has only four letters compared to 20 amino acids, which means that even unrelated DNA sequences will share roughly 25% identity by chance alone, making it much harder to distinguish true homology from noise. Second, protein alignments benefit from empirical substitution matrices such as BLOSUM-62, which capture the fact that amino acid replacements tend to be conservative. Equivalent matrices for DNA are far less informative because nucleotide substitution patterns vary strongly between genes and organisms. Third, synonymous substitutions accumulate faster than non-synonymous ones, so DNA sequences diverge more rapidly than the proteins they encode. Taken together, these factors mean that the phylogenetic signal in a DNA alignment erodes much faster than in the corresponding protein alignment.&lt;br /&gt;
&lt;br /&gt;
: However, while protein-level alignment gives us the most reliable homology signal, it discards exactly the information we need for studying selection: the synonymous substitutions. Estimating the ratio of non-synonymous to synonymous substitution rates (dN/dS) requires a codon-level alignment where analogous codon positions are properly lined up. We would therefore like to construct a multiple alignment at the DNA level, but using information at the protein level, and the RevTrans server does exactly that.&lt;br /&gt;
&lt;br /&gt;
: &#039;&#039;&#039;RevTrans&#039;&#039;&#039; takes as input an unaligned set of DNA sequences, automatically translates them to the equivalent amino acid sequences, constructs a multiple alignment of the protein sequences, and finally uses the protein alignment as a template for constructing a multiple DNA alignment. Because each amino acid maps back to its original codon, gaps are always inserted in groups of three and codon boundaries are preserved throughout the alignment. This is essential for the dN/dS analyses we will carry out in this exercise: if codon positions are misaligned, synonymous and non-synonymous changes cannot be correctly distinguished.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Construct RevTrans alignment&#039;&#039;&#039;&lt;br /&gt;
:* Open RevTrans server page: https://services.healthtech.dtu.dk/services/RevTrans-2.0/&lt;br /&gt;
:* On the RevTrans page: Choose the file gp120.fasta as input (or copy and paste the sequence into the sequence window)&lt;br /&gt;
:* Click the &amp;quot;Submit query&amp;quot; button&lt;br /&gt;
:* When the alignment is done you may have to click link named &amp;quot;here&amp;quot; to go to results page&lt;br /&gt;
:* Download DNA alignment, by right-clicking the link for &amp;quot;Download alignment in FASTA format&amp;quot;, and choosing &amp;quot;Save link as...&amp;quot; (save file under the name gp120align.fasta and make sure to save the file in the directory modelselect).&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Convert alignment to NEXUS format&#039;&#039;&#039;&lt;br /&gt;
: Convert the fasta file to NEXUS format and save file in the modelselect directory under the name gp120.nexus&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
== Selection of substitution model using jmodeltest2 ==&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Question 1&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
: As part of the present analysis we are going to build a phylogenetic tree based on the DNA alignment constructed above. We will construct the tree using maximum likelihood, but to do that we first have to decide which substitution model we want to use. Specifically, we are interested in using the model that best describes our data without having more parameters than strictly necessary (thus avoiding overfitting). We will investigate this issue by fitting a set of 56 different models to our data and then selecting one with a reasonable balance between model complexity and data fit.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Start jmodeltest2&#039;&#039;&#039;&lt;br /&gt;
 jmodeltest&lt;br /&gt;
&#039;&#039;&#039;Load data set&#039;&#039;&#039;&lt;br /&gt;
:* File -&amp;gt; Load DNA alignment -&amp;gt; File Format -&amp;gt; Select &amp;quot;All files&amp;quot;&lt;br /&gt;
:* Navigate to gp120.nexus and load it&lt;br /&gt;
&#039;&#039;&#039;Fit 56 models&#039;&#039;&#039;&lt;br /&gt;
:* Analysis -&amp;gt; Compute likelihood scores&lt;br /&gt;
:* Select &amp;quot;7&amp;quot; under &amp;quot;Number of substitution schemes&amp;quot;&lt;br /&gt;
:* Select &amp;quot;Fixed BIONJ-JC&amp;quot; under &amp;quot;Base tree for likelihood calculations&amp;quot;&lt;br /&gt;
:* Click &amp;quot;Compute likelihoods&amp;quot;&lt;br /&gt;
&lt;br /&gt;
: This causes jmodeltest2 to perform the following actions: first a neighbor joining tree is constructed using the Jukes and Cantor model. Then the tree is fixed and used as the basis for fitting a set of 56 different models to the data. For each model, the estimated model parameters and the negative log-likelihood are recorded. In addition to varying sets of substitution rate parameters (JC, K2P, ...), some of these models also include extra parameters that take into account the presence of different rates between sites. This is done in two ways: (1) by fitting a gamma distribution of rates (&amp;quot;+G&amp;quot;), and (2) by allowing for a proportion of constant (&amp;quot;invariable&amp;quot;) sites (&amp;quot;+I&amp;quot;).&lt;br /&gt;
&lt;br /&gt;
: Wait until jmodeltest2 is done fitting all 56 models (this will take a little while depending on your computer).&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Inspect result&#039;&#039;&#039;&lt;br /&gt;
:* Results -&amp;gt; Show results table&lt;br /&gt;
: For each model this table lists the negative log-likelihood (&amp;quot;-lnL&amp;quot;), the number of parameters (&amp;quot;p&amp;quot; - note this includes branch lengths), and estimates of all model parameters (excluding branch lengths).&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Manually compute model probabilities for three substitution models&#039;&#039;&#039;&lt;br /&gt;
: Use AIC-based model probabilities to investigate which of the following three substitution models are best at describing how the sequences have evolved:&lt;br /&gt;
:* Jukes and Cantor with fraction of invariant sites (JC+I)&lt;br /&gt;
:* Jukes and Cantor with gamma-distributed rates over sites (JC+G)&lt;br /&gt;
:* Jukes and Cantor with invariant sites and gamma-distributed rates (JC+I+G)&lt;br /&gt;
: Before you can do the computation you need to know the log likelihood and the number of parameters for each model. Locate these values in the table for the JC+I, JC+G, and JC+I+G models, and write them down. Close the window with the result table when you are done.&lt;br /&gt;
&lt;br /&gt;
:Make sure to get the signs right: the values reported in the table are -lnL values, so you will need to reverse the sign to get the lnL (the lnL values you write down should be negative). &lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Question: &#039;&#039;&#039; Use the recipe above to compute AIC values and model probabilities. Report the results in a table similar to the one shown above&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Question 2&#039;&#039;&#039; Based on the model probabilities: wich model has more support?&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Question 3&#039;&#039;&#039;&lt;br /&gt;
&#039;&#039;&#039;Use modeltest program to select best model&#039;&#039;&#039;&lt;br /&gt;
: What you just did manually for JC+I, JC+G and JC+I+G, jmodeltest2 can do automatically for the full set of 56 fitted models. Specifically, it uses the list of negative log likelihoods and parameter counts in the table to compute AIC and model probabilities, and uses this to select the model that best fits the sequence data:&lt;br /&gt;
:* Analysis -&amp;gt; Do AIC calculations -&amp;gt; &lt;br /&gt;
:* Select &amp;quot;Write PAUP* block&amp;quot;&lt;br /&gt;
:* click &amp;quot;Do AIC calculations&amp;quot;&lt;br /&gt;
:* Results -&amp;gt; Show results table&lt;br /&gt;
:* Select &amp;quot;AIC&amp;quot; tab&lt;br /&gt;
:* &#039;&#039;&#039;IMPORTANT:&#039;&#039;&#039; SHIFT+click on the header of the &amp;quot;weight&amp;quot; column. This sorts the rows according to model weight, in descending order.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Question: &#039;&#039;&#039;What model was selected by modeltest based on the AIC values? (This will be the model with the highest weight - and lowest AIC - and will be in the first row of the results table after SHIFT-clicking the &amp;quot;weight&amp;quot; header).&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
== Construction of phylogenetic tree using PAUP ==&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Question 4&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
: Close the results table. In the main window you should now scroll up to the lines giving PAUP commands that will implement the selected model. The command is enclosed between &amp;quot;BEGIN PAUP&amp;quot; and &amp;quot;END;&amp;quot; and should look something like this:&lt;br /&gt;
 Lset Base=(0.4064, [...]&lt;br /&gt;
: You will need to copy this command to a PAUP session in the next step.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Start PAUP&#039;&#039;&#039;&lt;br /&gt;
 paup&lt;br /&gt;
: Above you used jmodeltest2 to select the most suitable substitution model for the present data set. You will now use this model to construct a maximum likelihood tree. You will use PAUP for this purpose. (note: it is possible to create a maximum likelihood or a model-averaged tree directly from the jmodeltest2 program, but we will instead do it in PAUP in order to more clearly see each step that is taken).&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Load alignment:&#039;&#039;&#039;&lt;br /&gt;
 execute gp120.nexus&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Set tree-building criterion to maximum likelihood&#039;&#039;&#039;&lt;br /&gt;
 set criterion=likelihood&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Set model parameters to winning estimates&#039;&#039;&#039;&lt;br /&gt;
: Above you located a set of lines in the jmodeltest2 output giving a PAUP command that sets the model parameters to the estimates that were found using the winning model. Copy and paste this lset command (without the BEGIN and END parts) into the window where PAUP is running.&lt;br /&gt;
&lt;br /&gt;
 PASTE LSET COMMAND FROM MODELTEST RUN HERE&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Find best tree using selected model&#039;&#039;&#039;&lt;br /&gt;
: Still in the PAUP-window, enter the following command&lt;br /&gt;
 hsearch swap=tbr start=nj&lt;br /&gt;
: This command causes PAUP to perform a heuristic search for the best maximum likelihood tree. Once an initial tree has been constructed, the heuristic search proceeds by rearrangements of the &amp;quot;tree bisection and reconnection&amp;quot; type (TBR). We are using the model selected by modeltest, AND the parameter estimates found by modeltest on that model. You could also have chosen to simply estimate all the model parameters as part of this step (i.e., at the same time as finding the best tree), but fixing them improves speed tremendously. Findind the best tree should take a few minutes at most.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Save best tree to file&#039;&#039;&#039;&lt;br /&gt;
 savetrees format=newick brlens=yes file=gp120tree.phy from=1 to=1&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Quit program&#039;&#039;&#039;&lt;br /&gt;
 quit&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Have a look at the tree:&#039;&#039;&#039;&lt;br /&gt;
: You have now produced an unrooted tree of the HIV sequences and saved it in the file gp120tree.phy. Note that in this exercise we will not be interested in the tree as such - our focus is instead on finding positive selection on a subset of codon positions and the tree is just something we need in order to be able to fit the different codon models to the data. If you want to see the tree, you can do so with the following command:&lt;br /&gt;
 figtree gp120tree.phy &amp;amp;&lt;br /&gt;
: There is no meaningful root placed in this tree, so you may want to choose the unrooted view (the third icon in the Layout section of the figtree window). Close the figtree window when you have had a look&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Question: &#039;&#039;&#039;What is the negative log likelihood of the tree you just found?&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
== Detection of positively selected sites in gp120 ==&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Question 5&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
: There is much more to phylogenetic analyses than merely reconstructing trees. One interesting result of probabilistic methods, is that the parameters of a model will have their values estimated as part of the optimization procedure. This means that once such a model has been fitted to the data, it is possible to investigate these estimated parameter values to learn features about the evolutionary history of the sequences under investigation. In the present example we will focus on investigating whether we can find positively selected sites in our data set, defined as sites where the dN/dS ratio is larger than 1. We do that by using a codon substitution model where the dN/dS ratio is one of its parameters.&lt;br /&gt;
&lt;br /&gt;
: A further strength of the probabilistic approach is that you get a probabilistic measure of how well any model fits the data. This means you can use a stringent approach to determine which model fits the data best. In this framework one uses likelihoods (probabilities of data given model) to determine which model fits the data best. As you saw above, it is for instance possible to compute AIC values and weights (model probabilities) from the likelihood values of fitted models, Since each model essentially corresponds to a hypothesis about the evolutionary history of the data, we can thus use a stringent statistical approach to decide which hypothesis best describes our data.&lt;br /&gt;
&lt;br /&gt;
: In outline, you will now use the following steps to investigate whether there is any evidence for positively selected codons in your data set:&lt;br /&gt;
&lt;br /&gt;
:* Fit model M1, which assumes there are two classes of codons in the sequence: some with dN/dS &amp;lt; 1, some with dN/dS=1.&lt;br /&gt;
:* Fit model M2, which assumes 3 distinct classes of codons: two with dN/dS ratios as for M1, and one extra class with dN/dS &amp;gt; 1.&lt;br /&gt;
:* Assess the strength of evidence for the two models using AIC-based model probabilities&lt;br /&gt;
:* If M2 is better: identify the positively selected codons&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Inspect the parameter file&#039;&#039;&#039;&lt;br /&gt;
 nedit codeml.ctl &amp;amp;&lt;br /&gt;
: The file &amp;quot;codeml.ctl&amp;quot; contains several settings that are relevant for running the program &#039;&#039;&#039;codeml&#039;&#039;&#039;. Find the following lines and ensure that the file contains these values:&lt;br /&gt;
 &#039;&#039;&#039;seqfile =  gp120align.fasta&#039;&#039;&#039;:  name of alignment file&lt;br /&gt;
 &#039;&#039;&#039;treefile =  gp120tree.phy&#039;&#039;&#039;: name of tree file&lt;br /&gt;
 &#039;&#039;&#039;seqtype = 1&#039;&#039;&#039;: tells the program that our data consists of coding DNA.&lt;br /&gt;
 &#039;&#039;&#039;NSsites = 1 2&#039;&#039;&#039; : tells the program to analyze models M1 and M2.&lt;br /&gt;
 &#039;&#039;&#039;cleandata = 0&#039;&#039;&#039;: tells the program to keep positions with gaps.&lt;br /&gt;
&lt;br /&gt;
: The settings entered by us will cause codeml to analyze two hypotheses about dN/dS ratios. M1 says there are two classes of codons with different dN/dS ratios in the sequence: one class with dN/dS &amp;lt; 1 (codons under purifying or negative selection), and one class with dN/dS=1 (no selection - neutrally evolving sites). M2 says there are 3 distinct dN/dS ratios for different sites in the sequence: one class with dN/dS &amp;lt; 1, one class with dN/dS=1 (these are the same type of classes as for M1), and one class with dN/dS &amp;gt; 1 (corresponding to sites under positive selection). The value of the dN/dS ratios (for those classes that have dN/dS &amp;lt; 1 or dN/dS &amp;gt; 1), the fraction of sites belonging to each class, and the position of sites belonging to each class, are unknown at first and will be determined during the analysis.&lt;br /&gt;
&lt;br /&gt;
: (Regarding &amp;quot;cleandata&amp;quot; setting: &amp;quot;cleandata = 1&amp;quot; would cause codeml to discard any alignment columns with gaps or ambiguity symbols. For alignments with many sequences this can lead to discarding the majority of sites and I would therefore recommend not using that setting. At the same time you do want to be wary of columns where most sequences have gaps, since inference of selection for these will be very uncertain. It might be better to discard such columns from an alignment before doing the codeml analysis).&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Start the analysis&#039;&#039;&#039;&lt;br /&gt;
 codeml&lt;br /&gt;
: This will start the codeml program using the settings in the file codeml.ctl. Depending on your computer, this will take some minutes to finish. (You may be able to see how the optimization procedure results in progressively better fits: the likelihood increases, meaning that negative log-likelihood decreases, as the fit improves).&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Inspect result file:&#039;&#039;&#039;&lt;br /&gt;
: Wait for the run to finish, and then look at the result file:&lt;br /&gt;
 nedit selection.results &amp;amp;&lt;br /&gt;
: This file contains a wealth of information concerning your analysis. You may want to turn off line-wrapping to more clearly see the structure of the file (there will be some long lines that will otherwise wrap around). The top part of the file gives an overview of your sequences, codon usage and nucleotide frequencies. You can ignore this information for now, and move on to the interesting part, namely the model likelihoods and parameter values:&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Find likelihood, and number of free parameters for model M1&#039;&#039;&#039;&lt;br /&gt;
 Search ==&amp;gt; Find... ==&amp;gt; enter &amp;quot;Model 1&amp;quot; and click Find&lt;br /&gt;
: You are now in the region of the result file where the model likelihoods and parameter estimates are noted. Now, locate a line that looks a bit like the one shown below:&lt;br /&gt;
 lnL(ntime: 72  np: 74):  -4242.470345     +0.000000&lt;br /&gt;
: Identify the number of &amp;quot;free parameters&amp;quot;, K, used in model M1: This is indicated by &amp;quot;np&amp;quot;, and is 74 in the example shown above (most of these parameters are branch lengths in the tree; specifically, the number of branch length parameters is indicated by &amp;quot;ntime&amp;quot;, and is 72 in this example). Also note the log-likelihood of the fitted model. This is the number right after the parenthesis, and is -4242.470345 in the example here.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Question: &#039;&#039;&#039;What are the values of K and lnL for model M1?&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Question 6&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Find dn/dS ratios and codon class proportions for model M1:&#039;&#039;&#039;&lt;br /&gt;
: Scroll down a few lines until you get to a small table similar to this:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
      dN/dS for site classes (K=2)&lt;br /&gt;
      p:   0.75111  0.24889&lt;br /&gt;
      w:   0.06583  1.00000&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
: This gives a summary of the dN/dS ratios that were found in the data set. The line starting &amp;quot;w:&amp;quot; lists the two dN/dS ratios that were found (in this case 0.06583 and 1.00000 - the last one was pre-specified by us as part of the model and was therefore not a free parameter). In this context &amp;quot;w&amp;quot; means &amp;quot;omega&amp;quot;, which is the symbol typically used to represent the dN/dS rate ratio. The line starting p: gives the proportion of codon sites belonging to each of the dN/dS ratio classes (in the example above approximately 75% belong to the first class , while 25% of all sites belong to the class having dN/dS=1.00000).&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Question: &#039;&#039;&#039;What are the dN/dS value (w) and proportion (p) of sites for both classes. Report the following values: p(class1), w(class1), p(class2), w(class2)&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Question 7&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Find likelihood, and K for model M2&#039;&#039;&#039;&lt;br /&gt;
: Scroll past the M1 output until you get to the results for model M2.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Question: &#039;&#039;&#039;What are the values of K and lnL for model M2?&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Question 8&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Find dn/dS ratios and codon class proportions for model M2:&#039;&#039;&#039;&lt;br /&gt;
: Now, scroll down a few lines until you get to a small table similar to the one you examined for M1 before. For this model there are 3 separate classes of codons.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Question: &#039;&#039;&#039;What are the dN/dS value (w) and proportion (p) of sites for all three classes? Report these values: p(class1), w(class1), p(class2), w(class2), p(class3), w(class3)&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Question 9&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Assess strength of evidence for models M1 and M2:&#039;&#039;&#039;&lt;br /&gt;
: M2 will always have a better (higher) log-likelihood than model M1 because M2 has more free parameters, and M1 is nested within M2. You should now use the recipe given above to compute AIC values and model probabilities for M1 and M2.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Question: &#039;&#039;&#039; Report: AIC, ΔAIC, weight (model probability) for M1 and M2&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Question 10: &#039;&#039;&#039; Is M2 better than M1?&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Question 11&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Examine list of positively selected sites&#039;&#039;&#039;&lt;br /&gt;
: If your M2 is clearly better than M1 (I firmly believe it should be if you did things according to instructions...), then you have evidence for the existence of positively selected sites in the gp120 gene. Now, scroll down to the end of the result file and locate a list similar to the one below. Note: This is the &amp;quot;Bayes Empirical Bayes&amp;quot; table, not the &amp;quot;Naive Empirical Bayes&amp;quot; table. &lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Bayes Empirical Bayes (BEB) analysis&lt;br /&gt;
Positively selected sites&lt;br /&gt;
&lt;br /&gt;
         Prob(w&amp;gt;1)     mean w&lt;br /&gt;
&lt;br /&gt;
    25 A 0.959*        3.133 +- 0.769&lt;br /&gt;
    27 P 0.906         2.990 +- 0.877&lt;br /&gt;
    56 K 0.987*        3.197 +- 0.687&lt;br /&gt;
    59 V 0.915         3.032 +- 0.873&lt;br /&gt;
    78 R 0.637         2.351 +- 1.129&lt;br /&gt;
    88 K 0.573         2.148 +- 1.077&lt;br /&gt;
    95 V 0.925         3.046 +- 0.843&lt;br /&gt;
    ...&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
: It is not important what the distinction is in this context, but very briefly NEB ignores the fact that there is uncertainty about  maximum likelihood estimates, especially for smaller data sets (for instance w for some codon is perhaps not exactly 3.046, but could be in a region around that value), while [https://pubmed.ncbi.nlm.nih.gov/15689528/ BEB accounts for that uncertainty].&lt;br /&gt;
: This gives you a list of which residues (if any) that were found to belong to the positively selected dN/dS-class. Also listed is the probability that the site really is in the codon class where dN/dS &amp;gt; 1, and a weighted average of the w at the site. Using only DNA sequences you have now identified likely epitopes on the gp120 protein.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Question: &#039;&#039;&#039;List all sites having more than 95% probability of belonging to the positively selected class&lt;/div&gt;</summary>
		<author><name>Gorm</name></author>
	</entry>
	<entry>
		<id>https://teaching.healthtech.dtu.dk/22115/index.php?title=Model_selection&amp;diff=259</id>
		<title>Model selection</title>
		<link rel="alternate" type="text/html" href="https://teaching.healthtech.dtu.dk/22115/index.php?title=Model_selection&amp;diff=259"/>
		<updated>2026-04-15T05:49:12Z</updated>

		<summary type="html">&lt;p&gt;Gorm: /* Selection of substitution model using jmodeltest2 */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;This exercise is part of the course  [[22115_-_Computational_Molecular_Evolution|Computational Molecular Evolution (22115)]].&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Overview ==&lt;br /&gt;
&lt;br /&gt;
: In this exercise you are going to investigate features of HIV-1 evolution. You will do this by analyzing a large set of env-genes from HIV-1, subtype B. specifically, the DNA sequences analyzed here correspond to a region surrounding the hypervariable V3 region of the gp120 protein.&lt;br /&gt;
&lt;br /&gt;
: Like other retroviruses, particles of HIV are made up of 2 copies of a single-stranded RNA genome packaged inside a protein core, or capsid. The core particle also contains viral proteins that are essential for the early steps of the virus life cycle, such as reverse transcription and integration. A lipid envelope, derived from the infected cell, surrounds the core particle. Embedded in this envelope are the surface glycoproteins of HIV: gp120 and gp41. The gp120 protein is crucial for binding of the virus particle to target cells, while gp41 is important for the subsequent fusion event. It is the specific affinity of gp120 for the CD4 protein that targets HIV to those cells of the immune system that express CD4 on their surface (e.g., T-helper lymphocytes, monocytes, and macrophages).&lt;br /&gt;
&lt;br /&gt;
: The role gp120 plays in infection and the fact that it is situated on the surface of the HIV particle, means it is an obvious target for the immune response. That means that there may be a considerable selective pressure on gp120 for creating immune-escape mutants, where amino acids in the gp120 epitopes have been substituted. In this exercise you will construct a maximum likelihood tree that we will subsequently use to investigate whether you can detect such a selective pressure on parts of gp120, again using maximum likelihood methods.&lt;br /&gt;
&lt;br /&gt;
: One major goal with the exercise is to introduce you to statistically based methods for assessing the strength of evidence for a set of alternative hypotheses about some biological system of interest. The model selection method we will use is AIC (Akaike Information Criterion), based on which you will compute model probabilities. A second goal is to make you aware that phylogenetic analysis is not only about constructing trees, but that it is also a useful framework for analyzing biological questions more generally.&lt;br /&gt;
&lt;br /&gt;
: Specifically, you will&lt;br /&gt;
&lt;br /&gt;
:# perform a multiple alignment of gp120 DNA sequences taking protein-level information into account (using revtrans).&lt;br /&gt;
:# select a suitable nucleotide substitution model (using jmodeltest2)&lt;br /&gt;
:# construct a phylogenetic tree (using PAUP).&lt;br /&gt;
:# try to detect positively selected sites in gp120 (using PAML).&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
== Recipe for computing AIC values and model probabilities ==&lt;br /&gt;
&lt;br /&gt;
: Later in today&#039;s exercise you will be asked to compute AIC values and model probabilities. Return to this section and follow the instructions when you need to do so.&lt;br /&gt;
&lt;br /&gt;
:# Fit a set of models to your data, note the maximized log likelihoods (lnL) and the number of free parameters (K) for each model in the investigated set. The models you fit should represent a plausible and comprehensive set of hypotheses about your data.&lt;br /&gt;
:# Compute AIC for each of the models: &#039;&#039;&#039;AIC = -2 x lnL + 2K&#039;&#039;&#039;.  &amp;lt;br&amp;gt;For example: a model with lnL = -2010 and K = 5 will have AIC = -2 x -2010 + 2 x 5 = 4030.&lt;br /&gt;
:# Identify the model with the smallest AIC (this is the best model in the set). We will call the AIC for this model &#039;&#039;&#039;&amp;quot;AICmin&amp;quot;&#039;&#039;&#039;.&lt;br /&gt;
:# Compute the &amp;quot;ΔAIC&amp;quot; values for each model: &#039;&#039;&#039;ΔAIC = AIC - AICmin&#039;&#039;&#039; &amp;lt;br&amp;gt;For each model subtract the minimum AIC value. The best model will have a ΔAIC of zero. The rest of the models will have positive ΔAICs.&lt;br /&gt;
:# For each model compute the following quantity: &#039;&#039;&#039;numerator = exp(-0.5 x ΔAIC)&#039;&#039;&#039; &amp;lt;br&amp;gt;For example, a model with ΔAIC=4.2 will have numerator = exp(-0.5 x 4.2) = exp(-2.1) = 0.1225. Also compute the &#039;&#039;&#039;sum&#039;&#039;&#039; of the numerator values for all models.&lt;br /&gt;
:# Finally, the model probabilities for each model are found as: &#039;&#039;&#039;P(model) = numerator / sum&#039;&#039;&#039; &amp;lt;br&amp;gt;For example, if sum = 3.75 and a model has numerator = 1.3, then it has P(model) = 1.3 / 3.75 = 0.35&lt;br /&gt;
&lt;br /&gt;
: You may want to keep track of the computations by constructing a table along the following lines:&lt;br /&gt;
[[File:Molevol-Downloads-aictable.png|700px]]&lt;br /&gt;
&lt;br /&gt;
: Note that model probabilities can also be computed using Bayesian methods. One advantage of Bayesian methods over AIC is that instead of relying on a point estimate, uncertainty about parameter values is accounted for by integrating over all possible values (typically using MCMC).&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
== Getting started ==&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Create working directory, copy files&#039;&#039;&#039;&lt;br /&gt;
: In the command below: Instead of /path/to/molevol enter the path to the directory where you have placed your course files (for instance cd /Users/bob/Documents/molevol, or cd /home/student/molevol).&lt;br /&gt;
 cd /path/to/molevol&lt;br /&gt;
 mkdir modelselect&lt;br /&gt;
 cd modelselect&lt;br /&gt;
 cp ../data/gp120.fasta ./gp120.fasta&lt;br /&gt;
 cp ../data/codeml.ctl ./codeml.ctl&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Have a look at the DNA data file:&#039;&#039;&#039;&lt;br /&gt;
 nedit gp120.fasta &amp;amp;&lt;br /&gt;
: The file contains several DNA sequences from HIV-1, subtype B. The sequences are approximately 500 bp long, and correspond to a region surrounding the hypervariable V3 region in the gene encoding gp120. Close the nedit window when you&#039;ve had a look.&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
== Analysis of viral data set: alignment of coding DNA ==&lt;br /&gt;
&lt;br /&gt;
: There are three reasons why it is preferable to align coding DNA at the amino acid level rather than directly as nucleotides. First, the DNA alphabet has only four letters compared to 20 amino acids, which means that even unrelated DNA sequences will share roughly 25% identity by chance alone, making it much harder to distinguish true homology from noise. Second, protein alignments benefit from empirical substitution matrices such as BLOSUM-62, which capture the fact that amino acid replacements tend to be conservative. Equivalent matrices for DNA are far less informative because nucleotide substitution patterns vary strongly between genes and organisms. Third, synonymous substitutions accumulate faster than non-synonymous ones, so DNA sequences diverge more rapidly than the proteins they encode. Taken together, these factors mean that the phylogenetic signal in a DNA alignment erodes much faster than in the corresponding protein alignment.&lt;br /&gt;
&lt;br /&gt;
: However, while protein-level alignment gives us the most reliable homology signal, it discards exactly the information we need for studying selection: the synonymous substitutions. Estimating the ratio of non-synonymous to synonymous substitution rates (dN/dS) requires a codon-level alignment where analogous codon positions are properly lined up. We would therefore like to construct a multiple alignment at the DNA level, but using information at the protein level, and the RevTrans server does exactly that.&lt;br /&gt;
&lt;br /&gt;
: &#039;&#039;&#039;RevTrans&#039;&#039;&#039; takes as input an unaligned set of DNA sequences, automatically translates them to the equivalent amino acid sequences, constructs a multiple alignment of the protein sequences, and finally uses the protein alignment as a template for constructing a multiple DNA alignment. Because each amino acid maps back to its original codon, gaps are always inserted in groups of three and codon boundaries are preserved throughout the alignment. This is essential for the dN/dS analyses we will carry out in this exercise: if codon positions are misaligned, synonymous and non-synonymous changes cannot be correctly distinguished.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Construct RevTrans alignment&#039;&#039;&#039;&lt;br /&gt;
:* Open RevTrans server page: https://services.healthtech.dtu.dk/services/RevTrans-2.0/&lt;br /&gt;
:* On the RevTrans page: Choose the file gp120.fasta as input (or copy and paste the sequence into the sequence window)&lt;br /&gt;
:* Click the &amp;quot;Submit query&amp;quot; button&lt;br /&gt;
:* When the alignment is done you may have to click link named &amp;quot;here&amp;quot; to go to results page&lt;br /&gt;
:* Download DNA alignment, by right-clicking the link for &amp;quot;Download alignment in FASTA format&amp;quot;, and choosing &amp;quot;Save link as...&amp;quot; (save file under the name gp120align.fasta and make sure to save the file in the directory modelselect).&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Convert alignment to NEXUS format&#039;&#039;&#039;&lt;br /&gt;
: Convert the fasta file to NEXUS format and save file in the modelselect directory under the name gp120.nexus&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
== Selection of substitution model using jmodeltest2 ==&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Question 1&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
: As part of the present analysis we are going to build a phylogenetic tree based on the DNA alignment constructed above. We will construct the tree using maximum likelihood, but to do that we first have to decide which substitution model we want to use. Specifically, we are interested in using the model that best describes our data without having more parameters than strictly necessary (thus avoiding overfitting). We will investigate this issue by fitting a set of 56 different models to our data and then selecting one with a reasonable balance between model complexity and data fit.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Start jmodeltest2&#039;&#039;&#039;&lt;br /&gt;
 jmodeltest&lt;br /&gt;
&#039;&#039;&#039;Load data set&#039;&#039;&#039;&lt;br /&gt;
:* File -&amp;gt; Load DNA alignment -&amp;gt; File Format -&amp;gt; Select &amp;quot;All files&amp;quot;&lt;br /&gt;
:* Navigate to gp120.nexus and load it&lt;br /&gt;
&#039;&#039;&#039;Fit 56 models&#039;&#039;&#039;&lt;br /&gt;
:* Analysis -&amp;gt; Compute likelihood scores&lt;br /&gt;
:* Select &amp;quot;7&amp;quot; under &amp;quot;Number of substitution schemes&amp;quot;&lt;br /&gt;
:* Select &amp;quot;Fixed BIONJ-JC&amp;quot; under &amp;quot;Base tree for likelihood calculations&amp;quot;&lt;br /&gt;
:* Click &amp;quot;Compute likelihoods&amp;quot;&lt;br /&gt;
&lt;br /&gt;
: This causes jmodeltest2 to perform the following actions: first a neighbor joining tree is constructed using the Jukes and Cantor model. Then the tree is fixed and used as the basis for fitting a set of 56 different models to the data. For each model, the estimated model parameters and the negative log-likelihood are recorded. In addition to varying sets of substitution rate parameters (JC, K2P, ...), some of these models also include extra parameters that take into account the presence of different rates between sites. This is done in two ways: (1) by fitting a gamma distribution of rates (&amp;quot;+G&amp;quot;), and (2) by allowing for a proportion of constant (&amp;quot;invariable&amp;quot;) sites (&amp;quot;+I&amp;quot;).&lt;br /&gt;
&lt;br /&gt;
: Wait until jmodeltest2 is done fitting all 56 models (this will take a little while depending on your computer).&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Inspect result&#039;&#039;&#039;&lt;br /&gt;
:* Results -&amp;gt; Show results table&lt;br /&gt;
: For each model this table lists the negative log-likelihood (&amp;quot;-lnL&amp;quot;), the number of parameters (&amp;quot;p&amp;quot; - note this includes branch lengths), and estimates of all model parameters (excluding branch lengths).&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Manually compute model probabilities for three substitution models&#039;&#039;&#039;&lt;br /&gt;
: Use AIC-based model probabilities to investigate which of the following three substitution models are best at describing how the sequences have evolved:&lt;br /&gt;
:* Jukes and Cantor with fraction of invariant sites (JC+I)&lt;br /&gt;
:* Jukes and Cantor with gamma-distributed rates over sites (JC+G)&lt;br /&gt;
:* Jukes and Cantor with invariant sites and gamma-distributed rates (JC+I+G)&lt;br /&gt;
: Before you can do the computation you need to know the log likelihood and the number of parameters for each model. Locate these values in the table for the JC+I, JC+G, and JC+I+G models, and write them down. Close the window with the result table when you are done.&lt;br /&gt;
&lt;br /&gt;
:Make sure to get the signs right: the values reported in the table are -lnL values, so you will need to reverse the sign to get the lnL (the lnL values you write down should be negative). &lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Question: &#039;&#039;&#039; Use the recipe above to compute AIC values and model probabilities. Report the results in a table similar to the one shown above&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Question 2&#039;&#039;&#039; Based on the model probabilities: wich model has more support?&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Question 3&#039;&#039;&#039;&lt;br /&gt;
&#039;&#039;&#039;Use modeltest program to select best model&#039;&#039;&#039;&lt;br /&gt;
: What you just did manually for JC+I, JC+G and JC+I+G, jmodeltest2 can do automatically for the full set of 56 fitted models. Specifically, it uses the list of negative log likelihoods and parameter counts in the table to compute AIC and model probabilities, and uses this to select the model that best fits the sequence data:&lt;br /&gt;
:* Analysis -&amp;gt; Do AIC calculations -&amp;gt; &lt;br /&gt;
:* Select &amp;quot;Write PAUP* block&amp;quot;&lt;br /&gt;
:* click &amp;quot;Do AIC calculations&amp;quot;&lt;br /&gt;
:* Results -&amp;gt; Show results table&lt;br /&gt;
:* Select &amp;quot;AIC&amp;quot; tab&lt;br /&gt;
:* &#039;&#039;&#039;IMPORTANT:&#039;&#039;&#039; SHIFT+click on the header of the &amp;quot;weight&amp;quot; column. This sorts the rows according to model weight, in descending order.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Question: &#039;&#039;&#039;What model was selected by modeltest based on the AIC values? (This will be the model with the highest weight - and lowest AIC - and will be in the first row of the results table after SHIFT-clicking the &amp;quot;weight&amp;quot; header).&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
== Construction of phylogenetic tree using PAUP ==&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Question 4&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
: Close the results table. In the main window you should now scroll up to the lines giving PAUP commands that will implement the selected model. The command is enclosed between &amp;quot;BEGIN PAUP&amp;quot; and &amp;quot;END;&amp;quot; and should look something like this:&lt;br /&gt;
 Lset Base=(0.4064, [...]&lt;br /&gt;
: You will need to copy this command to a PAUP session in the next step.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Start PAUP&#039;&#039;&#039;&lt;br /&gt;
 paup&lt;br /&gt;
: Above you used jmodeltest2 to select the most suitable substitution model for the present data set. You will now use this model to construct a maximum likelihood tree. You will use PAUP for this purpose. (note: it is possible to create a maximum likelihood or a model-averaged tree directly from the jmodeltest2 program, but we will instead do it in PAUP in order to more clearly see each step that is taken).&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Load alignment:&#039;&#039;&#039;&lt;br /&gt;
 execute gp120.nexus&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Set tree-building criterion to maximum likelihood&#039;&#039;&#039;&lt;br /&gt;
 set criterion=likelihood&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Set model parameters to winning estimates&#039;&#039;&#039;&lt;br /&gt;
: Above you located a set of lines in the jmodeltest2 output giving a PAUP command that sets the model parameters to the estimates that were found using the winning model. Copy and paste this lset command (without the BEGIN and END parts) into the window where PAUP is running.&lt;br /&gt;
&lt;br /&gt;
 PASTE LSET COMMAND FROM MODELTEST RUN HERE&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Find best tree using selected model&#039;&#039;&#039;&lt;br /&gt;
: Still in the PAUP-window, enter the following command&lt;br /&gt;
 hsearch swap=tbr start=nj&lt;br /&gt;
: This command causes PAUP to perform a heuristic search for the best maximum likelihood tree. Once an initial tree has been constructed, the heuristic search proceeds by rearrangements of the &amp;quot;tree bisection and reconnection&amp;quot; type (TBR). We are using the model selected by modeltest, AND the parameter estimates found by modeltest on that model. You could also have chosen to simply estimate all the model parameters as part of this step (i.e., at the same time as finding the best tree), but fixing them improves speed tremendously. Findind the best tree should take a few minutes at most.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Save best tree to file&#039;&#039;&#039;&lt;br /&gt;
 savetrees format=newick brlens=yes file=gp120tree.phy from=1 to=1&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Quit program&#039;&#039;&#039;&lt;br /&gt;
 quit&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Have a look at the tree:&#039;&#039;&#039;&lt;br /&gt;
: You have now produced an unrooted tree of the HIV sequences and saved it in the file gp120tree.phy. Note that in this exercise we will not be interested in the tree as such - our focus is instead on finding positive selection on a subset of codon positions and the tree is just something we need in order to be able to fit the different codon models to the data. If you want to see the tree, you can do so with the following command:&lt;br /&gt;
 figtree gp120tree.phy &amp;amp;&lt;br /&gt;
: There is no meaningful root placed in this tree, so you may want to choose the unrooted view (the third icon in the Layout section of the figtree window). Close the figtree window when you have had a look&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Question: &#039;&#039;&#039;What is the negative log likelihood of the tree you just found?&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
== Detection of positively selected sites in gp120 ==&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Question 5&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
: There is much more to phylogenetic analyses than merely reconstructing trees. One interesting result of probabilistic methods, is that the parameters of a model will have their values estimated as part of the optimization procedure. This means that once such a model has been fitted to the data, it is possible to investigate these estimated parameter values to learn features about the evolutionary history of the sequences under investigation. In the present example we will focus on investigating whether we can find positively selected sites in our data set, defined as sites where the dN/dS ratio is larger than 1. We do that by using a codon substitution model where the dN/dS ratio is one of its parameters.&lt;br /&gt;
&lt;br /&gt;
: A further strength of the probabilistic approach is that you get a probabilistic measure of how well any model fits the data. This means you can use a stringent approach to determine which model fits the data best. In this framework one uses likelihoods (probabilities of data given model) to determine which model fits the data best. As you saw above, it is for instance possible to compute AIC values and weights (model probabilities) from the likelihood values of fitted models, Since each model essentially corresponds to a hypothesis about the evolutionary history of the data, we can thus use a stringent statistical approach to decide which hypothesis best describes our data.&lt;br /&gt;
&lt;br /&gt;
: In outline, you will now use the following steps to investigate whether there is any evidence for positively selected codons in your data set:&lt;br /&gt;
&lt;br /&gt;
:* Fit model M1, which assumes there are two classes of codons in the sequence: some with dN/dS &amp;lt; 1, some with dN/dS=1.&lt;br /&gt;
:* Fit model M2, which assumes 3 distinct classes of codons: two with dN/dS ratios as for M1, and one extra class with dN/dS &amp;gt; 1.&lt;br /&gt;
:* Assess the strength of evidence for the two models using AIC-based model probabilities&lt;br /&gt;
:* If M2 is better: identify the positively selected codons&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Inspect the parameter file&#039;&#039;&#039;&lt;br /&gt;
 nedit codeml.ctl &amp;amp;&lt;br /&gt;
: The file &amp;quot;codeml.ctl&amp;quot; contains several settings that are relevant for running the program &#039;&#039;&#039;codeml&#039;&#039;&#039;. Find the following lines and ensure that the file contains these values:&lt;br /&gt;
 &#039;&#039;&#039;seqfile =  gp120align.fasta&#039;&#039;&#039;:  name of alignment file&lt;br /&gt;
 &#039;&#039;&#039;treefile =  gp120tree.phy&#039;&#039;&#039;: name of tree file&lt;br /&gt;
 &#039;&#039;&#039;seqtype = 1&#039;&#039;&#039;: tells the program that our data consists of coding DNA.&lt;br /&gt;
 &#039;&#039;&#039;NSsites = 1 2&#039;&#039;&#039; : tells the program to analyze models M1 and M2.&lt;br /&gt;
 &#039;&#039;&#039;cleandata = 0&#039;&#039;&#039;: tells the program to keep positions with gaps.&lt;br /&gt;
&lt;br /&gt;
: The settings entered by us will cause codeml to analyze two hypotheses about dN/dS ratios. M1 says there are two classes of codons with different dN/dS ratios in the sequence: one class with dN/dS &amp;lt; 1 (codons under purifying or negative selection), and one class with dN/dS=1 (no selection - neutrally evolving sites). M2 says there are 3 distinct dN/dS ratios for different sites in the sequence: one class with dN/dS &amp;lt; 1, one class with dN/dS=1 (these are the same type of classes as for M1), and one class with dN/dS &amp;gt; 1 (corresponding to sites under positive selection). The value of the dN/dS ratios (for those classes that have dN/dS &amp;lt; 1 or dN/dS &amp;gt; 1), the fraction of sites belonging to each class, and the position of sites belonging to each class, are unknown at first and will be determined during the analysis.&lt;br /&gt;
&lt;br /&gt;
: (Regarding &amp;quot;cleandata&amp;quot; setting: &amp;quot;cleandata = 1&amp;quot; would cause codeml to discard any alignment columns with gaps or ambiguity symbols. For alignments with many sequences this can lead to discarding the majority of sites and I would therefore recommend not using that setting. At the same time you do want to be wary of columns where most sequences have gaps, since inference of selection for these will be very uncertain. It might be better to discard such columns from an alignment before doing the codeml analysis).&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Start the analysis&#039;&#039;&#039;&lt;br /&gt;
 codeml&lt;br /&gt;
: This will start the codeml program using the settings in the file codeml.ctl. Depending on your computer, this will take some minutes to finish. (You may be able to see how the optimization procedure results in progressively better fits: the likelihood increases, meaning that negative log-likelihood decreases, as the fit improves).&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Inspect result file:&#039;&#039;&#039;&lt;br /&gt;
: Wait for the run to finish, and then look at the result file:&lt;br /&gt;
 nedit selection.results &amp;amp;&lt;br /&gt;
: This file contains a wealth of information concerning your analysis. You may want to turn off line-wrapping to more clearly see the structure of the file (there will be some long lines that will otherwise wrap around). The top part of the file gives an overview of your sequences, codon usage and nucleotide frequencies. You can ignore this information for now, and move on to the interesting part, namely the model likelihoods and parameter values:&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Find likelihood, and number of free parameters for model M1&#039;&#039;&#039;&lt;br /&gt;
 Search ==&amp;gt; Find... ==&amp;gt; enter &amp;quot;Model 1&amp;quot; and click Find&lt;br /&gt;
: You are now in the region of the result file where the model likelihoods and parameter estimates are noted. Now, locate a line that looks a bit like the one shown below:&lt;br /&gt;
 lnL(ntime: 72  np: 74):  -4242.470345     +0.000000&lt;br /&gt;
: Identify the number of &amp;quot;free parameters&amp;quot;, K, used in model M1: This is indicated by &amp;quot;np&amp;quot;, and is 74 in the example shown above (most of these parameters are branch lengths in the tree; specifically, the number of branch length parameters is indicated by &amp;quot;ntime&amp;quot;, and is 72 in this example). Also note the log-likelihood of the fitted model. This is the number right after the parenthesis, and is -4242.470345 in the example here.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Question: &#039;&#039;&#039;What are the values of K and lnL for model M1?&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Question 6&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Find dn/dS ratios and codon class proportions for model M1:&#039;&#039;&#039;&lt;br /&gt;
: Scroll down a few lines until you get to a small table similar to this:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
      dN/dS for site classes (K=2)&lt;br /&gt;
      p:   0.75111  0.24889&lt;br /&gt;
      w:   0.06583  1.00000&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
: This gives a summary of the dN/dS ratios that were found in the data set. The line starting &amp;quot;w:&amp;quot; lists the two dN/dS ratios that were found (in this case 0.06583 and 1.00000 - the last one was pre-specified by us as part of the model and was therefore not a free parameter). In this context &amp;quot;w&amp;quot; means &amp;quot;omega&amp;quot;, which is the symbol typically used to represent the dN/dS rate ratio. The line starting p: gives the proportion of codon sites belonging to each of the dN/dS ratio classes (in the example above approximately 75% belong to the first class , while 25% of all sites belong to the class having dN/dS=1.00000).&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Question: &#039;&#039;&#039;What are the dN/dS value (w) and proportion (p) of sites for both classes. Report the following values: p(class1), w(class1), p(class2), w(class2)&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Question 7&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Find likelihood, and K for model M2&#039;&#039;&#039;&lt;br /&gt;
: Scroll past the M1 output until you get to the results for model M2.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Question: &#039;&#039;&#039;What are the values of K and lnL for model M2?&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Question 8&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Find dn/dS ratios and codon class proportions for model M2:&#039;&#039;&#039;&lt;br /&gt;
: Now, scroll down a few lines until you get to a small table similar to the one you examined for M1 before. For this model there are 3 separate classes of codons.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Question: &#039;&#039;&#039;What are the dN/dS value (w) and proportion (p) of sites for all three classes? Report these values: p(class1), w(class1), p(class2), w(class2), p(class3), w(class3)&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Question 9&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Assess strength of evidence for models M1 and M2:&#039;&#039;&#039;&lt;br /&gt;
: M2 will always have a better (higher) log-likelihood than model M1 because M2 has more free parameters, and M1 is nested within M2. You should now use the recipe given above to compute AIC values and model probabilities for M1 and M2.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Question: &#039;&#039;&#039; Report: AIC, ΔAIC, weight (model probability) for M1 and M2&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Question 10: &#039;&#039;&#039; Is M2 better than M1?&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Question 11&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Examine list of positively selected sites&#039;&#039;&#039;&lt;br /&gt;
: If your M2 is clearly better than M1 (I firmly believe it should be if you did things according to instructions...), then you have evidence for the existence of positively selected sites in the gp120 gene. Now, scroll down to the end of the result file and locate a list similar to the one below. Note: This is the &amp;quot;Bayes Empirical Bayes&amp;quot; table, not the &amp;quot;Naive Empirical Bayes&amp;quot; table. &lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Bayes Empirical Bayes (BEB) analysis&lt;br /&gt;
Positively selected sites&lt;br /&gt;
&lt;br /&gt;
         Prob(w&amp;gt;1)     mean w&lt;br /&gt;
&lt;br /&gt;
    25 A 0.959*        3.133 +- 0.769&lt;br /&gt;
    27 P 0.906         2.990 +- 0.877&lt;br /&gt;
    56 K 0.987*        3.197 +- 0.687&lt;br /&gt;
    59 V 0.915         3.032 +- 0.873&lt;br /&gt;
    78 R 0.637         2.351 +- 1.129&lt;br /&gt;
    88 K 0.573         2.148 +- 1.077&lt;br /&gt;
    95 V 0.925         3.046 +- 0.843&lt;br /&gt;
    ...&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
: It is not important what the distinction is in this context, but very briefly NEB ignores the fact that there is uncertainty about  maximum likelihood estimates, especially for smaller data sets (for instance w for some codon is perhaps not exactly 3.046, but could be in a region around that value), while [https://pubmed.ncbi.nlm.nih.gov/15689528/ BEB accounts for that uncertainty].&lt;br /&gt;
: This gives you a list of which residues (if any) that were found to belong to the positively selected dN/dS-class. Also listed is the probability that the site really is in the codon class where dN/dS &amp;gt; 1, and a weighted average of the w at the site. Using only DNA sequences you have now identified likely epitopes on the gp120 protein.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Question: &#039;&#039;&#039;List all sites having more than 95% probability of belonging to the positively selected class&lt;/div&gt;</summary>
		<author><name>Gorm</name></author>
	</entry>
	<entry>
		<id>https://teaching.healthtech.dtu.dk/22115/index.php?title=Model_selection&amp;diff=258</id>
		<title>Model selection</title>
		<link rel="alternate" type="text/html" href="https://teaching.healthtech.dtu.dk/22115/index.php?title=Model_selection&amp;diff=258"/>
		<updated>2026-04-15T05:47:22Z</updated>

		<summary type="html">&lt;p&gt;Gorm: /* Selection of substitution model using jmodeltest2 */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;This exercise is part of the course  [[22115_-_Computational_Molecular_Evolution|Computational Molecular Evolution (22115)]].&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Overview ==&lt;br /&gt;
&lt;br /&gt;
: In this exercise you are going to investigate features of HIV-1 evolution. You will do this by analyzing a large set of env-genes from HIV-1, subtype B. specifically, the DNA sequences analyzed here correspond to a region surrounding the hypervariable V3 region of the gp120 protein.&lt;br /&gt;
&lt;br /&gt;
: Like other retroviruses, particles of HIV are made up of 2 copies of a single-stranded RNA genome packaged inside a protein core, or capsid. The core particle also contains viral proteins that are essential for the early steps of the virus life cycle, such as reverse transcription and integration. A lipid envelope, derived from the infected cell, surrounds the core particle. Embedded in this envelope are the surface glycoproteins of HIV: gp120 and gp41. The gp120 protein is crucial for binding of the virus particle to target cells, while gp41 is important for the subsequent fusion event. It is the specific affinity of gp120 for the CD4 protein that targets HIV to those cells of the immune system that express CD4 on their surface (e.g., T-helper lymphocytes, monocytes, and macrophages).&lt;br /&gt;
&lt;br /&gt;
: The role gp120 plays in infection and the fact that it is situated on the surface of the HIV particle, means it is an obvious target for the immune response. That means that there may be a considerable selective pressure on gp120 for creating immune-escape mutants, where amino acids in the gp120 epitopes have been substituted. In this exercise you will construct a maximum likelihood tree that we will subsequently use to investigate whether you can detect such a selective pressure on parts of gp120, again using maximum likelihood methods.&lt;br /&gt;
&lt;br /&gt;
: One major goal with the exercise is to introduce you to statistically based methods for assessing the strength of evidence for a set of alternative hypotheses about some biological system of interest. The model selection method we will use is AIC (Akaike Information Criterion), based on which you will compute model probabilities. A second goal is to make you aware that phylogenetic analysis is not only about constructing trees, but that it is also a useful framework for analyzing biological questions more generally.&lt;br /&gt;
&lt;br /&gt;
: Specifically, you will&lt;br /&gt;
&lt;br /&gt;
:# perform a multiple alignment of gp120 DNA sequences taking protein-level information into account (using revtrans).&lt;br /&gt;
:# select a suitable nucleotide substitution model (using jmodeltest2)&lt;br /&gt;
:# construct a phylogenetic tree (using PAUP).&lt;br /&gt;
:# try to detect positively selected sites in gp120 (using PAML).&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
== Recipe for computing AIC values and model probabilities ==&lt;br /&gt;
&lt;br /&gt;
: Later in today&#039;s exercise you will be asked to compute AIC values and model probabilities. Return to this section and follow the instructions when you need to do so.&lt;br /&gt;
&lt;br /&gt;
:# Fit a set of models to your data, note the maximized log likelihoods (lnL) and the number of free parameters (K) for each model in the investigated set. The models you fit should represent a plausible and comprehensive set of hypotheses about your data.&lt;br /&gt;
:# Compute AIC for each of the models: &#039;&#039;&#039;AIC = -2 x lnL + 2K&#039;&#039;&#039;.  &amp;lt;br&amp;gt;For example: a model with lnL = -2010 and K = 5 will have AIC = -2 x -2010 + 2 x 5 = 4030.&lt;br /&gt;
:# Identify the model with the smallest AIC (this is the best model in the set). We will call the AIC for this model &#039;&#039;&#039;&amp;quot;AICmin&amp;quot;&#039;&#039;&#039;.&lt;br /&gt;
:# Compute the &amp;quot;ΔAIC&amp;quot; values for each model: &#039;&#039;&#039;ΔAIC = AIC - AICmin&#039;&#039;&#039; &amp;lt;br&amp;gt;For each model subtract the minimum AIC value. The best model will have a ΔAIC of zero. The rest of the models will have positive ΔAICs.&lt;br /&gt;
:# For each model compute the following quantity: &#039;&#039;&#039;numerator = exp(-0.5 x ΔAIC)&#039;&#039;&#039; &amp;lt;br&amp;gt;For example, a model with ΔAIC=4.2 will have numerator = exp(-0.5 x 4.2) = exp(-2.1) = 0.1225. Also compute the &#039;&#039;&#039;sum&#039;&#039;&#039; of the numerator values for all models.&lt;br /&gt;
:# Finally, the model probabilities for each model are found as: &#039;&#039;&#039;P(model) = numerator / sum&#039;&#039;&#039; &amp;lt;br&amp;gt;For example, if sum = 3.75 and a model has numerator = 1.3, then it has P(model) = 1.3 / 3.75 = 0.35&lt;br /&gt;
&lt;br /&gt;
: You may want to keep track of the computations by constructing a table along the following lines:&lt;br /&gt;
[[File:Molevol-Downloads-aictable.png|700px]]&lt;br /&gt;
&lt;br /&gt;
: Note that model probabilities can also be computed using Bayesian methods. One advantage of Bayesian methods over AIC is that instead of relying on a point estimate, uncertainty about parameter values is accounted for by integrating over all possible values (typically using MCMC).&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
== Getting started ==&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Create working directory, copy files&#039;&#039;&#039;&lt;br /&gt;
: In the command below: Instead of /path/to/molevol enter the path to the directory where you have placed your course files (for instance cd /Users/bob/Documents/molevol, or cd /home/student/molevol).&lt;br /&gt;
 cd /path/to/molevol&lt;br /&gt;
 mkdir modelselect&lt;br /&gt;
 cd modelselect&lt;br /&gt;
 cp ../data/gp120.fasta ./gp120.fasta&lt;br /&gt;
 cp ../data/codeml.ctl ./codeml.ctl&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Have a look at the DNA data file:&#039;&#039;&#039;&lt;br /&gt;
 nedit gp120.fasta &amp;amp;&lt;br /&gt;
: The file contains several DNA sequences from HIV-1, subtype B. The sequences are approximately 500 bp long, and correspond to a region surrounding the hypervariable V3 region in the gene encoding gp120. Close the nedit window when you&#039;ve had a look.&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
== Analysis of viral data set: alignment of coding DNA ==&lt;br /&gt;
&lt;br /&gt;
: There are three reasons why it is preferable to align coding DNA at the amino acid level rather than directly as nucleotides. First, the DNA alphabet has only four letters compared to 20 amino acids, which means that even unrelated DNA sequences will share roughly 25% identity by chance alone, making it much harder to distinguish true homology from noise. Second, protein alignments benefit from empirical substitution matrices such as BLOSUM-62, which capture the fact that amino acid replacements tend to be conservative. Equivalent matrices for DNA are far less informative because nucleotide substitution patterns vary strongly between genes and organisms. Third, synonymous substitutions accumulate faster than non-synonymous ones, so DNA sequences diverge more rapidly than the proteins they encode. Taken together, these factors mean that the phylogenetic signal in a DNA alignment erodes much faster than in the corresponding protein alignment.&lt;br /&gt;
&lt;br /&gt;
: However, while protein-level alignment gives us the most reliable homology signal, it discards exactly the information we need for studying selection: the synonymous substitutions. Estimating the ratio of non-synonymous to synonymous substitution rates (dN/dS) requires a codon-level alignment where analogous codon positions are properly lined up. We would therefore like to construct a multiple alignment at the DNA level, but using information at the protein level, and the RevTrans server does exactly that.&lt;br /&gt;
&lt;br /&gt;
: &#039;&#039;&#039;RevTrans&#039;&#039;&#039; takes as input an unaligned set of DNA sequences, automatically translates them to the equivalent amino acid sequences, constructs a multiple alignment of the protein sequences, and finally uses the protein alignment as a template for constructing a multiple DNA alignment. Because each amino acid maps back to its original codon, gaps are always inserted in groups of three and codon boundaries are preserved throughout the alignment. This is essential for the dN/dS analyses we will carry out in this exercise: if codon positions are misaligned, synonymous and non-synonymous changes cannot be correctly distinguished.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Construct RevTrans alignment&#039;&#039;&#039;&lt;br /&gt;
:* Open RevTrans server page: https://services.healthtech.dtu.dk/services/RevTrans-2.0/&lt;br /&gt;
:* On the RevTrans page: Choose the file gp120.fasta as input (or copy and paste the sequence into the sequence window)&lt;br /&gt;
:* Click the &amp;quot;Submit query&amp;quot; button&lt;br /&gt;
:* When the alignment is done you may have to click link named &amp;quot;here&amp;quot; to go to results page&lt;br /&gt;
:* Download DNA alignment, by right-clicking the link for &amp;quot;Download alignment in FASTA format&amp;quot;, and choosing &amp;quot;Save link as...&amp;quot; (save file under the name gp120align.fasta and make sure to save the file in the directory modelselect).&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Convert alignment to NEXUS format&#039;&#039;&#039;&lt;br /&gt;
: Convert the fasta file to NEXUS format and save file in the modelselect directory under the name gp120.nexus&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
== Selection of substitution model using jmodeltest2 ==&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Question 1&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
: As part of the present analysis we are going to build a phylogenetic tree based on the DNA alignment constructed above. We will construct the tree using maximum likelihood, but to do that we first have to decide which substitution model we want to use. Specifically, we are interested in using the model that best describes our data without having more parameters than strictly necessary (thus avoiding overfitting). We will investigate this issue by fitting a set of 56 different models to our data and then selecting one with a reasonable balance between model complexity and data fit.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Start jmodeltest2&#039;&#039;&#039;&lt;br /&gt;
 jmodeltest&lt;br /&gt;
&#039;&#039;&#039;Load data set&#039;&#039;&#039;&lt;br /&gt;
:* File -&amp;gt; Load DNA alignment -&amp;gt; File Format -&amp;gt; Select &amp;quot;All files&amp;quot;&lt;br /&gt;
:* Navigate to gp120.nexus and load it&lt;br /&gt;
&#039;&#039;&#039;Fit 56 models&#039;&#039;&#039;&lt;br /&gt;
:* Analysis -&amp;gt; Compute likelihood scores&lt;br /&gt;
:* Select &amp;quot;7&amp;quot; under &amp;quot;Number of substitution schemes&amp;quot;&lt;br /&gt;
:* Select &amp;quot;Fixed BIONJ-JC&amp;quot; under &amp;quot;Base tree for likelihood calculations&amp;quot;&lt;br /&gt;
:* Click &amp;quot;Compute likelihoods&amp;quot;&lt;br /&gt;
&lt;br /&gt;
: This causes jmodeltest2 to perform the following actions: first a neighbor joining tree is constructed using the Jukes and Cantor model. Then the tree is fixed and used as the basis for fitting a set of 56 different models to the data. For each model, the estimated model parameters and the negative log-likelihood are recorded. In addition to varying sets of substitution rate parameters (JC, K2P, ...), some of these models also include extra parameters that take into account the presence of different rates between sites. This is done in two ways: (1) by fitting a gamma distribution of rates (&amp;quot;+G&amp;quot;), and (2) by allowing for a proportion of constant (&amp;quot;invariable&amp;quot;) sites (&amp;quot;+I&amp;quot;).&lt;br /&gt;
&lt;br /&gt;
: Wait until jmodeltest2 is done fitting all 56 models (this will take a little while depending on your computer).&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Inspect result, manually check model probabilities for three models&#039;&#039;&#039;&lt;br /&gt;
:* Results -&amp;gt; Show results table&lt;br /&gt;
: For each model this table lists the negative log-likelihood (&amp;quot;-lnL&amp;quot;), the number of parameters (&amp;quot;p&amp;quot; - note this includes branch lengths), and estimates of all model parameters (excluding branch lengths).&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Manually compute model probabilities for three substitution models&#039;&#039;&#039;&lt;br /&gt;
: Use AIC-based model probabilities to investigate which of the following three substitution models are best at describing how the sequences have evolved:&lt;br /&gt;
:* Jukes and Cantor with fraction of invariant sites (JC+I)&lt;br /&gt;
:* Jukes and Cantor with gamma-distributed rates over sites (JC+G)&lt;br /&gt;
:* Jukes and Cantor with invariant sites and gamma-distributed rates (JC+I+G)&lt;br /&gt;
: Before you can do the computation you need to know the log likelihood and the number of parameters for each model. Locate these values in the table for the JC+I, JC+G, and JC+I+G models, and write them down. Close the window with the result table when you are done.&lt;br /&gt;
&lt;br /&gt;
:Make sure to get the signs right: the values reported in the table are -lnL values, so you will need to reverse the sign to get the lnL (the lnL values you write down should be negative). &lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Question: &#039;&#039;&#039; Use the recipe above to compute AIC values and model probabilities. Report the results in a table similar to the one shown above&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Question 2&#039;&#039;&#039; Based on the model probabilities: wich model has more support?&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Question 3&#039;&#039;&#039;&lt;br /&gt;
&#039;&#039;&#039;Use modeltest program to select best model&#039;&#039;&#039;&lt;br /&gt;
: What you just did manually for JC+I, JC+G and JC+I+G, jmodeltest2 can do automatically for the full set of 56 fitted models. Specifically, it uses the list of negative log likelihoods and parameter counts in the table to compute AIC and model probabilities, and uses this to select the model that best fits the sequence data:&lt;br /&gt;
:* Analysis -&amp;gt; Do AIC calculations -&amp;gt; &lt;br /&gt;
:* Select &amp;quot;Write PAUP* block&amp;quot;&lt;br /&gt;
:* click &amp;quot;Do AIC calculations&amp;quot;&lt;br /&gt;
:* Results -&amp;gt; Show results table&lt;br /&gt;
:* Select &amp;quot;AIC&amp;quot; tab&lt;br /&gt;
:* &#039;&#039;&#039;IMPORTANT:&#039;&#039;&#039; SHIFT+click on the header of the &amp;quot;weight&amp;quot; column. This sorts the rows according to model weight, in descending order.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Question: &#039;&#039;&#039;What model was selected by modeltest based on the AIC values? (This will be the model with the highest weight - and lowest AIC - and will be in the first row of the results table after SHIFT-clicking the &amp;quot;weight&amp;quot; header).&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
== Construction of phylogenetic tree using PAUP ==&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Question 4&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
: Close the results table. In the main window you should now scroll up to the lines giving PAUP commands that will implement the selected model. The command is enclosed between &amp;quot;BEGIN PAUP&amp;quot; and &amp;quot;END;&amp;quot; and should look something like this:&lt;br /&gt;
 Lset Base=(0.4064, [...]&lt;br /&gt;
: You will need to copy this command to a PAUP session in the next step.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Start PAUP&#039;&#039;&#039;&lt;br /&gt;
 paup&lt;br /&gt;
: Above you used jmodeltest2 to select the most suitable substitution model for the present data set. You will now use this model to construct a maximum likelihood tree. You will use PAUP for this purpose. (note: it is possible to create a maximum likelihood or a model-averaged tree directly from the jmodeltest2 program, but we will instead do it in PAUP in order to more clearly see each step that is taken).&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Load alignment:&#039;&#039;&#039;&lt;br /&gt;
 execute gp120.nexus&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Set tree-building criterion to maximum likelihood&#039;&#039;&#039;&lt;br /&gt;
 set criterion=likelihood&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Set model parameters to winning estimates&#039;&#039;&#039;&lt;br /&gt;
: Above you located a set of lines in the jmodeltest2 output giving a PAUP command that sets the model parameters to the estimates that were found using the winning model. Copy and paste this lset command (without the BEGIN and END parts) into the window where PAUP is running.&lt;br /&gt;
&lt;br /&gt;
 PASTE LSET COMMAND FROM MODELTEST RUN HERE&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Find best tree using selected model&#039;&#039;&#039;&lt;br /&gt;
: Still in the PAUP-window, enter the following command&lt;br /&gt;
 hsearch swap=tbr start=nj&lt;br /&gt;
: This command causes PAUP to perform a heuristic search for the best maximum likelihood tree. Once an initial tree has been constructed, the heuristic search proceeds by rearrangements of the &amp;quot;tree bisection and reconnection&amp;quot; type (TBR). We are using the model selected by modeltest, AND the parameter estimates found by modeltest on that model. You could also have chosen to simply estimate all the model parameters as part of this step (i.e., at the same time as finding the best tree), but fixing them improves speed tremendously. Findind the best tree should take a few minutes at most.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Save best tree to file&#039;&#039;&#039;&lt;br /&gt;
 savetrees format=newick brlens=yes file=gp120tree.phy from=1 to=1&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Quit program&#039;&#039;&#039;&lt;br /&gt;
 quit&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Have a look at the tree:&#039;&#039;&#039;&lt;br /&gt;
: You have now produced an unrooted tree of the HIV sequences and saved it in the file gp120tree.phy. Note that in this exercise we will not be interested in the tree as such - our focus is instead on finding positive selection on a subset of codon positions and the tree is just something we need in order to be able to fit the different codon models to the data. If you want to see the tree, you can do so with the following command:&lt;br /&gt;
 figtree gp120tree.phy &amp;amp;&lt;br /&gt;
: There is no meaningful root placed in this tree, so you may want to choose the unrooted view (the third icon in the Layout section of the figtree window). Close the figtree window when you have had a look&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Question: &#039;&#039;&#039;What is the negative log likelihood of the tree you just found?&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
== Detection of positively selected sites in gp120 ==&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Question 5&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
: There is much more to phylogenetic analyses than merely reconstructing trees. One interesting result of probabilistic methods, is that the parameters of a model will have their values estimated as part of the optimization procedure. This means that once such a model has been fitted to the data, it is possible to investigate these estimated parameter values to learn features about the evolutionary history of the sequences under investigation. In the present example we will focus on investigating whether we can find positively selected sites in our data set, defined as sites where the dN/dS ratio is larger than 1. We do that by using a codon substitution model where the dN/dS ratio is one of its parameters.&lt;br /&gt;
&lt;br /&gt;
: A further strength of the probabilistic approach is that you get a probabilistic measure of how well any model fits the data. This means you can use a stringent approach to determine which model fits the data best. In this framework one uses likelihoods (probabilities of data given model) to determine which model fits the data best. As you saw above, it is for instance possible to compute AIC values and weights (model probabilities) from the likelihood values of fitted models, Since each model essentially corresponds to a hypothesis about the evolutionary history of the data, we can thus use a stringent statistical approach to decide which hypothesis best describes our data.&lt;br /&gt;
&lt;br /&gt;
: In outline, you will now use the following steps to investigate whether there is any evidence for positively selected codons in your data set:&lt;br /&gt;
&lt;br /&gt;
:* Fit model M1, which assumes there are two classes of codons in the sequence: some with dN/dS &amp;lt; 1, some with dN/dS=1.&lt;br /&gt;
:* Fit model M2, which assumes 3 distinct classes of codons: two with dN/dS ratios as for M1, and one extra class with dN/dS &amp;gt; 1.&lt;br /&gt;
:* Assess the strength of evidence for the two models using AIC-based model probabilities&lt;br /&gt;
:* If M2 is better: identify the positively selected codons&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Inspect the parameter file&#039;&#039;&#039;&lt;br /&gt;
 nedit codeml.ctl &amp;amp;&lt;br /&gt;
: The file &amp;quot;codeml.ctl&amp;quot; contains several settings that are relevant for running the program &#039;&#039;&#039;codeml&#039;&#039;&#039;. Find the following lines and ensure that the file contains these values:&lt;br /&gt;
 &#039;&#039;&#039;seqfile =  gp120align.fasta&#039;&#039;&#039;:  name of alignment file&lt;br /&gt;
 &#039;&#039;&#039;treefile =  gp120tree.phy&#039;&#039;&#039;: name of tree file&lt;br /&gt;
 &#039;&#039;&#039;seqtype = 1&#039;&#039;&#039;: tells the program that our data consists of coding DNA.&lt;br /&gt;
 &#039;&#039;&#039;NSsites = 1 2&#039;&#039;&#039; : tells the program to analyze models M1 and M2.&lt;br /&gt;
 &#039;&#039;&#039;cleandata = 0&#039;&#039;&#039;: tells the program to keep positions with gaps.&lt;br /&gt;
&lt;br /&gt;
: The settings entered by us will cause codeml to analyze two hypotheses about dN/dS ratios. M1 says there are two classes of codons with different dN/dS ratios in the sequence: one class with dN/dS &amp;lt; 1 (codons under purifying or negative selection), and one class with dN/dS=1 (no selection - neutrally evolving sites). M2 says there are 3 distinct dN/dS ratios for different sites in the sequence: one class with dN/dS &amp;lt; 1, one class with dN/dS=1 (these are the same type of classes as for M1), and one class with dN/dS &amp;gt; 1 (corresponding to sites under positive selection). The value of the dN/dS ratios (for those classes that have dN/dS &amp;lt; 1 or dN/dS &amp;gt; 1), the fraction of sites belonging to each class, and the position of sites belonging to each class, are unknown at first and will be determined during the analysis.&lt;br /&gt;
&lt;br /&gt;
: (Regarding &amp;quot;cleandata&amp;quot; setting: &amp;quot;cleandata = 1&amp;quot; would cause codeml to discard any alignment columns with gaps or ambiguity symbols. For alignments with many sequences this can lead to discarding the majority of sites and I would therefore recommend not using that setting. At the same time you do want to be wary of columns where most sequences have gaps, since inference of selection for these will be very uncertain. It might be better to discard such columns from an alignment before doing the codeml analysis).&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Start the analysis&#039;&#039;&#039;&lt;br /&gt;
 codeml&lt;br /&gt;
: This will start the codeml program using the settings in the file codeml.ctl. Depending on your computer, this will take some minutes to finish. (You may be able to see how the optimization procedure results in progressively better fits: the likelihood increases, meaning that negative log-likelihood decreases, as the fit improves).&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Inspect result file:&#039;&#039;&#039;&lt;br /&gt;
: Wait for the run to finish, and then look at the result file:&lt;br /&gt;
 nedit selection.results &amp;amp;&lt;br /&gt;
: This file contains a wealth of information concerning your analysis. You may want to turn off line-wrapping to more clearly see the structure of the file (there will be some long lines that will otherwise wrap around). The top part of the file gives an overview of your sequences, codon usage and nucleotide frequencies. You can ignore this information for now, and move on to the interesting part, namely the model likelihoods and parameter values:&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Find likelihood, and number of free parameters for model M1&#039;&#039;&#039;&lt;br /&gt;
 Search ==&amp;gt; Find... ==&amp;gt; enter &amp;quot;Model 1&amp;quot; and click Find&lt;br /&gt;
: You are now in the region of the result file where the model likelihoods and parameter estimates are noted. Now, locate a line that looks a bit like the one shown below:&lt;br /&gt;
 lnL(ntime: 72  np: 74):  -4242.470345     +0.000000&lt;br /&gt;
: Identify the number of &amp;quot;free parameters&amp;quot;, K, used in model M1: This is indicated by &amp;quot;np&amp;quot;, and is 74 in the example shown above (most of these parameters are branch lengths in the tree; specifically, the number of branch length parameters is indicated by &amp;quot;ntime&amp;quot;, and is 72 in this example). Also note the log-likelihood of the fitted model. This is the number right after the parenthesis, and is -4242.470345 in the example here.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Question: &#039;&#039;&#039;What are the values of K and lnL for model M1?&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Question 6&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Find dn/dS ratios and codon class proportions for model M1:&#039;&#039;&#039;&lt;br /&gt;
: Scroll down a few lines until you get to a small table similar to this:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
      dN/dS for site classes (K=2)&lt;br /&gt;
      p:   0.75111  0.24889&lt;br /&gt;
      w:   0.06583  1.00000&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
: This gives a summary of the dN/dS ratios that were found in the data set. The line starting &amp;quot;w:&amp;quot; lists the two dN/dS ratios that were found (in this case 0.06583 and 1.00000 - the last one was pre-specified by us as part of the model and was therefore not a free parameter). In this context &amp;quot;w&amp;quot; means &amp;quot;omega&amp;quot;, which is the symbol typically used to represent the dN/dS rate ratio. The line starting p: gives the proportion of codon sites belonging to each of the dN/dS ratio classes (in the example above approximately 75% belong to the first class , while 25% of all sites belong to the class having dN/dS=1.00000).&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Question: &#039;&#039;&#039;What are the dN/dS value (w) and proportion (p) of sites for both classes. Report the following values: p(class1), w(class1), p(class2), w(class2)&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Question 7&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Find likelihood, and K for model M2&#039;&#039;&#039;&lt;br /&gt;
: Scroll past the M1 output until you get to the results for model M2.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Question: &#039;&#039;&#039;What are the values of K and lnL for model M2?&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Question 8&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Find dn/dS ratios and codon class proportions for model M2:&#039;&#039;&#039;&lt;br /&gt;
: Now, scroll down a few lines until you get to a small table similar to the one you examined for M1 before. For this model there are 3 separate classes of codons.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Question: &#039;&#039;&#039;What are the dN/dS value (w) and proportion (p) of sites for all three classes? Report these values: p(class1), w(class1), p(class2), w(class2), p(class3), w(class3)&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Question 9&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Assess strength of evidence for models M1 and M2:&#039;&#039;&#039;&lt;br /&gt;
: M2 will always have a better (higher) log-likelihood than model M1 because M2 has more free parameters, and M1 is nested within M2. You should now use the recipe given above to compute AIC values and model probabilities for M1 and M2.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Question: &#039;&#039;&#039; Report: AIC, ΔAIC, weight (model probability) for M1 and M2&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Question 10: &#039;&#039;&#039; Is M2 better than M1?&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Question 11&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Examine list of positively selected sites&#039;&#039;&#039;&lt;br /&gt;
: If your M2 is clearly better than M1 (I firmly believe it should be if you did things according to instructions...), then you have evidence for the existence of positively selected sites in the gp120 gene. Now, scroll down to the end of the result file and locate a list similar to the one below. Note: This is the &amp;quot;Bayes Empirical Bayes&amp;quot; table, not the &amp;quot;Naive Empirical Bayes&amp;quot; table. &lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Bayes Empirical Bayes (BEB) analysis&lt;br /&gt;
Positively selected sites&lt;br /&gt;
&lt;br /&gt;
         Prob(w&amp;gt;1)     mean w&lt;br /&gt;
&lt;br /&gt;
    25 A 0.959*        3.133 +- 0.769&lt;br /&gt;
    27 P 0.906         2.990 +- 0.877&lt;br /&gt;
    56 K 0.987*        3.197 +- 0.687&lt;br /&gt;
    59 V 0.915         3.032 +- 0.873&lt;br /&gt;
    78 R 0.637         2.351 +- 1.129&lt;br /&gt;
    88 K 0.573         2.148 +- 1.077&lt;br /&gt;
    95 V 0.925         3.046 +- 0.843&lt;br /&gt;
    ...&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
: It is not important what the distinction is in this context, but very briefly NEB ignores the fact that there is uncertainty about  maximum likelihood estimates, especially for smaller data sets (for instance w for some codon is perhaps not exactly 3.046, but could be in a region around that value), while [https://pubmed.ncbi.nlm.nih.gov/15689528/ BEB accounts for that uncertainty].&lt;br /&gt;
: This gives you a list of which residues (if any) that were found to belong to the positively selected dN/dS-class. Also listed is the probability that the site really is in the codon class where dN/dS &amp;gt; 1, and a weighted average of the w at the site. Using only DNA sequences you have now identified likely epitopes on the gp120 protein.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Question: &#039;&#039;&#039;List all sites having more than 95% probability of belonging to the positively selected class&lt;/div&gt;</summary>
		<author><name>Gorm</name></author>
	</entry>
	<entry>
		<id>https://teaching.healthtech.dtu.dk/22115/index.php?title=Model_selection&amp;diff=257</id>
		<title>Model selection</title>
		<link rel="alternate" type="text/html" href="https://teaching.healthtech.dtu.dk/22115/index.php?title=Model_selection&amp;diff=257"/>
		<updated>2026-04-15T05:31:28Z</updated>

		<summary type="html">&lt;p&gt;Gorm: /* Analysis of viral data set: alignment of coding DNA */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;This exercise is part of the course  [[22115_-_Computational_Molecular_Evolution|Computational Molecular Evolution (22115)]].&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Overview ==&lt;br /&gt;
&lt;br /&gt;
: In this exercise you are going to investigate features of HIV-1 evolution. You will do this by analyzing a large set of env-genes from HIV-1, subtype B. specifically, the DNA sequences analyzed here correspond to a region surrounding the hypervariable V3 region of the gp120 protein.&lt;br /&gt;
&lt;br /&gt;
: Like other retroviruses, particles of HIV are made up of 2 copies of a single-stranded RNA genome packaged inside a protein core, or capsid. The core particle also contains viral proteins that are essential for the early steps of the virus life cycle, such as reverse transcription and integration. A lipid envelope, derived from the infected cell, surrounds the core particle. Embedded in this envelope are the surface glycoproteins of HIV: gp120 and gp41. The gp120 protein is crucial for binding of the virus particle to target cells, while gp41 is important for the subsequent fusion event. It is the specific affinity of gp120 for the CD4 protein that targets HIV to those cells of the immune system that express CD4 on their surface (e.g., T-helper lymphocytes, monocytes, and macrophages).&lt;br /&gt;
&lt;br /&gt;
: The role gp120 plays in infection and the fact that it is situated on the surface of the HIV particle, means it is an obvious target for the immune response. That means that there may be a considerable selective pressure on gp120 for creating immune-escape mutants, where amino acids in the gp120 epitopes have been substituted. In this exercise you will construct a maximum likelihood tree that we will subsequently use to investigate whether you can detect such a selective pressure on parts of gp120, again using maximum likelihood methods.&lt;br /&gt;
&lt;br /&gt;
: One major goal with the exercise is to introduce you to statistically based methods for assessing the strength of evidence for a set of alternative hypotheses about some biological system of interest. The model selection method we will use is AIC (Akaike Information Criterion), based on which you will compute model probabilities. A second goal is to make you aware that phylogenetic analysis is not only about constructing trees, but that it is also a useful framework for analyzing biological questions more generally.&lt;br /&gt;
&lt;br /&gt;
: Specifically, you will&lt;br /&gt;
&lt;br /&gt;
:# perform a multiple alignment of gp120 DNA sequences taking protein-level information into account (using revtrans).&lt;br /&gt;
:# select a suitable nucleotide substitution model (using jmodeltest2)&lt;br /&gt;
:# construct a phylogenetic tree (using PAUP).&lt;br /&gt;
:# try to detect positively selected sites in gp120 (using PAML).&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
== Recipe for computing AIC values and model probabilities ==&lt;br /&gt;
&lt;br /&gt;
: Later in today&#039;s exercise you will be asked to compute AIC values and model probabilities. Return to this section and follow the instructions when you need to do so.&lt;br /&gt;
&lt;br /&gt;
:# Fit a set of models to your data, note the maximized log likelihoods (lnL) and the number of free parameters (K) for each model in the investigated set. The models you fit should represent a plausible and comprehensive set of hypotheses about your data.&lt;br /&gt;
:# Compute AIC for each of the models: &#039;&#039;&#039;AIC = -2 x lnL + 2K&#039;&#039;&#039;.  &amp;lt;br&amp;gt;For example: a model with lnL = -2010 and K = 5 will have AIC = -2 x -2010 + 2 x 5 = 4030.&lt;br /&gt;
:# Identify the model with the smallest AIC (this is the best model in the set). We will call the AIC for this model &#039;&#039;&#039;&amp;quot;AICmin&amp;quot;&#039;&#039;&#039;.&lt;br /&gt;
:# Compute the &amp;quot;ΔAIC&amp;quot; values for each model: &#039;&#039;&#039;ΔAIC = AIC - AICmin&#039;&#039;&#039; &amp;lt;br&amp;gt;For each model subtract the minimum AIC value. The best model will have a ΔAIC of zero. The rest of the models will have positive ΔAICs.&lt;br /&gt;
:# For each model compute the following quantity: &#039;&#039;&#039;numerator = exp(-0.5 x ΔAIC)&#039;&#039;&#039; &amp;lt;br&amp;gt;For example, a model with ΔAIC=4.2 will have numerator = exp(-0.5 x 4.2) = exp(-2.1) = 0.1225. Also compute the &#039;&#039;&#039;sum&#039;&#039;&#039; of the numerator values for all models.&lt;br /&gt;
:# Finally, the model probabilities for each model are found as: &#039;&#039;&#039;P(model) = numerator / sum&#039;&#039;&#039; &amp;lt;br&amp;gt;For example, if sum = 3.75 and a model has numerator = 1.3, then it has P(model) = 1.3 / 3.75 = 0.35&lt;br /&gt;
&lt;br /&gt;
: You may want to keep track of the computations by constructing a table along the following lines:&lt;br /&gt;
[[File:Molevol-Downloads-aictable.png|700px]]&lt;br /&gt;
&lt;br /&gt;
: Note that model probabilities can also be computed using Bayesian methods. One advantage of Bayesian methods over AIC is that instead of relying on a point estimate, uncertainty about parameter values is accounted for by integrating over all possible values (typically using MCMC).&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
== Getting started ==&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Create working directory, copy files&#039;&#039;&#039;&lt;br /&gt;
: In the command below: Instead of /path/to/molevol enter the path to the directory where you have placed your course files (for instance cd /Users/bob/Documents/molevol, or cd /home/student/molevol).&lt;br /&gt;
 cd /path/to/molevol&lt;br /&gt;
 mkdir modelselect&lt;br /&gt;
 cd modelselect&lt;br /&gt;
 cp ../data/gp120.fasta ./gp120.fasta&lt;br /&gt;
 cp ../data/codeml.ctl ./codeml.ctl&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Have a look at the DNA data file:&#039;&#039;&#039;&lt;br /&gt;
 nedit gp120.fasta &amp;amp;&lt;br /&gt;
: The file contains several DNA sequences from HIV-1, subtype B. The sequences are approximately 500 bp long, and correspond to a region surrounding the hypervariable V3 region in the gene encoding gp120. Close the nedit window when you&#039;ve had a look.&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
== Analysis of viral data set: alignment of coding DNA ==&lt;br /&gt;
&lt;br /&gt;
: There are three reasons why it is preferable to align coding DNA at the amino acid level rather than directly as nucleotides. First, the DNA alphabet has only four letters compared to 20 amino acids, which means that even unrelated DNA sequences will share roughly 25% identity by chance alone, making it much harder to distinguish true homology from noise. Second, protein alignments benefit from empirical substitution matrices such as BLOSUM-62, which capture the fact that amino acid replacements tend to be conservative. Equivalent matrices for DNA are far less informative because nucleotide substitution patterns vary strongly between genes and organisms. Third, synonymous substitutions accumulate faster than non-synonymous ones, so DNA sequences diverge more rapidly than the proteins they encode. Taken together, these factors mean that the phylogenetic signal in a DNA alignment erodes much faster than in the corresponding protein alignment.&lt;br /&gt;
&lt;br /&gt;
: However, while protein-level alignment gives us the most reliable homology signal, it discards exactly the information we need for studying selection: the synonymous substitutions. Estimating the ratio of non-synonymous to synonymous substitution rates (dN/dS) requires a codon-level alignment where analogous codon positions are properly lined up. We would therefore like to construct a multiple alignment at the DNA level, but using information at the protein level, and the RevTrans server does exactly that.&lt;br /&gt;
&lt;br /&gt;
: &#039;&#039;&#039;RevTrans&#039;&#039;&#039; takes as input an unaligned set of DNA sequences, automatically translates them to the equivalent amino acid sequences, constructs a multiple alignment of the protein sequences, and finally uses the protein alignment as a template for constructing a multiple DNA alignment. Because each amino acid maps back to its original codon, gaps are always inserted in groups of three and codon boundaries are preserved throughout the alignment. This is essential for the dN/dS analyses we will carry out in this exercise: if codon positions are misaligned, synonymous and non-synonymous changes cannot be correctly distinguished.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Construct RevTrans alignment&#039;&#039;&#039;&lt;br /&gt;
:* Open RevTrans server page: https://services.healthtech.dtu.dk/services/RevTrans-2.0/&lt;br /&gt;
:* On the RevTrans page: Choose the file gp120.fasta as input (or copy and paste the sequence into the sequence window)&lt;br /&gt;
:* Click the &amp;quot;Submit query&amp;quot; button&lt;br /&gt;
:* When the alignment is done you may have to click link named &amp;quot;here&amp;quot; to go to results page&lt;br /&gt;
:* Download DNA alignment, by right-clicking the link for &amp;quot;Download alignment in FASTA format&amp;quot;, and choosing &amp;quot;Save link as...&amp;quot; (save file under the name gp120align.fasta and make sure to save the file in the directory modelselect).&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Convert alignment to NEXUS format&#039;&#039;&#039;&lt;br /&gt;
: Convert the fasta file to NEXUS format and save file in the modelselect directory under the name gp120.nexus&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
== Selection of substitution model using jmodeltest2 ==&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Question 1&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
: As part of the present analysis we are going to build a phylogenetic tree based on the DNA alignment constructed above. We will construct the tree using maximum likelihood, but to do that we first have to decide which substitution model we want to use. Specifically, we are interested in using the model that best describes our data without having more parameters than strictly necessary (thus avoiding overfitting). We will investigate this issue by fitting a set of 56 different models to our data and then selecting one with a reasonable balance between model complexity and data fit.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Start jmodeltest2&#039;&#039;&#039;&lt;br /&gt;
 jmodeltest&lt;br /&gt;
&#039;&#039;&#039;Load data set&#039;&#039;&#039;&lt;br /&gt;
:* File -&amp;gt; Load DNA alignment -&amp;gt; File Format -&amp;gt; Select &amp;quot;All files&amp;quot;&lt;br /&gt;
:* Navigate to gp120.nexus and load it&lt;br /&gt;
&#039;&#039;&#039;Fit 56 models&#039;&#039;&#039;&lt;br /&gt;
:* Analysis -&amp;gt; Compute likelihood scores&lt;br /&gt;
:* Select &amp;quot;7&amp;quot; under &amp;quot;Number of substitution schemes&amp;quot;&lt;br /&gt;
:* Select &amp;quot;Fixed BIONJ-JC&amp;quot; under &amp;quot;Base tree for likelihood calculations&amp;quot;&lt;br /&gt;
:* Click &amp;quot;Compute likelihoods&amp;quot;&lt;br /&gt;
&lt;br /&gt;
: This causes jmodeltest2 to perform the following actions: first a neighbor joining tree is constructed using the Jukes and Cantor model. Then the tree is fixed and used as the basis for fitting a set of 56 different models to the data. For each model, the estimated model parameters and the negative log-likelihood are recorded. In addition to varying sets of substitution rate parameters (JC, K2P, ...), some of these models also include extra parameters that take into account the presence of different rates between sites. This is done in two ways: (1) by fitting a gamma distribution of rates (&amp;quot;+G&amp;quot;), and (2) by allowing for a proportion of constant (&amp;quot;invariable&amp;quot;) sites (&amp;quot;+I&amp;quot;).&lt;br /&gt;
&lt;br /&gt;
: Wait until jmodeltest2 is done fitting all 56 models (this will take a little while depending on your computer).&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Inspect result, manually check model probabilities for three models&#039;&#039;&#039;&lt;br /&gt;
:* Results -&amp;gt; Show results table&lt;br /&gt;
: For each model this table lists the negative log-likelihood (&amp;quot;-lnL&amp;quot;), the number of parameters (&amp;quot;p&amp;quot;), and estimates of all model parameters (excluding branch lengths).&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Manually compute model probabilities for three substitution models&#039;&#039;&#039;&lt;br /&gt;
: Use AIC-based model probabilities to investigate which of the following three substitution models are best at describing how the sequences have evolved:&lt;br /&gt;
:* Jukes and Cantor with fraction of invariant sites (JC+I)&lt;br /&gt;
:* Jukes and Cantor with gamma-distributed rates over sites (JC+G)&lt;br /&gt;
:* Jukes and Cantor with invariant sites and gamma-distributed rates (JC+I+G)&lt;br /&gt;
: Before you can do the computation you need to know the log likelihood and the number of parameters for each model. Locate these values in the table for the JC+I, JC+G, and JC+I+G models, and write them down. Close the window with the result table when you are done.&lt;br /&gt;
&lt;br /&gt;
:Make sure to get the signs right: the values reported in the table are -lnL values, so you will need to reverse the sign to get the lnL (the lnL values you write down should be negative). &lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Question: &#039;&#039;&#039; Use the recipe above to compute AIC values and model probabilities. Report the results in a table similar to the one shown above&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Question 2&#039;&#039;&#039; Based on the model probabilities: wich model has more support?&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Question 3&#039;&#039;&#039;&lt;br /&gt;
&#039;&#039;&#039;Use modeltest program to select best model&#039;&#039;&#039;&lt;br /&gt;
: What you just did manually for JC+I, JC+G and JC+I+G, jmodeltest2 can do automatically for the full set of 56 fitted models. Specifically, it uses the list of negative log likelihoods and parameter counts in the table to compute AIC and model probabilities, and uses this to select the model that best fits the sequence data:&lt;br /&gt;
:* Analysis -&amp;gt; Do AIC calculations -&amp;gt; &lt;br /&gt;
:* Select &amp;quot;Write PAUP* block&amp;quot;&lt;br /&gt;
:* click &amp;quot;Do AIC calculations&amp;quot;&lt;br /&gt;
:* Results -&amp;gt; Show results table&lt;br /&gt;
:* Select &amp;quot;AIC&amp;quot; tab&lt;br /&gt;
:* &#039;&#039;&#039;IMPORTANT:&#039;&#039;&#039; SHIFT+click on the header of the &amp;quot;weight&amp;quot; column. This sorts the rows according to model weight, in descending order.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Question: &#039;&#039;&#039;What model was selected by modeltest based on the AIC values? (This will be the model with the highest weight - and lowest AIC - and will be in the first row of the results table after SHIFT-clicking the &amp;quot;weight&amp;quot; header).&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
== Construction of phylogenetic tree using PAUP ==&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Question 4&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
: Close the results table. In the main window you should now scroll up to the lines giving PAUP commands that will implement the selected model. The command is enclosed between &amp;quot;BEGIN PAUP&amp;quot; and &amp;quot;END;&amp;quot; and should look something like this:&lt;br /&gt;
 Lset Base=(0.4064, [...]&lt;br /&gt;
: You will need to copy this command to a PAUP session in the next step.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Start PAUP&#039;&#039;&#039;&lt;br /&gt;
 paup&lt;br /&gt;
: Above you used jmodeltest2 to select the most suitable substitution model for the present data set. You will now use this model to construct a maximum likelihood tree. You will use PAUP for this purpose. (note: it is possible to create a maximum likelihood or a model-averaged tree directly from the jmodeltest2 program, but we will instead do it in PAUP in order to more clearly see each step that is taken).&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Load alignment:&#039;&#039;&#039;&lt;br /&gt;
 execute gp120.nexus&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Set tree-building criterion to maximum likelihood&#039;&#039;&#039;&lt;br /&gt;
 set criterion=likelihood&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Set model parameters to winning estimates&#039;&#039;&#039;&lt;br /&gt;
: Above you located a set of lines in the jmodeltest2 output giving a PAUP command that sets the model parameters to the estimates that were found using the winning model. Copy and paste this lset command (without the BEGIN and END parts) into the window where PAUP is running.&lt;br /&gt;
&lt;br /&gt;
 PASTE LSET COMMAND FROM MODELTEST RUN HERE&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Find best tree using selected model&#039;&#039;&#039;&lt;br /&gt;
: Still in the PAUP-window, enter the following command&lt;br /&gt;
 hsearch swap=tbr start=nj&lt;br /&gt;
: This command causes PAUP to perform a heuristic search for the best maximum likelihood tree. Once an initial tree has been constructed, the heuristic search proceeds by rearrangements of the &amp;quot;tree bisection and reconnection&amp;quot; type (TBR). We are using the model selected by modeltest, AND the parameter estimates found by modeltest on that model. You could also have chosen to simply estimate all the model parameters as part of this step (i.e., at the same time as finding the best tree), but fixing them improves speed tremendously. Findind the best tree should take a few minutes at most.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Save best tree to file&#039;&#039;&#039;&lt;br /&gt;
 savetrees format=newick brlens=yes file=gp120tree.phy from=1 to=1&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Quit program&#039;&#039;&#039;&lt;br /&gt;
 quit&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Have a look at the tree:&#039;&#039;&#039;&lt;br /&gt;
: You have now produced an unrooted tree of the HIV sequences and saved it in the file gp120tree.phy. Note that in this exercise we will not be interested in the tree as such - our focus is instead on finding positive selection on a subset of codon positions and the tree is just something we need in order to be able to fit the different codon models to the data. If you want to see the tree, you can do so with the following command:&lt;br /&gt;
 figtree gp120tree.phy &amp;amp;&lt;br /&gt;
: There is no meaningful root placed in this tree, so you may want to choose the unrooted view (the third icon in the Layout section of the figtree window). Close the figtree window when you have had a look&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Question: &#039;&#039;&#039;What is the negative log likelihood of the tree you just found?&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
== Detection of positively selected sites in gp120 ==&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Question 5&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
: There is much more to phylogenetic analyses than merely reconstructing trees. One interesting result of probabilistic methods, is that the parameters of a model will have their values estimated as part of the optimization procedure. This means that once such a model has been fitted to the data, it is possible to investigate these estimated parameter values to learn features about the evolutionary history of the sequences under investigation. In the present example we will focus on investigating whether we can find positively selected sites in our data set, defined as sites where the dN/dS ratio is larger than 1. We do that by using a codon substitution model where the dN/dS ratio is one of its parameters.&lt;br /&gt;
&lt;br /&gt;
: A further strength of the probabilistic approach is that you get a probabilistic measure of how well any model fits the data. This means you can use a stringent approach to determine which model fits the data best. In this framework one uses likelihoods (probabilities of data given model) to determine which model fits the data best. As you saw above, it is for instance possible to compute AIC values and weights (model probabilities) from the likelihood values of fitted models, Since each model essentially corresponds to a hypothesis about the evolutionary history of the data, we can thus use a stringent statistical approach to decide which hypothesis best describes our data.&lt;br /&gt;
&lt;br /&gt;
: In outline, you will now use the following steps to investigate whether there is any evidence for positively selected codons in your data set:&lt;br /&gt;
&lt;br /&gt;
:* Fit model M1, which assumes there are two classes of codons in the sequence: some with dN/dS &amp;lt; 1, some with dN/dS=1.&lt;br /&gt;
:* Fit model M2, which assumes 3 distinct classes of codons: two with dN/dS ratios as for M1, and one extra class with dN/dS &amp;gt; 1.&lt;br /&gt;
:* Assess the strength of evidence for the two models using AIC-based model probabilities&lt;br /&gt;
:* If M2 is better: identify the positively selected codons&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Inspect the parameter file&#039;&#039;&#039;&lt;br /&gt;
 nedit codeml.ctl &amp;amp;&lt;br /&gt;
: The file &amp;quot;codeml.ctl&amp;quot; contains several settings that are relevant for running the program &#039;&#039;&#039;codeml&#039;&#039;&#039;. Find the following lines and ensure that the file contains these values:&lt;br /&gt;
 &#039;&#039;&#039;seqfile =  gp120align.fasta&#039;&#039;&#039;:  name of alignment file&lt;br /&gt;
 &#039;&#039;&#039;treefile =  gp120tree.phy&#039;&#039;&#039;: name of tree file&lt;br /&gt;
 &#039;&#039;&#039;seqtype = 1&#039;&#039;&#039;: tells the program that our data consists of coding DNA.&lt;br /&gt;
 &#039;&#039;&#039;NSsites = 1 2&#039;&#039;&#039; : tells the program to analyze models M1 and M2.&lt;br /&gt;
 &#039;&#039;&#039;cleandata = 0&#039;&#039;&#039;: tells the program to keep positions with gaps.&lt;br /&gt;
&lt;br /&gt;
: The settings entered by us will cause codeml to analyze two hypotheses about dN/dS ratios. M1 says there are two classes of codons with different dN/dS ratios in the sequence: one class with dN/dS &amp;lt; 1 (codons under purifying or negative selection), and one class with dN/dS=1 (no selection - neutrally evolving sites). M2 says there are 3 distinct dN/dS ratios for different sites in the sequence: one class with dN/dS &amp;lt; 1, one class with dN/dS=1 (these are the same type of classes as for M1), and one class with dN/dS &amp;gt; 1 (corresponding to sites under positive selection). The value of the dN/dS ratios (for those classes that have dN/dS &amp;lt; 1 or dN/dS &amp;gt; 1), the fraction of sites belonging to each class, and the position of sites belonging to each class, are unknown at first and will be determined during the analysis.&lt;br /&gt;
&lt;br /&gt;
: (Regarding &amp;quot;cleandata&amp;quot; setting: &amp;quot;cleandata = 1&amp;quot; would cause codeml to discard any alignment columns with gaps or ambiguity symbols. For alignments with many sequences this can lead to discarding the majority of sites and I would therefore recommend not using that setting. At the same time you do want to be wary of columns where most sequences have gaps, since inference of selection for these will be very uncertain. It might be better to discard such columns from an alignment before doing the codeml analysis).&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Start the analysis&#039;&#039;&#039;&lt;br /&gt;
 codeml&lt;br /&gt;
: This will start the codeml program using the settings in the file codeml.ctl. Depending on your computer, this will take some minutes to finish. (You may be able to see how the optimization procedure results in progressively better fits: the likelihood increases, meaning that negative log-likelihood decreases, as the fit improves).&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Inspect result file:&#039;&#039;&#039;&lt;br /&gt;
: Wait for the run to finish, and then look at the result file:&lt;br /&gt;
 nedit selection.results &amp;amp;&lt;br /&gt;
: This file contains a wealth of information concerning your analysis. You may want to turn off line-wrapping to more clearly see the structure of the file (there will be some long lines that will otherwise wrap around). The top part of the file gives an overview of your sequences, codon usage and nucleotide frequencies. You can ignore this information for now, and move on to the interesting part, namely the model likelihoods and parameter values:&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Find likelihood, and number of free parameters for model M1&#039;&#039;&#039;&lt;br /&gt;
 Search ==&amp;gt; Find... ==&amp;gt; enter &amp;quot;Model 1&amp;quot; and click Find&lt;br /&gt;
: You are now in the region of the result file where the model likelihoods and parameter estimates are noted. Now, locate a line that looks a bit like the one shown below:&lt;br /&gt;
 lnL(ntime: 72  np: 74):  -4242.470345     +0.000000&lt;br /&gt;
: Identify the number of &amp;quot;free parameters&amp;quot;, K, used in model M1: This is indicated by &amp;quot;np&amp;quot;, and is 74 in the example shown above (most of these parameters are branch lengths in the tree; specifically, the number of branch length parameters is indicated by &amp;quot;ntime&amp;quot;, and is 72 in this example). Also note the log-likelihood of the fitted model. This is the number right after the parenthesis, and is -4242.470345 in the example here.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Question: &#039;&#039;&#039;What are the values of K and lnL for model M1?&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Question 6&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Find dn/dS ratios and codon class proportions for model M1:&#039;&#039;&#039;&lt;br /&gt;
: Scroll down a few lines until you get to a small table similar to this:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
      dN/dS for site classes (K=2)&lt;br /&gt;
      p:   0.75111  0.24889&lt;br /&gt;
      w:   0.06583  1.00000&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
: This gives a summary of the dN/dS ratios that were found in the data set. The line starting &amp;quot;w:&amp;quot; lists the two dN/dS ratios that were found (in this case 0.06583 and 1.00000 - the last one was pre-specified by us as part of the model and was therefore not a free parameter). In this context &amp;quot;w&amp;quot; means &amp;quot;omega&amp;quot;, which is the symbol typically used to represent the dN/dS rate ratio. The line starting p: gives the proportion of codon sites belonging to each of the dN/dS ratio classes (in the example above approximately 75% belong to the first class , while 25% of all sites belong to the class having dN/dS=1.00000).&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Question: &#039;&#039;&#039;What are the dN/dS value (w) and proportion (p) of sites for both classes. Report the following values: p(class1), w(class1), p(class2), w(class2)&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Question 7&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Find likelihood, and K for model M2&#039;&#039;&#039;&lt;br /&gt;
: Scroll past the M1 output until you get to the results for model M2.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Question: &#039;&#039;&#039;What are the values of K and lnL for model M2?&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Question 8&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Find dn/dS ratios and codon class proportions for model M2:&#039;&#039;&#039;&lt;br /&gt;
: Now, scroll down a few lines until you get to a small table similar to the one you examined for M1 before. For this model there are 3 separate classes of codons.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Question: &#039;&#039;&#039;What are the dN/dS value (w) and proportion (p) of sites for all three classes? Report these values: p(class1), w(class1), p(class2), w(class2), p(class3), w(class3)&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Question 9&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Assess strength of evidence for models M1 and M2:&#039;&#039;&#039;&lt;br /&gt;
: M2 will always have a better (higher) log-likelihood than model M1 because M2 has more free parameters, and M1 is nested within M2. You should now use the recipe given above to compute AIC values and model probabilities for M1 and M2.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Question: &#039;&#039;&#039; Report: AIC, ΔAIC, weight (model probability) for M1 and M2&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Question 10: &#039;&#039;&#039; Is M2 better than M1?&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Question 11&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Examine list of positively selected sites&#039;&#039;&#039;&lt;br /&gt;
: If your M2 is clearly better than M1 (I firmly believe it should be if you did things according to instructions...), then you have evidence for the existence of positively selected sites in the gp120 gene. Now, scroll down to the end of the result file and locate a list similar to the one below. Note: This is the &amp;quot;Bayes Empirical Bayes&amp;quot; table, not the &amp;quot;Naive Empirical Bayes&amp;quot; table. &lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Bayes Empirical Bayes (BEB) analysis&lt;br /&gt;
Positively selected sites&lt;br /&gt;
&lt;br /&gt;
         Prob(w&amp;gt;1)     mean w&lt;br /&gt;
&lt;br /&gt;
    25 A 0.959*        3.133 +- 0.769&lt;br /&gt;
    27 P 0.906         2.990 +- 0.877&lt;br /&gt;
    56 K 0.987*        3.197 +- 0.687&lt;br /&gt;
    59 V 0.915         3.032 +- 0.873&lt;br /&gt;
    78 R 0.637         2.351 +- 1.129&lt;br /&gt;
    88 K 0.573         2.148 +- 1.077&lt;br /&gt;
    95 V 0.925         3.046 +- 0.843&lt;br /&gt;
    ...&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
: It is not important what the distinction is in this context, but very briefly NEB ignores the fact that there is uncertainty about  maximum likelihood estimates, especially for smaller data sets (for instance w for some codon is perhaps not exactly 3.046, but could be in a region around that value), while [https://pubmed.ncbi.nlm.nih.gov/15689528/ BEB accounts for that uncertainty].&lt;br /&gt;
: This gives you a list of which residues (if any) that were found to belong to the positively selected dN/dS-class. Also listed is the probability that the site really is in the codon class where dN/dS &amp;gt; 1, and a weighted average of the w at the site. Using only DNA sequences you have now identified likely epitopes on the gp120 protein.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Question: &#039;&#039;&#039;List all sites having more than 95% probability of belonging to the positively selected class&lt;/div&gt;</summary>
		<author><name>Gorm</name></author>
	</entry>
	<entry>
		<id>https://teaching.healthtech.dtu.dk/22115/index.php?title=Bayesian_Phylogeny&amp;diff=256</id>
		<title>Bayesian Phylogeny</title>
		<link rel="alternate" type="text/html" href="https://teaching.healthtech.dtu.dk/22115/index.php?title=Bayesian_Phylogeny&amp;diff=256"/>
		<updated>2026-03-19T14:59:53Z</updated>

		<summary type="html">&lt;p&gt;Gorm: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;This exercise is part of the course  [[22115_-_Computational_Molecular_Evolution|Computational Molecular Evolution (22115)]].&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Overview ==&lt;br /&gt;
&lt;br /&gt;
Today&#039;s exercise will focus on phylogenetic analysis using Bayesian methods.&lt;br /&gt;
&lt;br /&gt;
As was the case for likelihood methods, Bayesian analysis is founded on having a probabilistic model of how the observed data is produced. This means that, for a given set of parameter values, you can compute the probability or [https://www.statlect.com/glossary/probability-density-function probability density] of any possible observation. For a full dataset, you then obtain the likelihood by multiplying these values across all observations. You will recall from the lecture that in Bayesian statistics the goal is to obtain a full posterior probability distribution over all possible parameter values. The posterior distribution quantifies our degree of belief in any possible parameter value after seeing the data. It is obtained by updating the prior probability distribution using the likelihood of the observed data.&lt;br /&gt;
&lt;br /&gt;
The prior probability distribution expresses your beliefs about the parameters before seeing any data, while the likelihood expresses what the observed data are telling you about the parameters. Specifically, the likelihood of a parameter value is the probability of the observed data given that parameter value. We regard a parameter value as more plausible the more probable it makes the observed data. This is the same measure we have previously used to find the maximum likelihood estimate. If the prior probability distribution is flat (i.e., if all possible parameter values have the same prior probability), then the posterior distribution is proportional to the likelihood, and the parameter value with the maximum likelihood also has the maximum posterior probability. However, even in this case, using a Bayesian approach still lets you interpret the result as a probability distribution over parameter values. &lt;br /&gt;
&lt;br /&gt;
If the prior is not flat, then it may have a substantial impact on the posterior, although this effect will usually diminish as the amount of data increases. A prior should ideally be based on domain knowledge and results from previous experiments. For instance one can use the posterior from one analysis as the prior in a new, independent analysis. Often a prior is chosen to be weakly informative, meaning that it places reasonable bounds on the parameter values without constraining them too narrowly. For instance the transition/transversion rate ratio kappa is typically 1.5-10. Values such as 100, 1,000 or 1,000,000 would be extremely unlikely, so a weakly informative prior for this parameter could be chosen to place 95% of its probability mass in the 0.5-20 range, slightly wider than what we think of as plausible values. For instance one could use a lognormal distribution with suitable parameters.&lt;br /&gt;
&lt;br /&gt;
In Bayesian phylogeny the parameters are of the same kind as in maximum likelihood phylogeny. Typical parameters include tree topology, branch lengths, nucleotide frequencies, and substitution model parameters such as the transition/transversion rate ratio or the gamma shape parameter. The difference is that, whereas in maximum likelihood phylogeny we seek the best point estimates of the parameter values, in Bayesian phylogeny the goal is instead to infer a full probability distribution over the possible parameter values. The observed data are again usually taken to be the alignment, although strictly speaking it would be more reasonable to say that the sequences are what have been observed, and that the alignment should then be inferred jointly with the phylogeny.&lt;br /&gt;
&lt;br /&gt;
In this exercise we will explore how one can determine and use posterior probability distributions over trees, over clades, and over substitution parameters. We will also touch upon the difference between marginal and joint probability distributions.&lt;br /&gt;
&lt;br /&gt;
== Getting started ==&lt;br /&gt;
&lt;br /&gt;
: In the command below: Instead of /path/to/molevol enter the path to the directory where you have placed your course files (for instance cd /Users/bob/Documents/molevol, or cd /home/student/molevol).&lt;br /&gt;
 cd /path/to/molevol&lt;br /&gt;
 mkdir bayes&lt;br /&gt;
 cd bayes&lt;br /&gt;
 cp ../data/primatemitDNA.nexus ./primatemitDNA.nexus&lt;br /&gt;
 cp ../data/neanderthal.nexus ./neanderthal.nexus&lt;br /&gt;
 cp ../data/hcvsmall.nexus ./hcvsmall.nexus&lt;br /&gt;
&lt;br /&gt;
: You have analyzed (versions of) all these data files previously in this course. We will now use Bayesian phylogenetic analysis to complement what we learned in those analyses.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Load R libraries&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
: In RStudio: set the working directory to the bayes directory. Then issue these commands:&lt;br /&gt;
 library(magrittr)&lt;br /&gt;
 library(tidyverse)&lt;br /&gt;
 library(bayesplot)&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
== Posterior samples and posterior probability of trees ==&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Question 1&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
: In today&#039;s exercise we will be using the program &amp;quot;MrBayes&amp;quot; to perform Bayesian phylogenetic analysis. MrBayes is a program that, like PAUP*, can be controlled by giving commands at a command line prompt. In fact, there is a substantial overlap between the commands used to control MrBayes and the PAUP command language. This should be a help when you are trying to understand how to use the program.&lt;br /&gt;
&lt;br /&gt;
: Note that the command &amp;quot;help&amp;quot; will give you a list of all available commands. Issuing &amp;quot;help &#039;&#039;command&#039;&#039;&amp;quot; will give you a more detailed description of the specified command along with current option values. This is similar to how &amp;quot;help &#039;&#039;command&#039;&#039;&amp;quot; works in PAUP.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Start program&#039;&#039;&#039;&lt;br /&gt;
: In a terminal window, issue the command:&lt;br /&gt;
 mb&lt;br /&gt;
: This starts the program, giving you a prompt (&amp;quot;MrBayes&amp;gt; &amp;quot;) where you can enter commands.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Get a quick overview of available commands&#039;&#039;&#039;&lt;br /&gt;
 help&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Load your sequences&#039;&#039;&#039;&lt;br /&gt;
 execute primatemitDNA.nexus&lt;br /&gt;
: This file contains mitochondrial DNA sequences from 5 different primates. Note that MrBayes accepts input in nexus format, and that this is the same command that was used to load sequences in PAUP*. In general, you can use many of the PAUP commands in MrBayes also.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Inspect data set&#039;&#039;&#039;&lt;br /&gt;
 showmatrix&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Define outgroup&#039;&#039;&#039;&lt;br /&gt;
 outgroup Gibbon&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Specify your model of sequence evolution&#039;&#039;&#039;&lt;br /&gt;
 lset nst=2 rates=gamma&lt;br /&gt;
: This command is again very much like the corresponding one in PAUP. You are specifying that you want to use a model with two substitution types (nst=2), and this is automatically taken to mean that you want to distinguish between transitions and transversions. Furthermore, rates=gamma means that you want the model to use a gamma distribution to account for different rates at different sites in the sequence.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Start Markov chain Monte Carlo sampling&#039;&#039;&#039;&lt;br /&gt;
:Make sure to make the shell window as wide as possible and then issue the following commands to start the run:&lt;br /&gt;
 mcmc ngen=1000000 samplefreq=100 nchains=3 diagnfreq=5000&lt;br /&gt;
: What you are doing here is to use the method known as MCMCMC (&amp;quot;Metropolis-coupled Markov chain Monte Carlo&amp;quot;) to empirically determine the posterior probability distribution of trees, branch lengths and substitution parameters. Recall that in the Bayesian framework this is how we learn about parameter values: instead of finding the best point estimates, we typically want to quantify the probability of the entire range of possible values. An estimate of the time left is shown in the last column of output.&lt;br /&gt;
&lt;br /&gt;
: Let us examine the command in detail. First, ngen=1000000 samplefreq=100 lets the search run for 1,000,000 MCMC steps (&amp;quot;generations&amp;quot;) and saves parameter values once every 100 rounds (meaning that a total of 10,000 sets of parameter values will be saved to sample files). You sometimes need to run longer (or shorter) than 1,000,000, and would then typically tweak samplefreq so you get around 1,000 - 10,000 samples in all. The option nchains=3 means that the MCMCMC sampling uses 3 parallel chains (but see below): one &amp;quot;cold&amp;quot; from which sampling takes place, and two &amp;quot;heated&amp;quot; that move around in the parameter space more quickly to find additional peaks in the probability distribution.&lt;br /&gt;
&lt;br /&gt;
: The option diagnfreq=5000 has to do with testing whether the MrBayes run is successful. Briefly, MrBayes will start two entirely independent runs starting from different random trees. In the early phases of the run, the two runs will sample very different trees, but when they have reached convergence (when they produce a good sample from the posterior probability distribution), the two tree samples should be very similar. Every diagnfreq generations, the program will compute a measure of how similar the tree samples are, specifically the average standard deviation of split frequencies. A “split” is the same as a bipartition, i.e. a division of all leaves in the tree into two groups, obtained by cutting an internal branch. For each split, MrBayes compares how often that split occurs in the two independent runs; if the runs have converged, these frequencies should be very similar, giving a small standard deviation. The program then averages this quantity across splits. As a rule of thumb, you may want to run until this value is less than 0.05 (the smaller the better)&lt;br /&gt;
&lt;br /&gt;
: During the run you will see reports about the progress of the two independent runs, each consisting of three chains. Each line of output lists the generation number and the log likelihoods of the current tree/parameter combination for each of the two groups of three chains (a column of asterisks separate the results for the independent runs). The cold chains are the ones enclosed in brackets [...], while the heated chains are enclosed in parentheses (...). Occasionally the chains will swap so one of the heated chains now becomes cold (and sampling then takes place from this chain).&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Continue run until parallel runs converge on same solution&#039;&#039;&#039;&lt;br /&gt;
:At the end of the run, Mrbayes will print the average standard deviation of split frequencies (which is a measure of how similar the tree samples of the two independent runs are). We recommend that you continue with the analysis until the value gets below 0.01 (if the value is larger than 0.01 then you should answer &amp;quot;yes&amp;quot; when the program asks &amp;quot;Continue the analysis? (yes/no)&amp;quot;.)&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Question: &#039;&#039;&#039;MrBayes starts two independent runs from different random trees. Why is it useful to run two independent analyses instead of just one? How does the average standard deviation of split frequencies help you decide whether the two runs have converged to the same posterior distribution? At approximately how many generations does this happen in your run?&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Question 2&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Have a look at the resulting sample files&#039;&#039;&#039;&lt;br /&gt;
: Open a new Terminal window (don&#039;t quit mrbayes in the other terminal yet!) and cd to the bayes directory. Open one of the parameter sampling files in a text editor:&lt;br /&gt;
 nedit primatemitDNA.nexus.run1.p &amp;amp;&lt;br /&gt;
: This file contains one line for each sampled point (you may want to turn off line-wrapping in nedit under the preferences menu). Each row corresponds to a certain sample time (or generation). Each column contains the sampled values of one specific parameter. The first line contains headings telling what the different columns are: &lt;br /&gt;
:* Gen: generation; number of MCMC steps taken so far&lt;br /&gt;
:* lnL: log likelihood of the current parameter estimates&lt;br /&gt;
:* LnPr: log of the prior probability&lt;br /&gt;
:* TL: tree length (sum of all branch lengths)&lt;br /&gt;
:* kappa: transition/transversion rate ratio&lt;br /&gt;
:* pi(A), pi(C), pi(G), pi(T): frequency of A, C, G, T&lt;br /&gt;
:* alpha: shape parameter for the gamma distribution. &lt;br /&gt;
&lt;br /&gt;
: (Column headings may be shifted relative to their corresponding columns). Note how the values of most parameters change a lot during the initial &amp;quot;burnin&amp;quot; period, before they settle near their most probable values. &lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Question: &#039;&#039;&#039;You will notice that lnL is always negative, while LnPr can sometimes be positive. At first sight this may seem impossible, since probabilities cannot be larger than 1. How can this happen?&lt;br /&gt;
&lt;br /&gt;
As a hint, note that (1) priors for continuous parameters are probability densities, and (2) the default prior for each branch length in MrBayes is an exponential distribution with rate 10. Use the following R code to plot this prior on both an ordinary y-axis and a log-scaled y-axis, and then explain why positive values of LnPr are possible.&lt;br /&gt;
&lt;br /&gt;
 df_expdist = tibble(&lt;br /&gt;
   x = seq(0, 1, by = 0.001),&lt;br /&gt;
   density = dexp(x, rate = 10),&lt;br /&gt;
   logdensity = log(dexp(x, rate = 10))&lt;br /&gt;
 )&lt;br /&gt;
 &lt;br /&gt;
 ggplot(df_expdist, aes(x = x, y = density)) +&lt;br /&gt;
   geom_line(color=&amp;quot;blue&amp;quot;) +&lt;br /&gt;
   geom_hline(yintercept = 1, lty=2) +&lt;br /&gt;
   labs(&lt;br /&gt;
     x = &amp;quot;Branch length&amp;quot;,&lt;br /&gt;
     y = &amp;quot;Probability density&amp;quot;,&lt;br /&gt;
     title = &amp;quot;Exponential prior on branch lengths: Exp(rate = 10)&amp;quot;&lt;br /&gt;
   )&lt;br /&gt;
 &lt;br /&gt;
 ggplot(df_expdist, aes(x = x, y = logdensity)) +&lt;br /&gt;
   geom_line(color=&amp;quot;blue&amp;quot;) +&lt;br /&gt;
   geom_hline(yintercept = 0, lty=2) +&lt;br /&gt;
   labs(&lt;br /&gt;
     x = &amp;quot;Branch length&amp;quot;,&lt;br /&gt;
     y = &amp;quot;log of Probability density&amp;quot;,&lt;br /&gt;
     title = &amp;quot;Exponential prior on branch lengths: Exp(rate = 10)&amp;quot;&lt;br /&gt;
   )&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Question 3&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Examine MCMC trajectory for gamma shape parameter, alpha&#039;&#039;&#039;&lt;br /&gt;
: Recall that the idea in MCMCMC sampling is to move around in parameter space in such a way that points are visited according to their posterior probability (i.e., regions with high posterior probability are visited frequently). Now, in RStudio, plot the sampled values for the gamma shape parameter, alpha, for one of the run files:&lt;br /&gt;
 df_primates = read_tsv(&amp;quot;primatemitDNA.nexus.run1.p&amp;quot;, skip=1)&lt;br /&gt;
 mcmc_trace(df_primates, pars=&amp;quot;alpha&amp;quot;)&lt;br /&gt;
: mcmc_trace is one of several plotting commands available in the bayesplot package. This command plots the sampled values of the parameter alpha from the first of the two parallel runs against MCMC generation number. Thus, the x-axis shows the progress of the run through time, with the leftmost values being the earliest samples and the rightmost values the later ones. Note how the Markov chain starts at the arbitrary value 1.0, rapidly moves to values that fit the observed data better, and then moves around in parameter space, sampling different plausible values of alpha. You can experiment with plotting other columns as well.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Question: &#039;&#039;&#039;Describe briefly what happens to the sampled values of alpha during the run. Why is it reasonable to discard the earliest samples as burn-in?&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Question 4&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Investigate posterior probability distribution over trees&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
: Now, close the nedit window and have a look at the file containing sampled trees:&lt;br /&gt;
 nedit primatemitDNA.nexus.run1.t &amp;amp;&lt;br /&gt;
: Tree topology is also a parameter in our model, and exactly like for the other parameters we also get samples from tree-space. One tree is printed per line in the parenthetical Newick format you have seen before. There are 5 taxa in the present data set, so the number of possible unrooted binary tree topologies is only 15. Since we have taken more than 15 sample points, there must be several lines containing the same tree topology. Close the nedit window when you are done.&lt;br /&gt;
&lt;br /&gt;
: MrBayes provides the sumt command to summarize the sampled trees. Before using it, we need to decide on the burn-in: The burn-in is the initial set of samples that are typically discarded, because we want to ensure that the MCMC has moved away from the random starting values, and has found the peaks of the probability landscape. Since the convergence diagnostic used a relative burn-in of 25%, we will also discard the first 25% of tree samples when summarizing the posterior.&lt;br /&gt;
&lt;br /&gt;
: Return to the shell window where you have MrBayes running. In the command below relburnin=yes and burninfrac=0.25 tells MrBayes to discard 25% of the samples as burnin (you could also have explicitly given the number of samples to discard - help sumt will give you details about the command and the current option settings).&lt;br /&gt;
 sumt contype=halfcompat conformat=simple relburnin=yes burninfrac=0.25 showtreeprobs=yes&lt;br /&gt;
: (Scroll back so you can see the top of the output when the command is done). This command gives you a summary of the trees that are in the file you examined manually above. The option contype=halfcompat requests that a majority rule consensus tree is calculated from the set of trees that are left after discarding the burnin. This consensus is the first tree plotted to the screen. Below the consensus cladogram, a consensus phylogram is plotted. The branch lengths in this have been averaged over the trees in which that branch was present (a particular branch corresponds to a bi-partition of the data, and will typically not be present in every sampled tree). The cladogram also has &amp;quot;clade credibility&amp;quot; values. We will return to the meaning of these later in today&#039;s exercise.&lt;br /&gt;
&lt;br /&gt;
: What most interests us right now is the list of trees that is printed after the phylogram. These trees are labeled &amp;quot;Tree 1&amp;quot;, &amp;quot;Tree 2&amp;quot;, etc, and are sorted according to their posterior probability which is indicated by a lower-case p after the tree number. (The upper-case P gives the cumulated probability of trees shown so far, and is useful for constructing a credible set). This list highlights how Bayesian phylogenetic analysis is different from maximum likelihood: Instead of finding the best tree(s), we here quantify our degree of belief in all possible trees.&lt;br /&gt;
&lt;br /&gt;
: The list of trees and probabilities was printed because of the option showtreeprobs=yes. Note that you probably do not want to issue that command if you have much more than 5 taxa! In that case you could instead inspect the file named primatemitDNA.nexus.trprobs which is now present in the same directory as your other files (this file is automatically produced by the sumt command).&lt;br /&gt;
&lt;br /&gt;
: &#039;&#039;&#039;NOTE&#039;&#039;&#039;: Annoyingly, there is a bug in the version of mrbayes we are using here, which means leaf names are not printed on the list of trees with probabilities. However, the most probable tree here in fact is identical to the consensus tree printed above it.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Question: &#039;&#039;&#039;What is the posterior probability of the most probable tree? Does the analysis strongly support a single tree, or is the posterior probability distributed across several different trees?&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
== Neanderthal data: posterior probability of clades ==&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Question 5&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
For many years, there was considerable debate about the origin of modern humans. One view, often called the [https://www.thoughtco.com/multiregional-hypothesis-167235 Multiregional Hypothesis], proposed that after Homo erectus spread from Africa into different parts of the world, regional populations gradually evolved into modern humans more or less in parallel. A different view, often called the Recent African Origin model, proposed that modern Homo sapiens evolved in Africa and later spread outward, largely replacing other archaic human groups such as the Neanderthals.&lt;br /&gt;
&lt;br /&gt;
Today it is clear that the history is more complicated than either simple extreme: modern humans arose in Africa, but there was also some interbreeding with Neanderthals and other archaic humans. However, in this exercise we will focus on a narrower question that can be addressed using a phylogeny of mitochondrial DNA: do the sampled Neanderthal and human mitochondrial sequences suggest that the Neanderthal sequence falls inside or outside modern human mitochondrial diversity?&lt;br /&gt;
&lt;br /&gt;
We will use the present data set to examine this question.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Load Neanderthal data set&#039;&#039;&#039;&lt;br /&gt;
: In the Terminal where you have MrBayes running:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
execute neanderthal.nexus&lt;br /&gt;
delete 5-40&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
: As we did for the maximum likelihood analysis, we will discard some of the human sequences in order to speed up the analysis. The command delete 5-40 removes sequence number 5 to sequence number 40 from the active data set.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Investigate data&#039;&#039;&#039;&lt;br /&gt;
 showmatrix&lt;br /&gt;
: This data set consists of an alignment of mitochondrial DNA from human (17 sequences), chimpanzee (1 sequence), and Neanderthal (1 sequence). The Neanderthal DNA was extracted from archaeological material, specifically bones found at Vindija in Croatia.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Start analysis&#039;&#039;&#039;&lt;br /&gt;
 outgroup Pan_troglodytes&lt;br /&gt;
 lset nst=mixed rates=gamma&lt;br /&gt;
 mcmc ngen=500000 nchains=3 diagnfreq=10000&lt;br /&gt;
&lt;br /&gt;
: Here we use the command `nst=mixed` which allows MrBayes to automatically explore all possible substitution models. Essentially, MrBayes now considers the substitution model as one more parameter, and uses MCMC to sample from the possible versions (with nst ranging from 1 to 6). This will often be the best choice when using MrBayes. (Below, I use nst=6 for pedagogical purposes, because it makes it simpler to analyse the output files).&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Find posterior probability of clades&#039;&#039;&#039;&lt;br /&gt;
: When the run has finished, issue this command to compute a consensus tree:&lt;br /&gt;
 sumt contype=halfcompat showtreeprobs=no relburnin=yes burninfrac=0.25&lt;br /&gt;
: Examine the consensus tree that is plotted to screen: On the branches that are resolved, you will notice that numbers have been plotted. These are clade-credibility values, and are in fact the posterior probability that the clade is real (based on the present data set).&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Question: &#039;&#039;&#039;What is the posterior probability that all sampled Homo sapiens sequences form a monophyletic group excluding the Neanderthal sequence? Does this support placing the Neanderthal outside modern human mitochondrial diversity?&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
== Posterior distributions over other parameters ==&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Question 6&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
: As the last thing, we will now turn away from the tree topology, and instead examine the other parameters that also form part of the probabilistic model. We will do this using a reduced version of the Hepatitis C virus data set that we have examined previously. Stay in the shell window where you just performed the analysis of Neanderthal sequences.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Load data set&#039;&#039;&#039;&lt;br /&gt;
 execute hcvsmall.nexus&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Define site partition&#039;&#039;&#039;&lt;br /&gt;
 charset 1stpos=1-.\3&lt;br /&gt;
 charset 2ndpos=2-.\3&lt;br /&gt;
 charset 3rdpos=3-.\3&lt;br /&gt;
 partition bycodon = 3:1stpos,2ndpos,3rdpos&lt;br /&gt;
 set partition=bycodon&lt;br /&gt;
 prset ratepr=variable&lt;br /&gt;
: This is an alternative way of specifying that different sites may evolve at different rates. With a gamma model, we allow rates to vary across sites but do not specify in advance which sites are fast or slow; instead, that pattern is inferred from the data. Here we instead use prior biological knowledge about the structure of the genetic code to divide sites into three classes: 1st, 2nd, and 3rd codon positions. We then allow each class to have its own rate, so that all 1st positions share one rate, all 2nd positions another, and all 3rd positions a third. Specifically, charset 1stpos=1-.\3 defines a character set named 1stpos consisting of site 1 followed by every third site (\3, i.e. sites 1, 4, 7, 10, …), continuing until the end of the alignment (denoted .).&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Specify model&#039;&#039;&#039;&lt;br /&gt;
 lset nst=6&lt;br /&gt;
: This specifies that we want to use a model of the General Time Reversible (GTR) type, where all 6 substitution types have separate rate parameters.&lt;br /&gt;
&lt;br /&gt;
: When the lset command was discussed previously, a few issues were glossed over. Importantly, and unlike PAUP, the lset command in MrBayes gives no information about whether nucleotide frequencies are equal or not, and whether they should be estimated from the data or not. In MrBayes this is instead controlled by defining the prior probability of the nucleotide frequencies (the command prset can be used to set priors). For instance, a model with equal nucleotide frequencies corresponds to having prior probability 1 (one) for the frequency vector (A=0.25, C=0.25, G=0.25, T=0.25), and zero prior probability for the infinitely many other possible frequency vectors. As you will see below, the default prior is not this limited, and the program will therefore estimate the frequencies from the data.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Inspect model details&#039;&#039;&#039;&lt;br /&gt;
 showmodel&lt;br /&gt;
: This command gives you a summary of the current model settings. You will also get a summary of how the prior probabilities of all model parameters are set. You will for instance notice that the nucleotide frequencies (parameter labeled &amp;quot;Statefreq&amp;quot;) have a &amp;quot;Dirichlet&amp;quot; prior. Without going into details, the Dirichlet distribution is a probability distribution over frequency vectors (i.e., vectors of positive values that sum to 1). Depending on the exact parameters the distribution can be more or less flat (flat here means that all sum-to-1 vectors are equally probable). The Dirichlet distribution is a handy way of specifying the prior probability distribution of nucleotide (or amino acid) frequency vectors. The default statefreq prior in MrBayes is the flat or un-informative prior dirichlet(1,1,1,1).&lt;br /&gt;
&lt;br /&gt;
: We will not go into the priors for the remaining parameters in any detail, but you may notice that by default all topologies are taken to be equally likely (a flat prior on trees).&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Start MCMC sampling&#039;&#039;&#039;&lt;br /&gt;
 mcmc ngen=1000000 samplefreq=100 diagnfreq=10000 nchains=3&lt;br /&gt;
: The run will take a while to finish (you may want to ensure that the average standard deviation of split frequencies is less than 0.01 before ending the analysis).&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Compute summary of parameter values&#039;&#039;&#039;&lt;br /&gt;
 sump relburnin=yes burninfrac=0.25&lt;br /&gt;
: The sump command (with a &amp;quot;p&amp;quot; at the end) works much like the sumt command (with a &amp;quot;t&amp;quot; at the end), but for other parameters than the tree-topology. Again, we are using 25% of the total number of samples as burnin.&lt;br /&gt;
&lt;br /&gt;
: First, you get a scatter plot of the lnL as a function of generation number. Values from the two independent runs are labeled &amp;quot;1&amp;quot; and &amp;quot;2&amp;quot; respectively. If the burnin is suitable, then the points should be randomly scattered over a narrow lnL interval.&lt;br /&gt;
&lt;br /&gt;
: Secondly, the posterior probability distribution of each parameter is summarized by giving the mean, variance, median, and 95% credible interval.&lt;br /&gt;
&lt;br /&gt;
: The last columns contain values indicating whether the run has converged. Specifically, ESS means Effective Sample Size, and measures how many effectively independent samples you have from the posterior — the higher the better, but this should be at least 100. [https://sites.stat.columbia.edu/gelman/research/published/brooksgelman2.pdf The column labeled PSRF+ gives another convergence diagnostic (also known as “R-hat”)] and should be close to 1 if the runs have converged. Specifically, it measures whether different chains (and different parts of the chains) are sampling the same distribution of values. As a rule of thumb, values less than 1.05 are good, values between 1.05 and 1.10 are acceptable, and values above 1.10 suggest poor convergence.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Question: &#039;&#039;&#039;What are the posterior mean values of the relative substitution rate parameters r(AC) and r(CG)?&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Question 7: &#039;&#039;&#039;Based on the reported posterior means, which of the two parameters, r(AC) or r(CG), appears to be larger on average?&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Question 8&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Marginal distributions&#039;&#039;&#039;&lt;br /&gt;
: Comparing posterior means gives a useful first summary of the two parameters, but it does not show how uncertain these estimates are. One of the strengths of Bayesian analysis is precisely that it gives us access not just to a single best estimate, but to a full posterior probability distribution over possible parameter values. We will now use this to get a fuller picture of the two substitution-rate parameters.&lt;br /&gt;
&lt;br /&gt;
: We start by looking at the posterior distribution of each parameter separately. Such a distribution for one parameter alone is called its marginal distribution.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Examine marginal distributions&#039;&#039;&#039;&lt;br /&gt;
: In RStudio, use the following commands to read and plot the marginal distributions of r(AC) and r(CG). Note that we are discarding the first 25% of the reads as burnin&lt;br /&gt;
 df_hcv = read_tsv(&amp;quot;hcvsmall.nexus.run1.p&amp;quot;, skip=1)&lt;br /&gt;
 burnin = df_hcv$Gen %&amp;gt;% &lt;br /&gt;
     max() %&amp;gt;% &lt;br /&gt;
     multiply_by(0.25) %&amp;gt;% &lt;br /&gt;
     floor()&lt;br /&gt;
 df_hcv2 = df_hcv %&amp;gt;% &lt;br /&gt;
     filter(Gen &amp;gt; burnin) %&amp;gt;%&lt;br /&gt;
     select(CG = `r(C&amp;lt;-&amp;gt;G){all}`,&lt;br /&gt;
            AC = `r(A&amp;lt;-&amp;gt;C){all}`&lt;br /&gt;
            )&lt;br /&gt;
 mcmc_intervals(df_hcv2, prob_outer = 1)&lt;br /&gt;
 mcmc_areas(df_hcv2, prob_outer = 1)&lt;br /&gt;
: The functions mcmc_intervals and mcmc_areas plot different views of the same posterior distributions. &lt;br /&gt;
&lt;br /&gt;
: You can also simply plot the data using ggplot:&lt;br /&gt;
 df_hcv2_long = pivot_longer(df_hcv2, cols = c(&amp;quot;CG&amp;quot;, &amp;quot;AC&amp;quot;))&lt;br /&gt;
 &lt;br /&gt;
 ggplot(df_hcv2_long) + &lt;br /&gt;
     geom_density(mapping=aes(x=value, fill=name), alpha=0.3) + &lt;br /&gt;
     labs(x=&amp;quot;Substitution rate&amp;quot;)&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Question&#039;&#039;&#039;: Based on the marginal distributions, r(AC) appears to be centered at a higher value than r(CG), but the two distributions overlap somewhat. Can you from these marginal distributions alone decide whether r(AC) is larger than r(CG) in most posterior samples? Why or why not?&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Question 9&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Marginal vs. joint distributions&#039;&#039;&#039;&lt;br /&gt;
: Looking at the marginal distributions gives us a fuller understanding of the uncertainty in each parameter separately. However, it still does not directly answer the question of whether r(AC) is larger than r(CG) in most posterior samples, because the two parameters may be associated with each other across samples. For instance, one parameter might be larger than the other in almost every individual sample, even though the two overall marginal distributions overlap. To answer such questions, we must examine the two parameters simultaneously. A probability distribution over several parameters at the same time is called a &amp;quot;joint distribution&amp;quot;, whereas a distribution for one parameter considered by itself is called a &amp;quot;marginal distribution&amp;quot;.&lt;br /&gt;
&lt;br /&gt;
: These plots and results explore the relationship between the A&amp;lt;-&amp;gt;C and C&amp;lt;-&amp;gt;G rates.&lt;br /&gt;
 ggplot(df_hcv2, aes(x=CG, y=AC)) + &lt;br /&gt;
     geom_point(col=&amp;quot;blue&amp;quot;) + &lt;br /&gt;
     geom_abline(intercept=0, slope=1, lty=2, col=&amp;quot;red&amp;quot;) +&lt;br /&gt;
     xlim(0,0.25) + &lt;br /&gt;
     ylim(0,0.25) + &lt;br /&gt;
     labs(x=&amp;quot;CG rate&amp;quot;, y =&amp;quot;AC rate&amp;quot;) &lt;br /&gt;
 &lt;br /&gt;
 ggplot(df_hcv2, aes(x=CG, y= AC)) + &lt;br /&gt;
     geom_hex(col=&amp;quot;blue&amp;quot;) + &lt;br /&gt;
     geom_abline(intercept=0, slope=1, lty=2, col=&amp;quot;red&amp;quot;) +&lt;br /&gt;
     xlim(0,0.25) + &lt;br /&gt;
     ylim(0, 0.25) + &lt;br /&gt;
     labs(x=&amp;quot;CG rate&amp;quot;, y =&amp;quot;AC rate&amp;quot;) &lt;br /&gt;
 &lt;br /&gt;
 df_hcv2 %&amp;gt;%&lt;br /&gt;
     nrow()&lt;br /&gt;
 df_hcv2 %&amp;gt;%&lt;br /&gt;
     filter(AC&amp;gt;CG) %&amp;gt;%&lt;br /&gt;
     nrow()&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Question: &#039;&#039;&#039;Based on the two different ways to plot the joint distribution and based on the unfiltered and filtered row counts, what is the posterior probability that r(AC) &amp;gt; r(CG)?&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
: Note how examining the joint distribution provides information that you could not obtain by simply comparing the marginal distributions. In particular, it lets you answer direct questions about how parameters relate to each other, for instance whether one is larger than another in most posterior samples. The same idea can be used to answer many other questions about posterior distributions.&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Question 10&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
: Now, plot the relative substitution rates at the first, second, and third codon positions:&lt;br /&gt;
 df_hcv3 = df_hcv %&amp;gt;% &lt;br /&gt;
     filter(Gen &amp;gt; 75000) %&amp;gt;%&lt;br /&gt;
     select(Codon_1st = `m{1}`,&lt;br /&gt;
            Codon_2nd = `m{2}`,&lt;br /&gt;
            Codon_3rd = `m{3}` ) %&amp;gt;%&lt;br /&gt;
     pivot_longer(cols=c(&amp;quot;Codon_1st&amp;quot;, &amp;quot;Codon_2nd&amp;quot;, &amp;quot;Codon_3rd&amp;quot;))&lt;br /&gt;
 &lt;br /&gt;
 ggplot(df_hcv3) + &lt;br /&gt;
     geom_density(mapping=aes(x=value, fill=name), alpha=0.3) + &lt;br /&gt;
     labs(x=&amp;quot;Relative substitution rate&amp;quot;)&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Question: &#039;&#039;&#039;Since random mutations presumably hit all three codon positions with the same frequency, any differences are expected to be caused by subsequent selection. Which of the following statement are correct ? (More than one answer may be correct)&lt;br /&gt;
&lt;br /&gt;
:* Codon position 2 is the most variable of the codon positions.&lt;br /&gt;
:* Codon position 1 is the most variable of the codon positions.&lt;br /&gt;
:* Codon position 1 is the most conserved codon position.&lt;br /&gt;
:* Codon position 3 is the most conserved codon position.&lt;br /&gt;
:* Codon position 3 is the most variable of the codon positions.&lt;br /&gt;
:* Codon position 2 is the most conserved codon position.&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Question 11&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Question: &#039;&#039;&#039;How does this result fit with your knowledge of the genetic code? Why are these codon positions the most conserved or the most variable?&lt;/div&gt;</summary>
		<author><name>Gorm</name></author>
	</entry>
	<entry>
		<id>https://teaching.healthtech.dtu.dk/22115/index.php?title=Bayesian_Phylogeny&amp;diff=255</id>
		<title>Bayesian Phylogeny</title>
		<link rel="alternate" type="text/html" href="https://teaching.healthtech.dtu.dk/22115/index.php?title=Bayesian_Phylogeny&amp;diff=255"/>
		<updated>2026-03-19T11:40:05Z</updated>

		<summary type="html">&lt;p&gt;Gorm: /* Probability distributions over other parameters */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;This exercise is part of the course  [[22115_-_Computational_Molecular_Evolution|Computational Molecular Evolution (22115)]].&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Overview ==&lt;br /&gt;
&lt;br /&gt;
Today&#039;s exercise will focus on phylogenetic analysis using Bayesian methods.&lt;br /&gt;
&lt;br /&gt;
As was the case for likelihood methods, Bayesian analysis is founded on having a probabilistic model of how the observed data is produced. This means that, for a given set of parameter values, you can compute the probability or [https://www.statlect.com/glossary/probability-density-function probability density] of any possible observation. For a full dataset, you then obtain the likelihood by multiplying these values across all observations. You will recall from the lecture that in Bayesian statistics the goal is to obtain a full posterior probability distribution over all possible parameter values. The posterior distribution quantifies our degree of belief in any possible parameter value after seeing the data. It is obtained by updating the prior probability distribution using the likelihood of the observed data.&lt;br /&gt;
&lt;br /&gt;
The prior probability distribution expresses your beliefs about the parameters before seeing any data, while the likelihood expresses what the observed data are telling you about the parameters. Specifically, the likelihood of a parameter value is the probability of the observed data given that parameter value. We regard a parameter value as more plausible the more probable it makes the observed data. This is the same measure we have previously used to find the maximum likelihood estimate. If the prior probability distribution is flat (i.e., if all possible parameter values have the same prior probability), then the posterior distribution is proportional to the likelihood, and the parameter value with the maximum likelihood also has the maximum posterior probability. However, even in this case, using a Bayesian approach still lets you interpret the result as a probability distribution over parameter values. &lt;br /&gt;
&lt;br /&gt;
If the prior is not flat, then it may have a substantial impact on the posterior, although this effect will usually diminish as the amount of data increases. A prior should ideally be based on domain knowledge and results from previous experiments. For instance one can use the posterior from one analysis as the prior in a new, independent analysis. Often a prior is chosen to be weakly informative, meaning that it places reasonable bounds on the parameter values without constraining them too narrowly. For instance the transition/transversion rate ratio kappa is typically 1.5-10. Values such as 100, 1,000 or 1,000,000 would be extremely unlikely, so a weakly informative prior for this parameter could be chosen to place 95% of its probability mass in the 0.5-20 range, slightly wider than what we think of as plausible values. For instance one could use a lognormal distribution with suitable parameters.&lt;br /&gt;
&lt;br /&gt;
In Bayesian phylogeny the parameters are of the same kind as in maximum likelihood phylogeny. Typical parameters include tree topology, branch lengths, nucleotide frequencies, and substitution model parameters such as the transition/transversion rate ratio or the gamma shape parameter. The difference is that, whereas in maximum likelihood phylogeny we seek the best point estimates of the parameter values, in Bayesian phylogeny the goal is instead to infer a full probability distribution over the possible parameter values. The observed data are again usually taken to be the alignment, although strictly speaking it would be more reasonable to say that the sequences are what have been observed, and that the alignment should then be inferred jointly with the phylogeny.&lt;br /&gt;
&lt;br /&gt;
In this exercise we will explore how one can determine and use posterior probability distributions over trees, over clades, and over substitution parameters. We will also touch upon the difference between marginal and joint probability distributions.&lt;br /&gt;
&lt;br /&gt;
== Getting started ==&lt;br /&gt;
&lt;br /&gt;
: In the command below: Instead of /path/to/molevol enter the path to the directory where you have placed your course files (for instance cd /Users/bob/Documents/molevol, or cd /home/student/molevol).&lt;br /&gt;
 cd /path/to/molevol&lt;br /&gt;
 mkdir bayes&lt;br /&gt;
 cd bayes&lt;br /&gt;
 cp ../data/primatemitDNA.nexus ./primatemitDNA.nexus&lt;br /&gt;
 cp ../data/neanderthal.nexus ./neanderthal.nexus&lt;br /&gt;
 cp ../data/hcvsmall.nexus ./hcvsmall.nexus&lt;br /&gt;
&lt;br /&gt;
: You have analyzed (versions of) all these data files previously in this course. We will now use Bayesian phylogenetic analysis to complement what we learned in those analyses.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Load R libraries&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
: In RStudio: set the working directory to the bayes directory. Then issue these commands:&lt;br /&gt;
 library(magrittr)&lt;br /&gt;
 library(tidyverse)&lt;br /&gt;
 library(bayesplot)&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
== Posterior probability of trees ==&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Question 1&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
: In today&#039;s exercise we will be using the program &amp;quot;MrBayes&amp;quot; to perform Bayesian phylogenetic analysis. MrBayes is a program that, like PAUP*, can be controlled by giving commands at a command line prompt. In fact, there is a substantial overlap between the commands used to control MrBayes and the PAUP command language. This should be a help when you are trying to understand how to use the program.&lt;br /&gt;
&lt;br /&gt;
: Note that the command &amp;quot;help&amp;quot; will give you a list of all available commands. Issuing &amp;quot;help &#039;&#039;command&#039;&#039;&amp;quot; will give you a more detailed description of the specified command along with current option values. This is similar to how &amp;quot;help &#039;&#039;command&#039;&#039;&amp;quot; works in PAUP.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Start program&#039;&#039;&#039;&lt;br /&gt;
: In a terminal window, issue the command:&lt;br /&gt;
 mb&lt;br /&gt;
: This starts the program, giving you a prompt (&amp;quot;MrBayes&amp;gt; &amp;quot;) where you can enter commands.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Get a quick overview of available commands&#039;&#039;&#039;&lt;br /&gt;
 help&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Load your sequences&#039;&#039;&#039;&lt;br /&gt;
 execute primatemitDNA.nexus&lt;br /&gt;
: This file contains mitochondrial DNA sequences from 5 different primates. Note that MrBayes accepts input in nexus format, and that this is the same command that was used to load sequences in PAUP*. In general, you can use many of the PAUP commands in MrBayes also.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Inspect data set&#039;&#039;&#039;&lt;br /&gt;
 showmatrix&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Define outgroup&#039;&#039;&#039;&lt;br /&gt;
 outgroup Gibbon&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Specify your model of sequence evolution&#039;&#039;&#039;&lt;br /&gt;
 lset nst=2 rates=gamma&lt;br /&gt;
: This command is again very much like the corresponding one in PAUP. You are specifying that you want to use a model with two substitution types (nst=2), and this is automatically taken to mean that you want to distinguish between transitions and transversions. Furthermore, rates=gamma means that you want the model to use a gamma distribution to account for different rates at different sites in the sequence.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Start Markov chain Monte Carlo sampling&#039;&#039;&#039;&lt;br /&gt;
:Make sure to make the shell window as wide as possible and then issue the following commands to start the run:&lt;br /&gt;
 mcmc ngen=1000000 samplefreq=100 nchains=3 diagnfreq=5000&lt;br /&gt;
: What you are doing here is to use the method known as MCMCMC (&amp;quot;Metropolis-coupled Markov chain Monte Carlo&amp;quot;) to empirically determine the posterior probability distribution of trees, branch lengths and substitution parameters. Recall that in the Bayesian framework this is how we learn about parameter values: instead of finding the best point estimates, we typically want to quantify the probability of the entire range of possible values. An estimate of the time left is shown in the last column of output.&lt;br /&gt;
&lt;br /&gt;
: Let us examine the command in detail. First, ngen=1000000 samplefreq=100 lets the search run for 1,000,000 MCMC steps (&amp;quot;generations&amp;quot;) and saves parameter values once every 100 rounds (meaning that a total of 10,000 sets of parameter values will be saved to sample files). You sometimes need to run longer (or shorter) than 1,000,000, and would then typically tweak samplefreq so you get around 1,000 - 10,000 samples in all. The option nchains=3 means that the MCMCMC sampling uses 3 parallel chains (but see below): one &amp;quot;cold&amp;quot; from which sampling takes place, and two &amp;quot;heated&amp;quot; that move around in the parameter space more quickly to find additional peaks in the probability distribution.&lt;br /&gt;
&lt;br /&gt;
: The option diagnfreq=5000 has to do with testing whether the MrBayes run is successful. Briefly, MrBayes will start two entirely independent runs starting from different random trees. In the early phases of the run, the two runs will sample very different trees, but when they have reached convergence (when they produce a good sample from the posterior probability distribution), the two tree samples should be very similar. Every diagnfreq generations, the program will compute a measure of how similar the tree samples are, specifically the average standard deviation of split frequencies. A “split” is the same as a bipartition, i.e. a division of all leaves in the tree into two groups, obtained by cutting an internal branch. For each split, MrBayes compares how often that split occurs in the two independent runs; if the runs have converged, these frequencies should be very similar, giving a small standard deviation. The program then averages this quantity across splits. As a rule of thumb, you may want to run until this value is less than 0.05 (the smaller the better)&lt;br /&gt;
&lt;br /&gt;
: During the run you will see reports about the progress of the two independent runs, each consisting of three chains. Each line of output lists the generation number and the log likelihoods of the current tree/parameter combination for each of the two groups of three chains (a column of asterisks separate the results for the independent runs). The cold chains are the ones enclosed in brackets [...], while the heated chains are enclosed in parentheses (...). Occasionally the chains will swap so one of the heated chains now becomes cold (and sampling then takes place from this chain).&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Continue run until parallel runs converge on same solution&#039;&#039;&#039;&lt;br /&gt;
:At the end of the run, Mrbayes will print the average standard deviation of split frequencies (which is a measure of how similar the tree samples of the two independent runs are). We recommend that you continue with the analysis until the value gets below 0.01 (if the value is larger than 0.01 then you should answer &amp;quot;yes&amp;quot; when the program asks &amp;quot;Continue the analysis? (yes/no)&amp;quot;.)&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Question: &#039;&#039;&#039;MrBayes starts two independent runs from different random trees. Why is it useful to run two independent analyses instead of just one? How does the average standard deviation of split frequencies help you decide whether the two runs have converged to the same posterior distribution? At approximately how many generations does this happen in your run?&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Question 2&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Have a look at the resulting sample files&#039;&#039;&#039;&lt;br /&gt;
: Open a new Terminal window (don&#039;t quit mrbayes in the other terminal yet!) and cd to the bayes directory. Open one of the parameter sampling files in a text editor:&lt;br /&gt;
 nedit primatemitDNA.nexus.run1.p &amp;amp;&lt;br /&gt;
: This file contains one line for each sampled point (you may want to turn off line-wrapping in nedit under the preferences menu). Each row corresponds to a certain sample time (or generation). Each column contains the sampled values of one specific parameter. The first line contains headings telling what the different columns are: &lt;br /&gt;
:* Gen: generation; number of MCMC steps taken so far&lt;br /&gt;
:* lnL: log likelihood of the current parameter estimates&lt;br /&gt;
:* LnPr: log of the prior probability&lt;br /&gt;
:* TL: tree length (sum of all branch lengths)&lt;br /&gt;
:* kappa: transition/transversion rate ratio&lt;br /&gt;
:* pi(A), pi(C), pi(G), pi(T): frequency of A, C, G, T&lt;br /&gt;
:* alpha: shape parameter for the gamma distribution. &lt;br /&gt;
&lt;br /&gt;
: (Column headings may be shifted relative to their corresponding columns). Note how the values of most parameters change a lot during the initial &amp;quot;burnin&amp;quot; period, before they settle near their most probable values. &lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Question: &#039;&#039;&#039;You will notice that lnL is always negative, while LnPr can sometimes be positive. At first sight this may seem impossible, since probabilities cannot be larger than 1. How can this happen?&lt;br /&gt;
&lt;br /&gt;
As a hint, note that (1) priors for continuous parameters are probability densities, and (2) the default prior for each branch length in MrBayes is an exponential distribution with rate 10. Use the following R code to plot this prior on both an ordinary y-axis and a log-scaled y-axis, and then explain why positive values of LnPr are possible.&lt;br /&gt;
&lt;br /&gt;
 df_expdist = tibble(&lt;br /&gt;
   x = seq(0, 1, by = 0.001),&lt;br /&gt;
   density = dexp(x, rate = 10),&lt;br /&gt;
   logdensity = log(dexp(x, rate = 10))&lt;br /&gt;
 )&lt;br /&gt;
 &lt;br /&gt;
 ggplot(df_expdist, aes(x = x, y = density)) +&lt;br /&gt;
   geom_line(color=&amp;quot;blue&amp;quot;) +&lt;br /&gt;
   geom_hline(yintercept = 1, lty=2) +&lt;br /&gt;
   labs(&lt;br /&gt;
     x = &amp;quot;Branch length&amp;quot;,&lt;br /&gt;
     y = &amp;quot;Probability density&amp;quot;,&lt;br /&gt;
     title = &amp;quot;Exponential prior on branch lengths: Exp(rate = 10)&amp;quot;&lt;br /&gt;
   )&lt;br /&gt;
 &lt;br /&gt;
 ggplot(df_expdist, aes(x = x, y = logdensity)) +&lt;br /&gt;
   geom_line(color=&amp;quot;blue&amp;quot;) +&lt;br /&gt;
   geom_hline(yintercept = 0, lty=2) +&lt;br /&gt;
   labs(&lt;br /&gt;
     x = &amp;quot;Branch length&amp;quot;,&lt;br /&gt;
     y = &amp;quot;log of Probability density&amp;quot;,&lt;br /&gt;
     title = &amp;quot;Exponential prior on branch lengths: Exp(rate = 10)&amp;quot;&lt;br /&gt;
   )&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Question 3&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Examine MCMC trajectory for gamma shape parameter, alpha&#039;&#039;&#039;&lt;br /&gt;
: Recall that the idea in MCMCMC sampling is to move around in parameter space in such a way that points are visited according to their posterior probability (i.e., regions with high posterior probability are visited frequently). Now, in RStudio, plot the sampled values for the gamma shape parameter, alpha, for one of the run files:&lt;br /&gt;
 df_primates = read_tsv(&amp;quot;primatemitDNA.nexus.run1.p&amp;quot;, skip=1)&lt;br /&gt;
 mcmc_trace(df_primates, pars=&amp;quot;alpha&amp;quot;)&lt;br /&gt;
: mcmc_trace is one of several plotting commands available in the bayesplot package. This command plots the sampled values of the parameter alpha from the first of the two parallel runs against MCMC generation number. Thus, the x-axis shows the progress of the run through time, with the leftmost values being the earliest samples and the rightmost values the later ones. Note how the Markov chain starts at the arbitrary value 1.0, rapidly moves to values that fit the observed data better, and then moves around in parameter space, sampling different plausible values of alpha. You can experiment with plotting other columns as well.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Question: &#039;&#039;&#039;Describe briefly what happens to the sampled values of alpha during the run. Why is it reasonable to discard the earliest samples as burn-in?&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Question 4&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Investigate posterior probability distribution over trees&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
: Now, close the nedit window and have a look at the file containing sampled trees:&lt;br /&gt;
 nedit primatemitDNA.nexus.run1.t &amp;amp;&lt;br /&gt;
: Tree topology is also a parameter in our model, and exactly like for the other parameters we also get samples from tree-space. One tree is printed per line in the parenthetical Newick format you have seen before. There are 5 taxa in the present data set, so the number of possible unrooted binary tree topologies is only 15. Since we have taken more than 15 sample points, there must be several lines containing the same tree topology. Close the nedit window when you are done.&lt;br /&gt;
&lt;br /&gt;
: MrBayes provides the sumt command to summarize the sampled trees. Before using it, we need to decide on the burn-in: The burn-in is the initial set of samples that are typically discarded, because we want to ensure that the MCMC has moved away from the random starting values, and has found the peaks of the probability landscape. Since the convergence diagnostic used a relative burn-in of 25%, we will also discard the first 25% of tree samples when summarizing the posterior.&lt;br /&gt;
&lt;br /&gt;
: Return to the shell window where you have MrBayes running. In the command below relburnin=yes and burninfrac=0.25 tells MrBayes to discard 25% of the samples as burnin (you could also have explicitly given the number of samples to discard - help sumt will give you details about the command and the current option settings).&lt;br /&gt;
 sumt contype=halfcompat conformat=simple relburnin=yes burninfrac=0.25 showtreeprobs=yes&lt;br /&gt;
: (Scroll back so you can see the top of the output when the command is done). This command gives you a summary of the trees that are in the file you examined manually above. The option contype=halfcompat requests that a majority rule consensus tree is calculated from the set of trees that are left after discarding the burnin. This consensus is the first tree plotted to the screen. Below the consensus cladogram, a consensus phylogram is plotted. The branch lengths in this have been averaged over the trees in which that branch was present (a particular branch corresponds to a bi-partition of the data, and will typically not be present in every sampled tree). The cladogram also has &amp;quot;clade credibility&amp;quot; values. We will return to the meaning of these later in today&#039;s exercise.&lt;br /&gt;
&lt;br /&gt;
: What most interests us right now is the list of trees that is printed after the phylogram. These trees are labeled &amp;quot;Tree 1&amp;quot;, &amp;quot;Tree 2&amp;quot;, etc, and are sorted according to their posterior probability which is indicated by a lower-case p after the tree number. (The upper-case P gives the cumulated probability of trees shown so far, and is useful for constructing a credible set). This list highlights how Bayesian phylogenetic analysis is different from maximum likelihood: Instead of finding the best tree(s), we here quantify our degree of belief in all possible trees.&lt;br /&gt;
&lt;br /&gt;
: The list of trees and probabilities was printed because of the option showtreeprobs=yes. Note that you probably do not want to issue that command if you have much more than 5 taxa! In that case you could instead inspect the file named primatemitDNA.nexus.trprobs which is now present in the same directory as your other files (this file is automatically produced by the sumt command).&lt;br /&gt;
&lt;br /&gt;
: &#039;&#039;&#039;NOTE&#039;&#039;&#039;: Annoyingly, there is a bug in the version of mrbayes we are using here, which means leaf names are not printed on the list of trees with probabilities. However, the most probable tree here in fact is identical to the consensus tree printed above it.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Question: &#039;&#039;&#039;What is the posterior probability of the most probable tree? Does the analysis strongly support a single tree, or is the posterior probability distributed across several different trees?&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
== Analysis of Neanderthal data (posterior probability of clades) ==&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Question 5&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
For many years, there was considerable debate about the origin of modern humans. One view, often called the [https://www.thoughtco.com/multiregional-hypothesis-167235 Multiregional Hypothesis], proposed that after Homo erectus spread from Africa into different parts of the world, regional populations gradually evolved into modern humans more or less in parallel. A different view, often called the Recent African Origin model, proposed that modern Homo sapiens evolved in Africa and later spread outward, largely replacing other archaic human groups such as the Neanderthals.&lt;br /&gt;
&lt;br /&gt;
Today it is clear that the history is more complicated than either simple extreme: modern humans arose in Africa, but there was also some interbreeding with Neanderthals and other archaic humans. However, in this exercise we will focus on a narrower question that can be addressed using a phylogeny of mitochondrial DNA: do the sampled Neanderthal and human mitochondrial sequences suggest that the Neanderthal sequence falls inside or outside modern human mitochondrial diversity?&lt;br /&gt;
&lt;br /&gt;
We will use the present data set to examine this question.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Load Neanderthal data set&#039;&#039;&#039;&lt;br /&gt;
: In the Terminal where you have MrBayes running:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
execute neanderthal.nexus&lt;br /&gt;
delete 5-40&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
: As we did for the maximum likelihood analysis, we will discard some of the human sequences in order to speed up the analysis. The command delete 5-40 removes sequence number 5 to sequence number 40 from the active data set.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Investigate data&#039;&#039;&#039;&lt;br /&gt;
 showmatrix&lt;br /&gt;
: This data set consists of an alignment of mitochondrial DNA from human (17 sequences), chimpanzee (1 sequence), and Neanderthal (1 sequence). The Neanderthal DNA was extracted from archaeological material, specifically bones found at Vindija in Croatia.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Start analysis&#039;&#039;&#039;&lt;br /&gt;
 outgroup Pan_troglodytes&lt;br /&gt;
 lset nst=mixed rates=gamma&lt;br /&gt;
 mcmc ngen=500000 nchains=3 diagnfreq=10000&lt;br /&gt;
&lt;br /&gt;
: Here we use the command `nst=mixed` which allows MrBayes to automatically explore all possible substitution models. Essentially, MrBayes now considers the substitution model as one more parameter, and uses MCMC to sample from the possible versions (with nst ranging from 1 to 6). This will often be the best choice when using MrBayes. (Below, I use nst=6 for pedagogical purposes, because it makes it simpler to analyse the output files).&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Find posterior probability of clades&#039;&#039;&#039;&lt;br /&gt;
: When the run has finished, issue this command to compute a consensus tree:&lt;br /&gt;
 sumt contype=halfcompat showtreeprobs=no relburnin=yes burninfrac=0.25&lt;br /&gt;
: Examine the consensus tree that is plotted to screen: On the branches that are resolved, you will notice that numbers have been plotted. These are clade-credibility values, and are in fact the posterior probability that the clade is real (based on the present data set).&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Question: &#039;&#039;&#039;What is the posterior probability that all sampled Homo sapiens sequences form a monophyletic group excluding the Neanderthal sequence? Does this support placing the Neanderthal outside modern human mitochondrial diversity?&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
== Probability distributions over other parameters ==&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Question 6&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
: As the last thing, we will now turn away from the tree topology, and instead examine the other parameters that also form part of the probabilistic model. We will do this using a reduced version of the Hepatitis C virus data set that we have examined previously. Stay in the shell window where you just performed the analysis of Neanderthal sequences.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Load data set&#039;&#039;&#039;&lt;br /&gt;
 execute hcvsmall.nexus&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Define site partition&#039;&#039;&#039;&lt;br /&gt;
 charset 1stpos=1-.\3&lt;br /&gt;
 charset 2ndpos=2-.\3&lt;br /&gt;
 charset 3rdpos=3-.\3&lt;br /&gt;
 partition bycodon = 3:1stpos,2ndpos,3rdpos&lt;br /&gt;
 set partition=bycodon&lt;br /&gt;
 prset ratepr=variable&lt;br /&gt;
: This is an alternative way of specifying that different sites may evolve at different rates. With a gamma model, we allow rates to vary across sites but do not specify in advance which sites are fast or slow; instead, that pattern is inferred from the data. Here we instead use prior biological knowledge about the structure of the genetic code to divide sites into three classes: 1st, 2nd, and 3rd codon positions. We then allow each class to have its own rate, so that all 1st positions share one rate, all 2nd positions another, and all 3rd positions a third. Specifically, charset 1stpos=1-.\3 defines a character set named 1stpos consisting of site 1 followed by every third site (\3, i.e. sites 1, 4, 7, 10, …), continuing until the end of the alignment (denoted .).&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Specify model&#039;&#039;&#039;&lt;br /&gt;
 lset nst=6&lt;br /&gt;
: This specifies that we want to use a model of the General Time Reversible (GTR) type, where all 6 substitution types have separate rate parameters.&lt;br /&gt;
&lt;br /&gt;
: When the lset command was discussed previously, a few issues were glossed over. Importantly, and unlike PAUP, the lset command in MrBayes gives no information about whether nucleotide frequencies are equal or not, and whether they should be estimated from the data or not. In MrBayes this is instead controlled by defining the prior probability of the nucleotide frequencies (the command prset can be used to set priors). For instance, a model with equal nucleotide frequencies corresponds to having prior probability 1 (one) for the frequency vector (A=0.25, C=0.25, G=0.25, T=0.25), and zero prior probability for the infinitely many other possible frequency vectors. As you will see below, the default prior is not this limited, and the program will therefore estimate the frequencies from the data.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Inspect model details&#039;&#039;&#039;&lt;br /&gt;
 showmodel&lt;br /&gt;
: This command gives you a summary of the current model settings. You will also get a summary of how the prior probabilities of all model parameters are set. You will for instance notice that the nucleotide frequencies (parameter labeled &amp;quot;Statefreq&amp;quot;) have a &amp;quot;Dirichlet&amp;quot; prior. Without going into details, the Dirichlet distribution is a probability distribution over frequency vectors (i.e., vectors of positive values that sum to 1). Depending on the exact parameters the distribution can be more or less flat (flat here means that all sum-to-1 vectors are equally probable). The Dirichlet distribution is a handy way of specifying the prior probability distribution of nucleotide (or amino acid) frequency vectors. The default statefreq prior in MrBayes is the flat or un-informative prior dirichlet(1,1,1,1).&lt;br /&gt;
&lt;br /&gt;
: We will not go into the priors for the remaining parameters in any detail, but you may notice that by default all topologies are taken to be equally likely (a flat prior on trees).&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Start MCMC sampling&#039;&#039;&#039;&lt;br /&gt;
 mcmc ngen=1000000 samplefreq=100 diagnfreq=10000 nchains=3&lt;br /&gt;
: The run will take a while to finish (you may want to ensure that the average standard deviation of split frequencies is less than 0.01 before ending the analysis).&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Compute summary of parameter values&#039;&#039;&#039;&lt;br /&gt;
 sump relburnin=yes burninfrac=0.25&lt;br /&gt;
: The sump command (with a &amp;quot;p&amp;quot; at the end) works much like the sumt command (with a &amp;quot;t&amp;quot; at the end), but for other parameters than the tree-topology. Again, we are using 25% of the total number of samples as burnin.&lt;br /&gt;
&lt;br /&gt;
: First, you get a scatter plot of the lnL as a function of generation number. Values from the two independent runs are labeled &amp;quot;1&amp;quot; and &amp;quot;2&amp;quot; respectively. If the burnin is suitable, then the points should be randomly scattered over a narrow lnL interval.&lt;br /&gt;
&lt;br /&gt;
: Secondly, the posterior probability distribution of each parameter is summarized by giving the mean, variance, median, and 95% credible interval.&lt;br /&gt;
&lt;br /&gt;
: The last columns contain values indicating whether the run has converged. Specifically, ESS means Effective Sample Size, and measures how many effectively independent samples you have from the posterior — the higher the better, but this should be at least 100. [https://sites.stat.columbia.edu/gelman/research/published/brooksgelman2.pdf The column labeled PSRF+ gives another convergence diagnostic (also known as “R-hat”)] and should be close to 1 if the runs have converged. Specifically, it measures whether different chains (and different parts of the chains) are sampling the same distribution of values. As a rule of thumb, values less than 1.05 are good, values between 1.05 and 1.10 are acceptable, and values above 1.10 suggest poor convergence.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Question: &#039;&#039;&#039;What are the posterior mean values of the relative substitution rate parameters r(AC) and r(CG)?&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Question 7: &#039;&#039;&#039;Based on the reported posterior means, which of the two parameters, r(AC) or r(CG), appears to be larger on average?&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Question 8&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Marginal distributions&#039;&#039;&#039;&lt;br /&gt;
: Comparing posterior means gives a useful first summary of the two parameters, but it does not show how uncertain these estimates are. One of the strengths of Bayesian analysis is precisely that it gives us access not just to a single best estimate, but to a full posterior probability distribution over possible parameter values. We will now use this to get a fuller picture of the two substitution-rate parameters.&lt;br /&gt;
&lt;br /&gt;
: We start by looking at the posterior distribution of each parameter separately. Such a distribution for one parameter alone is called its marginal distribution.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Examine marginal distributions&#039;&#039;&#039;&lt;br /&gt;
: In RStudio, use the following commands to read and plot the marginal distributions of r(AC) and r(CG). Note that we are discarding the first 25% of the reads as burnin&lt;br /&gt;
 df_hcv = read_tsv(&amp;quot;hcvsmall.nexus.run1.p&amp;quot;, skip=1)&lt;br /&gt;
 burnin = df_hcv$Gen %&amp;gt;% &lt;br /&gt;
     max() %&amp;gt;% &lt;br /&gt;
     multiply_by(0.25) %&amp;gt;% &lt;br /&gt;
     floor()&lt;br /&gt;
 df_hcv2 = df_hcv %&amp;gt;% &lt;br /&gt;
     filter(Gen &amp;gt; burnin) %&amp;gt;%&lt;br /&gt;
     select(CG = `r(C&amp;lt;-&amp;gt;G){all}`,&lt;br /&gt;
            AC = `r(A&amp;lt;-&amp;gt;C){all}`&lt;br /&gt;
            )&lt;br /&gt;
 mcmc_intervals(df_hcv2, prob_outer = 1)&lt;br /&gt;
 mcmc_areas(df_hcv2, prob_outer = 1)&lt;br /&gt;
: The functions mcmc_intervals and mcmc_areas plot different views of the same posterior distributions. &lt;br /&gt;
&lt;br /&gt;
: You can also simply plot the data using ggplot:&lt;br /&gt;
 df_hcv2_long = pivot_longer(df_hcv2, cols = c(&amp;quot;CG&amp;quot;, &amp;quot;AC&amp;quot;))&lt;br /&gt;
 &lt;br /&gt;
 ggplot(df_hcv2_long) + &lt;br /&gt;
     geom_density(mapping=aes(x=value, fill=name), alpha=0.3) + &lt;br /&gt;
     labs(x=&amp;quot;Substitution rate&amp;quot;)&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Question&#039;&#039;&#039;: Based on the marginal distributions, r(AC) appears to be centered at a higher value than r(CG), but the two distributions overlap somewhat. Can you from these marginal distributions alone decide whether r(AC) is larger than r(CG) in most posterior samples? Why or why not?&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Question 9&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Marginal vs. joint distributions&#039;&#039;&#039;&lt;br /&gt;
: Looking at the marginal distributions gives us a fuller understanding of the uncertainty in each parameter separately. However, it still does not directly answer the question of whether r(AC) is larger than r(CG) in most posterior samples, because the two parameters may be associated with each other across samples. For instance, one parameter might be larger than the other in almost every individual sample, even though the two overall marginal distributions overlap. To answer such questions, we must examine the two parameters simultaneously. A probability distribution over several parameters at the same time is called a &amp;quot;joint distribution&amp;quot;, whereas a distribution for one parameter considered by itself is called a &amp;quot;marginal distribution&amp;quot;.&lt;br /&gt;
&lt;br /&gt;
: These plots and results explore the relationship between the A&amp;lt;-&amp;gt;C and C&amp;lt;-&amp;gt;G rates.&lt;br /&gt;
 ggplot(df_hcv2, aes(x=CG, y=AC)) + &lt;br /&gt;
     geom_point(col=&amp;quot;blue&amp;quot;) + &lt;br /&gt;
     geom_abline(intercept=0, slope=1, lty=2, col=&amp;quot;red&amp;quot;) +&lt;br /&gt;
     xlim(0,0.25) + &lt;br /&gt;
     ylim(0,0.25) + &lt;br /&gt;
     labs(x=&amp;quot;CG rate&amp;quot;, y =&amp;quot;AC rate&amp;quot;) &lt;br /&gt;
 &lt;br /&gt;
 ggplot(df_hcv2, aes(x=CG, y= AC)) + &lt;br /&gt;
     geom_hex(col=&amp;quot;blue&amp;quot;) + &lt;br /&gt;
     geom_abline(intercept=0, slope=1, lty=2, col=&amp;quot;red&amp;quot;) +&lt;br /&gt;
     xlim(0,0.25) + &lt;br /&gt;
     ylim(0, 0.25) + &lt;br /&gt;
     labs(x=&amp;quot;CG rate&amp;quot;, y =&amp;quot;AC rate&amp;quot;) &lt;br /&gt;
 &lt;br /&gt;
 df_hcv2 %&amp;gt;%&lt;br /&gt;
     nrow()&lt;br /&gt;
 df_hcv2 %&amp;gt;%&lt;br /&gt;
     filter(AC&amp;gt;CG) %&amp;gt;%&lt;br /&gt;
     nrow()&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Question: &#039;&#039;&#039;Based on the two different ways to plot the joint distribution and based on the unfiltered and filtered row counts, what is the posterior probability that r(AC) &amp;gt; r(CG)?&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
: Note how examining the joint distribution provides information that you could not obtain by simply comparing the marginal distributions. In particular, it lets you answer direct questions about how parameters relate to each other, for instance whether one is larger than another in most posterior samples. The same idea can be used to answer many other questions about posterior distributions.&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Question 10&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
: Now, plot the relative substitution rates at the first, second, and third codon positions:&lt;br /&gt;
 df_hcv3 = df_hcv %&amp;gt;% &lt;br /&gt;
     filter(Gen &amp;gt; 75000) %&amp;gt;%&lt;br /&gt;
     select(Codon_1st = `m{1}`,&lt;br /&gt;
            Codon_2nd = `m{2}`,&lt;br /&gt;
            Codon_3rd = `m{3}` ) %&amp;gt;%&lt;br /&gt;
     pivot_longer(cols=c(&amp;quot;Codon_1st&amp;quot;, &amp;quot;Codon_2nd&amp;quot;, &amp;quot;Codon_3rd&amp;quot;))&lt;br /&gt;
 &lt;br /&gt;
 ggplot(df_hcv3) + &lt;br /&gt;
     geom_density(mapping=aes(x=value, fill=name), alpha=0.3) + &lt;br /&gt;
     labs(x=&amp;quot;Relative substitution rate&amp;quot;)&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Question: &#039;&#039;&#039;Since random mutations presumably hit all three codon positions with the same frequency, any differences are expected to be caused by subsequent selection. Which of the following statement are correct ? (More than one answer may be correct)&lt;br /&gt;
&lt;br /&gt;
:* Codon position 2 is the most variable of the codon positions.&lt;br /&gt;
:* Codon position 1 is the most variable of the codon positions.&lt;br /&gt;
:* Codon position 1 is the most conserved codon position.&lt;br /&gt;
:* Codon position 3 is the most conserved codon position.&lt;br /&gt;
:* Codon position 3 is the most variable of the codon positions.&lt;br /&gt;
:* Codon position 2 is the most conserved codon position.&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Question 11&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Question: &#039;&#039;&#039;How does this result fit with your knowledge of the genetic code? Why are these codon positions the most conserved or the most variable?&lt;/div&gt;</summary>
		<author><name>Gorm</name></author>
	</entry>
	<entry>
		<id>https://teaching.healthtech.dtu.dk/22115/index.php?title=Bayesian_Phylogeny&amp;diff=254</id>
		<title>Bayesian Phylogeny</title>
		<link rel="alternate" type="text/html" href="https://teaching.healthtech.dtu.dk/22115/index.php?title=Bayesian_Phylogeny&amp;diff=254"/>
		<updated>2026-03-19T11:09:13Z</updated>

		<summary type="html">&lt;p&gt;Gorm: /* Probability distributions over other parameters */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;This exercise is part of the course  [[22115_-_Computational_Molecular_Evolution|Computational Molecular Evolution (22115)]].&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Overview ==&lt;br /&gt;
&lt;br /&gt;
Today&#039;s exercise will focus on phylogenetic analysis using Bayesian methods.&lt;br /&gt;
&lt;br /&gt;
As was the case for likelihood methods, Bayesian analysis is founded on having a probabilistic model of how the observed data is produced. This means that, for a given set of parameter values, you can compute the probability or [https://www.statlect.com/glossary/probability-density-function probability density] of any possible observation. For a full dataset, you then obtain the likelihood by multiplying these values across all observations. You will recall from the lecture that in Bayesian statistics the goal is to obtain a full posterior probability distribution over all possible parameter values. The posterior distribution quantifies our degree of belief in any possible parameter value after seeing the data. It is obtained by updating the prior probability distribution using the likelihood of the observed data.&lt;br /&gt;
&lt;br /&gt;
The prior probability distribution expresses your beliefs about the parameters before seeing any data, while the likelihood expresses what the observed data are telling you about the parameters. Specifically, the likelihood of a parameter value is the probability of the observed data given that parameter value. We regard a parameter value as more plausible the more probable it makes the observed data. This is the same measure we have previously used to find the maximum likelihood estimate. If the prior probability distribution is flat (i.e., if all possible parameter values have the same prior probability), then the posterior distribution is proportional to the likelihood, and the parameter value with the maximum likelihood also has the maximum posterior probability. However, even in this case, using a Bayesian approach still lets you interpret the result as a probability distribution over parameter values. &lt;br /&gt;
&lt;br /&gt;
If the prior is not flat, then it may have a substantial impact on the posterior, although this effect will usually diminish as the amount of data increases. A prior should ideally be based on domain knowledge and results from previous experiments. For instance one can use the posterior from one analysis as the prior in a new, independent analysis. Often a prior is chosen to be weakly informative, meaning that it places reasonable bounds on the parameter values without constraining them too narrowly. For instance the transition/transversion rate ratio kappa is typically 1.5-10. Values such as 100, 1,000 or 1,000,000 would be extremely unlikely, so a weakly informative prior for this parameter could be chosen to place 95% of its probability mass in the 0.5-20 range, slightly wider than what we think of as plausible values. For instance one could use a lognormal distribution with suitable parameters.&lt;br /&gt;
&lt;br /&gt;
In Bayesian phylogeny the parameters are of the same kind as in maximum likelihood phylogeny. Typical parameters include tree topology, branch lengths, nucleotide frequencies, and substitution model parameters such as the transition/transversion rate ratio or the gamma shape parameter. The difference is that, whereas in maximum likelihood phylogeny we seek the best point estimates of the parameter values, in Bayesian phylogeny the goal is instead to infer a full probability distribution over the possible parameter values. The observed data are again usually taken to be the alignment, although strictly speaking it would be more reasonable to say that the sequences are what have been observed, and that the alignment should then be inferred jointly with the phylogeny.&lt;br /&gt;
&lt;br /&gt;
In this exercise we will explore how one can determine and use posterior probability distributions over trees, over clades, and over substitution parameters. We will also touch upon the difference between marginal and joint probability distributions.&lt;br /&gt;
&lt;br /&gt;
== Getting started ==&lt;br /&gt;
&lt;br /&gt;
: In the command below: Instead of /path/to/molevol enter the path to the directory where you have placed your course files (for instance cd /Users/bob/Documents/molevol, or cd /home/student/molevol).&lt;br /&gt;
 cd /path/to/molevol&lt;br /&gt;
 mkdir bayes&lt;br /&gt;
 cd bayes&lt;br /&gt;
 cp ../data/primatemitDNA.nexus ./primatemitDNA.nexus&lt;br /&gt;
 cp ../data/neanderthal.nexus ./neanderthal.nexus&lt;br /&gt;
 cp ../data/hcvsmall.nexus ./hcvsmall.nexus&lt;br /&gt;
&lt;br /&gt;
: You have analyzed (versions of) all these data files previously in this course. We will now use Bayesian phylogenetic analysis to complement what we learned in those analyses.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Load R libraries&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
: In RStudio: set the working directory to the bayes directory. Then issue these commands:&lt;br /&gt;
 library(magrittr)&lt;br /&gt;
 library(tidyverse)&lt;br /&gt;
 library(bayesplot)&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
== Posterior probability of trees ==&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Question 1&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
: In today&#039;s exercise we will be using the program &amp;quot;MrBayes&amp;quot; to perform Bayesian phylogenetic analysis. MrBayes is a program that, like PAUP*, can be controlled by giving commands at a command line prompt. In fact, there is a substantial overlap between the commands used to control MrBayes and the PAUP command language. This should be a help when you are trying to understand how to use the program.&lt;br /&gt;
&lt;br /&gt;
: Note that the command &amp;quot;help&amp;quot; will give you a list of all available commands. Issuing &amp;quot;help &#039;&#039;command&#039;&#039;&amp;quot; will give you a more detailed description of the specified command along with current option values. This is similar to how &amp;quot;help &#039;&#039;command&#039;&#039;&amp;quot; works in PAUP.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Start program&#039;&#039;&#039;&lt;br /&gt;
: In a terminal window, issue the command:&lt;br /&gt;
 mb&lt;br /&gt;
: This starts the program, giving you a prompt (&amp;quot;MrBayes&amp;gt; &amp;quot;) where you can enter commands.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Get a quick overview of available commands&#039;&#039;&#039;&lt;br /&gt;
 help&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Load your sequences&#039;&#039;&#039;&lt;br /&gt;
 execute primatemitDNA.nexus&lt;br /&gt;
: This file contains mitochondrial DNA sequences from 5 different primates. Note that MrBayes accepts input in nexus format, and that this is the same command that was used to load sequences in PAUP*. In general, you can use many of the PAUP commands in MrBayes also.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Inspect data set&#039;&#039;&#039;&lt;br /&gt;
 showmatrix&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Define outgroup&#039;&#039;&#039;&lt;br /&gt;
 outgroup Gibbon&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Specify your model of sequence evolution&#039;&#039;&#039;&lt;br /&gt;
 lset nst=2 rates=gamma&lt;br /&gt;
: This command is again very much like the corresponding one in PAUP. You are specifying that you want to use a model with two substitution types (nst=2), and this is automatically taken to mean that you want to distinguish between transitions and transversions. Furthermore, rates=gamma means that you want the model to use a gamma distribution to account for different rates at different sites in the sequence.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Start Markov chain Monte Carlo sampling&#039;&#039;&#039;&lt;br /&gt;
:Make sure to make the shell window as wide as possible and then issue the following commands to start the run:&lt;br /&gt;
 mcmc ngen=1000000 samplefreq=100 nchains=3 diagnfreq=5000&lt;br /&gt;
: What you are doing here is to use the method known as MCMCMC (&amp;quot;Metropolis-coupled Markov chain Monte Carlo&amp;quot;) to empirically determine the posterior probability distribution of trees, branch lengths and substitution parameters. Recall that in the Bayesian framework this is how we learn about parameter values: instead of finding the best point estimates, we typically want to quantify the probability of the entire range of possible values. An estimate of the time left is shown in the last column of output.&lt;br /&gt;
&lt;br /&gt;
: Let us examine the command in detail. First, ngen=1000000 samplefreq=100 lets the search run for 1,000,000 MCMC steps (&amp;quot;generations&amp;quot;) and saves parameter values once every 100 rounds (meaning that a total of 10,000 sets of parameter values will be saved to sample files). You sometimes need to run longer (or shorter) than 1,000,000, and would then typically tweak samplefreq so you get around 1,000 - 10,000 samples in all. The option nchains=3 means that the MCMCMC sampling uses 3 parallel chains (but see below): one &amp;quot;cold&amp;quot; from which sampling takes place, and two &amp;quot;heated&amp;quot; that move around in the parameter space more quickly to find additional peaks in the probability distribution.&lt;br /&gt;
&lt;br /&gt;
: The option diagnfreq=5000 has to do with testing whether the MrBayes run is successful. Briefly, MrBayes will start two entirely independent runs starting from different random trees. In the early phases of the run, the two runs will sample very different trees, but when they have reached convergence (when they produce a good sample from the posterior probability distribution), the two tree samples should be very similar. Every diagnfreq generations, the program will compute a measure of how similar the tree samples are, specifically the average standard deviation of split frequencies. A “split” is the same as a bipartition, i.e. a division of all leaves in the tree into two groups, obtained by cutting an internal branch. For each split, MrBayes compares how often that split occurs in the two independent runs; if the runs have converged, these frequencies should be very similar, giving a small standard deviation. The program then averages this quantity across splits. As a rule of thumb, you may want to run until this value is less than 0.05 (the smaller the better)&lt;br /&gt;
&lt;br /&gt;
: During the run you will see reports about the progress of the two independent runs, each consisting of three chains. Each line of output lists the generation number and the log likelihoods of the current tree/parameter combination for each of the two groups of three chains (a column of asterisks separate the results for the independent runs). The cold chains are the ones enclosed in brackets [...], while the heated chains are enclosed in parentheses (...). Occasionally the chains will swap so one of the heated chains now becomes cold (and sampling then takes place from this chain).&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Continue run until parallel runs converge on same solution&#039;&#039;&#039;&lt;br /&gt;
:At the end of the run, Mrbayes will print the average standard deviation of split frequencies (which is a measure of how similar the tree samples of the two independent runs are). We recommend that you continue with the analysis until the value gets below 0.01 (if the value is larger than 0.01 then you should answer &amp;quot;yes&amp;quot; when the program asks &amp;quot;Continue the analysis? (yes/no)&amp;quot;.)&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Question: &#039;&#039;&#039;MrBayes starts two independent runs from different random trees. Why is it useful to run two independent analyses instead of just one? How does the average standard deviation of split frequencies help you decide whether the two runs have converged to the same posterior distribution? At approximately how many generations does this happen in your run?&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Question 2&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Have a look at the resulting sample files&#039;&#039;&#039;&lt;br /&gt;
: Open a new Terminal window (don&#039;t quit mrbayes in the other terminal yet!) and cd to the bayes directory. Open one of the parameter sampling files in a text editor:&lt;br /&gt;
 nedit primatemitDNA.nexus.run1.p &amp;amp;&lt;br /&gt;
: This file contains one line for each sampled point (you may want to turn off line-wrapping in nedit under the preferences menu). Each row corresponds to a certain sample time (or generation). Each column contains the sampled values of one specific parameter. The first line contains headings telling what the different columns are: &lt;br /&gt;
:* Gen: generation; number of MCMC steps taken so far&lt;br /&gt;
:* lnL: log likelihood of the current parameter estimates&lt;br /&gt;
:* LnPr: log of the prior probability&lt;br /&gt;
:* TL: tree length (sum of all branch lengths)&lt;br /&gt;
:* kappa: transition/transversion rate ratio&lt;br /&gt;
:* pi(A), pi(C), pi(G), pi(T): frequency of A, C, G, T&lt;br /&gt;
:* alpha: shape parameter for the gamma distribution. &lt;br /&gt;
&lt;br /&gt;
: (Column headings may be shifted relative to their corresponding columns). Note how the values of most parameters change a lot during the initial &amp;quot;burnin&amp;quot; period, before they settle near their most probable values. &lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Question: &#039;&#039;&#039;You will notice that lnL is always negative, while LnPr can sometimes be positive. At first sight this may seem impossible, since probabilities cannot be larger than 1. How can this happen?&lt;br /&gt;
&lt;br /&gt;
As a hint, note that (1) priors for continuous parameters are probability densities, and (2) the default prior for each branch length in MrBayes is an exponential distribution with rate 10. Use the following R code to plot this prior on both an ordinary y-axis and a log-scaled y-axis, and then explain why positive values of LnPr are possible.&lt;br /&gt;
&lt;br /&gt;
 df_expdist = tibble(&lt;br /&gt;
   x = seq(0, 1, by = 0.001),&lt;br /&gt;
   density = dexp(x, rate = 10),&lt;br /&gt;
   logdensity = log(dexp(x, rate = 10))&lt;br /&gt;
 )&lt;br /&gt;
 &lt;br /&gt;
 ggplot(df_expdist, aes(x = x, y = density)) +&lt;br /&gt;
   geom_line(color=&amp;quot;blue&amp;quot;) +&lt;br /&gt;
   geom_hline(yintercept = 1, lty=2) +&lt;br /&gt;
   labs(&lt;br /&gt;
     x = &amp;quot;Branch length&amp;quot;,&lt;br /&gt;
     y = &amp;quot;Probability density&amp;quot;,&lt;br /&gt;
     title = &amp;quot;Exponential prior on branch lengths: Exp(rate = 10)&amp;quot;&lt;br /&gt;
   )&lt;br /&gt;
 &lt;br /&gt;
 ggplot(df_expdist, aes(x = x, y = logdensity)) +&lt;br /&gt;
   geom_line(color=&amp;quot;blue&amp;quot;) +&lt;br /&gt;
   geom_hline(yintercept = 0, lty=2) +&lt;br /&gt;
   labs(&lt;br /&gt;
     x = &amp;quot;Branch length&amp;quot;,&lt;br /&gt;
     y = &amp;quot;log of Probability density&amp;quot;,&lt;br /&gt;
     title = &amp;quot;Exponential prior on branch lengths: Exp(rate = 10)&amp;quot;&lt;br /&gt;
   )&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Question 3&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Examine MCMC trajectory for gamma shape parameter, alpha&#039;&#039;&#039;&lt;br /&gt;
: Recall that the idea in MCMCMC sampling is to move around in parameter space in such a way that points are visited according to their posterior probability (i.e., regions with high posterior probability are visited frequently). Now, in RStudio, plot the sampled values for the gamma shape parameter, alpha, for one of the run files:&lt;br /&gt;
 df_primates = read_tsv(&amp;quot;primatemitDNA.nexus.run1.p&amp;quot;, skip=1)&lt;br /&gt;
 mcmc_trace(df_primates, pars=&amp;quot;alpha&amp;quot;)&lt;br /&gt;
: mcmc_trace is one of several plotting commands available in the bayesplot package. This command plots the sampled values of the parameter alpha from the first of the two parallel runs against MCMC generation number. Thus, the x-axis shows the progress of the run through time, with the leftmost values being the earliest samples and the rightmost values the later ones. Note how the Markov chain starts at the arbitrary value 1.0, rapidly moves to values that fit the observed data better, and then moves around in parameter space, sampling different plausible values of alpha. You can experiment with plotting other columns as well.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Question: &#039;&#039;&#039;Describe briefly what happens to the sampled values of alpha during the run. Why is it reasonable to discard the earliest samples as burn-in?&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Question 4&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Investigate posterior probability distribution over trees&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
: Now, close the nedit window and have a look at the file containing sampled trees:&lt;br /&gt;
 nedit primatemitDNA.nexus.run1.t &amp;amp;&lt;br /&gt;
: Tree topology is also a parameter in our model, and exactly like for the other parameters we also get samples from tree-space. One tree is printed per line in the parenthetical Newick format you have seen before. There are 5 taxa in the present data set, so the number of possible unrooted binary tree topologies is only 15. Since we have taken more than 15 sample points, there must be several lines containing the same tree topology. Close the nedit window when you are done.&lt;br /&gt;
&lt;br /&gt;
: MrBayes provides the sumt command to summarize the sampled trees. Before using it, we need to decide on the burn-in: The burn-in is the initial set of samples that are typically discarded, because we want to ensure that the MCMC has moved away from the random starting values, and has found the peaks of the probability landscape. Since the convergence diagnostic used a relative burn-in of 25%, we will also discard the first 25% of tree samples when summarizing the posterior.&lt;br /&gt;
&lt;br /&gt;
: Return to the shell window where you have MrBayes running. In the command below relburnin=yes and burninfrac=0.25 tells MrBayes to discard 25% of the samples as burnin (you could also have explicitly given the number of samples to discard - help sumt will give you details about the command and the current option settings).&lt;br /&gt;
 sumt contype=halfcompat conformat=simple relburnin=yes burninfrac=0.25 showtreeprobs=yes&lt;br /&gt;
: (Scroll back so you can see the top of the output when the command is done). This command gives you a summary of the trees that are in the file you examined manually above. The option contype=halfcompat requests that a majority rule consensus tree is calculated from the set of trees that are left after discarding the burnin. This consensus is the first tree plotted to the screen. Below the consensus cladogram, a consensus phylogram is plotted. The branch lengths in this have been averaged over the trees in which that branch was present (a particular branch corresponds to a bi-partition of the data, and will typically not be present in every sampled tree). The cladogram also has &amp;quot;clade credibility&amp;quot; values. We will return to the meaning of these later in today&#039;s exercise.&lt;br /&gt;
&lt;br /&gt;
: What most interests us right now is the list of trees that is printed after the phylogram. These trees are labeled &amp;quot;Tree 1&amp;quot;, &amp;quot;Tree 2&amp;quot;, etc, and are sorted according to their posterior probability which is indicated by a lower-case p after the tree number. (The upper-case P gives the cumulated probability of trees shown so far, and is useful for constructing a credible set). This list highlights how Bayesian phylogenetic analysis is different from maximum likelihood: Instead of finding the best tree(s), we here quantify our degree of belief in all possible trees.&lt;br /&gt;
&lt;br /&gt;
: The list of trees and probabilities was printed because of the option showtreeprobs=yes. Note that you probably do not want to issue that command if you have much more than 5 taxa! In that case you could instead inspect the file named primatemitDNA.nexus.trprobs which is now present in the same directory as your other files (this file is automatically produced by the sumt command).&lt;br /&gt;
&lt;br /&gt;
: &#039;&#039;&#039;NOTE&#039;&#039;&#039;: Annoyingly, there is a bug in the version of mrbayes we are using here, which means leaf names are not printed on the list of trees with probabilities. However, the most probable tree here in fact is identical to the consensus tree printed above it.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Question: &#039;&#039;&#039;What is the posterior probability of the most probable tree? Does the analysis strongly support a single tree, or is the posterior probability distributed across several different trees?&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
== Analysis of Neanderthal data (posterior probability of clades) ==&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Question 5&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
For many years, there was considerable debate about the origin of modern humans. One view, often called the [https://www.thoughtco.com/multiregional-hypothesis-167235 Multiregional Hypothesis], proposed that after Homo erectus spread from Africa into different parts of the world, regional populations gradually evolved into modern humans more or less in parallel. A different view, often called the Recent African Origin model, proposed that modern Homo sapiens evolved in Africa and later spread outward, largely replacing other archaic human groups such as the Neanderthals.&lt;br /&gt;
&lt;br /&gt;
Today it is clear that the history is more complicated than either simple extreme: modern humans arose in Africa, but there was also some interbreeding with Neanderthals and other archaic humans. However, in this exercise we will focus on a narrower question that can be addressed using a phylogeny of mitochondrial DNA: do the sampled Neanderthal and human mitochondrial sequences suggest that the Neanderthal sequence falls inside or outside modern human mitochondrial diversity?&lt;br /&gt;
&lt;br /&gt;
We will use the present data set to examine this question.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Load Neanderthal data set&#039;&#039;&#039;&lt;br /&gt;
: In the Terminal where you have MrBayes running:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
execute neanderthal.nexus&lt;br /&gt;
delete 5-40&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
: As we did for the maximum likelihood analysis, we will discard some of the human sequences in order to speed up the analysis. The command delete 5-40 removes sequence number 5 to sequence number 40 from the active data set.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Investigate data&#039;&#039;&#039;&lt;br /&gt;
 showmatrix&lt;br /&gt;
: This data set consists of an alignment of mitochondrial DNA from human (17 sequences), chimpanzee (1 sequence), and Neanderthal (1 sequence). The Neanderthal DNA was extracted from archaeological material, specifically bones found at Vindija in Croatia.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Start analysis&#039;&#039;&#039;&lt;br /&gt;
 outgroup Pan_troglodytes&lt;br /&gt;
 lset nst=mixed rates=gamma&lt;br /&gt;
 mcmc ngen=500000 nchains=3 diagnfreq=10000&lt;br /&gt;
&lt;br /&gt;
: Here we use the command `nst=mixed` which allows MrBayes to automatically explore all possible substitution models. Essentially, MrBayes now considers the substitution model as one more parameter, and uses MCMC to sample from the possible versions (with nst ranging from 1 to 6). This will often be the best choice when using MrBayes. (Below, I use nst=6 for pedagogical purposes, because it makes it simpler to analyse the output files).&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Find posterior probability of clades&#039;&#039;&#039;&lt;br /&gt;
: When the run has finished, issue this command to compute a consensus tree:&lt;br /&gt;
 sumt contype=halfcompat showtreeprobs=no relburnin=yes burninfrac=0.25&lt;br /&gt;
: Examine the consensus tree that is plotted to screen: On the branches that are resolved, you will notice that numbers have been plotted. These are clade-credibility values, and are in fact the posterior probability that the clade is real (based on the present data set).&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Question: &#039;&#039;&#039;What is the posterior probability that all sampled Homo sapiens sequences form a monophyletic group excluding the Neanderthal sequence? Does this support placing the Neanderthal outside modern human mitochondrial diversity?&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
== Probability distributions over other parameters ==&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Question 6&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
: As the last thing, we will now turn away from the tree topology, and instead examine the other parameters that also form part of the probabilistic model. We will do this using a reduced version of the Hepatitis C virus data set that we have examined previously. Stay in the shell window where you just performed the analysis of Neanderthal sequences.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Load data set&#039;&#039;&#039;&lt;br /&gt;
 execute hcvsmall.nexus&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Define site partition&#039;&#039;&#039;&lt;br /&gt;
 charset 1stpos=1-.\3&lt;br /&gt;
 charset 2ndpos=2-.\3&lt;br /&gt;
 charset 3rdpos=3-.\3&lt;br /&gt;
 partition bycodon = 3:1stpos,2ndpos,3rdpos&lt;br /&gt;
 set partition=bycodon&lt;br /&gt;
 prset ratepr=variable&lt;br /&gt;
: This is an alternative way of specifying that different sites may evolve at different rates. With a gamma model, we allow rates to vary across sites but do not specify in advance which sites are fast or slow; instead, that pattern is inferred from the data. Here we instead use prior biological knowledge about the structure of the genetic code to divide sites into three classes: 1st, 2nd, and 3rd codon positions. We then allow each class to have its own rate, so that all 1st positions share one rate, all 2nd positions another, and all 3rd positions a third. Specifically, charset 1stpos=1-.\3 defines a character set named 1stpos consisting of site 1 followed by every third site (\3, i.e. sites 1, 4, 7, 10, …), continuing until the end of the alignment (denoted .).&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Specify model&#039;&#039;&#039;&lt;br /&gt;
 lset nst=6&lt;br /&gt;
: This specifies that we want to use a model of the General Time Reversible (GTR) type, where all 6 substitution types have separate rate parameters.&lt;br /&gt;
&lt;br /&gt;
: When the lset command was discussed previously, a few issues were glossed over. Importantly, and unlike PAUP, the lset command in MrBayes gives no information about whether nucleotide frequencies are equal or not, and whether they should be estimated from the data or not. In MrBayes this is instead controlled by defining the prior probability of the nucleotide frequencies (the command prset can be used to set priors). For instance, a model with equal nucleotide frequencies corresponds to having prior probability 1 (one) for the frequency vector (A=0.25, C=0.25, G=0.25, T=0.25), and zero prior probability for the infinitely many other possible frequency vectors. As you will see below, the default prior is not this limited, and the program will therefore estimate the frequencies from the data.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Inspect model details&#039;&#039;&#039;&lt;br /&gt;
 showmodel&lt;br /&gt;
: This command gives you a summary of the current model settings. You will also get a summary of how the prior probabilities of all model parameters are set. You will for instance notice that the nucleotide frequencies (parameter labeled &amp;quot;Statefreq&amp;quot;) have a &amp;quot;Dirichlet&amp;quot; prior. Without going into details, the Dirichlet distribution is a probability distribution over frequency vectors (i.e., vectors of positive values that sum to 1). Depending on the exact parameters the distribution can be more or less flat (flat here means that all sum-to-1 vectors are equally probable). The Dirichlet distribution is a handy way of specifying the prior probability distribution of nucleotide (or amino acid) frequency vectors. The default statefreq prior in MrBayes is the flat or un-informative prior dirichlet(1,1,1,1).&lt;br /&gt;
&lt;br /&gt;
: We will not go into the priors for the remaining parameters in any detail, but you may notice that by default all topologies are taken to be equally likely (a flat prior on trees).&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Start MCMC sampling&#039;&#039;&#039;&lt;br /&gt;
 mcmc ngen=1000000 samplefreq=100 diagnfreq=10000 nchains=3&lt;br /&gt;
: The run will take a while to finish (you may want to ensure that the average standard deviation of split frequencies is less than 0.01 before ending the analysis).&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Compute summary of parameter values&#039;&#039;&#039;&lt;br /&gt;
 sump relburnin=yes burninfrac=0.25&lt;br /&gt;
: The sump command (with a &amp;quot;p&amp;quot; at the end) works much like the sumt command (with a &amp;quot;t&amp;quot; at the end), but for other parameters than the tree-topology. Again, we are using 25% of the total number of samples as burnin.&lt;br /&gt;
&lt;br /&gt;
: First, you get a scatter plot of the lnL as a function of generation number. Values from the two independent runs are labeled &amp;quot;1&amp;quot; and &amp;quot;2&amp;quot; respectively. If the burnin is suitable, then the points should be randomly scattered over a narrow lnL interval.&lt;br /&gt;
&lt;br /&gt;
: Secondly, the posterior probability distribution of each parameter is summarized by giving the mean, variance, median, and 95% credible interval.&lt;br /&gt;
&lt;br /&gt;
: The last columns contain values indicating whether the run has converged. Specifically, ESS means Effective Sample Size, and measures how many effectively independent samples you have from the posterior — the higher the better, but this should be at least 100. [https://sites.stat.columbia.edu/gelman/research/published/brooksgelman2.pdf The column labeled PSRF+ gives another convergence diagnostic (also known as “R-hat”)] and should be close to 1 if the runs have converged. Specifically, it measures whether different chains (and different parts of the chains) are sampling the same distribution of values. As a rule of thumb, values less than 1.05 are good, values between 1.05 and 1.10 are acceptable, and values above 1.10 suggest poor convergence.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Question: &#039;&#039;&#039;What are the posterior mean values of the relative substitution rate parameters r(AC) and r(CG)?&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Question 7: &#039;&#039;&#039;Based on the reported posterior means, which of the two parameters, r(AC) or r(CG), appears to be larger on average?&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Question 8&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Marginal vs. joint distributions&#039;&#039;&#039;&lt;br /&gt;
: Strictly speaking the comparison above was not entirely appropriate. We first found the overall distribution of the r(CG) parameter and then compared its mean to the mean of the overall distribution of the r(AC) parameter. By doing things this way, we are ignoring the possibility that the two parameters might be associated in some way. For instance, one parameter might always be larger than the other in any individual sample, even though the total distributions overlap. We should instead be looking at the distribution over both parameters simultaneously. A probability distribution over several parameters simultaneously is called a &amp;quot;joint distribution&amp;quot; over the parameters.&lt;br /&gt;
&lt;br /&gt;
: By looking at one parameter at a time, we are summing its probability over all values of the other parameters. This is called the marginal distribution.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Examine marginal distributions&#039;&#039;&#039;&lt;br /&gt;
: In RStudio, use the following commands to read and plot the marginal distributions of r(AC) and r(CG). Note that we are discarding the first 25% of the reads as burnin&lt;br /&gt;
 df_hcv = read_tsv(&amp;quot;hcvsmall.nexus.run1.p&amp;quot;, skip=1)&lt;br /&gt;
 burnin = df_hcv$Gen %&amp;gt;% &lt;br /&gt;
     max() %&amp;gt;% &lt;br /&gt;
     multiply_by(0.25) %&amp;gt;% &lt;br /&gt;
     floor()&lt;br /&gt;
 df_hcv2 = df_hcv %&amp;gt;% &lt;br /&gt;
     filter(Gen &amp;gt; burnin) %&amp;gt;%&lt;br /&gt;
     select(CG = `r(C&amp;lt;-&amp;gt;G){all}`,&lt;br /&gt;
            AC = `r(A&amp;lt;-&amp;gt;C){all}`&lt;br /&gt;
            )&lt;br /&gt;
 mcmc_intervals(df_hcv2, prob_outer = 1)&lt;br /&gt;
 mcmc_areas(df_hcv2, prob_outer = 1)&lt;br /&gt;
: The functions mcmc_intervals and mcmc_areas plot different views of the same posterior distributions. &lt;br /&gt;
&lt;br /&gt;
: You can also simply plot the data using ggplot:&lt;br /&gt;
 df_hcv2_long = pivot_longer(df_hcv2, cols = c(&amp;quot;CG&amp;quot;, &amp;quot;AC&amp;quot;))&lt;br /&gt;
 &lt;br /&gt;
 ggplot(df_hcv2_long) + &lt;br /&gt;
     geom_density(mapping=aes(x=value, fill=name), alpha=0.3) + &lt;br /&gt;
     labs(x=&amp;quot;Substitution rate&amp;quot;)&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Question&#039;&#039;&#039;: Based on the marginal distributions, r(AC) appears to be centered at a higher value than r(CG), but the two distributions overlap somewhat. Can you from these marginal distributions alone decide whether r(AC) is larger than r(CG) in most posterior samples? Why or why not?&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Question 9&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Examine joint distributions&#039;&#039;&#039;&lt;br /&gt;
: These plots and results explore the relationship between the A&amp;lt;-&amp;gt;C and C&amp;lt;-&amp;gt;G rates.&lt;br /&gt;
 ggplot(df_hcv2, aes(x=CG, y=AC)) + &lt;br /&gt;
     geom_point(col=&amp;quot;blue&amp;quot;) + &lt;br /&gt;
     geom_abline(intercept=0, slope=1, lty=2, col=&amp;quot;red&amp;quot;) +&lt;br /&gt;
     xlim(0,0.25) + &lt;br /&gt;
     ylim(0,0.25) + &lt;br /&gt;
     labs(x=&amp;quot;CG rate&amp;quot;, y =&amp;quot;AC rate&amp;quot;) &lt;br /&gt;
 &lt;br /&gt;
 ggplot(df_hcv2, aes(x=CG, y= AC)) + &lt;br /&gt;
     geom_hex(col=&amp;quot;blue&amp;quot;) + &lt;br /&gt;
     geom_abline(intercept=0, slope=1, lty=2, col=&amp;quot;red&amp;quot;) +&lt;br /&gt;
     xlim(0,0.25) + &lt;br /&gt;
     ylim(0, 0.25) + &lt;br /&gt;
     labs(x=&amp;quot;CG rate&amp;quot;, y =&amp;quot;AC rate&amp;quot;) &lt;br /&gt;
 &lt;br /&gt;
 df_hcv2 %&amp;gt;%&lt;br /&gt;
     nrow()&lt;br /&gt;
 df_hcv2 %&amp;gt;%&lt;br /&gt;
     filter(AC&amp;gt;CG) %&amp;gt;%&lt;br /&gt;
     nrow()&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Question: &#039;&#039;&#039;Based on the two different ways to plot the joint distribution and based on the unfiltered and filtered row counts, what is the posterior probability that r(AC) &amp;gt; r(CG)?&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Question 10&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
: Note how examining the joint distribution provides you with information that you could not get from simply comparing the marginal distributions. This very simple procedure can be used to answer many different questions.&lt;br /&gt;
&lt;br /&gt;
: Now, plot the relative substitution rates at the first, second, and third codon positions:&lt;br /&gt;
 df_hcv3 = df_hcv %&amp;gt;% &lt;br /&gt;
     filter(Gen &amp;gt; 75000) %&amp;gt;%&lt;br /&gt;
     select(Codon_1st = `m{1}`,&lt;br /&gt;
            Codon_2nd = `m{2}`,&lt;br /&gt;
            Codon_3rd = `m{3}` ) %&amp;gt;%&lt;br /&gt;
     pivot_longer(cols=c(&amp;quot;Codon_1st&amp;quot;, &amp;quot;Codon_2nd&amp;quot;, &amp;quot;Codon_3rd&amp;quot;))&lt;br /&gt;
 &lt;br /&gt;
 ggplot(df_hcv3) + &lt;br /&gt;
     geom_density(mapping=aes(x=value, fill=name), alpha=0.3) + &lt;br /&gt;
     labs(x=&amp;quot;Relative substitution rate&amp;quot;)&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Question: &#039;&#039;&#039;Since random mutations presumably hit all three codon positions with the same frequency, any differences are expected to be caused by subsequent selection. Which of the following statement are correct ? (More than one answer may be correct)&lt;br /&gt;
&lt;br /&gt;
:* Codon position 2 is the most variable of the codon positions.&lt;br /&gt;
:* Codon position 1 is the most variable of the codon positions.&lt;br /&gt;
:* Codon position 1 is the most conserved codon position.&lt;br /&gt;
:* Codon position 3 is the most conserved codon position.&lt;br /&gt;
:* Codon position 3 is the most variable of the codon positions.&lt;br /&gt;
:* Codon position 2 is the most conserved codon position.&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Question 11&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Question: &#039;&#039;&#039;How does this result fit with your knowledge of the genetic code? Why are these codon positions the most conserved or the most variable?&lt;/div&gt;</summary>
		<author><name>Gorm</name></author>
	</entry>
	<entry>
		<id>https://teaching.healthtech.dtu.dk/22115/index.php?title=Bayesian_Phylogeny&amp;diff=253</id>
		<title>Bayesian Phylogeny</title>
		<link rel="alternate" type="text/html" href="https://teaching.healthtech.dtu.dk/22115/index.php?title=Bayesian_Phylogeny&amp;diff=253"/>
		<updated>2026-03-19T10:44:37Z</updated>

		<summary type="html">&lt;p&gt;Gorm: /* Probability distributions over other parameters */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;This exercise is part of the course  [[22115_-_Computational_Molecular_Evolution|Computational Molecular Evolution (22115)]].&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Overview ==&lt;br /&gt;
&lt;br /&gt;
Today&#039;s exercise will focus on phylogenetic analysis using Bayesian methods.&lt;br /&gt;
&lt;br /&gt;
As was the case for likelihood methods, Bayesian analysis is founded on having a probabilistic model of how the observed data is produced. This means that, for a given set of parameter values, you can compute the probability or [https://www.statlect.com/glossary/probability-density-function probability density] of any possible observation. For a full dataset, you then obtain the likelihood by multiplying these values across all observations. You will recall from the lecture that in Bayesian statistics the goal is to obtain a full posterior probability distribution over all possible parameter values. The posterior distribution quantifies our degree of belief in any possible parameter value after seeing the data. It is obtained by updating the prior probability distribution using the likelihood of the observed data.&lt;br /&gt;
&lt;br /&gt;
The prior probability distribution expresses your beliefs about the parameters before seeing any data, while the likelihood expresses what the observed data are telling you about the parameters. Specifically, the likelihood of a parameter value is the probability of the observed data given that parameter value. We regard a parameter value as more plausible the more probable it makes the observed data. This is the same measure we have previously used to find the maximum likelihood estimate. If the prior probability distribution is flat (i.e., if all possible parameter values have the same prior probability), then the posterior distribution is proportional to the likelihood, and the parameter value with the maximum likelihood also has the maximum posterior probability. However, even in this case, using a Bayesian approach still lets you interpret the result as a probability distribution over parameter values. &lt;br /&gt;
&lt;br /&gt;
If the prior is not flat, then it may have a substantial impact on the posterior, although this effect will usually diminish as the amount of data increases. A prior should ideally be based on domain knowledge and results from previous experiments. For instance one can use the posterior from one analysis as the prior in a new, independent analysis. Often a prior is chosen to be weakly informative, meaning that it places reasonable bounds on the parameter values without constraining them too narrowly. For instance the transition/transversion rate ratio kappa is typically 1.5-10. Values such as 100, 1,000 or 1,000,000 would be extremely unlikely, so a weakly informative prior for this parameter could be chosen to place 95% of its probability mass in the 0.5-20 range, slightly wider than what we think of as plausible values. For instance one could use a lognormal distribution with suitable parameters.&lt;br /&gt;
&lt;br /&gt;
In Bayesian phylogeny the parameters are of the same kind as in maximum likelihood phylogeny. Typical parameters include tree topology, branch lengths, nucleotide frequencies, and substitution model parameters such as the transition/transversion rate ratio or the gamma shape parameter. The difference is that, whereas in maximum likelihood phylogeny we seek the best point estimates of the parameter values, in Bayesian phylogeny the goal is instead to infer a full probability distribution over the possible parameter values. The observed data are again usually taken to be the alignment, although strictly speaking it would be more reasonable to say that the sequences are what have been observed, and that the alignment should then be inferred jointly with the phylogeny.&lt;br /&gt;
&lt;br /&gt;
In this exercise we will explore how one can determine and use posterior probability distributions over trees, over clades, and over substitution parameters. We will also touch upon the difference between marginal and joint probability distributions.&lt;br /&gt;
&lt;br /&gt;
== Getting started ==&lt;br /&gt;
&lt;br /&gt;
: In the command below: Instead of /path/to/molevol enter the path to the directory where you have placed your course files (for instance cd /Users/bob/Documents/molevol, or cd /home/student/molevol).&lt;br /&gt;
 cd /path/to/molevol&lt;br /&gt;
 mkdir bayes&lt;br /&gt;
 cd bayes&lt;br /&gt;
 cp ../data/primatemitDNA.nexus ./primatemitDNA.nexus&lt;br /&gt;
 cp ../data/neanderthal.nexus ./neanderthal.nexus&lt;br /&gt;
 cp ../data/hcvsmall.nexus ./hcvsmall.nexus&lt;br /&gt;
&lt;br /&gt;
: You have analyzed (versions of) all these data files previously in this course. We will now use Bayesian phylogenetic analysis to complement what we learned in those analyses.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Load R libraries&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
: In RStudio: set the working directory to the bayes directory. Then issue these commands:&lt;br /&gt;
 library(magrittr)&lt;br /&gt;
 library(tidyverse)&lt;br /&gt;
 library(bayesplot)&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
== Posterior probability of trees ==&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Question 1&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
: In today&#039;s exercise we will be using the program &amp;quot;MrBayes&amp;quot; to perform Bayesian phylogenetic analysis. MrBayes is a program that, like PAUP*, can be controlled by giving commands at a command line prompt. In fact, there is a substantial overlap between the commands used to control MrBayes and the PAUP command language. This should be a help when you are trying to understand how to use the program.&lt;br /&gt;
&lt;br /&gt;
: Note that the command &amp;quot;help&amp;quot; will give you a list of all available commands. Issuing &amp;quot;help &#039;&#039;command&#039;&#039;&amp;quot; will give you a more detailed description of the specified command along with current option values. This is similar to how &amp;quot;help &#039;&#039;command&#039;&#039;&amp;quot; works in PAUP.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Start program&#039;&#039;&#039;&lt;br /&gt;
: In a terminal window, issue the command:&lt;br /&gt;
 mb&lt;br /&gt;
: This starts the program, giving you a prompt (&amp;quot;MrBayes&amp;gt; &amp;quot;) where you can enter commands.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Get a quick overview of available commands&#039;&#039;&#039;&lt;br /&gt;
 help&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Load your sequences&#039;&#039;&#039;&lt;br /&gt;
 execute primatemitDNA.nexus&lt;br /&gt;
: This file contains mitochondrial DNA sequences from 5 different primates. Note that MrBayes accepts input in nexus format, and that this is the same command that was used to load sequences in PAUP*. In general, you can use many of the PAUP commands in MrBayes also.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Inspect data set&#039;&#039;&#039;&lt;br /&gt;
 showmatrix&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Define outgroup&#039;&#039;&#039;&lt;br /&gt;
 outgroup Gibbon&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Specify your model of sequence evolution&#039;&#039;&#039;&lt;br /&gt;
 lset nst=2 rates=gamma&lt;br /&gt;
: This command is again very much like the corresponding one in PAUP. You are specifying that you want to use a model with two substitution types (nst=2), and this is automatically taken to mean that you want to distinguish between transitions and transversions. Furthermore, rates=gamma means that you want the model to use a gamma distribution to account for different rates at different sites in the sequence.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Start Markov chain Monte Carlo sampling&#039;&#039;&#039;&lt;br /&gt;
:Make sure to make the shell window as wide as possible and then issue the following commands to start the run:&lt;br /&gt;
 mcmc ngen=1000000 samplefreq=100 nchains=3 diagnfreq=5000&lt;br /&gt;
: What you are doing here is to use the method known as MCMCMC (&amp;quot;Metropolis-coupled Markov chain Monte Carlo&amp;quot;) to empirically determine the posterior probability distribution of trees, branch lengths and substitution parameters. Recall that in the Bayesian framework this is how we learn about parameter values: instead of finding the best point estimates, we typically want to quantify the probability of the entire range of possible values. An estimate of the time left is shown in the last column of output.&lt;br /&gt;
&lt;br /&gt;
: Let us examine the command in detail. First, ngen=1000000 samplefreq=100 lets the search run for 1,000,000 MCMC steps (&amp;quot;generations&amp;quot;) and saves parameter values once every 100 rounds (meaning that a total of 10,000 sets of parameter values will be saved to sample files). You sometimes need to run longer (or shorter) than 1,000,000, and would then typically tweak samplefreq so you get around 1,000 - 10,000 samples in all. The option nchains=3 means that the MCMCMC sampling uses 3 parallel chains (but see below): one &amp;quot;cold&amp;quot; from which sampling takes place, and two &amp;quot;heated&amp;quot; that move around in the parameter space more quickly to find additional peaks in the probability distribution.&lt;br /&gt;
&lt;br /&gt;
: The option diagnfreq=5000 has to do with testing whether the MrBayes run is successful. Briefly, MrBayes will start two entirely independent runs starting from different random trees. In the early phases of the run, the two runs will sample very different trees, but when they have reached convergence (when they produce a good sample from the posterior probability distribution), the two tree samples should be very similar. Every diagnfreq generations, the program will compute a measure of how similar the tree samples are, specifically the average standard deviation of split frequencies. A “split” is the same as a bipartition, i.e. a division of all leaves in the tree into two groups, obtained by cutting an internal branch. For each split, MrBayes compares how often that split occurs in the two independent runs; if the runs have converged, these frequencies should be very similar, giving a small standard deviation. The program then averages this quantity across splits. As a rule of thumb, you may want to run until this value is less than 0.05 (the smaller the better)&lt;br /&gt;
&lt;br /&gt;
: During the run you will see reports about the progress of the two independent runs, each consisting of three chains. Each line of output lists the generation number and the log likelihoods of the current tree/parameter combination for each of the two groups of three chains (a column of asterisks separate the results for the independent runs). The cold chains are the ones enclosed in brackets [...], while the heated chains are enclosed in parentheses (...). Occasionally the chains will swap so one of the heated chains now becomes cold (and sampling then takes place from this chain).&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Continue run until parallel runs converge on same solution&#039;&#039;&#039;&lt;br /&gt;
:At the end of the run, Mrbayes will print the average standard deviation of split frequencies (which is a measure of how similar the tree samples of the two independent runs are). We recommend that you continue with the analysis until the value gets below 0.01 (if the value is larger than 0.01 then you should answer &amp;quot;yes&amp;quot; when the program asks &amp;quot;Continue the analysis? (yes/no)&amp;quot;.)&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Question: &#039;&#039;&#039;MrBayes starts two independent runs from different random trees. Why is it useful to run two independent analyses instead of just one? How does the average standard deviation of split frequencies help you decide whether the two runs have converged to the same posterior distribution? At approximately how many generations does this happen in your run?&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Question 2&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Have a look at the resulting sample files&#039;&#039;&#039;&lt;br /&gt;
: Open a new Terminal window (don&#039;t quit mrbayes in the other terminal yet!) and cd to the bayes directory. Open one of the parameter sampling files in a text editor:&lt;br /&gt;
 nedit primatemitDNA.nexus.run1.p &amp;amp;&lt;br /&gt;
: This file contains one line for each sampled point (you may want to turn off line-wrapping in nedit under the preferences menu). Each row corresponds to a certain sample time (or generation). Each column contains the sampled values of one specific parameter. The first line contains headings telling what the different columns are: &lt;br /&gt;
:* Gen: generation; number of MCMC steps taken so far&lt;br /&gt;
:* lnL: log likelihood of the current parameter estimates&lt;br /&gt;
:* LnPr: log of the prior probability&lt;br /&gt;
:* TL: tree length (sum of all branch lengths)&lt;br /&gt;
:* kappa: transition/transversion rate ratio&lt;br /&gt;
:* pi(A), pi(C), pi(G), pi(T): frequency of A, C, G, T&lt;br /&gt;
:* alpha: shape parameter for the gamma distribution. &lt;br /&gt;
&lt;br /&gt;
: (Column headings may be shifted relative to their corresponding columns). Note how the values of most parameters change a lot during the initial &amp;quot;burnin&amp;quot; period, before they settle near their most probable values. &lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Question: &#039;&#039;&#039;You will notice that lnL is always negative, while LnPr can sometimes be positive. At first sight this may seem impossible, since probabilities cannot be larger than 1. How can this happen?&lt;br /&gt;
&lt;br /&gt;
As a hint, note that (1) priors for continuous parameters are probability densities, and (2) the default prior for each branch length in MrBayes is an exponential distribution with rate 10. Use the following R code to plot this prior on both an ordinary y-axis and a log-scaled y-axis, and then explain why positive values of LnPr are possible.&lt;br /&gt;
&lt;br /&gt;
 df_expdist = tibble(&lt;br /&gt;
   x = seq(0, 1, by = 0.001),&lt;br /&gt;
   density = dexp(x, rate = 10),&lt;br /&gt;
   logdensity = log(dexp(x, rate = 10))&lt;br /&gt;
 )&lt;br /&gt;
 &lt;br /&gt;
 ggplot(df_expdist, aes(x = x, y = density)) +&lt;br /&gt;
   geom_line(color=&amp;quot;blue&amp;quot;) +&lt;br /&gt;
   geom_hline(yintercept = 1, lty=2) +&lt;br /&gt;
   labs(&lt;br /&gt;
     x = &amp;quot;Branch length&amp;quot;,&lt;br /&gt;
     y = &amp;quot;Probability density&amp;quot;,&lt;br /&gt;
     title = &amp;quot;Exponential prior on branch lengths: Exp(rate = 10)&amp;quot;&lt;br /&gt;
   )&lt;br /&gt;
 &lt;br /&gt;
 ggplot(df_expdist, aes(x = x, y = logdensity)) +&lt;br /&gt;
   geom_line(color=&amp;quot;blue&amp;quot;) +&lt;br /&gt;
   geom_hline(yintercept = 0, lty=2) +&lt;br /&gt;
   labs(&lt;br /&gt;
     x = &amp;quot;Branch length&amp;quot;,&lt;br /&gt;
     y = &amp;quot;log of Probability density&amp;quot;,&lt;br /&gt;
     title = &amp;quot;Exponential prior on branch lengths: Exp(rate = 10)&amp;quot;&lt;br /&gt;
   )&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Question 3&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Examine MCMC trajectory for gamma shape parameter, alpha&#039;&#039;&#039;&lt;br /&gt;
: Recall that the idea in MCMCMC sampling is to move around in parameter space in such a way that points are visited according to their posterior probability (i.e., regions with high posterior probability are visited frequently). Now, in RStudio, plot the sampled values for the gamma shape parameter, alpha, for one of the run files:&lt;br /&gt;
 df_primates = read_tsv(&amp;quot;primatemitDNA.nexus.run1.p&amp;quot;, skip=1)&lt;br /&gt;
 mcmc_trace(df_primates, pars=&amp;quot;alpha&amp;quot;)&lt;br /&gt;
: mcmc_trace is one of several plotting commands available in the bayesplot package. This command plots the sampled values of the parameter alpha from the first of the two parallel runs against MCMC generation number. Thus, the x-axis shows the progress of the run through time, with the leftmost values being the earliest samples and the rightmost values the later ones. Note how the Markov chain starts at the arbitrary value 1.0, rapidly moves to values that fit the observed data better, and then moves around in parameter space, sampling different plausible values of alpha. You can experiment with plotting other columns as well.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Question: &#039;&#039;&#039;Describe briefly what happens to the sampled values of alpha during the run. Why is it reasonable to discard the earliest samples as burn-in?&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Question 4&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Investigate posterior probability distribution over trees&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
: Now, close the nedit window and have a look at the file containing sampled trees:&lt;br /&gt;
 nedit primatemitDNA.nexus.run1.t &amp;amp;&lt;br /&gt;
: Tree topology is also a parameter in our model, and exactly like for the other parameters we also get samples from tree-space. One tree is printed per line in the parenthetical Newick format you have seen before. There are 5 taxa in the present data set, so the number of possible unrooted binary tree topologies is only 15. Since we have taken more than 15 sample points, there must be several lines containing the same tree topology. Close the nedit window when you are done.&lt;br /&gt;
&lt;br /&gt;
: MrBayes provides the sumt command to summarize the sampled trees. Before using it, we need to decide on the burn-in: The burn-in is the initial set of samples that are typically discarded, because we want to ensure that the MCMC has moved away from the random starting values, and has found the peaks of the probability landscape. Since the convergence diagnostic used a relative burn-in of 25%, we will also discard the first 25% of tree samples when summarizing the posterior.&lt;br /&gt;
&lt;br /&gt;
: Return to the shell window where you have MrBayes running. In the command below relburnin=yes and burninfrac=0.25 tells MrBayes to discard 25% of the samples as burnin (you could also have explicitly given the number of samples to discard - help sumt will give you details about the command and the current option settings).&lt;br /&gt;
 sumt contype=halfcompat conformat=simple relburnin=yes burninfrac=0.25 showtreeprobs=yes&lt;br /&gt;
: (Scroll back so you can see the top of the output when the command is done). This command gives you a summary of the trees that are in the file you examined manually above. The option contype=halfcompat requests that a majority rule consensus tree is calculated from the set of trees that are left after discarding the burnin. This consensus is the first tree plotted to the screen. Below the consensus cladogram, a consensus phylogram is plotted. The branch lengths in this have been averaged over the trees in which that branch was present (a particular branch corresponds to a bi-partition of the data, and will typically not be present in every sampled tree). The cladogram also has &amp;quot;clade credibility&amp;quot; values. We will return to the meaning of these later in today&#039;s exercise.&lt;br /&gt;
&lt;br /&gt;
: What most interests us right now is the list of trees that is printed after the phylogram. These trees are labeled &amp;quot;Tree 1&amp;quot;, &amp;quot;Tree 2&amp;quot;, etc, and are sorted according to their posterior probability which is indicated by a lower-case p after the tree number. (The upper-case P gives the cumulated probability of trees shown so far, and is useful for constructing a credible set). This list highlights how Bayesian phylogenetic analysis is different from maximum likelihood: Instead of finding the best tree(s), we here quantify our degree of belief in all possible trees.&lt;br /&gt;
&lt;br /&gt;
: The list of trees and probabilities was printed because of the option showtreeprobs=yes. Note that you probably do not want to issue that command if you have much more than 5 taxa! In that case you could instead inspect the file named primatemitDNA.nexus.trprobs which is now present in the same directory as your other files (this file is automatically produced by the sumt command).&lt;br /&gt;
&lt;br /&gt;
: &#039;&#039;&#039;NOTE&#039;&#039;&#039;: Annoyingly, there is a bug in the version of mrbayes we are using here, which means leaf names are not printed on the list of trees with probabilities. However, the most probable tree here in fact is identical to the consensus tree printed above it.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Question: &#039;&#039;&#039;What is the posterior probability of the most probable tree? Does the analysis strongly support a single tree, or is the posterior probability distributed across several different trees?&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
== Analysis of Neanderthal data (posterior probability of clades) ==&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Question 5&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
For many years, there was considerable debate about the origin of modern humans. One view, often called the [https://www.thoughtco.com/multiregional-hypothesis-167235 Multiregional Hypothesis], proposed that after Homo erectus spread from Africa into different parts of the world, regional populations gradually evolved into modern humans more or less in parallel. A different view, often called the Recent African Origin model, proposed that modern Homo sapiens evolved in Africa and later spread outward, largely replacing other archaic human groups such as the Neanderthals.&lt;br /&gt;
&lt;br /&gt;
Today it is clear that the history is more complicated than either simple extreme: modern humans arose in Africa, but there was also some interbreeding with Neanderthals and other archaic humans. However, in this exercise we will focus on a narrower question that can be addressed using a phylogeny of mitochondrial DNA: do the sampled Neanderthal and human mitochondrial sequences suggest that the Neanderthal sequence falls inside or outside modern human mitochondrial diversity?&lt;br /&gt;
&lt;br /&gt;
We will use the present data set to examine this question.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Load Neanderthal data set&#039;&#039;&#039;&lt;br /&gt;
: In the Terminal where you have MrBayes running:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
execute neanderthal.nexus&lt;br /&gt;
delete 5-40&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
: As we did for the maximum likelihood analysis, we will discard some of the human sequences in order to speed up the analysis. The command delete 5-40 removes sequence number 5 to sequence number 40 from the active data set.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Investigate data&#039;&#039;&#039;&lt;br /&gt;
 showmatrix&lt;br /&gt;
: This data set consists of an alignment of mitochondrial DNA from human (17 sequences), chimpanzee (1 sequence), and Neanderthal (1 sequence). The Neanderthal DNA was extracted from archaeological material, specifically bones found at Vindija in Croatia.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Start analysis&#039;&#039;&#039;&lt;br /&gt;
 outgroup Pan_troglodytes&lt;br /&gt;
 lset nst=mixed rates=gamma&lt;br /&gt;
 mcmc ngen=500000 nchains=3 diagnfreq=10000&lt;br /&gt;
&lt;br /&gt;
: Here we use the command `nst=mixed` which allows MrBayes to automatically explore all possible substitution models. Essentially, MrBayes now considers the substitution model as one more parameter, and uses MCMC to sample from the possible versions (with nst ranging from 1 to 6). This will often be the best choice when using MrBayes. (Below, I use nst=6 for pedagogical purposes, because it makes it simpler to analyse the output files).&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Find posterior probability of clades&#039;&#039;&#039;&lt;br /&gt;
: When the run has finished, issue this command to compute a consensus tree:&lt;br /&gt;
 sumt contype=halfcompat showtreeprobs=no relburnin=yes burninfrac=0.25&lt;br /&gt;
: Examine the consensus tree that is plotted to screen: On the branches that are resolved, you will notice that numbers have been plotted. These are clade-credibility values, and are in fact the posterior probability that the clade is real (based on the present data set).&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Question: &#039;&#039;&#039;What is the posterior probability that all sampled Homo sapiens sequences form a monophyletic group excluding the Neanderthal sequence? Does this support placing the Neanderthal outside modern human mitochondrial diversity?&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
== Probability distributions over other parameters ==&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Question 6&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
: As the last thing, we will now turn away from the tree topology, and instead examine the other parameters that also form part of the probabilistic model. We will do this using a reduced version of the Hepatitis C virus data set that we have examined previously. Stay in the shell window where you just performed the analysis of Neanderthal sequences.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Load data set&#039;&#039;&#039;&lt;br /&gt;
 execute hcvsmall.nexus&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Define site partition&#039;&#039;&#039;&lt;br /&gt;
 charset 1stpos=1-.\3&lt;br /&gt;
 charset 2ndpos=2-.\3&lt;br /&gt;
 charset 3rdpos=3-.\3&lt;br /&gt;
 partition bycodon = 3:1stpos,2ndpos,3rdpos&lt;br /&gt;
 set partition=bycodon&lt;br /&gt;
 prset ratepr=variable&lt;br /&gt;
: This is an alternative way of specifying that different sites may evolve at different rates. With a gamma model, we allow rates to vary across sites but do not specify in advance which sites are fast or slow; instead, that pattern is inferred from the data. Here we instead use prior biological knowledge about the structure of the genetic code to divide sites into three classes: 1st, 2nd, and 3rd codon positions. We then allow each class to have its own rate, so that all 1st positions share one rate, all 2nd positions another, and all 3rd positions a third. Specifically, charset 1stpos=1-.\3 defines a character set named 1stpos consisting of site 1 followed by every third site (\3, i.e. sites 1, 4, 7, 10, …), continuing until the end of the alignment (denoted .).&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Specify model&#039;&#039;&#039;&lt;br /&gt;
 lset nst=6&lt;br /&gt;
: This specifies that we want to use a model of the General Time Reversible (GTR) type, where all 6 substitution types have separate rate parameters.&lt;br /&gt;
&lt;br /&gt;
: When the lset command was discussed previously, a few issues were glossed over. Importantly, and unlike PAUP, the lset command in MrBayes gives no information about whether nucleotide frequencies are equal or not, and whether they should be estimated from the data or not. In MrBayes this is instead controlled by defining the prior probability of the nucleotide frequencies (the command prset can be used to set priors). For instance, a model with equal nucleotide frequencies corresponds to having prior probability 1 (one) for the frequency vector (A=0.25, C=0.25, G=0.25, T=0.25), and zero prior probability for the infinitely many other possible frequency vectors. As you will see below, the default prior is not this limited, and the program will therefore estimate the frequencies from the data.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Inspect model details&#039;&#039;&#039;&lt;br /&gt;
 showmodel&lt;br /&gt;
: This command gives you a summary of the current model settings. You will also get a summary of how the prior probabilities of all model parameters are set. You will for instance notice that the nucleotide frequencies (parameter labeled &amp;quot;Statefreq&amp;quot;) have a &amp;quot;Dirichlet&amp;quot; prior. Without going into details, the Dirichlet distribution is a probability distribution over frequency vectors (i.e., vectors of positive values that sum to 1). Depending on the exact parameters the distribution can be more or less flat (flat here means that all sum-to-1 vectors are equally probable). The Dirichlet distribution is a handy way of specifying the prior probability distribution of nucleotide (or amino acid) frequency vectors. The default statefreq prior in MrBayes is the flat or un-informative prior dirichlet(1,1,1,1).&lt;br /&gt;
&lt;br /&gt;
: We will not go into the priors for the remaining parameters in any detail, but you may notice that by default all topologies are taken to be equally likely (a flat prior on trees).&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Start MCMC sampling&#039;&#039;&#039;&lt;br /&gt;
 mcmc ngen=1000000 samplefreq=100 diagnfreq=10000 nchains=3&lt;br /&gt;
: The run will take a while to finish (you may want to ensure that the average standard deviation of split frequencies is less than 0.01 before ending the analysis).&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Compute summary of parameter values&#039;&#039;&#039;&lt;br /&gt;
 sump relburnin=yes burninfrac=0.25&lt;br /&gt;
: The sump command (with a &amp;quot;p&amp;quot; at the end) works much like the sumt command (with a &amp;quot;t&amp;quot; at the end), but for other parameters than the tree-topology. Again, we are using 25% of the total number of samples as burnin.&lt;br /&gt;
&lt;br /&gt;
: First, you get a scatter plot of the lnL as a function of generation number. Values from the two independent runs are labeled &amp;quot;1&amp;quot; and &amp;quot;2&amp;quot; respectively. If the burnin is suitable, then the points should be randomly scattered over a narrow lnL interval.&lt;br /&gt;
&lt;br /&gt;
: Secondly, the posterior probability distribution of each parameter is summarized by giving the mean, variance, median, and 95% credible interval.&lt;br /&gt;
&lt;br /&gt;
: The last columns contain values indicating whether the run has converged. Specifically, ESS means Effective Sample Size, and measures how many effectively independent samples you have from the posterior — the higher the better, but this should be at least 100. [https://sites.stat.columbia.edu/gelman/research/published/brooksgelman2.pdf The column labeled PSRF+ gives another convergence diagnostic (also known as “R-hat”)] and should be close to 1 if the runs have converged. Specifically, it measures whether different chains (and different parts of the chains) are sampling the same distribution of values. As a rule of thumb, values less than 1.05 are good, values between 1.05 and 1.10 are acceptable, and values above 1.10 suggest poor convergence.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Question: &#039;&#039;&#039;What are the posterior mean values of the relative substitution rate parameters r(AC) and r(CG)?&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Question 7: &#039;&#039;&#039; Based on the reported posterior means, does r(CG) appear to be larger than r(AC)?&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Question 8&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Marginal vs. joint distributions&#039;&#039;&#039;&lt;br /&gt;
: Strictly speaking the comparison above was not entirely appropriate. We first found the overall distribution of the r(CG) parameter and then compared its mean to the mean of the overall distribution of the r(AC) parameter. By doing things this way, we are ignoring the possibility that the two parameters might be associated in some way. For instance, one parameter might always be larger than the other in any individual sample, even though the total distributions overlap. We should instead be looking at the distribution over both parameters simultaneously. A probability distribution over several parameters simultaneously is called a &amp;quot;joint distribution&amp;quot; over the parameters.&lt;br /&gt;
&lt;br /&gt;
: By looking at one parameter at a time, we are summing its probability over all values of the other parameters. This is called the marginal distribution.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Examine marginal distributions&#039;&#039;&#039;&lt;br /&gt;
: In RStudio, use the following commands to read and plot the marginal distributions of r(AC) and r(CG). Note that we are discarding the first 25% of the reads as burnin&lt;br /&gt;
 df_hcv = read_tsv(&amp;quot;hcvsmall.nexus.run1.p&amp;quot;, skip=1)&lt;br /&gt;
 burnin = df_hcv$Gen %&amp;gt;% &lt;br /&gt;
     max() %&amp;gt;% &lt;br /&gt;
     multiply_by(0.25) %&amp;gt;% &lt;br /&gt;
     floor()&lt;br /&gt;
 df_hcv2 = df_hcv %&amp;gt;% &lt;br /&gt;
     filter(Gen &amp;gt; burnin) %&amp;gt;%&lt;br /&gt;
     select(CG = `r(C&amp;lt;-&amp;gt;G){all}`,&lt;br /&gt;
            AC = `r(A&amp;lt;-&amp;gt;C){all}`&lt;br /&gt;
            )&lt;br /&gt;
 mcmc_intervals(df_hcv2, prob_outer = 1)&lt;br /&gt;
 mcmc_areas(df_hcv2, prob_outer = 1)&lt;br /&gt;
: The functions mcmc_intervals and mcmc_areas plot different views of the same posterior distributions. &lt;br /&gt;
&lt;br /&gt;
: You can also simply plot the data using ggplot:&lt;br /&gt;
 df_hcv2_long = pivot_longer(df_hcv2, cols = c(&amp;quot;CG&amp;quot;, &amp;quot;AC&amp;quot;))&lt;br /&gt;
 &lt;br /&gt;
 ggplot(df_hcv2_long) + &lt;br /&gt;
     geom_density(mapping=aes(x=value, fill=name), alpha=0.3) + &lt;br /&gt;
     labs(x=&amp;quot;Substitution rate&amp;quot;)&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Question&#039;&#039;&#039;: Based on the marginal distributions, r(AC) appears to be centered at a higher value than r(CG), but the two distributions overlap somewhat. Can you from these marginal distributions alone decide whether r(AC) is larger than r(CG) in most posterior samples? Why or why not?&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Question 9&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Examine joint distributions&#039;&#039;&#039;&lt;br /&gt;
: These plots and results explore the relationship between the A&amp;lt;-&amp;gt;C and C&amp;lt;-&amp;gt;G rates.&lt;br /&gt;
 ggplot(df_hcv2, aes(x=CG, y=AC)) + &lt;br /&gt;
     geom_point(col=&amp;quot;blue&amp;quot;) + &lt;br /&gt;
     geom_abline(intercept=0, slope=1, lty=2, col=&amp;quot;red&amp;quot;) +&lt;br /&gt;
     xlim(0,0.25) + &lt;br /&gt;
     ylim(0,0.25) + &lt;br /&gt;
     labs(x=&amp;quot;CG rate&amp;quot;, y =&amp;quot;AC rate&amp;quot;) &lt;br /&gt;
 &lt;br /&gt;
 ggplot(df_hcv2, aes(x=CG, y= AC)) + &lt;br /&gt;
     geom_hex(col=&amp;quot;blue&amp;quot;) + &lt;br /&gt;
     geom_abline(intercept=0, slope=1, lty=2, col=&amp;quot;red&amp;quot;) +&lt;br /&gt;
     xlim(0,0.25) + &lt;br /&gt;
     ylim(0, 0.25) + &lt;br /&gt;
     labs(x=&amp;quot;CG rate&amp;quot;, y =&amp;quot;AC rate&amp;quot;) &lt;br /&gt;
 &lt;br /&gt;
 df_hcv2 %&amp;gt;%&lt;br /&gt;
     nrow()&lt;br /&gt;
 df_hcv2 %&amp;gt;%&lt;br /&gt;
     filter(AC&amp;gt;CG) %&amp;gt;%&lt;br /&gt;
     nrow()&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Question: &#039;&#039;&#039;Based on the two different ways to plot the joint distribution and based on the unfiltered and filtered row counts, what is the posterior probability that r(AC) &amp;gt; r(CG)?&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Question 10&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
: Note how examining the joint distribution provides you with information that you could not get from simply comparing the marginal distributions. This very simple procedure can be used to answer many different questions.&lt;br /&gt;
&lt;br /&gt;
: Now, plot the relative substitution rates at the first, second, and third codon positions:&lt;br /&gt;
 df_hcv3 = df_hcv %&amp;gt;% &lt;br /&gt;
     filter(Gen &amp;gt; 75000) %&amp;gt;%&lt;br /&gt;
     select(Codon_1st = `m{1}`,&lt;br /&gt;
            Codon_2nd = `m{2}`,&lt;br /&gt;
            Codon_3rd = `m{3}` ) %&amp;gt;%&lt;br /&gt;
     pivot_longer(cols=c(&amp;quot;Codon_1st&amp;quot;, &amp;quot;Codon_2nd&amp;quot;, &amp;quot;Codon_3rd&amp;quot;))&lt;br /&gt;
 &lt;br /&gt;
 ggplot(df_hcv3) + &lt;br /&gt;
     geom_density(mapping=aes(x=value, fill=name), alpha=0.3) + &lt;br /&gt;
     labs(x=&amp;quot;Relative substitution rate&amp;quot;)&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Question: &#039;&#039;&#039;Since random mutations presumably hit all three codon positions with the same frequency, any differences are expected to be caused by subsequent selection. Which of the following statement are correct ? (More than one answer may be correct)&lt;br /&gt;
&lt;br /&gt;
:* Codon position 2 is the most variable of the codon positions.&lt;br /&gt;
:* Codon position 1 is the most variable of the codon positions.&lt;br /&gt;
:* Codon position 1 is the most conserved codon position.&lt;br /&gt;
:* Codon position 3 is the most conserved codon position.&lt;br /&gt;
:* Codon position 3 is the most variable of the codon positions.&lt;br /&gt;
:* Codon position 2 is the most conserved codon position.&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Question 11&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Question: &#039;&#039;&#039;How does this result fit with your knowledge of the genetic code? Why are these codon positions the most conserved or the most variable?&lt;/div&gt;</summary>
		<author><name>Gorm</name></author>
	</entry>
	<entry>
		<id>https://teaching.healthtech.dtu.dk/22115/index.php?title=Bayesian_Phylogeny&amp;diff=252</id>
		<title>Bayesian Phylogeny</title>
		<link rel="alternate" type="text/html" href="https://teaching.healthtech.dtu.dk/22115/index.php?title=Bayesian_Phylogeny&amp;diff=252"/>
		<updated>2026-03-19T10:37:00Z</updated>

		<summary type="html">&lt;p&gt;Gorm: /* Probability distributions over other parameters */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;This exercise is part of the course  [[22115_-_Computational_Molecular_Evolution|Computational Molecular Evolution (22115)]].&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Overview ==&lt;br /&gt;
&lt;br /&gt;
Today&#039;s exercise will focus on phylogenetic analysis using Bayesian methods.&lt;br /&gt;
&lt;br /&gt;
As was the case for likelihood methods, Bayesian analysis is founded on having a probabilistic model of how the observed data is produced. This means that, for a given set of parameter values, you can compute the probability or [https://www.statlect.com/glossary/probability-density-function probability density] of any possible observation. For a full dataset, you then obtain the likelihood by multiplying these values across all observations. You will recall from the lecture that in Bayesian statistics the goal is to obtain a full posterior probability distribution over all possible parameter values. The posterior distribution quantifies our degree of belief in any possible parameter value after seeing the data. It is obtained by updating the prior probability distribution using the likelihood of the observed data.&lt;br /&gt;
&lt;br /&gt;
The prior probability distribution expresses your beliefs about the parameters before seeing any data, while the likelihood expresses what the observed data are telling you about the parameters. Specifically, the likelihood of a parameter value is the probability of the observed data given that parameter value. We regard a parameter value as more plausible the more probable it makes the observed data. This is the same measure we have previously used to find the maximum likelihood estimate. If the prior probability distribution is flat (i.e., if all possible parameter values have the same prior probability), then the posterior distribution is proportional to the likelihood, and the parameter value with the maximum likelihood also has the maximum posterior probability. However, even in this case, using a Bayesian approach still lets you interpret the result as a probability distribution over parameter values. &lt;br /&gt;
&lt;br /&gt;
If the prior is not flat, then it may have a substantial impact on the posterior, although this effect will usually diminish as the amount of data increases. A prior should ideally be based on domain knowledge and results from previous experiments. For instance one can use the posterior from one analysis as the prior in a new, independent analysis. Often a prior is chosen to be weakly informative, meaning that it places reasonable bounds on the parameter values without constraining them too narrowly. For instance the transition/transversion rate ratio kappa is typically 1.5-10. Values such as 100, 1,000 or 1,000,000 would be extremely unlikely, so a weakly informative prior for this parameter could be chosen to place 95% of its probability mass in the 0.5-20 range, slightly wider than what we think of as plausible values. For instance one could use a lognormal distribution with suitable parameters.&lt;br /&gt;
&lt;br /&gt;
In Bayesian phylogeny the parameters are of the same kind as in maximum likelihood phylogeny. Typical parameters include tree topology, branch lengths, nucleotide frequencies, and substitution model parameters such as the transition/transversion rate ratio or the gamma shape parameter. The difference is that, whereas in maximum likelihood phylogeny we seek the best point estimates of the parameter values, in Bayesian phylogeny the goal is instead to infer a full probability distribution over the possible parameter values. The observed data are again usually taken to be the alignment, although strictly speaking it would be more reasonable to say that the sequences are what have been observed, and that the alignment should then be inferred jointly with the phylogeny.&lt;br /&gt;
&lt;br /&gt;
In this exercise we will explore how one can determine and use posterior probability distributions over trees, over clades, and over substitution parameters. We will also touch upon the difference between marginal and joint probability distributions.&lt;br /&gt;
&lt;br /&gt;
== Getting started ==&lt;br /&gt;
&lt;br /&gt;
: In the command below: Instead of /path/to/molevol enter the path to the directory where you have placed your course files (for instance cd /Users/bob/Documents/molevol, or cd /home/student/molevol).&lt;br /&gt;
 cd /path/to/molevol&lt;br /&gt;
 mkdir bayes&lt;br /&gt;
 cd bayes&lt;br /&gt;
 cp ../data/primatemitDNA.nexus ./primatemitDNA.nexus&lt;br /&gt;
 cp ../data/neanderthal.nexus ./neanderthal.nexus&lt;br /&gt;
 cp ../data/hcvsmall.nexus ./hcvsmall.nexus&lt;br /&gt;
&lt;br /&gt;
: You have analyzed (versions of) all these data files previously in this course. We will now use Bayesian phylogenetic analysis to complement what we learned in those analyses.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Load R libraries&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
: In RStudio: set the working directory to the bayes directory. Then issue these commands:&lt;br /&gt;
 library(magrittr)&lt;br /&gt;
 library(tidyverse)&lt;br /&gt;
 library(bayesplot)&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
== Posterior probability of trees ==&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Question 1&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
: In today&#039;s exercise we will be using the program &amp;quot;MrBayes&amp;quot; to perform Bayesian phylogenetic analysis. MrBayes is a program that, like PAUP*, can be controlled by giving commands at a command line prompt. In fact, there is a substantial overlap between the commands used to control MrBayes and the PAUP command language. This should be a help when you are trying to understand how to use the program.&lt;br /&gt;
&lt;br /&gt;
: Note that the command &amp;quot;help&amp;quot; will give you a list of all available commands. Issuing &amp;quot;help &#039;&#039;command&#039;&#039;&amp;quot; will give you a more detailed description of the specified command along with current option values. This is similar to how &amp;quot;help &#039;&#039;command&#039;&#039;&amp;quot; works in PAUP.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Start program&#039;&#039;&#039;&lt;br /&gt;
: In a terminal window, issue the command:&lt;br /&gt;
 mb&lt;br /&gt;
: This starts the program, giving you a prompt (&amp;quot;MrBayes&amp;gt; &amp;quot;) where you can enter commands.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Get a quick overview of available commands&#039;&#039;&#039;&lt;br /&gt;
 help&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Load your sequences&#039;&#039;&#039;&lt;br /&gt;
 execute primatemitDNA.nexus&lt;br /&gt;
: This file contains mitochondrial DNA sequences from 5 different primates. Note that MrBayes accepts input in nexus format, and that this is the same command that was used to load sequences in PAUP*. In general, you can use many of the PAUP commands in MrBayes also.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Inspect data set&#039;&#039;&#039;&lt;br /&gt;
 showmatrix&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Define outgroup&#039;&#039;&#039;&lt;br /&gt;
 outgroup Gibbon&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Specify your model of sequence evolution&#039;&#039;&#039;&lt;br /&gt;
 lset nst=2 rates=gamma&lt;br /&gt;
: This command is again very much like the corresponding one in PAUP. You are specifying that you want to use a model with two substitution types (nst=2), and this is automatically taken to mean that you want to distinguish between transitions and transversions. Furthermore, rates=gamma means that you want the model to use a gamma distribution to account for different rates at different sites in the sequence.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Start Markov chain Monte Carlo sampling&#039;&#039;&#039;&lt;br /&gt;
:Make sure to make the shell window as wide as possible and then issue the following commands to start the run:&lt;br /&gt;
 mcmc ngen=1000000 samplefreq=100 nchains=3 diagnfreq=5000&lt;br /&gt;
: What you are doing here is to use the method known as MCMCMC (&amp;quot;Metropolis-coupled Markov chain Monte Carlo&amp;quot;) to empirically determine the posterior probability distribution of trees, branch lengths and substitution parameters. Recall that in the Bayesian framework this is how we learn about parameter values: instead of finding the best point estimates, we typically want to quantify the probability of the entire range of possible values. An estimate of the time left is shown in the last column of output.&lt;br /&gt;
&lt;br /&gt;
: Let us examine the command in detail. First, ngen=1000000 samplefreq=100 lets the search run for 1,000,000 MCMC steps (&amp;quot;generations&amp;quot;) and saves parameter values once every 100 rounds (meaning that a total of 10,000 sets of parameter values will be saved to sample files). You sometimes need to run longer (or shorter) than 1,000,000, and would then typically tweak samplefreq so you get around 1,000 - 10,000 samples in all. The option nchains=3 means that the MCMCMC sampling uses 3 parallel chains (but see below): one &amp;quot;cold&amp;quot; from which sampling takes place, and two &amp;quot;heated&amp;quot; that move around in the parameter space more quickly to find additional peaks in the probability distribution.&lt;br /&gt;
&lt;br /&gt;
: The option diagnfreq=5000 has to do with testing whether the MrBayes run is successful. Briefly, MrBayes will start two entirely independent runs starting from different random trees. In the early phases of the run, the two runs will sample very different trees, but when they have reached convergence (when they produce a good sample from the posterior probability distribution), the two tree samples should be very similar. Every diagnfreq generations, the program will compute a measure of how similar the tree samples are, specifically the average standard deviation of split frequencies. A “split” is the same as a bipartition, i.e. a division of all leaves in the tree into two groups, obtained by cutting an internal branch. For each split, MrBayes compares how often that split occurs in the two independent runs; if the runs have converged, these frequencies should be very similar, giving a small standard deviation. The program then averages this quantity across splits. As a rule of thumb, you may want to run until this value is less than 0.05 (the smaller the better)&lt;br /&gt;
&lt;br /&gt;
: During the run you will see reports about the progress of the two independent runs, each consisting of three chains. Each line of output lists the generation number and the log likelihoods of the current tree/parameter combination for each of the two groups of three chains (a column of asterisks separate the results for the independent runs). The cold chains are the ones enclosed in brackets [...], while the heated chains are enclosed in parentheses (...). Occasionally the chains will swap so one of the heated chains now becomes cold (and sampling then takes place from this chain).&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Continue run until parallel runs converge on same solution&#039;&#039;&#039;&lt;br /&gt;
:At the end of the run, Mrbayes will print the average standard deviation of split frequencies (which is a measure of how similar the tree samples of the two independent runs are). We recommend that you continue with the analysis until the value gets below 0.01 (if the value is larger than 0.01 then you should answer &amp;quot;yes&amp;quot; when the program asks &amp;quot;Continue the analysis? (yes/no)&amp;quot;.)&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Question: &#039;&#039;&#039;MrBayes starts two independent runs from different random trees. Why is it useful to run two independent analyses instead of just one? How does the average standard deviation of split frequencies help you decide whether the two runs have converged to the same posterior distribution? At approximately how many generations does this happen in your run?&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Question 2&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Have a look at the resulting sample files&#039;&#039;&#039;&lt;br /&gt;
: Open a new Terminal window (don&#039;t quit mrbayes in the other terminal yet!) and cd to the bayes directory. Open one of the parameter sampling files in a text editor:&lt;br /&gt;
 nedit primatemitDNA.nexus.run1.p &amp;amp;&lt;br /&gt;
: This file contains one line for each sampled point (you may want to turn off line-wrapping in nedit under the preferences menu). Each row corresponds to a certain sample time (or generation). Each column contains the sampled values of one specific parameter. The first line contains headings telling what the different columns are: &lt;br /&gt;
:* Gen: generation; number of MCMC steps taken so far&lt;br /&gt;
:* lnL: log likelihood of the current parameter estimates&lt;br /&gt;
:* LnPr: log of the prior probability&lt;br /&gt;
:* TL: tree length (sum of all branch lengths)&lt;br /&gt;
:* kappa: transition/transversion rate ratio&lt;br /&gt;
:* pi(A), pi(C), pi(G), pi(T): frequency of A, C, G, T&lt;br /&gt;
:* alpha: shape parameter for the gamma distribution. &lt;br /&gt;
&lt;br /&gt;
: (Column headings may be shifted relative to their corresponding columns). Note how the values of most parameters change a lot during the initial &amp;quot;burnin&amp;quot; period, before they settle near their most probable values. &lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Question: &#039;&#039;&#039;You will notice that lnL is always negative, while LnPr can sometimes be positive. At first sight this may seem impossible, since probabilities cannot be larger than 1. How can this happen?&lt;br /&gt;
&lt;br /&gt;
As a hint, note that (1) priors for continuous parameters are probability densities, and (2) the default prior for each branch length in MrBayes is an exponential distribution with rate 10. Use the following R code to plot this prior on both an ordinary y-axis and a log-scaled y-axis, and then explain why positive values of LnPr are possible.&lt;br /&gt;
&lt;br /&gt;
 df_expdist = tibble(&lt;br /&gt;
   x = seq(0, 1, by = 0.001),&lt;br /&gt;
   density = dexp(x, rate = 10),&lt;br /&gt;
   logdensity = log(dexp(x, rate = 10))&lt;br /&gt;
 )&lt;br /&gt;
 &lt;br /&gt;
 ggplot(df_expdist, aes(x = x, y = density)) +&lt;br /&gt;
   geom_line(color=&amp;quot;blue&amp;quot;) +&lt;br /&gt;
   geom_hline(yintercept = 1, lty=2) +&lt;br /&gt;
   labs(&lt;br /&gt;
     x = &amp;quot;Branch length&amp;quot;,&lt;br /&gt;
     y = &amp;quot;Probability density&amp;quot;,&lt;br /&gt;
     title = &amp;quot;Exponential prior on branch lengths: Exp(rate = 10)&amp;quot;&lt;br /&gt;
   )&lt;br /&gt;
 &lt;br /&gt;
 ggplot(df_expdist, aes(x = x, y = logdensity)) +&lt;br /&gt;
   geom_line(color=&amp;quot;blue&amp;quot;) +&lt;br /&gt;
   geom_hline(yintercept = 0, lty=2) +&lt;br /&gt;
   labs(&lt;br /&gt;
     x = &amp;quot;Branch length&amp;quot;,&lt;br /&gt;
     y = &amp;quot;log of Probability density&amp;quot;,&lt;br /&gt;
     title = &amp;quot;Exponential prior on branch lengths: Exp(rate = 10)&amp;quot;&lt;br /&gt;
   )&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Question 3&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Examine MCMC trajectory for gamma shape parameter, alpha&#039;&#039;&#039;&lt;br /&gt;
: Recall that the idea in MCMCMC sampling is to move around in parameter space in such a way that points are visited according to their posterior probability (i.e., regions with high posterior probability are visited frequently). Now, in RStudio, plot the sampled values for the gamma shape parameter, alpha, for one of the run files:&lt;br /&gt;
 df_primates = read_tsv(&amp;quot;primatemitDNA.nexus.run1.p&amp;quot;, skip=1)&lt;br /&gt;
 mcmc_trace(df_primates, pars=&amp;quot;alpha&amp;quot;)&lt;br /&gt;
: mcmc_trace is one of several plotting commands available in the bayesplot package. This command plots the sampled values of the parameter alpha from the first of the two parallel runs against MCMC generation number. Thus, the x-axis shows the progress of the run through time, with the leftmost values being the earliest samples and the rightmost values the later ones. Note how the Markov chain starts at the arbitrary value 1.0, rapidly moves to values that fit the observed data better, and then moves around in parameter space, sampling different plausible values of alpha. You can experiment with plotting other columns as well.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Question: &#039;&#039;&#039;Describe briefly what happens to the sampled values of alpha during the run. Why is it reasonable to discard the earliest samples as burn-in?&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Question 4&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Investigate posterior probability distribution over trees&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
: Now, close the nedit window and have a look at the file containing sampled trees:&lt;br /&gt;
 nedit primatemitDNA.nexus.run1.t &amp;amp;&lt;br /&gt;
: Tree topology is also a parameter in our model, and exactly like for the other parameters we also get samples from tree-space. One tree is printed per line in the parenthetical Newick format you have seen before. There are 5 taxa in the present data set, so the number of possible unrooted binary tree topologies is only 15. Since we have taken more than 15 sample points, there must be several lines containing the same tree topology. Close the nedit window when you are done.&lt;br /&gt;
&lt;br /&gt;
: MrBayes provides the sumt command to summarize the sampled trees. Before using it, we need to decide on the burn-in: The burn-in is the initial set of samples that are typically discarded, because we want to ensure that the MCMC has moved away from the random starting values, and has found the peaks of the probability landscape. Since the convergence diagnostic used a relative burn-in of 25%, we will also discard the first 25% of tree samples when summarizing the posterior.&lt;br /&gt;
&lt;br /&gt;
: Return to the shell window where you have MrBayes running. In the command below relburnin=yes and burninfrac=0.25 tells MrBayes to discard 25% of the samples as burnin (you could also have explicitly given the number of samples to discard - help sumt will give you details about the command and the current option settings).&lt;br /&gt;
 sumt contype=halfcompat conformat=simple relburnin=yes burninfrac=0.25 showtreeprobs=yes&lt;br /&gt;
: (Scroll back so you can see the top of the output when the command is done). This command gives you a summary of the trees that are in the file you examined manually above. The option contype=halfcompat requests that a majority rule consensus tree is calculated from the set of trees that are left after discarding the burnin. This consensus is the first tree plotted to the screen. Below the consensus cladogram, a consensus phylogram is plotted. The branch lengths in this have been averaged over the trees in which that branch was present (a particular branch corresponds to a bi-partition of the data, and will typically not be present in every sampled tree). The cladogram also has &amp;quot;clade credibility&amp;quot; values. We will return to the meaning of these later in today&#039;s exercise.&lt;br /&gt;
&lt;br /&gt;
: What most interests us right now is the list of trees that is printed after the phylogram. These trees are labeled &amp;quot;Tree 1&amp;quot;, &amp;quot;Tree 2&amp;quot;, etc, and are sorted according to their posterior probability which is indicated by a lower-case p after the tree number. (The upper-case P gives the cumulated probability of trees shown so far, and is useful for constructing a credible set). This list highlights how Bayesian phylogenetic analysis is different from maximum likelihood: Instead of finding the best tree(s), we here quantify our degree of belief in all possible trees.&lt;br /&gt;
&lt;br /&gt;
: The list of trees and probabilities was printed because of the option showtreeprobs=yes. Note that you probably do not want to issue that command if you have much more than 5 taxa! In that case you could instead inspect the file named primatemitDNA.nexus.trprobs which is now present in the same directory as your other files (this file is automatically produced by the sumt command).&lt;br /&gt;
&lt;br /&gt;
: &#039;&#039;&#039;NOTE&#039;&#039;&#039;: Annoyingly, there is a bug in the version of mrbayes we are using here, which means leaf names are not printed on the list of trees with probabilities. However, the most probable tree here in fact is identical to the consensus tree printed above it.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Question: &#039;&#039;&#039;What is the posterior probability of the most probable tree? Does the analysis strongly support a single tree, or is the posterior probability distributed across several different trees?&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
== Analysis of Neanderthal data (posterior probability of clades) ==&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Question 5&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
For many years, there was considerable debate about the origin of modern humans. One view, often called the [https://www.thoughtco.com/multiregional-hypothesis-167235 Multiregional Hypothesis], proposed that after Homo erectus spread from Africa into different parts of the world, regional populations gradually evolved into modern humans more or less in parallel. A different view, often called the Recent African Origin model, proposed that modern Homo sapiens evolved in Africa and later spread outward, largely replacing other archaic human groups such as the Neanderthals.&lt;br /&gt;
&lt;br /&gt;
Today it is clear that the history is more complicated than either simple extreme: modern humans arose in Africa, but there was also some interbreeding with Neanderthals and other archaic humans. However, in this exercise we will focus on a narrower question that can be addressed using a phylogeny of mitochondrial DNA: do the sampled Neanderthal and human mitochondrial sequences suggest that the Neanderthal sequence falls inside or outside modern human mitochondrial diversity?&lt;br /&gt;
&lt;br /&gt;
We will use the present data set to examine this question.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Load Neanderthal data set&#039;&#039;&#039;&lt;br /&gt;
: In the Terminal where you have MrBayes running:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
execute neanderthal.nexus&lt;br /&gt;
delete 5-40&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
: As we did for the maximum likelihood analysis, we will discard some of the human sequences in order to speed up the analysis. The command delete 5-40 removes sequence number 5 to sequence number 40 from the active data set.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Investigate data&#039;&#039;&#039;&lt;br /&gt;
 showmatrix&lt;br /&gt;
: This data set consists of an alignment of mitochondrial DNA from human (17 sequences), chimpanzee (1 sequence), and Neanderthal (1 sequence). The Neanderthal DNA was extracted from archaeological material, specifically bones found at Vindija in Croatia.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Start analysis&#039;&#039;&#039;&lt;br /&gt;
 outgroup Pan_troglodytes&lt;br /&gt;
 lset nst=mixed rates=gamma&lt;br /&gt;
 mcmc ngen=500000 nchains=3 diagnfreq=10000&lt;br /&gt;
&lt;br /&gt;
: Here we use the command `nst=mixed` which allows MrBayes to automatically explore all possible substitution models. Essentially, MrBayes now considers the substitution model as one more parameter, and uses MCMC to sample from the possible versions (with nst ranging from 1 to 6). This will often be the best choice when using MrBayes. (Below, I use nst=6 for pedagogical purposes, because it makes it simpler to analyse the output files).&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Find posterior probability of clades&#039;&#039;&#039;&lt;br /&gt;
: When the run has finished, issue this command to compute a consensus tree:&lt;br /&gt;
 sumt contype=halfcompat showtreeprobs=no relburnin=yes burninfrac=0.25&lt;br /&gt;
: Examine the consensus tree that is plotted to screen: On the branches that are resolved, you will notice that numbers have been plotted. These are clade-credibility values, and are in fact the posterior probability that the clade is real (based on the present data set).&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Question: &#039;&#039;&#039;What is the posterior probability that all sampled Homo sapiens sequences form a monophyletic group excluding the Neanderthal sequence? Does this support placing the Neanderthal outside modern human mitochondrial diversity?&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
== Probability distributions over other parameters ==&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Question 6&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
: As the last thing, we will now turn away from the tree topology, and instead examine the other parameters that also form part of the probabilistic model. We will do this using a reduced version of the Hepatitis C virus data set that we have examined previously. Stay in the shell window where you just performed the analysis of Neanderthal sequences.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Load data set&#039;&#039;&#039;&lt;br /&gt;
 execute hcvsmall.nexus&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Define site partition&#039;&#039;&#039;&lt;br /&gt;
 charset 1stpos=1-.\3&lt;br /&gt;
 charset 2ndpos=2-.\3&lt;br /&gt;
 charset 3rdpos=3-.\3&lt;br /&gt;
 partition bycodon = 3:1stpos,2ndpos,3rdpos&lt;br /&gt;
 set partition=bycodon&lt;br /&gt;
 prset ratepr=variable&lt;br /&gt;
: This is an alternative way of specifying that different sites may evolve at different rates. With a gamma model, we allow rates to vary across sites but do not specify in advance which sites are fast or slow; instead, that pattern is inferred from the data. Here we instead use prior biological knowledge about the structure of the genetic code to divide sites into three classes: 1st, 2nd, and 3rd codon positions. We then allow each class to have its own rate, so that all 1st positions share one rate, all 2nd positions another, and all 3rd positions a third. Specifically, charset 1stpos=1-.\3 defines a character set named 1stpos consisting of site 1 followed by every third site (\3, i.e. sites 1, 4, 7, 10, …), continuing until the end of the alignment (denoted .).&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Specify model&#039;&#039;&#039;&lt;br /&gt;
 lset nst=6&lt;br /&gt;
: This specifies that we want to use a model of the General Time Reversible (GTR) type, where all 6 substitution types have separate rate parameters.&lt;br /&gt;
&lt;br /&gt;
: When the lset command was discussed previously, a few issues were glossed over. Importantly, and unlike PAUP, the lset command in MrBayes gives no information about whether nucleotide frequencies are equal or not, and whether they should be estimated from the data or not. In MrBayes this is instead controlled by defining the prior probability of the nucleotide frequencies (the command prset can be used to set priors). For instance, a model with equal nucleotide frequencies corresponds to having prior probability 1 (one) for the frequency vector (A=0.25, C=0.25, G=0.25, T=0.25), and zero prior probability for the infinitely many other possible frequency vectors. As you will see below, the default prior is not this limited, and the program will therefore estimate the frequencies from the data.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Inspect model details&#039;&#039;&#039;&lt;br /&gt;
 showmodel&lt;br /&gt;
: This command gives you a summary of the current model settings. You will also get a summary of how the prior probabilities of all model parameters are set. You will for instance notice that the nucleotide frequencies (parameter labeled &amp;quot;Statefreq&amp;quot;) have a &amp;quot;Dirichlet&amp;quot; prior. Without going into details, the Dirichlet distribution is a probability distribution over frequency vectors (i.e., vectors of positive values that sum to 1). Depending on the exact parameters the distribution can be more or less flat (flat here means that all sum-to-1 vectors are equally probable). The Dirichlet distribution is a handy way of specifying the prior probability distribution of nucleotide (or amino acid) frequency vectors. The default statefreq prior in MrBayes is the flat or un-informative prior dirichlet(1,1,1,1).&lt;br /&gt;
&lt;br /&gt;
: We will not go into the priors for the remaining parameters in any detail, but you may notice that by default all topologies are taken to be equally likely (a flat prior on trees).&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Start MCMC sampling&#039;&#039;&#039;&lt;br /&gt;
 mcmc ngen=1000000 samplefreq=100 diagnfreq=10000 nchains=3&lt;br /&gt;
: The run will take a while to finish (you may want to ensure that the average standard deviation of split frequencies is less than 0.01 before ending the analysis).&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Compute summary of parameter values&#039;&#039;&#039;&lt;br /&gt;
 sump relburnin=yes burninfrac=0.25&lt;br /&gt;
: The sump command (with a &amp;quot;p&amp;quot; at the end) works much like the sumt command (with a &amp;quot;t&amp;quot; at the end), but for other parameters than the tree-topology. Again, we are using 25% of the total number of samples as burnin.&lt;br /&gt;
&lt;br /&gt;
: First, you get a scatter plot of the lnL as a function of generation number. Values from the two independent runs are labeled &amp;quot;1&amp;quot; and &amp;quot;2&amp;quot; respectively. If the burnin is suitable, then the points should be randomly scattered over a narrow lnL interval.&lt;br /&gt;
&lt;br /&gt;
: Secondly, the posterior probability distribution of each parameter is summarized by giving the mean, variance, median, and 95% credible interval.&lt;br /&gt;
&lt;br /&gt;
: The last columns contain values indicating if the run has converged. Specifically ESS means Effective Sample Size, and is a measure of how many independent samples you have from the posterior - the higher the better, but this should be at least 100. The column labeled PSRF+ is a measure (also known as &amp;quot;R-hat&amp;quot;) which should be close to 1 if the runs have converged. Specifically this measures whether different chains (and different parts of different chains) converge to sample the same set of values. As a rule of thumb: values less than 1.05 are good, between 1.05 and 1.10 are ok, and above 1.10 have not converged well.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Question: &#039;&#039;&#039;What are the posterior mean values of the relative substitution rate parameters r(AC) and r(CG)?&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Question 7: &#039;&#039;&#039; Based on the reported posterior means, does r(CG) appear to be larger than r(AC)?&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Question 8&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Marginal vs. joint distributions&#039;&#039;&#039;&lt;br /&gt;
: Strictly speaking the comparison above was not entirely appropriate. We first found the overall distribution of the r(CG) parameter and then compared its mean to the mean of the overall distribution of the r(AC) parameter. By doing things this way, we are ignoring the possibility that the two parameters might be associated in some way. For instance, one parameter might always be larger than the other in any individual sample, even though the total distributions overlap. We should instead be looking at the distribution over both parameters simultaneously. A probability distribution over several parameters simultaneously is called a &amp;quot;joint distribution&amp;quot; over the parameters.&lt;br /&gt;
&lt;br /&gt;
: By looking at one parameter at a time, we are summing its probability over all values of the other parameters. This is called the marginal distribution.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Examine marginal distributions&#039;&#039;&#039;&lt;br /&gt;
: In RStudio, use the following commands to read and plot the marginal distributions of r(AC) and r(CG). Note that we are discarding the first 25% of the reads as burnin&lt;br /&gt;
 df_hcv = read_tsv(&amp;quot;hcvsmall.nexus.run1.p&amp;quot;, skip=1)&lt;br /&gt;
 burnin = df_hcv$Gen %&amp;gt;% &lt;br /&gt;
     max() %&amp;gt;% &lt;br /&gt;
     multiply_by(0.25) %&amp;gt;% &lt;br /&gt;
     floor()&lt;br /&gt;
 df_hcv2 = df_hcv %&amp;gt;% &lt;br /&gt;
     filter(Gen &amp;gt; burnin) %&amp;gt;%&lt;br /&gt;
     select(CG = `r(C&amp;lt;-&amp;gt;G){all}`,&lt;br /&gt;
            AC = `r(A&amp;lt;-&amp;gt;C){all}`&lt;br /&gt;
            )&lt;br /&gt;
 mcmc_intervals(df_hcv2, prob_outer = 1)&lt;br /&gt;
 mcmc_areas(df_hcv2, prob_outer = 1)&lt;br /&gt;
: The functions mcmc_intervals and mcmc_areas plot different views of the same posterior distributions. &lt;br /&gt;
&lt;br /&gt;
: You can also simply plot the data using ggplot:&lt;br /&gt;
 df_hcv2_long = pivot_longer(df_hcv2, cols = c(&amp;quot;CG&amp;quot;, &amp;quot;AC&amp;quot;))&lt;br /&gt;
 &lt;br /&gt;
 ggplot(df_hcv2_long) + &lt;br /&gt;
     geom_density(mapping=aes(x=value, fill=name), alpha=0.3) + &lt;br /&gt;
     labs(x=&amp;quot;Substitution rate&amp;quot;)&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Question&#039;&#039;&#039;: Based on the marginal distributions, r(AC) appears to be centered at a higher value than r(CG), but the two distributions overlap somewhat. Can you from these marginal distributions alone decide whether r(AC) is larger than r(CG) in most posterior samples? Why or why not?&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Question 9&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Examine joint distributions&#039;&#039;&#039;&lt;br /&gt;
: These plots and results explore the relationship between the A&amp;lt;-&amp;gt;C and C&amp;lt;-&amp;gt;G rates.&lt;br /&gt;
 ggplot(df_hcv2, aes(x=CG, y=AC)) + &lt;br /&gt;
     geom_point(col=&amp;quot;blue&amp;quot;) + &lt;br /&gt;
     geom_abline(intercept=0, slope=1, lty=2, col=&amp;quot;red&amp;quot;) +&lt;br /&gt;
     xlim(0,0.25) + &lt;br /&gt;
     ylim(0,0.25) + &lt;br /&gt;
     labs(x=&amp;quot;CG rate&amp;quot;, y =&amp;quot;AC rate&amp;quot;) &lt;br /&gt;
 &lt;br /&gt;
 ggplot(df_hcv2, aes(x=CG, y= AC)) + &lt;br /&gt;
     geom_hex(col=&amp;quot;blue&amp;quot;) + &lt;br /&gt;
     geom_abline(intercept=0, slope=1, lty=2, col=&amp;quot;red&amp;quot;) +&lt;br /&gt;
     xlim(0,0.25) + &lt;br /&gt;
     ylim(0, 0.25) + &lt;br /&gt;
     labs(x=&amp;quot;CG rate&amp;quot;, y =&amp;quot;AC rate&amp;quot;) &lt;br /&gt;
 &lt;br /&gt;
 df_hcv2 %&amp;gt;%&lt;br /&gt;
     nrow()&lt;br /&gt;
 df_hcv2 %&amp;gt;%&lt;br /&gt;
     filter(AC&amp;gt;CG) %&amp;gt;%&lt;br /&gt;
     nrow()&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Question: &#039;&#039;&#039;Based on the two different ways to plot the joint distribution and based on the unfiltered and filtered row counts, what is the posterior probability that r(AC) &amp;gt; r(CG)?&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Question 10&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
: Note how examining the joint distribution provides you with information that you could not get from simply comparing the marginal distributions. This very simple procedure can be used to answer many different questions.&lt;br /&gt;
&lt;br /&gt;
: Now, plot the relative substitution rates at the first, second, and third codon positions:&lt;br /&gt;
 df_hcv3 = df_hcv %&amp;gt;% &lt;br /&gt;
     filter(Gen &amp;gt; 75000) %&amp;gt;%&lt;br /&gt;
     select(Codon_1st = `m{1}`,&lt;br /&gt;
            Codon_2nd = `m{2}`,&lt;br /&gt;
            Codon_3rd = `m{3}` ) %&amp;gt;%&lt;br /&gt;
     pivot_longer(cols=c(&amp;quot;Codon_1st&amp;quot;, &amp;quot;Codon_2nd&amp;quot;, &amp;quot;Codon_3rd&amp;quot;))&lt;br /&gt;
 &lt;br /&gt;
 ggplot(df_hcv3) + &lt;br /&gt;
     geom_density(mapping=aes(x=value, fill=name), alpha=0.3) + &lt;br /&gt;
     labs(x=&amp;quot;Relative substitution rate&amp;quot;)&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Question: &#039;&#039;&#039;Since random mutations presumably hit all three codon positions with the same frequency, any differences are expected to be caused by subsequent selection. Which of the following statement are correct ? (More than one answer may be correct)&lt;br /&gt;
&lt;br /&gt;
:* Codon position 2 is the most variable of the codon positions.&lt;br /&gt;
:* Codon position 1 is the most variable of the codon positions.&lt;br /&gt;
:* Codon position 1 is the most conserved codon position.&lt;br /&gt;
:* Codon position 3 is the most conserved codon position.&lt;br /&gt;
:* Codon position 3 is the most variable of the codon positions.&lt;br /&gt;
:* Codon position 2 is the most conserved codon position.&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Question 11&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Question: &#039;&#039;&#039;How does this result fit with your knowledge of the genetic code? Why are these codon positions the most conserved or the most variable?&lt;/div&gt;</summary>
		<author><name>Gorm</name></author>
	</entry>
	<entry>
		<id>https://teaching.healthtech.dtu.dk/22115/index.php?title=Bayesian_Phylogeny&amp;diff=251</id>
		<title>Bayesian Phylogeny</title>
		<link rel="alternate" type="text/html" href="https://teaching.healthtech.dtu.dk/22115/index.php?title=Bayesian_Phylogeny&amp;diff=251"/>
		<updated>2026-03-19T10:32:45Z</updated>

		<summary type="html">&lt;p&gt;Gorm: /* Probability distributions over other parameters */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;This exercise is part of the course  [[22115_-_Computational_Molecular_Evolution|Computational Molecular Evolution (22115)]].&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Overview ==&lt;br /&gt;
&lt;br /&gt;
Today&#039;s exercise will focus on phylogenetic analysis using Bayesian methods.&lt;br /&gt;
&lt;br /&gt;
As was the case for likelihood methods, Bayesian analysis is founded on having a probabilistic model of how the observed data is produced. This means that, for a given set of parameter values, you can compute the probability or [https://www.statlect.com/glossary/probability-density-function probability density] of any possible observation. For a full dataset, you then obtain the likelihood by multiplying these values across all observations. You will recall from the lecture that in Bayesian statistics the goal is to obtain a full posterior probability distribution over all possible parameter values. The posterior distribution quantifies our degree of belief in any possible parameter value after seeing the data. It is obtained by updating the prior probability distribution using the likelihood of the observed data.&lt;br /&gt;
&lt;br /&gt;
The prior probability distribution expresses your beliefs about the parameters before seeing any data, while the likelihood expresses what the observed data are telling you about the parameters. Specifically, the likelihood of a parameter value is the probability of the observed data given that parameter value. We regard a parameter value as more plausible the more probable it makes the observed data. This is the same measure we have previously used to find the maximum likelihood estimate. If the prior probability distribution is flat (i.e., if all possible parameter values have the same prior probability), then the posterior distribution is proportional to the likelihood, and the parameter value with the maximum likelihood also has the maximum posterior probability. However, even in this case, using a Bayesian approach still lets you interpret the result as a probability distribution over parameter values. &lt;br /&gt;
&lt;br /&gt;
If the prior is not flat, then it may have a substantial impact on the posterior, although this effect will usually diminish as the amount of data increases. A prior should ideally be based on domain knowledge and results from previous experiments. For instance one can use the posterior from one analysis as the prior in a new, independent analysis. Often a prior is chosen to be weakly informative, meaning that it places reasonable bounds on the parameter values without constraining them too narrowly. For instance the transition/transversion rate ratio kappa is typically 1.5-10. Values such as 100, 1,000 or 1,000,000 would be extremely unlikely, so a weakly informative prior for this parameter could be chosen to place 95% of its probability mass in the 0.5-20 range, slightly wider than what we think of as plausible values. For instance one could use a lognormal distribution with suitable parameters.&lt;br /&gt;
&lt;br /&gt;
In Bayesian phylogeny the parameters are of the same kind as in maximum likelihood phylogeny. Typical parameters include tree topology, branch lengths, nucleotide frequencies, and substitution model parameters such as the transition/transversion rate ratio or the gamma shape parameter. The difference is that, whereas in maximum likelihood phylogeny we seek the best point estimates of the parameter values, in Bayesian phylogeny the goal is instead to infer a full probability distribution over the possible parameter values. The observed data are again usually taken to be the alignment, although strictly speaking it would be more reasonable to say that the sequences are what have been observed, and that the alignment should then be inferred jointly with the phylogeny.&lt;br /&gt;
&lt;br /&gt;
In this exercise we will explore how one can determine and use posterior probability distributions over trees, over clades, and over substitution parameters. We will also touch upon the difference between marginal and joint probability distributions.&lt;br /&gt;
&lt;br /&gt;
== Getting started ==&lt;br /&gt;
&lt;br /&gt;
: In the command below: Instead of /path/to/molevol enter the path to the directory where you have placed your course files (for instance cd /Users/bob/Documents/molevol, or cd /home/student/molevol).&lt;br /&gt;
 cd /path/to/molevol&lt;br /&gt;
 mkdir bayes&lt;br /&gt;
 cd bayes&lt;br /&gt;
 cp ../data/primatemitDNA.nexus ./primatemitDNA.nexus&lt;br /&gt;
 cp ../data/neanderthal.nexus ./neanderthal.nexus&lt;br /&gt;
 cp ../data/hcvsmall.nexus ./hcvsmall.nexus&lt;br /&gt;
&lt;br /&gt;
: You have analyzed (versions of) all these data files previously in this course. We will now use Bayesian phylogenetic analysis to complement what we learned in those analyses.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Load R libraries&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
: In RStudio: set the working directory to the bayes directory. Then issue these commands:&lt;br /&gt;
 library(magrittr)&lt;br /&gt;
 library(tidyverse)&lt;br /&gt;
 library(bayesplot)&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
== Posterior probability of trees ==&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Question 1&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
: In today&#039;s exercise we will be using the program &amp;quot;MrBayes&amp;quot; to perform Bayesian phylogenetic analysis. MrBayes is a program that, like PAUP*, can be controlled by giving commands at a command line prompt. In fact, there is a substantial overlap between the commands used to control MrBayes and the PAUP command language. This should be a help when you are trying to understand how to use the program.&lt;br /&gt;
&lt;br /&gt;
: Note that the command &amp;quot;help&amp;quot; will give you a list of all available commands. Issuing &amp;quot;help &#039;&#039;command&#039;&#039;&amp;quot; will give you a more detailed description of the specified command along with current option values. This is similar to how &amp;quot;help &#039;&#039;command&#039;&#039;&amp;quot; works in PAUP.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Start program&#039;&#039;&#039;&lt;br /&gt;
: In a terminal window, issue the command:&lt;br /&gt;
 mb&lt;br /&gt;
: This starts the program, giving you a prompt (&amp;quot;MrBayes&amp;gt; &amp;quot;) where you can enter commands.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Get a quick overview of available commands&#039;&#039;&#039;&lt;br /&gt;
 help&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Load your sequences&#039;&#039;&#039;&lt;br /&gt;
 execute primatemitDNA.nexus&lt;br /&gt;
: This file contains mitochondrial DNA sequences from 5 different primates. Note that MrBayes accepts input in nexus format, and that this is the same command that was used to load sequences in PAUP*. In general, you can use many of the PAUP commands in MrBayes also.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Inspect data set&#039;&#039;&#039;&lt;br /&gt;
 showmatrix&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Define outgroup&#039;&#039;&#039;&lt;br /&gt;
 outgroup Gibbon&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Specify your model of sequence evolution&#039;&#039;&#039;&lt;br /&gt;
 lset nst=2 rates=gamma&lt;br /&gt;
: This command is again very much like the corresponding one in PAUP. You are specifying that you want to use a model with two substitution types (nst=2), and this is automatically taken to mean that you want to distinguish between transitions and transversions. Furthermore, rates=gamma means that you want the model to use a gamma distribution to account for different rates at different sites in the sequence.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Start Markov chain Monte Carlo sampling&#039;&#039;&#039;&lt;br /&gt;
:Make sure to make the shell window as wide as possible and then issue the following commands to start the run:&lt;br /&gt;
 mcmc ngen=1000000 samplefreq=100 nchains=3 diagnfreq=5000&lt;br /&gt;
: What you are doing here is to use the method known as MCMCMC (&amp;quot;Metropolis-coupled Markov chain Monte Carlo&amp;quot;) to empirically determine the posterior probability distribution of trees, branch lengths and substitution parameters. Recall that in the Bayesian framework this is how we learn about parameter values: instead of finding the best point estimates, we typically want to quantify the probability of the entire range of possible values. An estimate of the time left is shown in the last column of output.&lt;br /&gt;
&lt;br /&gt;
: Let us examine the command in detail. First, ngen=1000000 samplefreq=100 lets the search run for 1,000,000 MCMC steps (&amp;quot;generations&amp;quot;) and saves parameter values once every 100 rounds (meaning that a total of 10,000 sets of parameter values will be saved to sample files). You sometimes need to run longer (or shorter) than 1,000,000, and would then typically tweak samplefreq so you get around 1,000 - 10,000 samples in all. The option nchains=3 means that the MCMCMC sampling uses 3 parallel chains (but see below): one &amp;quot;cold&amp;quot; from which sampling takes place, and two &amp;quot;heated&amp;quot; that move around in the parameter space more quickly to find additional peaks in the probability distribution.&lt;br /&gt;
&lt;br /&gt;
: The option diagnfreq=5000 has to do with testing whether the MrBayes run is successful. Briefly, MrBayes will start two entirely independent runs starting from different random trees. In the early phases of the run, the two runs will sample very different trees, but when they have reached convergence (when they produce a good sample from the posterior probability distribution), the two tree samples should be very similar. Every diagnfreq generations, the program will compute a measure of how similar the tree samples are, specifically the average standard deviation of split frequencies. A “split” is the same as a bipartition, i.e. a division of all leaves in the tree into two groups, obtained by cutting an internal branch. For each split, MrBayes compares how often that split occurs in the two independent runs; if the runs have converged, these frequencies should be very similar, giving a small standard deviation. The program then averages this quantity across splits. As a rule of thumb, you may want to run until this value is less than 0.05 (the smaller the better)&lt;br /&gt;
&lt;br /&gt;
: During the run you will see reports about the progress of the two independent runs, each consisting of three chains. Each line of output lists the generation number and the log likelihoods of the current tree/parameter combination for each of the two groups of three chains (a column of asterisks separate the results for the independent runs). The cold chains are the ones enclosed in brackets [...], while the heated chains are enclosed in parentheses (...). Occasionally the chains will swap so one of the heated chains now becomes cold (and sampling then takes place from this chain).&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Continue run until parallel runs converge on same solution&#039;&#039;&#039;&lt;br /&gt;
:At the end of the run, Mrbayes will print the average standard deviation of split frequencies (which is a measure of how similar the tree samples of the two independent runs are). We recommend that you continue with the analysis until the value gets below 0.01 (if the value is larger than 0.01 then you should answer &amp;quot;yes&amp;quot; when the program asks &amp;quot;Continue the analysis? (yes/no)&amp;quot;.)&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Question: &#039;&#039;&#039;MrBayes starts two independent runs from different random trees. Why is it useful to run two independent analyses instead of just one? How does the average standard deviation of split frequencies help you decide whether the two runs have converged to the same posterior distribution? At approximately how many generations does this happen in your run?&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Question 2&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Have a look at the resulting sample files&#039;&#039;&#039;&lt;br /&gt;
: Open a new Terminal window (don&#039;t quit mrbayes in the other terminal yet!) and cd to the bayes directory. Open one of the parameter sampling files in a text editor:&lt;br /&gt;
 nedit primatemitDNA.nexus.run1.p &amp;amp;&lt;br /&gt;
: This file contains one line for each sampled point (you may want to turn off line-wrapping in nedit under the preferences menu). Each row corresponds to a certain sample time (or generation). Each column contains the sampled values of one specific parameter. The first line contains headings telling what the different columns are: &lt;br /&gt;
:* Gen: generation; number of MCMC steps taken so far&lt;br /&gt;
:* lnL: log likelihood of the current parameter estimates&lt;br /&gt;
:* LnPr: log of the prior probability&lt;br /&gt;
:* TL: tree length (sum of all branch lengths)&lt;br /&gt;
:* kappa: transition/transversion rate ratio&lt;br /&gt;
:* pi(A), pi(C), pi(G), pi(T): frequency of A, C, G, T&lt;br /&gt;
:* alpha: shape parameter for the gamma distribution. &lt;br /&gt;
&lt;br /&gt;
: (Column headings may be shifted relative to their corresponding columns). Note how the values of most parameters change a lot during the initial &amp;quot;burnin&amp;quot; period, before they settle near their most probable values. &lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Question: &#039;&#039;&#039;You will notice that lnL is always negative, while LnPr can sometimes be positive. At first sight this may seem impossible, since probabilities cannot be larger than 1. How can this happen?&lt;br /&gt;
&lt;br /&gt;
As a hint, note that (1) priors for continuous parameters are probability densities, and (2) the default prior for each branch length in MrBayes is an exponential distribution with rate 10. Use the following R code to plot this prior on both an ordinary y-axis and a log-scaled y-axis, and then explain why positive values of LnPr are possible.&lt;br /&gt;
&lt;br /&gt;
 df_expdist = tibble(&lt;br /&gt;
   x = seq(0, 1, by = 0.001),&lt;br /&gt;
   density = dexp(x, rate = 10),&lt;br /&gt;
   logdensity = log(dexp(x, rate = 10))&lt;br /&gt;
 )&lt;br /&gt;
 &lt;br /&gt;
 ggplot(df_expdist, aes(x = x, y = density)) +&lt;br /&gt;
   geom_line(color=&amp;quot;blue&amp;quot;) +&lt;br /&gt;
   geom_hline(yintercept = 1, lty=2) +&lt;br /&gt;
   labs(&lt;br /&gt;
     x = &amp;quot;Branch length&amp;quot;,&lt;br /&gt;
     y = &amp;quot;Probability density&amp;quot;,&lt;br /&gt;
     title = &amp;quot;Exponential prior on branch lengths: Exp(rate = 10)&amp;quot;&lt;br /&gt;
   )&lt;br /&gt;
 &lt;br /&gt;
 ggplot(df_expdist, aes(x = x, y = logdensity)) +&lt;br /&gt;
   geom_line(color=&amp;quot;blue&amp;quot;) +&lt;br /&gt;
   geom_hline(yintercept = 0, lty=2) +&lt;br /&gt;
   labs(&lt;br /&gt;
     x = &amp;quot;Branch length&amp;quot;,&lt;br /&gt;
     y = &amp;quot;log of Probability density&amp;quot;,&lt;br /&gt;
     title = &amp;quot;Exponential prior on branch lengths: Exp(rate = 10)&amp;quot;&lt;br /&gt;
   )&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Question 3&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Examine MCMC trajectory for gamma shape parameter, alpha&#039;&#039;&#039;&lt;br /&gt;
: Recall that the idea in MCMCMC sampling is to move around in parameter space in such a way that points are visited according to their posterior probability (i.e., regions with high posterior probability are visited frequently). Now, in RStudio, plot the sampled values for the gamma shape parameter, alpha, for one of the run files:&lt;br /&gt;
 df_primates = read_tsv(&amp;quot;primatemitDNA.nexus.run1.p&amp;quot;, skip=1)&lt;br /&gt;
 mcmc_trace(df_primates, pars=&amp;quot;alpha&amp;quot;)&lt;br /&gt;
: mcmc_trace is one of several plotting commands available in the bayesplot package. This command plots the sampled values of the parameter alpha from the first of the two parallel runs against MCMC generation number. Thus, the x-axis shows the progress of the run through time, with the leftmost values being the earliest samples and the rightmost values the later ones. Note how the Markov chain starts at the arbitrary value 1.0, rapidly moves to values that fit the observed data better, and then moves around in parameter space, sampling different plausible values of alpha. You can experiment with plotting other columns as well.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Question: &#039;&#039;&#039;Describe briefly what happens to the sampled values of alpha during the run. Why is it reasonable to discard the earliest samples as burn-in?&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Question 4&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Investigate posterior probability distribution over trees&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
: Now, close the nedit window and have a look at the file containing sampled trees:&lt;br /&gt;
 nedit primatemitDNA.nexus.run1.t &amp;amp;&lt;br /&gt;
: Tree topology is also a parameter in our model, and exactly like for the other parameters we also get samples from tree-space. One tree is printed per line in the parenthetical Newick format you have seen before. There are 5 taxa in the present data set, so the number of possible unrooted binary tree topologies is only 15. Since we have taken more than 15 sample points, there must be several lines containing the same tree topology. Close the nedit window when you are done.&lt;br /&gt;
&lt;br /&gt;
: MrBayes provides the sumt command to summarize the sampled trees. Before using it, we need to decide on the burn-in: The burn-in is the initial set of samples that are typically discarded, because we want to ensure that the MCMC has moved away from the random starting values, and has found the peaks of the probability landscape. Since the convergence diagnostic used a relative burn-in of 25%, we will also discard the first 25% of tree samples when summarizing the posterior.&lt;br /&gt;
&lt;br /&gt;
: Return to the shell window where you have MrBayes running. In the command below relburnin=yes and burninfrac=0.25 tells MrBayes to discard 25% of the samples as burnin (you could also have explicitly given the number of samples to discard - help sumt will give you details about the command and the current option settings).&lt;br /&gt;
 sumt contype=halfcompat conformat=simple relburnin=yes burninfrac=0.25 showtreeprobs=yes&lt;br /&gt;
: (Scroll back so you can see the top of the output when the command is done). This command gives you a summary of the trees that are in the file you examined manually above. The option contype=halfcompat requests that a majority rule consensus tree is calculated from the set of trees that are left after discarding the burnin. This consensus is the first tree plotted to the screen. Below the consensus cladogram, a consensus phylogram is plotted. The branch lengths in this have been averaged over the trees in which that branch was present (a particular branch corresponds to a bi-partition of the data, and will typically not be present in every sampled tree). The cladogram also has &amp;quot;clade credibility&amp;quot; values. We will return to the meaning of these later in today&#039;s exercise.&lt;br /&gt;
&lt;br /&gt;
: What most interests us right now is the list of trees that is printed after the phylogram. These trees are labeled &amp;quot;Tree 1&amp;quot;, &amp;quot;Tree 2&amp;quot;, etc, and are sorted according to their posterior probability which is indicated by a lower-case p after the tree number. (The upper-case P gives the cumulated probability of trees shown so far, and is useful for constructing a credible set). This list highlights how Bayesian phylogenetic analysis is different from maximum likelihood: Instead of finding the best tree(s), we here quantify our degree of belief in all possible trees.&lt;br /&gt;
&lt;br /&gt;
: The list of trees and probabilities was printed because of the option showtreeprobs=yes. Note that you probably do not want to issue that command if you have much more than 5 taxa! In that case you could instead inspect the file named primatemitDNA.nexus.trprobs which is now present in the same directory as your other files (this file is automatically produced by the sumt command).&lt;br /&gt;
&lt;br /&gt;
: &#039;&#039;&#039;NOTE&#039;&#039;&#039;: Annoyingly, there is a bug in the version of mrbayes we are using here, which means leaf names are not printed on the list of trees with probabilities. However, the most probable tree here in fact is identical to the consensus tree printed above it.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Question: &#039;&#039;&#039;What is the posterior probability of the most probable tree? Does the analysis strongly support a single tree, or is the posterior probability distributed across several different trees?&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
== Analysis of Neanderthal data (posterior probability of clades) ==&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Question 5&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
For many years, there was considerable debate about the origin of modern humans. One view, often called the [https://www.thoughtco.com/multiregional-hypothesis-167235 Multiregional Hypothesis], proposed that after Homo erectus spread from Africa into different parts of the world, regional populations gradually evolved into modern humans more or less in parallel. A different view, often called the Recent African Origin model, proposed that modern Homo sapiens evolved in Africa and later spread outward, largely replacing other archaic human groups such as the Neanderthals.&lt;br /&gt;
&lt;br /&gt;
Today it is clear that the history is more complicated than either simple extreme: modern humans arose in Africa, but there was also some interbreeding with Neanderthals and other archaic humans. However, in this exercise we will focus on a narrower question that can be addressed using a phylogeny of mitochondrial DNA: do the sampled Neanderthal and human mitochondrial sequences suggest that the Neanderthal sequence falls inside or outside modern human mitochondrial diversity?&lt;br /&gt;
&lt;br /&gt;
We will use the present data set to examine this question.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Load Neanderthal data set&#039;&#039;&#039;&lt;br /&gt;
: In the Terminal where you have MrBayes running:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
execute neanderthal.nexus&lt;br /&gt;
delete 5-40&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
: As we did for the maximum likelihood analysis, we will discard some of the human sequences in order to speed up the analysis. The command delete 5-40 removes sequence number 5 to sequence number 40 from the active data set.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Investigate data&#039;&#039;&#039;&lt;br /&gt;
 showmatrix&lt;br /&gt;
: This data set consists of an alignment of mitochondrial DNA from human (17 sequences), chimpanzee (1 sequence), and Neanderthal (1 sequence). The Neanderthal DNA was extracted from archaeological material, specifically bones found at Vindija in Croatia.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Start analysis&#039;&#039;&#039;&lt;br /&gt;
 outgroup Pan_troglodytes&lt;br /&gt;
 lset nst=mixed rates=gamma&lt;br /&gt;
 mcmc ngen=500000 nchains=3 diagnfreq=10000&lt;br /&gt;
&lt;br /&gt;
: Here we use the command `nst=mixed` which allows MrBayes to automatically explore all possible substitution models. Essentially, MrBayes now considers the substitution model as one more parameter, and uses MCMC to sample from the possible versions (with nst ranging from 1 to 6). This will often be the best choice when using MrBayes. (Below, I use nst=6 for pedagogical purposes, because it makes it simpler to analyse the output files).&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Find posterior probability of clades&#039;&#039;&#039;&lt;br /&gt;
: When the run has finished, issue this command to compute a consensus tree:&lt;br /&gt;
 sumt contype=halfcompat showtreeprobs=no relburnin=yes burninfrac=0.25&lt;br /&gt;
: Examine the consensus tree that is plotted to screen: On the branches that are resolved, you will notice that numbers have been plotted. These are clade-credibility values, and are in fact the posterior probability that the clade is real (based on the present data set).&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Question: &#039;&#039;&#039;What is the posterior probability that all sampled Homo sapiens sequences form a monophyletic group excluding the Neanderthal sequence? Does this support placing the Neanderthal outside modern human mitochondrial diversity?&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
== Probability distributions over other parameters ==&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Question 6&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
: As the last thing, we will now turn away from the tree topology, and instead examine the other parameters that also form part of the probabilistic model. We will do this using a reduced version of the Hepatitis C virus data set that we have examined previously. Stay in the shell window where you just performed the analysis of Neanderthal sequences.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Load data set&#039;&#039;&#039;&lt;br /&gt;
 execute hcvsmall.nexus&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Define site partition&#039;&#039;&#039;&lt;br /&gt;
 charset 1stpos=1-.\3&lt;br /&gt;
 charset 2ndpos=2-.\3&lt;br /&gt;
 charset 3rdpos=3-.\3&lt;br /&gt;
 partition bycodon = 3:1stpos,2ndpos,3rdpos&lt;br /&gt;
 set partition=bycodon&lt;br /&gt;
 prset ratepr=variable&lt;br /&gt;
: This is an alternative way of specifying that different sites may evolve at different rates. With a gamma model, we allow rates to vary across sites but do not specify in advance which sites are fast or slow; instead, that pattern is inferred from the data. Here we instead use prior biological knowledge about the structure of the genetic code to divide sites into three classes: 1st, 2nd, and 3rd codon positions. We then allow each class to have its own rate, so that all 1st positions share one rate, all 2nd positions another, and all 3rd positions a third. Specifically, charset 1stpos=1-.\3 defines a character set named 1stpos consisting of site 1 followed by every third site (\3, i.e. sites 1, 4, 7, 10, …), continuing until the end of the alignment (denoted .).&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Specify model&#039;&#039;&#039;&lt;br /&gt;
 lset nst=6&lt;br /&gt;
: This specifies that we want to use a model of the General Time Reversible (GTR) type, where all 6 substitution types have separate rate parameters.&lt;br /&gt;
&lt;br /&gt;
: When the lset command was discussed previously, a few issues were glossed over. Importantly, and unlike PAUP, the lset command in MrBayes gives no information about whether nucleotide frequencies are equal or not, and whether they should be estimated from the data or not. In MrBayes this is instead controlled by defining the prior probability of the nucleotide frequencies (the command prset can be used to set priors). For instance, a model with equal nucleotide frequencies corresponds to having prior probability 1 (one) for the frequency vector (A=0.25, C=0.25, G=0.25, T=0.25), and zero prior probability for the infinitely many other possible frequency vectors. As you will see below, the default prior is not this limited, and the program will therefore estimate the frequencies from the data.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Inspect model details&#039;&#039;&#039;&lt;br /&gt;
 showmodel&lt;br /&gt;
: This command gives you a summary of the current model settings. You will also get a summary of how the prior probabilities of all model parameters are set. You will for instance notice that the nucleotide frequencies (parameter labeled &amp;quot;Statefreq&amp;quot;) have a &amp;quot;Dirichlet&amp;quot; prior. Without going into details, the Dirichlet distribution is a probability distribution over frequency vectors (i.e., vectors of positive values that sum to 1). Depending on the exact parameters the distribution can be more or less flat (flat here means that all sum-to-1 vectors are equally probable). The Dirichlet distribution is a handy way of specifying the prior probability distribution of nucleotide (or amino acid) frequency vectors. The default statefreq prior in MrBayes is the flat or un-informative prior dirichlet(1,1,1,1).&lt;br /&gt;
&lt;br /&gt;
: We will not go into the priors for the remaining parameters in any detail, but you may notice that by default all topologies are taken to be equally likely (a flat prior on trees).&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Start MCMC sampling&#039;&#039;&#039;&lt;br /&gt;
 mcmc ngen=1000000 samplefreq=100 diagnfreq=10000 nchains=3&lt;br /&gt;
: The run will take a while to finish (you may want to ensure that the average standard deviation of split frequencies is less than 0.01 before ending the analysis).&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Compute summary of parameter values&#039;&#039;&#039;&lt;br /&gt;
 sump relburnin=yes burninfrac=0.25&lt;br /&gt;
: The sump command (with a &amp;quot;p&amp;quot; at the end) works much like the sumt command (with a &amp;quot;t&amp;quot; at the end), but for other parameters than the tree-topology. Again, we are using 25% of the total number of samples as burnin.&lt;br /&gt;
&lt;br /&gt;
: First, you get a scatter plot of the lnL as a function of generation number. Values from the two independent runs are labeled &amp;quot;1&amp;quot; and &amp;quot;2&amp;quot; respectively. If the burnin is suitable, then the points should be randomly scattered over a narrow lnL interval.&lt;br /&gt;
&lt;br /&gt;
: Secondly, the posterior probability distribution of each parameter is summarized by giving the mean, variance, median, and 95% credible interval.&lt;br /&gt;
&lt;br /&gt;
: The last columns contain values indicating if the run has converged. Specifically ESS means Effective Sample Size, and is a measure of how many independent samples you have from the posterior - the higher the better, but this should be at least 100. The column labeled PSRF+ is a measure (also known as &amp;quot;R-hat&amp;quot;) which should be close to 1 if the runs have converged. Specifically this measures whether different chains (and different parts of different chains) converge to sample the same set of values. &lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Question: &#039;&#039;&#039;What are the posterior mean values of the relative substitution rate parameters r(AC) and r(CG)?&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Question 7: &#039;&#039;&#039; Based on the reported posterior means, does r(CG) appear to be larger than r(AC)?&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Question 8&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Marginal vs. joint distributions&#039;&#039;&#039;&lt;br /&gt;
: Strictly speaking the comparison above was not entirely appropriate. We first found the overall distribution of the r(CG) parameter and then compared its mean to the mean of the overall distribution of the r(AC) parameter. By doing things this way, we are ignoring the possibility that the two parameters might be associated in some way. For instance, one parameter might always be larger than the other in any individual sample, even though the total distributions overlap. We should instead be looking at the distribution over both parameters simultaneously. A probability distribution over several parameters simultaneously is called a &amp;quot;joint distribution&amp;quot; over the parameters.&lt;br /&gt;
&lt;br /&gt;
: By looking at one parameter at a time, we are summing its probability over all values of the other parameters. This is called the marginal distribution.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Examine marginal distributions&#039;&#039;&#039;&lt;br /&gt;
: In RStudio, use the following commands to read and plot the marginal distributions of r(AC) and r(CG). Note that we are discarding the first 25% of the reads as burnin&lt;br /&gt;
 df_hcv = read_tsv(&amp;quot;hcvsmall.nexus.run1.p&amp;quot;, skip=1)&lt;br /&gt;
 burnin = df_hcv$Gen %&amp;gt;% &lt;br /&gt;
     max() %&amp;gt;% &lt;br /&gt;
     multiply_by(0.25) %&amp;gt;% &lt;br /&gt;
     floor()&lt;br /&gt;
 df_hcv2 = df_hcv %&amp;gt;% &lt;br /&gt;
     filter(Gen &amp;gt; burnin) %&amp;gt;%&lt;br /&gt;
     select(CG = `r(C&amp;lt;-&amp;gt;G){all}`,&lt;br /&gt;
            AC = `r(A&amp;lt;-&amp;gt;C){all}`&lt;br /&gt;
            )&lt;br /&gt;
 mcmc_intervals(df_hcv2, prob_outer = 1)&lt;br /&gt;
 mcmc_areas(df_hcv2, prob_outer = 1)&lt;br /&gt;
: The functions mcmc_intervals and mcmc_areas plot different views of the same posterior distributions. &lt;br /&gt;
&lt;br /&gt;
: You can also simply plot the data using ggplot:&lt;br /&gt;
 df_hcv2_long = pivot_longer(df_hcv2, cols = c(&amp;quot;CG&amp;quot;, &amp;quot;AC&amp;quot;))&lt;br /&gt;
 &lt;br /&gt;
 ggplot(df_hcv2_long) + &lt;br /&gt;
     geom_density(mapping=aes(x=value, fill=name), alpha=0.3) + &lt;br /&gt;
     labs(x=&amp;quot;Substitution rate&amp;quot;)&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Question&#039;&#039;&#039;: Based on the marginal distributions, r(AC) appears to be centered at a higher value than r(CG), but the two distributions overlap somewhat. Can you from these marginal distributions alone decide whether r(AC) is larger than r(CG) in most posterior samples? Why or why not?&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Question 9&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Examine joint distributions&#039;&#039;&#039;&lt;br /&gt;
: These plots and results explore the relationship between the A&amp;lt;-&amp;gt;C and C&amp;lt;-&amp;gt;G rates.&lt;br /&gt;
 ggplot(df_hcv2, aes(x=CG, y=AC)) + &lt;br /&gt;
     geom_point(col=&amp;quot;blue&amp;quot;) + &lt;br /&gt;
     geom_abline(intercept=0, slope=1, lty=2, col=&amp;quot;red&amp;quot;) +&lt;br /&gt;
     xlim(0,0.25) + &lt;br /&gt;
     ylim(0,0.25) + &lt;br /&gt;
     labs(x=&amp;quot;CG rate&amp;quot;, y =&amp;quot;AC rate&amp;quot;) &lt;br /&gt;
 &lt;br /&gt;
 ggplot(df_hcv2, aes(x=CG, y= AC)) + &lt;br /&gt;
     geom_hex(col=&amp;quot;blue&amp;quot;) + &lt;br /&gt;
     geom_abline(intercept=0, slope=1, lty=2, col=&amp;quot;red&amp;quot;) +&lt;br /&gt;
     xlim(0,0.25) + &lt;br /&gt;
     ylim(0, 0.25) + &lt;br /&gt;
     labs(x=&amp;quot;CG rate&amp;quot;, y =&amp;quot;AC rate&amp;quot;) &lt;br /&gt;
 &lt;br /&gt;
 df_hcv2 %&amp;gt;%&lt;br /&gt;
     nrow()&lt;br /&gt;
 df_hcv2 %&amp;gt;%&lt;br /&gt;
     filter(AC&amp;gt;CG) %&amp;gt;%&lt;br /&gt;
     nrow()&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Question: &#039;&#039;&#039;Based on the two different ways to plot the joint distribution and based on the unfiltered and filtered row counts, what is the posterior probability that r(AC) &amp;gt; r(CG)?&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Question 10&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
: Note how examining the joint distribution provides you with information that you could not get from simply comparing the marginal distributions. This very simple procedure can be used to answer many different questions.&lt;br /&gt;
&lt;br /&gt;
: Now, plot the relative substitution rates at the first, second, and third codon positions:&lt;br /&gt;
 df_hcv3 = df_hcv %&amp;gt;% &lt;br /&gt;
     filter(Gen &amp;gt; 75000) %&amp;gt;%&lt;br /&gt;
     select(Codon_1st = `m{1}`,&lt;br /&gt;
            Codon_2nd = `m{2}`,&lt;br /&gt;
            Codon_3rd = `m{3}` ) %&amp;gt;%&lt;br /&gt;
     pivot_longer(cols=c(&amp;quot;Codon_1st&amp;quot;, &amp;quot;Codon_2nd&amp;quot;, &amp;quot;Codon_3rd&amp;quot;))&lt;br /&gt;
 &lt;br /&gt;
 ggplot(df_hcv3) + &lt;br /&gt;
     geom_density(mapping=aes(x=value, fill=name), alpha=0.3) + &lt;br /&gt;
     labs(x=&amp;quot;Relative substitution rate&amp;quot;)&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Question: &#039;&#039;&#039;Since random mutations presumably hit all three codon positions with the same frequency, any differences are expected to be caused by subsequent selection. Which of the following statement are correct ? (More than one answer may be correct)&lt;br /&gt;
&lt;br /&gt;
:* Codon position 2 is the most variable of the codon positions.&lt;br /&gt;
:* Codon position 1 is the most variable of the codon positions.&lt;br /&gt;
:* Codon position 1 is the most conserved codon position.&lt;br /&gt;
:* Codon position 3 is the most conserved codon position.&lt;br /&gt;
:* Codon position 3 is the most variable of the codon positions.&lt;br /&gt;
:* Codon position 2 is the most conserved codon position.&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Question 11&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Question: &#039;&#039;&#039;How does this result fit with your knowledge of the genetic code? Why are these codon positions the most conserved or the most variable?&lt;/div&gt;</summary>
		<author><name>Gorm</name></author>
	</entry>
	<entry>
		<id>https://teaching.healthtech.dtu.dk/22115/index.php?title=Bayesian_Phylogeny&amp;diff=250</id>
		<title>Bayesian Phylogeny</title>
		<link rel="alternate" type="text/html" href="https://teaching.healthtech.dtu.dk/22115/index.php?title=Bayesian_Phylogeny&amp;diff=250"/>
		<updated>2026-03-19T10:31:39Z</updated>

		<summary type="html">&lt;p&gt;Gorm: /* Probability distributions over other parameters */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;This exercise is part of the course  [[22115_-_Computational_Molecular_Evolution|Computational Molecular Evolution (22115)]].&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Overview ==&lt;br /&gt;
&lt;br /&gt;
Today&#039;s exercise will focus on phylogenetic analysis using Bayesian methods.&lt;br /&gt;
&lt;br /&gt;
As was the case for likelihood methods, Bayesian analysis is founded on having a probabilistic model of how the observed data is produced. This means that, for a given set of parameter values, you can compute the probability or [https://www.statlect.com/glossary/probability-density-function probability density] of any possible observation. For a full dataset, you then obtain the likelihood by multiplying these values across all observations. You will recall from the lecture that in Bayesian statistics the goal is to obtain a full posterior probability distribution over all possible parameter values. The posterior distribution quantifies our degree of belief in any possible parameter value after seeing the data. It is obtained by updating the prior probability distribution using the likelihood of the observed data.&lt;br /&gt;
&lt;br /&gt;
The prior probability distribution expresses your beliefs about the parameters before seeing any data, while the likelihood expresses what the observed data are telling you about the parameters. Specifically, the likelihood of a parameter value is the probability of the observed data given that parameter value. We regard a parameter value as more plausible the more probable it makes the observed data. This is the same measure we have previously used to find the maximum likelihood estimate. If the prior probability distribution is flat (i.e., if all possible parameter values have the same prior probability), then the posterior distribution is proportional to the likelihood, and the parameter value with the maximum likelihood also has the maximum posterior probability. However, even in this case, using a Bayesian approach still lets you interpret the result as a probability distribution over parameter values. &lt;br /&gt;
&lt;br /&gt;
If the prior is not flat, then it may have a substantial impact on the posterior, although this effect will usually diminish as the amount of data increases. A prior should ideally be based on domain knowledge and results from previous experiments. For instance one can use the posterior from one analysis as the prior in a new, independent analysis. Often a prior is chosen to be weakly informative, meaning that it places reasonable bounds on the parameter values without constraining them too narrowly. For instance the transition/transversion rate ratio kappa is typically 1.5-10. Values such as 100, 1,000 or 1,000,000 would be extremely unlikely, so a weakly informative prior for this parameter could be chosen to place 95% of its probability mass in the 0.5-20 range, slightly wider than what we think of as plausible values. For instance one could use a lognormal distribution with suitable parameters.&lt;br /&gt;
&lt;br /&gt;
In Bayesian phylogeny the parameters are of the same kind as in maximum likelihood phylogeny. Typical parameters include tree topology, branch lengths, nucleotide frequencies, and substitution model parameters such as the transition/transversion rate ratio or the gamma shape parameter. The difference is that, whereas in maximum likelihood phylogeny we seek the best point estimates of the parameter values, in Bayesian phylogeny the goal is instead to infer a full probability distribution over the possible parameter values. The observed data are again usually taken to be the alignment, although strictly speaking it would be more reasonable to say that the sequences are what have been observed, and that the alignment should then be inferred jointly with the phylogeny.&lt;br /&gt;
&lt;br /&gt;
In this exercise we will explore how one can determine and use posterior probability distributions over trees, over clades, and over substitution parameters. We will also touch upon the difference between marginal and joint probability distributions.&lt;br /&gt;
&lt;br /&gt;
== Getting started ==&lt;br /&gt;
&lt;br /&gt;
: In the command below: Instead of /path/to/molevol enter the path to the directory where you have placed your course files (for instance cd /Users/bob/Documents/molevol, or cd /home/student/molevol).&lt;br /&gt;
 cd /path/to/molevol&lt;br /&gt;
 mkdir bayes&lt;br /&gt;
 cd bayes&lt;br /&gt;
 cp ../data/primatemitDNA.nexus ./primatemitDNA.nexus&lt;br /&gt;
 cp ../data/neanderthal.nexus ./neanderthal.nexus&lt;br /&gt;
 cp ../data/hcvsmall.nexus ./hcvsmall.nexus&lt;br /&gt;
&lt;br /&gt;
: You have analyzed (versions of) all these data files previously in this course. We will now use Bayesian phylogenetic analysis to complement what we learned in those analyses.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Load R libraries&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
: In RStudio: set the working directory to the bayes directory. Then issue these commands:&lt;br /&gt;
 library(magrittr)&lt;br /&gt;
 library(tidyverse)&lt;br /&gt;
 library(bayesplot)&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
== Posterior probability of trees ==&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Question 1&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
: In today&#039;s exercise we will be using the program &amp;quot;MrBayes&amp;quot; to perform Bayesian phylogenetic analysis. MrBayes is a program that, like PAUP*, can be controlled by giving commands at a command line prompt. In fact, there is a substantial overlap between the commands used to control MrBayes and the PAUP command language. This should be a help when you are trying to understand how to use the program.&lt;br /&gt;
&lt;br /&gt;
: Note that the command &amp;quot;help&amp;quot; will give you a list of all available commands. Issuing &amp;quot;help &#039;&#039;command&#039;&#039;&amp;quot; will give you a more detailed description of the specified command along with current option values. This is similar to how &amp;quot;help &#039;&#039;command&#039;&#039;&amp;quot; works in PAUP.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Start program&#039;&#039;&#039;&lt;br /&gt;
: In a terminal window, issue the command:&lt;br /&gt;
 mb&lt;br /&gt;
: This starts the program, giving you a prompt (&amp;quot;MrBayes&amp;gt; &amp;quot;) where you can enter commands.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Get a quick overview of available commands&#039;&#039;&#039;&lt;br /&gt;
 help&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Load your sequences&#039;&#039;&#039;&lt;br /&gt;
 execute primatemitDNA.nexus&lt;br /&gt;
: This file contains mitochondrial DNA sequences from 5 different primates. Note that MrBayes accepts input in nexus format, and that this is the same command that was used to load sequences in PAUP*. In general, you can use many of the PAUP commands in MrBayes also.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Inspect data set&#039;&#039;&#039;&lt;br /&gt;
 showmatrix&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Define outgroup&#039;&#039;&#039;&lt;br /&gt;
 outgroup Gibbon&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Specify your model of sequence evolution&#039;&#039;&#039;&lt;br /&gt;
 lset nst=2 rates=gamma&lt;br /&gt;
: This command is again very much like the corresponding one in PAUP. You are specifying that you want to use a model with two substitution types (nst=2), and this is automatically taken to mean that you want to distinguish between transitions and transversions. Furthermore, rates=gamma means that you want the model to use a gamma distribution to account for different rates at different sites in the sequence.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Start Markov chain Monte Carlo sampling&#039;&#039;&#039;&lt;br /&gt;
:Make sure to make the shell window as wide as possible and then issue the following commands to start the run:&lt;br /&gt;
 mcmc ngen=1000000 samplefreq=100 nchains=3 diagnfreq=5000&lt;br /&gt;
: What you are doing here is to use the method known as MCMCMC (&amp;quot;Metropolis-coupled Markov chain Monte Carlo&amp;quot;) to empirically determine the posterior probability distribution of trees, branch lengths and substitution parameters. Recall that in the Bayesian framework this is how we learn about parameter values: instead of finding the best point estimates, we typically want to quantify the probability of the entire range of possible values. An estimate of the time left is shown in the last column of output.&lt;br /&gt;
&lt;br /&gt;
: Let us examine the command in detail. First, ngen=1000000 samplefreq=100 lets the search run for 1,000,000 MCMC steps (&amp;quot;generations&amp;quot;) and saves parameter values once every 100 rounds (meaning that a total of 10,000 sets of parameter values will be saved to sample files). You sometimes need to run longer (or shorter) than 1,000,000, and would then typically tweak samplefreq so you get around 1,000 - 10,000 samples in all. The option nchains=3 means that the MCMCMC sampling uses 3 parallel chains (but see below): one &amp;quot;cold&amp;quot; from which sampling takes place, and two &amp;quot;heated&amp;quot; that move around in the parameter space more quickly to find additional peaks in the probability distribution.&lt;br /&gt;
&lt;br /&gt;
: The option diagnfreq=5000 has to do with testing whether the MrBayes run is successful. Briefly, MrBayes will start two entirely independent runs starting from different random trees. In the early phases of the run, the two runs will sample very different trees, but when they have reached convergence (when they produce a good sample from the posterior probability distribution), the two tree samples should be very similar. Every diagnfreq generations, the program will compute a measure of how similar the tree samples are, specifically the average standard deviation of split frequencies. A “split” is the same as a bipartition, i.e. a division of all leaves in the tree into two groups, obtained by cutting an internal branch. For each split, MrBayes compares how often that split occurs in the two independent runs; if the runs have converged, these frequencies should be very similar, giving a small standard deviation. The program then averages this quantity across splits. As a rule of thumb, you may want to run until this value is less than 0.05 (the smaller the better)&lt;br /&gt;
&lt;br /&gt;
: During the run you will see reports about the progress of the two independent runs, each consisting of three chains. Each line of output lists the generation number and the log likelihoods of the current tree/parameter combination for each of the two groups of three chains (a column of asterisks separate the results for the independent runs). The cold chains are the ones enclosed in brackets [...], while the heated chains are enclosed in parentheses (...). Occasionally the chains will swap so one of the heated chains now becomes cold (and sampling then takes place from this chain).&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Continue run until parallel runs converge on same solution&#039;&#039;&#039;&lt;br /&gt;
:At the end of the run, Mrbayes will print the average standard deviation of split frequencies (which is a measure of how similar the tree samples of the two independent runs are). We recommend that you continue with the analysis until the value gets below 0.01 (if the value is larger than 0.01 then you should answer &amp;quot;yes&amp;quot; when the program asks &amp;quot;Continue the analysis? (yes/no)&amp;quot;.)&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Question: &#039;&#039;&#039;MrBayes starts two independent runs from different random trees. Why is it useful to run two independent analyses instead of just one? How does the average standard deviation of split frequencies help you decide whether the two runs have converged to the same posterior distribution? At approximately how many generations does this happen in your run?&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Question 2&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Have a look at the resulting sample files&#039;&#039;&#039;&lt;br /&gt;
: Open a new Terminal window (don&#039;t quit mrbayes in the other terminal yet!) and cd to the bayes directory. Open one of the parameter sampling files in a text editor:&lt;br /&gt;
 nedit primatemitDNA.nexus.run1.p &amp;amp;&lt;br /&gt;
: This file contains one line for each sampled point (you may want to turn off line-wrapping in nedit under the preferences menu). Each row corresponds to a certain sample time (or generation). Each column contains the sampled values of one specific parameter. The first line contains headings telling what the different columns are: &lt;br /&gt;
:* Gen: generation; number of MCMC steps taken so far&lt;br /&gt;
:* lnL: log likelihood of the current parameter estimates&lt;br /&gt;
:* LnPr: log of the prior probability&lt;br /&gt;
:* TL: tree length (sum of all branch lengths)&lt;br /&gt;
:* kappa: transition/transversion rate ratio&lt;br /&gt;
:* pi(A), pi(C), pi(G), pi(T): frequency of A, C, G, T&lt;br /&gt;
:* alpha: shape parameter for the gamma distribution. &lt;br /&gt;
&lt;br /&gt;
: (Column headings may be shifted relative to their corresponding columns). Note how the values of most parameters change a lot during the initial &amp;quot;burnin&amp;quot; period, before they settle near their most probable values. &lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Question: &#039;&#039;&#039;You will notice that lnL is always negative, while LnPr can sometimes be positive. At first sight this may seem impossible, since probabilities cannot be larger than 1. How can this happen?&lt;br /&gt;
&lt;br /&gt;
As a hint, note that (1) priors for continuous parameters are probability densities, and (2) the default prior for each branch length in MrBayes is an exponential distribution with rate 10. Use the following R code to plot this prior on both an ordinary y-axis and a log-scaled y-axis, and then explain why positive values of LnPr are possible.&lt;br /&gt;
&lt;br /&gt;
 df_expdist = tibble(&lt;br /&gt;
   x = seq(0, 1, by = 0.001),&lt;br /&gt;
   density = dexp(x, rate = 10),&lt;br /&gt;
   logdensity = log(dexp(x, rate = 10))&lt;br /&gt;
 )&lt;br /&gt;
 &lt;br /&gt;
 ggplot(df_expdist, aes(x = x, y = density)) +&lt;br /&gt;
   geom_line(color=&amp;quot;blue&amp;quot;) +&lt;br /&gt;
   geom_hline(yintercept = 1, lty=2) +&lt;br /&gt;
   labs(&lt;br /&gt;
     x = &amp;quot;Branch length&amp;quot;,&lt;br /&gt;
     y = &amp;quot;Probability density&amp;quot;,&lt;br /&gt;
     title = &amp;quot;Exponential prior on branch lengths: Exp(rate = 10)&amp;quot;&lt;br /&gt;
   )&lt;br /&gt;
 &lt;br /&gt;
 ggplot(df_expdist, aes(x = x, y = logdensity)) +&lt;br /&gt;
   geom_line(color=&amp;quot;blue&amp;quot;) +&lt;br /&gt;
   geom_hline(yintercept = 0, lty=2) +&lt;br /&gt;
   labs(&lt;br /&gt;
     x = &amp;quot;Branch length&amp;quot;,&lt;br /&gt;
     y = &amp;quot;log of Probability density&amp;quot;,&lt;br /&gt;
     title = &amp;quot;Exponential prior on branch lengths: Exp(rate = 10)&amp;quot;&lt;br /&gt;
   )&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Question 3&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Examine MCMC trajectory for gamma shape parameter, alpha&#039;&#039;&#039;&lt;br /&gt;
: Recall that the idea in MCMCMC sampling is to move around in parameter space in such a way that points are visited according to their posterior probability (i.e., regions with high posterior probability are visited frequently). Now, in RStudio, plot the sampled values for the gamma shape parameter, alpha, for one of the run files:&lt;br /&gt;
 df_primates = read_tsv(&amp;quot;primatemitDNA.nexus.run1.p&amp;quot;, skip=1)&lt;br /&gt;
 mcmc_trace(df_primates, pars=&amp;quot;alpha&amp;quot;)&lt;br /&gt;
: mcmc_trace is one of several plotting commands available in the bayesplot package. This command plots the sampled values of the parameter alpha from the first of the two parallel runs against MCMC generation number. Thus, the x-axis shows the progress of the run through time, with the leftmost values being the earliest samples and the rightmost values the later ones. Note how the Markov chain starts at the arbitrary value 1.0, rapidly moves to values that fit the observed data better, and then moves around in parameter space, sampling different plausible values of alpha. You can experiment with plotting other columns as well.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Question: &#039;&#039;&#039;Describe briefly what happens to the sampled values of alpha during the run. Why is it reasonable to discard the earliest samples as burn-in?&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Question 4&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Investigate posterior probability distribution over trees&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
: Now, close the nedit window and have a look at the file containing sampled trees:&lt;br /&gt;
 nedit primatemitDNA.nexus.run1.t &amp;amp;&lt;br /&gt;
: Tree topology is also a parameter in our model, and exactly like for the other parameters we also get samples from tree-space. One tree is printed per line in the parenthetical Newick format you have seen before. There are 5 taxa in the present data set, so the number of possible unrooted binary tree topologies is only 15. Since we have taken more than 15 sample points, there must be several lines containing the same tree topology. Close the nedit window when you are done.&lt;br /&gt;
&lt;br /&gt;
: MrBayes provides the sumt command to summarize the sampled trees. Before using it, we need to decide on the burn-in: The burn-in is the initial set of samples that are typically discarded, because we want to ensure that the MCMC has moved away from the random starting values, and has found the peaks of the probability landscape. Since the convergence diagnostic used a relative burn-in of 25%, we will also discard the first 25% of tree samples when summarizing the posterior.&lt;br /&gt;
&lt;br /&gt;
: Return to the shell window where you have MrBayes running. In the command below relburnin=yes and burninfrac=0.25 tells MrBayes to discard 25% of the samples as burnin (you could also have explicitly given the number of samples to discard - help sumt will give you details about the command and the current option settings).&lt;br /&gt;
 sumt contype=halfcompat conformat=simple relburnin=yes burninfrac=0.25 showtreeprobs=yes&lt;br /&gt;
: (Scroll back so you can see the top of the output when the command is done). This command gives you a summary of the trees that are in the file you examined manually above. The option contype=halfcompat requests that a majority rule consensus tree is calculated from the set of trees that are left after discarding the burnin. This consensus is the first tree plotted to the screen. Below the consensus cladogram, a consensus phylogram is plotted. The branch lengths in this have been averaged over the trees in which that branch was present (a particular branch corresponds to a bi-partition of the data, and will typically not be present in every sampled tree). The cladogram also has &amp;quot;clade credibility&amp;quot; values. We will return to the meaning of these later in today&#039;s exercise.&lt;br /&gt;
&lt;br /&gt;
: What most interests us right now is the list of trees that is printed after the phylogram. These trees are labeled &amp;quot;Tree 1&amp;quot;, &amp;quot;Tree 2&amp;quot;, etc, and are sorted according to their posterior probability which is indicated by a lower-case p after the tree number. (The upper-case P gives the cumulated probability of trees shown so far, and is useful for constructing a credible set). This list highlights how Bayesian phylogenetic analysis is different from maximum likelihood: Instead of finding the best tree(s), we here quantify our degree of belief in all possible trees.&lt;br /&gt;
&lt;br /&gt;
: The list of trees and probabilities was printed because of the option showtreeprobs=yes. Note that you probably do not want to issue that command if you have much more than 5 taxa! In that case you could instead inspect the file named primatemitDNA.nexus.trprobs which is now present in the same directory as your other files (this file is automatically produced by the sumt command).&lt;br /&gt;
&lt;br /&gt;
: &#039;&#039;&#039;NOTE&#039;&#039;&#039;: Annoyingly, there is a bug in the version of mrbayes we are using here, which means leaf names are not printed on the list of trees with probabilities. However, the most probable tree here in fact is identical to the consensus tree printed above it.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Question: &#039;&#039;&#039;What is the posterior probability of the most probable tree? Does the analysis strongly support a single tree, or is the posterior probability distributed across several different trees?&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
== Analysis of Neanderthal data (posterior probability of clades) ==&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Question 5&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
For many years, there was considerable debate about the origin of modern humans. One view, often called the [https://www.thoughtco.com/multiregional-hypothesis-167235 Multiregional Hypothesis], proposed that after Homo erectus spread from Africa into different parts of the world, regional populations gradually evolved into modern humans more or less in parallel. A different view, often called the Recent African Origin model, proposed that modern Homo sapiens evolved in Africa and later spread outward, largely replacing other archaic human groups such as the Neanderthals.&lt;br /&gt;
&lt;br /&gt;
Today it is clear that the history is more complicated than either simple extreme: modern humans arose in Africa, but there was also some interbreeding with Neanderthals and other archaic humans. However, in this exercise we will focus on a narrower question that can be addressed using a phylogeny of mitochondrial DNA: do the sampled Neanderthal and human mitochondrial sequences suggest that the Neanderthal sequence falls inside or outside modern human mitochondrial diversity?&lt;br /&gt;
&lt;br /&gt;
We will use the present data set to examine this question.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Load Neanderthal data set&#039;&#039;&#039;&lt;br /&gt;
: In the Terminal where you have MrBayes running:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
execute neanderthal.nexus&lt;br /&gt;
delete 5-40&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
: As we did for the maximum likelihood analysis, we will discard some of the human sequences in order to speed up the analysis. The command delete 5-40 removes sequence number 5 to sequence number 40 from the active data set.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Investigate data&#039;&#039;&#039;&lt;br /&gt;
 showmatrix&lt;br /&gt;
: This data set consists of an alignment of mitochondrial DNA from human (17 sequences), chimpanzee (1 sequence), and Neanderthal (1 sequence). The Neanderthal DNA was extracted from archaeological material, specifically bones found at Vindija in Croatia.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Start analysis&#039;&#039;&#039;&lt;br /&gt;
 outgroup Pan_troglodytes&lt;br /&gt;
 lset nst=mixed rates=gamma&lt;br /&gt;
 mcmc ngen=500000 nchains=3 diagnfreq=10000&lt;br /&gt;
&lt;br /&gt;
: Here we use the command `nst=mixed` which allows MrBayes to automatically explore all possible substitution models. Essentially, MrBayes now considers the substitution model as one more parameter, and uses MCMC to sample from the possible versions (with nst ranging from 1 to 6). This will often be the best choice when using MrBayes. (Below, I use nst=6 for pedagogical purposes, because it makes it simpler to analyse the output files).&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Find posterior probability of clades&#039;&#039;&#039;&lt;br /&gt;
: When the run has finished, issue this command to compute a consensus tree:&lt;br /&gt;
 sumt contype=halfcompat showtreeprobs=no relburnin=yes burninfrac=0.25&lt;br /&gt;
: Examine the consensus tree that is plotted to screen: On the branches that are resolved, you will notice that numbers have been plotted. These are clade-credibility values, and are in fact the posterior probability that the clade is real (based on the present data set).&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Question: &#039;&#039;&#039;What is the posterior probability that all sampled Homo sapiens sequences form a monophyletic group excluding the Neanderthal sequence? Does this support placing the Neanderthal outside modern human mitochondrial diversity?&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
== Probability distributions over other parameters ==&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Question 6&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
: As the last thing, we will now turn away from the tree topology, and instead examine the other parameters that also form part of the probabilistic model. We will do this using a reduced version of the Hepatitis C virus data set that we have examined previously. Stay in the shell window where you just performed the analysis of Neanderthal sequences.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Load data set&#039;&#039;&#039;&lt;br /&gt;
 execute hcvsmall.nexus&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Define site partition&#039;&#039;&#039;&lt;br /&gt;
 charset 1stpos=1-.\3&lt;br /&gt;
 charset 2ndpos=2-.\3&lt;br /&gt;
 charset 3rdpos=3-.\3&lt;br /&gt;
 partition bycodon = 3:1stpos,2ndpos,3rdpos&lt;br /&gt;
 set partition=bycodon&lt;br /&gt;
 prset ratepr=variable&lt;br /&gt;
: This is an alternative way of specifying that different sites may evolve at different rates. With a gamma model, we allow rates to vary across sites but do not specify in advance which sites are fast or slow; instead, that pattern is inferred from the data. Here we instead use prior biological knowledge about the structure of the genetic code to divide sites into three classes: 1st, 2nd, and 3rd codon positions. We then allow each class to have its own rate, so that all 1st positions share one rate, all 2nd positions another, and all 3rd positions a third. Specifically, charset 1stpos=1-.\3 defines a character set named 1stpos consisting of site 1 followed by every third site (\3, i.e. sites 1, 4, 7, 10, …), continuing until the end of the alignment (denoted .).&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Specify model&#039;&#039;&#039;&lt;br /&gt;
 lset nst=6&lt;br /&gt;
: This specifies that we want to use a model of the General Time Reversible (GTR) type, where all 6 substitution types have separate rate parameters.&lt;br /&gt;
&lt;br /&gt;
: When the lset command was discussed previously, a few issues were glossed over. Importantly, and unlike PAUP, the lset command in MrBayes gives no information about whether nucleotide frequencies are equal or not, and whether they should be estimated from the data or not. In MrBayes this is instead controlled by defining the prior probability of the nucleotide frequencies (the command prset can be used to set priors). For instance, a model with equal nucleotide frequencies corresponds to having prior probability 1 (one) for the frequency vector (A=0.25, C=0.25, G=0.25, T=0.25), and zero prior probability for the infinitely many other possible frequency vectors. As you will see below, the default prior is not this limited, and the program will therefore estimate the frequencies from the data.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Inspect model details&#039;&#039;&#039;&lt;br /&gt;
 showmodel&lt;br /&gt;
: This command gives you a summary of the current model settings. You will also get a summary of how the prior probabilities of all model parameters are set. You will for instance notice that the nucleotide frequencies (parameter labeled &amp;quot;Statefreq&amp;quot;) have a &amp;quot;Dirichlet&amp;quot; prior. Without going into details, the Dirichlet distribution is a probability distribution over frequency vectors (i.e., vectors of positive values that sum to 1). Depending on the exact parameters the distribution can be more or less flat (flat here means that all sum-1 vectors are equally probable). The Dirichlet distribution is a handy way of specifying the prior probability distribution of nucleotide (or amino acid) frequency vectors. The default statefreq prior in MrBayes is the flat or un-informative prior dirichlet(1,1,1,1).&lt;br /&gt;
&lt;br /&gt;
: We will not go into the priors for the remaining parameters in any detail, but you may notice that by default all topologies are taken to be equally likely (a flat prior on trees).&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Start MCMC sampling&#039;&#039;&#039;&lt;br /&gt;
 mcmc ngen=1000000 samplefreq=100 diagnfreq=10000 nchains=3&lt;br /&gt;
: The run will take a while to finish (you may want to ensure that the average standard deviation of split frequencies is less than 0.01 before ending the analysis).&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Compute summary of parameter values&#039;&#039;&#039;&lt;br /&gt;
 sump relburnin=yes burninfrac=0.25&lt;br /&gt;
: The sump command (with a &amp;quot;p&amp;quot; at the end) works much like the sumt command (with a &amp;quot;t&amp;quot; at the end), but for other parameters than the tree-topology. Again, we are using 25% of the total number of samples as burnin.&lt;br /&gt;
&lt;br /&gt;
: First, you get a scatter plot of the lnL as a function of generation number. Values from the two independent runs are labeled &amp;quot;1&amp;quot; and &amp;quot;2&amp;quot; respectively. If the burnin is suitable, then the points should be randomly scattered over a narrow lnL interval.&lt;br /&gt;
&lt;br /&gt;
: Secondly, the posterior probability distribution of each parameter is summarized by giving the mean, variance, median, and 95% credible interval.&lt;br /&gt;
&lt;br /&gt;
: The last columns contain values indicating if the run has converged. Specifically ESS means Effective Sample Size, and is a measure of how many independent samples you have from the posterior - the higher the better, but this should be at least 100. The column labeled PSRF+ is a measure (also known as &amp;quot;R-hat&amp;quot;) which should be close to 1 if the runs have converged. Specifically this measures whether different chains (and different parts of different chains) converge to sample the same set of values. &lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Question: &#039;&#039;&#039;What are the posterior mean values of the relative substitution rate parameters r(AC) and r(CG)?&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Question 7: &#039;&#039;&#039; Based on the reported posterior means, does r(CG) appear to be larger than r(AC)?&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Question 8&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Marginal vs. joint distributions&#039;&#039;&#039;&lt;br /&gt;
: Strictly speaking the comparison above was not entirely appropriate. We first found the overall distribution of the r(CG) parameter and then compared its mean to the mean of the overall distribution of the r(AC) parameter. By doing things this way, we are ignoring the possibility that the two parameters might be associated in some way. For instance, one parameter might always be larger than the other in any individual sample, even though the total distributions overlap. We should instead be looking at the distribution over both parameters simultaneously. A probability distribution over several parameters simultaneously is called a &amp;quot;joint distribution&amp;quot; over the parameters.&lt;br /&gt;
&lt;br /&gt;
: By looking at one parameter at a time, we are summing its probability over all values of the other parameters. This is called the marginal distribution.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Examine marginal distributions&#039;&#039;&#039;&lt;br /&gt;
: In RStudio, use the following commands to read and plot the marginal distributions of r(AC) and r(CG). Note that we are discarding the first 25% of the reads as burnin&lt;br /&gt;
 df_hcv = read_tsv(&amp;quot;hcvsmall.nexus.run1.p&amp;quot;, skip=1)&lt;br /&gt;
 burnin = df_hcv$Gen %&amp;gt;% &lt;br /&gt;
     max() %&amp;gt;% &lt;br /&gt;
     multiply_by(0.25) %&amp;gt;% &lt;br /&gt;
     floor()&lt;br /&gt;
 df_hcv2 = df_hcv %&amp;gt;% &lt;br /&gt;
     filter(Gen &amp;gt; burnin) %&amp;gt;%&lt;br /&gt;
     select(CG = `r(C&amp;lt;-&amp;gt;G){all}`,&lt;br /&gt;
            AC = `r(A&amp;lt;-&amp;gt;C){all}`&lt;br /&gt;
            )&lt;br /&gt;
 mcmc_intervals(df_hcv2, prob_outer = 1)&lt;br /&gt;
 mcmc_areas(df_hcv2, prob_outer = 1)&lt;br /&gt;
: The functions mcmc_intervals and mcmc_areas plot different views of the same posterior distributions. &lt;br /&gt;
&lt;br /&gt;
: You can also simply plot the data using ggplot:&lt;br /&gt;
 df_hcv2_long = pivot_longer(df_hcv2, cols = c(&amp;quot;CG&amp;quot;, &amp;quot;AC&amp;quot;))&lt;br /&gt;
 &lt;br /&gt;
 ggplot(df_hcv2_long) + &lt;br /&gt;
     geom_density(mapping=aes(x=value, fill=name), alpha=0.3) + &lt;br /&gt;
     labs(x=&amp;quot;Substitution rate&amp;quot;)&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Question&#039;&#039;&#039;: Based on the marginal distributions, r(AC) appears to be centered at a higher value than r(CG), but the two distributions overlap somewhat. Can you from these marginal distributions alone decide whether r(AC) is larger than r(CG) in most posterior samples? Why or why not?&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Question 9&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Examine joint distributions&#039;&#039;&#039;&lt;br /&gt;
: These plots and results explore the relationship between the A&amp;lt;-&amp;gt;C and C&amp;lt;-&amp;gt;G rates.&lt;br /&gt;
 ggplot(df_hcv2, aes(x=CG, y=AC)) + &lt;br /&gt;
     geom_point(col=&amp;quot;blue&amp;quot;) + &lt;br /&gt;
     geom_abline(intercept=0, slope=1, lty=2, col=&amp;quot;red&amp;quot;) +&lt;br /&gt;
     xlim(0,0.25) + &lt;br /&gt;
     ylim(0,0.25) + &lt;br /&gt;
     labs(x=&amp;quot;CG rate&amp;quot;, y =&amp;quot;AC rate&amp;quot;) &lt;br /&gt;
 &lt;br /&gt;
 ggplot(df_hcv2, aes(x=CG, y= AC)) + &lt;br /&gt;
     geom_hex(col=&amp;quot;blue&amp;quot;) + &lt;br /&gt;
     geom_abline(intercept=0, slope=1, lty=2, col=&amp;quot;red&amp;quot;) +&lt;br /&gt;
     xlim(0,0.25) + &lt;br /&gt;
     ylim(0, 0.25) + &lt;br /&gt;
     labs(x=&amp;quot;CG rate&amp;quot;, y =&amp;quot;AC rate&amp;quot;) &lt;br /&gt;
 &lt;br /&gt;
 df_hcv2 %&amp;gt;%&lt;br /&gt;
     nrow()&lt;br /&gt;
 df_hcv2 %&amp;gt;%&lt;br /&gt;
     filter(AC&amp;gt;CG) %&amp;gt;%&lt;br /&gt;
     nrow()&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Question: &#039;&#039;&#039;Based on the two different ways to plot the joint distribution and based on the unfiltered and filtered row counts, what is the posterior probability that r(AC) &amp;gt; r(CG)?&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Question 10&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
: Note how examining the joint distribution provides you with information that you could not get from simply comparing the marginal distributions. This very simple procedure can be used to answer many different questions.&lt;br /&gt;
&lt;br /&gt;
: Now, plot the relative substitution rates at the first, second, and third codon positions:&lt;br /&gt;
 df_hcv3 = df_hcv %&amp;gt;% &lt;br /&gt;
     filter(Gen &amp;gt; 75000) %&amp;gt;%&lt;br /&gt;
     select(Codon_1st = `m{1}`,&lt;br /&gt;
            Codon_2nd = `m{2}`,&lt;br /&gt;
            Codon_3rd = `m{3}` ) %&amp;gt;%&lt;br /&gt;
     pivot_longer(cols=c(&amp;quot;Codon_1st&amp;quot;, &amp;quot;Codon_2nd&amp;quot;, &amp;quot;Codon_3rd&amp;quot;))&lt;br /&gt;
 &lt;br /&gt;
 ggplot(df_hcv3) + &lt;br /&gt;
     geom_density(mapping=aes(x=value, fill=name), alpha=0.3) + &lt;br /&gt;
     labs(x=&amp;quot;Relative substitution rate&amp;quot;)&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Question: &#039;&#039;&#039;Since random mutations presumably hit all three codon positions with the same frequency, any differences are expected to be caused by subsequent selection. Which of the following statement are correct ? (More than one answer may be correct)&lt;br /&gt;
&lt;br /&gt;
:* Codon position 2 is the most variable of the codon positions.&lt;br /&gt;
:* Codon position 1 is the most variable of the codon positions.&lt;br /&gt;
:* Codon position 1 is the most conserved codon position.&lt;br /&gt;
:* Codon position 3 is the most conserved codon position.&lt;br /&gt;
:* Codon position 3 is the most variable of the codon positions.&lt;br /&gt;
:* Codon position 2 is the most conserved codon position.&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Question 11&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Question: &#039;&#039;&#039;How does this result fit with your knowledge of the genetic code? Why are these codon positions the most conserved or the most variable?&lt;/div&gt;</summary>
		<author><name>Gorm</name></author>
	</entry>
	<entry>
		<id>https://teaching.healthtech.dtu.dk/22115/index.php?title=Bayesian_Phylogeny&amp;diff=249</id>
		<title>Bayesian Phylogeny</title>
		<link rel="alternate" type="text/html" href="https://teaching.healthtech.dtu.dk/22115/index.php?title=Bayesian_Phylogeny&amp;diff=249"/>
		<updated>2026-03-19T10:29:25Z</updated>

		<summary type="html">&lt;p&gt;Gorm: /* Probability distributions over other parameters */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;This exercise is part of the course  [[22115_-_Computational_Molecular_Evolution|Computational Molecular Evolution (22115)]].&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Overview ==&lt;br /&gt;
&lt;br /&gt;
Today&#039;s exercise will focus on phylogenetic analysis using Bayesian methods.&lt;br /&gt;
&lt;br /&gt;
As was the case for likelihood methods, Bayesian analysis is founded on having a probabilistic model of how the observed data is produced. This means that, for a given set of parameter values, you can compute the probability or [https://www.statlect.com/glossary/probability-density-function probability density] of any possible observation. For a full dataset, you then obtain the likelihood by multiplying these values across all observations. You will recall from the lecture that in Bayesian statistics the goal is to obtain a full posterior probability distribution over all possible parameter values. The posterior distribution quantifies our degree of belief in any possible parameter value after seeing the data. It is obtained by updating the prior probability distribution using the likelihood of the observed data.&lt;br /&gt;
&lt;br /&gt;
The prior probability distribution expresses your beliefs about the parameters before seeing any data, while the likelihood expresses what the observed data are telling you about the parameters. Specifically, the likelihood of a parameter value is the probability of the observed data given that parameter value. We regard a parameter value as more plausible the more probable it makes the observed data. This is the same measure we have previously used to find the maximum likelihood estimate. If the prior probability distribution is flat (i.e., if all possible parameter values have the same prior probability), then the posterior distribution is proportional to the likelihood, and the parameter value with the maximum likelihood also has the maximum posterior probability. However, even in this case, using a Bayesian approach still lets you interpret the result as a probability distribution over parameter values. &lt;br /&gt;
&lt;br /&gt;
If the prior is not flat, then it may have a substantial impact on the posterior, although this effect will usually diminish as the amount of data increases. A prior should ideally be based on domain knowledge and results from previous experiments. For instance one can use the posterior from one analysis as the prior in a new, independent analysis. Often a prior is chosen to be weakly informative, meaning that it places reasonable bounds on the parameter values without constraining them too narrowly. For instance the transition/transversion rate ratio kappa is typically 1.5-10. Values such as 100, 1,000 or 1,000,000 would be extremely unlikely, so a weakly informative prior for this parameter could be chosen to place 95% of its probability mass in the 0.5-20 range, slightly wider than what we think of as plausible values. For instance one could use a lognormal distribution with suitable parameters.&lt;br /&gt;
&lt;br /&gt;
In Bayesian phylogeny the parameters are of the same kind as in maximum likelihood phylogeny. Typical parameters include tree topology, branch lengths, nucleotide frequencies, and substitution model parameters such as the transition/transversion rate ratio or the gamma shape parameter. The difference is that, whereas in maximum likelihood phylogeny we seek the best point estimates of the parameter values, in Bayesian phylogeny the goal is instead to infer a full probability distribution over the possible parameter values. The observed data are again usually taken to be the alignment, although strictly speaking it would be more reasonable to say that the sequences are what have been observed, and that the alignment should then be inferred jointly with the phylogeny.&lt;br /&gt;
&lt;br /&gt;
In this exercise we will explore how one can determine and use posterior probability distributions over trees, over clades, and over substitution parameters. We will also touch upon the difference between marginal and joint probability distributions.&lt;br /&gt;
&lt;br /&gt;
== Getting started ==&lt;br /&gt;
&lt;br /&gt;
: In the command below: Instead of /path/to/molevol enter the path to the directory where you have placed your course files (for instance cd /Users/bob/Documents/molevol, or cd /home/student/molevol).&lt;br /&gt;
 cd /path/to/molevol&lt;br /&gt;
 mkdir bayes&lt;br /&gt;
 cd bayes&lt;br /&gt;
 cp ../data/primatemitDNA.nexus ./primatemitDNA.nexus&lt;br /&gt;
 cp ../data/neanderthal.nexus ./neanderthal.nexus&lt;br /&gt;
 cp ../data/hcvsmall.nexus ./hcvsmall.nexus&lt;br /&gt;
&lt;br /&gt;
: You have analyzed (versions of) all these data files previously in this course. We will now use Bayesian phylogenetic analysis to complement what we learned in those analyses.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Load R libraries&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
: In RStudio: set the working directory to the bayes directory. Then issue these commands:&lt;br /&gt;
 library(magrittr)&lt;br /&gt;
 library(tidyverse)&lt;br /&gt;
 library(bayesplot)&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
== Posterior probability of trees ==&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Question 1&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
: In today&#039;s exercise we will be using the program &amp;quot;MrBayes&amp;quot; to perform Bayesian phylogenetic analysis. MrBayes is a program that, like PAUP*, can be controlled by giving commands at a command line prompt. In fact, there is a substantial overlap between the commands used to control MrBayes and the PAUP command language. This should be a help when you are trying to understand how to use the program.&lt;br /&gt;
&lt;br /&gt;
: Note that the command &amp;quot;help&amp;quot; will give you a list of all available commands. Issuing &amp;quot;help &#039;&#039;command&#039;&#039;&amp;quot; will give you a more detailed description of the specified command along with current option values. This is similar to how &amp;quot;help &#039;&#039;command&#039;&#039;&amp;quot; works in PAUP.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Start program&#039;&#039;&#039;&lt;br /&gt;
: In a terminal window, issue the command:&lt;br /&gt;
 mb&lt;br /&gt;
: This starts the program, giving you a prompt (&amp;quot;MrBayes&amp;gt; &amp;quot;) where you can enter commands.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Get a quick overview of available commands&#039;&#039;&#039;&lt;br /&gt;
 help&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Load your sequences&#039;&#039;&#039;&lt;br /&gt;
 execute primatemitDNA.nexus&lt;br /&gt;
: This file contains mitochondrial DNA sequences from 5 different primates. Note that MrBayes accepts input in nexus format, and that this is the same command that was used to load sequences in PAUP*. In general, you can use many of the PAUP commands in MrBayes also.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Inspect data set&#039;&#039;&#039;&lt;br /&gt;
 showmatrix&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Define outgroup&#039;&#039;&#039;&lt;br /&gt;
 outgroup Gibbon&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Specify your model of sequence evolution&#039;&#039;&#039;&lt;br /&gt;
 lset nst=2 rates=gamma&lt;br /&gt;
: This command is again very much like the corresponding one in PAUP. You are specifying that you want to use a model with two substitution types (nst=2), and this is automatically taken to mean that you want to distinguish between transitions and transversions. Furthermore, rates=gamma means that you want the model to use a gamma distribution to account for different rates at different sites in the sequence.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Start Markov chain Monte Carlo sampling&#039;&#039;&#039;&lt;br /&gt;
:Make sure to make the shell window as wide as possible and then issue the following commands to start the run:&lt;br /&gt;
 mcmc ngen=1000000 samplefreq=100 nchains=3 diagnfreq=5000&lt;br /&gt;
: What you are doing here is to use the method known as MCMCMC (&amp;quot;Metropolis-coupled Markov chain Monte Carlo&amp;quot;) to empirically determine the posterior probability distribution of trees, branch lengths and substitution parameters. Recall that in the Bayesian framework this is how we learn about parameter values: instead of finding the best point estimates, we typically want to quantify the probability of the entire range of possible values. An estimate of the time left is shown in the last column of output.&lt;br /&gt;
&lt;br /&gt;
: Let us examine the command in detail. First, ngen=1000000 samplefreq=100 lets the search run for 1,000,000 MCMC steps (&amp;quot;generations&amp;quot;) and saves parameter values once every 100 rounds (meaning that a total of 10,000 sets of parameter values will be saved to sample files). You sometimes need to run longer (or shorter) than 1,000,000, and would then typically tweak samplefreq so you get around 1,000 - 10,000 samples in all. The option nchains=3 means that the MCMCMC sampling uses 3 parallel chains (but see below): one &amp;quot;cold&amp;quot; from which sampling takes place, and two &amp;quot;heated&amp;quot; that move around in the parameter space more quickly to find additional peaks in the probability distribution.&lt;br /&gt;
&lt;br /&gt;
: The option diagnfreq=5000 has to do with testing whether the MrBayes run is successful. Briefly, MrBayes will start two entirely independent runs starting from different random trees. In the early phases of the run, the two runs will sample very different trees, but when they have reached convergence (when they produce a good sample from the posterior probability distribution), the two tree samples should be very similar. Every diagnfreq generations, the program will compute a measure of how similar the tree samples are, specifically the average standard deviation of split frequencies. A “split” is the same as a bipartition, i.e. a division of all leaves in the tree into two groups, obtained by cutting an internal branch. For each split, MrBayes compares how often that split occurs in the two independent runs; if the runs have converged, these frequencies should be very similar, giving a small standard deviation. The program then averages this quantity across splits. As a rule of thumb, you may want to run until this value is less than 0.05 (the smaller the better)&lt;br /&gt;
&lt;br /&gt;
: During the run you will see reports about the progress of the two independent runs, each consisting of three chains. Each line of output lists the generation number and the log likelihoods of the current tree/parameter combination for each of the two groups of three chains (a column of asterisks separate the results for the independent runs). The cold chains are the ones enclosed in brackets [...], while the heated chains are enclosed in parentheses (...). Occasionally the chains will swap so one of the heated chains now becomes cold (and sampling then takes place from this chain).&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Continue run until parallel runs converge on same solution&#039;&#039;&#039;&lt;br /&gt;
:At the end of the run, Mrbayes will print the average standard deviation of split frequencies (which is a measure of how similar the tree samples of the two independent runs are). We recommend that you continue with the analysis until the value gets below 0.01 (if the value is larger than 0.01 then you should answer &amp;quot;yes&amp;quot; when the program asks &amp;quot;Continue the analysis? (yes/no)&amp;quot;.)&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Question: &#039;&#039;&#039;MrBayes starts two independent runs from different random trees. Why is it useful to run two independent analyses instead of just one? How does the average standard deviation of split frequencies help you decide whether the two runs have converged to the same posterior distribution? At approximately how many generations does this happen in your run?&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Question 2&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Have a look at the resulting sample files&#039;&#039;&#039;&lt;br /&gt;
: Open a new Terminal window (don&#039;t quit mrbayes in the other terminal yet!) and cd to the bayes directory. Open one of the parameter sampling files in a text editor:&lt;br /&gt;
 nedit primatemitDNA.nexus.run1.p &amp;amp;&lt;br /&gt;
: This file contains one line for each sampled point (you may want to turn off line-wrapping in nedit under the preferences menu). Each row corresponds to a certain sample time (or generation). Each column contains the sampled values of one specific parameter. The first line contains headings telling what the different columns are: &lt;br /&gt;
:* Gen: generation; number of MCMC steps taken so far&lt;br /&gt;
:* lnL: log likelihood of the current parameter estimates&lt;br /&gt;
:* LnPr: log of the prior probability&lt;br /&gt;
:* TL: tree length (sum of all branch lengths)&lt;br /&gt;
:* kappa: transition/transversion rate ratio&lt;br /&gt;
:* pi(A), pi(C), pi(G), pi(T): frequency of A, C, G, T&lt;br /&gt;
:* alpha: shape parameter for the gamma distribution. &lt;br /&gt;
&lt;br /&gt;
: (Column headings may be shifted relative to their corresponding columns). Note how the values of most parameters change a lot during the initial &amp;quot;burnin&amp;quot; period, before they settle near their most probable values. &lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Question: &#039;&#039;&#039;You will notice that lnL is always negative, while LnPr can sometimes be positive. At first sight this may seem impossible, since probabilities cannot be larger than 1. How can this happen?&lt;br /&gt;
&lt;br /&gt;
As a hint, note that (1) priors for continuous parameters are probability densities, and (2) the default prior for each branch length in MrBayes is an exponential distribution with rate 10. Use the following R code to plot this prior on both an ordinary y-axis and a log-scaled y-axis, and then explain why positive values of LnPr are possible.&lt;br /&gt;
&lt;br /&gt;
 df_expdist = tibble(&lt;br /&gt;
   x = seq(0, 1, by = 0.001),&lt;br /&gt;
   density = dexp(x, rate = 10),&lt;br /&gt;
   logdensity = log(dexp(x, rate = 10))&lt;br /&gt;
 )&lt;br /&gt;
 &lt;br /&gt;
 ggplot(df_expdist, aes(x = x, y = density)) +&lt;br /&gt;
   geom_line(color=&amp;quot;blue&amp;quot;) +&lt;br /&gt;
   geom_hline(yintercept = 1, lty=2) +&lt;br /&gt;
   labs(&lt;br /&gt;
     x = &amp;quot;Branch length&amp;quot;,&lt;br /&gt;
     y = &amp;quot;Probability density&amp;quot;,&lt;br /&gt;
     title = &amp;quot;Exponential prior on branch lengths: Exp(rate = 10)&amp;quot;&lt;br /&gt;
   )&lt;br /&gt;
 &lt;br /&gt;
 ggplot(df_expdist, aes(x = x, y = logdensity)) +&lt;br /&gt;
   geom_line(color=&amp;quot;blue&amp;quot;) +&lt;br /&gt;
   geom_hline(yintercept = 0, lty=2) +&lt;br /&gt;
   labs(&lt;br /&gt;
     x = &amp;quot;Branch length&amp;quot;,&lt;br /&gt;
     y = &amp;quot;log of Probability density&amp;quot;,&lt;br /&gt;
     title = &amp;quot;Exponential prior on branch lengths: Exp(rate = 10)&amp;quot;&lt;br /&gt;
   )&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Question 3&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Examine MCMC trajectory for gamma shape parameter, alpha&#039;&#039;&#039;&lt;br /&gt;
: Recall that the idea in MCMCMC sampling is to move around in parameter space in such a way that points are visited according to their posterior probability (i.e., regions with high posterior probability are visited frequently). Now, in RStudio, plot the sampled values for the gamma shape parameter, alpha, for one of the run files:&lt;br /&gt;
 df_primates = read_tsv(&amp;quot;primatemitDNA.nexus.run1.p&amp;quot;, skip=1)&lt;br /&gt;
 mcmc_trace(df_primates, pars=&amp;quot;alpha&amp;quot;)&lt;br /&gt;
: mcmc_trace is one of several plotting commands available in the bayesplot package. This command plots the sampled values of the parameter alpha from the first of the two parallel runs against MCMC generation number. Thus, the x-axis shows the progress of the run through time, with the leftmost values being the earliest samples and the rightmost values the later ones. Note how the Markov chain starts at the arbitrary value 1.0, rapidly moves to values that fit the observed data better, and then moves around in parameter space, sampling different plausible values of alpha. You can experiment with plotting other columns as well.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Question: &#039;&#039;&#039;Describe briefly what happens to the sampled values of alpha during the run. Why is it reasonable to discard the earliest samples as burn-in?&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Question 4&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Investigate posterior probability distribution over trees&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
: Now, close the nedit window and have a look at the file containing sampled trees:&lt;br /&gt;
 nedit primatemitDNA.nexus.run1.t &amp;amp;&lt;br /&gt;
: Tree topology is also a parameter in our model, and exactly like for the other parameters we also get samples from tree-space. One tree is printed per line in the parenthetical Newick format you have seen before. There are 5 taxa in the present data set, so the number of possible unrooted binary tree topologies is only 15. Since we have taken more than 15 sample points, there must be several lines containing the same tree topology. Close the nedit window when you are done.&lt;br /&gt;
&lt;br /&gt;
: MrBayes provides the sumt command to summarize the sampled trees. Before using it, we need to decide on the burn-in: The burn-in is the initial set of samples that are typically discarded, because we want to ensure that the MCMC has moved away from the random starting values, and has found the peaks of the probability landscape. Since the convergence diagnostic used a relative burn-in of 25%, we will also discard the first 25% of tree samples when summarizing the posterior.&lt;br /&gt;
&lt;br /&gt;
: Return to the shell window where you have MrBayes running. In the command below relburnin=yes and burninfrac=0.25 tells MrBayes to discard 25% of the samples as burnin (you could also have explicitly given the number of samples to discard - help sumt will give you details about the command and the current option settings).&lt;br /&gt;
 sumt contype=halfcompat conformat=simple relburnin=yes burninfrac=0.25 showtreeprobs=yes&lt;br /&gt;
: (Scroll back so you can see the top of the output when the command is done). This command gives you a summary of the trees that are in the file you examined manually above. The option contype=halfcompat requests that a majority rule consensus tree is calculated from the set of trees that are left after discarding the burnin. This consensus is the first tree plotted to the screen. Below the consensus cladogram, a consensus phylogram is plotted. The branch lengths in this have been averaged over the trees in which that branch was present (a particular branch corresponds to a bi-partition of the data, and will typically not be present in every sampled tree). The cladogram also has &amp;quot;clade credibility&amp;quot; values. We will return to the meaning of these later in today&#039;s exercise.&lt;br /&gt;
&lt;br /&gt;
: What most interests us right now is the list of trees that is printed after the phylogram. These trees are labeled &amp;quot;Tree 1&amp;quot;, &amp;quot;Tree 2&amp;quot;, etc, and are sorted according to their posterior probability which is indicated by a lower-case p after the tree number. (The upper-case P gives the cumulated probability of trees shown so far, and is useful for constructing a credible set). This list highlights how Bayesian phylogenetic analysis is different from maximum likelihood: Instead of finding the best tree(s), we here quantify our degree of belief in all possible trees.&lt;br /&gt;
&lt;br /&gt;
: The list of trees and probabilities was printed because of the option showtreeprobs=yes. Note that you probably do not want to issue that command if you have much more than 5 taxa! In that case you could instead inspect the file named primatemitDNA.nexus.trprobs which is now present in the same directory as your other files (this file is automatically produced by the sumt command).&lt;br /&gt;
&lt;br /&gt;
: &#039;&#039;&#039;NOTE&#039;&#039;&#039;: Annoyingly, there is a bug in the version of mrbayes we are using here, which means leaf names are not printed on the list of trees with probabilities. However, the most probable tree here in fact is identical to the consensus tree printed above it.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Question: &#039;&#039;&#039;What is the posterior probability of the most probable tree? Does the analysis strongly support a single tree, or is the posterior probability distributed across several different trees?&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
== Analysis of Neanderthal data (posterior probability of clades) ==&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Question 5&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
For many years, there was considerable debate about the origin of modern humans. One view, often called the [https://www.thoughtco.com/multiregional-hypothesis-167235 Multiregional Hypothesis], proposed that after Homo erectus spread from Africa into different parts of the world, regional populations gradually evolved into modern humans more or less in parallel. A different view, often called the Recent African Origin model, proposed that modern Homo sapiens evolved in Africa and later spread outward, largely replacing other archaic human groups such as the Neanderthals.&lt;br /&gt;
&lt;br /&gt;
Today it is clear that the history is more complicated than either simple extreme: modern humans arose in Africa, but there was also some interbreeding with Neanderthals and other archaic humans. However, in this exercise we will focus on a narrower question that can be addressed using a phylogeny of mitochondrial DNA: do the sampled Neanderthal and human mitochondrial sequences suggest that the Neanderthal sequence falls inside or outside modern human mitochondrial diversity?&lt;br /&gt;
&lt;br /&gt;
We will use the present data set to examine this question.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Load Neanderthal data set&#039;&#039;&#039;&lt;br /&gt;
: In the Terminal where you have MrBayes running:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
execute neanderthal.nexus&lt;br /&gt;
delete 5-40&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
: As we did for the maximum likelihood analysis, we will discard some of the human sequences in order to speed up the analysis. The command delete 5-40 removes sequence number 5 to sequence number 40 from the active data set.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Investigate data&#039;&#039;&#039;&lt;br /&gt;
 showmatrix&lt;br /&gt;
: This data set consists of an alignment of mitochondrial DNA from human (17 sequences), chimpanzee (1 sequence), and Neanderthal (1 sequence). The Neanderthal DNA was extracted from archaeological material, specifically bones found at Vindija in Croatia.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Start analysis&#039;&#039;&#039;&lt;br /&gt;
 outgroup Pan_troglodytes&lt;br /&gt;
 lset nst=mixed rates=gamma&lt;br /&gt;
 mcmc ngen=500000 nchains=3 diagnfreq=10000&lt;br /&gt;
&lt;br /&gt;
: Here we use the command `nst=mixed` which allows MrBayes to automatically explore all possible substitution models. Essentially, MrBayes now considers the substitution model as one more parameter, and uses MCMC to sample from the possible versions (with nst ranging from 1 to 6). This will often be the best choice when using MrBayes. (Below, I use nst=6 for pedagogical purposes, because it makes it simpler to analyse the output files).&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Find posterior probability of clades&#039;&#039;&#039;&lt;br /&gt;
: When the run has finished, issue this command to compute a consensus tree:&lt;br /&gt;
 sumt contype=halfcompat showtreeprobs=no relburnin=yes burninfrac=0.25&lt;br /&gt;
: Examine the consensus tree that is plotted to screen: On the branches that are resolved, you will notice that numbers have been plotted. These are clade-credibility values, and are in fact the posterior probability that the clade is real (based on the present data set).&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Question: &#039;&#039;&#039;What is the posterior probability that all sampled Homo sapiens sequences form a monophyletic group excluding the Neanderthal sequence? Does this support placing the Neanderthal outside modern human mitochondrial diversity?&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
== Probability distributions over other parameters ==&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Question 6&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
: As the last thing, we will now turn away from the tree topology, and instead examine the other parameters that also form part of the probabilistic model. We will do this using a reduced version of the Hepatitis C virus data set that we have examined previously. Stay in the shell window where you just performed the analysis of Neanderthal sequences.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Load data set&#039;&#039;&#039;&lt;br /&gt;
 execute hcvsmall.nexus&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Define site partition&#039;&#039;&#039;&lt;br /&gt;
 charset 1stpos=1-.\3&lt;br /&gt;
 charset 2ndpos=2-.\3&lt;br /&gt;
 charset 3rdpos=3-.\3&lt;br /&gt;
 partition bycodon = 3:1stpos,2ndpos,3rdpos&lt;br /&gt;
 set partition=bycodon&lt;br /&gt;
 prset ratepr=variable&lt;br /&gt;
: This is an alternative way of specifying that different sites may evolve at different rates. With a gamma model, we allow rates to vary across sites but do not specify in advance which sites are fast or slow; instead, that pattern is inferred from the data. Here we instead use prior biological knowledge about the structure of the genetic code to divide sites into three classes: 1st, 2nd, and 3rd codon positions. We then allow each class to have its own rate, so that all 1st positions share one rate, all 2nd positions another, and all 3rd positions a third. Specifically, charset 1stpos=1-.\3 defines a character set named 1stpos consisting of site 1 followed by every third site (\3, i.e. sites 1, 4, 7, 10, …), continuing until the end of the alignment (denoted .).&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Specify model&#039;&#039;&#039;&lt;br /&gt;
 lset nst=6&lt;br /&gt;
: This specifies that we want to use a model of the General Time Reversible (GTR) type, where all 6 substitution types have separate rate parameters.&lt;br /&gt;
&lt;br /&gt;
: When the lset command was discussed previously, a few issues were glossed over. Importantly, and unlike PAUP, the lset command in MrBayes gives no information about whether nucleotide frequencies are equal or not, and whether they should be estimated from the data or not. In MrBayes this is instead controlled by defining the prior probability of the nucleotide frequencies (the command prset can be used to set priors). For instance, a model with equal nucleotide frequencies corresponds to having prior probability 1 (one) for the frequency vector (A=0.25, C=0.25, G=0.25, T=0.25), and zero prior probability for the infinitely many other possible vectors. As you will see below, the default prior is not this limited, and the program will therefore estimate the frequencies from the data.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Inspect model details&#039;&#039;&#039;&lt;br /&gt;
 showmodel&lt;br /&gt;
: This command gives you a summary of the current model settings. You will also get a summary of how the prior probabilities of all model parameters are set. You will for instance notice that the nucleotide frequencies (parameter labeled &amp;quot;Statefreq&amp;quot;) have a &amp;quot;Dirichlet&amp;quot; prior. Without going into details, the Dirichlet distribution is a probability distribution over frequency vectors (i.e., vectors of positive values that sum to 1). Depending on the exact parameters the distribution can be more or less flat (flat here means that all sum-1 vectors are equally probable). The Dirichlet distribution is a handy way of specifying the prior probability distribution of nucleotide (or amino acid) frequency vectors. The default statefreq prior in MrBayes is the flat or un-informative prior dirichlet(1,1,1,1).&lt;br /&gt;
&lt;br /&gt;
: We will not go into the priors for the remaining parameters in any detail, but you may notice that by default all topologies are taken to be equally likely (a flat prior on trees).&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Start MCMC sampling&#039;&#039;&#039;&lt;br /&gt;
 mcmc ngen=1000000 samplefreq=100 diagnfreq=10000 nchains=3&lt;br /&gt;
: The run will take a while to finish (you may want to ensure that the average standard deviation of split frequencies is less than 0.01 before ending the analysis).&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Compute summary of parameter values&#039;&#039;&#039;&lt;br /&gt;
 sump relburnin=yes burninfrac=0.25&lt;br /&gt;
: The sump command (with a &amp;quot;p&amp;quot; at the end) works much like the sumt command (with a &amp;quot;t&amp;quot; at the end), but for other parameters than the tree-topology. Again, we are using 25% of the total number of samples as burnin.&lt;br /&gt;
&lt;br /&gt;
: First, you get a scatter plot of the lnL as a function of generation number. Values from the two independent runs are labeled &amp;quot;1&amp;quot; and &amp;quot;2&amp;quot; respectively. If the burnin is suitable, then the points should be randomly scattered over a narrow lnL interval.&lt;br /&gt;
&lt;br /&gt;
: Secondly, the posterior probability distribution of each parameter is summarized by giving the mean, variance, median, and 95% credible interval.&lt;br /&gt;
&lt;br /&gt;
: The last columns contain values indicating if the run has converged. Specifically ESS means Effective Sample Size, and is a measure of how many independent samples you have from the posterior - the higher the better, but this should be at least 100. The column labeled PSRF+ is a measure (also known as &amp;quot;R-hat&amp;quot;) which should be close to 1 if the runs have converged. Specifically this measures whether different chains (and different parts of different chains) converge to sample the same set of values. &lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Question: &#039;&#039;&#039;What are the posterior mean values of the relative substitution rate parameters r(AC) and r(CG)?&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Question 7: &#039;&#039;&#039; Based on the reported posterior means, does r(CG) appear to be larger than r(AC)?&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Question 8&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Marginal vs. joint distributions&#039;&#039;&#039;&lt;br /&gt;
: Strictly speaking the comparison above was not entirely appropriate. We first found the overall distribution of the r(CG) parameter and then compared its mean to the mean of the overall distribution of the r(AC) parameter. By doing things this way, we are ignoring the possibility that the two parameters might be associated in some way. For instance, one parameter might always be larger than the other in any individual sample, even though the total distributions overlap. We should instead be looking at the distribution over both parameters simultaneously. A probability distribution over several parameters simultaneously is called a &amp;quot;joint distribution&amp;quot; over the parameters.&lt;br /&gt;
&lt;br /&gt;
: By looking at one parameter at a time, we are summing its probability over all values of the other parameters. This is called the marginal distribution.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Examine marginal distributions&#039;&#039;&#039;&lt;br /&gt;
: In RStudio, use the following commands to read and plot the marginal distributions of r(AC) and r(CG). Note that we are discarding the first 25% of the reads as burnin&lt;br /&gt;
 df_hcv = read_tsv(&amp;quot;hcvsmall.nexus.run1.p&amp;quot;, skip=1)&lt;br /&gt;
 burnin = df_hcv$Gen %&amp;gt;% &lt;br /&gt;
     max() %&amp;gt;% &lt;br /&gt;
     multiply_by(0.25) %&amp;gt;% &lt;br /&gt;
     floor()&lt;br /&gt;
 df_hcv2 = df_hcv %&amp;gt;% &lt;br /&gt;
     filter(Gen &amp;gt; burnin) %&amp;gt;%&lt;br /&gt;
     select(CG = `r(C&amp;lt;-&amp;gt;G){all}`,&lt;br /&gt;
            AC = `r(A&amp;lt;-&amp;gt;C){all}`&lt;br /&gt;
            )&lt;br /&gt;
 mcmc_intervals(df_hcv2, prob_outer = 1)&lt;br /&gt;
 mcmc_areas(df_hcv2, prob_outer = 1)&lt;br /&gt;
: The functions mcmc_intervals and mcmc_areas plot different views of the same posterior distributions. &lt;br /&gt;
&lt;br /&gt;
: You can also simply plot the data using ggplot:&lt;br /&gt;
 df_hcv2_long = pivot_longer(df_hcv2, cols = c(&amp;quot;CG&amp;quot;, &amp;quot;AC&amp;quot;))&lt;br /&gt;
 &lt;br /&gt;
 ggplot(df_hcv2_long) + &lt;br /&gt;
     geom_density(mapping=aes(x=value, fill=name), alpha=0.3) + &lt;br /&gt;
     labs(x=&amp;quot;Substitution rate&amp;quot;)&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Question&#039;&#039;&#039;: Based on the marginal distributions, r(AC) appears to be centered at a higher value than r(CG), but the two distributions overlap somewhat. Can you from these marginal distributions alone decide whether r(AC) is larger than r(CG) in most posterior samples? Why or why not?&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Question 9&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Examine joint distributions&#039;&#039;&#039;&lt;br /&gt;
: These plots and results explore the relationship between the A&amp;lt;-&amp;gt;C and C&amp;lt;-&amp;gt;G rates.&lt;br /&gt;
 ggplot(df_hcv2, aes(x=CG, y=AC)) + &lt;br /&gt;
     geom_point(col=&amp;quot;blue&amp;quot;) + &lt;br /&gt;
     geom_abline(intercept=0, slope=1, lty=2, col=&amp;quot;red&amp;quot;) +&lt;br /&gt;
     xlim(0,0.25) + &lt;br /&gt;
     ylim(0,0.25) + &lt;br /&gt;
     labs(x=&amp;quot;CG rate&amp;quot;, y =&amp;quot;AC rate&amp;quot;) &lt;br /&gt;
 &lt;br /&gt;
 ggplot(df_hcv2, aes(x=CG, y= AC)) + &lt;br /&gt;
     geom_hex(col=&amp;quot;blue&amp;quot;) + &lt;br /&gt;
     geom_abline(intercept=0, slope=1, lty=2, col=&amp;quot;red&amp;quot;) +&lt;br /&gt;
     xlim(0,0.25) + &lt;br /&gt;
     ylim(0, 0.25) + &lt;br /&gt;
     labs(x=&amp;quot;CG rate&amp;quot;, y =&amp;quot;AC rate&amp;quot;) &lt;br /&gt;
 &lt;br /&gt;
 df_hcv2 %&amp;gt;%&lt;br /&gt;
     nrow()&lt;br /&gt;
 df_hcv2 %&amp;gt;%&lt;br /&gt;
     filter(AC&amp;gt;CG) %&amp;gt;%&lt;br /&gt;
     nrow()&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Question: &#039;&#039;&#039;Based on the two different ways to plot the joint distribution and based on the unfiltered and filtered row counts, what is the posterior probability that r(AC) &amp;gt; r(CG)?&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Question 10&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
: Note how examining the joint distribution provides you with information that you could not get from simply comparing the marginal distributions. This very simple procedure can be used to answer many different questions.&lt;br /&gt;
&lt;br /&gt;
: Now, plot the relative substitution rates at the first, second, and third codon positions:&lt;br /&gt;
 df_hcv3 = df_hcv %&amp;gt;% &lt;br /&gt;
     filter(Gen &amp;gt; 75000) %&amp;gt;%&lt;br /&gt;
     select(Codon_1st = `m{1}`,&lt;br /&gt;
            Codon_2nd = `m{2}`,&lt;br /&gt;
            Codon_3rd = `m{3}` ) %&amp;gt;%&lt;br /&gt;
     pivot_longer(cols=c(&amp;quot;Codon_1st&amp;quot;, &amp;quot;Codon_2nd&amp;quot;, &amp;quot;Codon_3rd&amp;quot;))&lt;br /&gt;
 &lt;br /&gt;
 ggplot(df_hcv3) + &lt;br /&gt;
     geom_density(mapping=aes(x=value, fill=name), alpha=0.3) + &lt;br /&gt;
     labs(x=&amp;quot;Relative substitution rate&amp;quot;)&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Question: &#039;&#039;&#039;Since random mutations presumably hit all three codon positions with the same frequency, any differences are expected to be caused by subsequent selection. Which of the following statement are correct ? (More than one answer may be correct)&lt;br /&gt;
&lt;br /&gt;
:* Codon position 2 is the most variable of the codon positions.&lt;br /&gt;
:* Codon position 1 is the most variable of the codon positions.&lt;br /&gt;
:* Codon position 1 is the most conserved codon position.&lt;br /&gt;
:* Codon position 3 is the most conserved codon position.&lt;br /&gt;
:* Codon position 3 is the most variable of the codon positions.&lt;br /&gt;
:* Codon position 2 is the most conserved codon position.&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Question 11&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Question: &#039;&#039;&#039;How does this result fit with your knowledge of the genetic code? Why are these codon positions the most conserved or the most variable?&lt;/div&gt;</summary>
		<author><name>Gorm</name></author>
	</entry>
	<entry>
		<id>https://teaching.healthtech.dtu.dk/22115/index.php?title=Bayesian_Phylogeny&amp;diff=248</id>
		<title>Bayesian Phylogeny</title>
		<link rel="alternate" type="text/html" href="https://teaching.healthtech.dtu.dk/22115/index.php?title=Bayesian_Phylogeny&amp;diff=248"/>
		<updated>2026-03-19T09:56:56Z</updated>

		<summary type="html">&lt;p&gt;Gorm: /* Analysis of Neanderthal data (posterior probability of clades) */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;This exercise is part of the course  [[22115_-_Computational_Molecular_Evolution|Computational Molecular Evolution (22115)]].&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Overview ==&lt;br /&gt;
&lt;br /&gt;
Today&#039;s exercise will focus on phylogenetic analysis using Bayesian methods.&lt;br /&gt;
&lt;br /&gt;
As was the case for likelihood methods, Bayesian analysis is founded on having a probabilistic model of how the observed data is produced. This means that, for a given set of parameter values, you can compute the probability or [https://www.statlect.com/glossary/probability-density-function probability density] of any possible observation. For a full dataset, you then obtain the likelihood by multiplying these values across all observations. You will recall from the lecture that in Bayesian statistics the goal is to obtain a full posterior probability distribution over all possible parameter values. The posterior distribution quantifies our degree of belief in any possible parameter value after seeing the data. It is obtained by updating the prior probability distribution using the likelihood of the observed data.&lt;br /&gt;
&lt;br /&gt;
The prior probability distribution expresses your beliefs about the parameters before seeing any data, while the likelihood expresses what the observed data are telling you about the parameters. Specifically, the likelihood of a parameter value is the probability of the observed data given that parameter value. We regard a parameter value as more plausible the more probable it makes the observed data. This is the same measure we have previously used to find the maximum likelihood estimate. If the prior probability distribution is flat (i.e., if all possible parameter values have the same prior probability), then the posterior distribution is proportional to the likelihood, and the parameter value with the maximum likelihood also has the maximum posterior probability. However, even in this case, using a Bayesian approach still lets you interpret the result as a probability distribution over parameter values. &lt;br /&gt;
&lt;br /&gt;
If the prior is not flat, then it may have a substantial impact on the posterior, although this effect will usually diminish as the amount of data increases. A prior should ideally be based on domain knowledge and results from previous experiments. For instance one can use the posterior from one analysis as the prior in a new, independent analysis. Often a prior is chosen to be weakly informative, meaning that it places reasonable bounds on the parameter values without constraining them too narrowly. For instance the transition/transversion rate ratio kappa is typically 1.5-10. Values such as 100, 1,000 or 1,000,000 would be extremely unlikely, so a weakly informative prior for this parameter could be chosen to place 95% of its probability mass in the 0.5-20 range, slightly wider than what we think of as plausible values. For instance one could use a lognormal distribution with suitable parameters.&lt;br /&gt;
&lt;br /&gt;
In Bayesian phylogeny the parameters are of the same kind as in maximum likelihood phylogeny. Typical parameters include tree topology, branch lengths, nucleotide frequencies, and substitution model parameters such as the transition/transversion rate ratio or the gamma shape parameter. The difference is that, whereas in maximum likelihood phylogeny we seek the best point estimates of the parameter values, in Bayesian phylogeny the goal is instead to infer a full probability distribution over the possible parameter values. The observed data are again usually taken to be the alignment, although strictly speaking it would be more reasonable to say that the sequences are what have been observed, and that the alignment should then be inferred jointly with the phylogeny.&lt;br /&gt;
&lt;br /&gt;
In this exercise we will explore how one can determine and use posterior probability distributions over trees, over clades, and over substitution parameters. We will also touch upon the difference between marginal and joint probability distributions.&lt;br /&gt;
&lt;br /&gt;
== Getting started ==&lt;br /&gt;
&lt;br /&gt;
: In the command below: Instead of /path/to/molevol enter the path to the directory where you have placed your course files (for instance cd /Users/bob/Documents/molevol, or cd /home/student/molevol).&lt;br /&gt;
 cd /path/to/molevol&lt;br /&gt;
 mkdir bayes&lt;br /&gt;
 cd bayes&lt;br /&gt;
 cp ../data/primatemitDNA.nexus ./primatemitDNA.nexus&lt;br /&gt;
 cp ../data/neanderthal.nexus ./neanderthal.nexus&lt;br /&gt;
 cp ../data/hcvsmall.nexus ./hcvsmall.nexus&lt;br /&gt;
&lt;br /&gt;
: You have analyzed (versions of) all these data files previously in this course. We will now use Bayesian phylogenetic analysis to complement what we learned in those analyses.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Load R libraries&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
: In RStudio: set the working directory to the bayes directory. Then issue these commands:&lt;br /&gt;
 library(magrittr)&lt;br /&gt;
 library(tidyverse)&lt;br /&gt;
 library(bayesplot)&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
== Posterior probability of trees ==&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Question 1&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
: In today&#039;s exercise we will be using the program &amp;quot;MrBayes&amp;quot; to perform Bayesian phylogenetic analysis. MrBayes is a program that, like PAUP*, can be controlled by giving commands at a command line prompt. In fact, there is a substantial overlap between the commands used to control MrBayes and the PAUP command language. This should be a help when you are trying to understand how to use the program.&lt;br /&gt;
&lt;br /&gt;
: Note that the command &amp;quot;help&amp;quot; will give you a list of all available commands. Issuing &amp;quot;help &#039;&#039;command&#039;&#039;&amp;quot; will give you a more detailed description of the specified command along with current option values. This is similar to how &amp;quot;help &#039;&#039;command&#039;&#039;&amp;quot; works in PAUP.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Start program&#039;&#039;&#039;&lt;br /&gt;
: In a terminal window, issue the command:&lt;br /&gt;
 mb&lt;br /&gt;
: This starts the program, giving you a prompt (&amp;quot;MrBayes&amp;gt; &amp;quot;) where you can enter commands.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Get a quick overview of available commands&#039;&#039;&#039;&lt;br /&gt;
 help&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Load your sequences&#039;&#039;&#039;&lt;br /&gt;
 execute primatemitDNA.nexus&lt;br /&gt;
: This file contains mitochondrial DNA sequences from 5 different primates. Note that MrBayes accepts input in nexus format, and that this is the same command that was used to load sequences in PAUP*. In general, you can use many of the PAUP commands in MrBayes also.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Inspect data set&#039;&#039;&#039;&lt;br /&gt;
 showmatrix&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Define outgroup&#039;&#039;&#039;&lt;br /&gt;
 outgroup Gibbon&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Specify your model of sequence evolution&#039;&#039;&#039;&lt;br /&gt;
 lset nst=2 rates=gamma&lt;br /&gt;
: This command is again very much like the corresponding one in PAUP. You are specifying that you want to use a model with two substitution types (nst=2), and this is automatically taken to mean that you want to distinguish between transitions and transversions. Furthermore, rates=gamma means that you want the model to use a gamma distribution to account for different rates at different sites in the sequence.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Start Markov chain Monte Carlo sampling&#039;&#039;&#039;&lt;br /&gt;
:Make sure to make the shell window as wide as possible and then issue the following commands to start the run:&lt;br /&gt;
 mcmc ngen=1000000 samplefreq=100 nchains=3 diagnfreq=5000&lt;br /&gt;
: What you are doing here is to use the method known as MCMCMC (&amp;quot;Metropolis-coupled Markov chain Monte Carlo&amp;quot;) to empirically determine the posterior probability distribution of trees, branch lengths and substitution parameters. Recall that in the Bayesian framework this is how we learn about parameter values: instead of finding the best point estimates, we typically want to quantify the probability of the entire range of possible values. An estimate of the time left is shown in the last column of output.&lt;br /&gt;
&lt;br /&gt;
: Let us examine the command in detail. First, ngen=1000000 samplefreq=100 lets the search run for 1,000,000 MCMC steps (&amp;quot;generations&amp;quot;) and saves parameter values once every 100 rounds (meaning that a total of 10,000 sets of parameter values will be saved to sample files). You sometimes need to run longer (or shorter) than 1,000,000, and would then typically tweak samplefreq so you get around 1,000 - 10,000 samples in all. The option nchains=3 means that the MCMCMC sampling uses 3 parallel chains (but see below): one &amp;quot;cold&amp;quot; from which sampling takes place, and two &amp;quot;heated&amp;quot; that move around in the parameter space more quickly to find additional peaks in the probability distribution.&lt;br /&gt;
&lt;br /&gt;
: The option diagnfreq=5000 has to do with testing whether the MrBayes run is successful. Briefly, MrBayes will start two entirely independent runs starting from different random trees. In the early phases of the run, the two runs will sample very different trees, but when they have reached convergence (when they produce a good sample from the posterior probability distribution), the two tree samples should be very similar. Every diagnfreq generations, the program will compute a measure of how similar the tree samples are, specifically the average standard deviation of split frequencies. A “split” is the same as a bipartition, i.e. a division of all leaves in the tree into two groups, obtained by cutting an internal branch. For each split, MrBayes compares how often that split occurs in the two independent runs; if the runs have converged, these frequencies should be very similar, giving a small standard deviation. The program then averages this quantity across splits. As a rule of thumb, you may want to run until this value is less than 0.05 (the smaller the better)&lt;br /&gt;
&lt;br /&gt;
: During the run you will see reports about the progress of the two independent runs, each consisting of three chains. Each line of output lists the generation number and the log likelihoods of the current tree/parameter combination for each of the two groups of three chains (a column of asterisks separate the results for the independent runs). The cold chains are the ones enclosed in brackets [...], while the heated chains are enclosed in parentheses (...). Occasionally the chains will swap so one of the heated chains now becomes cold (and sampling then takes place from this chain).&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Continue run until parallel runs converge on same solution&#039;&#039;&#039;&lt;br /&gt;
:At the end of the run, Mrbayes will print the average standard deviation of split frequencies (which is a measure of how similar the tree samples of the two independent runs are). We recommend that you continue with the analysis until the value gets below 0.01 (if the value is larger than 0.01 then you should answer &amp;quot;yes&amp;quot; when the program asks &amp;quot;Continue the analysis? (yes/no)&amp;quot;.)&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Question: &#039;&#039;&#039;MrBayes starts two independent runs from different random trees. Why is it useful to run two independent analyses instead of just one? How does the average standard deviation of split frequencies help you decide whether the two runs have converged to the same posterior distribution? At approximately how many generations does this happen in your run?&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Question 2&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Have a look at the resulting sample files&#039;&#039;&#039;&lt;br /&gt;
: Open a new Terminal window (don&#039;t quit mrbayes in the other terminal yet!) and cd to the bayes directory. Open one of the parameter sampling files in a text editor:&lt;br /&gt;
 nedit primatemitDNA.nexus.run1.p &amp;amp;&lt;br /&gt;
: This file contains one line for each sampled point (you may want to turn off line-wrapping in nedit under the preferences menu). Each row corresponds to a certain sample time (or generation). Each column contains the sampled values of one specific parameter. The first line contains headings telling what the different columns are: &lt;br /&gt;
:* Gen: generation; number of MCMC steps taken so far&lt;br /&gt;
:* lnL: log likelihood of the current parameter estimates&lt;br /&gt;
:* LnPr: log of the prior probability&lt;br /&gt;
:* TL: tree length (sum of all branch lengths)&lt;br /&gt;
:* kappa: transition/transversion rate ratio&lt;br /&gt;
:* pi(A), pi(C), pi(G), pi(T): frequency of A, C, G, T&lt;br /&gt;
:* alpha: shape parameter for the gamma distribution. &lt;br /&gt;
&lt;br /&gt;
: (Column headings may be shifted relative to their corresponding columns). Note how the values of most parameters change a lot during the initial &amp;quot;burnin&amp;quot; period, before they settle near their most probable values. &lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Question: &#039;&#039;&#039;You will notice that lnL is always negative, while LnPr can sometimes be positive. At first sight this may seem impossible, since probabilities cannot be larger than 1. How can this happen?&lt;br /&gt;
&lt;br /&gt;
As a hint, note that (1) priors for continuous parameters are probability densities, and (2) the default prior for each branch length in MrBayes is an exponential distribution with rate 10. Use the following R code to plot this prior on both an ordinary y-axis and a log-scaled y-axis, and then explain why positive values of LnPr are possible.&lt;br /&gt;
&lt;br /&gt;
 df_expdist = tibble(&lt;br /&gt;
   x = seq(0, 1, by = 0.001),&lt;br /&gt;
   density = dexp(x, rate = 10),&lt;br /&gt;
   logdensity = log(dexp(x, rate = 10))&lt;br /&gt;
 )&lt;br /&gt;
 &lt;br /&gt;
 ggplot(df_expdist, aes(x = x, y = density)) +&lt;br /&gt;
   geom_line(color=&amp;quot;blue&amp;quot;) +&lt;br /&gt;
   geom_hline(yintercept = 1, lty=2) +&lt;br /&gt;
   labs(&lt;br /&gt;
     x = &amp;quot;Branch length&amp;quot;,&lt;br /&gt;
     y = &amp;quot;Probability density&amp;quot;,&lt;br /&gt;
     title = &amp;quot;Exponential prior on branch lengths: Exp(rate = 10)&amp;quot;&lt;br /&gt;
   )&lt;br /&gt;
 &lt;br /&gt;
 ggplot(df_expdist, aes(x = x, y = logdensity)) +&lt;br /&gt;
   geom_line(color=&amp;quot;blue&amp;quot;) +&lt;br /&gt;
   geom_hline(yintercept = 0, lty=2) +&lt;br /&gt;
   labs(&lt;br /&gt;
     x = &amp;quot;Branch length&amp;quot;,&lt;br /&gt;
     y = &amp;quot;log of Probability density&amp;quot;,&lt;br /&gt;
     title = &amp;quot;Exponential prior on branch lengths: Exp(rate = 10)&amp;quot;&lt;br /&gt;
   )&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Question 3&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Examine MCMC trajectory for gamma shape parameter, alpha&#039;&#039;&#039;&lt;br /&gt;
: Recall that the idea in MCMCMC sampling is to move around in parameter space in such a way that points are visited according to their posterior probability (i.e., regions with high posterior probability are visited frequently). Now, in RStudio, plot the sampled values for the gamma shape parameter, alpha, for one of the run files:&lt;br /&gt;
 df_primates = read_tsv(&amp;quot;primatemitDNA.nexus.run1.p&amp;quot;, skip=1)&lt;br /&gt;
 mcmc_trace(df_primates, pars=&amp;quot;alpha&amp;quot;)&lt;br /&gt;
: mcmc_trace is one of several plotting commands available in the bayesplot package. This command plots the sampled values of the parameter alpha from the first of the two parallel runs against MCMC generation number. Thus, the x-axis shows the progress of the run through time, with the leftmost values being the earliest samples and the rightmost values the later ones. Note how the Markov chain starts at the arbitrary value 1.0, rapidly moves to values that fit the observed data better, and then moves around in parameter space, sampling different plausible values of alpha. You can experiment with plotting other columns as well.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Question: &#039;&#039;&#039;Describe briefly what happens to the sampled values of alpha during the run. Why is it reasonable to discard the earliest samples as burn-in?&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Question 4&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Investigate posterior probability distribution over trees&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
: Now, close the nedit window and have a look at the file containing sampled trees:&lt;br /&gt;
 nedit primatemitDNA.nexus.run1.t &amp;amp;&lt;br /&gt;
: Tree topology is also a parameter in our model, and exactly like for the other parameters we also get samples from tree-space. One tree is printed per line in the parenthetical Newick format you have seen before. There are 5 taxa in the present data set, so the number of possible unrooted binary tree topologies is only 15. Since we have taken more than 15 sample points, there must be several lines containing the same tree topology. Close the nedit window when you are done.&lt;br /&gt;
&lt;br /&gt;
: MrBayes provides the sumt command to summarize the sampled trees. Before using it, we need to decide on the burn-in: The burn-in is the initial set of samples that are typically discarded, because we want to ensure that the MCMC has moved away from the random starting values, and has found the peaks of the probability landscape. Since the convergence diagnostic used a relative burn-in of 25%, we will also discard the first 25% of tree samples when summarizing the posterior.&lt;br /&gt;
&lt;br /&gt;
: Return to the shell window where you have MrBayes running. In the command below relburnin=yes and burninfrac=0.25 tells MrBayes to discard 25% of the samples as burnin (you could also have explicitly given the number of samples to discard - help sumt will give you details about the command and the current option settings).&lt;br /&gt;
 sumt contype=halfcompat conformat=simple relburnin=yes burninfrac=0.25 showtreeprobs=yes&lt;br /&gt;
: (Scroll back so you can see the top of the output when the command is done). This command gives you a summary of the trees that are in the file you examined manually above. The option contype=halfcompat requests that a majority rule consensus tree is calculated from the set of trees that are left after discarding the burnin. This consensus is the first tree plotted to the screen. Below the consensus cladogram, a consensus phylogram is plotted. The branch lengths in this have been averaged over the trees in which that branch was present (a particular branch corresponds to a bi-partition of the data, and will typically not be present in every sampled tree). The cladogram also has &amp;quot;clade credibility&amp;quot; values. We will return to the meaning of these later in today&#039;s exercise.&lt;br /&gt;
&lt;br /&gt;
: What most interests us right now is the list of trees that is printed after the phylogram. These trees are labeled &amp;quot;Tree 1&amp;quot;, &amp;quot;Tree 2&amp;quot;, etc, and are sorted according to their posterior probability which is indicated by a lower-case p after the tree number. (The upper-case P gives the cumulated probability of trees shown so far, and is useful for constructing a credible set). This list highlights how Bayesian phylogenetic analysis is different from maximum likelihood: Instead of finding the best tree(s), we here quantify our degree of belief in all possible trees.&lt;br /&gt;
&lt;br /&gt;
: The list of trees and probabilities was printed because of the option showtreeprobs=yes. Note that you probably do not want to issue that command if you have much more than 5 taxa! In that case you could instead inspect the file named primatemitDNA.nexus.trprobs which is now present in the same directory as your other files (this file is automatically produced by the sumt command).&lt;br /&gt;
&lt;br /&gt;
: &#039;&#039;&#039;NOTE&#039;&#039;&#039;: Annoyingly, there is a bug in the version of mrbayes we are using here, which means leaf names are not printed on the list of trees with probabilities. However, the most probable tree here in fact is identical to the consensus tree printed above it.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Question: &#039;&#039;&#039;What is the posterior probability of the most probable tree? Does the analysis strongly support a single tree, or is the posterior probability distributed across several different trees?&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
== Analysis of Neanderthal data (posterior probability of clades) ==&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Question 5&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
For many years, there was considerable debate about the origin of modern humans. One view, often called the [https://www.thoughtco.com/multiregional-hypothesis-167235 Multiregional Hypothesis], proposed that after Homo erectus spread from Africa into different parts of the world, regional populations gradually evolved into modern humans more or less in parallel. A different view, often called the Recent African Origin model, proposed that modern Homo sapiens evolved in Africa and later spread outward, largely replacing other archaic human groups such as the Neanderthals.&lt;br /&gt;
&lt;br /&gt;
Today it is clear that the history is more complicated than either simple extreme: modern humans arose in Africa, but there was also some interbreeding with Neanderthals and other archaic humans. However, in this exercise we will focus on a narrower question that can be addressed using a phylogeny of mitochondrial DNA: do the sampled Neanderthal and human mitochondrial sequences suggest that the Neanderthal sequence falls inside or outside modern human mitochondrial diversity?&lt;br /&gt;
&lt;br /&gt;
We will use the present data set to examine this question.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Load Neanderthal data set&#039;&#039;&#039;&lt;br /&gt;
: In the Terminal where you have MrBayes running:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
execute neanderthal.nexus&lt;br /&gt;
delete 5-40&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
: As we did for the maximum likelihood analysis, we will discard some of the human sequences in order to speed up the analysis. The command delete 5-40 removes sequence number 5 to sequence number 40 from the active data set.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Investigate data&#039;&#039;&#039;&lt;br /&gt;
 showmatrix&lt;br /&gt;
: This data set consists of an alignment of mitochondrial DNA from human (17 sequences), chimpanzee (1 sequence), and Neanderthal (1 sequence). The Neanderthal DNA was extracted from archaeological material, specifically bones found at Vindija in Croatia.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Start analysis&#039;&#039;&#039;&lt;br /&gt;
 outgroup Pan_troglodytes&lt;br /&gt;
 lset nst=mixed rates=gamma&lt;br /&gt;
 mcmc ngen=500000 nchains=3 diagnfreq=10000&lt;br /&gt;
&lt;br /&gt;
: Here we use the command `nst=mixed` which allows MrBayes to automatically explore all possible substitution models. Essentially, MrBayes now considers the substitution model as one more parameter, and uses MCMC to sample from the possible versions (with nst ranging from 1 to 6). This will often be the best choice when using MrBayes. (Below, I use nst=6 for pedagogical purposes, because it makes it simpler to analyse the output files).&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Find posterior probability of clades&#039;&#039;&#039;&lt;br /&gt;
: When the run has finished, issue this command to compute a consensus tree:&lt;br /&gt;
 sumt contype=halfcompat showtreeprobs=no relburnin=yes burninfrac=0.25&lt;br /&gt;
: Examine the consensus tree that is plotted to screen: On the branches that are resolved, you will notice that numbers have been plotted. These are clade-credibility values, and are in fact the posterior probability that the clade is real (based on the present data set).&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Question: &#039;&#039;&#039;What is the posterior probability that all sampled Homo sapiens sequences form a monophyletic group excluding the Neanderthal sequence? Does this support placing the Neanderthal outside modern human mitochondrial diversity?&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
== Probability distributions over other parameters ==&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Question 6&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
: As the last thing, we will now turn away from the tree topology, and instead examine the other parameters that also form part of the probabilistic model. We will do this using a reduced version of the Hepatitis C virus data set that we have examined previously. Stay in the shell window where you just performed the analysis of Neanderthal sequences.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Load data set&#039;&#039;&#039;&lt;br /&gt;
 execute hcvsmall.nexus&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Define site partition&#039;&#039;&#039;&lt;br /&gt;
 charset 1stpos=1-.\3&lt;br /&gt;
 charset 2ndpos=2-.\3&lt;br /&gt;
 charset 3rdpos=3-.\3&lt;br /&gt;
 partition bycodon = 3:1stpos,2ndpos,3rdpos&lt;br /&gt;
 set partition=bycodon&lt;br /&gt;
 prset ratepr=variable&lt;br /&gt;
: This is an alternative way of specifying that different sites have different rates. Instead of using a gamma distribution and learning about site-specific rates from the data, we are instead using our prior knowledge about the structure of the genetic code to specify that all 1st codon positions have the same rate, all 2nd codon positions have the same rate, and all 3rd codon positions have the same rate. Specifically, charset 1stpos=1-.\3 means that we define a character set named &amp;quot;1stpos&amp;quot; which includes site 1 in the alignment followed by every third site (&amp;quot;\3&amp;quot;, meaning it includes sites 1, 4, 7, 11, ...) until the end of the alignment (here denoted &amp;quot;.&amp;quot;).&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Specify model&#039;&#039;&#039;&lt;br /&gt;
 lset nst=6&lt;br /&gt;
: This specifies that we want to use a model of the General Time Reversible (GTR) type, where all 6 substitution types have separate rate parameters.&lt;br /&gt;
&lt;br /&gt;
: When the lset command was discussed previously, a few issues were glossed over. Importantly, and unlike PAUP, the lset command in MrBayes gives no information about whether nucleotide frequencies are equal or not, and whether they should be estimated from the data or not. In MrBayes this is instead controlled by defining the prior probability of the nucleotide frequencies (the command prset can be used to set priors). For instance, a model with equal nucleotide frequencies corresponds to having prior probability 1 (one) for the frequency vector (A=0.25, C=0.25, G=0.25, T=0.25), and zero prior probability for the infinitely many other possible vectors. As you will see below, the default prior is not this limited, and the program will therefore estimate the frequencies from the data.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Inspect model details&#039;&#039;&#039;&lt;br /&gt;
 showmodel&lt;br /&gt;
: This command gives you a summary of the current model settings. You will also get a summary of how the prior probabilities of all model parameters are set. You will for instance notice that the nucleotide frequencies (parameter labeled &amp;quot;Statefreq&amp;quot;) have a &amp;quot;Dirichlet&amp;quot; prior. Without going into details, the Dirichlet distribution is a probability distribution over frequency vectors (i.e., vectors of positive values that sum to 1). Depending on the exact parameters the distribution can be more or less flat (flat here means that all sum-1 vectors are equally probable). The Dirichlet distribution is a handy way of specifying the prior probability distribution of nucleotide (or amino acid) frequency vectors. The default statefreq prior in MrBayes is the flat or un-informative prior dirichlet(1,1,1,1).&lt;br /&gt;
&lt;br /&gt;
: We will not go into the priors for the remaining parameters in any detail, but you may notice that by default all topologies are taken to be equally likely (a flat prior on trees).&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Start MCMC sampling&#039;&#039;&#039;&lt;br /&gt;
 mcmc ngen=1000000 samplefreq=100 diagnfreq=10000 nchains=3&lt;br /&gt;
: The run will take a while to finish (you may want to ensure that the average standard deviation of split frequencies is less than 0.01 before ending the analysis).&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Compute summary of parameter values&#039;&#039;&#039;&lt;br /&gt;
 sump relburnin=yes burninfrac=0.25&lt;br /&gt;
: The sump command (with a &amp;quot;p&amp;quot; at the end) works much like the sumt command (with a &amp;quot;t&amp;quot; at the end), but for other parameters than the tree-topology. Again, we are using 25% of the total number of samples as burnin.&lt;br /&gt;
&lt;br /&gt;
: First, you get a scatter plot of the lnL as a function of generation number. Values from the two independent runs are labeled &amp;quot;1&amp;quot; and &amp;quot;2&amp;quot; respectively. If the burnin is suitable, then the points should be randomly scattered over a narrow lnL interval.&lt;br /&gt;
&lt;br /&gt;
: Secondly, the posterior probability distribution of each parameter is summarized by giving the mean, variance, median, and 95% credible interval.&lt;br /&gt;
&lt;br /&gt;
: The last columns contain values indicating if the run has converged. Specifically ESS means Effective Sample Size, and is a measure of how many independent samples you have from the posterior - the higher the better, but this should be at least 100. The column labeled PSRF+ is a measure (also known as &amp;quot;R-hat&amp;quot;) which should be close to 1 if the runs have converged. Specifically this measures whether different chains (and different parts of different chains) converge to sample the same set of values. &lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Question: &#039;&#039;&#039;What are the posterior mean values of the relative substitution rate parameters r(AC) and r(CG)?&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Question 7: &#039;&#039;&#039; Based on the reported posterior means, does r(CG) appear to be larger than r(AC)?&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Question 8&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Marginal vs. joint distributions&#039;&#039;&#039;&lt;br /&gt;
: Strictly speaking the comparison above was not entirely appropriate. We first found the overall distribution of the r(CG) parameter and then compared its mean to the mean of the overall distribution of the r(AC) parameter. By doing things this way, we are ignoring the possibility that the two parameters might be associated in some way. For instance, one parameter might always be larger than the other in any individual sample, even though the total distributions overlap. We should instead be looking at the distribution over both parameters simultaneously. A probability distribution over several parameters simultaneously is called a &amp;quot;joint distribution&amp;quot; over the parameters.&lt;br /&gt;
&lt;br /&gt;
: By looking at one parameter at a time, we are summing its probability over all values of the other parameters. This is called the marginal distribution.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Examine marginal distributions&#039;&#039;&#039;&lt;br /&gt;
: In RStudio, use the following commands to read and plot the marginal distributions of r(AC) and r(CG). Note that we are discarding the first 25% of the reads as burnin&lt;br /&gt;
 df_hcv = read_tsv(&amp;quot;hcvsmall.nexus.run1.p&amp;quot;, skip=1)&lt;br /&gt;
 burnin = df_hcv$Gen %&amp;gt;% &lt;br /&gt;
     max() %&amp;gt;% &lt;br /&gt;
     multiply_by(0.25) %&amp;gt;% &lt;br /&gt;
     floor()&lt;br /&gt;
 df_hcv2 = df_hcv %&amp;gt;% &lt;br /&gt;
     filter(Gen &amp;gt; burnin) %&amp;gt;%&lt;br /&gt;
     select(CG = `r(C&amp;lt;-&amp;gt;G){all}`,&lt;br /&gt;
            AC = `r(A&amp;lt;-&amp;gt;C){all}`&lt;br /&gt;
            )&lt;br /&gt;
 mcmc_intervals(df_hcv2, prob_outer = 1)&lt;br /&gt;
 mcmc_areas(df_hcv2, prob_outer = 1)&lt;br /&gt;
: The functions mcmc_intervals and mcmc_areas plot different views of the same posterior distributions. &lt;br /&gt;
&lt;br /&gt;
: You can also simply plot the data using ggplot:&lt;br /&gt;
 df_hcv2_long = pivot_longer(df_hcv2, cols = c(&amp;quot;CG&amp;quot;, &amp;quot;AC&amp;quot;))&lt;br /&gt;
 &lt;br /&gt;
 ggplot(df_hcv2_long) + &lt;br /&gt;
     geom_density(mapping=aes(x=value, fill=name), alpha=0.3) + &lt;br /&gt;
     labs(x=&amp;quot;Substitution rate&amp;quot;)&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Question&#039;&#039;&#039;: Based on the marginal distributions, r(AC) appears to be centered at a higher value than r(CG), but the two distributions overlap somewhat. Can you from these marginal distributions alone decide whether r(AC) is larger than r(CG) in most posterior samples? Why or why not?&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Question 9&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Examine joint distributions&#039;&#039;&#039;&lt;br /&gt;
: These plots and results explore the relationship between the A&amp;lt;-&amp;gt;C and C&amp;lt;-&amp;gt;G rates.&lt;br /&gt;
 ggplot(df_hcv2, aes(x=CG, y=AC)) + &lt;br /&gt;
     geom_point(col=&amp;quot;blue&amp;quot;) + &lt;br /&gt;
     geom_abline(intercept=0, slope=1, lty=2, col=&amp;quot;red&amp;quot;) +&lt;br /&gt;
     xlim(0,0.25) + &lt;br /&gt;
     ylim(0,0.25) + &lt;br /&gt;
     labs(x=&amp;quot;CG rate&amp;quot;, y =&amp;quot;AC rate&amp;quot;) &lt;br /&gt;
 &lt;br /&gt;
 ggplot(df_hcv2, aes(x=CG, y= AC)) + &lt;br /&gt;
     geom_hex(col=&amp;quot;blue&amp;quot;) + &lt;br /&gt;
     geom_abline(intercept=0, slope=1, lty=2, col=&amp;quot;red&amp;quot;) +&lt;br /&gt;
     xlim(0,0.25) + &lt;br /&gt;
     ylim(0, 0.25) + &lt;br /&gt;
     labs(x=&amp;quot;CG rate&amp;quot;, y =&amp;quot;AC rate&amp;quot;) &lt;br /&gt;
 &lt;br /&gt;
 df_hcv2 %&amp;gt;%&lt;br /&gt;
     nrow()&lt;br /&gt;
 df_hcv2 %&amp;gt;%&lt;br /&gt;
     filter(AC&amp;gt;CG) %&amp;gt;%&lt;br /&gt;
     nrow()&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Question: &#039;&#039;&#039;Based on the two different ways to plot the joint distribution and based on the unfiltered and filtered row counts, what is the posterior probability that r(AC) &amp;gt; r(CG)?&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Question 10&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
: Note how examining the joint distribution provides you with information that you could not get from simply comparing the marginal distributions. This very simple procedure can be used to answer many different questions.&lt;br /&gt;
&lt;br /&gt;
: Now, plot the relative substitution rates at the first, second, and third codon positions:&lt;br /&gt;
 df_hcv3 = df_hcv %&amp;gt;% &lt;br /&gt;
     filter(Gen &amp;gt; 75000) %&amp;gt;%&lt;br /&gt;
     select(Codon_1st = `m{1}`,&lt;br /&gt;
            Codon_2nd = `m{2}`,&lt;br /&gt;
            Codon_3rd = `m{3}` ) %&amp;gt;%&lt;br /&gt;
     pivot_longer(cols=c(&amp;quot;Codon_1st&amp;quot;, &amp;quot;Codon_2nd&amp;quot;, &amp;quot;Codon_3rd&amp;quot;))&lt;br /&gt;
 &lt;br /&gt;
 ggplot(df_hcv3) + &lt;br /&gt;
     geom_density(mapping=aes(x=value, fill=name), alpha=0.3) + &lt;br /&gt;
     labs(x=&amp;quot;Relative substitution rate&amp;quot;)&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Question: &#039;&#039;&#039;Since random mutations presumably hit all three codon positions with the same frequency, any differences are expected to be caused by subsequent selection. Which of the following statement are correct ? (More than one answer may be correct)&lt;br /&gt;
&lt;br /&gt;
:* Codon position 2 is the most variable of the codon positions.&lt;br /&gt;
:* Codon position 1 is the most variable of the codon positions.&lt;br /&gt;
:* Codon position 1 is the most conserved codon position.&lt;br /&gt;
:* Codon position 3 is the most conserved codon position.&lt;br /&gt;
:* Codon position 3 is the most variable of the codon positions.&lt;br /&gt;
:* Codon position 2 is the most conserved codon position.&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Question 11&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Question: &#039;&#039;&#039;How does this result fit with your knowledge of the genetic code? Why are these codon positions the most conserved or the most variable?&lt;/div&gt;</summary>
		<author><name>Gorm</name></author>
	</entry>
	<entry>
		<id>https://teaching.healthtech.dtu.dk/22115/index.php?title=Bayesian_Phylogeny&amp;diff=247</id>
		<title>Bayesian Phylogeny</title>
		<link rel="alternate" type="text/html" href="https://teaching.healthtech.dtu.dk/22115/index.php?title=Bayesian_Phylogeny&amp;diff=247"/>
		<updated>2026-03-19T09:53:42Z</updated>

		<summary type="html">&lt;p&gt;Gorm: /* Analysis of Neanderthal data (posterior probability of clades) */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;This exercise is part of the course  [[22115_-_Computational_Molecular_Evolution|Computational Molecular Evolution (22115)]].&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Overview ==&lt;br /&gt;
&lt;br /&gt;
Today&#039;s exercise will focus on phylogenetic analysis using Bayesian methods.&lt;br /&gt;
&lt;br /&gt;
As was the case for likelihood methods, Bayesian analysis is founded on having a probabilistic model of how the observed data is produced. This means that, for a given set of parameter values, you can compute the probability or [https://www.statlect.com/glossary/probability-density-function probability density] of any possible observation. For a full dataset, you then obtain the likelihood by multiplying these values across all observations. You will recall from the lecture that in Bayesian statistics the goal is to obtain a full posterior probability distribution over all possible parameter values. The posterior distribution quantifies our degree of belief in any possible parameter value after seeing the data. It is obtained by updating the prior probability distribution using the likelihood of the observed data.&lt;br /&gt;
&lt;br /&gt;
The prior probability distribution expresses your beliefs about the parameters before seeing any data, while the likelihood expresses what the observed data are telling you about the parameters. Specifically, the likelihood of a parameter value is the probability of the observed data given that parameter value. We regard a parameter value as more plausible the more probable it makes the observed data. This is the same measure we have previously used to find the maximum likelihood estimate. If the prior probability distribution is flat (i.e., if all possible parameter values have the same prior probability), then the posterior distribution is proportional to the likelihood, and the parameter value with the maximum likelihood also has the maximum posterior probability. However, even in this case, using a Bayesian approach still lets you interpret the result as a probability distribution over parameter values. &lt;br /&gt;
&lt;br /&gt;
If the prior is not flat, then it may have a substantial impact on the posterior, although this effect will usually diminish as the amount of data increases. A prior should ideally be based on domain knowledge and results from previous experiments. For instance one can use the posterior from one analysis as the prior in a new, independent analysis. Often a prior is chosen to be weakly informative, meaning that it places reasonable bounds on the parameter values without constraining them too narrowly. For instance the transition/transversion rate ratio kappa is typically 1.5-10. Values such as 100, 1,000 or 1,000,000 would be extremely unlikely, so a weakly informative prior for this parameter could be chosen to place 95% of its probability mass in the 0.5-20 range, slightly wider than what we think of as plausible values. For instance one could use a lognormal distribution with suitable parameters.&lt;br /&gt;
&lt;br /&gt;
In Bayesian phylogeny the parameters are of the same kind as in maximum likelihood phylogeny. Typical parameters include tree topology, branch lengths, nucleotide frequencies, and substitution model parameters such as the transition/transversion rate ratio or the gamma shape parameter. The difference is that, whereas in maximum likelihood phylogeny we seek the best point estimates of the parameter values, in Bayesian phylogeny the goal is instead to infer a full probability distribution over the possible parameter values. The observed data are again usually taken to be the alignment, although strictly speaking it would be more reasonable to say that the sequences are what have been observed, and that the alignment should then be inferred jointly with the phylogeny.&lt;br /&gt;
&lt;br /&gt;
In this exercise we will explore how one can determine and use posterior probability distributions over trees, over clades, and over substitution parameters. We will also touch upon the difference between marginal and joint probability distributions.&lt;br /&gt;
&lt;br /&gt;
== Getting started ==&lt;br /&gt;
&lt;br /&gt;
: In the command below: Instead of /path/to/molevol enter the path to the directory where you have placed your course files (for instance cd /Users/bob/Documents/molevol, or cd /home/student/molevol).&lt;br /&gt;
 cd /path/to/molevol&lt;br /&gt;
 mkdir bayes&lt;br /&gt;
 cd bayes&lt;br /&gt;
 cp ../data/primatemitDNA.nexus ./primatemitDNA.nexus&lt;br /&gt;
 cp ../data/neanderthal.nexus ./neanderthal.nexus&lt;br /&gt;
 cp ../data/hcvsmall.nexus ./hcvsmall.nexus&lt;br /&gt;
&lt;br /&gt;
: You have analyzed (versions of) all these data files previously in this course. We will now use Bayesian phylogenetic analysis to complement what we learned in those analyses.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Load R libraries&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
: In RStudio: set the working directory to the bayes directory. Then issue these commands:&lt;br /&gt;
 library(magrittr)&lt;br /&gt;
 library(tidyverse)&lt;br /&gt;
 library(bayesplot)&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
== Posterior probability of trees ==&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Question 1&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
: In today&#039;s exercise we will be using the program &amp;quot;MrBayes&amp;quot; to perform Bayesian phylogenetic analysis. MrBayes is a program that, like PAUP*, can be controlled by giving commands at a command line prompt. In fact, there is a substantial overlap between the commands used to control MrBayes and the PAUP command language. This should be a help when you are trying to understand how to use the program.&lt;br /&gt;
&lt;br /&gt;
: Note that the command &amp;quot;help&amp;quot; will give you a list of all available commands. Issuing &amp;quot;help &#039;&#039;command&#039;&#039;&amp;quot; will give you a more detailed description of the specified command along with current option values. This is similar to how &amp;quot;help &#039;&#039;command&#039;&#039;&amp;quot; works in PAUP.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Start program&#039;&#039;&#039;&lt;br /&gt;
: In a terminal window, issue the command:&lt;br /&gt;
 mb&lt;br /&gt;
: This starts the program, giving you a prompt (&amp;quot;MrBayes&amp;gt; &amp;quot;) where you can enter commands.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Get a quick overview of available commands&#039;&#039;&#039;&lt;br /&gt;
 help&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Load your sequences&#039;&#039;&#039;&lt;br /&gt;
 execute primatemitDNA.nexus&lt;br /&gt;
: This file contains mitochondrial DNA sequences from 5 different primates. Note that MrBayes accepts input in nexus format, and that this is the same command that was used to load sequences in PAUP*. In general, you can use many of the PAUP commands in MrBayes also.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Inspect data set&#039;&#039;&#039;&lt;br /&gt;
 showmatrix&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Define outgroup&#039;&#039;&#039;&lt;br /&gt;
 outgroup Gibbon&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Specify your model of sequence evolution&#039;&#039;&#039;&lt;br /&gt;
 lset nst=2 rates=gamma&lt;br /&gt;
: This command is again very much like the corresponding one in PAUP. You are specifying that you want to use a model with two substitution types (nst=2), and this is automatically taken to mean that you want to distinguish between transitions and transversions. Furthermore, rates=gamma means that you want the model to use a gamma distribution to account for different rates at different sites in the sequence.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Start Markov chain Monte Carlo sampling&#039;&#039;&#039;&lt;br /&gt;
:Make sure to make the shell window as wide as possible and then issue the following commands to start the run:&lt;br /&gt;
 mcmc ngen=1000000 samplefreq=100 nchains=3 diagnfreq=5000&lt;br /&gt;
: What you are doing here is to use the method known as MCMCMC (&amp;quot;Metropolis-coupled Markov chain Monte Carlo&amp;quot;) to empirically determine the posterior probability distribution of trees, branch lengths and substitution parameters. Recall that in the Bayesian framework this is how we learn about parameter values: instead of finding the best point estimates, we typically want to quantify the probability of the entire range of possible values. An estimate of the time left is shown in the last column of output.&lt;br /&gt;
&lt;br /&gt;
: Let us examine the command in detail. First, ngen=1000000 samplefreq=100 lets the search run for 1,000,000 MCMC steps (&amp;quot;generations&amp;quot;) and saves parameter values once every 100 rounds (meaning that a total of 10,000 sets of parameter values will be saved to sample files). You sometimes need to run longer (or shorter) than 1,000,000, and would then typically tweak samplefreq so you get around 1,000 - 10,000 samples in all. The option nchains=3 means that the MCMCMC sampling uses 3 parallel chains (but see below): one &amp;quot;cold&amp;quot; from which sampling takes place, and two &amp;quot;heated&amp;quot; that move around in the parameter space more quickly to find additional peaks in the probability distribution.&lt;br /&gt;
&lt;br /&gt;
: The option diagnfreq=5000 has to do with testing whether the MrBayes run is successful. Briefly, MrBayes will start two entirely independent runs starting from different random trees. In the early phases of the run, the two runs will sample very different trees, but when they have reached convergence (when they produce a good sample from the posterior probability distribution), the two tree samples should be very similar. Every diagnfreq generations, the program will compute a measure of how similar the tree samples are, specifically the average standard deviation of split frequencies. A “split” is the same as a bipartition, i.e. a division of all leaves in the tree into two groups, obtained by cutting an internal branch. For each split, MrBayes compares how often that split occurs in the two independent runs; if the runs have converged, these frequencies should be very similar, giving a small standard deviation. The program then averages this quantity across splits. As a rule of thumb, you may want to run until this value is less than 0.05 (the smaller the better)&lt;br /&gt;
&lt;br /&gt;
: During the run you will see reports about the progress of the two independent runs, each consisting of three chains. Each line of output lists the generation number and the log likelihoods of the current tree/parameter combination for each of the two groups of three chains (a column of asterisks separate the results for the independent runs). The cold chains are the ones enclosed in brackets [...], while the heated chains are enclosed in parentheses (...). Occasionally the chains will swap so one of the heated chains now becomes cold (and sampling then takes place from this chain).&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Continue run until parallel runs converge on same solution&#039;&#039;&#039;&lt;br /&gt;
:At the end of the run, Mrbayes will print the average standard deviation of split frequencies (which is a measure of how similar the tree samples of the two independent runs are). We recommend that you continue with the analysis until the value gets below 0.01 (if the value is larger than 0.01 then you should answer &amp;quot;yes&amp;quot; when the program asks &amp;quot;Continue the analysis? (yes/no)&amp;quot;.)&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Question: &#039;&#039;&#039;MrBayes starts two independent runs from different random trees. Why is it useful to run two independent analyses instead of just one? How does the average standard deviation of split frequencies help you decide whether the two runs have converged to the same posterior distribution? At approximately how many generations does this happen in your run?&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Question 2&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Have a look at the resulting sample files&#039;&#039;&#039;&lt;br /&gt;
: Open a new Terminal window (don&#039;t quit mrbayes in the other terminal yet!) and cd to the bayes directory. Open one of the parameter sampling files in a text editor:&lt;br /&gt;
 nedit primatemitDNA.nexus.run1.p &amp;amp;&lt;br /&gt;
: This file contains one line for each sampled point (you may want to turn off line-wrapping in nedit under the preferences menu). Each row corresponds to a certain sample time (or generation). Each column contains the sampled values of one specific parameter. The first line contains headings telling what the different columns are: &lt;br /&gt;
:* Gen: generation; number of MCMC steps taken so far&lt;br /&gt;
:* lnL: log likelihood of the current parameter estimates&lt;br /&gt;
:* LnPr: log of the prior probability&lt;br /&gt;
:* TL: tree length (sum of all branch lengths)&lt;br /&gt;
:* kappa: transition/transversion rate ratio&lt;br /&gt;
:* pi(A), pi(C), pi(G), pi(T): frequency of A, C, G, T&lt;br /&gt;
:* alpha: shape parameter for the gamma distribution. &lt;br /&gt;
&lt;br /&gt;
: (Column headings may be shifted relative to their corresponding columns). Note how the values of most parameters change a lot during the initial &amp;quot;burnin&amp;quot; period, before they settle near their most probable values. &lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Question: &#039;&#039;&#039;You will notice that lnL is always negative, while LnPr can sometimes be positive. At first sight this may seem impossible, since probabilities cannot be larger than 1. How can this happen?&lt;br /&gt;
&lt;br /&gt;
As a hint, note that (1) priors for continuous parameters are probability densities, and (2) the default prior for each branch length in MrBayes is an exponential distribution with rate 10. Use the following R code to plot this prior on both an ordinary y-axis and a log-scaled y-axis, and then explain why positive values of LnPr are possible.&lt;br /&gt;
&lt;br /&gt;
 df_expdist = tibble(&lt;br /&gt;
   x = seq(0, 1, by = 0.001),&lt;br /&gt;
   density = dexp(x, rate = 10),&lt;br /&gt;
   logdensity = log(dexp(x, rate = 10))&lt;br /&gt;
 )&lt;br /&gt;
 &lt;br /&gt;
 ggplot(df_expdist, aes(x = x, y = density)) +&lt;br /&gt;
   geom_line(color=&amp;quot;blue&amp;quot;) +&lt;br /&gt;
   geom_hline(yintercept = 1, lty=2) +&lt;br /&gt;
   labs(&lt;br /&gt;
     x = &amp;quot;Branch length&amp;quot;,&lt;br /&gt;
     y = &amp;quot;Probability density&amp;quot;,&lt;br /&gt;
     title = &amp;quot;Exponential prior on branch lengths: Exp(rate = 10)&amp;quot;&lt;br /&gt;
   )&lt;br /&gt;
 &lt;br /&gt;
 ggplot(df_expdist, aes(x = x, y = logdensity)) +&lt;br /&gt;
   geom_line(color=&amp;quot;blue&amp;quot;) +&lt;br /&gt;
   geom_hline(yintercept = 0, lty=2) +&lt;br /&gt;
   labs(&lt;br /&gt;
     x = &amp;quot;Branch length&amp;quot;,&lt;br /&gt;
     y = &amp;quot;log of Probability density&amp;quot;,&lt;br /&gt;
     title = &amp;quot;Exponential prior on branch lengths: Exp(rate = 10)&amp;quot;&lt;br /&gt;
   )&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Question 3&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Examine MCMC trajectory for gamma shape parameter, alpha&#039;&#039;&#039;&lt;br /&gt;
: Recall that the idea in MCMCMC sampling is to move around in parameter space in such a way that points are visited according to their posterior probability (i.e., regions with high posterior probability are visited frequently). Now, in RStudio, plot the sampled values for the gamma shape parameter, alpha, for one of the run files:&lt;br /&gt;
 df_primates = read_tsv(&amp;quot;primatemitDNA.nexus.run1.p&amp;quot;, skip=1)&lt;br /&gt;
 mcmc_trace(df_primates, pars=&amp;quot;alpha&amp;quot;)&lt;br /&gt;
: mcmc_trace is one of several plotting commands available in the bayesplot package. This command plots the sampled values of the parameter alpha from the first of the two parallel runs against MCMC generation number. Thus, the x-axis shows the progress of the run through time, with the leftmost values being the earliest samples and the rightmost values the later ones. Note how the Markov chain starts at the arbitrary value 1.0, rapidly moves to values that fit the observed data better, and then moves around in parameter space, sampling different plausible values of alpha. You can experiment with plotting other columns as well.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Question: &#039;&#039;&#039;Describe briefly what happens to the sampled values of alpha during the run. Why is it reasonable to discard the earliest samples as burn-in?&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Question 4&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Investigate posterior probability distribution over trees&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
: Now, close the nedit window and have a look at the file containing sampled trees:&lt;br /&gt;
 nedit primatemitDNA.nexus.run1.t &amp;amp;&lt;br /&gt;
: Tree topology is also a parameter in our model, and exactly like for the other parameters we also get samples from tree-space. One tree is printed per line in the parenthetical Newick format you have seen before. There are 5 taxa in the present data set, so the number of possible unrooted binary tree topologies is only 15. Since we have taken more than 15 sample points, there must be several lines containing the same tree topology. Close the nedit window when you are done.&lt;br /&gt;
&lt;br /&gt;
: MrBayes provides the sumt command to summarize the sampled trees. Before using it, we need to decide on the burn-in: The burn-in is the initial set of samples that are typically discarded, because we want to ensure that the MCMC has moved away from the random starting values, and has found the peaks of the probability landscape. Since the convergence diagnostic used a relative burn-in of 25%, we will also discard the first 25% of tree samples when summarizing the posterior.&lt;br /&gt;
&lt;br /&gt;
: Return to the shell window where you have MrBayes running. In the command below relburnin=yes and burninfrac=0.25 tells MrBayes to discard 25% of the samples as burnin (you could also have explicitly given the number of samples to discard - help sumt will give you details about the command and the current option settings).&lt;br /&gt;
 sumt contype=halfcompat conformat=simple relburnin=yes burninfrac=0.25 showtreeprobs=yes&lt;br /&gt;
: (Scroll back so you can see the top of the output when the command is done). This command gives you a summary of the trees that are in the file you examined manually above. The option contype=halfcompat requests that a majority rule consensus tree is calculated from the set of trees that are left after discarding the burnin. This consensus is the first tree plotted to the screen. Below the consensus cladogram, a consensus phylogram is plotted. The branch lengths in this have been averaged over the trees in which that branch was present (a particular branch corresponds to a bi-partition of the data, and will typically not be present in every sampled tree). The cladogram also has &amp;quot;clade credibility&amp;quot; values. We will return to the meaning of these later in today&#039;s exercise.&lt;br /&gt;
&lt;br /&gt;
: What most interests us right now is the list of trees that is printed after the phylogram. These trees are labeled &amp;quot;Tree 1&amp;quot;, &amp;quot;Tree 2&amp;quot;, etc, and are sorted according to their posterior probability which is indicated by a lower-case p after the tree number. (The upper-case P gives the cumulated probability of trees shown so far, and is useful for constructing a credible set). This list highlights how Bayesian phylogenetic analysis is different from maximum likelihood: Instead of finding the best tree(s), we here quantify our degree of belief in all possible trees.&lt;br /&gt;
&lt;br /&gt;
: The list of trees and probabilities was printed because of the option showtreeprobs=yes. Note that you probably do not want to issue that command if you have much more than 5 taxa! In that case you could instead inspect the file named primatemitDNA.nexus.trprobs which is now present in the same directory as your other files (this file is automatically produced by the sumt command).&lt;br /&gt;
&lt;br /&gt;
: &#039;&#039;&#039;NOTE&#039;&#039;&#039;: Annoyingly, there is a bug in the version of mrbayes we are using here, which means leaf names are not printed on the list of trees with probabilities. However, the most probable tree here in fact is identical to the consensus tree printed above it.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Question: &#039;&#039;&#039;What is the posterior probability of the most probable tree? Does the analysis strongly support a single tree, or is the posterior probability distributed across several different trees?&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
== Analysis of Neanderthal data (posterior probability of clades) ==&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Question 5&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
For many years, there was considerable debate about the origin of modern humans. One view, often called the [https://www.thoughtco.com/multiregional-hypothesis-167235 Multiregional Hypothesis], proposed that after Homo erectus spread from Africa into different parts of the world, regional populations gradually evolved into modern humans more or less in parallel. A different view, often called the Recent African Origin model, proposed that modern Homo sapiens evolved in Africa and later spread outward, largely replacing other archaic human groups such as the Neanderthals.&lt;br /&gt;
&lt;br /&gt;
Today it is clear that the history is more complicated than either simple extreme: modern humans arose in Africa, but there was also some interbreeding with Neanderthals and other archaic humans. However, in this exercise we will focus on a narrower question that can be addressed using a phylogeny of mitochondrial DNA: do the sampled Neanderthal and human mitochondrial sequences suggest that the Neanderthal sequence falls inside or outside modern human mitochondrial diversity?&lt;br /&gt;
&lt;br /&gt;
We will use the present data set to examine this question.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Load Neanderthal data set&#039;&#039;&#039;&lt;br /&gt;
: In the Terminal where you have MrBayes running:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
execute neanderthal.nexus&lt;br /&gt;
delete 5-40&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
: As we did for the maximum likelihood analysis, we will discard some of the human sequences in order to speed up the analysis. The command delete 5-40 removes sequence number 5 to sequence number 40 from the active data set.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Investigate data&#039;&#039;&#039;&lt;br /&gt;
 showmatrix&lt;br /&gt;
: This data set consists of an alignment of mitochondrial DNA from human (17 sequences), chimpanzee (1 sequence), and Neanderthal (1 sequence). The Neanderthal DNA was extracted from archaeological material, specifically bones found at Vindija in Croatia.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Start analysis&#039;&#039;&#039;&lt;br /&gt;
 outgroup Pan_troglodytes&lt;br /&gt;
 lset nst=mixed rates=gamma&lt;br /&gt;
 mcmc ngen=500000 nchains=3 diagnfreq=10000&lt;br /&gt;
&lt;br /&gt;
: Here we use the command `nst=mixed` which allows MrBayes to automatically explore all possible substitution models. Essentially, MrBayes now considers the substitution model as one more parameter, and uses MCMC to sample from the possible versions (with nst ranging from 1 to 6). This will often be the best choice when using MrBayes. (Below, I use nst=6 for pedagogical purposes, because it makes it simpler to analyse the output files).&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Find posterior probability of clades&#039;&#039;&#039;&lt;br /&gt;
 sumt contype=halfcompat showtreeprobs=no relburnin=yes burninfrac=0.25&lt;br /&gt;
: Examine the consensus tree that is plotted to screen: On the branches that are resolved, you will notice that numbers have been plotted. These are clade-credibility values, and are in fact the posterior probability that the clade is real (based on the present data set).&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Question: &#039;&#039;&#039;What is the posterior probability that all sampled Homo sapiens sequences form a monophyletic group excluding the Neanderthal sequence? Does this support placing the Neanderthal outside modern human mitochondrial diversity?&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
== Probability distributions over other parameters ==&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Question 6&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
: As the last thing, we will now turn away from the tree topology, and instead examine the other parameters that also form part of the probabilistic model. We will do this using a reduced version of the Hepatitis C virus data set that we have examined previously. Stay in the shell window where you just performed the analysis of Neanderthal sequences.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Load data set&#039;&#039;&#039;&lt;br /&gt;
 execute hcvsmall.nexus&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Define site partition&#039;&#039;&#039;&lt;br /&gt;
 charset 1stpos=1-.\3&lt;br /&gt;
 charset 2ndpos=2-.\3&lt;br /&gt;
 charset 3rdpos=3-.\3&lt;br /&gt;
 partition bycodon = 3:1stpos,2ndpos,3rdpos&lt;br /&gt;
 set partition=bycodon&lt;br /&gt;
 prset ratepr=variable&lt;br /&gt;
: This is an alternative way of specifying that different sites have different rates. Instead of using a gamma distribution and learning about site-specific rates from the data, we are instead using our prior knowledge about the structure of the genetic code to specify that all 1st codon positions have the same rate, all 2nd codon positions have the same rate, and all 3rd codon positions have the same rate. Specifically, charset 1stpos=1-.\3 means that we define a character set named &amp;quot;1stpos&amp;quot; which includes site 1 in the alignment followed by every third site (&amp;quot;\3&amp;quot;, meaning it includes sites 1, 4, 7, 11, ...) until the end of the alignment (here denoted &amp;quot;.&amp;quot;).&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Specify model&#039;&#039;&#039;&lt;br /&gt;
 lset nst=6&lt;br /&gt;
: This specifies that we want to use a model of the General Time Reversible (GTR) type, where all 6 substitution types have separate rate parameters.&lt;br /&gt;
&lt;br /&gt;
: When the lset command was discussed previously, a few issues were glossed over. Importantly, and unlike PAUP, the lset command in MrBayes gives no information about whether nucleotide frequencies are equal or not, and whether they should be estimated from the data or not. In MrBayes this is instead controlled by defining the prior probability of the nucleotide frequencies (the command prset can be used to set priors). For instance, a model with equal nucleotide frequencies corresponds to having prior probability 1 (one) for the frequency vector (A=0.25, C=0.25, G=0.25, T=0.25), and zero prior probability for the infinitely many other possible vectors. As you will see below, the default prior is not this limited, and the program will therefore estimate the frequencies from the data.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Inspect model details&#039;&#039;&#039;&lt;br /&gt;
 showmodel&lt;br /&gt;
: This command gives you a summary of the current model settings. You will also get a summary of how the prior probabilities of all model parameters are set. You will for instance notice that the nucleotide frequencies (parameter labeled &amp;quot;Statefreq&amp;quot;) have a &amp;quot;Dirichlet&amp;quot; prior. Without going into details, the Dirichlet distribution is a probability distribution over frequency vectors (i.e., vectors of positive values that sum to 1). Depending on the exact parameters the distribution can be more or less flat (flat here means that all sum-1 vectors are equally probable). The Dirichlet distribution is a handy way of specifying the prior probability distribution of nucleotide (or amino acid) frequency vectors. The default statefreq prior in MrBayes is the flat or un-informative prior dirichlet(1,1,1,1).&lt;br /&gt;
&lt;br /&gt;
: We will not go into the priors for the remaining parameters in any detail, but you may notice that by default all topologies are taken to be equally likely (a flat prior on trees).&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Start MCMC sampling&#039;&#039;&#039;&lt;br /&gt;
 mcmc ngen=1000000 samplefreq=100 diagnfreq=10000 nchains=3&lt;br /&gt;
: The run will take a while to finish (you may want to ensure that the average standard deviation of split frequencies is less than 0.01 before ending the analysis).&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Compute summary of parameter values&#039;&#039;&#039;&lt;br /&gt;
 sump relburnin=yes burninfrac=0.25&lt;br /&gt;
: The sump command (with a &amp;quot;p&amp;quot; at the end) works much like the sumt command (with a &amp;quot;t&amp;quot; at the end), but for other parameters than the tree-topology. Again, we are using 25% of the total number of samples as burnin.&lt;br /&gt;
&lt;br /&gt;
: First, you get a scatter plot of the lnL as a function of generation number. Values from the two independent runs are labeled &amp;quot;1&amp;quot; and &amp;quot;2&amp;quot; respectively. If the burnin is suitable, then the points should be randomly scattered over a narrow lnL interval.&lt;br /&gt;
&lt;br /&gt;
: Secondly, the posterior probability distribution of each parameter is summarized by giving the mean, variance, median, and 95% credible interval.&lt;br /&gt;
&lt;br /&gt;
: The last columns contain values indicating if the run has converged. Specifically ESS means Effective Sample Size, and is a measure of how many independent samples you have from the posterior - the higher the better, but this should be at least 100. The column labeled PSRF+ is a measure (also known as &amp;quot;R-hat&amp;quot;) which should be close to 1 if the runs have converged. Specifically this measures whether different chains (and different parts of different chains) converge to sample the same set of values. &lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Question: &#039;&#039;&#039;What are the posterior mean values of the relative substitution rate parameters r(AC) and r(CG)?&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Question 7: &#039;&#039;&#039; Based on the reported posterior means, does r(CG) appear to be larger than r(AC)?&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Question 8&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Marginal vs. joint distributions&#039;&#039;&#039;&lt;br /&gt;
: Strictly speaking the comparison above was not entirely appropriate. We first found the overall distribution of the r(CG) parameter and then compared its mean to the mean of the overall distribution of the r(AC) parameter. By doing things this way, we are ignoring the possibility that the two parameters might be associated in some way. For instance, one parameter might always be larger than the other in any individual sample, even though the total distributions overlap. We should instead be looking at the distribution over both parameters simultaneously. A probability distribution over several parameters simultaneously is called a &amp;quot;joint distribution&amp;quot; over the parameters.&lt;br /&gt;
&lt;br /&gt;
: By looking at one parameter at a time, we are summing its probability over all values of the other parameters. This is called the marginal distribution.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Examine marginal distributions&#039;&#039;&#039;&lt;br /&gt;
: In RStudio, use the following commands to read and plot the marginal distributions of r(AC) and r(CG). Note that we are discarding the first 25% of the reads as burnin&lt;br /&gt;
 df_hcv = read_tsv(&amp;quot;hcvsmall.nexus.run1.p&amp;quot;, skip=1)&lt;br /&gt;
 burnin = df_hcv$Gen %&amp;gt;% &lt;br /&gt;
     max() %&amp;gt;% &lt;br /&gt;
     multiply_by(0.25) %&amp;gt;% &lt;br /&gt;
     floor()&lt;br /&gt;
 df_hcv2 = df_hcv %&amp;gt;% &lt;br /&gt;
     filter(Gen &amp;gt; burnin) %&amp;gt;%&lt;br /&gt;
     select(CG = `r(C&amp;lt;-&amp;gt;G){all}`,&lt;br /&gt;
            AC = `r(A&amp;lt;-&amp;gt;C){all}`&lt;br /&gt;
            )&lt;br /&gt;
 mcmc_intervals(df_hcv2, prob_outer = 1)&lt;br /&gt;
 mcmc_areas(df_hcv2, prob_outer = 1)&lt;br /&gt;
: The functions mcmc_intervals and mcmc_areas plot different views of the same posterior distributions. &lt;br /&gt;
&lt;br /&gt;
: You can also simply plot the data using ggplot:&lt;br /&gt;
 df_hcv2_long = pivot_longer(df_hcv2, cols = c(&amp;quot;CG&amp;quot;, &amp;quot;AC&amp;quot;))&lt;br /&gt;
 &lt;br /&gt;
 ggplot(df_hcv2_long) + &lt;br /&gt;
     geom_density(mapping=aes(x=value, fill=name), alpha=0.3) + &lt;br /&gt;
     labs(x=&amp;quot;Substitution rate&amp;quot;)&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Question&#039;&#039;&#039;: Based on the marginal distributions, r(AC) appears to be centered at a higher value than r(CG), but the two distributions overlap somewhat. Can you from these marginal distributions alone decide whether r(AC) is larger than r(CG) in most posterior samples? Why or why not?&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Question 9&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Examine joint distributions&#039;&#039;&#039;&lt;br /&gt;
: These plots and results explore the relationship between the A&amp;lt;-&amp;gt;C and C&amp;lt;-&amp;gt;G rates.&lt;br /&gt;
 ggplot(df_hcv2, aes(x=CG, y=AC)) + &lt;br /&gt;
     geom_point(col=&amp;quot;blue&amp;quot;) + &lt;br /&gt;
     geom_abline(intercept=0, slope=1, lty=2, col=&amp;quot;red&amp;quot;) +&lt;br /&gt;
     xlim(0,0.25) + &lt;br /&gt;
     ylim(0,0.25) + &lt;br /&gt;
     labs(x=&amp;quot;CG rate&amp;quot;, y =&amp;quot;AC rate&amp;quot;) &lt;br /&gt;
 &lt;br /&gt;
 ggplot(df_hcv2, aes(x=CG, y= AC)) + &lt;br /&gt;
     geom_hex(col=&amp;quot;blue&amp;quot;) + &lt;br /&gt;
     geom_abline(intercept=0, slope=1, lty=2, col=&amp;quot;red&amp;quot;) +&lt;br /&gt;
     xlim(0,0.25) + &lt;br /&gt;
     ylim(0, 0.25) + &lt;br /&gt;
     labs(x=&amp;quot;CG rate&amp;quot;, y =&amp;quot;AC rate&amp;quot;) &lt;br /&gt;
 &lt;br /&gt;
 df_hcv2 %&amp;gt;%&lt;br /&gt;
     nrow()&lt;br /&gt;
 df_hcv2 %&amp;gt;%&lt;br /&gt;
     filter(AC&amp;gt;CG) %&amp;gt;%&lt;br /&gt;
     nrow()&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Question: &#039;&#039;&#039;Based on the two different ways to plot the joint distribution and based on the unfiltered and filtered row counts, what is the posterior probability that r(AC) &amp;gt; r(CG)?&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Question 10&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
: Note how examining the joint distribution provides you with information that you could not get from simply comparing the marginal distributions. This very simple procedure can be used to answer many different questions.&lt;br /&gt;
&lt;br /&gt;
: Now, plot the relative substitution rates at the first, second, and third codon positions:&lt;br /&gt;
 df_hcv3 = df_hcv %&amp;gt;% &lt;br /&gt;
     filter(Gen &amp;gt; 75000) %&amp;gt;%&lt;br /&gt;
     select(Codon_1st = `m{1}`,&lt;br /&gt;
            Codon_2nd = `m{2}`,&lt;br /&gt;
            Codon_3rd = `m{3}` ) %&amp;gt;%&lt;br /&gt;
     pivot_longer(cols=c(&amp;quot;Codon_1st&amp;quot;, &amp;quot;Codon_2nd&amp;quot;, &amp;quot;Codon_3rd&amp;quot;))&lt;br /&gt;
 &lt;br /&gt;
 ggplot(df_hcv3) + &lt;br /&gt;
     geom_density(mapping=aes(x=value, fill=name), alpha=0.3) + &lt;br /&gt;
     labs(x=&amp;quot;Relative substitution rate&amp;quot;)&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Question: &#039;&#039;&#039;Since random mutations presumably hit all three codon positions with the same frequency, any differences are expected to be caused by subsequent selection. Which of the following statement are correct ? (More than one answer may be correct)&lt;br /&gt;
&lt;br /&gt;
:* Codon position 2 is the most variable of the codon positions.&lt;br /&gt;
:* Codon position 1 is the most variable of the codon positions.&lt;br /&gt;
:* Codon position 1 is the most conserved codon position.&lt;br /&gt;
:* Codon position 3 is the most conserved codon position.&lt;br /&gt;
:* Codon position 3 is the most variable of the codon positions.&lt;br /&gt;
:* Codon position 2 is the most conserved codon position.&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Question 11&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Question: &#039;&#039;&#039;How does this result fit with your knowledge of the genetic code? Why are these codon positions the most conserved or the most variable?&lt;/div&gt;</summary>
		<author><name>Gorm</name></author>
	</entry>
	<entry>
		<id>https://teaching.healthtech.dtu.dk/22115/index.php?title=Bayesian_Phylogeny&amp;diff=246</id>
		<title>Bayesian Phylogeny</title>
		<link rel="alternate" type="text/html" href="https://teaching.healthtech.dtu.dk/22115/index.php?title=Bayesian_Phylogeny&amp;diff=246"/>
		<updated>2026-03-19T09:53:10Z</updated>

		<summary type="html">&lt;p&gt;Gorm: /* Analysis of Neanderthal data (posterior probability of clades) */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;This exercise is part of the course  [[22115_-_Computational_Molecular_Evolution|Computational Molecular Evolution (22115)]].&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Overview ==&lt;br /&gt;
&lt;br /&gt;
Today&#039;s exercise will focus on phylogenetic analysis using Bayesian methods.&lt;br /&gt;
&lt;br /&gt;
As was the case for likelihood methods, Bayesian analysis is founded on having a probabilistic model of how the observed data is produced. This means that, for a given set of parameter values, you can compute the probability or [https://www.statlect.com/glossary/probability-density-function probability density] of any possible observation. For a full dataset, you then obtain the likelihood by multiplying these values across all observations. You will recall from the lecture that in Bayesian statistics the goal is to obtain a full posterior probability distribution over all possible parameter values. The posterior distribution quantifies our degree of belief in any possible parameter value after seeing the data. It is obtained by updating the prior probability distribution using the likelihood of the observed data.&lt;br /&gt;
&lt;br /&gt;
The prior probability distribution expresses your beliefs about the parameters before seeing any data, while the likelihood expresses what the observed data are telling you about the parameters. Specifically, the likelihood of a parameter value is the probability of the observed data given that parameter value. We regard a parameter value as more plausible the more probable it makes the observed data. This is the same measure we have previously used to find the maximum likelihood estimate. If the prior probability distribution is flat (i.e., if all possible parameter values have the same prior probability), then the posterior distribution is proportional to the likelihood, and the parameter value with the maximum likelihood also has the maximum posterior probability. However, even in this case, using a Bayesian approach still lets you interpret the result as a probability distribution over parameter values. &lt;br /&gt;
&lt;br /&gt;
If the prior is not flat, then it may have a substantial impact on the posterior, although this effect will usually diminish as the amount of data increases. A prior should ideally be based on domain knowledge and results from previous experiments. For instance one can use the posterior from one analysis as the prior in a new, independent analysis. Often a prior is chosen to be weakly informative, meaning that it places reasonable bounds on the parameter values without constraining them too narrowly. For instance the transition/transversion rate ratio kappa is typically 1.5-10. Values such as 100, 1,000 or 1,000,000 would be extremely unlikely, so a weakly informative prior for this parameter could be chosen to place 95% of its probability mass in the 0.5-20 range, slightly wider than what we think of as plausible values. For instance one could use a lognormal distribution with suitable parameters.&lt;br /&gt;
&lt;br /&gt;
In Bayesian phylogeny the parameters are of the same kind as in maximum likelihood phylogeny. Typical parameters include tree topology, branch lengths, nucleotide frequencies, and substitution model parameters such as the transition/transversion rate ratio or the gamma shape parameter. The difference is that, whereas in maximum likelihood phylogeny we seek the best point estimates of the parameter values, in Bayesian phylogeny the goal is instead to infer a full probability distribution over the possible parameter values. The observed data are again usually taken to be the alignment, although strictly speaking it would be more reasonable to say that the sequences are what have been observed, and that the alignment should then be inferred jointly with the phylogeny.&lt;br /&gt;
&lt;br /&gt;
In this exercise we will explore how one can determine and use posterior probability distributions over trees, over clades, and over substitution parameters. We will also touch upon the difference between marginal and joint probability distributions.&lt;br /&gt;
&lt;br /&gt;
== Getting started ==&lt;br /&gt;
&lt;br /&gt;
: In the command below: Instead of /path/to/molevol enter the path to the directory where you have placed your course files (for instance cd /Users/bob/Documents/molevol, or cd /home/student/molevol).&lt;br /&gt;
 cd /path/to/molevol&lt;br /&gt;
 mkdir bayes&lt;br /&gt;
 cd bayes&lt;br /&gt;
 cp ../data/primatemitDNA.nexus ./primatemitDNA.nexus&lt;br /&gt;
 cp ../data/neanderthal.nexus ./neanderthal.nexus&lt;br /&gt;
 cp ../data/hcvsmall.nexus ./hcvsmall.nexus&lt;br /&gt;
&lt;br /&gt;
: You have analyzed (versions of) all these data files previously in this course. We will now use Bayesian phylogenetic analysis to complement what we learned in those analyses.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Load R libraries&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
: In RStudio: set the working directory to the bayes directory. Then issue these commands:&lt;br /&gt;
 library(magrittr)&lt;br /&gt;
 library(tidyverse)&lt;br /&gt;
 library(bayesplot)&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
== Posterior probability of trees ==&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Question 1&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
: In today&#039;s exercise we will be using the program &amp;quot;MrBayes&amp;quot; to perform Bayesian phylogenetic analysis. MrBayes is a program that, like PAUP*, can be controlled by giving commands at a command line prompt. In fact, there is a substantial overlap between the commands used to control MrBayes and the PAUP command language. This should be a help when you are trying to understand how to use the program.&lt;br /&gt;
&lt;br /&gt;
: Note that the command &amp;quot;help&amp;quot; will give you a list of all available commands. Issuing &amp;quot;help &#039;&#039;command&#039;&#039;&amp;quot; will give you a more detailed description of the specified command along with current option values. This is similar to how &amp;quot;help &#039;&#039;command&#039;&#039;&amp;quot; works in PAUP.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Start program&#039;&#039;&#039;&lt;br /&gt;
: In a terminal window, issue the command:&lt;br /&gt;
 mb&lt;br /&gt;
: This starts the program, giving you a prompt (&amp;quot;MrBayes&amp;gt; &amp;quot;) where you can enter commands.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Get a quick overview of available commands&#039;&#039;&#039;&lt;br /&gt;
 help&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Load your sequences&#039;&#039;&#039;&lt;br /&gt;
 execute primatemitDNA.nexus&lt;br /&gt;
: This file contains mitochondrial DNA sequences from 5 different primates. Note that MrBayes accepts input in nexus format, and that this is the same command that was used to load sequences in PAUP*. In general, you can use many of the PAUP commands in MrBayes also.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Inspect data set&#039;&#039;&#039;&lt;br /&gt;
 showmatrix&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Define outgroup&#039;&#039;&#039;&lt;br /&gt;
 outgroup Gibbon&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Specify your model of sequence evolution&#039;&#039;&#039;&lt;br /&gt;
 lset nst=2 rates=gamma&lt;br /&gt;
: This command is again very much like the corresponding one in PAUP. You are specifying that you want to use a model with two substitution types (nst=2), and this is automatically taken to mean that you want to distinguish between transitions and transversions. Furthermore, rates=gamma means that you want the model to use a gamma distribution to account for different rates at different sites in the sequence.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Start Markov chain Monte Carlo sampling&#039;&#039;&#039;&lt;br /&gt;
:Make sure to make the shell window as wide as possible and then issue the following commands to start the run:&lt;br /&gt;
 mcmc ngen=1000000 samplefreq=100 nchains=3 diagnfreq=5000&lt;br /&gt;
: What you are doing here is to use the method known as MCMCMC (&amp;quot;Metropolis-coupled Markov chain Monte Carlo&amp;quot;) to empirically determine the posterior probability distribution of trees, branch lengths and substitution parameters. Recall that in the Bayesian framework this is how we learn about parameter values: instead of finding the best point estimates, we typically want to quantify the probability of the entire range of possible values. An estimate of the time left is shown in the last column of output.&lt;br /&gt;
&lt;br /&gt;
: Let us examine the command in detail. First, ngen=1000000 samplefreq=100 lets the search run for 1,000,000 MCMC steps (&amp;quot;generations&amp;quot;) and saves parameter values once every 100 rounds (meaning that a total of 10,000 sets of parameter values will be saved to sample files). You sometimes need to run longer (or shorter) than 1,000,000, and would then typically tweak samplefreq so you get around 1,000 - 10,000 samples in all. The option nchains=3 means that the MCMCMC sampling uses 3 parallel chains (but see below): one &amp;quot;cold&amp;quot; from which sampling takes place, and two &amp;quot;heated&amp;quot; that move around in the parameter space more quickly to find additional peaks in the probability distribution.&lt;br /&gt;
&lt;br /&gt;
: The option diagnfreq=5000 has to do with testing whether the MrBayes run is successful. Briefly, MrBayes will start two entirely independent runs starting from different random trees. In the early phases of the run, the two runs will sample very different trees, but when they have reached convergence (when they produce a good sample from the posterior probability distribution), the two tree samples should be very similar. Every diagnfreq generations, the program will compute a measure of how similar the tree samples are, specifically the average standard deviation of split frequencies. A “split” is the same as a bipartition, i.e. a division of all leaves in the tree into two groups, obtained by cutting an internal branch. For each split, MrBayes compares how often that split occurs in the two independent runs; if the runs have converged, these frequencies should be very similar, giving a small standard deviation. The program then averages this quantity across splits. As a rule of thumb, you may want to run until this value is less than 0.05 (the smaller the better)&lt;br /&gt;
&lt;br /&gt;
: During the run you will see reports about the progress of the two independent runs, each consisting of three chains. Each line of output lists the generation number and the log likelihoods of the current tree/parameter combination for each of the two groups of three chains (a column of asterisks separate the results for the independent runs). The cold chains are the ones enclosed in brackets [...], while the heated chains are enclosed in parentheses (...). Occasionally the chains will swap so one of the heated chains now becomes cold (and sampling then takes place from this chain).&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Continue run until parallel runs converge on same solution&#039;&#039;&#039;&lt;br /&gt;
:At the end of the run, Mrbayes will print the average standard deviation of split frequencies (which is a measure of how similar the tree samples of the two independent runs are). We recommend that you continue with the analysis until the value gets below 0.01 (if the value is larger than 0.01 then you should answer &amp;quot;yes&amp;quot; when the program asks &amp;quot;Continue the analysis? (yes/no)&amp;quot;.)&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Question: &#039;&#039;&#039;MrBayes starts two independent runs from different random trees. Why is it useful to run two independent analyses instead of just one? How does the average standard deviation of split frequencies help you decide whether the two runs have converged to the same posterior distribution? At approximately how many generations does this happen in your run?&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Question 2&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Have a look at the resulting sample files&#039;&#039;&#039;&lt;br /&gt;
: Open a new Terminal window (don&#039;t quit mrbayes in the other terminal yet!) and cd to the bayes directory. Open one of the parameter sampling files in a text editor:&lt;br /&gt;
 nedit primatemitDNA.nexus.run1.p &amp;amp;&lt;br /&gt;
: This file contains one line for each sampled point (you may want to turn off line-wrapping in nedit under the preferences menu). Each row corresponds to a certain sample time (or generation). Each column contains the sampled values of one specific parameter. The first line contains headings telling what the different columns are: &lt;br /&gt;
:* Gen: generation; number of MCMC steps taken so far&lt;br /&gt;
:* lnL: log likelihood of the current parameter estimates&lt;br /&gt;
:* LnPr: log of the prior probability&lt;br /&gt;
:* TL: tree length (sum of all branch lengths)&lt;br /&gt;
:* kappa: transition/transversion rate ratio&lt;br /&gt;
:* pi(A), pi(C), pi(G), pi(T): frequency of A, C, G, T&lt;br /&gt;
:* alpha: shape parameter for the gamma distribution. &lt;br /&gt;
&lt;br /&gt;
: (Column headings may be shifted relative to their corresponding columns). Note how the values of most parameters change a lot during the initial &amp;quot;burnin&amp;quot; period, before they settle near their most probable values. &lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Question: &#039;&#039;&#039;You will notice that lnL is always negative, while LnPr can sometimes be positive. At first sight this may seem impossible, since probabilities cannot be larger than 1. How can this happen?&lt;br /&gt;
&lt;br /&gt;
As a hint, note that (1) priors for continuous parameters are probability densities, and (2) the default prior for each branch length in MrBayes is an exponential distribution with rate 10. Use the following R code to plot this prior on both an ordinary y-axis and a log-scaled y-axis, and then explain why positive values of LnPr are possible.&lt;br /&gt;
&lt;br /&gt;
 df_expdist = tibble(&lt;br /&gt;
   x = seq(0, 1, by = 0.001),&lt;br /&gt;
   density = dexp(x, rate = 10),&lt;br /&gt;
   logdensity = log(dexp(x, rate = 10))&lt;br /&gt;
 )&lt;br /&gt;
 &lt;br /&gt;
 ggplot(df_expdist, aes(x = x, y = density)) +&lt;br /&gt;
   geom_line(color=&amp;quot;blue&amp;quot;) +&lt;br /&gt;
   geom_hline(yintercept = 1, lty=2) +&lt;br /&gt;
   labs(&lt;br /&gt;
     x = &amp;quot;Branch length&amp;quot;,&lt;br /&gt;
     y = &amp;quot;Probability density&amp;quot;,&lt;br /&gt;
     title = &amp;quot;Exponential prior on branch lengths: Exp(rate = 10)&amp;quot;&lt;br /&gt;
   )&lt;br /&gt;
 &lt;br /&gt;
 ggplot(df_expdist, aes(x = x, y = logdensity)) +&lt;br /&gt;
   geom_line(color=&amp;quot;blue&amp;quot;) +&lt;br /&gt;
   geom_hline(yintercept = 0, lty=2) +&lt;br /&gt;
   labs(&lt;br /&gt;
     x = &amp;quot;Branch length&amp;quot;,&lt;br /&gt;
     y = &amp;quot;log of Probability density&amp;quot;,&lt;br /&gt;
     title = &amp;quot;Exponential prior on branch lengths: Exp(rate = 10)&amp;quot;&lt;br /&gt;
   )&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Question 3&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Examine MCMC trajectory for gamma shape parameter, alpha&#039;&#039;&#039;&lt;br /&gt;
: Recall that the idea in MCMCMC sampling is to move around in parameter space in such a way that points are visited according to their posterior probability (i.e., regions with high posterior probability are visited frequently). Now, in RStudio, plot the sampled values for the gamma shape parameter, alpha, for one of the run files:&lt;br /&gt;
 df_primates = read_tsv(&amp;quot;primatemitDNA.nexus.run1.p&amp;quot;, skip=1)&lt;br /&gt;
 mcmc_trace(df_primates, pars=&amp;quot;alpha&amp;quot;)&lt;br /&gt;
: mcmc_trace is one of several plotting commands available in the bayesplot package. This command plots the sampled values of the parameter alpha from the first of the two parallel runs against MCMC generation number. Thus, the x-axis shows the progress of the run through time, with the leftmost values being the earliest samples and the rightmost values the later ones. Note how the Markov chain starts at the arbitrary value 1.0, rapidly moves to values that fit the observed data better, and then moves around in parameter space, sampling different plausible values of alpha. You can experiment with plotting other columns as well.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Question: &#039;&#039;&#039;Describe briefly what happens to the sampled values of alpha during the run. Why is it reasonable to discard the earliest samples as burn-in?&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Question 4&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Investigate posterior probability distribution over trees&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
: Now, close the nedit window and have a look at the file containing sampled trees:&lt;br /&gt;
 nedit primatemitDNA.nexus.run1.t &amp;amp;&lt;br /&gt;
: Tree topology is also a parameter in our model, and exactly like for the other parameters we also get samples from tree-space. One tree is printed per line in the parenthetical Newick format you have seen before. There are 5 taxa in the present data set, so the number of possible unrooted binary tree topologies is only 15. Since we have taken more than 15 sample points, there must be several lines containing the same tree topology. Close the nedit window when you are done.&lt;br /&gt;
&lt;br /&gt;
: MrBayes provides the sumt command to summarize the sampled trees. Before using it, we need to decide on the burn-in: The burn-in is the initial set of samples that are typically discarded, because we want to ensure that the MCMC has moved away from the random starting values, and has found the peaks of the probability landscape. Since the convergence diagnostic used a relative burn-in of 25%, we will also discard the first 25% of tree samples when summarizing the posterior.&lt;br /&gt;
&lt;br /&gt;
: Return to the shell window where you have MrBayes running. In the command below relburnin=yes and burninfrac=0.25 tells MrBayes to discard 25% of the samples as burnin (you could also have explicitly given the number of samples to discard - help sumt will give you details about the command and the current option settings).&lt;br /&gt;
 sumt contype=halfcompat conformat=simple relburnin=yes burninfrac=0.25 showtreeprobs=yes&lt;br /&gt;
: (Scroll back so you can see the top of the output when the command is done). This command gives you a summary of the trees that are in the file you examined manually above. The option contype=halfcompat requests that a majority rule consensus tree is calculated from the set of trees that are left after discarding the burnin. This consensus is the first tree plotted to the screen. Below the consensus cladogram, a consensus phylogram is plotted. The branch lengths in this have been averaged over the trees in which that branch was present (a particular branch corresponds to a bi-partition of the data, and will typically not be present in every sampled tree). The cladogram also has &amp;quot;clade credibility&amp;quot; values. We will return to the meaning of these later in today&#039;s exercise.&lt;br /&gt;
&lt;br /&gt;
: What most interests us right now is the list of trees that is printed after the phylogram. These trees are labeled &amp;quot;Tree 1&amp;quot;, &amp;quot;Tree 2&amp;quot;, etc, and are sorted according to their posterior probability which is indicated by a lower-case p after the tree number. (The upper-case P gives the cumulated probability of trees shown so far, and is useful for constructing a credible set). This list highlights how Bayesian phylogenetic analysis is different from maximum likelihood: Instead of finding the best tree(s), we here quantify our degree of belief in all possible trees.&lt;br /&gt;
&lt;br /&gt;
: The list of trees and probabilities was printed because of the option showtreeprobs=yes. Note that you probably do not want to issue that command if you have much more than 5 taxa! In that case you could instead inspect the file named primatemitDNA.nexus.trprobs which is now present in the same directory as your other files (this file is automatically produced by the sumt command).&lt;br /&gt;
&lt;br /&gt;
: &#039;&#039;&#039;NOTE&#039;&#039;&#039;: Annoyingly, there is a bug in the version of mrbayes we are using here, which means leaf names are not printed on the list of trees with probabilities. However, the most probable tree here in fact is identical to the consensus tree printed above it.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Question: &#039;&#039;&#039;What is the posterior probability of the most probable tree? Does the analysis strongly support a single tree, or is the posterior probability distributed across several different trees?&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
== Analysis of Neanderthal data (posterior probability of clades) ==&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Question 5&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
For many years, there was considerable debate about the origin of modern humans. One view, often called the [https://www.thoughtco.com/multiregional-hypothesis-167235 Multiregional Hypothesis], proposed that after Homo erectus spread from Africa into different parts of the world, regional populations gradually evolved into modern humans more or less in parallel. A different view, often called the Recent African Origin model, proposed that modern Homo sapiens evolved in Africa and later spread outward, largely replacing other archaic human groups such as the Neanderthals.&lt;br /&gt;
&lt;br /&gt;
Today it is clear that the history is more complicated than either simple extreme: modern humans arose in Africa, but there was also some interbreeding with Neanderthals and other archaic humans. However, in this exercise we will focus on a narrower question that can be addressed using a phylogeny of mitochondrial DNA: do the sampled Neanderthal and human mitochondrial sequences suggest that the Neanderthal sequence falls inside or outside modern human mitochondrial diversity?&lt;br /&gt;
&lt;br /&gt;
We will use the present data set to examine this question.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Load Neanderthal data set&#039;&#039;&#039;&lt;br /&gt;
: In the Terminal where you have MrBayes running:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
execute neanderthal.nexus&lt;br /&gt;
delete 5-40&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
: As we did for the maximum likelihood analysis, we will discard some of the human sequences in order to speed up the analysis. The command delete 5-40 removes sequence number 5 to sequence number 40 from the active data set.&lt;br /&gt;
&lt;br /&gt;
: As we did for the maximum likelihood analysis, we will discard some of the human sequences in order to speed up the analysis. The command delete 5-40 removes sequence number 5 to sequence number 40 from the active data set.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Investigate data&#039;&#039;&#039;&lt;br /&gt;
 showmatrix&lt;br /&gt;
: This data set consists of an alignment of mitochondrial DNA from human (17 sequences), chimpanzee (1 sequence), and Neanderthal (1 sequence). The Neanderthal DNA was extracted from archaeological material, specifically bones found at Vindija in Croatia.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Start analysis&#039;&#039;&#039;&lt;br /&gt;
 outgroup Pan_troglodytes&lt;br /&gt;
 lset nst=mixed rates=gamma&lt;br /&gt;
 mcmc ngen=500000 nchains=3 diagnfreq=10000&lt;br /&gt;
&lt;br /&gt;
: Here we use the command `nst=mixed` which allows MrBayes to automatically explore all possible substitution models. Essentially, MrBayes now considers the substitution model as one more parameter, and uses MCMC to sample from the possible versions (with nst ranging from 1 to 6). This will often be the best choice when using MrBayes. (Below, I use nst=6 for pedagogical purposes, because it makes it simpler to analyse the output files).&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Find posterior probability of clades&#039;&#039;&#039;&lt;br /&gt;
 sumt contype=halfcompat showtreeprobs=no relburnin=yes burninfrac=0.25&lt;br /&gt;
: Examine the consensus tree that is plotted to screen: On the branches that are resolved, you will notice that numbers have been plotted. These are clade-credibility values, and are in fact the posterior probability that the clade is real (based on the present data set).&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Question: &#039;&#039;&#039;What is the posterior probability that all sampled Homo sapiens sequences form a monophyletic group excluding the Neanderthal sequence? Does this support placing the Neanderthal outside modern human mitochondrial diversity?&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
== Probability distributions over other parameters ==&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Question 6&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
: As the last thing, we will now turn away from the tree topology, and instead examine the other parameters that also form part of the probabilistic model. We will do this using a reduced version of the Hepatitis C virus data set that we have examined previously. Stay in the shell window where you just performed the analysis of Neanderthal sequences.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Load data set&#039;&#039;&#039;&lt;br /&gt;
 execute hcvsmall.nexus&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Define site partition&#039;&#039;&#039;&lt;br /&gt;
 charset 1stpos=1-.\3&lt;br /&gt;
 charset 2ndpos=2-.\3&lt;br /&gt;
 charset 3rdpos=3-.\3&lt;br /&gt;
 partition bycodon = 3:1stpos,2ndpos,3rdpos&lt;br /&gt;
 set partition=bycodon&lt;br /&gt;
 prset ratepr=variable&lt;br /&gt;
: This is an alternative way of specifying that different sites have different rates. Instead of using a gamma distribution and learning about site-specific rates from the data, we are instead using our prior knowledge about the structure of the genetic code to specify that all 1st codon positions have the same rate, all 2nd codon positions have the same rate, and all 3rd codon positions have the same rate. Specifically, charset 1stpos=1-.\3 means that we define a character set named &amp;quot;1stpos&amp;quot; which includes site 1 in the alignment followed by every third site (&amp;quot;\3&amp;quot;, meaning it includes sites 1, 4, 7, 11, ...) until the end of the alignment (here denoted &amp;quot;.&amp;quot;).&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Specify model&#039;&#039;&#039;&lt;br /&gt;
 lset nst=6&lt;br /&gt;
: This specifies that we want to use a model of the General Time Reversible (GTR) type, where all 6 substitution types have separate rate parameters.&lt;br /&gt;
&lt;br /&gt;
: When the lset command was discussed previously, a few issues were glossed over. Importantly, and unlike PAUP, the lset command in MrBayes gives no information about whether nucleotide frequencies are equal or not, and whether they should be estimated from the data or not. In MrBayes this is instead controlled by defining the prior probability of the nucleotide frequencies (the command prset can be used to set priors). For instance, a model with equal nucleotide frequencies corresponds to having prior probability 1 (one) for the frequency vector (A=0.25, C=0.25, G=0.25, T=0.25), and zero prior probability for the infinitely many other possible vectors. As you will see below, the default prior is not this limited, and the program will therefore estimate the frequencies from the data.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Inspect model details&#039;&#039;&#039;&lt;br /&gt;
 showmodel&lt;br /&gt;
: This command gives you a summary of the current model settings. You will also get a summary of how the prior probabilities of all model parameters are set. You will for instance notice that the nucleotide frequencies (parameter labeled &amp;quot;Statefreq&amp;quot;) have a &amp;quot;Dirichlet&amp;quot; prior. Without going into details, the Dirichlet distribution is a probability distribution over frequency vectors (i.e., vectors of positive values that sum to 1). Depending on the exact parameters the distribution can be more or less flat (flat here means that all sum-1 vectors are equally probable). The Dirichlet distribution is a handy way of specifying the prior probability distribution of nucleotide (or amino acid) frequency vectors. The default statefreq prior in MrBayes is the flat or un-informative prior dirichlet(1,1,1,1).&lt;br /&gt;
&lt;br /&gt;
: We will not go into the priors for the remaining parameters in any detail, but you may notice that by default all topologies are taken to be equally likely (a flat prior on trees).&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Start MCMC sampling&#039;&#039;&#039;&lt;br /&gt;
 mcmc ngen=1000000 samplefreq=100 diagnfreq=10000 nchains=3&lt;br /&gt;
: The run will take a while to finish (you may want to ensure that the average standard deviation of split frequencies is less than 0.01 before ending the analysis).&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Compute summary of parameter values&#039;&#039;&#039;&lt;br /&gt;
 sump relburnin=yes burninfrac=0.25&lt;br /&gt;
: The sump command (with a &amp;quot;p&amp;quot; at the end) works much like the sumt command (with a &amp;quot;t&amp;quot; at the end), but for other parameters than the tree-topology. Again, we are using 25% of the total number of samples as burnin.&lt;br /&gt;
&lt;br /&gt;
: First, you get a scatter plot of the lnL as a function of generation number. Values from the two independent runs are labeled &amp;quot;1&amp;quot; and &amp;quot;2&amp;quot; respectively. If the burnin is suitable, then the points should be randomly scattered over a narrow lnL interval.&lt;br /&gt;
&lt;br /&gt;
: Secondly, the posterior probability distribution of each parameter is summarized by giving the mean, variance, median, and 95% credible interval.&lt;br /&gt;
&lt;br /&gt;
: The last columns contain values indicating if the run has converged. Specifically ESS means Effective Sample Size, and is a measure of how many independent samples you have from the posterior - the higher the better, but this should be at least 100. The column labeled PSRF+ is a measure (also known as &amp;quot;R-hat&amp;quot;) which should be close to 1 if the runs have converged. Specifically this measures whether different chains (and different parts of different chains) converge to sample the same set of values. &lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Question: &#039;&#039;&#039;What are the posterior mean values of the relative substitution rate parameters r(AC) and r(CG)?&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Question 7: &#039;&#039;&#039; Based on the reported posterior means, does r(CG) appear to be larger than r(AC)?&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Question 8&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Marginal vs. joint distributions&#039;&#039;&#039;&lt;br /&gt;
: Strictly speaking the comparison above was not entirely appropriate. We first found the overall distribution of the r(CG) parameter and then compared its mean to the mean of the overall distribution of the r(AC) parameter. By doing things this way, we are ignoring the possibility that the two parameters might be associated in some way. For instance, one parameter might always be larger than the other in any individual sample, even though the total distributions overlap. We should instead be looking at the distribution over both parameters simultaneously. A probability distribution over several parameters simultaneously is called a &amp;quot;joint distribution&amp;quot; over the parameters.&lt;br /&gt;
&lt;br /&gt;
: By looking at one parameter at a time, we are summing its probability over all values of the other parameters. This is called the marginal distribution.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Examine marginal distributions&#039;&#039;&#039;&lt;br /&gt;
: In RStudio, use the following commands to read and plot the marginal distributions of r(AC) and r(CG). Note that we are discarding the first 25% of the reads as burnin&lt;br /&gt;
 df_hcv = read_tsv(&amp;quot;hcvsmall.nexus.run1.p&amp;quot;, skip=1)&lt;br /&gt;
 burnin = df_hcv$Gen %&amp;gt;% &lt;br /&gt;
     max() %&amp;gt;% &lt;br /&gt;
     multiply_by(0.25) %&amp;gt;% &lt;br /&gt;
     floor()&lt;br /&gt;
 df_hcv2 = df_hcv %&amp;gt;% &lt;br /&gt;
     filter(Gen &amp;gt; burnin) %&amp;gt;%&lt;br /&gt;
     select(CG = `r(C&amp;lt;-&amp;gt;G){all}`,&lt;br /&gt;
            AC = `r(A&amp;lt;-&amp;gt;C){all}`&lt;br /&gt;
            )&lt;br /&gt;
 mcmc_intervals(df_hcv2, prob_outer = 1)&lt;br /&gt;
 mcmc_areas(df_hcv2, prob_outer = 1)&lt;br /&gt;
: The functions mcmc_intervals and mcmc_areas plot different views of the same posterior distributions. &lt;br /&gt;
&lt;br /&gt;
: You can also simply plot the data using ggplot:&lt;br /&gt;
 df_hcv2_long = pivot_longer(df_hcv2, cols = c(&amp;quot;CG&amp;quot;, &amp;quot;AC&amp;quot;))&lt;br /&gt;
 &lt;br /&gt;
 ggplot(df_hcv2_long) + &lt;br /&gt;
     geom_density(mapping=aes(x=value, fill=name), alpha=0.3) + &lt;br /&gt;
     labs(x=&amp;quot;Substitution rate&amp;quot;)&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Question&#039;&#039;&#039;: Based on the marginal distributions, r(AC) appears to be centered at a higher value than r(CG), but the two distributions overlap somewhat. Can you from these marginal distributions alone decide whether r(AC) is larger than r(CG) in most posterior samples? Why or why not?&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Question 9&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Examine joint distributions&#039;&#039;&#039;&lt;br /&gt;
: These plots and results explore the relationship between the A&amp;lt;-&amp;gt;C and C&amp;lt;-&amp;gt;G rates.&lt;br /&gt;
 ggplot(df_hcv2, aes(x=CG, y=AC)) + &lt;br /&gt;
     geom_point(col=&amp;quot;blue&amp;quot;) + &lt;br /&gt;
     geom_abline(intercept=0, slope=1, lty=2, col=&amp;quot;red&amp;quot;) +&lt;br /&gt;
     xlim(0,0.25) + &lt;br /&gt;
     ylim(0,0.25) + &lt;br /&gt;
     labs(x=&amp;quot;CG rate&amp;quot;, y =&amp;quot;AC rate&amp;quot;) &lt;br /&gt;
 &lt;br /&gt;
 ggplot(df_hcv2, aes(x=CG, y= AC)) + &lt;br /&gt;
     geom_hex(col=&amp;quot;blue&amp;quot;) + &lt;br /&gt;
     geom_abline(intercept=0, slope=1, lty=2, col=&amp;quot;red&amp;quot;) +&lt;br /&gt;
     xlim(0,0.25) + &lt;br /&gt;
     ylim(0, 0.25) + &lt;br /&gt;
     labs(x=&amp;quot;CG rate&amp;quot;, y =&amp;quot;AC rate&amp;quot;) &lt;br /&gt;
 &lt;br /&gt;
 df_hcv2 %&amp;gt;%&lt;br /&gt;
     nrow()&lt;br /&gt;
 df_hcv2 %&amp;gt;%&lt;br /&gt;
     filter(AC&amp;gt;CG) %&amp;gt;%&lt;br /&gt;
     nrow()&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Question: &#039;&#039;&#039;Based on the two different ways to plot the joint distribution and based on the unfiltered and filtered row counts, what is the posterior probability that r(AC) &amp;gt; r(CG)?&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Question 10&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
: Note how examining the joint distribution provides you with information that you could not get from simply comparing the marginal distributions. This very simple procedure can be used to answer many different questions.&lt;br /&gt;
&lt;br /&gt;
: Now, plot the relative substitution rates at the first, second, and third codon positions:&lt;br /&gt;
 df_hcv3 = df_hcv %&amp;gt;% &lt;br /&gt;
     filter(Gen &amp;gt; 75000) %&amp;gt;%&lt;br /&gt;
     select(Codon_1st = `m{1}`,&lt;br /&gt;
            Codon_2nd = `m{2}`,&lt;br /&gt;
            Codon_3rd = `m{3}` ) %&amp;gt;%&lt;br /&gt;
     pivot_longer(cols=c(&amp;quot;Codon_1st&amp;quot;, &amp;quot;Codon_2nd&amp;quot;, &amp;quot;Codon_3rd&amp;quot;))&lt;br /&gt;
 &lt;br /&gt;
 ggplot(df_hcv3) + &lt;br /&gt;
     geom_density(mapping=aes(x=value, fill=name), alpha=0.3) + &lt;br /&gt;
     labs(x=&amp;quot;Relative substitution rate&amp;quot;)&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Question: &#039;&#039;&#039;Since random mutations presumably hit all three codon positions with the same frequency, any differences are expected to be caused by subsequent selection. Which of the following statement are correct ? (More than one answer may be correct)&lt;br /&gt;
&lt;br /&gt;
:* Codon position 2 is the most variable of the codon positions.&lt;br /&gt;
:* Codon position 1 is the most variable of the codon positions.&lt;br /&gt;
:* Codon position 1 is the most conserved codon position.&lt;br /&gt;
:* Codon position 3 is the most conserved codon position.&lt;br /&gt;
:* Codon position 3 is the most variable of the codon positions.&lt;br /&gt;
:* Codon position 2 is the most conserved codon position.&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Question 11&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Question: &#039;&#039;&#039;How does this result fit with your knowledge of the genetic code? Why are these codon positions the most conserved or the most variable?&lt;/div&gt;</summary>
		<author><name>Gorm</name></author>
	</entry>
	<entry>
		<id>https://teaching.healthtech.dtu.dk/22115/index.php?title=Bayesian_Phylogeny&amp;diff=245</id>
		<title>Bayesian Phylogeny</title>
		<link rel="alternate" type="text/html" href="https://teaching.healthtech.dtu.dk/22115/index.php?title=Bayesian_Phylogeny&amp;diff=245"/>
		<updated>2026-03-19T09:51:49Z</updated>

		<summary type="html">&lt;p&gt;Gorm: /* Analysis of Neanderthal data (posterior probability of clades) */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;This exercise is part of the course  [[22115_-_Computational_Molecular_Evolution|Computational Molecular Evolution (22115)]].&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Overview ==&lt;br /&gt;
&lt;br /&gt;
Today&#039;s exercise will focus on phylogenetic analysis using Bayesian methods.&lt;br /&gt;
&lt;br /&gt;
As was the case for likelihood methods, Bayesian analysis is founded on having a probabilistic model of how the observed data is produced. This means that, for a given set of parameter values, you can compute the probability or [https://www.statlect.com/glossary/probability-density-function probability density] of any possible observation. For a full dataset, you then obtain the likelihood by multiplying these values across all observations. You will recall from the lecture that in Bayesian statistics the goal is to obtain a full posterior probability distribution over all possible parameter values. The posterior distribution quantifies our degree of belief in any possible parameter value after seeing the data. It is obtained by updating the prior probability distribution using the likelihood of the observed data.&lt;br /&gt;
&lt;br /&gt;
The prior probability distribution expresses your beliefs about the parameters before seeing any data, while the likelihood expresses what the observed data are telling you about the parameters. Specifically, the likelihood of a parameter value is the probability of the observed data given that parameter value. We regard a parameter value as more plausible the more probable it makes the observed data. This is the same measure we have previously used to find the maximum likelihood estimate. If the prior probability distribution is flat (i.e., if all possible parameter values have the same prior probability), then the posterior distribution is proportional to the likelihood, and the parameter value with the maximum likelihood also has the maximum posterior probability. However, even in this case, using a Bayesian approach still lets you interpret the result as a probability distribution over parameter values. &lt;br /&gt;
&lt;br /&gt;
If the prior is not flat, then it may have a substantial impact on the posterior, although this effect will usually diminish as the amount of data increases. A prior should ideally be based on domain knowledge and results from previous experiments. For instance one can use the posterior from one analysis as the prior in a new, independent analysis. Often a prior is chosen to be weakly informative, meaning that it places reasonable bounds on the parameter values without constraining them too narrowly. For instance the transition/transversion rate ratio kappa is typically 1.5-10. Values such as 100, 1,000 or 1,000,000 would be extremely unlikely, so a weakly informative prior for this parameter could be chosen to place 95% of its probability mass in the 0.5-20 range, slightly wider than what we think of as plausible values. For instance one could use a lognormal distribution with suitable parameters.&lt;br /&gt;
&lt;br /&gt;
In Bayesian phylogeny the parameters are of the same kind as in maximum likelihood phylogeny. Typical parameters include tree topology, branch lengths, nucleotide frequencies, and substitution model parameters such as the transition/transversion rate ratio or the gamma shape parameter. The difference is that, whereas in maximum likelihood phylogeny we seek the best point estimates of the parameter values, in Bayesian phylogeny the goal is instead to infer a full probability distribution over the possible parameter values. The observed data are again usually taken to be the alignment, although strictly speaking it would be more reasonable to say that the sequences are what have been observed, and that the alignment should then be inferred jointly with the phylogeny.&lt;br /&gt;
&lt;br /&gt;
In this exercise we will explore how one can determine and use posterior probability distributions over trees, over clades, and over substitution parameters. We will also touch upon the difference between marginal and joint probability distributions.&lt;br /&gt;
&lt;br /&gt;
== Getting started ==&lt;br /&gt;
&lt;br /&gt;
: In the command below: Instead of /path/to/molevol enter the path to the directory where you have placed your course files (for instance cd /Users/bob/Documents/molevol, or cd /home/student/molevol).&lt;br /&gt;
 cd /path/to/molevol&lt;br /&gt;
 mkdir bayes&lt;br /&gt;
 cd bayes&lt;br /&gt;
 cp ../data/primatemitDNA.nexus ./primatemitDNA.nexus&lt;br /&gt;
 cp ../data/neanderthal.nexus ./neanderthal.nexus&lt;br /&gt;
 cp ../data/hcvsmall.nexus ./hcvsmall.nexus&lt;br /&gt;
&lt;br /&gt;
: You have analyzed (versions of) all these data files previously in this course. We will now use Bayesian phylogenetic analysis to complement what we learned in those analyses.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Load R libraries&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
: In RStudio: set the working directory to the bayes directory. Then issue these commands:&lt;br /&gt;
 library(magrittr)&lt;br /&gt;
 library(tidyverse)&lt;br /&gt;
 library(bayesplot)&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
== Posterior probability of trees ==&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Question 1&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
: In today&#039;s exercise we will be using the program &amp;quot;MrBayes&amp;quot; to perform Bayesian phylogenetic analysis. MrBayes is a program that, like PAUP*, can be controlled by giving commands at a command line prompt. In fact, there is a substantial overlap between the commands used to control MrBayes and the PAUP command language. This should be a help when you are trying to understand how to use the program.&lt;br /&gt;
&lt;br /&gt;
: Note that the command &amp;quot;help&amp;quot; will give you a list of all available commands. Issuing &amp;quot;help &#039;&#039;command&#039;&#039;&amp;quot; will give you a more detailed description of the specified command along with current option values. This is similar to how &amp;quot;help &#039;&#039;command&#039;&#039;&amp;quot; works in PAUP.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Start program&#039;&#039;&#039;&lt;br /&gt;
: In a terminal window, issue the command:&lt;br /&gt;
 mb&lt;br /&gt;
: This starts the program, giving you a prompt (&amp;quot;MrBayes&amp;gt; &amp;quot;) where you can enter commands.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Get a quick overview of available commands&#039;&#039;&#039;&lt;br /&gt;
 help&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Load your sequences&#039;&#039;&#039;&lt;br /&gt;
 execute primatemitDNA.nexus&lt;br /&gt;
: This file contains mitochondrial DNA sequences from 5 different primates. Note that MrBayes accepts input in nexus format, and that this is the same command that was used to load sequences in PAUP*. In general, you can use many of the PAUP commands in MrBayes also.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Inspect data set&#039;&#039;&#039;&lt;br /&gt;
 showmatrix&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Define outgroup&#039;&#039;&#039;&lt;br /&gt;
 outgroup Gibbon&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Specify your model of sequence evolution&#039;&#039;&#039;&lt;br /&gt;
 lset nst=2 rates=gamma&lt;br /&gt;
: This command is again very much like the corresponding one in PAUP. You are specifying that you want to use a model with two substitution types (nst=2), and this is automatically taken to mean that you want to distinguish between transitions and transversions. Furthermore, rates=gamma means that you want the model to use a gamma distribution to account for different rates at different sites in the sequence.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Start Markov chain Monte Carlo sampling&#039;&#039;&#039;&lt;br /&gt;
:Make sure to make the shell window as wide as possible and then issue the following commands to start the run:&lt;br /&gt;
 mcmc ngen=1000000 samplefreq=100 nchains=3 diagnfreq=5000&lt;br /&gt;
: What you are doing here is to use the method known as MCMCMC (&amp;quot;Metropolis-coupled Markov chain Monte Carlo&amp;quot;) to empirically determine the posterior probability distribution of trees, branch lengths and substitution parameters. Recall that in the Bayesian framework this is how we learn about parameter values: instead of finding the best point estimates, we typically want to quantify the probability of the entire range of possible values. An estimate of the time left is shown in the last column of output.&lt;br /&gt;
&lt;br /&gt;
: Let us examine the command in detail. First, ngen=1000000 samplefreq=100 lets the search run for 1,000,000 MCMC steps (&amp;quot;generations&amp;quot;) and saves parameter values once every 100 rounds (meaning that a total of 10,000 sets of parameter values will be saved to sample files). You sometimes need to run longer (or shorter) than 1,000,000, and would then typically tweak samplefreq so you get around 1,000 - 10,000 samples in all. The option nchains=3 means that the MCMCMC sampling uses 3 parallel chains (but see below): one &amp;quot;cold&amp;quot; from which sampling takes place, and two &amp;quot;heated&amp;quot; that move around in the parameter space more quickly to find additional peaks in the probability distribution.&lt;br /&gt;
&lt;br /&gt;
: The option diagnfreq=5000 has to do with testing whether the MrBayes run is successful. Briefly, MrBayes will start two entirely independent runs starting from different random trees. In the early phases of the run, the two runs will sample very different trees, but when they have reached convergence (when they produce a good sample from the posterior probability distribution), the two tree samples should be very similar. Every diagnfreq generations, the program will compute a measure of how similar the tree samples are, specifically the average standard deviation of split frequencies. A “split” is the same as a bipartition, i.e. a division of all leaves in the tree into two groups, obtained by cutting an internal branch. For each split, MrBayes compares how often that split occurs in the two independent runs; if the runs have converged, these frequencies should be very similar, giving a small standard deviation. The program then averages this quantity across splits. As a rule of thumb, you may want to run until this value is less than 0.05 (the smaller the better)&lt;br /&gt;
&lt;br /&gt;
: During the run you will see reports about the progress of the two independent runs, each consisting of three chains. Each line of output lists the generation number and the log likelihoods of the current tree/parameter combination for each of the two groups of three chains (a column of asterisks separate the results for the independent runs). The cold chains are the ones enclosed in brackets [...], while the heated chains are enclosed in parentheses (...). Occasionally the chains will swap so one of the heated chains now becomes cold (and sampling then takes place from this chain).&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Continue run until parallel runs converge on same solution&#039;&#039;&#039;&lt;br /&gt;
:At the end of the run, Mrbayes will print the average standard deviation of split frequencies (which is a measure of how similar the tree samples of the two independent runs are). We recommend that you continue with the analysis until the value gets below 0.01 (if the value is larger than 0.01 then you should answer &amp;quot;yes&amp;quot; when the program asks &amp;quot;Continue the analysis? (yes/no)&amp;quot;.)&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Question: &#039;&#039;&#039;MrBayes starts two independent runs from different random trees. Why is it useful to run two independent analyses instead of just one? How does the average standard deviation of split frequencies help you decide whether the two runs have converged to the same posterior distribution? At approximately how many generations does this happen in your run?&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Question 2&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Have a look at the resulting sample files&#039;&#039;&#039;&lt;br /&gt;
: Open a new Terminal window (don&#039;t quit mrbayes in the other terminal yet!) and cd to the bayes directory. Open one of the parameter sampling files in a text editor:&lt;br /&gt;
 nedit primatemitDNA.nexus.run1.p &amp;amp;&lt;br /&gt;
: This file contains one line for each sampled point (you may want to turn off line-wrapping in nedit under the preferences menu). Each row corresponds to a certain sample time (or generation). Each column contains the sampled values of one specific parameter. The first line contains headings telling what the different columns are: &lt;br /&gt;
:* Gen: generation; number of MCMC steps taken so far&lt;br /&gt;
:* lnL: log likelihood of the current parameter estimates&lt;br /&gt;
:* LnPr: log of the prior probability&lt;br /&gt;
:* TL: tree length (sum of all branch lengths)&lt;br /&gt;
:* kappa: transition/transversion rate ratio&lt;br /&gt;
:* pi(A), pi(C), pi(G), pi(T): frequency of A, C, G, T&lt;br /&gt;
:* alpha: shape parameter for the gamma distribution. &lt;br /&gt;
&lt;br /&gt;
: (Column headings may be shifted relative to their corresponding columns). Note how the values of most parameters change a lot during the initial &amp;quot;burnin&amp;quot; period, before they settle near their most probable values. &lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Question: &#039;&#039;&#039;You will notice that lnL is always negative, while LnPr can sometimes be positive. At first sight this may seem impossible, since probabilities cannot be larger than 1. How can this happen?&lt;br /&gt;
&lt;br /&gt;
As a hint, note that (1) priors for continuous parameters are probability densities, and (2) the default prior for each branch length in MrBayes is an exponential distribution with rate 10. Use the following R code to plot this prior on both an ordinary y-axis and a log-scaled y-axis, and then explain why positive values of LnPr are possible.&lt;br /&gt;
&lt;br /&gt;
 df_expdist = tibble(&lt;br /&gt;
   x = seq(0, 1, by = 0.001),&lt;br /&gt;
   density = dexp(x, rate = 10),&lt;br /&gt;
   logdensity = log(dexp(x, rate = 10))&lt;br /&gt;
 )&lt;br /&gt;
 &lt;br /&gt;
 ggplot(df_expdist, aes(x = x, y = density)) +&lt;br /&gt;
   geom_line(color=&amp;quot;blue&amp;quot;) +&lt;br /&gt;
   geom_hline(yintercept = 1, lty=2) +&lt;br /&gt;
   labs(&lt;br /&gt;
     x = &amp;quot;Branch length&amp;quot;,&lt;br /&gt;
     y = &amp;quot;Probability density&amp;quot;,&lt;br /&gt;
     title = &amp;quot;Exponential prior on branch lengths: Exp(rate = 10)&amp;quot;&lt;br /&gt;
   )&lt;br /&gt;
 &lt;br /&gt;
 ggplot(df_expdist, aes(x = x, y = logdensity)) +&lt;br /&gt;
   geom_line(color=&amp;quot;blue&amp;quot;) +&lt;br /&gt;
   geom_hline(yintercept = 0, lty=2) +&lt;br /&gt;
   labs(&lt;br /&gt;
     x = &amp;quot;Branch length&amp;quot;,&lt;br /&gt;
     y = &amp;quot;log of Probability density&amp;quot;,&lt;br /&gt;
     title = &amp;quot;Exponential prior on branch lengths: Exp(rate = 10)&amp;quot;&lt;br /&gt;
   )&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Question 3&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Examine MCMC trajectory for gamma shape parameter, alpha&#039;&#039;&#039;&lt;br /&gt;
: Recall that the idea in MCMCMC sampling is to move around in parameter space in such a way that points are visited according to their posterior probability (i.e., regions with high posterior probability are visited frequently). Now, in RStudio, plot the sampled values for the gamma shape parameter, alpha, for one of the run files:&lt;br /&gt;
 df_primates = read_tsv(&amp;quot;primatemitDNA.nexus.run1.p&amp;quot;, skip=1)&lt;br /&gt;
 mcmc_trace(df_primates, pars=&amp;quot;alpha&amp;quot;)&lt;br /&gt;
: mcmc_trace is one of several plotting commands available in the bayesplot package. This command plots the sampled values of the parameter alpha from the first of the two parallel runs against MCMC generation number. Thus, the x-axis shows the progress of the run through time, with the leftmost values being the earliest samples and the rightmost values the later ones. Note how the Markov chain starts at the arbitrary value 1.0, rapidly moves to values that fit the observed data better, and then moves around in parameter space, sampling different plausible values of alpha. You can experiment with plotting other columns as well.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Question: &#039;&#039;&#039;Describe briefly what happens to the sampled values of alpha during the run. Why is it reasonable to discard the earliest samples as burn-in?&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Question 4&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Investigate posterior probability distribution over trees&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
: Now, close the nedit window and have a look at the file containing sampled trees:&lt;br /&gt;
 nedit primatemitDNA.nexus.run1.t &amp;amp;&lt;br /&gt;
: Tree topology is also a parameter in our model, and exactly like for the other parameters we also get samples from tree-space. One tree is printed per line in the parenthetical Newick format you have seen before. There are 5 taxa in the present data set, so the number of possible unrooted binary tree topologies is only 15. Since we have taken more than 15 sample points, there must be several lines containing the same tree topology. Close the nedit window when you are done.&lt;br /&gt;
&lt;br /&gt;
: MrBayes provides the sumt command to summarize the sampled trees. Before using it, we need to decide on the burn-in: The burn-in is the initial set of samples that are typically discarded, because we want to ensure that the MCMC has moved away from the random starting values, and has found the peaks of the probability landscape. Since the convergence diagnostic used a relative burn-in of 25%, we will also discard the first 25% of tree samples when summarizing the posterior.&lt;br /&gt;
&lt;br /&gt;
: Return to the shell window where you have MrBayes running. In the command below relburnin=yes and burninfrac=0.25 tells MrBayes to discard 25% of the samples as burnin (you could also have explicitly given the number of samples to discard - help sumt will give you details about the command and the current option settings).&lt;br /&gt;
 sumt contype=halfcompat conformat=simple relburnin=yes burninfrac=0.25 showtreeprobs=yes&lt;br /&gt;
: (Scroll back so you can see the top of the output when the command is done). This command gives you a summary of the trees that are in the file you examined manually above. The option contype=halfcompat requests that a majority rule consensus tree is calculated from the set of trees that are left after discarding the burnin. This consensus is the first tree plotted to the screen. Below the consensus cladogram, a consensus phylogram is plotted. The branch lengths in this have been averaged over the trees in which that branch was present (a particular branch corresponds to a bi-partition of the data, and will typically not be present in every sampled tree). The cladogram also has &amp;quot;clade credibility&amp;quot; values. We will return to the meaning of these later in today&#039;s exercise.&lt;br /&gt;
&lt;br /&gt;
: What most interests us right now is the list of trees that is printed after the phylogram. These trees are labeled &amp;quot;Tree 1&amp;quot;, &amp;quot;Tree 2&amp;quot;, etc, and are sorted according to their posterior probability which is indicated by a lower-case p after the tree number. (The upper-case P gives the cumulated probability of trees shown so far, and is useful for constructing a credible set). This list highlights how Bayesian phylogenetic analysis is different from maximum likelihood: Instead of finding the best tree(s), we here quantify our degree of belief in all possible trees.&lt;br /&gt;
&lt;br /&gt;
: The list of trees and probabilities was printed because of the option showtreeprobs=yes. Note that you probably do not want to issue that command if you have much more than 5 taxa! In that case you could instead inspect the file named primatemitDNA.nexus.trprobs which is now present in the same directory as your other files (this file is automatically produced by the sumt command).&lt;br /&gt;
&lt;br /&gt;
: &#039;&#039;&#039;NOTE&#039;&#039;&#039;: Annoyingly, there is a bug in the version of mrbayes we are using here, which means leaf names are not printed on the list of trees with probabilities. However, the most probable tree here in fact is identical to the consensus tree printed above it.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Question: &#039;&#039;&#039;What is the posterior probability of the most probable tree? Does the analysis strongly support a single tree, or is the posterior probability distributed across several different trees?&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
== Analysis of Neanderthal data (posterior probability of clades) ==&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Question 5&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
For many years, there was considerable debate about the origin of modern humans. One view, often called the Multiregional Hypothesis, proposed that after Homo erectus spread from Africa into different parts of the world, regional populations gradually evolved into modern humans more or less in parallel. A different view, often called the Recent African Origin model, proposed that modern Homo sapiens evolved in Africa and later spread outward, largely replacing other archaic human groups such as the Neanderthals.&lt;br /&gt;
&lt;br /&gt;
Today it is clear that the history is more complicated than either simple extreme: modern humans arose in Africa, but there was also some interbreeding with Neanderthals and other archaic humans. However, in this exercise we will focus on a narrower question that can be addressed using a phylogeny of mitochondrial DNA: do the sampled Neanderthal and human mitochondrial sequences suggest that the Neanderthal sequence falls inside or outside modern human mitochondrial diversity?&lt;br /&gt;
&lt;br /&gt;
We will use the present data set to examine this question.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Load Neanderthal data set&#039;&#039;&#039;&lt;br /&gt;
: In the Terminal where you have MrBayes running:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
execute neanderthal.nexus&lt;br /&gt;
delete 5-40&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
: As we did for the maximum likelihood analysis, we will discard some of the human sequences in order to speed up the analysis. The command delete 5-40 removes sequence number 5 to sequence number 40 from the active data set.&lt;br /&gt;
&lt;br /&gt;
: As we did for the maximum likelihood analysis, we will discard some of the human sequences in order to speed up the analysis. The command delete 5-40 removes sequence number 5 to sequence number 40 from the active data set.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Investigate data&#039;&#039;&#039;&lt;br /&gt;
 showmatrix&lt;br /&gt;
: This data set consists of an alignment of mitochondrial DNA from human (17 sequences), chimpanzee (1 sequence), and Neanderthal (1 sequence). The Neanderthal DNA was extracted from archaeological material, specifically bones found at Vindija in Croatia.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Start analysis&#039;&#039;&#039;&lt;br /&gt;
 outgroup Pan_troglodytes&lt;br /&gt;
 lset nst=mixed rates=gamma&lt;br /&gt;
 mcmc ngen=500000 nchains=3 diagnfreq=10000&lt;br /&gt;
&lt;br /&gt;
: Here we use the command `nst=mixed` which allows MrBayes to automatically explore all possible substitution models. Essentially, MrBayes now considers the substitution model as one more parameter, and uses MCMC to sample from the possible versions (with nst ranging from 1 to 6). This will often be the best choice when using MrBayes. (Below, I use nst=6 for pedagogical purposes, because it makes it simpler to analyse the output files).&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Find posterior probability of clades&#039;&#039;&#039;&lt;br /&gt;
 sumt contype=halfcompat showtreeprobs=no relburnin=yes burninfrac=0.25&lt;br /&gt;
: Examine the consensus tree that is plotted to screen: On the branches that are resolved, you will notice that numbers have been plotted. These are clade-credibility values, and are in fact the posterior probability that the clade is real (based on the present data set).&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Question: &#039;&#039;&#039;What is the posterior probability that all sampled Homo sapiens sequences form a monophyletic group excluding the Neanderthal sequence? Does this support placing the Neanderthal outside modern human mitochondrial diversity?&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
== Probability distributions over other parameters ==&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Question 6&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
: As the last thing, we will now turn away from the tree topology, and instead examine the other parameters that also form part of the probabilistic model. We will do this using a reduced version of the Hepatitis C virus data set that we have examined previously. Stay in the shell window where you just performed the analysis of Neanderthal sequences.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Load data set&#039;&#039;&#039;&lt;br /&gt;
 execute hcvsmall.nexus&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Define site partition&#039;&#039;&#039;&lt;br /&gt;
 charset 1stpos=1-.\3&lt;br /&gt;
 charset 2ndpos=2-.\3&lt;br /&gt;
 charset 3rdpos=3-.\3&lt;br /&gt;
 partition bycodon = 3:1stpos,2ndpos,3rdpos&lt;br /&gt;
 set partition=bycodon&lt;br /&gt;
 prset ratepr=variable&lt;br /&gt;
: This is an alternative way of specifying that different sites have different rates. Instead of using a gamma distribution and learning about site-specific rates from the data, we are instead using our prior knowledge about the structure of the genetic code to specify that all 1st codon positions have the same rate, all 2nd codon positions have the same rate, and all 3rd codon positions have the same rate. Specifically, charset 1stpos=1-.\3 means that we define a character set named &amp;quot;1stpos&amp;quot; which includes site 1 in the alignment followed by every third site (&amp;quot;\3&amp;quot;, meaning it includes sites 1, 4, 7, 11, ...) until the end of the alignment (here denoted &amp;quot;.&amp;quot;).&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Specify model&#039;&#039;&#039;&lt;br /&gt;
 lset nst=6&lt;br /&gt;
: This specifies that we want to use a model of the General Time Reversible (GTR) type, where all 6 substitution types have separate rate parameters.&lt;br /&gt;
&lt;br /&gt;
: When the lset command was discussed previously, a few issues were glossed over. Importantly, and unlike PAUP, the lset command in MrBayes gives no information about whether nucleotide frequencies are equal or not, and whether they should be estimated from the data or not. In MrBayes this is instead controlled by defining the prior probability of the nucleotide frequencies (the command prset can be used to set priors). For instance, a model with equal nucleotide frequencies corresponds to having prior probability 1 (one) for the frequency vector (A=0.25, C=0.25, G=0.25, T=0.25), and zero prior probability for the infinitely many other possible vectors. As you will see below, the default prior is not this limited, and the program will therefore estimate the frequencies from the data.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Inspect model details&#039;&#039;&#039;&lt;br /&gt;
 showmodel&lt;br /&gt;
: This command gives you a summary of the current model settings. You will also get a summary of how the prior probabilities of all model parameters are set. You will for instance notice that the nucleotide frequencies (parameter labeled &amp;quot;Statefreq&amp;quot;) have a &amp;quot;Dirichlet&amp;quot; prior. Without going into details, the Dirichlet distribution is a probability distribution over frequency vectors (i.e., vectors of positive values that sum to 1). Depending on the exact parameters the distribution can be more or less flat (flat here means that all sum-1 vectors are equally probable). The Dirichlet distribution is a handy way of specifying the prior probability distribution of nucleotide (or amino acid) frequency vectors. The default statefreq prior in MrBayes is the flat or un-informative prior dirichlet(1,1,1,1).&lt;br /&gt;
&lt;br /&gt;
: We will not go into the priors for the remaining parameters in any detail, but you may notice that by default all topologies are taken to be equally likely (a flat prior on trees).&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Start MCMC sampling&#039;&#039;&#039;&lt;br /&gt;
 mcmc ngen=1000000 samplefreq=100 diagnfreq=10000 nchains=3&lt;br /&gt;
: The run will take a while to finish (you may want to ensure that the average standard deviation of split frequencies is less than 0.01 before ending the analysis).&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Compute summary of parameter values&#039;&#039;&#039;&lt;br /&gt;
 sump relburnin=yes burninfrac=0.25&lt;br /&gt;
: The sump command (with a &amp;quot;p&amp;quot; at the end) works much like the sumt command (with a &amp;quot;t&amp;quot; at the end), but for other parameters than the tree-topology. Again, we are using 25% of the total number of samples as burnin.&lt;br /&gt;
&lt;br /&gt;
: First, you get a scatter plot of the lnL as a function of generation number. Values from the two independent runs are labeled &amp;quot;1&amp;quot; and &amp;quot;2&amp;quot; respectively. If the burnin is suitable, then the points should be randomly scattered over a narrow lnL interval.&lt;br /&gt;
&lt;br /&gt;
: Secondly, the posterior probability distribution of each parameter is summarized by giving the mean, variance, median, and 95% credible interval.&lt;br /&gt;
&lt;br /&gt;
: The last columns contain values indicating if the run has converged. Specifically ESS means Effective Sample Size, and is a measure of how many independent samples you have from the posterior - the higher the better, but this should be at least 100. The column labeled PSRF+ is a measure (also known as &amp;quot;R-hat&amp;quot;) which should be close to 1 if the runs have converged. Specifically this measures whether different chains (and different parts of different chains) converge to sample the same set of values. &lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Question: &#039;&#039;&#039;What are the posterior mean values of the relative substitution rate parameters r(AC) and r(CG)?&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Question 7: &#039;&#039;&#039; Based on the reported posterior means, does r(CG) appear to be larger than r(AC)?&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Question 8&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Marginal vs. joint distributions&#039;&#039;&#039;&lt;br /&gt;
: Strictly speaking the comparison above was not entirely appropriate. We first found the overall distribution of the r(CG) parameter and then compared its mean to the mean of the overall distribution of the r(AC) parameter. By doing things this way, we are ignoring the possibility that the two parameters might be associated in some way. For instance, one parameter might always be larger than the other in any individual sample, even though the total distributions overlap. We should instead be looking at the distribution over both parameters simultaneously. A probability distribution over several parameters simultaneously is called a &amp;quot;joint distribution&amp;quot; over the parameters.&lt;br /&gt;
&lt;br /&gt;
: By looking at one parameter at a time, we are summing its probability over all values of the other parameters. This is called the marginal distribution.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Examine marginal distributions&#039;&#039;&#039;&lt;br /&gt;
: In RStudio, use the following commands to read and plot the marginal distributions of r(AC) and r(CG). Note that we are discarding the first 25% of the reads as burnin&lt;br /&gt;
 df_hcv = read_tsv(&amp;quot;hcvsmall.nexus.run1.p&amp;quot;, skip=1)&lt;br /&gt;
 burnin = df_hcv$Gen %&amp;gt;% &lt;br /&gt;
     max() %&amp;gt;% &lt;br /&gt;
     multiply_by(0.25) %&amp;gt;% &lt;br /&gt;
     floor()&lt;br /&gt;
 df_hcv2 = df_hcv %&amp;gt;% &lt;br /&gt;
     filter(Gen &amp;gt; burnin) %&amp;gt;%&lt;br /&gt;
     select(CG = `r(C&amp;lt;-&amp;gt;G){all}`,&lt;br /&gt;
            AC = `r(A&amp;lt;-&amp;gt;C){all}`&lt;br /&gt;
            )&lt;br /&gt;
 mcmc_intervals(df_hcv2, prob_outer = 1)&lt;br /&gt;
 mcmc_areas(df_hcv2, prob_outer = 1)&lt;br /&gt;
: The functions mcmc_intervals and mcmc_areas plot different views of the same posterior distributions. &lt;br /&gt;
&lt;br /&gt;
: You can also simply plot the data using ggplot:&lt;br /&gt;
 df_hcv2_long = pivot_longer(df_hcv2, cols = c(&amp;quot;CG&amp;quot;, &amp;quot;AC&amp;quot;))&lt;br /&gt;
 &lt;br /&gt;
 ggplot(df_hcv2_long) + &lt;br /&gt;
     geom_density(mapping=aes(x=value, fill=name), alpha=0.3) + &lt;br /&gt;
     labs(x=&amp;quot;Substitution rate&amp;quot;)&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Question&#039;&#039;&#039;: Based on the marginal distributions, r(AC) appears to be centered at a higher value than r(CG), but the two distributions overlap somewhat. Can you from these marginal distributions alone decide whether r(AC) is larger than r(CG) in most posterior samples? Why or why not?&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Question 9&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Examine joint distributions&#039;&#039;&#039;&lt;br /&gt;
: These plots and results explore the relationship between the A&amp;lt;-&amp;gt;C and C&amp;lt;-&amp;gt;G rates.&lt;br /&gt;
 ggplot(df_hcv2, aes(x=CG, y=AC)) + &lt;br /&gt;
     geom_point(col=&amp;quot;blue&amp;quot;) + &lt;br /&gt;
     geom_abline(intercept=0, slope=1, lty=2, col=&amp;quot;red&amp;quot;) +&lt;br /&gt;
     xlim(0,0.25) + &lt;br /&gt;
     ylim(0,0.25) + &lt;br /&gt;
     labs(x=&amp;quot;CG rate&amp;quot;, y =&amp;quot;AC rate&amp;quot;) &lt;br /&gt;
 &lt;br /&gt;
 ggplot(df_hcv2, aes(x=CG, y= AC)) + &lt;br /&gt;
     geom_hex(col=&amp;quot;blue&amp;quot;) + &lt;br /&gt;
     geom_abline(intercept=0, slope=1, lty=2, col=&amp;quot;red&amp;quot;) +&lt;br /&gt;
     xlim(0,0.25) + &lt;br /&gt;
     ylim(0, 0.25) + &lt;br /&gt;
     labs(x=&amp;quot;CG rate&amp;quot;, y =&amp;quot;AC rate&amp;quot;) &lt;br /&gt;
 &lt;br /&gt;
 df_hcv2 %&amp;gt;%&lt;br /&gt;
     nrow()&lt;br /&gt;
 df_hcv2 %&amp;gt;%&lt;br /&gt;
     filter(AC&amp;gt;CG) %&amp;gt;%&lt;br /&gt;
     nrow()&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Question: &#039;&#039;&#039;Based on the two different ways to plot the joint distribution and based on the unfiltered and filtered row counts, what is the posterior probability that r(AC) &amp;gt; r(CG)?&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Question 10&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
: Note how examining the joint distribution provides you with information that you could not get from simply comparing the marginal distributions. This very simple procedure can be used to answer many different questions.&lt;br /&gt;
&lt;br /&gt;
: Now, plot the relative substitution rates at the first, second, and third codon positions:&lt;br /&gt;
 df_hcv3 = df_hcv %&amp;gt;% &lt;br /&gt;
     filter(Gen &amp;gt; 75000) %&amp;gt;%&lt;br /&gt;
     select(Codon_1st = `m{1}`,&lt;br /&gt;
            Codon_2nd = `m{2}`,&lt;br /&gt;
            Codon_3rd = `m{3}` ) %&amp;gt;%&lt;br /&gt;
     pivot_longer(cols=c(&amp;quot;Codon_1st&amp;quot;, &amp;quot;Codon_2nd&amp;quot;, &amp;quot;Codon_3rd&amp;quot;))&lt;br /&gt;
 &lt;br /&gt;
 ggplot(df_hcv3) + &lt;br /&gt;
     geom_density(mapping=aes(x=value, fill=name), alpha=0.3) + &lt;br /&gt;
     labs(x=&amp;quot;Relative substitution rate&amp;quot;)&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Question: &#039;&#039;&#039;Since random mutations presumably hit all three codon positions with the same frequency, any differences are expected to be caused by subsequent selection. Which of the following statement are correct ? (More than one answer may be correct)&lt;br /&gt;
&lt;br /&gt;
:* Codon position 2 is the most variable of the codon positions.&lt;br /&gt;
:* Codon position 1 is the most variable of the codon positions.&lt;br /&gt;
:* Codon position 1 is the most conserved codon position.&lt;br /&gt;
:* Codon position 3 is the most conserved codon position.&lt;br /&gt;
:* Codon position 3 is the most variable of the codon positions.&lt;br /&gt;
:* Codon position 2 is the most conserved codon position.&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Question 11&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Question: &#039;&#039;&#039;How does this result fit with your knowledge of the genetic code? Why are these codon positions the most conserved or the most variable?&lt;/div&gt;</summary>
		<author><name>Gorm</name></author>
	</entry>
	<entry>
		<id>https://teaching.healthtech.dtu.dk/22115/index.php?title=Bayesian_Phylogeny&amp;diff=244</id>
		<title>Bayesian Phylogeny</title>
		<link rel="alternate" type="text/html" href="https://teaching.healthtech.dtu.dk/22115/index.php?title=Bayesian_Phylogeny&amp;diff=244"/>
		<updated>2026-03-19T09:28:41Z</updated>

		<summary type="html">&lt;p&gt;Gorm: /* Posterior probability of trees */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;This exercise is part of the course  [[22115_-_Computational_Molecular_Evolution|Computational Molecular Evolution (22115)]].&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Overview ==&lt;br /&gt;
&lt;br /&gt;
Today&#039;s exercise will focus on phylogenetic analysis using Bayesian methods.&lt;br /&gt;
&lt;br /&gt;
As was the case for likelihood methods, Bayesian analysis is founded on having a probabilistic model of how the observed data is produced. This means that, for a given set of parameter values, you can compute the probability or [https://www.statlect.com/glossary/probability-density-function probability density] of any possible observation. For a full dataset, you then obtain the likelihood by multiplying these values across all observations. You will recall from the lecture that in Bayesian statistics the goal is to obtain a full posterior probability distribution over all possible parameter values. The posterior distribution quantifies our degree of belief in any possible parameter value after seeing the data. It is obtained by updating the prior probability distribution using the likelihood of the observed data.&lt;br /&gt;
&lt;br /&gt;
The prior probability distribution expresses your beliefs about the parameters before seeing any data, while the likelihood expresses what the observed data are telling you about the parameters. Specifically, the likelihood of a parameter value is the probability of the observed data given that parameter value. We regard a parameter value as more plausible the more probable it makes the observed data. This is the same measure we have previously used to find the maximum likelihood estimate. If the prior probability distribution is flat (i.e., if all possible parameter values have the same prior probability), then the posterior distribution is proportional to the likelihood, and the parameter value with the maximum likelihood also has the maximum posterior probability. However, even in this case, using a Bayesian approach still lets you interpret the result as a probability distribution over parameter values. &lt;br /&gt;
&lt;br /&gt;
If the prior is not flat, then it may have a substantial impact on the posterior, although this effect will usually diminish as the amount of data increases. A prior should ideally be based on domain knowledge and results from previous experiments. For instance one can use the posterior from one analysis as the prior in a new, independent analysis. Often a prior is chosen to be weakly informative, meaning that it places reasonable bounds on the parameter values without constraining them too narrowly. For instance the transition/transversion rate ratio kappa is typically 1.5-10. Values such as 100, 1,000 or 1,000,000 would be extremely unlikely, so a weakly informative prior for this parameter could be chosen to place 95% of its probability mass in the 0.5-20 range, slightly wider than what we think of as plausible values. For instance one could use a lognormal distribution with suitable parameters.&lt;br /&gt;
&lt;br /&gt;
In Bayesian phylogeny the parameters are of the same kind as in maximum likelihood phylogeny. Typical parameters include tree topology, branch lengths, nucleotide frequencies, and substitution model parameters such as the transition/transversion rate ratio or the gamma shape parameter. The difference is that, whereas in maximum likelihood phylogeny we seek the best point estimates of the parameter values, in Bayesian phylogeny the goal is instead to infer a full probability distribution over the possible parameter values. The observed data are again usually taken to be the alignment, although strictly speaking it would be more reasonable to say that the sequences are what have been observed, and that the alignment should then be inferred jointly with the phylogeny.&lt;br /&gt;
&lt;br /&gt;
In this exercise we will explore how one can determine and use posterior probability distributions over trees, over clades, and over substitution parameters. We will also touch upon the difference between marginal and joint probability distributions.&lt;br /&gt;
&lt;br /&gt;
== Getting started ==&lt;br /&gt;
&lt;br /&gt;
: In the command below: Instead of /path/to/molevol enter the path to the directory where you have placed your course files (for instance cd /Users/bob/Documents/molevol, or cd /home/student/molevol).&lt;br /&gt;
 cd /path/to/molevol&lt;br /&gt;
 mkdir bayes&lt;br /&gt;
 cd bayes&lt;br /&gt;
 cp ../data/primatemitDNA.nexus ./primatemitDNA.nexus&lt;br /&gt;
 cp ../data/neanderthal.nexus ./neanderthal.nexus&lt;br /&gt;
 cp ../data/hcvsmall.nexus ./hcvsmall.nexus&lt;br /&gt;
&lt;br /&gt;
: You have analyzed (versions of) all these data files previously in this course. We will now use Bayesian phylogenetic analysis to complement what we learned in those analyses.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Load R libraries&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
: In RStudio: set the working directory to the bayes directory. Then issue these commands:&lt;br /&gt;
 library(magrittr)&lt;br /&gt;
 library(tidyverse)&lt;br /&gt;
 library(bayesplot)&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
== Posterior probability of trees ==&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Question 1&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
: In today&#039;s exercise we will be using the program &amp;quot;MrBayes&amp;quot; to perform Bayesian phylogenetic analysis. MrBayes is a program that, like PAUP*, can be controlled by giving commands at a command line prompt. In fact, there is a substantial overlap between the commands used to control MrBayes and the PAUP command language. This should be a help when you are trying to understand how to use the program.&lt;br /&gt;
&lt;br /&gt;
: Note that the command &amp;quot;help&amp;quot; will give you a list of all available commands. Issuing &amp;quot;help &#039;&#039;command&#039;&#039;&amp;quot; will give you a more detailed description of the specified command along with current option values. This is similar to how &amp;quot;help &#039;&#039;command&#039;&#039;&amp;quot; works in PAUP.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Start program&#039;&#039;&#039;&lt;br /&gt;
: In a terminal window, issue the command:&lt;br /&gt;
 mb&lt;br /&gt;
: This starts the program, giving you a prompt (&amp;quot;MrBayes&amp;gt; &amp;quot;) where you can enter commands.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Get a quick overview of available commands&#039;&#039;&#039;&lt;br /&gt;
 help&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Load your sequences&#039;&#039;&#039;&lt;br /&gt;
 execute primatemitDNA.nexus&lt;br /&gt;
: This file contains mitochondrial DNA sequences from 5 different primates. Note that MrBayes accepts input in nexus format, and that this is the same command that was used to load sequences in PAUP*. In general, you can use many of the PAUP commands in MrBayes also.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Inspect data set&#039;&#039;&#039;&lt;br /&gt;
 showmatrix&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Define outgroup&#039;&#039;&#039;&lt;br /&gt;
 outgroup Gibbon&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Specify your model of sequence evolution&#039;&#039;&#039;&lt;br /&gt;
 lset nst=2 rates=gamma&lt;br /&gt;
: This command is again very much like the corresponding one in PAUP. You are specifying that you want to use a model with two substitution types (nst=2), and this is automatically taken to mean that you want to distinguish between transitions and transversions. Furthermore, rates=gamma means that you want the model to use a gamma distribution to account for different rates at different sites in the sequence.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Start Markov chain Monte Carlo sampling&#039;&#039;&#039;&lt;br /&gt;
:Make sure to make the shell window as wide as possible and then issue the following commands to start the run:&lt;br /&gt;
 mcmc ngen=1000000 samplefreq=100 nchains=3 diagnfreq=5000&lt;br /&gt;
: What you are doing here is to use the method known as MCMCMC (&amp;quot;Metropolis-coupled Markov chain Monte Carlo&amp;quot;) to empirically determine the posterior probability distribution of trees, branch lengths and substitution parameters. Recall that in the Bayesian framework this is how we learn about parameter values: instead of finding the best point estimates, we typically want to quantify the probability of the entire range of possible values. An estimate of the time left is shown in the last column of output.&lt;br /&gt;
&lt;br /&gt;
: Let us examine the command in detail. First, ngen=1000000 samplefreq=100 lets the search run for 1,000,000 MCMC steps (&amp;quot;generations&amp;quot;) and saves parameter values once every 100 rounds (meaning that a total of 10,000 sets of parameter values will be saved to sample files). You sometimes need to run longer (or shorter) than 1,000,000, and would then typically tweak samplefreq so you get around 1,000 - 10,000 samples in all. The option nchains=3 means that the MCMCMC sampling uses 3 parallel chains (but see below): one &amp;quot;cold&amp;quot; from which sampling takes place, and two &amp;quot;heated&amp;quot; that move around in the parameter space more quickly to find additional peaks in the probability distribution.&lt;br /&gt;
&lt;br /&gt;
: The option diagnfreq=5000 has to do with testing whether the MrBayes run is successful. Briefly, MrBayes will start two entirely independent runs starting from different random trees. In the early phases of the run, the two runs will sample very different trees, but when they have reached convergence (when they produce a good sample from the posterior probability distribution), the two tree samples should be very similar. Every diagnfreq generations, the program will compute a measure of how similar the tree samples are, specifically the average standard deviation of split frequencies. A “split” is the same as a bipartition, i.e. a division of all leaves in the tree into two groups, obtained by cutting an internal branch. For each split, MrBayes compares how often that split occurs in the two independent runs; if the runs have converged, these frequencies should be very similar, giving a small standard deviation. The program then averages this quantity across splits. As a rule of thumb, you may want to run until this value is less than 0.05 (the smaller the better)&lt;br /&gt;
&lt;br /&gt;
: During the run you will see reports about the progress of the two independent runs, each consisting of three chains. Each line of output lists the generation number and the log likelihoods of the current tree/parameter combination for each of the two groups of three chains (a column of asterisks separate the results for the independent runs). The cold chains are the ones enclosed in brackets [...], while the heated chains are enclosed in parentheses (...). Occasionally the chains will swap so one of the heated chains now becomes cold (and sampling then takes place from this chain).&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Continue run until parallel runs converge on same solution&#039;&#039;&#039;&lt;br /&gt;
:At the end of the run, Mrbayes will print the average standard deviation of split frequencies (which is a measure of how similar the tree samples of the two independent runs are). We recommend that you continue with the analysis until the value gets below 0.01 (if the value is larger than 0.01 then you should answer &amp;quot;yes&amp;quot; when the program asks &amp;quot;Continue the analysis? (yes/no)&amp;quot;.)&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Question: &#039;&#039;&#039;MrBayes starts two independent runs from different random trees. Why is it useful to run two independent analyses instead of just one? How does the average standard deviation of split frequencies help you decide whether the two runs have converged to the same posterior distribution? At approximately how many generations does this happen in your run?&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Question 2&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Have a look at the resulting sample files&#039;&#039;&#039;&lt;br /&gt;
: Open a new Terminal window (don&#039;t quit mrbayes in the other terminal yet!) and cd to the bayes directory. Open one of the parameter sampling files in a text editor:&lt;br /&gt;
 nedit primatemitDNA.nexus.run1.p &amp;amp;&lt;br /&gt;
: This file contains one line for each sampled point (you may want to turn off line-wrapping in nedit under the preferences menu). Each row corresponds to a certain sample time (or generation). Each column contains the sampled values of one specific parameter. The first line contains headings telling what the different columns are: &lt;br /&gt;
:* Gen: generation; number of MCMC steps taken so far&lt;br /&gt;
:* lnL: log likelihood of the current parameter estimates&lt;br /&gt;
:* LnPr: log of the prior probability&lt;br /&gt;
:* TL: tree length (sum of all branch lengths)&lt;br /&gt;
:* kappa: transition/transversion rate ratio&lt;br /&gt;
:* pi(A), pi(C), pi(G), pi(T): frequency of A, C, G, T&lt;br /&gt;
:* alpha: shape parameter for the gamma distribution. &lt;br /&gt;
&lt;br /&gt;
: (Column headings may be shifted relative to their corresponding columns). Note how the values of most parameters change a lot during the initial &amp;quot;burnin&amp;quot; period, before they settle near their most probable values. &lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Question: &#039;&#039;&#039;You will notice that lnL is always negative, while LnPr can sometimes be positive. At first sight this may seem impossible, since probabilities cannot be larger than 1. How can this happen?&lt;br /&gt;
&lt;br /&gt;
As a hint, note that (1) priors for continuous parameters are probability densities, and (2) the default prior for each branch length in MrBayes is an exponential distribution with rate 10. Use the following R code to plot this prior on both an ordinary y-axis and a log-scaled y-axis, and then explain why positive values of LnPr are possible.&lt;br /&gt;
&lt;br /&gt;
 df_expdist = tibble(&lt;br /&gt;
   x = seq(0, 1, by = 0.001),&lt;br /&gt;
   density = dexp(x, rate = 10),&lt;br /&gt;
   logdensity = log(dexp(x, rate = 10))&lt;br /&gt;
 )&lt;br /&gt;
 &lt;br /&gt;
 ggplot(df_expdist, aes(x = x, y = density)) +&lt;br /&gt;
   geom_line(color=&amp;quot;blue&amp;quot;) +&lt;br /&gt;
   geom_hline(yintercept = 1, lty=2) +&lt;br /&gt;
   labs(&lt;br /&gt;
     x = &amp;quot;Branch length&amp;quot;,&lt;br /&gt;
     y = &amp;quot;Probability density&amp;quot;,&lt;br /&gt;
     title = &amp;quot;Exponential prior on branch lengths: Exp(rate = 10)&amp;quot;&lt;br /&gt;
   )&lt;br /&gt;
 &lt;br /&gt;
 ggplot(df_expdist, aes(x = x, y = logdensity)) +&lt;br /&gt;
   geom_line(color=&amp;quot;blue&amp;quot;) +&lt;br /&gt;
   geom_hline(yintercept = 0, lty=2) +&lt;br /&gt;
   labs(&lt;br /&gt;
     x = &amp;quot;Branch length&amp;quot;,&lt;br /&gt;
     y = &amp;quot;log of Probability density&amp;quot;,&lt;br /&gt;
     title = &amp;quot;Exponential prior on branch lengths: Exp(rate = 10)&amp;quot;&lt;br /&gt;
   )&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Question 3&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Examine MCMC trajectory for gamma shape parameter, alpha&#039;&#039;&#039;&lt;br /&gt;
: Recall that the idea in MCMCMC sampling is to move around in parameter space in such a way that points are visited according to their posterior probability (i.e., regions with high posterior probability are visited frequently). Now, in RStudio, plot the sampled values for the gamma shape parameter, alpha, for one of the run files:&lt;br /&gt;
 df_primates = read_tsv(&amp;quot;primatemitDNA.nexus.run1.p&amp;quot;, skip=1)&lt;br /&gt;
 mcmc_trace(df_primates, pars=&amp;quot;alpha&amp;quot;)&lt;br /&gt;
: mcmc_trace is one of several plotting commands available in the bayesplot package. This command plots the sampled values of the parameter alpha from the first of the two parallel runs against MCMC generation number. Thus, the x-axis shows the progress of the run through time, with the leftmost values being the earliest samples and the rightmost values the later ones. Note how the Markov chain starts at the arbitrary value 1.0, rapidly moves to values that fit the observed data better, and then moves around in parameter space, sampling different plausible values of alpha. You can experiment with plotting other columns as well.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Question: &#039;&#039;&#039;Describe briefly what happens to the sampled values of alpha during the run. Why is it reasonable to discard the earliest samples as burn-in?&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Question 4&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Investigate posterior probability distribution over trees&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
: Now, close the nedit window and have a look at the file containing sampled trees:&lt;br /&gt;
 nedit primatemitDNA.nexus.run1.t &amp;amp;&lt;br /&gt;
: Tree topology is also a parameter in our model, and exactly like for the other parameters we also get samples from tree-space. One tree is printed per line in the parenthetical Newick format you have seen before. There are 5 taxa in the present data set, so the number of possible unrooted binary tree topologies is only 15. Since we have taken more than 15 sample points, there must be several lines containing the same tree topology. Close the nedit window when you are done.&lt;br /&gt;
&lt;br /&gt;
: MrBayes provides the sumt command to summarize the sampled trees. Before using it, we need to decide on the burn-in: The burn-in is the initial set of samples that are typically discarded, because we want to ensure that the MCMC has moved away from the random starting values, and has found the peaks of the probability landscape. Since the convergence diagnostic used a relative burn-in of 25%, we will also discard the first 25% of tree samples when summarizing the posterior.&lt;br /&gt;
&lt;br /&gt;
: Return to the shell window where you have MrBayes running. In the command below relburnin=yes and burninfrac=0.25 tells MrBayes to discard 25% of the samples as burnin (you could also have explicitly given the number of samples to discard - help sumt will give you details about the command and the current option settings).&lt;br /&gt;
 sumt contype=halfcompat conformat=simple relburnin=yes burninfrac=0.25 showtreeprobs=yes&lt;br /&gt;
: (Scroll back so you can see the top of the output when the command is done). This command gives you a summary of the trees that are in the file you examined manually above. The option contype=halfcompat requests that a majority rule consensus tree is calculated from the set of trees that are left after discarding the burnin. This consensus is the first tree plotted to the screen. Below the consensus cladogram, a consensus phylogram is plotted. The branch lengths in this have been averaged over the trees in which that branch was present (a particular branch corresponds to a bi-partition of the data, and will typically not be present in every sampled tree). The cladogram also has &amp;quot;clade credibility&amp;quot; values. We will return to the meaning of these later in today&#039;s exercise.&lt;br /&gt;
&lt;br /&gt;
: What most interests us right now is the list of trees that is printed after the phylogram. These trees are labeled &amp;quot;Tree 1&amp;quot;, &amp;quot;Tree 2&amp;quot;, etc, and are sorted according to their posterior probability which is indicated by a lower-case p after the tree number. (The upper-case P gives the cumulated probability of trees shown so far, and is useful for constructing a credible set). This list highlights how Bayesian phylogenetic analysis is different from maximum likelihood: Instead of finding the best tree(s), we here quantify our degree of belief in all possible trees.&lt;br /&gt;
&lt;br /&gt;
: The list of trees and probabilities was printed because of the option showtreeprobs=yes. Note that you probably do not want to issue that command if you have much more than 5 taxa! In that case you could instead inspect the file named primatemitDNA.nexus.trprobs which is now present in the same directory as your other files (this file is automatically produced by the sumt command).&lt;br /&gt;
&lt;br /&gt;
: &#039;&#039;&#039;NOTE&#039;&#039;&#039;: Annoyingly, there is a bug in the version of mrbayes we are using here, which means leaf names are not printed on the list of trees with probabilities. However, the most probable tree here in fact is identical to the consensus tree printed above it.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Question: &#039;&#039;&#039;What is the posterior probability of the most probable tree? Does the analysis strongly support a single tree, or is the posterior probability distributed across several different trees?&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
== Analysis of Neanderthal data (posterior probability of clades) ==&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Question 5&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
The predominant theory in the 1950s and 60s (although it varied greatly from scholar to scholar) was that our earliest hominid ancestors (specifically Homo erectus) evolved in Africa and then radiated out into the world. This so-called [https://www.thoughtco.com/multiregional-hypothesis-167235 Multiregional Hypothesis] says that after H. erectus arrived in the various regions in the world hundreds of thousands of years ago, they slowly evolved into modern humans. The hypothesis thus posits that there were nearly independent origins of modern humans within the various regions of the world.&lt;br /&gt;
&lt;br /&gt;
In the 1970s, paleontologist W.W. Howells proposed an alternate theory: the first Recent African Origin model. Howells argued that H. sapiens evolved solely in Africa. By the 1980s, growing data from human genetics led Stringer and Andrews to develop a model that said that the very earliest anatomically modern humans arose in Africa about 100,000 years ago and archaic populations found throughout Eurasia (including Neanderthals) might be descendants of H. erectus and later archaic types but they were not related to modern humans.&lt;br /&gt;
&lt;br /&gt;
We will use the present data set to consider this issue.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Load Neanderthal data set&#039;&#039;&#039;&lt;br /&gt;
: In the Terminal where you have MrBayes running:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
execute neanderthal.nexus&lt;br /&gt;
delete 5-40&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
: As we did for the maximum likelihood analysis, we will discard some of the human sequences in order to speed up the analysis. The command delete 5-40 removes sequence number 5 to sequence number 40 from the active data set.&lt;br /&gt;
&lt;br /&gt;
: As we did for the maximum likelihood analysis, we will discard some of the human sequences in order to speed up the analysis. The command delete 5-40 removes sequence number 5 to sequence number 40 from the active data set.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Investigate data&#039;&#039;&#039;&lt;br /&gt;
 showmatrix&lt;br /&gt;
: This data set consists of an alignment of mitochondrial DNA from human (17 sequences), chimpanzee (1 sequence), and Neanderthal (1 sequence). The Neanderthal DNA was extracted from archaeological material, specifically bones found at Vindija in Croatia.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Start analysis&#039;&#039;&#039;&lt;br /&gt;
 outgroup Pan_troglodytes&lt;br /&gt;
 lset nst=mixed rates=gamma&lt;br /&gt;
 mcmc ngen=500000 nchains=3 diagnfreq=10000&lt;br /&gt;
&lt;br /&gt;
: Here we use the command `nst=mixed` which allows MrBayes to automatically explore all possible substitution models. Essentially, MrBayes now considers the substitution model as one more parameter, and uses MCMC to sample from the possible versions (with nst ranging from 1 to 6). This will often be the best choice when using MrBayes. (Below, I use nst=6 for pedagogical purposes, because it makes it simpler to analyse the output files).&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Find posterior probability of clades&#039;&#039;&#039;&lt;br /&gt;
 sumt contype=halfcompat showtreeprobs=no relburnin=yes burninfrac=0.25&lt;br /&gt;
: Examine the consensus tree that is plotted to screen: On the branches that are resolved, you will notice that numbers have been plotted. These are clade-credibility values, and are in fact the posterior probability that the clade is real (based on the present data set).&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Question: &#039;&#039;&#039;What is the posterior probability that all sampled Homo sapiens sequences form a monophyletic group excluding the Neanderthal sequence? Does this support placing the Neanderthal outside modern human mitochondrial diversity?&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
== Probability distributions over other parameters ==&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Question 6&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
: As the last thing, we will now turn away from the tree topology, and instead examine the other parameters that also form part of the probabilistic model. We will do this using a reduced version of the Hepatitis C virus data set that we have examined previously. Stay in the shell window where you just performed the analysis of Neanderthal sequences.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Load data set&#039;&#039;&#039;&lt;br /&gt;
 execute hcvsmall.nexus&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Define site partition&#039;&#039;&#039;&lt;br /&gt;
 charset 1stpos=1-.\3&lt;br /&gt;
 charset 2ndpos=2-.\3&lt;br /&gt;
 charset 3rdpos=3-.\3&lt;br /&gt;
 partition bycodon = 3:1stpos,2ndpos,3rdpos&lt;br /&gt;
 set partition=bycodon&lt;br /&gt;
 prset ratepr=variable&lt;br /&gt;
: This is an alternative way of specifying that different sites have different rates. Instead of using a gamma distribution and learning about site-specific rates from the data, we are instead using our prior knowledge about the structure of the genetic code to specify that all 1st codon positions have the same rate, all 2nd codon positions have the same rate, and all 3rd codon positions have the same rate. Specifically, charset 1stpos=1-.\3 means that we define a character set named &amp;quot;1stpos&amp;quot; which includes site 1 in the alignment followed by every third site (&amp;quot;\3&amp;quot;, meaning it includes sites 1, 4, 7, 11, ...) until the end of the alignment (here denoted &amp;quot;.&amp;quot;).&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Specify model&#039;&#039;&#039;&lt;br /&gt;
 lset nst=6&lt;br /&gt;
: This specifies that we want to use a model of the General Time Reversible (GTR) type, where all 6 substitution types have separate rate parameters.&lt;br /&gt;
&lt;br /&gt;
: When the lset command was discussed previously, a few issues were glossed over. Importantly, and unlike PAUP, the lset command in MrBayes gives no information about whether nucleotide frequencies are equal or not, and whether they should be estimated from the data or not. In MrBayes this is instead controlled by defining the prior probability of the nucleotide frequencies (the command prset can be used to set priors). For instance, a model with equal nucleotide frequencies corresponds to having prior probability 1 (one) for the frequency vector (A=0.25, C=0.25, G=0.25, T=0.25), and zero prior probability for the infinitely many other possible vectors. As you will see below, the default prior is not this limited, and the program will therefore estimate the frequencies from the data.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Inspect model details&#039;&#039;&#039;&lt;br /&gt;
 showmodel&lt;br /&gt;
: This command gives you a summary of the current model settings. You will also get a summary of how the prior probabilities of all model parameters are set. You will for instance notice that the nucleotide frequencies (parameter labeled &amp;quot;Statefreq&amp;quot;) have a &amp;quot;Dirichlet&amp;quot; prior. Without going into details, the Dirichlet distribution is a probability distribution over frequency vectors (i.e., vectors of positive values that sum to 1). Depending on the exact parameters the distribution can be more or less flat (flat here means that all sum-1 vectors are equally probable). The Dirichlet distribution is a handy way of specifying the prior probability distribution of nucleotide (or amino acid) frequency vectors. The default statefreq prior in MrBayes is the flat or un-informative prior dirichlet(1,1,1,1).&lt;br /&gt;
&lt;br /&gt;
: We will not go into the priors for the remaining parameters in any detail, but you may notice that by default all topologies are taken to be equally likely (a flat prior on trees).&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Start MCMC sampling&#039;&#039;&#039;&lt;br /&gt;
 mcmc ngen=1000000 samplefreq=100 diagnfreq=10000 nchains=3&lt;br /&gt;
: The run will take a while to finish (you may want to ensure that the average standard deviation of split frequencies is less than 0.01 before ending the analysis).&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Compute summary of parameter values&#039;&#039;&#039;&lt;br /&gt;
 sump relburnin=yes burninfrac=0.25&lt;br /&gt;
: The sump command (with a &amp;quot;p&amp;quot; at the end) works much like the sumt command (with a &amp;quot;t&amp;quot; at the end), but for other parameters than the tree-topology. Again, we are using 25% of the total number of samples as burnin.&lt;br /&gt;
&lt;br /&gt;
: First, you get a scatter plot of the lnL as a function of generation number. Values from the two independent runs are labeled &amp;quot;1&amp;quot; and &amp;quot;2&amp;quot; respectively. If the burnin is suitable, then the points should be randomly scattered over a narrow lnL interval.&lt;br /&gt;
&lt;br /&gt;
: Secondly, the posterior probability distribution of each parameter is summarized by giving the mean, variance, median, and 95% credible interval.&lt;br /&gt;
&lt;br /&gt;
: The last columns contain values indicating if the run has converged. Specifically ESS means Effective Sample Size, and is a measure of how many independent samples you have from the posterior - the higher the better, but this should be at least 100. The column labeled PSRF+ is a measure (also known as &amp;quot;R-hat&amp;quot;) which should be close to 1 if the runs have converged. Specifically this measures whether different chains (and different parts of different chains) converge to sample the same set of values. &lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Question: &#039;&#039;&#039;What are the posterior mean values of the relative substitution rate parameters r(AC) and r(CG)?&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Question 7: &#039;&#039;&#039; Based on the reported posterior means, does r(CG) appear to be larger than r(AC)?&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Question 8&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Marginal vs. joint distributions&#039;&#039;&#039;&lt;br /&gt;
: Strictly speaking the comparison above was not entirely appropriate. We first found the overall distribution of the r(CG) parameter and then compared its mean to the mean of the overall distribution of the r(AC) parameter. By doing things this way, we are ignoring the possibility that the two parameters might be associated in some way. For instance, one parameter might always be larger than the other in any individual sample, even though the total distributions overlap. We should instead be looking at the distribution over both parameters simultaneously. A probability distribution over several parameters simultaneously is called a &amp;quot;joint distribution&amp;quot; over the parameters.&lt;br /&gt;
&lt;br /&gt;
: By looking at one parameter at a time, we are summing its probability over all values of the other parameters. This is called the marginal distribution.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Examine marginal distributions&#039;&#039;&#039;&lt;br /&gt;
: In RStudio, use the following commands to read and plot the marginal distributions of r(AC) and r(CG). Note that we are discarding the first 25% of the reads as burnin&lt;br /&gt;
 df_hcv = read_tsv(&amp;quot;hcvsmall.nexus.run1.p&amp;quot;, skip=1)&lt;br /&gt;
 burnin = df_hcv$Gen %&amp;gt;% &lt;br /&gt;
     max() %&amp;gt;% &lt;br /&gt;
     multiply_by(0.25) %&amp;gt;% &lt;br /&gt;
     floor()&lt;br /&gt;
 df_hcv2 = df_hcv %&amp;gt;% &lt;br /&gt;
     filter(Gen &amp;gt; burnin) %&amp;gt;%&lt;br /&gt;
     select(CG = `r(C&amp;lt;-&amp;gt;G){all}`,&lt;br /&gt;
            AC = `r(A&amp;lt;-&amp;gt;C){all}`&lt;br /&gt;
            )&lt;br /&gt;
 mcmc_intervals(df_hcv2, prob_outer = 1)&lt;br /&gt;
 mcmc_areas(df_hcv2, prob_outer = 1)&lt;br /&gt;
: The functions mcmc_intervals and mcmc_areas plot different views of the same posterior distributions. &lt;br /&gt;
&lt;br /&gt;
: You can also simply plot the data using ggplot:&lt;br /&gt;
 df_hcv2_long = pivot_longer(df_hcv2, cols = c(&amp;quot;CG&amp;quot;, &amp;quot;AC&amp;quot;))&lt;br /&gt;
 &lt;br /&gt;
 ggplot(df_hcv2_long) + &lt;br /&gt;
     geom_density(mapping=aes(x=value, fill=name), alpha=0.3) + &lt;br /&gt;
     labs(x=&amp;quot;Substitution rate&amp;quot;)&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Question&#039;&#039;&#039;: Based on the marginal distributions, r(AC) appears to be centered at a higher value than r(CG), but the two distributions overlap somewhat. Can you from these marginal distributions alone decide whether r(AC) is larger than r(CG) in most posterior samples? Why or why not?&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Question 9&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Examine joint distributions&#039;&#039;&#039;&lt;br /&gt;
: These plots and results explore the relationship between the A&amp;lt;-&amp;gt;C and C&amp;lt;-&amp;gt;G rates.&lt;br /&gt;
 ggplot(df_hcv2, aes(x=CG, y=AC)) + &lt;br /&gt;
     geom_point(col=&amp;quot;blue&amp;quot;) + &lt;br /&gt;
     geom_abline(intercept=0, slope=1, lty=2, col=&amp;quot;red&amp;quot;) +&lt;br /&gt;
     xlim(0,0.25) + &lt;br /&gt;
     ylim(0,0.25) + &lt;br /&gt;
     labs(x=&amp;quot;CG rate&amp;quot;, y =&amp;quot;AC rate&amp;quot;) &lt;br /&gt;
 &lt;br /&gt;
 ggplot(df_hcv2, aes(x=CG, y= AC)) + &lt;br /&gt;
     geom_hex(col=&amp;quot;blue&amp;quot;) + &lt;br /&gt;
     geom_abline(intercept=0, slope=1, lty=2, col=&amp;quot;red&amp;quot;) +&lt;br /&gt;
     xlim(0,0.25) + &lt;br /&gt;
     ylim(0, 0.25) + &lt;br /&gt;
     labs(x=&amp;quot;CG rate&amp;quot;, y =&amp;quot;AC rate&amp;quot;) &lt;br /&gt;
 &lt;br /&gt;
 df_hcv2 %&amp;gt;%&lt;br /&gt;
     nrow()&lt;br /&gt;
 df_hcv2 %&amp;gt;%&lt;br /&gt;
     filter(AC&amp;gt;CG) %&amp;gt;%&lt;br /&gt;
     nrow()&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Question: &#039;&#039;&#039;Based on the two different ways to plot the joint distribution and based on the unfiltered and filtered row counts, what is the posterior probability that r(AC) &amp;gt; r(CG)?&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Question 10&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
: Note how examining the joint distribution provides you with information that you could not get from simply comparing the marginal distributions. This very simple procedure can be used to answer many different questions.&lt;br /&gt;
&lt;br /&gt;
: Now, plot the relative substitution rates at the first, second, and third codon positions:&lt;br /&gt;
 df_hcv3 = df_hcv %&amp;gt;% &lt;br /&gt;
     filter(Gen &amp;gt; 75000) %&amp;gt;%&lt;br /&gt;
     select(Codon_1st = `m{1}`,&lt;br /&gt;
            Codon_2nd = `m{2}`,&lt;br /&gt;
            Codon_3rd = `m{3}` ) %&amp;gt;%&lt;br /&gt;
     pivot_longer(cols=c(&amp;quot;Codon_1st&amp;quot;, &amp;quot;Codon_2nd&amp;quot;, &amp;quot;Codon_3rd&amp;quot;))&lt;br /&gt;
 &lt;br /&gt;
 ggplot(df_hcv3) + &lt;br /&gt;
     geom_density(mapping=aes(x=value, fill=name), alpha=0.3) + &lt;br /&gt;
     labs(x=&amp;quot;Relative substitution rate&amp;quot;)&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Question: &#039;&#039;&#039;Since random mutations presumably hit all three codon positions with the same frequency, any differences are expected to be caused by subsequent selection. Which of the following statement are correct ? (More than one answer may be correct)&lt;br /&gt;
&lt;br /&gt;
:* Codon position 2 is the most variable of the codon positions.&lt;br /&gt;
:* Codon position 1 is the most variable of the codon positions.&lt;br /&gt;
:* Codon position 1 is the most conserved codon position.&lt;br /&gt;
:* Codon position 3 is the most conserved codon position.&lt;br /&gt;
:* Codon position 3 is the most variable of the codon positions.&lt;br /&gt;
:* Codon position 2 is the most conserved codon position.&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Question 11&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Question: &#039;&#039;&#039;How does this result fit with your knowledge of the genetic code? Why are these codon positions the most conserved or the most variable?&lt;/div&gt;</summary>
		<author><name>Gorm</name></author>
	</entry>
</feed>