Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Avoid duplicate code when unsetting multiple boolean arguments #133

Merged
merged 3 commits into from
Aug 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@

* Update CI to reusable workflow in `viash-io/viash-actions` (PR #86).

* Update several components in order to avoid duplicate code when using `unset` on boolean arguments (PR #133).

## DOCUMENTATION

* Extend the contributing guidelines (PR #82):
Expand Down
25 changes: 25 additions & 0 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -320,6 +320,31 @@ Notes:

* If your tool allows for multiple inputs using a separator other than `;` (which is the default Viash multiple separator), you can substitute these values with a command like: `par_disable_filters=$(echo $par_disable_filters | tr ';' ',')`.

* If you have a lot of boolean variables that you would like to unset when the value is `false`, you can avoid duplicate code by using the following syntax:

```bash
unset_if_false=(
par_argument_1
par_argument_2
par_argument_3
par_argument_4
)

for par in ${unset_if_false[@]}; do
test_val="${!par}"
[[ "$test_val" == "false" ]] && unset $par
done
```

this code is equivalent to

```bash
[[ "$par_argument_1" == "false" ]] && unset par_argument_1
[[ "$par_argument_2" == "false" ]] && unset par_argument_2
[[ "$par_argument_3" == "false" ]] && unset par_argument_3
[[ "$par_argument_4" == "false" ]] && unset par_argument_4
```


### Step 12: Create test script

Expand Down
49 changes: 28 additions & 21 deletions src/bedtools/bedtools_intersect/script.sh
Original file line number Diff line number Diff line change
Expand Up @@ -3,27 +3,34 @@
## VIASH START
## VIASH END

[[ "$par_write_a" == "false" ]] && unset par_write_a
[[ "$par_write_b" == "false" ]] && unset par_write_b
[[ "$par_left_outer_join" == "false" ]] && unset par_left_outer_join
[[ "$par_write_overlap" == "false" ]] && unset par_write_overlap
[[ "$par_write_overlap_plus" == "false" ]] && unset par_write_overlap_plus
[[ "$par_report_A_if_no_overlap" == "false" ]] && unset par_report_A_if_no_overlap
[[ "$par_number_of_overlaps_A" == "false" ]] && unset par_number_of_overlaps_A
[[ "$par_report_no_overlaps_A" == "false" ]] && unset par_report_no_overlaps_A
[[ "$par_uncompressed_bam" == "false" ]] && unset par_uncompressed_bam
[[ "$par_same_strand" == "false" ]] && unset par_same_strand
[[ "$par_opposite_strand" == "false" ]] && unset par_opposite_strand
[[ "$par_reciprocal_overlap" == "false" ]] && unset par_reciprocal_overlap
[[ "$par_either_overlap" == "false" ]] && unset par_either_overlap
[[ "$par_split" == "false" ]] && unset par_split
[[ "$par_nonamecheck" == "false" ]] && unset par_nonamecheck
[[ "$par_sorted" == "false" ]] && unset par_sorted
[[ "$par_filenames" == "false" ]] && unset par_filenames
[[ "$par_sortout" == "false" ]] && unset par_sortout
[[ "$par_bed" == "false" ]] && unset par_bed
[[ "$par_header" == "false" ]] && unset par_header
[[ "$par_no_buffer_output" == "false" ]] && unset par_no_buffer_output
unset_if_false=(
par_write_a
par_write_b
par_left_outer_join
par_write_overlap
par_write_overlap_plus
par_report_A_if_no_overlap
par_number_of_overlaps_A
par_report_no_overlaps_A
par_uncompressed_bam
par_same_strand
par_opposite_strand
par_reciprocal_overlap
par_either_overlap
par_split
par_nonamecheck
par_sorted
par_filenames
par_sortout
par_bed
par_no_buffer_output
)

for par in ${unset_if_false[@]}; do
test_val="${!par}"
[[ "$test_val" == "false" ]] && unset $par
done


# Create input array
IFS=";" read -ra input <<< $par_input_b
Expand Down
21 changes: 14 additions & 7 deletions src/bedtools/bedtools_sort/script.sh
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,20 @@
## VIASH END

# Unset parameters
[[ "$par_sizeA" == "false" ]] && unset par_sizeA
[[ "$par_sizeD" == "false" ]] && unset par_sizeD
[[ "$par_chrThenSizeA" == "false" ]] && unset par_chrThenSizeA
[[ "$par_chrThenSizeD" == "false" ]] && unset par_chrThenSizeD
[[ "$par_chrThenScoreA" == "false" ]] && unset par_chrThenScoreA
[[ "$par_chrThenScoreD" == "false" ]] && unset par_chrThenScoreD
[[ "$par_header" == "false" ]] && unset par_header
unset_if_false=(
par_sizeA
par_sizeD
par_chrThenSizeA
par_chrThenSizeD
par_chrThenScoreA
par_chrThenScoreD
par_header
)

for par in ${unset_if_false[@]}; do
test_val="${!par}"
[[ "$test_val" == "false" ]] && unset $par
done

# Execute bedtools sort with the provided arguments
bedtools sort \
Expand Down
28 changes: 17 additions & 11 deletions src/busco/busco_run/script.sh
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,24 @@
## VIASH START
## VIASH END

unset_if_false=(
par_tar
par_force
par_quiet
par_restart
par_auto_lineage
par_auto_lineage_euk
par_auto_lineage_prok
par_augustus
par_long
par_scaffold_composition
par_miniprot
)

[[ "$par_tar" == "false" ]] && unset par_tar
[[ "$par_force" == "false" ]] && unset par_force
[[ "$par_quiet" == "false" ]] && unset par_quiet
[[ "$par_restart" == "false" ]] && unset par_restart
[[ "$par_auto_lineage" == "false" ]] && unset par_auto_lineage
[[ "$par_auto_lineage_euk" == "false" ]] && unset par_auto_lineage_euk
[[ "$par_auto_lineage_prok" == "false" ]] && unset par_auto_lineage_prok
[[ "$par_augustus" == "false" ]] && unset par_augustus
[[ "$par_long" == "false" ]] && unset par_long
[[ "$par_scaffold_composition" == "false" ]] && unset par_scaffold_composition
[[ "$par_miniprot" == "false" ]] && unset par_miniprot
for par in ${unset_if_false[@]}; do
test_val="${!par}"
[[ "$test_val" == "false" ]] && unset $par
done

tmp_dir=$(mktemp -d -p "$meta_temp_dir" busco_XXXXXXXXX)
prefix=$(openssl rand -hex 8)
Expand Down
45 changes: 26 additions & 19 deletions src/fastp/script.sh
Original file line number Diff line number Diff line change
Expand Up @@ -4,25 +4,32 @@
## VIASH END

# disable flags
[[ "$par_disable_adapter_trimming" == "false" ]] && unset par_disable_adapter_trimming
[[ "$par_detect_adapter_for_pe" == "false" ]] && unset par_detect_adapter_for_pe
[[ "$par_merge" == "false" ]] && unset par_merge
[[ "$par_include_unmerged" == "false" ]] && unset par_include_unmerged
[[ "$par_interleaved_in" == "false" ]] && unset par_interleaved_in
[[ "$par_fix_mgi_id" == "false" ]] && unset par_fix_mgi_id
[[ "$par_phred64" == "false" ]] && unset par_phred64
[[ "$par_dont_overwrite" == "false" ]] && unset par_dont_overwrite
[[ "$par_verbose" == "false" ]] && unset par_verbose
[[ "$par_dedup" == "false" ]] && unset par_dedup
[[ "$par_dont_eval_duplication" == "false" ]] && unset par_dont_eval_duplication
[[ "$par_trim_poly_g" == "false" ]] && unset par_trim_poly_g
[[ "$par_disable_trim_poly_g" == "false" ]] && unset par_disable_trim_poly_g
[[ "$par_trim_poly_x" == "false" ]] && unset par_trim_poly_x
[[ "$par_disable_quality_filtering" == "false" ]] && unset par_disable_quality_filtering
[[ "$par_disable_length_filtering" == "false" ]] && unset par_disable_length_filtering
[[ "$par_low_complexity_filter" == "false" ]] && unset par_low_complexity_filter
[[ "$par_umi" == "false" ]] && unset par_umi
[[ "$par_overrepresentation_analysis" == "false" ]] && unset par_overrepresentation_analysis
unset_if_false=(
par_disable_adapter_trimming
par_detect_adapter_for_pe
par_merge
par_include_unmerged
par_interleaved_in
par_fix_mgi_id
par_phred64
par_dont_overwrite
par_verbose
par_dedup
par_dont_eval_duplication
par_trim_poly_g
par_disable_trim_poly_g
par_trim_poly_x
par_disable_quality_filtering
par_disable_length_filtering
par_low_complexity_filter
par_umi
par_overrepresentation_analysis
)

for par in ${unset_if_false[@]}; do
test_val="${!par}"
[[ "$test_val" == "false" ]] && unset $par
done

# run command
fastp \
Expand Down
43 changes: 25 additions & 18 deletions src/featurecounts/script.sh
Original file line number Diff line number Diff line change
Expand Up @@ -19,24 +19,31 @@ par_feature_type=$(echo $par_feature_type | tr ',' ';')
par_extra_attributes=$(echo $par_extra_attributes | tr ',' ';')

# unset flag variables
[[ "$par_feature_level" == "false" ]] && unset par_feature_level
[[ "$par_overlapping" == "false" ]] && unset par_overlapping
[[ "$par_largest_overlap" == "false" ]] && unset par_largest_overlap
[[ "$par_multi_mapping" == "false" ]] && unset par_multi_mapping
[[ "$par_fraction" == "false" ]] && unset par_fraction
[[ "$par_split_only" == "false" ]] && unset par_split_only
[[ "$par_non_split_only" == "false" ]] && unset par_non_split_only
[[ "$par_primary" == "false" ]] && unset par_primary
[[ "$par_ignore_dup" == "false" ]] && unset par_ignore_dup
[[ "$par_paired" == "false" ]] && unset par_paired
[[ "$par_count_read_pairs" == "false" ]] && unset par_count_read_pairs
[[ "$par_both_aligned" == "false" ]] && unset par_both_aligned
[[ "$par_check_pe_dist" == "false" ]] && unset par_check_pe_dist
[[ "$par_same_strand" == "false" ]] && unset par_same_strand
[[ "$par_donotsort" == "false" ]] && unset par_donotsort
[[ "$par_by_read_group" == "false" ]] && unset par_by_read_group
[[ "$par_long_reads" == "false" ]] && unset par_long_reads
[[ "$par_verbose" == "false" ]] && unset par_verbose
unset_if_false=(
par_feature_level
par_overlapping
par_largest_overlap
par_multi_mapping
par_fraction
par_split_only
par_non_split_only
par_primary
par_ignore_dup
par_paired
par_count_read_pairs
par_both_aligned
par_check_pe_dist
par_same_strand
par_donotsort
par_by_read_group
par_long_reads
par_verbose
)

for par in ${unset_if_false[@]}; do
test_val="${!par}"
[[ "$test_val" == "false" ]] && unset $par
done

IFS=";" read -ra input <<< $par_input

Expand Down
97 changes: 52 additions & 45 deletions src/gffread/script.sh
Original file line number Diff line number Diff line change
Expand Up @@ -4,51 +4,58 @@
## VIASH END

# unset flags
[[ "$par_coding" == "false" ]] && unset par_coding
[[ "$par_strict_range" == "false" ]] && unset par_strict_range
[[ "$par_no_single_exon" == "false" ]] && unset par_no_single_exon
[[ "$par_no_exon_attrs" == "false" ]] && unset par_no_exon_attrs
[[ "$par_nc" == "false" ]] && unset par_nc
[[ "$par_ignore_locus" == "false" ]] && unset par_ignore_locus
[[ "$par_description" == "false" ]] && unset par_description
[[ "$par_sort_alpha" == "false" ]] && unset par_sort_alpha
[[ "$par_keep_genes" == "false" ]] && unset par_keep_genes
[[ "$par_keep_attrs" == "false" ]] && unset par_keep_attrs
[[ "$par_keep_exon_attrs" == "false" ]] && unset par_keep_exon_attrs
[[ "$par_keep_comments" == "false" ]] && unset par_keep_comments
[[ "$par_process_other" == "false" ]] && unset par_process_other
[[ "$par_rm_stop_codons" == "false" ]] && unset par_rm_stop_codons
[[ "$par_adj_cds_start" == "false" ]] && unset par_adj_cds_start
[[ "$par_opposite_strand" == "false" ]] && unset par_opposite_strand
[[ "$par_coding_status" == "false" ]] && unset par_coding_status
[[ "$par_add_hasCDS" == "false" ]] && unset par_add_hasCDS
[[ "$par_adj_stop" == "false" ]] && unset par_adj_stop
[[ "$par_rm_noncanon" == "false" ]] && unset par_rm_noncanon
[[ "$par_complete_cds" == "false" ]] && unset par_complete_cds
[[ "$par_no_pseudo" == "false" ]] && unset par_no_pseudo
[[ "$par_in_bed" == "false" ]] && unset par_in_bed
[[ "$par_in_tlf" == "false" ]] && unset par_in_tlf
[[ "$par_stream" == "false" ]] && unset par_stream
[[ "$par_merge" == "false" ]] && unset par_merge
[[ "$par_rm_redundant" == "false" ]] && unset par_rm_redundant
[[ "$par_no_boundary" == "false" ]] && unset par_no_boundary
[[ "$par_no_overlap" == "false" ]] && unset par_no_overlap
[[ "$par_force_exons" == "false" ]] && unset par_force_exons
[[ "$par_gene2exon" == "false" ]] && unset par_gene2exon
[[ "$par_t_adopt" == "false" ]] && unset par_t_adopt
[[ "$par_decode" == "false" ]] && unset par_decode
[[ "$par_merge_exons" == "false" ]] && unset par_merge_exons
[[ "$par_junctions" == "false" ]] && unset par_junctions
[[ "$par_w_nocds" == "false" ]] && unset par_w_nocds
[[ "$par_tr_cds" == "false" ]] && unset par_tr_cds
[[ "$par_w_coords" == "false" ]] && unset par_w_coords
[[ "$par_stop_dot" == "false" ]] && unset par_stop_dot
[[ "$par_id_version" == "false" ]] && unset par_id_version
[[ "$par_gtf_output" == "false" ]] && unset par_gtf_output
[[ "$par_bed" == "false" ]] && unset par_bed
[[ "$par_tlf" == "false" ]] && unset par_tlf
[[ "$par_expose_dups" == "false" ]] && unset par_expose_dups
[[ "$par_cluster_only" == "false" ]] && unset par_cluster_only
unset_if_false=(
par_coding
par_strict_range
par_no_single_exon
par_no_exon_attrs
par_nc
par_ignore_locus
par_description
par_sort_alpha
par_keep_genes
par_keep_attrs
par_keep_exon_attrs
par_keep_comments
par_process_other
par_rm_stop_codons
par_adj_cds_start
par_opposite_strand
par_coding_status
par_add_hasCDS
par_adj_stop
par_rm_noncanon
par_complete_cds
par_no_pseudo
par_in_bed
par_in_tlf
par_stream
par_merge
par_rm_redundant
par_no_boundary
par_no_overlap
par_force_exons
par_gene2exon
par_t_adopt
par_decode
par_merge_exons
par_junctions
par_w_nocds
par_tr_cds
par_w_coords
par_stop_dot
par_id_version
par_gtf_output
par_bed
par_tlf
par_expose_dups
par_cluster_only
)

for par in ${unset_if_false[@]}; do
test_val="${!par}"
[[ "$test_val" == "false" ]] && unset $par
done

# if par_table is not empty, replace ";" with ","
par_table=$(echo "$par_table" | tr ';' ',')
Expand Down
Loading