-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsuggestion.py
More file actions
97 lines (79 loc) · 3.69 KB
/
Copy pathsuggestion.py
File metadata and controls
97 lines (79 loc) · 3.69 KB
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
import sys
import os
import json
import urllib.request
import urllib.error
def get_biotools_data(tool_id):
"""Fetches raw tool metadata from the bio.tools API."""
url = f"https://bio.tools/api/tool/{tool_id}?format=json"
print(f"Fetching metadata for '{tool_id}' from bio.tools...")
try:
with urllib.request.urlopen(url) as response:
if response.status == 200:
return json.loads(response.read().decode())
except urllib.error.HTTPError as e:
print(f"Error fetching data from bio.tools: {e}", file=sys.stderr)
sys.exit(1)
except urllib.error.URLError as e:
print(f"Network error: {e}", file=sys.stderr)
sys.exit(1)
def load_valid_operations(obo_path):
"""Reads the clean OBO file and collects all present operation codes."""
valid_ops = set()
if not os.path.exists(obo_path):
print(f"Warning: '{obo_path}' not found. All codes will be marked as 'unknown code'.")
return valid_ops
with open(obo_path, "r", encoding="utf-8") as f:
for line in f:
if line.strip().startswith("id:"):
# Extracts "operation_XXXX" from lines like "id: operation_XXXX" or URIs
raw_id = line.split("id:")[1].strip()
clean_id = raw_id.split("/")[-1]
valid_ops.add(clean_id)
return valid_ops
def parse_operations(data, tool_id, valid_ops):
"""Parses JSON payload, validates IDs against the OBO file, and formats lines."""
formatted_operations = []
functions = data.get("function", [])
for func in functions:
for op in func.get("operation", []):
term_name = op.get("term")
uri = op.get("uri", "")
if term_name and uri:
# Extract the operation ID from the URI
operation_id = uri.split("/")[-1]
# Convert operation_XXXX to DIO:000XXXX to match the new OBO format
if "operation_" in operation_id:
num = operation_id.split("operation_")[1]
dio_id = f"DIO:{num.zfill(7)}"
else:
dio_id = operation_id
# Check if the operation code exists in our clean OBO set
if dio_id in valid_ops:
formatted_line = f'"{term_name}": {dio_id}\t{tool_id}'
else:
# Append 'unknown code' after the program name if missing
formatted_line = f'"{term_name}": {dio_id}\t{tool_id} unknown code'
if formatted_line not in formatted_operations:
formatted_operations.append(formatted_line)
return sorted(formatted_operations)
def main():
if len(sys.argv) != 2:
print("Usage: python3 script.py <name_of_program>", file=sys.stderr)
sys.exit(1)
tool_id = sys.argv[1]
obo_file_path = "/data/dio.obo" # Points to the finalized DIO OBO file
# 1. Load existing valid operations from the clean OBO file
valid_ops = load_valid_operations(obo_file_path)
# 2. Fetch & Parse data from bio.tools
raw_data = get_biotools_data(tool_id)
operations = parse_operations(raw_data, tool_id, valid_ops)
# 3. Save formatted classifications to the designated file (append mode)
output_filename = "/data/software_classification_suggestion"
print(f"Adding classification suggestions to '{output_filename}'...")
with open(output_filename, "a", encoding="utf-8") as out:
for op in operations:
out.write(op + "\n")
print("Execution complete. Data saved successfully.")
if __name__ == "__main__":
main()