com.hfg.bio.seq.translation.TranslationFrame 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.seq.translation;
import com.hfg.bio.Strand;
import com.hfg.util.CompareUtil;
import com.hfg.util.collection.OrderedMap;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Map;
//------------------------------------------------------------------------------
/**
Frame for translation.
@author J. Alex Taylor, hairyfatguy.com
*/
//------------------------------------------------------------------------------
// com.hfg 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 TranslationFrame implements Comparable
{
private char mLetter;
private Strand mStrand;
private int mOffset;
private static Map sValueMap = new OrderedMap(7);
public static final TranslationFrame A = new TranslationFrame('A', Strand.PLUS, 0);
public static final TranslationFrame B = new TranslationFrame('B', Strand.PLUS, 1);
public static final TranslationFrame C = new TranslationFrame('C', Strand.PLUS, 2);
public static final TranslationFrame D = new TranslationFrame('D', Strand.MINUS, 0);
public static final TranslationFrame E = new TranslationFrame('E', Strand.MINUS, 1);
public static final TranslationFrame F = new TranslationFrame('F', Strand.MINUS, 2);
//###########################################################################
// CONSTRUCTORS
//###########################################################################
//---------------------------------------------------------------------------
private TranslationFrame(char inLetter, Strand inStrand, int inOffset)
{
mLetter = inLetter;
mStrand = inStrand;
mOffset = inOffset;
sValueMap.put(mLetter, this);
}
//###########################################################################
// PUBLIC METHODS
//###########################################################################
//---------------------------------------------------------------------------
public static Collection values()
{
return sValueMap.values();
}
//---------------------------------------------------------------------------
public static Collection values(Strand inStrand)
{
List values = new ArrayList(3);
for (TranslationFrame frame : sValueMap.values())
{
if (frame.getStrand().equals(inStrand))
{
values.add(frame);
}
}
return values;
}
//---------------------------------------------------------------------------
public static TranslationFrame valueOf(String inStringValue)
{
TranslationFrame frame = null;
if (inStringValue != null)
{
if (1 == inStringValue.length())
{
frame = sValueMap.get(inStringValue.toUpperCase().charAt(0));
}
// Maybe it was a number formatted as a string.
if (null == frame)
{
try
{
String intString = inStringValue.trim();
if (intString.startsWith("+"))
{
intString = intString.substring(1);
}
int intValue = Integer.parseInt(intString);
frame = valueOf(intValue);
}
catch (Exception e)
{
// Ignore
}
}
}
return frame;
}
//---------------------------------------------------------------------------
public static TranslationFrame valueOf(char inCharValue)
{
TranslationFrame value = sValueMap.get(Character.toUpperCase(inCharValue));
if (null == value)
{
// Maybe it was a number formatted as a char.
try
{
int intValue = Integer.parseInt(inCharValue + "");
value = valueOf(intValue);
}
catch (Exception e)
{
// Ignore
}
}
return value;
}
//---------------------------------------------------------------------------
public static TranslationFrame valueOf(int inIntValue)
{
TranslationFrame outFrame = null;
if (inIntValue < 0)
{
inIntValue = (- inIntValue) + 3;
}
for (TranslationFrame frame : sValueMap.values())
{
if (frame.toInt() == inIntValue)
{
outFrame = frame;
}
}
return outFrame;
}
//---------------------------------------------------------------------------
public Strand getStrand()
{
return mStrand;
}
//---------------------------------------------------------------------------
public int getOffset()
{
return mOffset;
}
//---------------------------------------------------------------------------
@Override
public String toString()
{
return mLetter + "";
}
//---------------------------------------------------------------------------
public int toChar()
{
return mLetter;
}
//---------------------------------------------------------------------------
public int toInt()
{
return mLetter - 'A' + 1;
}
//---------------------------------------------------------------------------
public int compareTo(TranslationFrame inObj2)
{
return (inObj2 != null ? CompareUtil.compare(toChar(), inObj2.toChar()) : -1);
}
}