001package com.hfg.bio;
002
003import com.hfg.bio.seq.format.feature.FeatureQualifier;
004import com.hfg.bio.seq.format.feature.genbank.GenBankFeatureQualifierName;
005import com.hfg.util.CompareUtil;
006import com.hfg.util.StringUtil;
007
008//------------------------------------------------------------------------------
009/**
010 * Simple container for a database cross-reference.
011 * <div>
012 *  @author J. Alex Taylor, hairyfatguy.com
013 * </div>
014 * */
015//------------------------------------------------------------------------------
016// com.hfg Library
017//
018// This library is free software; you can redistribute it and/or
019// modify it under the terms of the GNU Lesser General Public
020// License as published by the Free Software Foundation; either
021// version 2.1 of the License, or (at your option) any later version.
022//
023// This library is distributed in the hope that it will be useful,
024// but WITHOUT ANY WARRANTY; without even the implied warranty of
025// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
026// Lesser General Public License for more details.
027//
028// You should have received a copy of the GNU Lesser General Public
029// License along with this library; if not, write to the Free Software
030// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
031//
032// J. Alex Taylor, President, Founder, CEO, COO, CFO, OOPS hairyfatguy.com
033// jataylor@hairyfatguy.com
034//------------------------------------------------------------------------------
035
036public class DbXref implements Comparable<DbXref>
037{
038   private String mDB;
039   private String mId;
040   private String mDescription;
041
042   //###########################################################################
043   // CONSTRUCTORS
044   //###########################################################################
045
046   //--------------------------------------------------------------------------
047   public DbXref(String inDB, String inId)
048   {
049      mDB = inDB;
050      mId = inId;
051   }
052
053   //--------------------------------------------------------------------------
054   /**
055    * If the qualifier is 'db_xref', this method can be used to generate a DbXref
056    * from the qualifier value.
057    * @throws RuntimeException if the qualifier name is not 'db_xref' or the value
058    *         is not formatted as db:id
059    */
060   public DbXref(FeatureQualifier inFeatureQualifier)
061   {
062      int index = inFeatureQualifier.getValue().indexOf(":");
063      if (index <= 0
064            || ! inFeatureQualifier.name().equals(GenBankFeatureQualifierName.db_xref.name()))
065      {
066         throw new RuntimeException("Qualifier " + StringUtil.singleQuote(toString()) + " cannot be converted to a DbXref!");
067      }
068
069      mDB = inFeatureQualifier.getValue().substring(0, index);
070      mId = inFeatureQualifier.getValue().substring(index + 1);
071      // Shouldn't have to do this but I've
072      // seen cases like this: /db_xref="HGNC:HGNC:5709"
073      if (mId.contains(":"))
074      {
075         mId = mId.substring(mId.lastIndexOf(":") + 1);
076      }
077   }
078
079   //###########################################################################
080   // PUBLIC METHODS
081   //###########################################################################
082
083   //--------------------------------------------------------------------------
084   public String getDB()
085   {
086      return mDB;
087   }
088
089   //--------------------------------------------------------------------------
090   public String getId()
091   {
092      return mId;
093   }
094
095   //--------------------------------------------------------------------------
096   public DbXref setDescription(String inValue)
097   {
098      mDescription = inValue;
099      return this;
100   }
101
102   //--------------------------------------------------------------------------
103   public String getDescription()
104   {
105      return mDescription;
106   }
107
108   //--------------------------------------------------------------------------
109   @Override
110   public String toString()
111   {
112      return mDB + ":" + mId;
113   }
114
115   //---------------------------------------------------------------------------
116   @Override
117   public boolean equals(Object inObj2)
118   {
119      return (inObj2 != null
120              && inObj2 instanceof DbXref
121              && 0 == compareTo((DbXref) inObj2));
122   }
123
124   //---------------------------------------------------------------------------
125   @Override
126   public int hashCode()
127   {
128      return toString().hashCode();
129   }
130
131   //---------------------------------------------------------------------------
132   @Override
133   public int compareTo(DbXref inObj2)
134   {
135      int result = 1;
136
137      if (inObj2 != null)
138      {
139         result = CompareUtil.compare(toString(), inObj2.toString());
140      }
141
142      return result;
143   }
144}