> 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/git-dependency.md).

# Git Dependency

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

## What does this applet do?

This applet performs a basic SAMtools count of alignments present in an input BAM.

## Prerequisites

The applet must have network access to the hostname where the git repository is located. In this example, `access.network` is set to:

```json
"access": {
  "network": ["github.com"]
}
```

To learn more about the `access` and `network` fields, see [Execution Environment Reference](/developer/apps/execution-environment.md).

## How is the SAMtools dependency added?

SAMtools is cloned and built from the [SAMtools GitHub](https://github.com/samtools/samtools) repository. The following is a closer look at the `dxapp.json` file's `runSpec.execDepends` property:

```json
{
  "runSpec": {
    "execDepends": [
      {
        "name": "htslib",
        "package_manager": "git",
        "url": "https://github.com/samtools/htslib.git",
        "tag": "1.3.1",
        "destdir": "/home/dnanexus"
      },
      {
        "name": "samtools",
        "package_manager": "git",
        "url": "https://github.com/samtools/samtools.git",
        "tag": "1.3.1",
        "destdir": "/home/dnanexus",
        "build_commands": "make samtools"
      }
    ]
  }
}
```

The `execDepends` value is a JSON array of dependencies to resolve before the applet source code is run. In this applet, the git fetch dependencies for `htslib` and SAMtools are specified. Dependencies resolve in the order listed. Specify `htslib` first, before the SAMtools `build_commands`, because later versions of SAMtools depend on `htslib`. An overview of each property in the git dependency:

* `package_manager` - Details the type of dependency and how to resolve. [supplementary details](/developer/apps/execution-environment.md).
* `url` - Must point to the server containing the repository. In this case, a GitHub URL.
* `tag` or `branch` - Git tag or branch to fetch.
* `destdir` - Directory on the worker to which the git repository is cloned.
* `build_commands` - Commands to build the dependency, run from the repository `destdir`. In this example, `htslib` is built when SAMtools is built, so only the SAMtools entry includes `build_commands`.

The `build_commands` are executed from the `destdir`. Use `cd` when appropriate.

## How is SAMtools called in the `src` script?

Because `"destdir": "/home/dnanexus"` is set in `dxapp.json`, the git repository is cloned to the same directory from which the script executes. The example directory structure is:

```
├── home
│   ├── dnanexus
│       ├── < app script >
│       ├── htslib
│       ├── samtools
│           ├── < samtools binary >
```

The SAMtools command in the app script is `samtools/samtools`.

## Applet Script

```shell
main() {
  set -e -x -o pipefail

  dx download "$mappings_bam"

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

  counts_txt=$(dx upload "${count_filename}" --brief)
  dx-jobutil-add-output counts_txt "${counts_txt}" --class=file
}
```

You can build SAMtools in a directory that is on the `$PATH` or add the binary directory to `$PATH`. Keep this in mind for your app(let) development.


---

# 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 by asking a question.

Perform an HTTP GET request on the following URL with the `ask` and `goal` query parameters:

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

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is what the user is ultimately trying to achieve, the reason they need the answer. Sharing it helps GitBook give you a better, more relevant answer. A goal is most helpful when it describes the outcome the user wants rather than restating the question. For example, with `ask=how do I create an API token`, a goal like `automate deployments from our CI pipeline` lets GitBook tailor the answer to that use case.

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.
