big refactoring (moving of methods)
[jalview.git] / forester / java / src / org / forester / sdi / GSDI.java
1 // $Id:
2 // FORESTER -- software libraries and applications
3 // for evolutionary biology research and applications.
4 //
5 // Copyright (C) 2008-2009 Christian M. Zmasek
6 // Copyright (C) 2008-2009 Burnham Institute for Medical Research
7 // All rights reserved
8 //
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.
13 //
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.
18 //
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
22 //
23 // Contact: phylosoft @ gmail . com
24 // WWW: www.phylosoft.org/forester
25
26 package org.forester.sdi;
27
28 import java.util.HashMap;
29
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;
36
37 /*
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
46  * greater.
47  * 
48  * @see SDI#linkNodesOfG()
49  * 
50  * @see Phylogeny#preorderReID(int)
51  * 
52  * @see
53  * PhylogenyMethods#taxonomyBasedDeletionOfExternalNodes(Phylogeny,Phylogeny)
54  * 
55  * @see #geneTreePostOrderTraversal(PhylogenyNode)
56  * 
57  * @author Christian M. Zmasek
58  */
59 public class GSDI extends SDI {
60
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;
65
66     /**
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()".
73      * <p>
74      * 
75      * @see #infer(boolean)
76      * @see SDI#computeMappingCostL()
77      * @param gene_tree
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
81      * @param species_tree
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
85      * 
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
89      *            species tree.
90      * 
91      */
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;
97         _speciations_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();
102         linkNodesOfG();
103         geneTreePostOrderTraversal( getGeneTree().getRoot() );
104     }
105
106     private Event createDuplicationEvent() {
107         final Event event = Event.createSingleDuplicationEvent();
108         ++_duplications_sum;
109         return event;
110     }
111
112     private Event createSingleSpeciationOrDuplicationEvent() {
113         final Event event = Event.createSingleSpeciationOrDuplicationEvent();
114         ++_speciation_or_duplication_events_sum;
115         return event;
116     }
117
118     private Event createSpeciationEvent() {
119         final Event event = Event.createSingleSpeciationEvent();
120         ++_speciations_sum;
121         return event;
122     }
123
124     // s is the node on the species tree g maps to.
125     private void determineEvent( final PhylogenyNode s, final PhylogenyNode g ) {
126         Event event = null;
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;
132             }
133         }
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;
146                 }
147             }
148         }
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();
155             }
156             else if ( traversals_sum > 2 ) {
157                 if ( max_traversals <= 1 ) {
158                     if ( _most_parsimonious_duplication_model ) {
159                         event = createSpeciationEvent();
160                     }
161                     else {
162                         event = createSingleSpeciationOrDuplicationEvent();
163                     }
164                 }
165                 else {
166                     event = createDuplicationEvent();
167                     _transversal_counts.put( max_traversals_node, 1 );
168                 }
169             }
170             else {
171                 event = createDuplicationEvent();
172             }
173         }
174         else {
175             event = createSpeciationEvent();
176         }
177         g.getNodeData().setEvent( event );
178     }
179
180     /**
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.
184      * <p>
185      * Preconditions: Mapping M for external nodes must have been calculated and
186      * the species tree must be labeled in preorder.
187      * <p>
188      * (Last modified: )
189      * 
190      * @param g
191      *            starting node of a gene tree - normally the root
192      */
193     void geneTreePostOrderTraversal( final PhylogenyNode g ) {
194         if ( !g.isExternal() ) {
195             for( final PhylogenyNodeIterator iter = g.iterateChildNodesForward(); iter.hasNext(); ) {
196                 geneTreePostOrderTraversal( iter.next() );
197             }
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();
201             }
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 ];
212             }
213             final PhylogenyNode s = linked_nodes[ max_i ];
214             g.setLink( s );
215             // Determines whether dup. or spec.
216             determineEvent( s, g );
217             // _transversal_counts.clear();
218         }
219     }
220
221     public int getSpeciationOrDuplicationEventsSum() {
222         return _speciation_or_duplication_events_sum;
223     }
224
225     public int getSpeciationsSum() {
226         return _speciations_sum;
227     }
228
229     private int getTraversalCount( final PhylogenyNode node ) {
230         if ( _transversal_counts.containsKey( node ) ) {
231             return _transversal_counts.get( node );
232         }
233         return 0;
234     }
235
236     private void increaseTraversalCount( final PhylogenyNode node ) {
237         if ( _transversal_counts.containsKey( node ) ) {
238             _transversal_counts.put( node, _transversal_counts.get( node ) + 1 );
239         }
240         else {
241             _transversal_counts.put( node, 1 );
242         }
243         // System.out.println( "count for node " + node.getID() + " is now "
244         // + getTraversalCount( node ) );
245     }
246
247     /**
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.
250      * 
251      */
252     @Override
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" );
261                 }
262                 speciestree_ext_nodes.put( n.getNodeData().getTaxonomy(), n );
263             }
264         }
265         // Retrieve the reference to the PhylogenyNode with a matching species
266         // name.
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" );
271             }
272             final PhylogenyNode s = speciestree_ext_nodes.get( g.getNodeData().getTaxonomy() );
273             if ( s == null ) {
274                 throw new IllegalArgumentException( "species " + g.getNodeData().getTaxonomy()
275                         + " not present in species tree." );
276             }
277             g.setLink( s );
278         }
279     }
280
281     @Override
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() );
293         }
294         sb.append( "mapping cost L                     : " + computeMappingCostL() );
295         return sb.toString();
296     }
297
298     static int[] obtainMinMaxIdIndices( final PhylogenyNode[] linked_nodes ) {
299         int max_i = 0;
300         int min_i = 0;
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 ) {
306                 max_i = i;
307                 max_i_id = linked_nodes[ max_i ].getId();
308             }
309             if ( id_i < min_i_id ) {
310                 min_i = i;
311                 min_i_id = linked_nodes[ min_i ].getId();
312             }
313         }
314         return new int[] { min_i, max_i };
315     }
316     /**
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.
320      * <p>
321      * To be used ONLY by method "SDIunrooted.fastInfer(Phylogeny,Phylogeny)".
322      * <p>
323      * (Last modfied: )
324      * 
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
332      */
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 );
339     // }
340     // else {
341     // calculateMforNode( prev_root_c2 );
342     // }
343     // Event event = null;
344     // if ( prev_root_was_dup ) {
345     // event = Event.createSingleDuplicationEvent();
346     // }
347     // else {
348     // event = Event.createSingleSpeciationEvent();
349     // }
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();
366     // }
367     // else {
368     // b = b.getParent();
369     // }
370     // }
371     // n.setLink( a );
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();
378     // }
379     // }
380     // else {
381     // event = Event.createSingleSpeciationEvent();
382     // if ( was_duplication ) {
383     // decreaseDuplications();
384     // }
385     // }
386     // n.getPhylogenyNodeData().setEvent( event );
387     // }
388     // } // calculateMforNode( PhylogenyNode )
389 } // End of class GSDI.