# Buggy code
def picard_reorder(picard, in_bam, ref_file, out_file):
    """Reorder BAM file to match reference file ordering."""
    if not file_exists(out_file):
        with tx_tmpdir(picard._config) as tmp_dir:
            with file_transaction(picard._config, out_file) as tx_out_file:
                opts = [
                    ("INPUT", in_bam),
                    ("OUTPUT", tx_out_file),
                    ("REFERENCE", ref_file),
                    ("ALLOW_INCOMPLETE_DICT_CONCORDANCE", "true"),
                    ("TMP_DIR", tmp_dir),
                ]
                picard.run("ReorderSam", opts)
    return out_file


# Traceback (subprocess.CalledProcessError)
Uncaught exception occurred

Traceback (most recent call last):

File "/path/anaconda/lib/python3.6/site-packages/bcbio/provenance/do.py", line 26, in run
_do_run(cmd, checks, log_stdout, env=env)

File "/path/anaconda/lib/python3.6/site-packages/bcbio/provenance/do.py", line 106, in _do_run
raise subprocess.CalledProcessError(exitcode, error_msg)

subprocess.CalledProcessError: Command 'set -o pipefail; unset JAVA_HOME &amp;&amp; export PATH=/path/anaconda/bin:"$PAT

INFO    2019-10-18 21:14:01     ReorderSam

********** NOTE: Picard's command line syntax is changing.

**********

********** For more information, please see:

********** https://github.com/broadinstitute/picard/wiki/Command-Line-Syntax-Transition-For-Users-(Pre-Transition)

**********

********** The command line looks like this in the new syntax:

**********

**********    ReorderSam -INPUT /path/bam/sample-ready.bam -OUTPUT /path

********** The command line looks like this in the new syntax:

**********

**********    ReorderSam -INPUT /path/bam/sample.bam -OUTPUT /path

**********

ERROR: Unrecognized option: REFERENCE

USAGE: ReorderSam [options]

Documentation: http://broadinstitute.github.io/picard/command-line-overview.html#ReorderSam

Not to be confused with SortSam which sorts a SAM or BAM file with a valid sequence dictionary, ReorderSam reorders

reads in a SAM/BAM file to match the contig ordering in a provided reference file, as determined by exact name matching

of contigs.  Reads mapped to contigs absent in the new reference are unmapped. Runs substantially faster if the input is

an indexed BAM file.

Example

java -jar picard.jar ReorderSam \

INPUT=sample.bam \

OUTPUT=reordered.bam \

SEQUENCE_DICTIONARY=reference_with_different_order.dict

Version: 2.21.1-SNAPSHOT

Options:

--help

-h                            Displays options specific to this tool.

--stdhelp

-H                            Displays options specific to this tool AND options common to all Picard command line

tools.

--version                     Displays program version.

INPUT=File

I=File                        Input file (SAM or BAM) to extract reads from.  Required.

OUTPUT=File

O=File                        Output file (SAM or BAM) to write extracted reads to.  Required.

SEQUENCE_DICTIONARY=File

SD=File                       A Sequence Dictionary for the OUTPUT file (can be read from one of the following file

types (SAM, BAM, VCF, BCF, Interval List, Fasta, or Dict)  Required.

ALLOW_INCOMPLETE_DICT_CONCORDANCE=Boolean

S=Boolean                     If true, allows only a partial overlap of the original contigs with the new reference

sequence contigs.  By default, this tool requires a corresponding contig in the new

reference for each read contig  Default value: false. This option can be set to 'null' to

clear the default value. Possible values: {true, false}

ALLOW_CONTIG_LENGTH_DISCORDANCE=Boolean

U=Boolean                     If true, then permits mapping from a read contig to a new reference contig with the same

name but a different length.  Highly dangerous, only use if you know what you are doing.

Default value: false. This option can be set to 'null' to clear the default value.

Possible values: {true, false}

TMP_DIR=File                  One or more directories with space available to be used by this program for temporary

storage of working files  Default value: null. This option may be specified 0 or more

times.

VERBOSITY=LogLevel            Control verbosity of logging.  Default value: INFO. This option can be set to 'null' to

clear the default value. Possible values: {ERROR, WARNING, INFO, DEBUG}

QUIET=Boolean                 Whether to suppress job-summary info on System.err.  Default value: false. This option can

be set to 'null' to clear the default value. Possible values: {true, false}

VALIDATION_STRINGENCY=ValidationStringency

Validation stringency for all SAM files read by this program.  Setting stringency to

SILENT can improve performance when processing a BAM file in which variable-length data

(read, qualities, tags) do not otherwise need to be decoded.  Default value: STRICT. This

option can be set to 'null' to clear the default value. Possible values: {STRICT, LENIENT,

SILENT}

COMPRESSION_LEVEL=Integer     Compression level for all compressed files created (e.g. BAM and VCF).  Default value: 5.

This option can be set to 'null' to clear the default value.

MAX_RECORDS_IN_RAM=Integer    When writing files that need to be sorted, this will specify the number of records stored

in RAM before spilling to disk. Increasing this number reduces the number of file handles

needed to sort the file, and increases the amount of RAM needed.  Default value: 500000.

This option can be set to 'null' to clear the default value.

CREATE_INDEX=Boolean          Whether to create a BAM index when writing a coordinate-sorted BAM file.  Default value:

false. This option can be set to 'null' to clear the default value. Possible values:

{true, false}

CREATE_MD5_FILE=Boolean       Whether to create an MD5 digest for any BAM or FASTQ files created.    Default value:

false. This option can be set to 'null' to clear the default value. Possible values:

{true, false}

REFERENCE_SEQUENCE=File

R=File                        Reference sequence file.  Default value: null.

GA4GH_CLIENT_SECRETS=String   Google Genomics API client_secrets.json file path.  Default value: client_secrets.json.

This option can be set to 'null' to clear the default value.

USE_JDK_DEFLATER=Boolean

USE_JDK_DEFLATER=Boolean      Use the JDK Deflater instead of the Intel Deflater for writing compressed output  Default

value: false. This option can be set to 'null' to clear the default value. Possible

values: {true, false}

USE_JDK_INFLATER=Boolean

USE_JDK_INFLATER=Boolean      Use the JDK Inflater instead of the Intel Inflater for reading compressed input  Default

value: false. This option can be set to 'null' to clear the default value. Possible

values: {true, false}

OPTIONS_FILE=File             File of OPTION_NAME=value pairs.  No positional parameters allowed.  Unlike command-line

options, unrecognized options are ignored.  A single-valued option set in an options file

may be overridden by a subsequent command-line option.  A line starting with '#' is

considered a comment.  Required.

' returned non-zero exit status 1.

[2019-10-18T21:14Z] seskscpn129.prim.scp: Uncaught exception occurred

Traceback (most recent call last):

File "/path/anaconda/lib/python3.6/site-packages/bcbio/provenance/do.py", line 26, in run

_do_run(cmd, checks, log_stdout, env=env)

File "/path/anaconda/lib/python3.6/site-packages/bcbio/provenance/do.py", line 106, in _do_run

raise subprocess.CalledProcessError(exitcode, error_msg)

subprocess.CalledProcessError: Command 'set -o pipefail; unset JAVA_HOME &amp;&amp; export PATH=/path/anaconda/bin:"$PAT

INFO    2019-10-18 21:14:01     ReorderSam

# Fixed code
def picard_reorder(picard, in_bam, ref_file, out_file):
    """Reorder BAM file to match reference file ordering."""
    if not file_exists(out_file):
        with tx_tmpdir(picard._config) as tmp_dir:
            with file_transaction(picard._config, out_file) as tx_out_file:
                dict_file = "%s.dict" % os.path.splitext(ref_file)[0]
                opts = [
                    ("INPUT", in_bam),
                    ("OUTPUT", tx_out_file),
                    ("SEQUENCE_DICTIONARY", dict_file),
                    ("ALLOW_INCOMPLETE_DICT_CONCORDANCE", "true"),
                    ("TMP_DIR", tmp_dir),
                ]
                picard.run("ReorderSam", opts)
    return out_file