001package com.hfg.bio;
002
003import java.util.ArrayList;
004import java.util.Collections;
005import java.util.HashMap;
006import java.util.List;
007import java.util.Map;
008
009import com.hfg.util.CompareUtil;
010import com.hfg.util.collection.CollectionUtil;
011
012//------------------------------------------------------------------------------
013/**
014 Base class for amino acid grouping schemes.
015 <div>
016 @author J. Alex Taylor, hairyfatguy.com
017 </div>
018 */
019//------------------------------------------------------------------------------
020// com.hfg XML/HTML Coding Library
021//
022// This library is free software; you can redistribute it and/or
023// modify it under the terms of the GNU Lesser General Public
024// License as published by the Free Software Foundation; either
025// version 2.1 of the License, or (at your option) any later version.
026//
027// This library is distributed in the hope that it will be useful,
028// but WITHOUT ANY WARRANTY; without even the implied warranty of
029// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
030// Lesser General Public License for more details.
031//
032// You should have received a copy of the GNU Lesser General Public
033// License along with this library; if not, write to the Free Software
034// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
035//
036// J. Alex Taylor, President, Founder, CEO, COO, CFO, OOPS hairyfatguy.com
037// jataylor@hairyfatguy.com
038//------------------------------------------------------------------------------
039
040public abstract class AAGroupingSchemeImpl implements AAGroupingScheme, Comparable<AAGroupingScheme>
041{
042   private String        mName;
043   private List<AAGroup> mAAGroups;
044
045   // Cached values
046   private Map<AminoAcid, AAGroup> mAAMap;
047   private Map<Character, AAGroup> mCharMap;
048
049
050   //##########################################################################
051   // CONSTRUCTORS
052   //##########################################################################
053
054   //--------------------------------------------------------------------------
055   public AAGroupingSchemeImpl(String inName)
056   {
057      mName = inName;
058   }
059
060   //##########################################################################
061   // PUBLIC METHODS
062   //##########################################################################
063
064   //--------------------------------------------------------------------------
065   @Override
066   public String toString()
067   {
068      return name();
069   }
070
071   //--------------------------------------------------------------------------
072   public String name()
073   {
074      return mName;
075   }
076
077   //--------------------------------------------------------------------------
078   @Override
079   public boolean equals(Object inObj2)
080   {
081      return (inObj2 != null
082              && inObj2 instanceof AAGroupingSchemeImpl
083              && 0 == compareTo((AAGroupingSchemeImpl) inObj2));
084   }
085
086   //--------------------------------------------------------------------------
087   @Override
088   public int hashCode()
089   {
090      int hashCode = 0;
091      if (CollectionUtil.hasValues(getAAGroups()))
092      {
093         for (AAGroup group : getAAGroups())
094         {
095            hashCode += 31 * group.hashCode();
096         }
097      }
098
099      return hashCode;
100   }
101
102   //--------------------------------------------------------------------------
103   @Override
104   public int compareTo(AAGroupingScheme inObj2)
105   {
106      return CompareUtil.compare(getAAGroups(), inObj2.getAAGroups());
107   }
108
109   //--------------------------------------------------------------------------
110   public List<AAGroup> getAAGroups()
111   {
112      return Collections.unmodifiableList(mAAGroups);
113   }
114
115   //--------------------------------------------------------------------------
116   public AAGroup getAAGroup(AminoAcid inAA)
117   {
118      return getAAMap().get(inAA);
119   }
120
121   //--------------------------------------------------------------------------
122   public AAGroup getAAGroup(Character inResidue)
123   {
124      return getCharMap().get(inResidue);
125   }
126
127   //##########################################################################
128   // PROTECTED METHODS
129   //##########################################################################
130
131   //--------------------------------------------------------------------------
132   protected AAGroupingSchemeImpl add(AAGroup inAAGroup)
133   {
134      if (null == mAAGroups)
135      {
136         mAAGroups = new ArrayList<>(10);
137      }
138
139      mAAGroups.add(inAAGroup);
140
141      // Force the maps to get rebuilt.
142      mAAMap = null;
143      mCharMap = null;
144
145      return this;
146   }
147
148   //--------------------------------------------------------------------------
149   private Map<AminoAcid, AAGroup> getAAMap()
150   {
151      if (null == mAAMap)
152      {
153         Map<AminoAcid, AAGroup> map = new HashMap<>(30);
154         for (AAGroup group : mAAGroups)
155         {
156            for (AminoAcid aa : group.getAminoAcids())
157            {
158               map.put(aa, group);
159            }
160         }
161
162         mAAMap = map;
163      }
164
165      return mAAMap;
166   }
167
168   //--------------------------------------------------------------------------
169   private Map<Character, AAGroup> getCharMap()
170   {
171      if (null == mCharMap)
172      {
173         Map<Character, AAGroup> map = new HashMap<>(30);
174         for (AAGroup group : mAAGroups)
175         {
176            for (Character residue : group.getResidues())
177            {
178               map.put(residue, group);
179            }
180         }
181
182         mCharMap = map;
183      }
184
185      return mCharMap;
186   }
187}