001/*
002 * DummyRichSequenceHandler.java
003 *
004 * Created on March 7, 2006, 3:12 PM
005 */
006
007package org.biojavax.bio.seq;
008
009import java.util.Iterator;
010import java.util.List;
011
012import org.biojava.bio.seq.io.ChunkedSymbolListFactory;
013import org.biojava.bio.symbol.Edit;
014import org.biojava.bio.symbol.IllegalAlphabetException;
015import org.biojava.bio.symbol.PackedSymbolListFactory;
016import org.biojava.bio.symbol.Symbol;
017import org.biojava.bio.symbol.SymbolList;
018import org.biojava.utils.ChangeVetoException;
019import org.biojavax.bio.seq.RichLocation.Tools;
020
021/**
022 *
023 * @author Richard Holland
024 * @since 1.5
025 */
026public class DummyRichSequenceHandler implements RichSequenceHandler {
027    /**
028     * {@inheritDoc}
029     */
030    public void edit(RichSequence seq, Edit edit) throws IndexOutOfBoundsException, IllegalAlphabetException, ChangeVetoException {
031        seq.getInternalSymbolList().edit(edit);
032    }
033    
034    /**
035     * {@inheritDoc}
036     */
037    public Symbol symbolAt(RichSequence seq, int index) throws IndexOutOfBoundsException {
038        if (seq.getCircular()) index = RichLocation.Tools.modulateCircularIndex(index,seq.length());
039        return seq.getInternalSymbolList().symbolAt(index);
040    }
041    
042    /**
043     * {@inheritDoc}
044     */
045    public List<Symbol> toList(RichSequence seq) { return seq.getInternalSymbolList().toList();}
046    
047    /**
048     * {@inheritDoc}
049     */
050    public String subStr(RichSequence seq, int start, int end) throws IndexOutOfBoundsException {
051        return seq.getInternalSymbolList().subList(start, end).seqString();
052    }
053    
054    /**
055     * {@inheritDoc}
056     */
057    public SymbolList subList(RichSequence seq, int start, int end) throws IndexOutOfBoundsException {
058        if (seq.getCircular()) {
059            try {
060                int[] modLocation = Tools.modulateCircularLocation(start,end,seq.length());
061                int modStart = modLocation[0];
062                int modEnd = modLocation[1];
063                int modLength = (modEnd - modStart)+1;
064                int seqLength = seq.length();
065                if (modStart==0) modStart = seqLength;
066                if (modEnd==0) modEnd = seqLength;
067                // Use the packed symbol factory
068                ChunkedSymbolListFactory symsf = new ChunkedSymbolListFactory(new PackedSymbolListFactory());
069                if (modEnd>seqLength) {
070                    // add it in chunks
071                    int remaining = modLength;
072                    int chunkSize = (seqLength-modStart)+1;
073                    //   add modStart -> seqLength
074                    symsf.addSymbols(
075                            seq.getAlphabet(),
076                            (Symbol[])seq.getInternalSymbolList().subList(modStart,seqLength).toList().toArray(new Symbol[0]),
077                            0,
078                            chunkSize);
079                    remaining -= chunkSize;
080                    //   repeat add seqLength
081                    while (remaining > seqLength) {
082                        chunkSize = seqLength;
083                        symsf.addSymbols(
084                                seq.getAlphabet(),
085                                (Symbol[])seq.getInternalSymbolList().subList(1,seqLength).toList().toArray(new Symbol[0]),
086                                0,
087                                chunkSize);
088                        remaining -= chunkSize;
089                    }
090                    //   add 0 -> remaining
091                    chunkSize = remaining;
092                    symsf.addSymbols(
093                            seq.getAlphabet(),
094                            (Symbol[])seq.getInternalSymbolList().subList(1,chunkSize).toList().toArray(new Symbol[0]),
095                            0,
096                            chunkSize);
097                } else {
098                    //   add modStart->modEnd
099                    symsf.addSymbols(
100                            seq.getAlphabet(),
101                            (Symbol[])seq.getInternalSymbolList().subList(modStart,modEnd).toList().toArray(new Symbol[0]),
102                            0,
103                            modLength);
104                }
105                return symsf.makeSymbolList();
106            } catch (IllegalAlphabetException e) {
107                throw new RuntimeException("Don't understand our own alphabet?",e);
108            }
109        } else return seq.getInternalSymbolList().subList(start, end);
110    }
111    
112    /**
113     * {@inheritDoc}
114     */
115    public String seqString(RichSequence seq) { return seq.getInternalSymbolList().seqString(); }
116        
117    /**
118     * {@inheritDoc}
119     */
120    public Iterator<Symbol> iterator(RichSequence seq) {return seq.getInternalSymbolList().iterator(); }
121}