Samlocusiterator Skips Every Second Locus
2
3
Entering edit mode
11.8 years ago
Johan ▴ 890

I'm writing some Scala code which uses the SamLocusIterator after a excellent tip here: A: Java fasta reference iterator

However I'm getting some pretty weird output, which makes it seem like the iterator is skipping every other locus. Here's an example:

pos: chr1:99984
pos: chr1:99986
pos: chr1:99988
pos: chr1:99990
pos: chr1:99992
pos: chr1:99994
pos: chr1:99996
pos: chr1:99998
pos: chr1:100000

I can replicate this both in Scala and in Java with the following example programs:

The java version

import java.io.File;
import java.util.Iterator;

import net.sf.picard.util.SamLocusIterator;
import net.sf.picard.util.SamLocusIterator.LocusInfo;
import net.sf.samtools.SAMFileReader;


public class JavaMinimalIteratorTest {
    /**
     * @param args
     */
    public static void main(String[] args) {
        File testFile = new File("exampleBAM.bam");
        SAMFileReader fileReader = new SAMFileReader(testFile);

        SamLocusIterator samLocusIterator = new SamLocusIterator(fileReader);
        Iterator<LocusInfo> iterator = samLocusIterator.iterator();

        while (iterator.hasNext()) {
            LocusInfo locus = iterator.next();
            System.out.println("pos: " + locus.getSequenceName() + ":" + locus.getPosition());
        }
    }
}

The Scala version

import net.sf.samtools.SAMFileReader
import net.sf.picard.util.SamLocusIterator
import java.io.File

object MinimalIteratorExample extends App {

    val testFile = new File("exampleBAM.bam")
    val fileReader = new SAMFileReader(testFile)

    val samLocusIterator: SamLocusIterator = new SamLocusIterator(fileReader)
    val iterator = samLocusIterator.iterator()

    while (iterator.hasNext()) {
        val locus = iterator.next()
        println("pos: " + locus.getSequenceName() + ":" + locus.getPosition())
    }
}

Am I'm using the iterator in the wrong way, or is this some bug in the Picard code? I've been looking debugging and looking into the Picard classes, but this far I've not been able to make sense of it. Any help figuring this out would be much appreciated.

picard java • 3.9k views
ADD COMMENT
3
Entering edit mode

I'm afraid 'hasNext()' calls 'next()' ...

ADD REPLY
0
Entering edit mode

Well, I see. That's not great, now, is it? I'll post it as an issue to the picard/samtools-dev list.

ADD REPLY
0
Entering edit mode

Thank you. I just had a glance at github: the only place I saw this API used was a test in GATK (a simple loop to get the time for scanning a bam.... )

ADD REPLY
0
Entering edit mode

did you post your question on the samtools mailing list ? I cannot see the post. http://sourceforge.net/mailarchive/forum.php?forum_name=samtools-devel

ADD REPLY
0
Entering edit mode

I did, but only realized this morning that I had to be a member on the list to post. I posted it now though.

ADD REPLY
0
Entering edit mode

I think it is not the case. I just tried the similar code above replacing the "hasNext()" condition to true. But got the same result.

ADD REPLY
0
Entering edit mode

I've got the same problem here. Thanks I've never noticed this problem. you'd better ask the picard/samtools-dev mailing list.

ADD REPLY
0
Entering edit mode

In case you did not know already: You can import scala.collection.JavaConversions.asScalaIterator and you the SamLocusIterator as every iterator. e.g.: for( entry <- iterator) { do something meaningful with entry } Saves some LOC and looks much cleaner.

ADD REPLY
2
Entering edit mode
11.8 years ago
Sangwoo Kim ▴ 440

I just found that this occurs in regions where no reads have been aligned. Because these regions are not of our interest, we should skip them by setting setEmitUncoveredLoci()

Try this.

SamLocusIterator samLocusIterator = new SamLocusIterator(fileReader);
samLocusIterator.setEmitUncoveredLoci(false);
Iterator<LocusInfo> iterator = samLocusIterator.iterator();
ADD COMMENT
1
Entering edit mode

You're right, this only happens in uncovered regions. :) Still somewhat unexpected, though.

ADD REPLY
0
Entering edit mode

Nice to know ! Thank you.

ADD REPLY
0
Entering edit mode
10.8 years ago
agolicz ▴ 30

This bug is fixed now, as of picard tools v1.88. In case somebody also needs non-covered regions.

ADD COMMENT

Login before adding your answer.

Traffic: 1752 users visited in the last hour
Help About
FAQ
Access RSS
API
Stats

Use of this site constitutes acceptance of our User Agreement and Privacy Policy.

Powered by the version 2.3.6