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 *
033 * @author Hanning Ni     Doubletwist Inc
034 */
035public class AGAVECompResultHandler
036               extends StAXFeatureHandler implements AGAVEFeatureCallbackItf
037
038{
039  public static final StAXHandlerFactory AGAVE_COMP_RESULT_HANDLER_FACTORY
040    = new StAXHandlerFactory() {
041    public StAXContentHandler getHandler(StAXFeatureHandler staxenv) {
042      return new AGAVECompResultHandler(staxenv);
043    }
044  };
045
046
047  AGAVECompResultHandler(StAXFeatureHandler staxenv) {
048    // setup up environment stuff
049    super( staxenv );
050    featureListener = staxenv.featureListener;
051    setHandlerCharacteristics("comp_result", true);
052
053    // setup handlers
054        //
055       super.addHandler(new ElementRecognizer.ByLocalName("note"),
056         AGAVENotePropHandler.AGAVE_NOTE_PROP_HANDLER_FACTORY);
057       //
058       super.addHandler(new ElementRecognizer.ByLocalName("match_desc"),
059         AGAVEMatchDescPropHandler.AGAVE_MATCH_DESC_PROP_HANDLER_FACTORY);
060       //
061       super.addHandler(new ElementRecognizer.ByLocalName("match_align"),
062         AGAVEMatchAlignPropHandler.AGAVE_MATCH_ALIGN_PROP_HANDLER_FACTORY);
063       //
064       super.addHandler(new ElementRecognizer.ByLocalName("query_region"),
065         AGAVEQueryRegionPropHandler.AGAVE_QUERY_REGION_PROP_HANDLER_FACTORY);
066       //
067       super.addHandler(new ElementRecognizer.ByLocalName("match_region"),
068         AGAVEMatchRegionPropHandler.AGAVE_MATCH_REGION_PROP_HANDLER_FACTORY);
069       //
070       super.addHandler(new ElementRecognizer.ByLocalName("result_property"),
071         AGAVEResultPropertyPropHandler.AGAVE_RESULT_PROPERTY_PROP_HANDLER_FACTORY);
072       //
073       super.addHandler(new ElementRecognizer.ByLocalName("result_group"),
074         AGAVEResultGroupHandler.AGAVE_RESULT_GROUP_HANDLER_FACTORY);
075       //
076       super.addHandler(new ElementRecognizer.ByLocalName("related_annot"),
077         AGAVERelatedAnnotPropHandler.AGAVE_RELATED_ANNOT_PROP_HANDLER_FACTORY);
078
079  }
080
081
082
083  public void startElementHandler(
084                String nsURI,
085                String localName,
086                String qName,
087                Attributes attrs)
088                throws SAXException
089  {
090     try{
091      featureListener.startFeature( featureTemplate );
092      boolean forFeature = true ;
093      setProperty( "element_id",  attrs.getValue("element_id") , forFeature) ;
094      setProperty( "result_id",  attrs.getValue("result_id") , forFeature) ;
095      setProperty( "group_order",  attrs.getValue("group_order") , forFeature) ;
096      setProperty("result_type",  attrs.getValue("result_type"), forFeature ) ;
097      setProperty( "feature_type",  attrs.getValue("feature_type"), forFeature ) ;
098      setProperty( "on_complement_strand",  attrs.getValue("on_complement_strand") , forFeature) ;
099      setProperty( "confidence",  attrs.getValue("confidence"), forFeature ) ;
100      setProperty( "align_length",  attrs.getValue("align_length") , forFeature) ;
101      setProperty( "align_unit",  attrs.getValue("align_unit") , forFeature) ;
102      String strand = attrs.getValue("on_complement_strand") ;
103      if( strand.equalsIgnoreCase("true") )
104        ((StrandedFeature.Template) featureTemplate).strand =  StrandedFeature.NEGATIVE ;
105      else if( strand.equalsIgnoreCase("false") )
106         ((StrandedFeature.Template) featureTemplate).strand =  StrandedFeature.POSITIVE ;
107      else
108        ((StrandedFeature.Template) featureTemplate).strand =  StrandedFeature.UNKNOWN ;
109      featureTemplate.type = attrs.getValue("result_type") ;
110    }catch(Exception e){
111        throw new SAXException( e.getMessage() ) ;
112    }
113  }
114
115
116
117  public void reportFeature(Location loc)
118  {
119    ((StrandedFeature.Template) featureTemplate).location = loc;
120  }
121  public void reportStrand(StrandedFeature.Strand strand)
122  {
123    // obtains strand from elements that are in the know.
124    ((StrandedFeature.Template) featureTemplate).strand = strand;
125  }
126  public void addProperty(AGAVEProperty prop)
127  {
128      try{
129         Object ob = UtilHelper.getProperty(staxenv.featureTemplate.annotation,"result_property");
130         if( ob != null )
131             ((List)ob).add( prop ) ;
132         else
133         {
134             List props = new ArrayList(1) ;
135             props.add( prop ) ;
136             staxenv.featureTemplate.annotation.setProperty("result_property", props)  ;
137         }
138      }catch (ChangeVetoException cve) {
139         cve.printStackTrace() ;
140      }
141   }
142
143  public void addRelatedAnnot(AGAVERelatedAnnot prop)
144  {
145      try{
146         Object ob = UtilHelper.getProperty(staxenv.featureTemplate.annotation, "related_annot");
147         if( ob != null )
148             ((List)ob).add( prop ) ;
149         else
150         {
151             List props = new ArrayList(1) ;
152             props.add( prop ) ;
153             staxenv.featureTemplate.annotation.setProperty("related_annot", props)  ;
154         }
155      }catch (ChangeVetoException cve) {
156        cve.printStackTrace() ;
157      }
158   }
159
160}
161