# Buggy code
def process_alignment(data, alt_input=None):
    """Do an alignment of fastq files, preparing a sorted BAM output file."""
    data = cwlutils.normalize_missing(utils.to_single_data(data))
    data = cwlutils.unpack_tarballs(data, data)
    fastq1, fastq2 = dd.get_input_sequence_files(data)
    if alt_input:
        fastq1, fastq2 = alt_input
    config = data["config"]
    aligner = config["algorithm"].get("aligner", None)
    if fastq1 and objectstore.file_exists_or_remote(fastq1) and aligner:
        logger.info(
            "Aligning lane %s with %s aligner" % (data["rgnames"]["lane"], aligner)
        )
        data = align_to_sort_bam(fastq1, fastq2, aligner, data)
        if dd.get_correct_umis(data):
            data["work_bam"] = postalign.correct_umis(data)
        if dd.get_umi_consensus(data):
            data["umi_bam"] = dd.get_work_bam(data)
            if fastq2:
                f1, f2, avg_cov = postalign.umi_consensus(data)
                data["config"]["algorithm"]["rawumi_avg_cov"] = avg_cov
                del data["config"]["algorithm"]["umi_type"]
                data["config"]["algorithm"]["mark_duplicates"] = False
                data = align_to_sort_bam(f1, f2, aligner, data)
            else:
                raise ValueError(
                    "Single fastq input for UMI processing; fgbio needs paired reads: %s"
                    % dd.get_sample_name(data)
                )
        data = _add_supplemental_bams(data)
    elif (
        fastq1 and objectstore.file_exists_or_remote(fastq1) and fastq1.endswith(".bam")
    ):
        sort_method = config["algorithm"].get("bam_sort")
        bamclean = config["algorithm"].get("bam_clean")
        if bamclean is True or bamclean == "picard":
            if sort_method and sort_method != "coordinate":
                raise ValueError(
                    "Cannot specify `bam_clean: picard` with `bam_sort` other than coordinate: %s"
                    % sort_method
                )
            out_bam = cleanbam.picard_prep(
                fastq1, data["rgnames"], dd.get_ref_file(data), data["dirs"], data
            )
        elif bamclean == "fixrg":
            out_bam = cleanbam.fixrg(
                fastq1, data["rgnames"], dd.get_ref_file(data), data["dirs"], data
            )
        elif bamclean == "remove_extracontigs":
            out_bam = cleanbam.remove_extracontigs(fastq1, data)
        elif sort_method:
            runner = broad.runner_from_path("picard", config)
            out_file = os.path.join(
                data["dirs"]["work"],
                "{}-sort.bam".format(os.path.splitext(os.path.basename(fastq1))[0]),
            )
            if not utils.file_exists(out_file):
                work_dir = utils.safe_makedir(
                    os.path.join(
                        dd.get_work_dir(data), "bamclean", dd.get_sample_name(data)
                    )
                )
                out_file = os.path.join(
                    work_dir, "{}-sort.bam".format(dd.get_sample_name(data))
                )
            out_bam = runner.run_fn("picard_sort", fastq1, sort_method, out_file)
        else:
            out_bam = _link_bam_file(
                fastq1,
                os.path.join(
                    dd.get_work_dir(data), "prealign", dd.get_sample_name(data)
                ),
                data,
            )
        bam.index(out_bam, data["config"])
        bam.check_header(
            out_bam, data["rgnames"], dd.get_ref_file(data), data["config"]
        )
        dedup_bam = postalign.dedup_bam(out_bam, data)
        bam.index(dedup_bam, data["config"])
        data["work_bam"] = dedup_bam
    elif (
        fastq1
        and objectstore.file_exists_or_remote(fastq1)
        and fastq1.endswith(".cram")
    ):
        data["work_bam"] = fastq1
    elif fastq1 is None and not dd.get_aligner(data):
        data["config"]["algorithm"]["variantcaller"] = False
        data["work_bam"] = None
    elif not fastq1:
        raise ValueError(
            "No 'files' specified for input sample: %s" % dd.get_sample_name(data)
        )
    elif "kraken" in config["algorithm"]:  # kraken doesn's need bam
        pass
    else:
        raise ValueError(
            "Could not process input file from sample configuration. \n"
            + fastq1
            + "\nIs the path to the file correct or is empty?\n"
            + "If it is a fastq file (not pre-aligned BAM or CRAM), "
            "is an aligner specified in the input configuration?"
        )
    if data.get("work_bam"):
        # Add stable 'align_bam' target to use for retrieving raw alignment
        data["align_bam"] = data["work_bam"]
        data = _add_hla_files(data)
    return [[data]]


# 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 process_alignment(data, alt_input=None):
    """Do an alignment of fastq files, preparing a sorted BAM output file."""
    data = cwlutils.normalize_missing(utils.to_single_data(data))
    data = cwlutils.unpack_tarballs(data, data)
    fastq1, fastq2 = dd.get_input_sequence_files(data)
    if alt_input:
        fastq1, fastq2 = alt_input
    config = data["config"]
    aligner = config["algorithm"].get("aligner", None)
    if fastq1 and objectstore.file_exists_or_remote(fastq1) and aligner:
        logger.info(
            "Aligning lane %s with %s aligner" % (data["rgnames"]["lane"], aligner)
        )
        data = align_to_sort_bam(fastq1, fastq2, aligner, data)
        if dd.get_correct_umis(data):
            data["work_bam"] = postalign.correct_umis(data)
        if dd.get_umi_consensus(data):
            data["umi_bam"] = dd.get_work_bam(data)
            if fastq2:
                f1, f2, avg_cov = postalign.umi_consensus(data)
                data["config"]["algorithm"]["rawumi_avg_cov"] = avg_cov
                del data["config"]["algorithm"]["umi_type"]
                data["config"]["algorithm"]["mark_duplicates"] = False
                data = align_to_sort_bam(f1, f2, aligner, data)
            else:
                raise ValueError(
                    "Single fastq input for UMI processing; fgbio needs paired reads: %s"
                    % dd.get_sample_name(data)
                )
        data = _add_supplemental_bams(data)
    elif (
        fastq1 and objectstore.file_exists_or_remote(fastq1) and fastq1.endswith(".bam")
    ):
        sort_method = config["algorithm"].get("bam_sort")
        bamclean = config["algorithm"].get("bam_clean")
        if bamclean is True or bamclean == "picard":
            if sort_method and sort_method != "coordinate":
                raise ValueError(
                    "Cannot specify `bam_clean: picard` with `bam_sort` other than coordinate: %s"
                    % sort_method
                )
            ref_file = dd.get_ref_file(data)
            out_bam = cleanbam.picard_prep(
                fastq1, data["rgnames"], ref_file, data["dirs"], data
            )
        elif bamclean == "fixrg":
            out_bam = cleanbam.fixrg(
                fastq1, data["rgnames"], dd.get_ref_file(data), data["dirs"], data
            )
        elif bamclean == "remove_extracontigs":
            out_bam = cleanbam.remove_extracontigs(fastq1, data)
        elif sort_method:
            runner = broad.runner_from_path("picard", config)
            out_file = os.path.join(
                data["dirs"]["work"],
                "{}-sort.bam".format(os.path.splitext(os.path.basename(fastq1))[0]),
            )
            if not utils.file_exists(out_file):
                work_dir = utils.safe_makedir(
                    os.path.join(
                        dd.get_work_dir(data), "bamclean", dd.get_sample_name(data)
                    )
                )
                out_file = os.path.join(
                    work_dir, "{}-sort.bam".format(dd.get_sample_name(data))
                )
            out_bam = runner.run_fn("picard_sort", fastq1, sort_method, out_file)
        else:
            out_bam = _link_bam_file(
                fastq1,
                os.path.join(
                    dd.get_work_dir(data), "prealign", dd.get_sample_name(data)
                ),
                data,
            )
        bam.index(out_bam, data["config"])
        bam.check_header(
            out_bam, data["rgnames"], dd.get_ref_file(data), data["config"]
        )
        dedup_bam = postalign.dedup_bam(out_bam, data)
        bam.index(dedup_bam, data["config"])
        data["work_bam"] = dedup_bam
    elif (
        fastq1
        and objectstore.file_exists_or_remote(fastq1)
        and fastq1.endswith(".cram")
    ):
        data["work_bam"] = fastq1
    elif fastq1 is None and not dd.get_aligner(data):
        data["config"]["algorithm"]["variantcaller"] = False
        data["work_bam"] = None
    elif not fastq1:
        raise ValueError(
            "No 'files' specified for input sample: %s" % dd.get_sample_name(data)
        )
    elif "kraken" in config["algorithm"]:  # kraken doesn's need bam
        pass
    else:
        raise ValueError(
            "Could not process input file from sample configuration. \n"
            + fastq1
            + "\nIs the path to the file correct or is empty?\n"
            + "If it is a fastq file (not pre-aligned BAM or CRAM), "
            "is an aligner specified in the input configuration?"
        )
    if data.get("work_bam"):
        # Add stable 'align_bam' target to use for retrieving raw alignment
        data["align_bam"] = data["work_bam"]
        data = _add_hla_files(data)
    return [[data]]