001    package org.biojava3.core.sequence.storage;
002    
003    import java.util.Iterator;
004    import java.util.List;
005    import org.biojava3.core.sequence.AccessionID;
006    import org.biojava3.core.sequence.template.Compound;
007    import org.biojava3.core.sequence.template.CompoundSet;
008    import org.biojava3.core.sequence.template.ProxySequenceReader;
009    import org.biojava3.core.sequence.template.SequenceMixin;
010    import org.biojava3.core.sequence.template.SequenceProxyView;
011    import org.biojava3.core.sequence.template.SequenceView;
012    
013    /**
014     * An implementation of the SequenceReader interface which for every
015     * call will return only 1 compound (given to it during construction; a String
016     * is also valid but will require a CompoundSet). The idea is to represent
017     * large runs of a single compound without the memory footprint of storing these
018     * compounds e.g. a run of 10KB of Ns in a DNASequence.
019     *
020     * @author ayates
021     */
022    public class SingleCompoundSequenceReader<C extends Compound> implements ProxySequenceReader<C> {
023    
024        private final C compound;
025        private final CompoundSet<C> compoundSet;
026        private final int length;
027    
028        /**
029         * Public constructor to be used with String based constructor
030         */
031        public SingleCompoundSequenceReader(String compound, CompoundSet<C> compoundSet, int length) {
032            this(compoundSet.getCompoundForString(compound), compoundSet, length);
033        }
034    
035        /**
036         * Build the object with a compound rather than a String
037         */
038        public SingleCompoundSequenceReader(C compound, CompoundSet<C> compoundSet, int length) {
039            this.compound = compound;
040            this.compoundSet = compoundSet;
041            this.length = length;
042        }
043    
044        /**
045         * Unsupported
046         */
047        
048        public void setCompoundSet(CompoundSet<C> compoundSet) {
049            throw new UnsupportedOperationException("Not supported.");
050        }
051    
052        /**
053         * Unsupported
054         */
055        
056        public void setContents(String sequence) {
057            throw new UnsupportedOperationException("Not supported.");
058        }
059    
060        /**
061         * Returns the length given during construction
062         */
063        
064        public int getLength() {
065            return length;
066        }
067    
068        /**
069         * Always returns the compound given at construction
070         */
071        
072        public C getCompoundAt(int position) {
073            return compound;
074        }
075    
076        /**
077         * Returns 1 if the given compound is equal to the one given during
078         * construction; otherwise will return -1.
079         */
080        
081        public int getIndexOf(C compound) {
082            if(compound.equals(this.compound)) {
083                return 1;
084            }
085            return -1;
086        }
087    
088        /**
089         * Returns the length of the Sequence if the given compound was equal to
090         * the one given during construction. Otherwise returns -1
091         */
092        
093        public int getLastIndexOf(C compound) {
094            if(compound.equals(this.compound)) {
095                return getLength();
096            }
097            return -1;
098        }
099    
100        /**
101         * Delegates to {@link SequenceMixin#toList(org.biojava3.core.sequence.template.Sequence) }
102         */
103        
104        public String getSequenceAsString() {
105            return SequenceMixin.toString(this);
106        }
107    
108        /**
109         * Delegates to {@link SequenceMixin#toList(org.biojava3.core.sequence.template.Sequence) }
110         */
111        
112        public List<C> getAsList() {
113            return SequenceMixin.toList(this);
114        }
115    
116        /**
117         * Creates a {@link SequenceProxyView} for the given coordinates
118         */
119        
120        public SequenceView<C> getSubSequence(Integer start, Integer end) {
121            return new SequenceProxyView<C>(this, start, end);
122        }
123    
124        /**
125         * Returns the compound set given at construction
126         */
127        
128        public CompoundSet<C> getCompoundSet() {
129            return compoundSet;
130        }
131    
132        /**
133         * Unsupoorted
134         */
135        
136        public AccessionID getAccession() {
137            throw new UnsupportedOperationException("Not supported yet.");
138        }
139    
140        /**
141         * Delegates to {@link SequenceMixin#countCompounds(org.biojava3.core.sequence.template.Sequence, C[]) }
142         */
143        
144        public int countCompounds(C... compounds) {
145            return SequenceMixin.countCompounds(this, compounds);
146        }
147    
148        /**
149         * Returns an instance of {@link SequenceMixin.SequenceIterator}
150         */
151        
152        public Iterator<C> iterator() {
153            return new SequenceMixin.SequenceIterator<C>(this);
154        }
155    
156        @Override
157        public SequenceView<C> getInverse() {
158            return SequenceMixin.inverse(this);
159        }
160    }