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     * Created on 01-21-2010
021     *
022     * @author Richard Holland
023     *
024     *
025     */
026    package org.biojava3.core.sequence.template;
027    
028    import java.util.Iterator;
029    import java.util.List;
030    import org.biojava3.core.sequence.AccessionID;
031    
032    public class SequenceProxyView<C extends Compound> implements SequenceView<C> {
033    
034        private Integer bioStart;
035        private Integer bioEnd;
036        private Sequence<C> sequence;
037    
038        public SequenceProxyView() {
039        }
040    
041        public SequenceProxyView(Sequence<C> sequence) {
042            this(sequence, 1, sequence.getLength());
043        }
044    
045        /**
046         * Main constructor for working with SequenceProxyViews
047         *
048         * @param sequence Sequence to proxy
049         * @param bioStart Start; cannot be less than 1
050         * @param bioEnd End; cannot be greater than the sequence length
051         */
052        public SequenceProxyView(Sequence<C> sequence, Integer bioStart, Integer bioEnd) {
053            this.sequence = sequence;
054            setBioStart(bioStart);
055            setBioEnd(bioEnd);
056        }
057    
058        public Sequence<C> getViewedSequence() {
059            return sequence;
060        }
061    
062        public String getSequenceAsString() {
063            return SequenceMixin.toString(this);
064        }
065    
066        public List<C> getAsList() {
067            return SequenceMixin.toList(this);
068        }
069    
070        public C getCompoundAt(int position) {
071            return getViewedSequence().getCompoundAt((getBioStart() + position) - 1);
072        }
073    
074        public int getIndexOf(C compound) {
075            return SequenceMixin.indexOf(this, compound);
076        }
077    
078        public int getLastIndexOf(C compound) {
079            return SequenceMixin.lastIndexOf(this, compound);
080        }
081    
082        public int getLength() {
083            return (getBioEnd() - getBioStart()) + 1;
084        }
085    
086        public CompoundSet<C> getCompoundSet() {
087            return getViewedSequence().getCompoundSet();
088        }
089    
090        public SequenceView<C> getSubSequence(final Integer bioStart, final Integer bioEnd) {
091            return new SequenceProxyView<C>(this, bioStart, bioEnd);
092        }
093    
094        public Iterator<C> iterator() {
095            return new SequenceMixin.SequenceIterator<C>(this);
096        }
097    
098        public AccessionID getAccession() {
099            return getViewedSequence().getAccession();
100        }
101    
102        /**
103         * @return the bioStart
104         */
105        public Integer getBioStart() {
106            return bioStart;
107        }
108    
109        /**
110         * @param bioStart the bioStart to set
111         */
112        public void setBioStart(Integer bioStart) {
113            if (bioStart < 1) {
114                throw new IllegalArgumentException("The given start "
115                        + bioStart + " is less than 1; cannot index less than 1");
116            }
117            this.bioStart = bioStart;
118        }
119    
120        /**
121         * @return the bioEnd
122         */
123        public Integer getBioEnd() {
124            return bioEnd;
125        }
126    
127        /**
128         * @param bioEnd the bioEnd to set
129         */
130        public void setBioEnd(Integer bioEnd) {
131            if(sequence == null) {
132                throw new NullPointerException("No sequence given before setting the end coordinate; cannot be done");
133            }
134            if (bioEnd > sequence.getLength()) {
135                throw new IllegalArgumentException("The given end "
136                        + bioEnd + " is greater than sequence length"
137                        + sequence.getLength());
138            }
139            this.bioEnd = bioEnd;
140        }
141    
142        public int countCompounds(C... compounds) {
143            return SequenceMixin.countCompounds(this, compounds);
144        }
145    
146        @Override
147        public SequenceView<C> getInverse() {
148            return SequenceMixin.inverse(this);
149        }
150    }