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.development;
28 import java.util.Random;
30 import org.forester.phylogeny.Phylogeny;
31 import org.forester.phylogeny.PhylogenyMethods;
32 import org.forester.phylogeny.PhylogenyNode;
34 public final class DevelopmentTools {
37 * Creates a completely unbalanced Phylogeny with i external nodes.
39 * @return a newly created unbalanced Phylogeny
41 // public static Phylogeny createUnbalancedTree( int i ) {
43 // Phylogeny t1 = null;
46 // PhylogenyFactory factory = ParserBasedPhylogenyFactory.getInstance();
47 // t1 = factory.create( ":S=", new SimpleNHXParser() );
49 // t1.setRooted( true );
51 // for ( int j = 1; j < i; ++j ) {
52 // t1.addNodeAndConnect( "", "" );
54 // t1.setRoot( t1.getFirstExternalNode().getRoot() );
55 // t1.calculateRealHeight();
58 // catch ( PhylogenyParserException e ) {
60 // .println( "Unexpected exception during \"createUnbalancedTree\":" );
61 // System.err.println( e.toString() );
67 private DevelopmentTools() {
71 * Creates a completely balanced rooted phylogeny with a given number of levels and
75 * @param children_per_node
76 * @return a completely balanced rooted phylogeny
78 public static Phylogeny createBalancedPhylogeny( final int levels, final int children_per_node ) {
79 final PhylogenyNode root = new PhylogenyNode();
80 final Phylogeny p = new Phylogeny();
83 DevelopmentTools.createBalancedPhylogenyRecursion( levels, children_per_node, root );
87 private static void createBalancedPhylogenyRecursion( int current_level,
88 final int children_per_node,
89 final PhylogenyNode current_node ) {
90 if ( current_level > 0 ) {
92 for( int i = 0; i < children_per_node; ++i ) {
93 final PhylogenyNode new_node = new PhylogenyNode();
94 current_node.addAsChild( new_node );
95 DevelopmentTools.createBalancedPhylogenyRecursion( current_level, children_per_node, new_node );
101 * Sets the species name of the external Nodes of Phylogeny t to 1, 1+i, 2,
102 * 2+i, 3, 3+i, .... Examples: i=2: 1, 3, 2, 4 i=4: 1, 5, 2, 6, 3, 7, 4, 8
103 * i=8: 1, 9, 2, 10, 3, 11, 4, 12, ...
105 public static void intervalNumberSpecies( final Phylogeny t, final int i ) {
106 if ( ( t == null ) || t.isEmpty() ) {
109 PhylogenyNode n = t.getFirstExternalNode();
112 while ( n != null ) {
114 PhylogenyMethods.setScientificName( n, j + "" );
117 PhylogenyMethods.setScientificName( n, ( j + i ) + "" );
121 n = n.getNextExternalNode();
126 * Sets the species namea of the external Nodes of Phylogeny t to descending
127 * integers, ending with 1.
129 public static void numberSpeciesInDescOrder( final Phylogeny t ) {
130 if ( ( t == null ) || t.isEmpty() ) {
133 PhylogenyNode n = t.getFirstExternalNode();
134 int j = t.getRoot().getNumberOfExternalNodes();
135 while ( n != null ) {
136 PhylogenyMethods.setTaxonomyCode( n, j + "" );
138 n = n.getNextExternalNode();
143 * Sets the species namea of the external Nodes of Phylogeny t to ascending
144 * integers, starting with 1.
146 public static void numberSpeciesInOrder( final Phylogeny t ) {
147 if ( ( t == null ) || t.isEmpty() ) {
150 PhylogenyNode n = t.getFirstExternalNode();
152 while ( n != null ) {
153 PhylogenyMethods.setScientificName( n, j + "" );
155 n = n.getNextExternalNode();
160 * Sets the species names of the external Nodes of Phylogeny t to a random
161 * positive integer number between (and including) min and max.
164 * whose external species names are to be randomized
166 * minimal value for random numbers
168 * maximum value for random numbers
170 public static void randomizeSpecies( final int min, final int max, final Phylogeny t ) {
171 if ( ( t == null ) || t.isEmpty() ) {
174 final int mi = Math.abs( min );
175 final int ma = Math.abs( max );
176 final Random r = new Random();
177 PhylogenyNode n = t.getFirstExternalNode();
178 while ( n != null ) {
179 final String code = ( ( Math.abs( r.nextInt() ) % ( ma - mi + 1 ) ) + mi ) + "";
180 PhylogenyMethods.setTaxonomyCode( n, code );
181 n = n.getNextExternalNode();