com.hfg.bio.Nucleotide 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;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import com.hfg.chem.Molecule;
import com.hfg.util.StringUtil;
//------------------------------------------------------------------------------
/**
* Nucleotide.
* @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 class Nucleotide extends Molecule
{
//##########################################################################
// PRIVATE FIELDS
//##########################################################################
private Character m1LetterCode;
private BaseType mBaseType;
private List mDegeneracy;
private Nucleotide mComplement;
// This declaration has to come before the public constants below.
private static Set sValues = new HashSet<>();
//##########################################################################
// PUBLIC FIELDS
//##########################################################################
public enum BaseType { PURINE, PYRIMIDINE };
public static final Nucleotide ADENINE = new Nucleotide("Adenine", 'A', BaseType.PURINE)
.lock()
.register();
public static final Nucleotide GUANINE = new Nucleotide("Guanine", 'G', BaseType.PURINE)
.lock()
.register();
public static final Nucleotide CYTOSINE = new Nucleotide("Cytosine",'C', BaseType.PYRIMIDINE)
.lock()
.register();
// DNA only
public static final Nucleotide THYMINE = new Nucleotide("Thymine", 'T', BaseType.PYRIMIDINE)
.lock()
.register();
// RNA only
public static final Nucleotide URACIL = new Nucleotide("Uracil", 'U', BaseType.PYRIMIDINE)
.lock()
.register();
// TODO Add elemental composition. How to distinguish RNA & DNA?
// Degenerate (ambiguous) nucleotides
public static final Nucleotide W = new Nucleotide("W", 'W').addDegeneracy(ADENINE).addDegeneracy(THYMINE)
.lock()
.register();
public static final Nucleotide S = new Nucleotide("S", 'S').addDegeneracy(CYTOSINE).addDegeneracy(GUANINE)
.lock()
.register();
public static final Nucleotide M = new Nucleotide("M", 'M').addDegeneracy(ADENINE).addDegeneracy(CYTOSINE)
.lock()
.register();
public static final Nucleotide K = new Nucleotide("K", 'K').addDegeneracy(GUANINE).addDegeneracy(THYMINE)
.lock()
.register();
public static final Nucleotide R = new Nucleotide("R", 'R').addDegeneracy(ADENINE).addDegeneracy(GUANINE)
.lock()
.register();
public static final Nucleotide Y = new Nucleotide("Y", 'Y').addDegeneracy(CYTOSINE).addDegeneracy(THYMINE)
.lock()
.register();
public static final Nucleotide B = new Nucleotide("B", 'B').addDegeneracy(CYTOSINE).addDegeneracy(GUANINE).addDegeneracy(THYMINE)
.lock()
.register();
public static final Nucleotide D = new Nucleotide("D", 'D').addDegeneracy(ADENINE).addDegeneracy(GUANINE).addDegeneracy(THYMINE)
.lock()
.register();
public static final Nucleotide H = new Nucleotide("H", 'H').addDegeneracy(ADENINE).addDegeneracy(CYTOSINE).addDegeneracy(THYMINE)
.lock()
.register();
public static final Nucleotide V = new Nucleotide("V", 'V').addDegeneracy(ADENINE).addDegeneracy(CYTOSINE).addDegeneracy(GUANINE)
.lock()
.register();
public static final Nucleotide N = new Nucleotide("N", 'N').addDegeneracy(ADENINE).addDegeneracy(CYTOSINE).addDegeneracy(GUANINE).addDegeneracy(THYMINE)
.lock()
.register();
static
{
// Set the complement values
ADENINE.setComplement(THYMINE);
THYMINE.setComplement(ADENINE);
CYTOSINE.setComplement(GUANINE);
GUANINE.setComplement(CYTOSINE);
R.setComplement(Y);
Y.setComplement(R);
S.setComplement(W);
W.setComplement(S);
K.setComplement(M);
M.setComplement(K);
B.setComplement(V);
V.setComplement(B);
D.setComplement(H);
H.setComplement(D);
N.setComplement(N);
}
//##########################################################################
// CONSTRUCTORS
//##########################################################################
//--------------------------------------------------------------------------
public Nucleotide(String inName)
{
super(inName);
}
//--------------------------------------------------------------------------
public Nucleotide(String inName, char in1LetterCode)
{
this(inName);
m1LetterCode = in1LetterCode;
}
//--------------------------------------------------------------------------
public Nucleotide(String inName, char in1LetterCode, BaseType inBaseType)
{
this(inName, in1LetterCode);
mBaseType = inBaseType;
}
//##########################################################################
// PUBLIC METHODS
//##########################################################################
//--------------------------------------------------------------------------
@Override
public Nucleotide lock()
{
return (Nucleotide) super.lock();
}
//--------------------------------------------------------------------------
/**
Puts the Nucleotide into the Set of unique Nucleotides returned by Nucleotide.values().
*/
public Nucleotide register()
{
if (! isLocked())
{
throw new RuntimeException("Only locked Nucleotides can be added to the values list!");
}
sValues.add(this);
return this;
}
//--------------------------------------------------------------------------
/**
Returns the Nucleotide whose name or 1-letter code matches the specified String.
@param inString the name or 1-letter code for the Nucleotide to retrieve
@return the Nucleotide whose name or 1-letter code matches the specified String
*/
public static Nucleotide valueOf(String inString)
{
Nucleotide value = null;
if (StringUtil.isSet(inString))
{
for (Nucleotide nuc : sValues)
{
if (nuc.name().equalsIgnoreCase(inString)
|| inString.charAt(0) == nuc.getOneLetterCode())
{
value = nuc;
break;
}
}
}
return value;
}
//--------------------------------------------------------------------------
/**
Returns the Nucleotide whose 1-letter code matches the specified String.
@param inChar the 1-letter code for the Nucleotide to retrieve
@return the Nucleotide whose 1-letter code matches the specified String
*/
public static Nucleotide valueOf(char inChar)
{
return valueOf(inChar + "");
}
//--------------------------------------------------------------------------
public static Nucleotide[] values()
{
return sValues.toArray(new Nucleotide[sValues.size()]);
}
//--------------------------------------------------------------------------
public static Nucleotide[] degenerateValues()
{
Set values = new HashSet<>(11);
for (Nucleotide nucleotide : sValues)
{
if (nucleotide.isAmbiguous())
{
values.add(nucleotide);
}
}
return values.toArray(new Nucleotide[values.size()]);
}
//--------------------------------------------------------------------------
/**
Returns an unlocked copy of the Nucleotide.
*/
@Override
public Nucleotide clone()
{
return (Nucleotide) super.clone();
}
//--------------------------------------------------------------------------
public BaseType getBaseType()
{
return mBaseType;
}
//--------------------------------------------------------------------------
public Character getOneLetterCode()
{
return m1LetterCode;
}
//--------------------------------------------------------------------------
private Nucleotide setComplement(Nucleotide inValue)
{
mComplement = inValue;
return this;
}
//--------------------------------------------------------------------------
public Nucleotide getComplement()
{
return mComplement;
}
//--------------------------------------------------------------------------
public Nucleotide addDegeneracy(Nucleotide inValue)
{
if (null == mDegeneracy)
{
mDegeneracy = new ArrayList<>(4);
}
mDegeneracy.add(inValue);
return this;
}
//--------------------------------------------------------------------------
public Collection getDegeneracy()
{
return mDegeneracy;
}
//--------------------------------------------------------------------------
public boolean isAmbiguous()
{
return mDegeneracy != null;
}
}