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.ArrayList;
29 import java.util.HashMap;
30 import java.util.HashSet;
31 import java.util.List;
34 import java.util.SortedSet;
35 import java.util.TreeSet;
37 import org.forester.phylogeny.Phylogeny;
38 import org.forester.phylogeny.PhylogenyNode;
39 import org.forester.phylogeny.data.Event;
40 import org.forester.phylogeny.data.Taxonomy;
41 import org.forester.phylogeny.iterators.PhylogenyNodeIterator;
42 import org.forester.util.ForesterUtil;
45 * Implements our algorithm for speciation - duplication inference (SDI). <p>
46 * The initialization is accomplished by: </p> <ul> <li>method
47 * "linkExtNodesOfG()" of class SDI: setting the links for the external nodes of
48 * the gene tree <li>"preorderReID(int)" from class Phylogeny: numbering of
49 * nodes of the species tree in preorder <li>the optional stripping of the
50 * species tree is accomplished by method "stripTree(Phylogeny,Phylogeny)" of
51 * class Phylogeny </ul> <p> The recursion part is accomplished by this class'
52 * method "geneTreePostOrderTraversal(PhylogenyNode)". <p> Requires JDK 1.5 or
55 * @see SDI#linkNodesOfG()
57 * @see Phylogeny#preorderReID(int)
60 * PhylogenyMethods#taxonomyBasedDeletionOfExternalNodes(Phylogeny,Phylogeny)
62 * @see #geneTreePostOrderTraversal(PhylogenyNode)
64 * @author Christian M. Zmasek
66 public final class GSDI extends SDI {
68 private final HashMap<PhylogenyNode, Integer> _transversal_counts;
69 private final boolean _most_parsimonious_duplication_model;
70 private final boolean _strip_gene_tree;
71 private final boolean _strip_species_tree;
72 private int _speciation_or_duplication_events_sum;
73 private int _speciations_sum;
74 private final List<PhylogenyNode> _stripped_gene_tree_nodes;
75 private final List<PhylogenyNode> _stripped_species_tree_nodes;
76 private final SortedSet<PhylogenyNode> _mapped_species_tree_nodes;
79 * Constructor which sets the gene tree and the species tree to be compared.
80 * species_tree is the species tree to which the gene tree gene_tree will be
81 * compared to - with method "infer(boolean)". Both Trees must be completely
82 * binary and rooted. The actual inference is accomplished with method
83 * "infer(boolean)". The mapping cost L can then be calculated with method
84 * "computeMappingCost()".
87 * @see #infer(boolean)
88 * @see SDI#computeMappingCostL()
90 * reference to a rooted gene tree to which assign duplication vs
91 * speciation, must have species names in the species name fields
92 * for all external nodes
94 * reference to a rooted binary species tree which might get
95 * stripped in the process, must have species names in the
96 * species name fields for all external nodes
98 * @param most_parsimonious_duplication_model
99 * set to true to assign nodes as speciations which would
100 * otherwise be assiged as unknown because of polytomies in the
102 * @throws SdiException
105 public GSDI( final Phylogeny gene_tree,
106 final Phylogeny species_tree,
107 final boolean most_parsimonious_duplication_model,
108 final boolean strip_gene_tree,
109 final boolean strip_species_tree ) throws SdiException {
110 super( gene_tree, species_tree );
111 _speciation_or_duplication_events_sum = 0;
112 _speciations_sum = 0;
113 _most_parsimonious_duplication_model = most_parsimonious_duplication_model;
114 _transversal_counts = new HashMap<PhylogenyNode, Integer>();
115 _duplications_sum = 0;
116 _strip_gene_tree = strip_gene_tree;
117 _strip_species_tree = strip_species_tree;
118 _stripped_gene_tree_nodes = new ArrayList<PhylogenyNode>();
119 _stripped_species_tree_nodes = new ArrayList<PhylogenyNode>();
120 _mapped_species_tree_nodes = new TreeSet<PhylogenyNode>();
121 getSpeciesTree().preOrderReId();
123 geneTreePostOrderTraversal( getGeneTree().getRoot() );
126 GSDI( final Phylogeny gene_tree, final Phylogeny species_tree, final boolean most_parsimonious_duplication_model )
127 throws SdiException {
128 this( gene_tree, species_tree, most_parsimonious_duplication_model, false, false );
131 private final Event createDuplicationEvent() {
132 final Event event = Event.createSingleDuplicationEvent();
137 private final Event createSingleSpeciationOrDuplicationEvent() {
138 final Event event = Event.createSingleSpeciationOrDuplicationEvent();
139 ++_speciation_or_duplication_events_sum;
143 private final Event createSpeciationEvent() {
144 final Event event = Event.createSingleSpeciationEvent();
149 // s is the node on the species tree g maps to.
150 private final void determineEvent( final PhylogenyNode s, final PhylogenyNode g ) {
152 // Determine how many children map to same node as parent.
153 int sum_g_childs_mapping_to_s = 0;
154 for( final PhylogenyNodeIterator iter = g.iterateChildNodesForward(); iter.hasNext(); ) {
155 if ( iter.next().getLink() == s ) {
156 ++sum_g_childs_mapping_to_s;
159 // Determine the sum of traversals.
160 int traversals_sum = 0;
161 int max_traversals = 0;
162 PhylogenyNode max_traversals_node = null;
163 if ( !s.isExternal() ) {
164 for( final PhylogenyNodeIterator iter = s.iterateChildNodesForward(); iter.hasNext(); ) {
165 final PhylogenyNode current_node = iter.next();
166 final int traversals = getTraversalCount( current_node );
167 traversals_sum += traversals;
168 if ( traversals > max_traversals ) {
169 max_traversals = traversals;
170 max_traversals_node = current_node;
174 // System.out.println( " sum=" + traversals_sum );
175 // System.out.println( " max=" + max_traversals );
176 // System.out.println( " m=" + sum_g_childs_mapping_to_s );
177 if ( sum_g_childs_mapping_to_s > 0 ) {
178 if ( traversals_sum == 2 ) {
179 event = createDuplicationEvent();
181 else if ( traversals_sum > 2 ) {
182 if ( max_traversals <= 1 ) {
183 if ( _most_parsimonious_duplication_model ) {
184 event = createSpeciationEvent();
187 event = createSingleSpeciationOrDuplicationEvent();
191 event = createDuplicationEvent();
192 _transversal_counts.put( max_traversals_node, 1 );
196 event = createDuplicationEvent();
200 event = createSpeciationEvent();
202 g.getNodeData().setEvent( event );
206 * Traverses the subtree of PhylogenyNode g in postorder, calculating the
207 * mapping function M, and determines which nodes represent speciation
208 * events and which ones duplication events.
210 * Preconditions: Mapping M for external nodes must have been calculated and
211 * the species tree must be labeled in preorder.
215 * starting node of a gene tree - normally the root
217 final void geneTreePostOrderTraversal( final PhylogenyNode g ) {
218 if ( !g.isExternal() ) {
219 for( final PhylogenyNodeIterator iter = g.iterateChildNodesForward(); iter.hasNext(); ) {
220 geneTreePostOrderTraversal( iter.next() );
222 final PhylogenyNode[] linked_nodes = new PhylogenyNode[ g.getNumberOfDescendants() ];
223 for( int i = 0; i < linked_nodes.length; ++i ) {
224 if ( g.getChildNode( i ).getLink() == null ) {
225 System.out.println( "link is null for " + g.getChildNode( i ) );
228 linked_nodes[ i ] = g.getChildNode( i ).getLink();
230 final int[] min_max = obtainMinMaxIdIndices( linked_nodes );
231 int min_i = min_max[ 0 ];
232 int max_i = min_max[ 1 ];
233 // initTransversalCounts();
234 while ( linked_nodes[ min_i ] != linked_nodes[ max_i ] ) {
235 increaseTraversalCount( linked_nodes[ max_i ] );
236 linked_nodes[ max_i ] = linked_nodes[ max_i ].getParent();
237 final int[] min_max_ = obtainMinMaxIdIndices( linked_nodes );
238 min_i = min_max_[ 0 ];
239 max_i = min_max_[ 1 ];
241 final PhylogenyNode s = linked_nodes[ max_i ];
243 // Determines whether dup. or spec.
244 determineEvent( s, g );
245 // _transversal_counts.clear();
249 public final int getSpeciationOrDuplicationEventsSum() {
250 return _speciation_or_duplication_events_sum;
253 public final int getSpeciationsSum() {
254 return _speciations_sum;
257 private final int getTraversalCount( final PhylogenyNode node ) {
258 if ( _transversal_counts.containsKey( node ) ) {
259 return _transversal_counts.get( node );
264 private final void increaseTraversalCount( final PhylogenyNode node ) {
265 if ( _transversal_counts.containsKey( node ) ) {
266 _transversal_counts.put( node, _transversal_counts.get( node ) + 1 );
269 _transversal_counts.put( node, 1 );
271 // System.out.println( "count for node " + node.getID() + " is now "
272 // + getTraversalCount( node ) );
276 * This allows for linking of internal nodes of the species tree (as opposed
277 * to just external nodes, as in the method it overrides.
278 * @throws SdiException
282 // final void linkNodesOfG() {
283 // final HashMap<Taxonomy, PhylogenyNode> speciestree_ext_nodes = createTaxonomyToNodeMap();
284 // if ( _strip_gene_tree ) {
285 // stripGeneTree( speciestree_ext_nodes );
286 // if ( ( _gene_tree == null ) || ( _gene_tree.getNumberOfExternalNodes() < 2 ) ) {
287 // throw new IllegalArgumentException( "species tree does not contain any"
288 // + " nodes matching species in the gene tree" );
291 // // Retrieve the reference to the PhylogenyNode with a matching species.
292 // for( final PhylogenyNodeIterator iter = _gene_tree.iteratorExternalForward(); iter.hasNext(); ) {
293 // final PhylogenyNode g = iter.next();
294 // if ( !g.getNodeData().isHasTaxonomy() ) {
295 // throw new IllegalArgumentException( "gene tree node " + g + " has no taxonomic data" );
297 // final PhylogenyNode s = speciestree_ext_nodes.get( g.getNodeData().getTaxonomy() );
298 // if ( s == null ) {
299 // throw new IllegalArgumentException( "species " + g.getNodeData().getTaxonomy()
300 // + " not present in species tree" );
305 final void linkNodesOfG() throws SdiException {
306 final Map<String, PhylogenyNode> species_to_node_map = new HashMap<String, PhylogenyNode>();
307 final Set<PhylogenyNode> species_tree_ext_nodes = new HashSet<PhylogenyNode>();
308 final TaxonomyComparisonBase tax_comp_base = determineTaxonomyComparisonBase( _gene_tree );
309 System.out.println( "comp base is: " + tax_comp_base );
310 // Stringyfied taxonomy is the key, node is the value.
311 for( final PhylogenyNodeIterator iter = _species_tree.iteratorExternalForward(); iter.hasNext(); ) {
312 final PhylogenyNode s = iter.next();
313 species_tree_ext_nodes.add( s );
314 final String tax_str = taxonomyToString( s, tax_comp_base );
315 if ( !ForesterUtil.isEmpty( tax_str ) ) {
316 if ( species_to_node_map.containsKey( tax_str ) ) {
317 throw new SdiException( "taxonomy \"" + s + "\" is not unique in species tree" );
319 species_to_node_map.put( tax_str, s );
322 // Retrieve the reference to the node with a matching stringyfied taxonomy.
323 for( final PhylogenyNodeIterator iter = _gene_tree.iteratorExternalForward(); iter.hasNext(); ) {
324 final PhylogenyNode g = iter.next();
325 if ( !g.getNodeData().isHasTaxonomy() ) {
326 if ( _strip_gene_tree ) {
327 _stripped_gene_tree_nodes.add( g );
330 throw new SdiException( "gene tree node \"" + g + "\" has no taxonomic data" );
334 final String tax_str = taxonomyToString( g, tax_comp_base );
335 if ( ForesterUtil.isEmpty( tax_str ) ) {
336 if ( _strip_gene_tree ) {
337 _stripped_gene_tree_nodes.add( g );
340 throw new SdiException( "gene tree node \"" + g + "\" has no appropriate taxonomic data" );
344 final PhylogenyNode s = species_to_node_map.get( tax_str );
346 if ( _strip_gene_tree ) {
347 _stripped_gene_tree_nodes.add( g );
350 throw new SdiException( "taxonomy \"" + g.getNodeData().getTaxonomy()
351 + "\" not present in species tree" );
356 _mapped_species_tree_nodes.add( s );
357 System.out.println( "setting link of " + g + " to " + s );
362 if ( _strip_gene_tree ) {
363 for( final PhylogenyNode g : _stripped_gene_tree_nodes ) {
364 _gene_tree.deleteSubtree( g, true );
367 if ( _strip_species_tree ) {
368 for( final PhylogenyNode s : species_tree_ext_nodes ) {
369 if ( !_mapped_species_tree_nodes.contains( s ) ) {
370 _species_tree.deleteSubtree( s, true );
376 public SortedSet<PhylogenyNode> getMappedExternalSpeciesTreeNodes() {
377 return _mapped_species_tree_nodes;
380 // final private HashMap<Taxonomy, PhylogenyNode> createTaxonomyToNodeMap() {
381 // final HashMap<Taxonomy, PhylogenyNode> speciestree_ext_nodes = new HashMap<Taxonomy, PhylogenyNode>();
382 // for( final PhylogenyNodeIterator iter = _species_tree.iteratorLevelOrder(); iter.hasNext(); ) {
383 // final PhylogenyNode n = iter.next();
384 // if ( n.getNodeData().isHasTaxonomy() ) {
385 // if ( speciestree_ext_nodes.containsKey( n.getNodeData().getTaxonomy() ) ) {
386 // throw new IllegalArgumentException( "taxonomy [" + n.getNodeData().getTaxonomy()
387 // + "] is not unique in species phylogeny" );
389 // speciestree_ext_nodes.put( n.getNodeData().getTaxonomy(), n );
392 // return speciestree_ext_nodes;
394 // private final void stripGeneTree( final HashMap<Taxonomy, PhylogenyNode> speciestree_ext_nodes ) {
395 // // final Set<PhylogenyNode> to_delete = new HashSet<PhylogenyNode>();
396 // for( final PhylogenyNodeIterator iter = _gene_tree.iteratorExternalForward(); iter.hasNext(); ) {
397 // final PhylogenyNode g = iter.next();
398 // if ( !g.getNodeData().isHasTaxonomy() ) {
399 // throw new IllegalArgumentException( "gene tree node " + g + " has no taxonomic data" );
401 // if ( !speciestree_ext_nodes.containsKey( g.getNodeData().getTaxonomy() ) ) {
402 // _stripped_gene_tree_nodes.add( g );
405 // for( final PhylogenyNode n : _stripped_gene_tree_nodes ) {
406 // _gene_tree.deleteSubtree( n, true );
409 // private final void stripGeneTree2( final HashMap<Taxonomy, PhylogenyNode> speciestree_ext_nodes ) {
410 // // final Set<PhylogenyNode> to_delete = new HashSet<PhylogenyNode>();
411 // for( final PhylogenyNodeIterator iter = _gene_tree.iteratorExternalForward(); iter.hasNext(); ) {
412 // final PhylogenyNode g = iter.next();
413 // if ( !g.getNodeData().isHasTaxonomy() ) {
414 // _stripped_gene_tree_nodes.add( g );
417 // if ( !speciestree_ext_nodes.containsKey( g.getNodeData().getTaxonomy() ) ) {
418 // _stripped_gene_tree_nodes.add( g );
422 // for( final PhylogenyNode n : _stripped_gene_tree_nodes ) {
423 // _gene_tree.deleteSubtree( n, true );
426 public static TaxonomyComparisonBase determineTaxonomyComparisonBase( final Phylogeny gene_tree ) {
427 int with_id_count = 0;
428 int with_code_count = 0;
429 int with_sn_count = 0;
431 for( final PhylogenyNodeIterator iter = gene_tree.iteratorExternalForward(); iter.hasNext(); ) {
432 final PhylogenyNode g = iter.next();
433 if ( g.getNodeData().isHasTaxonomy() ) {
434 final Taxonomy tax = g.getNodeData().getTaxonomy();
435 if ( ( tax.getIdentifier() != null ) && !ForesterUtil.isEmpty( tax.getIdentifier().getValue() ) ) {
436 if ( ++with_id_count > max ) {
440 if ( !ForesterUtil.isEmpty( tax.getTaxonomyCode() ) ) {
441 if ( ++with_code_count > max ) {
442 max = with_code_count;
445 if ( !ForesterUtil.isEmpty( tax.getScientificName() ) ) {
446 if ( ++with_sn_count > max ) {
453 throw new IllegalArgumentException( "gene tree has no taxonomic data" );
455 else if ( max == 1 ) {
456 throw new IllegalArgumentException( "gene tree has only one node with taxonomic data" );
458 else if ( max == with_sn_count ) {
459 return SDI.TaxonomyComparisonBase.SCIENTIFIC_NAME;
461 else if ( max == with_id_count ) {
462 return SDI.TaxonomyComparisonBase.ID;
465 return SDI.TaxonomyComparisonBase.CODE;
469 public List<PhylogenyNode> getStrippedExternalGeneTreeNodes() {
470 return _stripped_gene_tree_nodes;
474 public final String toString() {
475 final StringBuffer sb = new StringBuffer();
476 sb.append( "Most parsimonious duplication model: " + _most_parsimonious_duplication_model );
477 sb.append( ForesterUtil.getLineSeparator() );
478 sb.append( "Speciations sum : " + getSpeciationsSum() );
479 sb.append( ForesterUtil.getLineSeparator() );
480 sb.append( "Duplications sum : " + getDuplicationsSum() );
481 sb.append( ForesterUtil.getLineSeparator() );
482 if ( !_most_parsimonious_duplication_model ) {
483 sb.append( "Speciation or duplications sum : " + getSpeciationOrDuplicationEventsSum() );
484 sb.append( ForesterUtil.getLineSeparator() );
486 sb.append( "mapping cost L : " + computeMappingCostL() );
487 return sb.toString();
490 static final int[] obtainMinMaxIdIndices( final PhylogenyNode[] linked_nodes ) {
493 int max_i_id = -Integer.MAX_VALUE;
494 int min_i_id = Integer.MAX_VALUE;
495 for( int i = 0; i < linked_nodes.length; ++i ) {
496 final int id_i = linked_nodes[ i ].getId();
497 if ( id_i > max_i_id ) {
499 max_i_id = linked_nodes[ max_i ].getId();
501 if ( id_i < min_i_id ) {
503 min_i_id = linked_nodes[ min_i ].getId();
506 return new int[] { min_i, max_i };
509 * Updates the mapping function M after the root of the gene tree has been
510 * moved by one branch. It calculates M for the root of the gene tree and
511 * one of its two children.
513 * To be used ONLY by method "SDIunrooted.fastInfer(Phylogeny,Phylogeny)".
517 * @param prev_root_was_dup
518 * true if the previous root was a duplication, false otherwise
519 * @param prev_root_c1
520 * child 1 of the previous root
521 * @param prev_root_c2
522 * child 2 of the previous root
523 * @return number of duplications which have been assigned in gene tree
525 // int updateM( final boolean prev_root_was_dup,
526 // final PhylogenyNode prev_root_c1, final PhylogenyNode prev_root_c2 ) {
527 // final PhylogenyNode root = getGeneTree().getRoot();
528 // if ( ( root.getChildNode1() == prev_root_c1 )
529 // || ( root.getChildNode2() == prev_root_c1 ) ) {
530 // calculateMforNode( prev_root_c1 );
533 // calculateMforNode( prev_root_c2 );
535 // Event event = null;
536 // if ( prev_root_was_dup ) {
537 // event = Event.createSingleDuplicationEvent();
540 // event = Event.createSingleSpeciationEvent();
542 // root.getPhylogenyNodeData().setEvent( event );
543 // calculateMforNode( root );
544 // return getDuplications();
545 // } // updateM( boolean, PhylogenyNode, PhylogenyNode )
546 // Helper method for updateM( boolean, PhylogenyNode, PhylogenyNode )
547 // Calculates M for PhylogenyNode n, given that M for the two children
548 // of n has been calculated.
549 // (Last modified: 10/02/01)
550 // private void calculateMforNode( final PhylogenyNode n ) {
551 // if ( !n.isExternal() ) {
552 // boolean was_duplication = n.isDuplication();
553 // PhylogenyNode a = n.getChildNode1().getLink(), b = n
554 // .getChildNode2().getLink();
555 // while ( a != b ) {
556 // if ( a.getID() > b.getID() ) {
557 // a = a.getParent();
560 // b = b.getParent();
564 // Event event = null;
565 // if ( ( a == n.getChildNode1().getLink() )
566 // || ( a == n.getChildNode2().getLink() ) ) {
567 // event = Event.createSingleDuplicationEvent();
568 // if ( !was_duplication ) {
569 // increaseDuplications();
573 // event = Event.createSingleSpeciationEvent();
574 // if ( was_duplication ) {
575 // decreaseDuplications();
578 // n.getPhylogenyNodeData().setEvent( event );
580 // } // calculateMforNode( PhylogenyNode )