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 */
022package org.biojava.nbio.core.sequence.views;
023
024import org.biojava.nbio.core.sequence.template.Compound;
025import org.biojava.nbio.core.sequence.template.Sequence;
026import org.biojava.nbio.core.sequence.template.SequenceMixin;
027import org.biojava.nbio.core.sequence.template.SequenceProxyView;
028
029/**
030 * For a given sequence this class will return the base at the reversed
031 * position i.e. in a sequence of size 10, if you request base 2 you will get
032 * back the base at position 9. Sub-views can be made of this class which
033 * also respect the reversed calls.
034 *
035 * @author Andy Yates
036 * @param <C> Must be a subtype of @{link Compound}
037 */
038public class ReversedSequenceView<C extends Compound> extends SequenceProxyView<C> {
039
040        private final int sequenceSize;
041
042        public ReversedSequenceView(Sequence<C> sequence) {
043                super(sequence);
044                this.sequenceSize = sequence.getLength();
045        }
046
047        @Override
048        public String getSequenceAsString() {
049                return SequenceMixin.toString(this);
050        }
051
052        protected int toIndex(int index) {
053                return (sequenceSize - index) + 1;
054        }
055
056        @Override
057        public C getCompoundAt(int position) {
058                return super.getCompoundAt(toIndex(position));
059        }
060}