Skip to content

Commit 7cd4988

Browse files
committed
Added parsing of input matrix cells. Values seem to have been ignored previously.
1 parent c803408 commit 7cd4988

7 files changed

Lines changed: 5718 additions & 12 deletions

File tree

src/org/nexml/model/impl/CategoricalMatrixImpl.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ protected CategoricalMatrixImpl(Document document, Element element, OTUsImpl otu
6363

6464
for ( Element row : getChildrenByTagName( getMatrixElement(), "row") ) {
6565
OTU otu = otus.getThingById(row.getAttribute("otu"));
66-
MatrixRowImpl<CharacterState> matrixRow = new MatrixRowImpl<CharacterState>(getDocument(),row);
66+
MatrixRowImpl<CharacterState> matrixRow = new MatrixRowImpl<CharacterState>(getDocument(),row, this, false);
6767
matrixRow.setOTU(otu);
6868
mMatrixRows.put(otu, matrixRow);
6969
}

src/org/nexml/model/impl/ContinuousMatrixImpl.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ private void processMatrix(Element matrix,OTUsImpl otus,Map<String,Character> ch
6363
setMatrixElement(matrix);
6464
for ( Element rowElement : getChildrenByTagName(matrix, "row") ) {
6565
OTU otu = otus.getThingById(rowElement.getAttribute("otu"));
66-
MatrixRow<Double> matrixRow = new MatrixRowImpl<Double>(getDocument(),rowElement);
66+
MatrixRow<Double> matrixRow = new MatrixRowImpl<Double>(getDocument(),rowElement, this, true);
6767
matrixRow.setOTU(otu);
6868
mMatrixRows.put(otu, matrixRow);
6969
}

src/org/nexml/model/impl/MatrixImpl.java

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -62,14 +62,7 @@ protected MatrixImpl(Document document,String type) {
6262
* @author rvosa
6363
*/
6464
protected MatrixImpl(Document document,Element element) {
65-
super(document,element);
66-
for (Element rowElement : getChildrenByTagName(element, "row")) {
67-
String otuId = rowElement.getAttribute("otu");
68-
OTU otu = ((OTUsImpl)getOTUs()).getThingById(otuId);
69-
MatrixRow<T> row = new MatrixRowImpl<T>(document, rowElement);
70-
row.setOTU(otu);
71-
mMatrixRows.put(otu, row);
72-
}
65+
super(document,element);
7366
}
7467

7568
/*

src/org/nexml/model/impl/MatrixRowImpl.java

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
import java.util.Map;
77

88
import org.nexml.model.Character;
9+
import org.nexml.model.CharacterState;
910
import org.nexml.model.MatrixCell;
1011
import org.nexml.model.MatrixRow;
1112
import org.w3c.dom.Document;
@@ -55,15 +56,27 @@ protected MatrixRowImpl(Document document,MatrixImpl<T> matrix) {
5556
* the <otus/> element)
5657
* @author rvosa
5758
*/
58-
protected MatrixRowImpl(Document document, Element element) {
59+
protected MatrixRowImpl(Document document, Element element, MatrixImpl<T> matrix, boolean continuous) {
5960
super(document, element);
61+
mMatrix = matrix;
6062
List<Element> seqElements = getChildrenByTagName(element, "seq");
6163
if ( ! seqElements.isEmpty() ) {
6264
mSeqElement = seqElements.get(0);
6365
}
6466
List<Element> cellElements = getChildrenByTagName(element, "cell");
6567
for ( Element cellElement : cellElements ) {
68+
Character character = findCharacter(cellElement.getAttribute("char"));
69+
Object value;
70+
if (continuous) {
71+
value = Double.parseDouble(cellElement.getAttribute("state"));
72+
73+
} else {
74+
value = findState(cellElement.getAttribute("state"), character);
75+
}
76+
mStateForCharacter.put(character, (T)value);
6677
MatrixCellImpl<T> cell = new MatrixCellImpl<T>(getDocument(),cellElement);
78+
cell.setValue((T)value);
79+
mCellForCharacter.put(character, cell);
6780
mMatrixCell.add(cell);
6881
}
6982
}
@@ -195,5 +208,23 @@ public String getSeq() {
195208
private MatrixImpl<T> getMatrix() {
196209
return mMatrix;
197210
}
211+
212+
private Character findCharacter(String id) {
213+
for (Character character : getMatrix().getCharacters()) {
214+
if (character.getId().equals(id)) {
215+
return character;
216+
}
217+
}
218+
return null;
219+
}
220+
221+
private CharacterState findState(String id, Character character) {
222+
for (CharacterState state : character.getCharacterStateSet().getCharacterStates()) {
223+
if (state.getId().equals(id)) {
224+
return state;
225+
}
226+
}
227+
return null;
228+
}
198229

199230
}

src/org/nexml/model/impl/MolecularMatrixImpl.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ protected MolecularMatrixImpl(Document document, Element element, OTUsImpl otus,
6565

6666
for ( Element row : getChildrenByTagName( getMatrixElement(), "row") ) {
6767
OTU otu = otus.getThingById(row.getAttribute("otu"));
68-
MatrixRowImpl<CharacterState> matrixRow = new MatrixRowImpl<CharacterState>(getDocument(),row);
68+
MatrixRowImpl<CharacterState> matrixRow = new MatrixRowImpl<CharacterState>(getDocument(),row, this, false);
6969
matrixRow.setOTU(otu);
7070
mMatrixRows.put(otu, matrixRow);
7171
}

test/org/nexml/model/TestFileParse.java

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55

66
import java.io.File;
77
import java.io.IOException;
8+
89
import javax.xml.parsers.ParserConfigurationException;
910

1011
import junit.framework.Assert;
@@ -39,5 +40,18 @@ public void parseCharacters() {
3940
}
4041
System.out.println(doc.getXmlString());
4142
}
43+
44+
@Test
45+
public void parsePhenoscapeMatrix() throws SAXException, IOException, ParserConfigurationException {
46+
final File file = new File("test_files/Buckup_1998.xml");
47+
final Document doc = DocumentFactory.parse(file);
48+
for (Matrix<?> matrix : doc.getMatrices()) {
49+
for (Character character : matrix.getCharacters()) {
50+
for (OTU otu : matrix.getOTUs()) {
51+
System.out.println(matrix.getRowObject(otu).getCell(character).getValue());
52+
}
53+
}
54+
}
55+
}
4256

4357
}

0 commit comments

Comments
 (0)