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. * @param bases List of bases.
* @return Iterator to alignments. * @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. * 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. * @param newHeader Optional new header to use when aligning the read. If present, it must be null.
* @return Iterator to alignments. * @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); if(read.getReadNegativeStrandFlag()) bases = BaseUtils.simpleReverseComplement(bases);
boolean matches = true; 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(); matches = read.getReadUnmappedFlag();
} }
else { else {
Alignment[] alignmentsOfBestQuality = alignments.next(); Alignment[] alignmentsOfBestQuality = alignmentIterator.next();
for(Alignment alignment: alignmentsOfBestQuality) { for(Alignment alignment: alignmentsOfBestQuality) {
matches = (alignment.getContigIndex() == read.getReferenceIndex()); matches = (alignment.getContigIndex() == read.getReferenceIndex());
matches &= (alignment.getAlignmentStart() == read.getAlignmentStart()); 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(" Negative strand: %b", read.getReadNegativeStrandFlag()));
logger.error(String.format(" Cigar: %s%n", read.getCigarString())); logger.error(String.format(" Cigar: %s%n", read.getCigarString()));
logger.error(String.format(" Mapping quality: %s%n", read.getMappingQuality())); logger.error(String.format(" Mapping quality: %s%n", read.getMappingQuality()));
Iterator<Alignment[]> alignmentIterator = aligner.getAllAlignments(bases); for(Alignment[] alignmentsByScore: alignments) {
while(alignmentIterator.hasNext()) {
Alignment[] alignmentsByScore = alignmentIterator.next();
for(int i = 0; i < alignmentsByScore.length; i++) { for(int i = 0; i < alignmentsByScore.length; i++) {
logger.error(String.format("Alignment %d:",i)); logger.error(String.format("Alignment %d:",i));
logger.error(String.format(" Contig index: %d",alignmentsByScore[i].getContigIndex())); 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. * @return Iterator to alignments.
*/ */
@Override @Override
public Iterator<Alignment[]> getAllAlignments(final byte[] bases) { public Iterable<Alignment[]> getAllAlignments(final byte[] bases) {
final BWAPath[] paths = getPaths(bases); final BWAPath[] paths = getPaths(bases);
return new Iterator<Alignment[]>() { return new Iterable<Alignment[]>() {
/** public Iterator<Alignment[]> iterator() {
* The last position accessed. return new Iterator<Alignment[]>() {
*/ /**
private int position = 0; * The last position accessed.
*/
private int position = 0;
/** /**
* Whether all alignments have been seen based on the current position. * Whether all alignments have been seen based on the current position.
* @return True if any more alignments are pending. False otherwise. * @return True if any more alignments are pending. False otherwise.
*/ */
public boolean hasNext() { return position < paths.length; } public boolean hasNext() { return position < paths.length; }
/** /**
* Return the next cross-section of alignments, based on mapping quality. * Return the next cross-section of alignments, based on mapping quality.
* @return Array of the next set of alignments of a given mapping quality. * @return Array of the next set of alignments of a given mapping quality.
*/ */
public Alignment[] next() { public Alignment[] next() {
if(position >= paths.length) if(position >= paths.length)
throw new UnsupportedOperationException("Out of alignments to return."); throw new UnsupportedOperationException("Out of alignments to return.");
int score = paths[position].score; int score = paths[position].score;
int startingPosition = position; int startingPosition = position;
while(position < paths.length && paths[position].score == score) position++; while(position < paths.length && paths[position].score == score) position++;
return convertPathsToAlignments(bases,Arrays.copyOfRange(paths,startingPosition,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. * @return Iterator to alignments.
*/ */
@Override @Override
public Iterator<SAMRecord[]> alignAll(final SAMRecord read, final SAMFileHeader newHeader) { public Iterable<SAMRecord[]> alignAll(final SAMRecord read, final SAMFileHeader newHeader) {
final Iterator<Alignment[]> alignments = getAllAlignments(read.getReadBases()); final Iterable<Alignment[]> alignments = getAllAlignments(read.getReadBases());
return new Iterator<SAMRecord[]>() { return new Iterable<SAMRecord[]>() {
/** public Iterator<SAMRecord[]> iterator() {
* Whether all alignments have been seen based on the current position. final Iterator<Alignment[]> alignmentIterator = alignments.iterator();
* @return True if any more alignments are pending. False otherwise. return new Iterator<SAMRecord[]>() {
*/ /**
public boolean hasNext() { return alignments.hasNext(); } * 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 the next cross-section of alignments, based on mapping quality.
* @return Array of the next set of alignments of a given mapping quality. * @return Array of the next set of alignments of a given mapping quality.
*/ */
public SAMRecord[] next() { public SAMRecord[] next() {
Alignment[] alignmentsOfQuality = alignments.next(); Alignment[] alignmentsOfQuality = alignmentIterator.next();
SAMRecord[] reads = new SAMRecord[alignmentsOfQuality.length]; SAMRecord[] reads = new SAMRecord[alignmentsOfQuality.length];
for(int i = 0; i < alignmentsOfQuality.length; i++) { for(int i = 0; i < alignmentsOfQuality.length; i++) {
reads[i] = Alignment.convertToRead(alignmentsOfQuality[i],read,newHeader); reads[i] = Alignment.convertToRead(alignmentsOfQuality[i],read,newHeader);
} }
return reads; 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.Aligner;
import org.broadinstitute.sting.alignment.Alignment; 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.StingException;
import org.broadinstitute.sting.utils.BaseUtils; import org.broadinstitute.sting.utils.BaseUtils;
import org.broadinstitute.sting.utils.fasta.IndexedFastaSequenceFile; import org.broadinstitute.sting.utils.fasta.IndexedFastaSequenceFile;
import java.io.File; import java.io.File;
import java.io.FileNotFoundException; import java.io.FileNotFoundException;
import java.util.Iterator;
import net.sf.samtools.*; import net.sf.samtools.*;
@ -79,16 +75,15 @@ public class AlignerTestHarness {
// Clear everything except flags pertaining to pairing and set 'unmapped' status to true. // Clear everything except flags pertaining to pairing and set 'unmapped' status to true.
alignmentCleaned.setFlags(alignmentCleaned.getFlags() & 0x00A1 | 0x000C); alignmentCleaned.setFlags(alignmentCleaned.getFlags() & 0x00A1 | 0x000C);
Iterator<Alignment[]> alignments = aligner.getAllAlignments(alignmentCleaned.getReadBases()); Iterable<Alignment[]> alignments = aligner.getAllAlignments(alignmentCleaned.getReadBases());
if(!alignments.hasNext() ) { if(!alignments.iterator().hasNext() ) {
//throw new StingException(String.format("Unable to align read %s to reference; count = %d",read.getReadName(),count)); //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); System.out.printf("Unable to align read %s to reference; count = %d%n",read.getReadName(),count);
failures++; failures++;
} }
Alignment foundAlignment = null; Alignment foundAlignment = null;
while(alignments.hasNext()) { for(Alignment[] alignmentsOfQuality: alignments) {
Alignment[] alignmentsOfQuality = alignments.next();
for(Alignment alignment: alignmentsOfQuality) { for(Alignment alignment: alignmentsOfQuality) {
if( read.getReadNegativeStrandFlag() != alignment.isNegativeStrand() ) if( read.getReadNegativeStrandFlag() != alignment.isNegativeStrand() )
continue; 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()); 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)); System.out.printf("expected ref = %s%n", formatBasesBasedOnCigar(expectedRef,read.getCigar(),CigarOperator.INSERTION));
Iterator<Alignment[]> alignmentsToOutput = aligner.getAllAlignments(alignmentCleaned.getReadBases()); for(Alignment[] alignmentsOfQuality: alignments) {
while(alignmentsToOutput.hasNext()) {
Alignment[] alignmentsOfQuality = alignments.next();
for(Alignment alignment: alignmentsOfQuality) { for(Alignment alignment: alignmentsOfQuality) {
System.out.println(); System.out.println();

View File

@ -108,7 +108,7 @@ public class BWAJavaAligner implements Aligner {
* @param bases List of bases. * @param bases List of bases.
* @return Iterator to alignments. * @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. * 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. * @param newHeader Optional new header to use when aligning the read. If present, it must be null.
* @return Iterator to alignments. * @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 ) { public List<Alignment> align( SAMRecord read ) {