DiscreteChar columnChar = (DiscreteChar) matrixColumn.getCharacter();
// convert the mesquite character state to treebase state:
long statesLong = categoricalData.getState(colIndex, pRowIndex);
MatrixElement element = null;
if (categoricalData.isInapplicable(colIndex, pRowIndex)) {
// gap:
element = createElement(columnChar, gapSymbolStr, 0, matrix);
} else if (categoricalData.isUnassigned(colIndex, pRowIndex)) {
// missing
element = createElement(columnChar, missingSymbolStr, 0, matrix);
} else {
int[] states = CategoricalState.expand(statesLong);
if (states.length > 1) {
// multiple states, use compound element:
CompoundMatrixElement compoundElement = new CompoundMatrixElement();
Set<DiscreteMatrixElement> elements = new HashSet<DiscreteMatrixElement>();
// pre calculate the compound values and cache it in compoundElement.
StringBuilder compoundValues = new StringBuilder();
compoundValues.append('{');
for (int i = 0; i < states.length; i++) {
int aState = states[i];
String stateName = categoricalData.getStateName(colIndex, aState);
DiscreteMatrixElement anElement = createElement(
columnChar,
stateName,
i,
matrix);
anElement.setColumn(matrixColumn);
elements.add(anElement);
anElement.appendValue(compoundValues);
if (i < states.length - 1) {
compoundValues.append(' ');
}
}
compoundValues.append('}');
compoundElement.setCompoundValue(compoundValues.toString());
compoundElement.setElements(elements);
element = compoundElement;
} else {
// single state, use single element:
String stateName = categoricalData.getStateName(colIndex, states[0]);
element = createElement(columnChar, stateName, 0, matrix);
}
}
element.setColumn(matrixColumn);
pRow.addElement(element);
}
}