Skip to content

Commit

Permalink
Merge pull request #48 from shirte/main
Browse files Browse the repository at this point in the history
Make SdfWriter handle mol=None
  • Loading branch information
shirte authored Dec 13, 2024
2 parents 840af50 + 752b53f commit 9916c36
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 16 deletions.
2 changes: 1 addition & 1 deletion nerdd_module/converters/__init__.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from .basic_type_converter import *
from .converter import *
from .converter_config import *
from .identity_converter import *
from .mol_converter import *
from .mol_to_image_converter import *
from .problem_list_converter import *
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,21 +3,21 @@
from .converter import Converter
from .converter_config import ALL, ConverterConfig

__all__ = ["IdentityConverter", "primitive_data_types"]
__all__ = ["BasicTypeConverter", "basic_data_types"]

primitive_data_types = [
basic_data_types = [
"int",
"float",
"string",
"bool",
]


class IdentityConverter(Converter):
class BasicTypeConverter(Converter):
def _convert(self, input: Any, context: dict) -> Any:
return input

config = ConverterConfig(
data_types=primitive_data_types,
data_types=basic_data_types,
output_formats=ALL,
)
17 changes: 13 additions & 4 deletions nerdd_module/output/sdf_writer.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from typing import IO, Any, Dict, Iterable

from rdkit.Chem import SDWriter
from rdkit.Chem import Mol, SDWriter

from .file_writer import FileLike, FileWriter

Expand All @@ -18,13 +18,22 @@ def _write(self, output: IO[Any], entries: Iterable[Dict]) -> None:
# assume that there is a mol object
mol = entry["input_mol"]

# if the molecule is erroneous, use an empty molecule
if mol is None:
mol = Mol()

# write (almost) all properties to the mol object
for key, value in entry.items():
value_as_str = str(value)
if "\n" in value_as_str:
# SDF can't write multi-line strings
# skip "input_mol" key, because we use it as the main molecule
if key == "input_mol":
continue

value_as_str = str(value)

# SDF can't write multi-line strings
# -> replace newline with space
value_as_str = value_as_str.replace("\n", " ")

mol.SetProp(key, value_as_str)

# write molecule
Expand Down
10 changes: 3 additions & 7 deletions tests/basic/converters/test_converters.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,7 @@
from nerdd_module import Problem
from nerdd_module.config import ResultProperty
from nerdd_module.converters import (
Converter,
IdentityConverter,
ProblemListConverter,
VoidConverter,
)
from nerdd_module.converters import (BasicTypeConverter, Converter,
ProblemListConverter, VoidConverter)

primitive_data_types = [
"int",
Expand All @@ -23,7 +19,7 @@ def test_basic_data_types():
result_property = ResultProperty(name="test", type=primitive_data_type)
converter = Converter.get_converter(result_property, output_format)
assert converter is not None
assert isinstance(converter, IdentityConverter)
assert isinstance(converter, BasicTypeConverter)


def test_non_existing_data_type():
Expand Down

0 comments on commit 9916c36

Please sign in to comment.