rule bz2_to_dsrc:
    """**Convert fastq.bz2 files to fastq.dsrc files**

    Here are the steps followed by the rule. Any failure stops the
    process and the original file is untouched. If all succeed, the
    input is deleted.

        #. the input BZ2 file is checked for integrity.
        #. the input B22 file is decompressed with **pbunzip2** and redirected
           a pipe to **dsrc** executable into a DSRC output.
        #. the input GZ file is deleted.

    :param input: {dataset}.bz2
    :param output: {dataset}.dsrc 
    :third-party executables: pbunzip2 and dsrc

    configuration requirements::

        compressor:
            - threads

    """
    input: "{dataset}.bz2"
    output: "{dataset}.dsrc"
    params: "-m2"  # type of compression (slow but best ratio
    threads: config["compressor"]["threads"]
    run:
        # check integrity of the input file
        cmd = "pbunzip2 {input} -p{threads} --test" 
        shell(cmd)

        # Here is the real transformation
	# Conversion of bz2 to dsrc format.
	# dsrc -s  means use stdin/stdout
        cmd = "pbunzip2 {input} -p{threads} --stdout | dsrc c -s -t{threads} {params} {output}"
        shell(cmd)

        # IF and ONLY IF this is correct, we can now delete the input file
        cmd = "rm -f {input}"
        shell(cmd)

