001package com.hfg.bio.seq;
002
003import java.awt.*;
004import java.util.ArrayList;
005import java.util.Collection;
006import java.util.List;
007
008import com.hfg.bio.Strand;
009import com.hfg.bio.taxonomy.ncbi.NCBITaxon;
010import com.hfg.html.Link;
011import com.hfg.util.collection.CollectionUtil;
012
013//------------------------------------------------------------------------------
014/**
015 * A gene representation.
016 *
017 * @author J. Alex Taylor, hairyfatguy.com
018 */
019//------------------------------------------------------------------------------
020// com.hfg XML/HTML Coding Library
021//
022// This library is free software; you can redistribute it and/or
023// modify it under the terms of the GNU Lesser General Public
024// License as published by the Free Software Foundation; either
025// version 2.1 of the License, or (at your option) any later version.
026//
027// This library is distributed in the hope that it will be useful,
028// but WITHOUT ANY WARRANTY; without even the implied warranty of
029// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
030// Lesser General Public License for more details.
031//
032// You should have received a copy of the GNU Lesser General Public
033// License along with this library; if not, write to the Free Software
034// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
035//
036// J. Alex Taylor, President, Founder, CEO, COO, CFO, OOPS hairyfatguy.com
037// jataylor@hairyfatguy.com
038//------------------------------------------------------------------------------
039
040public class Gene implements Comparable
041{
042   //##########################################################################
043   // PRIVATE FIELDS
044   //##########################################################################
045
046
047   private String     mId;
048   private String     mDescription;
049   private List<Link> mURLs;
050   private Integer    mStartLocation;
051   private Integer    mEndLocation;
052   private GenomicLocation mGenomicLocation;
053   private Strand     mStrand;
054   private List<Exon> mExons;
055   private NCBITaxon  mSpecies;
056   private Color      mColor;
057   private SeqMappingCoverage mSeqMappingCoverage;
058
059   //##########################################################################
060   // CONSTRUCTORS
061   //##########################################################################
062
063   //--------------------------------------------------------------------------
064   public Gene()
065   {
066   }
067
068
069   //##########################################################################
070   // PUBLIC METHODS
071   //##########################################################################
072
073
074   //--------------------------------------------------------------------------
075   public Gene setLocation(GenomicLocation inValue)
076   {
077      mGenomicLocation = inValue;
078      return this;
079   }
080
081   //--------------------------------------------------------------------------
082   public GenomicLocation getLocation()
083   {
084      return mGenomicLocation;
085   }
086
087   //--------------------------------------------------------------------------
088   public Gene addExons(Collection<? extends Exon> inValues)
089   {
090      if (CollectionUtil.hasValues(inValues))
091      {
092         for (Exon exon : inValues)
093         {
094            addExon(exon);
095         }
096      }
097
098      return this;
099   }
100
101   //--------------------------------------------------------------------------
102   public Gene addExon(Exon inValue)
103   {
104      if (null == mExons)
105      {
106         mExons = new ArrayList<>();
107      }
108
109      mExons.add(inValue);
110
111      if (null == getStrand()
112          && inValue.getStrand() != null)
113      {
114         setStrand(inValue.getStrand());
115      }
116
117      if (null == getStartLocation()
118          || inValue.getLeft() < getStartLocation())
119      {
120         setStartLocation(inValue.getLeft());
121      }
122
123      if (null == getEndLocation()
124          || inValue.getRight() > getEndLocation())
125      {
126         setEndLocation(inValue.getRight());
127      }
128
129      return this;
130   }
131
132   //--------------------------------------------------------------------------
133   public List<Exon> getExons()
134   {
135      return mExons;
136   }
137
138   //--------------------------------------------------------------------------
139   public Gene setId(String inValue)
140   {
141      mId = inValue;
142      return this;
143   }
144
145   //--------------------------------------------------------------------------
146   public String getId()
147   {
148      return mId;
149   }
150
151   //--------------------------------------------------------------------------
152   public Gene setDescription(String inValue)
153   {
154      mDescription = inValue;
155      return this;
156   }
157
158   //--------------------------------------------------------------------------
159   public String getDescription()
160   {
161      return mDescription;
162   }
163
164
165   //--------------------------------------------------------------------------
166   public void setSeqMappingCoverage(SeqMappingCoverage inValue)
167   {
168      mSeqMappingCoverage = inValue;
169   }
170
171   //--------------------------------------------------------------------------
172   public SeqMappingCoverage getSeqMappingCoverage()
173   {
174      return mSeqMappingCoverage;
175   }
176
177
178   //--------------------------------------------------------------------------
179   public Gene setSpecies(NCBITaxon inValue)
180   {
181      mSpecies = inValue;
182      return this;
183   }
184
185   //--------------------------------------------------------------------------
186   public NCBITaxon getSpecies()
187   {
188      return mSpecies;
189   }
190
191
192   //--------------------------------------------------------------------------
193   /**
194    Add a URL to the gene. If more than one URL is defined, a popup menu will be
195    shown.
196    @param inValue the link to associate with the gene
197    */
198   public Gene addURL(Link inValue)
199   {
200      if (null == mURLs)
201      {
202         mURLs = new ArrayList<>(2);
203      }
204
205      mURLs.add(inValue);
206      return this;
207   }
208
209   //--------------------------------------------------------------------------
210   public List<Link> getURLs()
211   {
212      return mURLs;
213   }
214
215
216   //--------------------------------------------------------------------------
217   /**
218    Basepair location of the start of the gene.
219    @return the 1-based location
220    */
221   public Integer getStartLocation()
222   {
223      return mStartLocation != null ? mStartLocation : (mGenomicLocation != null ? mGenomicLocation.toIntRange().getStart() : null);
224   }
225
226   //--------------------------------------------------------------------------
227   public Gene setStartLocation(int inValue)
228   {
229      mStartLocation = inValue;
230      return this;
231   }
232
233   //--------------------------------------------------------------------------
234   public Integer getEndLocation()
235   {
236      return mEndLocation != null ? mEndLocation : (mGenomicLocation != null ? mGenomicLocation.toIntRange().getEnd() : null);
237   }
238
239   //--------------------------------------------------------------------------
240   public Gene setEndLocation(int inValue)
241   {
242      mEndLocation = inValue;
243      return this;
244   }
245
246
247   //--------------------------------------------------------------------------
248   /**
249    The leftmost basepair of the gene (the start location if the gene is on the
250    forward strand or the end location if the gene is on the reverse strand).
251    @return the 1-based left location
252    */
253   public int getLeft()
254   {
255      return Math.min(getStartLocation(), getEndLocation());
256   }
257
258   //--------------------------------------------------------------------------
259   /**
260    The rightmost basepair of the gene (the end location if the gene is on the
261    forward strand or the start location if the gene is on the reverse strand).
262    @return the 1-based right location
263    */
264   public int getRight()
265   {
266      return Math.max(getStartLocation(), getEndLocation());
267   }
268
269   //--------------------------------------------------------------------------
270   public Gene setStrand(Strand inValue)
271   {
272      mStrand = inValue;
273      return this;
274   }
275
276   //--------------------------------------------------------------------------
277   public Strand getStrand()
278   {
279      return mStrand != null ? mStrand : (mGenomicLocation != null ? mGenomicLocation.getStrand() : null);
280   }
281
282
283   //--------------------------------------------------------------------------
284   public Gene setColor(Color inValue)
285   {
286      mColor = inValue;
287      return this;
288   }
289
290   //--------------------------------------------------------------------------
291   public Color getColor()
292   {
293      return mColor;
294   }
295
296
297
298   //--------------------------------------------------------------------------
299   public int compareTo(Object inObj2)
300   {
301      int returnValue = -1;
302
303      if (inObj2 instanceof Gene)
304      {
305         Gene gene2 = (Gene) inObj2;
306
307         if (mStartLocation > gene2.mStartLocation)
308         {
309            returnValue = 1;
310         }
311         else if (mStartLocation < gene2.mStartLocation)
312         {
313            returnValue = -1;
314         }
315         else
316         {
317            if (mEndLocation > gene2.mEndLocation)
318            {
319               returnValue = 1;
320            }
321            else if (mEndLocation < gene2.mEndLocation)
322            {
323               returnValue = -1;
324            }
325         }
326      }
327
328      return returnValue;
329   }
330
331}