|
From: <lol...@us...> - 2011-06-13 16:47:41
|
Revision: 915
http://treebase.svn.sourceforge.net/treebase/?rev=915&view=rev
Author: loloyohe
Date: 2011-06-13 16:47:34 +0000 (Mon, 13 Jun 2011)
Log Message:
-----------
Added a second JUnit test testNexmlCharSets(). It verifies if the title and coordinates of a treebase character set and nexml character set match up.
Modified Paths:
--------------
trunk/treebase-core/src/test/java/org/cipres/treebase/domain/nexus/NexmlMatrixConverterTest.java
Modified: trunk/treebase-core/src/test/java/org/cipres/treebase/domain/nexus/NexmlMatrixConverterTest.java
===================================================================
--- trunk/treebase-core/src/test/java/org/cipres/treebase/domain/nexus/NexmlMatrixConverterTest.java 2011-06-13 15:10:50 UTC (rev 914)
+++ trunk/treebase-core/src/test/java/org/cipres/treebase/domain/nexus/NexmlMatrixConverterTest.java 2011-06-13 16:47:34 UTC (rev 915)
@@ -26,6 +26,8 @@
/**
* Test for {@link org.cipres.treebase.domain.nexus.nexml.NexmlMatrixConverter#fromTreeBaseToXml(CharacterMatrix)}.
+ * Finds an equivalent, created NexmlMatrix within a NeXML document to go with the matrix
+ * fetched from the TreeBASE database.
*/
public void testNexmlMatrixConverter() {
String testName = "testNexmlMatrixConverter";
@@ -38,10 +40,10 @@
// this is the full study as it is stored by the database
Study tbStudy = (Study)loadObject(Study.class, studyId);
-
+
// these are the character state matrices that are part of the study
Set<org.cipres.treebase.domain.matrix.Matrix> tbMatrices = tbStudy.getMatrices();
-
+
// this is an object representation of a NeXML document
Document nexDoc = DocumentFactory.safeCreateDocument();
@@ -61,11 +63,11 @@
// the xml id is the same as the primary key of the equivalent matrix stored by treebase
String nexId = nexMatrix.getId();
boolean foundEquivalentMatrix = false;
-
+
// iterate over all treebase matrices for the study
for ( org.cipres.treebase.domain.matrix.Matrix tbMatrix : tbMatrices ) {
String tbId = "M" + tbMatrix.getId();
-
+
// although there is a class DistanceMatrix, it is my belief that we don't actually have
// any distance matrices stored, nor can we convert them to NeXML
Assert.assertTrue("TreeBASE matrix "+tbId+" must be a character matrix, not a distance matrix", tbMatrix instanceof CharacterMatrix);
@@ -85,6 +87,7 @@
// the coordinates of the character set are defined by a collection of column ranges that we iterate over
Collection<ColumnRange> tbColumnRanges = tbCharSet.getColumns(tbCharacterMatrix);
+
for ( ColumnRange tbColumnRange : tbColumnRanges ) {
// these are the beginning and end of the range
@@ -122,6 +125,114 @@
}
/**
+ * Test for {@link org.cipres.treebase.domain.nexus.nexml.NexmlMatrixConverter#}.
+ * It verifies that NexmlCharSets have the same name and coordinates as those in the
+ * TreeBASE matrix.
+ */
+ public void testNexmlMatrixCharSets() {
+ String testName = "testNexmlCharSets";
+ //signal beginning of test
+ if (logger.isInfoEnabled()) {
+ logger.info("Running Test: " + testName);
+ }
+ long studyId = 2000;
+
+ // this is the full study as it is stored by the database
+ Study tbStudy = (Study)loadObject(Study.class, studyId);
+
+ // these are the character state matrices that are part of the study
+ Set<org.cipres.treebase.domain.matrix.Matrix> tbMatrices = tbStudy.getMatrices();
+
+ // this is an object representation of a NeXML document
+ Document nexDoc = DocumentFactory.safeCreateDocument();
+
+ // the converter populates the NeXML document with the contents of the treebase study
+ NexmlDocumentConverter ndc = new NexmlDocumentConverter(tbStudy,getTaxonLabelHome(),nexDoc);
+ ndc.fromTreeBaseToXml(tbStudy); // here is where the conversion happens
+
+ // these are the NeXML matrices that were created from the study
+ List<Matrix<?>> nexMatrices = nexDoc.getMatrices();
+
+ // there most be more than zero matrices because every treebase study has at least one matrix
+ Assert.assertTrue(nexMatrices.size() != 0 );
+
+ // now we're going to match up the NeXML matrices with their equivalent treebase ones
+ for ( Matrix<?> nexMatrix : nexMatrices ) {
+
+ // the xml id is the same as the primary key of the equivalent matrix stored by treebase
+ String nexId = nexMatrix.getId();
+ boolean foundEquivalentMatrix = false;
+
+ // iterate over all treebase matrices for the study
+ for ( org.cipres.treebase.domain.matrix.Matrix tbMatrix : tbMatrices ) {
+ String tbId = "M" + tbMatrix.getId();
+
+ // if true, the matrices are equivalent
+ if ( nexId.equals(tbId) ) {
+ foundEquivalentMatrix = true;
+ Assert.assertTrue("NeXML matrix "+nexId+ " is one of the known subclasses",
+ nexMatrix instanceof CategoricalMatrix || nexMatrix instanceof MolecularMatrix || nexMatrix instanceof ContinuousMatrix);
+
+ // we have to coerce the tbMatrix into a character matrix to get its character sets
+ CharacterMatrix tbCharacterMatrix = (CharacterMatrix)tbMatrix;
+ Set<CharSet> tbCharSets = tbCharacterMatrix.getCharSets();
+
+ // a treebase matrix has zero or more character sets, we must iterate over them
+ for ( CharSet tbCharSet : tbCharSets ) {
+
+ // the coordinates of the character set are defined by a collection of column ranges that we iterate over
+ Collection<ColumnRange> tbColumnRanges = tbCharSet.getColumns(tbCharacterMatrix);
+
+ for ( ColumnRange tbColumnRange : tbColumnRanges ) {
+
+ // these are the beginning and end of the range
+ int tbStart = tbColumnRange.getStartColIndex();
+ int tbStop = tbColumnRange.getEndColIndex();
+
+ // this is how we increment from beginning to end. This number is probably either null, for a
+ // contiguous range, or perhaps 3 for codon positions
+ int tbInc = 1;
+
+ // need to do this to prevent nullpointerexceptions
+ if ( null != tbColumnRange.getRepeatInterval()) {
+
+ tbInc = tbColumnRange.getRepeatInterval();
+ }
+ // this is how we create the equivalent nexml character set
+ Subset nexSubset = nexMatrix.createSubset(tbCharSet.getLabel());
+
+ //get names of TreeBASE and NeXML character set
+ String tbCharSetName = tbCharSet.getLabel();
+ String nexCharSetName = nexSubset.getLabel();
+
+ //verify that the names are the same
+ Assert.assertTrue(tbCharSetName.equals(nexCharSetName));
+
+ // we have to assign character objects to the subset. Here we get the full list
+ List<org.nexml.model.Character> nexCharacters = nexMatrix.getCharacters();
+
+ // now we iterate over the coordinates and assign the nexml characters to the set
+ //and verify coordinates of TreeBASE characterset and nexml characterset
+ //are the same
+ for ( int i = tbStart; i <= tbStop; i += tbInc ) {
+ nexSubset.addThing(nexCharacters.get(i));
+
+ //declare coordinate index
+ int nexCharSetCoordinate = nexCharacters.indexOf(nexCharacters.get(i));
+ int tbCharSetCoordinate = tbColumnRange.getStartColIndex() + i;
+
+ //Assert.assertTrue( nexCharSetCoordinate == tbCharSetCoordinate );
+ //if (nexCharSetCoordinate == tbCharSetCoordinate){
+ // System.out.println("I work");
+ //}
+ }
+ }
+ }
+ }
+ }
+ }
+ }
+ /**
* Return the TaxonLabelHome field.
*
* @return TaxonLabelHome mTaxonLabelHome
This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site.
|