001package com.hfg.bio.seq.translation;
002
003
004import com.hfg.util.StringUtil;
005import com.hfg.xml.XMLTag;
006
007//------------------------------------------------------------------------------
008/**
009 Codon usage data container.
010 <div>
011  @author J. Alex Taylor, hairyfatguy.com
012 </div>
013 */
014//------------------------------------------------------------------------------
015// com.hfg Library
016//
017// This library is free software; you can redistribute it and/or
018// modify it under the terms of the GNU Lesser General Public
019// License as published by the Free Software Foundation; either
020// version 2.1 of the License, or (at your option) any later version.
021//
022// This library is distributed in the hope that it will be useful,
023// but WITHOUT ANY WARRANTY; without even the implied warranty of
024// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
025// Lesser General Public License for more details.
026//
027// You should have received a copy of the GNU Lesser General Public
028// License along with this library; if not, write to the Free Software
029// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
030//
031// J. Alex Taylor, President, Founder, CEO, COO, CFO, OOPS hairyfatguy.com
032// jataylor@hairyfatguy.com
033//------------------------------------------------------------------------------
034
035public class CodonUsage
036{
037   private Integer mNumber;
038   private Float   mFreqPer1000;
039   private Float   mBias;
040
041   public static final String XML_CODON_USAGE       = "CodonUsage";
042   public static final String XML_NUMBER_ATT        = "number";
043   public static final String XML_FREQ_PER_1000_ATT = "freqPer1000";
044   public static final String XML_BIAS_ATT          = "bias";
045
046
047   //###########################################################################
048   // CONSTRUCTORS
049   //###########################################################################
050
051   //---------------------------------------------------------------------------
052   public CodonUsage()
053   {
054   }
055
056   //---------------------------------------------------------------------------
057   public CodonUsage(XMLTag inXMLTag)
058   {
059      inXMLTag.verifyTagName(XML_CODON_USAGE);
060
061      String numberString = inXMLTag.getAttributeValue(XML_NUMBER_ATT);
062      {
063         if (StringUtil.isSet(numberString))
064         {
065            try
066            {
067               setNumber(Integer.parseInt(numberString));
068            }
069            catch (Exception e)
070            {
071               throw new RuntimeException("The specified " + XML_NUMBER_ATT + " value "
072                     + StringUtil.singleQuote(numberString) + " couldn't be parsed!", e);
073            }
074         }
075      }
076
077      String freqString = inXMLTag.getAttributeValue(XML_FREQ_PER_1000_ATT);
078      {
079         if (StringUtil.isSet(freqString))
080         {
081            try
082            {
083               setFreqPer1000(Float.parseFloat(freqString));
084            }
085            catch (Exception e)
086            {
087               throw new RuntimeException("The specified " + XML_FREQ_PER_1000_ATT + " value "
088                     + StringUtil.singleQuote(freqString) + " couldn't be parsed!", e);
089            }
090         }
091      }
092
093      String biasString = inXMLTag.getAttributeValue(XML_BIAS_ATT);
094      {
095         if (StringUtil.isSet(biasString))
096         {
097            try
098            {
099               setBias(Float.parseFloat(biasString));
100            }
101            catch (Exception e)
102            {
103               throw new RuntimeException("The specified " + XML_BIAS_ATT + " value "
104                     + StringUtil.singleQuote(biasString) + " couldn't be parsed!", e);
105            }
106         }
107      }
108   }
109
110   //###########################################################################
111   // PUBLIC METHODS
112   //###########################################################################
113
114   //---------------------------------------------------------------------------
115   /**
116    Specifies the number of times this codon was observed in the set of sequences.
117    @param inValue the number of times this codon was observed in the set of sequences
118    @return this CodonUsage object (for potential method chaining)
119    */
120   public CodonUsage setNumber(Integer inValue)
121   {
122      mNumber = inValue;
123      return this;
124   }
125
126   //---------------------------------------------------------------------------
127   /**
128    Returns the number of times this codon was observed in the set of sequences.
129    @return the number of times this codon was observed in the set of sequences
130    */
131   public Integer getNumber()
132   {
133      return mNumber;
134   }
135
136   //---------------------------------------------------------------------------
137   /**
138    Specifies the frequency (per 1000 codons) that this codon was observed in the set of sequences.
139    @param inValue the frequency (per 1000 codons) that this codon was observed in the set of sequences
140    @return this CodonUsage object (for potential method chaining)
141    */
142   public CodonUsage setFreqPer1000(Float inValue)
143   {
144      mFreqPer1000 = inValue;
145      return this;
146   }
147
148   //---------------------------------------------------------------------------
149   /**
150    Returns the frequency (per 1000 codons) that this codon was observed in the set of sequences.
151    @return the frequency (per 1000 codons) that this codon was observed in the set of sequences
152    */
153   public Float getFreqPer1000()
154   {
155      return mFreqPer1000;
156   }
157
158   //---------------------------------------------------------------------------
159   /**
160    Specifies the bias - the fraction of this codon's use as compared to the other codons that
161    code for the same amino acid.
162    @param inValue the fraction of this codon's use out of the total for all codons coding for the same amino acid
163    @return this CodonUsage object (for potential method chaining)
164    */
165   public CodonUsage setBias(Float inValue)
166   {
167      mBias = inValue;
168      return this;
169   }
170
171   //---------------------------------------------------------------------------
172   /**
173    Returns the bias - the fraction of this codon's use as compared to the other codons that
174    code for the same amino acid.
175    @return the fraction of this codon's use out of the total for all codons coding for the same amino acid
176    */
177   public Float getBias()
178   {
179      return mBias;
180   }
181
182
183   //---------------------------------------------------------------------------
184   public XMLTag toXMLTag()
185   {
186      XMLTag tag = new XMLTag(XML_CODON_USAGE);
187
188      if (getNumber() != null)
189      {
190         tag.setAttribute(XML_NUMBER_ATT, getNumber());
191      }
192
193      if (getFreqPer1000() != null)
194      {
195         tag.setAttribute(XML_FREQ_PER_1000_ATT, String.format("%.2f", getFreqPer1000()));
196      }
197
198      if (getBias() != null)
199      {
200         tag.setAttribute(XML_BIAS_ATT, String.format("%.2f", getBias()));
201      }
202
203      return tag;
204   }
205
206}