diff --git a/java/src/org/broadinstitute/sting/playground/gatk/walkers/ReadValidationWalker.java b/java/src/org/broadinstitute/sting/playground/gatk/walkers/ReadValidationWalker.java new file mode 100644 index 000000000..533b69d93 --- /dev/null +++ b/java/src/org/broadinstitute/sting/playground/gatk/walkers/ReadValidationWalker.java @@ -0,0 +1,144 @@ +package org.broadinstitute.sting.playground.gatk.walkers; + +import org.broadinstitute.sting.gatk.walkers.Walker; +import org.broadinstitute.sting.gatk.walkers.ReadWalker; +import org.broadinstitute.sting.utils.cmdLine.Argument; +import org.broadinstitute.sting.utils.Utils; +import org.broadinstitute.sting.utils.StingException; +import net.sf.samtools.SAMRecord; +import net.sf.samtools.SAMFileWriter; +import net.sf.samtools.SAMReadGroupRecord; +import net.sf.samtools.SAMFileHeader; + +import java.security.MessageDigest; +import java.security.NoSuchAlgorithmException; +import java.util.List; +import java.util.ArrayList; +import java.math.BigInteger; + + +/* + * Copyright (c) 2009 The Broad Institute + * + * Permission is hereby granted, free of charge, to any person + * obtaining a copy of this software and associated documentation + * files (the "Software"), to deal in the Software without + * restriction, including without limitation the rights to use, + * copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the + * Software is furnished to do so, subject to the following + * conditions: + * + * The above copyright notice and this permission notice shall be + * included in all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, + * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES + * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND + * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT + * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, + * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING + * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR + * OTHER DEALINGS IN THE SOFTWARE. + */ + +/** + * + * @author aaron + * + * Class ReadValidationWalker + * + * A descriptions should go here. Blame aaron if it's missing. + */ +public class ReadValidationWalker extends ReadWalker { + + // our MD5 sum + private MessageDigest m; + + // private list of md5sums + private final List list = new ArrayList(); + + /** + * The initialize function. + */ + public void initialize() { + try { + m = MessageDigest.getInstance("MD5"); + } catch (NoSuchAlgorithmException e) { + throw new StingException("Unable to get the MD5 algorithm. Get a more eXtreme version of JAVA!@!@!!"); + } + } + + /** + * 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 true; + } + + /** + * The reads map 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 the read itself + */ + public SAMRecord map( char[] ref, SAMRecord read ) { + return read; + } + + /** + * reduceInit is called once before any calls to the map function. We use it here to setup the output + * bam file, if it was specified on the command line + * @return SAMFileWriter, set to the BAM output file if the command line option was set, null otherwise + */ + public Integer reduceInit() { + return new Integer(0); + } + + /** + * given a read and a output location, reduce by emitting the read + * @param read the read itself + * @param output the output source + * @return the SAMFileWriter, so that the next reduce can emit to the same source + */ + public Integer reduce( SAMRecord read, Integer output ) { + byte[] ray = new byte[read.getReadName().length() + 5]; + int x = 0; + for (char c: read.getReadName().toCharArray()) { + ray[x] = (byte)c; + //System.err.println("adding " + c + " to pos " + x); + x++; + } + //System.err.println(read.getReadName() + " name, alignment = " + read.getAlignmentStart()); + int y = 0; + for (;y < 4; y++) { + ray[x+y] = (byte)((read.getAlignmentStart() >> y * 8) & 0x000f); + } + ray[x+y] = read.getSecondOfPairFlag() ? (byte)1 : (byte)0; + BigInteger bigInt = new BigInteger(m.digest(ray)); + //System.err.println(bigInt.toString()); + m.reset(); + if (this.list.contains(bigInt)) { + throw new StingException("Seen Read: " + bigInt + "-> " + read.getReadName() + " before (list size = " + list.size() + ")"); + } + if (read.getAlignmentStart() < output) { + throw new StingException("Seen Read " + read.getReadName() + " has alignment of " + read.getAlignmentStart() + " before (list size = " + output + ")"); + } + list.add(bigInt); + return read.getAlignmentStart(); + } + + + /** + * when we're done traversing, close the reads file + * @param output the SAMFileWriter we've used in the reduce phase + */ + public void onTraversalDone( SAMFileWriter output ) { + if (output != null) { + output.close(); + } + } +} \ No newline at end of file