33
44"""Generates quality metrics from the output of MetaTOR.
55
6- General utilities functions to extract high quality contigs which will be used
7- to estimate the quality of the metaHiC librairies nad generates some plots to
6+ General utilities functions to extract high quality contigs which will be used
7+ to estimate the quality of the metaHiC librairies nad generates some plots to
88illustrate it.
99
1010Core functions to assess the quality:
2626from os .path import join
2727
2828
29- def extract_hq_contigs (bin_summary , contigs_data ):
29+ def extract_hq_contigs (bin_summary , contigs_data , completeness_threshold = 0.7 , redundancy_threshold = 1.15 ):
3030 """Function to extract the high quality contigs from the metator output.
3131 These contigs will be the one used to assess the quality of the dataset.
3232
@@ -47,8 +47,8 @@ def extract_hq_contigs(bin_summary, contigs_data):
4747 # Extract high quality MAGs.
4848 hq_mags = bin_summary .index [
4949 np .logical_and (
50- (bin_summary ["Weighted completeness" ] > 0.7 ),
51- (bin_summary ["Weighted redundancy" ] < 1.15 ),
50+ (bin_summary ["Weighted completeness" ] > completeness_threshold ),
51+ (bin_summary ["Weighted redundancy" ] < redundancy_threshold ),
5252 )
5353 ]
5454 n_mags = len (hq_mags )
@@ -83,13 +83,12 @@ def extract_pairs(pairs_files, out_file, contigs, contigs_data):
8383 with open (out_file , "w" ) as output_pairs :
8484 # Write the header of the output pairs
8585 output_pairs .write ("## pairs format v1.0\n " )
86- output_pairs .write (
87- "#columns: readID chr1 pos1 chr2 pos2 strand1 strand2\n "
88- )
86+ output_pairs .write ("#columns: readID chr1 pos1 chr2 pos2 strand1 strand2\n " )
8987 for contig in contigs :
9088 output_pairs .write (
9189 "#chromsize: {0} {1}\n " .format (
92- contig , contigs_data .loc [contig , "Size" ],
90+ contig ,
91+ contigs_data .loc [contig , "Size" ],
9392 )
9493 )
9594 for pairs_file in pairs_files :
@@ -166,9 +165,7 @@ def hic_quality(
166165 restrict_table = {}
167166 for record in SeqIO .parse (mio .read_compressed (fasta ), "fasta" ):
168167 # Get chromosome restriction table
169- restrict_table [record .id ] = hcd .get_restriction_table (
170- record .seq , enzyme , circular = False
171- )
168+ restrict_table [record .id ] = hcd .get_restriction_table (record .seq , enzyme , circular = False )
172169
173170 # Add fragment index to pairs (readID, chr1, pos1, chr2,
174171 # pos2, strand1, strand2, frag1, frag2)
@@ -187,11 +184,7 @@ def hic_quality(
187184 fig_path = plot_event ,
188185 prefix = prefix ,
189186 )
190- logger .info (
191- "Filtering with thresholds: uncuts={0} loops={1}" .format (
192- uncut_thr , loop_thr
193- )
194- )
187+ logger .info ("Filtering with thresholds: uncuts={0} loops={1}" .format (uncut_thr , loop_thr ))
195188 # Filter reads and save metrics on informative reads
196189 n_religated = 0
197190 n_loops = 0
@@ -230,11 +223,7 @@ def hic_quality(
230223 else :
231224 n_inter_mags += 1
232225 if n_intra_mags + n_inter_mags > 0 :
233- rat_info = (
234- 100
235- * (n_informative_intra + n_informative_inter )
236- / (n_intra_mags + n_inter_mags )
237- )
226+ rat_info = 100 * (n_informative_intra + n_informative_inter ) / (n_intra_mags + n_inter_mags )
238227 noise_ratio = 100 * n_inter_mags / (n_inter_mags + n_intra_mags )
239228 else :
240229 logger .warning ("No pairs have benn extracted. All scores set to 0." )
@@ -251,9 +240,7 @@ def hic_quality(
251240 logger .info (f"Loop ratio: { 100 * n_loops / n_intra_mags :.2f} %." )
252241 logger .info (f"Weirds ratio: { 100 * n_weirds / n_intra_mags :.2f} %." )
253242 logger .info (f"Informative contacts estimation: { rat_info :.2f} %." )
254- logger .info (
255- f"Ratio inter/intra contigs: { n_informative_inter / (n_informative_intra + n_informative_inter ):.2f} %."
256- )
243+ logger .info (f"Ratio inter/intra contigs: { n_informative_inter / (n_informative_intra + n_informative_inter ):.2f} %." )
257244 logger .info (f"Noise contact ratio: { noise_ratio :.2f} %" )
258245 logger .info (f"Noise score: { noise_score :.2E} " )
259246
@@ -293,6 +280,8 @@ def quality_check(
293280 plot ,
294281 enzyme ,
295282 threshold ,
283+ completeness_threshold = 0.7 ,
284+ redundancy_threshold = 1.15 ,
296285):
297286 """Main function to compute the quality of the metaHiC library and to
298287 display some metrics about it.
@@ -327,11 +316,11 @@ def quality_check(
327316 pairs_idx = join (tmp_dir , f"{ prefix } _idx.pairs" )
328317
329318 # Extract high quality contigs.
330- hq_contigs , n_mags = extract_hq_contigs (bin_summary , contigs_data )
331- # Extract pairs
332- n_pairs = extract_pairs (
333- pairs_files , pairs , list (hq_contigs .keys ()), contigs_data
319+ hq_contigs , n_mags = extract_hq_contigs (
320+ bin_summary , contigs_data , completeness_threshold = completeness_threshold , redundancy_threshold = redundancy_threshold
334321 )
322+ # Extract pairs
323+ n_pairs = extract_pairs (pairs_files , pairs , list (hq_contigs .keys ()), contigs_data )
335324
336325 # Estimate HiC quality.
337326 if n_pairs > 0 :
0 commit comments