com.hfg.chem.IonizableGroup 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.chem;
import com.hfg.util.CompareUtil;
import com.hfg.xml.XMLNode;
import com.hfg.xml.XMLTag;
//------------------------------------------------------------------------------
/**
Ionizable chemical group.
@author J. Alex Taylor, hairyfatguy.com
*/
//------------------------------------------------------------------------------
// com.hfg XML/HTML Coding 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 IonizableGroup implements Comparable
{
public static final String IONIZABLE_GROUP_TAG = "IonizableGrp";
public static final String PROTONATED_FORM_ATT = "protonatedForm";
public static final String KA_ATT = "ka";
//##########################################################################
// PRIVATE FIELDS
//##########################################################################
private Charge mProtonatedForm;
private double mKa;
//##########################################################################
// CONSTRUCTORS
//##########################################################################
//--------------------------------------------------------------------------
public IonizableGroup(double inKa, Charge inProtonatedForm)
{
if (inKa > 1
|| inKa < 0)
{
throw new RuntimeException("Illegal Ka value: " + inKa + "! The Ka cannot be > 1 or < 0! (Be sure the value entered is not the pKa.)");
}
mKa = inKa;
if (null == inProtonatedForm)
{
throw new RuntimeException("The protonated form cannot be null!");
}
else if (inProtonatedForm == Charge.NEGATIVE)
{
throw new RuntimeException("The protonated form cannot be negative!");
}
mProtonatedForm = inProtonatedForm;
}
//--------------------------------------------------------------------------
public IonizableGroup(XMLNode inXML)
{
if (! inXML.getTagName().equals(IONIZABLE_GROUP_TAG))
{
throw new RuntimeException("Cannot construct an " + this.getClass().getSimpleName() + " from a " + inXML.getTagName() + " tag!");
}
mKa = Double.parseDouble(inXML.getAttributeValue(KA_ATT));
mProtonatedForm = Charge.valueOf(inXML.getAttributeValue(PROTONATED_FORM_ATT));
}
//##########################################################################
// PUBLIC METHODS
//##########################################################################
//--------------------------------------------------------------------------
@Override
public int hashCode()
{
return new Float(getKa()).hashCode() + 31 * getProtonatedForm().hashCode();
}
//--------------------------------------------------------------------------
@Override
public boolean equals(Object inObj2)
{
boolean result = false;
if (inObj2 != null
&& inObj2 instanceof IonizableGroup)
{
result = (0 == compareTo((IonizableGroup) inObj2));
}
return result;
}
//--------------------------------------------------------------------------
@Override
public int compareTo(IonizableGroup inObj2)
{
int result = -1;
if (inObj2 != null)
{
result = 0;
if (this != inObj2)
{
result = CompareUtil.compare(getKa(), inObj2.getKa());
if (0 == result)
{
result = CompareUtil.compare(getProtonatedForm(), inObj2.getProtonatedForm());
}
}
}
return result;
}
//--------------------------------------------------------------------------
public Charge getProtonatedForm()
{
return mProtonatedForm;
}
//--------------------------------------------------------------------------
public float getpKa()
{
return (float) - Math.log10(mKa);
}
//--------------------------------------------------------------------------
public double getKa()
{
return mKa;
}
//--------------------------------------------------------------------------
public double getCharge(int inCount, double inConcOfHIons)
{
double value = 0;
if (inCount > 0)
{
if (getProtonatedForm() == Charge.POSITIVE)
{
value = inCount * ((inConcOfHIons / (inConcOfHIons + getKa())));
}
else if (getProtonatedForm() == Charge.NEUTRAL)
{
value = -1 * inCount * (1 - (inConcOfHIons / (inConcOfHIons + getKa())));
}
}
return value;
}
//--------------------------------------------------------------------------
@Override
public String toString()
{
return String.format("%.2E (%s)", mKa, mProtonatedForm.toString());
}
//--------------------------------------------------------------------------
public XMLNode toXMLNode()
{
XMLNode node = new XMLTag(IONIZABLE_GROUP_TAG);
node.setAttribute(KA_ATT, mKa + "");
node.setAttribute(PROTONATED_FORM_ATT, mProtonatedForm.toString());
return node;
}
}