Debian Codegraph @

pycoQC/__main__.py

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