001/*
002 *                    BioJava development code
003 *
004 * This code may be freely distributed and modified under the
005 * terms of the GNU Lesser General Public Licence.  This should
006 * be distributed with the code.  If you do not have a copy,
007 * see:
008 *
009 *      http://www.gnu.org/copyleft/lesser.html
010 *
011 * Copyright for this code is held jointly by the individual
012 * authors.  These should be listed in @author doc comments.
013 *
014 * For more information on the BioJava project and its aims,
015 * or to join the biojava-l mailing list, visit the home page
016 * at:
017 *
018 *      http://www.biojava.org/
019 */
020
021
022package org.biojava.bio.dist;
023
024import java.io.BufferedWriter;
025import java.io.IOException;
026import java.io.OutputStream;
027import java.io.OutputStreamWriter;
028import java.util.Iterator;
029
030import org.biojava.bio.BioError;
031import org.biojava.bio.symbol.FiniteAlphabet;
032import org.biojava.bio.symbol.IllegalSymbolException;
033import org.biojava.bio.symbol.Symbol;
034
035
036/**
037 * Writes an OrderNDistribution or simple Distribution to an XML file.
038 *
039 * @author Russell Smithies
040 * @author Mark Schreiber
041 * @since 1.3
042 */
043public class XMLDistributionWriter{
044
045  BufferedWriter out = null;
046
047
048    private void writeOND2XML(OrderNDistribution ond, OutputStream os) throws IOException{
049        Distribution conditionedDist = null;
050        FiniteAlphabet conditionedAlpha = null;
051        out = new BufferedWriter(new OutputStreamWriter(os));
052
053
054        out.write("<?xml version=\"1.0\" ?>");
055        out.write("<Distribution type=\"OrderNDistribution\">");
056        out.write("<alphabet name=\"" + ond.getAlphabet().getName() +
057                  "\"/>");
058
059        for (Iterator i = ((FiniteAlphabet) ond.getConditioningAlphabet()).iterator();
060             i.hasNext();) {
061          Symbol sym = (Symbol) i.next();
062          out.write("<conditioning_symbol name=\"" + sym.getName() +
063                    "\">");
064
065          try {
066            conditionedDist = ond.getDistribution(sym);
067          } catch (IllegalSymbolException ex) {
068            throw new BioError("Distribution has been built with Illegal Symbols !?", ex);
069          }
070
071          conditionedAlpha = (FiniteAlphabet) conditionedDist.getAlphabet();
072
073          for (Iterator it = conditionedAlpha.iterator(); it.hasNext();) {
074            Symbol condSym = (Symbol) it.next();
075            double weight = 0.0;
076
077            try {
078              weight = conditionedDist.getWeight(condSym);
079            } catch (IllegalSymbolException ex) {
080              throw new BioError("Distribution has been built with Illegal Symbols !?", ex);
081            }
082
083            out.write("<weight sym=\"" + condSym.getName() +
084                      "\" prob=\"" + weight + "\"/>");
085          }
086
087          out.write("</conditioning_symbol>");
088        }
089
090        out.write("</Distribution>");
091        out.flush();
092    } //end writeXML
093
094
095    private void writeDist2XML(Distribution d, OutputStream os) throws IOException{
096         out = new BufferedWriter(new OutputStreamWriter(os));
097
098
099         out.write("<?xml version=\"1.0\" ?>");
100         out.write("<Distribution type=\"Distribution\">");
101         out.write("<alphabet name=\"" + d.getAlphabet().getName() +
102                   "\"/>");
103
104         for (Iterator i = ((FiniteAlphabet) d.getAlphabet()).iterator();
105              i.hasNext();) {
106           Symbol sym = (Symbol) i.next();
107           double weight = 0.0;
108
109           try {
110             weight = d.getWeight(sym);
111           } catch (IllegalSymbolException ex) {
112             throw new BioError("Distribution has been built with Illegal Symbols !?", ex);
113           }
114
115           out.write("<weight sym=\"" + sym.getName() +
116                     "\" prob=\"" + weight + "\"/>");
117         }
118         out.write("</Distribution>");
119         out.flush();
120
121    } //end writeXML
122
123    /**
124     * Writes an OrderNDistribution or simple Distribution to an XML file.
125     * @param d  Distribution to write.
126     * @param os OutputStream to write to.
127     * @throws IOException if an error occurs during writing
128     */
129    public void writeDistribution(Distribution d, OutputStream os) throws IOException{
130        if (d instanceof OrderNDistribution) {
131            writeOND2XML((OrderNDistribution) d, os);
132        } else {
133            writeDist2XML(d, os);
134        }
135
136    }
137}