-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathsearch.py
95 lines (72 loc) · 3.71 KB
/
search.py
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
import json
import os
import csv
import re
import GlobalData as GD
def search(term):
project = GD.data["actPro"]
if project != "none":
term = term.replace("\n", "")
results = []
nodes = GD.nodes["nodes"]
for node in nodes:
# search for term in node name
if "n" in node:
nodename = node["n"]
if term.lower() in nodename.lower():
res = {"id": node["id"], "name": node["n"], "color": GD.pixel_valuesc[node["id"]] }
results.append(res)
#break
# search for term in attributes
if "attrlist" in node:
# check if attribute structure is a list or a dict
if isinstance(node["attrlist"], list):
for attr in node["attrlist"]:
if term.lower() in attr.lower() or attr.lower() in term.lower() or re.search(r'\b'+term.lower()+r'\b', attr.lower()):
res = {"id": node["id"], "name": node["n"], "color": GD.pixel_valuesc[node["id"]] }
results.append(res)
#break
if isinstance(node["attrlist"], dict):
for key,value in node["attrlist"].items():
if isinstance(value, list):
for v in value:
if term.lower() in v.lower() or v.lower() in term.lower() or re.search(r'\b'+term.lower()+r'\b', v.lower()):
res = {"id": node["id"], "name": node["n"], "color": GD.pixel_valuesc[node["id"]] }
results.append(res)
#break
elif isinstance(value, str):
if term.lower() in value.lower():
res = {"id": node["id"], "name": node["n"], "color": GD.pixel_valuesc[node["id"]] }
results.append(res)
#break
# make sure no duplicates are in the results
results = [dict(t) for t in {tuple(d.items()) for d in results}]
return results
def get_structure_scale(uniprot, mode) -> float or str:
"""Return the scale of the structure as a float. If the structure is not found (or not provided), the size file is not available or the mode is not given, the function will return an error message as string. To provide the UniProtID add the 'uniprot=<UniProtID>', for the mode add 'mode=<mode>' to the URL. Currently available modes are 'cartoon' and 'electrostatic'. The default mode is 'cartoon'."""
if mode is None:
print("Error: No mode provided. Will use default mode 'cartoon'.")
mode = "cartoon"
if uniprot is None:
return "Error: No UniProtID provided."
possible_files = {
"cartoon": os.path.join(".", "static", "example_files", "protein_structure_info", "scales_Cartoon.csv"),
"electrostatic": os.path.join(
".", "static", "example_files", "protein_structure_info", "scales_electrostatic_surface.csv"
),
}
scale_file = possible_files.get(mode)
# Prevent FileNotFound errors.
if scale_file is None:
return "Error: Mode not available."
if not os.path.exists(scale_file):
return "Error: File not found."
# Search for size of structure.
with open(scale_file, "r") as f:
csv_file = csv.reader(f)
for row in csv_file:
if row[0] == uniprot:
scale = row[1]
return scale
# Structure not found in the scale file -> no available.
return "Error: No structure available for this UniProtID."