com.hfg.bio.AAGroup 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;
import java.awt.Color;
import java.util.Collection;
import java.util.Collections;
import java.util.Set;
import com.hfg.exception.UnmodifyableObjectException;
import com.hfg.util.CompareUtil;
import com.hfg.util.collection.CollectionUtil;
import com.hfg.util.collection.OrderedSet;
//------------------------------------------------------------------------------
/**
Amino acid group. Used with AAGroupingScheme.
@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 AAGroup implements Comparable
{
private Set mAAs;
private String mName;
private Character mLetter;
private boolean mLocked;
private Color mColor;
// Cached values
private Set mResidues;
private Integer mHashCode;
//##########################################################################
// CONSTRUCTORS
//##########################################################################
//--------------------------------------------------------------------------
public AAGroup(String inName, Character inLetter)
{
setName(inName);
mLetter = inLetter;
}
//--------------------------------------------------------------------------
public AAGroup(String inName, Character inLetter, Collection inAAs)
{
this(inName, inLetter);
if (inAAs != null)
{
mAAs = new OrderedSet<>(inAAs);
}
}
//##########################################################################
// PUBLIC METHODS
//##########################################################################
//--------------------------------------------------------------------------
public void setName(String inValue)
{
if (mLocked) throw new UnmodifyableObjectException(mName + " is locked and cannot be modified!");
mName = inValue;
}
//--------------------------------------------------------------------------
public String name()
{
return mName;
}
//--------------------------------------------------------------------------
@Override
public boolean equals(Object inObj2)
{
return (inObj2 != null
&& inObj2 instanceof AAGroup
&& 0 == compareTo((AAGroup) inObj2));
}
//--------------------------------------------------------------------------
@Override
public int hashCode()
{
if (null == mHashCode)
{
int hashCode = 0;
if (CollectionUtil.hasValues(getResidues()))
{
for (Character residue : getResidues())
{
hashCode += 31 * residue.hashCode();
}
}
mHashCode = hashCode;
}
return mHashCode;
}
//--------------------------------------------------------------------------
@Override
public int compareTo(AAGroup inObj2)
{
return CompareUtil.compare(getResidues(), inObj2.getResidues());
}
//--------------------------------------------------------------------------
public Character getLetter()
{
return mLetter;
}
//--------------------------------------------------------------------------
public AAGroup setColor(Color inValue)
{
mColor = inValue;
return this;
}
//--------------------------------------------------------------------------
public Color getColor()
{
return mColor;
}
//--------------------------------------------------------------------------
@Override
public String toString()
{
return name();
}
//--------------------------------------------------------------------------
public AAGroup add(AminoAcid inAA)
{
if (mLocked) throw new UnmodifyableObjectException(mName + " is locked and cannot be modified!");
if (inAA != null)
{
if (null == mAAs)
{
mAAs = new OrderedSet<>(26);
}
if (mAAs.add(inAA))
{
clearCachedValues();
}
}
return this;
}
//--------------------------------------------------------------------------
public Set getAminoAcids()
{
return Collections.unmodifiableSet(mAAs);
}
//--------------------------------------------------------------------------
public Set getResidues()
{
if (null == mResidues
&& CollectionUtil.hasValues(mAAs))
{
OrderedSet residues = new OrderedSet<>(mAAs.size());
for (AminoAcid aa : mAAs)
{
residues.add(aa.getOneLetterCode());
}
mResidues = residues;
}
return mResidues;
}
//--------------------------------------------------------------------------
public boolean contains(Character inResidue)
{
Set residues = getResidues();
return (residues != null
&& residues.contains(inResidue));
}
//--------------------------------------------------------------------------
public boolean contains(AminoAcid inAA)
{
return (mAAs != null
&& mAAs.contains(inAA));
}
//--------------------------------------------------------------------------
public boolean isLocked()
{
return mLocked;
}
//--------------------------------------------------------------------------
public void lock()
{
mLocked = true;
}
//##########################################################################
// PRIVATE METHODS
//##########################################################################
//--------------------------------------------------------------------------
private void clearCachedValues()
{
mResidues = null;
mHashCode = null;
}
}