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
37 changes: 24 additions & 13 deletions nanoplotter/plot.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,15 @@
from io import BytesIO
from urllib.parse import quote as urlquote
import sys
from kaleido.scopes.plotly import PlotlyScope
import logging

# bring in kaleido and ensure Chrome is installed
import kaleido
# this will download a small headless Chrome build the first time you run it
kaleido.get_chrome_sync()
Comment on lines +8 to +11
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@wdecoster This should resolve the chrome issue from what I understand.


from kaleido import write_fig_sync


class Plot(object):
"""A Plot object is defined by a path to the output file and the title of the plot."""
Expand All @@ -27,20 +33,18 @@ def encode(self):
return self.encode1()

def encode1(self):
"""Return the base64 encoding of the figure file and insert in html image tag."""
data_uri = b64encode(open(self.path, "rb").read()).decode("utf-8").replace("\n", "")
return '<img src="data:image/png;base64,{0}">'.format(data_uri)

def encode2(self):
"""Return the base64 encoding of the fig attribute and insert in html image tag."""
buf = BytesIO()
self.fig.savefig(buf, format="png", bbox_inches="tight", dpi=100)
buf.seek(0)
string = b64encode(buf.read())
return '<img src="data:image/png;base64,{0}">'.format(urlquote(string))

def save(self, settings):
if not(self.only_report):
if not self.only_report:
if self.html:
with open(self.path, "w") as html_out:
html_out.write(self.html)
Expand All @@ -52,12 +56,10 @@ def save(self, settings):
p = os.path.splitext(self.path)[0] + ".png"
if os.path.exists(p):
os.remove(p)

logging.warning("No static plots are saved due to some kaleido problem:")
logging.warning(e)

elif self.fig:
# if settings["format"] is a list, save the figure in all formats
if isinstance(settings["format"], list):
for fmt in settings["format"]:
self.fig.savefig(
Expand All @@ -66,7 +68,11 @@ def save(self, settings):
bbox_inches="tight",
)
else:
self.fig.savefig(fname=self.path, format=settings["format"], bbox_inches="tight")
self.fig.savefig(
fname=self.path,
format=settings["format"],
bbox_inches="tight",
)
else:
sys.exit("No method to save plot object: no html or fig defined.")

Expand All @@ -77,9 +83,14 @@ def show(self):
sys.stderr.write(".show not implemented for Plot instance without fig attribute!")

def save_static(self, figformat):
scope = PlotlyScope()
with open(self.path.replace("html", figformat), "wb") as f:
f.write(scope.transform(self.fig, format=figformat))
logging.info(
f"Saved {self.path.replace('.html', '')} as {figformat} (or png for --legacy)"
)
"""
Export a Plotly figure via Kaleido v1’s write_fig_sync.
"""
output_path = self.path.replace(".html", f".{figformat}")
try:
write_fig_sync(self.fig, path=output_path)
logging.info(f"Saved {output_path} as {figformat}")
except Exception as e:
logging.warning("No static plots are saved due to some kaleido problem:")
logging.warning(e)

16 changes: 16 additions & 0 deletions scripts/agm_test.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
#! /bin/bash

#SBATCH --time=04-00:00:00
#SBATCH --partition=defq
#SBATCH --mail-user=myemail@email.org
#SBATCH --mail-type=BEGIN,END,FAIL
#SBATCH --ntasks-per-node=64
#SBATCH --mem=128GB
#SBATCH --nodes=1
#SBATCH --job-name=nplot
#SBATCH --comment=nplot

source /path/to/nanoplot_env/bin/activate

# test fresh nanoplot with kaleido update
NanoPlot --fastq /path/to/test_file.fastq.gz --verbose --minqual 4 --color red -o scripts/agm_tests
2 changes: 2 additions & 0 deletions scripts/agm_tests/LengthvsQualityScatterPlot_dot.html

Large diffs are not rendered by default.

Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
2 changes: 2 additions & 0 deletions scripts/agm_tests/LengthvsQualityScatterPlot_kde.html

Large diffs are not rendered by default.

Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading