001package com.hfg.bio.seq.pattern;
002
003import java.util.Comparator;
004
005import com.hfg.bio.Strand;
006import com.hfg.bio.seq.SeqLocation;
007import com.hfg.util.CompareUtil;
008
009//------------------------------------------------------------------------------
010/**
011 Container for a nucleotide pattern match.
012 <div>
013 @author J. Alex Taylor, hairyfatguy.com
014 </div>
015 */
016//------------------------------------------------------------------------------
017// com.hfg 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 NucleotidePatternMatch extends SeqPatternMatch
038{
039   private Strand            mStrand;
040
041   public static final Comparator<NucleotidePatternMatch> LOCATION_COMPARATOR = new NucleotidePatternMatch().new LocationComparator();
042   public static final Comparator<NucleotidePatternMatch> MISMATCH_COMPARATOR = new NucleotidePatternMatch().new MismatchComparator();
043
044   //###########################################################################
045   // CONSTRUCTORS
046   //###########################################################################
047
048   //--------------------------------------------------------------------------
049   private NucleotidePatternMatch()
050   {
051
052   }
053
054   //--------------------------------------------------------------------------
055   public NucleotidePatternMatch(NucleotidePattern inPattern, String inSeq, SeqLocation inLocation)
056   {
057      super(inPattern, inSeq, inLocation);
058   }
059
060   //###########################################################################
061   // PUBLIC METHODS
062   //###########################################################################
063
064   //--------------------------------------------------------------------------
065   @Override
066   public boolean equals(Object inObj2)
067   {
068      boolean result = false;
069
070      if (inObj2 instanceof NucleotidePatternMatch)
071      {
072         NucleotidePatternMatch match2 = (NucleotidePatternMatch) inObj2;
073
074         if (getPattern().equals(match2.getPattern())
075               && getSeqLocation().equals(match2.getSeqLocation())
076               && 0 == CompareUtil.compare(getStrand(), match2.getStrand()))
077         {
078            result = true;
079         }
080      }
081
082      return result;
083   }
084
085   //--------------------------------------------------------------------------
086   @Override
087   public int hashCode()
088   {
089      int result = super.hashCode();
090
091      if (getStrand() != null)
092      {
093         result += 31 * getStrand().hashCode();
094      }
095
096      return result;
097   }
098
099   //--------------------------------------------------------------------------
100   @Override
101   public NucleotidePattern getPattern()
102   {
103      return (NucleotidePattern) super.getPattern();
104   }
105
106
107   //--------------------------------------------------------------------------
108   public NucleotidePatternMatch setSequence(String inValue)
109   {
110      return (NucleotidePatternMatch) super.setSequence(inValue);
111   }
112
113
114   //--------------------------------------------------------------------------
115   public NucleotidePatternMatch setStrand(Strand inValue)
116   {
117      mStrand = inValue;
118      return this;
119   }
120
121   //--------------------------------------------------------------------------
122   public Strand getStrand()
123   {
124      return mStrand;
125   }
126
127   //###########################################################################
128   // INNER CLASS
129   //###########################################################################
130
131   private class LocationComparator implements Comparator<NucleotidePatternMatch>
132   {
133      //--------------------------------------------------------------------------
134      public int compare(NucleotidePatternMatch inObj1, NucleotidePatternMatch inObj2)
135      {
136         return CompareUtil.compare(inObj1.getSeqLocation(), inObj2.getSeqLocation());
137      }
138   }
139
140   //###########################################################################
141   // INNER CLASS
142   //###########################################################################
143
144   private class MismatchComparator implements Comparator<NucleotidePatternMatch>
145   {
146      //--------------------------------------------------------------------------
147      public int compare(NucleotidePatternMatch inObj1, NucleotidePatternMatch inObj2)
148      {
149         int result = CompareUtil.compare(inObj1.getNumMismatches(), inObj2.getNumMismatches());
150
151         if (0 == result)
152         {
153            result = CompareUtil.compare(inObj1.getSeqLocation(), inObj2.getSeqLocation());
154         }
155
156         return result;
157      }
158   }
159
160}