# Text searching

Text searching comes in two forms, both built with
[`jbrowse text-index`](https://jbrowse.org/jb2/docs/cli#jbrowse-text-index). An **aggregate index**
(top-level `aggregateTextSearchAdapters`) is searched across many tracks at
once, for a genome-wide gene-name index. A **per-track index** (a track's
`textSearching` slot) makes just one track searchable.

An index is written as the `.ix` that `jbrowse text-index` wrote, and the `.ixx`
beside it derives from that name. In a config with one assembly, that path is
the whole aggregate entry:

```json
{
  "aggregateTextSearchAdapters": ["trix/hg19.ix"]
}
```

A config holding several assemblies names the one each index covers:

```json
{
  "aggregateTextSearchAdapters": [
    { "uri": "trix/hg19.ix", "assemblyNames": ["hg19"] },
    { "uri": "trix/mm10.ix", "assemblyNames": ["mm10"] }
  ]
}
```

A per-track index searches its track's own assemblies, shown here with the two
slots that set what `text-index` puts in it:

```json
{
  "trackId": "mytrack",
  "name": "My track name",
  "uri": "yourfile.gff.gz",
  "assemblyNames": ["hg19"],
  "textSearching": {
    "textSearchAdapter": "trix/mytrack.ix",
    "indexingAttributes": ["Name", "ID"],
    "indexingFeatureTypesToExclude": ["CDS", "exon"]
  }
}
```

CLI:

```bash
jbrowse add-track-json '{
  "trackId": "mytrack",
  "name": "My track name",
  "uri": "yourfile.gff.gz",
  "assemblyNames": ["hg19"],
  "textSearching": {
    "textSearchAdapter": "trix/mytrack.ix",
    "indexingAttributes": ["Name", "ID"],
    "indexingFeatureTypesToExclude": ["CDS", "exon"]
  }
}'
```

[`indexingAttributes`](https://jbrowse.org/jb2/docs/config/basetrack/#slot-textsearchingindexingattributes)
and
[`indexingFeatureTypesToExclude`](https://jbrowse.org/jb2/docs/config/basetrack/#slot-textsearchingindexingfeaturetypestoexclude)
are the per-track form of `--attributes` and `--exclude`.

## Indexable formats

`text-index` reads GFF3, GTF and VCF tracks and skips every other adapter type;
[the CLI page](https://jbrowse.org/jb2/docs/cli#jbrowse-text-index) lists the adapters. Two formats
differ from GFF3:

- **VCF** indexes the variant IDs plus any INFO fields named in `--attributes`.
- **GTF** has no gene or transcript rows, only exon/CDS/UTR rows repeating a
  `gene_id`, so `text-index` groups rows by `gene_id` and `transcript_id` and
  indexes each gene or transcript once. `indexingFeatureTypesToExclude` does not
  apply to GTF, since dropping rows would only truncate those spans.

## TrixTextSearchAdapter config

`text-index` writes three files, and the adapter reads two of them: the `uri`
shorthand names `ixFilePath` (the `.ix` the search box reads) and derives
`ixxFilePath` (`uri` plus an `x`, the prefix index). Set the two
[slots](https://jbrowse.org/jb2/docs/config/trixtextsearchadapter) individually when the files do not
sit together under those names. The third file, `<name>_meta.json`, records what
the index was built from for whoever built it; JBrowse does not read it and has
no slot for it.

A names index from JBrowse 1's `generate-names.pl` still works through
[`JBrowse1TextSearchAdapter`](https://jbrowse.org/jb2/docs/config/jbrowse1textsearchadapter), with
`namesIndexLocation` pointing at the names directory. To build your own adapter,
see
[creating a text search adapter](https://jbrowse.org/jb2/docs/developer_guides/creating_text_search_adapter).

## Track hubs

A UCSC track hub needs no index of your own. A hub track that declares
`searchIndex` was built with those columns extra-indexed in its BigBed, and the
[connection](https://jbrowse.org/jb2/docs/user_guides/connections) searches it through a
[`BigBedTextSearchAdapter`](https://jbrowse.org/jb2/docs/config/bigbedtextsearchadapter). A track that
also declares `searchTrix`, as the gene tracks of UCSC's GenArk hubs do, gains
prefix and case-insensitive matching. Without `searchTrix`, a name matches only
as the hub spells it. Transcripts that share a gene name and overlap are found
as the one gene.

## Troubleshooting

- **No results after running text-index.** Usually stale 0-byte `.ix`/`.ixx`
  files from an interrupted run; `jbrowse text-index --force` overwrites them.
- **Out of disk space while indexing.** `text-index` writes temporary data to
  `/tmp`; `TMPDIR=~/alt_tmp_dir jbrowse text-index` moves it.
- **Only some genes are searchable.** The default attributes are `Name`, `ID`
  and `symbol`; add others with `--attributes=Name,ID,symbol,gene_name`, and
  check that the feature type with the name is not in
  [`--exclude`](https://jbrowse.org/jb2/docs/cli#jbrowse-text-index).

## The trix index format

`jbrowse text-index` re-implements the
[UCSC trix format](https://genome.ucsc.edu/goldenPath/help/trix.html) so no UCSC
tools are needed. Given input like:

```text
GENEID001  Wnt signalling
GENEID002  ey  Pax6
```

it writes an `.ix` file sorted alphabetically by word:

```text
ey  GENEID002
Pax6  GENEID002
signalling  GENEID001
Wnt  GENEID001
```

and an `.ixx` file recording the byte offset of each prefix (`signa000000435`).
JBrowse extends the format: each `.ix` line also contains the feature's name and
genomic location in an encoded form.

A search matches a word from its first letter. So that `drb1` finds `HLA-DRB1`,
a name with punctuation in it is also indexed by what follows each mark, where
that is at least three characters and not a number, and where no more than 100
features share it. `as1` ends thousands of antisense gene names, so it is not
indexed as a word of its own, and `brca1-as1` is still found by typing `brca1`.

## See also

- **User guide:** [Basic usage: the location search box](https://jbrowse.org/jb2/docs/user_guides/basic_usage#using-the-location-search-box)
- **User guide:** [Connections](https://jbrowse.org/jb2/docs/user_guides/connections)
