moved to: https://sites.google.com/site/cmzmasek/home/software/forester
[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: https://sites.google.com/site/cmzmasek/home/software/forester
25
26 package org.forester.development;
27
28 import java.util.Random;
29
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;
34
35 public final class DevelopmentTools {
36
37     /**
38      * Creates a completely unbalanced Phylogeny with i external nodes.
39      * 
40      * @return a newly created unbalanced Phylogeny
41      */
42     // public static Phylogeny createUnbalancedTree( int i ) {
43     //
44     // Phylogeny t1 = null;
45     //
46     // try {
47     // PhylogenyFactory factory = ParserBasedPhylogenyFactory.getInstance();
48     // t1 = factory.create( ":S=", new SimpleNHXParser() );
49     //            
50     // t1.setRooted( true );
51     //
52     // for ( int j = 1; j < i; ++j ) {
53     // t1.addNodeAndConnect( "", "" );
54     // }
55     // t1.setRoot( t1.getFirstExternalNode().getRoot() );
56     // t1.calculateRealHeight();
57     // }
58     //
59     // catch ( PhylogenyParserException e ) {
60     // System.err
61     // .println( "Unexpected exception during \"createUnbalancedTree\":" );
62     // System.err.println( e.toString() );
63     // System.exit( -1 );
64     // }
65     //
66     // return t1;
67     // }
68     private DevelopmentTools() {
69     }
70
71     /**
72      * Creates a completely balanced rooted phylogeny with a given number of levels and
73      * children per node.
74      * 
75      * @param levels
76      * @param children_per_node
77      * @return a completely balanced rooted phylogeny
78      */
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();
82         p.setRoot( root );
83         p.setRooted( true );
84         DevelopmentTools.createBalancedPhylogenyRecursion( levels, children_per_node, root );
85         return p;
86     }
87
88     private static void createBalancedPhylogenyRecursion( int current_level,
89                                                           final int children_per_node,
90                                                           final PhylogenyNode current_node ) {
91         if ( current_level > 0 ) {
92             --current_level;
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 );
97             }
98         }
99     }
100
101     /**
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, ...
105      */
106     public static void intervalNumberSpecies( final Phylogeny t, final int i ) {
107         if ( ( t == null ) || t.isEmpty() ) {
108             return;
109         }
110         PhylogenyNode n = t.getFirstExternalNode();
111         int j = 1;
112         boolean odd = true;
113         while ( n != null ) {
114             if ( odd ) {
115                 PhylogenyMethods.setScientificName( n, j + "" );
116             }
117             else {
118                 PhylogenyMethods.setScientificName( n, ( j + i ) + "" );
119                 j++;
120             }
121             odd = !odd;
122             n = n.getNextExternalNode();
123         }
124     }
125
126     /**
127      * Sets the species namea of the external Nodes of Phylogeny t to descending
128      * integers, ending with 1.
129      */
130     public static void numberSpeciesInDescOrder( final Phylogeny t ) {
131         if ( ( t == null ) || t.isEmpty() ) {
132             return;
133         }
134         PhylogenyNode n = t.getFirstExternalNode();
135         int j = t.getRoot().getNumberOfExternalNodes();
136         while ( n != null ) {
137             try {
138                 PhylogenyMethods.setTaxonomyCode( n, j + "" );
139             }
140             catch ( final PhyloXmlDataFormatException e ) {
141                 e.printStackTrace();
142             }
143             j--;
144             n = n.getNextExternalNode();
145         }
146     }
147
148     /**
149      * Sets the species namea of the external Nodes of Phylogeny t to ascending
150      * integers, starting with 1.
151      */
152     public static void numberSpeciesInOrder( final Phylogeny t ) {
153         if ( ( t == null ) || t.isEmpty() ) {
154             return;
155         }
156         PhylogenyNode n = t.getFirstExternalNode();
157         int j = 1;
158         while ( n != null ) {
159             PhylogenyMethods.setScientificName( n, j + "" );
160             j++;
161             n = n.getNextExternalNode();
162         }
163     }
164
165     /**
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.
168      * 
169      * @param t
170      *            whose external species names are to be randomized
171      * @param min
172      *            minimal value for random numbers
173      * @param max
174      *            maximum value for random numbers
175      */
176     public static void randomizeSpecies( final int min, final int max, final Phylogeny t ) {
177         if ( ( t == null ) || t.isEmpty() ) {
178             return;
179         }
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 ) + "";
186             try {
187                 PhylogenyMethods.setTaxonomyCode( n, code );
188             }
189             catch ( final PhyloXmlDataFormatException e ) {
190                 e.printStackTrace();
191             }
192             n = n.getNextExternalNode();
193         }
194     }
195 }