Skip to content

Helpers

helpers module.

This module houses a collection of auxiliary functions designed to provide general-purpose utility and support across different parts of the GRAMEP package. These functions are not domain-specific and are meant to assist with common tasks that might be needed in various contexts.

Contents
  • get_sequence_interval: Create a closed interval using the 'start' and 'end' values from a row of data.
  • make_report: Generate a report for variations and annotations in a sequence.
  • get_colours: Get a dictionary of colours based on the specified palette.
  • next_colour: Get the next colour from the predefined colour cycle.
Todo
  • Implement tests.

COLOR_CYCLE = cycle(COLOR_LIST) module-attribute

itertools.cycle: A cycle iterator that iterates through the COLOR_LIST repeatedly.

COLOR_LIST = ['lightgrey', 'white'] module-attribute

list[str]: A list of color values for cycling through in the color cycle.

message = Messages() module-attribute

Set the Message class for logging.

get_colours(colour_palette)

Get a dictionary of colours based on the specified palette.

This function takes a colour palette name and returns a dictionary mapping predefined keys to nested dictionaries containing colour values based on the chosen palette.

Parameters:

Name Type Description Default
colour_palette str

The name of the colour palette to use.

required

Returns:

Type Description
dict[str, dict[str, str]]

dict[str, dict[str, str]]: A dictionary mapping predefined keys to nested dictionaries containing colour values.

Source code in python/gramep/helpers.py
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
def get_colours(colour_palette: str) -> dict[str, dict[str, str]]:
    """
    Get a dictionary of colours based on the specified palette.

    This function takes a colour palette name and returns a dictionary mapping
    predefined keys to nested dictionaries containing colour values based on \
    the chosen palette.

    Args:
        colour_palette (str): The name of the colour palette to use.

    Returns:
        dict[str, dict[str, str]]: A dictionary mapping predefined keys to \
        nested dictionaries containing colour values.
    """

    palettes = {
        'classic': {
            'A': 'steelblue',
            'C': 'indianred',
            'T': 'darkseagreen',
            'G': 'skyblue',
        },
        'wes': {
            'A': '#CC8B3C',
            'C': '#456355',
            'T': '#541F12',
            'G': '#B62A3D',
        },
        'primary': {
            'A': 'green',
            'C': 'goldenrod',
            'T': 'steelblue',
            'G': 'indianred',
        },
        'purine-pyrimidine': {
            'A': 'indianred',
            'C': 'teal',
            'T': 'teal',
            'G': 'indianred',
        },
        'greyscale': {
            'A': '#CCCCCC',
            'C': '#999999',
            'T': '#666666',
            'G': '#333333',
        },
        'blues': {
            'A': '#3DB19D',
            'C': '#76C5BF',
            'T': '#423761',
            'G': 'steelblue',
        },
        'verity': {
            'A': '#EC799A',
            'C': '#df6eb7',
            'T': '#FF0080',
            'G': '#9F0251',
        },
    }
    if colour_palette not in palettes:
        exit(message.error_color_pallete())
    else:
        color_dict = palettes[colour_palette]

    return color_dict

get_sequence_interval(row)

Create a closed interval using the 'start' and 'end' values from a row of data.

This function constructs a closed interval using the 'start' and 'end' values from a pandas Series row. The interval is closed on both ends.

Parameters:

Name Type Description Default
row Series

A pandas Series containing 'start' and 'end' values.

required

Returns:

Type Description
Interval

pd.Interval: A closed interval defined by the 'start' and 'end' values.

Source code in python/gramep/helpers.py
39
40
41
42
43
44
45
46
47
48
49
50
51
52
def get_sequence_interval(row: pd.Series) -> pd.Interval:
    """
    Create a closed interval using the 'start' and 'end' values from a row of data.

    This function constructs a closed interval using the 'start' and 'end' values from
    a pandas Series row. The interval is closed on both ends.

    Args:
        row (pd.Series): A pandas Series containing 'start' and 'end' values.

    Returns:
        pd.Interval: A closed interval defined by the 'start' and 'end' values.
    """
    return pd.Interval(row.start, row.end, closed='both')

make_report(variation_position, seq_name, sequence_interval, annotation_dataframe)

Generate a report for variations and annotations in a sequence.

This function takes variation positions, sequence name, sequence intervals, and an annotation DataFrame, and generates a report detailing variations and annotations present in the sequence.

Parameters:

Name Type Description Default
variation_position list

A list of variation positions in the sequence.

required
seq_name str

The name of the sequence being analyzed.

required
sequence_interval IntervalIndex

Interval index representing sequence intervals.

required
annotation_dataframe DataFrame

DataFrame containing sequence annotations.

required

Returns:

Type Description
list[str]

list[str]: A list of strings representing the generated report.

Source code in python/gramep/helpers.py
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
def make_report(
    variation_position: list,
    seq_name: str,
    sequence_interval: pd.IntervalIndex,
    annotation_dataframe: pd.DataFrame,
) -> list[str]:
    """
    Generate a report for variations and annotations in a sequence.

    This function takes variation positions, sequence name, sequence intervals,
    and an annotation DataFrame, and generates a report detailing variations and
    annotations present in the sequence.

    Args:
        variation_position (list): A list of variation positions in the sequence.
        seq_name (str): The name of the sequence being analyzed.
        sequence_interval (pd.IntervalIndex): Interval index representing \
        sequence intervals.
        annotation_dataframe (pd.DataFrame): DataFrame containing sequence annotations.

    Returns:
        list[str]: A list of strings representing the generated report.
    """

    report_list = []
    for var in variation_position:
        variation_index, snp_value, ref_value, var_value = var.split(':')
        if sequence_interval is not None:
            annotation_index = []
            for interval in sequence_interval:
                if int(variation_index) in interval:
                    annotation_index.append(
                        list(
                            sequence_interval[
                                sequence_interval == interval
                            ].index
                        )
                    )
            annotation_index = [
                auxIndex1[auxIndex2]
                for auxIndex1 in list(set(map(tuple, annotation_index)))
                for auxIndex2 in range(len(auxIndex1))
            ]

            if len(annotation_index) > 0:
                for index_element in annotation_index:
                    report_line = str()
                    if (
                        annotation_dataframe.loc[index_element]['product']
                        is None
                    ):
                        report_line = str(
                            str(seq_name)
                            + ';'
                            + str(
                                annotation_dataframe.loc[index_element]['Name']
                            )
                            + ';'
                            + str(
                                annotation_dataframe.loc[index_element][
                                    'start'
                                ]
                            )
                            + ';'
                            + str(
                                annotation_dataframe.loc[index_element]['end']
                            )
                            + ';'
                            + str(
                                annotation_dataframe.loc[index_element]['type']
                            )
                            + ';'
                            + str(variation_index)
                            + ';'
                            + str(ref_value)
                            + ';'
                            + str(var_value)
                            + ';'
                            + str(snp_value[0])
                            + ';'
                            + str(snp_value[1])
                        )
                    report_list.append(report_line)
            else:
                report_line = str(
                    str(seq_name)
                    + ';'
                    + str('NO_ANNOTATIONS_AVAILABLE')
                    + ';'
                    + str('NO_ANNOTATIONS_AVAILABLE')
                    + ';'
                    + str('NO_ANNOTATIONS_AVAILABLE')
                    + ';'
                    + str('NO_ANNOTATIONS_AVAILABLE')
                    + ';'
                    + str(variation_index)
                    + ';'
                    + str(ref_value)
                    + ';'
                    + str(var_value)
                    + ';'
                    + str(snp_value[0])
                    + ';'
                    + str(snp_value[1])
                )
                report_list.append(report_line)
        else:
            report_line = str(
                str(seq_name)
                + ';'
                + str('NO_ANNOTATIONS_AVAILABLE')
                + ';'
                + str('NO_ANNOTATIONS_AVAILABLE')
                + ';'
                + str('NO_ANNOTATIONS_AVAILABLE')
                + ';'
                + str('NO_ANNOTATIONS_AVAILABLE')
                + ';'
                + str(variation_index)
                + ';'
                + str(ref_value)
                + ';'
                + str(var_value)
                + ';'
                + str(snp_value[0])
                + ';'
                + str(snp_value[1])
            )
            report_list.append(report_line)

    return list(set(report_list))

next_colour()

Get the next colour from the predefined colour cycle.

This function returns the next colour from the predefined colour cycle, which is used to cycle through a list of colour values.

Returns:

Name Type Description
str str

The next colour from the colour cycle.

Source code in python/gramep/helpers.py
256
257
258
259
260
261
262
263
264
265
266
def next_colour() -> str:
    """
    Get the next colour from the predefined colour cycle.

    This function returns the next colour from the predefined colour cycle,
    which is used to cycle through a list of colour values.

    Returns:
        str: The next colour from the colour cycle.
    """
    return next(COLOR_CYCLE)