001/*
002
003 *                    BioJava development code
004
005 *
006
007 * This code may be freely distributed and modified under the
008
009 * terms of the GNU Lesser General Public Licence.  This should
010
011 * be distributed with the code.  If you do not have a copy,
012
013 * see:
014
015 *
016
017 *      http://www.gnu.org/copyleft/lesser.html
018
019 *
020
021 * Copyright for this code is held jointly by the individual
022
023 * authors.  These should be listed in @author doc comments.
024
025 *
026
027 * For more information on the BioJava project and its aims,
028
029 * or to join the biojava-l mailing list, visit the home page
030
031 * at:
032
033 *
034
035 *      http://www.biojava.org/
036
037 *
038
039 */
040
041package org.biojava.bio.program.xff;
042
043
044
045import java.io.File;
046import java.io.FileReader;
047import java.io.FileWriter;
048import java.io.IOException;
049import java.io.PrintWriter;
050
051import javax.xml.parsers.SAXParserFactory;
052
053import org.biojava.bio.Annotation;
054import org.biojava.bio.BioException;
055import org.biojava.bio.SmallAnnotation;
056import org.biojava.bio.seq.FeatureHolder;
057import org.biojava.bio.seq.Sequence;
058import org.biojava.bio.seq.impl.SimpleSequence;
059import org.biojava.bio.seq.io.SequenceBuilder;
060import org.biojava.bio.seq.io.SequenceBuilderBase;
061import org.biojava.bio.symbol.Alphabet;
062import org.biojava.bio.symbol.DummySymbolList;
063import org.biojava.bio.symbol.FiniteAlphabet;
064import org.biojava.bio.symbol.Symbol;
065import org.biojava.bio.symbol.SymbolList;
066import org.biojava.utils.stax.SAX2StAXAdaptor;
067import org.biojava.utils.xml.PrettyXMLWriter;
068import org.biojava.utils.xml.XMLWriter;
069import org.xml.sax.ContentHandler;
070import org.xml.sax.InputSource;
071import org.xml.sax.SAXException;
072import org.xml.sax.XMLReader;
073
074
075
076/**
077
078 * Common functionality for manipulating XFF.
079
080 *
081
082 * @author Matthew Pocock
083
084 */
085
086public class XFFTools {
087        
088        public static final String XFF_NS = "http://www.bioxml.org/2000/xff";
089        
090        public static final String XFF_BIOJAVA_NS = "http://www.biojava.org/2001/xff-biojava";
091
092    public static void annotateXFF(File xffFile, final Sequence sequence)
093
094    throws IOException, SAXException, BioException {
095
096        annotateXFF(xffFile, sequence, Annotation.EMPTY_ANNOTATION);
097
098    }
099
100
101
102    public static void annotateXFF(File xffFile, final Sequence sequence, Annotation ann)
103
104    throws IOException, SAXException, BioException {
105
106        SequenceBuilder sb = new SequenceBuilderBase() {
107
108            { seq = sequence; }
109
110            public void addSymbols(Alphabet alpha, Symbol[] syms, int start, int length) {}
111
112        };
113
114
115
116        XFFFeatureSetHandler xffHandler = new XFFFeatureSetHandler();
117
118        xffHandler.setFeatureListener(sb);
119
120        xffHandler.setMergeAnnotation(ann);
121
122
123
124        ContentHandler saxHandler = new SAX2StAXAdaptor(xffHandler);
125
126        XMLReader parser;
127
128        try {
129
130            SAXParserFactory spf = SAXParserFactory.newInstance();
131
132            spf.setValidating(false);
133
134            spf.setNamespaceAware(true);
135
136            parser = spf.newSAXParser().getXMLReader();
137
138        } catch (Exception ex) {
139
140            throw new BioException("Error creating SAX parser",ex);
141
142        }
143
144        parser.setContentHandler(saxHandler);
145
146        InputSource is = new InputSource(new FileReader(xffFile));
147
148        parser.parse(is);
149
150
151
152        sb.makeSequence();
153
154    }
155
156
157
158    public static Sequence readXFF(File xffFile, String seqID, FiniteAlphabet alpha)
159
160    throws IOException, SAXException, BioException {
161
162        SymbolList dummy = new DummySymbolList(alpha, Integer.MAX_VALUE);
163
164        Sequence ourSeq = new SimpleSequence(dummy, seqID, seqID, new SmallAnnotation());
165
166        annotateXFF(xffFile, ourSeq);
167
168        return ourSeq;
169
170    }
171
172
173    public static Sequence readXFF(File xffFile, String seqID)
174
175    throws IOException, SAXException, BioException {
176
177        SymbolList dummy = new DummySymbolList(Alphabet.EMPTY_ALPHABET, Integer.MAX_VALUE);
178
179        Sequence ourSeq = new SimpleSequence(dummy, seqID, seqID, new SmallAnnotation());
180
181        annotateXFF(xffFile, ourSeq);
182
183        return ourSeq;
184
185    }
186        
187
188    public static void writeXFF(File xffFile, FeatureHolder features)
189
190    throws IOException {
191
192        PrintWriter xffPR = new PrintWriter(new FileWriter(xffFile));
193
194        writeXFF(xffPR, features);
195
196    }
197
198
199
200    public static void writeXFF(PrintWriter xffPR, FeatureHolder features)
201
202    throws IOException {
203
204        XMLWriter xmlWriter = new PrettyXMLWriter(xffPR);
205
206        XFFWriter xffWriter = new XFFWriter(new PropertyWriter());
207
208        xffWriter.writeFeatureSet(features, xmlWriter);
209
210        xffPR.flush();
211
212        xffPR.close();
213
214    }
215
216}
217