name: bgzip
description: Block compression/decompression utility
links:
  homepage: https://www.htslib.org/
  documentation: https://www.htslib.org/doc/bgzip.html
  repository: https://github.com/samtools/htslib
references:
  doi: 10.1093/gigascience/giab007
license: MIT
requirements:
  commands: [ bgzip ]
argument_groups:
  - name: Inputs
    arguments:
    - name: --input
      type: file
      direction: input
      description: file to be compressed or decompressed
      required: true
  - name: Outputs
    arguments:
    - name: --output
      type: file
      direction: output
      description: compressed or decompressed output
      required: true
    - name: --index_name
      alternatives: -I
      type: file
      direction: output
      description: name of BGZF index file [file.gz.gzi]
  - name: Arguments
    arguments:
    - name: --offset
      alternatives: -b
      type: integer
      description: decompress at virtual file pointer (0-based uncompressed offset)
    - name: --decompress
      alternatives: -d
      type: boolean_true
      description: decompress the input file
    - name: --rebgzip
      alternatives: -g
      type: boolean_true
      description: use an index file to bgzip a file
    - name: --index
      alternatives: -i
      type: boolean_true
      description: compress and create BGZF index
    - name: --compress_level
      alternatives: -l
      type: integer
      description: compression level to use when compressing; 0 to 9, or -1 for default [-1]
      min: -1
      max: 9
    - name: --reindex
      alternatives: -r
      type: boolean_true
      description: (re)index the output file
    - name: --size
      alternatives: -s
      type: integer
      description: decompress INT bytes (uncompressed size)
      min: 0
    - name: --test
      alternatives: -t
      type: boolean_true
      description: test integrity of compressed file
    - name: --binary
      type: boolean_true
      description: Don't align blocks with text lines
resources:
  - type: bash_script
    text: |
      [[ "$par_decompress" == "false" ]] && unset par_decompress
      [[ "$par_rebgzip" == "false" ]] && unset par_rebgzip
      [[ "$par_index" == "false" ]] && unset par_index
      [[ "$par_reindex" == "false" ]] && unset par_reindex
      [[ "$par_test" == "false" ]] && unset par_test
      [[ "$par_binary" == "false" ]] && unset par_binary
      bgzip -c \
        ${meta_cpus:+--threads "${meta_cpus}"} \
        ${par_offset:+-b "${par_offset}"} \
        ${par_decompress:+-d} \
        ${par_rebgzip:+-g} \
        ${par_index:+-i} \
        ${par_index_name:+-I "${par_index_name}"} \
        ${par_compress_level:+-l "${par_compress_level}"} \
        ${par_reindex:+-r} \
        ${par_size:+-s "${par_size}"} \
        ${par_test:+-t} \
        ${par_binary:+--binary} \
        "$par_input" > "$par_output"
test_resources:
  - type: bash_script
    text: |
      set -e

      "$meta_executable" --input "$meta_resources_dir/test_data/test.vcf" --output "test.vcf.gz"

      echo ">> Checking output of compressing"
      [ ! -f "test.vcf.gz" ] && echo "Output file test.vcf.gz does not exist" && exit 1

      "$meta_executable" --input "test.vcf.gz" --output "test.vcf" --decompress

      echo ">> Checking output of decompressing"
      [ ! -f "test.vcf" ] && echo "Output file test.vcf does not exist" && exit 1

      echo ">> Checking original and decompressed files are the same"
      set +e
      cmp --silent -- "$meta_resources_dir/test_data/test.vcf" "test.vcf"
      [ $? -ne 0 ] && echo "files are different" && exit 1
      set -e
      
      echo "> Test successful"
  - type: file
    path: test_data

engines:
  - type: docker
    image: quay.io/biocontainers/htslib:1.19--h81da01d_0
    setup:
      - type: docker
        run: |
          bgzip -h | grep 'Version:' 2>&1 |  sed 's/Version:\s\(.*\)/bgzip: "\1"/' > /var/software_versions.txt
runners:
  - type: executable
  - type: nextflow