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.distributed;
023
024import java.util.Set;
025
026import org.biojava.bio.BioException;
027import org.biojava.bio.seq.FeatureFilter;
028import org.biojava.bio.seq.FeatureHolder;
029import org.biojava.bio.seq.FilterUtils;
030import org.biojava.bio.seq.Sequence;
031import org.biojava.bio.seq.db.IllegalIDException;
032import org.biojava.bio.seq.db.SequenceDB;
033
034/**
035 * Turn an entire SequenceDB instance into a DistDataSource.
036 * This is very usefull for providing the 'reference' sequence and feature set
037 * upon which you can layer any other features you have.
038 * 
039 * @author Thomas Down
040 * 
041 */
042public class SequenceDBDataSource implements DistDataSource {
043    private SequenceDB db;
044
045    public SequenceDBDataSource(SequenceDB seqDB) {
046        this.db = seqDB;
047    }
048
049    public boolean hasSequence(String id) throws BioException {
050        return db.ids().contains(id);
051    }
052
053    public boolean hasFeatures(String id) throws BioException {
054        return hasSequence(id);
055    }
056
057    public FeatureHolder getFeatures(FeatureFilter ff) throws BioException {
058        throw new BioException();
059    }
060
061    public FeatureHolder getFeatures(String id, FeatureFilter ff, boolean recurse) throws BioException {
062        FeatureHolder fh;
063        try {
064            fh = db.getSequence(id);
065        } catch (IllegalIDException ex) {
066            return FeatureHolder.EMPTY_FEATURE_HOLDER;
067        }
068        
069        if (recurse == false && FilterUtils.areProperSubset(FeatureFilter.all, ff)) {
070            return fh;
071        } else {
072            return fh.filter(ff, recurse);
073        }
074    }
075
076    public Sequence getSequence(String id) throws BioException {
077        return db.getSequence(id);
078    }
079
080    public Set ids(boolean topLevel) throws BioException {
081        return db.ids();
082    }
083}