| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 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 |
|
| 42 |
|
| 43 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 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; |
| 211 |
} |
| 212 |
} |