From: <rv...@us...> - 2009-06-23 10:52:29
|
Revision: 73 http://treebase.svn.sourceforge.net/treebase/?rev=73&view=rev Author: rvos Date: 2009-06-23 10:52:08 +0000 (Tue, 23 Jun 2009) Log Message: ----------- Now emit valid nexml. Modified Paths: -------------- trunk/treebase-core/src/main/java/org/cipres/treebase/domain/nexus/nexml/NexmlDocumentConverter.java trunk/treebase-core/src/main/java/org/cipres/treebase/domain/nexus/nexml/NexmlMatrixConverter.java trunk/treebase-core/src/main/java/org/cipres/treebase/domain/nexus/nexml/NexmlOTUConverter.java trunk/treebase-core/src/main/java/org/cipres/treebase/domain/nexus/nexml/NexmlObjectConverter.java trunk/treebase-core/src/main/java/org/cipres/treebase/domain/nexus/nexml/NexmlTreeBlockConverter.java Modified: trunk/treebase-core/src/main/java/org/cipres/treebase/domain/nexus/nexml/NexmlDocumentConverter.java =================================================================== --- trunk/treebase-core/src/main/java/org/cipres/treebase/domain/nexus/nexml/NexmlDocumentConverter.java 2009-06-23 10:50:48 UTC (rev 72) +++ trunk/treebase-core/src/main/java/org/cipres/treebase/domain/nexus/nexml/NexmlDocumentConverter.java 2009-06-23 10:52:08 UTC (rev 73) @@ -1,6 +1,7 @@ package org.cipres.treebase.domain.nexus.nexml; import org.cipres.treebase.domain.admin.Person; +import org.cipres.treebase.domain.matrix.CharacterMatrix; import org.cipres.treebase.domain.nexus.NexusDataSet; import org.cipres.treebase.domain.study.ArticleCitation; import org.cipres.treebase.domain.study.Citation; @@ -69,7 +70,9 @@ NexmlMatrixConverter nmc = new NexmlMatrixConverter(getStudy(),getTaxonLabelHome(),getDocument()); for (org.cipres.treebase.domain.matrix.Matrix matrix : pNexusDataSet.getMatrices() ) { - nmc.fromTreeBaseToXml(matrix); + if ( matrix instanceof CharacterMatrix ) { + nmc.fromTreeBaseToXml((CharacterMatrix)matrix); + } } NexmlTreeBlockConverter ntc = new NexmlTreeBlockConverter(getStudy(),getTaxonLabelHome(),getDocument()); @@ -87,22 +90,40 @@ * @param document */ private void copyCitationMetadata(Citation citation,Document document) { - attachAnnotation("prism:publicationDate",citation.getPublishYear().toString(),mPrismURI,document); - attachAnnotation("prism:doi",citation.getDoi(),mPrismURI,document); - String[] pages = citation.getPages().split("-"); - if ( pages.length > 2 ) { - attachAnnotation("prism:startingPage",pages[0],mPrismURI,document); - attachAnnotation("prism:endingPage",pages[1],mPrismURI,document); - attachAnnotation("prism:pageRange",citation.getPages(),mPrismURI,document); + if ( null != citation.getTitle() ) { + attachAnnotation("dc:title",citation.getTitle(),mPrismURI,document); } - String[] keywords = citation.getKeywords().split(", "); - for ( int i = 0; i < keywords.length; i++ ) { - attachAnnotation("prism:keyword",keywords[i],mPrismURI,document); - } + if ( null != citation.getPublishYear() ) { + attachAnnotation("prism:publicationDate",citation.getPublishYear().toString(),mPrismURI,document); + } + if ( null != citation.getDoi() ) { + attachAnnotation("prism:doi",citation.getDoi(),mPrismURI,document); + } + if ( null != citation.getPages() ) { + String[] pages = citation.getPages().split("\\-"); + if ( pages.length == 2 ) { + attachAnnotation("prism:startingPage",pages[0],mPrismURI,document); + attachAnnotation("prism:endingPage",pages[1],mPrismURI,document); + attachAnnotation("prism:pageRange",citation.getPages(),mPrismURI,document); + } + } + if ( null != citation.getKeywords() ) { + String[] keywords = citation.getKeywords().split(", "); + for ( int i = 0; i < keywords.length; i++ ) { + attachAnnotation("prism:keyword",keywords[i],mPrismURI,document); + } + } if ( citation instanceof ArticleCitation ) { - attachAnnotation("prism:publicationName",((ArticleCitation)citation).getJournal(),mPrismURI,document); - attachAnnotation("prism:volume",((ArticleCitation)citation).getVolume(),mPrismURI,document); - attachAnnotation("prism:number",((ArticleCitation)citation).getIssue(),mPrismURI,document); + ArticleCitation ac = (ArticleCitation)citation; + if ( null != ac.getJournal() ) { + attachAnnotation("prism:publicationName",ac.getJournal(),mPrismURI,document); + } + if ( null != ac.getVolume() ) { + attachAnnotation("prism:volume",ac.getVolume(),mPrismURI,document); + } + if ( null != ac.getIssue() ) { + attachAnnotation("prism:number",ac.getIssue(),mPrismURI,document); + } } } @@ -112,22 +133,7 @@ * @return */ public Document fromTreeBaseToXml(Study pStudy) { - attachTreeBaseID(getDocument(), pStudy); - attachAnnotation("dc:title", pStudy.getName(), mDCURI, getDocument()); - attachAnnotation("dc:abstract",pStudy.getCitation().getAbstract(), mDCURI,getDocument()); - attachAnnotation( - "dc:creator", - pStudy.getSubmission().getSubmitter().getPerson().getFullNameCitationStyle(), - mDCURI, - getDocument() - ); - for ( Person person : pStudy.getAuthors() ) { - String personName = person.getFullNameCitationStyle(); - attachAnnotation("dc:contributor",personName,mDCURI,getDocument()); - } - attachAnnotation("prism:creationDate",pStudy.getSubmission().getCreateDate().toString(),mPrismURI,getDocument()); - attachAnnotation("prism:embargoDate",pStudy.getReleaseDate().toString(),mPrismURI,getDocument()); - copyCitationMetadata(pStudy.getCitation(),getDocument()); + copyMetadata(pStudy); NexmlOTUConverter noc = new NexmlOTUConverter(getStudy(),getTaxonLabelHome(),getDocument()); for ( TaxonLabelSet taxonLabelSet : pStudy.getTaxonLabelSets() ) { @@ -136,14 +142,48 @@ NexmlMatrixConverter nmc = new NexmlMatrixConverter(getStudy(),getTaxonLabelHome(),getDocument()); for (org.cipres.treebase.domain.matrix.Matrix matrix : pStudy.getMatrices() ) { - nmc.fromTreeBaseToXml(matrix); + if ( matrix instanceof CharacterMatrix ) { + nmc.fromTreeBaseToXml((CharacterMatrix)matrix); + } } NexmlTreeBlockConverter ntc = new NexmlTreeBlockConverter(getStudy(),getTaxonLabelHome(),getDocument()); for ( org.cipres.treebase.domain.tree.TreeBlock treeBlock : pStudy.getTreeBlocks() ) { ntc.fromTreeBaseToXML(treeBlock); } + return getDocument(); } + private void copyMetadata(Study pStudy) { + attachTreeBaseID(getDocument(), pStudy,Study.class); + if ( null != pStudy.getName() ) { + attachAnnotation("dc:title", pStudy.getName(), mDCURI, getDocument()); + } + for ( Person person : pStudy.getAuthors() ) { + String personName = person.getFullNameCitationStyle(); + attachAnnotation("dc:contributor",personName,mDCURI,getDocument()); + } + if ( null != pStudy.getReleaseDate() ) { + attachAnnotation("prism:embargoDate",pStudy.getReleaseDate().toString(),mPrismURI,getDocument()); + } + if ( null != pStudy.getSubmission() ) { + if ( null != pStudy.getSubmission().getSubmitter() ) { + attachAnnotation( + "dc:creator", + pStudy.getSubmission().getSubmitter().getPerson().getFullNameCitationStyle(), + mDCURI, + getDocument() + ); + } + if ( null != pStudy.getSubmission().getCreateDate() ) { + attachAnnotation("prism:creationDate",pStudy.getSubmission().getCreateDate().toString(),mPrismURI,getDocument()); + } + } + if ( null != pStudy.getCitation() ) { + //attachAnnotation("dc:abstract",forXML(pStudy.getCitation().getAbstract()), mDCURI,getDocument()); + copyCitationMetadata(pStudy.getCitation(),getDocument()); + } + } + } Modified: trunk/treebase-core/src/main/java/org/cipres/treebase/domain/nexus/nexml/NexmlMatrixConverter.java =================================================================== --- trunk/treebase-core/src/main/java/org/cipres/treebase/domain/nexus/nexml/NexmlMatrixConverter.java 2009-06-23 10:50:48 UTC (rev 72) +++ trunk/treebase-core/src/main/java/org/cipres/treebase/domain/nexus/nexml/NexmlMatrixConverter.java 2009-06-23 10:52:08 UTC (rev 73) @@ -2,29 +2,45 @@ import java.util.ArrayList; import java.util.List; + import org.cipres.treebase.dao.jdbc.ContinuousMatrixElementJDBC; import org.cipres.treebase.dao.jdbc.ContinuousMatrixJDBC; import org.cipres.treebase.dao.jdbc.DiscreteMatrixElementJDBC; import org.cipres.treebase.dao.jdbc.DiscreteMatrixJDBC; import org.cipres.treebase.dao.jdbc.MatrixColumnJDBC; +import org.cipres.treebase.domain.matrix.CharacterMatrix; import org.cipres.treebase.domain.matrix.ContinuousChar; +import org.cipres.treebase.domain.matrix.ContinuousMatrix; +import org.cipres.treebase.domain.matrix.ContinuousMatrixElement; import org.cipres.treebase.domain.matrix.DiscreteChar; +import org.cipres.treebase.domain.matrix.DiscreteCharState; import org.cipres.treebase.domain.matrix.DiscreteMatrix; +import org.cipres.treebase.domain.matrix.DiscreteMatrixElement; import org.cipres.treebase.domain.matrix.Matrix; +import org.cipres.treebase.domain.matrix.MatrixColumn; +import org.cipres.treebase.domain.matrix.MatrixDataType; +import org.cipres.treebase.domain.matrix.MatrixElement; +import org.cipres.treebase.domain.matrix.MatrixRow; +import org.cipres.treebase.domain.matrix.PhyloChar; +import org.cipres.treebase.domain.matrix.StandardMatrix; import org.cipres.treebase.domain.study.Study; import org.cipres.treebase.domain.taxon.TaxonLabelHome; import org.cipres.treebase.domain.taxon.TaxonLabelSet; import org.nexml.model.Annotatable; import org.nexml.model.CategoricalMatrix; import org.nexml.model.CharacterState; -import org.nexml.model.ContinuousMatrix; +import org.nexml.model.CharacterStateSet; import org.nexml.model.Document; import org.nexml.model.MatrixCell; +import org.nexml.model.MolecularMatrix; import org.nexml.model.OTUs; import org.nexml.model.OTU; public class NexmlMatrixConverter extends NexmlObjectConverter { + private static final int MAX_GRANULAR_NCHAR = 1000; + private static final int MAX_GRANULAR_NTAX = 30; + /** * * @param study @@ -56,6 +72,7 @@ columnJDBCs.add(aColumnJDBC); int rowIndex = 0; for ( OTU xmlOTU : xmlMatrix.getOTUs().getAllOTUs() ) { + @SuppressWarnings("unused") MatrixCell<CharacterState> xmlCell = xmlMatrix.getCell(xmlOTU, xmlCharacter); DiscreteMatrixElementJDBC element = new DiscreteMatrixElementJDBC(); //element.setValue(xmlCell.getValue()); // XXX nested stateset lookup song & dance here @@ -76,9 +93,8 @@ * @param xmlMatrix * @return */ - public Matrix fromXmlToTreeBase(ContinuousMatrix xmlMatrix) { - org.cipres.treebase.domain.matrix.ContinuousMatrix tbMatrix = - new org.cipres.treebase.domain.matrix.ContinuousMatrix(); + public Matrix fromXmlToTreeBase(org.nexml.model.ContinuousMatrix xmlMatrix) { + ContinuousMatrix tbMatrix = new ContinuousMatrix(); ContinuousMatrixJDBC matrixJDBC = new ContinuousMatrixJDBC(tbMatrix, xmlMatrix, this); List<MatrixColumnJDBC> columnJDBCs = new ArrayList<MatrixColumnJDBC>(); long[] colIds = matrixJDBC.getColIDs(); @@ -119,7 +135,7 @@ OTUs xmlOTUs = xmlMatrix.getOTUs(); Matrix tbMatrix = null; TaxonLabelSet tbTaxa = null; - Long tbTaxonLabelSetID = readTreeBaseID((Annotatable) xmlOTUs,TaxonLabelSet.class); + Long tbTaxonLabelSetID = readTreeBaseID((Annotatable) xmlOTUs); if ( null != tbTaxonLabelSetID ) { tbTaxa = getTaxonLabelHome() .findPersistedObjectByID(TaxonLabelSet.class, tbTaxonLabelSetID); @@ -127,11 +143,11 @@ if ( xmlMatrix instanceof CategoricalMatrix ) { tbMatrix = fromXmlToTreeBase((CategoricalMatrix) xmlMatrix); } - else if ( xmlMatrix instanceof ContinuousMatrix ) { - tbMatrix = fromXmlToTreeBase((ContinuousMatrix) xmlMatrix); + else if ( xmlMatrix instanceof org.nexml.model.ContinuousMatrix ) { + tbMatrix = fromXmlToTreeBase((org.nexml.model.ContinuousMatrix) xmlMatrix); } if ( null != tbMatrix ) { - attachTreeBaseID((Annotatable) xmlMatrix, tbMatrix); + attachTreeBaseID((Annotatable) xmlMatrix, tbMatrix,Matrix.class); if ( null != tbTaxa ) { tbMatrix.setTaxa(tbTaxa); } @@ -142,21 +158,192 @@ return tbMatrix; } - public org.nexml.model.Matrix<?> fromTreeBaseToXml(Matrix tbMatrix) { - TaxonLabelSet taxonLabelSet = tbMatrix.getTaxa(); - Long taxonLabelSetId = taxonLabelSet.getId(); - OTUs xmlOTUs = null; - for ( OTUs otus : getDocument().getOTUsList() ) { - Long annotatedID = readTreeBaseID(otus, TaxonLabelSet.class); - if ( taxonLabelSetId == annotatedID ) { - xmlOTUs = otus; - break; + /** + * Creates and populates characters (i.e. columns) with their annotations, + * and state sets, with their annotations + * + * @param tbMatrix + * @return an xml matrix with empty rows + */ + public CategoricalMatrix fromTreeBaseToXml(StandardMatrix tbMatrix) { + OTUs xmlOTUs = getOTUsById(tbMatrix.getTaxa().getId()); + CategoricalMatrix xmlMatrix = getDocument().createCategoricalMatrix(xmlOTUs); + List<List<DiscreteCharState>> tbStateLabels = tbMatrix.getStateLabels(); + List<MatrixColumn> tbColumns = tbMatrix.getColumnsReadOnly(); + for ( int i = 0; i < tbColumns.size(); i++ ) { + CharacterStateSet xmlStateSet = xmlMatrix.createCharacterStateSet(); + for ( DiscreteCharState tbState : tbStateLabels.get(i) ) { + CharacterState xmlState = xmlStateSet.createCharacterState(tbState.getSymbol().toString()); + if ( null != tbState.getDescription() ) { + xmlState.setLabel(tbState.getDescription()); + } + if ( null != tbState.getNotes() ) { + ((Annotatable)xmlState).addAnnotationValue("dc:description", mDCURI, tbState.getNotes()); + } + 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,tbCharacter,PhyloChar.class); + } + return xmlMatrix; + } + + /** + * Creates and populates characters (i.e. columns) with their annotations, + * and state sets, with their annotations + * + * @param tbMatrix + * @return an xml matrix with empty rows + */ + public MolecularMatrix fromTreeBaseToXml(DiscreteMatrix tbMatrix) { + OTUs xmlOTUs = getOTUsById(tbMatrix.getTaxa().getId()); + String tbDataType = tbMatrix.getDataType().getDescription(); + MolecularMatrix xmlMatrix = null; + CharacterStateSet xmlStateSet = null; + if ( tbDataType.equals(MatrixDataType.MATRIX_DATATYPE_DNA) ) { + xmlMatrix = getDocument().createMolecularMatrix(xmlOTUs, MolecularMatrix.DNA); + xmlStateSet = ((MolecularMatrix)xmlMatrix).getDNACharacterStateSet(); + } + else if ( tbDataType.equals(MatrixDataType.MATRIX_DATATYPE_RNA) ) { + xmlMatrix = getDocument().createMolecularMatrix(xmlOTUs, MolecularMatrix.RNA); + xmlStateSet = ((MolecularMatrix)xmlMatrix).getRNACharacterStateSet(); + } + else if ( tbDataType.equals(MatrixDataType.MATRIX_DATATYPE_PROTEIN) ) { + xmlMatrix = getDocument().createMolecularMatrix(xmlOTUs, MolecularMatrix.Protein); + xmlStateSet = ((MolecularMatrix)xmlMatrix).getProteinCharacterStateSet(); + } + for ( MatrixColumn tbColumn : tbMatrix.getColumnsReadOnly() ) { + org.nexml.model.Character xmlCharacter = xmlMatrix.createCharacter(xmlStateSet); + PhyloChar tbCharacter = tbColumn.getCharacter(); + if ( null != tbCharacter.getDescription() && ! tbCharacter.getDescription().equals(tbDataType) ) { + ((Annotatable)xmlCharacter).addAnnotationValue("dc:description", mDCURI, tbCharacter.getDescription()); + } + if ( null != tbCharacter.getId() && tbCharacter.getId() != 2 ) { // XXX is PhyloChar.id 2 some sort of magic number? + attachTreeBaseID((Annotatable)xmlCharacter,tbCharacter,PhyloChar.class); } } - org.nexml.model.Matrix<?> xmlMatrix = getDocument().createCategoricalMatrix(xmlOTUs); + return xmlMatrix; + } + + /** + * Creates and populates characters (i.e. columns) with their annotations, + * and state sets, with their annotations + * + * @param tbMatrix + * @return an xml matrix with empty rows + */ + public org.nexml.model.ContinuousMatrix fromTreeBaseToXml(ContinuousMatrix tbMatrix) { + OTUs xmlOTUs = getOTUsById(tbMatrix.getTaxa().getId()); + org.nexml.model.ContinuousMatrix xmlMatrix = getDocument().createContinuousMatrix(xmlOTUs); + for ( MatrixColumn tbColumn : tbMatrix.getColumnsReadOnly() ) { + org.nexml.model.Character xmlCharacter = xmlMatrix.createCharacter(); + PhyloChar tbCharacter = tbColumn.getCharacter(); + if ( null != tbCharacter.getDescription() ) { + ((Annotatable)xmlCharacter).addAnnotationValue("dc:description", mDCURI, tbCharacter.getDescription()); + } + attachTreeBaseID((Annotatable)xmlCharacter,tbCharacter,PhyloChar.class); + } + return xmlMatrix; + } + + +// XXX doesn't handle the following data types: +// public static final String MATRIX_DATATYPE_NUCLEOTIDE = "Nucleotide"; +// public static final String MATRIX_DATATYPE_DISTANCE = "Distance"; +// public static final String MATRIX_DATATYPE_MIXED = "Mixed"; + @SuppressWarnings("unchecked") + public org.nexml.model.Matrix<?> fromTreeBaseToXml(CharacterMatrix tbMatrix) { + org.nexml.model.Matrix<?> xmlMatrix = null; + if ( tbMatrix instanceof DiscreteMatrix ) { + if ( tbMatrix.getDataType().getDescription().equals(MatrixDataType.MATRIX_DATATYPE_STANDARD) ) { + xmlMatrix = fromTreeBaseToXml((StandardMatrix) tbMatrix); + } + else { + xmlMatrix = fromTreeBaseToXml((DiscreteMatrix) tbMatrix); + } + populateXmlMatrix((org.nexml.model.Matrix<CharacterState>)xmlMatrix,(DiscreteMatrix)tbMatrix); + } + else if ( tbMatrix instanceof ContinuousMatrix ) { + xmlMatrix = fromTreeBaseToXml((ContinuousMatrix) tbMatrix); + populateXmlMatrix((org.nexml.model.ContinuousMatrix)xmlMatrix,(ContinuousMatrix)tbMatrix); + } xmlMatrix.setLabel(tbMatrix.getTitle()); - attachTreeBaseID((Annotatable)xmlMatrix, tbMatrix); + attachTreeBaseID((Annotatable)xmlMatrix, tbMatrix,Matrix.class); return xmlMatrix; } + /** + * + * @param xmlMatrix + * @param tbMatrix + * @param xmlOTUs + * @param stateSet + */ + private void populateXmlMatrix( + org.nexml.model.Matrix<CharacterState> xmlMatrix, + DiscreteMatrix tbMatrix) { + OTUs xmlOTUs = xmlMatrix.getOTUs(); + List<org.nexml.model.Character> characterList = xmlMatrix.getCharacters(); + for ( MatrixRow row : tbMatrix.getRowsReadOnly() ) { + OTU xmlOTU = getOTUById(xmlOTUs, row.getTaxonLabel().getId()); + int charIndex = 0; + if ( characterList.size() <= MAX_GRANULAR_NCHAR && xmlOTUs.getAllOTUs().size() <= MAX_GRANULAR_NTAX ) { + for ( MatrixElement tbCell : row.getElements() ) { + org.nexml.model.Character xmlCharacter = characterList.get(charIndex); + MatrixCell<CharacterState> xmlCell = xmlMatrix.getCell(xmlOTU, xmlCharacter); + DiscreteCharState tbState = ((DiscreteMatrixElement)tbCell).getCharState(); + String tbSymbolString = ( null == tbState ) ? "?" : tbState.getSymbol().toString(); + CharacterState xmlState = xmlCharacter.getCharacterStateSet().lookupCharacterStateBySymbol(tbSymbolString); + xmlCell.setValue(xmlState); + attachTreeBaseID((Annotatable)xmlCell,tbCell,DiscreteMatrixElement.class); + charIndex++; + } + } + else { + String seq = row.buildElementAsString(); + if ( tbMatrix.getDataType().getDescription().equals(MatrixDataType.MATRIX_DATATYPE_STANDARD) ) { + StringBuilder sb = new StringBuilder(); + for ( int i = 0; i < seq.length(); i++ ) { + sb.append(seq.charAt(i)); + if ( i < seq.length() - 1 ) { + sb.append(' '); + } + } + } + xmlMatrix.setSeq(seq,xmlOTU); + } + } + } + + /** + * + * @param xmlMatrix + * @param tbMatrix + */ + private void populateXmlMatrix(org.nexml.model.ContinuousMatrix xmlMatrix, + ContinuousMatrix tbMatrix) { + List<org.nexml.model.Character> characterList = xmlMatrix.getCharacters(); + OTUs xmlOTUs = xmlMatrix.getOTUs(); + for ( MatrixRow row : tbMatrix.getRowsReadOnly() ) { + List<MatrixElement> elements = row.getElements(); + OTU xmlOTU = getOTUById(xmlOTUs, row.getTaxonLabel().getId()); + if ( characterList.size() <= MAX_GRANULAR_NCHAR && xmlOTUs.getAllOTUs().size() <= MAX_GRANULAR_NTAX ) { + for ( int elementIndex = 0; elementIndex < tbMatrix.getnChar(); elementIndex++ ) { + ContinuousMatrixElement tbCell = (ContinuousMatrixElement)elements.get(elementIndex); + MatrixCell<Double> xmlCell = xmlMatrix.getCell(xmlOTU, characterList.get(elementIndex)); + xmlCell.setValue(tbCell.getValue()); + attachTreeBaseID((Annotatable)xmlCell,tbCell,DiscreteMatrixElement.class); + } + } + else { + String seq = row.buildElementAsString(); + xmlMatrix.setSeq(seq,xmlOTU); + } + } + } + } Modified: trunk/treebase-core/src/main/java/org/cipres/treebase/domain/nexus/nexml/NexmlOTUConverter.java =================================================================== --- trunk/treebase-core/src/main/java/org/cipres/treebase/domain/nexus/nexml/NexmlOTUConverter.java 2009-06-23 10:50:48 UTC (rev 72) +++ trunk/treebase-core/src/main/java/org/cipres/treebase/domain/nexus/nexml/NexmlOTUConverter.java 2009-06-23 10:52:08 UTC (rev 73) @@ -27,9 +27,11 @@ */ public TaxonLabelSet fromXmlToTreeBase (OTUs xmlOTUs) { TaxonLabelSet labelSet = new TaxonLabelSet(); - attachTreeBaseID(xmlOTUs,labelSet); + attachTreeBaseID(xmlOTUs,labelSet,TaxonLabelSet.class); labelSet.setStudy(getStudy()); - labelSet.setTitle(xmlOTUs.getLabel()); + if ( null != xmlOTUs.getLabel() ) { + labelSet.setTitle(xmlOTUs.getLabel()); + } labelSet.setTaxa(true); for ( OTU xmlOTU : xmlOTUs.getAllOTUs() ) { TaxonLabel taxonLabel = fromXmlToTreeBase(xmlOTU); @@ -46,7 +48,7 @@ public OTUs fromTreeBaseToXml(TaxonLabelSet taxonLabelSet) { OTUs xmlOTUs = getDocument().createOTUs(); xmlOTUs.setLabel(taxonLabelSet.getTitle()); - attachTreeBaseID(xmlOTUs,taxonLabelSet); + attachTreeBaseID(xmlOTUs,taxonLabelSet,TaxonLabelSet.class); for ( TaxonLabel taxonLabel : taxonLabelSet.getTaxonLabelsReadOnly() ) { fromTreeBaseToXml(taxonLabel,xmlOTUs); } @@ -61,7 +63,7 @@ public TaxonLabel fromXmlToTreeBase(OTU xmlOTU) { TaxonLabel taxonLabel = getTaxonLabelHome().getByDescriptionAndStudy(xmlOTU.getLabel(), getStudy()); taxonLabel.setStudy(getStudy()); - attachTreeBaseID(xmlOTU,taxonLabel); + attachTreeBaseID(xmlOTU,taxonLabel,TaxonLabel.class); return taxonLabel; } @@ -73,8 +75,10 @@ */ public OTU fromTreeBaseToXml(TaxonLabel taxonLabel,OTUs xmlOTUs) { OTU xmlOTU = xmlOTUs.createOTU(); - xmlOTU.setLabel(taxonLabel.getTaxonLabel()); - attachTreeBaseID(xmlOTU,taxonLabel); + if ( null != taxonLabel.getTaxonLabel() ) { + xmlOTU.setLabel(taxonLabel.getTaxonLabel()); + } + attachTreeBaseID(xmlOTU,taxonLabel,TaxonLabel.class); if ( null != taxonLabel.getNcbiTaxID() ) { attachAnnotation("dc:identifier", "NCBI:" + taxonLabel.getNcbiTaxID(), mDCURI, xmlOTU); } Modified: trunk/treebase-core/src/main/java/org/cipres/treebase/domain/nexus/nexml/NexmlObjectConverter.java =================================================================== --- trunk/treebase-core/src/main/java/org/cipres/treebase/domain/nexus/nexml/NexmlObjectConverter.java 2009-06-23 10:50:48 UTC (rev 72) +++ trunk/treebase-core/src/main/java/org/cipres/treebase/domain/nexus/nexml/NexmlObjectConverter.java 2009-06-23 10:52:08 UTC (rev 73) @@ -2,16 +2,22 @@ import java.net.URI; import java.net.URISyntaxException; +import java.text.CharacterIterator; +import java.text.StringCharacterIterator; import java.util.Iterator; import java.util.Set; import org.cipres.treebase.TreebaseIDString; +import org.cipres.treebase.NamespacedGUID; +import org.cipres.treebase.TreebaseIDString.MalformedTreebaseIDString; import org.cipres.treebase.domain.TBPersistable; import org.cipres.treebase.domain.nexus.AbstractNexusConverter; import org.cipres.treebase.domain.study.Study; import org.cipres.treebase.domain.taxon.TaxonLabelHome; import org.nexml.model.Annotatable; import org.nexml.model.Document; +import org.nexml.model.OTU; +import org.nexml.model.OTUs; public class NexmlObjectConverter extends AbstractNexusConverter { protected static URI mDCURI; @@ -44,14 +50,9 @@ * @param tbPersistable * @return */ - public String makeNamespacedID (TBPersistable tbPersistable) { - StringBuilder sb = new StringBuilder(); - sb - .append(TreeBASE2Prefix) - .append(':') - .append(TreebaseIDString.getPrefixForClass(tbPersistable.getClass())) - .append(tbPersistable.getId()); - return sb.toString(); + private String makeNamespacedID (TBPersistable tbPersistable,Class<?> persistableClass) { + TreebaseIDString tbIDString = new TreebaseIDString(persistableClass,tbPersistable.getId()); + return tbIDString.getNamespacedGUID().toString(); } /** @@ -59,8 +60,10 @@ * @param annotatable * @param tbPersistable */ - protected void attachTreeBaseID(Annotatable annotatable,TBPersistable tbPersistable) { - attachAnnotation(mDCIdentifier,makeNamespacedID(tbPersistable),mDCURI,annotatable); + protected void attachTreeBaseID(Annotatable annotatable,TBPersistable tbPersistable,Class<?> persistableClass) { + if ( null != tbPersistable.getId() ) { + attachAnnotation(mDCIdentifier,makeNamespacedID(tbPersistable,persistableClass),mDCURI,annotatable); + } } /** @@ -71,7 +74,7 @@ * @param annotatable */ protected void attachAnnotation(String key,String value,URI namespace,Annotatable annotatable) { - annotatable.addAnnotationValue(key, namespace, value); + annotatable.addAnnotationValue(key, namespace, value); // FIXME! Attaches meta element as last child } /** @@ -80,22 +83,49 @@ * @param persistableClass * @return */ - protected Long readTreeBaseID(Annotatable annotatable, Class<? extends TBPersistable> persistableClass) { - Set<Object> values = annotatable.getAnnotationValues(mDCIdentifier); - Iterator<Object> objectIterator = values.iterator(); + protected Long readTreeBaseID(Annotatable annotatable) { + + // this will return the value object associated with a + // dc:identifier predicate in a nexml meta annotation, + // e.g. <meta property="dc:identifier" content="TB2:Tr231"/> + // this will return something that stringifies to TB2:Tr231 + Set<Object> dublinCoreIdentifierObjects = annotatable.getAnnotationValues(mDCIdentifier); + Iterator<Object> objectIterator = dublinCoreIdentifierObjects.iterator(); while ( objectIterator.hasNext() ) { - String namespacedId = objectIterator.next().toString(); - StringBuffer prefix = new StringBuffer(); - prefix - .append(TreeBASE2Prefix) - .append(':') - .append(TreebaseIDString.getPrefixForClass(persistableClass)); - if ( namespacedId.startsWith(prefix.toString()) ) { - return Long.parseLong(namespacedId.substring(prefix.length())); + TreebaseIDString treebaseIDString = null; + NamespacedGUID namespacedGUID = null; + try { + namespacedGUID = new NamespacedGUID(objectIterator.next().toString()); + treebaseIDString = namespacedGUID.getTreebaseIDString(); + return treebaseIDString.getId(); + } catch ( MalformedTreebaseIDString e ) { + // XXX do nothing, it's OK, it means we're + // parsing an id from a different naming + // authority, e.g. uBio or NCBI } } return null; } + + protected OTUs getOTUsById(Long taxonLabelSetId) { + for ( OTUs otus : getDocument().getOTUsList() ) { + Long annotatedID = readTreeBaseID(otus); + if ( taxonLabelSetId.equals(annotatedID) ) { + return otus; + } + } + return null; + } + + protected OTU getOTUById(OTUs otus,Long taxonLabelId) { + for ( OTU otu : otus.getAllOTUs() ) { + Long annotatedID = readTreeBaseID(otu); + if ( taxonLabelId.equals(annotatedID) ) { + return otu; + } + } + return null; + } public Document getDocument() { return mDocument; @@ -104,4 +134,29 @@ public void setDocument(Document document) { mDocument = document; } + + public static String forXML(String aText){ + final StringBuilder result = new StringBuilder(); + final StringCharacterIterator iterator = new StringCharacterIterator(aText); + char character = iterator.current(); + while (character != CharacterIterator.DONE) { + if (character == '<') { + result.append("<"); + } else if (character == '>') { + result.append(">"); + } else if (character == '\"') { + result.append("""); + } else if (character == '\'') { + result.append("'"); + } else if (character == '&') { + result.append("&"); + } else { + //the char is not a special one + //add it to the result as is + result.append(character); + } + character = iterator.next(); + } + return result.toString(); + } } Modified: trunk/treebase-core/src/main/java/org/cipres/treebase/domain/nexus/nexml/NexmlTreeBlockConverter.java =================================================================== --- trunk/treebase-core/src/main/java/org/cipres/treebase/domain/nexus/nexml/NexmlTreeBlockConverter.java 2009-06-23 10:50:48 UTC (rev 72) +++ trunk/treebase-core/src/main/java/org/cipres/treebase/domain/nexus/nexml/NexmlTreeBlockConverter.java 2009-06-23 10:52:08 UTC (rev 73) @@ -17,6 +17,7 @@ import org.nexml.model.IntEdge; import org.nexml.model.Network; import org.nexml.model.Node; +import org.nexml.model.OTU; import org.nexml.model.OTUs; import org.nexml.model.Tree; @@ -38,7 +39,7 @@ */ public TreeBlock fromXmlToTreeBase(org.nexml.model.TreeBlock xmlTreeBlock) { OTUs xmlOTUs = xmlTreeBlock.getOTUs(); - Long tbTaxonLabelSetId = readTreeBaseID(xmlOTUs,TaxonLabelSet.class); + Long tbTaxonLabelSetId = readTreeBaseID(xmlOTUs); TreeBlock tbTreeBlock = new TreeBlock(); tbTreeBlock.setTitle(xmlTreeBlock.getLabel()); if ( null != tbTaxonLabelSetId ) { @@ -80,8 +81,10 @@ */ public Tree<?> fromTreeBaseToXml(PhyloTree phyloTree,org.nexml.model.TreeBlock xmlTreeBlock) { Tree<FloatEdge> xmlTree = xmlTreeBlock.createFloatTree(); - xmlTree.setLabel(phyloTree.getLabel()); - attachTreeBaseID(xmlTree, phyloTree); + if ( null != phyloTree.getLabel() ) { + xmlTree.setLabel(phyloTree.getLabel()); + } + attachTreeBaseID(xmlTree, phyloTree,PhyloTree.class); copyTreeBaseTree(phyloTree, xmlTree); return xmlTree; } @@ -93,18 +96,12 @@ */ public org.nexml.model.TreeBlock fromTreeBaseToXML(TreeBlock treeBlock) { TaxonLabelSet taxonLabelSet = treeBlock.getTaxonLabelSet(); - OTUs xmlOTUs = null; - Long taxonLabelSetId = taxonLabelSet.getId(); - for ( OTUs otus : getDocument().getOTUsList() ) { - Long otusId = readTreeBaseID(otus, TaxonLabelSet.class); - if ( taxonLabelSetId == otusId ) { - xmlOTUs = otus; - break; - } - } + OTUs xmlOTUs = getOTUsById(taxonLabelSet.getId()); org.nexml.model.TreeBlock xmlTreeBlock = getDocument().createTreeBlock(xmlOTUs); - xmlTreeBlock.setLabel(treeBlock.getTitle()); - attachTreeBaseID((Annotatable)xmlTreeBlock,treeBlock); + if ( null != treeBlock.getTitle() ) { + xmlTreeBlock.setLabel(treeBlock.getTitle()); + } + attachTreeBaseID((Annotatable)xmlTreeBlock,treeBlock,TreeBlock.class); for ( PhyloTree phyloTree : treeBlock.getTreeList() ) { fromTreeBaseToXml(phyloTree,xmlTreeBlock); } @@ -128,25 +125,27 @@ * @param xmlTree */ private void traverseTreeBaseTree(PhyloTree tbTree,PhyloTreeNode tbNode,Node xmlNode,Tree<FloatEdge> xmlTree) { - xmlNode.setLabel(tbNode.getName()); - attachTreeBaseID(xmlNode, tbNode); + if ( null != tbNode.getName() ) { + xmlNode.setLabel(tbNode.getName()); + } + attachTreeBaseID(xmlNode, tbNode,PhyloTreeNode.class); TaxonLabel taxonLabel = tbNode.getTaxonLabel(); if ( null != taxonLabel ) { Long taxonId = taxonLabel.getId(); for ( OTUs xmlOTUs : getDocument().getOTUsList() ) { - for ( org.nexml.model.OTU xmlOTU : xmlOTUs.getAllOTUs() ) { - Long currentTaxonId = readTreeBaseID(xmlOTU, TaxonLabel.class); - if ( taxonId == currentTaxonId ) { - xmlNode.setOTU(xmlOTU); - break; - } + OTU xmlOTU = getOTUById(xmlOTUs, taxonId); + if ( null != xmlOTU ) { + xmlNode.setOTU(xmlOTU); + break; } } } for ( PhyloTreeNode tbChildNode : tbNode.getChildNodes() ) { Node xmlChildNode = xmlTree.createNode(); FloatEdge xmlEdge = xmlTree.createEdge(xmlNode, xmlChildNode); - xmlEdge.setLength(tbChildNode.getBranchLength()); + if ( null != tbChildNode.getBranchLength() ) { + xmlEdge.setLength(tbChildNode.getBranchLength()); + } traverseTreeBaseTree(tbTree, tbChildNode, xmlChildNode, xmlTree); } } @@ -176,7 +175,7 @@ private void traverseXmlTree(Tree<?> xmlTree, StringBuilder sb, Node xmlNode, PhyloTreeNode tbNode, PhyloTree tbTree) { tbTree.addTreeNode(tbNode); if ( null != xmlNode.getOTU() ) { - Long tbTaxonLabelId = readTreeBaseID(xmlNode.getOTU(),TaxonLabel.class); + Long tbTaxonLabelId = readTreeBaseID(xmlNode.getOTU()); if ( null != tbTaxonLabelId ) { TaxonLabel tbTaxonLabel = getTaxonLabelHome() .findPersistedObjectByID(TaxonLabel.class, tbTaxonLabelId); This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |