001package com.hfg.bio.seq;
002
003
004import java.util.regex.Pattern;
005
006import com.hfg.bio.HfgBioXML;
007import com.hfg.bio.Strand;
008import com.hfg.bio.seq.format.feature.genbank.GenBankFeatureLocation;
009import com.hfg.bio.taxonomy.ncbi.NCBITaxon;
010import com.hfg.util.CompareUtil;
011import com.hfg.util.StringUtil;
012import com.hfg.xml.XMLTag;
013
014//------------------------------------------------------------------------------
015/**
016 Genomic location.
017 <p>
018 @author J. Alex Taylor, hairyfatguy.com
019 </p>
020 */
021//------------------------------------------------------------------------------
022// com.hfg XML/HTML Coding Library
023//
024// This library is free software; you can redistribute it and/or
025// modify it under the terms of the GNU Lesser General Public
026// License as published by the Free Software Foundation; either
027// version 2.1 of the License, or (at your option) any later version.
028//
029// This library is distributed in the hope that it will be useful,
030// but WITHOUT ANY WARRANTY; without even the implied warranty of
031// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
032// Lesser General Public License for more details.
033//
034// You should have received a copy of the GNU Lesser General Public
035// License along with this library; if not, write to the Free Software
036// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
037//
038// J. Alex Taylor, President, Founder, CEO, COO, CFO, OOPS hairyfatguy.com
039// jataylor@hairyfatguy.com
040//------------------------------------------------------------------------------
041
042public class GenomicLocation extends GenBankFeatureLocation
043{
044   private String mAssembly;
045   private String mBuild;
046   private NCBITaxon mTaxon;
047   private String mChromosome;
048   private Integer mChromosomeNum;
049   private String mContig;
050   private Strand mStrand;
051
052   private static final Pattern NUM_PATTERN = Pattern.compile("\\d+");
053
054   //###########################################################################
055   // CONSTRUCTORS
056   //###########################################################################
057
058   //--------------------------------------------------------------------------
059   public GenomicLocation()
060   {
061      super(null);
062   }
063
064   //--------------------------------------------------------------------------
065   public GenomicLocation(String inLocationString)
066   {
067      super(inLocationString);
068   }
069
070   //---------------------------------------------------------------------------
071   public GenomicLocation(XMLTag inXMLTag)
072   {
073      this();
074
075      inXMLTag.verifyTagName(HfgBioXML.GENOMIC_LOC_TAG);
076
077      setAssembly(inXMLTag.getAttributeValue(HfgBioXML.ASSEMBLY_ATT));
078      setBuild(inXMLTag.getAttributeValue(HfgBioXML.BUILD_ATT));
079
080      if (inXMLTag.hasAttribute(HfgBioXML.CHROMOSOME_ATT))
081      {
082         setChromosome(inXMLTag.getAttributeValue(HfgBioXML.CHROMOSOME_ATT));
083      }
084
085      if (inXMLTag.hasAttribute(HfgBioXML.CONTIG_ATT))
086      {
087         setContig(inXMLTag.getAttributeValue(HfgBioXML.CONTIG_ATT));
088      }
089
090      if (inXMLTag.hasAttribute(HfgBioXML.CONTIG_LENGTH_ATT))
091      {
092         setSeqLength(Integer.parseInt(inXMLTag.getAttributeValue(HfgBioXML.CONTIG_LENGTH_ATT)));
093      }
094
095      if (inXMLTag.hasAttribute(HfgBioXML.TAXON_ATT))
096      {
097         setTaxon(NCBITaxon.getByTaxonId(Integer.parseInt(inXMLTag.getAttributeValue(HfgBioXML.TAXON_ATT))));
098      }
099
100      if (inXMLTag.hasAttribute(HfgBioXML.STRAND_ATT))
101      {
102         setStrand(Strand.valueOf(inXMLTag.getAttributeValue(HfgBioXML.STRAND_ATT)));
103      }
104
105      append(inXMLTag.getUnescapedContent());
106   }
107
108   //###########################################################################
109   // PUBLIC METHODS
110   //###########################################################################
111
112   //--------------------------------------------------------------------------
113   @Override
114   public GenomicLocation clone()
115   {
116      GenomicLocation copy = (GenomicLocation) super.clone();
117
118      return copy;
119   }
120
121   //--------------------------------------------------------------------------
122   public XMLTag toXMLTag()
123   {
124      XMLTag tag = new XMLTag(HfgBioXML.GENOMIC_LOC_TAG);
125
126      if (StringUtil.isSet(getAssembly()))
127      {
128         tag.setAttribute(HfgBioXML.ASSEMBLY_ATT, getAssembly());
129      }
130
131      if (StringUtil.isSet(getBuild()))
132      {
133         tag.setAttribute(HfgBioXML.BUILD_ATT, getBuild());
134      }
135
136      if (getTaxon() != null)
137      {
138         tag.setAttribute(HfgBioXML.TAXON_ATT, getTaxon().getTaxonId());
139      }
140
141      if (getChromosome() != null)
142      {
143         tag.setAttribute(HfgBioXML.CHROMOSOME_ATT, getChromosome());
144      }
145
146      if (getContig() != null)
147      {
148         tag.setAttribute(HfgBioXML.CONTIG_ATT, getContig());
149      }
150
151      if (getSeqLength() != null)
152      {
153         tag.setAttribute(HfgBioXML.CONTIG_LENGTH_ATT, getSeqLength());
154      }
155
156      if (getStrand() != null)
157      {
158         tag.setAttribute(HfgBioXML.STRAND_ATT, getStrand());
159      }
160
161      tag.setContent(toString());
162
163      return tag;
164   }
165
166
167   //--------------------------------------------------------------------------
168   public GenomicLocation setAssembly(String inValue)
169   {
170      mAssembly = inValue;
171      return this;
172   }
173
174   //--------------------------------------------------------------------------
175   public String getAssembly()
176   {
177      return mAssembly;
178   }
179
180
181   //--------------------------------------------------------------------------
182   public GenomicLocation setBuild(String inValue)
183   {
184      mBuild = inValue;
185      return this;
186   }
187
188   //--------------------------------------------------------------------------
189   public String getBuild()
190   {
191      return mBuild;
192   }
193
194
195   //--------------------------------------------------------------------------
196   public GenomicLocation setChromosome(String inValue)
197   {
198      mChromosome = inValue;
199
200      if (inValue != null
201          && NUM_PATTERN.matcher(inValue).matches())
202      {
203         mChromosomeNum = Integer.parseInt(inValue);
204      }
205
206      return this;
207   }
208
209   //--------------------------------------------------------------------------
210   public String getChromosome()
211   {
212      return mChromosome;
213   }
214
215
216   //--------------------------------------------------------------------------
217   public GenomicLocation setContig(String inValue)
218   {
219      mContig = inValue;
220      return this;
221   }
222
223   //--------------------------------------------------------------------------
224   public String getContig()
225   {
226      return mContig;
227   }
228
229   //--------------------------------------------------------------------------
230   @Override
231   public GenomicLocation setSeqLength(Integer inValue)
232   {
233      return (GenomicLocation) super.setSeqLength(inValue);
234   }
235
236
237   //--------------------------------------------------------------------------
238   public GenomicLocation setTaxon(NCBITaxon inValue)
239   {
240      mTaxon = inValue;
241      return this;
242   }
243
244   //--------------------------------------------------------------------------
245   public NCBITaxon getTaxon()
246   {
247      return mTaxon;
248   }
249
250
251   //--------------------------------------------------------------------------
252   public GenomicLocation setStrand(Strand inValue)
253   {
254      mStrand = inValue;
255      return this;
256   }
257
258   //--------------------------------------------------------------------------
259   public Strand getStrand()
260   {
261      return mStrand;
262   }
263
264
265   //--------------------------------------------------------------------------
266   public int compareTo(GenomicLocation inObj2)
267   {
268      int result = -1;
269      if (inObj2 != null)
270      {
271         if (mChromosomeNum != null
272             && inObj2.mChromosomeNum != null)
273         {
274            result = CompareUtil.compare(mChromosomeNum, inObj2.mChromosomeNum);
275         }
276         else
277         {
278            result = CompareUtil.compare(getChromosome(), inObj2.getChromosome());
279            // Handle situation where one chromosome is called 'Unknown' and the other is called 'Un'
280            if (result != 0
281                && getChromosome() != null
282                && inObj2.getChromosome() != null
283                && ((getChromosome().equalsIgnoreCase("Unknown") || getChromosome().equalsIgnoreCase("Un"))
284                    && (inObj2.getChromosome().equalsIgnoreCase("Unknown") || inObj2.getChromosome().equalsIgnoreCase("Un"))))
285            {
286               result = 0;
287            }
288         }
289
290
291         if (0 == result)
292         {
293            result = CompareUtil.compare(getContig(), inObj2.getContig());
294         }
295
296         if (0 == result)
297         {
298            result = super.compareTo(inObj2);
299         }
300      }
301
302      return result;
303   }
304}