Debian Codegraph @

src/dr/evomodelxml/CSVExporterParser.java

raw symbols SCIP
1 /*
2 * CSVExporterParser.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.evomodelxml;
27
28 import dr.inference.model.Parameter;
29 import dr.util.FileHelpers;
30 import dr.util.TabularData;
31 import dr.xml.*;
32
33 import java.io.File;
34 import java.io.FileWriter;
35 import java.io.IOException;
36 import java.io.PrintWriter;
37 import java.util.ArrayList;
38 import java.util.List;
39
40 /**
41 * @author Joseph Heled
42 * <p/>
43 * Very lame so far. need checks for possible qouting of elements.
44 */
45 public class CSVExporterParser extends AbstractXMLObjectParser {
46
47 final static public String CSV_EXPORT = "CSVexport";
48
49 public String getParserName() {
50 return CSV_EXPORT;
51 }
52
53 public String getParserDescription() {
54 return "Write tabular data as an CSV file.";
55 }
56
57 public Object parseXMLObject(XMLObject xo) throws XMLParseException {
58 final String fileName = xo.getStringAttribute(FILE_NAME);
59
60 final PrintWriter writer = XMLParser.getFilePrintWriter(xo, fileName);
61
62 final String sep = xo.getAttribute(SEPARATOR, "\t");
63
64 for (int k = 0; k < xo.getChildCount(); ++k) {
65 final XMLObject columns = (XMLObject) xo.getChild(k);
66 if (columns.getName().equals(COLUMNS)) {
67 TabularData source = (TabularData) columns.getChild(TabularData.class);
68 // look for columns
69 List<Integer> iColumns = new ArrayList<Integer>();
70 List<Parameter[]> adds = new ArrayList<Parameter[]>();
71 int maxAdds = 0;
72
73 for (int nc = 0; nc < columns.getChildCount(); ++nc) {
74 final Object child = columns.getChild(nc);
75 if (child instanceof XMLObject) {
76 final XMLObject column = (XMLObject) child;
77 if (column.getName().equals(COLUMN)) {
78 final String name = column.getStringAttribute(COLUMN_NAME);
79 final int n = source.getColumn(name);
80 if (n < 0) {
81 throw new XMLParseException("column '" + name + "' not found in log.");
82 }
83 iColumns.add(n);
84
85 final int nAddtional = column.getChildCount();
86 Parameter[] additionals = nAddtional > 0 ? new Parameter[nAddtional] : null;
87 for (int nc1 = 0; nc1 < nAddtional; ++nc1) {
88 additionals[nc1] = (Parameter) column.getChild(nc1);
89 }
90 maxAdds = Math.max(maxAdds, nAddtional);
91
92 adds.add(additionals);
93 }
94 }
95 }
96
97 final boolean hasAllAttribute = columns.hasAttribute(ALL_COLUMNS);
98
99
100 if (iColumns.size() == 0) {
101 // when no columns specified - default is all
102 if (!hasAllAttribute || columns.getBooleanAttribute(ALL_COLUMNS)) {
103 for (int nc = 0; nc < source.nColumns(); ++nc) {
104 iColumns.add(nc);
105 adds.add(null);
106 }
107 }
108 } else {
109 // some column specification
110 if (hasAllAttribute && columns.getBooleanAttribute(ALL_COLUMNS)) {
111 for (int nc = 0; nc < source.nColumns(); ++nc) {
112 if (!iColumns.contains(nc)) {
113 iColumns.add(nc);
114 adds.add(null);
115 }
116 }
117 }
118 }
119
120 if (columns.hasAttribute(AS_ROWS) && columns.getBooleanAttribute(AS_ROWS)) {
121 for (int nc = 0; nc < iColumns.size(); ++nc) {
122 writer.print(source.columnName(iColumns.get(nc)));
123
124 for (int nr = 0; nr < source.nRows(); ++nr) {
125 final Object value = source.data(nr, iColumns.get(nc));
126 writer.print(sep);
127 writer.print(value);
128
129 }
130 for (int nr = 0; nr < maxAdds; ++nr) {
131 final Parameter[] addsnc = adds.get(nc);
132 if (addsnc != null && nr < addsnc.length) {
133 final Object value = addsnc[nr].getParameterValues()[0];
134 writer.print(sep);
135 writer.print(value);
136 }
137 }
138 writer.println();
139 }
140
141 } else {
142 for (int nc = 0; nc < iColumns.size(); ++nc) {
143 if (nc > 0) {
144 writer.print(sep);
145 }
146 writer.print(source.columnName(iColumns.get(nc)));
147 }
148 writer.println();
149
150 for (int nr = 0; nr < source.nRows(); ++nr) {
151 for (int nc = 0; nc < iColumns.size(); ++nc) {
152 if (nc > 0) {
153 writer.print(sep);
154 }
155 final Object value = source.data(nr, iColumns.get(nc));
156 writer.print(value);
157 }
158 writer.println();
159 }
160
161 for (int nr = 0; nr < maxAdds; ++nr) {
162 for (int nc = 0; nc < iColumns.size(); ++nc) {
163 if (nc > 0) {
164 writer.print(sep);
165 }
166 final Parameter[] addsnc = adds.get(nc);
167 if (addsnc != null && nr < addsnc.length) {
168 final Object value = addsnc[nr].getParameterValues()[0];
169 writer.print(value);
170 }
171 }
172 writer.println();
173 }
174 }
175 }
176 }
177 writer.close();
178 return null;
179 }
180
181 public static final String FILE_NAME = "fileName";
182 public static final String SEPARATOR = "separator";
183 public static final String COLUMNS = "columns";
184 public static final String ALL_COLUMNS = "all";
185 public static final String AS_ROWS = "rows";
186 public static final String COLUMN = "CSVcolumn";
187 public static final String COLUMN_NAME = "name";
188
189 public XMLSyntaxRule[] getSyntaxRules() {
190 return new XMLSyntaxRule[]{
191 new StringAttributeRule(FILE_NAME,
192 "The name of a BEAST log file (can not include trees, which should be logged separately"),
193 new StringAttributeRule(SEPARATOR, "Values separator (default is tab)", true),
194 new ElementRule(COLUMNS, new XMLSyntaxRule[]{
195 AttributeRule.newBooleanRule(ALL_COLUMNS, true,
196 "Dump all columns. default is TRUE when no columns are specified, FALSE otherwise"),
197 AttributeRule.newBooleanRule(AS_ROWS, true,
198 "Write data in rows (default is columns)"),
199 new ElementRule(TabularData.class),
200 new ElementRule(COLUMN, new XMLSyntaxRule[]{
201 AttributeRule.newStringArrayRule(COLUMN_NAME),
202 new ElementRule(Parameter.class)
203 }, "column name", 0, Integer.MAX_VALUE)
204 }, "A subset of columns from one source", 1, Integer.MAX_VALUE)
205 };
206 }
207
208
209 public Class getReturnType() {
210 return CSVExporterParser.class; //TODO write CSVExporter
211 }
212 }