001package com.hfg.bio.seq.format.feature.uniprot;
002
003import com.hfg.bio.seq.SeqLocation;
004import com.hfg.bio.seq.format.feature.SeqFeatureLocation;
005import com.hfg.math.Range;
006import com.hfg.util.CompareUtil;
007import com.hfg.util.StringUtil;
008
009//------------------------------------------------------------------------------
010/**
011 UniProt feature location.
012 <div>
013 </div>
014 @author J. Alex Taylor, hairyfatguy.com
015 */
016//------------------------------------------------------------------------------
017// com.hfg XML/HTML Coding Library
018//
019// This library is free software; you can redistribute it and/or
020// modify it under the terms of the GNU Lesser General Public
021// License as published by the Free Software Foundation; either
022// version 2.1 of the License, or (at your option) any later version.
023//
024// This library is distributed in the hope that it will be useful,
025// but WITHOUT ANY WARRANTY; without even the implied warranty of
026// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
027// Lesser General Public License for more details.
028//
029// You should have received a copy of the GNU Lesser General Public
030// License along with this library; if not, write to the Free Software
031// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
032//
033// J. Alex Taylor, President, Founder, CEO, COO, CFO, OOPS hairyfatguy.com
034// jataylor@hairyfatguy.com
035//------------------------------------------------------------------------------
036
037public class UniProtFeatureLocation implements SeqFeatureLocation
038{
039   private String mStringValue;
040   private SeqLocation mSeqLocation;
041   private boolean mParsed;
042
043   //###########################################################################
044   // CONSTRUCTORS
045   //###########################################################################
046
047   //---------------------------------------------------------------------------
048   public UniProtFeatureLocation(String inFrom, String inTo)
049   {
050      mStringValue = inFrom + " - " + inTo;
051
052      // Example values: '1', '?', '<1', '>12'. Strip non-digits
053      String from = StringUtil.replaceAllRegexp(inFrom, "[^\\d]", "");
054      String to   = StringUtil.replaceAllRegexp(inTo, "[^\\d]", "");
055
056      mSeqLocation = new SeqLocation();
057      if (StringUtil.isSet(from))
058      {
059         mSeqLocation.setStart(Integer.parseInt(from));
060      }
061
062      if (StringUtil.isSet(from))
063      {
064         mSeqLocation.setEnd(Integer.parseInt(to));
065      }
066
067      mParsed = true;
068   }
069
070   //###########################################################################
071   // PUBLIC METHODS
072   //###########################################################################
073
074   //--------------------------------------------------------------------------
075   @Override
076   public UniProtFeatureLocation clone()
077   {
078      UniProtFeatureLocation copy;
079      try
080      {
081         copy = (UniProtFeatureLocation) super.clone();
082      }
083      catch (CloneNotSupportedException e)
084      {
085         throw new RuntimeException("Coding problem! CloneNotSupportedException should not be possible when cloning a "
086                                    + this.getClass().getSimpleName() + " object!", e);
087      }
088
089      return copy;
090   }
091
092
093   //---------------------------------------------------------------------------
094   public void append(String inAdditionalLocationData)
095   {
096      mStringValue = (StringUtil.isSet(mStringValue) ? mStringValue : "") + inAdditionalLocationData;
097      mParsed = false;
098   }
099
100   //---------------------------------------------------------------------------
101   public String eval(String inSequence)
102   {
103      if (! mParsed)
104      {
105         parse();
106      }
107
108      return inSequence.substring(mSeqLocation.getStart() - 1, mSeqLocation.getEnd());
109   }
110
111   //---------------------------------------------------------------------------
112   @Override
113   public String toString()
114   {
115      if (null == mStringValue)
116      {
117         String stringValue = null;
118
119         mStringValue = stringValue;
120      }
121
122      return (mStringValue != null ? mStringValue : mSeqLocation.toString());
123   }
124
125   //---------------------------------------------------------------------------
126   @Override
127   public boolean equals(Object inObj2)
128   {
129      return (inObj2 != null
130              && inObj2 instanceof SeqFeatureLocation
131              && 0 == compareTo(inObj2));
132   }
133
134   //---------------------------------------------------------------------------
135   @Override
136   public int hashCode()
137   {
138      return mStringValue.hashCode();
139   }
140
141   //---------------------------------------------------------------------------
142   @Override
143   public int compareTo(Object inObj2)
144   {
145      int result = 1;
146
147      if (inObj2 != null
148          && inObj2 instanceof SeqFeatureLocation)
149      {
150         SeqFeatureLocation seqLoc2 = (SeqFeatureLocation) inObj2;
151         result = CompareUtil.compare(toIntRange(), seqLoc2.toIntRange());
152
153         if (0 == result)
154         {
155            result = CompareUtil.compare(toString(), seqLoc2.toString());
156         }
157      }
158
159      return result;
160   }
161
162   //---------------------------------------------------------------------------
163   public Range<Integer> toIntRange()
164   {
165      if (! mParsed)
166      {
167         parse();
168      }
169
170      return mSeqLocation;
171   }
172
173   //###########################################################################
174   // PRIVATE METHODS
175   //###########################################################################
176
177   //--------------------------------------------------------------------------
178   private void parse()
179   {
180      // TODO
181   }
182}