generated from UCL-MIRSG/mirsg-template
-
Notifications
You must be signed in to change notification settings - Fork 0
/
share_subject_to_genetic_project.py
73 lines (61 loc) · 2.46 KB
/
share_subject_to_genetic_project.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
import re
import sys
from drc_containers.xnat_utils.xnat_credentials import (
open_xnat_session,
XnatContainerCredentials,
XnatCredentials,
)
def share_to_project(subject, other_project_id, debug=False):
try:
shared_to_project = [
x for x in subject.sharing.values() if x.id == other_project_id
]
if shared_to_project:
print(
f"{subject.label} is already shared with project " f"{other_project_id}"
)
if not shared_to_project:
print(f"Sharing {subject.label} to {other_project_id}")
if debug:
print("DEBUG MODE: no actual sharing will be performed")
else:
subject.share(other_project_id, label=subject.label)
except Exception as ex:
raise RuntimeError(
f"Exception {str(ex)} while trying to share " f"subject {subject.label}"
)
def share_subject_to_genetic_project(credentials: XnatCredentials, subject_id: str):
"""Share the specified subject to the corresponding genetic project
Args:
credentials: XNAT host name and user login details
subject_id: ID of the subject to share
"""
site_pattern = re.compile("^GENFI_\d\d$")
with open_xnat_session(credentials) as xnat_session:
if subject_id not in xnat_session.subjects:
raise ValueError(f"Subject {subject_id} not found")
subject = xnat_session.subjects[subject_id]
project_id = subject.project
# If this project has a GENFI3 naming convention then share to the
# genetic project
if site_pattern.match(project_id):
genetic_project_id = project_id + "_GEN"
if genetic_project_id not in xnat_session.projects:
print(
f"Not sharing subject {subject_id} because no genetic "
f"project {genetic_project_id} was found"
)
else:
share_to_project(subject=subject, other_project_id=genetic_project_id)
else:
print(
f"Not sharing subject {subject_id} because parent project "
f"{project_id} is not a GENFI3 project"
)
def main():
if len(sys.argv) < 2:
raise ValueError("No subject label specified")
credentials = XnatContainerCredentials()
share_subject_to_genetic_project(credentials=credentials, subject_id=sys.argv[1])
if __name__ == "__main__":
main()