Last active
December 25, 2015 12:19
-
-
Save johandahlberg/6975726 to your computer and use it in GitHub Desktop.
Having problems with the SortingCollection which seems to skip records. Created this gist to use for reference when e-mailing samtools-devel list.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package molmed | |
import net.sf.samtools.util.SortingCollection | |
import net.sf.samtools.BAMRecordCodec | |
import net.sf.picard.fastq.FastqRecord | |
import java.util.Comparator | |
import java.io.InputStream | |
import java.io.PrintStream | |
import net.sf.picard.fastq.FastqConstants | |
object DemoIteratorSkipsRecords extends App { | |
val sortingCollection = SortingCollection.newInstance( | |
classOf[FastqRecord], | |
new FastqRecordCodec(), new FastqRecordComparator(), | |
2) | |
sortingCollection.add(new FastqRecord("a1", "AA", "", "55")) | |
sortingCollection.add(new FastqRecord("a2", "AA", "", "55")) | |
sortingCollection.add(new FastqRecord("a3", "AA", "", "55")) | |
sortingCollection.add(new FastqRecord("a4", "AA", "", "55")) | |
sortingCollection.add(new FastqRecord("a5", "AA", "", "55")) | |
sortingCollection.add(new FastqRecord("a6", "AA", "", "55")) | |
sortingCollection.add(new FastqRecord("a7", "AA", "", "55")) | |
sortingCollection.add(new FastqRecord("a8", "AA", "", "55")) | |
sortingCollection.doneAdding() | |
val iterator = sortingCollection.iterator() | |
while (iterator.hasNext()) { | |
val rec = iterator.next() | |
println("record=" + rec.getReadHeader()) | |
} | |
class FastqRecordComparator extends Comparator[FastqRecord] { | |
def compare(a: FastqRecord, b: FastqRecord): Int = | |
a.getReadHeader().compareTo(b.getReadHeader()) | |
} | |
class FastqRecordCodec extends SortingCollection.Codec[FastqRecord] { | |
var writer: PrintStream = null | |
var in: scala.io.BufferedSource = null | |
def decode(): FastqRecord = { | |
val lines = in.getLines | |
val fourLines = lines.take(4).toList | |
if (fourLines.isEmpty) | |
null | |
else | |
new FastqRecord(fourLines(0), fourLines(1), fourLines(2), fourLines(3)) | |
} | |
def encode(rec: FastqRecord): Unit = { | |
writer.print(FastqConstants.SEQUENCE_HEADER) | |
writer.println(rec.getReadHeader()) | |
writer.println(rec.getReadString()) | |
writer.print(FastqConstants.QUALITY_HEADER) | |
writer.println(if (rec.getBaseQualityHeader() == null) "" else rec.getBaseQualityHeader()) | |
writer.println(rec.getBaseQualityString()) | |
} | |
def setInputStream(inputStream: java.io.InputStream): Unit = { | |
this.in = scala.io.Source.fromInputStream(inputStream) | |
} | |
def setOutputStream(outputStream: java.io.OutputStream): Unit = { | |
this.writer = new PrintStream(outputStream) | |
} | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This was the problematic first input, now it's working fine though. | |
record=@a1 | |
record=@a3 | |
record=@a5 | |
record=@a7 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment