# randseq


<!-- WARNING: THIS FILE WAS AUTOGENERATED! DO NOT EDIT! -->

## Installation

``` sh
$ pip install randseq
```

Optionally, with a compiled scan that is about 13x faster on 8 cores and
gives identical results:

``` sh
$ pip install "randseq[fast]"
```

The development version installs from the GitHub
[repository](https://github.com/dbikard/randseq):

``` sh
$ pip install git+https://github.com/dbikard/randseq.git
```

## Usage

RandSeq conjugates a library of plasmids with random inserts from a
donor into a recipient strain and counts each plasmid on both sides. A
plasmid that carries a site the recipient’s defences recognise is lost.
The example here, `counts_panel.csv.gz`, is a library of 150 nt random
inserts counted in the donor (two replicates, `Control_R1` and
`Control_R2`) and in 8 recipient strains in two replicates each;
`counts_panel_key.csv` says what each strain shows.

``` python
from randseq import calculate_log2fc, find_restricted_motifs, plot_motif_analysis, get_example_data_dir
import pandas as pd
```

``` python
# Read counts per plasmid: the donor library and 8 recipient strains, two replicates each
counts = pd.read_csv(os.path.join(get_example_data_dir(), "counts_panel.csv.gz"), index_col=0)
left, right = "GTCTAGGGCGGCGGTAAAAC", "ACTAGAGCACCAGAAGTCTA"   # vector sequence flanking the insert

# log2 fold change of each plasmid in each sample, against the first donor replicate
log2fc_df = calculate_log2fc(counts, reference_column="Control_R1")
```

[`find_restricted_motifs`](https://dbikard.github.io/randseq/core.html#find_restricted_motifs)
searches one sample’s log2 fold changes for the motifs whose carriers
are depleted. It prints a log of its steps (not shown here) and returns
the motifs it calls, in five columns. For JJ1886 these are its three
restriction-modification sites, the Type I site written as one
degenerate motif, `RTACNNNNGTG`:

``` python
motifs = find_restricted_motifs(log2fc_df["JJ1886_R1"], left, right)
```

``` python
motifs
```

<div>
<style scoped>
    .dataframe tbody tr th:only-of-type {
        vertical-align: middle;
    }
&#10;    .dataframe tbody tr th {
        vertical-align: top;
    }
&#10;    .dataframe thead th {
        text-align: right;
    }
</style>

<table class="dataframe" data-quarto-postprocess="true" data-border="1">
<thead>
<tr style="text-align: right;">
<th data-quarto-table-cell-role="th"></th>
<th data-quarto-table-cell-role="th">motif</th>
<th data-quarto-table-cell-role="th">num_sequences</th>
<th data-quarto-table-cell-role="th">fraction_depleted</th>
<th data-quarto-table-cell-role="th">avg_log2fc</th>
<th data-quarto-table-cell-role="th">q_value</th>
</tr>
</thead>
<tbody>
<tr>
<td data-quarto-table-cell-role="th">0</td>
<td>AAAGNNNNGTT</td>
<td>913</td>
<td>0.997809</td>
<td>-3.425663</td>
<td>0.000000e+00</td>
</tr>
<tr>
<td data-quarto-table-cell-role="th">1</td>
<td>RTACNNNNGTG</td>
<td>963</td>
<td>1.000000</td>
<td>-3.285517</td>
<td>0.000000e+00</td>
</tr>
<tr>
<td data-quarto-table-cell-role="th">2</td>
<td>GGTCTC</td>
<td>55</td>
<td>0.981818</td>
<td>-2.851532</td>
<td>2.999895e-43</td>
</tr>
</tbody>
</table>

</div>

- `num_sequences`: the plasmids carrying the motif, of the 30,108 that
  pass the donor count filter.
- `fraction_depleted`: the share of them depleted, at log2FC below −0.5
  after normalising to the sample’s median.
- `avg_log2fc`: the mean change of the carriers, relative to the
  sample’s median plasmid.
- `q_value`: significance, adjusted for the false-discovery rate across
  the hundreds of thousands of motifs tested. A motif is called when q
  \< 0.05 and most of its carriers are confidently depleted.

The plot shows the called motifs’ carriers among all plasmids, and their
log2FC distribution:

``` python
fig, axes = plot_motif_analysis(counts, "JJ1886_R1", "Control_R1", motifs, left, right)
```

![](index_files/figure-commonmark/cell-6-output-1.png)

`all_motifs=True` also returns the motifs that were scored but not
called, with diagnostic columns and the reason for each verdict.
[Reading the result
table](https://dbikard.github.io/randseq/core.html#reading-the-result-table)
in the core documentation explains them, and *The example panel at a
glance* there runs every strain of the panel.

## Documentation

The documentation is at
[dbikard.github.io/randseq](https://dbikard.github.io/randseq/): one
page per module, each a walkthrough of the code with examples. The
changes between versions are in
[CHANGELOG.md](https://github.com/dbikard/randseq/blob/main/CHANGELOG.md).
