> For the complete documentation index, see [llms.txt](https://documentation.dnanexus.com/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://documentation.dnanexus.com/getting-started/developer-tutorials/bash/samtools-count.md).

# SAMtools count

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

This applet performs a basic `samtools view -c {bam}` command, referred to as "SAMtools count", on the DNAnexus Platform.

## Download BAM Files

For bash scripts, inputs to a job execution become environment variables. The inputs from the `dxapp.json` file are formatted as shown in the following example:

```json
{
  "inputSpec": [
    {
      "name": "mappings_bam",
      "label": "Mapping",
      "class": "file",
      "patterns": ["*.bam"],
      "help": "BAM format file."
    }
  ]
}
```

The object `mappings_bam`, a DNAnexus link containing the file ID of that file, is available as an environment variable in the applet's execution. Use the command `dx download` to download the BAM file. By default, downloading a file preserves the filename of the object on the platform.

```shell
dx download "${mappings_bam}"
```

## SAMtools Count

Use the bash helper variable `mappings_bam_name` for file inputs. For these inputs, the DNAnexus Platform creates a bash variable `[VARIABLE]_name` that holds the platform filename. Because the file was downloaded with default parameters, the worker filename matches the platform filename. The helper variable `[VARIABLE]_prefix` contains the filename minus any suffixes specified in the input field patterns (for example, the platform removes the trailing `.bam` to create `[VARIABLE]_prefix`).

```shell
readcount=$(samtools view -c "${mappings_bam_name}")
echo "Total reads: ${readcount}" > "${mappings_bam_prefix}.txt"
```

## Upload Result

Use the [`dx upload`](/user/helpstrings-of-sdk-command-line-utilities.md#upload) command to upload data to the platform. This uploads the file into the job container, a temporary project that holds onto files associated with the job. When running the command `dx upload` with the flag `--brief`, the command returns only the file ID.

```shell
counts_txt_id=$(dx upload "${mappings_bam_prefix}.txt" --brief)
```

Job containers are an integral part of the execution process. To learn more see [Containers for Execution](/developer/api/running-analyses/containers-for-execution.md).

## Associate With Output

The output of an applet must be declared before the applet is even built. Looking back to the `dxapp.json` file, you see the following:

```json
{
  "name": "counts_txt",
  "class": "file",
  "label": "Read count file",
  "patterns": [
    "*.txt"
  ],
  "help": "Output file with Total reads as the first line."
}
```

The applet declares a **file** type output named `counts_txt`. In the applet script, specify which file is associated with the output `counts_txt`. On job completion, this file is copied from the temporary job container to the project that launched the job.

```shell
dx-jobutil-add-output counts_txt "${counts_txt_id}" --class=file
```


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter, and the optional `goal` query parameter:

```
GET https://documentation.dnanexus.com/getting-started/developer-tutorials/bash/samtools-count.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
