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; 024import java.util.ListIterator; 025 026import org.biojava.bio.Annotation; 027import org.biojava.bio.seq.ComponentFeature; 028import org.biojava.bio.seq.DNATools; 029import org.biojava.bio.seq.Sequence; 030import org.biojava.bio.seq.SimpleAssembly; 031import org.biojava.bio.seq.StrandedFeature; 032import org.biojava.bio.seq.impl.SimpleSequence; 033import org.biojava.bio.symbol.RangeLocation; 034import org.biojava.bio.symbol.SymbolList; 035import org.xml.sax.Attributes; 036import org.xml.sax.SAXException; 037 038/** 039 * 040 * Handles the AGAVE <contig> element 041 * @author Hanning Ni Doubletwist Inc 042 * @author Greg Cox 043 */ 044public class AGAVEContigHandler 045 extends StAXFeatureHandler implements AGAVEContigCallbackItf, SequenceHandler 046{ 047 public static final StAXHandlerFactory AGAVE_CONTIG_HANDLER_FACTORY 048 = new StAXHandlerFactory() { 049 public StAXContentHandler getHandler(StAXFeatureHandler staxenv) { 050 return new AGAVEContigHandler(staxenv); 051 } 052 }; 053 054 protected Sequence sequence ; 055 private List sequenceSet ; 056 private SymbolList dna ; 057 058 AGAVEContigHandler(StAXFeatureHandler staxenv) { 059 // setup up environment stuff 060 super( staxenv ); 061 featureListener = staxenv.featureListener; 062 setHandlerCharacteristics("contig", true); 063 sequenceSet = new ArrayList(1) ; 064 065 // setup handlers 066 // <db_id> 067 super.addHandler(new ElementRecognizer.ByLocalName("db_id"), 068 AGAVEDbIdPropHandler.AGAVE_DBID_PROP_HANDLER_FACTORY); 069 //view 070 super.addHandler(new ElementRecognizer.ByLocalName("view"), 071 AGAVEViewPropHandler.AGAVE_VIEW_PROP_HANDLER_FACTORY); 072 // <note> 073 super.addHandler(new ElementRecognizer.ByLocalName("note"), 074 AGAVENotePropHandler.AGAVE_NOTE_PROP_HANDLER_FACTORY); 075 // 076 super.addHandler(new ElementRecognizer.ByLocalName("fragment_order"), 077 AGAVEFragmentOrderHandler.AGAVE_FRAGMENT_ORDER_HANDLER_FACTORY); 078 // 079 super.addHandler(new ElementRecognizer.ByLocalName("unordered_fragments"), 080 AGAVEUnorderedFragmentsHandler.AGAVE_UNORDERED_FRAGMENTS_HANDLER_FACTORY); 081 // 082 super.addHandler(new ElementRecognizer.ByLocalName("assembly"), 083 AGAVEAssemblyHandler.AGAVE_ASSEMBLY_HANDLER_FACTORY); 084 // 085 super.addHandler(new ElementRecognizer.ByLocalName("sequence"), 086 AGAVESeqPropHandler.AGAVE_SEQ_PROP_HANDLER_FACTORY); 087 //<sequence_map> 088 super.addHandler(new ElementRecognizer.ByLocalName("sequence_map"), 089 AGAVESeqMapHandler.AGAVE_SEQ_MAP_HANDLER_FACTORY); 090 //<map_location> 091 super.addHandler(new ElementRecognizer.ByLocalName("map_location"), 092 AGAVEMapLocationPropHandler.AGAVE_MAP_LOCATION_PROP_HANDLER_FACTORY); 093 } 094 public void reportSequence(Sequence sequence) 095 { 096 sequenceSet.add( sequence ) ; 097 } 098 099 public void reportDna(String dna) 100 { 101 try{ 102 StringBuffer sb = new StringBuffer() ; 103 for( int i = 0 ; i < dna.length(); i++) 104 { 105 char c = dna.charAt(i) ; 106 if( c != ' ' && c != '\n' && c!= '\t') 107 sb.append( c ); 108 } 109 this.dna = DNATools.createDNA( sb.substring(0) ); 110 }catch(Exception e){ 111 e.printStackTrace( ); 112 } 113 } 114 115 public void startElementHandler( 116 String nsURI, 117 String localName, 118 String qName, 119 Attributes attrs) 120 throws SAXException 121 { 122 try{ 123 featureListener.startSequence(); 124 boolean forFeature = true ; 125 setProperty( "length", attrs.getValue("length") , forFeature) ; 126 }catch(Exception e){ 127 throw new SAXException( e.getMessage() ) ; 128 } 129 } 130 131 /* 132 protected Feature.Template createTemplate() { 133 // create Gene Template for this 134 StrandedFeature.Template st = new StrandedFeature.Template(); 135 136 // assume feature set to describe a transcript 137 st.type = "contig"; 138 st.strand = StrandedFeature.UNKNOWN; 139 // set up annotation bundle 140 st.annotation = annot; //new SmallAnnotation(); 141 st.location = new Location.EmptyLocation(); 142 if( staxenv != null ) 143 staxenv. subFeatures .add( this ) ; 144 145 return st; 146 }*/ 147 148 public void endElementHandler( 149 String nsURI, 150 String localName, 151 String qName, 152 StAXContentHandler handler) 153 throws SAXException 154 { 155 try{ 156 if( sequenceSet.size() == 0 ) 157 { 158 if( dna == null ) 159 throw new SAXException("dna sequence must offered ") ; 160 sequence = new SimpleSequence( dna, " ", "simple_sequence " , annot ) ; 161 //Feature feature = sequence.createFeature( featureTemplate ) ; 162 //realizeSubFeatures( feature ) ; 163 addFeatureToSequence(sequence) ; 164 } 165 else 166 { 167 int contig_len =new Integer( (String) UtilHelper.getProperty(featureTemplate.annotation, "length" )).intValue() ; 168 sequence = new SimpleAssembly(contig_len , "contig", "contig") ; 169 // sequence.createFeature( featureTemplate ) ; 170 ComponentFeature.Template cft = new ComponentFeature.Template(); 171 int global_start = 1 ; 172 for (ListIterator i = sequenceSet.listIterator(); i.hasNext();) 173 { 174 Sequence seq = ( Sequence ) i.next() ; 175 // ComponentFeature.Template cft = new ComponentFeature.Template(); 176 cft.type = "fragment"; 177 cft.source = "contig"; 178 cft.annotation = Annotation.EMPTY_ANNOTATION; 179 cft.strand = StrandedFeature.POSITIVE; 180 cft.location = new RangeLocation(global_start, global_start + seq.length() - 1 ); 181 cft.componentSequence = seq; 182 cft.componentLocation = new RangeLocation(1, seq.length()); 183 sequence.createFeature(cft); 184 global_start += seq.length() ; 185 } 186 } 187 //add sequence to chromsome 188 appendToTop(sequence, staxenv) ; 189 featureListener.endSequence() ; 190 }catch(Exception e){ 191 throw new SAXException( e.getMessage() ) ; 192 } 193 } 194 private void appendToTop(Sequence sequence, StAXFeatureHandler staxenv) 195 { 196 if (staxenv instanceof AGAVEChromosomeCallbackItf) 197 { 198 ((AGAVEChromosomeCallbackItf) staxenv).reportSequence( sequence ); 199 return; 200 } 201 if (staxenv instanceof AGAVECallbackItf) 202 { 203 ((AGAVECallbackItf) staxenv).reportSequence( sequence ); 204 return; 205 } 206 appendToTop(sequence, staxenv.staxenv ); 207 } 208 209} 210