@Override
protected MatrixJDBC createMatrix(CharacterData pMesqMatrix) {
CategoricalData categoricalData = (CategoricalData) pMesqMatrix;
CharacterMatrix m = new StandardMatrix();
DiscreteMatrixJDBC matrixJDBC = new DiscreteMatrixJDBC(m, categoricalData, this);
int numChars = categoricalData.getNumChars();
DiscreteChar symbolChar = null; // shared phyloChar for symbols.
// get symbols as one String. need to do conversion:
buildSymbols(categoricalData, m);
// first consider the default char based on matrix data type.
// For standard matrix, we know it has to be discrete char:
MatrixDataType dataType = getMatrixDataType();
DiscreteChar defaultChar = (DiscreteChar) dataType.getDefaultCharacter();
if (defaultChar != null) {
matrixJDBC.setDefaultCharID(defaultChar.getId());
}
List<MatrixColumnJDBC> columnJDBCs = new ArrayList<MatrixColumnJDBC>();
// add character/column:
for (int i = 0; i < numChars; i++) {
// MatrixColumn aColumn = new MatrixColumn();
MatrixColumnJDBC aColumnJDBC = new MatrixColumnJDBC();
DiscreteChar tbChar = defaultChar;
if (tbChar == null) {
// it is probably a Standard dataType,
// need to create characters for each column.
if (!categoricalData.characterHasName(i) && !categoricalData.hasStateNames(i)) {
// 1. if there is no character name defined and no state names defined,
// need to use the defined symbol character
// which is shared for the entire matrix.
if (symbolChar == null) {
tbChar = createCharUseSymbols(categoricalData, i);
symbolChar = tbChar;
} else {
tbChar = symbolChar;
// if (LOGGER.isDebugEnabled()) {
// LOGGER.debug(" symbolChar reused for column: " + i); //$NON-NLS-1$
// }
}
} else if (!categoricalData.hasStateNames(i)) {
// 2. if there is character name defined but still no state names,
// create a new character but need to use symbols as state names.
tbChar = createCharUseSymbols(categoricalData, i);
String charName = null;
if (categoricalData.characterHasName(i)) {
charName = categoricalData.getCharacterName(i);
}
tbChar.setDescription(charName);
} else {
// 3. has stateNames defined,
// create a new character and set defined state names and symbols.
tbChar = new DiscreteChar();
String charName = null;
if (categoricalData.characterHasName(i)) {
charName = categoricalData.getCharacterName(i);
}
tbChar.setDescription(charName);
int lastState = categoricalData.maxStateWithName(i);
for (int j = 0; j <= lastState; j++) {
String stateName = categoricalData.getStateName(i, j);
String stateNote = categoricalData.getStateNote(i, j);
char stateSymbol = categoricalData.getSymbol(j);
DiscreteCharState tbCharState = new DiscreteCharState();
tbCharState.setDescription(stateName);
tbCharState.setSymbol(stateSymbol);
if (!TreebaseUtil.isEmpty(stateNote)) {
tbCharState.setNotes(stateNote);
}
tbChar.addCharState(tbCharState);
}
}
// ////////////////////////////
// // Need to create a new char. Add states.
// if (categoricalData.hasStateNames(i)) {
//
// String charName = null;
// if (categoricalData.characterHasName(i)) {
// charName = categoricalData.getCharacterName(i);
// }
// tbChar = new DiscreteChar();
// tbChar.setDescription(charName);
//
// int lastState = categoricalData.maxStateWithName(i);
//
// for (int j = 0; j <= lastState; j++) {
// String stateName = categoricalData.getStateName(i, j);
// String stateNote = categoricalData.getStateNote(i, j);
// char stateSymbol = categoricalData.getSymbol(j);
//
// DiscreteCharState tbCharState = new DiscreteCharState();
// tbCharState.setDescription(stateName);
// tbCharState.setSymbol(stateSymbol);
//
// if (!TreebaseUtil.isEmpty(stateNote)) {
// tbCharState.setNotes(stateNote);
// }
//
// tbChar.addCharState(tbCharState);
// }
// } else {
// // no state name defined. In this case
// // 1. if there is no character name defined, need to use the defined symbol
// character
// // which is shared for the entire matrix.
// // 2. if there is character name defined, create a new character but need to
// // use symbol.
//
// if (symbolChar == null) {
//
// tbChar = new DiscreteChar();
// // do we need the symbol boolean property?
// //tbChar.setSymbol(true);
//
// int lastSymbol = categoricalData.getMaxSymbolDefined();
//
// for (int j = 0; j <= lastSymbol; j++) {
// //String stateSymbolStr = categoricalData.getStateSymbol(i, j);
// String stateNote = categoricalData.getStateNote(i, j);
// char stateSymbol = categoricalData.getSymbol(j);
//
// DiscreteCharState tbCharState = new DiscreteCharState();
// //tbCharState.setDescription(stateSymbolStr);
// tbCharState.setSymbol(stateSymbol);
//
// if (!TreebaseUtil.isEmpty(stateNote)) {
// tbCharState.setNotes(stateNote);
// }
//
// tbChar.addCharState(tbCharState);
// }
//
// symbolChar = tbChar;
//
// if (LOGGER.isDebugEnabled()) {
// LOGGER.debug(" symbolChar created for column: " + i); //$NON-NLS-1$
// }
// } else {
// tbChar = symbolChar;
//
// if (LOGGER.isDebugEnabled()) {
// LOGGER.debug(" symbolChar reused for column: " + i); //$NON-NLS-1$
// }
// }
// }
}
// aColumn.setCharacter(tbChar);
// m.addColumn(aColumn);
aColumnJDBC.setPhyloChar(tbChar);
columnJDBCs.add(aColumnJDBC);
}
matrixJDBC.setMatrixColumnJDBCs(columnJDBCs);
return matrixJDBC;
}