-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathadd_ants_segvols.py
93 lines (78 loc) · 5.03 KB
/
add_ants_segvols.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
import argparse
from os.path import join,basename,splitext,isfile
from os import system
def main():
parser = argparse.ArgumentParser(prog='add_ants_segvols.py',
description='''This is essentially a helper script which takes a list of
BIDS-formatted subject IDs with paths to the BIDS subject folders for abide
and adhd200 datasets and downloads the ANTS-generated stats files
from an Amazon S3 bucket and adds the brain volume data to existing NIDM-E files
if the particular subject ID exists. The NIDM-E files are expected to be
in the site's directory in the BIDS dataset and called nidm.ttl (e.g. for
ABIDE site SDSU: ./datasets.datalad.org/abide/RawDataBIDS/SDSU/nidm.ttl''',
formatter_class=argparse.RawDescriptionHelpFormatter)
parser.add_argument('-s','--subj',dest='subj',required=True, help='Text file containing 1 line per participant'
'with relative paths to the location of the'
'BIDS subject directory:'
''
'./datasets.datalad.org/abide/RawDataBIDS/SDSU/sub-0050197'
'./datasets.datalad.org/abide/RawDataBIDS/SDSU/sub-0050190'
'./datasets.datalad.org/abide/RawDataBIDS/SDSU/sub-0050199'
'...')
parser.add_argument('-new','--new', action='store_true', required=False, help="If flag set then new NIDM files will")
parser.add_argument('-forcenidm','--forcenidm', action='store_true', required=False, help="If flag set then data will be"
"added to existing NIDM file regardless if subject already exists.")
args = parser.parse_args()
new = args.new
with open(args.subj,'r') as f:
for line in f:
# set up command line for get brain volume data and adding it to existing NIDM file
# first check if a NIDM file exists in the site location
#strip training \n from line
line=line.rstrip('\n')
loc = line.find("sub")
if not new:
if not isfile(join(line[:loc-1],"nidm.ttl")):
print("No existing NIDM-E file for site: %s" %line[:loc-1])
print("Skipping subject: %s" %line[loc:])
continue
else:
#check if nidm.ttl file actually exists and if so use it otherwise create from scratch
if isfile(join(line[:loc-1],"nidm.ttl")):
new = False
# set up command to add brain volumes from Amazon bucket
# -f "https://fcp-indi.s3.amazonaws.com/data/Projects/ABIDE/Outputs/mindboggle_swf/mindboggle/ants_subjects/sub-0050002/antslabelstats.csv,
# https://fcp-indi.s3.amazonaws.com/data/Projects/ABIDE/Outputs/mindboggle_swf/mindboggle/ants_subjects/sub-0050002/antsbrainvols.csv,
# https://fcp-indi.s3.amazonaws.com/data/Projects/ABIDE/Outputs/mindboggle_swf/mindboggle/ants_subjects/sub-0050002/antsBrainSegmentation.nii.gz"
base_url = "https://fcp-indi.s3.amazonaws.com/data/Projects/"
#get dataset (abide or adhd200) from line
if "abide" in line:
base_url=base_url + "ABIDE/"
elif "adhd200" in line:
base_url=base_url + "ADHD200/"
elif "corr" in line:
base_url=base_url + "CORR/"
elif "openneuro" in line:
cmd=cmd + "OPENNEURO/"
else:
print("Error, can't find dataset (abide | adhd200) in line: %s" %line)
print("Skipping...")
continue
base_url = base_url + "Outputs/mindboggle_swf/mindboggle/ants_subjects/"
cmd="antsegstats2nidm -f \"" + base_url + line[loc:] + "/antslabelstats.csv," + \
base_url + line[loc:] + "/antsbrainvols.csv," + \
base_url + line[loc:] + "/antsBrainSegmentation.nii.gz\" -subjid " + line[loc+4:]
if not new:
cmd = cmd + " -n " + line[:loc] + "nidm.ttl -o " + line[:loc] + \
"nidm.ttl"
if args.forcenidm is not False:
cmd = cmd + " -forcenidm "
else:
cmd = cmd + " -o " + join(line[:loc],"nidm.ttl")
if args.forcenidm is not False:
cmd = cmd + " -forcenidm "
# execute command
print(cmd)
system(cmd)
if __name__ == "__main__":
main()