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