2 // FORESTER -- software libraries and applications
3 // for evolutionary biology research and applications.
5 // Copyright (C) 2008-2009 Christian M. Zmasek
6 // Copyright (C) 2008-2009 Burnham Institute for Medical Research
9 // This library is free software; you can redistribute it and/or
10 // modify it under the terms of the GNU Lesser General Public
11 // License as published by the Free Software Foundation; either
12 // version 2.1 of the License, or (at your option) any later version.
14 // This library is distributed in the hope that it will be useful,
15 // but WITHOUT ANY WARRANTY; without even the implied warranty of
16 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 // Lesser General Public License for more details.
19 // You should have received a copy of the GNU Lesser General Public
20 // License along with this library; if not, write to the Free Software
21 // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
23 // Contact: phylosoft @ gmail . com
24 // WWW: www.phylosoft.org/forester
26 package org.forester.sdi;
28 import java.util.HashMap;
30 import org.forester.phylogeny.Phylogeny;
31 import org.forester.phylogeny.PhylogenyNode;
32 import org.forester.phylogeny.data.Event;
33 import org.forester.phylogeny.data.Taxonomy;
34 import org.forester.phylogeny.iterators.PhylogenyNodeIterator;
35 import org.forester.util.ForesterUtil;
38 * Implements our algorithm for speciation - duplication inference (SDI). <p>
39 * The initialization is accomplished by: </p> <ul> <li>method
40 * "linkExtNodesOfG()" of class SDI: setting the links for the external nodes of
41 * the gene tree <li>"preorderReID(int)" from class Phylogeny: numbering of
42 * nodes of the species tree in preorder <li>the optional stripping of the
43 * species tree is accomplished by method "stripTree(Phylogeny,Phylogeny)" of
44 * class Phylogeny </ul> <p> The recursion part is accomplished by this class'
45 * method "geneTreePostOrderTraversal(PhylogenyNode)". <p> Requires JDK 1.5 or
48 * @see SDI#linkNodesOfG()
50 * @see Phylogeny#preorderReID(int)
53 * PhylogenyMethods#taxonomyBasedDeletionOfExternalNodes(Phylogeny,Phylogeny)
55 * @see #geneTreePostOrderTraversal(PhylogenyNode)
57 * @author Christian M. Zmasek
59 public class GSDI extends SDI {
61 private final HashMap<PhylogenyNode, Integer> _transversal_counts;
62 private final boolean _most_parsimonious_duplication_model;
63 private int _speciation_or_duplication_events_sum;
64 private int _speciations_sum;
67 * Constructor which sets the gene tree and the species tree to be compared.
68 * species_tree is the species tree to which the gene tree gene_tree will be
69 * compared to - with method "infer(boolean)". Both Trees must be completely
70 * binary and rooted. The actual inference is accomplished with method
71 * "infer(boolean)". The mapping cost L can then be calculated with method
72 * "computeMappingCost()".
75 * @see #infer(boolean)
76 * @see SDI#computeMappingCostL()
78 * reference to a rooted gene tree to which assign duplication vs
79 * speciation, must have species names in the species name fields
80 * for all external nodes
82 * reference to a rooted binary species tree which might get
83 * stripped in the process, must have species names in the
84 * species name fields for all external nodes
86 * @param most_parsimonious_duplication_model
87 * set to true to assign nodes as speciations which would
88 * otherwise be assiged as unknown because of polytomies in the
92 public GSDI( final Phylogeny gene_tree,
93 final Phylogeny species_tree,
94 final boolean most_parsimonious_duplication_model ) {
95 super( gene_tree, species_tree );
96 _speciation_or_duplication_events_sum = 0;
98 _most_parsimonious_duplication_model = most_parsimonious_duplication_model;
99 _transversal_counts = new HashMap<PhylogenyNode, Integer>();
100 _duplications_sum = 0;
101 getSpeciesTree().preOrderReId();
103 geneTreePostOrderTraversal( getGeneTree().getRoot() );
106 private Event createDuplicationEvent() {
107 final Event event = Event.createSingleDuplicationEvent();
112 private Event createSingleSpeciationOrDuplicationEvent() {
113 final Event event = Event.createSingleSpeciationOrDuplicationEvent();
114 ++_speciation_or_duplication_events_sum;
118 private Event createSpeciationEvent() {
119 final Event event = Event.createSingleSpeciationEvent();
124 // s is the node on the species tree g maps to.
125 private void determineEvent( final PhylogenyNode s, final PhylogenyNode g ) {
127 // Determine how many children map to same node as parent.
128 int sum_g_childs_mapping_to_s = 0;
129 for( final PhylogenyNodeIterator iter = g.iterateChildNodesForward(); iter.hasNext(); ) {
130 if ( iter.next().getLink() == s ) {
131 ++sum_g_childs_mapping_to_s;
134 // Determine the sum of traversals.
135 int traversals_sum = 0;
136 int max_traversals = 0;
137 PhylogenyNode max_traversals_node = null;
138 if ( !s.isExternal() ) {
139 for( final PhylogenyNodeIterator iter = s.iterateChildNodesForward(); iter.hasNext(); ) {
140 final PhylogenyNode current_node = iter.next();
141 final int traversals = getTraversalCount( current_node );
142 traversals_sum += traversals;
143 if ( traversals > max_traversals ) {
144 max_traversals = traversals;
145 max_traversals_node = current_node;
149 // System.out.println( " sum=" + traversals_sum );
150 // System.out.println( " max=" + max_traversals );
151 // System.out.println( " m=" + sum_g_childs_mapping_to_s );
152 if ( sum_g_childs_mapping_to_s > 0 ) {
153 if ( traversals_sum == 2 ) {
154 event = createDuplicationEvent();
156 else if ( traversals_sum > 2 ) {
157 if ( max_traversals <= 1 ) {
158 if ( _most_parsimonious_duplication_model ) {
159 event = createSpeciationEvent();
162 event = createSingleSpeciationOrDuplicationEvent();
166 event = createDuplicationEvent();
167 _transversal_counts.put( max_traversals_node, 1 );
171 event = createDuplicationEvent();
175 event = createSpeciationEvent();
177 g.getNodeData().setEvent( event );
181 * Traverses the subtree of PhylogenyNode g in postorder, calculating the
182 * mapping function M, and determines which nodes represent speciation
183 * events and which ones duplication events.
185 * Preconditions: Mapping M for external nodes must have been calculated and
186 * the species tree must be labeled in preorder.
191 * starting node of a gene tree - normally the root
193 void geneTreePostOrderTraversal( final PhylogenyNode g ) {
194 if ( !g.isExternal() ) {
195 for( final PhylogenyNodeIterator iter = g.iterateChildNodesForward(); iter.hasNext(); ) {
196 geneTreePostOrderTraversal( iter.next() );
198 final PhylogenyNode[] linked_nodes = new PhylogenyNode[ g.getNumberOfDescendants() ];
199 for( int i = 0; i < linked_nodes.length; ++i ) {
200 linked_nodes[ i ] = g.getChildNode( i ).getLink();
202 final int[] min_max = obtainMinMaxIdIndices( linked_nodes );
203 int min_i = min_max[ 0 ];
204 int max_i = min_max[ 1 ];
205 // initTransversalCounts();
206 while ( linked_nodes[ min_i ] != linked_nodes[ max_i ] ) {
207 increaseTraversalCount( linked_nodes[ max_i ] );
208 linked_nodes[ max_i ] = linked_nodes[ max_i ].getParent();
209 final int[] min_max_ = obtainMinMaxIdIndices( linked_nodes );
210 min_i = min_max_[ 0 ];
211 max_i = min_max_[ 1 ];
213 final PhylogenyNode s = linked_nodes[ max_i ];
215 // Determines whether dup. or spec.
216 determineEvent( s, g );
217 // _transversal_counts.clear();
221 public int getSpeciationOrDuplicationEventsSum() {
222 return _speciation_or_duplication_events_sum;
225 public int getSpeciationsSum() {
226 return _speciations_sum;
229 private int getTraversalCount( final PhylogenyNode node ) {
230 if ( _transversal_counts.containsKey( node ) ) {
231 return _transversal_counts.get( node );
236 private void increaseTraversalCount( final PhylogenyNode node ) {
237 if ( _transversal_counts.containsKey( node ) ) {
238 _transversal_counts.put( node, _transversal_counts.get( node ) + 1 );
241 _transversal_counts.put( node, 1 );
243 // System.out.println( "count for node " + node.getID() + " is now "
244 // + getTraversalCount( node ) );
248 * This allows for linking of internal nodes of the species tree (as opposed
249 * to just external nodes, as in the method it overrides.
253 void linkNodesOfG() {
254 final HashMap<Taxonomy, PhylogenyNode> speciestree_ext_nodes = new HashMap<Taxonomy, PhylogenyNode>();
255 for( final PhylogenyNodeIterator iter = _species_tree.iteratorLevelOrder(); iter.hasNext(); ) {
256 final PhylogenyNode n = iter.next();
257 if ( n.getNodeData().isHasTaxonomy() ) {
258 if ( speciestree_ext_nodes.containsKey( n.getNodeData().getTaxonomy() ) ) {
259 throw new IllegalArgumentException( "taxonomy [" + n.getNodeData().getTaxonomy()
260 + "] is not unique in species phylogeny" );
262 speciestree_ext_nodes.put( n.getNodeData().getTaxonomy(), n );
265 // Retrieve the reference to the PhylogenyNode with a matching species
267 for( final PhylogenyNodeIterator iter = _gene_tree.iteratorExternalForward(); iter.hasNext(); ) {
268 final PhylogenyNode g = iter.next();
269 if ( !g.getNodeData().isHasTaxonomy() ) {
270 throw new IllegalArgumentException( "gene tree node " + g + " has no taxonomic data" );
272 final PhylogenyNode s = speciestree_ext_nodes.get( g.getNodeData().getTaxonomy() );
274 throw new IllegalArgumentException( "species " + g.getNodeData().getTaxonomy()
275 + " not present in species tree." );
282 public String toString() {
283 final StringBuffer sb = new StringBuffer();
284 sb.append( "Most parsimonious duplication model: " + _most_parsimonious_duplication_model );
285 sb.append( ForesterUtil.getLineSeparator() );
286 sb.append( "Speciations sum : " + getSpeciationsSum() );
287 sb.append( ForesterUtil.getLineSeparator() );
288 sb.append( "Duplications sum : " + getDuplicationsSum() );
289 sb.append( ForesterUtil.getLineSeparator() );
290 if ( !_most_parsimonious_duplication_model ) {
291 sb.append( "Speciation or duplications sum : " + getSpeciationOrDuplicationEventsSum() );
292 sb.append( ForesterUtil.getLineSeparator() );
294 sb.append( "mapping cost L : " + computeMappingCostL() );
295 return sb.toString();
298 static int[] obtainMinMaxIdIndices( final PhylogenyNode[] linked_nodes ) {
301 int max_i_id = -Integer.MAX_VALUE;
302 int min_i_id = Integer.MAX_VALUE;
303 for( int i = 0; i < linked_nodes.length; ++i ) {
304 final int id_i = linked_nodes[ i ].getId();
305 if ( id_i > max_i_id ) {
307 max_i_id = linked_nodes[ max_i ].getId();
309 if ( id_i < min_i_id ) {
311 min_i_id = linked_nodes[ min_i ].getId();
314 return new int[] { min_i, max_i };
317 * Updates the mapping function M after the root of the gene tree has been
318 * moved by one branch. It calculates M for the root of the gene tree and
319 * one of its two children.
321 * To be used ONLY by method "SDIunrooted.fastInfer(Phylogeny,Phylogeny)".
325 * @param prev_root_was_dup
326 * true if the previous root was a duplication, false otherwise
327 * @param prev_root_c1
328 * child 1 of the previous root
329 * @param prev_root_c2
330 * child 2 of the previous root
331 * @return number of duplications which have been assigned in gene tree
333 // int updateM( final boolean prev_root_was_dup,
334 // final PhylogenyNode prev_root_c1, final PhylogenyNode prev_root_c2 ) {
335 // final PhylogenyNode root = getGeneTree().getRoot();
336 // if ( ( root.getChildNode1() == prev_root_c1 )
337 // || ( root.getChildNode2() == prev_root_c1 ) ) {
338 // calculateMforNode( prev_root_c1 );
341 // calculateMforNode( prev_root_c2 );
343 // Event event = null;
344 // if ( prev_root_was_dup ) {
345 // event = Event.createSingleDuplicationEvent();
348 // event = Event.createSingleSpeciationEvent();
350 // root.getPhylogenyNodeData().setEvent( event );
351 // calculateMforNode( root );
352 // return getDuplications();
353 // } // updateM( boolean, PhylogenyNode, PhylogenyNode )
354 // Helper method for updateM( boolean, PhylogenyNode, PhylogenyNode )
355 // Calculates M for PhylogenyNode n, given that M for the two children
356 // of n has been calculated.
357 // (Last modified: 10/02/01)
358 // private void calculateMforNode( final PhylogenyNode n ) {
359 // if ( !n.isExternal() ) {
360 // boolean was_duplication = n.isDuplication();
361 // PhylogenyNode a = n.getChildNode1().getLink(), b = n
362 // .getChildNode2().getLink();
363 // while ( a != b ) {
364 // if ( a.getID() > b.getID() ) {
365 // a = a.getParent();
368 // b = b.getParent();
372 // Event event = null;
373 // if ( ( a == n.getChildNode1().getLink() )
374 // || ( a == n.getChildNode2().getLink() ) ) {
375 // event = Event.createSingleDuplicationEvent();
376 // if ( !was_duplication ) {
377 // increaseDuplications();
381 // event = Event.createSingleSpeciationEvent();
382 // if ( was_duplication ) {
383 // decreaseDuplications();
386 // n.getPhylogenyNodeData().setEvent( event );
388 // } // calculateMforNode( PhylogenyNode )
389 } // End of class GSDI.