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

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

    configuration requirements::

        compressor:
            - threads

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

        # conversion
        cmd = "pigz -d -c -p {threads} {input} | pbzip2 -p{threads} > {output}"
        shell(cmd)

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

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

