33import json
44import logging
55import os
6+ import re
67from collections import defaultdict
78
89from jinja2 import Environment , FileSystemLoader , select_autoescape
@@ -29,10 +30,6 @@ def parse_args():
2930 "after chrom, start, and end."
3031 ),
3132 )
32-
33- # TODO calculate from bed
34- p .add_argument ("roc" , help = "indexcov .roc output" )
35-
3633 p .add_argument (
3734 "-e" ,
3835 "--exclude" ,
@@ -92,6 +89,14 @@ def parse_args():
9289 p .add_argument (
9390 "-o" , "--output" , default = "covviz_report.html" , help = "output file path"
9491 )
92+ p .add_argument (
93+ "--skip-norm" ,
94+ action = "store_true" ,
95+ help = (
96+ "skip normalization by global sample median if the depths "
97+ "in your .bed are already normalized"
98+ ),
99+ )
95100 p .add_argument (
96101 "--min-samples" ,
97102 default = 8 ,
@@ -118,42 +123,6 @@ def parse_args():
118123 return p .parse_args ()
119124
120125
121- # TODO calculate this based on the bed
122- def parse_roc (path , traces , samples ):
123- chroms = list (traces .keys ())
124- chroms .pop (chroms .index ("chromosomes" ))
125-
126- traces ["roc" ] = dict ()
127- data = defaultdict (lambda : defaultdict (list ))
128-
129- with gzopen (path ) as fh :
130- reader = csv .DictReader (fh , delimiter = "\t " )
131- for row in reader :
132- # header row is repeated at chromosome breaks
133- if row ["#chrom" ] == "#chrom" :
134- continue
135- chr = row ["#chrom" ].strip ("chr" )
136- if chr not in traces .keys ():
137- continue
138- data [chr ]["x" ].append (row ["cov" ])
139- for sample in samples :
140- data [chr ][sample ].append (row [sample ])
141-
142- for chr in chroms :
143- traces ["roc" ][chr ] = list ()
144- for sample in samples :
145- trace = dict (
146- x = data [chr ]["x" ],
147- y = data [chr ][sample ][1 :],
148- hoverinfo = "text" ,
149- mode = "lines" ,
150- text = sample ,
151- marker = {"color" : "rgba(108,117,125,0.2)" },
152- )
153- traces ["roc" ][chr ].append (trace )
154- return traces
155-
156-
157126def compress_data (data ):
158127 json_str = json .dumps (data ).encode ("utf-8" , "ignore" ).decode ("utf-8" )
159128 json_str = json_str .replace ("NaN" , "null" )
@@ -168,12 +137,13 @@ def cli():
168137 autoescape = select_autoescape (["html" ]),
169138 )
170139
171- exclude = args .exclude .replace ("~" , "" ).replace ("," , "|" )
172-
173140 logger .info ("parsing bed file (%s)" % args .bed )
141+
142+ exclude = re .compile (args .exclude .replace ("~" , "" ).replace ("," , "|" ))
143+
174144 traces , samples = parse_bed (
175145 args .bed ,
176- args . exclude ,
146+ exclude ,
177147 args .ped ,
178148 args .sample_col ,
179149 args .sex_col ,
@@ -182,19 +152,18 @@ def cli():
182152 args .distance_threshold ,
183153 args .slop ,
184154 args .min_samples ,
155+ args .skip_norm ,
185156 )
186157
187158 logger .info ("parsing gff file (%s)" % args .gff )
188- traces = parse_gff (args .gff , traces )
189-
190- logger .info ("parsing roc file (%s)" % args .roc )
191- traces = parse_roc (args .roc , traces , samples )
159+ traces = parse_gff (args .gff , traces , exclude )
192160
193161 if args .ped :
194162 logger .info ("parsing ped file (%s)" % args .ped )
195163 traces = parse_ped (args .ped , traces , args .sample_col , args .sex_chroms )
196164
197165 with open (args .output , "w" ) as fh :
166+ logger .info ("preparing output" )
198167 compressed_json_data = compress_data (traces )
199168 html_template = env .get_template ("covviz.html" )
200169 print (html_template .render (data = compressed_json_data ), file = fh )
0 commit comments