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 org.biojava.bio.BioException;
023import org.biojava.bio.seq.DNATools;
024import org.biojava.bio.seq.Sequence;
025import org.biojava.bio.seq.StrandedFeature;
026import org.biojava.bio.seq.impl.SimpleSequence;
027import org.biojava.bio.symbol.Location;
028import org.biojava.bio.symbol.SymbolList;
029import org.biojava.utils.ChangeVetoException;
030import org.xml.sax.Attributes;
031import org.xml.sax.SAXException;
032
033/**
034 *
035 * Handles the AGAVE <bio_sequence> element
036 *
037 * @author David Huen
038 * @author Hanning Ni     Doubletwist Inc
039 * @author Greg Cox
040 */
041public class AGAVEBioSeqHandler
042               extends StAXFeatureHandler
043               implements  AGAVEBioSeqCallbackItf, SequenceHandler
044{
045  public static final StAXHandlerFactory AGAVE_BIO_SEQ_HANDLER_FACTORY
046    = new StAXHandlerFactory() {
047    public StAXContentHandler getHandler(StAXFeatureHandler staxenv) {
048      return new AGAVEBioSeqHandler(staxenv);
049    }
050  };
051
052  //dna sequence,
053  private SymbolList dna ;
054  protected Sequence sequence ;
055
056  AGAVEBioSeqHandler(StAXFeatureHandler staxenv) {
057    // setup up environment stuff
058    super( staxenv );
059    featureListener = staxenv.featureListener;
060    setHandlerCharacteristics("bio_sequence", true);
061
062    // setup handlers
063       // <db_id>
064       super.addHandler(new ElementRecognizer.ByLocalName("db_id"),
065         AGAVEDbIdPropHandler.AGAVE_DBID_PROP_HANDLER_FACTORY);
066       // <note>
067       super.addHandler(new ElementRecognizer.ByLocalName("note"),
068         AGAVENotePropHandler.AGAVE_NOTE_PROP_HANDLER_FACTORY);
069       // <gene>
070       super.addHandler(new ElementRecognizer.ByLocalName("description"),
071         AGAVEDescPropHandler.AGAVE_DESC_PROP_HANDLER_FACTORY);
072       // <keyword>
073       super.addHandler(new ElementRecognizer.ByLocalName("keyword"),
074         AGAVEKeywordPropHandler.AGAVE_KEYWORD_PROP_HANDLER_FACTORY);
075       // <sequence>
076       super.addHandler(new ElementRecognizer.ByLocalName("sequence"),
077         AGAVESeqPropHandler.AGAVE_SEQ_PROP_HANDLER_FACTORY);
078       // <alt_ids>
079       super.addHandler(new ElementRecognizer.ByLocalName("alt_ids"),
080         AGAVEAltIdsPropHandler.AGAVE_ALT_IDS_PROP_HANDLER_FACTORY);
081       // <xrefs>
082       super.addHandler(new ElementRecognizer.ByLocalName("xrefs"),
083         AGAVEXrefsPropHandler.AGAVE_XREFS_PROP_HANDLER_FACTORY);
084       //<sequence_map>
085       super.addHandler(new ElementRecognizer.ByLocalName("sequence_map"),
086         AGAVESeqMapHandler.AGAVE_SEQ_MAP_HANDLER_FACTORY);
087       //<map_location>
088       super.addHandler(new ElementRecognizer.ByLocalName("map_location"),
089         AGAVEMapLocationPropHandler.AGAVE_MAP_LOCATION_PROP_HANDLER_FACTORY);
090       //<classification>
091       super.addHandler(new ElementRecognizer.ByLocalName("classification"),
092         AGAVEClassificationHandler.AGAVE_CLASSIFICATION_HANDLER_FACTORY);
093  }
094
095  public void reportStrand(StrandedFeature.Strand strand)
096  {
097    // obtains strand from elements that are in the know.
098    ((StrandedFeature.Template) featureTemplate).strand = strand;
099  }
100  public void reportFeature(Location loc)
101  {
102    // obtains strand from elements that are in the know.
103    ((StrandedFeature.Template) featureTemplate).location = loc;
104  }
105
106
107  public void reportDna(String dna_seq)
108  {
109    try{
110       StringBuffer sb = new StringBuffer()  ;
111       for( int i = 0 ; i < dna_seq.length(); i++)
112       {
113           char c = dna_seq.charAt(i) ;
114           if( c != ' '  && c != '\n' && c!= '\t')
115              sb.append( c  );
116       }
117       dna = DNATools.createDNA( sb.substring(0) );
118     }catch(Exception e){ e.printStackTrace() ; }
119  }
120
121  public void startElementHandler(
122                String nsURI,
123                String localName,
124                String qName,
125                Attributes attrs)
126                throws SAXException
127  {
128      try{
129      featureListener.startSequence();
130      boolean forFeature = false ;
131      setProperty( "element_id",  attrs.getValue("element_id") , forFeature) ;
132      setProperty( "sequence_id",  attrs.getValue("sequence_id") , forFeature) ;
133      setProperty( "seq_length",  attrs.getValue("seq_length") , forFeature) ;
134      setProperty( "molecule_type",  attrs.getValue("molecule_type") , forFeature) ;
135      setProperty( "organism_name",  attrs.getValue("organism_name"), forFeature ) ;
136      setProperty( "taxon_id",  attrs.getValue("taxon_id") ,   forFeature) ;
137      setProperty( "clone_id",  attrs.getValue("clone_id"), forFeature ) ;
138      setProperty( "clone_library",  attrs.getValue("clone_library"), forFeature ) ;
139      setProperty( "chromosome",  attrs.getValue("chromosome") , forFeature) ;
140      setProperty( "map_position",  attrs.getValue("map_position") , forFeature) ;
141      setProperty( "ec_number",  attrs.getValue("ec_number"), forFeature ) ;
142      setProperty( "create_date",  attrs.getValue("create_date") ,  forFeature) ;
143      setProperty( "update_date",  attrs.getValue("update_date") , forFeature) ;
144      }catch(Exception e)
145      {
146          throw new SAXException( e.getMessage() ) ;
147      }
148  }
149
150
151   /*
152   protected Feature.Template createTemplate() {
153    // create Gene Template for this
154    StrandedFeature.Template st = new StrandedFeature.Template();
155
156    // assume feature set to describe a transcript
157    st.type = "bio_sequence";
158    st.strand = StrandedFeature.UNKNOWN;
159    // set up annotation bundle
160    st.annotation = new SmallAnnotation();
161    st.location = new  Location.EmptyLocation();
162
163
164    if( staxenv != null )
165        staxenv. subFeatures .add( this ) ;
166
167    return st;
168  }*/
169
170
171  public void endElementHandler(
172                String nsURI,
173                String localName,
174                String qName,
175                StAXContentHandler handler)
176                throws SAXException
177  {
178       //super.endElement( nsURI, localName, qName, handler);
179      //create sequence
180      try{
181       if( dna == null )
182          throw new SAXException("dna sequence need offered for creating sequence object");
183
184       sequence  = new SimpleSequence( dna, " ", "simple_sequence " , annot ) ;
185       if( featureTemplate == null )
186          throw new SAXException("feature template is null ") ;
187
188       //Feature feature = sequence.createFeature(  featureTemplate ) ;
189       //realizeSubFeatures( feature ) ;
190       addFeatureToSequence(sequence) ;
191       appendToTop(sequence, staxenv) ;
192       featureListener.endSequence();
193       }catch(BioException be){
194            throw new SAXException( "bio exception" ) ;
195       }catch(ChangeVetoException cve){
196            throw new SAXException("change veto exception") ;
197       }catch(Exception e)
198       {
199          throw new SAXException( e.getMessage() ) ;
200       }
201  }
202   private void appendToTop(Sequence sequence, StAXFeatureHandler staxenv)
203    {
204       if (staxenv instanceof AGAVEContigCallbackItf)
205       {
206             ((AGAVEContigCallbackItf) staxenv).reportSequence( sequence );
207             return;
208       }
209       if (staxenv instanceof AGAVECallbackItf)
210       {
211             ((AGAVECallbackItf) staxenv).reportSequence( sequence );
212              return;
213       }
214       appendToTop(sequence, staxenv.staxenv );
215    }
216
217}
218