Debian Codegraph @

pycoQC/__main__.py

raw symbols ◀ ▶ SCIP
1 Debian patch touch this file
1 #!/usr/bin/env python3
2 # -*- coding: utf-8 -*-
3
4 #~~~~~~~~~~~~~~IMPORTS~~~~~~~~~~~~~~#
5 # Standard library imports
6 import argparse
7 import sys
8 import textwrap
9
10 # Third party imports
11 from jinja2 import Environment, PackageLoader, Template
12
13 # Local imports
14 from pycoQC.pycoQC import pycoQC
15 from pycoQC.Fast5_to_seq_summary import Fast5_to_seq_summary
16 from pycoQC.Barcode_split import Barcode_split
17 from pycoQC.common import get_logger
18 from pycoQC import __version__ as package_version
19 from pycoQC import __name__ as package_name
20
21
22 #~~~~~~~~~~~~~~pycoQC CLI ENTRY POINT~~~~~~~~~~~~~~#
23 def main_pycoQC (args=None):
24 if args is None:
25 args = sys.argv[1:]
26
27 # Define parser object
28 parser = argparse.ArgumentParser(
29 formatter_class = argparse.RawDescriptionHelpFormatter,
30 description = textwrap.dedent("""
31 pycoQC computes metrics and generates interactive QC plots from the sequencing summary
32 report generated by Oxford Nanopore technologies basecallers\n
33 * Minimal usage
34 pycoQC -f sequencing_summary.txt -o pycoQC_output.html
35 * Including Guppy barcoding file + html output + json output
36 pycoQC -f sequencing_summary.txt -b barcoding_sequencing.txt -o pycoQC_output.html -j pycoQC_output.json
37 * Including Bam file + html output
38 pycoQC -f sequencing_summary.txt -a alignment.bam -o pycoQC_output.html"""))
39 parser.add_argument('--version', action='version', version="{} v{}".format(package_name, package_version))
40
41 # Define arguments
42 parser_io = parser.add_argument_group('Input/output options')
43 parser_io.add_argument("--summary_file", "-f", default=[], nargs='*',
44 help=textwrap.dedent("""Path to a sequencing_summary generated by Albacore 1.0.0 + (read_fast5_basecaller.py) / Guppy 2.1.3+ (guppy_basecaller).
45 One can also pass multiple space separated file paths or a UNIX style regex matching multiple files (Required)"""))
46 parser_io.add_argument("--barcode_file", "-b", default=[], nargs='*',
47 help=textwrap.dedent("""Path to the barcode_file generated by Guppy 2.1.3+ (guppy_barcoder) or Deepbinner 0.2.0+. This is not a required file.
48 One can also pass multiple space separated file paths or a UNIX style regex matching multiple files (optional)"""))
49 parser_io.add_argument("--bam_file", "-a", default=[], nargs='*',
50 help=textwrap.dedent("""Path to a Bam file corresponding to reads in the summary_file. Preferably aligned with Minimap2
51 One can also pass multiple space separated file paths or a UNIX style regex matching multiple files (optional)"""))
52 parser_io.add_argument("--html_outfile", "-o", default="", type=str,
53 help="Path to an output html file report (required if json_outfile not given)")
54 parser_io.add_argument("--json_outfile", "-j", default="", type=str,
55 help="Path to an output json file report (required if html_outfile not given)")
56 parser_filt = parser.add_argument_group('Filtering options')
57 parser_filt.add_argument("--min_pass_qual", default=7, type=float,
58 help="Minimum quality to consider a read as 'pass' (default: %(default)s)")
59 parser_filt.add_argument("--min_pass_len", default=0, type=int,
60 help="Minimum read length to consider a read as 'pass' (default: %(default)s)")
61 parser_filt.add_argument("--filter_calibration", default=False, action='store_true',
62 help="If given, reads flagged as calibration strand by the basecaller are removed (default: %(default)s)")
63 parser_filt.add_argument("--filter_duplicated", default=False, action='store_true',
64 help=textwrap.dedent("""If given, duplicated read_ids are removed but the first occurence is kept
65 (Guppy sometimes outputs the same read multiple times) (default: %(default)s)"""))
66 parser_filt.add_argument("--min_barcode_percent", default=0.1, type=float,
67 help="Minimal percent of total reads to retain barcode label. If below, the barcode value is set as `unclassified` (default: %(default)s)")
68 parser_html = parser.add_argument_group('HTML report options')
69 parser_html.add_argument("--report_title", default="PycoQC report", type=str,
70 help="Title to use in the html report (default: %(default)s)")
71 parser_html.add_argument("--template_file", type=str, default="",
72 help="Jinja2 html template for the html report (default: %(default)s)")
73 parser_html.add_argument("--config_file", type=str, default="",
74 help=textwrap.dedent("""Path to a JSON configuration file for the html report.
75 If not provided, looks for it in ~/.pycoQC and ~/.config/pycoQC/config. If it's still not found, falls back to default parameters.
76 The first level keys are the names of the plots to be included.
77 The second level keys are the parameters to pass to each plotting function (default: %(default)s)")"""))
78 parser_html.add_argument("--skip_coverage_plot", default=False, action='store_true',
79 help="Skip the coverage plot in HTML report. Useful when using a reference file containing many sequences, i.e. transcriptome (default: %(default)s)")
80 parser_other = parser.add_argument_group('Other options')
81 parser_other.add_argument("--sample", default=100000, type=int,
82 help=textwrap.dedent("""If not None a n number of reads will be randomly selected instead of the entire dataset for ploting function
83 (deterministic sampling) (default: %(default)s)"""))
84 parser_other.add_argument("--default_config", "-d", action='store_true',
85 help="Print default configuration file. Can be used to generate a template JSON file (default: %(default)s)")
86 parser_verbosity = parser.add_mutually_exclusive_group()
87 parser_verbosity.add_argument("-v", "--verbose", action="store_true", default=False, help="Increase verbosity")
88 parser_verbosity.add_argument("-q", "--quiet", action="store_true", default=False, help="Reduce verbosity")
89
90 # Try to parse arguments
91 args = parser.parse_args()
92
93 # Set logging level
94 logger = get_logger (name=__name__, verbose=args.verbose, quiet=args.quiet)
95
96 # Print the default config parameters and exit
97 if args.default_config:
•98 config_file = "/usr/lib/python3/dist-packages/pycoQC/templates/pycoQC_config.json"
99 with open (config_file) as fp:
100 sys.stdout.write(fp.read())
101 sys.exit()
102
103 elif not args.summary_file:
104 logger.warning ("ERROR: `--summary_file` is a required argument")
105 parser.print_help()
106 sys.exit()
107
108 elif not args.html_outfile and not args.json_outfile:
109 logger.warning ("ERROR: At least one output file required `--html_outfile` or `--json_outfile`")
110 parser.print_help()
111 sys.exit()
112
113 # Run pycoQC
114 pycoQC (
115 summary_file = args.summary_file,
116 barcode_file = args.barcode_file,
117 bam_file = args.bam_file,
118 filter_calibration = args.filter_calibration,
119 filter_duplicated = args.filter_duplicated,
120 min_barcode_percent = args.min_barcode_percent,
121 min_pass_qual = args.min_pass_qual,
122 min_pass_len = args.min_pass_len,
123 sample = args.sample,
124 html_outfile = args.html_outfile,
125 report_title = args.report_title,
126 config_file = args.config_file,
127 skip_coverage_plot = args.skip_coverage_plot,
128 template_file = args.template_file,
129 json_outfile = args.json_outfile,
130 verbose = args.verbose,
131 quiet = args.quiet)
132
133 #~~~~~~~~~~~~~~Fast5_to_seq_summary CLI ENTRY POINT~~~~~~~~~~~~~~#
134 def main_Fast5_to_seq_summary (args=None):
135 if args is None:
136 args = sys.argv[1:]
137
138 # Define parser object
139 parser = argparse.ArgumentParser(
140 description ="Fast5_to_seq_summary generate a sequencing summary like file from a directory containing Fast5 files")
141 parser.add_argument('--version', '-v', action='version', version="{} v{}".format(package_name, package_version))
142 # Define arguments
143 parser.add_argument("--fast5_dir", "-f", required=True, type=str,
144 help="""Directory containing fast5 files. Can contain multiple subdirectories""")
145 parser.add_argument("--seq_summary_fn", "-s", required=True, type=str,
146 help="""path of the summary sequencing file where to write the data extracted from the fast5 files""")
147 parser.add_argument("--max_fast5", type=int, default=0,
148 help="Maximum number of file to try to parse. 0 to deactivate (default: %(default)s)")
149 parser.add_argument("--threads", "-t", type=int, default=4,
150 help="Total number of threads to use. 1 thread is used for the reader and 1 for the writer. Minimum 3 (default: %(default)s)")
151 parser.add_argument("--basecall_id", type=int, default=0,
152 help=textwrap.dedent("""id of the basecalling group. By default leave to 0, but if you perfome multiple basecalling on the same fast5 files,
153 this can be used to indicate the corresponding group (1, 2 ...) (default: %(default)s)"""))
154 parser.add_argument("--fields", type=str, nargs="+",
155 default=["read_id", "run_id", "channel", "start_time", "sequence_length_template",
156 "mean_qscore_template", "calibration_strand_genome_template", "barcode_arrangement"],
157 help="list of field names corresponding to attributes to try to fetch from the fast5 files (default: %(default)s)")
158 parser.add_argument("--include_path", action='store_true', default=False,
159 help="If given, the absolute path to the corresponding file is added in an extra column (default: %(default)s)")
160 parser.add_argument("--verbose_level", type=int, default=0,
161 help="Level of verbosity, from 2 (Chatty) to 0 (Nothing) (default: %(default)s)")
162
163 # Try to parse arguments
164 args = parser.parse_args()
165
166 # Run main function
167 Fast5_to_seq_summary (
168 fast5_dir = args.fast5_dir,
169 seq_summary_fn = args.seq_summary_fn,
170 max_fast5 = args.max_fast5,
171 threads = args.threads,
172 basecall_id = args.basecall_id,
173 fields = args.fields,
174 include_path = args.include_path,
175 verbose_level = args.verbose_level)
176
177 #~~~~~~~~~~~~~~Barcode_split CLI ENTRY POINT~~~~~~~~~~~~~~#
178 def main_Barcode_split (args=None):
179 if args is None:
180 args = sys.argv[1:]
181
182 # Define parser object
183 parser = argparse.ArgumentParser(
184 description ="Barcode_split is a simple tool to split sequencing summary report in per barcodes")
185 parser.add_argument('--version', action='version', version="{} v{}".format(package_name, package_version))
186 # Define arguments
187 parser.add_argument("--summary_file", "-f", required=True, nargs='*',
188 help=textwrap.dedent("""Path to a sequencing_summary generated by Albacore 1.0.0 + (read_fast5_basecaller.py) / Guppy 2.1.3+ (guppy_basecaller).
189 One can also pass multiple space separated file paths or a UNIX style regex matching multiple files"""))
190 parser.add_argument("--barcode_file", "-b", default=[], nargs='*',
191 help=textwrap.dedent("""Path to the barcode_file generated by Guppy 2.1.3+ (guppy_barcoder) or Deepbinner 0.2.0+.
192 One can also pass multiple space separated file paths or a UNIX style regex matching multiple files"""))
193 parser.add_argument("--output_dir", "-o", type=str, default="",
194 help="Folder where to output split barcode data (default: current dir")
195 parser.add_argument("--output_unclassified", "-u", action='store_true', default=False,
196 help="If given, unclassified barcodes are also written in a file. By default they are skiped")
197 parser.add_argument("--min_barcode_percent", "-p", default=0.1, type=float,
198 help="Minimal percent of total reads to retain barcode label. If below, the barcode value is set as `unclassified` (default: %(default)s)")
199 parser_verbosity = parser.add_mutually_exclusive_group()
200 parser_verbosity.add_argument("-v", "--verbose", action="store_true", default=False, help="Increase verbosity")
201 parser_verbosity.add_argument("-q", "--quiet", action="store_true", default=False, help="Reduce verbosity")
202
203 # Try to parse arguments
204 args = parser.parse_args()
205
206 # Run main function
207 Barcode_split (
208 summary_file=args.summary_file,
209 barcode_file=args.barcode_file,
210 output_dir=args.output_dir,
211 output_unclassified=args.output_unclassified,
212 min_barcode_percent=args.min_barcode_percent,
213 verbose=args.verbose,
214 quiet=args.quiet)