+ }
+
+ public static boolean isHasExternalDescendant( final PhylogenyNode node ) {
+ for( int i = 0; i < node.getNumberOfDescendants(); ++i ) {
+ if ( node.getChildNode( i ).isExternal() ) {
+ return true;
+ }
+ }
+ return false;
+ }
+
+ /*
+ * This is case insensitive.
+ *
+ */
+ public synchronized static boolean isTaxonomyHasIdentifierOfGivenProvider( final Taxonomy tax,
+ final String[] providers ) {
+ if ( ( tax.getIdentifier() != null ) && !ForesterUtil.isEmpty( tax.getIdentifier().getProvider() ) ) {
+ final String my_tax_prov = tax.getIdentifier().getProvider();
+ for( final String provider : providers ) {
+ if ( provider.equalsIgnoreCase( my_tax_prov ) ) {
+ return true;
+ }
+ }
+ return false;
+ }
+ else {
+ return false;
+ }
+ }
+
+ public static void midpointRoot( final Phylogeny phylogeny ) {
+ if ( ( phylogeny.getNumberOfExternalNodes() < 2 ) || ( calculateMaxDistanceToRoot( phylogeny ) <= 0 ) ) {
+ return;
+ }
+ int counter = 0;
+ final int total_nodes = phylogeny.getNodeCount();
+ while ( true ) {
+ if ( ++counter > total_nodes ) {
+ throw new RuntimeException( "this should not have happened: midpoint rooting does not converge" );
+ }
+ PhylogenyNode a = null;
+ double da = 0;
+ double db = 0;
+ for( int i = 0; i < phylogeny.getRoot().getNumberOfDescendants(); ++i ) {
+ final PhylogenyNode f = getFurthestDescendant( phylogeny.getRoot().getChildNode( i ) );
+ final double df = getDistance( f, phylogeny.getRoot() );
+ if ( df > 0 ) {
+ if ( df > da ) {
+ db = da;
+ da = df;
+ a = f;
+ }
+ else if ( df > db ) {
+ db = df;
+ }
+ }
+ }
+ final double diff = da - db;
+ if ( diff < 0.000001 ) {
+ break;
+ }
+ double x = da - ( diff / 2.0 );
+ while ( ( x > a.getDistanceToParent() ) && !a.isRoot() ) {
+ x -= ( a.getDistanceToParent() > 0 ? a.getDistanceToParent() : 0 );
+ a = a.getParent();
+ }
+ phylogeny.reRoot( a, x );
+ }
+ phylogeny.recalculateNumberOfExternalDescendants( true );
+ }
+
+ public static void normalizeBootstrapValues( final Phylogeny phylogeny,
+ final double max_bootstrap_value,
+ final double max_normalized_value ) {
+ for( final PhylogenyNodeIterator iter = phylogeny.iteratorPreorder(); iter.hasNext(); ) {
+ final PhylogenyNode node = iter.next();
+ if ( node.isInternal() ) {
+ final double confidence = getConfidenceValue( node );
+ if ( confidence != Confidence.CONFIDENCE_DEFAULT_VALUE ) {
+ if ( confidence >= max_bootstrap_value ) {
+ setBootstrapConfidence( node, max_normalized_value );
+ }
+ else {
+ setBootstrapConfidence( node, ( confidence * max_normalized_value ) / max_bootstrap_value );
+ }
+ }
+ }
+ }
+ }
+
+ public static List<PhylogenyNode> obtainAllNodesAsList( final Phylogeny phy ) {
+ final List<PhylogenyNode> nodes = new ArrayList<PhylogenyNode>();
+ if ( phy.isEmpty() ) {
+ return nodes;
+ }
+ for( final PhylogenyNodeIterator iter = phy.iteratorPreorder(); iter.hasNext(); ) {
+ nodes.add( iter.next() );
+ }
+ return nodes;
+ }
+
+ /**
+ * Returns a map of distinct taxonomies of
+ * all external nodes of node.
+ * If at least one of the external nodes has no taxonomy,
+ * null is returned.
+ *
+ */
+ public static Map<Taxonomy, Integer> obtainDistinctTaxonomyCounts( final PhylogenyNode node ) {
+ final List<PhylogenyNode> descs = node.getAllExternalDescendants();
+ final Map<Taxonomy, Integer> tax_map = new HashMap<Taxonomy, Integer>();
+ for( final PhylogenyNode n : descs ) {
+ if ( !n.getNodeData().isHasTaxonomy() || n.getNodeData().getTaxonomy().isEmpty() ) {
+ return null;
+ }
+ final Taxonomy t = n.getNodeData().getTaxonomy();
+ if ( tax_map.containsKey( t ) ) {
+ tax_map.put( t, tax_map.get( t ) + 1 );
+ }
+ else {
+ tax_map.put( t, 1 );
+ }
+ }
+ return tax_map;
+ }
+
+ /**
+ * Arranges the order of childern for each node of this Phylogeny in such a
+ * way that either the branch with more children is on top (right) or on
+ * bottom (left), dependent on the value of boolean order.
+ *
+ * @param order
+ * decides in which direction to order
+ * @param pri
+ */
+ public static void orderAppearance( final PhylogenyNode n,
+ final boolean order,
+ final boolean order_ext_alphabetically,
+ final DESCENDANT_SORT_PRIORITY pri ) {
+ if ( n.isExternal() ) {
+ return;
+ }
+ else {
+ if ( ( n.getNumberOfDescendants() == 2 )
+ && ( n.getChildNode1().getNumberOfExternalNodes() != n.getChildNode2().getNumberOfExternalNodes() )
+ && ( ( n.getChildNode1().getNumberOfExternalNodes() < n.getChildNode2()
+ .getNumberOfExternalNodes() ) == order ) ) {
+ final PhylogenyNode temp = n.getChildNode1();
+ n.setChild1( n.getChildNode2() );
+ n.setChild2( temp );
+ _order_changed = true;
+ }
+ else if ( order_ext_alphabetically ) {
+ boolean all_ext = true;
+ for( final PhylogenyNode i : n.getDescendants() ) {
+ if ( !i.isExternal() ) {
+ all_ext = false;
+ break;
+ }
+ }
+ if ( all_ext ) {
+ PhylogenyMethods.sortNodeDescendents( n, pri );
+ }
+ }
+ for( int i = 0; i < n.getNumberOfDescendants(); ++i ) {
+ orderAppearance( n.getChildNode( i ), order, order_ext_alphabetically, pri );
+ }
+ }
+ }
+
+ public synchronized static void orderAppearanceX( final PhylogenyNode n,
+ final boolean order_ext_alphabetically,
+ final DESCENDANT_SORT_PRIORITY pri ) {
+ if ( n.isExternal() ) {
+ return;
+ }
+ else {
+ _order_changed = false;
+ orderAppearance( n, true, order_ext_alphabetically, pri );
+ if ( !_order_changed ) {
+ orderAppearance( n, false, order_ext_alphabetically, pri );
+ }
+ }
+ }
+
+ public static void postorderBranchColorAveragingExternalNodeBased( final Phylogeny p ) {
+ for( final PhylogenyNodeIterator iter = p.iteratorPostorder(); iter.hasNext(); ) {
+ final PhylogenyNode node = iter.next();
+ double red = 0.0;
+ double green = 0.0;
+ double blue = 0.0;
+ int n = 0;
+ if ( node.isInternal() ) {
+ //for( final PhylogenyNodeIterator iterator = node.iterateChildNodesForward(); iterator.hasNext(); ) {
+ for( int i = 0; i < node.getNumberOfDescendants(); ++i ) {
+ final PhylogenyNode child_node = node.getChildNode( i );
+ final Color child_color = getBranchColorValue( child_node );
+ if ( child_color != null ) {
+ ++n;
+ red += child_color.getRed();
+ green += child_color.getGreen();
+ blue += child_color.getBlue();
+ }
+ }
+ setBranchColorValue( node,
+ new Color( ForesterUtil.roundToInt( red / n ),
+ ForesterUtil.roundToInt( green / n ),
+ ForesterUtil.roundToInt( blue / n ) ) );
+ }
+ }
+ }
+
+ public static final void preOrderReId( final Phylogeny phy ) {
+ if ( phy.isEmpty() ) {
+ return;
+ }
+ phy.setIdToNodeMap( null );
+ long i = PhylogenyNode.getNodeCount();
+ for( final PhylogenyNodeIterator it = phy.iteratorPreorder(); it.hasNext(); ) {
+ it.next().setId( i++ );
+ }
+ PhylogenyNode.setNodeCount( i );
+ }
+
+ public final static Phylogeny[] readPhylogenies( final PhylogenyParser parser, final File file )
+ throws IOException {
+ final PhylogenyFactory factory = ParserBasedPhylogenyFactory.getInstance();
+ final Phylogeny[] trees = factory.create( file, parser );
+ if ( ( trees == null ) || ( trees.length == 0 ) ) {
+ throw new PhylogenyParserException( "Unable to parse phylogeny from file: " + file );
+ }
+ return trees;
+ }
+
+ public final static Phylogeny[] readPhylogenies( final PhylogenyParser parser, final List<File> files )
+ throws IOException {
+ final List<Phylogeny> tree_list = new ArrayList<Phylogeny>();
+ for( final File file : files ) {
+ final PhylogenyFactory factory = ParserBasedPhylogenyFactory.getInstance();
+ final Phylogeny[] trees = factory.create( file, parser );
+ if ( ( trees == null ) || ( trees.length == 0 ) ) {
+ throw new PhylogenyParserException( "Unable to parse phylogeny from file: " + file );
+ }
+ tree_list.addAll( Arrays.asList( trees ) );
+ }
+ return tree_list.toArray( new Phylogeny[ tree_list.size() ] );
+ }
+
+ public static void removeNode( final PhylogenyNode remove_me, final Phylogeny phylogeny ) {
+ if ( remove_me.isRoot() ) {
+ if ( remove_me.getNumberOfDescendants() == 1 ) {
+ final PhylogenyNode desc = remove_me.getDescendants().get( 0 );
+ desc.setDistanceToParent( addPhylogenyDistances( remove_me.getDistanceToParent(),
+ desc.getDistanceToParent() ) );
+ desc.setParent( null );
+ phylogeny.setRoot( desc );
+ phylogeny.clearHashIdToNodeMap();
+ }
+ else {
+ throw new IllegalArgumentException( "attempt to remove a root node with more than one descendants" );
+ }
+ }
+ else if ( remove_me.isExternal() ) {
+ phylogeny.deleteSubtree( remove_me, false );
+ phylogeny.clearHashIdToNodeMap();
+ phylogeny.externalNodesHaveChanged();
+ }
+ else {
+ final PhylogenyNode parent = remove_me.getParent();
+ final List<PhylogenyNode> descs = remove_me.getDescendants();
+ parent.removeChildNode( remove_me );
+ for( final PhylogenyNode desc : descs ) {
+ parent.addAsChild( desc );
+ desc.setDistanceToParent( addPhylogenyDistances( remove_me.getDistanceToParent(),
+ desc.getDistanceToParent() ) );
+ }
+ remove_me.setParent( null );
+ phylogeny.clearHashIdToNodeMap();
+ phylogeny.externalNodesHaveChanged();
+ }
+ }
+
+ private static enum NDF {
+ NodeName( "NN" ),
+ TaxonomyCode( "TC" ),
+ TaxonomyCommonName( "TN" ),
+ TaxonomyScientificName( "TS" ),
+ TaxonomyIdentifier( "TI" ),
+ TaxonomySynonym( "SY" ),
+ SequenceName( "SN" ),
+ GeneName( "GN" ),
+ SequenceSymbol( "SS" ),
+ SequenceAccession( "SA" ),
+ Domain( "DO" ),
+ Annotation( "AN" ),
+ CrossRef( "XR" ),
+ BinaryCharacter( "BC" ),
+ MolecularSequence( "MS" );
+
+ private final String _text;
+
+ NDF( final String text ) {
+ _text = text;
+ }
+
+ public static NDF fromString( final String text ) {
+ for( final NDF n : NDF.values() ) {
+ if ( text.startsWith( n._text ) ) {
+ return n;
+ }
+ }
+ return null;
+ }
+ }
+
+ public static List<Long> searchData( final String query,
+ final Phylogeny phy,
+ final boolean case_sensitive,
+ final boolean partial,
+ final boolean regex,
+ final boolean search_domains,
+ final double domains_confidence_threshold ) {
+ final List<Long> nodes = new ArrayList<Long>();
+ if ( phy.isEmpty() || ( query == null ) ) {
+ return nodes;
+ }
+ if ( ForesterUtil.isEmpty( query ) ) {
+ return nodes;
+ }
+ String my_query = query;
+ NDF ndf = null;
+ if ( ( my_query.length() > 2 ) && ( my_query.indexOf( ":" ) == 2 ) ) {
+ ndf = NDF.fromString( my_query );
+ if ( ndf != null ) {
+ my_query = my_query.substring( 3 );
+ }
+ }
+ for( final PhylogenyNodeIterator iter = phy.iteratorPreorder(); iter.hasNext(); ) {
+ final PhylogenyNode node = iter.next();
+ boolean match = false;
+ if ( ( ( ndf == null ) || ( ndf == NDF.NodeName ) )
+ && match( node.getName(), my_query, case_sensitive, partial, regex ) ) {
+ match = true;
+ }
+ else if ( ( ( ndf == null ) || ( ndf == NDF.TaxonomyCode ) ) && node.getNodeData().isHasTaxonomy()
+ && match( node.getNodeData().getTaxonomy().getTaxonomyCode(),
+ my_query,
+ case_sensitive,
+ partial,
+ regex ) ) {
+ match = true;
+ }
+ else if ( ( ( ndf == null ) || ( ndf == NDF.TaxonomyCommonName ) ) && node.getNodeData().isHasTaxonomy()
+ && match( node.getNodeData().getTaxonomy().getCommonName(),
+ my_query,
+ case_sensitive,
+ partial,
+ regex ) ) {
+ match = true;
+ }
+ else if ( ( ( ndf == null ) || ( ndf == NDF.TaxonomyScientificName ) ) && node.getNodeData().isHasTaxonomy()
+ && match( node.getNodeData().getTaxonomy().getScientificName(),
+ my_query,
+ case_sensitive,
+ partial,
+ regex ) ) {
+ match = true;
+ }
+ else if ( ( ( ndf == null ) || ( ndf == NDF.TaxonomyIdentifier ) ) && node.getNodeData().isHasTaxonomy()
+ && ( node.getNodeData().getTaxonomy().getIdentifier() != null )
+ && match( node.getNodeData().getTaxonomy().getIdentifier().getValue(),
+ my_query,
+ case_sensitive,
+ partial,
+ regex ) ) {
+ match = true;
+ }
+ else if ( ( ( ndf == null ) || ( ndf == NDF.TaxonomySynonym ) ) && node.getNodeData().isHasTaxonomy()
+ && !node.getNodeData().getTaxonomy().getSynonyms().isEmpty() ) {
+ final List<String> syns = node.getNodeData().getTaxonomy().getSynonyms();
+ I: for( final String syn : syns ) {
+ if ( match( syn, my_query, case_sensitive, partial, regex ) ) {
+ match = true;
+ break I;
+ }
+ }
+ }
+ if ( !match && ( ( ndf == null ) || ( ndf == NDF.SequenceName ) ) && node.getNodeData().isHasSequence()
+ && match( node.getNodeData().getSequence().getName(), my_query, case_sensitive, partial, regex ) ) {
+ match = true;
+ }
+ if ( !match && ( ( ndf == null ) || ( ndf == NDF.GeneName ) ) && node.getNodeData().isHasSequence()
+ && match( node.getNodeData().getSequence().getGeneName(),
+ my_query,
+ case_sensitive,
+ partial,
+ regex ) ) {
+ match = true;
+ }
+ if ( !match && ( ( ndf == null ) || ( ndf == NDF.SequenceSymbol ) ) && node.getNodeData().isHasSequence()
+ && match( node.getNodeData().getSequence().getSymbol(),
+ my_query,
+ case_sensitive,
+ partial,
+ regex ) ) {
+ match = true;
+ }
+ if ( !match && ( ( ndf == null ) || ( ndf == NDF.SequenceAccession ) ) && node.getNodeData().isHasSequence()
+ && ( node.getNodeData().getSequence().getAccession() != null )
+ && match( node.getNodeData().getSequence().getAccession().getValue(),
+ my_query,
+ case_sensitive,
+ partial,
+ regex ) ) {
+ match = true;
+ }
+ if ( !match && ( ( ( ndf == null ) && search_domains ) || ( ndf == NDF.Domain ) )
+ && node.getNodeData().isHasSequence()
+ && ( node.getNodeData().getSequence().getDomainArchitecture() != null ) ) {
+ final DomainArchitecture da = node.getNodeData().getSequence().getDomainArchitecture();
+ I: for( int i = 0; i < da.getNumberOfDomains(); ++i ) {
+ if ( ( da.getDomain( i ).getConfidence() <= domains_confidence_threshold )
+ && ( match( da.getDomain( i ).getName(), my_query, case_sensitive, partial, regex ) ) ) {
+ match = true;
+ break I;
+ }
+ }
+ }
+ if ( !match && ( ( ndf == null ) || ( ndf == NDF.Annotation ) ) && node.getNodeData().isHasSequence()
+ && ( node.getNodeData().getSequence().getAnnotations() != null ) ) {
+ for( final Annotation ann : node.getNodeData().getSequence().getAnnotations() ) {
+ if ( match( ann.getDesc(), my_query, case_sensitive, partial, regex ) ) {
+ match = true;
+ break;
+ }
+ if ( match( ann.getRef(), my_query, case_sensitive, partial, regex ) ) {
+ match = true;
+ break;
+ }
+ }
+ }
+ if ( !match && ( ( ndf == null ) || ( ndf == NDF.CrossRef ) ) && node.getNodeData().isHasSequence()
+ && ( node.getNodeData().getSequence().getCrossReferences() != null ) ) {
+ for( final Accession x : node.getNodeData().getSequence().getCrossReferences() ) {
+ if ( match( x.getComment(), my_query, case_sensitive, partial, regex ) ) {
+ match = true;
+ break;
+ }
+ if ( match( x.getSource(), my_query, case_sensitive, partial, regex ) ) {
+ match = true;
+ break;
+ }
+ if ( match( x.getValue(), my_query, case_sensitive, partial, regex ) ) {
+ match = true;
+ break;
+ }
+ }
+ }
+ if ( !match && ( ( ndf == null ) || ( ndf == NDF.BinaryCharacter ) )
+ && ( node.getNodeData().getBinaryCharacters() != null ) ) {
+ Iterator<String> it = node.getNodeData().getBinaryCharacters().getPresentCharacters().iterator();
+ I: while ( it.hasNext() ) {
+ if ( match( it.next(), my_query, case_sensitive, partial, regex ) ) {
+ match = true;
+ break I;
+ }
+ }
+ it = node.getNodeData().getBinaryCharacters().getGainedCharacters().iterator();
+ I: while ( it.hasNext() ) {
+ if ( match( it.next(), my_query, case_sensitive, partial, regex ) ) {
+ match = true;
+ break I;
+ }
+ }
+ }
+ if ( !match && ( ndf == NDF.MolecularSequence ) && node.getNodeData().isHasSequence()
+ && match( node.getNodeData().getSequence().getMolecularSequence(),
+ my_query,
+ case_sensitive,
+ true,
+ regex ) ) {
+ match = true;
+ }
+ if ( match ) {
+ nodes.add( node.getId() );
+ }
+ }
+ return nodes;
+ }
+
+ public static List<Long> searchDataLogicalAnd( final String[] queries,
+ final Phylogeny phy,
+ final boolean case_sensitive,
+ final boolean partial,
+ final boolean search_domains,
+ final double domains_confidence_threshold ) {
+ final List<Long> nodes = new ArrayList<Long>();
+ if ( phy.isEmpty() || ( queries == null ) || ( queries.length < 1 ) ) {
+ return nodes;
+ }
+ for( final PhylogenyNodeIterator iter = phy.iteratorPreorder(); iter.hasNext(); ) {
+ final PhylogenyNode node = iter.next();
+ boolean all_matched = true;
+ for( String query : queries ) {
+ if ( query == null ) {
+ continue;
+ }
+ query = query.trim();
+ NDF ndf = null;
+ if ( ( query.length() > 2 ) && ( query.indexOf( ":" ) == 2 ) ) {
+ ndf = NDF.fromString( query );
+ if ( ndf != null ) {
+ query = query.substring( 3 );
+ }
+ }
+ boolean match = false;
+ if ( ForesterUtil.isEmpty( query ) ) {
+ continue;
+ }
+ if ( ( ( ndf == null ) || ( ndf == NDF.NodeName ) )
+ && match( node.getName(), query, case_sensitive, partial, false ) ) {
+ match = true;
+ }
+ else if ( ( ( ndf == null ) || ( ndf == NDF.TaxonomyCode ) ) && node.getNodeData().isHasTaxonomy()
+ && match( node.getNodeData().getTaxonomy().getTaxonomyCode(),
+ query,
+ case_sensitive,
+ partial,
+ false ) ) {
+ match = true;
+ }
+ else if ( ( ( ndf == null ) || ( ndf == NDF.TaxonomyCommonName ) ) && node.getNodeData().isHasTaxonomy()
+ && match( node.getNodeData().getTaxonomy().getCommonName(),
+ query,
+ case_sensitive,
+ partial,
+ false ) ) {
+ match = true;
+ }
+ else if ( ( ( ndf == null ) || ( ndf == NDF.TaxonomyScientificName ) )
+ && node.getNodeData().isHasTaxonomy()
+ && match( node.getNodeData().getTaxonomy().getScientificName(),
+ query,
+ case_sensitive,
+ partial,
+ false ) ) {
+ match = true;
+ }
+ else if ( ( ( ndf == null ) || ( ndf == NDF.TaxonomyIdentifier ) ) && node.getNodeData().isHasTaxonomy()
+ && ( node.getNodeData().getTaxonomy().getIdentifier() != null )
+ && match( node.getNodeData().getTaxonomy().getIdentifier().getValue(),
+ query,
+ case_sensitive,
+ partial,
+ false ) ) {
+ match = true;
+ }
+ else if ( ( ( ndf == null ) || ( ndf == NDF.TaxonomySynonym ) ) && node.getNodeData().isHasTaxonomy()
+ && !node.getNodeData().getTaxonomy().getSynonyms().isEmpty() ) {
+ final List<String> syns = node.getNodeData().getTaxonomy().getSynonyms();
+ I: for( final String syn : syns ) {
+ if ( match( syn, query, case_sensitive, partial, false ) ) {
+ match = true;
+ break I;
+ }
+ }
+ }
+ if ( !match && ( ( ndf == null ) || ( ndf == NDF.SequenceName ) ) && node.getNodeData().isHasSequence()
+ && match( node.getNodeData().getSequence().getName(),
+ query,
+ case_sensitive,
+ partial,
+ false ) ) {
+ match = true;
+ }
+ if ( !match && ( ( ndf == null ) || ( ndf == NDF.GeneName ) ) && node.getNodeData().isHasSequence()
+ && match( node.getNodeData().getSequence().getGeneName(),
+ query,
+ case_sensitive,
+ partial,
+ false ) ) {
+ match = true;
+ }
+ if ( !match && ( ( ndf == null ) || ( ndf == NDF.SequenceSymbol ) )
+ && node.getNodeData().isHasSequence() && match( node.getNodeData().getSequence().getSymbol(),
+ query,
+ case_sensitive,
+ partial,
+ false ) ) {
+ match = true;
+ }
+ if ( !match && ( ( ndf == null ) || ( ndf == NDF.SequenceAccession ) )
+ && node.getNodeData().isHasSequence()
+ && ( node.getNodeData().getSequence().getAccession() != null )
+ && match( node.getNodeData().getSequence().getAccession().getValue(),
+ query,
+ case_sensitive,
+ partial,
+ false ) ) {
+ match = true;
+ }
+ if ( !match && ( ( ( ndf == null ) && search_domains ) || ( ndf == NDF.Domain ) )
+ && node.getNodeData().isHasSequence()
+ && ( node.getNodeData().getSequence().getDomainArchitecture() != null ) ) {
+ final DomainArchitecture da = node.getNodeData().getSequence().getDomainArchitecture();
+ I: for( int i = 0; i < da.getNumberOfDomains(); ++i ) {
+ if ( ( da.getDomain( i ).getConfidence() <= domains_confidence_threshold )
+ && match( da.getDomain( i ).getName(), query, case_sensitive, partial, false ) ) {
+ match = true;
+ break I;
+ }
+ }
+ }
+ if ( !match && ( ( ndf == null ) || ( ndf == NDF.Annotation ) ) && node.getNodeData().isHasSequence()
+ && ( node.getNodeData().getSequence().getAnnotations() != null ) ) {
+ for( final Annotation ann : node.getNodeData().getSequence().getAnnotations() ) {
+ if ( match( ann.getDesc(), query, case_sensitive, partial, false ) ) {
+ match = true;
+ break;
+ }
+ if ( match( ann.getRef(), query, case_sensitive, partial, false ) ) {
+ match = true;
+ break;
+ }
+ }
+ }
+ if ( !match && ( ( ndf == null ) || ( ndf == NDF.CrossRef ) ) && node.getNodeData().isHasSequence()
+ && ( node.getNodeData().getSequence().getCrossReferences() != null ) ) {
+ for( final Accession x : node.getNodeData().getSequence().getCrossReferences() ) {
+ if ( match( x.getComment(), query, case_sensitive, partial, false ) ) {
+ match = true;
+ break;
+ }
+ if ( match( x.getSource(), query, case_sensitive, partial, false ) ) {
+ match = true;
+ break;
+ }
+ if ( match( x.getValue(), query, case_sensitive, partial, false ) ) {
+ match = true;
+ break;
+ }
+ }
+ }
+ if ( !match && ( ( ndf == null ) || ( ndf == NDF.BinaryCharacter ) )
+ && ( node.getNodeData().getBinaryCharacters() != null ) ) {
+ Iterator<String> it = node.getNodeData().getBinaryCharacters().getPresentCharacters().iterator();
+ I: while ( it.hasNext() ) {
+ if ( match( it.next(), query, case_sensitive, partial, false ) ) {
+ match = true;
+ break I;
+ }
+ }
+ it = node.getNodeData().getBinaryCharacters().getGainedCharacters().iterator();
+ I: while ( it.hasNext() ) {
+ if ( match( it.next(), query, case_sensitive, partial, false ) ) {
+ match = true;
+ break I;
+ }
+ }
+ }
+ if ( !match && ( ndf == NDF.MolecularSequence ) && node.getNodeData().isHasSequence()
+ && match( node.getNodeData().getSequence().getMolecularSequence(),
+ query,
+ case_sensitive,
+ true,
+ false ) ) {
+ match = true;
+ }
+ if ( !match ) {
+ all_matched = false;
+ break;
+ }
+ }
+ if ( all_matched ) {
+ nodes.add( node.getId() );
+ }
+ }
+ return nodes;
+ }
+
+ public static void setAllIndicatorsToZero( final Phylogeny phy ) {
+ for( final PhylogenyNodeIterator it = phy.iteratorPostorder(); it.hasNext(); ) {
+ it.next().setIndicator( ( byte ) 0 );
+ }
+ }
+
+ /**
+ * Convenience method.
+ * Sets value for the first confidence value (created if not present, values overwritten otherwise).
+ */
+ public static void setBootstrapConfidence( final PhylogenyNode node, final double bootstrap_confidence_value ) {
+ setConfidence( node, bootstrap_confidence_value, "bootstrap" );
+ }
+
+ public static void setBranchColorValue( final PhylogenyNode node, final Color color ) {
+ if ( node.getBranchData().getBranchColor() == null ) {
+ node.getBranchData().setBranchColor( new BranchColor() );
+ }
+ node.getBranchData().getBranchColor().setValue( color );
+ }
+
+ /**
+ * Convenience method
+ */
+ public static void setBranchWidthValue( final PhylogenyNode node, final double branch_width_value ) {
+ node.getBranchData().setBranchWidth( new BranchWidth( branch_width_value ) );
+ }
+
+ /**
+ * Convenience method.
+ * Sets value for the first confidence value (created if not present, values overwritten otherwise).
+ */
+ public static void setConfidence( final PhylogenyNode node, final double confidence_value ) {
+ setConfidence( node, confidence_value, "" );
+ }
+
+ /**
+ * Convenience method.
+ * Sets value for the first confidence value (created if not present, values overwritten otherwise).
+ */
+ public static void setConfidence( final PhylogenyNode node, final double confidence_value, final String type ) {
+ Confidence c = null;
+ if ( node.getBranchData().getNumberOfConfidences() > 0 ) {
+ c = node.getBranchData().getConfidence( 0 );
+ }
+ else {
+ c = new Confidence();
+ node.getBranchData().addConfidence( c );
+ }
+ c.setType( type );
+ c.setValue( confidence_value );
+ }
+
+ public static void setScientificName( final PhylogenyNode node, final String scientific_name ) {
+ if ( !node.getNodeData().isHasTaxonomy() ) {
+ node.getNodeData().setTaxonomy( new Taxonomy() );
+ }
+ node.getNodeData().getTaxonomy().setScientificName( scientific_name );
+ }
+
+ /**
+ * Convenience method to set the taxonomy code of a phylogeny node.
+ *
+ *
+ * @param node
+ * @param taxonomy_code
+ * @throws PhyloXmlDataFormatException
+ */
+ public static void setTaxonomyCode( final PhylogenyNode node, final String taxonomy_code )
+ throws PhyloXmlDataFormatException {
+ if ( !node.getNodeData().isHasTaxonomy() ) {
+ node.getNodeData().setTaxonomy( new Taxonomy() );
+ }
+ node.getNodeData().getTaxonomy().setTaxonomyCode( taxonomy_code );
+ }
+
+ final static public void sortNodeDescendents( final PhylogenyNode node, final DESCENDANT_SORT_PRIORITY pri ) {
+ Comparator<PhylogenyNode> c;
+ switch ( pri ) {
+ case SEQUENCE:
+ c = new PhylogenyNodeSortSequencePriority();
+ break;
+ case NODE_NAME:
+ c = new PhylogenyNodeSortNodeNamePriority();
+ break;
+ default:
+ c = new PhylogenyNodeSortTaxonomyPriority();
+ }
+ final List<PhylogenyNode> descs = node.getDescendants();
+ Collections.sort( descs, c );
+ int i = 0;
+ for( final PhylogenyNode desc : descs ) {
+ node.setChildNode( i++, desc );
+ }
+ }
+
+ /**
+ * Removes from Phylogeny to_be_stripped all external Nodes which are
+ * associated with a species NOT found in Phylogeny reference.
+ *
+ * @param reference
+ * a reference Phylogeny
+ * @param to_be_stripped
+ * Phylogeny to be stripped
+ * @return nodes removed from to_be_stripped
+ */
+ public static List<PhylogenyNode> taxonomyBasedDeletionOfExternalNodes( final Phylogeny reference,
+ final Phylogeny to_be_stripped ) {
+ final Set<String> ref_ext_taxo = new HashSet<String>();
+ for( final PhylogenyNodeIterator it = reference.iteratorExternalForward(); it.hasNext(); ) {
+ final PhylogenyNode n = it.next();
+ if ( !n.getNodeData().isHasTaxonomy() ) {
+ throw new IllegalArgumentException( "no taxonomic data in node: " + n );
+ }
+ if ( !ForesterUtil.isEmpty( n.getNodeData().getTaxonomy().getScientificName() ) ) {
+ ref_ext_taxo.add( n.getNodeData().getTaxonomy().getScientificName() );
+ }
+ if ( !ForesterUtil.isEmpty( n.getNodeData().getTaxonomy().getTaxonomyCode() ) ) {
+ ref_ext_taxo.add( n.getNodeData().getTaxonomy().getTaxonomyCode() );
+ }
+ if ( ( n.getNodeData().getTaxonomy().getIdentifier() != null )
+ && !ForesterUtil.isEmpty( n.getNodeData().getTaxonomy().getIdentifier().getValue() ) ) {
+ ref_ext_taxo.add( n.getNodeData().getTaxonomy().getIdentifier().getValuePlusProvider() );
+ }
+ }
+ final ArrayList<PhylogenyNode> nodes_to_delete = new ArrayList<PhylogenyNode>();
+ for( final PhylogenyNodeIterator it = to_be_stripped.iteratorExternalForward(); it.hasNext(); ) {
+ final PhylogenyNode n = it.next();
+ if ( !n.getNodeData().isHasTaxonomy() ) {
+ nodes_to_delete.add( n );
+ }
+ else if ( !( ref_ext_taxo.contains( n.getNodeData().getTaxonomy().getScientificName() ) )
+ && !( ref_ext_taxo.contains( n.getNodeData().getTaxonomy().getTaxonomyCode() ) )
+ && !( ( n.getNodeData().getTaxonomy().getIdentifier() != null ) && ref_ext_taxo
+ .contains( n.getNodeData().getTaxonomy().getIdentifier().getValuePlusProvider() ) ) ) {
+ nodes_to_delete.add( n );
+ }
+ }
+ for( final PhylogenyNode n : nodes_to_delete ) {
+ to_be_stripped.deleteSubtree( n, true );
+ }
+ to_be_stripped.clearHashIdToNodeMap();
+ to_be_stripped.externalNodesHaveChanged();
+ return nodes_to_delete;
+ }
+
+ final static public void transferInternalNamesToConfidenceValues( final Phylogeny phy,
+ final String confidence_type ) {
+ final PhylogenyNodeIterator it = phy.iteratorPostorder();
+ while ( it.hasNext() ) {
+ final PhylogenyNode n = it.next();
+ if ( !n.isExternal() && !ForesterUtil.isEmpty( n.getName() ) ) {
+ double value = -1;
+ try {
+ value = Double.parseDouble( n.getName() );
+ }
+ catch ( final NumberFormatException e ) {
+ throw new IllegalArgumentException( "failed to parse number from [" + n.getName() + "]: "
+ + e.getLocalizedMessage() );
+ }
+ if ( value >= 0.0 ) {
+ n.getBranchData().addConfidence( new Confidence( value, confidence_type ) );
+ n.setName( "" );
+ }
+ }
+ }
+ }
+
+ final static public boolean isInternalNamesLookLikeConfidences( final Phylogeny phy ) {
+ final PhylogenyNodeIterator it = phy.iteratorPostorder();
+ while ( it.hasNext() ) {
+ final PhylogenyNode n = it.next();
+ if ( !n.isExternal() && !n.isRoot() ) {
+ if ( !ForesterUtil.isEmpty( n.getName() ) ) {
+ double value = -1;
+ try {
+ value = Double.parseDouble( n.getName() );
+ }
+ catch ( final NumberFormatException e ) {
+ return false;
+ }
+ if ( ( value < 0.0 ) || ( value > 100 ) ) {
+ return false;
+ }
+ }
+ }
+ }
+ return true;
+ }
+
+ final static public void transferInternalNodeNamesToConfidence( final Phylogeny phy,
+ final String confidence_type ) {
+ final PhylogenyNodeIterator it = phy.iteratorPostorder();
+ while ( it.hasNext() ) {
+ transferInternalNodeNameToConfidence( confidence_type, it.next() );
+ }
+ }
+
+ private static void transferInternalNodeNameToConfidence( final String confidence_type, final PhylogenyNode n ) {
+ if ( !n.isExternal() && !n.getBranchData().isHasConfidences() ) {
+ if ( !ForesterUtil.isEmpty( n.getName() ) ) {
+ double d = -1.0;
+ try {
+ d = Double.parseDouble( n.getName() );
+ }
+ catch ( final Exception e ) {
+ d = -1.0;
+ }
+ if ( d >= 0.0 ) {
+ n.getBranchData().addConfidence( new Confidence( d, confidence_type ) );
+ n.setName( "" );
+ }
+ }
+ }
+ }
+
+ final static public void transferNodeNameToField( final Phylogeny phy,
+ final PhylogenyNodeField field,
+ final boolean external_only )
+ throws PhyloXmlDataFormatException {
+ final PhylogenyNodeIterator it = phy.iteratorPostorder();
+ while ( it.hasNext() ) {
+ final PhylogenyNode n = it.next();
+ if ( external_only && n.isInternal() ) {
+ continue;
+ }
+ final String name = n.getName().trim();
+ if ( !ForesterUtil.isEmpty( name ) ) {
+ switch ( field ) {
+ case TAXONOMY_CODE:
+ n.setName( "" );
+ setTaxonomyCode( n, name );
+ break;
+ case TAXONOMY_SCIENTIFIC_NAME:
+ n.setName( "" );
+ if ( !n.getNodeData().isHasTaxonomy() ) {
+ n.getNodeData().setTaxonomy( new Taxonomy() );
+ }
+ n.getNodeData().getTaxonomy().setScientificName( name );
+ break;
+ case TAXONOMY_COMMON_NAME:
+ n.setName( "" );
+ if ( !n.getNodeData().isHasTaxonomy() ) {
+ n.getNodeData().setTaxonomy( new Taxonomy() );
+ }
+ n.getNodeData().getTaxonomy().setCommonName( name );
+ break;
+ case SEQUENCE_SYMBOL:
+ n.setName( "" );
+ if ( !n.getNodeData().isHasSequence() ) {
+ n.getNodeData().setSequence( new Sequence() );
+ }
+ n.getNodeData().getSequence().setSymbol( name );
+ break;
+ case SEQUENCE_NAME:
+ n.setName( "" );
+ if ( !n.getNodeData().isHasSequence() ) {
+ n.getNodeData().setSequence( new Sequence() );
+ }
+ n.getNodeData().getSequence().setName( name );
+ break;
+ case TAXONOMY_ID_UNIPROT_1: {
+ if ( !n.getNodeData().isHasTaxonomy() ) {
+ n.getNodeData().setTaxonomy( new Taxonomy() );
+ }
+ String id = name;
+ final int i = name.indexOf( '_' );
+ if ( i > 0 ) {
+ id = name.substring( 0, i );
+ }
+ else {
+ n.setName( "" );
+ }
+ n.getNodeData().getTaxonomy()
+ .setIdentifier( new Identifier( id, PhyloXmlUtil.UNIPROT_TAX_PROVIDER ) );
+ break;
+ }
+ case TAXONOMY_ID_UNIPROT_2: {
+ if ( !n.getNodeData().isHasTaxonomy() ) {
+ n.getNodeData().setTaxonomy( new Taxonomy() );
+ }
+ String id = name;
+ final int i = name.indexOf( '_' );
+ if ( i > 0 ) {
+ id = name.substring( i + 1, name.length() );
+ }
+ else {
+ n.setName( "" );
+ }
+ n.getNodeData().getTaxonomy()
+ .setIdentifier( new Identifier( id, PhyloXmlUtil.UNIPROT_TAX_PROVIDER ) );
+ break;