|
46 | 46 |
|
47 | 47 | '''CanSNPer2 settings'''
|
48 | 48 | 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 | + |
139 | 50 |
|
140 | 51 | def main():
|
141 | 52 | '''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 | + |
142 | 144 | CanSNPer2_obj = CanSNPer2(args.query,
|
143 | 145 | refdir=args.refdir,
|
144 | 146 | verbose=args.verbose,
|
|
0 commit comments