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.Collections;
025import java.util.HashSet;
026import java.util.Set;
027
028import org.biojava.bio.BioException;
029import org.biojava.bio.seq.Sequence;
030
031/**
032 * @author Matthew Pocock
033 */
034public class SubSequenceDB extends SequenceDBWrapper {
035  private final Set ids;
036  
037  public SubSequenceDB(SequenceDB parent, Set ids)
038  throws BioException {
039    super(parent);
040    this.ids = new HashSet(ids);
041    Set pids = parent.ids();
042    if(!pids.containsAll(ids)) {
043      throw new BioException(
044        "IDs must all be contained in the parent database " +
045        parent.getName()
046      );
047    }
048  }
049  
050  public String getName() {
051    return getParent().getName() + " subset " + ids.toString();
052  }
053  
054  public Sequence getSequence(String id) throws BioException {
055    if(!ids.contains(id)) {
056      throw new BioException(
057        "No sequence for " + id + " found in database " + getName()
058      );
059    }
060    return getParent().getSequence(id);
061  }
062  
063  public Set ids() {
064    return Collections.unmodifiableSet(ids);
065  }
066}