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 June 9, 2010
021 * Author: Mark Chapman
022 */
023
024package org.biojava.nbio.alignment;
025
026import java.io.Serializable;
027
028import org.biojava.nbio.alignment.template.GapPenalty;
029
030/**
031 * Implements a data structure for the gap penalties used during a sequence alignment routine.
032 *
033 * @author Mark Chapman
034 */
035public class SimpleGapPenalty implements GapPenalty, Serializable {
036
037        private static final long serialVersionUID = 3945671344135815456L;
038
039        private static int dgop = 10, dgep = 1;
040
041        /**
042         * Sets the default gap extension penalty.
043         *
044         * @param gep the default gap extension penalty
045         */
046        public static void setDefaultExtensionPenalty(int gep) {
047                dgep = gep;
048        }
049
050        /**
051         * Sets the default gap open penalty.
052         *
053         * @param gop the default gap open penalty
054         */
055        public static void setDefaultOpenPenalty(int gop) {
056                dgop = gop;
057        }
058
059        private GapPenalty.Type type;
060        private int gop, gep;
061
062        /**
063         * Creates a new set of gap penalties using the defaults.
064         */
065        public SimpleGapPenalty() {
066                this(dgop, dgep);
067        }
068
069        /**
070         * Creates a new set of gap penalties.
071         *
072         * @param gop the gap open penalty; should be nonnegative
073         * @param gep the gap extension penalty; should be nonnegative
074         */
075        public SimpleGapPenalty(int gop, int gep) {
076                this.gop = -Math.abs(gop);
077                this.gep = -Math.abs(gep);
078                setType();
079        }
080
081        /**
082         * <strong>Returns the negative of the extension penalty passed to the constructor.</strong>
083         */
084        @Override
085        public int getExtensionPenalty() {
086                return gep;
087        }
088
089        /**
090         * <strong>Returns the negative of the opening penalty passed to the constructor.</strong>
091         */
092        @Override
093        public int getOpenPenalty() {
094                return gop;
095        }
096
097        @Override
098        public Type getType() {
099                return type;
100        }
101
102        /**
103         * @param gep Should be nonnegative
104         */
105        @Override
106        public void setExtensionPenalty(int gep) {
107                this.gep = -Math.abs(gep);
108                setType();
109        }
110
111        /**
112         * @param gop Should be nonnegative
113         */
114        @Override
115        public void setOpenPenalty(int gop) {
116                this.gop = -Math.abs(gop);
117                setType();
118        }
119
120        // helper method to set the type given the open and extension penalties
121        private void setType() {
122                type = (gop == 0) ? GapPenalty.Type.LINEAR : ((gep == 0) ? GapPenalty.Type.CONSTANT : GapPenalty.Type.AFFINE);
123        }
124
125}