001/**
002 *  BioJava development code This code may be freely distributed and modified
003 *  under the terms of the GNU Lesser General Public Licence. This should be
004 *  distributed with the code. If you do not have a copy, see:
005 *  http://www.gnu.org/copyleft/lesser.html Copyright for this code is held
006 *  jointly by the individual authors. These should be listed in
007 *
008 *@author    doc comments. For more information on the BioJava project and its
009 *      aims, or to join the biojava-l mailing list, visit the home page at:
010 *      http://www.biojava.org/
011 */
012
013package org.biojava.bio.seq.io.game12;
014
015import org.biojava.bio.seq.StrandedFeature;
016import org.biojava.bio.seq.io.game.ElementRecognizer;
017import org.biojava.bio.symbol.Location;
018import org.biojava.utils.stax.StAXContentHandler;
019import org.xml.sax.Attributes;
020import org.xml.sax.SAXException;
021
022/**
023 *  Handles the GAME &lt;<seq_relationship>&gt; element
024 *
025 * @author     David Huen
026 * @since      1.2
027 */
028public class GAMESeqRelHandler
029         extends StAXFeatureHandler {
030    // <seq_relationship> provide feature positions on
031    // specified sequences.
032    // This element always has a <span> element that
033    // provides the actual coordinates.  This element
034    // just adds to it the target sequence name.
035    // I will assume there's only one <span> for
036    // each <seq_relationship>
037
038    // the only features I will need to consider for
039    // now are exons and translate offsets.
040
041    // this is not actually a good place to decide what to 
042    // do with incoming data.  I should shove it up the chain
043    // to the containing class.
044
045    // conclusion: I think I will forget about exons and
046    // and just have all transcripts as compound locations.
047
048    // database columns
049    private String type = null;
050    private String seq;
051//    private String alignment = null;
052    Location seqRelLoc;
053    StrandedFeature.Strand seqRelStrand;
054
055    // subclass the <span> handler to get access to its data
056    private class SpanHandler extends GAMESpanHandler
057    {
058        private SpanHandler(StAXFeatureHandler staxenv)
059        {
060            super(staxenv);
061//            System.out.println("entering SpanHandler");
062        }
063
064        public void endElementHandler(
065                String nsURI,
066                String localName,
067                String qName,
068                StAXContentHandler contentHandler) 
069        {
070            // validate
071            super.endElementHandler(nsURI, localName, qName, contentHandler);
072
073            // return the values
074//            System.out.println("in SpanHandler: " + loc + " " + strand);
075            seqRelLoc = loc;
076            seqRelStrand = strand;
077        }
078    }
079
080    // set up factory method
081    /**
082     *  Description of the Field
083     */
084    public final static StAXHandlerFactory GAME_SEQ_REL_HANDLER_FACTORY
085             =
086        new StAXHandlerFactory() {
087            public StAXContentHandler getHandler(StAXFeatureHandler staxenv) {
088                return new GAMESeqRelHandler(staxenv);
089            }
090        };
091
092
093    GAMESeqRelHandler(StAXFeatureHandler staxenv) {
094        // setup environment
095        super(staxenv);
096
097        // set handlers
098        // <span> external handler type
099        super.addHandler(new ElementRecognizer.ByLocalName("span"),
100            new StAXHandlerFactory() {
101                public StAXContentHandler getHandler(StAXFeatureHandler staxenv) {
102                    return new SpanHandler(staxenv);
103                }
104            }
105        );
106
107        // <alignment>
108//        super.addHandler(new ElementRecognizer.ByLocalName("alignment"),
109//            new StAXHandlerFactory() {
110//                public StAXContentHandler getHandler(StAXFeatureHandler staxenv, long parentID) {
111//                    return new AlignmentHandler();
112//                }
113//            }
114//                );
115    }
116
117
118//    private class AlignmentHandler extends StringElementHandlerBase {
119//        /**
120//         *  Sets the stringValue attribute of the AlignmentHandler object
121//         *
122//         *@param  s  The new stringValue value
123//         */
124//        protected void setStringValue(String s) {
125//            alignment = s.trim();
126//        }
127//    }
128
129    public void startElementHandler(
130            String nsURI,
131            String localName,
132            String qName,
133            Attributes attrs) {
134        // acquire attributes here
135        type = attrs.getValue("type");
136        seq = attrs.getValue("seq");
137    }
138
139    public void endElementHandler(
140            String nsURI,
141            String localName,
142            String qName,
143            StAXContentHandler contentHandler) 
144        throws SAXException 
145    {
146        // prevalidate
147        if ((type == null) || (seq == null) ) {
148            System.err.println("improperly formed <span> element.");
149        }
150    }
151}