annotate cobraxy-9688ad27287b/COBRAxy/utils/general_utils.py @ 90:a48b2e06ebe7 draft

Uploaded
author luca_milaz
date Sun, 13 Oct 2024 11:35:56 +0000
parents
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
90
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
1 import math
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
2 import re
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
3 import sys
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
4 import csv
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
5 import pickle
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
6 import lxml.etree as ET
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
7
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
8 from enum import Enum
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
9 from itertools import count
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
10 from typing import Any, Callable, Dict, Generic, List, Optional, TypeVar, Union
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
11
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
12 import pandas as pd
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
13 import cobra
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
14
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
15 # FILES
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
16 class FileFormat(Enum):
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
17 """
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
18 Encodes possible file extensions to conditionally save data in a different format.
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
19 """
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
20 DAT = ("dat",) # this is how galaxy treats all your files!
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
21 CSV = ("csv",) # this is how most editable input data is written
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
22 TSV = ("tsv",) # this is how most editable input data is ACTUALLY written TODO:more support pls!!
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
23
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
24 SVG = ("svg",) # this is how most metabolic maps are written
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
25 PNG = ("png",) # this is a common output format for images (such as metabolic maps)
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
26 PDF = ("pdf",) # this is also a common output format for images, as it's required in publications.
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
27
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
28 XML = ("xml",) # this is one main way cobra models appear in
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
29 JSON = ("json",) # this is the other
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
30
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
31 PICKLE = ("pickle", "pk", "p") # this is how all runtime data structures are saved
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
32 #TODO: we're in a pickle (ba dum tss), there's no point in supporting many extensions internally. The
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
33 # issue will never be solved for user-uploaded files and those are saved as .dat by galaxy anyway so it
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
34 # doesn't matter as long as we CAN recognize these 3 names as valid pickle extensions. We must however
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
35 # agree on an internal standard and use only that one, otherwise constructing usable paths becomes a nightmare.
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
36 @classmethod
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
37 def fromExt(cls, ext :str) -> "FileFormat":
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
38 """
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
39 Converts a file extension string to a FileFormat instance.
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
40
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
41 Args:
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
42 ext : The file extension as a string.
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
43
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
44 Returns:
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
45 FileFormat: The FileFormat instance corresponding to the file extension.
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
46 """
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
47 variantName = ext.upper()
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
48 if variantName in FileFormat.__members__: return FileFormat[variantName]
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
49
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
50 variantName = variantName.lower()
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
51 for member in cls:
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
52 if variantName in member.value: return member
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
53
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
54 raise ValueErr("ext", "a valid FileFormat file extension", ext)
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
55
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
56 def __str__(self) -> str:
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
57 """
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
58 (Private) converts to str representation. Good practice for usage with argparse.
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
59
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
60 Returns:
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
61 str : the string representation of the file extension.
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
62 """
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
63 return self.value[-1] #TODO: fix, it's the dumb pickle thing
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
64
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
65 class FilePath():
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
66 """
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
67 Represents a file path. View this as an attempt to standardize file-related operations by expecting
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
68 values of this type in any process requesting a file path.
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
69 """
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
70 def __init__(self, filePath :str, ext :FileFormat, *, prefix = "") -> None:
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
71 """
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
72 (Private) Initializes an instance of FilePath.
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
73
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
74 Args:
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
75 path : the end of the path, containing the file name.
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
76 ext : the file's extension.
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
77 prefix : anything before path, if the last '/' isn't there it's added by the code.
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
78
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
79 Returns:
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
80 None : practically, a FilePath instance.
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
81 """
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
82 self.ext = ext
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
83 self.filePath = filePath
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
84
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
85 if prefix and prefix[-1] != '/': prefix += '/'
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
86 self.prefix = prefix
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
87
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
88 @classmethod
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
89 def fromStrPath(cls, path :str) -> "FilePath":
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
90 """
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
91 Factory method to parse a string from which to obtain, if possible, a valid FilePath instance.
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
92
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
93 Args:
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
94 path : the string containing the path
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
95
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
96 Raises:
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
97 PathErr : if the provided string doesn't represent a valid path.
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
98
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
99 Returns:
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
100 FilePath : the constructed instance.
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
101 """
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
102 # This method is often used to construct FilePath instances from ARGS UI arguments. These arguments *should*
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
103 # always be correct paths and could be used as raw strings, however most if not all functions that work with
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
104 # file paths request the FilePath objects specifically, which is a very good thing in any case other than this.
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
105 # What ends up happening is we spend time parsing a string into a FilePath so that the function accepts it, only
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
106 # to call show() immediately to bring back the string and open the file it points to.
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
107 # TODO: this is an indication that the arguments SHOULD BE OF TYPE FilePath if they are filepaths, this ENSURES
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
108 # their correctness when modifying the UI and avoids the pointless back-and-forth.
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
109 result = re.search(r"^(?P<prefix>.*\/)?(?P<name>.*)\.(?P<ext>[^.]*)$", path)
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
110 if not result or not result["name"] or not result["ext"]:
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
111 raise PathErr(path, "cannot recognize folder structure or extension in path")
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
112
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
113 prefix = result["prefix"] if result["prefix"] else ""
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
114 return cls(result["name"], FileFormat.fromExt(result["ext"]), prefix = prefix)
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
115
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
116 def show(self) -> str:
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
117 """
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
118 Shows the path as a string.
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
119
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
120 Returns:
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
121 str : the path shown as a string.
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
122 """
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
123 return f"{self.prefix}{self.filePath}.{self.ext}"
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
124
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
125 def __str__(self) -> str: return self.show()
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
126
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
127 # ERRORS
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
128 def terminate(msg :str) -> None:
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
129 """
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
130 Terminate the execution of the script with an error message.
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
131
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
132 Args:
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
133 msg (str): The error message to be displayed.
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
134
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
135 Returns:
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
136 None
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
137 """
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
138 sys.exit(f"Execution aborted: {msg}\n")
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
139
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
140 def logWarning(msg :str, loggerPath :str) -> None:
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
141 """
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
142 Log a warning message to an output log file and print it to the console. The final period and a
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
143 newline is added by the function.
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
144
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
145 Args:
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
146 s (str): The warning message to be logged and printed.
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
147 loggerPath : The file path of the output log file. Given as a string, parsed to a FilePath and
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
148 immediately read back (beware relative expensive operation, log with caution).
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
149
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
150 Returns:
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
151 None
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
152 """
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
153 # building the path and then reading it immediately seems useless, but it's actually a way of
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
154 # validating that reduces repetition on the caller's side. Besides, logging a message by writing
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
155 # to a file is supposed to be computationally expensive anyway, so this is also a good deterrent from
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
156 # mindlessly logging whenever something comes up, log at the very end and tell the user everything
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
157 # that went wrong. If you don't like it: implement a persistent runtime buffer that gets dumped to
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
158 # the file only at the end of the program's execution.
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
159 with open(FilePath.fromStrPath(loggerPath).show(), 'a') as log: log.write(f"{msg}.\n")
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
160
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
161 class CustomErr(Exception):
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
162 """
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
163 Custom error class to handle exceptions in a structured way, with a unique identifier and a message.
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
164 """
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
165 __idGenerator = count()
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
166 errName = "Custom Error"
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
167 def __init__(self, msg :str, details = "", explicitErrCode = -1) -> None:
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
168 """
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
169 (Private) Initializes an instance of CustomErr.
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
170
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
171 Args:
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
172 msg (str): Error message to be displayed.
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
173 details (str): Informs the user more about the error encountered. Defaults to "".
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
174 explicitErrCode (int): Explicit error code to be used. Defaults to -1.
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
175
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
176 Returns:
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
177 None : practically, a CustomErr instance.
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
178 """
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
179 self.msg = msg
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
180 self.details = details
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
181
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
182 self.id = max(explicitErrCode, next(CustomErr.__idGenerator))
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
183
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
184 def throw(self, loggerPath = "") -> None:
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
185 """
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
186 Raises the current CustomErr instance, logging a warning message before doing so.
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
187
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
188 Raises:
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
189 self: The current CustomErr instance.
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
190
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
191 Returns:
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
192 None
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
193 """
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
194 if loggerPath: logWarning(str(self), loggerPath)
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
195 raise self
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
196
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
197 def abort(self) -> None:
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
198 """
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
199 Aborts the execution of the script.
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
200
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
201 Returns:
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
202 None
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
203 """
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
204 terminate(str(self))
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
205
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
206 def __str__(self) -> str:
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
207 """
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
208 (Private) Returns a string representing the current CustomErr instance.
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
209
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
210 Returns:
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
211 str: A string representing the current CustomErr instance.
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
212 """
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
213 return f"{CustomErr.errName} #{self.id}: {self.msg}, {self.details}."
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
214
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
215 class ArgsErr(CustomErr):
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
216 """
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
217 CustomErr subclass for UI arguments errors.
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
218 """
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
219 errName = "Args Error"
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
220 def __init__(self, argName :str, expected :Any, actual :Any, msg = "no further details provided") -> None:
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
221 super().__init__(f"argument \"{argName}\" expected {expected} but got {actual}", msg)
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
222
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
223 class DataErr(CustomErr):
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
224 """
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
225 CustomErr subclass for data formatting errors.
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
226 """
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
227 errName = "Data Format Error"
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
228 def __init__(self, fileName :str, msg = "no further details provided") -> None:
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
229 super().__init__(f"file \"{fileName}\" contains malformed data", msg)
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
230
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
231 class PathErr(CustomErr):
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
232 """
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
233 CustomErr subclass for filepath formatting errors.
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
234 """
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
235 errName = "Path Error"
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
236 def __init__(self, path :FilePath, msg = "no further details provided") -> None:
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
237 super().__init__(f"path \"{path}\" is invalid", msg)
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
238
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
239 class ValueErr(CustomErr):
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
240 """
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
241 CustomErr subclass for any value error.
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
242 """
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
243 errName = "Value Error"
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
244 def __init__(self, valueName: str, expected :Any, actual :Any, msg = "no further details provided") -> None:
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
245 super().__init__("value " + f"\"{valueName}\" " * bool(valueName) + f"was supposed to be {expected}, but got {actual} instead", msg)
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
246
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
247 # RESULT
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
248 T = TypeVar('T')
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
249 E = TypeVar('E', bound = CustomErr) # should bind to Result.ResultErr but python happened!
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
250 class Result(Generic[T, E]):
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
251 class ResultErr(CustomErr):
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
252 """
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
253 CustomErr subclass for all Result errors.
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
254 """
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
255 errName = "Result Error"
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
256 def __init__(self, msg = "no further details provided") -> None:
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
257 super().__init__(msg)
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
258 """
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
259 Class to handle the result of an operation, with a value and a boolean flag to indicate
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
260 whether the operation was successful or not.
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
261 """
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
262 def __init__(self, value :Union[T, E], isOk :bool) -> None:
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
263 """
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
264 (Private) Initializes an instance of Result.
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
265
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
266 Args:
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
267 value (Union[T, E]): The value to be stored in the Result instance.
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
268 isOk (bool): A boolean flag to indicate whether the operation was successful or not.
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
269
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
270 Returns:
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
271 None : practically, a Result instance.
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
272 """
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
273 self.isOk = isOk
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
274 self.isErr = not isOk
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
275 self.value = value
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
276
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
277 @classmethod
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
278 def Ok(cls, value :T) -> "Result":
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
279 """
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
280 Constructs a new Result instance with a successful operation.
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
281
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
282 Args:
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
283 value (T): The value to be stored in the Result instance, set as successful.
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
284
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
285 Returns:
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
286 Result: A new Result instance with a successful operation.
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
287 """
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
288 return Result(value, isOk = True)
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
289
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
290 @classmethod
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
291 def Err(cls, value :E) -> "Result":
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
292 """
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
293 Constructs a new Result instance with a failed operation.
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
294
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
295 Args:
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
296 value (E): The value to be stored in the Result instance, set as failed.
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
297
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
298 Returns:
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
299 Result: A new Result instance with a failed operation.
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
300 """
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
301 return Result(value, isOk = False)
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
302
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
303 def unwrap(self) -> T:
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
304 """
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
305 Unwraps the value of the Result instance, if the operation was successful.
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
306
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
307 Raises:
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
308 ResultErr: If the operation was not successful.
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
309
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
310 Returns:
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
311 T: The value of the Result instance, if the operation was successful.
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
312 """
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
313 if self.isOk: return self.value
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
314 raise Result.ResultErr(f"Unwrapped Result.Err : {self.value}")
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
315
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
316 def unwrapOr(self, default :T) -> T:
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
317 """
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
318 Unwraps the value of the Result instance, if the operation was successful, otherwise
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
319 it returns a default value.
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
320
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
321 Args:
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
322 default (T): The default value to be returned if the operation was not successful.
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
323
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
324 Returns:
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
325 T: The value of the Result instance, if the operation was successful,
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
326 otherwise the default value.
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
327 """
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
328 return self.value if self.isOk else default
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
329
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
330 def expect(self, err :"Result.ResultErr") -> T:
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
331 """
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
332 Expects that the value of the Result instance is successful, otherwise it raises an error.
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
333
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
334 Args:
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
335 err (Exception): The error to be raised if the operation was not successful.
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
336
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
337 Raises:
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
338 err: The error raised if the operation was not successful.
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
339
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
340 Returns:
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
341 T: The value of the Result instance, if the operation was successful.
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
342 """
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
343 if self.isOk: return self.value
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
344 raise err
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
345
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
346 U = TypeVar("U")
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
347 def map(self, mapper: Callable[[T], U]) -> "Result[U, E]":
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
348 """
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
349 Maps the value of the current Result to whatever is returned by the mapper function.
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
350 If the Result contained an unsuccessful operation to begin with it remains unchanged
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
351 (a reference to the current instance is returned).
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
352 If the mapper function panics the returned result instance will be of the error kind.
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
353
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
354 Args:
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
355 mapper (Callable[[T], U]): The mapper operation to be applied to the Result value.
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
356
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
357 Returns:
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
358 Result[U, E]: The result of the mapper operation applied to the Result value.
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
359 """
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
360 if self.isErr: return self
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
361 try: return Result.Ok(mapper(self.value))
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
362 except Exception as e: return Result.Err(e)
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
363
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
364 D = TypeVar("D", bound = "Result.ResultErr")
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
365 def mapErr(self, mapper :Callable[[E], D]) -> "Result[T, D]":
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
366 """
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
367 Maps the error of the current Result to whatever is returned by the mapper function.
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
368 If the Result contained a successful operation it remains unchanged
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
369 (a reference to the current instance is returned).
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
370 If the mapper function panics this method does as well.
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
371
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
372 Args:
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
373 mapper (Callable[[E], D]): The mapper operation to be applied to the Result error.
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
374
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
375 Returns:
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
376 Result[U, E]: The result of the mapper operation applied to the Result error.
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
377 """
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
378 if self.isOk: return self
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
379 return Result.Err(mapper(self.value))
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
380
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
381 def __str__(self):
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
382 return f"Result::{'Ok' if self.isOk else 'Err'}({self.value})"
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
383
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
384 # FILES
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
385 def read_dataset(path :FilePath, datasetName = "Dataset (not actual file name!)") -> pd.DataFrame:
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
386 """
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
387 Reads a .csv or .tsv file and returns it as a Pandas DataFrame.
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
388
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
389 Args:
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
390 path : the path to the dataset file.
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
391 datasetName : the name of the dataset.
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
392
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
393 Raises:
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
394 DataErr: If anything goes wrong when trying to open the file, if pandas thinks the dataset is empty or if
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
395 it has less than 2 columns.
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
396
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
397 Returns:
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
398 pandas.DataFrame: The dataset loaded as a Pandas DataFrame.
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
399 """
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
400 # I advise against the use of this function. This is an attempt at standardizing bad legacy code rather than
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
401 # removing / replacing it to avoid introducing as many bugs as possible in the tools still relying on this code.
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
402 # First off, this is not the best way to distinguish between .csv and .tsv files and Galaxy itself makes it really
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
403 # hard to implement anything better. Also, this function's name advertizes it as a dataset-specific operation and
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
404 # contains dubious responsibility (how many columns..) while being a file-opening function instead. My suggestion is
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
405 # TODO: stop using dataframes ever at all in anything and find a way to have tight control over file extensions.
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
406 try: dataset = pd.read_csv(path.show(), sep = '\t', header = None, engine = "python")
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
407 except:
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
408 try: dataset = pd.read_csv(path.show(), sep = ',', header = 0, engine = "python")
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
409 except Exception as err: raise DataErr(datasetName, f"encountered empty or wrongly formatted data: {err}")
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
410
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
411 if len(dataset.columns) < 2: raise DataErr(datasetName, "a dataset is always meant to have at least 2 columns")
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
412 return dataset
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
413
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
414 def readPickle(path :FilePath) -> Any:
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
415 """
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
416 Reads the contents of a .pickle file, which needs to exist at the given path.
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
417
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
418 Args:
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
419 path : the path to the .pickle file.
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
420
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
421 Returns:
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
422 Any : the data inside a pickle file, could be anything.
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
423 """
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
424 with open(path.show(), "rb") as fd: return pickle.load(fd)
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
425
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
426 def writePickle(path :FilePath, data :Any) -> None:
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
427 """
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
428 Saves any data in a .pickle file, created at the given path.
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
429
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
430 Args:
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
431 path : the path to the .pickle file.
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
432 data : the data to be written to the file.
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
433
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
434 Returns:
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
435 None
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
436 """
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
437 with open(path.show(), "wb") as fd: pickle.dump(data, fd)
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
438
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
439 def readCsv(path :FilePath, delimiter = ',', *, skipHeader = True) -> List[List[str]]:
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
440 """
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
441 Reads the contents of a .csv file, which needs to exist at the given path.
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
442
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
443 Args:
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
444 path : the path to the .csv file.
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
445 delimiter : allows other subformats such as .tsv to be opened by the same method (\\t delimiter).
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
446 skipHeader : whether the first row of the file is a header and should be skipped.
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
447
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
448 Returns:
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
449 List[List[str]] : list of rows from the file, each parsed as a list of strings originally separated by commas.
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
450 """
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
451 with open(path.show(), "r", newline = "") as fd: return list(csv.reader(fd, delimiter = delimiter))[skipHeader:]
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
452
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
453 def readSvg(path :FilePath, customErr :Optional[Exception] = None) -> ET.ElementTree:
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
454 """
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
455 Reads the contents of a .svg file, which needs to exist at the given path.
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
456
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
457 Args:
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
458 path : the path to the .svg file.
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
459
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
460 Raises:
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
461 DataErr : if the map is malformed.
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
462
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
463 Returns:
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
464 Any : the data inside a svg file, could be anything.
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
465 """
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
466 try: return ET.parse(path.show())
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
467 except (ET.XMLSyntaxError, ET.XMLSchemaParseError) as err:
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
468 raise customErr if customErr else err
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
469
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
470 def writeSvg(path :FilePath, data:ET.ElementTree) -> None:
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
471 """
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
472 Saves svg data opened with lxml.etree in a .svg file, created at the given path.
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
473
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
474 Args:
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
475 path : the path to the .svg file.
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
476 data : the data to be written to the file.
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
477
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
478 Returns:
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
479 None
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
480 """
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
481 with open(path.show(), "wb") as fd: fd.write(ET.tostring(data))
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
482
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
483 # UI ARGUMENTS
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
484 class Bool:
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
485 def __init__(self, argName :str) -> None:
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
486 self.argName = argName
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
487
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
488 def __call__(self, s :str) -> bool: return self.check(s)
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
489
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
490 def check(self, s :str) -> bool:
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
491 s = s.lower()
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
492 if s == "true" : return True
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
493 if s == "false": return False
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
494 raise ArgsErr(self.argName, "boolean string (true or false, not case sensitive)", f"\"{s}\"")
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
495
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
496 class Float:
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
497 def __init__(self, argName = "Dataset values, not an argument") -> None:
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
498 self.argName = argName
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
499
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
500 def __call__(self, s :str) -> float: return self.check(s)
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
501
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
502 def check(self, s :str) -> float:
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
503 try: return float(s)
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
504 except ValueError:
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
505 s = s.lower()
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
506 if s == "nan" or s == "none": return math.nan
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
507 raise ArgsErr(self.argName, "numeric string or \"None\" or \"NaN\" (not case sensitive)", f"\"{s}\"")
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
508
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
509 # MODELS
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
510 OldRule = List[Union[str, "OldRule"]]
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
511 class Model(Enum):
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
512 """
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
513 Represents a metabolic model, either custom or locally supported. Custom models don't point
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
514 to valid file paths.
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
515 """
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
516
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
517 Recon = "Recon"
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
518 ENGRO2 = "ENGRO2"
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
519 ENGRO2_no_legend = "ENGRO2_no_legend"
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
520 HMRcore = "HMRcore"
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
521 HMRcore_no_legend = "HMRcore_no_legend"
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
522 Custom = "Custom" # Exists as a valid variant in the UI, but doesn't point to valid file paths.
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
523
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
524 def __raiseMissingPathErr(self, path :Optional[FilePath]) -> None:
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
525 if not path: raise PathErr("<<MISSING>>", "it's necessary to provide a custom path when retrieving files from a custom model")
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
526
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
527 def getRules(self, toolDir :str, customPath :Optional[FilePath] = None) -> Dict[str, Dict[str, OldRule]]:
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
528 """
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
529 Open "rules" file for this model.
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
530
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
531 Returns:
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
532 Dict[str, Dict[str, OldRule]] : the rules for this model.
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
533 """
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
534 path = customPath if self is Model.Custom else FilePath(f"{self.name}_rules", FileFormat.PICKLE, prefix = f"{toolDir}/local/pickle files/")
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
535 self.__raiseMissingPathErr(path)
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
536 return readPickle(path)
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
537
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
538 def getTranslator(self, toolDir :str, customPath :Optional[FilePath] = None) -> Dict[str, Dict[str, str]]:
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
539 """
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
540 Open "gene translator (old: gene_in_rule)" file for this model.
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
541
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
542 Returns:
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
543 Dict[str, Dict[str, str]] : the translator dict for this model.
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
544 """
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
545 path = customPath if self is Model.Custom else FilePath(f"{self.name}_genes", FileFormat.PICKLE, prefix = f"{toolDir}/local/pickle files/")
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
546 self.__raiseMissingPathErr(path)
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
547 return readPickle(path)
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
548
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
549 def getMap(self, toolDir = ".", customPath :Optional[FilePath] = None) -> ET.ElementTree:
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
550 path = customPath if self is Model.Custom else FilePath(f"{self.name}_map", FileFormat.SVG, prefix = f"{toolDir}/local/svg metabolic maps/")
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
551 self.__raiseMissingPathErr(path)
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
552 return readSvg(path, customErr = DataErr(path, f"custom map in wrong format"))
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
553
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
554 def getCOBRAmodel(self, toolDir = ".", customPath :Optional[FilePath] = None, customExtension :Optional[FilePath]=None)->cobra.Model:
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
555 if(self is Model.Custom):
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
556 return self.load_custom_model(customPath, customExtension)
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
557 else:
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
558 return cobra.io.read_sbml_model(FilePath(f"{self.name}", FileFormat.XML, prefix = f"{toolDir}/local/models/").show())
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
559
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
560 def load_custom_model(self, file_path :FilePath, ext :Optional[FileFormat] = None) -> cobra.Model:
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
561 ext = ext if ext else file_path.ext
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
562 try:
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
563 if ext is FileFormat.XML:
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
564 return cobra.io.read_sbml_model(file_path.show())
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
565
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
566 if ext is FileFormat.JSON:
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
567 return cobra.io.load_json_model(file_path.show())
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
568
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
569 except Exception as e: raise DataErr(file_path, e.__str__())
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
570 raise DataErr(file_path,
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
571 f"Fomat \"{file_path.ext}\" is not recognized, only JSON and XML files are supported.")
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
572
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
573 def __str__(self) -> str: return self.value