# `BAM.Cigar`
[🔗](https://github.com/baoulo/bam-elixir/blob/v0.1.0/lib/bam/cigar.ex#L1)

CIGAR operations.

On disk, each BAM operation is a 32-bit little-endian word. The low 4 bits
are the operation code and the high 28 bits are the length. This module
keeps both the word (`cigar_raw` on an alignment) and the pair
`{op, length}` where `op` is one of the nine characters defined by SAMv1:

| Code | Op | Consumes query | Consumes reference |
| --- | --- | --- | --- |
| 0 | M | yes | yes |
| 1 | I | yes | no |
| 2 | D | no | yes |
| 3 | N | no | yes |
| 4 | S | yes | no |
| 5 | H | no | no |
| 6 | P | no | no |
| 7 | = | yes | yes |
| 8 | X | yes | yes |

`H` and `P` do not appear in the SEQ field. Query length is
`M + I + S + = + X`. Reference length is `M + D + N + = + X`.
An empty operation list is the SAM sentinel `*`.

# `op`

```elixir
@type op() :: String.t()
```

# `operation`

```elixir
@type operation() :: {op(), non_neg_integer()}
```

# `decode_word`

```elixir
@spec decode_word(non_neg_integer()) :: {:ok, operation()} | :error
```

Decodes one BAM word.

## Examples

    iex> BAM.Cigar.decode_word(0xA0)
    {:ok, {"M", 10}}

    iex> BAM.Cigar.decode_word(0xF)
    :error

# `encode_word`

```elixir
@spec encode_word(operation()) :: {:ok, non_neg_integer()} | {:error, BAM.Error.t()}
```

Encodes one operation as a BAM word.

## Examples

    iex> BAM.Cigar.encode_word({"M", 10})
    {:ok, 0xA0}

# `format`

```elixir
@spec format([operation()]) :: String.t()
```

Formats operations as a SAM CIGAR string. An empty list is `*`.

## Examples

    iex> BAM.Cigar.format([{"M", 8}, {"D", 2}])
    "8M2D"

# `op`

```elixir
@spec op(non_neg_integer()) :: op() | nil
```

Operation character for a BAM code, or `nil`.

# `parse`

```elixir
@spec parse(String.t()) :: {:ok, [operation()]} | {:error, BAM.Error.t()}
```

Parses a SAM CIGAR string. `*` is an empty list.

## Examples

    iex> BAM.Cigar.parse("8M2I4M")
    {:ok, [{"M", 8}, {"I", 2}, {"M", 4}]}

    iex> BAM.Cigar.parse("*")
    {:ok, []}

# `query_length`

```elixir
@spec query_length([operation()]) :: non_neg_integer()
```

Query bases consumed by the operations. Hard clipping and padding are excluded.

# `reference_length`

```elixir
@spec reference_length([operation()]) :: non_neg_integer()
```

Reference bases consumed by the operations.

---

*Consult [api-reference.md](api-reference.md) for complete listing*
