001package com.hfg.bio.taxonomy.ncbi;
002
003import java.io.InputStream;
004import java.util.HashMap;
005import java.util.Map;
006import java.util.zip.GZIPInputStream;
007
008import com.hfg.bio.seq.translation.CodonTable;
009import com.hfg.bio.seq.translation.TranslationTable;
010import com.hfg.exception.ProgrammingException;
011import com.hfg.util.io.StreamUtil;
012import com.hfg.xml.XMLTag;
013
014//------------------------------------------------------------------------------
015/**
016 Reference genetic code information used by the NCBI.
017 <div>
018  @author J. Alex Taylor, hairyfatguy.com
019 </div>
020 */
021//------------------------------------------------------------------------------
022// com.hfg 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 NCBIGeneticCode implements TranslationTable
043{
044   private int        mNCBIId;
045   private CodonTable mCodonTable;
046
047   private String mRsrc;
048
049   private static Map<Integer, NCBIGeneticCode> sLookupMap = new HashMap<>(34);
050
051   public static final NCBIGeneticCode UNSPECIFIED                    = new NCBIGeneticCode( 0, "rsrc/cod_unspecified.xml.gz");
052   public static final NCBIGeneticCode STANDARD                       = new NCBIGeneticCode( 1, "rsrc/cod_standard.xml.gz");
053   public static final NCBIGeneticCode VERTEBRATE_MITO                = new NCBIGeneticCode( 2, "rsrc/cod_vertebrate_mito.xml.gz");
054   public static final NCBIGeneticCode YEAST_MITO                     = new NCBIGeneticCode( 3, "rsrc/cod_yeast_mito.xml.gz");
055   public static final NCBIGeneticCode MOLD_MITO                      = new NCBIGeneticCode( 4, "rsrc/cod_mold_mito.xml.gz");
056   public static final NCBIGeneticCode INVERTEBRATE_MITO              = new NCBIGeneticCode( 5, "rsrc/cod_invertebrate_mito.xml.gz");
057   public static final NCBIGeneticCode CILIATE_NUCLEAR                = new NCBIGeneticCode( 6, "rsrc/cod_ciliate_nuclear.xml.gz");
058   public static final NCBIGeneticCode ECHINODERM_MITO                = new NCBIGeneticCode( 9, "rsrc/cod_echinoderm_mito.xml.gz");
059   public static final NCBIGeneticCode EUPLOTID_NUCLEAR               = new NCBIGeneticCode(10, "rsrc/cod_euplotid_nuclear.xml.gz");
060   public static final NCBIGeneticCode BACTERIAL                      = new NCBIGeneticCode(11, "rsrc/cod_bacterial.xml.gz");
061   public static final NCBIGeneticCode ALT_YEAST_NUCLEAR              = new NCBIGeneticCode(12, "rsrc/cod_alt_yeast_nuclear.xml.gz");
062   public static final NCBIGeneticCode ASCIDIAN_MITO                  = new NCBIGeneticCode(13, "rsrc/cod_ascidian_mito.xml.gz");
063   public static final NCBIGeneticCode ALT_FLATWORM_MITO              = new NCBIGeneticCode(14, "rsrc/cod_alt_flatworm_mito.xml.gz");
064   public static final NCBIGeneticCode CHLOROPHYCEAN_MITO             = new NCBIGeneticCode(16, "rsrc/cod_chlorophycean_mito.xml.gz");
065   public static final NCBIGeneticCode TREMATODE_MITO                 = new NCBIGeneticCode(21, "rsrc/cod_trematode_mito.xml.gz");
066   public static final NCBIGeneticCode SCENEDESMUS_MITO               = new NCBIGeneticCode(22, "rsrc/cod_scenedesmus_obliquus_mito.xml.gz");
067   public static final NCBIGeneticCode THRAUSTOCHYTRIUM_MITO          = new NCBIGeneticCode(23, "rsrc/cod_thraustochytrium_mito.xml.gz");
068   public static final NCBIGeneticCode RHABDOPLEURIDAE_MITO           = new NCBIGeneticCode(24, "rsrc/cod_rhabdopleuridae_mito.xml.gz");
069   public static final NCBIGeneticCode CANDIDATE_DIVISION_SR1_MITO    = new NCBIGeneticCode(25, "rsrc/cod_candidate_div_sr1_mito.xml.gz");
070   public static final NCBIGeneticCode PACHYSOLEN_TANNOPHILUS_NUCLEAR = new NCBIGeneticCode(26, "rsrc/cod_pachysolen_tannophilus_nuclear.xml.gz");
071   public static final NCBIGeneticCode KARYORELICT_NUCLEAR            = new NCBIGeneticCode(27, "rsrc/cod_karyorelict_nuclear.xml.gz");
072   public static final NCBIGeneticCode CONDYLOSTOMA_NUCLEAR           = new NCBIGeneticCode(28, "rsrc/cod_condylostoma_nuclear.xml.gz");
073   public static final NCBIGeneticCode MESODINIUM_NUCLEAR             = new NCBIGeneticCode(29, "rsrc/cod_mesodinium_nuclear.xml.gz");
074   public static final NCBIGeneticCode PERITRICH_NUCLEAR              = new NCBIGeneticCode(30, "rsrc/cod_peritrich_nuclear.xml.gz");
075   public static final NCBIGeneticCode BLASTOCRITHIDIA_NUCLEAR        = new NCBIGeneticCode(31, "rsrc/cod_blastocrithidia_nuclear.xml.gz");
076   public static final NCBIGeneticCode BALANOPHORACEAE_NUCLEAR        = new NCBIGeneticCode(32, "rsrc/cod_balanophoraceae_nuclear.xml.gz");
077   public static final NCBIGeneticCode CEPHALODISCIDAE_MITO           = new NCBIGeneticCode(33, "rsrc/cod_cephalodiscidae_mito.xml.gz");
078
079   //###########################################################################
080   // CONSTRUCTORS
081   //###########################################################################
082
083   //---------------------------------------------------------------------------
084   private NCBIGeneticCode(int inId, String inRsrc)
085   {
086      mNCBIId = inId;
087      mRsrc = inRsrc;
088
089      InputStream stream = getClass().getResourceAsStream(mRsrc);
090      if (null == stream)
091      {
092         throw new ProgrammingException("The rsrc " + mRsrc + " couldn't be found!?");
093      }
094
095      try
096      {
097         if (mRsrc.endsWith(".gz"))
098         {
099            stream = new GZIPInputStream(stream);
100         }
101
102         mCodonTable = new CodonTable(new XMLTag(stream));
103      }
104      catch (Exception e)
105      {
106         throw new ProgrammingException(e);
107      }
108      finally
109      {
110         StreamUtil.close(stream);
111      }
112
113      sLookupMap.put(inId, this);
114   }
115
116   //###########################################################################
117   // PUBLIC METHODS
118   //###########################################################################
119
120   //---------------------------------------------------------------------------
121   public static NCBIGeneticCode getById(int inId)
122   {
123      return sLookupMap.get(inId);
124   }
125
126   //---------------------------------------------------------------------------
127   @Override
128   public String toString()
129   {
130      return name();
131   }
132
133   //---------------------------------------------------------------------------
134   public String name()
135   {
136      return mCodonTable.name();
137   }
138
139   //---------------------------------------------------------------------------
140   public int getId()
141   {
142      return mNCBIId;
143   }
144
145   //---------------------------------------------------------------------------
146   /**
147    Converts the specified codon (3 nucleotides) into a character representing
148    the corresponding amino acid.
149    @param inCodon 3-letter nucleotide (DNA or RNA) string to be translated
150    @return single letter amino acid
151    */
152   public char translateCodon(String inCodon)
153   {
154      return mCodonTable.translateCodon(inCodon);
155   }
156}