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.seq.db;
023
024import java.util.Iterator;
025
026import org.biojava.bio.BioError;
027import org.biojava.bio.BioException;
028import org.biojava.bio.BioRuntimeException;
029import org.biojava.bio.seq.FeatureFilter;
030import org.biojava.bio.seq.FeatureHolder;
031import org.biojava.bio.seq.MergeFeatureHolder;
032import org.biojava.bio.seq.Sequence;
033import org.biojava.bio.seq.SequenceIterator;
034import org.biojava.utils.AbstractChangeable;
035import org.biojava.utils.ChangeVetoException;
036
037/**
038 * An abstract implementation of SequenceDB that provides the sequenceIterator
039 * method.
040 *
041 * @author Matthew Pocock
042 * @author Thomas Down
043 */
044public abstract class AbstractSequenceDB
045  extends
046    AbstractChangeable
047  implements
048    SequenceDB
049{
050  public SequenceIterator sequenceIterator() {
051    return new SequenceIterator() {
052      private Iterator pID = ids().iterator();
053
054      public boolean hasNext() {
055        return pID.hasNext();
056      }
057
058      public Sequence nextSequence() throws BioException {
059        return getSequence((String) pID.next());
060      }
061    };
062  }
063
064  public FeatureHolder filter(FeatureFilter ff) {
065      MergeFeatureHolder results = new MergeFeatureHolder();
066      try {
067          for (SequenceIterator si = sequenceIterator(); si.hasNext(); ) {
068              Sequence seq = si.nextSequence();
069              FeatureHolder fh = seq.filter(ff);
070              if (fh != FeatureHolder.EMPTY_FEATURE_HOLDER) {
071                  results.addFeatureHolder(fh);
072              }
073          }
074      } catch (BioException ex) {
075          throw new BioRuntimeException(ex);
076      } catch (ChangeVetoException cve) {
077          throw new BioError("Assertion failed: couldn't modify newly created MergeFeatureHolder",cve);
078      }
079      return results;
080  }
081
082  public void addSequence(Sequence seq)
083  throws BioException, ChangeVetoException {
084    throw new ChangeVetoException("AbstractSequenceDB is immutable");
085  }
086
087  public void removeSequence(String id)
088  throws BioException, ChangeVetoException {
089    throw new ChangeVetoException("AbstractSequenceDB is immutable");
090  }
091}