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 */
021
022package org.biojava.bio.seq.io.game;
023
024import org.biojava.bio.SmallAnnotation;
025import org.biojava.bio.seq.Feature;
026import org.biojava.bio.seq.StrandedFeature;
027import org.biojava.bio.symbol.Location;
028import org.biojava.bio.symbol.RangeLocation;
029import org.biojava.utils.ChangeVetoException;
030import org.biojava.utils.stax.StAXContentHandler;
031import org.xml.sax.Attributes;
032
033/**
034 * Handles the GAME <annotation> element
035 *
036 * @author David Huen
037 * @since 1.2
038 */
039public class GAMEAnnotationHandler 
040               extends StAXFeatureHandler 
041               implements GAMEFeatureCallbackItf {
042  // all Gadfly data concerning a single "gene" appears in
043  // single <annotation>.
044  // set up factory method
045  public static final StAXHandlerFactory GAME_ANNOTATION_HANDLER_FACTORY
046    = new StAXHandlerFactory() {
047    public StAXContentHandler getHandler(StAXFeatureHandler staxenv) {
048      return new GAMEAnnotationHandler(staxenv);
049    }
050  };
051
052  private Location range = Location.empty;
053
054  GAMEAnnotationHandler(StAXFeatureHandler staxenv) {
055    // setup up environment stuff
056    featureListener = staxenv.featureListener;
057    setHandlerCharacteristics("annotation", true);
058
059    // setup handlers
060       // <seq>: never seen it used yet.
061//       super.addHandler(new ElementRecognizer.ByLocalName("seq"),
062//         GAMESeqPropHandler.GAME_SEQ_PROP_HANDLER_FACTORY);
063       // <map_position>
064//       super.addHandler(new ElementRecognizer.ByLocalName("map_position"),
065//         GAMEMapPosPropHandler.GAME_MAP_POS_PROP_HANDLER_FACTORY);
066       // <gene>
067       super.addHandler(new ElementRecognizer.ByLocalName("gene"),
068         GAMEGenePropHandler.GAME_GENE_PROP_HANDLER_FACTORY);
069       // <feature_set>
070       super.addHandler(new ElementRecognizer.ByLocalName("feature_set"),
071         GAMEFeatureSetHandler.GAME_FEATURESET_HANDLER_FACTORY);
072       // <dbxref>
073       super.addHandler(new ElementRecognizer.ByLocalName("dbxref"),
074         GAMEDbxrefPropHandler.GAME_DBXREF_PROP_HANDLER_FACTORY);
075       // <Aspect>
076//       super.addHandler(new ElementRecognizer.ByLocalName("aspect"),
077//         GAMEAspectPropHandler.GAME_ASPECT_PROP_HANDLER_FACTORY);
078  }
079
080  protected Feature.Template createTemplate() {
081    // create Gene Template for this
082    StrandedFeature.Template gt = new StrandedFeature.Template();
083
084    // set up annotation bundle
085    gt.type = "gene";
086    gt.source = "";
087    gt.location = Location.empty;
088    gt.annotation = new SmallAnnotation();
089    gt.strand = StrandedFeature.UNKNOWN;
090
091    return gt;
092  }
093
094  public void reportFeature(Location loc)
095  {
096//    System.out.println("GAMEAnnotationHandler location is " + loc);
097    // accumulate locations of features here.
098    range = range.union(loc);
099//    System.out.println("GAMEAnnotationHandler after union is  " + range);
100  }
101
102  public void reportStrand(StrandedFeature.Strand strand)
103  {
104    // obtains strand from elements that are in the know.
105    ((StrandedFeature.Template) featureTemplate).strand = strand;
106  }
107
108  public void startElementHandler(
109                String nsURI,
110                String localName,
111                String qName,
112                Attributes attrs)
113  {
114    String annotationId =  attrs.getValue("id");
115    if (annotationId != null) {
116      // stuff Gadfly annotation id into our annotation bundle for info.
117//      System.out.println("GAMEAnnotationHandler is setting id to " + annotationId);
118      try {
119         featureTemplate.annotation.setProperty(
120                          "annotation_id", annotationId);
121      }
122      catch (ChangeVetoException cae) {
123        System.err.println("GAMEAnnotationHandler: veto exception caught.");
124      }
125    }
126//    System.out.println("GAMEAnnotationHandler.startElementHandler: leaving.");
127  }
128
129  public void endElementHandler(
130                String nsURI,
131                String localName,
132                String qName,
133                StAXContentHandler handler)
134  {
135    // finalise the sequence extent to encompass all reported features
136    if (range != Location.empty)
137      featureTemplate.location = new RangeLocation(
138                                       range.getMin(), 
139                                       range.getMax());
140  }
141
142}
143