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; 025import java.util.ListIterator; 026 027import org.biojava.bio.seq.Sequence; 028import org.xml.sax.Attributes; 029import org.xml.sax.SAXException; 030 031/** 032 * 033 * Handles the AGAVE <chromosome> element 034 * 035 * @author Hanning Ni Doubletwist Inc 036 */ 037public class AGAVEChromosomeHandler 038 extends StAXFeatureHandler implements AGAVEChromosomeCallbackItf, SequenceHandler 039{ 040 public static final StAXHandlerFactory AGAVE_CHROMOSOME_HANDLER_FACTORY 041 = new StAXHandlerFactory() { 042 public StAXContentHandler getHandler(StAXFeatureHandler staxenv) { 043 return new AGAVEChromosomeHandler(); 044 } 045 }; 046 047 private List sequenceSet ; 048 private String chromNum ; 049 private String chromLen ; 050 051 AGAVEChromosomeHandler() { 052 super(); 053 setHandlerCharacteristics("chromosome", true); 054 sequenceSet = new ArrayList(1) ; 055 056 // setup handlers 057 // ignore it , view 058 super.addHandler(new ElementRecognizer.ByLocalName("view"), 059 AGAVEViewPropHandler.AGAVE_VIEW_PROP_HANDLER_FACTORY); 060 // <note> 061 super.addHandler(new ElementRecognizer.ByLocalName("contig"), 062 AGAVEContigHandler.AGAVE_CONTIG_HANDLER_FACTORY); 063 } 064 065 public void reportSequence(Sequence sequence) 066 { 067 sequenceSet.add( sequence ) ; 068 } 069 public Iterator getSequences() 070 { 071 return sequenceSet.iterator() ; 072 } 073 public void startElementHandler( 074 String nsURI, 075 String localName, 076 String qName, 077 Attributes attrs) 078 throws SAXException 079 { 080 chromNum = attrs.getValue("number") ; 081 chromLen = attrs.getValue("length") ; 082 } 083 084 public void endElementHandler( 085 String nsURI, 086 String localName, 087 String qName, 088 StAXContentHandler handler) 089 throws SAXException 090 { 091 try{ 092 for (ListIterator i = sequenceSet.listIterator(); i.hasNext();) 093 { 094 Sequence seq = ( Sequence ) i.next() ; 095 if( chromNum != null ) 096 seq. getAnnotation().setProperty( "chromosome_number", chromNum); 097 if( chromLen != null ) 098 seq. getAnnotation().setProperty( "chromosome_length", chromLen); 099 100 appendToTop(seq, staxenv) ; 101 } 102 }catch(Exception e){ 103 throw new SAXException( e.getMessage() ) ; 104 } 105 106 } 107 108 private void appendToTop(Sequence sequence, StAXFeatureHandler staxenv) 109 { 110 if( staxenv instanceof AGAVECallbackItf) 111 { 112 ((AGAVECallbackItf) staxenv).reportSequence( sequence ); 113 return; 114 } 115 else appendToTop(sequence, staxenv.staxenv ); 116 } 117 118} 119