acmc.main

main.py module

This module provides the functionality for the acmc command line interface

  1"""
  2main.py module
  3
  4This module provides the functionality for the acmc command line interface
  5"""
  6
  7import argparse
  8import logging
  9from pathlib import Path
 10
 11import acmc
 12from acmc import trud, omop, phen, parse, logging_config as lc
 13
 14
 15DEFAULT_WORKSPACE_PATH = Path("./workspace")
 16"""Default phenotype workspace path"""
 17
 18
 19def _trud_install(args: argparse.Namespace):
 20    """Handle the `trud install` command."""
 21    trud.install()
 22
 23
 24def _omop_install(args: argparse.Namespace):
 25    """Handle the `omop install` command."""
 26    omop.install(args.omop_zip_file, args.version)
 27
 28
 29def _omop_clear(args: argparse.Namespace):
 30    """Handle the `omop clear` command."""
 31    omop.clear(omop.DB_PATH)
 32
 33
 34def _omop_delete(args: argparse.Namespace):
 35    """Handle the `omop delete` command."""
 36    omop.delete(omop.DB_PATH)
 37
 38
 39def _phen_init(args: argparse.Namespace):
 40    """Handle the `phen init` command."""
 41    phen.init(args.phen_dir, args.remote_url)
 42
 43
 44def _phen_fork(args: argparse.Namespace):
 45    """Handle the `phen fork` command."""
 46    phen.fork(
 47        args.phen_dir,
 48        args.upstream_url,
 49        args.upstream_version,
 50        new_origin_url=args.remote_url,
 51    )
 52
 53
 54def _phen_validate(args: argparse.Namespace):
 55    """Handle the `phen validate` command."""
 56    phen.validate(args.phen_dir)
 57
 58
 59def _phen_map(args: argparse.Namespace):
 60    """Handle the `phen map` command."""
 61    phen.map(args.phen_dir, args.target_coding)
 62
 63
 64def _phen_export(args: argparse.Namespace):
 65    """Handle the `phen copy` command."""
 66    phen.export(args.phen_dir, args.version)
 67
 68
 69def _phen_publish(args: argparse.Namespace):
 70    """Handle the `phen publish` command."""
 71    phen.publish(args.phen_dir, args.msg, args.remote_url, args.increment)
 72
 73
 74def _phen_copy(args: argparse.Namespace):
 75    """Handle the `phen copy` command."""
 76    phen.copy(args.phen_dir, args.target_dir, args.version)
 77
 78
 79def _phen_diff(args: argparse.Namespace):
 80    """Handle the `phen diff` command."""
 81    phen.diff(args.phen_dir, args.version, args.old_phen_dir, args.old_version)
 82
 83
 84def main():
 85    parser = argparse.ArgumentParser(description="ACMC command-line tool")
 86    parser.add_argument("--debug", action="store_true", help="Enable debug mode")
 87    parser.add_argument(
 88        "--version", action="version", version=f"acmc {acmc.__version__}"
 89    )
 90
 91    # Top-level commands
 92    subparsers = parser.add_subparsers(
 93        dest="command", required=True, help="Available commands"
 94    )
 95
 96    ### TRUD Command ###
 97    trud_parser = subparsers.add_parser("trud", help="TRUD commands")
 98    trud_subparsers = trud_parser.add_subparsers(
 99        dest="subcommand", required=True, help="TRUD subcommands"
100    )
101
102    # trud install
103    trud_install_parser = trud_subparsers.add_parser(
104        "install", help="Install TRUD components"
105    )
106    trud_install_parser.set_defaults(func=_trud_install)
107
108    ### OMOP Command ###
109    omop_parser = subparsers.add_parser("omop", help="OMOP commands")
110    omop_subparsers = omop_parser.add_subparsers(
111        dest="subcommand", required=True, help="OMOP subcommands"
112    )
113
114    # omop install
115    omop_install_parser = omop_subparsers.add_parser(
116        "install", help="Install OMOP codes within database"
117    )
118    omop_install_parser.add_argument(
119        "-f", "--omop-zip-file", required=True, help="Path to downloaded OMOP zip file"
120    )
121    omop_install_parser.add_argument(
122        "-v", "--version", required=True, help="OMOP vocabularies release version"
123    )
124    omop_install_parser.set_defaults(func=_omop_install)
125
126    # omop clear
127    omop_clear_parser = omop_subparsers.add_parser(
128        "clear", help="Clear OMOP data from database"
129    )
130    omop_clear_parser.set_defaults(func=_omop_clear)
131
132    # omop delete
133    omop_delete_parser = omop_subparsers.add_parser(
134        "delete", help="Delete OMOP database"
135    )
136    omop_delete_parser.set_defaults(func=_omop_delete)
137
138    ### PHEN Command ###
139    phen_parser = subparsers.add_parser("phen", help="Phen commands")
140    phen_subparsers = phen_parser.add_subparsers(
141        dest="subcommand", required=True, help="Phen subcommands"
142    )
143
144    # phen init
145    phen_init_parser = phen_subparsers.add_parser(
146        "init", help="Initiatise phenotype directory"
147    )
148    phen_init_parser.add_argument(
149        "-d",
150        "--phen-dir",
151        type=str,
152        default=str(phen.DEFAULT_PHEN_PATH.resolve()),
153        help="(Optional) Local phenotype workspace directory (default is ./workspace/phen).",
154    )
155    phen_init_parser.add_argument(
156        "-r",
157        "--remote_url",
158        help="(Optional) URL to repository where the phenotype will be published.",
159    )
160    phen_init_parser.set_defaults(func=_phen_init)
161
162    # phen fork
163    phen_fork_parser = phen_subparsers.add_parser(
164        "fork", help="Fork an existing phenotype"
165    )
166    phen_fork_parser.add_argument(
167        "-d",
168        "--phen-dir",
169        type=str,
170        default=str(phen.DEFAULT_PHEN_PATH.resolve()),
171        help="(Optional) Local phenotype workspace directory (default is ./workspace/phen).",
172    )
173    phen_fork_parser.add_argument(
174        "-r",
175        "--remote_url",
176        help="(Optional) URL to repository where the forked phenotype will be published.",
177    )
178    phen_fork_parser.add_argument(
179        "-u",
180        "--upstream-url",
181        required=True,
182        help="(Required) URL to the phenotype repository to fork.",
183    )
184    phen_fork_parser.add_argument(
185        "-v",
186        "--upstream-version",
187        required=True,
188        help="(Required) Phenotype version to fork.",
189    )
190    phen_fork_parser.set_defaults(func=_phen_fork)
191
192    # phen validate
193    phen_validate_parser = phen_subparsers.add_parser(
194        "validate", help="Validate phenotype configuration"
195    )
196    phen_validate_parser.add_argument(
197        "-d",
198        "--phen-dir",
199        type=str,
200        default=str(phen.DEFAULT_PHEN_PATH.resolve()),
201        help="(Optional) Local phenotype workspace directory (default is ./workspace/phen).",
202    )
203    phen_validate_parser.set_defaults(func=_phen_validate)
204
205    # phen map
206    phen_map_parser = phen_subparsers.add_parser("map", help="Process phen mapping")
207    phen_map_parser.add_argument(
208        "-d",
209        "--phen-dir",
210        type=str,
211        default=str(phen.DEFAULT_PHEN_PATH.resolve()),
212        help="(Optional) Local phenotype workspace directory (default is ./workspace/phen).",
213    )
214    phen_map_parser.add_argument(
215        "-t",
216        "--target-coding",
217        choices=parse.SUPPORTED_CODE_TYPES,
218        help=f"Specify the target coding {parse.SUPPORTED_CODE_TYPES}",
219    )
220    phen_map_parser.set_defaults(func=_phen_map)
221
222    # phen export
223    phen_export_parser = phen_subparsers.add_parser(
224        "export", help="Export phen to OMOP database"
225    )
226    phen_export_parser.add_argument(
227        "-d",
228        "--phen-dir",
229        type=str,
230        default=str(phen.DEFAULT_PHEN_PATH.resolve()),
231        help="(Optional) Local phenotype workspace directory (default is ./workspace/phen).",
232    )
233    phen_export_parser.add_argument(
234        "-v",
235        "--version",
236        type=str,
237        default="latest",
238        help="Phenotype version to export, defaults to the latest version",
239    )
240    phen_export_parser.set_defaults(func=_phen_export)
241
242    # phen publish
243    phen_publish_parser = phen_subparsers.add_parser(
244        "publish", help="Publish phenotype configuration"
245    )
246    phen_publish_parser.add_argument(
247        "-d",
248        "--phen-dir",
249        type=str,
250        default=str(phen.DEFAULT_PHEN_PATH.resolve()),
251        help="(Optional) Local phenotype workspace directory (default is ./workspace/phen).",
252    )
253    phen_publish_parser.add_argument(
254        "-i",
255        "--increment",
256        type=str,
257        default=phen.DEFAULT_VERSION_INC,
258        choices=phen.SEMANTIC_VERSION_TYPES,
259        help=f"Version increment: {phen.SEMANTIC_VERSION_TYPES}, default is {phen.DEFAULT_VERSION_INC} increment",
260    )
261    phen_publish_parser.add_argument(
262        "-m", "--msg", help="Message to include with the published version"
263    )
264    phen_publish_parser.add_argument(
265        "-r", "--remote_url", help="URL to remote git repository"
266    )
267    phen_publish_parser.set_defaults(func=_phen_publish)
268
269    # phen copy
270    phen_copy_parser = phen_subparsers.add_parser(
271        "copy", help="Publish phenotype configuration"
272    )
273    phen_copy_parser.add_argument(
274        "-d",
275        "--phen-dir",
276        type=str,
277        default=str(phen.DEFAULT_PHEN_PATH.resolve()),
278        help="(Optional) Local phenotype workspace directory (default is ./workspace/phen).",
279    )
280    phen_copy_parser.add_argument(
281        "-td",
282        "--target-dir",
283        type=str,
284        default=str(DEFAULT_WORKSPACE_PATH.resolve()),
285        help="Target directory for the copy",
286    )
287    phen_copy_parser.add_argument(
288        "-v",
289        "--version",
290        type=str,
291        default="latest",
292        help="Phenotype version to copy, defaults to the latest version",
293    )
294    phen_copy_parser.set_defaults(func=_phen_copy)
295
296    # phen diff
297    phen_diff_parser = phen_subparsers.add_parser(
298        "diff", help="Publish phenotype configuration"
299    )
300    phen_diff_parser.add_argument(
301        "-d",
302        "--phen-dir",
303        type=str,
304        default=str(phen.DEFAULT_PHEN_PATH.resolve()),
305        help="(Optional) Local phenotype workspace directory (default is ./workspace/phen).",
306    )
307    phen_diff_parser.add_argument(
308        "-v",
309        "--version",
310        default="latest",
311        help="Phenotype version to compare with an old version, defaults to the HEAD of the workspace directory",
312    )
313    phen_diff_parser.add_argument(
314        "-od",
315        "--old-phen-dir",
316        type=str,
317        default=str(phen.DEFAULT_PHEN_PATH.resolve()),
318        help="Directory for the old phenotype version, defaults to workspace directory",
319    )
320    phen_diff_parser.add_argument(
321        "-ov",
322        "--old-version",
323        required=True,
324        help="Old phenotype version to compare with the changed version",
325    )
326    phen_diff_parser.set_defaults(func=_phen_diff)
327
328    # Parse arguments
329    args = parser.parse_args()
330
331    # setup logging
332    if args.debug:
333        lc.set_log_level(logging.DEBUG)
334
335    # Call the function associated with the command
336    args.func(args)
337
338
339if __name__ == "__main__":
340    main()
DEFAULT_WORKSPACE_PATH = PosixPath('workspace')

Default phenotype workspace path

def main():
 85def main():
 86    parser = argparse.ArgumentParser(description="ACMC command-line tool")
 87    parser.add_argument("--debug", action="store_true", help="Enable debug mode")
 88    parser.add_argument(
 89        "--version", action="version", version=f"acmc {acmc.__version__}"
 90    )
 91
 92    # Top-level commands
 93    subparsers = parser.add_subparsers(
 94        dest="command", required=True, help="Available commands"
 95    )
 96
 97    ### TRUD Command ###
 98    trud_parser = subparsers.add_parser("trud", help="TRUD commands")
 99    trud_subparsers = trud_parser.add_subparsers(
100        dest="subcommand", required=True, help="TRUD subcommands"
101    )
102
103    # trud install
104    trud_install_parser = trud_subparsers.add_parser(
105        "install", help="Install TRUD components"
106    )
107    trud_install_parser.set_defaults(func=_trud_install)
108
109    ### OMOP Command ###
110    omop_parser = subparsers.add_parser("omop", help="OMOP commands")
111    omop_subparsers = omop_parser.add_subparsers(
112        dest="subcommand", required=True, help="OMOP subcommands"
113    )
114
115    # omop install
116    omop_install_parser = omop_subparsers.add_parser(
117        "install", help="Install OMOP codes within database"
118    )
119    omop_install_parser.add_argument(
120        "-f", "--omop-zip-file", required=True, help="Path to downloaded OMOP zip file"
121    )
122    omop_install_parser.add_argument(
123        "-v", "--version", required=True, help="OMOP vocabularies release version"
124    )
125    omop_install_parser.set_defaults(func=_omop_install)
126
127    # omop clear
128    omop_clear_parser = omop_subparsers.add_parser(
129        "clear", help="Clear OMOP data from database"
130    )
131    omop_clear_parser.set_defaults(func=_omop_clear)
132
133    # omop delete
134    omop_delete_parser = omop_subparsers.add_parser(
135        "delete", help="Delete OMOP database"
136    )
137    omop_delete_parser.set_defaults(func=_omop_delete)
138
139    ### PHEN Command ###
140    phen_parser = subparsers.add_parser("phen", help="Phen commands")
141    phen_subparsers = phen_parser.add_subparsers(
142        dest="subcommand", required=True, help="Phen subcommands"
143    )
144
145    # phen init
146    phen_init_parser = phen_subparsers.add_parser(
147        "init", help="Initiatise phenotype directory"
148    )
149    phen_init_parser.add_argument(
150        "-d",
151        "--phen-dir",
152        type=str,
153        default=str(phen.DEFAULT_PHEN_PATH.resolve()),
154        help="(Optional) Local phenotype workspace directory (default is ./workspace/phen).",
155    )
156    phen_init_parser.add_argument(
157        "-r",
158        "--remote_url",
159        help="(Optional) URL to repository where the phenotype will be published.",
160    )
161    phen_init_parser.set_defaults(func=_phen_init)
162
163    # phen fork
164    phen_fork_parser = phen_subparsers.add_parser(
165        "fork", help="Fork an existing phenotype"
166    )
167    phen_fork_parser.add_argument(
168        "-d",
169        "--phen-dir",
170        type=str,
171        default=str(phen.DEFAULT_PHEN_PATH.resolve()),
172        help="(Optional) Local phenotype workspace directory (default is ./workspace/phen).",
173    )
174    phen_fork_parser.add_argument(
175        "-r",
176        "--remote_url",
177        help="(Optional) URL to repository where the forked phenotype will be published.",
178    )
179    phen_fork_parser.add_argument(
180        "-u",
181        "--upstream-url",
182        required=True,
183        help="(Required) URL to the phenotype repository to fork.",
184    )
185    phen_fork_parser.add_argument(
186        "-v",
187        "--upstream-version",
188        required=True,
189        help="(Required) Phenotype version to fork.",
190    )
191    phen_fork_parser.set_defaults(func=_phen_fork)
192
193    # phen validate
194    phen_validate_parser = phen_subparsers.add_parser(
195        "validate", help="Validate phenotype configuration"
196    )
197    phen_validate_parser.add_argument(
198        "-d",
199        "--phen-dir",
200        type=str,
201        default=str(phen.DEFAULT_PHEN_PATH.resolve()),
202        help="(Optional) Local phenotype workspace directory (default is ./workspace/phen).",
203    )
204    phen_validate_parser.set_defaults(func=_phen_validate)
205
206    # phen map
207    phen_map_parser = phen_subparsers.add_parser("map", help="Process phen mapping")
208    phen_map_parser.add_argument(
209        "-d",
210        "--phen-dir",
211        type=str,
212        default=str(phen.DEFAULT_PHEN_PATH.resolve()),
213        help="(Optional) Local phenotype workspace directory (default is ./workspace/phen).",
214    )
215    phen_map_parser.add_argument(
216        "-t",
217        "--target-coding",
218        choices=parse.SUPPORTED_CODE_TYPES,
219        help=f"Specify the target coding {parse.SUPPORTED_CODE_TYPES}",
220    )
221    phen_map_parser.set_defaults(func=_phen_map)
222
223    # phen export
224    phen_export_parser = phen_subparsers.add_parser(
225        "export", help="Export phen to OMOP database"
226    )
227    phen_export_parser.add_argument(
228        "-d",
229        "--phen-dir",
230        type=str,
231        default=str(phen.DEFAULT_PHEN_PATH.resolve()),
232        help="(Optional) Local phenotype workspace directory (default is ./workspace/phen).",
233    )
234    phen_export_parser.add_argument(
235        "-v",
236        "--version",
237        type=str,
238        default="latest",
239        help="Phenotype version to export, defaults to the latest version",
240    )
241    phen_export_parser.set_defaults(func=_phen_export)
242
243    # phen publish
244    phen_publish_parser = phen_subparsers.add_parser(
245        "publish", help="Publish phenotype configuration"
246    )
247    phen_publish_parser.add_argument(
248        "-d",
249        "--phen-dir",
250        type=str,
251        default=str(phen.DEFAULT_PHEN_PATH.resolve()),
252        help="(Optional) Local phenotype workspace directory (default is ./workspace/phen).",
253    )
254    phen_publish_parser.add_argument(
255        "-i",
256        "--increment",
257        type=str,
258        default=phen.DEFAULT_VERSION_INC,
259        choices=phen.SEMANTIC_VERSION_TYPES,
260        help=f"Version increment: {phen.SEMANTIC_VERSION_TYPES}, default is {phen.DEFAULT_VERSION_INC} increment",
261    )
262    phen_publish_parser.add_argument(
263        "-m", "--msg", help="Message to include with the published version"
264    )
265    phen_publish_parser.add_argument(
266        "-r", "--remote_url", help="URL to remote git repository"
267    )
268    phen_publish_parser.set_defaults(func=_phen_publish)
269
270    # phen copy
271    phen_copy_parser = phen_subparsers.add_parser(
272        "copy", help="Publish phenotype configuration"
273    )
274    phen_copy_parser.add_argument(
275        "-d",
276        "--phen-dir",
277        type=str,
278        default=str(phen.DEFAULT_PHEN_PATH.resolve()),
279        help="(Optional) Local phenotype workspace directory (default is ./workspace/phen).",
280    )
281    phen_copy_parser.add_argument(
282        "-td",
283        "--target-dir",
284        type=str,
285        default=str(DEFAULT_WORKSPACE_PATH.resolve()),
286        help="Target directory for the copy",
287    )
288    phen_copy_parser.add_argument(
289        "-v",
290        "--version",
291        type=str,
292        default="latest",
293        help="Phenotype version to copy, defaults to the latest version",
294    )
295    phen_copy_parser.set_defaults(func=_phen_copy)
296
297    # phen diff
298    phen_diff_parser = phen_subparsers.add_parser(
299        "diff", help="Publish phenotype configuration"
300    )
301    phen_diff_parser.add_argument(
302        "-d",
303        "--phen-dir",
304        type=str,
305        default=str(phen.DEFAULT_PHEN_PATH.resolve()),
306        help="(Optional) Local phenotype workspace directory (default is ./workspace/phen).",
307    )
308    phen_diff_parser.add_argument(
309        "-v",
310        "--version",
311        default="latest",
312        help="Phenotype version to compare with an old version, defaults to the HEAD of the workspace directory",
313    )
314    phen_diff_parser.add_argument(
315        "-od",
316        "--old-phen-dir",
317        type=str,
318        default=str(phen.DEFAULT_PHEN_PATH.resolve()),
319        help="Directory for the old phenotype version, defaults to workspace directory",
320    )
321    phen_diff_parser.add_argument(
322        "-ov",
323        "--old-version",
324        required=True,
325        help="Old phenotype version to compare with the changed version",
326    )
327    phen_diff_parser.set_defaults(func=_phen_diff)
328
329    # Parse arguments
330    args = parser.parse_args()
331
332    # setup logging
333    if args.debug:
334        lc.set_log_level(logging.DEBUG)
335
336    # Call the function associated with the command
337    args.func(args)