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 */
021
022package org.biojavax.bio.seq;
023
024import org.biojava.bio.BioException;
025import org.biojava.bio.symbol.IllegalSymbolException;
026import org.biojava.bio.symbol.SimpleSymbolList;
027import org.biojava.bio.symbol.SymbolList;
028import org.biojavax.Namespace;
029
030/**
031 * A simple implementation of RichSequence.
032 * @author Richard Holland
033 * @author Bubba Puryear
034 * @since 1.5
035 */
036public class SimpleRichSequence extends ThinRichSequence {
037    
038    private SymbolList symList;
039        
040    /**
041     * Creates a new instance of SimpleRichSequence. Note the use of Double for
042     * seqversion, which indicates that it is nullable.
043     * @param ns the namespace for this sequence.
044     * @param name the name of the sequence.
045     * @param accession the accession of the sequence.
046     * @param version the version of the sequence.
047     * @param symList the symbols for the sequence.
048     * @param seqversion the version of the symbols for the sequence.
049     */
050    public SimpleRichSequence(Namespace ns, String name, String accession, int version, SymbolList symList, Double seqversion) {
051        super(ns,name,accession,version,symList.getAlphabet(),seqversion);
052        this.symList = symList;
053    }
054
055    // Hibernate requirement - not for public use.
056    protected SimpleRichSequence() {} 
057                    
058    // Hibernate requirement - not for public use.
059    protected void setAlphabetName(String alphaname) throws IllegalSymbolException, BioException {
060        super.setAlphabetName(alphaname);
061        this.checkMakeSequence();
062    }
063        
064    // Hibernate requirement - not for public use.
065    private String seqstring;
066    
067    // Hibernate requirement - not for public use.
068    protected void setStringSequence(String seq) throws IllegalSymbolException, BioException {
069        this.seqstring = seq;
070        // convert the string into a symbollist
071        this.checkMakeSequence();
072    }
073    
074    // Hibernate requirement - not for public use.
075    protected String getStringSequence() { 
076        // convert the symbollist into a string
077        return (this.symList==SymbolList.EMPTY_LIST?null:this.seqString()); 
078    }
079    
080    // when both alphabet and sequence have been received, make a symbollist out of them
081    private void checkMakeSequence() throws IllegalSymbolException, BioException {
082        if (this.getAlphabet()!=null && this.seqstring!=null) {
083            // Make the symbol list and assign it.
084            this.symList = new SimpleSymbolList(this.getAlphabet().getTokenization("token"), seqstring);
085        }
086    }    
087    
088    /**
089     * {@inheritDoc}
090     */
091    public int length() { return this.symList.length(); }
092        
093    // Hibernate requirement - not for public use.
094    protected void setSequenceLength(int length) {} // ignore this calculated field
095    
096    // Hibernate requirement - not for public use.
097    protected int getSequenceLength() { return this.length(); }
098    
099    /**
100     * {@inheritDoc}
101     */
102    public SymbolList getInternalSymbolList() {
103        return this.symList;
104    }
105}