1
2
3
4
5
6
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
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
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
186
187
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
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
|
- """
- Classes that are used to configure workflow
- Classes:
- Enums
- Locations
- """
- from typing import *
- from enum import Enum, auto
- from yspecies.dataset import ExpressionDataset
- from yspecies.preprocess import FeatureSelection
- from dataclasses import *
- class Normalize(Enum):
- log2 = "log2"
- standardize = "standardize"
- clr = "clr"
- class AnimalClass(Enum):
- Mammalia = "Mammalia"
- mammals = "Mammalia"
- Aves = "Aves"
- birds = "Aves"
- Reptilia = "Reptilia"
- reptiles = "Reptilia"
- Coelacanthi = "Coelacanthi"
- Teleostei = "Teleostei"
- bone_fish = "Teleostei"
- @staticmethod
- def tsv():
- return [cl.name.capitalize()+".tsv" for cl in AnimalClass]
- class Orthology(Enum):
- one2one = "one2one"
- one2many = "one2many"
- one2many_directed = "one2many_directed"
- one2oneplus_directed = "one2oneplus_directed"
- many2many = "many2many"
- all = "all"
- class CleaningTarget(Enum):
- expressions = "expressions"
- genes = "genes"
- from pathlib import Path
- class Locations:
- @property
- def logs(self):
- return self.dir / "logs"
- class Genes:
- def __init__(self, base: Path):
- self.dir: Path = base
- self.genes = self.dir
- self.by_class = self.dir / "by_animal_class"
- self.all = self.dir / "all"
- self.genes_meta = self.dir / "reference_genes.tsv"
- class Expressions:
- def __init__(self, base: Path):
- self.dir = base
- self.expressions = self.dir
- self.by_class: Path = self.dir / "by_animal_class"
- class Input:
- class Annotations:
- class Genage:
- def __init__(self, base: Path):
- self.dir = base
- self.orthologs = Locations.Genes(base / "genage_orthologs")
- self.conversion = self.dir / "genage_conversion.tsv"
- self.human = self.dir / "genage_human.tsv"
- self.models = self.dir / "genage_models.tsv"
- def __init__(self, base: Path):
- self.dir = base
- self.genage = Locations.Input.Annotations.Genage(self.dir / "genage")
- def __init__(self, base: Path):
- self.dir = base
- self.intput = self.dir
- self.genes: Locations.Genes = Locations.Genes(self.dir / "genes")
- self.expressions: Locations.Expressions = Locations.Expressions(self.dir / "expressions")
- self.species = self.dir / "species.tsv"
- self.samples = self.dir / "samples.tsv"
- self.annotations = Locations.Input.Annotations(self.dir / "annotations")
- class Interim:
- def stage(self, num: str or int):
- if num == "2" or num == "_2" or num == 2 or num == "two":
- return self.stage_two
- elif num == "3" or num == "_3" or num == 3 or num == "three":
- return self.stage_three
- else:
- return self.stage_one
- def __init__(self, base: Path):
- self.dir = base
- self.selected = self.dir / "selected"
- self.optimization = self.dir / "optimization"
- self.stage_one = self.dir / "stage_1"
- self.stage_two = self.dir / "stage_2"
- self.stage_three = self.dir / "stage_3"
- class Metrics:
- def __init__(self, base: Path):
- self.dir = base
- self.optimization = self.dir / "optimization"
- class Output:
- class External:
- def __init__(self, base: Path):
- self.dir: Path = base
- self.linear = self.dir / "linear"
- self.shap = self.dir / "shap"
- self.causal = self.dir / "causal"
- def __init__(self, base: Path):
- self.dir = base
- self.external = Locations.Output.External(self.dir / "external")
- self.intersections = self.dir / "intersections"
- self.stage_one = self.dir / "stage_1"
- self.stage_two = self.dir / "stage_2"
- self.plots = self.dir / "plots"
- def __init__(self, base: str):
- self.base: Path = Path(base)
- self.data: Path = self.base / "data"
- self.dir: Path = self.base / "data"
- self.input: Locations.Input = Locations.Input(self.dir / "input")
- self.interim: Locations.Interim = Locations.Interim(self.dir / "interim")
- self.metrics: Locations.Metrics = Locations.Metrics(self.dir / "metrics")
- self.output: Locations.Output = Locations.Output(self.dir / "output")
- class Parameters(Enum):
- lifespan = {"objective": "regression",
- 'boosting_type': 'gbdt',
- 'lambda_l1': 2.649670285109348,
- 'lambda_l2': 3.651743005278647,
- 'max_leaves': 21,
- 'max_depth': 3,
- 'feature_fraction': 0.7381836300988616,
- 'bagging_fraction': 0.5287709904685758,
- 'learning_rate': 0.054438364299744225,
- 'min_data_in_leaf': 7,
- 'drop_rate': 0.13171689004108006,
- 'metric': ['mae','mse', 'huber'],
- }
- mass_g = {"objective": "regression",
- 'boosting_type': 'gbdt',
- 'lambda_l1': 2.649670285109348,
- 'lambda_l2': 3.651743005278647,
- 'max_leaves': 21,
- 'max_depth': 3,
- 'feature_fraction': 0.7381836300988616,
- 'bagging_fraction': 0.5287709904685758,
- 'learning_rate': 0.054438364299744225,
- 'min_data_in_leaf': 7,
- 'drop_rate': 0.13171689004108006,
- 'metric': ['mae','mse', 'huber'],
- }
- mtGC = {"objective": "regression",
- 'boosting_type': 'gbdt',
- 'lambda_l1': 2.649670285109348,
- 'lambda_l2': 3.651743005278647,
- 'max_leaves': 21,
- 'max_depth': 3,
- 'feature_fraction': 0.7381836300988616,
- 'bagging_fraction': 0.5287709904685758,
- 'learning_rate': 0.054438364299744225,
- 'min_data_in_leaf': 7,
- 'drop_rate': 0.13171689004108006,
- 'metric': ['mae','mse', 'huber'],
- }
- temperature = {"objective": "regression",
- 'boosting_type': 'gbdt',
- 'lambda_l1': 2.649670285109348,
- 'lambda_l2': 3.651743005278647,
- 'max_leaves': 21,
- 'max_depth': 3,
- 'feature_fraction': 0.7381836300988616,
- 'bagging_fraction': 0.5287709904685758,
- 'learning_rate': 0.054438364299744225,
- 'min_data_in_leaf': 7,
- 'drop_rate': 0.13171689004108006,
- 'metric': ['mae','mse', 'huber'],
- }
- gestation = {"objective": "regression",
- 'boosting_type': 'gbdt',
- 'lambda_l1': 2.649670285109348,
- 'lambda_l2': 3.651743005278647,
- 'max_leaves': 21,
- 'max_depth': 3,
- 'feature_fraction': 0.7381836300988616,
- 'bagging_fraction': 0.5287709904685758,
- 'learning_rate': 0.054438364299744225,
- 'min_data_in_leaf': 7,
- 'drop_rate': 0.13171689004108006,
- 'metric': ['mae','mse', 'huber'],
- }
- metabolic_rate = {"objective": "regression",
- 'boosting_type': 'gbdt',
- 'lambda_l1': 2.649670285109348,
- 'lambda_l2': 3.651743005278647,
- 'max_leaves': 21,
- 'max_depth': 3,
- 'feature_fraction': 0.7381836300988616,
- 'bagging_fraction': 0.5287709904685758,
- 'learning_rate': 0.054438364299744225,
- 'min_data_in_leaf': 7,
- 'drop_rate': 0.13171689004108006,
- 'metric': ['mae','mse', 'huber'],
- }
- @dataclass(frozen=True)
- class DataLoader:
- locations: Locations
- selection: FeatureSelection
- def load_life_history(self,
- life_history: List[str]=["lifespan", "mass_kg", "mtGC", "metabolic_rate", "temperature", "gestation_days"],
- exclude_min_max: bool = True
- ) -> Dict[str, Tuple[ExpressionDataset, FeatureSelection]]:
- return OrderedDict([(trait, self.load_trait(trait)) for trait in life_history])
- def load_trait(self, trait: str, protected_species: Union[bool, List[str]] = True) -> Tuple[ExpressionDataset, FeatureSelection]:
- f = replace(self.selection, to_predict = trait)
- data = ExpressionDataset.from_folder(self.locations.interim.selected / trait)
- if isinstance(protected_species, List):
- return (data, replace(f, not_validated_species = protected_species))
- elif protected_species:
- return (data, replace(f, not_validated_species = data.min_max_trait(trait)))
- return (data, f)
- import optuna
- @dataclass
- class StudyLoader:
- locations: Locations
- metrics_to_improve = OrderedDict()
- def load_study(self, trait: str, study_filename: str = None) -> optuna.multi_objective.study.MultiObjectiveStudy:
- study_filename = trait if study_filename is None else study_filename
- url = f'sqlite:///' +str((self.locations.interim.optimization / (study_filename+".sqlite")).absolute())
- print('loading (if exists) study from '+url)
- storage = optuna.storages.RDBStorage(
- url=url
- #engine_kwargs={'check_same_thread': False}
- )
- return optuna.multi_objective.study.create_study(directions=['maximize','minimize','maximize'], storage = storage, study_name = f"{trait}_r2_huber_kendall", load_if_exists = True)
|