001/*
002 *                    BioJava development code
003 *
004 * This code may be freely distributed and modified under the
005 * terms of the GNU Lesser General Public Licence.  This should
006 * be distributed with the code.  If you do not have a copy,
007 * see:
008 *
009 *      http://www.gnu.org/copyleft/lesser.html
010 *
011 * Copyright for this code is held jointly by the individual
012 * authors.  These should be listed in @author doc comments.
013 *
014 * For more information on the BioJava project and its aims,
015 * or to join the biojava-l mailing list, visit the home page
016 * at:
017 *
018 *      http://www.biojava.org/
019 *
020 */
021
022package org.biojava.bio.program.gff;
023
024import org.biojava.bio.seq.StrandedFeature;
025
026/**
027 * A filter that will accept or reject a <span class="type">GFFEntry</span>.
028 *
029 * @author Matthew Pocock
030 * @author Keith James (docs)
031 */
032public interface GFFRecordFilter {
033  /**
034   * Return whether or not to accept <span class="arg">record</span>.
035   *
036   * @param record the <span class="type">GFFRecord</span> to filter
037   * @return <span class="kw">true</span> if <span class="arg">record</span>
038   *         should be accepted or <span class="kw">false</span> otherwise
039   */
040  boolean accept(GFFRecord record);
041  
042  /**
043   * A <span class="type">GFFRecordFilter</span> that accepts everything.
044   */
045  final static GFFRecordFilter ACCEPT_ALL = new AcceptAll();
046  
047  /**
048   * Implementation of <span class="type">GFFRecordFilter</span> that accepts everything.
049   *
050   * @author Matthew Pocock
051   */
052  public class AcceptAll implements GFFRecordFilter {
053    /**
054     * @return <span class="kw">true</span>
055     */
056    public boolean accept(GFFRecord record) {
057      return true;
058    }
059  }
060  
061  /**
062   * Implementation of <span class="type">GFFRecordFilter</span> that accepts
063   * records based upon the sequence name.
064   *
065   * @author Matthew Pocock
066   */
067  public class SequenceFilter implements GFFRecordFilter {
068    /**
069     * The sequence name to accept.
070     */
071    private String seqName;
072
073    public SequenceFilter() {}
074    public SequenceFilter(String seqName) { setSeqName(seqName); }
075    
076    /**
077     * Retrieve the current sequence name.
078     *
079     * @return the sequence name <span class="type">String</span>
080     */
081    public String getSeqName() {
082      return seqName;
083    }
084    
085    /**
086     * Set the sequence name to <span class="arg">seqName</span>.
087     *
088     * @param seqName the new sequence name to match
089     */
090    public void setSeqName(String seqName) {
091      this.seqName = seqName;
092    }
093    
094    /**
095     * @return <span class="arg">record</span>.
096     * <span class="method">getSeqName</span><code>()</code> <code>==</code>
097     * <span class="const">this</span>.<span class="method">getSeqName</span><code>()</code>
098     */
099    public boolean accept(GFFRecord record) {
100      return record.getSeqName().equals(seqName);
101    }
102  }
103
104  public static class StrandFilter implements GFFRecordFilter {
105    private StrandedFeature.Strand strand;
106
107    public StrandFilter() {}
108    public StrandFilter(StrandedFeature.Strand strand) { setStrand(strand); }
109
110    public void setStrand(StrandedFeature.Strand strand) {
111      this.strand = strand;
112    }
113
114    public StrandedFeature.Strand getStrand() {
115      return this.strand;
116    }
117
118    public boolean accept(GFFRecord record) {
119      return record.getStrand().equals(strand);
120    }
121  }
122  
123  /**
124   * Implementation of <span class="type">GFFRecordFilter</span> that accepts
125   * records based upon the source field.
126   *
127   * @author Matthew Pocock
128   */
129  public static class SourceFilter implements GFFRecordFilter {
130    private String source;
131    
132    public SourceFilter() {}
133    public SourceFilter(String source) { setSource(source); }
134
135    /**
136     * Retrieve the current source.
137     *
138     * @return the source <span class="type">String</span>
139     */
140    public String getSource() {
141      return source;
142    }
143    
144    /**
145     * Set the source to <span class="arg">source</span>.
146     *
147     * @param source the new source to match
148     */
149    public void setSource(String source) {
150      this.source = source;
151    }
152    
153    /**
154     * @return <span class="arg">record</span>.
155     * <span class="method">getSource</span><code>()</code> <code>==</code>
156     * <span class="const">this</span>.<span class="method">getSource</span><code>()</code>
157     */
158    public boolean accept(GFFRecord record) {
159      return record.getSource().equals(source);
160    }
161  }
162  
163  /**
164   * Implementation of <span class="type">GFFRecordFilter</span> that accepts
165   * records based upon the feature field.
166   *
167   * @author Matthew Pocock
168   */
169  public class FeatureFilter implements GFFRecordFilter {
170    private String feature;
171    
172    public FeatureFilter() {}
173    public FeatureFilter(String feature) { setFeature(feature); }
174
175    /**
176     * Set the feature to <span class="arg">feature</span>.
177     *
178     * @param feature the feature
179     */
180    public void setFeature(String feature) {
181      this.feature = feature;
182    }
183    
184    /**
185     * Retrieve the current feature.
186     *
187     * @return the feature <span class="type">String</span>
188     */
189    public String getFeature() {
190      return feature;
191    }
192    
193    /**
194     * @return <span class="arg">record</span>.
195     * <span class="method">getFeature</span><code>()</code> <code>==</code>
196     * <span class="const">this</span>.<span class="method">getFeature</span><code>()</code>
197     */
198    public boolean accept(GFFRecord record) {
199      return record.getFeature().equals(feature);
200    }
201  }
202
203  public static class FrameFilter implements GFFRecordFilter {
204    private int frame;
205
206    public FrameFilter() {}
207    public FrameFilter(int frame) { setFrame(frame); }
208
209    public int getFrame() {
210      return this.frame;
211    }
212
213    public void setFrame(int frame) {
214      this.frame = frame;
215    }
216
217    public boolean accept(GFFRecord record) {
218      return record.getFrame() == frame;
219    }
220  }
221
222  public static class NotFilter implements GFFRecordFilter {
223    private GFFRecordFilter filter;
224
225    public NotFilter() {}
226    public NotFilter(GFFRecordFilter filter) { setFilter(filter); }
227
228    public void setFilter(GFFRecordFilter filter) {
229      this.filter = filter;
230    }
231
232    public GFFRecordFilter getFilter() {
233      return filter;
234    }
235
236    public boolean accept(GFFRecord record) {
237      return !filter.accept(record);
238    }
239
240  }
241}