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.seq.io.agave;
022import java.util.ListIterator;
023
024import org.biojava.bio.seq.StrandedFeature;
025import org.biojava.bio.symbol.RangeLocation;
026import org.xml.sax.Attributes;
027import org.xml.sax.SAXException;
028
029/**
030 * seq_location
031 *
032 * @author Hanning Ni    Doubletwist Inc
033 */
034public class AGAVESeqLocationPropHandler
035 extends StAXPropertyHandler
036{
037
038   // set up factory metho
039  public static final StAXHandlerFactory AGAVE_SEQ_LOCATION_PROP_HANDLER_FACTORY
040    = new StAXHandlerFactory() {
041    public StAXContentHandler getHandler(StAXFeatureHandler staxenv) {
042      return new AGAVESeqLocationPropHandler(staxenv);
043    }
044  };
045   private int start , end ;
046   private String strand = "false" ;
047
048   AGAVESeqLocationPropHandler(StAXFeatureHandler staxenv) {
049    // execute superclass method to setup environment
050    super(staxenv);
051    setHandlerCharacteristics("seq_location", true);
052   }
053
054  public void startElementHandler(
055                String nsURI,
056                String localName,
057                String qName,
058                Attributes attrs)
059         throws SAXException
060  {
061      start =new Integer( attrs.getValue( "least_start" ) ) .intValue() ;
062      end = new Integer( attrs.getValue( "greatest_end" ) ) .intValue() ;
063      strand = attrs.getValue("is_on_complement") ;
064  }
065
066
067
068   public void endElementHandler(
069                String nsURI,
070                String localName,
071                String qName,
072                StAXContentHandler handler)
073                throws SAXException
074  {
075        int currLevel = staxenv.getLevel();
076        if (currLevel >=1)
077        {
078            ListIterator li = staxenv.getHandlerStackIterator(currLevel);
079            while (li.hasPrevious())
080            {
081                // THOMASD fixed strand == null crash
082
083                Object ob = li.previous();
084                if (ob instanceof AGAVEFeatureCallbackItf)
085                {
086                    ((AGAVEFeatureCallbackItf) ob).reportFeature(  new RangeLocation(start, end) );
087                    if( "true".equalsIgnoreCase(strand) )
088                        ((AGAVEFeatureCallbackItf) ob).reportStrand( StrandedFeature.NEGATIVE );
089                   else if( "false".equalsIgnoreCase(strand) )
090                         ((AGAVEFeatureCallbackItf) ob).reportStrand( StrandedFeature.POSITIVE );
091                   else
092                       ((AGAVEFeatureCallbackItf) ob).reportStrand( StrandedFeature.UNKNOWN );
093                    return;
094                }
095            }
096
097        }
098   }
099}