001package com.hfg.bio.seq.translation; 002 003import com.hfg.bio.Strand; 004import com.hfg.util.CompareUtil; 005import com.hfg.util.collection.OrderedMap; 006 007import java.util.ArrayList; 008import java.util.Collection; 009import java.util.List; 010import java.util.Map; 011 012//------------------------------------------------------------------------------ 013/** 014 Frame for translation. 015 <div> 016 @author J. Alex Taylor, hairyfatguy.com 017 </div> 018 */ 019//------------------------------------------------------------------------------ 020// com.hfg 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 class TranslationFrame implements Comparable<TranslationFrame> 041{ 042 private char mLetter; 043 private Strand mStrand; 044 private int mOffset; 045 046 private static Map<Character, TranslationFrame> sValueMap = new OrderedMap<Character, TranslationFrame>(7); 047 048 049 public static final TranslationFrame A = new TranslationFrame('A', Strand.PLUS, 0); 050 public static final TranslationFrame B = new TranslationFrame('B', Strand.PLUS, 1); 051 public static final TranslationFrame C = new TranslationFrame('C', Strand.PLUS, 2); 052 public static final TranslationFrame D = new TranslationFrame('D', Strand.MINUS, 0); 053 public static final TranslationFrame E = new TranslationFrame('E', Strand.MINUS, 1); 054 public static final TranslationFrame F = new TranslationFrame('F', Strand.MINUS, 2); 055 056 057 //########################################################################### 058 // CONSTRUCTORS 059 //########################################################################### 060 061 //--------------------------------------------------------------------------- 062 private TranslationFrame(char inLetter, Strand inStrand, int inOffset) 063 { 064 mLetter = inLetter; 065 mStrand = inStrand; 066 mOffset = inOffset; 067 068 sValueMap.put(mLetter, this); 069 } 070 071 //########################################################################### 072 // PUBLIC METHODS 073 //########################################################################### 074 075 //--------------------------------------------------------------------------- 076 public static Collection<TranslationFrame> values() 077 { 078 return sValueMap.values(); 079 } 080 081 //--------------------------------------------------------------------------- 082 public static Collection<TranslationFrame> values(Strand inStrand) 083 { 084 List<TranslationFrame> values = new ArrayList<TranslationFrame>(3); 085 for (TranslationFrame frame : sValueMap.values()) 086 { 087 if (frame.getStrand().equals(inStrand)) 088 { 089 values.add(frame); 090 } 091 } 092 093 return values; 094 } 095 096 //--------------------------------------------------------------------------- 097 public static TranslationFrame valueOf(String inStringValue) 098 { 099 TranslationFrame frame = null; 100 if (inStringValue != null) 101 { 102 if (1 == inStringValue.length()) 103 { 104 frame = sValueMap.get(inStringValue.toUpperCase().charAt(0)); 105 } 106 107 // Maybe it was a number formatted as a string. 108 if (null == frame) 109 { 110 try 111 { 112 String intString = inStringValue.trim(); 113 if (intString.startsWith("+")) 114 { 115 intString = intString.substring(1); 116 } 117 118 int intValue = Integer.parseInt(intString); 119 frame = valueOf(intValue); 120 } 121 catch (Exception e) 122 { 123 // Ignore 124 } 125 } 126 } 127 128 return frame; 129 } 130 131 //--------------------------------------------------------------------------- 132 public static TranslationFrame valueOf(char inCharValue) 133 { 134 TranslationFrame value = sValueMap.get(Character.toUpperCase(inCharValue)); 135 136 if (null == value) 137 { 138 // Maybe it was a number formatted as a char. 139 try 140 { 141 int intValue = Integer.parseInt(inCharValue + ""); 142 value = valueOf(intValue); 143 } 144 catch (Exception e) 145 { 146 // Ignore 147 } 148 } 149 150 return value; 151 } 152 153 //--------------------------------------------------------------------------- 154 public static TranslationFrame valueOf(int inIntValue) 155 { 156 TranslationFrame outFrame = null; 157 158 if (inIntValue < 0) 159 { 160 inIntValue = (- inIntValue) + 3; 161 } 162 for (TranslationFrame frame : sValueMap.values()) 163 { 164 if (frame.toInt() == inIntValue) 165 { 166 outFrame = frame; 167 } 168 } 169 170 return outFrame; 171 } 172 173 //--------------------------------------------------------------------------- 174 public Strand getStrand() 175 { 176 return mStrand; 177 } 178 179 //--------------------------------------------------------------------------- 180 public int getOffset() 181 { 182 return mOffset; 183 } 184 185 //--------------------------------------------------------------------------- 186 @Override 187 public String toString() 188 { 189 return mLetter + ""; 190 } 191 192 //--------------------------------------------------------------------------- 193 public int toChar() 194 { 195 return mLetter; 196 } 197 198 //--------------------------------------------------------------------------- 199 public int toInt() 200 { 201 return mLetter - 'A' + 1; 202 } 203 204 //--------------------------------------------------------------------------- 205 public int compareTo(TranslationFrame inObj2) 206 { 207 return (inObj2 != null ? CompareUtil.compare(toChar(), inObj2.toChar()) : -1); 208 } 209}