This repository has been archived by the owner on Jul 13, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 52
/
queries.py
163 lines (130 loc) · 6.94 KB
/
queries.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
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
from grakn.client import GraknClient
from util import print_to_log
# Which Homo Sepiens protein sequences are aligned with the sequence MNVGTAHSEVNPNTRVMNSRGIWLSYVLAIGLLHIVLLSIPFVSVPVVWTLTNLIHNMGMYIFLHTVKGTPFETPDQGKARLLTHWEQMDYGVQFTASRKFLTITPIVLYFLTSFYTKYDQIHFVLNTVSLMSVLIPKLPQLHGVRIFGINKY?
def execute_query_1(question, transaction):
print_to_log("Question: ", question)
query = [
'match',
' $t-pr isa protein, has sequence $t-seq;',
' $t-seq == "MNVGTAHSEVNPNTRVMNSRGIWLSYVLAIGLLHIVLLSIPFVSVPVVWTLTNLIHNMGMYIFLHTVKGTPFETPDQGKARLLTHWEQMDYGVQFTASRKFLTITPIVLYFLTSFYTKYDQIHFVLNTVSLMSVLIPKLPQLHGVRIFGINKY";'
' $alignment (target-sequence: $t-seq, matched-sequence: $m-seq) isa sequence-sequence-alignment;',
' $alignment has sequence-identicality $ident, has sequence-positivity $pos, has sequence-midline $midline;',
' $species isa species, has name "Fukomys damarensis";',
' $ownership (owned-protein: $m-pr, species-owner: $species) isa protein-ownership;',
'get $m-seq, $midline, $ident, $pos;'
]
print_to_log("Query:", "\n".join(query))
query = "".join(query)
answers = transaction.query(query)
result = []
for structured_answer in answers:
var_map = structured_answer.map()
var_value_dict = {
var_name: var_map[var_name].value() for var_name in var_map
}
result.append(var_value_dict)
print_to_log("Result:", result, pretty=True)
return result
# Which proteins sequences are aligned with the sequence MNVGTAHSEVNPNTRVMNSRGIWLSYVLAIGLLHIVLLSIPFVSVPVVWTLTNLIHNMGMYIFLHTVKGTPFETPDQGKARLLTHWEQMDYGVQFTASRKFLTITPIVLYFLTSFYTKYDQIHFVLNTVSLMSVLIPKLPQLHGVRIFGINKY and have an identicality of at lease 0.9 and a positivity of at least 0.85?
def execute_query_2(question, transaction):
print_to_log("Question: ", question)
query = [
'match',
' $t-pr isa protein, has sequence $t-seq;',
' $t-seq == "MNVGTAHSEVNPNTRVMNSRGIWLSYVLAIGLLHIVLLSIPFVSVPVVWTLTNLIHNMGMYIFLHTVKGTPFETPDQGKARLLTHWEQMDYGVQFTASRKFLTITPIVLYFLTSFYTKYDQIHFVLNTVSLMSVLIPKLPQLHGVRIFGINKY";'
' $alignment (target-sequence: $t-seq, matched-sequence: $m-seq) isa sequence-sequence-alignment;',
' $alignment has sequence-identicality $ident, has sequence-positivity $pos, has sequence-midline $midline;',
' $ident >= 0.8; $pos >= 0.85;',
'get $m-seq, $midline, $ident, $pos;'
]
print_to_log("Query:", "\n".join(query))
query = "".join(query)
answers = transaction.query(query)
result = []
for structured_answer in answers:
var_map = structured_answer.map()
var_value_dict = {
var_name: var_map[var_name].value() for var_name in var_map
}
result.append(var_value_dict)
print_to_log("Result:", result, pretty=True)
return result
# Which alignments with sequence MNVGTAHSEVNPNTRVMNSRGIWLSYVLAIGLLHIVLLSIPFVSVPVVWTLTNLIHNMGMYIFLHTVKGTPFETPDQGKARLLTHWEQMDYGVQFTASRKFLTITPIVLYFLTSFYTKYDQIHFVLNTVSLMSVLIPKLPQLHGVRIFGINKY contain the subset GIGLLHII?
def execute_query_3(question, transaction):
print_to_log("Question: ", question)
query = [
'match',
' $t-pr isa protein, has sequence $t-seq;',
' $t-seq == "MNVGTAHSEVNPNTRVMNSRGIWLSYVLAIGLLHIVLLSIPFVSVPVVWTLTNLIHNMGMYIFLHTVKGTPFETPDQGKARLLTHWEQMDYGVQFTASRKFLTITPIVLYFLTSFYTKYDQIHFVLNTVSLMSVLIPKLPQLHGVRIFGINKY";'
' $alignment (target-sequence: $t-seq, matched-sequence: $m-seq) isa sequence-sequence-alignment;',
' $alignment has sequence-identicality $ident, has sequence-positivity $pos, has sequence-midline $midline;',
' $m-seq contains "GIGLLHII";',
'get $m-seq, $midline, $ident, $pos;'
]
print_to_log("Query:", "\n".join(query))
query = "".join(query)
answers = transaction.query(query)
result = []
for structured_answer in answers:
var_map = structured_answer.map()
var_value_dict = {
var_name: var_map[var_name].value() for var_name in var_map
}
result.append(var_value_dict)
print_to_log("Result:", result, pretty=True)
return result
def execute_query_all(transaction):
for query_example in query_examples:
question = query_example["question"]
query_function = query_example["query_function"]
query_function(question, transaction)
print("\n - - - - - - - - - - - - \n")
query_examples = [
{
"question": "Which Homo Sepiens protein sequences are aligned with the sequence MNVGTAHSEVNPNTRVMNSRGIWLSYVLAIGLLHIVLLSIPFVSVPVVWTLTNLIHNMGMYIFLHTVKGTPFETPDQGKARLLTHWEQMDYGVQFTASRKFLTITPIVLYFLTSFYTKYDQIHFVLNTVSLMSVLIPKLPQLHGVRIFGINKY?",
"query_function": execute_query_1
},
{
"question": "Which proteins sequences are aligned with the sequence MNVGTAHSEVNPNTRVMNSRGIWLSYVLAIGLLHIVLLSIPFVSVPVVWTLTNLIHNMGMYIFLHTVKGTPFETPDQGKARLLTHWEQMDYGVQFTASRKFLTITPIVLYFLTSFYTKYDQIHFVLNTVSLMSVLIPKLPQLHGVRIFGINKY and have an identicality of at lease 0.8 and a positivity of at least 0.85?",
"query_function": execute_query_2
},
{
"question": "Which alignments with the sequence MNVGTAHSEVNPNTRVMNSRGIWLSYVLAIGLLHIVLLSIPFVSVPVVWTLTNLIHNMGMYIFLHTVKGTPFETPDQGKARLLTHWEQMDYGVQFTASRKFLTITPIVLYFLTSFYTKYDQIHFVLNTVSLMSVLIPKLPQLHGVRIFGINKY contain the subset GIGLLHII?",
"query_function": execute_query_3
}
]
if __name__ == "__main__":
"""
The code below:
- gets user's selection wrt the queries to be executed
- creates a Grakn client > session > transaction connected to the phone_calls keyspace
- runs the right function based on the user's selection
- closes the session
"""
# ask user which question to execute the query for
print("")
print("For which of these questions, on the blast knowledge graph, do you want to execute the query?\n")
for index, query_example in enumerate(query_examples):
print(str(index + 1) + ". " + query_example["question"])
print("")
# get user's question selection
qs_number = -1
while qs_number < 0 or qs_number > len(query_examples):
try:
qs_number = int(
input("choose a number (0 for to answer all questions): "))
except ValueError:
print("Please enter valid number")
continue
print("")
# open/close client, session and transaction to talk to the blast keyspace
with GraknClient(uri="localhost:48555") as client:
with client.session(keyspace="proteins") as session:
with session.transaction().read() as transaction:
# execute the query for the selected question
if qs_number == 0:
execute_query_all(transaction)
else:
question = query_examples[qs_number - 1]["question"]
query_function = query_examples[qs_number - 1]["query_function"]
query_function(question, transaction)