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.biojava.bio.program.phred;
023
024import org.biojava.bio.Annotation;
025import org.biojava.bio.BioError;
026import org.biojava.bio.seq.DNATools;
027import org.biojava.bio.seq.impl.SimpleSequence;
028import org.biojava.bio.symbol.IllegalAlphabetException;
029import org.biojava.bio.symbol.IllegalSymbolException;
030import org.biojava.bio.symbol.IntegerAlphabet;
031import org.biojava.bio.symbol.SimpleSymbolList;
032import org.biojava.bio.symbol.Symbol;
033import org.biojava.bio.symbol.SymbolList;
034import org.biojava.utils.ChangeVetoException;
035
036/**
037 * <p>PhredSequence is an extension of SimpleSequence that implements
038 * Qualitative to hold Phred quality scores.</p>
039 *
040 * <p>Copyright:    Copyright (c) 2001</p>
041 * <p>Company:      AgResearch</p>
042 * @author Mark Schreiber
043 * @since 1.1
044 */
045
046public class PhredSequence extends SimpleSequence implements Qualitative{
047
048  /**
049   * Constructs a new PhredSequence.
050   * @param phredSequence - a SymbolList over the Phred Alphabet.
051   * @param name - the name for the sequence.
052   * @param urn - the URN for the sequence.
053   * @param anno - the Annotation object for the sequence.
054   */
055  public PhredSequence(SymbolList phredSequence, String name, String urn, Annotation anno)
056  throws IllegalAlphabetException{
057    super(phredSequence,urn,name,anno);
058    if(this.getAlphabet() != PhredTools.getPhredAlphabet()){
059      throw new IllegalAlphabetException(
060        "Cannot build a PhredSequence using a "+
061        phredSequence.getAlphabet().getName()+
062        " SymbolList.");
063    }
064  }
065
066  /**
067   * Extracts the quality part if the Phred Alphabet and returns it as a SymbolList
068   * over the Integer SubAlphabet from 0..99.
069   */
070  public SymbolList getQuality(){
071    SimpleSymbolList qual = new SimpleSymbolList(IntegerAlphabet.getSubAlphabet(0,99));
072    for(int i = 1; i < this.length() + 1; i++){
073      try{
074        qual.addSymbol(PhredTools.integerSymbolFromPhred(this.symbolAt(i)));
075      }catch(IllegalSymbolException ise){
076        throw new BioError(
077        "PhredTools.integerSymbolFromPhred() has returned a symbol not in this SymbolLists alphabet", ise);
078      }catch(ChangeVetoException cve){
079        throw new BioError( "Cannot construct symbol list as it has becomed locked?", cve);
080      }
081    }
082    return qual;
083  }
084
085  /**
086   * Extracts the DNA part of the PhredAlpahbet SymbolList and returns it as a SymbolList
087   */
088  public SymbolList getDNA(){
089    SimpleSymbolList dna = new SimpleSymbolList(DNATools.getDNA());
090    for(int i = 1; i < this.length() + 1; i++){
091      try{
092        dna.addSymbol(PhredTools.dnaSymbolFromPhred(this.symbolAt(i)));
093      }catch(ChangeVetoException cve){
094        throw new BioError("Cannot construct symbol list as it has becomed locked?", cve);
095      }catch(IllegalSymbolException ise){
096        throw new BioError(
097        "PhredTools.dnaSymbolFromPhred() has returned a symbol not in the DNA alphabet", ise);
098      }
099    }
100    return dna;
101  }
102
103  public Symbol getQualityAt(int index) throws IndexOutOfBoundsException{
104    try{
105      return PhredTools.integerSymbolFromPhred(this.symbolAt(index));
106    }catch(IllegalSymbolException ise){
107      throw new BioError("Something has gone badly wrong with the Phred Alphabet!", ise);
108    }
109  }
110
111  public Symbol getDNAAt(int index) throws IndexOutOfBoundsException{
112    try{
113      return PhredTools.dnaSymbolFromPhred(this.symbolAt(index));
114    }catch(IllegalSymbolException ise){
115      throw new BioError("Something has gone badly wrong with the Phred Alphabet!", ise);
116    }
117  }
118}