Skip to content

uorf4u.manager

This module provides managing classes and methods for the tool.

Parameters

A Parameters object holds and parse cmd's and config's arguments for the tool.

Note:

A Parameters object have to be created in each script since it's used by each class of the tool as a mandatory argument.

Source code in uorf4u/manager.py
 21
 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
 50
 51
 52
 53
 54
 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
class Parameters:
    """A Parameters object holds and parse cmd's and config's arguments for the tool.

    Note:
        A Parameters object have to be created in each script since it's used by each
            class of the tool as a mandatory argument.

    """

    def __init__(self):
        self.arguments = dict(assemblies_list="NA", debug=False, verbose=False)
        self.cmd_arguments = {"assemblies_list": "NA", "verbose": True}

    def parse_cmd_arguments(self) -> None:
        parser = argparse.ArgumentParser(prog="uorf4u", add_help=False,
                                         usage="uorf4u [-an accession_number | -hl [ac1, ac2..] | -hlf path | -fa path]"
                                               "[optional arguments]")
        mutually_exclusive_group = parser.add_mutually_exclusive_group()
        mutually_exclusive_group.add_argument("-an", dest="accession_number", type=str, default=None)
        mutually_exclusive_group.add_argument("-hl", dest="homologues_list", nargs="*", default=None)
        mutually_exclusive_group.add_argument("-hlf", dest="homologues_list_file", type=str, default=None)
        mutually_exclusive_group.add_argument("-fa", dest="fasta", type=str, default=None)
        parser.add_argument("-data", "--data", dest="uorf4u_data", action="store_true")
        parser.add_argument("-linux", "--linux", dest="linux", action="store_true", default=None)
        parser.add_argument("-blastp_path", "--blastp_path", dest="blastp_path", type=str, default=None)
        parser.add_argument("-bdb", dest="blastp_database", choices=["refseq_protein", "refseq_select", None], type=str,
                            default=None)
        parser.add_argument("-lbdb", dest="local_blastp_database", type=str, default=None)
        parser.add_argument("-bh", dest="blastp_hit_list_size", type=int, default=None)
        parser.add_argument("-bid", dest="blastp_pident_to_query_length_cutoff", type=float, default=None)
        parser.add_argument("-mna", dest="max_number_of_assemblies", type=int, default=None)
        parser.add_argument("-al", dest="assemblies_list", type=str, default="NA")
        parser.add_argument("-annot", dest="check_assembly_annotation", action="store_true", default=None)
        parser.add_argument("-ul", dest="upstream_region_length", type=int, default=None)
        parser.add_argument("-dl", dest="downstream_region_length", type=int, default=None)
        parser.add_argument("-asc", dest="alternative_start_codons", action="store_true", default=None)
        parser.add_argument("-nsd", dest="filter_by_sd", action="store_false", default=None)
        parser.add_argument("-at", dest="alignment_type", choices=["nt", "aa", None], type=str, default=None)
        parser.add_argument("-pc", dest="orfs_presence_cutoff", type=float, default=None)
        parser.add_argument("-fast", dest="fast_searching", action="store_true", default=None)
        parser.add_argument("-o", dest="output_dir", type=str, default=None)
        parser.add_argument("-c", dest="config_file", type=str, default="Not selected")
        parser.add_argument("-v", "--version", action="version", version="%(prog)s 0.9.5")
        parser.add_argument("-q", "--quiet", dest="verbose", default=True, action="store_false")
        parser.add_argument("--debug", "-debug", dest="debug", action="store_true")
        parser.add_argument("-h", "--help", dest="help", action="store_true")
        args = parser.parse_args()
        args = vars(args)

        if len(sys.argv[1:]) == 0:
            args["help"] = True

        if args["help"]:
            help_message_path = os.path.join(os.path.dirname(__file__), "uorf4u_data", "help.txt")
            with open(help_message_path, "r") as help_message:
                print(help_message.read(), file=sys.stdout)
                sys.exit()

        if args["uorf4u_data"]:
            uorf4u.methods.copy_package_data()
            sys.exit()

        if args["linux"]:
            uorf4u.methods.adjust_paths_for_linux()
            sys.exit()

        if args["blastp_path"]:
            uorf4u.methods.set_blastp_path(args["blastp_path"])
            sys.exit()

        filtered_args = {k: v for k, v in args.items() if v is not None}
        self.cmd_arguments = filtered_args

    def load_config(self, path_c):
        if path_c == "Not selected":
            raise uORF4uError("Please, specify -c argument <bacteria|eukaryotes|<file.cfg>. "
                              "It can be either a path to a configuration file or name of a premade config file "
                              "(bacteria or eukaryotes)")
        try:
            if path_c == "bacteria" or path_c == "eukaryotes":
                path_c = os.path.join(os.path.dirname(__file__), "uorf4u_data", f"uorf4u_{path_c}.cfg")
            config = configs.load(path_c)
            config = config.get_config()
            internal_dir = os.path.dirname(__file__)
            config["root"]["output_dir"] = config["root"]["output_dir"].replace("{current_date}",
                                                                                time.strftime("%Y_%m_%d-%H_%M"))
            for key in config["root"].keys():
                if type(config["root"][key]) is str and "{config_path}" in config["root"][key]:
                    config["root"][key] = config["root"][key].replace("{config_path}", os.path.dirname(path_c))
            if config["root"]["blastp_database"] not in ["refseq_select", "refseq_protein"]:
                raise uORF4uError("Config's parameter 'blastp_database' should be either refseq_select or "
                                  "refseq_protein. ")

            self.arguments.update(config["root"])
            self.arguments.update(self.cmd_arguments)
            self.load_palette()
            self.load_color_config()
            Bio.Entrez.tool = "uorf4u"
            Bio.Entrez.email = self.arguments["ncbi_entrez_email"]
            if "ncbi_entrez_api_key" in self.arguments.keys():
                Bio.Entrez.api_key = self.arguments["ncbi_entrez_api_key"]
        except Exception as error:
            raise uORF4uError(
                "Unable to parse the specified config file. Please check your config file or written name.") from error

    def load_palette(self) -> None:
        palette_path = self.arguments[f"palette"]
        self.arguments[f"palette"] = configs.load(palette_path).get_config()["root"]

    def load_color_config(self) -> None:
        for seq_type in ["nt", "aa"]:
            path = self.arguments[f"colors_{seq_type}"]
            colors_pre_dict = configs.load(path).get_config()["root"]
            colors_dict = dict()
            for elements, color in colors_pre_dict.items():
                for element in elements:
                    colors_dict[element] = color
            self.arguments[f"colors_{seq_type}"] = colors_dict

    def update(self, parameters):
        self.arguments.update(parameters)

uORF4uError

Bases: Exception

A helper for exceptions parsing inherited from the Exception class.

Source code in uorf4u/manager.py
14
15
16
17
18
class uORF4uError(Exception):
    """A helper for exceptions parsing inherited from the Exception class.

    """
    pass