Skip to content

Commit

Permalink
feat: selects the resolution for the logo output in png
Browse files Browse the repository at this point in the history
  • Loading branch information
thomas-bailly committed Jun 10, 2024
1 parent 57ca0ba commit ef47772
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 37 deletions.
56 changes: 24 additions & 32 deletions asmc/asmc.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ def build_ds(ref: Path, outdir: Path, chains: str) -> Tuple[Path, str]:
"""Build dataset file for p2rank
Args:
ref (patlib.Path): Path to reference file
ref (pathlib.Path): Path to reference file
outdir (pathlib.Path): Path to the output directory
chains (str): String indicating which chain to search
Expand Down Expand Up @@ -120,7 +120,7 @@ def build_pocket_text(ref: Path, res_dict: Dict[str, List[int]], outdir: Path,
"""Build the pocket file
Args:
ref (patlib.Path): Path to reference file
ref (pathlib.Path): Path to reference file
res_dict (dict): Dict containing as key the chain and as values the
positions
outdir (pathlib.Path): Path to the output directory
Expand Down Expand Up @@ -691,7 +691,7 @@ def build_fasta(group: List[Tuple[str, str, Optional[Any]]]) -> str:
return text

def build_logo(lenght: int, fasta: Path, outdir: Path, n: int, prefix: str,
out_format: str) -> None:
out_format: str, resolution: int) -> None:
"""Build weblogo for a Group
Args:
Expand All @@ -701,6 +701,7 @@ def build_logo(lenght: int, fasta: Path, outdir: Path, n: int, prefix: str,
n (int): Cluster id
prefix (str): Prefix for the weblogo title
out_format (str): eps or png
resolution (int): image resolution
Raises:
Exception: Raised when an error has occured during the creation of the
Expand All @@ -719,7 +720,8 @@ def build_logo(lenght: int, fasta: Path, outdir: Path, n: int, prefix: str,
options.color_scheme = weblogo.chemistry
logo_format = weblogo.LogoFormat(data, options)
if out_format == "png":
logo_bytes = weblogo.png_print_formatter(data, logo_format)
logo_format.resolution = resolution
logo_bytes = weblogo.png_formatter(data, logo_format)
output = Path.joinpath(outdir, f"{prefix}{n}.png")
elif out_format == "eps":
logo_bytes = weblogo.eps_formatter(data, logo_format)
Expand All @@ -731,18 +733,15 @@ def build_logo(lenght: int, fasta: Path, outdir: Path, n: int, prefix: str,
raise Exception("An error has occured when creating the logo of"
f" {prefix}{n}:\n{error}")

def merge_logo(outdir: Path, n: int, prefix: str, out_format: str) -> None:
def merge_logo(outdir: Path, prefix: str, out_format: str) -> None:
"""Merge single logo files
Args:
outdir (pathib.Path): Output directory
n (int): The number of groups
prefix (str): Prefix for the weblogo title
out_format (str): eps or png
out_format (str): png
"""

LOGO_PAD = 10

IM_WIDTH = 500

all_file = [f for f in outdir.iterdir() if f.match(f"{prefix}*.{out_format}")]
Expand All @@ -755,36 +754,29 @@ def merge_logo(outdir: Path, n: int, prefix: str, out_format: str) -> None:
except:
pass

logo_list = []

if out_format == "png":
LOGO_WIDTH = 450
LOGO_HEIGHT = 175
im_height = n * LOGO_HEIGHT + ((n-1) * LOGO_PAD) + LOGO_PAD * 2
else:
LOGO_HEIGHT = 92*2
im_height = n * LOGO_HEIGHT + ((n-1) * LOGO_PAD) + LOGO_PAD * 2
for f in all_file:
logo = Image.open(f)
logo_list.append(logo)

logo_size = logo_list[0].size

IM_WIDTH = logo_size[0]
IM_HEIGHT = logo_size[1] * len(logo_list)

img = Image.new(mode='RGB', size=(IM_WIDTH, im_height), color=(255,255,255))
img = Image.new(mode="RGB", size=(IM_WIDTH, IM_HEIGHT), color=(255,255,255))
draw = ImageDraw.Draw(img)

top_left_coord = (LOGO_PAD, LOGO_PAD)
top_left_coord = (0,0)

for i, f in enumerate(all_file):

logo = Image.open(f)
if out_format == "png":
logo = logo.resize(size=(LOGO_WIDTH, LOGO_HEIGHT))
else:
logo.load(scale=2)

for i, logo in enumerate(logo_list):
if i != 0:
top_left_coord = (top_left_coord[0],
top_left_coord[1] + LOGO_PAD + LOGO_HEIGHT)

top_left_coord = (0, top_left_coord[1] + logo_size[1])

img.paste(logo, top_left_coord)

output = Path.joinpath(outdir,
f"groups_logo.{out_format}")

output = outdir / f"groups_logo.png"

img.save(output)

Expand Down
15 changes: 10 additions & 5 deletions run_asmc.py
Original file line number Diff line number Diff line change
Expand Up @@ -111,9 +111,9 @@ def run_build_ali(ref, seq, pocket, outdir, pid, log):
build_ali.py is the script used to prepare the modeling step.
Args:
ref (patlib.Path): Path to reference file
seq (patlib.Path): Path to a multi fasta file
pocket (patlib.Path): Path to the pocket file
ref (pathlib.Path): Path to reference file
seq (pathlib.Path): Path to a multi fasta file
pocket (pathlib.Path): Path to the pocket file
outdir (pathlib.Path): Path to the output directory
pid (float): identity cutoff
Expand Down Expand Up @@ -413,6 +413,10 @@ def run_usalign(job, usalign, log):
choices=["eps", "png"],
help="file format for output logos, 'eps' or 'png'"
" [default: 'png']")
weblogo_opt.add_argument("--resolution", type=int, metavar="", default=300,
choices=[150,300,600],
help="image resolution (png only), 150, 300 or 600"
" dpi [default: 300]")

args = parser.parse_args()

Expand Down Expand Up @@ -759,12 +763,13 @@ def run_usalign(job, usalign, log):
fasta.write_text(fasta_text)
try:
asmc.build_logo(len(group_seq), fasta, outdir, n, args.prefix,
args.format)
args.format, args.resolution)
except Exception as error:
logging.error(error)

# Merge logos in a single file
asmc.merge_logo(outdir, len(unique), args.prefix, args.format)
if args.format == "png":
asmc.merge_logo(outdir, args.prefix, args.format)
outdir = Path(args.outdir).absolute()

logging.info(f"Total Elapsed time: {datetime.datetime.now() - start}")

0 comments on commit ef47772

Please sign in to comment.