All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.hfg.bio.AAGroupingSchemeImpl Maven / Gradle / Ivy

There is a newer version: 20240423
Show newest version
package com.hfg.bio;

import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.hfg.util.CompareUtil;
import com.hfg.util.collection.CollectionUtil;

//------------------------------------------------------------------------------
/**
 Base class for amino acid grouping schemes.
 
@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 abstract class AAGroupingSchemeImpl implements AAGroupingScheme, Comparable { private String mName; private List mAAGroups; // Cached values private Map mAAMap; private Map mCharMap; //########################################################################## // CONSTRUCTORS //########################################################################## //-------------------------------------------------------------------------- public AAGroupingSchemeImpl(String inName) { mName = inName; } //########################################################################## // PUBLIC METHODS //########################################################################## //-------------------------------------------------------------------------- @Override public String toString() { return name(); } //-------------------------------------------------------------------------- public String name() { return mName; } //-------------------------------------------------------------------------- @Override public boolean equals(Object inObj2) { return (inObj2 != null && inObj2 instanceof AAGroupingSchemeImpl && 0 == compareTo((AAGroupingSchemeImpl) inObj2)); } //-------------------------------------------------------------------------- @Override public int hashCode() { int hashCode = 0; if (CollectionUtil.hasValues(getAAGroups())) { for (AAGroup group : getAAGroups()) { hashCode += 31 * group.hashCode(); } } return hashCode; } //-------------------------------------------------------------------------- @Override public int compareTo(AAGroupingScheme inObj2) { return CompareUtil.compare(getAAGroups(), inObj2.getAAGroups()); } //-------------------------------------------------------------------------- public List getAAGroups() { return Collections.unmodifiableList(mAAGroups); } //-------------------------------------------------------------------------- public AAGroup getAAGroup(AminoAcid inAA) { return getAAMap().get(inAA); } //-------------------------------------------------------------------------- public AAGroup getAAGroup(Character inResidue) { return getCharMap().get(inResidue); } //########################################################################## // PROTECTED METHODS //########################################################################## //-------------------------------------------------------------------------- protected AAGroupingSchemeImpl add(AAGroup inAAGroup) { if (null == mAAGroups) { mAAGroups = new ArrayList<>(10); } mAAGroups.add(inAAGroup); // Force the maps to get rebuilt. mAAMap = null; mCharMap = null; return this; } //-------------------------------------------------------------------------- private Map getAAMap() { if (null == mAAMap) { Map map = new HashMap<>(30); for (AAGroup group : mAAGroups) { for (AminoAcid aa : group.getAminoAcids()) { map.put(aa, group); } } mAAMap = map; } return mAAMap; } //-------------------------------------------------------------------------- private Map getCharMap() { if (null == mCharMap) { Map map = new HashMap<>(30); for (AAGroup group : mAAGroups) { for (Character residue : group.getResidues()) { map.put(residue, group); } } mCharMap = map; } return mCharMap; } }




© 2015 - 2024 Weber Informatics LLC | Privacy Policy