<commit_before>import argparse
import pandas as pd
from GamTools import segmentation
import os
import sys
import itertools

parser = argparse.ArgumentParser(description='Takes in a segmentation multibam file and returns coverage stats for each column')
parser.add_argument('segmentation', metavar='SEGMENTATION_MULTIBAM', help='Path to a segmentation multibam')

chroms = ['chr{0}'.format(c) for c in range(1,20)]

def proportion_with_neighbours(block_list):
    no_with_neighbours = 0
    for k, g in itertools.groupby(block_list):
        g = list(g)
        if k and len(g) > 1:
            no_with_neighbours += len(g)
            
    return float(no_with_neighbours) / sum(block_list)

def extract_sample_name(path):
    return os.path.basename(path).split('.')[0]

def get_df_stats(segmentation_df):

    prop_neighb = segmentation_df.apply(proportion_with_neighbours, axis=0)
    prop_neighb.name = 'Proportion_with_neighbours'

    genome_cover = segmentation_df.mean()
    genome_cover.name = 'Genome_coverage'

    positive_chroms = (segmentation_df.groupby(level=0).mean() > segmentation_df.groupby(level=0).mean().mean()).sum()
    positive_chroms.name = 'Positive_chromosomes'

    stats_df = pd.concat([genome_cover, positive_chroms, prop_neighb], axis=1).reset_index()

    stats_df['Sample'] = stats_df['index'].apply(extract_sample_name)

    return stats_df[['Sample', 'Genome_coverage', 'Positive_chromosomes',
            'Proportion_with_neighbours']]

def print_file_stats(multibam_handle):

    segmentation_df = segmentation.open_segmentation(multibam_handle)<commit_msg>Catch zero division errors in segmentation stats.<commit_after>import argparse
import pandas as pd
from GamTools import segmentation
import os
import sys
import itertools

parser = argparse.ArgumentParser(description='Takes in a segmentation multibam file and returns coverage stats for each column')
parser.add_argument('segmentation', metavar='SEGMENTATION_MULTIBAM', help='Path to a segmentation multibam')

chroms = ['chr{0}'.format(c) for c in range(1,20)]

def proportion_with_neighbours(block_list):
    no_with_neighbours = 0
    for k, g in itertools.groupby(block_list):
        g = list(g)
        if k and len(g) > 1:
            no_with_neighbours += len(g)
            
    try:
        return float(no_with_neighbours) / sum(block_list)
    except ZeroDivisionError:
        return 0.

def extract_sample_name(path):
    return os.path.basename(path).split('.')[0]

def get_df_stats(segmentation_df):

    prop_neighb = segmentation_df.apply(proportion_with_neighbours, axis=0)
    prop_neighb.name = 'Proportion_with_neighbours'

    genome_cover = segmentation_df.mean()
    genome_cover.name = 'Genome_coverage'

    positive_chroms = (segmentation_df.groupby(level=0).mean() > segmentation_df.groupby(level=0).mean().mean()).sum()
    positive_chroms.name = 'Positive_chromosomes'

    stats_df = pd.concat([genome_cover, positive_chroms, prop_neighb], axis=1).reset_index()

    stats_df['Sample'] = stats_df['index'].apply(extract_sample_name)

    return stats_df[['Sample', 'Genome_coverage', 'Positive_chromosomes',
            'Proportion_with_neighbours']]

def print_file_stats(multibam_handle):

    segmentation_df = segmentation.open_segmentation(multibam_handle)
{'fraction_non_alphanumeric': 0.10580723624201885, 'fraction_numerical': 0.006993006993006993, 'mean_word_length': 4.896057347670251, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4081468', 'n_tokens_mistral': 1143, 'n_tokens_neox': 1051, 'n_words': 210}