Skip to content

Commit

Permalink
Merge pull request #55 from sanjaynagi/addspanstodf
Browse files Browse the repository at this point in the history
Add primer spans to dataframe and round floats
  • Loading branch information
sanjaynagi authored Sep 27, 2024
2 parents 48b2810 + c5ffe64 commit 1c0a370
Show file tree
Hide file tree
Showing 4 changed files with 64 additions and 9 deletions.
48 changes: 43 additions & 5 deletions AnoPrimer/evaluate.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
_retrieve_span,
_return_oligo_list,
retrieve_data_resource,
round_floats_in_df,
)


Expand Down Expand Up @@ -71,14 +72,16 @@ def __init__(
self.assay_type = assay_type
self.assay_name = assay_name

self.df = primer_df
self.seq_parameters = seq_parameters
self.primer_parameters = primer_parameters

# Extract additional attributes from seq_parameters
self.target_sequence = seq_parameters.get("SEQUENCE_TEMPLATE")
self.gdna_pos = np.array(seq_parameters.get("GENOMIC_DNA_POSITIONS"))

self.df = primer_df
self.df = round_floats_in_df(self.add_spans_to_df(), decimal_places=2)

def evaluate_primers(
self,
sample_sets,
Expand Down Expand Up @@ -120,6 +123,31 @@ def evaluate_primers(
if out_dir is not None and blat_df is not None:
blat_df.to_csv(f"{out_dir}/{self.assay_name}_blat_results.csv")

def add_spans_to_df(self):
df = self.df
oligos, _ = _return_oligo_list(assay_type=self.assay_type)

oligo_spans = {}
for oligo in oligos:
spans = []
for pair in df:
pos = _retrieve_span(
df,
gdna_pos=self.gdna_pos,
oligo=oligo,
assay_type=self.assay_type,
pair=pair,
)
span = f"{self.contig}:{pos.min()}-{pos.max()}"
spans.append(span)

oligo_spans[oligo] = pd.Series(
spans, name=f"primer_{oligo}_span", index=self.df.columns
)
df = pd.concat([df, oligo_spans[oligo].to_frame().T])

return df

def summarise_metadata(self, sample_sets=None, sample_query=None):
"""
Retrieve a summary of metadata for samples in the ag3/af1 resource.
Expand Down Expand Up @@ -350,11 +378,20 @@ def _plot_genes(self, ax, genegff, min_, max_):

def _plot_primers(self, ax, oligos):
"""Helper method to plot primers."""

def _generate_primer_pair_positions(num_pairs, start=1, end=1.45):
if num_pairs == 1:
return [start]

step = (end - start) / (num_pairs - 1)
return [start + i * step for i in range(num_pairs)]

pal = sns.color_palette("Set2", len(self.df.columns))
handles, labels = ax.get_legend_handles_labels()
for pair in self.df:
pair = int(pair)
pair_idx = pair - 1 # python based indexing
pair_ypos = _generate_primer_pair_positions(len(self.df.columns))
for oligo in oligos:
oligo_pos = _retrieve_span(
primer_df=self.df,
Expand All @@ -368,7 +405,7 @@ def _plot_primers(self, ax, oligos):
if oligo == "forward":
plt.arrow(
lower,
0.8 + (2 / (10 - (pair_idx))),
pair_ypos[pair_idx],
upper - lower,
0,
width=0.03,
Expand All @@ -378,18 +415,19 @@ def _plot_primers(self, ax, oligos):
elif oligo == "reverse":
plt.arrow(
upper,
0.8 + (2 / (10 - (pair_idx))),
pair_ypos[pair_idx],
lower - upper,
0,
width=0.03,
length_includes_head=True,
color=pal[pair_idx],
)
elif oligo == "probe":
ax.axhline(y=0.8 + (2 / (10 - (pair_idx))), xmin=lower, xmax=upper)
ax.axhline(y=pair_ypos[pair_idx], xmin=lower, xmax=upper)
line = plt.Line2D(
(lower, upper),
(0.8 + (2 / (10 - (pair))), 0.8 + (2 / (10 - (pair)))),
pair_ypos[pair_idx],
pair_ypos[pair_idx],
lw=2.5,
color=pal[pair_idx],
)
Expand Down
17 changes: 17 additions & 0 deletions AnoPrimer/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,23 @@ def _plotly_frequencies(
#### utility functions ####


def round_floats_in_df(df, decimal_places=1):
import numpy as np

def round_if_float(val):
if isinstance(val, (int, np.integer)):
return val
try:
float_val = float(val)
if float_val.is_integer():
return int(float_val)
return round(float_val, decimal_places)
except (ValueError, TypeError):
return val

return df.applymap(round_if_float)


def extract_trailing_digits(string):
import re

Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[tool.poetry]
name = "AnoPrimer"
version = "2.0.2"
version = "2.0.3"
description = "A package to design primers in Anopheles gambiae whilst considering genetic variation with malariagen_data"
readme = "README.md"
documentation = "https://sanjaynagi.github.io/anoprimer/latest/"
Expand Down
6 changes: 3 additions & 3 deletions tests/run_ci_notebooks.sh
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@

papermill notebooks/AnoPrimer-long.ipynb tests/qPCR_run.ipynb -k AnoPrimer -f tests/cDNA_Params_fun.json &&
papermill notebooks/AnoPrimer-long.ipynb tests/qPCR2_run.ipynb -k AnoPrimer -f tests/cDNA_Params_2_fun.json &&
papermill notebooks/AnoPrimer-long.ipynb tests/gDNA_run.ipynb -k AnoPrimer -f tests/gDNA_probe_Params_fun.json &&
papermill notebooks/AnoPrimer-long.ipynb tests/qPCR_run.ipynb -k AnoPrimer -f tests/cDNA_Params.json &&
papermill notebooks/AnoPrimer-long.ipynb tests/qPCR2_run.ipynb -k AnoPrimer -f tests/cDNA_Params_2.json &&
papermill notebooks/AnoPrimer-long.ipynb tests/gDNA_run.ipynb -k AnoPrimer -f tests/gDNA_probe_Params.json &&
papermill notebooks/AnoPrimer-long.ipynb tests/probe_run.ipynb -k AnoPrimer -f tests/probe_Params.json &&
papermill notebooks/AnoPrimer-long.ipynb tests/qPCR_run.ipynb -k AnoPrimer -f tests/cDNA_Params_fun.json &&
papermill notebooks/AnoPrimer-long.ipynb tests/qPCR2_run.ipynb -k AnoPrimer -f tests/cDNA_Params_2_fun.json &&
papermill notebooks/AnoPrimer-long.ipynb tests/gDNA_run.ipynb -k AnoPrimer -f tests/gDNA_probe_Params_fun.json &&
papermill notebooks/AnoPrimer-short.ipynb tests/short_run.ipynb -k AnoPrimer

0 comments on commit 1c0a370

Please sign in to comment.