From 8f4bc8cb6e0ce297dda19cb0a3be46a97b2314cc Mon Sep 17 00:00:00 2001 From: ebanks Date: Mon, 15 Jun 2009 16:38:08 +0000 Subject: [PATCH] Move filtering functionality into the PrintReadsWalker. More to come. git-svn-id: file:///humgen/gsa-scr1/gsa-engineering/svn_contents/trunk@1010 348d0f76-0448-11de-a6fe-93d51630548a --- .../sting/gatk/walkers/PrintReadsWalker.java | 13 +++++ .../gatk/walkers/ReadFilterWalker.java | 51 ------------------- 2 files changed, 13 insertions(+), 51 deletions(-) delete mode 100644 java/src/org/broadinstitute/sting/playground/gatk/walkers/ReadFilterWalker.java diff --git a/java/src/org/broadinstitute/sting/gatk/walkers/PrintReadsWalker.java b/java/src/org/broadinstitute/sting/gatk/walkers/PrintReadsWalker.java index 91a5d4d32..885343d9e 100755 --- a/java/src/org/broadinstitute/sting/gatk/walkers/PrintReadsWalker.java +++ b/java/src/org/broadinstitute/sting/gatk/walkers/PrintReadsWalker.java @@ -40,12 +40,25 @@ import java.util.Random; * This walker prints out the reads from the BAM files provided to the traversal engines. * It also supports the command line option '-outputBamFile filname', which outputs all the * reads to a specified BAM file + * The walker now also optionally filters reads based on command line options. */ public class PrintReadsWalker extends ReadWalker { /** an optional argument to dump the reads out to a BAM file */ @Argument(fullName = "outputBamFile", shortName = "of", doc = "Write output to this BAM filename instead of STDOUT", required = false) String outputBamFile = null; + @Argument(fullName = "maxReadLength", shortName = "maxRead", doc="Discard reads with length greater than the specified value", required = false) + Integer maxLength = null; + + /** + * The reads filter function. + * @param ref the reference bases that correspond to our read, if a reference was provided + * @param read the read itself, as a SAMRecord + * @return true if the read passes the filter, false if it doesn't + */ + public boolean filter(char[] ref, SAMRecord read) { + return (maxLength == null || read.getReadLength() <= maxLength); + } /** * The reads map function. diff --git a/java/src/org/broadinstitute/sting/playground/gatk/walkers/ReadFilterWalker.java b/java/src/org/broadinstitute/sting/playground/gatk/walkers/ReadFilterWalker.java deleted file mode 100644 index fcf2293f5..000000000 --- a/java/src/org/broadinstitute/sting/playground/gatk/walkers/ReadFilterWalker.java +++ /dev/null @@ -1,51 +0,0 @@ -package org.broadinstitute.sting.playground.gatk.walkers; - -import java.io.File; - -import net.sf.samtools.SAMFileHeader; -import net.sf.samtools.SAMFileWriter; -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; -import org.broadinstitute.sting.utils.Utils; - -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; - - public void initialize() { - SAMFileHeader header = this.getToolkit().getEngine().getSAMHeader(); - writer = Utils.createSAMFileWriterWithCompression(header, header.getSortOrder() != SAMFileHeader.SortOrder.unsorted, output, getToolkit().getBAMCompression()); - } - - @Override - public boolean filter(char[] ref, SAMRecord read) { - return read.getReadLength() <= max_len; - } - - @Override - public Integer map(char[] ref, SAMRecord read) { - 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) { - writer.close(); - out.println(nReads +" reads passed the filter and were written into output file "+output); - } - -}