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.ArrayList;
023import java.util.List;
024
025import org.biojava.bio.seq.StrandedFeature;
026import org.biojava.bio.symbol.Location;
027import org.biojava.utils.ChangeVetoException;
028import org.xml.sax.Attributes;
029import org.xml.sax.SAXException;
030
031/**
032 * @author Hanning Ni    Doubletwist Inc
033 */
034public class AGAVEGeneHandler
035               extends StAXFeatureHandler implements AGAVEFeatureCallbackItf
036
037{
038  public static final StAXHandlerFactory AGAVE_GENE_HANDLER_FACTORY
039    = new StAXHandlerFactory() {
040    public StAXContentHandler getHandler(StAXFeatureHandler staxenv) {
041      return new AGAVEGeneHandler(staxenv);
042    }
043  };
044
045 AGAVEGeneHandler(StAXFeatureHandler staxenv) {
046    // setup up environment stuff
047    super( staxenv );
048    featureListener = staxenv.featureListener;
049    setHandlerCharacteristics("gene", true);
050
051    // setup handlers
052       //
053       super.addHandler(new ElementRecognizer.ByLocalName("classification"),
054         AGAVEClassificationHandler.AGAVE_CLASSIFICATION_HANDLER_FACTORY);
055       //
056       super.addHandler(new ElementRecognizer.ByLocalName("note"),
057         AGAVENotePropHandler.AGAVE_NOTE_PROP_HANDLER_FACTORY);
058
059      super.addHandler(new ElementRecognizer.ByLocalName("seq_location"),
060         AGAVESeqLocationPropHandler.AGAVE_SEQ_LOCATION_PROP_HANDLER_FACTORY);
061
062      super.addHandler(new ElementRecognizer.ByLocalName("xrefs"),
063         AGAVEXrefsPropHandler.AGAVE_XREFS_PROP_HANDLER_FACTORY);
064       //
065       super.addHandler(new ElementRecognizer.ByLocalName("evidence"),
066         AGAVEEvidenceHandler.AGAVE_EVIDENCE_HANDLER_FACTORY);
067
068      super.addHandler(new ElementRecognizer.ByLocalName("qualifier"),
069         AGAVEQualifierPropHandler.AGAVE_QUALIFIER_PROP_HANDLER_FACTORY);
070
071      super.addHandler(new ElementRecognizer.ByLocalName("seq_feature"),
072         AGAVESeqFeatureHandler.AGAVE_SEQ_FEATURE_HANDLER_FACTORY);
073
074      super.addHandler(new ElementRecognizer.ByLocalName("related_annot"),
075         AGAVERelatedAnnotPropHandler.AGAVE_RELATED_ANNOT_PROP_HANDLER_FACTORY);
076
077      super.addHandler(new ElementRecognizer.ByLocalName("transcript"),
078         AGAVETranscriptHandler.AGAVE_TRANSCRIPT_HANDLER_FACTORY);
079
080  }
081
082  public void startElementHandler(
083                String nsURI,
084                String localName,
085                String qName,
086                Attributes attrs)
087                throws SAXException
088  {
089      try{
090      featureListener.startFeature( featureTemplate );
091      boolean forFeature = true ;
092      setProperty( "element_id",  attrs.getValue("element_id") , forFeature) ;
093      setProperty( "label",  attrs.getValue("label"), forFeature ) ;
094
095      featureTemplate.type = "gene" ;
096      }catch(Exception e){
097          throw new SAXException( e.getMessage() ) ;
098      }
099
100  }
101
102
103   /*
104   protected Feature.Template createTemplate() {
105    // create Gene Template for this
106    StrandedFeature.Template st = new StrandedFeature.Template();
107
108    // assume feature set to describe a transcript
109    st.type = "transcript";
110    st.strand = StrandedFeature.UNKNOWN;
111    // set up annotation bundle
112    st.annotation = annot;
113    st.location = new  Location.EmptyLocation();
114    if( staxenv != null )
115        staxenv. subFeatures .add( this ) ;
116
117    return st;
118  }*/
119
120
121  public void addProperty(AGAVEProperty prop)
122  {
123      try{
124         Object ob =UtilHelper.getProperty(staxenv.featureTemplate.annotation,"qualifier");
125         if( ob != null )
126             ((List)ob).add( prop ) ;
127         else
128         {
129             List props = new ArrayList(1) ;
130             props.add( prop ) ;
131             staxenv.featureTemplate.annotation.setProperty("qualifier", props)  ;
132         }
133      }catch (ChangeVetoException cve) {
134         cve.printStackTrace() ;
135      }
136   }
137  public void reportFeature(Location loc)
138  {
139      ((StrandedFeature.Template)featureTemplate).location = loc  ;
140  }
141  public void reportStrand(StrandedFeature.Strand strand)
142  {
143      ((StrandedFeature.Template)featureTemplate).strand = strand ;
144  }
145
146  public void addRelatedAnnot(AGAVERelatedAnnot prop)
147  {
148      try{
149         Object ob = UtilHelper.getProperty(staxenv.featureTemplate.annotation,"related_annot");
150         if( ob != null )
151             ((List)ob).add( prop ) ;
152         else
153         {
154             List props = new ArrayList(1) ;
155             props.add( prop ) ;
156             staxenv.featureTemplate.annotation.setProperty("related_annot", props)  ;
157         }
158      }catch (ChangeVetoException cve) {
159         cve.printStackTrace() ;
160      }
161   }
162}
163