asthara commited on
Commit
bc0cb2b
·
1 Parent(s): b8d1fb2

add outputs

Browse files
Files changed (2) hide show
  1. main.py +56 -27
  2. tools/meta_yml_tools.py +44 -0
main.py CHANGED
@@ -1,5 +1,5 @@
1
  import gradio as gr
2
- from tools.meta_yml_tools import get_meta_yml_file, extract_tools_from_meta_json, extract_information_from_meta_json, extract_module_name_description
3
  from tools.bio_tools_tools import get_biotools_response, get_biotools_ontology
4
  from agents.query_ontology_db import agent
5
  import yaml
@@ -90,7 +90,7 @@ def extract_format_terms_from_result(result):
90
  return [item for item in result if isinstance(item, str) and item.startswith('format_')]
91
  return []
92
 
93
- def create_progress_bar_html(progress, status, current_input, current_count=0, total_count=0):
94
  """Create an animated progress bar HTML with nf-core styling"""
95
 
96
  # Determine progress bar color based on status
@@ -150,7 +150,10 @@ def format_ontology_results_html(results, meta_yml):
150
  if value.get("type") == "file":
151
  input_info[key] = value.get("description", "No description available")
152
 
153
- for input_name, result in results["input"].items():
 
 
 
154
  format_terms = extract_format_terms_from_result(result)
155
  description = input_info.get(input_name, "No description available")
156
 
@@ -165,7 +168,6 @@ def format_ontology_results_html(results, meta_yml):
165
 
166
  if format_terms:
167
  for term in format_terms:
168
- term_id = term.replace("format_", "")
169
  link_url = f"http://edamontology.org/{term}"
170
  html_content += f"""
171
  <div class='ontology-card'>
@@ -216,50 +218,77 @@ def run_multi_agent_with_logs(module_name, progress_callback=None):
216
  time.sleep(0.5)
217
 
218
  ### FETCH ONTOLOGY TERMS FROM EDAM DATABASE ###
219
- # Count total file inputs
220
- total_file_inputs = 0
221
- file_inputs = []
222
  for input_channel in meta_yml.get("input", []):
223
  for ch_element in input_channel:
224
  for key, value in ch_element.items():
225
  if value.get("type") == "file":
226
- total_file_inputs += 1
227
- file_inputs.append((key, value))
228
-
229
- if total_file_inputs == 0:
 
 
 
 
 
 
 
230
  if progress_callback:
231
  progress_callback(100, "No file inputs found", "", 0, 0)
232
  formatted_results = format_ontology_results_html(results, meta_yml)
233
  return formatted_results, "tmp_meta.yml"
 
 
234
 
235
- current_input = 0
236
-
237
  for input_channel in meta_yml["input"]:
238
  for ch_element in input_channel:
239
  for key, value in ch_element.items():
240
  if value["type"] == "file":
241
  # Update progress BEFORE processing starts for this input
242
  if progress_callback:
243
- progress_callback(int((current_input / total_file_inputs) * 100), f"Starting analysis of input: {key}", key, current_input, total_file_inputs)
244
 
245
  # This is where the agent runs - logs should be captured automatically
246
  result = agent.run(f"You are presentend with a file format for the input {key}, which is a file and is described by the following description: '{value['description']}', search for the best matches out of possible matches in the edam ontology (formated as format_XXXX), and return the answer (a list of ontology classes) in a final_answer call such as final_answer([format_XXXX, format_XXXX, ...])")
247
  results["input"][key] = result
248
-
249
- format_terms = extract_format_terms_from_result(result)
250
-
251
  # Update progress AFTER processing completes for this input
252
- current_input += 1
253
- progress = int((current_input / total_file_inputs) * 100)
254
  if progress_callback:
255
- progress_callback(progress, f"Completed analysis of input: {key}", key, current_input, total_file_inputs)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
256
 
257
  if progress_callback:
258
- progress_callback(100, "Analysis complete! Generating results...", "", total_file_inputs, total_file_inputs)
259
 
260
  ### UPDATE META.YML FILE ADDING ONTOLOGIES AND RETURN THE ANSWER ###
261
  with open("tmp_meta.yml", "w") as fh:
262
- yaml.dump(meta_yml, fh)
 
263
 
264
  except Exception as e:
265
  if progress_callback:
@@ -276,12 +305,12 @@ def stream_logs_and_run_agent(module_name):
276
 
277
  # Start the agent in a separate thread
278
  result_container = {"ontology_output": None, "file_output": None, "error": None}
279
- progress_container = {"progress": 0, "status": "Initializing...", "current_input": "", "current_count": 0, "total_count": 0}
280
 
281
- def progress_callback(progress, status, current_input, current_count=0, total_count=0):
282
  progress_container["progress"] = progress
283
  progress_container["status"] = status
284
- progress_container["current_input"] = current_input
285
  progress_container["current_count"] = current_count
286
  progress_container["total_count"] = total_count
287
 
@@ -314,7 +343,7 @@ def stream_logs_and_run_agent(module_name):
314
  progress_html = create_progress_bar_html(
315
  progress_container["progress"],
316
  progress_container["status"],
317
- progress_container["current_input"],
318
  progress_container["current_count"],
319
  progress_container["total_count"]
320
  )
@@ -329,7 +358,7 @@ def stream_logs_and_run_agent(module_name):
329
  progress_html = create_progress_bar_html(
330
  progress_container["progress"],
331
  progress_container["status"],
332
- progress_container["current_input"],
333
  progress_container["current_count"],
334
  progress_container["total_count"]
335
  )
 
1
  import gradio as gr
2
+ from tools.meta_yml_tools import get_meta_yml_file, extract_tools_from_meta_json, extract_information_from_meta_json, extract_module_name_description, update_meta_yml
3
  from tools.bio_tools_tools import get_biotools_response, get_biotools_ontology
4
  from agents.query_ontology_db import agent
5
  import yaml
 
90
  return [item for item in result if isinstance(item, str) and item.startswith('format_')]
91
  return []
92
 
93
+ def create_progress_bar_html(progress, status, current_files, current_count=0, total_count=0):
94
  """Create an animated progress bar HTML with nf-core styling"""
95
 
96
  # Determine progress bar color based on status
 
150
  if value.get("type") == "file":
151
  input_info[key] = value.get("description", "No description available")
152
 
153
+ final = {}
154
+ final.update(results["input"])
155
+ final.update(results["output"])
156
+ for input_name, result in final.items():
157
  format_terms = extract_format_terms_from_result(result)
158
  description = input_info.get(input_name, "No description available")
159
 
 
168
 
169
  if format_terms:
170
  for term in format_terms:
 
171
  link_url = f"http://edamontology.org/{term}"
172
  html_content += f"""
173
  <div class='ontology-card'>
 
218
  time.sleep(0.5)
219
 
220
  ### FETCH ONTOLOGY TERMS FROM EDAM DATABASE ###
221
+ # Count total file inputs + outputs
222
+ total_files = 0
223
+ file = []
224
  for input_channel in meta_yml.get("input", []):
225
  for ch_element in input_channel:
226
  for key, value in ch_element.items():
227
  if value.get("type") == "file":
228
+ total_files += 1
229
+ file.append((key, value))
230
+ for output in meta_yml.get("output", []):
231
+ for key, output_channel in output.items():
232
+ for out_element in output_channel:
233
+ for element_name, value in out_element.items():
234
+ if value["type"] == "file" and element_name != "versions.yml":
235
+ total_files += 1
236
+ file.append((key, value))
237
+
238
+ if total_files == 0:
239
  if progress_callback:
240
  progress_callback(100, "No file inputs found", "", 0, 0)
241
  formatted_results = format_ontology_results_html(results, meta_yml)
242
  return formatted_results, "tmp_meta.yml"
243
+
244
+ current_files = 0
245
 
246
+ #inputs
 
247
  for input_channel in meta_yml["input"]:
248
  for ch_element in input_channel:
249
  for key, value in ch_element.items():
250
  if value["type"] == "file":
251
  # Update progress BEFORE processing starts for this input
252
  if progress_callback:
253
+ progress_callback(int((current_files / total_files) * 100), f"Starting analysis of input: {key}", key, current_files, total_files)
254
 
255
  # This is where the agent runs - logs should be captured automatically
256
  result = agent.run(f"You are presentend with a file format for the input {key}, which is a file and is described by the following description: '{value['description']}', search for the best matches out of possible matches in the edam ontology (formated as format_XXXX), and return the answer (a list of ontology classes) in a final_answer call such as final_answer([format_XXXX, format_XXXX, ...])")
257
  results["input"][key] = result
258
+
 
 
259
  # Update progress AFTER processing completes for this input
260
+ current_files += 1
261
+ progress = int((current_files / total_files) * 100)
262
  if progress_callback:
263
+ progress_callback(progress, f"Completed analysis of input: {key}", key, current_files, total_files)
264
+
265
+ #outputs
266
+ for output in meta_yml.get("output", []):
267
+ for key, output_channel in output.items():
268
+ for out_element in output_channel:
269
+ for element_name, value in out_element.items():
270
+ if value["type"] == "file" and element_name != "versions.yml":
271
+ # Update progress BEFORE processing starts for this output
272
+ if progress_callback:
273
+ progress_callback(int((current_files / total_files) * 100), f"Starting analysis of output: {key}", key, current_files, total_files)
274
+
275
+ # This is where the agent runs - logs should be captured automatically
276
+ result = agent.run(f"You are presentend with a file format for the output '{element_name}', which is a file and is described by the following description: '{value['description']}', search for the best matches out of possible matches in the edam ontology (formated as format_XXXX), and return the answer (a list of ontology classes) in a final_answer call such as final_answer([format_XXXX, format_XXXX, ...]). The output name {key} can also give you more information.")
277
+ results["output"][key] = result
278
+
279
+ # Update progress AFTER processing completes for this output
280
+ current_files += 1
281
+ progress = int((current_files / total_files) * 100)
282
+ if progress_callback:
283
+ progress_callback(progress, f"Completed analysis of output: {key}", key, current_files, total_files)
284
 
285
  if progress_callback:
286
+ progress_callback(100, "Analysis complete! Generating results...", "", total_files, total_files)
287
 
288
  ### UPDATE META.YML FILE ADDING ONTOLOGIES AND RETURN THE ANSWER ###
289
  with open("tmp_meta.yml", "w") as fh:
290
+ updated_meta_yml = update_meta_yml(results["input"], results["output"], meta_yml)
291
+ yaml.dump(updated_meta_yml, fh)
292
 
293
  except Exception as e:
294
  if progress_callback:
 
305
 
306
  # Start the agent in a separate thread
307
  result_container = {"ontology_output": None, "file_output": None, "error": None}
308
+ progress_container = {"progress": 0, "status": "Initializing...", "current_files": "", "current_count": 0, "total_count": 0}
309
 
310
+ def progress_callback(progress, status, current_files, current_count=0, total_count=0):
311
  progress_container["progress"] = progress
312
  progress_container["status"] = status
313
+ progress_container["current_files"] = current_files
314
  progress_container["current_count"] = current_count
315
  progress_container["total_count"] = total_count
316
 
 
343
  progress_html = create_progress_bar_html(
344
  progress_container["progress"],
345
  progress_container["status"],
346
+ progress_container["current_files"],
347
  progress_container["current_count"],
348
  progress_container["total_count"]
349
  )
 
358
  progress_html = create_progress_bar_html(
359
  progress_container["progress"],
360
  progress_container["status"],
361
+ progress_container["current_files"],
362
  progress_container["current_count"],
363
  progress_container["total_count"]
364
  )
tools/meta_yml_tools.py CHANGED
@@ -104,3 +104,47 @@ def extract_information_from_meta_json(meta_file: dict, tool_name: str) -> dict:
104
  print("Extracted metadata information from nf-core module meta.yml")
105
  return {"inputs": inputs, "outputs": outputs, "homepage": homepage_url, "documentation": documentation_rul, "bio_tools_id": bio_tools_id}
106
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
104
  print("Extracted metadata information from nf-core module meta.yml")
105
  return {"inputs": inputs, "outputs": outputs, "homepage": homepage_url, "documentation": documentation_rul, "bio_tools_id": bio_tools_id}
106
 
107
+ def update_meta_yml(input_ontologies: dict, output_ontologies: dict, meta_yml:dict) -> dict:
108
+ """
109
+ Update the meta.yml file with the final obtained ontologies
110
+ Args:
111
+ input_ontologies (dict): The final ontologies for inputs.
112
+ The dictionary contains the name of the file as key and a list of ontologies as value.
113
+ output_ontologies (dict): The final ontologies for outputs.
114
+ The dictionary contains the name of the file as key and a list of ontologies as value.
115
+ meta_yml (dict): The original meta.yml file content to be modified
116
+ Returns:
117
+ (dict): The updated meta.yml file
118
+ """
119
+ # Format ontology links
120
+ def format_ontology_links(ontology_dict):
121
+ for key in ontology_dict.keys():
122
+ updated_list = []
123
+ for format in ontology_dict[key]:
124
+ updated_list.append({"edam": f"http://edamontology.org/{format}"})
125
+ ontology_dict[key] = updated_list
126
+ return ontology_dict
127
+ input_ontologies = format_ontology_links(input_ontologies)
128
+ output_ontologies = format_ontology_links(output_ontologies)
129
+
130
+ # inputs
131
+ for i, input_ch in enumerate(meta_yml["input"]):
132
+ for j, ch_element in enumerate(input_ch):
133
+ for key, value in ch_element.items():
134
+ if key in input_ontologies:
135
+ try:
136
+ meta_yml["input"][i][j][key]["ontologies"].append(input_ontologies[key])
137
+ except KeyError:
138
+ meta_yml["input"][i][j][key]["ontologies"] = input_ontologies[key]
139
+ # outputs
140
+ for i, output in enumerate(meta_yml["output"]):
141
+ for key, output_channel in output.items():
142
+ for j, out_element in enumerate(output_channel):
143
+ for element_name, value in out_element.items():
144
+ if element_name in output_ontologies:
145
+ try:
146
+ meta_yml["output"][i][key][j][element_name]["ontologies"].append(output_ontologies[element_name])
147
+ except KeyError:
148
+ meta_yml["output"][i][key][j][element_name]["ontologies"] = output_ontologies[element_name]
149
+
150
+ return meta_yml