|
1
|
|
|
from subprocess import check_call |
|
|
|
|
|
|
2
|
|
|
import sys |
|
3
|
|
|
import os |
|
4
|
|
|
import re |
|
5
|
|
|
import json |
|
6
|
|
|
import logging |
|
7
|
|
|
from dotenv import load_dotenv |
|
8
|
|
|
from LORIS.query import LORIS_query |
|
|
|
|
|
|
9
|
|
|
from LORIS.helper import LORIS_helper |
|
|
|
|
|
|
10
|
|
|
from LocalDB.schema import CNBP_blueprint |
|
|
|
|
|
|
11
|
|
|
logging.basicConfig(stream=sys.stdout, level=logging.INFO) |
|
12
|
|
|
#logger = logging.getLogger('LORISQuery') |
|
13
|
|
|
|
|
14
|
|
|
class LORIS_candidates: |
|
|
|
|
|
|
15
|
|
|
|
|
16
|
|
|
@staticmethod |
|
17
|
|
|
def parse_PSCID(PSCID): |
|
|
|
|
|
|
18
|
|
|
""" |
|
19
|
|
|
Return three parts of the PSCID: institution, project, subject |
|
20
|
|
|
:param PSCID: |
|
21
|
|
|
:return: |
|
22
|
|
|
""" |
|
23
|
|
|
success = load_dotenv() |
|
24
|
|
|
if not success: |
|
25
|
|
|
raise ImportError("Credential .env NOT FOUND! Please ensure .env is set with all the necessary credentials!") |
|
|
|
|
|
|
26
|
|
|
|
|
27
|
|
|
# Loading regular expression |
|
28
|
|
|
re_institution = CNBP_blueprint.PSCID_schema_institution |
|
29
|
|
|
re_project = CNBP_blueprint.PSCID_schema_project |
|
30
|
|
|
re_subject = CNBP_blueprint.PSCID_schema_subject |
|
31
|
|
|
|
|
32
|
|
|
# Use expression to extract from the inputted PSCID |
|
33
|
|
|
input_institution = re.search(re_institution, PSCID).group(0) |
|
34
|
|
|
input_project = re.search(re_project, PSCID).group(0) |
|
35
|
|
|
input_subject = re.search(re_subject, PSCID).group(0) |
|
36
|
|
|
|
|
37
|
|
|
if input_subject is None or input_project is None or input_institution is None: |
|
38
|
|
|
success = False |
|
39
|
|
|
else: |
|
40
|
|
|
success = True |
|
41
|
|
|
|
|
42
|
|
|
return success, input_institution, input_project, input_subject |
|
43
|
|
|
|
|
44
|
|
|
@staticmethod |
|
45
|
|
|
def check_instutitionID_compliance(input_institutionID): |
|
|
|
|
|
|
46
|
|
|
# Parse from the .env standardization |
|
47
|
|
|
InsitituionID = os.getenv("institutionID") |
|
|
|
|
|
|
48
|
|
|
|
|
49
|
|
|
# Check if institution ID matches |
|
50
|
|
|
if not (input_institutionID == InsitituionID): |
|
|
|
|
|
|
51
|
|
|
return False |
|
52
|
|
|
else: |
|
53
|
|
|
return True |
|
54
|
|
|
|
|
55
|
|
|
@staticmethod |
|
56
|
|
|
def check_projectID_compliance(input_projectID): |
|
|
|
|
|
|
57
|
|
|
|
|
58
|
|
|
# Load ProjectIDs from the environment. |
|
59
|
|
|
success = load_dotenv() |
|
60
|
|
|
if not success: |
|
61
|
|
|
raise ImportError("Credential .env NOT FOUND! Please ensure .env is set with all the necessary credentials!") |
|
|
|
|
|
|
62
|
|
|
|
|
63
|
|
|
projectID_dictionary_json: str = os.getenv("projectID_dictionary") |
|
|
|
|
|
|
64
|
|
|
projectID_list = json.loads(projectID_dictionary_json) |
|
|
|
|
|
|
65
|
|
|
|
|
66
|
|
|
# check if project ID is in the projectID list. |
|
67
|
|
|
isRecognized = input_projectID in projectID_list |
|
|
|
|
|
|
68
|
|
|
|
|
69
|
|
|
return isRecognized |
|
70
|
|
|
|
|
71
|
|
|
@staticmethod |
|
72
|
|
|
def check_subjectID_compliance(input_subjectID): |
|
|
|
|
|
|
73
|
|
|
if not input_subjectID.isdigit(): |
|
74
|
|
|
return False |
|
75
|
|
|
|
|
76
|
|
|
if int(input_subjectID) < 9999 or int(input_subjectID) > 0: |
|
|
|
|
|
|
77
|
|
|
return True |
|
78
|
|
|
else: |
|
79
|
|
|
return False |
|
80
|
|
|
|
|
81
|
|
|
@staticmethod |
|
82
|
|
|
def check_PSCID_compliance(PSCID): |
|
|
|
|
|
|
83
|
|
|
""" |
|
84
|
|
|
Checks PSCID inputed for 1) InstitionID format, 2) ProjectID format, 3) SubjectID format. |
|
85
|
|
|
:param PSCID: |
|
86
|
|
|
:return: |
|
87
|
|
|
""" |
|
88
|
|
|
logger = logging.getLogger(__name__) |
|
89
|
|
|
|
|
90
|
|
|
# Parse from input PSCID |
|
91
|
|
|
success, input_institution, input_project, input_subject = LORIS_candidates.parse_PSCID(PSCID) |
|
|
|
|
|
|
92
|
|
|
|
|
93
|
|
|
# Ensure parsing success |
|
94
|
|
|
if not success: |
|
95
|
|
|
return False |
|
96
|
|
|
|
|
97
|
|
|
# Check institution ID to ensure that |
|
98
|
|
|
if not LORIS_candidates.check_instutitionID_compliance(input_institution): |
|
99
|
|
|
logger.info("Institution not compliant") |
|
100
|
|
|
return False |
|
101
|
|
|
|
|
102
|
|
|
# Check if projectID already exist |
|
103
|
|
|
if not LORIS_candidates.check_projectID_compliance(input_project): |
|
104
|
|
|
logger.info("ProjectID not recognized") |
|
105
|
|
|
return False |
|
106
|
|
|
|
|
107
|
|
|
# Check last four digits: make sure the last four characters are digits. |
|
108
|
|
|
if not LORIS_candidates.check_subjectID_compliance(str(input_subject)): |
|
109
|
|
|
logger.info("SubjectID not standardized") |
|
110
|
|
|
return False |
|
111
|
|
|
|
|
112
|
|
|
return True |
|
113
|
|
|
|
|
114
|
|
|
@staticmethod |
|
115
|
|
|
def check_DCCID(DCCID): |
|
|
|
|
|
|
116
|
|
|
""" |
|
117
|
|
|
Check if DCCID id conform. |
|
118
|
|
|
:param DCCID: |
|
119
|
|
|
:return: |
|
120
|
|
|
""" |
|
121
|
|
|
if not len(str(DCCID)) == 6: |
|
|
|
|
|
|
122
|
|
|
return False |
|
123
|
|
|
elif not str(DCCID).isnumeric(): |
|
124
|
|
|
return False |
|
125
|
|
|
elif DCCID > 999999: |
|
126
|
|
|
return False |
|
127
|
|
|
elif DCCID < 0: |
|
128
|
|
|
return False |
|
129
|
|
|
else: |
|
130
|
|
|
return True |
|
131
|
|
|
|
|
132
|
|
|
@staticmethod |
|
133
|
|
|
def deleteCandidateCNBP(token, DCCID, PSCID): |
|
|
|
|
|
|
134
|
|
|
logger = logging.getLogger('UT_LORIS_delete_subject') |
|
135
|
|
|
|
|
136
|
|
|
# Load the hard coded variables. |
|
137
|
|
|
success = load_dotenv() |
|
138
|
|
|
if not success: |
|
139
|
|
|
raise ImportError("Credential .env NOT FOUND! Please ensure .env is set with all the necessary credentials!") |
|
|
|
|
|
|
140
|
|
|
ProxyIP = os.getenv("ProxyIP") |
|
|
|
|
|
|
141
|
|
|
ProxyUsername = os.getenv("ProxyUsername") |
|
|
|
|
|
|
142
|
|
|
ProxyPassword = os.getenv("ProxyPassword") |
|
|
|
|
|
|
143
|
|
|
LORISHostPassword = os.getenv("LORISHostPassword") |
|
|
|
|
|
|
144
|
|
|
LORISHostUsername = os.getenv("LORISHostUsername") |
|
|
|
|
|
|
145
|
|
|
LORISHostIP = os.getenv("LORISHostIP") |
|
|
|
|
|
|
146
|
|
|
DeletionScript = os.getenv("DeletionScript") |
|
|
|
|
|
|
147
|
|
|
|
|
148
|
|
|
# Export some variables for subsequent deletion clean script against production database (yes... because we could not automate LORIS development...): |
|
|
|
|
|
|
149
|
|
|
command_string = ["sshpass", "-p", ProxyPassword, "ssh", ProxyUsername + "@" + ProxyIP, "-t", "sshpass", "-p", |
|
|
|
|
|
|
150
|
|
|
LORISHostPassword, "ssh", "-L", "3001:localhost:22", |
|
151
|
|
|
LORISHostUsername + "@" + LORISHostIP, "php", DeletionScript, "delete_candidate", str(DCCID), |
|
|
|
|
|
|
152
|
|
|
PSCID, "confirm"] |
|
153
|
|
|
|
|
154
|
|
|
logger.info(command_string) |
|
155
|
|
|
if 'TRAVIS' in os.environ: |
|
156
|
|
|
logger.info("Running LORIS delete candidate that was created for: " + PSCID) |
|
157
|
|
|
check_call(command_string) |
|
158
|
|
|
|
|
159
|
|
|
@staticmethod |
|
160
|
|
|
def createCandidateCNBP(token, proposed_PSCID): |
|
|
|
|
|
|
161
|
|
|
""" |
|
162
|
|
|
Create a candidate using the given PSCID |
|
163
|
|
|
:param token |
|
164
|
|
|
:param proposed_PSCID: |
|
165
|
|
|
:return: DCCID |
|
166
|
|
|
""" |
|
167
|
|
|
logger = logging.getLogger('LORIS_CreateCNBPCandidates') |
|
168
|
|
|
logger.info("Creating CNBP Candidates: " + proposed_PSCID) |
|
169
|
|
|
|
|
170
|
|
|
PSCID_exist = LORIS_candidates.checkPSCIDExist(token, proposed_PSCID) |
|
|
|
|
|
|
171
|
|
|
if PSCID_exist: |
|
172
|
|
|
logger.info("PSCID already exist. Quitting.") |
|
173
|
|
|
return False |
|
174
|
|
|
|
|
175
|
|
|
Candidate = {} |
|
|
|
|
|
|
176
|
|
|
Candidate['Project'] = 'loris' |
|
177
|
|
|
Candidate['PSCID'] = proposed_PSCID |
|
178
|
|
|
Candidate['DoB'] = '2018-05-04' |
|
179
|
|
|
Candidate['Gender'] = 'Female' |
|
180
|
|
|
|
|
181
|
|
|
data = {"Candidate":Candidate} |
|
182
|
|
|
|
|
183
|
|
|
data_json = json.dumps(data) |
|
184
|
|
|
|
|
185
|
|
|
response_code, response = LORIS_query.postCNBP(token, "candidates/", data_json) |
|
186
|
|
|
if not LORIS_helper.is_response_success(response_code, 201): |
|
|
|
|
|
|
187
|
|
|
return False, None |
|
188
|
|
|
elif response is not None: # only try to decode if response is not empty! |
|
189
|
|
|
response_json = response.json() |
|
190
|
|
|
meta = response_json.get('Meta') |
|
191
|
|
|
CandID = meta.get('CandID') |
|
|
|
|
|
|
192
|
|
|
return True, CandID |
|
193
|
|
|
else: |
|
194
|
|
|
return False, None |
|
195
|
|
|
|
|
196
|
|
|
@staticmethod |
|
197
|
|
|
def checkPSCIDExist(token, proposed_PSCID): |
|
|
|
|
|
|
198
|
|
|
""" |
|
199
|
|
|
Check if Site/Study already contain the PSCID |
|
200
|
|
|
:param token: |
|
201
|
|
|
:param proposed_PSCID: |
|
202
|
|
|
:return: bool for connection, bool on if such PSCID (INSTITUTIONID + PROJECTID + SUBJECTID) exist already. |
|
|
|
|
|
|
203
|
|
|
""" |
|
204
|
|
|
logger = logging.getLogger('LORIS_checkPSCIDExist') |
|
205
|
|
|
logger.info("Checking if PSCID exist: "+proposed_PSCID) |
|
206
|
|
|
success = load_dotenv() |
|
207
|
|
|
if not success: |
|
208
|
|
|
raise ImportError("Credential .env NOT FOUND! Please ensure .env is set with all the necessary credentials!") |
|
|
|
|
|
|
209
|
|
|
institution_check = os.getenv("institutionID") |
|
210
|
|
|
|
|
211
|
|
|
|
|
212
|
|
|
#Get list of projects |
|
213
|
|
|
response_success, loris_project = LORIS_query.getCNBP(token, r"projects/loris") |
|
214
|
|
|
if not response_success: |
|
215
|
|
|
return response_success, None |
|
216
|
|
|
|
|
217
|
|
|
#Get list of candidates (Candidates in v0.0.1) |
|
218
|
|
|
candidates = loris_project.get("Candidates") |
|
219
|
|
|
logger.info(candidates) |
|
220
|
|
|
|
|
221
|
|
|
for DCCID in candidates: #these candidates should really be only from the same ID regions. |
|
|
|
|
|
|
222
|
|
|
response_success, candidate_json = LORIS_query.getCNBP(token, r"candidates/"+DCCID) |
|
223
|
|
|
if not response_success: |
|
224
|
|
|
return response_success, False |
|
225
|
|
|
# Each site following the naming convention should have SITE prefix and PI prefix and PROJECT prefix to avoid collision |
|
|
|
|
|
|
226
|
|
|
|
|
227
|
|
|
# A site check here. |
|
228
|
|
|
candidate_meta = candidate_json.get("Meta") |
|
229
|
|
|
candidate_pscID = candidate_meta.get("PSCID") |
|
|
|
|
|
|
230
|
|
|
|
|
231
|
|
|
# Not gonna check is institution ID doesn't even match. |
|
232
|
|
|
if candidate_pscID[0:3] != institution_check: |
|
233
|
|
|
continue |
|
234
|
|
|
|
|
235
|
|
|
elif candidate_pscID == proposed_PSCID: |
|
236
|
|
|
return response_success, True |
|
237
|
|
|
|
|
238
|
|
|
#latest_timepoint = findLatestTimePoint(DCCID) |
|
239
|
|
|
|
|
240
|
|
|
return True, False |
|
241
|
|
|
|
|
242
|
|
|
@staticmethod |
|
243
|
|
|
def checkDCCIDExist(token, proposed_DCCID): |
|
|
|
|
|
|
244
|
|
|
""" |
|
245
|
|
|
Check if Site/Study already contain the PSCID |
|
246
|
|
|
:param token: |
|
247
|
|
|
:param proposed_DCCID: |
|
248
|
|
|
:return: |
|
249
|
|
|
""" |
|
250
|
|
|
logger = logging.getLogger('LORIS_checkDCCIDExist') |
|
251
|
|
|
logger.info("Checking if DCCID exist: "+str(proposed_DCCID)) |
|
252
|
|
|
success = load_dotenv() |
|
253
|
|
|
if not success: |
|
254
|
|
|
raise ImportError("Credential .env NOT FOUND! Please ensure .env is set with all the necessary credentials!") |
|
|
|
|
|
|
255
|
|
|
institution_check = os.getenv("institutionID") |
|
|
|
|
|
|
256
|
|
|
|
|
257
|
|
|
assert (LORIS_candidates.check_DCCID(proposed_DCCID)) |
|
|
|
|
|
|
258
|
|
|
|
|
259
|
|
|
#Get list of projects |
|
260
|
|
|
response, loris_project = LORIS_query.getCNBP(token, r"projects/loris") |
|
261
|
|
|
response_success = LORIS_helper.is_response_success(response, 200) |
|
262
|
|
|
|
|
263
|
|
|
if not response_success: |
|
264
|
|
|
logger.info("FAILED log response: " + str(response)) |
|
265
|
|
|
return response_success, None |
|
266
|
|
|
|
|
267
|
|
|
#Get list of candidates (Candidates in v0.0.1) |
|
268
|
|
|
candidates = loris_project.get("Candidates") |
|
269
|
|
|
logger.info(candidates) |
|
270
|
|
|
|
|
271
|
|
|
for DCCID in candidates: |
|
|
|
|
|
|
272
|
|
|
if str(proposed_DCCID) == DCCID: |
|
273
|
|
|
return response_success, True |
|
274
|
|
|
else: |
|
275
|
|
|
continue |
|
276
|
|
|
return response_success, False |
|
277
|
|
|
|
|
278
|
|
|
if __name__ == "__main__": |
|
279
|
|
|
print(LORIS_candidates.check_projectID_compliance("GL01")) |
|
|
|
|
|
The coding style of this project requires that you add a docstring to this code element. Below, you find an example for methods:
If you would like to know more about docstrings, we recommend to read PEP-257: Docstring Conventions.