rule bz2_to_gz:
    """**Convert fastq.bz2 files to fastq.gz 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 BZ2 file is decompressed with **pbunzip2** and redirected
           a pipe to **pigz** executable into a GZ output.
        #. the output is checked for integrity with **pigz**.
        #. the input BZ2 file is deleted.

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

    configuration requirements::

        compressor:
            - threads

    """
    input: "{dataset}.bz2"
    output: "{dataset}.gz"
    threads: config['compressor']['threads']
    run:
        # check integrity input
        cmd = "pbunzip2 {input} -p{threads} --test" 
        shell(cmd)

        # conversion
        cmd = "pbunzip2 {input} -p{threads} --stdout | pigz -p {threads}  > {output}"
        shell(cmd)

        # integrity output
        cmd = "pigz -p {threads} --test {output}" 
        shell(cmd)

        # remove original file
        cmd = "rm -f {input}"
        shell(cmd)

