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.biojavax.bio.db; 023 024import java.util.Iterator; 025import java.util.Set; 026 027import org.biojava.bio.BioError; 028import org.biojava.bio.BioException; 029import org.biojava.bio.BioRuntimeException; 030import org.biojava.bio.seq.FeatureFilter; 031import org.biojava.bio.seq.FeatureHolder; 032import org.biojava.bio.seq.MergeFeatureHolder; 033import org.biojava.bio.seq.Sequence; 034import org.biojava.bio.seq.SequenceIterator; 035import org.biojava.bio.seq.db.IllegalIDException; 036import org.biojava.utils.ChangeVetoException; 037import org.biojavax.bio.BioEntry; 038import org.biojavax.bio.BioEntryIterator; 039import org.biojavax.bio.seq.RichSequence; 040import org.biojavax.bio.seq.RichSequenceIterator; 041 042/** 043 * An abstract implementation of RichSequenceDB that provides the getRichSequenceIterator 044 * method. 045 * 046 * @author Matthew Pocock 047 * @author Thomas Down 048 * @author Richard Holland 049 * @since 1.5 050 */ 051public abstract class AbstractRichSequenceDB extends AbstractBioEntryDB implements RichSequenceDB { 052 053 public Sequence getSequence(String id) throws BioException, IllegalIDException { 054 return this.getRichSequence(id); 055 } 056 057 public BioEntry getBioEntry(String id) throws BioException, IllegalIDException { 058 return this.getRichSequence(id); 059 } 060 061 public BioEntryDB getBioEntrys(Set ids) throws BioException, IllegalIDException { 062 return this.getRichSequences(ids); 063 } 064 065 public BioEntryDB getBioEntrys(Set ids, BioEntryDB db) throws BioException, IllegalIDException { 066 RichSequenceDB db2 = this.getRichSequences(ids); 067 for (BioEntryIterator i = db2.getBioEntryIterator(); i.hasNext(); ) { 068 BioEntry be = i.nextBioEntry(); 069 try { 070 db.addBioEntry(be); 071 } catch (ChangeVetoException ce) { 072 throw new BioException("Unexpectedly unable to add to a BioEntryDB", ce); 073 } 074 } 075 return db; 076 } 077 078 public void addSequence(Sequence seq) throws IllegalIDException, BioException, ChangeVetoException { 079 this.addRichSequence(RichSequence.Tools.enrich(seq)); 080 } 081 082 public void removeSequence(String id) throws IllegalIDException, BioException, ChangeVetoException { 083 this.removeRichSequence(id); 084 } 085 086 public void addBioEntry(BioEntry seq) throws IllegalIDException, BioException, ChangeVetoException { 087 throw new ChangeVetoException("Cannot add BioEntrys to a RichSequence database - use addRichSequence"); 088 } 089 090 public void removeBioEntry(String id) throws IllegalIDException, BioException, ChangeVetoException { 091 throw new ChangeVetoException("Cannot remove BioEntrys from a RichSequence database - use addRichSequence"); 092 } 093 094 public void addRichSequence(RichSequence seq) throws IllegalIDException, BioException, ChangeVetoException { 095 throw new ChangeVetoException("Cannot add RichSequences to a read-only database"); 096 } 097 098 public void removeRichSequence(String id) throws IllegalIDException, BioException, ChangeVetoException { 099 throw new ChangeVetoException("Cannot remove RichSequences from a read-only database"); 100 } 101 102 public SequenceIterator sequenceIterator() { 103 return this.getRichSequenceIterator(); 104 } 105 106 public BioEntryIterator getBioEntryIterator() { 107 return this.getRichSequenceIterator(); 108 } 109 110 public RichSequenceIterator getRichSequenceIterator() { 111 return new RichSequenceIterator() { 112 private Iterator pID = ids().iterator(); 113 114 public boolean hasNext() { 115 return pID.hasNext(); 116 } 117 118 public Sequence nextSequence() throws BioException { 119 return nextRichSequence(); 120 } 121 122 public BioEntry nextBioEntry() throws BioException { 123 return nextRichSequence(); 124 } 125 126 public RichSequence nextRichSequence() throws BioException { 127 return getRichSequence((String)pID.next()); 128 } 129 }; 130 } 131 132 public FeatureHolder filter(FeatureFilter ff) { 133 // Default behaviour is accept-all. 134 MergeFeatureHolder results = new MergeFeatureHolder(); 135 try { 136 for (RichSequenceIterator si = getRichSequenceIterator(); si.hasNext(); ) { 137 RichSequence seq = si.nextRichSequence(); 138 results.addFeatureHolder(seq.filter(ff)); 139 } 140 } catch (BioException ex) { 141 throw new BioRuntimeException(ex); 142 } catch (ChangeVetoException cve) { 143 throw new BioError("Assertion failed: couldn't modify newly created MergeFeatureHolder",cve); 144 } 145 return results; 146 } 147}