com.hfg.bio.seq.GenomicLocation 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;
import java.util.regex.Pattern;
import com.hfg.bio.HfgBioXML;
import com.hfg.bio.Strand;
import com.hfg.bio.seq.format.feature.genbank.GenBankFeatureLocation;
import com.hfg.bio.taxonomy.ncbi.NCBITaxon;
import com.hfg.util.CompareUtil;
import com.hfg.util.StringUtil;
import com.hfg.xml.XMLTag;
//------------------------------------------------------------------------------
/**
Genomic location.
@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 GenomicLocation extends GenBankFeatureLocation
{
private String mAssembly;
private String mBuild;
private NCBITaxon mTaxon;
private String mChromosome;
private Integer mChromosomeNum;
private String mContig;
private Strand mStrand;
private static final Pattern NUM_PATTERN = Pattern.compile("\\d+");
//###########################################################################
// CONSTRUCTORS
//###########################################################################
//--------------------------------------------------------------------------
public GenomicLocation()
{
super(null);
}
//--------------------------------------------------------------------------
public GenomicLocation(String inLocationString)
{
super(inLocationString);
}
//---------------------------------------------------------------------------
public GenomicLocation(XMLTag inXMLTag)
{
this();
inXMLTag.verifyTagName(HfgBioXML.GENOMIC_LOC_TAG);
setAssembly(inXMLTag.getAttributeValue(HfgBioXML.ASSEMBLY_ATT));
setBuild(inXMLTag.getAttributeValue(HfgBioXML.BUILD_ATT));
if (inXMLTag.hasAttribute(HfgBioXML.CHROMOSOME_ATT))
{
setChromosome(inXMLTag.getAttributeValue(HfgBioXML.CHROMOSOME_ATT));
}
if (inXMLTag.hasAttribute(HfgBioXML.CONTIG_ATT))
{
setContig(inXMLTag.getAttributeValue(HfgBioXML.CONTIG_ATT));
}
if (inXMLTag.hasAttribute(HfgBioXML.CONTIG_LENGTH_ATT))
{
setSeqLength(Integer.parseInt(inXMLTag.getAttributeValue(HfgBioXML.CONTIG_LENGTH_ATT)));
}
if (inXMLTag.hasAttribute(HfgBioXML.TAXON_ATT))
{
setTaxon(NCBITaxon.getByTaxonId(Integer.parseInt(inXMLTag.getAttributeValue(HfgBioXML.TAXON_ATT))));
}
if (inXMLTag.hasAttribute(HfgBioXML.STRAND_ATT))
{
setStrand(Strand.valueOf(inXMLTag.getAttributeValue(HfgBioXML.STRAND_ATT)));
}
append(inXMLTag.getUnescapedContent());
}
//###########################################################################
// PUBLIC METHODS
//###########################################################################
//--------------------------------------------------------------------------
@Override
public GenomicLocation clone()
{
GenomicLocation copy = (GenomicLocation) super.clone();
return copy;
}
//--------------------------------------------------------------------------
public XMLTag toXMLTag()
{
XMLTag tag = new XMLTag(HfgBioXML.GENOMIC_LOC_TAG);
if (StringUtil.isSet(getAssembly()))
{
tag.setAttribute(HfgBioXML.ASSEMBLY_ATT, getAssembly());
}
if (StringUtil.isSet(getBuild()))
{
tag.setAttribute(HfgBioXML.BUILD_ATT, getBuild());
}
if (getTaxon() != null)
{
tag.setAttribute(HfgBioXML.TAXON_ATT, getTaxon().getTaxonId());
}
if (getChromosome() != null)
{
tag.setAttribute(HfgBioXML.CHROMOSOME_ATT, getChromosome());
}
if (getContig() != null)
{
tag.setAttribute(HfgBioXML.CONTIG_ATT, getContig());
}
if (getSeqLength() != null)
{
tag.setAttribute(HfgBioXML.CONTIG_LENGTH_ATT, getSeqLength());
}
if (getStrand() != null)
{
tag.setAttribute(HfgBioXML.STRAND_ATT, getStrand());
}
tag.setContent(toString());
return tag;
}
//--------------------------------------------------------------------------
public GenomicLocation setAssembly(String inValue)
{
mAssembly = inValue;
return this;
}
//--------------------------------------------------------------------------
public String getAssembly()
{
return mAssembly;
}
//--------------------------------------------------------------------------
public GenomicLocation setBuild(String inValue)
{
mBuild = inValue;
return this;
}
//--------------------------------------------------------------------------
public String getBuild()
{
return mBuild;
}
//--------------------------------------------------------------------------
public GenomicLocation setChromosome(String inValue)
{
mChromosome = inValue;
if (inValue != null
&& NUM_PATTERN.matcher(inValue).matches())
{
mChromosomeNum = Integer.parseInt(inValue);
}
return this;
}
//--------------------------------------------------------------------------
public String getChromosome()
{
return mChromosome;
}
//--------------------------------------------------------------------------
public GenomicLocation setContig(String inValue)
{
mContig = inValue;
return this;
}
//--------------------------------------------------------------------------
public String getContig()
{
return mContig;
}
//--------------------------------------------------------------------------
@Override
public GenomicLocation setSeqLength(Integer inValue)
{
return (GenomicLocation) super.setSeqLength(inValue);
}
//--------------------------------------------------------------------------
public GenomicLocation setTaxon(NCBITaxon inValue)
{
mTaxon = inValue;
return this;
}
//--------------------------------------------------------------------------
public NCBITaxon getTaxon()
{
return mTaxon;
}
//--------------------------------------------------------------------------
public GenomicLocation setStrand(Strand inValue)
{
mStrand = inValue;
return this;
}
//--------------------------------------------------------------------------
public Strand getStrand()
{
return mStrand;
}
//--------------------------------------------------------------------------
public int compareTo(GenomicLocation inObj2)
{
int result = -1;
if (inObj2 != null)
{
if (mChromosomeNum != null
&& inObj2.mChromosomeNum != null)
{
result = CompareUtil.compare(mChromosomeNum, inObj2.mChromosomeNum);
}
else
{
result = CompareUtil.compare(getChromosome(), inObj2.getChromosome());
// Handle situation where one chromosome is called 'Unknown' and the other is called 'Un'
if (result != 0
&& getChromosome() != null
&& inObj2.getChromosome() != null
&& ((getChromosome().equalsIgnoreCase("Unknown") || getChromosome().equalsIgnoreCase("Un"))
&& (inObj2.getChromosome().equalsIgnoreCase("Unknown") || inObj2.getChromosome().equalsIgnoreCase("Un"))))
{
result = 0;
}
}
if (0 == result)
{
result = CompareUtil.compare(getContig(), inObj2.getContig());
}
if (0 == result)
{
result = super.compareTo(inObj2);
}
}
return result;
}
}