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: https://sites.google.com/site/cmzmasek/home/software/forester
26 package org.forester.development;
28 import java.util.Random;
30 import org.forester.io.parsers.phyloxml.PhyloXmlDataFormatException;
31 import org.forester.phylogeny.Phylogeny;
32 import org.forester.phylogeny.PhylogenyMethods;
33 import org.forester.phylogeny.PhylogenyNode;
35 public final class DevelopmentTools {
38 * Creates a completely unbalanced Phylogeny with i external nodes.
40 * @return a newly created unbalanced Phylogeny
42 // public static Phylogeny createUnbalancedTree( int i ) {
44 // Phylogeny t1 = null;
47 // PhylogenyFactory factory = ParserBasedPhylogenyFactory.getInstance();
48 // t1 = factory.create( ":S=", new SimpleNHXParser() );
50 // t1.setRooted( true );
52 // for ( int j = 1; j < i; ++j ) {
53 // t1.addNodeAndConnect( "", "" );
55 // t1.setRoot( t1.getFirstExternalNode().getRoot() );
56 // t1.calculateRealHeight();
59 // catch ( PhylogenyParserException e ) {
61 // .println( "Unexpected exception during \"createUnbalancedTree\":" );
62 // System.err.println( e.toString() );
68 private DevelopmentTools() {
72 * Creates a completely balanced rooted phylogeny with a given number of levels and
76 * @param children_per_node
77 * @return a completely balanced rooted phylogeny
79 public static Phylogeny createBalancedPhylogeny( final int levels, final int children_per_node ) {
80 final PhylogenyNode root = new PhylogenyNode();
81 final Phylogeny p = new Phylogeny();
84 DevelopmentTools.createBalancedPhylogenyRecursion( levels, children_per_node, root );
88 private static void createBalancedPhylogenyRecursion( int current_level,
89 final int children_per_node,
90 final PhylogenyNode current_node ) {
91 if ( current_level > 0 ) {
93 for( int i = 0; i < children_per_node; ++i ) {
94 final PhylogenyNode new_node = new PhylogenyNode();
95 current_node.addAsChild( new_node );
96 DevelopmentTools.createBalancedPhylogenyRecursion( current_level, children_per_node, new_node );
102 * Sets the species name of the external Nodes of Phylogeny t to 1, 1+i, 2,
103 * 2+i, 3, 3+i, .... Examples: i=2: 1, 3, 2, 4 i=4: 1, 5, 2, 6, 3, 7, 4, 8
104 * i=8: 1, 9, 2, 10, 3, 11, 4, 12, ...
106 public static void intervalNumberSpecies( final Phylogeny t, final int i ) {
107 if ( ( t == null ) || t.isEmpty() ) {
110 PhylogenyNode n = t.getFirstExternalNode();
113 while ( n != null ) {
115 PhylogenyMethods.setScientificName( n, j + "" );
118 PhylogenyMethods.setScientificName( n, ( j + i ) + "" );
122 n = n.getNextExternalNode();
127 * Sets the species namea of the external Nodes of Phylogeny t to descending
128 * integers, ending with 1.
130 public static void numberSpeciesInDescOrder( final Phylogeny t ) {
131 if ( ( t == null ) || t.isEmpty() ) {
134 PhylogenyNode n = t.getFirstExternalNode();
135 int j = t.getRoot().getNumberOfExternalNodes();
136 while ( n != null ) {
138 PhylogenyMethods.setTaxonomyCode( n, j + "" );
140 catch ( final PhyloXmlDataFormatException e ) {
144 n = n.getNextExternalNode();
149 * Sets the species namea of the external Nodes of Phylogeny t to ascending
150 * integers, starting with 1.
152 public static void numberSpeciesInOrder( final Phylogeny t ) {
153 if ( ( t == null ) || t.isEmpty() ) {
156 PhylogenyNode n = t.getFirstExternalNode();
158 while ( n != null ) {
159 PhylogenyMethods.setScientificName( n, j + "" );
161 n = n.getNextExternalNode();
166 * Sets the species names of the external Nodes of Phylogeny t to a random
167 * positive integer number between (and including) min and max.
170 * whose external species names are to be randomized
172 * minimal value for random numbers
174 * maximum value for random numbers
176 public static void randomizeSpecies( final int min, final int max, final Phylogeny t ) {
177 if ( ( t == null ) || t.isEmpty() ) {
180 final int mi = Math.abs( min );
181 final int ma = Math.abs( max );
182 final Random r = new Random();
183 PhylogenyNode n = t.getFirstExternalNode();
184 while ( n != null ) {
185 final String code = ( ( Math.abs( r.nextInt() ) % ( ( ma - mi ) + 1 ) ) + mi ) + "";
187 PhylogenyMethods.setTaxonomyCode( n, code );
189 catch ( final PhyloXmlDataFormatException e ) {
192 n = n.getNextExternalNode();