From 2d939443adfcce61ab59e01e14c1b1435e50068a Mon Sep 17 00:00:00 2001 From: emmarousseau Date: Thu, 23 May 2024 15:40:39 +0200 Subject: [PATCH] Fasta component --- CHANGELOG.md | 2 + src/samtools/samtools_fasta/config.vsh.yaml | 189 ++++++++++++++++++ src/samtools/samtools_fasta/help.txt | 80 ++++++++ src/samtools/samtools_fasta/script.sh | 40 ++++ src/samtools/samtools_fasta/test.sh | 96 +++++++++ src/samtools/samtools_fasta/test_data/a.1.fa | 6 + src/samtools/samtools_fasta/test_data/a.2.fa | 6 + src/samtools/samtools_fasta/test_data/a.bam | Bin 0 -> 184 bytes src/samtools/samtools_fasta/test_data/a.fa | 12 ++ src/samtools/samtools_fasta/test_data/a.sam | 7 + src/samtools/samtools_fasta/test_data/half.fa | 6 + .../samtools_fasta/test_data/script.sh | 11 + 12 files changed, 455 insertions(+) create mode 100644 src/samtools/samtools_fasta/config.vsh.yaml create mode 100644 src/samtools/samtools_fasta/help.txt create mode 100644 src/samtools/samtools_fasta/script.sh create mode 100644 src/samtools/samtools_fasta/test.sh create mode 100644 src/samtools/samtools_fasta/test_data/a.1.fa create mode 100644 src/samtools/samtools_fasta/test_data/a.2.fa create mode 100644 src/samtools/samtools_fasta/test_data/a.bam create mode 100644 src/samtools/samtools_fasta/test_data/a.fa create mode 100644 src/samtools/samtools_fasta/test_data/a.sam create mode 100644 src/samtools/samtools_fasta/test_data/half.fa create mode 100755 src/samtools/samtools_fasta/test_data/script.sh diff --git a/CHANGELOG.md b/CHANGELOG.md index 151b7a41..bac18769 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -47,6 +47,8 @@ - `samtools/samtools_collate`: Shuffles and groups reads in SAM/BAM/CRAM files together by their names (PR #42). - `samtools/samtools_view`: Views and converts SAM/BAM/CRAM files (PR #48). - `samtools/samtools_fastq`: Converts a SAM/BAM/CRAM file to FASTQ (PR #52). + - `samtools/samtools_fastq`: Converts a SAM/BAM/CRAM file to FASTA (PR #53). + * `falco`: A C++ drop-in replacement of FastQC to assess the quality of sequence read data (PR #43). diff --git a/src/samtools/samtools_fasta/config.vsh.yaml b/src/samtools/samtools_fasta/config.vsh.yaml new file mode 100644 index 00000000..e5651789 --- /dev/null +++ b/src/samtools/samtools_fasta/config.vsh.yaml @@ -0,0 +1,189 @@ +name: samtools_fasta +namespace: samtools +description: Converts a SAM, BAM or CRAM to FASTA format. +keywords: [fasta, bam, sam, cram] +links: + homepage: https://www.htslib.org/ + documentation: https://www.htslib.org/doc/samtools-fasta.html + repository: https://github.com/samtools/samtools +references: + doi: [10.1093/bioinformatics/btp352, 10.1093/gigascience/giab008] +license: MIT/Expat + +argument_groups: + - name: Inputs + arguments: + - name: --input + type: file + description: input SAM/BAM/CRAM file + required: true + - name: Outputs + arguments: + - name: --output + type: file + description: output FASTA file + required: true + direction: output + - name: Options + arguments: + - name: --no_suffix + alternatives: -n + type: boolean_true + description: | + By default, either '/1' or '/2' is added to the end of read names where the corresponding + READ1 or READ2 FLAG bit is set. Using -n causes read names to be left as they are. + - name: --suffix + alternatives: -N + type: boolean_true + description: | + Always add either '/1' or '/2' to the end of read names even when put into different files. + - name: --use_oq + alternatives: -O + type: boolean_true + description: | + Use quality values from OQ tags in preference to standard quality string if available. + - name: --singleton + alternatives: -s + type: file + description: write singleton reads to FILE. + - name: --copy_tags + alternatives: -t + type: boolean_true + description: | + Copy RG, BC and QT tags to the FASTA header line, if they exist. + - name: --copy_tags_list + alternatives: -T + type: string + description: | + Specify a comma-separated list of tags to copy to the FASTA header line, if they exist. + TAGLIST can be blank or * to indicate all tags should be copied to the output. If using *, + be careful to quote it to avoid unwanted shell expansion. + - name: --read1 + alternatives: -1 + type: file + description: | + Write reads with the READ1 FLAG set (and READ2 not set) to FILE instead of outputting them. + If the -s option is used, only paired reads will be written to this file. + direction: output + - name: --read2 + alternatives: -2 + type: file + description: | + Write reads with the READ2 FLAG set (and READ1 not set) to FILE instead of outputting them. + If the -s option is used, only paired reads will be written to this file. + direction: output + - name: --output_reads + alternatives: -o + type: file + description: | + Write reads with either READ1 FLAG or READ2 flag set to FILE instead of outputting them to stdout. + This is equivalent to -1 FILE -2 FILE. + direction: output + - name: --output_reads_both + alternatives: -0 + type: file + description: | + Write reads where the READ1 and READ2 FLAG bits set are either both set or both unset to FILE + instead of outputting them. + direction: output + - name: --filter_flags + alternatives: -f + type: integer + description: | + Only output alignments with all bits set in INT present in the FLAG field. INT can be specified + in hex by beginning with `0x' (i.e. /^0x[0-9A-F]+/) or in octal by beginning with `0' + (i.e. /^0[0-7]+/). + default: 0 + - name: --excl_flags + alternatives: -F + type: string + description: | + Do not output alignments with any bits set in INT present in the FLAG field. INT can be specified + in hex by beginning with `0x' (i.e. /^0x[0-9A-F]+/) or in octal by beginning with `0' + (i.e. /^0[0-7]+/). This defaults to 0x900 representing filtering of secondary and + supplementary alignments. + default: 0x900 + - name: --incl_flags + alternatives: --rf + type: string + description: | + Only output alignments with any bits set in INT present in the FLAG field. INT can be specified + in hex by beginning with `0x' (i.e. /^0x[0-9A-F]+/), in octal by beginning with `0' + (i.e. /^0[0-7]+/), as a decimal number not beginning with '0' or as a comma-separated list of + flag names. + default: 0 + - name: --excl_flags_all + alternatives: -G + type: integer + description: | + Only EXCLUDE reads with all of the bits set in INT present in the FLAG field. INT can be specified + in hex by beginning with `0x' (i.e. /^0x[0-9A-F]+/) or in octal by beginning with `0' + (i.e. /^0[0-7]+/). + default: 0 + - name: --aux_tag + alternatives: -d + type: string + description: | + Only output alignments containing an auxiliary tag matching both TAG and VAL. If VAL is omitted + then any value is accepted. The tag types supported are i, f, Z, A and H. "B" arrays are not + supported. This is comparable to the method used in samtools view --tag. The option may be specified + multiple times and is equivalent to using the --aux_tag_file option. + - name: --aux_tag_file + alternatives: -D + type: string + description: | + Only output alignments containing an auxiliary tag matching TAG and having a value listed in FILE. + The format of the file is one line per value. This is equivalent to specifying --aux_tag multiple times. + - name: --casava + alternatives: -i + type: boolean_true + description: add Illumina Casava 1.8 format entry to header (eg 1:N:0:ATCACG) + - name: --compression + alternatives: -c + type: integer + description: set compression level when writing gz or bgzf fasta files. + default: 0 + - name: --index1 + alternatives: --i1 + type: file + description: write first index reads to FILE. + - name: --index2 + alternatives: --i2 + type: file + description: write second index reads to FILE. + - name: --barcode_tag + type: string + description: Auxiliary tag to find index reads in. + default: BC + - name: --quality_tag + type: string + description: Auxiliary tag to find index quality in. + default: QT + - name: --index_format + type: string + description: | + string to describe how to parse the barcode and quality tags. For example: + [i14i8]: the first 14 characters are index 1, the next 8 characters are index 2. + [n8i14]: ignore the first 8 characters, and use the next 14 characters for index 1. + If the tag contains a separator, then the numeric part can be replaced with '*' to mean + 'read until the separator or end of tag', for example: [n*i*]. + +resources: + - type: bash_script + path: script.sh +test_resources: + - type: bash_script + path: test.sh + - type: file + path: test_data +engines: + - type: docker + image: quay.io/biocontainers/samtools:1.19.2--h50ea8bc_1 + setup: + - type: docker + run: | + samtools --version 2>&1 | grep -E '^(samtools|Using htslib)' | \ + sed 's#Using ##;s# \([0-9\.]*\)$#: \1#' > /var/software_versions.txt +runners: +- type: executable +- type: nextflow diff --git a/src/samtools/samtools_fasta/help.txt b/src/samtools/samtools_fasta/help.txt new file mode 100644 index 00000000..39ed0d00 --- /dev/null +++ b/src/samtools/samtools_fasta/help.txt @@ -0,0 +1,80 @@ +``` +samtools fastq +``` + +Usage: samtools fastq [options...] + +Description: +Converts a SAM, BAM or CRAM to FASTQ format. + +Options: + -0 FILE write reads designated READ_OTHER to FILE + -1 FILE write reads designated READ1 to FILE + -2 FILE write reads designated READ2 to FILE + -o FILE write reads designated READ1 or READ2 to FILE + note: if a singleton file is specified with -s, only + paired reads will be written to the -1 and -2 files. + -d, --tag TAG[:VAL] + only include reads containing TAG, optionally with value VAL + -f, --require-flags INT + only include reads with all of the FLAGs in INT present [0] + -F, --excl[ude]-flags INT + only include reads with none of the FLAGs in INT present [0x900] + --rf, --incl[ude]-flags INT + only include reads with any of the FLAGs in INT present [0] + -G INT only EXCLUDE reads with all of the FLAGs in INT present [0] + -n don't append /1 and /2 to the read name + -N always append /1 and /2 to the read name + -O output quality in the OQ tag if present + -s FILE write singleton reads designated READ1 or READ2 to FILE + -t copy RG, BC and QT tags to the FASTQ header line + -T TAGLIST copy arbitrary tags to the FASTQ header line, '*' for all + -v INT default quality score if not given in file [1] + -i add Illumina Casava 1.8 format entry to header (eg 1:N:0:ATCACG) + -c INT compression level [0..9] to use when writing bgzf files [1] + --i1 FILE write first index reads to FILE + --i2 FILE write second index reads to FILE + --barcode-tag TAG + Barcode tag [BC] + --quality-tag TAG + Quality tag [QT] + --index-format STR + How to parse barcode and quality tags + + --input-fmt-option OPT[=VAL] + Specify a single input file format option in the form + of OPTION or OPTION=VALUE + --reference FILE + Reference sequence FASTA FILE [null] + -@, --threads INT + Number of additional threads to use [0] + --verbosity INT + Set level of verbosity + +The files will be automatically compressed if the file names have a .gz +or .bgzf extension. The input to this program must be collated by name. +Run 'samtools collate' or 'samtools sort -n' to achieve this. + +Reads are designated READ1 if FLAG READ1 is set and READ2 is not set. +Reads are designated READ2 if FLAG READ1 is not set and READ2 is set. +Otherwise reads are designated READ_OTHER (both flags set or both flags unset). +Run 'samtools flags' for more information on flag codes and meanings. + +The index-format string describes how to parse the barcode and quality tags. +It is made up of 'i' or 'n' followed by a length or '*'. For example: + i14i8 The first 14 characters are index 1, the next 8 are index 2 + n8i14 Ignore the first 8 characters, and use the next 14 for index 1 + +If the tag contains a separator, then the numeric part can be replaced with +'*' to mean 'read until the separator or end of tag', for example: + i*i* Break the tag at the separator into index 1 and index 2 + n*i* Ignore the left part of the tag until the separator, + then use the second part of the tag as index 1 + +Examples: +To get just the paired reads in separate files, use: + samtools fastq -1 pair1.fq -2 pair2.fq -0 /dev/null -s /dev/null -n in.bam + +To get all non-supplementary/secondary reads in a single file, redirect +the output: + samtools fastq in.bam > all_reads.fq \ No newline at end of file diff --git a/src/samtools/samtools_fasta/script.sh b/src/samtools/samtools_fasta/script.sh new file mode 100644 index 00000000..a3eb50e6 --- /dev/null +++ b/src/samtools/samtools_fasta/script.sh @@ -0,0 +1,40 @@ +#!/bin/bash + +## VIASH START +## VIASH END + +set -e + +[[ "$par_no_suffix" == "false" ]] && unset par_no_suffix +[[ "$par_suffix" == "false" ]] && unset par_suffix +[[ "$par_use_oq" == "false" ]] && unset par_use_oq +[[ "$par_copy_tags" == "false" ]] && unset par_copy_tags +[[ "$par_casava" == "false" ]] && unset par_casava + +samtools fasta \ + ${par_no_suffix:+-n} \ + ${par_suffix:+-N} \ + ${par_use_oq:+-O} \ + ${par_singleton:+-s "$par_singleton"} \ + ${par_copy_tags:+-t} \ + ${par_copy_tags_list:+-T "$par_copy_tags_list"} \ + ${par_read1:+-1 "$par_read1"} \ + ${par_read2:+-2 "$par_read2"} \ + ${par_output_reads:+-o "$par_output_reads"} \ + ${par_output_reads_both:+-0 "$par_output_reads_both"} \ + ${par_filter_flags:+-f "$par_filter_flags"} \ + ${par_excl_flags:+-F "$par_excl_flags"} \ + ${par_incl_flags:+--rf "$par_incl_flags"} \ + ${par_excl_flags_all:+-G "$par_excl_flags_all"} \ + ${par_aux_tag:+-d "$par_aux_tag"} \ + ${par_aux_tag_file:+-D "$par_aux_tag_file"} \ + ${par_casava:+-i} \ + ${par_compression:+-c "$par_compression"} \ + ${par_index1:+--i1 "$par_index1"} \ + ${par_index2:+--i2 "$par_index2"} \ + ${par_barcode_tag:+--barcode-tag "$par_barcode_tag"} \ + ${par_quality_tag:+--quality-tag "$par_quality_tag"} \ + ${par_index_format:+--index-format "$par_index_format"} \ + "$par_input" \ + > "$par_output" + diff --git a/src/samtools/samtools_fasta/test.sh b/src/samtools/samtools_fasta/test.sh new file mode 100644 index 00000000..687965ae --- /dev/null +++ b/src/samtools/samtools_fasta/test.sh @@ -0,0 +1,96 @@ +#!/bin/bash + +test_dir="${meta_resources_dir}/test_data" +out_dir="${meta_resources_dir}/out_data" + +############################################################################################ + +echo ">>> Test 1: Convert all reads from a bam file to fasta format" +"$meta_executable" \ + --input "$test_dir/a.bam" \ + --output "$out_dir/a.fa" + +echo ">>> Check if output file exists" +[ ! -f "$out_dir/a.fa" ] && echo "Output file a.fa does not exist" && exit 1 + +echo ">>> Check if output is empty" +[ ! -s "$out_dir/a.fa" ] && echo "Output file a.fa is empty" && exit 1 + +echo ">>> Check if output matches expected output" +diff "$out_dir/a.fa" "$test_dir/a.fa" || + (echo "Output file a.fa does not match expected output" && exit 1) + +rm "$out_dir/a.fa" + +############################################################################################ + +echo ">>> Test 2: Convert all reads from a sam file to fasta format" +"$meta_executable" \ + --input "$test_dir/a.sam" \ + --output "$out_dir/a.fa" + +echo ">>> Check if output file exists" +[ ! -f "$out_dir/a.fa" ] && echo "Output file a.fa does not exist" && exit 1 + +echo ">>> Check if output is empty" +[ ! -s "$out_dir/a.fa" ] && echo "Output file a.fa is empty" && exit 1 + +echo ">>> Check if output matches expected output" +diff "$out_dir/a.fa" "$test_dir/a.fa" || + (echo "Output file a.fa does not match expected output" && exit 1) + +rm "$out_dir/a.fa" + +############################################################################################ + +echo ">>> Test 3: Output reads from bam file to separate files" + +"$meta_executable" \ + --input "$test_dir/a.bam" \ + --read1 "$out_dir/a.1.fa" \ + --read2 "$out_dir/a.2.fa" \ + --output "$out_dir/a.fa" + +echo ">>> Check if output files exist" +[ ! -f "$out_dir/a.1.fa" ] && echo "Output file a.1.fa does not exist" && exit 1 +[ ! -f "$out_dir/a.2.fa" ] && echo "Output file a.2.fa does not exist" && exit 1 +[ ! -f "$out_dir/a.fa" ] && echo "Output file a.fa does not exist" && exit 1 + +echo ">>> Check if output files are empty" +[ ! -s "$out_dir/a.1.fa" ] && echo "Output file a.1.fa is empty" && exit 1 +[ ! -s "$out_dir/a.2.fa" ] && echo "Output file a.2.fa is empty" && exit 1 +# output should be empty since input has no singleton reads + +echo ">>> Check if output files match expected output" +diff "$out_dir/a.1.fa" "$test_dir/a.1.fa" || + (echo "Output file a.1.fa does not match expected output" && exit 1) +diff "$out_dir/a.2.fa" "$test_dir/a.2.fa" || + (echo "Output file a.2.fa does not match expected output" && exit 1) + +rm "$out_dir/a.1.fa" "$out_dir/a.2.fa" "$out_dir/a.fa" + +############################################################################################ + +echo ">>> Test 4: Output only forward reads from bam file to fasta format" + +"$meta_executable" \ + --input "$test_dir/a.sam" \ + --excl_flags "0x80" \ + --output "$out_dir/half.fa" + +echo ">>> Check if output file exists" +[ ! -f "$out_dir/half.fa" ] && echo "Output file half.fa does not exist" && exit 1 + +echo ">>> Check if output is empty" +[ ! -s "$out_dir/half.fa" ] && echo "Output file half.fa is empty" && exit 1 + +echo ">>> Check if output matches expected output" +diff "$out_dir/half.fa" "$test_dir/half.fa" || + (echo "Output file half.fa does not match expected output" && exit 1) + +rm "$out_dir/half.fa" + +############################################################################################ + +echo "All tests succeeded!" +exit 0 \ No newline at end of file diff --git a/src/samtools/samtools_fasta/test_data/a.1.fa b/src/samtools/samtools_fasta/test_data/a.1.fa new file mode 100644 index 00000000..2c9fdbe5 --- /dev/null +++ b/src/samtools/samtools_fasta/test_data/a.1.fa @@ -0,0 +1,6 @@ +>a1 +AAAAAAAAAA +>b1 +AAAAAAAAAA +>c1 +AAAAAAAAAA diff --git a/src/samtools/samtools_fasta/test_data/a.2.fa b/src/samtools/samtools_fasta/test_data/a.2.fa new file mode 100644 index 00000000..2c9fdbe5 --- /dev/null +++ b/src/samtools/samtools_fasta/test_data/a.2.fa @@ -0,0 +1,6 @@ +>a1 +AAAAAAAAAA +>b1 +AAAAAAAAAA +>c1 +AAAAAAAAAA diff --git a/src/samtools/samtools_fasta/test_data/a.bam b/src/samtools/samtools_fasta/test_data/a.bam new file mode 100644 index 0000000000000000000000000000000000000000..dba1268acbd8446e4fde54d7da33434597fbe635 GIT binary patch literal 184 zcmb2|=3rp}f&Xj_PR>jWb_~TuUs6R95)ukH_@3~5+q`PUgD)R98yP)FV(BtuE_7vW z=9s|5aI{h|P#vgC9!+};gK@G0Lz-KrbIh-tVq12fpEAOZjf CvNY8I literal 0 HcmV?d00001 diff --git a/src/samtools/samtools_fasta/test_data/a.fa b/src/samtools/samtools_fasta/test_data/a.fa new file mode 100644 index 00000000..693cd395 --- /dev/null +++ b/src/samtools/samtools_fasta/test_data/a.fa @@ -0,0 +1,12 @@ +>a1/1 +AAAAAAAAAA +>b1/1 +AAAAAAAAAA +>c1/1 +AAAAAAAAAA +>a1/2 +AAAAAAAAAA +>b1/2 +AAAAAAAAAA +>c1/2 +AAAAAAAAAA diff --git a/src/samtools/samtools_fasta/test_data/a.sam b/src/samtools/samtools_fasta/test_data/a.sam new file mode 100644 index 00000000..aa8c77b3 --- /dev/null +++ b/src/samtools/samtools_fasta/test_data/a.sam @@ -0,0 +1,7 @@ +@SQ SN:xx LN:20 +a1 99 xx 1 1 10M = 11 20 AAAAAAAAAA ********** +b1 99 xx 1 1 10M = 11 20 AAAAAAAAAA ********** +c1 99 xx 1 1 10M = 11 20 AAAAAAAAAA ********** +a1 147 xx 11 1 10M = 1 -20 TTTTTTTTTT ********** +b1 147 xx 11 1 10M = 1 -20 TTTTTTTTTT ********** +c1 147 xx 11 1 10M = 1 -20 TTTTTTTTTT ********** diff --git a/src/samtools/samtools_fasta/test_data/half.fa b/src/samtools/samtools_fasta/test_data/half.fa new file mode 100644 index 00000000..36cd438c --- /dev/null +++ b/src/samtools/samtools_fasta/test_data/half.fa @@ -0,0 +1,6 @@ +>a1/1 +AAAAAAAAAA +>b1/1 +AAAAAAAAAA +>c1/1 +AAAAAAAAAA diff --git a/src/samtools/samtools_fasta/test_data/script.sh b/src/samtools/samtools_fasta/test_data/script.sh new file mode 100755 index 00000000..b59bc1bd --- /dev/null +++ b/src/samtools/samtools_fasta/test_data/script.sh @@ -0,0 +1,11 @@ +#!/bin/bash + +# dowload test data from snakemake wrapper +if [ ! -d /tmp/fastq_source ]; then + git clone --depth 1 --single-branch --branch master https://github.com/snakemake/snakemake-wrappers.git /tmp/fastq_source +fi + +cp -r /tmp/fastq_source/bio/samtools/fastx/test/*.sam src/samtools/samtools_fastq/test_data/ +cp -r /tmp/fastq_source/bio/samtools/fastq/interleaved/test/mapped/*.bam src/samtools/samtools_fastq/test_data/ +cp -r /tmp/fastq_source/bio/samtools/fastq/interleaved/test/reads/*.fq src/samtools/samtools_fastq/test_data/ +cp -r /tmp/fastq_source/bio/samtools/fastq/separate/test/reads/*.fq src/samtools/samtools_fastq/test_data/ \ No newline at end of file