Debian Codegraph @

src/dr/oldevomodel/substmodel/SubstitutionModelUtils.java

raw symbols SCIP
1 /*
2 * SubstitutionModelUtils.java
3 *
4 * Copyright (c) 2002-2015 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.oldevomodel.substmodel;
27
28 import dr.evolution.datatype.DataType;
29
30 import java.text.NumberFormat;
31
32 /**
33 * @author Alexei Drummond
34 */
35 public class SubstitutionModelUtils {
36
37 public static String toString(double[] matrix, DataType dataType, boolean isTriangle, int indent) {
38
39 int size = dataType.getStateCount();
40
41 int expectedSize = size * size;
42 if (isTriangle) expectedSize = (size * size) / 2 - size;
43
44 assert matrix.length == expectedSize;
45
46 double[][] amat = new double[size][size];
47
48 int k = 0;
49 for (int i = 0; i < size; i++) {
50 for (int j = isTriangle ? i + 1 : 0; j < size; j++) {
51 amat[i][j] = matrix[k];
52 if (isTriangle) amat[j][i] = matrix[k];
53 k += 1;
54 }
55 }
56 return toString(amat, dataType, indent);
57 }
58
59 public static String frequenciesToString(double[] pi, DataType dataType, int indent) {
60
61 int stateCount = pi.length;
62 final int columnWidth = 7;
63
64 StringBuilder builder = new StringBuilder();
65 builder.append(header(dataType, indent, columnWidth));
66 builder.append("\n");
67
68 // write body
69 NumberFormat formatter = NumberFormat.getNumberInstance();
70 formatter.setMaximumFractionDigits(3);
71 builder.append(spaces(indent));
72 builder.append(" {");
73 for (int j = 0; j < stateCount; j++) {
74
75 builder.append(padded(formatter.format(pi[j]), columnWidth));
76 }
77 builder.append("}\n");
78 return builder.toString();
79 }
80
81
82 public static String toString(double[][] matrix, DataType dataType, int indent) {
83
84 int stateCount = matrix.length;
85 final int columnWidth = 7;
86
87 StringBuilder builder = new StringBuilder();
88 builder.append(header(dataType, indent, columnWidth));
89 builder.append("\n");
90
91 // write matrix body
92
93 NumberFormat formatter = NumberFormat.getNumberInstance();
94 formatter.setMaximumFractionDigits(3);
95 for (int i = 0; i < stateCount; i++) {
96 builder.append(spaces(indent));
97 builder.append(dataType.getChar(i)).append(" ");
98 if (i == 0) {
99 builder.append("/");
100 } else if (i == stateCount - 1) {
101 builder.append("\\");
102 } else builder.append("|");
103 for (int j = 0; j < stateCount; j++) {
104
105 builder.append(padded(formatter.format(matrix[i][j]), columnWidth));
106 }
107 if (i == 0) {
108 builder.append("\\\n");
109 } else if (i == stateCount - 1) {
110 builder.append("/\n");
111 } else builder.append("|\n");
112 }
113 return builder.toString();
114 }
115
116 private static String spaces(int indent) {
117 StringBuilder builder = new StringBuilder();
118 for (int i = 0; i < indent; i++) {
119 builder.append(' ');
120 }
121 return builder.toString();
122 }
123
124 private static String padded(String s, int width) {
125 int extra = width - s.length();
126 for (int i = 0; i < (extra / 2); i++) {
127 s = " " + s;
128 }
129 extra = width - s.length();
130 for (int i = 0; i < extra; i++) {
131 s += " ";
132 }
133 return s;
134 }
135
136 private static String header(DataType dataType, int indent, int columnWidth) {
137 // write header
138 StringBuilder builder = new StringBuilder();
139 builder.append(spaces(indent)).append(" ");
140 for (int i = 0; i < dataType.getStateCount(); i++) {
141 builder.append(padded(dataType.getChar(i) + "", columnWidth));
142 }
143 return builder.toString();
144 }
145 }