001package com.hfg.bio.seq.pattern;
002
003import java.util.ArrayList;
004import java.util.Collection;
005import java.util.List;
006
007import com.hfg.bio.seq.Protein;
008import com.hfg.util.collection.CollectionUtil;
009
010//------------------------------------------------------------------------------
011/**
012 Examines a query sequence to find instances of the specified patterns.
013 <div>
014 @author J. Alex Taylor, hairyfatguy.com
015 </div>
016 */
017//------------------------------------------------------------------------------
018// com.hfg Library
019//
020// This library is free software; you can redistribute it and/or
021// modify it under the terms of the GNU Lesser General Public
022// License as published by the Free Software Foundation; either
023// version 2.1 of the License, or (at your option) any later version.
024//
025// This library is distributed in the hope that it will be useful,
026// but WITHOUT ANY WARRANTY; without even the implied warranty of
027// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
028// Lesser General Public License for more details.
029//
030// You should have received a copy of the GNU Lesser General Public
031// License along with this library; if not, write to the Free Software
032// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
033//
034// J. Alex Taylor, President, Founder, CEO, COO, CFO, OOPS hairyfatguy.com
035// jataylor@hairyfatguy.com
036//------------------------------------------------------------------------------
037
038public class ProteinPatternMatcher
039{
040   private List<ProteinPattern> mPatterns;
041
042   private boolean mIgnoreGaps = false;
043
044   //###########################################################################
045   // CONSTRUCTORS
046   //###########################################################################
047
048   //--------------------------------------------------------------------------
049   public ProteinPatternMatcher()
050   {
051   }
052
053   //--------------------------------------------------------------------------
054   public ProteinPatternMatcher(ProteinPattern inPattern)
055   {
056      addPattern(inPattern);
057   }
058
059   //###########################################################################
060   // PUBLIC METHODS
061   //###########################################################################
062
063   //--------------------------------------------------------------------------
064   public ProteinPatternMatcher addPattern(ProteinPattern inValue)
065   {
066      if (null == mPatterns)
067      {
068         mPatterns = new ArrayList<>(25);
069      }
070
071      mPatterns.add(inValue);
072      inValue.setIgnoreGaps(mIgnoreGaps);
073
074      return this;
075   }
076
077   //--------------------------------------------------------------------------
078   public ProteinPatternMatcher addPatterns(Collection<ProteinPattern> inValues)
079   {
080      if (CollectionUtil.hasValues(inValues))
081      {
082         for (ProteinPattern pattern : inValues)
083         {
084            addPattern(pattern);
085         }
086      }
087
088      return this;
089   }
090
091   //--------------------------------------------------------------------------
092   public ProteinPatternMatcher setIgnoreGaps(boolean inValue)
093   {
094      mIgnoreGaps = inValue;
095      if (CollectionUtil.hasValues(mPatterns))
096      {
097         for (ProteinPattern pattern : mPatterns)
098         {
099            pattern.setIgnoreGaps(inValue);
100         }
101      }
102
103      return this;
104   }
105
106   //--------------------------------------------------------------------------
107   public List<ProteinPatternMatch> match(Protein inQuery)
108   {
109      List<ProteinPatternMatch> matches = new ArrayList<>(25);
110
111      for (ProteinPattern proteinPattern : mPatterns)
112      {
113         List<Protein> chains = new ArrayList<>(5);
114         if (CollectionUtil.hasValues(inQuery.getChains()))
115         {
116            chains.addAll(inQuery.getChains());
117         }
118         else
119         {
120            chains.add(inQuery);
121         }
122
123         for (Protein chain : chains)
124         {
125            SeqPatternMatcher m = proteinPattern.matcher(chain);
126            List<ProteinPatternMatch> patternMatches = m.findAll();
127            if (CollectionUtil.hasValues(patternMatches))
128            {
129               matches.addAll(patternMatches);
130            }
131         }
132      }
133
134      return matches;
135   }
136}