Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 25 additions & 6 deletions ddmc/figures/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,15 @@
import importlib
import sys
import time
import xml.etree.ElementTree as ET
from collections.abc import Sequence
from string import ascii_uppercase

import drawsvg as draw
import logomaker as lm
import numpy as np
import pandas as pd
import seaborn as sns
import svgutils.transform as st
from matplotlib import axes, gridspec, rcParams
from matplotlib import pyplot as plt
from scipy.stats import mannwhitneyu
Expand All @@ -39,6 +40,9 @@

rcParams["font.sans-serif"] = "Arial"

ET.register_namespace("", "http://www.w3.org/2000/svg")
ET.register_namespace("xlink", "http://www.w3.org/1999/xlink")


def getSetup(
figsize: tuple[int, int],
Expand Down Expand Up @@ -112,6 +116,13 @@ def subplotLabel(axs: list[axes.Axes]) -> None:
)


def _read_svg(path: str) -> tuple[dict, str]:
"""Parse an SVG file, returning its root attributes and inner markup."""
root = ET.parse(path).getroot()
inner = "".join(ET.tostring(child, encoding="unicode") for child in root)
return root.attrib, inner


def overlayCartoon(
figFile: str, cartoonFile: str, x: float, y: float, scalee: float = 1.0
) -> None:
Expand All @@ -126,13 +137,21 @@ def overlayCartoon(
"""

# Overlay Figure cartoons
template = st.fromfile(figFile)
cartoon = st.fromfile(cartoonFile).getroot()
fig_attrib, fig_inner = _read_svg(figFile)
_, cartoon_inner = _read_svg(cartoonFile)

d = draw.Drawing(
fig_attrib.get("width"),
fig_attrib.get("height"),
viewBox=fig_attrib.get("viewBox"),
)
d.append(draw.Raw(fig_inner))

cartoon.moveto(x, y, scale_x=scalee, scale_y=scalee)
cartoon = draw.Group(transform=f"translate({x}, {y}) scale({scalee} {scalee})")
cartoon.append(draw.Raw(cartoon_inner))
d.append(cartoon)

template.append(cartoon)
template.save(figFile)
d.save_svg(figFile)


def genFigure() -> None:
Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ dependencies = [
"scipy>=1.16",
"matplotlib>=3.10",
"seaborn>=0.13.2",
"svgutils>=0.3.4",
"drawsvg>=2.4",
"pandas>=3",
"biopython>=1.85",
"scikit-learn>=1.5",
Expand Down
Loading