From 4a6be896b9ef0977e8346ea2fdce36d1fbee4a07 Mon Sep 17 00:00:00 2001 From: hanna Date: Fri, 27 Mar 2009 15:03:32 +0000 Subject: [PATCH] Provide out and err PrintStreams to the walkers. git-svn-id: file:///humgen/gsa-scr1/gsa-engineering/svn_contents/trunk@213 348d0f76-0448-11de-a6fe-93d51630548a --- .../sting/gatk/GenomeAnalysisTK.java | 61 +++++++++++++++++++ .../gatk/walkers/DepthOfCoverageWalker.java | 2 +- .../sting/gatk/walkers/PileupWalker.java | 4 +- .../sting/gatk/walkers/PrintReadsWalker.java | 2 +- .../sting/gatk/walkers/Walker.java | 14 +++++ .../gatk/walkers/AlignedReadsHistoWalker.java | 2 +- .../walkers/AlleleFrequencyMetricsWalker.java | 48 +++++++-------- .../gatk/walkers/BaseQualityDumpWalker.java | 4 +- .../gatk/walkers/BaseQualityHistoWalker.java | 2 +- .../gatk/walkers/MismatchCounterWalker.java | 8 +-- .../gatk/walkers/MismatchHistoWalker.java | 2 +- .../gatk/walkers/SingleSampleGenotyper.java | 2 +- .../sting/utils/cmdLine/ArgumentParser.java | 2 - 13 files changed, 112 insertions(+), 41 deletions(-) diff --git a/java/src/org/broadinstitute/sting/gatk/GenomeAnalysisTK.java b/java/src/org/broadinstitute/sting/gatk/GenomeAnalysisTK.java index 50cad2126..82299eefe 100644 --- a/java/src/org/broadinstitute/sting/gatk/GenomeAnalysisTK.java +++ b/java/src/org/broadinstitute/sting/gatk/GenomeAnalysisTK.java @@ -19,6 +19,8 @@ import org.broadinstitute.sting.utils.Utils; import org.broadinstitute.sting.utils.cmdLine.CommandLineProgram; import java.io.File; +import java.io.PrintStream; +import java.io.FileNotFoundException; import java.util.ArrayList; import java.util.HashMap; import java.util.List; @@ -49,6 +51,33 @@ public class GenomeAnalysisTK extends CommandLineProgram { public boolean DEBUGGING = false; public boolean WALK_ALL_LOCI = false; + /** + * An output file presented to the walker. + */ + public String outFileName = null; + + /** + * An error output file presented to the walker. + */ + public String errFileName = null; + + /** + * A joint file for both 'normal' and error output presented to the walker. + */ + public String outErrFileName = null; + + /** + * The output stream, initialized from OUTFILENAME / OUTERRFILENAME. + * Used by the walker. + */ + public PrintStream out = System.out; + + /** + * The output stream, initialized from ERRFILENAME / OUTERRFILENAME. + * Used by the walker. + */ + public PrintStream err = System.err; + /** * our log, which we want to capture anything from this class */ @@ -74,6 +103,9 @@ public class GenomeAnalysisTK extends CommandLineProgram { m_parser.addOptionalFlag("sort_on_the_fly", "F", "If set, enables on fly sorting of reads file.", "ENABLED_SORT_ON_FLY"); m_parser.addOptionalArg("intervals_file", "V", "File containing list of genomic intervals to operate on. line := ", "INTERVALS_FILE"); m_parser.addOptionalArg("all_loci", "A", "Should we process all loci, not just those covered by reads", "WALK_ALL_LOCI"); + m_parser.addOptionalArg("out", "o", "An output file presented to the walker. Will overwrite contents if file exists.", "outFileName" ); + m_parser.addOptionalArg("err", "e", "An error output file presented to the walker. Will overwrite contents if file exists.", "errFileName" ); + m_parser.addOptionalArg("outerr", "oe", "A joint file for 'normal' and error output presented to the walker. Will overwrite contents if file exists.", "outErrFileName"); } /** @@ -136,6 +168,8 @@ public class GenomeAnalysisTK extends CommandLineProgram { rods.add(gff); } + initializeOutputStreams(); + this.engine = new TraversalEngine(INPUT_FILE, REF_FILE_ARG, rods); // Prepare the sort ordering w.r.t. the sequence dictionary @@ -206,6 +240,33 @@ public class GenomeAnalysisTK extends CommandLineProgram { return 0; } + /** + * Initialize the output streams as specified by the user. + */ + private void initializeOutputStreams() { + if( outErrFileName != null && (outFileName != null || errFileName != null) ) + throw new IllegalArgumentException("Can't set output/error output file with either out file name or err file name"); + + try { + if( outErrFileName != null ) { + PrintStream outErrStream = new PrintStream( outErrFileName ); + out = outErrStream; + err = outErrStream; + } + + if ( outFileName != null ) { + out = new PrintStream( outFileName ); + } + + if( errFileName != null ) { + err = new PrintStream( errFileName ); + } + } + catch( FileNotFoundException ex ) { + throw new RuntimeException("Unable to open a walker output file.", ex); + } + } + /** * An inappropriately placed validation and performance testing routine for jumping * around in the fasta sequence file. diff --git a/java/src/org/broadinstitute/sting/gatk/walkers/DepthOfCoverageWalker.java b/java/src/org/broadinstitute/sting/gatk/walkers/DepthOfCoverageWalker.java index 4ceb70b63..4643f4ecf 100755 --- a/java/src/org/broadinstitute/sting/gatk/walkers/DepthOfCoverageWalker.java +++ b/java/src/org/broadinstitute/sting/gatk/walkers/DepthOfCoverageWalker.java @@ -14,7 +14,7 @@ import java.util.List; */ public class DepthOfCoverageWalker extends LocusWalker { public Integer map(List rodData, char ref, LocusContext context) { - System.out.printf("%s: %d%n", context.getLocation(), context.getReads().size() ); + out.printf("%s: %d%n", context.getLocation(), context.getReads().size() ); return 1; } diff --git a/java/src/org/broadinstitute/sting/gatk/walkers/PileupWalker.java b/java/src/org/broadinstitute/sting/gatk/walkers/PileupWalker.java index 260fa2d2a..4e3aac348 100644 --- a/java/src/org/broadinstitute/sting/gatk/walkers/PileupWalker.java +++ b/java/src/org/broadinstitute/sting/gatk/walkers/PileupWalker.java @@ -18,8 +18,6 @@ public class PileupWalker extends LocusWalker { public void initialize() { } - public String walkerType() { return "ByLocus"; } - // Do we actually want to operate on the context? public boolean filter(List rodData, char ref, LocusContext context) { return true; // We are keeping all the reads @@ -55,7 +53,7 @@ public class PileupWalker extends LocusWalker { rodString = "[ROD: " + rodString + "]"; //if ( context.getLocation().getStart() % 1 == 0 ) { - System.out.printf("%s: %s %s %s %s%n", context.getLocation(), ref, bases, quals, rodString); + out.printf("%s: %s %s %s %s%n", context.getLocation(), ref, bases, quals, rodString); //} return 1; diff --git a/java/src/org/broadinstitute/sting/gatk/walkers/PrintReadsWalker.java b/java/src/org/broadinstitute/sting/gatk/walkers/PrintReadsWalker.java index 8baf7158c..769a2a63e 100755 --- a/java/src/org/broadinstitute/sting/gatk/walkers/PrintReadsWalker.java +++ b/java/src/org/broadinstitute/sting/gatk/walkers/PrintReadsWalker.java @@ -5,7 +5,7 @@ import org.broadinstitute.sting.gatk.LocusContext; public class PrintReadsWalker extends ReadWalker { public Integer map(LocusContext context, SAMRecord read) { - System.out.println(read.format()); + out.println(read.format()); return 1; } diff --git a/java/src/org/broadinstitute/sting/gatk/walkers/Walker.java b/java/src/org/broadinstitute/sting/gatk/walkers/Walker.java index 101595a1c..e895d84d6 100755 --- a/java/src/org/broadinstitute/sting/gatk/walkers/Walker.java +++ b/java/src/org/broadinstitute/sting/gatk/walkers/Walker.java @@ -1,5 +1,7 @@ package org.broadinstitute.sting.gatk.walkers; +import java.io.PrintStream; + import org.broadinstitute.sting.gatk.GenomeAnalysisTK; /** @@ -12,8 +14,20 @@ import org.broadinstitute.sting.gatk.GenomeAnalysisTK; public abstract class Walker { // TODO: Can a walker be templatized so that map and reduce live here? + /** + * A stream for writing normal (non-error) output. System.out by default. + */ + protected PrintStream out = null; + + /** + * A stream for writing error output. System.err by default. + */ + protected PrintStream err = null; + protected Walker() { GenomeAnalysisTK.Instance.loadArgumentsIntoObject(this); + out = GenomeAnalysisTK.Instance.out; + err = GenomeAnalysisTK.Instance.err; } /** diff --git a/java/src/org/broadinstitute/sting/playground/gatk/walkers/AlignedReadsHistoWalker.java b/java/src/org/broadinstitute/sting/playground/gatk/walkers/AlignedReadsHistoWalker.java index 70f08e075..7657d71b7 100755 --- a/java/src/org/broadinstitute/sting/playground/gatk/walkers/AlignedReadsHistoWalker.java +++ b/java/src/org/broadinstitute/sting/playground/gatk/walkers/AlignedReadsHistoWalker.java @@ -49,7 +49,7 @@ public class AlignedReadsHistoWalker extends ReadWalker { int curTotal = 0; for ( int i = 0; i < alignCounts.length; i++ ) { curTotal += alignCounts[i]; - System.out.printf("%3d %10d%n", i, curTotal); + out.printf("%3d %10d%n", i, curTotal); } } } diff --git a/java/src/org/broadinstitute/sting/playground/gatk/walkers/AlleleFrequencyMetricsWalker.java b/java/src/org/broadinstitute/sting/playground/gatk/walkers/AlleleFrequencyMetricsWalker.java index 5509a4fd9..4f33378f8 100755 --- a/java/src/org/broadinstitute/sting/playground/gatk/walkers/AlleleFrequencyMetricsWalker.java +++ b/java/src/org/broadinstitute/sting/playground/gatk/walkers/AlleleFrequencyMetricsWalker.java @@ -94,9 +94,9 @@ public class AlleleFrequencyMetricsWalker extends LocusWalker= LOD_cutoff) { @@ -124,7 +124,7 @@ public class AlleleFrequencyMetricsWalker extends LocusWalker= %.0f)\n", LOD_cutoff); - System.out.printf("METRICS -------------------------------------------------\n"); - System.out.printf("METRICS Total loci : %d\n", num_loci_total); - System.out.printf("METRICS Total called with confidence : %d (%.2f%%)\n", num_loci_confident, 100.0 * (float)num_loci_confident / (float)num_loci_total); + out.printf("\n"); + out.printf("METRICS Allele Frequency Metrics (LOD >= %.0f)\n", LOD_cutoff); + out.printf("METRICS -------------------------------------------------\n"); + out.printf("METRICS Total loci : %d\n", num_loci_total); + out.printf("METRICS Total called with confidence : %d (%.2f%%)\n", num_loci_confident, 100.0 * (float)num_loci_confident / (float)num_loci_total); if (num_variants != 0) { - System.out.printf("METRICS Number of variants : %d (%.2f%%) (1/%d)\n", num_variants, 100.0 * (float)num_variants / (float)num_loci_confident, num_loci_confident / num_variants); - System.out.printf("METRICS Fraction of variant sites in dbSNP : %.2f%%\n", 100.0 * (float)dbsnp_hits / (float)num_variants); - System.out.printf("METRICS -------------------------------------------------\n"); - System.out.printf("METRICS -- Hapmap Genotyping performance --\n"); - System.out.printf("METRICS Num. conf. calls at Hapmap chip sites : %d\n", hapmap_genotype_correct + hapmap_genotype_incorrect); - System.out.printf("METRICS Conf. calls at chip sites correct : %d\n", hapmap_genotype_correct); - System.out.printf("METRICS Conf. calls at chip sites incorrect : %d\n", hapmap_genotype_incorrect); - System.out.printf("METRICS %% of confident calls that are correct : %.2f%%\n", 100.0 * (float) hapmap_genotype_correct / (float)(hapmap_genotype_correct + hapmap_genotype_incorrect)); - System.out.printf("METRICS -------------------------------------------------\n"); - System.out.printf("METRICS -- Hapmap Reference/Variant performance --\n"); - System.out.printf("METRICS Num. conf. calls at Hapmap chip sites : %d\n", hapmap_refvar_correct + hapmap_refvar_incorrect); - System.out.printf("METRICS Conf. calls at chip sites correct : %d\n", hapmap_refvar_correct); - System.out.printf("METRICS Conf. calls at chip sites incorrect : %d\n", hapmap_refvar_incorrect); - System.out.printf("METRICS %% of confident calls that are correct : %.2f%%\n", 100.0 * (float) hapmap_refvar_correct / (float)(hapmap_refvar_correct + hapmap_refvar_incorrect)); + out.printf("METRICS Number of variants : %d (%.2f%%) (1/%d)\n", num_variants, 100.0 * (float)num_variants / (float)num_loci_confident, num_loci_confident / num_variants); + out.printf("METRICS Fraction of variant sites in dbSNP : %.2f%%\n", 100.0 * (float)dbsnp_hits / (float)num_variants); + out.printf("METRICS -------------------------------------------------\n"); + out.printf("METRICS -- Hapmap Genotyping performance --\n"); + out.printf("METRICS Num. conf. calls at Hapmap chip sites : %d\n", hapmap_genotype_correct + hapmap_genotype_incorrect); + out.printf("METRICS Conf. calls at chip sites correct : %d\n", hapmap_genotype_correct); + out.printf("METRICS Conf. calls at chip sites incorrect : %d\n", hapmap_genotype_incorrect); + out.printf("METRICS %% of confident calls that are correct : %.2f%%\n", 100.0 * (float) hapmap_genotype_correct / (float)(hapmap_genotype_correct + hapmap_genotype_incorrect)); + out.printf("METRICS -------------------------------------------------\n"); + out.printf("METRICS -- Hapmap Reference/Variant performance --\n"); + out.printf("METRICS Num. conf. calls at Hapmap chip sites : %d\n", hapmap_refvar_correct + hapmap_refvar_incorrect); + out.printf("METRICS Conf. calls at chip sites correct : %d\n", hapmap_refvar_correct); + out.printf("METRICS Conf. calls at chip sites incorrect : %d\n", hapmap_refvar_incorrect); + out.printf("METRICS %% of confident calls that are correct : %.2f%%\n", 100.0 * (float) hapmap_refvar_correct / (float)(hapmap_refvar_correct + hapmap_refvar_incorrect)); } - System.out.println(); + out.println(); } public void onTraversalDone() @@ -174,7 +174,7 @@ public class AlleleFrequencyMetricsWalker extends LocusWalker= 5) || (alleleFreq.lodVsRef <= -5)) { System.out.print(alleleFreq.asTabularString()); } - System.out.print(alleleFreq.asTabularString()); + out.print(alleleFreq.asTabularString()); if (this.num_loci_total % 1000 == 0) { printMetrics(); } diff --git a/java/src/org/broadinstitute/sting/playground/gatk/walkers/BaseQualityDumpWalker.java b/java/src/org/broadinstitute/sting/playground/gatk/walkers/BaseQualityDumpWalker.java index 282688efb..27461a63e 100755 --- a/java/src/org/broadinstitute/sting/playground/gatk/walkers/BaseQualityDumpWalker.java +++ b/java/src/org/broadinstitute/sting/playground/gatk/walkers/BaseQualityDumpWalker.java @@ -38,8 +38,8 @@ public class BaseQualityDumpWalker extends ReadWalker { for ( int i = 0; i < qualStr.length(); i++) scores[(reverseFlag ? (qualStr.length()-1-i) : i)] += (int)qualStr.charAt(i) - 33; for ( int i = 0; i < scores.length; i++ ) - System.out.print(scores[i] + " "); - System.out.println(""); + out.print(scores[i] + " "); + out.println(""); } return 1; diff --git a/java/src/org/broadinstitute/sting/playground/gatk/walkers/BaseQualityHistoWalker.java b/java/src/org/broadinstitute/sting/playground/gatk/walkers/BaseQualityHistoWalker.java index 0decc9ae9..74c4358b4 100755 --- a/java/src/org/broadinstitute/sting/playground/gatk/walkers/BaseQualityHistoWalker.java +++ b/java/src/org/broadinstitute/sting/playground/gatk/walkers/BaseQualityHistoWalker.java @@ -53,7 +53,7 @@ public class BaseQualityHistoWalker extends ReadWalker { } for ( int i = 0; i < lastNonZero+1; i++ ) { - System.out.printf("%3d : %10d%n", i, this.qualCounts[i]); + out.printf("%3d : %10d%n", i, this.qualCounts[i]); } } } diff --git a/java/src/org/broadinstitute/sting/playground/gatk/walkers/MismatchCounterWalker.java b/java/src/org/broadinstitute/sting/playground/gatk/walkers/MismatchCounterWalker.java index b42834c49..1ac0fe4eb 100755 --- a/java/src/org/broadinstitute/sting/playground/gatk/walkers/MismatchCounterWalker.java +++ b/java/src/org/broadinstitute/sting/playground/gatk/walkers/MismatchCounterWalker.java @@ -27,10 +27,10 @@ public class MismatchCounterWalker extends ReadWalker { assert(refSeq.size() == readBases.size()); - System.out.printf("start, stop = %d %d%n", start, stop); - System.out.println(read.format()); - System.out.println(Utils.baseList2string(refSeq)); - System.out.println(Utils.baseList2string(readBases)); + out.printf("start, stop = %d %d%n", start, stop); + out.println(read.format()); + out.println(Utils.baseList2string(refSeq)); + out.println(Utils.baseList2string(readBases)); } return nMismatches; diff --git a/java/src/org/broadinstitute/sting/playground/gatk/walkers/MismatchHistoWalker.java b/java/src/org/broadinstitute/sting/playground/gatk/walkers/MismatchHistoWalker.java index 10ef4a4e5..4a5c8051a 100755 --- a/java/src/org/broadinstitute/sting/playground/gatk/walkers/MismatchHistoWalker.java +++ b/java/src/org/broadinstitute/sting/playground/gatk/walkers/MismatchHistoWalker.java @@ -72,7 +72,7 @@ public class MismatchHistoWalker extends ReadWalker { public void onTraversalDone() { for ( int i = 0; i < mismatchCounts.length; i++ ) - System.out.println((i+1) + "\t" + mismatchCounts[i]); + out.println((i+1) + "\t" + mismatchCounts[i]); } private static Object resizeArray (Object oldArray, int newSize) { diff --git a/java/src/org/broadinstitute/sting/playground/gatk/walkers/SingleSampleGenotyper.java b/java/src/org/broadinstitute/sting/playground/gatk/walkers/SingleSampleGenotyper.java index 1c6445bd7..259c41d9c 100644 --- a/java/src/org/broadinstitute/sting/playground/gatk/walkers/SingleSampleGenotyper.java +++ b/java/src/org/broadinstitute/sting/playground/gatk/walkers/SingleSampleGenotyper.java @@ -126,7 +126,7 @@ public class SingleSampleGenotyper extends LocusWalker { if ( context.getLocation().getStart() % 1 == 0 ) { //System.out.printf("%s: %s %s %s %s%n", context.getLocation(), ref, bases, quals, rodString); - System.out.printf("%s %s %s %s\n", ref, bases, G.toString(), rodString); + out.printf("%s %s %s %s\n", ref, bases, G.toString(), rodString); } return 1; diff --git a/java/src/org/broadinstitute/sting/utils/cmdLine/ArgumentParser.java b/java/src/org/broadinstitute/sting/utils/cmdLine/ArgumentParser.java index 37cc4f749..9e0d9a8cc 100644 --- a/java/src/org/broadinstitute/sting/utils/cmdLine/ArgumentParser.java +++ b/java/src/org/broadinstitute/sting/utils/cmdLine/ArgumentParser.java @@ -412,8 +412,6 @@ public class ArgumentParser { String fullName = (argument.fullName().length() != 0) ? argument.fullName() : field.getName().trim().toLowerCase(); String shortName = (argument.shortName().length() != 0) ? argument.shortName() : fullName.substring(0,1); - if(shortName.length() != 1) - throw new IllegalArgumentException("Invalid short name: " + shortName); String description = argument.doc(); boolean isRequired = argument.required(); boolean isFlag = (field.getType() == Boolean.class) || (field.getType() == Boolean.TYPE);