-
Notifications
You must be signed in to change notification settings - Fork 22
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* add selectable output to sdfield * fix sdfilter output * add sdmodify * add sdmodify basic tests
- Loading branch information
1 parent
960ddd0
commit 2cfa165
Showing
8 changed files
with
125 additions
and
8 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
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,42 @@ | ||
import argparse | ||
import logging | ||
import sys | ||
from dataclasses import dataclass | ||
|
||
from rdock_utils.common import inputs_generator, read_molecules_from_all_inputs | ||
|
||
logger = logging.getLogger("sdmodify") | ||
|
||
|
||
def get_parser() -> argparse.ArgumentParser: | ||
parser = argparse.ArgumentParser(description="Set the first title line equal to a given data field") | ||
parser.add_argument( | ||
"-f", "--field", type=str, required=True, help="Data field to set the first title line equal to" | ||
) | ||
infile_help = "input file[s] to be processed. if not provided, stdin is used." | ||
parser.add_argument("infiles", type=str, nargs="*", help=infile_help) | ||
return parser | ||
|
||
|
||
@dataclass | ||
class SDModifyConfig: | ||
field: str | ||
infiles: list[str] | ||
|
||
|
||
def get_config(argv: list[str] | None = None) -> SDModifyConfig: | ||
parser = get_parser() | ||
args = parser.parse_args(argv) | ||
return SDModifyConfig(field=args.field, infiles=args.infiles) | ||
|
||
|
||
def main(argv: list[str] | None = None) -> None: | ||
config = get_config(argv) | ||
inputs = inputs_generator(config.infiles) | ||
for index, mol in enumerate(read_molecules_from_all_inputs(inputs), start=1): | ||
value = mol.get_field(config.field) if config.field != "_REC" else str(index) | ||
if value is None: | ||
logger.warning(f"field {config.field} not found in molecule {mol.title}, skipping...") | ||
else: | ||
mol.set_title(value) | ||
mol.write(sys.stdout) |
Empty file.
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,5 @@ | ||
from ..conftest import FIXTURES_FOLDER | ||
|
||
# reuse sdfilter fixture | ||
SDFILTER_FIXTURES_FOLDER = FIXTURES_FOLDER / "sdfilter" | ||
INPUT_FILE = str(SDFILTER_FIXTURES_FOLDER / "input.sdf") |
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,37 @@ | ||
from io import StringIO | ||
|
||
import pytest | ||
|
||
from rdock_utils.common import read_molecules | ||
from rdock_utils.sdmodify import main | ||
|
||
from .conftest import INPUT_FILE | ||
|
||
|
||
def test_do_nothing(): | ||
with pytest.raises(SystemExit): | ||
main() | ||
|
||
|
||
@pytest.mark.parametrize( | ||
"args, expected_titles", | ||
[ | ||
pytest.param( | ||
["-f", "test_field", INPUT_FILE], | ||
(["0.0", "0.0", "2.0", "3.0", "4.0", "0.0"]), | ||
id="molecule field filter", | ||
), | ||
pytest.param( | ||
["-f", "_REC", INPUT_FILE], | ||
list(map(str, range(1, 7))), | ||
id="molecule field filter with output file", | ||
), | ||
], | ||
) | ||
def test_basic_run(args: list[str], expected_titles: list[str], capsys: pytest.CaptureFixture): | ||
main(args) | ||
captured = capsys.readouterr() | ||
input = StringIO(captured.out) | ||
molecules = read_molecules(input) | ||
titles = [m.title for m in molecules] | ||
assert titles == expected_titles |