Debian Codegraph @

pycoQC/pycoQC.py

raw symbols SCIP
1 # -*- coding: utf-8 -*-
2
3 #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~IMPORTS~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~#
4
5 # Standard library imports
6 from collections import *
7 import warnings
8 import datetime
9
10 # Local lib import
11 from pycoQC.common import *
12 from pycoQC.pycoQC_parse import pycoQC_parse
13 from pycoQC.pycoQC_plot import pycoQC_plot
14 from pycoQC.pycoQC_report import pycoQC_report
15 from pycoQC import __name__ as package_name
16 from pycoQC import __version__ as package_version
17
18 #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~GLOBAL SETTINGS~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~#
19
20 # Silence futurewarnings
21 warnings.filterwarnings("ignore", category=FutureWarning)
22
23 #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~MAIN CLASS~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~#
24 def pycoQC (
25 summary_file:str,
26 barcode_file:str="",
27 bam_file:str="",
28 runid_list:list=[],
29 filter_calibration:bool=False,
30 filter_duplicated:bool=False,
31 min_barcode_percent:float=0.1,
32 min_pass_qual:float=7,
33 min_pass_len:int=0,
34 sample:int=100000,
35 html_outfile:str="",
36 report_title:str="PycoQC report",
37 config_file:str="",
38 template_file:str="",
39 json_outfile:str="",
40 skip_coverage_plot:bool=False,
41 verbose:bool=False,
42 quiet:bool=False):
43 """
44 Parse Albacore sequencing_summary.txt file and clean-up the data
45 * summary_file
46 Path to a sequencing_summary generated by Albacore 1.0.0 + (read_fast5_basecaller.py) / Guppy 2.1.3+ (guppy_basecaller).
47 One can also pass multiple space separated file paths or a UNIX style regex matching multiple files
48 * barcode_file
49 Path to the barcode_file generated by Guppy 2.1.3+ (guppy_barcoder) or Deepbinner 0.2.0+. This is not a required file.
50 One can also pass multiple space separated file paths or a UNIX style regex matching multiple files
51 * bam_file
52 Path to a Bam file corresponding to reads in the summary_file. Preferably aligned with Minimap2
53 One can also pass multiple space separated file paths or a UNIX style regex matching multiple files
54 * runid_list
55 Select only specific runids to be analysed. Can also be used to force pycoQC to order the runids for
56 temporal plots, if the sequencing_summary file contain several sucessive runs. By default pycoQC analyses
57 all the runids in the file and uses the runid order as defined in the file.
58 * filter_calibration
59 If True read flagged as calibration strand by the software are removed
60 * filter_duplicated
61 If True duplicated read_ids are removed but the first occurence is kept (Guppy sometimes outputs the same read multiple times)
62 * min_barcode_percent
63 Minimal percent of total reads to retain barcode label. If below the barcode value is set as `unclassified`.
64 * min_pass_qual
65 Minimum quality to consider a read as 'pass'
66 * min_pass_len
67 Minimum read length to consider a read as 'pass'
68 * sample
69 If not None a n number of reads will be randomly selected instead of the entire dataset for ploting function (deterministic sampling)
70 * html_outfile
71 Path to an output html file report
72 * report_title
73 Title to use in the html report
74 * config_file
75 Path to a JSON configuration file for the html report.
76 If not provided, 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
79 * template_file
80 Jinja2 html template for the html report
81 * json_outfile
82 Path to an output json file report
83 * verbose
84 Increase verbosity
85 * quiet
86 Reduce verbosity
87 """
88
89 # Save args and init options in dict for report
90 options_d = locals()
91 info_d = {"package_name":package_name, "package_version":package_version, "timestamp":str(datetime.datetime.now())}
92
93 # Set logging level
94 logger = get_logger (name=__name__, verbose=verbose, quiet=quiet)
95 logger.warning ("Checking arguments values")
96
97 # Save all verified values + type
98 runid_list = check_arg("runid_list", runid_list, required_type=list, allow_none=True)
99 filter_calibration = check_arg("filter_calibration", filter_calibration, required_type=bool, allow_none=False)
100 filter_duplicated = check_arg("filter_duplicated", filter_duplicated, required_type=bool, allow_none=False)
101 min_barcode_percent = check_arg("min_barcode_percent", min_barcode_percent, required_type=float, min=0, max=100, allow_none=False)
102 min_pass_qual = check_arg("min_pass_qual", min_pass_qual, required_type=float, min=0, max=60, allow_none=False)
103 min_pass_len = check_arg("min_pass_len", min_pass_len, required_type=int, min=0, allow_none=False)
104 sample = check_arg("sample", sample, required_type=int, min=0, allow_none=True)
105 html_outfile = check_arg("html_outfile", html_outfile, required_type=str, allow_none=True)
106 html_outfile = check_arg("html_outfile", html_outfile, required_type=str, allow_none=True)
107 report_title = check_arg("report_title", report_title, required_type=str, allow_none=True)
108 config_file = check_arg("config_file", config_file, required_type=str, allow_none=True)
109 template_file = check_arg("template_file", template_file, required_type=str, allow_none=True)
110 json_outfile = check_arg("json_outfile", json_outfile, required_type=str, allow_none=True)
111 skip_coverage_plot = check_arg("skip_coverage_plot", skip_coverage_plot, required_type=bool, allow_none=False)
112
113 # Print debug info
114 logger.debug("General info")
115 logger.debug(dict_to_str(info_d))
116 logger.debug("Runtime options")
117 logger.debug(dict_to_str(options_d))
118
119 #~~~~~~~~~~pycoQC_parse~~~~~~~~~~#
120 parser = pycoQC_parse (
121 summary_file=summary_file,
122 barcode_file=barcode_file,
123 bam_file=bam_file,
124 runid_list=runid_list,
125 filter_calibration=filter_calibration,
126 filter_duplicated=filter_duplicated,
127 min_barcode_percent=min_barcode_percent,
128 verbose=verbose,
129 quiet=quiet)
130
131 logger.debug("Parser stats")
132 logger.debug(parser)
133
134 #~~~~~~~~~~pycoQC_plot~~~~~~~~~~#
135 plotter = pycoQC_plot(
136 parser=parser,
137 min_pass_qual=min_pass_qual,
138 min_pass_len=min_pass_len,
139 sample=sample,
140 verbose=verbose,
141 quiet=quiet)
142
143 logger.debug("Plotter stats")
144 logger.debug(plotter)
145
146 #~~~~~~~~~~pycoQC_report~~~~~~~~~~#
147 if html_outfile or json_outfile:
148 reporter = pycoQC_report (
149 parser=parser,
150 plotter=plotter,
151 verbose=verbose,
152 quiet=quiet)
153
154 if html_outfile:
155 reporter.html_report(
156 outfile=html_outfile,
157 config_file=config_file,
158 template_file=template_file,
159 report_title=report_title,
160 skip_coverage_plot=skip_coverage_plot)
161
162 # Run json output function
163 if json_outfile:
164 reporter.json_report(
165 outfile=json_outfile)
166
167 #~~~~~~~~~~return plotting object for API~~~~~~~~~~#
168 return plotter