Bash Helpers

Learn to build an applet that performs a basic SAMtools count with the aid of bash helper variables.

Source Code

Step 1. Download BAM Files

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

Step 2. Create an Output Directory

We create an output directory in preparation for dx-upload-all-outputs DNAnexus command in the (Upload Results)[#Upload Result] section.
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 will have a helper variables:
# [VARIABLE]_path the absolute string path to the file.
$ echo $mappings_bam_path
# [VARIABLE]_prefix the file name minus the longest matching pattern in the dxapp.json file
$ echo $mappings_bam_prefix
# [VARIABLE]_name the file name from the platform
$ echo $mappings_bam_name
We 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.
samtools view -c "${mappings_bam_path}" > out/counts_txt/"${mappings_bam_prefix}.txt"

Step 4. Upload Result

We 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 will upload matching files and then associate them as the output corresponding to [VARIABLE]. In this case, the output is called counts_txt. Earlier we created the folders, and we can now place the outputs there.