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.Iterator;
024import java.util.List;
025
026import org.biojava.bio.seq.Sequence;
027import org.xml.sax.Attributes;
028import org.xml.sax.SAXException;
029
030/**
031 * Handles the root AGAVE element
032 * modified for agave format
033 * 
034 * @author Hanning Ni    Doubletwist Inc
035 */
036public class AGAVEHandler extends StAXFeatureHandler implements AGAVECallbackItf{
037
038  // there is only one AGAVE element encompassing the entire file
039  // AGAVE files.
040
041  //store the sequences from each direct-subtag of sciobj
042  private List sequenceSet ;
043
044  public AGAVEHandler() {
045     super() ;
046     sequenceSet = new ArrayList(1) ;
047     setHandlerCharacteristics("sciobj", true);
048
049    // setup handlers
050       // <bio_sequence>
051       super.addHandler(new ElementRecognizer.ByLocalName("bio_sequence"),
052         AGAVEBioSeqHandler.AGAVE_BIO_SEQ_HANDLER_FACTORY);
053       // <contig>
054       super.addHandler(new ElementRecognizer.ByLocalName("contig"),
055         AGAVEContigHandler.AGAVE_CONTIG_HANDLER_FACTORY);
056       // <computation> not used and handled yet
057        super.addHandler(new ElementRecognizer.ByLocalName("computation"),
058        AGAVEComputationHandler.AGAVE_COMPUTATION_HANDLER_FACTORY);
059       //chromosome
060       super.addHandler(new ElementRecognizer.ByLocalName("chromosome"),
061         AGAVEChromosomeHandler.AGAVE_CHROMOSOME_HANDLER_FACTORY);
062  }
063
064  /**
065   * @param sequence from sub-tag &lt;bio_sequence&gt;/&lt;contig&gt;/&lt;chromosome&gt;
066   * <pre>
067   * bio_sequence --> SimpleSequence
068   * contig   --> SimpleAssembly
069   *          --> SimpleSequence( if only one sequence )
070   * chromosome -> SimpleAssembly
071   *            -> SimpleSequence( if only one sequence)
072   * </pre>
073   */
074  public void reportSequence(Sequence sequence)
075  {
076      sequenceSet.add( sequence ) ;
077  }
078
079  /**
080   * get all the top level sequences
081   * bio_sequence --> SimpleSequence
082   * contig   --> SimpleAssembly
083   *          --> SimpleSequence( if only one sequence )
084   * chromosome -> SimpleAssembly
085   *            -> SimpleSequence( if only one sequence)
086   */
087  public Iterator getSequences()
088  {
089      return sequenceSet.iterator() ;
090  }
091
092  public void startElementHandler(
093                String nsURI,
094                String localName,
095                String qName,
096                Attributes attrs,
097                DelegationManager dm)
098         throws SAXException
099  {
100    // check the element type
101    if (!(localName.equals( "sciobj")) )
102      throw new SAXException("root element of file is not a sciobj element");
103
104    // check file version
105    String version = attrs.getValue("version");
106    if (! version.startsWith("2")  )
107      throw new SAXException("AGAVE version is not 2.*,  we only support 2.* ");
108 }
109
110
111}
112