<tool name="RevertSam" id="picard_RevertSam" version="@TOOL_VERSION@.@WRAPPER_VERSION@">
  <description>revert SAM/BAM datasets to a previous state</description>
  <macros>
    <import>picard_macros.xml</import>
    <token name="@WRAPPER_VERSION@">1</token>
  </macros>
  <expand macro="requirements" />
  <command detect_errors="exit_code"><![CDATA[
    @java_options@
    picard
    RevertSam

    INPUT='$inputFile'
    OUTPUT="${outFile}"

    RESTORE_ORIGINAL_QUALITIES="${restore_original_qualities}"
    REMOVE_DUPLICATE_INFORMATION="${remove_duplicate_information}"
    REMOVE_ALIGNMENT_INFORMATION="${remove_alignment_information}"

    #for $attribute_to_clear in $attributes_to_clear:
      ATTRIBUTE_TO_CLEAR="${attribute_to_clear.attribute}"
    #end for

    SANITIZE="${sanitize}"
    MAX_DISCARD_FRACTION="${max_discard_fraction}"
    SAMPLE_ALIAS="${sample_alias}"
    LIBRARY_NAME="${library_name}"

    SORT_ORDER="${sort_order}"
    VALIDATION_STRINGENCY="${validation_stringency}"
    QUIET=true
    VERBOSITY=ERROR
    @TMPDIR_OPTION@

  ]]></command>
  <inputs>
    <param format="sam,bam" name="inputFile" type="data" multiple="True" label="Select SAM/BAM dataset or dataset collection" help="If empty, upload or import a SAM/BAM dataset"/>
    <param name="restore_original_qualities" type="boolean" checked="True" label="Restore original qualities from the OQ field to the QUAL field if available" help="RESTORE_ORIGINAL_QUALITIES; default=True"/>
    <param name="remove_duplicate_information" type="boolean" checked="True" label="Remove duplicate read flags from all reads" help="REMOVE_DUPLICATE_INFORMATION; Note that if this is true and REMOVE_ALIGNMENT_INFORMATION is set to False, the output may have the unusual but sometimes desirable trait of having unmapped reads that are marked as duplicates; default=True"/>
    <param name="remove_alignment_information" type="boolean" checked="True" label="Remove all alignment information from the file" help="REMOVE_ALIGNMENT_INFORMATION; default=True"/>
    <repeat name="attributes_to_clear" title="Clear attribute" min="0" help="You can provide multiple attributes">
      <param name="attribute" type="text" label="When removing alignment information, specify optional tags to remove (e.g., XM)" help="ATTRIBUTE_TO_CLEAR"/>
    </repeat>
    <param name="sanitize" type="boolean" label="Discard reads in order to produce a consistent output BAM" help="SANITIZE; WARNING: This option is potentially destructive. Reads discarded include (but are not limited to) paired reads with missing mates, duplicated records, records with mismatches in length of bases and qualities. This option can only be enabled if the output sort order is queryname and will always cause sorting to occur; default=False"/>
    <param name="max_discard_fraction" value="0.01" type="float" min="0.0" max="1.0" label="If SANITIZE=true and higher than MAX_DISCARD_FRACTION reads are discarded due to sanitization then the program will exit with an Exception instead of exiting cleanly" help="MAX_DISCARD_FRACTION; default=0.01"/>
    <param name="sample_alias" type="text" value="null" label="The sample alias to use in the reverted output file. This will override the existing sample alias in the file and is used only if all the read groups in the input file have the same sample alias" help="SAMPLE_ALIAS; default=Null"/>
    <param name="library_name" type="text" value="null" label="The library name to use in the reverted output file. This will override the existing sample alias in the file and is used only if all the read groups in the input file have the same sample alias" help="LIBRARY_NAME; default=Null"/>
    <param name="sort_order" type="select" label="The sort order to create the reverted output file with" help="SORT_ORDER; Picard default=queryname; Galaxy default=coordinate">
      <option value="coordinate" selected="True">Coordinate</option>
      <option value="queryname">Queryname</option>
      <option value="unsorted">Unsorted</option>
    </param>
    <expand macro="VS" />

  </inputs>

  <outputs>
    <data format="bam" name="outFile" label="${tool.name} on ${on_string}: Reverted BAM dataset"/>
  </outputs>

  <tests>
    <test>
      <param name="inputFile" value="picard_RevertSam.bam" ftype="bam"/>
      <param name="restore_original_qualities" value="True"/>
      <param name="remove_duplicate_information" value="True"/>
      <param name="remove_alignment_information" value="True"/>
      <param name="attribute" value="XM"/>
      <param name="sanitize" value="False"/>
      <param name="max_discard_fraction" value="0.01"/>
      <param name="sample_alias" value="null"/>
      <param name="library_name" value="null"/>
      <param name="sort_order" value="coordinate"/>
      <param name="validation_stringency" value="LENIENT"/>
      <output name="outFile" file="picard_RevertSam_test1.bam" ftype="bam" lines_diff="4"/>
    </test>
  </tests>


  <help>

**Purpose**

Reverts SAM or BAM files to a previous state by removing certain types of information and/or substituting in the original quality scores when available.

@dataset_collections@

@description@

  SORT_ORDER=SortOrder
  SO=SortOrder                  The sort order to create the reverted output file with.  Default value: queryname.
                                Possible values: {unsorted, queryname, coordinate}

  RESTORE_ORIGINAL_QUALITIES=Boolean
  OQ=Boolean                    True to restore original qualities from the OQ field to the QUAL field if available.
                                Default value: true. Possible values: {true, false}

  REMOVE_DUPLICATE_INFORMATION=Boolean
                                Remove duplicate read flags from all reads.  Note that if this is true and
                                REMOVE_ALIGNMENT_INFORMATION==false,  the output may have the unusual but sometimes
                                desirable trait of having unmapped reads that are marked as duplicates.  Default value:
                                true. Possible values: {true, false}

  REMOVE_ALIGNMENT_INFORMATION=Boolean
                                Remove all alignment information from the file.  Default value: true. TPossible values: {true, false}

  ATTRIBUTE_TO_CLEAR=String     When removing alignment information, the set of optional tags to remove.  This option may
                                be specified 0 or more times.

  SANITIZE=Boolean              WARNING: This option is potentially destructive. If enabled will discard reads in order
                                to produce a consistent output BAM. Reads discarded include (but are not limited to)
                                paired reads with missing mates, duplicated records, records with mismatches in length of
                                bases and qualities. This option can only be enabled if the output sort order is
                                queryname and will always cause sorting to occur.  Possible values: {true, false}

  MAX_DISCARD_FRACTION=Double   If SANITIZE=true and higher than MAX_DISCARD_FRACTION reads are discarded due to
                                sanitization thenthe program will exit with an Exception instead of exiting cleanly.
                                Output BAM will still be valid.  Default value: 0.01.

  SAMPLE_ALIAS=String
  ALIAS=String                  The sample alias to use in the reverted output file.  This will override the existing
                                sample alias in the file and is used only if all the read groups in the input file have
                                the same sample alias   Default value: null.

  LIBRARY_NAME=String
  LIB=String                    The library name to use in the reverted output file.  This will override the existing
                                sample alias in the file and is used only if all the read groups in the input file have
                                the same sample alias   Default value: null.

@more_info@

  </help>
  <expand macro="citations" />
</tool>
