Debian Codegraph @

src/dr/evomodel/alloppnet/util/AlloppMisc.java

raw symbols SCIP
1 /*
2 * AlloppMisc.java
3 *
4 * Copyright (c) 2002-2017 Alexei Drummond, Andrew Rambaut and Marc Suchard
5 *
6 * This file is part of BEAST.
7 * See the NOTICE file distributed with this work for additional
8 * information regarding copyright ownership and licensing.
9 *
10 * BEAST is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU Lesser General Public License as
12 * published by the Free Software Foundation; either version 2
13 * of the License, or (at your option) any later version.
14 *
15 * BEAST is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU Lesser General Public License for more details.
19 *
20 * You should have received a copy of the GNU Lesser General Public
21 * License along with BEAST; if not, write to the
22 * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
23 * Boston, MA 02110-1301 USA
24 */
25
26 package dr.evomodel.alloppnet.util;
27
28 import java.util.Formatter;
29 import java.util.Iterator;
30
31 /**
32 *
33 * @author Graham Jones
34 * Date: 01/07/2011
35 */
36
37 import java.util.Locale;
38
39 import dr.evolution.tree.NodeRef;
40 import dr.evolution.tree.SimpleTree;
41 import dr.evolution.util.Taxon;
42 import dr.math.MathUtils;
43
44 import jebl.util.FixedBitSet;
45
46 public class AlloppMisc {
47
48
49 public static String FixedBitSetasText(FixedBitSet x) {
50 if (x == null) {
51 return "{?}";
52 }
53 StringBuilder rep = new StringBuilder();
54 rep.append("{");
55 for (int b = 0; b < x.cardinality() + FixedBitSet.complement(x).cardinality(); ++b) {
56 String comma = (b==0) ? "" : ",";
57 if (x.contains(b)) {
58 rep.append(comma + b);
59 } else {
60 rep.append(comma + " ");
61 }
62 }
63 rep.append("}");
64 return rep.toString();
65 }
66
67
68
69 public static String nonnegIn8Chars(double x)
70 {
71 StringBuilder s = new StringBuilder();
72 Formatter formatter = new Formatter(s, Locale.US);
73 if (x < 0) {
74 formatter.format("%8s", "NA");
75 } else if (x == 0.0) {
76 formatter.format("%8s", "zero");
77 } else if (x < 1e-3) {
78 formatter.format("%8.2e", x);
79 } else if (x < 9.999) {
80 formatter.format("%8.5f", x);
81 } else if (x < 99.99) {
82 formatter.format("%8.4f", x);
83 } else if (x < 999.9) {
84 formatter.format("%8.3f", x);
85 } else if (x < 9999) {
86 formatter.format("%8.2f", x);
87 } else {
88 formatter.format("%8.0f", x);
89 }
90 return s.toString();
91 }
92
93
94 public static String nonnegIntIn2Chars(int x)
95 {
96 StringBuilder s = new StringBuilder();
97 Formatter formatter = new Formatter(s, Locale.US);
98 if (x < 0) {
99 formatter.format("%2s", "NA");
100 } else {
101 formatter.format("%2d", x);
102 }
103 return s.toString();
104 }
105
106
107 public static String SimpleNodeAsText(SimpleTree stree, NodeRef node) {
108 String s = "" + node.getNumber() + " ";
109 while (s.length() < 3) { s += " "; }
110 int nch = stree.getChildCount(node);
111 if (nch> 0) {
112 assert(nch==2);
113 s += stree.getChild(node, 0).getNumber();
114 while (s.length() < 6) { s += " "; }
115 s += stree.getChild(node, 1).getNumber();
116 }
117 while (s.length() < 9) { s += " "; }
118 Taxon tx = stree.getNodeTaxon(node);
119 String taxonid = "*";
120 if (tx != null) {
121 taxonid = tx.getId();
122 if (taxonid == null || taxonid.length() == 0) {
123 taxonid = "*";
124 }
125
126 }
127 s += taxonid;
128 while (s.length() < 20) { s += " "; }
129
130 stree.getNodeHeight(node);
131 s += " height=";
132 s += stree.getNodeHeight(node);
133 Iterator iter = stree.getNodeAttributeNames(node);
134 if (iter != null) {
135 while (iter.hasNext()) {
136 String name = (String) iter.next();
137 s += " ";
138 s += name;
139 s += "=";
140 s += stree.getNodeAttribute(node, name);
141 }
142 }
143 return s;
144 }
145
146
147
148
149
150 public static double uniformInRange(double oldx, double min, double max, double halfwidth) {
151 assert halfwidth > 0.0;
152 assert halfwidth < 0.5;
153 assert min < max;
154 double change = MathUtils.uniform(-1.0, 1.0) * (max - min) * halfwidth;
155 double newx = oldx + change;
156 if (newx < min) { newx = 2*min - newx; }
157 if (newx > max) { newx = 2*max - newx; }
158 assert newx > min;
159 assert newx < max;
160 return newx;
161 }
162
163 }
164
165