def make_key_genes_cov_report(experiment_by_key):
        info('Making key genes coverage report...')

        ms = [
            Metric('Gene'),
            Metric('Chr', with_heatmap=False, max_width=20, align='right')
        ]

        for i, (k, e) in enumerate(experiment_by_key.items()):
            ms.extend([
                Metric(k + ' Ave depth',
                       short_name=k + '\nave depth',
                       med=e.ave_depth,
                       class_='shifted_column' if i == 0 else ''),
                Metric(k + ' % cov at {}x'.format(e.depth_cutoff),
                       short_name='% at {}x'.format(e.depth_cutoff),
                       unit='%',
                       med=1,
                       low_inner_fence=0.5,
                       low_outer_fence=0.1),
                Metric(k + ' CNV', short_name='  CNV')
            ]  # short name is hack for IE9 who doesn't have "text-align: left" and tries to stick "CNV" to the previous col header
                      )
        clinical_cov_metric_storage = MetricStorage(
            sections=[ReportSection(metrics=ms)])
        key_genes_report = PerRegionSampleReport(
            sample=experiment_by_key.values()[0].sample,
            metric_storage=clinical_cov_metric_storage)

        # Writing records
        hits_by_gene_by_experiment = OrderedDefaultDict(OrderedDict)
        for k, e in experiment_by_key.items():
            for gene in e.key_gene_by_name.values():
                hits_by_gene_by_experiment[gene.name][e] = gene

        for gname, hit_by_experiment in sorted(
                hits_by_gene_by_experiment.items(), key=lambda
            (gname, h): gname):
            gene = next(
                (m for m in hit_by_experiment.values() if m is not None), None)

            row = key_genes_report.add_row()
            row.add_record('Gene', gene.name)
            row.add_record('Chr', gene.chrom.replace('chr', ''))

            for e, hit in hit_by_experiment.items():
                row.add_record(e.key + ' Ave depth', hit.ave_depth)
                m = clinical_cov_metric_storage.find_metric(
                    e.key + ' % cov at {}x'.format(e.depth_cutoff))
                row.add_record(
                    m.name,
                    next((cov for cutoff, cov in hit.cov_by_threshs.items()
                          if cutoff == e.depth_cutoff), None))
                if hit.seq2c_event and (hit.seq2c_event.is_amp()
                                        or hit.seq2c_event.is_del()):
                    row.add_record(
                        e.key + ' CNV', hit.seq2c_event.amp_del + ', ' +
                        hit.seq2c_event.fragment)

        return key_genes_report
Exemplo n.º 2
0
def _prep_comb_report(metric_storage, samples, shared_general_metrics,
                      shared_metrics):
    comb_general_metrics = shared_general_metrics[:]
    comb_general_metrics.append(Metric('For each sample'))
    for s in samples:
        comb_general_metrics.append(Metric(s.name + ' ave depth'))

    comb_metrics = shared_metrics[:]
    for s in samples:
        comb_metrics.append(
            DepthsMetric(s.name + ' hotspots depths/norm depths',
                         short_name=s.name))

    comb_report_metric_storage = MetricStorage(
        general_section=ReportSection('general_section',
                                      metrics=comb_general_metrics),
        sections=[ReportSection(metrics=comb_metrics)])

    report = PerRegionSampleReport(sample='Combined',
                                   metric_storage=comb_report_metric_storage)

    report.add_record(
        'Sample', 'contains values from all samples: ' +
        ', '.join([s.name for s in samples]))
    report.add_record('For each sample',
                      'Depths and normalized depths for each hotspot.')

    m = metric_storage.find_metric('Average sample depth')
    for s in samples:
        val = BaseReport.find_record(s.report.records, m.name).value
        report.add_record(s.name + ' ave depth', val)

    return report
Exemplo n.º 3
0
def make_expression_heatmap(bcbio_structure, gene_counts):
    samples_names = [sample.name for sample in bcbio_structure.samples]
    metrics = [Metric('Gene')] + [
        Metric(sample_name, max_width=40) for sample_name in samples_names
    ]
    metric_storage = MetricStorage(
        sections=[ReportSection(metrics=metrics, name='samples')])
    report = PerRegionSampleReport(metric_storage=metric_storage,
                                   expandable=True,
                                   unique=True,
                                   heatmap_by_rows=True,
                                   keep_order=True,
                                   large_table=True,
                                   vertical_sample_names=True)
    printed_genes = set()

    # Writing records
    for gene in sorted(gene_counts.keys()):
        first_record = gene_counts[gene][0]
        if first_record.is_hidden_row:
            printed_genes.add(first_record.gene_name)
            row = report.add_row()
            row.add_record('Gene', first_record.gene_name)
            for sample in samples_names:
                row.add_record(
                    sample,
                    sum([
                        record.counts[sample] for record in gene_counts[gene]
                    ]))
            row.class_ = ' expandable_gene_row collapsed'
        for record in gene_counts[gene]:
            gene_expression = record.counts
            row = report.add_row()
            if record.is_hidden_row:
                row_class = ' row_to_hide row_hidden'
            else:
                row_class = ' expandable_gene_row collapsed'
            row.add_record('Gene', record.name)
            for sample, count in gene_expression.iteritems():
                if sample in samples_names:
                    row.add_record(sample, count)
            row.class_ = row_class
    return report
Exemplo n.º 4
0
def _prep_best_report(metric_storage, samples):
    report = PerRegionSampleReport(sample='Best',
                                   metric_storage=metric_storage)

    report.add_record(
        'Sample', 'contains best values from all samples: ' +
        ', '.join([s.name for s in samples]))

    m = metric_storage.find_metric('Average sample depth')
    ave_sample_depth = max(
        BaseReport.find_record(s.report.records, m.name).value
        for s in samples)
    report.add_record('Average sample depth', ave_sample_depth)

    return report
Exemplo n.º 5
0
def _report_normalize_coverage_for_variant_sites(cnf, sample, ave_sample_depth,
                                                 vcf_key, bed_fpath,
                                                 selected_regions,
                                                 depth_cutoff, region_type,
                                                 coverage_type):
    info()
    info('Normalized coverage for ' + vcf_key + ' hotspots (' + region_type +
         ')')
    vcf_fpath = cnf.genome.get(vcf_key)
    if not vcf_fpath:
        err('Error: no ' + vcf_key + ' for ' + cnf.genome.name +
            ' VCF fpath specified in ' + cnf.sys_cnf)
        return None

    clipped_gz_vcf_fpath, regions_in_order, vars_by_region, var_by_site = \
        _read_vcf_records_per_bed_region_and_clip_vcf(cnf, vcf_fpath, bed_fpath, region_type, sample)
    if not clipped_gz_vcf_fpath:
        return None

    depth_by_var = _get_depth_for_each_variant(cnf, var_by_site,
                                               clipped_gz_vcf_fpath, bed_fpath,
                                               sample.bam)

    info()
    info('Saving report for ' + coverage_type + ' covered ' + region_type +
         ' for sample ' + sample.name)
    report = PerRegionSampleReport(sample=sample,
                                   metric_storage=single_report_metric_storage)
    report.add_record('Sample', sample.name)
    report.add_record('Average sample depth', ave_sample_depth)

    total_variants = 0
    total_variants_below_cutoff = 0
    total_regions = 0
    for r in regions_in_order:
        total_regions += 1

        (chrom, id_, start, end, gene, strand, feature, biotype) = r
        for region in selected_regions:
            if region.start == int(start) and region.end == int(end):
                strand = region.strand
                feature = region.feature
                biotype = region.biotype
                break

        rep_region = report.add_row()
        rep_region.add_record('Gene', gene)
        rep_region.add_record('Chr', chrom)
        rep_region.add_record('Start', start)
        rep_region.add_record('End', end)
        rep_region.add_record('Strand', strand)
        rep_region.add_record('Feature', feature)
        rep_region.add_record('Biotype', biotype)
        rep_region.add_record('ID', id_)
        variants, depths = [], []
        for var in sorted(vars_by_region[r].values(), key=lambda v: v.pos):
            total_variants += 1

            depth = depth_by_var.get((var.get_site()))
            if depth >= depth_cutoff:
                continue

            total_variants_below_cutoff += 1
            variants.append(var)

            norm_depth = depth / ave_sample_depth if depth and ave_sample_depth > 0 else None
            depths.append((depth, norm_depth))

            total_variants += 1
            if total_variants % 10000 == 0:
                info('Processed {0:,} variants, {0:,} regions.'.format(
                    total_variants, total_regions))

        rep_region.add_record('Hotspots num', len(variants))
        rep_region.add_record('Hotspots list', variants)
        rep_region.add_record('Hotspots depths/norm depths', depths)

    best_report_basename = coverage_type + '_cov_' + region_type + '.' + vcf_key
    report.save_txt(
        join(sample.flagged_regions_dirpath, best_report_basename + '.txt'))
    report.save_tsv(
        join(sample.flagged_regions_dirpath, best_report_basename + '.tsv'))
    info('')
    info(
        vcf_key +
        ' variants coverage report (total: {0:,} variants, {1:,} variants below cut-off {2}, {3:,} regions) '
        'saved into:'.format(total_variants, total_variants_below_cutoff,
                             depth_cutoff, total_regions))
    info('  ' + report.txt_fpath)

    return report
Exemplo n.º 6
0
def generate_flagged_regions_report(cnf, output_dir, sample, ave_depth,
                                    gene_by_key):
    depth_threshs = cnf.coverage_reports.depth_thresholds
    report = PerRegionSampleReport(
        sample=sample,
        metric_storage=get_detailed_metric_storage(depth_threshs))
    report.add_record('Sample', sample.name)
    safe_mkdir(sample.flagged_regions_dirpath)
    ''' 1. Detect depth threshold (ave sample coverage * DEPTH_THRESH_FROM_AVE_COV)
        2. Select regions covered in less than MIN_DEPTH_PERCENT_AT_THRESH at threshold
        3. Sort by % at threshold
        4. Select those parts of those regions where % = 0, save to BED
        5. Find HotSpots at those regions
        6. Intersect HotSpots with tracks

        For each gene where are regions with parts % = 0:
            sort them by part where % = 0
    '''
    #vcf_dbs = ['oncomine', 'dbsnp', 'cosmic']
    vcf_dbs = ['oncomine']

    from source._deprecated_clinical_reporting.clinical_parser import get_key_or_target_bed_genes
    key_genes, _ = get_key_or_target_bed_genes(
        cnf.bed, verify_file(adjust_system_path(cnf.key_genes), 'key genes'))
    depth_cutoff = get_depth_cutoff(ave_depth, depth_threshs)
    genes_sorted = sorted(gene_by_key.values())
    min_cov, max_cov = min_and_max_based_on_outliers(genes_sorted)

    for coverage_type in ['low', 'high']:
        info('Selecting and saving ' + coverage_type + ' covered genes')
        selected_genes = []

        if coverage_type == 'low':
            selected_genes = [
                g for g in genes_sorted if g.gene_name in key_genes and (any(
                    e.rates_within_threshs[depth_cutoff] <
                    MIN_DEPTH_PERCENT_AT_THRESH for e in g.get_exons()) or any(
                        a.rates_within_threshs[depth_cutoff] <
                        MIN_DEPTH_PERCENT_AT_THRESH
                        for a in g.get_amplicons()))
            ]
        else:
            if max_cov:
                selected_genes = [
                    g for g in genes_sorted
                    if g.gene_name in key_genes and (any(
                        e.avg_depth > max_cov for e in g.get_exons()) or any(
                            a.avg_depth > max_cov for a in g.get_amplicons()))
                ]
        for region_type in ['exons', 'target']:
            selected_regions = []
            for gene in selected_genes:
                if coverage_type == 'low':
                    cur_regions = [
                        a for a in (gene.get_amplicons() if region_type ==
                                    'target' else gene.get_exons())
                        if a.rates_within_threshs[depth_cutoff] <
                        MIN_DEPTH_PERCENT_AT_THRESH
                        and 'Multi' not in a.feature
                    ]
                else:
                    cur_regions = [
                        a for a in (gene.get_amplicons() if region_type ==
                                    'target' else gene.get_exons())
                        if a.avg_depth > max_cov and 'Multi' not in a.feature
                    ]
                selected_regions.extend(cur_regions)

            if selected_regions:
                selected_regions_bed_fpath = join(
                    sample.flagged_regions_dirpath,
                    coverage_type + '_cov_' + region_type + '.bed')
                save_regions_to_bed(cnf, selected_regions,
                                    selected_regions_bed_fpath)

                # Report cov for Hotspots
                for db in vcf_dbs:
                    res = _report_normalize_coverage_for_variant_sites(
                        cnf, sample, ave_depth, db, selected_regions_bed_fpath,
                        selected_regions, depth_cutoff, region_type,
                        coverage_type)
                    if not res:
                        return None

            report = make_flat_region_report(sample, selected_regions,
                                             depth_threshs)
            flagged_txt_fpath = add_suffix(
                add_suffix(sample.flagged_txt, region_type), coverage_type)
            flagged_tsv_fpath = add_suffix(
                add_suffix(sample.flagged_tsv, region_type), coverage_type)
            report.save_txt(flagged_txt_fpath)
            report.save_tsv(flagged_tsv_fpath)

            info('')
            info(coverage_type + ' covered ' + region_type + '(total ' +
                 str(len(selected_regions)) + ') for sample ' + sample.name +
                 ' saved into:')
            info('  ' + flagged_txt_fpath + ', ' + flagged_tsv_fpath)

    return report
Exemplo n.º 7
0
def _save_best_details_for_each_gene(depth_threshs, samples, output_dir):
    metric_storage = get_detailed_metric_storage(depth_threshs)

    report = PerRegionSampleReport(sample='Best',
                                   metric_storage=metric_storage)
    report.add_record(
        'Sample', 'contains best values from all samples: ' +
        ', '.join([s.name for s in samples]))

    total_regions = 0
    fpaths = [
        s.targetcov_detailed_tsv for s in samples
        if verify_file(s.targetcov_detailed_tsv)
    ]
    if not fpaths:
        err('No targetcov detailed per-gene report was generated; skipping.')
        return None

    open_tsv_files = [open(fpath) for fpath in fpaths]

    first_col = 0
    while True:
        lines_for_each_sample = [next(f, None) for f in open_tsv_files]
        if not all(lines_for_each_sample):
            break
        l = lines_for_each_sample[0]
        if l.startswith('##'):
            continue
        elif l.startswith('#'):
            if l.startswith('#Sample'):
                first_col = 1
            break

    while True:
        lines_for_each_sample = [next(f, None) for f in open_tsv_files]
        if not all(lines_for_each_sample):
            break

        if all([
                not l.startswith('#')
                and ('Whole-Gene' in l or 'Gene-Exon' in l)
                for l in lines_for_each_sample
        ]):
            shared_fields = lines_for_each_sample[0].split(
                '\t')[first_col:first_col + 9]
            reg = report.add_row()
            reg.add_record('Chr', get_val(shared_fields[0]))
            reg.add_record('Start', get_int_val(shared_fields[1]))
            reg.add_record('End', get_int_val(shared_fields[2]))
            reg.add_record('Size', get_int_val(shared_fields[3]))
            reg.add_record('Gene', get_val(shared_fields[4]))
            reg.add_record('Strand', get_val(shared_fields[5]))
            reg.add_record('Feature', get_val(shared_fields[6]))
            reg.add_record('Biotype', get_val(shared_fields[7]))
            reg.add_record('Transcript', get_val(shared_fields[8]))

            min_depths, ave_depths, stddevs, withins = ([], [], [], [])
            percents_by_threshs = {t: [] for t in depth_threshs}

            for l in lines_for_each_sample:
                fs = l.split('\t')

                min_depths.append(get_int_val(fs[first_col + 9]))
                ave_depths.append(get_float_val(fs[first_col + 10]))
                stddevs.append(get_float_val(fs[first_col + 11]))
                withins.append(get_float_val(fs[first_col + 12]))
                for t, f in zip(depth_threshs, fs[first_col + 13:]):
                    percents_by_threshs[t].append(get_float_val(f))

            # counting bests
            reg.add_record('Min depth', select_best(min_depths))
            reg.add_record('Ave depth', select_best(ave_depths))
            reg.add_record('Std dev', select_best(stddevs, max))
            reg.add_record('W/n 20% of ave depth', select_best(withins))
            for t in depth_threshs:
                reg.add_record('{}x'.format(t),
                               select_best(percents_by_threshs[t]))

            total_regions += 1

    for f in open_tsv_files:
        f.close()

    gene_report_basename = 'Best.' + source.targetseq_name + source.detail_gene_report_baseending
    txt_rep_fpath = report.save_txt(
        join(output_dir, gene_report_basename + '.txt'))
    tsv_rep_fpath = report.save_tsv(
        join(output_dir, gene_report_basename + '.tsv'))
    info('')
    info('Best values for the regions (total ' + str(total_regions) +
         ') saved into:')
    info('  ' + txt_rep_fpath)

    return txt_rep_fpath
Exemplo n.º 8
0
def _generate_summary_flagged_regions_report(cnf, bcbio_structure, samples,
                                             mutations, key_or_target_genes):
    region_types = ['exons', 'target']
    coverage_types = ['low', 'high']
    flagged_regions_metrics = [
        Metric('Gene', min_width=50, max_width=70),
        Metric('Chr', with_heatmap=False, max_width=20, align='right'),
        Metric('Position', td_class='td_position', min_width=70,
               max_width=120),
        Metric('Ave depth',
               td_class='long_expanded_line right_aligned',
               max_width=100,
               with_heatmap=False),
        Metric('#HS', quality='Less is better', align='right', max_width=30),
        Metric('Hotspots & Deleterious',
               td_class='long_expanded_line',
               min_width=100,
               max_width=150),
        Metric('Found mutations',
               td_class='long_expanded_line',
               min_width=150,
               max_width=200),
        Metric('Samples',
               td_class='long_expanded_line',
               min_width=100,
               max_width=120),
        Metric('Possible reasons',
               td_class='long_expanded_line',
               max_width=120)
    ]
    flagged_regions_metric_storage = MetricStorage(
        sections=[ReportSection(metrics=flagged_regions_metrics)])
    flagged_regions_report_dirpath = bcbio_structure.flagged_regions_dirpath
    safe_mkdir(flagged_regions_report_dirpath)
    if key_or_target_genes == 'target':
        genes_description = 'genes'
    else:
        genes_description = 'genes that have been previously implicated in various cancers'
    for region_type in region_types:
        regions_dict = {}
        total_regions = 0
        info()
        info('Preparing report for ' + region_type)
        for coverage_type in coverage_types:
            regions_by_gene = {}
            for sample in samples:
                selected_regions_bed_fpath = join(
                    sample.flagged_regions_dirpath,
                    coverage_type + '_cov_' + region_type + '.bed')
                regions_by_reasons = {}
                if verify_file(selected_regions_bed_fpath, is_critical=False):
                    intersection_fpath = _intersect_with_tricky_regions(
                        cnf, selected_regions_bed_fpath, sample.name)
                    regions_by_reasons = _parse_intersection_with_tricky_regions(
                        cnf, intersection_fpath)
                total_report_fpath = add_suffix(
                    add_suffix(sample.flagged_tsv, region_type), coverage_type)
                if verify_file(total_report_fpath, is_critical=False):
                    with open(total_report_fpath) as f:
                        for l in f:
                            l = l.strip()
                            if not l or l.startswith('#'):
                                continue
                            fs = l.split('\t')
                            (chrom, start, end, size, gene, strand, feature,
                             biotype, min_depth, avg_depth) = fs[:10]
                            start, end = int(start), int(end)
                            regions_by_gene.setdefault(gene, [])
                            cur_region = Region(sample_name=[sample.name],
                                                avg_depth=[avg_depth],
                                                gene_name=gene,
                                                strand=strand,
                                                feature=feature,
                                                biotype=biotype,
                                                chrom=chrom,
                                                start=start,
                                                end=end)
                            for r in regions_by_reasons:
                                if r[0] <= start and end <= r[1]:
                                    cur_region.extra_fields = regions_by_reasons[
                                        r]
                            cur_region.missed_by_db = []
                            was_added = False
                            for r in regions_by_gene[gene]:
                                if r.start <= cur_region.start <= r.end and r.start <= cur_region.end <= r.end:
                                    was_added = True
                                    if sample.name not in r.sample_name:
                                        r.sample_name.append(sample.name)
                                        r.avg_depth.append(avg_depth)
                            if not was_added:
                                regions_by_gene[gene].append(cur_region)
                report_fpath = join(
                    sample.flagged_regions_dirpath,
                    coverage_type + '_cov_' + region_type + '.oncomine.tsv')
                if verify_file(report_fpath, is_critical=False):
                    with open(report_fpath) as f:
                        for l in f:
                            l = l.strip()
                            if not l or l.startswith('#'):
                                continue
                            fs = l.split('\t')
                            hotspots = []
                            (gene, chrom, start, end, strand, feature, biotype,
                             id_, num_hotspots) = fs[:9]
                            start, end = int(start), int(end)
                            if int(num_hotspots) != 0:
                                hotspots = fs[9].split()

                            regions_by_gene.setdefault(gene, [])
                            cur_region = Region(sample_name=[sample.name],
                                                gene_name=gene,
                                                strand=strand,
                                                feature=feature,
                                                biotype=biotype,
                                                chrom=chrom,
                                                start=start,
                                                end=end)
                            for r in regions_by_gene[gene]:
                                if r.start <= cur_region.start <= r.end and r.start <= cur_region.end <= r.end:
                                    if sample.name not in r.sample_name:
                                        r.sample_name.append(sample.name)
                                        r.avg_depth.append('.')
                                    new_hotspots = [
                                        hs for hs in hotspots
                                        if hs not in r.missed_by_db
                                    ]
                                    r.missed_by_db.extend(new_hotspots)
            flagged_regions_report = PerRegionSampleReport(
                name='Flagged regions',
                metric_storage=flagged_regions_metric_storage)
            num_regions = 0
            non_hs_class = ' no_hotspots'
            slash_with_zero_space = '/&#x200b;'
            for gene in regions_by_gene.keys():
                if regions_by_gene[gene]:
                    num_regions += len(regions_by_gene[gene])
                    row_class = ' expandable_row collapsed'
                    if len(regions_by_gene[gene]) > 1:
                        reg = flagged_regions_report.add_row()
                        reg.class_ = ' expandable_gene_row collapsed'
                        chr = regions_by_gene[gene][0].chrom
                        num_hotspots = [
                            len(r.missed_by_db) for r in regions_by_gene[gene]
                        ]
                        all_samples = [
                            sample for r in regions_by_gene[gene]
                            for sample in r.sample_name
                        ]
                        all_unique_samples = []
                        all_unique_samples = [
                            sample for sample in all_samples
                            if sample not in all_unique_samples
                            and not all_unique_samples.append(sample)
                        ]
                        all_tricky_regions = sorted(
                            set([
                                tricky_region for r in regions_by_gene[gene]
                                for tricky_region in r.extra_fields
                            ]))
                        all_depths = [[]
                                      for x in range(len(all_unique_samples))]
                        for r in regions_by_gene[gene]:
                            for sample_num, sample in enumerate(
                                    all_unique_samples):
                                if sample in r.sample_name:
                                    cur_sample_index = r.sample_name.index(
                                        sample)
                                    if r.avg_depth[cur_sample_index] != '.':
                                        all_depths[sample_num].append(
                                            float(
                                                r.avg_depth[cur_sample_index]))
                        avg_depth_per_samples = [
                            sum(all_depths[i]) /
                            len(all_depths[i]) if len(all_depths[i]) > 0 else 0
                            for i in range(len(all_depths))
                        ]
                        reg.add_record('Gene', gene)
                        reg.add_record('Chr', chr.replace('chr', ''))
                        reg.add_record('#HS', sum(num_hotspots))
                        reg.add_record(
                            'Position',
                            str(len(regions_by_gene[gene])) + ' regions')
                        reg.add_record(
                            'Ave depth',
                            slash_with_zero_space.join([
                                format(depth, '.2f') if depth != '.' else '.'
                                for depth in avg_depth_per_samples
                            ]),
                            num=sum(avg_depth_per_samples) /
                            len(avg_depth_per_samples))
                        reg.add_record('Hotspots & Deleterious', '')
                        reg.add_record('Possible reasons',
                                       ', '.join(all_tricky_regions))
                        reg.add_record('Samples',
                                       ',\n'.join(all_unique_samples))
                        reg.add_record('Found mutations', '')
                        if sum(num_hotspots) == 0:
                            reg.class_ += non_hs_class
                        row_class += ' row_to_hide row_hidden'
                    else:
                        row_class += ' not_to_hide'
                    for r in regions_by_gene[gene]:
                        reg = flagged_regions_report.add_row()
                        reg.class_ = row_class
                        reg.add_record('Gene', r.gene_name)
                        reg.add_record('Chr', r.chrom.replace('chr', ''))
                        avg_depths = [
                            float(depth) for depth in r.avg_depth
                            if depth != '.'
                        ]
                        reg.add_record(
                            'Ave depth',
                            slash_with_zero_space.join([
                                format(depth, '.2f') if depth != '.' else depth
                                for depth in avg_depths
                            ]),
                            num=sum(avg_depths) / len(avg_depths))
                        reg.add_record(
                            'Position',
                            Metric.format_value(
                                r.start, human_readable=True, is_html=True) +
                            '-' + Metric.format_value(
                                r.end, human_readable=True, is_html=True))
                        reg.add_record('#HS', len(r.missed_by_db))
                        if len(r.missed_by_db) == 0:
                            reg.class_ += non_hs_class
                        uniq_hs_positions = sorted(
                            set([
                                hotspot.split(':')[0]
                                for hotspot in r.missed_by_db
                            ]))
                        hs_by_pos = {
                            pos: [
                                h.split(':')[1] for h in r.missed_by_db
                                if h.split(':')[0] == pos
                            ]
                            for pos in uniq_hs_positions
                        }
                        hs_breakable = [
                            gray(
                                Metric.format_value(int(pos.replace(',', '')),
                                                    human_readable=True,
                                                    is_html=True)) + ': ' +
                            ','.join([
                                h.replace('/', slash_with_zero_space)
                                for h in hs_by_pos[pos]
                            ]) for pos in uniq_hs_positions
                        ]
                        reg.add_record('Hotspots & Deleterious',
                                       '\n'.join(hs_breakable))
                        reg.add_record('Possible reasons',
                                       ', '.join(r.extra_fields))
                        reg.add_record('Samples', ',\n'.join(r.sample_name))
                        found_mutations = []
                        for sample in samples:
                            if sample.name in r.sample_name:
                                for mut in mutations[sample.name]:
                                    if mut.gene.name == r.gene_name and r.start <= mut.pos <= r.end:
                                        found_mutations.append(
                                            gray(
                                                Metric.format_value(
                                                    mut.pos,
                                                    human_readable=True,
                                                    is_html=True)) + ':' +
                                            mut.ref + '>' + mut.alt + ' (' +
                                            sample.name + ')')
                        reg.add_record('Found mutations',
                                       '\n'.join(found_mutations))
            flagged_regions_report.expandable = True
            flagged_regions_report.unique = True
            regions_dict[coverage_type] = create_section(
                flagged_regions_report, num_regions, regions_by_gene.keys(),
                region_type)
            total_regions += num_regions
        flagged_report_fpath = join(flagged_regions_report_dirpath,
                                    'flagged_' + region_type + '.html')
        write_static_html_report(cnf, {
            'key_or_target': key_or_target_genes,
            'region_type': region_type,
            'genes_description': genes_description,
            'flagged_low': regions_dict['low'],
            'flagged_high': regions_dict['high'],
        },
                                 flagged_report_fpath,
                                 tmpl_fpath=join(
                                     dirname(abspath(__file__)),
                                     'template_flagged_regions.html'),
                                 extra_js_fpaths=[
                                     join(dirname(abspath(__file__)), 'static',
                                          'flagged_regions.js')
                                 ],
                                 extra_css_fpaths=[
                                     join(dirname(abspath(__file__)), 'static',
                                          'flagged_regions.css')
                                 ])
        #BaseReport.save_html(flagged_regions_report, cnf, flagged_report_fpath, caption='Flagged regions')
        info('')
        info('Flagged regions (total ' + str(total_regions) + ' ' +
             region_type + ') saved into:')
        info('  ' + flagged_report_fpath)