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;
35 import org.forester.phylogeny.Phylogeny;
36 import org.forester.phylogeny.PhylogenyNode;
37 import org.forester.phylogeny.data.Event;
38 import org.forester.phylogeny.data.Taxonomy;
39 import org.forester.phylogeny.iterators.PhylogenyNodeIterator;
40 import org.forester.util.ForesterUtil;
43 * Implements our algorithm for speciation - duplication inference (SDI). <p>
44 * The initialization is accomplished by: </p> <ul> <li>method
45 * "linkExtNodesOfG()" of class SDI: setting the links for the external nodes of
46 * the gene tree <li>"preorderReID(int)" from class Phylogeny: numbering of
47 * nodes of the species tree in preorder <li>the optional stripping of the
48 * species tree is accomplished by method "stripTree(Phylogeny,Phylogeny)" of
49 * class Phylogeny </ul> <p> The recursion part is accomplished by this class'
50 * method "geneTreePostOrderTraversal(PhylogenyNode)". <p> Requires JDK 1.5 or
53 * @see SDI#linkNodesOfG()
55 * @see Phylogeny#preorderReID(int)
58 * PhylogenyMethods#taxonomyBasedDeletionOfExternalNodes(Phylogeny,Phylogeny)
60 * @see #geneTreePostOrderTraversal(PhylogenyNode)
62 * @author Christian M. Zmasek
64 public final class GSDIold extends SDI {
66 private final HashMap<PhylogenyNode, Integer> _transversal_counts;
67 private final boolean _most_parsimonious_duplication_model;
68 private final boolean _strip_gene_tree;
69 private final boolean _strip_species_tree;
70 private int _speciation_or_duplication_events_sum;
71 private int _speciations_sum;
72 private final List<PhylogenyNode> _stripped_gene_tree_nodes;
73 private final List<PhylogenyNode> _stripped_species_tree_nodes;
74 private final Set<PhylogenyNode> _mapped_species_tree_nodes;
77 * Constructor which sets the gene tree and the species tree to be compared.
78 * species_tree is the species tree to which the gene tree gene_tree will be
79 * compared to - with method "infer(boolean)". Both Trees must be completely
80 * binary and rooted. The actual inference is accomplished with method
81 * "infer(boolean)". The mapping cost L can then be calculated with method
82 * "computeMappingCost()".
85 * @see #infer(boolean)
86 * @see SDI#computeMappingCostL()
88 * reference to a rooted gene tree to which assign duplication vs
89 * speciation, must have species names in the species name fields
90 * for all external nodes
92 * reference to a rooted binary species tree which might get
93 * stripped in the process, must have species names in the
94 * species name fields for all external nodes
96 * @param most_parsimonious_duplication_model
97 * set to true to assign nodes as speciations which would
98 * otherwise be assiged as unknown because of polytomies in the
100 * @throws SdiException
103 public GSDIold( final Phylogeny gene_tree,
104 final Phylogeny species_tree,
105 final boolean most_parsimonious_duplication_model,
106 final boolean strip_gene_tree,
107 final boolean strip_species_tree ) throws SdiException {
108 super( gene_tree, species_tree );
109 _speciation_or_duplication_events_sum = 0;
110 _speciations_sum = 0;
111 _most_parsimonious_duplication_model = most_parsimonious_duplication_model;
112 _transversal_counts = new HashMap<PhylogenyNode, Integer>();
113 _duplications_sum = 0;
114 _strip_gene_tree = strip_gene_tree;
115 _strip_species_tree = strip_species_tree;
116 _stripped_gene_tree_nodes = new ArrayList<PhylogenyNode>();
117 _stripped_species_tree_nodes = new ArrayList<PhylogenyNode>();
118 _mapped_species_tree_nodes = new HashSet<PhylogenyNode>();
119 getSpeciesTree().preOrderReId();
121 //geneTreePostOrderTraversal( getGeneTree().getRoot(), null );
122 geneTreePostOrderTraversal2();
125 GSDIold( final Phylogeny gene_tree, final Phylogeny species_tree, final boolean most_parsimonious_duplication_model )
126 throws SdiException {
127 this( gene_tree, species_tree, most_parsimonious_duplication_model, false, false );
130 // s is the node on the species tree g maps to.
131 private final void determineEvent( final PhylogenyNode s, final PhylogenyNode g ) {
133 // Determine how many children map to same node as parent.
134 int sum_g_childs_mapping_to_s = 0;
135 for( int i = 0; i < g.getNumberOfDescendants(); ++i ) {
136 final PhylogenyNode c = g.getChildNode( i );
137 if ( c.getLink() == s ) {
138 ++sum_g_childs_mapping_to_s;
141 // Determine the sum of traversals.
142 int traversals_sum = 0;
143 int max_traversals = 0;
144 PhylogenyNode max_traversals_node = null;
145 if ( !s.isExternal() ) {
146 for( int i = 0; i < s.getNumberOfDescendants(); ++i ) {
147 final PhylogenyNode current_node = s.getChildNode( i );
148 final int traversals = getTraversalCount( current_node );
149 traversals_sum += traversals;
150 if ( traversals > max_traversals ) {
151 max_traversals = traversals;
152 max_traversals_node = current_node;
156 // System.out.println( " sum=" + traversals_sum );
157 // System.out.println( " max=" + max_traversals );
158 // System.out.println( " m=" + sum_g_childs_mapping_to_s );
159 if ( sum_g_childs_mapping_to_s > 0 ) {
160 if ( traversals_sum == 2 ) {
161 event = createDuplicationEvent();
162 System.out.print( g.toString() );
163 System.out.println( " : ==2" );
164 // _transversal_counts.clear();
166 else if ( traversals_sum > 2 ) {
167 if ( max_traversals <= 1 ) {
168 if ( _most_parsimonious_duplication_model ) {
169 event = createSpeciationEvent();
172 event = createSingleSpeciationOrDuplicationEvent();
176 event = createDuplicationEvent();
177 // normalizeTcounts( s );
178 //System.out.println( g.toString() );
179 _transversal_counts.put( max_traversals_node, 1 );
180 // _transversal_counts.clear();
184 event = createDuplicationEvent();
185 // _transversal_counts.clear();
187 //normalizeTcounts( s );
190 event = createSpeciationEvent();
192 g.getNodeData().setEvent( event );
195 // private final void determineEvent2( final PhylogenyNode s, final PhylogenyNode g ) {
196 // Event event = null;
197 // // Determine how many children map to same node as parent.
198 // int sum_g_childs_mapping_to_s = 0;
199 // for( int i = 0; i < g.getNumberOfDescendants(); ++i ) {
200 // final PhylogenyNode c = g.getChildNode( i );
201 // if ( c.getLink() == s ) {
202 // ++sum_g_childs_mapping_to_s;
205 // // Determine the sum of traversals.
206 // int traversals_sum = 0;
207 // int max_traversals = 0;
208 // PhylogenyNode max_traversals_node = null;
209 // if ( !s.isExternal() ) {
210 // for( int i = 0; i < s.getNumberOfDescendants(); ++i ) {
211 // final PhylogenyNode current_node = s.getChildNode( i );
212 // final int traversals = getTraversalCount( current_node );
213 // traversals_sum += traversals;
214 // if ( traversals > max_traversals ) {
215 // max_traversals = traversals;
216 // max_traversals_node = current_node;
220 // // System.out.println( " sum=" + traversals_sum );
221 // // System.out.println( " max=" + max_traversals );
222 // // System.out.println( " m=" + sum_g_childs_mapping_to_s );
223 // if ( s.getNumberOfDescendants() == 2 ) {
224 // if ( sum_g_childs_mapping_to_s == 0 ) {
225 // event = createSpeciationEvent();
228 // event = createDuplicationEvent();
232 // if ( sum_g_childs_mapping_to_s == 2 ) {
233 // event = createDuplicationEvent();
235 // else if ( sum_g_childs_mapping_to_s == 1 ) {
236 // event = createSingleSpeciationOrDuplicationEvent();
239 // event = createSpeciationEvent();
242 // g.getNodeData().setEvent( event );
244 final void geneTreePostOrderTraversal2() {
245 PhylogenyNode g = null;
246 for( PhylogenyNodeIterator it = getGeneTree().iteratorPostorder(); it.hasNext(); ) {
248 if ( !g.isExternal() ) {
249 for( PhylogenyNodeIterator itw = getGeneTree().iteratorPostorder(); it.hasNext(); ) {
250 PhylogenyNode gg = it.next();
256 catch ( SdiException e ) {
257 // TODO Auto-generated catch block
260 _transversal_counts.clear();
261 final PhylogenyNode[] linked_nodes = new PhylogenyNode[ g.getNumberOfDescendants() ];
262 for( int i = 0; i < linked_nodes.length; ++i ) {
263 if ( g.getChildNode( i ).getLink() == null ) {
264 System.out.println( "link is null for " + g.getChildNode( i ) );
267 linked_nodes[ i ] = g.getChildNode( i ).getLink();
269 final int[] min_max = obtainMinMaxIdIndices( linked_nodes );
270 int min_i = min_max[ 0 ];
271 int max_i = min_max[ 1 ];
272 // initTransversalCounts();
273 while ( linked_nodes[ min_i ] != linked_nodes[ max_i ] ) {
274 increaseTraversalCount( linked_nodes[ max_i ] );
275 // if ( linked_nodes[ max_i ].isExternal() ) {
276 // System.out.println( "i am ext!" );
277 // PhylogenyNode n = linked_nodes[ max_i ];
278 // while ( !n.isRoot() ) {
279 // _transversal_counts.put( n, 0 );
280 // n = n.getParent();
283 linked_nodes[ max_i ] = linked_nodes[ max_i ].getParent();
284 final int[] min_max_ = obtainMinMaxIdIndices( linked_nodes );
285 min_i = min_max_[ 0 ];
286 max_i = min_max_[ 1 ];
288 final PhylogenyNode s = linked_nodes[ max_i ];
290 // Determines whether dup. or spec.
291 determineEvent( s, g );
296 final void geneTreePostOrderTraversal() {
298 PhylogenyNode g = null;
299 for( PhylogenyNodeIterator it = getGeneTree().iteratorPostorder(); it.hasNext(); ) {
302 if ( !g.isExternal() ) {
303 final PhylogenyNode[] linked_nodes = new PhylogenyNode[ g.getNumberOfDescendants() ];
304 for( int i = 0; i < linked_nodes.length; ++i ) {
305 if ( g.getChildNode( i ).getLink() == null ) {
306 System.out.println( "link is null for " + g.getChildNode( i ) );
309 linked_nodes[ i ] = g.getChildNode( i ).getLink();
311 final int[] min_max = obtainMinMaxIdIndices( linked_nodes );
312 int min_i = min_max[ 0 ];
313 int max_i = min_max[ 1 ];
314 // initTransversalCounts();
315 while ( linked_nodes[ min_i ] != linked_nodes[ max_i ] ) {
316 increaseTraversalCount( linked_nodes[ max_i ] );
317 // if ( linked_nodes[ max_i ].isExternal() ) {
318 // System.out.println( "i am ext!" );
319 // PhylogenyNode n = linked_nodes[ max_i ];
320 // while ( !n.isRoot() ) {
321 // _transversal_counts.put( n, 0 );
322 // n = n.getParent();
325 linked_nodes[ max_i ] = linked_nodes[ max_i ].getParent();
326 final int[] min_max_ = obtainMinMaxIdIndices( linked_nodes );
327 min_i = min_max_[ 0 ];
328 max_i = min_max_[ 1 ];
330 final PhylogenyNode s = linked_nodes[ max_i ];
332 // Determines whether dup. or spec.
333 determineEvent( s, g );
335 System.out.println( "******" );
336 _transversal_counts.clear();
343 * Traverses the subtree of PhylogenyNode g in postorder, calculating the
344 * mapping function M, and determines which nodes represent speciation
345 * events and which ones duplication events.
347 * Preconditions: Mapping M for external nodes must have been calculated and
348 * the species tree must be labeled in preorder.
352 * starting node of a gene tree - normally the root
354 final void geneTreePostOrderTraversal( final PhylogenyNode g ) {
355 if ( !g.isExternal() ) {
356 for( int i = 0; i < g.getNumberOfDescendants(); ++i ) {
357 geneTreePostOrderTraversal( g.getChildNode( i ) );
359 final PhylogenyNode[] linked_nodes = new PhylogenyNode[ g.getNumberOfDescendants() ];
360 for( int i = 0; i < linked_nodes.length; ++i ) {
361 if ( g.getChildNode( i ).getLink() == null ) {
362 System.out.println( "link is null for " + g.getChildNode( i ) );
365 linked_nodes[ i ] = g.getChildNode( i ).getLink();
367 final int[] min_max = obtainMinMaxIdIndices( linked_nodes );
368 int min_i = min_max[ 0 ];
369 int max_i = min_max[ 1 ];
370 // initTransversalCounts();
371 while ( linked_nodes[ min_i ] != linked_nodes[ max_i ] ) {
372 increaseTraversalCount( linked_nodes[ max_i ] );
373 // if ( linked_nodes[ max_i ].isExternal() ) {
374 // System.out.println( "i am ext!" );
375 // PhylogenyNode n = linked_nodes[ max_i ];
376 // while ( !n.isRoot() ) {
377 // _transversal_counts.put( n, 0 );
378 // n = n.getParent();
381 linked_nodes[ max_i ] = linked_nodes[ max_i ].getParent();
382 final int[] min_max_ = obtainMinMaxIdIndices( linked_nodes );
383 min_i = min_max_[ 0 ];
384 max_i = min_max_[ 1 ];
386 final PhylogenyNode s = linked_nodes[ max_i ];
388 // Determines whether dup. or spec.
389 determineEvent( s, g );
393 private final Event createDuplicationEvent() {
394 final Event event = Event.createSingleDuplicationEvent();
399 private final Event createSingleSpeciationOrDuplicationEvent() {
400 final Event event = Event.createSingleSpeciationOrDuplicationEvent();
401 ++_speciation_or_duplication_events_sum;
405 private final Event createSpeciationEvent() {
406 final Event event = Event.createSingleSpeciationEvent();
411 public final int getSpeciationOrDuplicationEventsSum() {
412 return _speciation_or_duplication_events_sum;
415 public final int getSpeciationsSum() {
416 return _speciations_sum;
419 private final int getTraversalCount( final PhylogenyNode node ) {
420 if ( _transversal_counts.containsKey( node ) ) {
421 return _transversal_counts.get( node );
426 private final void increaseTraversalCount( final PhylogenyNode node ) {
427 if ( _transversal_counts.containsKey( node ) ) {
428 _transversal_counts.put( node, _transversal_counts.get( node ) + 1 );
431 _transversal_counts.put( node, 1 );
433 // System.out.println( "count for node " + node.getID() + " is now "
434 // + getTraversalCount( node ) );
437 private void normalizeTcounts( final PhylogenyNode s ) {
438 // int min_traversals = Integer.MAX_VALUE;
439 // for( int i = 0; i < s.getNumberOfDescendants(); ++i ) {
440 // final PhylogenyNode current_node = s.getChildNode( i );
441 // final int traversals = getTraversalCount( current_node );
442 // if ( traversals < min_traversals ) {
443 // min_traversals = traversals;
446 for( int i = 0; i < s.getNumberOfDescendants(); ++i ) {
447 final PhylogenyNode current_node = s.getChildNode( i );
448 // _transversal_counts.put( current_node, getTraversalCount( current_node ) - min_traversals );
449 if ( getTraversalCount( current_node ) > 1 ) {
450 _transversal_counts.put( current_node, 1 );
456 * This allows for linking of internal nodes of the species tree (as opposed
457 * to just external nodes, as in the method it overrides.
458 * @throws SdiException
462 // final void linkNodesOfG() {
463 // final HashMap<Taxonomy, PhylogenyNode> speciestree_ext_nodes = createTaxonomyToNodeMap();
464 // if ( _strip_gene_tree ) {
465 // stripGeneTree( speciestree_ext_nodes );
466 // if ( ( _gene_tree == null ) || ( _gene_tree.getNumberOfExternalNodes() < 2 ) ) {
467 // throw new IllegalArgumentException( "species tree does not contain any"
468 // + " nodes matching species in the gene tree" );
471 // // Retrieve the reference to the PhylogenyNode with a matching species.
472 // for( final PhylogenyNodeIterator iter = _gene_tree.iteratorExternalForward(); iter.hasNext(); ) {
473 // final PhylogenyNode g = iter.next();
474 // if ( !g.getNodeData().isHasTaxonomy() ) {
475 // throw new IllegalArgumentException( "gene tree node " + g + " has no taxonomic data" );
477 // final PhylogenyNode s = speciestree_ext_nodes.get( g.getNodeData().getTaxonomy() );
478 // if ( s == null ) {
479 // throw new IllegalArgumentException( "species " + g.getNodeData().getTaxonomy()
480 // + " not present in species tree" );
485 final void linkNodesOfG() throws SdiException {
486 final Map<String, PhylogenyNode> species_to_node_map = new HashMap<String, PhylogenyNode>();
487 final List<PhylogenyNode> species_tree_ext_nodes = new ArrayList<PhylogenyNode>();
488 final TaxonomyComparisonBase tax_comp_base = determineTaxonomyComparisonBase( _gene_tree );
489 // System.out.println( "comp base is: " + tax_comp_base );
490 // Stringyfied taxonomy is the key, node is the value.
491 for( final PhylogenyNodeIterator iter = _species_tree.iteratorExternalForward(); iter.hasNext(); ) {
492 final PhylogenyNode s = iter.next();
493 species_tree_ext_nodes.add( s );
494 final String tax_str = taxonomyToString( s, tax_comp_base );
495 if ( !ForesterUtil.isEmpty( tax_str ) ) {
496 if ( species_to_node_map.containsKey( tax_str ) ) {
497 throw new SdiException( "taxonomy \"" + s + "\" is not unique in species tree" );
499 species_to_node_map.put( tax_str, s );
502 // Retrieve the reference to the node with a matching stringyfied taxonomy.
503 for( final PhylogenyNodeIterator iter = _gene_tree.iteratorExternalForward(); iter.hasNext(); ) {
504 final PhylogenyNode g = iter.next();
505 if ( !g.getNodeData().isHasTaxonomy() ) {
506 if ( _strip_gene_tree ) {
507 _stripped_gene_tree_nodes.add( g );
510 throw new SdiException( "gene tree node \"" + g + "\" has no taxonomic data" );
514 final String tax_str = taxonomyToString( g, tax_comp_base );
515 if ( ForesterUtil.isEmpty( tax_str ) ) {
516 if ( _strip_gene_tree ) {
517 _stripped_gene_tree_nodes.add( g );
520 throw new SdiException( "gene tree node \"" + g + "\" has no appropriate taxonomic data" );
524 final PhylogenyNode s = species_to_node_map.get( tax_str );
526 if ( _strip_gene_tree ) {
527 _stripped_gene_tree_nodes.add( g );
530 throw new SdiException( "taxonomy \"" + g.getNodeData().getTaxonomy()
531 + "\" not present in species tree" );
536 _mapped_species_tree_nodes.add( s );
537 // System.out.println( "setting link of " + g + " to " + s );
542 if ( _strip_gene_tree ) {
543 for( final PhylogenyNode g : _stripped_gene_tree_nodes ) {
544 _gene_tree.deleteSubtree( g, true );
547 if ( _strip_species_tree ) {
548 for( final PhylogenyNode s : species_tree_ext_nodes ) {
549 if ( !_mapped_species_tree_nodes.contains( s ) ) {
550 _species_tree.deleteSubtree( s, true );
556 public Set<PhylogenyNode> getMappedExternalSpeciesTreeNodes() {
557 return _mapped_species_tree_nodes;
560 // final private HashMap<Taxonomy, PhylogenyNode> createTaxonomyToNodeMap() {
561 // final HashMap<Taxonomy, PhylogenyNode> speciestree_ext_nodes = new HashMap<Taxonomy, PhylogenyNode>();
562 // for( final PhylogenyNodeIterator iter = _species_tree.iteratorLevelOrder(); iter.hasNext(); ) {
563 // final PhylogenyNode n = iter.next();
564 // if ( n.getNodeData().isHasTaxonomy() ) {
565 // if ( speciestree_ext_nodes.containsKey( n.getNodeData().getTaxonomy() ) ) {
566 // throw new IllegalArgumentException( "taxonomy [" + n.getNodeData().getTaxonomy()
567 // + "] is not unique in species phylogeny" );
569 // speciestree_ext_nodes.put( n.getNodeData().getTaxonomy(), n );
572 // return speciestree_ext_nodes;
574 // private final void stripGeneTree( final HashMap<Taxonomy, PhylogenyNode> speciestree_ext_nodes ) {
575 // // final Set<PhylogenyNode> to_delete = new HashSet<PhylogenyNode>();
576 // for( final PhylogenyNodeIterator iter = _gene_tree.iteratorExternalForward(); iter.hasNext(); ) {
577 // final PhylogenyNode g = iter.next();
578 // if ( !g.getNodeData().isHasTaxonomy() ) {
579 // throw new IllegalArgumentException( "gene tree node " + g + " has no taxonomic data" );
581 // if ( !speciestree_ext_nodes.containsKey( g.getNodeData().getTaxonomy() ) ) {
582 // _stripped_gene_tree_nodes.add( g );
585 // for( final PhylogenyNode n : _stripped_gene_tree_nodes ) {
586 // _gene_tree.deleteSubtree( n, true );
589 // private final void stripGeneTree2( final HashMap<Taxonomy, PhylogenyNode> speciestree_ext_nodes ) {
590 // // final Set<PhylogenyNode> to_delete = new HashSet<PhylogenyNode>();
591 // for( final PhylogenyNodeIterator iter = _gene_tree.iteratorExternalForward(); iter.hasNext(); ) {
592 // final PhylogenyNode g = iter.next();
593 // if ( !g.getNodeData().isHasTaxonomy() ) {
594 // _stripped_gene_tree_nodes.add( g );
597 // if ( !speciestree_ext_nodes.containsKey( g.getNodeData().getTaxonomy() ) ) {
598 // _stripped_gene_tree_nodes.add( g );
602 // for( final PhylogenyNode n : _stripped_gene_tree_nodes ) {
603 // _gene_tree.deleteSubtree( n, true );
606 public static TaxonomyComparisonBase determineTaxonomyComparisonBase( final Phylogeny gene_tree ) {
607 int with_id_count = 0;
608 int with_code_count = 0;
609 int with_sn_count = 0;
611 for( final PhylogenyNodeIterator iter = gene_tree.iteratorExternalForward(); iter.hasNext(); ) {
612 final PhylogenyNode g = iter.next();
613 if ( g.getNodeData().isHasTaxonomy() ) {
614 final Taxonomy tax = g.getNodeData().getTaxonomy();
615 if ( ( tax.getIdentifier() != null ) && !ForesterUtil.isEmpty( tax.getIdentifier().getValue() ) ) {
616 if ( ++with_id_count > max ) {
620 if ( !ForesterUtil.isEmpty( tax.getTaxonomyCode() ) ) {
621 if ( ++with_code_count > max ) {
622 max = with_code_count;
625 if ( !ForesterUtil.isEmpty( tax.getScientificName() ) ) {
626 if ( ++with_sn_count > max ) {
633 throw new IllegalArgumentException( "gene tree has no taxonomic data" );
635 else if ( max == 1 ) {
636 throw new IllegalArgumentException( "gene tree has only one node with taxonomic data" );
638 else if ( max == with_sn_count ) {
639 return SDI.TaxonomyComparisonBase.SCIENTIFIC_NAME;
641 else if ( max == with_id_count ) {
642 return SDI.TaxonomyComparisonBase.ID;
645 return SDI.TaxonomyComparisonBase.CODE;
649 public List<PhylogenyNode> getStrippedExternalGeneTreeNodes() {
650 return _stripped_gene_tree_nodes;
654 public final String toString() {
655 final StringBuffer sb = new StringBuffer();
656 sb.append( "Most parsimonious duplication model: " + _most_parsimonious_duplication_model );
657 sb.append( ForesterUtil.getLineSeparator() );
658 sb.append( "Speciations sum : " + getSpeciationsSum() );
659 sb.append( ForesterUtil.getLineSeparator() );
660 sb.append( "Duplications sum : " + getDuplicationsSum() );
661 sb.append( ForesterUtil.getLineSeparator() );
662 if ( !_most_parsimonious_duplication_model ) {
663 sb.append( "Speciation or duplications sum : " + getSpeciationOrDuplicationEventsSum() );
664 sb.append( ForesterUtil.getLineSeparator() );
666 sb.append( "mapping cost L : " + computeMappingCostL() );
667 return sb.toString();
670 static final int[] obtainMinMaxIdIndices( final PhylogenyNode[] linked_nodes ) {
673 int max_i_id = -Integer.MAX_VALUE;
674 int min_i_id = Integer.MAX_VALUE;
675 for( int i = 0; i < linked_nodes.length; ++i ) {
676 final int id_i = linked_nodes[ i ].getId();
677 if ( id_i > max_i_id ) {
679 max_i_id = linked_nodes[ max_i ].getId();
681 if ( id_i < min_i_id ) {
683 min_i_id = linked_nodes[ min_i ].getId();
686 return new int[] { min_i, max_i };
689 * Updates the mapping function M after the root of the gene tree has been
690 * moved by one branch. It calculates M for the root of the gene tree and
691 * one of its two children.
693 * To be used ONLY by method "SDIunrooted.fastInfer(Phylogeny,Phylogeny)".
697 * @param prev_root_was_dup
698 * true if the previous root was a duplication, false otherwise
699 * @param prev_root_c1
700 * child 1 of the previous root
701 * @param prev_root_c2
702 * child 2 of the previous root
703 * @return number of duplications which have been assigned in gene tree
705 // int updateM( final boolean prev_root_was_dup,
706 // final PhylogenyNode prev_root_c1, final PhylogenyNode prev_root_c2 ) {
707 // final PhylogenyNode root = getGeneTree().getRoot();
708 // if ( ( root.getChildNode1() == prev_root_c1 )
709 // || ( root.getChildNode2() == prev_root_c1 ) ) {
710 // calculateMforNode( prev_root_c1 );
713 // calculateMforNode( prev_root_c2 );
715 // Event event = null;
716 // if ( prev_root_was_dup ) {
717 // event = Event.createSingleDuplicationEvent();
720 // event = Event.createSingleSpeciationEvent();
722 // root.getPhylogenyNodeData().setEvent( event );
723 // calculateMforNode( root );
724 // return getDuplications();
725 // } // updateM( boolean, PhylogenyNode, PhylogenyNode )
726 // Helper method for updateM( boolean, PhylogenyNode, PhylogenyNode )
727 // Calculates M for PhylogenyNode n, given that M for the two children
728 // of n has been calculated.
729 // (Last modified: 10/02/01)
730 // private void calculateMforNode( final PhylogenyNode n ) {
731 // if ( !n.isExternal() ) {
732 // boolean was_duplication = n.isDuplication();
733 // PhylogenyNode a = n.getChildNode1().getLink(), b = n
734 // .getChildNode2().getLink();
735 // while ( a != b ) {
736 // if ( a.getID() > b.getID() ) {
737 // a = a.getParent();
740 // b = b.getParent();
744 // Event event = null;
745 // if ( ( a == n.getChildNode1().getLink() )
746 // || ( a == n.getChildNode2().getLink() ) ) {
747 // event = Event.createSingleDuplicationEvent();
748 // if ( !was_duplication ) {
749 // increaseDuplications();
753 // event = Event.createSingleSpeciationEvent();
754 // if ( was_duplication ) {
755 // decreaseDuplications();
758 // n.getPhylogenyNodeData().setEvent( event );
760 // } // calculateMforNode( PhylogenyNode )