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 */
021package org.biojava.bio.symbol;
022
023import org.biojava.bio.seq.DNATools;
024
025/**
026 * A <code>Packing</code> implementation which handles the DNA alphabet, without any
027 * support for ambiguity symbols.  In normal use, the only returns values between
028 * 0 and 3, and so requires only two bits of storage per symbol.
029 *
030 * @since 1.3
031 * @author Matthew Pocock
032 * @author Thomas Down
033 */  
034public class DNANoAmbPack
035  implements
036    Packing,
037    java.io.Serializable
038{
039  final byte placeHolder;
040    
041    /**
042     * Construct a new packing which returns the specified byte value for unknown Symbols
043     * (such as ambiguity symbols).  This might be outside the normal range of return
044     * values (0-3), allowing callers to detect ambiguity symbols and ignore them.
045     */
046    
047    public DNANoAmbPack(byte placeHolder) {
048        this.placeHolder = placeHolder;
049    }
050  
051    /**
052     * Construct a new packing which translates unknown symbols into
053     * the specified symbol.
054     */
055  
056    public DNANoAmbPack(Symbol placeHolderSymbol) {
057        this.placeHolder = pack(placeHolderSymbol);
058    }
059
060    public FiniteAlphabet getAlphabet() {
061        return DNATools.getDNA();
062    }
063  
064  public byte pack(Symbol sym) {
065    if(false) {
066    } else if(sym == DNATools.a()) {
067      return 0;
068    } else if(sym == DNATools.g()) {
069      return 1;
070    } else if(sym == DNATools.c()) {
071      return 2;
072    } else if(sym == DNATools.t()) {
073      return 3;
074    }
075    
076    return placeHolder;
077  }
078  
079  public Symbol unpack(byte b)
080  throws IllegalSymbolException {
081    if(false) {
082    } else if(b == 0) {
083      return DNATools.a();
084    } else if(b == 1) {
085      return DNATools.g();
086    } else if(b == 2) {
087      return DNATools.c();
088    } else if(b == 3) {
089      return DNATools.t();
090    }
091    
092    throw new IllegalSymbolException("Can't unpack: " + b);
093  }
094  
095  public byte wordSize() {
096    return 2;
097  }
098  
099  public boolean handlesAmbiguity() {
100    return false;
101  }
102}
103