# Assess Module

<a id="geniml.assess"></a>

<a id="geniml.assess.assess"></a>

## `geniml.assess.assess`

<a id="geniml.assess.assess.run_all_assessment_methods"></a>

### `run_all_assessment_methods`

```python
def run_all_assessment_methods(raw_data_folder, file_list, universe, no_workers, folder_out, pref, save_each, overlap=False, distance_f_t_u=False, distance_f_t_u_flex=False, distance_u_t_f=False, distance_u_t_f_flex=False)
```

Assess universe fit to collection using overlap and distance metrics.

Args:
    raw_data_folder (str): path to raw files from the collection
    file_list (str): path to file with list of files in the collection
    universe (str): path to universe that is being assessed
    no_workers (int): number of workers for multiprocessing
    folder_out (str): output folder
    pref (str): prefixed used for creating output files
    save_each (bool): if save output of distance metrics for each region
    overlap (bool): if calculate overlap metrics
    distance_f_t_u (bool): if calculate distance from file to universe metrics
    distance_f_t_u_flex (bool): if calculate flexible distance from file to universe metrics
    distance_u_t_f (bool): if calculate distance from universes to file metrics
    distance_u_t_f_flex (bool): if calculate flexible distance from universes to file metrics

<a id="geniml.assess.assess.get_rbs"></a>

### `get_rbs`

```python
def get_rbs(f_t_u, u_t_f)
```

Calculate RBS

<a id="geniml.assess.assess.get_mean_rbs"></a>

### `get_mean_rbs`

```python
def get_mean_rbs(folder, file_list, universe, no_workers, flexible=False)
```

Calculate average RBS of the collection.

Args:
    folder (str): path to folder with the collection
    file_list (str): path to file with list of files in the collection
    universe (str): path to the universe
    no_workers (int): number of workers for multiprocessing
    flexible (bool): if to calculate flexible version of the metric

Returns:
    int: average RBS

<a id="geniml.assess.assess.get_rbs_from_assessment_file"></a>

### `get_rbs_from_assessment_file`

```python
def get_rbs_from_assessment_file(file, cs_each_file=False, flexible=False)
```

Calculate RBS form file with results of metrics per file.

Args:
    file (str): path to file with assessment results
    cs_each_file (bool): if report RBS for each file, not average for the collection
    flexible (bool): if use flexible version of the metric

<a id="geniml.assess.assess.get_f_10_score"></a>

### `get_f_10_score`

```python
def get_f_10_score(folder, file_list, universe, no_workers)
```

Get F10 score for a universes and collection of files.

Args:
    folder (str): path to folder with the collection
    file_list (str): path to file with list of files in the collection
    universe (str): path to the universe
    no_workers (int): number of workers for multiprocessing

Returns:
    int: average F10 score

<a id="geniml.assess.assess.get_f_10_score_from_assessment_file"></a>

### `get_f_10_score_from_assessment_file`

```python
def get_f_10_score_from_assessment_file(file, f10_each_file=False)
```

Get F10 score from assessment output file.

Args:
    file (str): path to file with assessment results
    f10_each_file (bool): if report F10 for each file, not average for the collection

<a id="geniml.assess.assess.get_likelihood"></a>

### `get_likelihood`

```python
def get_likelihood(model_file, universe, cove_folder, cove_prefix='all', flexible=False, save_peak_input=False)
```

Calculate universe likelihood given collection.

Args:
    model_file (str): path to file with likelihood model
    universe (str): path to the universe
    cove_folder (str): path to the coverage folder
    cove_prefix (str): prefixed used for generating coverage
    flexible (bool): if to calculate flexible likelihood
    save_peak_input (bool): if to save likelihood input of each region

<a id="geniml.assess.assess.filter_universe"></a>

### `filter_universe`

```python
def filter_universe(universe, universe_filtered, min_size=0, min_coverage=0, filter_lh=False, model_file=None, cove_folder=None, cove_prefix=None, lh_cutoff=0)
```

Filter universe by region size, coverage by collection, likelihood.

Args:
    universe (str): path to input universe
    universe_filtered (str): path to output filtered universe
    min_size (int): minimum size of the region in the output universe
    min_coverage (int): minimum number coverage of universe region by collection
    filter_lh (bool): if use likelihood to filter universe
    model_file (str): path to collection likelihood model
    cove_folder (str): path to folder with coverage tracks
    cove_prefix (str): prefixed used for creating tracks
    lh_cutoff (int): minimum likelihood input

<a id="geniml.assess.cli"></a>

## `geniml.assess.cli`

<a id="geniml.assess.cli.build_subparser"></a>

### `build_subparser`

```python
def build_subparser(parser)
```

Builds argument parser.

**Returns:**

- Argument parser

<a id="geniml.assess.distance"></a>

## `geniml.assess.distance`

<a id="geniml.assess.distance.flexible_distance_between_two_regions"></a>

### `flexible_distance_between_two_regions`

```python
def flexible_distance_between_two_regions(region, query)
```

Calculate distance between region and flexible region from flexible universe.

Args:
    region ([int, int]): region from flexible universe
    query (int): analyzed region

Returns:
    int: distance

<a id="geniml.assess.distance.distance_between_two_regions"></a>

### `distance_between_two_regions`

```python
def distance_between_two_regions(region, query)
```

Calculate distance between region in database and region from the query.

Args:
    region ([int]): region from hard universe
    query (int): analysed region

Returns:
    int: distance

<a id="geniml.assess.distance.distance_to_closest_region"></a>

### `distance_to_closest_region`

```python
def distance_to_closest_region(db, db_queue, i, current_chrom, unused_db, pos_index, flexible, uni_to_file)
```

Calculate distance from given peak to the closest region in database.

Args:
    db (file): database file
    db_queue (list): queue of three last positions in database
    i: analyzed position from the query
    current_chrom (str): current analyzed chromosome from query
    unused_db (list): list of positions from universe that were not compared to query
    pos_index (list): which indexes from universe region use to calculate distance
    flexible (bool): whether the universe if flexible
    uni_to_file (bool): whether calculate distance from universe to file

Returns:
    int: peak distance to universe

<a id="geniml.assess.distance.read_in_new_universe_regions"></a>

### `read_in_new_universe_regions`

```python
def read_in_new_universe_regions(db, q_chrom, current_chrom, unused_db, db_queue, waiting, pos_index)
```

Read in new universe regions closest to the peak.

Args:
    db (file): universe file
    q_chrom (str): new peak's chromosome
    current_chrom (str): chromosome that was analyzed so far
    unused_db (list): list of positions from universe that were not compared to query
    db_queue (list): que of three last positions in universe
    waiting (bool): whether iterating through file, without calculating
        distance, if present chromosome not present in universe
    pos_index (list): which indexes from universe region use to calculate distance

Returns:
    tuple: (bool, str) - if iterating through chromosome not present in universe; current chromosome in query

<a id="geniml.assess.distance.calc_distance_between_two_files"></a>

### `calc_distance_between_two_files`

```python
def calc_distance_between_two_files(universe, q_folder, q_file, flexible, save_each, folder_out, pref, uni_to_file=False)
```

Main function for calculating distance between regions in file query to regions in database.

Args:
    universe (str): path to universe
    q_folder (str): path to folder containing query files
    q_file (str): query file
    flexible (bool): whether the universe if flexible
    save_each (bool): whether to save calculated distances for each file
    folder_out (str): output folder
    pref (str): prefix used as the name of the folder containing calculated distance for each file
    uni_to_file (bool): whether to calculate distance from universe to file

Returns:
    tuple: (str, int, int) - file name; median od distance of starts to starts in universe;
        median od distance of ends to ends in universe

<a id="geniml.assess.distance.run_distance"></a>

### `run_distance`

```python
def run_distance(folder, file_list, universe, no_workers, flexible=False, folder_out=None, pref=None, save_each=False, uni_to_file=False)
```

For group of files calculate distance to the nearest region in universe.

Args:
    folder (str): path to folder containing query files
    file_list (str): path to file containing list of query files
    universe (str): path to universe file
    no_workers (int): number of parallel processes
    flexible (bool): whether the universe if flexible
    folder_out (str): output folder
    pref (str): prefix used for saving
    save_each (bool): whether to save calculated distances for each file
    uni_to_file (bool): whether to calculate distance from universe to file

Returns:
    tuple: (float, float) - mean of median distances from starts in query to the nearest starts in universe;
        mean of median distances from ends in query to the nearest ends in universe

<a id="geniml.assess.intersection"></a>

## `geniml.assess.intersection`

<a id="geniml.assess.intersection.chrom_cmp"></a>

### `chrom_cmp`

```python
def chrom_cmp(a, b)
```

Return smaller chromosome name

<a id="geniml.assess.intersection.relationship_helper"></a>

### `relationship_helper`

```python
def relationship_helper(region_a, region_b, only_in, overlap)
```

For two region calculate their overlap; for earlier region calculate how many base pair only in it.

Args:
    region_a ([int, int]): region that starts first
    region_b ([int, int]): region that starts second
    only_in (int): number of positions only in a so far
    overlap (int): number of overlapping so far

<a id="geniml.assess.intersection.two_region_intersection_diff"></a>

### `two_region_intersection_diff`

```python
def two_region_intersection_diff(region_d, region_q, only_in_d, only_in_q, inside_d, inside_q, overlap, start_d, start_q, waiting_d, waiting_q)
```

Check mutual position of two regions and calculate intersection and difference of two regions.

Args:
    region_d (list): region from universe
    region_q (list): region from query
    only_in_d (int): number of base pair only in universe
    only_in_q (int): number of base pair only in query
    inside_d (bool): whether there is still part of the region from universe to analyse
    inside_q (bool): whether there is still part of the region from query to analyse
    overlap (int): size of overlap
    start_d (int): start position of currently analyzed universe region
    start_q (int): start position of currently analyzed query region
    waiting_d (bool): whether waiting for the query to finish chromosome
    waiting_q (bool): whether waiting for the universe to finish chromosome

<a id="geniml.assess.intersection.read_in_new_line"></a>

### `read_in_new_line`

```python
def read_in_new_line(region, start, chrom, inside, waiting, lines, c_chrom, not_e)
```

Read in a new line from query or universe file

<a id="geniml.assess.intersection.calc_diff_intersection"></a>

### `calc_diff_intersection`

```python
def calc_diff_intersection(db, folder, query)
```

Difference and overlap of two files on base pair level.

Args:
    db (str): path to universe file
    folder (str): path to folder with query file
    query (str): query file name

Returns:
    tuple: (str, int, int, int) - file name; bp only in universe; bp only in query; overlap in bp

<a id="geniml.assess.intersection.run_intersection"></a>

### `run_intersection`

```python
def run_intersection(folder, file_list, universe, no_workers)
```

Calculate the base pair intersection of universe and group of files.

Args:
    folder (str): path to folder containing query files
    file_list (str): path to file containing list of query files
    universe (str): path to universe file
    no_workers (int): number of parallel processes
    save_to_file (str): whether to save median of calculated distances for each file
    folder_out (str): output folder
    pref (str): prefix used for saving

Returns:
    tuple: (float, float) - mean of fractions of intersection of file and universe divided by universe size;
        mean of fractions of intersection of file and universe divided by file size

<a id="geniml.assess.likelihood"></a>

## `geniml.assess.likelihood`

<a id="geniml.assess.likelihood.LhModel"></a>

### `LhModel`

```python
class LhModel(model, cove)
```

Object with combined information about lh model and coverage.

Args:
    model (ndarray): lh model array
    cove (ndarray): coverage array

#### Attributes

- <a id="geniml.assess.likelihood.LhModel.model"></a>**`model`**
- <a id="geniml.assess.likelihood.LhModel.cove"></a>**`cove`**

<a id="geniml.assess.likelihood.calc_likelihood_hard"></a>

### `calc_likelihood_hard`

```python
def calc_likelihood_hard(universe, chroms, model_lh, coverage_folder, coverage_prefix, name, s_index, e_index=None)
```

Calculate likelihood of universe for given type of model. To be used with binomial model.

Args:
    universe (str): path to universe file
    chroms (list): list of chromosomes present in model
    model_lh (ModelLH): likelihood model
    coverage_folder: path to a folder with genome coverage by tracks
    coverage_prefix: prefix used in uniwig for creating coverage
    name (str): suffix of model file name, which contains information about model type
    s_index (int): from which position in universe line take assess region start position
    e_index (int): from which position in universe line take assess region end position

Returns:
    float: likelihood of universe for given model

<a id="geniml.assess.likelihood.hard_universe_likelihood"></a>

### `hard_universe_likelihood`

```python
def hard_universe_likelihood(model, universe, coverage_folder, coverage_prefix)
```

Calculate likelihood of hard universe based on core, start, end coverage model.

Args:
    model (str): path to file containing model
    universe (str): path to universe
    coverage_folder: path to a folder with genome coverage by tracks
    coverage_prefix: prefix used in uniwig for creating coverage

Returns:
    float: likelihood

<a id="geniml.assess.likelihood.likelihood_only_core"></a>

### `likelihood_only_core`

```python
def likelihood_only_core(model_file, universe, coverage_folder, coverage_prefix)
```

Calculate likelihood of universe based only on core coverage model.

Args:
    model_file (str): path to name containing model
    universe (str): path to universe
    coverage_folder: path to a folder with genome coverage by tracks
    coverage_prefix: prefix used in uniwig for creating coverage

Returns:
    float: likelihood

<a id="geniml.assess.likelihood.background_likelihood"></a>

### `background_likelihood`

```python
def background_likelihood(start, end, model_start, model_cove, model_end)
```

Calculate likelihood of background for given region

<a id="geniml.assess.likelihood.weigh_livelihood"></a>

### `weigh_livelihood`

```python
def weigh_livelihood(start, end, model_process, model_cove, model_out, reverse)
```

Calculate weighted likelihood of flexible part of the region.

Args:
    start (int): start of the region
    end (int): end of the region
    model_process (array): model for analyzed type of flexible region
    model_cove (array): model for coverage
    model_out (array): model for flexible region that is not being analyzed
    reverse (bool): if model_process corespondents to end we have to reverse the weighs

Returns:
    float: likelihood of flexible part of the region

<a id="geniml.assess.likelihood.flexible_peak_likelihood"></a>

### `flexible_peak_likelihood`

```python
def flexible_peak_likelihood(start_s, start_e, end_s, end_e, model_start, model_cove, model_end)
```

<a id="geniml.assess.likelihood.read_coverage"></a>

### `read_coverage`

```python
def read_coverage(cove_folder, cove_prefix, current_chrom)
```

<a id="geniml.assess.likelihood.likelihood_flexible_universe"></a>

### `likelihood_flexible_universe`

```python
def likelihood_flexible_universe(model_file, universe, cove_folder, cove_prefix, save_peak_input=False)
```

Likelihood of given universe under the model.

Args:
    model_file (str): path to file with lh model
    universe (str): path to universe
    cove_folder: path to a folder with genome coverage by tracks
    cove_prefix: prefix used in uniwig for creating coverage
    save_peak_input (bool): whether to save universe with each peak lh

Returns:
    float: lh of the flexible universe

<a id="geniml.assess.utils"></a>

## `geniml.assess.utils`

<a id="geniml.assess.utils.prep_data"></a>

### `prep_data`

```python
def prep_data(folder, file, tmp_file)
```

File sort and merge

<a id="geniml.assess.utils.check_if_uni_sorted"></a>

### `check_if_uni_sorted`

```python
def check_if_uni_sorted(universe)
```

Check if regions in file are sorted

<a id="geniml.assess.utils.check_if_uni_flexible"></a>

### `check_if_uni_flexible`

```python
def check_if_uni_flexible(universe)
```

<a id="geniml.assess.utils.process_line"></a>

### `process_line`

```python
def process_line(line)
```

Helper for reading in bed file line

<a id="geniml.assess.utils.chrom_cmp_bigger"></a>

### `chrom_cmp_bigger`

```python
def chrom_cmp_bigger(a, b)
```

Natural check if chromosomes name is bigger

<a id="geniml.assess.utils.process_db_line"></a>

### `process_db_line`

```python
def process_db_line(dn, pos_index)
```

Helper for reading in universe bed file line
