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 */
021package org.biojava.bio.program.sax;
022
023import java.io.BufferedReader;
024import java.io.IOException;
025
026import org.xml.sax.InputSource;
027import org.xml.sax.SAXException;
028import org.xml.sax.XMLReader;
029
030/**
031 * A SAX2 parser for dealing with a sequence alignments.  The format
032 * of any given alignment is automatically detected (e.g. ClustalW,
033 * Needle).
034 *
035 * Supported alignment formats are:
036 * <ul>
037 * <li>ClustalW
038 * <li>Needle
039 * </ul>
040 *
041 * Copyright &copy; 2000-2002 Cambridge Antibody Technology.
042 * 
043 * <p>
044 * Primary author -<ul>
045 * <li>Simon Brocklehurst (CAT)
046 * </ul>
047 * Other authors  -<ul>
048 * <li>Neil Benn          (CAT)
049 * <li>Lawrence Bower     (CAT)
050 * <li>Derek Crockford    (CAT)
051 * <li>Tim Dilks          (CAT)
052 * <li>Colin Hardman      (CAT)
053 * <li>Stuart Johnston    (CAT)
054 *</ul>
055 *
056 * @author Cambridge Antibody Technology (CAT)
057 * @version 1.0
058 *
059 */
060public class SequenceAlignmentSAXParser extends AbstractNativeAppSAXParser {
061
062
063    private static final int        CLUSTALW           = 2;
064    private static final int        NEEDLE             = 3;
065
066    private int                     iAlignmentType     = -1;
067    /**
068     * Initialises internal state
069     * Sets namespace prefix to "biojava"
070     */
071    public SequenceAlignmentSAXParser() {
072        this.setNamespacePrefix("biojava");
073    }
074
075    /**
076     * Describe 'parse' method here.
077     *
078     * @param poSource   -
079     */
080    public void parse(InputSource poSource ) 
081        throws IOException,SAXException {
082
083        BufferedReader            oContents;
084        String                    oLine = null;
085
086        //Use method form superclass
087        oContents = this.getContentStream(poSource);
088
089        oContents.mark(500);
090
091        oLine = null;
092        try {
093            oLine = oContents.readLine();
094        } catch (java.io.IOException x) {
095            System.out.println(x.getMessage());
096            System.out.println("Stream read interupted");
097        } // end try/catch
098
099        //at end of stream...
100
101        //System.out.println(oLine);
102        //Choose SAX Parser
103
104        XMLReader oChosenSAXParser = null;
105
106        if (oLine.startsWith("CLUSTAL W")) {
107            iAlignmentType = CLUSTALW;
108        }
109
110        if (oLine.startsWith("Global: ")) {
111            iAlignmentType = NEEDLE;
112
113        }
114
115        switch (iAlignmentType) {
116        case CLUSTALW:
117            //      System.out.println("FOUND CLUSTALW");
118            oChosenSAXParser = new ClustalWAlignmentSAXParser();
119            break;
120        case NEEDLE:
121            //System.out.println("FOUND NEEDLE");
122            oChosenSAXParser = new NeedleAlignmentSAXParser();
123            break;
124        default:
125            //      System.out.println("ALIGNMENT TYPE NOT FOUND");
126            //TODO SHOULD THROW AN EXCEPTION!
127            break;
128        }
129
130        oContents.reset();
131
132        XMLReader oParser = oChosenSAXParser;
133
134        oParser.setContentHandler(oHandler);
135
136        /*
137         * Parse stream with appropriate parser
138         */
139        oParser.parse(new InputSource(oContents));
140        
141        oContents.close();
142
143    }
144
145}