Package picard.sam

## Class DownsampleSam

- java.lang.Object
- - picard.cmdline.CommandLineProgram
  - - picard.sam.DownsampleSam

-     @DocumentedFeature
      public class DownsampleSam
      extends CommandLineProgram

  ### Summary

  This tool applies a downsampling algorithm to a SAM or BAM file to retain only a (deterministically random) subset of the reads. Reads from the same template (e.g. read-pairs, secondary and supplementary reads) are all either kept or discarded as a unit, with the goal of retaining reads from `PROBABILITY * (input templates)`. The results will contain approximately `PROBABILITY * (input reads)`, however for very small probabilities this may not be the case. A number of different downsampling strategies are supported using the `STRATEGY` option:
  ConstantMemory
    Downsamples a stream or file of SAMRecords using a hash-projection strategy such that it can run in constant memory. The downsampling is stochastic, and therefore the actual retained proportion will vary around the requested proportion. Due to working in fixed memory this strategy is good for large inputs, and due to the stochastic nature the accuracy of this strategy is highest with a high number of output records, and diminishes at low output volumes.
  HighAccuracy
    Attempts (but does not guarantee) to provide accuracy up to a specified limit. Accuracy is defined as emitting a proportion of reads as close to the requested proportion as possible. In order to do so this strategy requires memory that is proportional to the number of template names in the incoming stream of reads, and will thus require large amounts of memory when running on large input files.
  Chained
    Attempts to provide a compromise strategy that offers some of the advantages of both the ConstantMemory and HighAccuracy strategies. Uses a ConstantMemory strategy to downsample the incoming stream to approximately the desired proportion, and then a HighAccuracy strategy to finish. Works in a single pass, and will provide accuracy close to (but often not as good as) HighAccuracy while requiring memory proportional to the set of reads emitted from the ConstantMemory strategy to the HighAccuracy strategy. Works well when downsampling large inputs to small proportions (e.g. downsampling hundreds of millions of reads and retaining only 2%. Should be accurate 99.9% of the time when the input contains more than 50,000 templates (read names). For smaller inputs, HighAccuracy is recommended instead.
  The number of records written can be output to a `CollectQualityYieldMetrics.QualityYieldMetrics` metrics file via the `METRICS_FILE`.

  ### Usage examples:

  #### Downsample file, keeping about 10% of the reads

       java -jar picard.jar DownsampleSam \
             I=input.bam \
             O=downsampled.bam \
             P=0.1

  #### Downsample file, keeping 2% of the reads

       java -jar picard.jar DownsampleSam \
             I=input.bam \
             O=downsampled.bam \
             STRATEGY=Chained \
             P=0.02 \
             ACCURACY=0.0001

  #### Downsample file, keeping 0.001% of the reads (may require more memory)

       java -jar picard.jar DownsampleSam \
             I=input.bam \
             O=downsampled.bam \
             STRATEGY=HighAccuracy \
             P=0.00001 \
             ACCURACY=0.0000001

- - ### Field Summary

    Fields
    Modifier and Type | Field | Description
    --- | --- | ---
    `double` | `ACCURACY` |
    `File` | `INPUT` |
    `File` | `METRICS_FILE` |
    `File` | `OUTPUT` |
    `double` | `PROBABILITY` |
    `Integer` | `RANDOM_SEED` |
    `htsjdk.samtools.DownsamplingIteratorFactory.Strategy` | `STRATEGY` |

    - ### Fields inherited from class picard.cmdline.CommandLineProgram

      `COMPRESSION_LEVEL, CREATE_INDEX, CREATE_MD5_FILE, GA4GH_CLIENT_SECRETS, MAX_RECORDS_IN_RAM, QUIET, REFERENCE_SEQUENCE, referenceSequence, specialArgumentsCollection, TMP_DIR, USE_JDK_DEFLATER, USE_JDK_INFLATER, VALIDATION_STRINGENCY, VERBOSITY`

  - ### Constructor Summary

    Constructors
    Constructor | Description
    --- | ---
    `DownsampleSam()` |

  - ### Method Summary

    All Methods Instance Methods Concrete Methods
    Modifier and Type | Method | Description
    --- | --- | ---
    `protected String[]` | `customCommandLineValidation()` | Put any custom command-line validation in an override of this method.
    `protected int` | `doWork()` | Do the work after command line has been parsed.
    `protected ReferenceArgumentCollection` | `makeReferenceArgumentCollection()` |

    - ### Methods inherited from class picard.cmdline.CommandLineProgram

      `getCommandLine, getCommandLineParser, getDefaultHeaders, getFaqLink, getMetricsFile, getStandardUsagePreamble, getStandardUsagePreamble, getVersion, hasWebDocumentation, instanceMain, instanceMainWithExit, parseArgs, requiresReference, setDefaultHeaders, useLegacyParser`

    - ### Methods inherited from class java.lang.Object

      `clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait`

- - ### Field Detail

    - #### INPUT

          @Argument(shortName="I",
                    doc="The input SAM or BAM file to downsample.")
          public File INPUT

    - #### OUTPUT

          @Argument(shortName="O",
                    doc="The output, downsampled, SAM or BAM file to write.")
          public File OUTPUT

    - #### STRATEGY

          @Argument(shortName="S",
                    doc="The downsampling strategy to use. See usage for discussion.")
          public htsjdk.samtools.DownsamplingIteratorFactory.Strategy STRATEGY

    - #### RANDOM_SEED

          @Argument(shortName="R",
                    doc="Random seed used for deterministic results. Setting to null will cause multiple invocations to produce different results.")
          public Integer RANDOM_SEED

    - #### PROBABILITY

          @Argument(shortName="P",
                    doc="The probability of keeping any individual read, between 0 and 1.")
          public double PROBABILITY

    - #### ACCURACY

          @Argument(shortName="A",
                    doc="The accuracy that the downsampler should try to achieve if the selected strategy supports it. Note that accuracy is never guaranteed, but some strategies will attempt to provide accuracy within the requested bounds.Higher accuracy will generally require more memory.")
          public double ACCURACY

    - #### METRICS_FILE

          @Argument(shortName="M",
                    doc="The metrics file (of type QualityYieldMetrics) which will contain information about the downsampled file.",
                    optional=true)
          public File METRICS_FILE

  - ### Constructor Detail

    - #### DownsampleSam

          public DownsampleSam()

  - ### Method Detail

    - #### customCommandLineValidation

          protected String[] customCommandLineValidation()

      Description copied from class: `CommandLineProgram`
      Put any custom command-line validation in an override of this method. clp is initialized at this point and can be used to print usage and access argv. Any options set by command-line parser can be validated.
      Overrides:
        `customCommandLineValidation` in class `CommandLineProgram`
      Returns:
        null if command line is valid. If command line is invalid, returns an array of error message to be written to the appropriate place.

    - #### doWork

          protected int doWork()

      Description copied from class: `CommandLineProgram`
      Do the work after command line has been parsed. RuntimeException may be thrown by this method, and are reported appropriately.
      Specified by:
        `doWork` in class `CommandLineProgram`
      Returns:
        program exit status.

    - #### makeReferenceArgumentCollection

          protected ReferenceArgumentCollection makeReferenceArgumentCollection()

      Overrides:
        `makeReferenceArgumentCollection` in class `CommandLineProgram`