Isometric gene tree reconciliation revisited

Background Isometric gene tree reconciliation is a gene tree/species tree reconciliation problem where both the gene tree and the species tree include branch lengths, and these branch lengths must be respected by the reconciliation. The problem was introduced by Ma et al. in 2008 in the context of reconstructing evolutionary histories of genomes in the infinite sites model. Results In this paper, we show that the original algorithm by Ma et al. is incorrect, and we propose a modified algorithm that addresses the problems that we discovered. We have also improved the running time from \documentclass[12pt]{minimal} \usepackage{amsmath} \usepackage{wasysym} \usepackage{amsfonts} \usepackage{amssymb} \usepackage{amsbsy} \usepackage{mathrsfs} \usepackage{upgreek} \setlength{\oddsidemargin}{-69pt} \begin{document}$$O(N^2)$$\end{document}O(N2) to \documentclass[12pt]{minimal} \usepackage{amsmath} \usepackage{wasysym} \usepackage{amsfonts} \usepackage{amssymb} \usepackage{amsbsy} \usepackage{mathrsfs} \usepackage{upgreek} \setlength{\oddsidemargin}{-69pt} \begin{document}$$O(N\log N)$$\end{document}O(NlogN), where N is the total number of nodes in the two input trees. Finally, we examine two new variants of the problem: reconciliation of two unrooted trees and scaling of branch lengths of the gene tree during reconciliation of two rooted trees. Conclusions We provide several new algorithms for isometric reconciliation of trees. Some questions in this area remain open; most importantly extensions of the problem allowing for imprecise estimates of branch lengths.


Background
In this paper, we revisit the problem of isometric gene tree reconciliation introduced by Ma et al. [1,2]. We point out several mistakes in the original publications and provide a corrected and simplified version of the algorithm. We also improve its running time by employing appropriate data structures and solve two new variants of the problem.
We will consider evolution of a single gene family. The evolutionary history starts with a single ancestral gene which evolves by a series of duplications, speciations, and losses, resulting in several present-day species, each carrying some number of copies of the studied gene. A particular evolutionary history of a gene family defines gene tree G and species tree S (see Fig. 1). The leaves of the species tree S are the present-day species, and the internal nodes correspond to the speciation events in the history. The leaves of the gene tree G are the present-day copies of the gene and the internal nodes correspond to the duplications and the speciations.
Species trees and gene trees can be reconstructed from sequence data by well-established methods [3]. However, one pair of a gene tree and a species tree may correspond to many different histories, because it is not clear, which nodes of the gene tree correspond to speciations in the species tree. The goal of gene tree/species tree reconciliation is to map nodes of the gene tree to the species tree, and thus to reconstruct the evolutionary history of the gene family.
Classical approaches to reconciliation consider only topologies of the gene tree and the species tree. As the reconciliation is not unique, the goal is to find the most parsimonious reconciliation minimizing the number of events. This problem is studied since 1979 [4], and multiple algorithms were developed [5][6][7][8].
In this paper, we consider a different variant of the problem called isometric gene tree reconciliation. In this problem, branch lengths in both the gene tree and the species tree are known exactly, and the reconciliation should obey them. This problem was introduced by Ma et al. [1], who used this form of gene tree reconciliation as one of the steps in their polynomial-time algorithm to reconstruct evolutionary history of several genomes in a rich model which includes duplications, two and three breakpoint rearrangements, deletions, and insertions under the assumptions of the infinite sites model. This result is rather remarkable, as reconstruction of rearrangement histories is typically NP-hard even in simple models [9].
If both the gene tree and the species tree are rooted, their isometric reconciliation can be found by a straightforward algorithm. Mapping of leaves is given on input, because we know the species of origin for each gene. To map an internal node v of the gene tree, we choose one leaf u in its subtree and map v to the unique place in the species tree determined by the distance between u and v. For example, node y in Fig. 2 maps to a point in the middle of edge (r, x), because this is the unique point on the path from the root to leaf c, which is situated in distance 3 from c. This algorithm is described in more detail as Algorithm 1.
However, Ma et al. [1,2] consider a more difficult problem, in which the species tree is rooted, but the gene tree is unrooted. This is needed, because in practice most of the phylogenetic reconstruction methods produce unrooted trees. While the species tree can be rooted by including an outgroup, finding an appropriate outgroup for a multi-gene family, which may harbor ancient duplications, is more problematic. Ma et al. give a polynomialtime algorithm for the isometric reconciliation problem, and after some unspecified modifications, apply it to real data with inexact branch lengths. In this paper, we point out several mistakes in their version of the algorithm and provide a corrected version.
We also study two extensions of the problem which were not considered before. First, we extend our algorithm to the case when the species tree is unrooted as well. We also provide an algorithm for the case when both trees are rooted, but the branch lengths of the gene tree are scaled by some unknown factor which needs to be discovered by the algorithm. This is a first step towards a more realistic scenario where the branch length are not known exactly. This particular variant of the problem is motivated by the observation that individual genes often differ in their substitution rates; rate variation is a common component in models of evolution [3].
Reconciliation with some branch length information was also previously studied in several more complex models, such as probabilistic approaches considering branch lengths in S [10][11][12] and models allowing horizontal gene transfer [13,14].
The rest of the paper is organized as follows. We start by formally defining the problem and describing the simple algorithm for the case when both trees are rooted. Then we briefly describe the algorithm from the work of Ma et al. and point out its problems. Next, we provide a corrected algorithm for reconciling an unrooted gene tree with a rooted species tree. We also describe algorithms for two extensions of the original problem. At the history diagram gene tree G species tree S Fig. 1 An example of the evolutionary history of a gene family and its corresponding gene tree G and species tree S. In the history diagram, species are shown as gray bands, genes within species as black lines, gene losses as empty circles. Gene identifiers start with species label; thus a 1 , a 2 and a 3 are three copies of the studied gene in species a. Duplications are highlighted by gray dotted horizontal lines, speciations by dashed lines Mapping of node y in the isometric reconciliation of rooted gene tree G and species tree S. A more detailed view of this reconciliation can be found in Fig. 3 end of the article, we provide further details concerning our evolutionary models; these details are not necessary for the algorithms, but help to interpret their results.

Preliminaries
In this section, we introduce the notation used in this paper and formally define the problem of isometric reconciliation. Note that Ma et al. define the problem only briefly, and as we will discuss in the next section, their definition is not sufficiently stringent. We will also describe a simple algorithm for isometric reconciliation of two rooted trees.

Basic tree notation
We will consider rooted and unrooted phylogenetic trees with non-negative branch lengths. Although in this section we briefly consider branches of length zero, all our algorithms assume that the input trees have strictly positive branch lengths. On the other hand, the algorithms do not require that the trees are binary; they can have nodes of higher degree. We might obtain such nodes by contracting branches of length zero in a binary phylogenetic tree. Some of the trees considered in this work also have nodes with only one child; these nodes correspond to subdivisions of edges in a binary tree.
We will now briefly introduce useful tree-related notation. Given two nodes u and v belonging to the same phylogenetic tree, d(u, v) denotes their distance, i.e. the sum of branch lengths on the unique simple path between u and v. An edge (u, v) of a rooted or unrooted tree can be considered as an interval of length d (u, v). The point in this interval in distance d from node u will be denoted as pt (u, v, d); this is defined for 0 ≤ d ≤ d(u, v). If a tree is rooted, we will also use notation pt (u, d) as a shortcut for pt (u, v, d), where v is the parent of u. We will also use pt (u, d) when u is the root; it will represent the point on an implicit edge of infinite length leading to the root from above.
We can subdivide edge (u, v) at pt (u, v, d) by replacing it with two edges of lengths d and d(u, v)−d connected to a new node x. The inverse operation (i.e. replacing a path of two edges leading through node x of degree two by one edge of the same length) will be called bypassing node x. A rooted version of an unrooted tree T is created either by choosing one internal node of T as the root or by subdividing one of the edges of T and selecting the new node as the root.
An ancestor of node v in a rooted tree is any node on the path from v to the root, including v. We use the term proper ancestor for ancestors excluding v. By anc (u, d) we denote the point in the tree which is at a distance exactly d from u on the path towards the root. It can be one of the ancestors of u, or if no ancestor is in distance exactly d, it is pt (u ′ , d ′ ) such that u ′ is an ancestor of u, d ′ = d − d(u, u ′ ) > 0 and for any ancestor u ′′ of u ′ we have d − d(u, u ′′ ) < 0. Note that we will use this notation only in trees with strictly positive branch lengths, as otherwise there could be multiple ancestors with distance d. By lca (u, v), we denote the lowest common ancestor of nodes u and v.

Isometric mapping and history
Using the introduced notation, we will now define the central object of our study, the isometric mapping between trees.
Definition 1 An isometric mapping from a rooted phylogenetic tree T 1 to a rooted phylogenetic tree T 2 is a mapping of nodes of T 1 to nodes of T 2 such that if node u ∈ T 1 is the parent of v, then �(u) is a proper ancestor of �(v) and d(�(u), �(v)) = d(u, v); for trees with strictly positive branch lengths this condition is equivalent to Note that by induction, the relationship �(u) = anc (�(v), d(u, v)) holds for any nodes u and v in G such that u is an ancestor of v. Given an isometric mapping and node v in T 2 , by � −1 (v) we denote the (possibly empty) set of nodes of T 1 that map to v.
Recall that we are interested in studying the evolutionary history of a single gene family consisting of speciations, duplications, and losses. An isometric mapping from the gene tree to the species tree helps us to interpret the nodes of the two trees as these events. A formal definition of a history in our model follows; an example of a history is triple (G, S, �) in Fig. 3.

Definition 2
A history is a triple (G, S, �), where the gene tree G and the species tree S are rooted phylogenetic trees and is an isometric mapping from G to S.

Inferable histories
Our basic definition of a history is quite general. For example, it allows trees with zero branch lengths. The gene tree may also contain leaves that map to internal nodes of the species tree; such leaves correspond to gene losses. However, input trees for the isometric reconciliation problem are in practice constructed from extant genes, and thus they do not contain any deleted genes. Therefore, our algorithms work with a more restricted set of inferable histories. To define them, we first need the following terminology. Consider an arbitrary history (G, S, �). We will call a leaf v of G extant if �(v) is also a leaf in S (node v then corresponds to an observed gene in a sampled taxon). Node v of G is called observable, if it is an extant leaf or if at least two of its children have at least one extant leaf among their descendants. Nonobservable nodes in G thus include leaves corresponding to gene losses, internal nodes with only gene loss leaves in their subtrees, but also internal nodes that have extant leaves among descendants, but all such leaves are descendants of a single child. Such a node corresponds to a duplication or a speciation which is not observable because only one copy remains. Note that the input gene trees will contain only observable nodes. The definition ensures that in an inferable history, all nodes with one child in S correspond to duplications. Note that such a node can be the root of the species tree if some duplication happened before the first speciation.

Problem specification
Finally, we define the input to our algorithm and the correspondence between this input and the desired output. An example illustrating the following definitions can be seen in Fig. 3.

Definition 4
An input partial history is a triple (G I , S I , µ) such that G I and S I are rooted or unrooted phylogenetic trees with positive edge weights, µ is a mapping from leaves of G I to leaves of S I , and each internal node v in both G I and S I satisfies the following: • If the tree is rooted, v has at least two children.
• If the tree is unrooted, v has at least three neighbors.
Definition 5 An inferable history (G, S, �) is called an isometric reconciliation of the input partial history (G I , S I , µ) if the following holds.
• Tree G is equal to G I if rooted, or it is a rooted version of G I . • Tree S is obtained from S I by rooting it (if unrooted), potentially subdividing some edges by new nodes, and potentially adding a path leading to the original root of S I from above. • Mapping of every leaf v of G I satisfies �(v) = µ(v).

Reconciliation algorithm for rooted trees
If both the gene tree and the species tree are rooted on the input, the reconciliation can be constructed by the following simple algorithm. Algorithm 1 Input: partial input history (G I , S I , µ), G I and S I rooted.
We start by mapping nodes of G = G I to nodes or points on edges of S I . In particular, for every internal node v of G, we choose some leaf u which is a descendant of v. The definition of isometric reconciliation then implies that v should map to anc (µ(u), d(u, v)).
Once all nodes are mapped in this way, we create tree S by subdividing edges of S I and adding a new path from  3 An example of isometric reconciliation. The underlying history, depicted in the diagram on the right, consists of two speciations, one duplication and two losses. The two input trees G I and S I are shown on the left. Note that G I does not contain the lost genes. Reconciliation results in a new species tree S, in which a new node for duplication was added. Isometric mapping is shown as labels in tree S. Input leaf mapping µ agrees with leaf labels and is shown for completeness as labels in tree S I above to the original root so that each node of G maps to a node of S and not to a point inside an edge. A more detailed description of this process can be found in Algorithm 2.
If the input partial history has an isometric reconciliation, this algorithm is sufficient to find it, because mapping of each node is uniquely determined with respect to the chosen leaf u. However, it is possible that the input has no reconciliation, because different choices of leaf u can give different mappings of node v. Therefore in the final phase of the algorithm, we verify if for each edge (u, v) of G (where u is the parent of v) we have that �(u) = anc (�(v), d(u, v)), i.e. that the constructed mapping is a proper isometric mapping. If this condition is violated for any edge, we reject the input as irreconcilable.
Using the techniques which we describe for Algorithm 2, this algorithm can be implemented in O(N log N ) time, where N is the total number of nodes in the two input trees.

Triangulation
The previous simple algorithm maps each internal node v based on the distance to a single already mapped descendant. We cannot use this approach for reconciling unrooted gene trees, because we do not know which leaves in the gene tree are descendants of v. Instead, the basic ingredient in our algorithm will be a triangulation, which maps an internal node based on its distance to two different already mapped nodes. This idea is implicitly used also in the algorithm by Ma et al.; here we state and prove it explicitly.

Definition 6
Consider a rooted or unrooted tree G I and a rooted tree S I with strictly positive branch lengths. Let x, u and v be three (not necessarily distinct) nodes in G I such that x lies on the unique simple path connecting u and v. Let u ′ and v ′ be nodes in S I . By triangulation �(x, u, v, u ′ , v ′ ) we understand the node or point in S I constructed as follows: • If one of x u and x v is a descendant of the other, use the descendant as the triangulation (this includes the case where x u = x v ). • Otherwise, the triangulation is undefined.
In our algorithms, we will choose nodes u and v for which �(u) and �(v) have already been found, and we will use �(x, u, v, �(u), �(v)) to determine �(x) for another node x. The following lemma proves the correctness of such a mapping by triangulation. Proof Let us consider node a = lca (u, v). This node is located on the path connecting u and v in G. There are two possibilities: either a is located on the portion of this path between u and x (including the endpoints), or on the portion between x and v. These two possibilities are in fact symmetric after switching u and v. Therefore, we will assume that node a is on the path from u to x. Vertex u is then a descendant of a, and thus �(a) = anc (�(u), d(u, a)). Since d(u, a) ≤ d(u, x) , x u is an ancestor of �(a). On the other hand, x is a descendant of a and v is a descendant of x. Therefore, �(x) = anc (�(v), d(v, x)) and so node x v will be equal to �(x). Since x u is an ancestor of �(a) and

Problems in the original algorithm
The two papers by Ma et al. [1,2] include the same algorithm for isometric gene tree reconciliation in the case when the input gene tree G I is unrooted and the species tree S I is rooted. In this section, we describe some of its details and point out mistakes in the original paper. We start with the original definition of isometric reconciliation as given by Ma et al. (with only slight changes due to different notation and terminology used in this paper). Note that Ma et al. use the same symbol for the input trees G I and S I and their output versions G and S; we have kept their notation for the purpose of the next definition.

Definition 7 (Original definition)
Any mapping from a gene tree G to a species tree S that roots the gene tree is an isometric reconciliation if 1. Every leaf of G maps to the leaf of the designated species in S. 2. Each internal node of G maps to a speciation node in S or a point on a branch in S. 3. The new root q of G maps to a point �(q) on a branch in S such that any other node x in G maps to �(x) below �(q) and d(�(x), �(q)) = d(x, q). Figure 4 shows that this definition is not sufficiently stringent to characterize meaningful reconciliations. The mapping shown in the figure satisfies all of the above conditions, but does not correspond to any valid history. In particular, node x in G is a parent of leaf a 1 , but node �(x) is not an ancestor of �(a 1 ).
This problem is very easily corrected by demanding that mapping preserves distances and ancestor relationships between every pair of nodes in G, or equivalently, between every pair of adjacent nodes in G, as we have done in our Definition 1.

Overall scheme of the original algorithm
The algorithm of Ma et al. proceeds by first mapping leaves of G I to corresponding leaves of S I and then repeatedly choosing one unmapped node x from G I which has at least two of its three neighbours already mapped. Each such node x is mapped to its corresponding point �(x), and if one of the edges incident to x contains the correct position of the root, the gene tree is rooted. This process continues, until only one node remains.
The overall scheme of the algorithm reveals another minor issue: the algorithm does not work for gene trees with two leaves. The leaves can be mapped trivially, but we also need to find the position of the root on the edge connecting them in G I , and since in the algorithm, rooting is done simultaneously with mapping internal nodes, it is not obvious how to find the root in this case.

Mapping one node
The algorithm for mapping an internal node and, if appropriate, rooting the gene tree, consists of a rather extensive case analysis, with about ten different cases. After simulating the algorithm on several examples, we have discovered that it does not always work correctly. Figure 5 shows a simple input, which can be reconciled. The algorithm maps the only internal node x correctly, but sometimes fails during rooting, rejecting the input as irreconcilable. When mapping the last internal vertex, the algorithm arbitrarily chooses, which two neighbours of this vertex are considered first, and depending on this choice, the algorithm may fail or succeed on this input.

Speciation and duplication happening at the same time
Ma et al. assume that the input trees are binary and that two events (two duplications or a duplication and a speciation) never happen at the same time. However, even binary input trees may lead to situations, where two events happen at the same time. A simple example is when the root of G coincides with one of the internal vertices of G I , and thus it has three children. This situation is handled by the original algorithm and rejected in case 7(b)iii. An example of such an input is shown in Fig. 6. Figure 7 shows a similar input, with only a single branch length changed. Here also a duplication happens at the same time as a speciation, but the rooted gene tree G remains binary thanks to later losses. The original algorithm accepts this input, which seems inconsistent with handling the input from Fig. 6. Note that both of these inputs can be reconciled so that they satisfy our definition of isometric reconciliation (as well as the weaker original definition).
Our algorithms work under a relaxed evolutionary model, in which we allow an arbitrary combination of events to happen at the same time. Thus our modified algorithm shown in the next section will reconcile both of these inputs.
If instead we wish to stipulate that no two events may happen at the same time, we need to modify the algorithm so that it rejects both of these inputs and also modify the definition of isometric reconciliation so that it allows only histories satisfying this requirement. At the end of this article, we discuss simple histories in which each node of the species tree corresponds to a single event. The strict definition of isometric reconciliation could require that the output history is an inferable version of some simple history with strictly positive branch lengths (see Definition 10). Note that this condition can be verified for a given inferable history in a postprocessing step of a reconciliation algorithm using the characterization given in Claim 4 and Definition 8. Fig. 4 A counter-example for the original definition of isometric reconciliation. Trees G I and S I are the input trees, G is the rooted version of G I and S is S I with duplication nodes added. This reconciliation satisfies Definition 7, but it does not correspond to any evolutionary history

Summary of issues
To summarize, the original definition of isometric reconciliation allows nonsense mappings that do not correspond to any evolutionary history and does not adequately handle cases with simultaneous duplication and speciation. In addition, the original algorithm does not handle gene trees with two leaves and sometimes fails to root valid inputs. In the next section, we present a new algorithm that corrects these problems and, at the same time, simplifies the proof of correctness by reducing the case analysis to a minimum.

The modified algorithm
In this section, we describe a new version of the algorithm for finding isometric gene tree reconciliation (G, S, �) satisfying Definition 5, provided that in the input partial history (G I , S I , µ), gene tree G I is unrooted and species tree S I is rooted. Although the overall idea is similar to the original algorithm, we have made it more modular, with several passes through the tree, each relatively simple. This allows us to avoid complicated case analysis in both the algorithm and the proof.
To keep the algorithm efficient, we will defer explicit construction of S with added duplication nodes. In the first stages of the algorithm we create mapping , but when a node of G I maps to pt (u, d), we will keep the mapping simply as a pair (u, d).
Algorithm 2 Input: partial input history (G I , S I , µ), G I unrooted, S I rooted.
). If this triangulation is not defined, reject the input. Stage 3: Add new nodes to G. Consider each edge (u, v) of G. We want to decide if this edge should be subdivided by a new node, which will be a potential root. Let = lca (�(u), �(v)) and let Fig. 8). Set �(q) = anc ( , ǫ).

Proof of correctness
First let us assume that input partial history (G I , S I , µ) has some isometric reconciliation (G * , S * , � * ). We will prove that the algorithm will indeed output (G * , S * , � * ) on this input, regardless of arbitrary choices in the algorithm, such as the order of processing of vertices in Stage 2. This implies that the isometric reconciliation is unique, because if there were two distinct reconciliations, the algorithm cannot produce both of them simultaneously.  Correctness of the mapping constructed in Stage 2 can be obtained by induction from the properties of triangulation. If for both mapped neighbors of x we have �(u) = � * (u) and �(v) = � * (v), then by Lemma 1 we have that �(x) = � * (x). Since the mapping of leaves is given on the input, the algorithm will correctly recover * for each internal node of G I . Note that the existence of an unmapped node with at least two mapped neighbors in each iteration of the algorithm is guaranteed by basic properties of trees.
The root of G * is either one of the nodes of G I or a new node subdividing some edge; this is the only allowed addition of a node to G. We will now prove that the only node potentially added in Stage 3 is the root of G * .

Lemma 2 If Stage 3 of the algorithm considers an edge
(u, v) of G I with its endpoints correctly mapped to S, it will subdivide this edge by a new node if and only if G * has a root inside this edge. The new node will be created at the correct position and mapped correctly.
Proof Let q * be the root of G * , and let us consider three cases regarding the position of q * with respect to u and v.
• If � * (q * ) is not , must be a descendant of � * (q * ), with d( , � * (q * )) = ǫ ′ > 0 . Note that in this case, it is possible that Thus a node will be created, and its distance from u will be correctly set to d(� * (u), ) + ǫ = δ.
Case 2 After removal of edge (u, v) from G * , q * is in the connected component containing u, including the case q * = u. Then � * (u) = anc (� * (v), d(u, v)) and thus = � * (u). In addition, d(� * (u), ) = 0, d(� * (v), ) = d(u, v) and thus ǫ = 0. No node will be created, which is correct, as in this case the root is not inside this edge. Case 3q * is in the connected component containing v. This case is symmetrical to case 2. □ After Stage 3, we will thus have all nodes of G I correctly mapped to S, and if the root of G is not one of the nodes of G I , it was also correctly added and mapped. Stage 4 simply changes the representation of S from implicit to explicit. The definition of isometric mapping implies that if q * is the root of G * , then all other nodes of G * map to proper descendants of � * (q * ) and thus q * is the only node that can be selected in Stage 5 as the root. Thus the algorithm will correctly recover the correct answer (G * , S * , � * ).
To finish the proof of correctness, we need to prove that the algorithm will correctly reject any input for which no reconciliation exists. Thanks to explicit checks in Stage 6, the output will be always a correct isometric reconciliation, with one possible exception: Stage 3 may possibly subdivide edges of G by nodes which are not the root. However if a triple (G, S, �) exists which satisfies all conditions of isometric reconciliation except for some spurious edge subdivisions in G, a proper isometric reconciliation would exists as well. This is a contradiction, because in this part of the proof, we assume that the input cannot be reconciled.

Running time analysis
Let m be the number of nodes of G I and n the number of nodes of S I . Ma et al. claim that their algorithm works in O(mn) time. We will prove that a more efficient implementation of isometric reconciliation is possible. Within the algorithm, we use several nontrivial operations on S: • Finding lca of two nodes. We can use efficient data structures for solving lca queries in O(1) time after O(n) preprocessing of the tree [15,16]. • Determining if node u is an ancestor of v. This is equivalent to asking if u = lca (u, v). • Computing the distance between node v and its ancestor u. This can be done in O(1) time by keeping the distance from the root of S I in each node and subtracting these distances for u and v. • Finding anc (u, d). This operation is known as level ancestor. For unweighted trees, it can be solved in O(1) time after O(n) preprocessing [17] and for trees with integer weights in O(log log u) time, where u is the maximum edge weight [18]. Below, we outline a simplified version of this data structure which achieves O(log n) time per query, but works for arbitrary edge weights, as edge weights in phylogenetics are typically not expressed as integers.
Using these building blocks, the rest of the algorithm is relatively straightforward. For Stage 2, we maintain a counter of mapped neighbors for each internal node of G I and a stack of unprocessed nodes with at least two neighbors already mapped. In each step, we remove one node from the stack, map it, and increase the counters of its neighbors. If any counter reaches 2, the corresponding node is added to the stack. The overall overhead for selecting nodes for mapping in Stage 2 is thus O(m), and mapping each node works in O(log n). Linear-time precomputation for various tree operations can be initially done for the original tree S I and after Stage 4 recomputed for S. However, in Stages 2 and 3, some queries will use as arguments implicit nodes pt (u, d) instead of regular nodes of S I . All tree queries can be easily extended to work also for such generalized arguments. For example, anc ( pt (u, d 1 ), d 2 ) = anc (u, d 1 + d 2 ). When computing lca ( pt (u, d 1 ), pt (v, d 2 )), we can compute = lca (u, v) and then distinguish several cases based on whether ∈ {u, v}.

A simple level ancestor for arbitrary weights
For completeness, we briefly describe a data structure for finding anc (u, d) in O(log n) time for arbitrary edge weights, provided that we can do addition, subtraction, and sign operation in constant time. We use a simplified version of the data structure by Amir et al. [18]; the simplification is possible thanks to the fact that the running time is worse than the running time achievable for integer edge weights.
Let node weight w(u) be the sum of edge weights on the path from the root to node u. To compute anc (u, d) , we are looking for the highest ancestor v of u such that w(u) − w(v) ≤ d. If we had only a single path instead of a tree, we would be looking for a predecessor of value x = w(u) − d in the sequence of node weights. Since this sequence is increasing, we can use binary search to find the desired index v.
In a general tree, we will use the heavy path decomposition [15]. An edge connecting node v to its parent p in a tree is called heavy if the size of the subtree rooted at v (the number of nodes) is at least half of the size of the subtree rooted at p. Otherwise the edge is called light. For each node, at most one of its children is connected to it by a heavy edge. Therefore, heavy edges form a set of vertex-disjoint paths. Vertices which are not incident to any heavy edge will be considered as heavy paths of length 0 so that each node is included in exactly one heavy path.
We create an array of node weights for each heavy path. Each vertex v also keeps the reference to the highest node on its heavy path. When searching for anc (u, d), we search along the path from u to the root to find the heavy path that contains the answer. Thanks to the properties of the heavy path decomposition, there are at most O(log n) light edges on any leaf-to-root path, and thus we can use linear search to iterate through heavy paths encountered on the way to the root. In constant time, we can jump to the head of the path and comparing x to the value stored in the head and in the head's parent, we can determine if this path contains the answer. Within the correct path, we then find the answer by binary search. The overall time is thus O(log n).
The data structure for integer weights by Amir et al. [18] uses binary search over heavy paths, which requires repeated use of the unweighted level ancestor data structure. Instead of binary search within a path, they use efficient data structures for the predecessor problem with integer keys.

Extensions of the problem
In this section, we consider two extensions of the isometric reconciliation problem. First, we consider the situation when both input trees are unrooted. Note that if the input species tree is rooted, there is at most one reconciliation, even if G I was unrooted. This is no longer the case when both S I and G I are unrooted. Next we will return to the simple scenario when both input trees are rooted, but allow the branch lengths of G I to be scaled by an arbitrary positive scaling factor.

Unrooted input trees
If both input trees are unrooted, some inputs may have multiple reconciliations. For example, the input in Fig. 9 has a unique reconciliation, while in a similar input in Fig. 10, the species tree can be rooted at any point pt (a, b, ρ) for ρ ∈ [1, 4) inside its only edge (a, b).
We will now describe a different version of the algorithm for unrooted G I and rooted S I , which will form the basis of our algorithm for unrooted S I . Algorithm 3 Input: partial input history (G I , S I , µ), G I unrooted, S I rooted.
In the first part of this algorithm, we consider each edge (u, v) of G I separately, and run modified Stages 2, 3, and a part of Stage 6 in the following three steps.
Step B: If needed, subdivide edge (u, v) by a new potential root q, as in Stage 3 of Algorithm 2.
Step C: If the edge was not subdivided, check that d(�(u), �(v)) = d(u, v). If the edge was subdivided by q, do an analogous distance check for both new edges (u, q), (q, v). If the check fails, reject the input.
Step A is a modified version of Stage 2. Originally we mapped nodes by triangulating from already mapped neighbors of a node; now we use only known mapping for leaves of G. This change allows individual edges to be processed independently from each other. Correctness of the mapping is guaranteed by Lemma 1. However, internal nodes belong to multiple edges, and thus they will be mapped multiple times in Step A. To keep the mapping consistent, we will deterministically choose the same pair of leaves ℓ 1 and ℓ 2 each time when mapping node x. Once this process is completed for all edges, we will run the original Stages 4, 5 and 6 to finish creating S I , G I and to check the correctness of the mapping.
We will now describe the algorithm for reconciling two unrooted trees. Algorithm 4 Input: partial input history (G I , S I , µ), G I and S I unrooted.
Output: the set of all isometric gene tree reconciliations (G, S, �) of (G I , S I , µ).
This algorithm is more complex, and we will describe it on the following pages. It is based on simulating Algorithm 3 on multiple inputs with different root positions in S I . The problem with applying the above algorithm for unrooted species tree is that the results of many intermediate operations (namely computing anc and lca , determining descendant-ancestor relationships, computing ǫ when adding a root, and computing distances) are dependent on the position of the root of the species tree. However, when we move the root only by a small distance, the results of these operations often remain the same or also change only by a small amount. The idea of our algorithm is to split all possible positions of the root into intervals that will produce essentially the same results.   ∈ (3, 4). These two cases differ in the position of �(x). For ρ = 1, node q coincides with x (and thus �(q) coincides with �(x)) and for ρ = 3, node r coincides with �(x). Case ρ = 4 is not possible because our definitions do not allow rooting a phylogenetic tree in its leaf. Note that the species tree cannot be rooted at position pt (a, b, ρ) for ρ ∈ (0, 1) we get at most 10 endpoints and 11 subintervals in total. In Step B (Stage 3 of Algorithm 2), we start by computing = lca (�(u), �(v)). Again, if at least one of �(u) and �(v) is fixed or if they are both parametric with the same c, can be easily computed. Conversely if they are both parametric with different values of c, we solve the linear equation, split the interval as needed, and determine which is the ancestor of the other in each resulting interval.
To determine, if ǫ is positive, we solve the equation d + cρ = 0 and split the current interval if the solution is inside it. Note that the position of the new root x in G might also be parametric of the form pt (u, v, d + cρ) and �(x) = anc ( , ǫ) might be parametric with c from some finite set of values.
Finally, in Step C we compare distances between adjacent nodes p and q in G and �(p) and �(q) in S; this is done for one or two pairs of nodes depending on whether edge (u, v) was subdivided. The distances in both trees might be parametric and to establish equality, we may again need to solve a linear equation and split the interval. In addition, to measure distance between two parametric points in S of the form pt (r, d 1 + c 1 ρ) and pt (r, d 2 + c 2 ρ) , we need to distinguish, which of them is the ancestor of the other, and thus we may need to split the current interval at the point where Overall, in processing one edge (u, v) of G I , we have split the original candidate unequivocal interval covering the whole edge (x a , x b ) into O(1) subintervals and for each we have executed Steps A, B and C. Some of these subintervals might have been rejected by the algorithm. This is done independently for every edge (x a , x b ) of S I and every edge (u, v) of G I , and thus overall we may have up to O(nm) interval endpoints. We will create a new set of candidate unequivocal intervals by pooling all interval endpoints together and creating a candidate interval between every two adjacent endpoints. Within such a new candidate unequivocal interval, the completed stages of the algorithm would run without producing further endpoints.
For each of these intervals we now consider the remaining stages of the algorithm. Let us first assume that we do not need an explicit representation of S and let us skip Stage 4. In Stage 5, we create lists � −1 (x) for both explicit and implicit nodes of S; note that implicit nodes might be fixed or parametric. Let us consider the set R of nodes for which � −1 (x) is non-empty. Among nodes of R which are parametric with the same value of c, we can eliminate all except the one with the highest value of d, which is the only potential candidate for the root. Also any parametric node eliminates all fixed nodes, which are necessarily its descendants. If we are left with several candidate parametric nodes with different values of c, we can perform all pairwise comparisons by solving linear equations and subdividing the current interval further until each new interval has a uniquely determined highest value. Since c can have at most O(1) different values, we will subdivide each interval O(1) times, thus keeping the number of intervals O(nm).
In Stage 6, we need to compare distances and ancestor relationships between adjacent nodes u and v of G and their counterparts �(u) and �(v) of S. However, since we have already done a partial correctness check in Step C, no further interval subdivision is necessary in this step.
If desired, it is also possible to explicitly construct S. For that we need to order all the parametric nodes by the size of their value d + cρ so that they can be placed on the new path leading to the root. Again, implicit nodes with the same c can be ordered easily by their value of d. To compare other pairs, we solve all pairwise equations of the form d 1 + c 1 ρ = d 2 + c 2 ρ and subdivide the current interval at the points where this equation has a solution. In each resulting interval, the relative order of all implicit nodes is uniquely determined. For each starting interval, we solve O(m 2 ) equations, obtaining O(m 2 ) intervals. Thus the overall number of intervals is O(nm 3 ) . The running time is O(nm 3 (n + m) log(n + m)) due to data structures needed for level ancestor.
To summarize, the algorithm described above produces a solution which is a set of intervals on the edges of S I . For each interval we obtain a parametric reconciliation in which the position of the nodes above the root is given in the form pt (r, d + cρ) where ρ is the distance of the root position from some node x a of S I . Our algorithm runs in O(nm 3 (n + m) log(n + m)) time. The main factor is the number of intervals which we estimated as O(nm 3 ), but perhaps a better upper bound on the number of intervals can be found.

Scaling branch lengths
So far we have assumed that the branch lengths of the two input trees are known exactly. In this section, we consider a slight relaxation of this assumption, in which we assume that all branch lengths in the gene tree should be multiplied by some unknown constant factor α > 0 . For tree G, let αG denote the tree obtained from G by multiplying all branch lengths by α. The question is for which values of α is gene tree αG I reconcilable with S I ? This variant of the problem is an idealized model of the situation where different genes evolve by different rates. We will address this problem for the simplest scenario where both input trees G I and S I are rooted. We will also assume that for each leaf v of S I , µ −1 (v) is non-empty. This condition can be easily ensured by input preprocessing, where we delete all subtrees of S I to which no leaf of G I maps.
We say that a rooted tree is ultrametric, if all its leaves are at the same distance from the root [3]. Clearly, if the input trees can be reconciled and one of them is ultrametric, the other must be ultrametric as well; otherwise distances from the root to some leaves could not match in the mapping. It turns out that ultrametric and nonultrametric trees behave quite differently. The following two claims state the results, and their proofs include algorithms for finding appropriate values of α.

Claim 1
If neither G I nor S I are ultrametric, there is at most one value of α > 0 such that αG I can be reconciled with S I .
Proof Let q be the root of G I and let a and b be two leaves of G I such that δ G = d(q, a) − d(q, b) is greater than zero. Such leaves must exist in a tree which is not ultrametric. Let r be the root of S I , and let δ S = d(r, µ(a)) − d(r, µ(b)). Clearly, we need to scale G I so that αδ G = δ S , which is possible for at most one value of α > 0.
Once we have α fixed, we can run Algorithm 1 for rooted G I and S I to check if αG I and S I are reconcilable. □ Claim 2 If both G I and S I are ultrametric, then αG I and S I can be reconciled for all α ≥ α * for some value α * .
Proof Let v be a node of G I . Let h G (v) denote its height, i.e., the distance of v from the leaves in its subtree. Next, let X v be the set of all leaves which are descendants of v and let µ(X v ) be the set of leaves of S I where they map via input mapping µ. Let x v be the lowest common ancestor of all leaves in µ(X v ). By h S (x v ), we will denote the height of node x v in S I .
Clearly, must map v to x v or its ancestor, because �(v) must be an ancestor of each leaf in X v . Therefore αh G (v) must be at least h S (x v ). We will set α * to be the maximum of values h S (x v )/h G (v) for all internal nodes v of G I .
Consider now some α ≥ α * and assume that we use Algorithm 1 for reconciling αG I to S I . To map an internal node v of G I , the algorithm uses an arbitrarily chosen leaf u from the subtree rooted at v. Since αh G (v) ≥ h S (x v ), v will map to an ancestor of x v . Since G I and S I are ultrametric, all choices of u will map v to the same height, and since this height is at or above the height of the common ancestor x v , all choices of u will map v to the same node or point of S I . This means that the algorithm will construct a valid isometric reconciliation. □

Simple histories
All algorithms presented in this paper output inferable histories. An inferable history captures only events that can be inferred from the input partial history. As a result, there is at most one inferable history for each input partial history with rooted species tree. However, it is not always easy to interpret individual nodes of the two trees in an inferable history as evolutionary events (speciations, duplications, losses). Indeed, some nodes may correspond to more than one event and conversely, some events may be missing in the trees. In this section, we define simple histories, in which each node of the species tree corresponds to a single event. These histories thus correspond to an intuitive model of gene family evolution.
Moreover, we describe a correspondence between simple and inferable histories which sheds more light on information missing in the inferable histories returned by our algorithm. Although every inferable history corresponds to some simple history, this simple history may have some branches of zero length. Sometimes we might be interested to know if an inferable history produced by one of our algorithms corresponds to an evolutionary scenario in which only one event happens at any given time on a given branch. This is equivalent to asking if the inferable history corresponds to some simple history with non-zero branch lengths. In Claim 4, we give a characterization of such inferable histories, which can be easily tested algorithmically.
We will start by characterizing nodes of a species tree that correspond to a single evolutionary event.
To do so, we need to define an extended inverse mapping � −1(ext) (v), which contains � −1 (v) as well as pt (a, d(�(a), v)) for each a such that a maps to a proper descendant of v, and the parent of a maps to a proper ancestor of v (i.e., if we subdivide edge from a to its parent at this point, the new node would also map to v). For example in Fig. 3, � −1(ext) (x) = { pt (c 1 , 2), pt (c 2 , 2)}.

Definition 8
Consider an arbitrary history (G, S, �) and a node v in S.
• We call v a sample if v is a leaf (i.e. it corresponds to a sampled taxon). • We call v a generalized speciation if it has exactly two children v 1 and v 2 and each u ∈ � −1 (v) has exactly two children u 1 and u 2 such that �(u 1 ) is a descend-ant of v 1 and �(u 2 ) is a descendant of v 2 (i.e. v corresponds to a single speciation, but some genes present in the ancestral species might bypass v and continue to one of the daughter species; this is prevented by the next definition). • We call v a speciation if it is a generalized speciation and moreover non-empty and each u ∈ � −1 (v) has exactly two children.
Note that a speciation and a sample do not need to have −1 non-empty, as some species may have lost all copies from the considered family. Also note that a single duplication or deletion event can affect several genes simultaneously, which is consistent with events acting on longer chromosomal segments.
For example in tree S in Fig. 3, nodes a, b and c are samples, r is a speciation, r and x are generalized speciations and �(y) is a duplication. Node z in tree S ′ in Fig. 11 is a loss.

Definition 9
A simple history is a history (G, S, �) in which each node v ∈ S is either a speciation, a duplication, a loss, or a sample.

Definition 10
Consider a simple history (G, S, �) in which all edges leading to leaves in both trees have positive branch lengths. The inferable version of this history is a history obtained as follows.
• Delete each node in G with no extant leaf among its descendants. • Bypass each node in G with a single child.
• Bypass each node v in S if it has a single child and � −1 (v) is empty.
• Contract all edges of length zero in both G and S.
• If the root q of G has a single child, delete q (the child becomes a new root). • If the root r of S has a single child, and � −1 (r) is empty, delete r (the child becomes a new root).
For example, history (G, S, �) in Fig. 3 is an inferable version of simple history (G ′ , S ′ , � ′ ) in Fig. 11. The simple history has leaves for deleted genes in the gene tree, and the species tree contains an internal node with one child to which these deleted genes map. Note that the simple history very naturally corresponds to the history diagram shown in these figures.
The following two claims establish relationship between inferable and simple histories.

Claim 3 Every inferable history is the inferable version of infinitely many simple histories.
Proof It is sufficient to prove that every inferable history (G, S, �) corresponds to at least one simple history; additional simple histories can be created by adding unobservable events, such as a duplication followed by a deletion of one of the resulting copies.
Let v be a node of S. If v does not correspond to a single event allowed in simple histories, we will convert it to a series of such events. We will distinguish several cases, depending on the number of children of v. If v has no children, it is a sample, which is one of allowed node types. Now let us consider the case when v has exactly one child. Note that for a node with one child, � −1 (v) must be non-empty. In addition, each node in � −1 (v) has at least two children because all leaves of G are extant, i.e., they map to leaves of S, and G has no nodes with one child. Let k ≥ 2 be a value such that each node in � −1 (v) has at most k children. We replace node v in S by a path  Fig. 11 An example of a simple history (G ′ , S ′ , � ′ ). The history (G, S, �) in Fig. 3 is an inferable version of (G ′ , S ′ , � ′ ) of nodes v 1 , v 2 , . . . , v k−1 , each having one child. Successive nodes in this path are connected by edges of length zero. The parent of v is connected to v 1 and the child of v is connected to v k−1 . Each node u ∈ � −1 (v) with ℓ children (ℓ ≥ 2) is similarly replaced by a path of nodes u 1 , . . . , u ℓ−1 with zero branch lengths. The original children of u are distributed among the new nodes so that each new node u i has exactly two children. Node u i maps to v i . By this transformation, we have converted v to a series of k − 1 duplications. Finally, if node v from S has g ≥ 2 children, we replace it with g − 1 speciation nodes v 1 , . . . , v g−1 connected by a path with edges of length zero. The original children of v are again distributed among these new nodes so that each v i has exactly two children. We also replace each node u ∈ � −1 (v) by a path u 1 , . . . , u g−1 and map u i to v i . Each child u ′ of u maps to a descendant of some child v ′ of v.
Node v ′ was placed as a child of some node v i , and similarly, u ′ will become a child of the corresponding node u i . Ideally, we would have one node u ′ for every child v ′ of v. If some v ′ has no corresponding node u ′ , we will create a new leaf u ′ corresponding to a loss. It will be connected to appropriate u i by an edge of length smaller than edge from v i to v ′ . Edge from v i to v ′ will be subdivided at the corresponding point so that we can map u ′ . On the other hand, if some v ′ has multiple corresponding nodes u ′ 1 , . . . u ′ ℓ , we will create a path of ℓ − 1 duplication nodes immediately following v i , similarly as in the case when v has one child. Corresponding nodes will be also created in G and u ′ 1 , . . . u ′ ℓ will become their children. Finally, to ensure that v 1 , . . . , v g−1 are proper speciation nodes, we will also consider points in � −1(ext) (v) and subdivide each edge with such a point by a similar path u 1 , . . . , u g−1 . Since only one branch leading from this path will be non-empty, we will add new leaves for losses similarly as before. Claim 4 An inferable history (G, S, �) is the inferable version of some simple history with strictly positive branch lengths if and only if every node of S is a sample, a duplication, or a generalized speciation.
Proof If every node of S is a sample, a duplication or a generalized speciation, the construction given in the previous claim will provide a history with strictly positive branch lengths. Now let us assume that (G, S, �) is the inferable version of some simple history (G ′ , S ′ , � ′ ) with positive branch lengths. In the process of obtaining (G, S, �), no edges are contracted, because there are no branches of zero length in G ′ and S ′ . The only operations performed on G ′ and S ′ are deletions of nodes and edges and bypassing nodes with one child. As a result, each node of G or S corresponds to a single node of G ′ or S ′ . Also each node of G or S has at most two children.
Consider node v of S. If it has no children, it is a sample. If it has one child, � −1 (v) is non-empty and all nodes on � −1 (v) have exactly two children; therefore v is a duplication. If v has two children in S, it must also have two children in S ′ , and thus it is a speciation in S ′ . Each node u ∈ � ′−1 (v) thus has two children, each corresponding to a different branch leading from v. During changes transforming (G ′ , S ′ , � ′ ) to (G, S, �) some nodes u ∈ � ′−1 (v) may be deleted or bypassed, but the remaining ones will satisfy the criteria required by generalized speciation. Thus we have proved that every node of S is a sample, a duplication or a generalized speciation.

Conclusions
In this paper, we have corrected an algorithm for isometric gene tree reconciliation, first presented by Ma et al. [1,2] in the context of reconstruction of evolutionary histories in the infinite sites model. We have also improved the running time of the algorithm from O(N 2 ) to O (N log N ), where N is the total size of the two input trees.
We have also studied two extensions of the problem. First, we have considered the case when both the gene tree and the species tree are unrooted. We have designed an algorithm with running time O(N 5 log N ), which is much slower than the algorithm for reconciling rooted species trees. Perhaps this could be improved by proving a better upper bound on the number of intervals which we need to consider as possible locations of the root. Another related problem is to consider rooted gene tree and unrooted species tree.
In practical applications, we cannot rely on the assumption that the branch lengths are exactly correct. Algorithms that would allow for errors in branch lengths, e.g. assuming that branch lengths are correct up to some degree of tolerance, would be of a large practical value. As the first step in this direction, we have designed an algorithm which allows all branch lengths of the gene tree to be scaled by a constant factor α. Such scaling factors are used in practice to model different substitution rates in different gene families. However, we have considered only the case when both input trees are rooted; the variants of the problem with one or both trees unrooted remain open.