Skip to content

Commit fc9b115

Browse files
committedMay 4, 2020
Bugfix, setup argparse moved under main
1 parent 29d1e63 commit fc9b115

File tree

3 files changed

+108
-101
lines changed

3 files changed

+108
-101
lines changed
 

‎CanSNPer2/CanSNPerTree.py

+92-90
Original file line numberDiff line numberDiff line change
@@ -46,99 +46,101 @@
4646

4747
'''CanSNPer2 settings'''
4848
import argparse
49-
parser = argparse.ArgumentParser(description='CanSNPer2 ')
50-
required_arguments = parser.add_argument_group("Required arguments")
51-
required_arguments.add_argument('query', nargs=argparse.ZERO_OR_MORE, metavar='query', help="File(s) to align (fasta)")
52-
required_arguments.add_argument('-db', '--database', metavar='', help='CanSNP database')
53-
54-
output_options = parser.add_argument_group("Output options")
55-
output_options.add_argument('-o', '--outdir', metavar='DIR', default="results", help="Output directory")
56-
output_options.add_argument('--save_tree', action='store_true', help='Save tree as PDF using ETE3 (default False)')
57-
output_options.add_argument('--no_snpfiles', action='store_false', help="Don´t save output files.")
58-
output_options.add_argument('--summary', action='store_true', help="Output a summary file and tree with all called SNPs\nnot affected by no_snpfiles")
59-
60-
run_options = parser.add_argument_group("Run options")
61-
run_options.add_argument('--refdir', metavar='', default="references/", help="Specify reference directory")
62-
run_options.add_argument('--workdir', metavar='', default="./", help="Change workdir default (./)")
63-
run_options.add_argument('--read_input', action='store_true', help="Select if input is reads not fasta")
64-
run_options.add_argument('--min_required_hits', type=int, default=3, help="Minimum sequential hits to call a SNP!")
65-
run_options.add_argument('--strictness', type=float, default=0.7, help="Percent of snps in path reqired for calling SNP (default 0.7)")
66-
run_options.add_argument('--keep_going', action='store_true', help="If Error occurs, continue with the rest of samples")
67-
run_options.add_argument('--rerun', action='store_true', help="Rerun already processed files (else skip if result file exists)")
68-
69-
'''Remove the two below when script is complete, possibly keep as hidden for debug'''
70-
run_options.add_argument('--skip_mauve' , action='store_true', help="If xmfa files already exists skip step")
71-
run_options.add_argument('--keep_temp', action='store_true', help="keep temporary files")
72-
73-
74-
debugopts = parser.add_argument_group("Logging and debug options")
75-
debugopts.add_argument('--tmpdir', metavar='', default="/tmp/CanSNPer2", help="Specify reference directory")
76-
debugopts.add_argument('--logdir', metavar='', default="logs/", help="Specify log directory")
77-
debugopts.add_argument('--verbose', action='store_const', const=logging.INFO, help="Verbose output")
78-
debugopts.add_argument('--debug', action='store_const', const=logging.DEBUG, help="Debug output")
79-
debugopts.add_argument('--supress', action='store_const', const=logging.ERROR, default=logging.WARNING, help="Supress warnings")
80-
parser.add_argument("--version", action='store_true', help=argparse.SUPPRESS)
81-
82-
args = parser.parse_args()
83-
if len(sys.argv)==1:
84-
parser.print_help()
85-
parser.exit()
86-
'''
87-
Setup logging and debug options
88-
'''
89-
if args.version:
90-
print("CanSNPer2 - version {version}".format(version=__version__))
91-
exit()
92-
93-
logval = args.supress
94-
if args.debug:
95-
logval = args.debug
96-
elif args.verbose:
97-
logval = args.verbose
98-
99-
if args.skip_mauve and not args.keep_temp: ## Make sure keep temp is default if --skip_mauve is used
100-
args.keep_temp = True
101-
102-
'''Make sure log directory path exists'''
103-
logdircreated = False
104-
if not os.path.exists(args.logdir):
105-
logdircreated = True
106-
os.makedirs(args.logdir)
107-
108-
109-
from datetime import date,time
110-
t = time()
111-
today = date.today()
112-
logpath = args.logdir+"CanSNPer2-"+today.strftime("%b-%d-%Y")+"{}.log"
113-
if os.path.exists(logpath):
114-
logpath=logpath.format("-{:%H:%M}".format(t))
115-
else: logpath = logpath.format("")
116-
117-
118-
logging.basicConfig(
119-
#filename=logpath,
120-
level=logval,
121-
format="%(asctime)s %(module)s [%(levelname)-5.5s] %(message)s",
122-
handlers=[
123-
logging.FileHandler(logpath),
124-
logging.StreamHandler()
125-
])
126-
logger = logging.getLogger(__name__)
127-
if logdircreated: logger.info("Creating logs directory {logdir}".format(logdir=args.logdir))
128-
129-
'''Fix temp path''' ## If tmp is in standard root folder create sub folder for CanSNPer2
130-
if args.tmpdir.rstrip("/") == "/tmp": ## Only root tmp name was chosen
131-
logger.debug("Tmp directory was selected as root tmp, add subfolder for CanSNPer2 to path!")
132-
args.tmpdir = args.tmpdir.rstrip()+"CanSNPer2"
133-
if not os.path.exists(args.tmpdir):
134-
logger.debug("Tmp directory {path} did not exist tmp, add subfolder(s) for CanSNPer2 ".format(path=args.tmpdir))
135-
os.makedirs(args.tmpdir,exist_ok=True)
136-
137-
## Add log level and log file path
138-
logger.debug(args)
49+
13950

14051
def main():
14152
'''Initiate CanSNPer2 object'''
53+
parser = argparse.ArgumentParser(description='CanSNPer2 ')
54+
required_arguments = parser.add_argument_group("Required arguments")
55+
required_arguments.add_argument('query', nargs=argparse.ZERO_OR_MORE, metavar='query', help="File(s) to align (fasta)")
56+
required_arguments.add_argument('-db', '--database', metavar='', help='CanSNP database')
57+
58+
output_options = parser.add_argument_group("Output options")
59+
output_options.add_argument('-o', '--outdir', metavar='DIR', default="results", help="Output directory")
60+
output_options.add_argument('--save_tree', action='store_true', help='Save tree as PDF using ETE3 (default False)')
61+
output_options.add_argument('--no_snpfiles', action='store_false', help="Don´t save output files.")
62+
output_options.add_argument('--summary', action='store_true', help="Output a summary file and tree with all called SNPs\nnot affected by no_snpfiles")
63+
64+
run_options = parser.add_argument_group("Run options")
65+
run_options.add_argument('--refdir', metavar='', default="references/", help="Specify reference directory")
66+
run_options.add_argument('--workdir', metavar='', default="./", help="Change workdir default (./)")
67+
run_options.add_argument('--read_input', action='store_true', help="Select if input is reads not fasta")
68+
run_options.add_argument('--min_required_hits', type=int, default=3, help="Minimum sequential hits to call a SNP!")
69+
run_options.add_argument('--strictness', type=float, default=0.7, help="Percent of snps in path reqired for calling SNP (default 0.7)")
70+
run_options.add_argument('--keep_going', action='store_true', help="If Error occurs, continue with the rest of samples")
71+
run_options.add_argument('--rerun', action='store_true', help="Rerun already processed files (else skip if result file exists)")
72+
73+
'''Remove the two below when script is complete, possibly keep as hidden for debug'''
74+
run_options.add_argument('--skip_mauve' , action='store_true', help="If xmfa files already exists skip step")
75+
run_options.add_argument('--keep_temp', action='store_true', help="keep temporary files")
76+
77+
78+
debugopts = parser.add_argument_group("Logging and debug options")
79+
debugopts.add_argument('--tmpdir', metavar='', default="/tmp/CanSNPer2", help="Specify reference directory")
80+
debugopts.add_argument('--logdir', metavar='', default="logs/", help="Specify log directory")
81+
debugopts.add_argument('--verbose', action='store_const', const=logging.INFO, help="Verbose output")
82+
debugopts.add_argument('--debug', action='store_const', const=logging.DEBUG, help="Debug output")
83+
debugopts.add_argument('--supress', action='store_const', const=logging.ERROR, default=logging.WARNING, help="Supress warnings")
84+
parser.add_argument("--version", action='store_true', help=argparse.SUPPRESS)
85+
86+
args = parser.parse_args()
87+
if len(sys.argv)==1:
88+
parser.print_help()
89+
parser.exit()
90+
'''
91+
Setup logging and debug options
92+
'''
93+
if args.version:
94+
print("CanSNPer2 - version {version}".format(version=__version__))
95+
exit()
96+
97+
logval = args.supress
98+
if args.debug:
99+
logval = args.debug
100+
elif args.verbose:
101+
logval = args.verbose
102+
103+
if args.skip_mauve and not args.keep_temp: ## Make sure keep temp is default if --skip_mauve is used
104+
args.keep_temp = True
105+
106+
'''Make sure log directory path exists'''
107+
logdircreated = False
108+
if not os.path.exists(args.logdir):
109+
logdircreated = True
110+
os.makedirs(args.logdir)
111+
112+
113+
from datetime import date,time
114+
t = time()
115+
today = date.today()
116+
logpath = args.logdir+"CanSNPer2-"+today.strftime("%b-%d-%Y")+"{}.log"
117+
if os.path.exists(logpath):
118+
logpath=logpath.format("-{:%H:%M}".format(t))
119+
else: logpath = logpath.format("")
120+
121+
122+
logging.basicConfig(
123+
#filename=logpath,
124+
level=logval,
125+
format="%(asctime)s %(module)s [%(levelname)-5.5s] %(message)s",
126+
handlers=[
127+
logging.FileHandler(logpath),
128+
logging.StreamHandler()
129+
])
130+
logger = logging.getLogger(__name__)
131+
if logdircreated: logger.info("Creating logs directory {logdir}".format(logdir=args.logdir))
132+
133+
'''Fix temp path''' ## If tmp is in standard root folder create sub folder for CanSNPer2
134+
if args.tmpdir.rstrip("/") == "/tmp": ## Only root tmp name was chosen
135+
logger.debug("Tmp directory was selected as root tmp, add subfolder for CanSNPer2 to path!")
136+
args.tmpdir = args.tmpdir.rstrip()+"CanSNPer2"
137+
if not os.path.exists(args.tmpdir):
138+
logger.debug("Tmp directory {path} did not exist tmp, add subfolder(s) for CanSNPer2 ".format(path=args.tmpdir))
139+
os.makedirs(args.tmpdir,exist_ok=True)
140+
141+
## Add log level and log file path
142+
logger.debug(args)
143+
142144
CanSNPer2_obj = CanSNPer2(args.query,
143145
refdir=args.refdir,
144146
verbose=args.verbose,

‎CanSNPer2/modules/NewickTree.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -206,7 +206,7 @@ def CanSNPer_tree_layout(self,node):
206206
'''Layout function for ETE3 trees.'''
207207
# Adds the name face to the image at the top side of the branch
208208
if not node.is_root():
209-
ete3.faces.add_face_to_node(AttrFace("name"), node, column=0, position="branch-top")
209+
ete3.faces.add_face_to_node(ete3.AttrFace("name"), node, column=0, position="branch-top")
210210

211211
def _confirm_path(self,dist_list,called_snps,snplist):
212212
'''Confirm path of snps'''

‎setup.py

+15-10
Original file line numberDiff line numberDiff line change
@@ -6,23 +6,28 @@
66
version=__version__,
77
url="https://git-int.foi.se/bioinfo/cansnper2",
88
description="CanSNPer2: A toolkit for SNP-typing bacterial genomes.",
9-
license="GPLv3'",
109

1110
# Author details
1211
author='David Sundell',
1312
author_email='david.sundell@foi.se',
1413

15-
keywords="Bioinformatics SNP-typing sequence-data",
14+
license=' GNU GENERAL PUBLIC LICENSE version 3',
15+
1616
classifiers=[
17-
'Development Status :: 5 - Beta',
18-
'License :: OSI Approved :: GPL',
19-
'Programming Language :: Python :: 3'
20-
],
17+
'Programming Language :: Python :: 3',
18+
'License :: OSI Approved :: GNU General Public License v3 (GPLv3)',
19+
"Operating System :: OS Independent"
20+
],
21+
python_requires='>=3.6',
22+
23+
keywords="Bioinformatics SNP-typing sequence-data",
24+
2125
install_requires=['ete3','flextaxd'],
2226
packages=find_packages(exclude=['contrib', 'docs', 'test*']),
2327
#py_modules=['CanSNPer2.modules.ParseXMFA',"CanSNPer2.modules.DatabaseConnection","CanSNPer2.modules.CanSNPer2","CanSNPer2.modules.NewickTree"],
24-
entry_points={'console_scripts': [ 'CanSNPer2=CanSNPer2.CanSNPerTree:main',
25-
'CanSNPer2-database=CanSNPer2.SNPDatabase:main',
26-
'CanSNPer2-download=CanSNPer2.DownloadGenomes:main',
28+
entry_points={'console_scripts': [
29+
'CanSNPer2=CanSNPer2.CanSNPerTree:main',
30+
'CanSNPer2-database=CanSNPer2.SNPDatabase:main',
31+
'CanSNPer2-download=CanSNPer2.DownloadGenomes:main',
2732
'CanSNPer2-test=CanSNPer2.selftest:main'
28-
]})
33+
]})

0 commit comments

Comments
 (0)
Please sign in to comment.