| 1 | /* |
| 2 | * XMLExporter.java |
| 3 | * |
| 4 | * Copyright (c) 2002-2015 Alexei Drummond, Andrew Rambaut and Marc Suchard |
| 5 | * |
| 6 | * This file is part of BEAST. |
| 7 | * See the NOTICE file distributed with this work for additional |
| 8 | * information regarding copyright ownership and licensing. |
| 9 | * |
| 10 | * BEAST is free software; you can redistribute it and/or modify |
| 11 | * it under the terms of the GNU Lesser General Public License as |
| 12 | * published by the Free Software Foundation; either version 2 |
| 13 | * of the License, or (at your option) any later version. |
| 14 | * |
| 15 | * BEAST is distributed in the hope that it will be useful, |
| 16 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 17 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 18 | * GNU Lesser General Public License for more details. |
| 19 | * |
| 20 | * You should have received a copy of the GNU Lesser General Public |
| 21 | * License along with BEAST; if not, write to the |
| 22 | * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor, |
| 23 | * Boston, MA 02110-1301 USA |
| 24 | */ |
| 25 | |
| 26 | package dr.app.bss; |
| 27 | |
| 28 | import java.io.IOException; |
| 29 | import java.io.StringWriter; |
| 30 | |
| 31 | import dr.app.beauti.util.XMLWriter; |
| 32 | import dr.evolution.alignment.SimpleAlignment; |
| 33 | import dr.evolution.datatype.DataType; |
| 34 | import dr.evolution.util.Taxon; |
| 35 | import dr.evoxml.AlignmentParser; |
| 36 | import dr.evoxml.SequenceParser; |
| 37 | import dr.evoxml.TaxonParser; |
| 38 | import dr.util.Attribute; |
| 39 | import dr.xml.XMLParser; |
| 40 | |
| 41 | public class XMLExporter { |
| 42 | |
| 43 | public XMLExporter() { |
| 44 | |
| 45 | }// END: Constructor |
| 46 | |
| 47 | public String exportAlignment(SimpleAlignment alignment) throws IOException { |
| 48 | |
| 49 | StringWriter sw = new StringWriter(); |
| 50 | XMLWriter writer = new XMLWriter(sw); |
| 51 | |
| 52 | // TODO: if we keep the taxa element than lets also write dates |
| 53 | // writer.writeOpenTag(TaxaParser.TAXA, // tagname |
| 54 | // new Attribute[] { // attributes[] |
| 55 | // new Attribute.Default<String>(XMLParser.ID, TaxaParser.TAXA) }); |
| 56 | // |
| 57 | // for (int i = 0; i < alignment.getTaxonCount(); i++) { |
| 58 | // |
| 59 | // Taxon taxon = alignment.getTaxon(i); |
| 60 | // |
| 61 | // writer.writeTag( |
| 62 | // TaxonParser.TAXON, // tagname |
| 63 | // new Attribute[] { // attributes[] |
| 64 | // new Attribute.Default<String>(XMLParser.ID, taxon.getId()) }, |
| 65 | // true // close |
| 66 | // ); |
| 67 | // |
| 68 | //// System.out.println(taxon.getAttribute(Utils.ABSOLUTE_HEIGHT)); |
| 69 | // |
| 70 | //// writer.writeCloseTag(TaxonParser.TAXON); |
| 71 | // |
| 72 | // }// END: taxon loop |
| 73 | // |
| 74 | // writer.writeCloseTag(TaxaParser.TAXA); |
| 75 | // |
| 76 | // writer.writeBlankLine(); |
| 77 | |
| 78 | writer.writeOpenTag(AlignmentParser.ALIGNMENT, // tagname |
| 79 | new Attribute[] { // attributes[] |
| 80 | new Attribute.Default<String>(XMLParser.ID, AlignmentParser.ALIGNMENT), |
| 81 | new Attribute.Default<String>(DataType.DATA_TYPE, alignment.getDataType().getDescription()) |
| 82 | }); |
| 83 | |
| 84 | for (int i = 0; i < alignment.getSequenceCount(); i++) { |
| 85 | |
| 86 | Taxon taxon = alignment.getTaxon(i); |
| 87 | |
| 88 | writer.writeOpenTag(SequenceParser.SEQUENCE); |
| 89 | writer.writeIDref(TaxonParser.TAXON, taxon.getId()); |
| 90 | writer.writeText(alignment.getSequence(i).getSequenceString()); |
| 91 | writer.writeCloseTag(SequenceParser.SEQUENCE); |
| 92 | |
| 93 | }//END: sequences loop |
| 94 | |
| 95 | writer.writeCloseTag(AlignmentParser.ALIGNMENT); |
| 96 | writer.close(); |
| 97 | |
| 98 | return sw.toString(); |
| 99 | }//END: exportAlignment |
| 100 | |
| 101 | }//END: class |