Skip to content

Commit

Permalink
update code style: black
Browse files Browse the repository at this point in the history
using black code style
  • Loading branch information
Augus1999 authored Mar 12, 2022
1 parent 98debf5 commit 7dab30c
Show file tree
Hide file tree
Showing 13 changed files with 742 additions and 659 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
[![Downloads](https://static.pepy.tech/personalized-badge/mol2chemfigpy3?period=total&units=international_system&left_color=black&right_color=green&left_text=Downloads)](https://pepy.tech/project/mol2chemfigpy3)
![OS](https://img.shields.io/badge/OS-Win%20|%20Linux%20|%20macOS-blue?color=00B16A)
![python](https://img.shields.io/badge/Python-3.8%20|%203.9%20|%203.10-blue.svg?color=dd9b65)
![black](https://img.shields.io/badge/code%20style-black-black)

This is NOT an official version of mol2chemfig for python 3.

Expand Down
54 changes: 32 additions & 22 deletions mol2chemfigPy3/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,21 +9,24 @@
from .main import main
from .processor import process
from .common import program_version

__version__ = program_version
__Author__ = 'Nianze A. TAO'
__all__ = ['main', 'mol2chemfig', '__version__']
__Author__ = "Nianze A. TAO"
__all__ = ["main", "mol2chemfig", "__version__"]


def mol2chemfig(content: str,
*args: str,
rotate: float = 0.0,
aromatic: bool = True,
marker: Optional[str] = None,
name: Optional[str] = None,
relative_angle: bool = False,
show_carbon: bool = False,
show_methyl: bool = False,
inline: bool = False) -> Optional[str]:
def mol2chemfig(
content: str,
*args: str,
rotate: float = 0.0,
aromatic: bool = True,
marker: Optional[str] = None,
name: Optional[str] = None,
relative_angle: bool = False,
show_carbon: bool = False,
show_methyl: bool = False,
inline: bool = False,
) -> Optional[str]:
"""
wrapper of mol2chemfigPy3.process(.)
Expand All @@ -42,20 +45,27 @@ def mol2chemfig(content: str,
assert isinstance(relative_angle, bool), "This value should be in type Bool"
assert isinstance(show_carbon, bool), "This value should be in type Bool"
assert isinstance(show_methyl, bool), "This value should be in type Bool"
others = ' '.join(args)
arg = f'-wz{"o" if aromatic else ""}{"v" if relative_angle else ""}' \
f'{"c" if show_carbon else ""}{"m" if show_methyl else ""}' \
f' -a {rotate} {"" if marker is None else "-g "+marker}' \
f' {"" if name is None else "-l "+name} {others}'
arg = re.sub(r'\s+', ' ', arg).split()
if content.endswith(('.mol', '.smi',)):
arg += ['-i', 'file', content]
others = " ".join(args)
arg = (
f'-wz{"o" if aromatic else ""}{"v" if relative_angle else ""}'
f'{"c" if show_carbon else ""}{"m" if show_methyl else ""}'
f' -a {rotate} {"" if marker is None else "-g "+marker}'
f' {"" if name is None else "-l "+name} {others}'
)
arg = re.sub(r"\s+", " ", arg).split()
if content.endswith(
(
".mol",
".smi",
)
):
arg += ["-i", "file", content]
else:
try:
pubchem_id = int(content)
arg += ['-i', 'pubchem', str(pubchem_id)]
arg += ["-i", "pubchem", str(pubchem_id)]
except ValueError:
arg += ['-i', 'direct', content]
arg += ["-i", "direct", content]
success, result = process(raw_args=arg, inline=True)
if inline:
return result.render_user() if success else result
Expand Down
2 changes: 1 addition & 1 deletion mol2chemfigPy3/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,4 @@


if __name__ == "__main__":
mol2chemfigPy3.main('mol2chemfig')
mol2chemfigPy3.main("mol2chemfig")
70 changes: 37 additions & 33 deletions mol2chemfigPy3/atom.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import string
from typing import Optional, Union
from . import chemfig_mappings as cfm

# some atoms should carry their hydrogen to the left, rather than
# to the right. This is applied to solitary atoms, but not to bonded
# functional groups that contain those elements.
Expand All @@ -17,38 +18,41 @@ class Atom:
wrapper around toolkit atom object, augmented with coordinates
helper class for molecule.Molecule
"""

explicit_characters = set(string.ascii_uppercase + string.digits)

quadrant_turf = 80 # 80 degrees have to remain free on either side

quadrants = [ # quadrants for hydrogen placement
[0, 0, 'east'],
[1, 180, 'west'],
[2, 270, 'south'],
[3, 90, 'north']
[0, 0, "east"],
[1, 180, "west"],
[2, 270, "south"],
[3, 90, "north"],
]

charge_positions = [ # angles for placement of detached charges
[0, 15, 'top_right'],
[1, 165, 'top_left'],
[2, 90, 'top_center'],
[3, 270, 'bottom_center'],
[4, 345, 'bottom_right'],
[5, 195, 'bottom_left']
[0, 15, "top_right"],
[1, 165, "top_left"],
[2, 90, "top_center"],
[3, 270, "bottom_center"],
[4, 345, "bottom_right"],
[5, 195, "bottom_left"],
]

charge_turf = 50 # reserved angle for charges - needs to be big enough for 2+

def __init__(self,
options: dict,
idx: int,
x: Union[int, float],
y: Union[int, float],
element: Optional[str],
hydrogens: Optional[int],
charge: int,
radical: int,
neighbors: list[int]):
def __init__(
self,
options: dict,
idx: int,
x: Union[int, float],
y: Union[int, float],
element: Optional[str],
hydrogens: Optional[int],
charge: int,
radical: int,
neighbors: list[int],
):
self.options = options
self.idx = idx
self.x = x
Expand All @@ -64,16 +68,14 @@ def __init__(self,
# angles of all attached bonds - to be populated later
self.bond_angles = []
self.explicit = False # flag for explicitly printed atoms - set later
marker = self.options.get('markers', None)
marker = self.options.get("markers", None)
if marker is not None:
self.marker = f"{marker}{self.idx + 1}"
else:
self.marker = ""

@staticmethod
def _score_angle(a: int,
b: int,
turf: int) -> int:
def _score_angle(a: int, b: int, turf: int) -> int:
"""
helper. calculates absolute angle between a and b,
i.e., 0 <= angle <= 180.
Expand All @@ -89,9 +91,7 @@ def _score_angle(a: int,
angle = min(diff, 360 - diff)
return (max(0, turf - angle)) ** 2

def _score_angles(self,
choices: list[list[int, int, str]],
turf: int) -> list[str]:
def _score_angles(self, choices: list[list[int, int, str]], turf: int) -> list[str]:
"""
backend for score_angles
Expand Down Expand Up @@ -127,17 +127,21 @@ def score_angles(self) -> None:
if len(self.bond_angles) > 0: # this atom is bonded
quadrants = self._score_angles(self.quadrants, self.quadrant_turf)
self.first_quadrant = quadrants[0]
self.second_quadrant = quadrants[1] # 2nd choice may be used for radical electrons
self.second_quadrant = quadrants[
1
] # 2nd choice may be used for radical electrons

else: # this atom is solitary
if self.element in hydrogen_lefties:
self.first_quadrant = 'west'
self.second_quadrant = 'east'
self.first_quadrant = "west"
self.second_quadrant = "east"
else:
self.first_quadrant = 'east'
self.second_quadrant = 'west'
self.first_quadrant = "east"
self.second_quadrant = "west"

self.charge_angle = self._score_angles(self.charge_positions, self.charge_turf)[0]
self.charge_angle = self._score_angles(self.charge_positions, self.charge_turf)[
0
]

def render_phantom(self) -> tuple[Optional[str], str]:
"""
Expand Down
Loading

0 comments on commit 7dab30c

Please sign in to comment.