From 03ec3452f261419694ca2260b4702d9d5fbb3654 Mon Sep 17 00:00:00 2001 From: asivache Date: Fri, 17 Apr 2009 18:51:39 +0000 Subject: [PATCH] a first, simplest version of a walker that filters out reads based on user-specified criteria and writes remaining reads into a new bam file git-svn-id: file:///humgen/gsa-scr1/gsa-engineering/svn_contents/trunk@467 348d0f76-0448-11de-a6fe-93d51630548a --- .../gatk/walkers/ReadFilterWalker.java | 48 +++++++++++++++++++ 1 file changed, 48 insertions(+) create mode 100644 java/src/org/broadinstitute/sting/playground/gatk/walkers/ReadFilterWalker.java diff --git a/java/src/org/broadinstitute/sting/playground/gatk/walkers/ReadFilterWalker.java b/java/src/org/broadinstitute/sting/playground/gatk/walkers/ReadFilterWalker.java new file mode 100644 index 000000000..c3f7b30ec --- /dev/null +++ b/java/src/org/broadinstitute/sting/playground/gatk/walkers/ReadFilterWalker.java @@ -0,0 +1,48 @@ +package org.broadinstitute.sting.playground.gatk.walkers; + +import java.io.File; + +import net.sf.samtools.SAMFileWriter; +import net.sf.samtools.SAMFileWriterFactory; +import net.sf.samtools.SAMRecord; + +import org.broadinstitute.sting.gatk.LocusContext; +import org.broadinstitute.sting.gatk.walkers.ReadWalker; +import org.broadinstitute.sting.utils.cmdLine.Argument; + +public class ReadFilterWalker extends ReadWalker { + @Argument(fullName="output_file", shortName="O",doc="SAM or BAM file to write filtered reads into (will be overwritten if exists)",required=true ) public String output; + @Argument(fullName="max_read_length",doc="Discard reads with length greater than the specified value",required=false) public Integer max_len; + + + private SAMFileWriter writer = null; + + @Override + public boolean filter(LocusContext context, SAMRecord read) { + if ( read.getReadLength() > max_len ) return false; + return true; + } + + @Override + public Integer map(LocusContext context, SAMRecord read) { + if ( writer == null ) writer = new SAMFileWriterFactory().makeSAMOrBAMWriter(read.getHeader(), true, new File(output)); + writer.addAlignment(read); + return 1; + } + + @Override + public Integer reduce(Integer value, Integer sum) { + return sum+value; + } + + @Override + public Integer reduceInit() { + return 0; + } + + public void onTraversalDone(Integer nReads) { + super.onTraversalDone(nReads); + out.println(nReads +" reads passed the filter and were written into output file "+output); + } + +}