Skip to content

Commit 7ef2c13

Browse files
committed
add motif logo to report tables
1 parent aebd76f commit 7ef2c13

2 files changed

Lines changed: 16 additions & 9 deletions

File tree

tpcav/cavs.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -567,11 +567,11 @@ def compute_motif_auc_fscore(num_motif_insertions: List[int], cav_trainers: List
567567

568568
assert motif_file_fmt in ['meme', 'consensus']
569569

570-
cavs_fscores_df = pd.DataFrame({nm: cav_trainer.cav_fscores for nm, cav_trainer in zip(num_motif_insertions, cav_trainers)})
570+
cavs_fscores_df = pd.DataFrame({f"fscore_{nm}_insertions": cav_trainer.cav_fscores for nm, cav_trainer in zip(num_motif_insertions, cav_trainers)})
571571
cavs_fscores_df['concept'] = list(cav_trainers[0].cav_fscores.keys())
572572

573573
def compute_auc_fscore(row):
574-
y = [row[nm] for nm in num_motif_insertions]
574+
y = [row[f"fscore_{nm}_insertions"] for nm in num_motif_insertions]
575575
return np.trapz(y, num_motif_insertions) / (
576576
num_motif_insertions[-1] - num_motif_insertions[0]
577577
)

tpcav/report.py

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ def _render_kv_table(rows: list[tuple[str, str]]) -> str:
2828
def _render_df_table(df: Any, max_rows: int = 200) -> str:
2929
try:
3030
df2 = df.head(max_rows)
31-
return df2.to_html(index=False, escape=True, classes="df")
31+
return df2.to_html(index=False, escape=False, classes="df")
3232
except Exception:
3333
return f"<pre>{_html.escape(str(df)[:20000])}</pre>"
3434

@@ -198,7 +198,7 @@ def generate_tpcav_html_report(
198198
"source": "motif",
199199
}
200200
for nm in motif_insertions:
201-
out[f"fscore_{nm}"] = row.get(nm)
201+
out[f"fscore_{nm}_insertions"] = row.get(nm)
202202
out["AUC_fscores"] = row.get("AUC_fscores")
203203
out["Motif_concept_sensitivity_score (AUC_fscores_residual)"] = row.get("Motif_concept_sensitivity_score (AUC_fscores_residual)")
204204
concept_rows.append(out)
@@ -344,15 +344,16 @@ def _new_trainer() -> Any:
344344
# 5) Build JS payload (used by Plotly)
345345
# -----------------------------------------------------------------------------
346346
motif_logo_concepts = selected_motif_concepts[:]
347+
motif_logo_dict = _maybe_build_motif_logo_data_uris(
348+
motif_file if motif_file_fmt == "meme" else None,
349+
motif_logo_concepts,
350+
)
347351
js_payload: dict[str, Any] = {
348352
"motif_file_fmt": motif_file_fmt,
349353
"motif_auc_rows": motif_auc_df.to_dict(orient="records") if motif_auc_df is not None else None,
350354
"motif_insertions": motif_insertions,
351355
"concept_rows": concept_rows,
352-
"motif_logos": _maybe_build_motif_logo_data_uris(
353-
motif_file if motif_file_fmt == "meme" else None,
354-
motif_logo_concepts,
355-
),
356+
"motif_logos": motif_logo_dict,
356357
"heatmaps": {
357358
"motif": {"heatmap": None, "heatmap_div_id": "heatmap__motif", "hover_div_id": "hover__motif"},
358359
"non-motif": {"heatmap": None, "heatmap_div_id": "heatmap__non_motif", "hover_div_id": "hover__non_motif"},
@@ -432,6 +433,9 @@ def _to_list(x: Any) -> Any:
432433

433434
motif_auc_table_html = ""
434435
if motif_auc_df is not None:
436+
# append motif logo column if exists
437+
if motif_logo_dict is not None:
438+
motif_auc_df['motif_logo'] = motif_auc_df.apply(lambda x: "<img src=\"" + motif_logo_dict[x['concept']] + "\" width=\"100\">", axis=1)
435439
motif_auc_table_html = _render_df_table(motif_auc_df, max_rows=5000)
436440

437441
if embed_images:
@@ -480,14 +484,17 @@ def _to_list(x: Any) -> Any:
480484

481485
motif_cols = (
482486
["rank", "concept"]
483-
+ [f"fscore_{nm}" for nm in motif_insertions]
487+
+ [f"fscore_{nm}_insertions" for nm in motif_insertions]
484488
+ ["AUC_fscores", "Motif_concept_sensitivity_score (AUC_fscores_residual)", "source"]
485489
)
486490
extra_cols = ["concept", "source", "fscore"]
487491

488492
if motif_concept_rows:
489493
motif_df = pd.DataFrame(motif_concept_rows)
490494
motif_df = motif_df[[c for c in motif_cols if c in motif_df.columns]]
495+
# append motif logo column if exists
496+
if motif_logo_dict is not None:
497+
motif_df['motif_logo'] = motif_df.apply(lambda x: "<img src=\"" + motif_logo_dict[x['concept']] + "\" width=\"100\">", axis=1)
491498
motif_table_html = _render_df_table(motif_df, max_rows=5000)
492499
if non_motif_concept_rows:
493500
extra_df = pd.DataFrame(non_motif_concept_rows)

0 commit comments

Comments
 (0)