001package com.hfg.bio; 002 003import java.awt.Color; 004import java.util.Collection; 005import java.util.Collections; 006import java.util.Set; 007 008import com.hfg.exception.UnmodifyableObjectException; 009import com.hfg.util.CompareUtil; 010import com.hfg.util.collection.CollectionUtil; 011import com.hfg.util.collection.OrderedSet; 012 013//------------------------------------------------------------------------------ 014/** 015 Amino acid group. Used with AAGroupingScheme. 016 <div> 017 @author J. Alex Taylor, hairyfatguy.com 018 </div> 019 */ 020//------------------------------------------------------------------------------ 021// com.hfg XML/HTML Coding Library 022// 023// This library is free software; you can redistribute it and/or 024// modify it under the terms of the GNU Lesser General Public 025// License as published by the Free Software Foundation; either 026// version 2.1 of the License, or (at your option) any later version. 027// 028// This library is distributed in the hope that it will be useful, 029// but WITHOUT ANY WARRANTY; without even the implied warranty of 030// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 031// Lesser General Public License for more details. 032// 033// You should have received a copy of the GNU Lesser General Public 034// License along with this library; if not, write to the Free Software 035// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 036// 037// J. Alex Taylor, President, Founder, CEO, COO, CFO, OOPS hairyfatguy.com 038// jataylor@hairyfatguy.com 039//------------------------------------------------------------------------------ 040 041public class AAGroup implements Comparable<AAGroup> 042{ 043 private Set<AminoAcid> mAAs; 044 private String mName; 045 private Character mLetter; 046 private boolean mLocked; 047 private Color mColor; 048 049 // Cached values 050 private Set<Character> mResidues; 051 private Integer mHashCode; 052 053 //########################################################################## 054 // CONSTRUCTORS 055 //########################################################################## 056 057 //-------------------------------------------------------------------------- 058 public AAGroup(String inName, Character inLetter) 059 { 060 setName(inName); 061 mLetter = inLetter; 062 } 063 064 //-------------------------------------------------------------------------- 065 public AAGroup(String inName, Character inLetter, Collection<AminoAcid> inAAs) 066 { 067 this(inName, inLetter); 068 069 if (inAAs != null) 070 { 071 mAAs = new OrderedSet<>(inAAs); 072 } 073 } 074 075 //########################################################################## 076 // PUBLIC METHODS 077 //########################################################################## 078 079 //-------------------------------------------------------------------------- 080 public void setName(String inValue) 081 { 082 if (mLocked) throw new UnmodifyableObjectException(mName + " is locked and cannot be modified!"); 083 mName = inValue; 084 } 085 086 //-------------------------------------------------------------------------- 087 public String name() 088 { 089 return mName; 090 } 091 092 //-------------------------------------------------------------------------- 093 @Override 094 public boolean equals(Object inObj2) 095 { 096 return (inObj2 != null 097 && inObj2 instanceof AAGroup 098 && 0 == compareTo((AAGroup) inObj2)); 099 } 100 101 //-------------------------------------------------------------------------- 102 @Override 103 public int hashCode() 104 { 105 if (null == mHashCode) 106 { 107 int hashCode = 0; 108 if (CollectionUtil.hasValues(getResidues())) 109 { 110 for (Character residue : getResidues()) 111 { 112 hashCode += 31 * residue.hashCode(); 113 } 114 } 115 116 mHashCode = hashCode; 117 } 118 119 return mHashCode; 120 } 121 122 //-------------------------------------------------------------------------- 123 @Override 124 public int compareTo(AAGroup inObj2) 125 { 126 return CompareUtil.compare(getResidues(), inObj2.getResidues()); 127 } 128 129 //-------------------------------------------------------------------------- 130 public Character getLetter() 131 { 132 return mLetter; 133 } 134 135 //-------------------------------------------------------------------------- 136 public AAGroup setColor(Color inValue) 137 { 138 mColor = inValue; 139 return this; 140 } 141 142 //-------------------------------------------------------------------------- 143 public Color getColor() 144 { 145 return mColor; 146 } 147 148 //-------------------------------------------------------------------------- 149 @Override 150 public String toString() 151 { 152 return name(); 153 } 154 155 //-------------------------------------------------------------------------- 156 public AAGroup add(AminoAcid inAA) 157 { 158 if (mLocked) throw new UnmodifyableObjectException(mName + " is locked and cannot be modified!"); 159 160 if (inAA != null) 161 { 162 if (null == mAAs) 163 { 164 mAAs = new OrderedSet<>(26); 165 } 166 167 if (mAAs.add(inAA)) 168 { 169 clearCachedValues(); 170 } 171 } 172 173 return this; 174 } 175 176 //-------------------------------------------------------------------------- 177 public Set<AminoAcid> getAminoAcids() 178 { 179 return Collections.unmodifiableSet(mAAs); 180 } 181 182 //-------------------------------------------------------------------------- 183 public Set<Character> getResidues() 184 { 185 if (null == mResidues 186 && CollectionUtil.hasValues(mAAs)) 187 { 188 OrderedSet<Character> residues = new OrderedSet<>(mAAs.size()); 189 for (AminoAcid aa : mAAs) 190 { 191 residues.add(aa.getOneLetterCode()); 192 } 193 194 mResidues = residues; 195 } 196 197 return mResidues; 198 } 199 200 //-------------------------------------------------------------------------- 201 public boolean contains(Character inResidue) 202 { 203 Set<Character> residues = getResidues(); 204 return (residues != null 205 && residues.contains(inResidue)); 206 } 207 208 //-------------------------------------------------------------------------- 209 public boolean contains(AminoAcid inAA) 210 { 211 return (mAAs != null 212 && mAAs.contains(inAA)); 213 } 214 215 //-------------------------------------------------------------------------- 216 public boolean isLocked() 217 { 218 return mLocked; 219 } 220 221 //-------------------------------------------------------------------------- 222 public void lock() 223 { 224 mLocked = true; 225 } 226 227 //########################################################################## 228 // PRIVATE METHODS 229 //########################################################################## 230 231 //-------------------------------------------------------------------------- 232 private void clearCachedValues() 233 { 234 mResidues = null; 235 mHashCode = null; 236 } 237}