Skip to content

qbed_df_to_pyranges

Convert a qbed dataframe to a pyranges dataframe.

:param df: The qbed dataframe. :type df: pd.DataFrame :return: The pyranges dataframe. :rtype: pd.DataFrame

:Example:

import pandas as pd df = pd.DataFrame({‘chr’: [‘chr1’, ‘chr1’], … ‘start’: [1, 2], … ‘end’: [2, 3], … ‘strand’: [‘+’, ‘-‘], … ‘depth’: [1, 2]}) pyranges_df = qbed_df_to_pyranges(df) list(pyranges_df.columns) == [‘Chromosome’, ‘Start’, ‘End’, ‘Strand’, ‘depth’] True

Source code in callingcardstools/PeakCalling/yeast/read_in_data.py
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
def qbed_df_to_pyranges(df: pd.DataFrame) -> pd.DataFrame:
    """
    Convert a qbed dataframe to a pyranges dataframe.

    :param df: The qbed dataframe.
    :type df: pd.DataFrame
    :return: The pyranges dataframe.
    :rtype: pd.DataFrame

    :Example:

    >>> import pandas as pd
    >>> df = pd.DataFrame({'chr': ['chr1', 'chr1'],
    ...                    'start': [1, 2],
    ...                    'end': [2, 3],
    ...                    'strand': ['+', '-'],
    ...                    'depth': [1, 2]})
    >>> pyranges_df = qbed_df_to_pyranges(df)
    >>> list(pyranges_df.columns) == ['Chromosome', 'Start', 'End', 'Strand', 'depth']
    True
    """
    # check input
    if not isinstance(df, pd.DataFrame):
        raise ValueError("df must be a DataFrame")
    if not all(col in df.columns for col in ["chr", "start", "end", "strand", "depth"]):
        raise ValueError(
            "df must have columns `chr`, `start`, `end`, `strand`, " "and `depth`"
        )

    return pr.PyRanges(
        df.rename(
            {"chr": "Chromosome", "start": "Start", "end": "End", "strand": "Strand"},
            axis=1,
        )
    )