- /**
- * Updates the mapping function M after the root of the gene tree has been
- * moved by one branch. It calculates M for the root of the gene tree and
- * one of its two children.
- * <p>
- * To be used ONLY by method "SDIunrooted.fastInfer(Phylogeny,Phylogeny)".
- * <p>
- * (Last modfied: )
- *
- * @param prev_root_was_dup
- * true if the previous root was a duplication, false otherwise
- * @param prev_root_c1
- * child 1 of the previous root
- * @param prev_root_c2
- * child 2 of the previous root
- * @return number of duplications which have been assigned in gene tree
- */
- // int updateM( final boolean prev_root_was_dup,
- // final PhylogenyNode prev_root_c1, final PhylogenyNode prev_root_c2 ) {
- // final PhylogenyNode root = getGeneTree().getRoot();
- // if ( ( root.getChildNode1() == prev_root_c1 )
- // || ( root.getChildNode2() == prev_root_c1 ) ) {
- // calculateMforNode( prev_root_c1 );
- // }
- // else {
- // calculateMforNode( prev_root_c2 );
- // }
- // Event event = null;
- // if ( prev_root_was_dup ) {
- // event = Event.createSingleDuplicationEvent();
- // }
- // else {
- // event = Event.createSingleSpeciationEvent();
- // }
- // root.getPhylogenyNodeData().setEvent( event );
- // calculateMforNode( root );
- // return getDuplications();
- // } // updateM( boolean, PhylogenyNode, PhylogenyNode )
- // Helper method for updateM( boolean, PhylogenyNode, PhylogenyNode )
- // Calculates M for PhylogenyNode n, given that M for the two children
- // of n has been calculated.
- // (Last modified: 10/02/01)
- // private void calculateMforNode( final PhylogenyNode n ) {
- // if ( !n.isExternal() ) {
- // boolean was_duplication = n.isDuplication();
- // PhylogenyNode a = n.getChildNode1().getLink(), b = n
- // .getChildNode2().getLink();
- // while ( a != b ) {
- // if ( a.getID() > b.getID() ) {
- // a = a.getParent();
- // }
- // else {
- // b = b.getParent();
- // }
- // }
- // n.setLink( a );
- // Event event = null;
- // if ( ( a == n.getChildNode1().getLink() )
- // || ( a == n.getChildNode2().getLink() ) ) {
- // event = Event.createSingleDuplicationEvent();
- // if ( !was_duplication ) {
- // increaseDuplications();
- // }
- // }
- // else {
- // event = Event.createSingleSpeciationEvent();
- // if ( was_duplication ) {
- // decreaseDuplications();
- // }
- // }
- // n.getPhylogenyNodeData().setEvent( event );
- // }
- // } // calculateMforNode( PhylogenyNode )
+
+ private final static void stripSpeciesTree( final Phylogeny species_tree,
+ final List<PhylogenyNode> species_tree_ext_nodes,
+ final NodesLinkingResult res ) {
+ for( final PhylogenyNode s : species_tree_ext_nodes ) {
+ if ( !res.getMappedSpeciesTreeNodes().contains( s ) ) {
+ species_tree.deleteSubtree( s, true );
+ res.getStrippedSpeciesTreeNodes().add( s );
+ }
+ }
+ species_tree.clearHashIdToNodeMap();
+ species_tree.externalNodesHaveChanged();
+ }
+
+ private final static void stripTree( final Phylogeny phy, final List<PhylogenyNode> strip_nodes ) {
+ for( final PhylogenyNode g : strip_nodes ) {
+ phy.deleteSubtree( g, true );
+ }
+ phy.clearHashIdToNodeMap();
+ phy.externalNodesHaveChanged();
+ }
+
+ private final static PhylogenyNode tryMapByRemovingOverlySpecificData( final Map<String, PhylogenyNode> species_to_node_map,
+ final String tax_str,
+ final SortedSet<String> scientific_names_mapped_to_reduced_specificity ) {
+ PhylogenyNode s = tryMapByRemovingOverlySpecificData( species_to_node_map,
+ tax_str,
+ " (",
+ scientific_names_mapped_to_reduced_specificity );
+ if ( s == null ) {
+ if ( ForesterUtil.countChars( tax_str, ' ' ) == 2 ) {
+ final String new_tax_str = tax_str.substring( 0, tax_str.lastIndexOf( ' ' ) ).trim();
+ s = species_to_node_map.get( new_tax_str );
+ if ( s != null ) {
+ addScientificNamesMappedToReducedSpecificity( tax_str,
+ new_tax_str,
+ scientific_names_mapped_to_reduced_specificity );
+ }
+ }
+ }
+ if ( s == null ) {
+ for( final String t : new String[] { " subspecies ", " strain ", " variety ", " varietas ", " subvariety ",
+ " form ", " subform ", " cultivar ", " section ", " subsection " } ) {
+ s = tryMapByRemovingOverlySpecificData( species_to_node_map,
+ tax_str,
+ t,
+ scientific_names_mapped_to_reduced_specificity );
+ if ( s != null ) {
+ break;
+ }
+ }
+ }
+ return s;
+ }
+
+ private final static PhylogenyNode tryMapByRemovingOverlySpecificData( final Map<String, PhylogenyNode> species_to_node_map,
+ final String tax_str,
+ final String term,
+ final SortedSet<String> scientific_names_mapped_to_reduced_specificity ) {
+ final int i = tax_str.indexOf( term );
+ if ( i > 4 ) {
+ final String new_tax_str = tax_str.substring( 0, i ).trim();
+ final PhylogenyNode s = species_to_node_map.get( new_tax_str );
+ if ( s != null ) {
+ addScientificNamesMappedToReducedSpecificity( tax_str,
+ new_tax_str,
+ scientific_names_mapped_to_reduced_specificity );
+ }
+ return s;
+ }
+ return null;
+ }