from randseq import calculate_log2fc, find_restricted_motifs, plot_motif_analysis, get_example_data_dir
import pandas as pdrandseq
Installation
$ pip install randseqOptionally, with a compiled scan that is about 13x faster on 8 cores and gives identical results:
$ pip install "randseq[fast]"The development version installs from the GitHub repository:
$ pip install git+https://github.com/dbikard/randseq.gitUsage
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.
# 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 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:
motifs = find_restricted_motifs(log2fc_df["JJ1886_R1"], left, right)motifs| motif | num_sequences | fraction_depleted | avg_log2fc | q_value | |
|---|---|---|---|---|---|
| 0 | AAAGNNNNGTT | 913 | 0.997809 | -3.425663 | 0.000000e+00 |
| 1 | RTACNNNNGTG | 963 | 1.000000 | -3.285517 | 0.000000e+00 |
| 2 | GGTCTC | 55 | 0.981818 | -2.851532 | 2.999895e-43 |
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:
fig, axes = plot_motif_analysis(counts, "JJ1886_R1", "Control_R1", motifs, left, right)
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 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: one page per module, each a walkthrough of the code with examples. The changes between versions are in CHANGELOG.md.