com.hfg.bio.seq.translation.Codon Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of com_hfg Show documentation
Show all versions of com_hfg Show documentation
com.hfg xml, html, svg, and bioinformatics utility library
package com.hfg.bio.seq.translation;
import com.hfg.util.StringUtil;
import com.hfg.xml.XMLTag;
//------------------------------------------------------------------------------
/**
Codon container.
@author J. Alex Taylor, hairyfatguy.com
*/
//------------------------------------------------------------------------------
// com.hfg Library
//
// This library is free software; you can redistribute it and/or
// modify it under the terms of the GNU Lesser General Public
// License as published by the Free Software Foundation; either
// version 2.1 of the License, or (at your option) any later version.
//
// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
// Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public
// License along with this library; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
//
// J. Alex Taylor, President, Founder, CEO, COO, CFO, OOPS hairyfatguy.com
// [email protected]
//------------------------------------------------------------------------------
public class Codon
{
private String mTriplet;
private Character mAA;
private boolean mContainsDegenerateBases;
private CodonUsage mCodonUsage;
public static final String XML_CODON = "Codon";
public static final String XML_AA_ATT = "aa";
public static final String XML_TRIPLET_ATT = "triplet";
//###########################################################################
// CONSTRUCTORS
//###########################################################################
//---------------------------------------------------------------------------
public Codon(String inTriplet)
{
if (! StringUtil.isSet(inTriplet)
|| 3 != inTriplet.length())
{
throw new RuntimeException("Codons must be three nucleotides long!");
}
setTriplet(inTriplet);
}
//---------------------------------------------------------------------------
public Codon(XMLTag inXMLTag)
{
inXMLTag.verifyTagName(XML_CODON);
setTriplet(inXMLTag.getAttributeValue(XML_TRIPLET_ATT));
String aa = inXMLTag.getAttributeValue(XML_AA_ATT);
{
if (StringUtil.isSet(aa))
{
if (aa.length() > 1)
{
throw new RuntimeException("The specified aa value " + StringUtil.singleQuote(aa) + " is longer than 1 character!");
}
mAA = aa.charAt(0);
}
}
XMLTag codonUsageTag = inXMLTag.getOptionalSubtagByName(CodonUsage.XML_CODON_USAGE);
if (codonUsageTag != null)
{
mCodonUsage = new CodonUsage(codonUsageTag);
}
}
//###########################################################################
// PUBLIC METHODS
//###########################################################################
//---------------------------------------------------------------------------
public Codon setAA(Character inValue)
{
mAA = inValue;
return this;
}
//---------------------------------------------------------------------------
public Character getAA()
{
return mAA;
}
//---------------------------------------------------------------------------
public Codon setCodonUsage(CodonUsage inValue)
{
mCodonUsage = inValue;
return this;
}
//---------------------------------------------------------------------------
public CodonUsage getCodonUsage()
{
return mCodonUsage;
}
//---------------------------------------------------------------------------
@Override
public String toString()
{
return mTriplet;
}
//---------------------------------------------------------------------------
public XMLTag toXMLTag()
{
XMLTag tag = new XMLTag(XML_CODON);
tag.setAttribute(XML_TRIPLET_ATT, mTriplet);
if (mAA != null)
{
tag.setAttribute(XML_AA_ATT, mAA);
}
if (mCodonUsage != null)
{
tag.addSubtag(mCodonUsage.toXMLTag());
}
return tag;
}
//---------------------------------------------------------------------------
@Override
public boolean equals(Object inObj2)
{
boolean result = false;
if (inObj2 != null
&& inObj2 instanceof Codon)
{
Codon codon2 = (Codon) inObj2;
result = toString().equals(codon2.toString());
}
return result;
}
//---------------------------------------------------------------------------
@Override
public int hashCode()
{
return toString().hashCode();
}
//---------------------------------------------------------------------------
public boolean containsDegenerateBases()
{
return mContainsDegenerateBases;
}
//###########################################################################
// PRIVATE METHODS
//###########################################################################
//---------------------------------------------------------------------------
private void setTriplet(String inValue)
{
mTriplet = inValue.toUpperCase().replaceAll("U", "T"); // Force everything into uppercase DNA
mContainsDegenerateBases = false;
for (Character nucleotide : mTriplet.toCharArray())
{
if (! "ATGC".contains(nucleotide + ""))
{
mContainsDegenerateBases = true;
break;
}
}
}
}