[Biojava-l] adding toSequenceIterator method for Alignment

Singh, Nimesh Nimesh.Singh@maxygen.com
Thu, 18 Jul 2002 10:06:27 -0700


     I've created a class called AlignmentSequenceIterator that I intend to put in the org.biojava.bio.seq package.  It will do the real work.  I've also added
        public SequenceIterator sequenceIterator() {
            return new AlignmentSequenceIterator(this);
        }
to each alignment class.  It should work fine in every alignment, because AlignmentSequenceIterator uses the getLabels and symbolListForLabel methods from the Alignment interface.  
 
     If this is fine, then I'll upload everything later today.  If you have any suggestions for changes, then let me know.
 
Nimesh
 
 
Here is the cod for AlignmentSequenceIterator:
 
public class AlignmentSequenceIterator implements SequenceIterator {
    private Alignment align;
    private Iterator labels;
    private SequenceFactory sf;
    public AlignmentSequenceIterator(Alignment align) {
        this.align = align;
        labels = align.getLabels().iterator();
        sf = new SimpleSequenceFactory();
    }
    public boolean hasNext() {
        return labels.hasNext();
    }
    public Sequence nextSequence() throws NoSuchElementException, BioException {
        if (!hasNext()) {
            throw new NoSuchElementException("No more sequences in the alignment.");
        }
        else {
            try {
                Object label = labels.next();
                SymbolList symList = align.symbolListForLabel(label);
                Sequence seq = sf.createSequence(symList, label.toString(), label.toString(), null);
                return seq;
            } catch (Exception e) {
         throw new BioException(e, "Could not read sequence");
     }
        }
    }
}