clean up
[jalview.git] / forester / java / src / org / forester / development / DevelopmentTools.java
1 // $Id:
2 // FORESTER -- software libraries and applications
3 // for evolutionary biology research and applications.
4 //
5 // Copyright (C) 2008-2009 Christian M. Zmasek
6 // Copyright (C) 2008-2009 Burnham Institute for Medical Research
7 // All rights reserved
8 //
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.
13 //
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.
18 //
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
22 //
23 // Contact: phylosoft @ gmail . com
24 // WWW: www.phylosoft.org/forester
25
26 package org.forester.development;
27
28 import java.util.Random;
29
30 import org.forester.phylogeny.Phylogeny;
31 import org.forester.phylogeny.PhylogenyMethods;
32 import org.forester.phylogeny.PhylogenyNode;
33
34 public final class DevelopmentTools {
35
36     /**
37      * Creates a completely unbalanced Phylogeny with i external nodes.
38      * 
39      * @return a newly created unbalanced Phylogeny
40      */
41     // public static Phylogeny createUnbalancedTree( int i ) {
42     //
43     // Phylogeny t1 = null;
44     //
45     // try {
46     // PhylogenyFactory factory = ParserBasedPhylogenyFactory.getInstance();
47     // t1 = factory.create( ":S=", new SimpleNHXParser() );
48     //            
49     // t1.setRooted( true );
50     //
51     // for ( int j = 1; j < i; ++j ) {
52     // t1.addNodeAndConnect( "", "" );
53     // }
54     // t1.setRoot( t1.getFirstExternalNode().getRoot() );
55     // t1.calculateRealHeight();
56     // }
57     //
58     // catch ( PhylogenyParserException e ) {
59     // System.err
60     // .println( "Unexpected exception during \"createUnbalancedTree\":" );
61     // System.err.println( e.toString() );
62     // System.exit( -1 );
63     // }
64     //
65     // return t1;
66     // }
67     private DevelopmentTools() {
68     }
69
70     /**
71      * Creates a completely balanced rooted phylogeny with a given number of levels and
72      * children per node.
73      * 
74      * @param levels
75      * @param children_per_node
76      * @return a completely balanced rooted phylogeny
77      */
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();
81         p.setRoot( root );
82         p.setRooted( true );
83         DevelopmentTools.createBalancedPhylogenyRecursion( levels, children_per_node, root );
84         return p;
85     }
86
87     private static void createBalancedPhylogenyRecursion( int current_level,
88                                                           final int children_per_node,
89                                                           final PhylogenyNode current_node ) {
90         if ( current_level > 0 ) {
91             --current_level;
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 );
96             }
97         }
98     }
99
100     /**
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, ...
104      */
105     public static void intervalNumberSpecies( final Phylogeny t, final int i ) {
106         if ( ( t == null ) || t.isEmpty() ) {
107             return;
108         }
109         PhylogenyNode n = t.getFirstExternalNode();
110         int j = 1;
111         boolean odd = true;
112         while ( n != null ) {
113             if ( odd ) {
114                 PhylogenyMethods.setScientificName( n, j + "" );
115             }
116             else {
117                 PhylogenyMethods.setScientificName( n, ( j + i ) + "" );
118                 j++;
119             }
120             odd = !odd;
121             n = n.getNextExternalNode();
122         }
123     }
124
125     /**
126      * Sets the species namea of the external Nodes of Phylogeny t to descending
127      * integers, ending with 1.
128      */
129     public static void numberSpeciesInDescOrder( final Phylogeny t ) {
130         if ( ( t == null ) || t.isEmpty() ) {
131             return;
132         }
133         PhylogenyNode n = t.getFirstExternalNode();
134         int j = t.getRoot().getNumberOfExternalNodes();
135         while ( n != null ) {
136             PhylogenyMethods.setTaxonomyCode( n, j + "" );
137             j--;
138             n = n.getNextExternalNode();
139         }
140     }
141
142     /**
143      * Sets the species namea of the external Nodes of Phylogeny t to ascending
144      * integers, starting with 1.
145      */
146     public static void numberSpeciesInOrder( final Phylogeny t ) {
147         if ( ( t == null ) || t.isEmpty() ) {
148             return;
149         }
150         PhylogenyNode n = t.getFirstExternalNode();
151         int j = 1;
152         while ( n != null ) {
153             PhylogenyMethods.setScientificName( n, j + "" );
154             j++;
155             n = n.getNextExternalNode();
156         }
157     }
158
159     /**
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.
162      * 
163      * @param t
164      *            whose external species names are to be randomized
165      * @param min
166      *            minimal value for random numbers
167      * @param max
168      *            maximum value for random numbers
169      */
170     public static void randomizeSpecies( final int min, final int max, final Phylogeny t ) {
171         if ( ( t == null ) || t.isEmpty() ) {
172             return;
173         }
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();
182         }
183     }
184 }