-
Notifications
You must be signed in to change notification settings - Fork 16
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #831 from Clinical-Genomics/release_8.2.4
feat: Balsamic Release 8.2.4
- Loading branch information
Showing
12 changed files
with
243 additions
and
21 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,105 @@ | ||
#!/usr/bin/env python | ||
import os | ||
import click | ||
|
||
from fpdf import FPDF | ||
from PIL import Image | ||
|
||
|
||
@click.command( | ||
short_help="Merge images and a txt file into a single PDF", | ||
) | ||
@click.argument("output", type=click.Path(exists=False), required=True) | ||
@click.argument("data", type=click.Path(exists=True), required=True) | ||
@click.argument("images", nargs=-1, type=click.Path(exists=True), required=True) | ||
def create_pdf(output, data, images): | ||
pdf = generate_fpdf() | ||
pdf = add_table_pdf(pdf, data) | ||
pdf = add_images_pdf(pdf, images) | ||
pdf.output(output) | ||
|
||
|
||
class PDF(FPDF): | ||
def footer(self): | ||
self.set_y(-15) | ||
self.set_font("helvetica", "I", 8) | ||
self.cell(0, 10, f"Page {self.page_no()}/{{nb}}", 0, 0, "C") | ||
|
||
|
||
def generate_fpdf(): | ||
pdf = PDF() | ||
pdf.alias_nb_pages(alias="{nb}") | ||
return pdf | ||
|
||
|
||
def add_images_pdf(pdf, img_paths): | ||
pdf.set_font("helvetica", "B", 15) | ||
|
||
for path in img_paths: | ||
title = os.path.basename(path).replace(".png", "") | ||
|
||
# Image & page layout parameters | ||
if "sunrise" in title: | ||
page_orientation = "portrait" | ||
img_size = 500, 500 | ||
title_w_pos = 25 | ||
title_wh = 140, 10 | ||
img_xy = 10, 55 | ||
else: | ||
page_orientation = "landscape" | ||
img_size = 800, 800 | ||
title_w_pos = 68.5 | ||
title_wh = 140, 10 | ||
img_xy = 5, 40 | ||
|
||
pdf.add_page(orientation=page_orientation) | ||
|
||
# Title position & styling | ||
pdf.cell(title_w_pos) | ||
pdf.cell(title_wh[0], title_wh[1], title, 1, 0, "C") | ||
|
||
# Image position & resizing | ||
img = Image.open(path) | ||
img.thumbnail(img_size, Image.ANTIALIAS) | ||
pdf.image(img, img_xy[0], img_xy[1]) | ||
|
||
return pdf | ||
|
||
|
||
def add_table_pdf(pdf, data_path): | ||
|
||
with open(data_path) as data: | ||
data = data.readlines() | ||
|
||
pdf.add_page() | ||
pdf.set_font("helvetica", "B", 15) | ||
|
||
# Title layout & styling | ||
title = os.path.basename(data_path).replace(".txt", "") | ||
pdf.cell(25) | ||
pdf.cell(140, 10, title, 1, 0, "C") | ||
pdf.cell(35, 25, ln=1) # Post title indentation | ||
|
||
# Table layout & styling | ||
pdf.set_font("Times", size=11) | ||
line_height = pdf.font_size * 2.5 | ||
col_width = pdf.epw / 4 # Even distribution of the content | ||
for row in data: | ||
pdf.cell(45) | ||
for statistic in row.split(): | ||
pdf.multi_cell( | ||
col_width, | ||
line_height, | ||
statistic, | ||
align="C", | ||
border=1, | ||
ln=3, | ||
max_line_height=pdf.font_size, | ||
) | ||
pdf.ln(line_height) | ||
|
||
return pdf | ||
|
||
|
||
if __name__ == "__main__": | ||
create_pdf() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
from pathlib import Path | ||
|
||
from fpdf import FPDF | ||
|
||
from BALSAMIC.assets.scripts.create_pdf import ( | ||
generate_fpdf, | ||
add_images_pdf, | ||
add_table_pdf, | ||
create_pdf, | ||
) | ||
|
||
|
||
def test_generate_fpdf(): | ||
# WHEN creating a dummy FPDF file | ||
pdf = generate_fpdf() | ||
|
||
# THEN check if the pdf has been correctly created | ||
assert isinstance(pdf, FPDF) | ||
|
||
|
||
def test_add_images_pdf(): | ||
# GIVEN ascatNGgs output PNG images | ||
test_images_path = [ | ||
"tests/test_data/ascat_output/CNV.somatic.SAMPLE.ascat.sunrise.png", | ||
"tests/test_data/ascat_output/CNV.somatic.SAMPLE.ascat.germline.png", | ||
] | ||
|
||
# WHEN calling the function | ||
pdf = add_images_pdf(generate_fpdf(), test_images_path) | ||
|
||
# THEN check if the images are appended to the PDF | ||
assert isinstance(pdf, FPDF) | ||
assert pdf.page_no() == 2 | ||
|
||
|
||
def test_add_table_pdf(): | ||
# GIVEN ascatNGgs output sample statistics .txt | ||
test_statistics_path = ( | ||
"tests/test_data/ascat_output/CNV.somatic.SAMPLE.ascat.samplestatistics.txt" | ||
) | ||
|
||
# WHEN calling the function | ||
pdf = add_table_pdf(generate_fpdf(), test_statistics_path) | ||
|
||
# THEN check if the table is appended to the created PDF | ||
assert isinstance(pdf, FPDF) | ||
assert pdf.page_no() == 1 | ||
|
||
|
||
def test_create_pdf(tmp_path, cli_runner): | ||
# GIVEN ascatNGgs output statistics | ||
statistics_path = ( | ||
"tests/test_data/ascat_output/CNV.somatic.SAMPLE.ascat.samplestatistics.txt" | ||
) | ||
|
||
# GIVEN ascatNGgs output plots | ||
plots_path = [ | ||
"tests/test_data/ascat_output/CNV.somatic.SAMPLE.ascat.germline.png", | ||
"tests/test_data/ascat_output/CNV.somatic.SAMPLE.ascat.sunrise.png", | ||
] | ||
|
||
# GIVEN the output path | ||
output_path = tmp_path / "ascat.output.pdf" | ||
|
||
print(output_path) | ||
|
||
# WHEN invoking the python script | ||
result = cli_runner.invoke( | ||
create_pdf, [str(output_path), statistics_path, plots_path[0], plots_path[1]] | ||
) | ||
|
||
# THEN check if the PDF is correctly created and there is no errors | ||
assert result.exit_code == 0 | ||
assert Path(output_path).exists() |
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
7 changes: 7 additions & 0 deletions
7
tests/test_data/ascat_output/CNV.somatic.SAMPLE.ascat.samplestatistics.txt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
NormalContamination 0.378222936036507 | ||
Ploidy 2.69008904657384 | ||
rho 0.55 | ||
psi 2.75 | ||
goodnessOfFit 93.9311185291303 | ||
GenderChr Y | ||
GenderChrFound N |
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.