Skip to content

uorf4u.methods

This module provides some methods (e.g. colors tranformation, data copying) used by the tool.

adjust_paths_for_linux()

Change paths in the internal config files for linux.

Returns:

  • None

    None

Source code in uorf4u/methods.py
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
def adjust_paths_for_linux() -> None:
    """Change paths in the internal config files for linux.

    Returns:
        None

    """
    internal_dir = os.path.join(os.path.dirname(__file__), "uorf4u_data")
    config_files = ["uorf4u_eukaryotes.cfg", "uorf4u_bacteria.cfg"]
    for config_file in config_files:
        config_file_path = os.path.join(internal_dir, config_file)
        with open(config_file_path, "r+") as config:
            config_txt = re.sub(r"/muscle5\.1\.macos_arm64", "/muscle5.1.linux_intel64", config.read())
            config_txt = re.sub(r"/mafft-mac/mafft\.bat", "/mafft-linux64/mafft.bat", config_txt)
            config.seek(0)
            config.truncate()
            config.write(config_txt)
    return None

copy_package_data()

Copy the uorf4u package data folder to your current dir.

Returns:

  • None

    None

Source code in uorf4u/methods.py
57
58
59
60
61
62
63
64
65
66
67
68
69
70
def copy_package_data() -> None:
    """Copy the uorf4u package data folder to your current dir.

    Returns:
        None

    """
    try:
        users_dir = os.path.join(os.getcwd(), "uorf4u_data")
        internal_dir = os.path.join(os.path.dirname(__file__), "uorf4u_data")
        shutil.copytree(internal_dir, users_dir, ignore=shutil.ignore_patterns("help*", ".*", "msa_plot_dir.R"))
        return None
    except Exception as error:
        raise uorf4u.manager.uORF4uError(f"Unable to copy uorf4u_data folder in your working dir.") from error

get_color(name, parameters)

Get color code by a name.

Parameters:

  • name (str) –

    name of a color.

  • parameters (dict) –

    Parameters' object dict.

Returns:

  • tuple( tuple ) –

    RGB color.

Source code in uorf4u/methods.py
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
def get_color(name: str, parameters: dict) -> tuple:
    """Get color code by a name.

    Arguments:
        name (str): name of a color.
        parameters (dict): Parameters' object dict.

    Returns:
        tuple: RGB color.

    """
    rgb_color = *hex_to_rgb(parameters['palette'][parameters[name]]), parameters[f"{name}_alpha"]
    return rgb_color

hex_to_rgb(value)

Convert HEX color to RGB format.

Parameters:

  • value (str) –

    color in HEX format.

Returns:

  • list( list ) –

    RGB color.

Source code in uorf4u/methods.py
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
def hex_to_rgb(value: str) -> list:
    """Convert HEX color to RGB format.

    Arguments:
        value (str): color in HEX format.

    Returns:
        list: RGB color.

    """
    try:
        value = value.lstrip("#")
        lv = len(value)
        rgb = [i / 255 for i in tuple(int(value[i:i + lv // 3], 16) for i in range(0, lv, lv // 3))]
        return rgb
    except Exception as error:
        raise uorf4u.manager.uORF4uError(
            f"Unable to convert color definition from HEX to RGB. Please check the palette config file.") from error

parse_fasta_file(path, parameters)

Parse fasta file with sequences.

Arguments path: path to a fasta file.

Returns:

  • list( list ) –

    list of processed Bio.SeqRecord.SeqRecord objects.

Source code in uorf4u/methods.py
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
def parse_fasta_file(path: str, parameters) -> list:
    """Parse fasta file with sequences.

    Arguments
        path: path to a fasta file.

    Returns:
        list: list of processed Bio.SeqRecord.SeqRecord objects.

    """
    try:
        processed_records = []
        record_ids = []
        with open(path) as handle:
            for record in Bio.SeqIO.parse(handle, "fasta"):
                label = record.description
                record.description = record.description.replace(record.id, "")
                length = len(record.seq)
                record_annotation = dict(RefSeq=False, length=length,
                                         upstream_region_length=length - parameters.arguments[
                                             "downstream_region_length"],
                                         downstream_region_length=parameters.arguments["downstream_region_length"],
                                         label=label, start=0, stop=length, strand="+")
                record.annotations = record_annotation
                processed_records.append(record)
        return processed_records
    except Exception as error:
        raise uorf4u.manager.uORF4uError(f"Unable to process the fasta file with sequences.") from error

set_blastp_path(blastp_path)

Set a path to blastp.

Returns:

  • None

    None

Source code in uorf4u/methods.py
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
def set_blastp_path(blastp_path) -> None:
    """Set a path to blastp.

    Returns:
        None

    """
    if blastp_path[0] != "/":
        raise uorf4u.manager.uORF4uError(f"You have to specify the absolute path (starts with /) to blastp.")
    if not os.path.exists(blastp_path):
        raise uorf4u.manager.uORF4uError(f"A file with path you provided does not exist.")
    internal_dir = os.path.join(os.path.dirname(__file__), "uorf4u_data")
    config_files = ["uorf4u_eukaryotes.cfg", "uorf4u_bacteria.cfg"]
    for config_file in config_files:
        config_file_path = os.path.join(internal_dir, config_file)
        with open(config_file_path, "r+") as config:
            config_txt = re.sub(r"(blastp\s*=\s*).*", r"\1{}".format(blastp_path), config.read())
            config.seek(0)
            config.truncate()
            config.write(config_txt)
    print("✨ blastp path was successfuly updated.")
    return None

string_height_to_font_size(height, font_type, parameters)

Transform string height to the font size.

Parameters:

  • height (float) –

    available height of the string.

  • font_type (str) –

    font type (see config file; at this moment only regular is available).

  • parameters (dict) –

    Parameters' object dict.

Returns:

  • float( float ) –

    font size defined by height.

Source code in uorf4u/methods.py
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
def string_height_to_font_size(height: float, font_type: str, parameters: dict) -> float:
    """Transform string height to the font size.

    Arguments:
        height (float): available height of the string.
        font_type (str): font type (see config file; at this moment only regular is available).
        parameters (dict): Parameters' object dict.

    Returns:
        float: font size defined by height.

    """
    pdfmetrics.registerFont(TTFont(font_type, parameters[f"font_{font_type}"]))
    face = pdfmetrics.getFont('regular').face
    font_size = (1000 * 1.38 * height) / (face.ascent - face.descent)
    return font_size