From: <rv...@us...> - 2011-10-13 18:40:34
|
Revision: 972 http://treebase.svn.sourceforge.net/treebase/?rev=972&view=rev Author: rvos Date: 2011-10-13 18:40:28 +0000 (Thu, 13 Oct 2011) Log Message: ----------- Factored out methods to apply default attributes to matrices and characters Modified Paths: -------------- trunk/treebase-core/src/main/java/org/cipres/treebase/domain/nexus/nexml/NexmlMatrixWriter.java Modified: trunk/treebase-core/src/main/java/org/cipres/treebase/domain/nexus/nexml/NexmlMatrixWriter.java =================================================================== --- trunk/treebase-core/src/main/java/org/cipres/treebase/domain/nexus/nexml/NexmlMatrixWriter.java 2011-10-13 16:26:47 UTC (rev 971) +++ trunk/treebase-core/src/main/java/org/cipres/treebase/domain/nexus/nexml/NexmlMatrixWriter.java 2011-10-13 18:40:28 UTC (rev 972) @@ -59,15 +59,13 @@ */ private CategoricalMatrix fromTreeBaseToXml(StandardMatrix tbMatrix) { OTUs xmlOTUs = getOTUsById(tbMatrix.getTaxa().getId()); - CategoricalMatrix xmlMatrix = getDocument().createCategoricalMatrix(xmlOTUs); + CategoricalMatrix xmlMatrix = getDocument().createCategoricalMatrix(xmlOTUs); + setMatrixAttributes(xmlMatrix,tbMatrix); - // attach base uri and history note - xmlMatrix.addAnnotationValue("skos:historyNote", Constants.SKOSURI, "Mapped from TreeBASE schema using NexmlMatrixConverter $Rev$"); - xmlMatrix.setBaseURI(mMatrixBaseURI); - List<List<DiscreteCharState>> tbStateLabels = tbMatrix.getStateLabels(); List<MatrixColumn> tbColumns = tbMatrix.getColumnsReadOnly(); for ( int i = 0; i < tbColumns.size(); i++ ) { + MatrixColumn tbColumn = tbColumns.get(i); CharacterStateSet xmlStateSet = xmlMatrix.createCharacterStateSet(); for ( DiscreteCharState tbState : tbStateLabels.get(i) ) { CharacterState xmlState = xmlStateSet.createCharacterState(tbState.getSymbol().toString()); @@ -77,14 +75,31 @@ attachTreeBaseID((Annotatable)xmlState,tbState,DiscreteCharState.class); } org.nexml.model.Character xmlCharacter = xmlMatrix.createCharacter(xmlStateSet); - PhyloChar tbCharacter = tbColumns.get(i).getCharacter(); - if ( null != tbCharacter.getDescription() ) { - xmlCharacter.setLabel(tbCharacter.getDescription()); - } - attachTreeBaseID((Annotatable)xmlCharacter,tbColumns.get(i),MatrixColumn.class); + setCharacterAttributes(tbColumn, xmlCharacter); } return xmlMatrix; } + + private void setCharacterAttributes(MatrixColumn tbColumn,org.nexml.model.Character xmlCharacter) { + PhyloChar tbCharacter = tbColumn.getCharacter(); + if ( null != tbCharacter.getDescription() ) { + xmlCharacter.setLabel(tbCharacter.getLabel()); + } + attachTreeBaseID((Annotatable)xmlCharacter,tbColumn,MatrixColumn.class); + } + + private void setMatrixAttributes(org.nexml.model.Matrix<?> xmlMatrix,CharacterMatrix tbMatrix) { + xmlMatrix.addAnnotationValue("skos:historyNote", Constants.SKOSURI, "Mapped from TreeBASE schema using "+this.toString()+" $Rev$"); + xmlMatrix.setBaseURI(mMatrixBaseURI); + xmlMatrix.setLabel(tbMatrix.getLabel()); + + // attach matrix identifiers + attachTreeBaseID((Annotatable)xmlMatrix, tbMatrix,Matrix.class); + String tb1MatrixID = tbMatrix.getTB1MatrixID(); + if ( null != tb1MatrixID ) { + ((Annotatable)xmlMatrix).addAnnotationValue("tb:identifier.matrix.tb1", Constants.TBTermsURI, tb1MatrixID); + } + } /** * Creates and populates characters (i.e. columns) with their annotations, @@ -112,11 +127,8 @@ xmlMatrix = getDocument().createMolecularMatrix(xmlOTUs, MolecularMatrix.Protein); xmlStateSet = ((MolecularMatrix)xmlMatrix).getProteinCharacterStateSet(); } + setMatrixAttributes(xmlMatrix,tbMatrix); - // attach base uri and history note - xmlMatrix.setBaseURI(mMatrixBaseURI); - xmlMatrix.addAnnotationValue("skos:historyNote", Constants.SKOSURI, "Mapped from TreeBASE schema using NexmlMatrixConverter $Rev$"); - // lookup the equivalent state in tb and attach identifiers for(StateSet tbStateSet : tbMatrix.getStateSets() ) { for (DiscreteCharState tbState : tbStateSet.getStates() ) { @@ -132,7 +144,7 @@ // create columns and attach identifiers for ( MatrixColumn tbColumn : tbMatrix.getColumnsReadOnly() ) { org.nexml.model.Character xmlCharacter = xmlMatrix.createCharacter(xmlStateSet); - attachTreeBaseID((Annotatable)xmlCharacter,tbColumn,MatrixColumn.class); + setCharacterAttributes(tbColumn, xmlCharacter); } return xmlMatrix; } @@ -147,19 +159,11 @@ private org.nexml.model.ContinuousMatrix fromTreeBaseToXml(ContinuousMatrix tbMatrix) { OTUs xmlOTUs = getOTUsById(tbMatrix.getTaxa().getId()); org.nexml.model.ContinuousMatrix xmlMatrix = getDocument().createContinuousMatrix(xmlOTUs); + setMatrixAttributes(xmlMatrix,tbMatrix); - // attach base uri and history note - xmlMatrix.setBaseURI(mMatrixBaseURI); - xmlMatrix.addAnnotationValue("skos:historyNote", Constants.SKOSURI, "Mapped from TreeBASE schema using NexmlMatrixConverter $Rev$"); - for ( MatrixColumn tbColumn : tbMatrix.getColumnsReadOnly() ) { org.nexml.model.Character xmlCharacter = xmlMatrix.createCharacter(); - PhyloChar tbCharacter = tbColumn.getCharacter(); - if ( null != tbCharacter.getDescription() ) { - xmlCharacter.setLabel(tbCharacter.getDescription()); - ((Annotatable)xmlCharacter).addAnnotationValue("dcterms:description", Constants.DCTermsURI, tbCharacter.getDescription()); - } - attachTreeBaseID((Annotatable)xmlCharacter,tbColumn,MatrixColumn.class); + setCharacterAttributes(tbColumn, xmlCharacter); //coerce the tbMatrix into a character matrix to get its character sets CharacterMatrix tbCharacterMatrix = (CharacterMatrix)tbMatrix; @@ -215,12 +219,8 @@ xmlMatrix = fromTreeBaseToXml((ContinuousMatrix) tbMatrix); populateXmlMatrix((org.nexml.model.ContinuousMatrix)xmlMatrix,(ContinuousMatrix)tbMatrix); } - xmlMatrix.setLabel(tbMatrix.getTitle()); - attachTreeBaseID((Annotatable)xmlMatrix, tbMatrix,Matrix.class); - String tb1MatrixID = tbMatrix.getTB1MatrixID(); - if ( null != tb1MatrixID ) { - ((Annotatable)xmlMatrix).addAnnotationValue("tb:identifier.matrix.tb1", Constants.TBTermsURI, tb1MatrixID); - } + + // here we copy the character sets for all matrix types Set<CharSet> tbCharSets = tbMatrix.getCharSets(); for ( CharSet tbCharSet : tbCharSets ) { Collection<ColumnRange> tbColumnRanges = tbCharSet.getColumns(tbMatrix); @@ -234,6 +234,12 @@ // 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(); + } + // create the equivalent nexml character set Subset nexSubset = xmlMatrix.createSubset(tbCharSet.getLabel()); This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |