001package com.hfg.bio.seq.pattern;
002
003import java.util.ArrayList;
004import java.util.List;
005
006import com.hfg.bio.seq.BioSequence;
007import com.hfg.bio.seq.SeqLocation;
008import com.hfg.util.collection.CollectionUtil;
009
010public interface SeqPatternMatcher<S extends BioSequence, T extends SeqPatternMatch>
011{
012   public S getTarget();
013   public SeqLocation getSeqLocation();
014
015   //---------------------------------------------------------------------------
016   public default T find()
017   {
018      return find(null);
019   }
020
021   public T find(SeqLocation inSeqLocation);
022
023   //---------------------------------------------------------------------------
024   public default T findLast()
025   {
026      return findLast(null);
027   }
028
029   public default T findLast(SeqLocation inSeqLocation)
030   {
031      List<T> matches = findAll(inSeqLocation);
032      return (CollectionUtil.hasValues(matches) ? matches.get(matches.size() - 1) : null);
033   }
034
035   //---------------------------------------------------------------------------
036   public default List<T> findAll()
037   {
038      return findAll(null);
039   }
040
041   //---------------------------------------------------------------------------
042   public default List<T> findAll(SeqLocation inSeqLocation)
043   {
044      List<T> matches = null;
045
046      SeqLocation seqLocation;
047      if (inSeqLocation != null)
048      {
049         seqLocation = inSeqLocation.clone();
050      }
051      else if (getSeqLocation() != null)
052      {
053         seqLocation = getSeqLocation().clone();
054      }
055      else
056      {
057         seqLocation = new SeqLocation(1, getTarget().length());
058      }
059
060      T match;
061      while ((match = find(seqLocation)) != null)
062      {
063         if (null == matches)
064         {
065            matches = new ArrayList<>();
066         }
067
068         matches.add(match);
069
070         seqLocation.setStart(match.getSeqLocation().getStart() + 1);
071
072         if (seqLocation.length() <= 0)
073         {
074            break;
075         }
076      }
077
078      return matches;
079   }
080
081}