comparison COBRAxy/custom_data_generator.py @ 23:20c30b1a032d draft

Uploaded
author luca_milaz
date Thu, 19 Sep 2024 08:03:27 +0000
parents 41f35c2f0c7b
children 80bfd8743ea6
comparison
equal deleted inserted replaced
22:f3b46a1361ea 23:20c30b1a032d
24 parser = argparse.ArgumentParser( 24 parser = argparse.ArgumentParser(
25 usage = "%(prog)s [options]", 25 usage = "%(prog)s [options]",
26 description = "generate custom data from a given model") 26 description = "generate custom data from a given model")
27 27
28 parser.add_argument("-ol", "--out_log", type = str, required = True, help = "Output log") 28 parser.add_argument("-ol", "--out_log", type = str, required = True, help = "Output log")
29
30 parser.add_argument("-orules", "--out_rules", type = str, required = True, help = "Output rules")
31 parser.add_argument("-orxns", "--out_reactions", type = str, required = True, help = "Output reactions")
32 parser.add_argument("-omedium", "--out_medium", type = str, required = True, help = "Output medium")
33 parser.add_argument("-obnds", "--out_bounds", type = str, required = True, help = "Output bounds")
34
29 parser.add_argument("-id", "--input", type = str, required = True, help = "Input model") 35 parser.add_argument("-id", "--input", type = str, required = True, help = "Input model")
30 parser.add_argument("-mn", "--name", type = str, required = True, help = "Input model name") 36 parser.add_argument("-mn", "--name", type = str, required = True, help = "Input model name")
31 # ^ I need this because galaxy converts my files into .dat but I need to know what extension they were in 37 # ^ I need this because galaxy converts my files into .dat but I need to know what extension they were in
32
33 parser.add_argument(
34 "-of", "--output_format",
35 # vvv I have to use .fromExt because enums in python are the plague and have been implemented by a chimpanzee.
36 type = utils.FileFormat.fromExt, default = utils.FileFormat.PICKLE,
37 choices = [utils.FileFormat.CSV, utils.FileFormat.PICKLE],
38 # ^^^ Not all variants are valid here, otherwise list(utils.FileFormat) would be best.
39 required = True, help = "Extension of all output files")
40 38
41 argsNamespace = parser.parse_args() 39 argsNamespace = parser.parse_args()
42 argsNamespace.out_dir = "result" 40 argsNamespace.out_dir = "result"
43 # ^ can't get this one to work from xml, there doesn't seem to be a way to get the directory attribute from the collection 41 # ^ can't get this one to work from xml, there doesn't seem to be a way to get the directory attribute from the collection
44 42
158 156
159 Returns: 157 Returns:
160 None 158 None
161 """ 159 """
162 with open(file_path.show(), 'w', newline='') as csvfile: 160 with open(file_path.show(), 'w', newline='') as csvfile:
163 writer = csv.DictWriter(csvfile, fieldnames = fieldNames) 161 writer = csv.DictWriter(csvfile, fieldnames = fieldNames, dialect="excel-tab")
164 writer.writeheader() 162 writer.writeheader()
165 163
166 for key, value in data.items(): 164 for key, value in data.items():
167 writer.writerow({ fieldNames[0] : key, fieldNames[1] : value }) 165 writer.writerow({ fieldNames[0] : key, fieldNames[1] : value })
168 166
182 if os.path.isdir(ARGS.out_dir) == False: os.makedirs(ARGS.out_dir) 180 if os.path.isdir(ARGS.out_dir) == False: os.makedirs(ARGS.out_dir)
183 181
184 # load custom model 182 # load custom model
185 model = load_custom_model( 183 model = load_custom_model(
186 utils.FilePath.fromStrPath(ARGS.input), utils.FilePath.fromStrPath(ARGS.name).ext) 184 utils.FilePath.fromStrPath(ARGS.input), utils.FilePath.fromStrPath(ARGS.name).ext)
187 185
188 # generate data and save it in the desired format and in a location galaxy understands 186 rules = generate_rules(model, asParsed = False)
189 # (it should show up as a collection in the history) 187 reactions = generate_reactions(model, asParsed = False)
190 rulesPath = utils.FilePath("rules", ARGS.output_format, prefix = ARGS.out_dir) 188 bounds = generate_bounds(model)
191 reactionsPath = utils.FilePath("reactions", ARGS.output_format, prefix = ARGS.out_dir) 189 medium = get_medium(model)
192 boundsPath = utils.FilePath("bounds", ARGS.output_format, prefix = ARGS.out_dir) 190
193 mediumPath = utils.FilePath("medium", ARGS.output_format, prefix = ARGS.out_dir) 191 rulesPath = utils.FilePath("rules", "csv")
194 192 reactionsPath = utils.FilePath("reactions", "csv")
195 if ARGS.output_format is utils.FileFormat.PICKLE: 193 boundsPath = utils.FilePath("bounds", "csv")
196 rules = generate_rules(model, asParsed = True) 194 mediumPath = utils.FilePath("medium", "csv")
197 reactions = generate_reactions(model, asParsed = True) 195
198 bounds = generate_bounds(model) 196 save_as_csv(rules, rulesPath, ("ReactionID", "Rule"))
199 medium = get_medium(model) 197 save_as_csv(reactions, reactionsPath, ("ReactionID", "Reaction"))
200 utils.writePickle(rulesPath, rules) 198 bounds.to_csv(boundsPath.show(), sep = '\t')
201 utils.writePickle(reactionsPath, reactions) 199 medium.to_csv(mediumPath.show(), sep = '\t')
202 utils.writePickle(boundsPath, bounds)
203 utils.writePickle(mediumPath, medium)
204 bounds.to_pickle(boundsPath.show())
205 medium.to_pickle(mediumPath.show())
206
207 elif ARGS.output_format is utils.FileFormat.CSV:
208 rules = generate_rules(model, asParsed = False)
209 reactions = generate_reactions(model, asParsed = False)
210 bounds = generate_bounds(model)
211 medium = get_medium(model)
212 save_as_csv(rules, rulesPath, ("ReactionID", "Rule"))
213 save_as_csv(reactions, reactionsPath, ("ReactionID", "Reaction"))
214 bounds.to_csv(boundsPath.show())
215 medium.to_csv(mediumPath.show())
216 200
217 201
218 # ^ Please if anyone works on this after updating python to 3.12 change the if/elif into a match statement!! 202 # ^ Please if anyone works on this after updating python to 3.12 change the if/elif into a match statement!!
219 203
220 if __name__ == '__main__': 204 if __name__ == '__main__':