Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ dependencies = [
dynamic = ["version"]

[project.scripts]
calc-pr = "metfish.commands:get_Pr_cli"
metfish = "metfish:main"

[tool.ruff]
# Exclude a variety of commonly ignored directories.
Expand Down
47 changes: 47 additions & 0 deletions src/metfish/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
import numpy as np
from importlib import import_module

class Command:
def __init__(self, module, doc):
ar = f'metfish.{module}'.split('.')
self.pkg = '.'.join(ar[:-1])
self.func = ar[-1]
self.doc = doc

def get_func(self):
return getattr(import_module(self.pkg), self.func)


def main():

# Add commands here
command_dict = {
'SAXS': {
'calc-pr': Command('calc_pr.main', 'Calculate P(r) curves for PDB structures'),
},
}
import sys
if len(sys.argv) == 1:
print('Usage: metfish <command> [options]')
print('Available commands are:\n')
for g, d in command_dict.items():
print(f' {g}')
for c, f in d.items():
nspaces = 16 - len(c)
desc = ''
print(f' {c}' + ' '*nspaces + f.doc)
print()
else:
cmd = sys.argv[1]
for g, d in command_dict.items():
func = d.get(cmd)
if func is not None:
func = func.get_func()
break
if func is not None:
argv = sys.argv[2:]
sys.argv[0] = sys.argv[0] + " " + sys.argv[1]
func(argv)
else:
print("Unrecognized command: '%s'" % cmd, file=sys.stderr)

7 changes: 5 additions & 2 deletions src/metfish/commands.py → src/metfish/calc_pr.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

from .utils import get_Pr

def get_Pr_cli(argv=None):
def main(argv=None):

desc = "Calculate an exact P(r) curve for a given structure"
epi = """By default, Dmax is calculated from the max distance found in the structure."""
Expand All @@ -23,7 +23,7 @@ def get_Pr_cli(argv=None):
help="the path to write the file to. by default, write to stdout")
parser.add_argument("-f", "--force", action='store_true', help="overwrite output if it exist", default=False)

args = parser.parse_args()
args = parser.parse_args(argv)

out = sys.stdout
if args.output is not None:
Expand All @@ -40,3 +40,6 @@ def get_Pr_cli(argv=None):
step=args.step)

pd.DataFrame({"r": r, "P(r)": p}).to_csv(out, index=False)

if __name__ == '__main__':
main()