# Bash Helpers

## Source Code

[View full source code on GitHub](https://github.com/dnanexus/dnanexus-example-applets/tree/master/Tutorials/bash/samtools_count_bashhelper_sh)

### Step 1. Download BAM Files

Download input files using the `dx-download-all-inputs` command. The `dx-download-all-inputs` command goes through all inputs and downloads into folders with the pattern `/home/dnanexus/in/[VARIABLE]/[file or subfolder with files]`.

```shell
dx-download-all-inputs
```

### Step 2. Create an Output Directory

Create an output directory in preparation for the `dx-upload-all-outputs` DNAnexus command in the [Upload Results](#step-4-upload-result) section.

```shell
mkdir -p out/counts_txt
```

### Step 3. Run SAMtools View

After executing the `dx-download-all-inputs` command, there are three helper variables created to aid in scripting. For this applet, the input variable name `mappings_bam` with platform filename `my_mappings.bam` has a helper variables:

```shell
# [VARIABLE]_path the absolute string path to the file.
$ echo $mappings_bam_path
/home/dnanexus/in/mappings_bam/my_mappings.bam
# [VARIABLE]_prefix the file name minus the longest matching pattern in the dxapp.json file
$ echo $mappings_bam_prefix
my_mappings
# [VARIABLE]_name the file name from the platform
$ echo $mappings_bam_name
my_mappings.bam
```

Use the bash helper variable `mappings_bam_path` to reference the location of a file after it has been downloaded using `dx-download-all-inputs`.

```shell
samtools view -c "${mappings_bam_path}" \
  > out/counts_txt/"${mappings_bam_prefix}.txt"
```

### Step 4. Upload Result

Use the `dx-upload-all-outputs` command to upload data to the platform and specify it as the job's output. The `dx-upload-all-outputs` command expects to find file paths matching the pattern `/home/dnanexus/out/[VARIABLE]/*`. It uploads matching files and then associates them as the output corresponding to `[VARIABLE]`. In this case, the output is called `counts_txt`. After creating the folders, place the outputs there.

```shell
dx-upload-all-outputs
```
