Minor tweak to improve ease-of-use of iterator system.

git-svn-id: file:///humgen/gsa-scr1/gsa-engineering/svn_contents/trunk@2104 348d0f76-0448-11de-a6fe-93d51630548a
This commit is contained in:
hanna 2009-11-20 18:24:19 +00:00
parent 4fbb6d05d0
commit a78bc60c0f
5 changed files with 73 additions and 72 deletions

View File

@ -38,7 +38,7 @@ public interface Aligner {
* @param bases List of bases.
* @return Iterator to alignments.
*/
public Iterator<Alignment[]> getAllAlignments(final byte[] bases);
public Iterable<Alignment[]> getAllAlignments(final byte[] bases);
/**
* Get a iterator of aligned reads, batched by mapping quality.
@ -46,7 +46,7 @@ public interface Aligner {
* @param newHeader Optional new header to use when aligning the read. If present, it must be null.
* @return Iterator to alignments.
*/
public Iterator<SAMRecord[]> alignAll(final SAMRecord read, final SAMFileHeader newHeader);
public Iterable<SAMRecord[]> alignAll(final SAMRecord read, final SAMFileHeader newHeader);
}

View File

@ -66,13 +66,14 @@ public class AlignmentValidationWalker extends ReadWalker<Integer,Integer> {
if(read.getReadNegativeStrandFlag()) bases = BaseUtils.simpleReverseComplement(bases);
boolean matches = true;
Iterator<Alignment[]> alignments = aligner.getAllAlignments(bases);
Iterable<Alignment[]> alignments = aligner.getAllAlignments(bases);
Iterator<Alignment[]> alignmentIterator = alignments.iterator();
if(!alignments.hasNext()) {
if(!alignmentIterator.hasNext()) {
matches = read.getReadUnmappedFlag();
}
else {
Alignment[] alignmentsOfBestQuality = alignments.next();
Alignment[] alignmentsOfBestQuality = alignmentIterator.next();
for(Alignment alignment: alignmentsOfBestQuality) {
matches = (alignment.getContigIndex() == read.getReferenceIndex());
matches &= (alignment.getAlignmentStart() == read.getAlignmentStart());
@ -91,9 +92,7 @@ public class AlignmentValidationWalker extends ReadWalker<Integer,Integer> {
logger.error(String.format(" Negative strand: %b", read.getReadNegativeStrandFlag()));
logger.error(String.format(" Cigar: %s%n", read.getCigarString()));
logger.error(String.format(" Mapping quality: %s%n", read.getMappingQuality()));
Iterator<Alignment[]> alignmentIterator = aligner.getAllAlignments(bases);
while(alignmentIterator.hasNext()) {
Alignment[] alignmentsByScore = alignmentIterator.next();
for(Alignment[] alignmentsByScore: alignments) {
for(int i = 0; i < alignmentsByScore.length; i++) {
logger.error(String.format("Alignment %d:",i));
logger.error(String.format(" Contig index: %d",alignmentsByScore[i].getContigIndex()));

View File

@ -94,37 +94,41 @@ public class BWACAligner extends BWAAligner {
* @return Iterator to alignments.
*/
@Override
public Iterator<Alignment[]> getAllAlignments(final byte[] bases) {
public Iterable<Alignment[]> getAllAlignments(final byte[] bases) {
final BWAPath[] paths = getPaths(bases);
return new Iterator<Alignment[]>() {
/**
* The last position accessed.
*/
private int position = 0;
return new Iterable<Alignment[]>() {
public Iterator<Alignment[]> iterator() {
return new Iterator<Alignment[]>() {
/**
* The last position accessed.
*/
private int position = 0;
/**
* Whether all alignments have been seen based on the current position.
* @return True if any more alignments are pending. False otherwise.
*/
public boolean hasNext() { return position < paths.length; }
/**
* Whether all alignments have been seen based on the current position.
* @return True if any more alignments are pending. False otherwise.
*/
public boolean hasNext() { return position < paths.length; }
/**
* Return the next cross-section of alignments, based on mapping quality.
* @return Array of the next set of alignments of a given mapping quality.
*/
public Alignment[] next() {
if(position >= paths.length)
throw new UnsupportedOperationException("Out of alignments to return.");
int score = paths[position].score;
int startingPosition = position;
while(position < paths.length && paths[position].score == score) position++;
return convertPathsToAlignments(bases,Arrays.copyOfRange(paths,startingPosition,position));
/**
* Return the next cross-section of alignments, based on mapping quality.
* @return Array of the next set of alignments of a given mapping quality.
*/
public Alignment[] next() {
if(position >= paths.length)
throw new UnsupportedOperationException("Out of alignments to return.");
int score = paths[position].score;
int startingPosition = position;
while(position < paths.length && paths[position].score == score) position++;
return convertPathsToAlignments(bases,Arrays.copyOfRange(paths,startingPosition,position));
}
/**
* Unsupported.
*/
public void remove() { throw new UnsupportedOperationException("Cannot remove from an alignment iterator"); }
};
}
/**
* Unsupported.
*/
public void remove() { throw new UnsupportedOperationException("Cannot remove from an alignment iterator"); }
};
}
@ -135,32 +139,37 @@ public class BWACAligner extends BWAAligner {
* @return Iterator to alignments.
*/
@Override
public Iterator<SAMRecord[]> alignAll(final SAMRecord read, final SAMFileHeader newHeader) {
final Iterator<Alignment[]> alignments = getAllAlignments(read.getReadBases());
return new Iterator<SAMRecord[]>() {
/**
* Whether all alignments have been seen based on the current position.
* @return True if any more alignments are pending. False otherwise.
*/
public boolean hasNext() { return alignments.hasNext(); }
public Iterable<SAMRecord[]> alignAll(final SAMRecord read, final SAMFileHeader newHeader) {
final Iterable<Alignment[]> alignments = getAllAlignments(read.getReadBases());
return new Iterable<SAMRecord[]>() {
public Iterator<SAMRecord[]> iterator() {
final Iterator<Alignment[]> alignmentIterator = alignments.iterator();
return new Iterator<SAMRecord[]>() {
/**
* Whether all alignments have been seen based on the current position.
* @return True if any more alignments are pending. False otherwise.
*/
public boolean hasNext() { return alignmentIterator.hasNext(); }
/**
* Return the next cross-section of alignments, based on mapping quality.
* @return Array of the next set of alignments of a given mapping quality.
*/
public SAMRecord[] next() {
Alignment[] alignmentsOfQuality = alignments.next();
SAMRecord[] reads = new SAMRecord[alignmentsOfQuality.length];
for(int i = 0; i < alignmentsOfQuality.length; i++) {
reads[i] = Alignment.convertToRead(alignmentsOfQuality[i],read,newHeader);
}
return reads;
/**
* Return the next cross-section of alignments, based on mapping quality.
* @return Array of the next set of alignments of a given mapping quality.
*/
public SAMRecord[] next() {
Alignment[] alignmentsOfQuality = alignmentIterator.next();
SAMRecord[] reads = new SAMRecord[alignmentsOfQuality.length];
for(int i = 0; i < alignmentsOfQuality.length; i++) {
reads[i] = Alignment.convertToRead(alignmentsOfQuality[i],read,newHeader);
}
return reads;
}
/**
* Unsupported.
*/
public void remove() { throw new UnsupportedOperationException("Cannot remove from an alignment iterator"); }
};
}
/**
* Unsupported.
*/
public void remove() { throw new UnsupportedOperationException("Cannot remove from an alignment iterator"); }
};
}

View File

@ -2,16 +2,12 @@ package org.broadinstitute.sting.alignment.bwa.java;
import org.broadinstitute.sting.alignment.Aligner;
import org.broadinstitute.sting.alignment.Alignment;
import org.broadinstitute.sting.alignment.bwa.java.BWAJavaAligner;
import org.broadinstitute.sting.alignment.bwa.java.BWAAlignment;
import org.broadinstitute.sting.alignment.bwa.java.AlignmentState;
import org.broadinstitute.sting.utils.StingException;
import org.broadinstitute.sting.utils.BaseUtils;
import org.broadinstitute.sting.utils.fasta.IndexedFastaSequenceFile;
import java.io.File;
import java.io.FileNotFoundException;
import java.util.Iterator;
import net.sf.samtools.*;
@ -79,16 +75,15 @@ public class AlignerTestHarness {
// Clear everything except flags pertaining to pairing and set 'unmapped' status to true.
alignmentCleaned.setFlags(alignmentCleaned.getFlags() & 0x00A1 | 0x000C);
Iterator<Alignment[]> alignments = aligner.getAllAlignments(alignmentCleaned.getReadBases());
if(!alignments.hasNext() ) {
Iterable<Alignment[]> alignments = aligner.getAllAlignments(alignmentCleaned.getReadBases());
if(!alignments.iterator().hasNext() ) {
//throw new StingException(String.format("Unable to align read %s to reference; count = %d",read.getReadName(),count));
System.out.printf("Unable to align read %s to reference; count = %d%n",read.getReadName(),count);
failures++;
}
Alignment foundAlignment = null;
while(alignments.hasNext()) {
Alignment[] alignmentsOfQuality = alignments.next();
for(Alignment[] alignmentsOfQuality: alignments) {
for(Alignment alignment: alignmentsOfQuality) {
if( read.getReadNegativeStrandFlag() != alignment.isNegativeStrand() )
continue;
@ -116,9 +111,7 @@ public class AlignerTestHarness {
String expectedRef = new String(reference.getSubsequenceAt(reference.getSequenceDictionary().getSequences().get(0).getSequenceName(),read.getAlignmentStart(),read.getAlignmentStart()+read.getReadLength()+numDeletions-1).getBases());
System.out.printf("expected ref = %s%n", formatBasesBasedOnCigar(expectedRef,read.getCigar(),CigarOperator.INSERTION));
Iterator<Alignment[]> alignmentsToOutput = aligner.getAllAlignments(alignmentCleaned.getReadBases());
while(alignmentsToOutput.hasNext()) {
Alignment[] alignmentsOfQuality = alignments.next();
for(Alignment[] alignmentsOfQuality: alignments) {
for(Alignment alignment: alignmentsOfQuality) {
System.out.println();

View File

@ -108,7 +108,7 @@ public class BWAJavaAligner implements Aligner {
* @param bases List of bases.
* @return Iterator to alignments.
*/
public Iterator<Alignment[]> getAllAlignments(final byte[] bases) { throw new UnsupportedOperationException("BWAJavaAligner does not yet support the standard Aligner interface."); }
public Iterable<Alignment[]> getAllAlignments(final byte[] bases) { throw new UnsupportedOperationException("BWAJavaAligner does not yet support the standard Aligner interface."); }
/**
* Get a iterator of aligned reads, batched by mapping quality.
@ -116,7 +116,7 @@ public class BWAJavaAligner implements Aligner {
* @param newHeader Optional new header to use when aligning the read. If present, it must be null.
* @return Iterator to alignments.
*/
public Iterator<SAMRecord[]> alignAll(final SAMRecord read, final SAMFileHeader newHeader) { throw new UnsupportedOperationException("BWAJavaAligner does not yet support the standard Aligner interface."); }
public Iterable<SAMRecord[]> alignAll(final SAMRecord read, final SAMFileHeader newHeader) { throw new UnsupportedOperationException("BWAJavaAligner does not yet support the standard Aligner interface."); }
public List<Alignment> align( SAMRecord read ) {