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.transcription;
023
024import org.biojava.nbio.core.sequence.compound.NucleotideCompound;
025import org.biojava.nbio.core.sequence.template.Sequence;
026import org.biojava.nbio.core.sequence.views.ComplementSequenceView;
027import org.biojava.nbio.core.sequence.views.ReversedSequenceView;
028
029/**
030 * Indicates a way of translating a sequence.
031 *
032 * @author ayates
033 */
034public enum Frame {
035        ONE(1, false),
036        TWO(2, false),
037        THREE(3, false),
038        REVERSED_ONE(1, true),
039        REVERSED_TWO(2, true),
040        REVERSED_THREE(3, true);
041
042        private final boolean reverse;
043        private final int start;
044
045        private Frame(int start, boolean reverse) {
046                this.start = start;
047                this.reverse = reverse;
048        }
049
050        public static Frame getDefaultFrame() {
051                return ONE;
052        }
053
054        /**
055         * Returns all frames in the forward orientation
056         */
057        public static Frame[] getForwardFrames() {
058                return new Frame[]{ONE,TWO,THREE};
059        }
060
061        /**
062         * Returns all frames which are in the reverse orientation
063         */
064        public static Frame[] getReverseFrames() {
065                return new Frame[]{REVERSED_ONE,REVERSED_TWO,REVERSED_THREE};
066        }
067
068        /**
069         * Delegates to {@link Frame#values()}
070         */
071        public static Frame[] getAllFrames() {
072                return Frame.values();
073        }
074
075        /**
076         * Optionally wraps a Sequence in a reverse complementing view (if the
077         * frame is on the reverse strand) and creates a sub sequence view if
078         * it is required.
079         *
080         * If you pass in {@link #ONE} in you will get the same {@link Sequence}
081         * returned.
082         */
083        public <C extends NucleotideCompound> Sequence<C> wrap(Sequence<C> incoming) {
084                Sequence<C> reversed;
085                if(reverse) {
086                        reversed = new ComplementSequenceView<C>(new ReversedSequenceView<C>(incoming));
087                }
088                else {
089                        reversed = incoming;
090                }
091
092                Sequence<C> finalSeq;
093
094                if(start == 1) {
095                        finalSeq = reversed;
096                }
097                else {
098                        finalSeq = reversed.getSubSequence(start, reversed.getLength());
099                }
100
101                return finalSeq;
102        }
103
104}