All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.hfg.bio.seq.Gene Maven / Gradle / Ivy

There is a newer version: 20240423
Show newest version
package com.hfg.bio.seq;

import java.awt.*;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

import com.hfg.bio.Strand;
import com.hfg.bio.taxonomy.NCBITaxon;
import com.hfg.html.Link;
import com.hfg.math.Range;
import com.hfg.util.collection.CollectionUtil;

//------------------------------------------------------------------------------
/**
 * A gene representation.
 *
 * @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 Gene implements Comparable
{
   //##########################################################################
   // PRIVATE FIELDS
   //##########################################################################


   private String     mId;
   private String     mDescription;
   private List mURLs;
   private Integer    mStartLocation;
   private Integer    mEndLocation;
   private GenomicLocation mGenomicLocation;
   private Strand     mStrand;
   private List mExons;
   private NCBITaxon  mSpecies;
   private Color      mColor;
   private SeqMappingCoverage mSeqMappingCoverage;

   //##########################################################################
   // CONSTRUCTORS
   //##########################################################################

   //--------------------------------------------------------------------------
   public Gene()
   {
   }


   //##########################################################################
   // PUBLIC METHODS
   //##########################################################################


   //--------------------------------------------------------------------------
   public Gene setLocation(GenomicLocation inValue)
   {
      mGenomicLocation = inValue;
      return this;
   }

   //--------------------------------------------------------------------------
   public GenomicLocation getLocation()
   {
      return mGenomicLocation;
   }

   //--------------------------------------------------------------------------
   public Gene addExons(Collection inValues)
   {
      if (CollectionUtil.hasValues(inValues))
      {
         for (Exon exon : inValues)
         {
            addExon(exon);
         }
      }

      return this;
   }

   //--------------------------------------------------------------------------
   public Gene addExon(Exon inValue)
   {
      if (null == mExons)
      {
         mExons = new ArrayList<>();
      }

      mExons.add(inValue);

      if (null == getStrand()
          && inValue.getStrand() != null)
      {
         setStrand(inValue.getStrand());
      }

      if (null == getStartLocation()
          || inValue.getLeft() < getStartLocation())
      {
         setStartLocation(inValue.getLeft());
      }

      if (null == getEndLocation()
          || inValue.getRight() > getEndLocation())
      {
         setEndLocation(inValue.getRight());
      }

      return this;
   }

   //--------------------------------------------------------------------------
   public List getExons()
   {
      return mExons;
   }

   //--------------------------------------------------------------------------
   public Gene setId(String inValue)
   {
      mId = inValue;
      return this;
   }

   //--------------------------------------------------------------------------
   public String getId()
   {
      return mId;
   }

   //--------------------------------------------------------------------------
   public Gene setDescription(String inValue)
   {
      mDescription = inValue;
      return this;
   }

   //--------------------------------------------------------------------------
   public String getDescription()
   {
      return mDescription;
   }


   //--------------------------------------------------------------------------
   public void setSeqMappingCoverage(SeqMappingCoverage inValue)
   {
      mSeqMappingCoverage = inValue;
   }

   //--------------------------------------------------------------------------
   public SeqMappingCoverage getSeqMappingCoverage()
   {
      return mSeqMappingCoverage;
   }


   //--------------------------------------------------------------------------
   public Gene setSpecies(NCBITaxon inValue)
   {
      mSpecies = inValue;
      return this;
   }

   //--------------------------------------------------------------------------
   public NCBITaxon getSpecies()
   {
      return mSpecies;
   }


   //--------------------------------------------------------------------------
   /**
    Add a URL to the gene. If more than one URL is defined, a popup menu will be
    shown.
    @param inValue the link to associate with the gene
    */
   public Gene addURL(Link inValue)
   {
      if (null == mURLs)
      {
         mURLs = new ArrayList<>(2);
      }

      mURLs.add(inValue);
      return this;
   }

   //--------------------------------------------------------------------------
   public List getURLs()
   {
      return mURLs;
   }


   //--------------------------------------------------------------------------
   /**
    Basepair location of the start of the gene.
    @return the 1-based location
    */
   public Integer getStartLocation()
   {
      return mStartLocation != null ? mStartLocation : (mGenomicLocation != null ? mGenomicLocation.toIntRange().getStart() : null);
   }

   //--------------------------------------------------------------------------
   public Gene setStartLocation(int inValue)
   {
      mStartLocation = inValue;
      return this;
   }

   //--------------------------------------------------------------------------
   public Integer getEndLocation()
   {
      return mEndLocation != null ? mEndLocation : (mGenomicLocation != null ? mGenomicLocation.toIntRange().getEnd() : null);
   }

   //--------------------------------------------------------------------------
   public Gene setEndLocation(int inValue)
   {
      mEndLocation = inValue;
      return this;
   }


   //--------------------------------------------------------------------------
   /**
    The leftmost basepair of the gene (the start location if the gene is on the
    forward strand or the end location if the gene is on the reverse strand).
    @return the 1-based left location
    */
   public int getLeft()
   {
      return Math.min(getStartLocation(), getEndLocation());
   }

   //--------------------------------------------------------------------------
   /**
    The rightmost basepair of the gene (the end location if the gene is on the
    forward strand or the start location if the gene is on the reverse strand).
    @return the 1-based right location
    */
   public int getRight()
   {
      return Math.max(getStartLocation(), getEndLocation());
   }

   //--------------------------------------------------------------------------
   public Gene setStrand(Strand inValue)
   {
      mStrand = inValue;
      return this;
   }

   //--------------------------------------------------------------------------
   public Strand getStrand()
   {
      return mStrand != null ? mStrand : (mGenomicLocation != null ? mGenomicLocation.getStrand() : null);
   }


   //--------------------------------------------------------------------------
   public Gene setColor(Color inValue)
   {
      mColor = inValue;
      return this;
   }

   //--------------------------------------------------------------------------
   public Color getColor()
   {
      return mColor;
   }



   //--------------------------------------------------------------------------
   public int compareTo(Object inObj2)
   {
      int returnValue = -1;

      if (inObj2 instanceof Gene)
      {
         Gene gene2 = (Gene) inObj2;

         if (mStartLocation > gene2.mStartLocation)
         {
            returnValue = 1;
         }
         else if (mStartLocation < gene2.mStartLocation)
         {
            returnValue = -1;
         }
         else
         {
            if (mEndLocation > gene2.mEndLocation)
            {
               returnValue = 1;
            }
            else if (mEndLocation < gene2.mEndLocation)
            {
               returnValue = -1;
            }
         }
      }

      return returnValue;
   }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy