/
bioprov.py
159 lines (137 loc) 路 4.64 KB
/
bioprov.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
__author__ = "Vini Salazar"
__license__ = "MIT"
__maintainer__ = "Vini Salazar"
__url__ = "https://github.com/vinisalazar/bioprov"
__version__ = "0.1.19"
"""
BioProv command-line application. This module holds the main executable.
"""
import argparse
import sys
from pathlib import Path
import bioprov.src.config as bp_config_module
from bioprov.src.config import config
from bioprov.utils import parser_help, dict_to_string
from bioprov.workflows import (
WorkflowOptionsParser,
genome_annotation,
blastn_alignment,
KaijuWorkflow,
)
def main(args=None):
"""
Main function to run the BioProv command-line application.
Calls the subparsers defined in the Workflows module.
:param args: args to call the function with. Usually is None and
args are automatically extracted from the command line.
Passing args is used for testing only.
"""
bioprov_parser = argparse.ArgumentParser(
description="BioProv command-line application. Choose a command to begin.\n"
)
# TODO: Make this into a subparser
commands = bioprov_parser.add_mutually_exclusive_group()
commands.add_argument(
"--show_config", help="Show location of config file.", action="store_true"
)
commands.add_argument(
"--show_provstore",
help="Show location of ProvStore credentials file.",
action="store_true",
)
commands.add_argument(
"--create_provstore",
help="Create ProvStore file credentials file.",
action="store_true",
)
commands.add_argument(
"--show_db", help="Show location of database file.", action="store_true"
)
commands.add_argument(
"--clear_db", help="Clears all records in database.", action="store_true"
)
commands.add_argument(
"-v", "--version", help="Show BioProv version", action="store_true"
)
commands.add_argument(
"-l",
"--list",
help="List Projects in the BioProv database.",
action="store_true",
)
workflows = bioprov_parser.add_subparsers(title="workflows", dest="subparser_name")
_ = workflows.add_parser("genome_annotation")
_ = workflows.add_parser("blastn")
_ = workflows.add_parser("kaiju")
workflows.choices["genome_annotation"] = genome_annotation().parser
workflows.choices["blastn"] = blastn_alignment().parser
workflows.choices["kaiju"] = KaijuWorkflow.parser()
# Refactoring to subparsers
if args is None:
if len(sys.argv) == 1 or sys.argv[1] == "-h" or sys.argv[1] == "--help":
parser_help(bioprov_parser)
else:
args = bioprov_parser.parse_args() # no cover
for command, value in args.__dict__.items():
if value:
try:
getattr(CommandOptionsParser, command)()
except AttributeError:
pass
parser = WorkflowOptionsParser() # no cover
try:
parser.parse_options(args) # no cover
except KeyError:
raise
class CommandOptionsParser:
def __init__(self):
pass
@classmethod
def show_config(cls):
print(
"This is the location of the config module.\n"
"Edit it to alter your BioProv settings.\n\n",
f"'{bp_config_module.__file__}'\n",
)
print("These are your configuration settings:")
print(dict_to_string(config.serializer()))
sys.exit(0)
@classmethod
def show_provstore(cls):
print(
"This is the location of your ProvStore credentials file.\n"
"Edit it to alter your BioProv settings.\n\n",
f"{config.provstore_file}\n",
)
if not Path(config.provstore_file).is_file():
print("It does not exist, but you can create one with the command:")
print(" $ bioprov --create_provstore")
sys.exit(0)
@classmethod
def create_provstore(cls): # no cover
config.create_provstore_file()
sys.exit(0)
@classmethod
def show_db(cls):
print(
"This is the location of your BioProv database file.\n"
f"'{config.db_path}'\n",
)
sys.exit(0)
@classmethod
def version(cls):
print(f"Local BioProv version is v{__version__}")
sys.exit(0)
@classmethod
def list(cls):
tags = [f"'{project['tag']}'" for project in config.db.all()]
print(f"You have {len(tags)} Projects in your database.")
if tags:
print("\n".join(tags))
sys.exit(0)
@classmethod
def clear_db(cls): # no cover
config.db.clear_db()
sys.exit(0)
if __name__ == "__main__":
main() # no cover