peaks.py

extract_blocks

Use find_peaks from the SciPy signal processing library to look for sequences of SNPs. Sequences that "stand out" are collected into a block, represented by a data frame with a new column appended to hold the block ID.

Parameters:
  • chromosome –

    a data frame where each row describes a SNP

  • max_block_size –

    the maximum number of SNPs to include in a block

Returns:
  • –

    a data frame containing all the SNPs in blocks.

Source code in src/xo/peaks.py
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
def extract_blocks(chromosome, max_block_size):
    '''
    Use `find_peaks` from the SciPy signal processing library to look for
    sequences of SNPs.  Sequences that "stand out" are collected into a block,
    represented by a data frame with a new column appended to hold the block ID.

    Arguments:
      chromosome:  a data frame where each row describes a SNP
      max_block_size:  the maximum number of SNPs to include in a block

    Returns:
      a data frame containing all the SNPs in blocks.
    '''
    signal = ((chromosome.hmm_state1 == 'CB4856').cumsum() - (chromosome.hmm_state1 == 'N2').cumsum()).to_numpy()
    px, prop = find_peaks(signal, prominence=1)
    blocks = []
    for i in range(len(px)):
        if prop['left_bases'][i] == px[i] - prop['prominences'][i]:
            blk_start = prop['left_bases'][i] + 1
            blk_end = px[i]
        else:
            blk_start = px[i]+1
            blk_end = prop['right_bases'][i]
        if blk_end - blk_start > max_block_size:
            continue
        df = chromosome.iloc[blk_start:blk_end+1]
        blocks.append(df.assign(blk_id=i))
    return pd.concat(blocks) if blocks else None