Plotting

visualizing the results

Reading a RandSeq result

Two plots, answering different questions.

plot_motif_analysis shows the whole library: donor against recipient counts, one point per plasmid, with the carriers of each called motif highlighted, and beside it the log2FC distribution of each motif’s carriers against all plasmids. Plasmids absent from the recipient sit at y = 1, because a pseudocount is added before the log. It answers did this strain restrict anything, and does each call look like one shifted population rather than a handful of outliers.

plot_motif_depletion takes motifs you supply instead of the ones the pipeline found: a motif REBASE predicts for the strain, or one called in another strain. It tests a specific hypothesis, including when the answer is that the motif was not restricted.

Read either plot beside the result table, whose columns answer separate questions:

  • q_value: how sure we are that the carriers are depleted more than their background allows by chance — the depletion rate, in this sample, of plasmids with the same insert GC content. It grows with support, so a weak effect on many plasmids can be very significant.
  • fraction_depleted: how strong the effect is, the share of carriers depleted. For a weak motif, compare it with background (with all_motifs=True).
  • bimodality (with all_motifs=True): whether the carriers’ log2FC form one population or two. The row is flagged bimodal when they do (coefficient above 5/9) and one of the two groups is at background level: some carriers destroyed, others untouched, the signature of a motif that dilutes a different site.

The histogram shows log2FC as calculate_log2fc returns it, median-normalised: the median plasmid is at 0, and the pipeline’s default depletion threshold is at -0.5 on this axis.


source

plot_motif_analysis

Donor against recipient counts with each motif’s carriers highlighted, beside their log2FC.

plot_motif_analysis(counts_df, sample_col, ref_col, motifs, left_context,
                    right_context, title_main=None, pseudocount=1,
                    log2fc_count_threshold=20, log2fc_pseudocount=1,
                    figsize=(12, 6), save_filename=None,
                    distribution_plot_log_xscale=False)
Parameter Type Default Description
counts_df DataFrame read counts, one row per insert, one column per sample
sample_col str the recipient sample to plot
ref_col str the input-library column it is compared with
motifs DataFrame | None motifs to highlight, in a ‘motif’ column (find_restricted_motifs)
left_context str vector sequence 5’ of the insert
right_context str vector sequence 3’ of the insert
title_main str | None None figure title; default “Motif Analysis: <sample_col> vs <ref_col>”
pseudocount float 1 added to the counts per million of the scatter, before the log
log2fc_count_threshold int 20 count_threshold of calculate_log2fc, for the histogram
log2fc_pseudocount float 1 pseudocount of calculate_log2fc, for the histogram
figsize tuple (12, 6) figure size, in inches
save_filename str | None None also save the figure to this file, at 300 dpi
distribution_plot_log_xscale bool False log scale on the histogram’s x axis
returns tuple (fig, axes); (None, None) when counts_df is empty or lacks either column

Example: JJ1886 on the 20 nt library

countsTable.csv.gz is a run of the 20 nt library (11,629 inserts) through 19 strains, with the donor MFDpir as reference. JJ1886 at the default settings:

counts_file="countsTable.csv.gz"
file_path=os.path.join(data_path,counts_file)
counts=pd.read_csv(file_path, index_col=0)
counts = counts[[col for col in counts.columns if ("_T0" in col) or ("MFDpir" in col)]]

log2fc_df = calculate_log2fc(counts, reference_column='MFDpir', count_threshold=20, pseudocount=1)
left, right = "GTCCTAGGTATAATACTAGT", "GTTTTAGAGCTAGAAATAGC"
motifs = find_restricted_motifs(log2fc_df["JJ1886_T0"], left, right)
motifs
667 candidate(s) across all patterns; 464 remain after step 3.
158 candidate(s) that need the vector flanks, without enough internal sites (15 would otherwise be called).
293 candidate(s) whose score was borrowed from a stronger one.
28 motif(s) scored.
1 degenerate site(s) reported as one motif:
  RTACNNNNGTG <- ATACNNNNGTG + CACNNNNGTAC
3 motif(s) called.

Scanning patterns:   0%|          | 0/43 [00:00<?, ?it/s]
Scanning patterns:   2%|2         | 1/43 [00:00<00:10,  3.98it/s]
Scanning patterns:  12%|#1        | 5/43 [00:00<00:02, 16.53it/s]
Scanning patterns:  21%|##        | 9/43 [00:00<00:01, 23.37it/s]
Scanning patterns:  30%|###       | 13/43 [00:00<00:01, 27.93it/s]
Scanning patterns:  40%|###9      | 17/43 [00:00<00:00, 28.24it/s]
Scanning patterns:  49%|####8     | 21/43 [00:00<00:00, 31.41it/s]
Scanning patterns:  58%|#####8    | 25/43 [00:00<00:00, 33.72it/s]
Scanning patterns:  67%|######7   | 29/43 [00:01<00:00, 29.62it/s]
Scanning patterns:  77%|#######6  | 33/43 [00:01<00:00, 25.72it/s]
Scanning patterns:  84%|########3 | 36/43 [00:01<00:00, 22.31it/s]
Scanning patterns:  91%|######### | 39/43 [00:01<00:00, 22.78it/s]
Scanning patterns:  98%|#########7| 42/43 [00:01<00:00, 22.67it/s]
Scanning patterns: 100%|##########| 43/43 [00:01<00:00, 24.30it/s]
motif num_sequences fraction_depleted avg_log2fc q_value
0 RTACNNNNGTG 212 1.0 -4.850774 3.828852e-242
1 AAAGNNNNGTT 63 1.0 -5.209253 1.040810e-70
2 GGTCTC 9 1.0 -4.363412 1.867698e-08
fig, axes = plot_motif_analysis(counts, "JJ1886_T0", "MFDpir", motifs, left, right)

The three calls are JJ1886’s three RM sites: AAAGNNNNGTT, GGTCTC, and RTACNNNNGTG, reported as one motif from its two variants, ATACNNNNGTG and GTACNNNNGTG (the second read on the other strand, CACNNNNGTAC). Every carrier is destroyed: fraction_depleted is 1.

all_motifs=True also returns the motifs scored but not called, 24 here. Thirteen would be calls but need the vector flank and lack internal sites (core, Sites completed by the flank): CACANNAGTA has 190 of its 192 carriers depleted, but 188 of them read part of it in the flank, and its 4 internal carriers are too few to stand on. The rest fail the bound on the depleted fraction: CTTNCACA has 10 of its 20 carriers depleted (q ≈ 10⁻⁴), which bounds the fraction at 0.30, short of 0.5.

Custom-motif depletion plot

Donor against recipient counts, coloured by motifs you supply. A plasmid carrying several takes the colour of the last one in the list. Requires plotnine: the function returns a ggplot rather than drawing it, so a caller can add layers or save it at a set size, p.save(filename, width=6, height=5, units="in").


source

plot_motif_depletion

Log-log plot of one sample’s counts against the reference, coloured by the motifs you name.

plot_motif_depletion(counts_df, sample_col, ref_col, motif_list, left_context,
                     right_context, pseudocount=1, palette=['#F0A226',
                     '#0A795B', '#800074'])
Parameter Type Default Description
counts_df DataFrame read counts, one row per insert, one column per sample
sample_col str the recipient sample to plot
ref_col str the input-library column it is compared with
motif_list list motifs to colour by, in IUPAC letters and/or bracketed classes
left_context str vector sequence 5’ of the insert
right_context str vector sequence 3’ of the insert
pseudocount float 1 added to the counts per million, before the log
palette list ['#F0A226', '#0A795B', '#800074'] one colour per motif, at least as many as motif_list

Example: JJ1886 on the 150 nt panel

counts_panel.csv.gz holds UMI counts for eight strains in duplicate and the Control input library (counts_panel_key.csv says what each strain shows), flanked by the contexts below. The calls use the defaults: log2FC median-normalised by calculate_log2fc, a plasmid counted as depleted at log2FC < -0.5, and a motif kept when at least half of its carriers are depleted (score ≥ 0.5).

counts_panel = pd.read_csv(os.path.join(data_path, "counts_panel.csv.gz"), index_col=0)
left_panel, right_panel = "GTCTAGGGCGGCGGTAAAAC", "ACTAGAGCACCAGAAGTCTA"
log2fc_panel = calculate_log2fc(counts_panel, reference_column="Control_R1", count_threshold=20, pseudocount=1)
log2fc_panel.describe()
Control_R2 JJ1886_R1 JJ1886_R2 CFT073_R1 CFT073_R2 HS_R1 HS_R2 LMR_503_R1 LMR_503_R2 B156_R1 B156_R2 381A_R1 381A_R2 EDL933_R1 EDL933_R2 EDL933_dRM_R1 EDL933_dRM_R2
count 30108.000000 3.010800e+04 30108.000000 30108.000000 30108.000000 30108.000000 30108.000000 30108.000000 30108.000000 30108.000000 3.010800e+04 30108.000000 30108.000000 30108.000000 3.010800e+04 30108.000000 3.010800e+04
mean -0.092785 -1.928490e-01 -0.235595 -0.027408 -0.100585 0.013360 -0.086414 -0.167986 -0.203716 -0.131418 -1.488361e-01 -0.008703 0.040885 0.005516 -7.569209e-02 0.016939 -8.404360e-02
std 0.662484 9.257199e-01 0.980291 0.584757 0.841629 0.548044 0.878549 0.985485 1.040806 0.823174 8.363603e-01 1.140848 1.403861 0.505451 7.503933e-01 0.477549 9.469204e-01
min -4.602036 -7.591883e+00 -7.471095 -6.797323 -7.173709 -7.462027 -7.930737 -6.886132 -7.352450 -6.642861 -5.341084e+00 -5.498806 -4.891134 -8.484900 -7.952919e+00 -7.982994 -7.284022e+00
25% -0.155492 -2.366480e-01 -0.302307 -0.334449 -0.223882 -0.244978 -0.250706 -0.360724 -0.347019 -0.257186 -3.349095e-01 -0.672200 -0.883312 -0.251691 -2.594316e-01 -0.235059 -4.701161e-01
50% 0.000000 -3.469447e-18 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 -8.673617e-19 0.000000 0.000000 0.000000 6.938894e-18 0.000000 -1.387779e-17
75% 0.154173 2.150265e-01 0.252766 0.302789 0.224322 0.251669 0.251436 0.321928 0.315946 0.254159 2.931217e-01 0.649093 0.929936 0.259469 2.641303e-01 0.249652 4.400626e-01
max 1.404808 2.108241e+00 2.176589 2.610762 2.723555 3.356025 3.096185 3.392317 3.375307 2.850857 3.761570e+00 6.309287 6.175992 2.793549 2.605639e+00 2.461265 3.302067e+00
motifs_jj1886 = find_restricted_motifs(log2fc_panel["JJ1886_R1"], left_panel, right_panel)
motifs_jj1886
85 candidate(s) across all patterns; 4 remain after step 3.
4 motif(s) scored.
1 degenerate site(s) reported as one motif:
  RTACNNNNGTG <- ATACNNNNGTG + CACNNNNGTAC
3 motif(s) called.

Scanning patterns:   0%|          | 0/43 [00:00<?, ?it/s]
Scanning patterns:   5%|4         | 2/43 [00:00<00:03, 12.76it/s]
Scanning patterns:   9%|9         | 4/43 [00:00<00:03, 10.89it/s]
Scanning patterns:  14%|#3        | 6/43 [00:00<00:03, 10.66it/s]
Scanning patterns:  19%|#8        | 8/43 [00:00<00:04,  8.71it/s]
Scanning patterns:  21%|##        | 9/43 [00:00<00:03,  8.92it/s]
Scanning patterns:  26%|##5       | 11/43 [00:01<00:03,  9.84it/s]
Scanning patterns:  30%|###       | 13/43 [00:01<00:03,  9.51it/s]
Scanning patterns:  35%|###4      | 15/43 [00:01<00:03,  9.32it/s]
Scanning patterns:  37%|###7      | 16/43 [00:01<00:03,  8.65it/s]
Scanning patterns:  40%|###9      | 17/43 [00:01<00:03,  7.86it/s]
Scanning patterns:  42%|####1     | 18/43 [00:02<00:03,  7.60it/s]
Scanning patterns:  47%|####6     | 20/43 [00:02<00:02,  9.00it/s]
Scanning patterns:  49%|####8     | 21/43 [00:02<00:02,  8.62it/s]
Scanning patterns:  53%|#####3    | 23/43 [00:02<00:02,  9.04it/s]
Scanning patterns:  56%|#####5    | 24/43 [00:02<00:02,  8.76it/s]
Scanning patterns:  60%|######    | 26/43 [00:02<00:01, 10.12it/s]
Scanning patterns:  65%|######5   | 28/43 [00:02<00:01, 11.45it/s]
Scanning patterns:  70%|######9   | 30/43 [00:03<00:01, 11.33it/s]
Scanning patterns:  74%|#######4  | 32/43 [00:03<00:00, 11.46it/s]
Scanning patterns:  79%|#######9  | 34/43 [00:03<00:00, 11.44it/s]
Scanning patterns:  84%|########3 | 36/43 [00:03<00:00, 10.48it/s]
Scanning patterns:  88%|########8 | 38/43 [00:03<00:00,  9.87it/s]
Scanning patterns:  93%|#########3| 40/43 [00:04<00:00,  8.91it/s]
Scanning patterns:  95%|#########5| 41/43 [00:04<00:00,  8.86it/s]
Scanning patterns: 100%|##########| 43/43 [00:04<00:00,  8.65it/s]
Scanning patterns: 100%|##########| 43/43 [00:04<00:00,  9.45it/s]
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
fig, axes = plot_motif_analysis(counts_panel, "JJ1886_R1", "Control_R1", motifs_jj1886, left_panel, right_panel)

The same comparison for motifs you name, without running the search: here JJ1886’s three RM sites as REBASE gives them. [AG] and the IUPAC R are equivalent.

jj1886_motifs = ["AAAGNNNNGTT", "[AG]TACNNNNGTG", "GGTCTC"]  # JJ1886's RM sites
p = plot_motif_depletion(counts_panel, "JJ1886_R1", "Control_R1", jj1886_motifs, left_panel, right_panel)
p