1
- import os
2
1
import shutil
3
2
from pathlib import Path
4
3
14
13
15
14
class MCRIBReconAllInputSpec (CommandLineInputSpec ):
16
15
# Input structure massaging
16
+ outdir = Directory (
17
+ exists = True ,
18
+ hash_files = False ,
19
+ desc = 'Path to save output, or path of existing MCRIBS output' ,
20
+ )
17
21
subjects_dir = Directory (
18
22
exists = True ,
19
23
hash_files = False ,
@@ -100,6 +104,21 @@ class MCRIBReconAll(CommandLine):
100
104
_cmd = 'MCRIBReconAll'
101
105
input_spec = MCRIBReconAllInputSpec
102
106
output_spec = MCRIBReconAllOutputSpec
107
+ _no_run = False
108
+
109
+ @property
110
+ def cmdline (self ):
111
+ cmd = super ().cmdline
112
+ # Avoid processing if valid
113
+ if self .inputs .outdir :
114
+ sid = self .inputs .subject_id
115
+ logf = self .inputs .outdir / sid / 'logs' / f'{ sid } .log'
116
+ if logf .exists ():
117
+ logtxt = logf .read_text ().splitlines ()[- 3 :]
118
+ self ._no_run = 'Finished without error' in logtxt
119
+ if self ._no_run :
120
+ return "echo MCRIBSReconAll: nothing to do"
121
+ return cmd
103
122
104
123
def _setup_directory_structure (self , mcribs_dir : Path ) -> None :
105
124
'''
@@ -138,9 +157,9 @@ def _setup_directory_structure(self, mcribs_dir: Path) -> None:
138
157
t2wiso = root / 'RawT2RadiologicalIsotropic' / f'{ sid } .nii.gz'
139
158
t2wiso .parent .mkdir (** mkdir_kw )
140
159
if not t2wiso .exists ():
141
- t2wiso .symlink_to (f'../../ RawT2/{ sid } .nii.gz' )
160
+ t2wiso .symlink_to (f'../RawT2/{ sid } .nii.gz' )
142
161
143
- n4 = root / sid / 'N4' / f'{ sid } .nii.gz'
162
+ n4 = root / 'TissueSegDrawEM' / sid / 'N4' / f'{ sid } .nii.gz'
144
163
n4 .parent .mkdir (** mkdir_kw )
145
164
if not n4 .exists ():
146
165
n4 .symlink_to (f'../../../RawT2/{ sid } .nii.gz' )
@@ -151,37 +170,42 @@ def _setup_directory_structure(self, mcribs_dir: Path) -> None:
151
170
tisseg = root / 'TissueSeg' / f'{ sid } _all_labels.nii.gz'
152
171
tisseg .parent .mkdir (** mkdir_kw )
153
172
if not tisseg .exists ():
154
- shutil .copy (self .inputs .segmentation , str (tisseg ))
173
+ shutil .copy (self .inputs .segmentation_file , str (tisseg ))
155
174
manedit = tisseg .parent / f'{ sid } _all_labels_manedit.nii.gz'
156
175
if not manedit .exists ():
157
176
manedit .symlink_to (tisseg .name )
158
177
159
178
if self .inputs .surfrecon :
179
+ t2wseg = root / 'TissueSeg' / f'{ sid } _t2w_restore.nii.gz'
180
+ if not t2wseg .exists ():
181
+ t2wseg .symlink_to (f'../RawT2/{ sid } .nii.gz' )
182
+
160
183
surfrec = root / 'SurfReconDeformable' / sid / 'temp' / 't2w-image.nii.gz'
161
184
surfrec .parent .mkdir (** mkdir_kw )
162
185
if not surfrec .exists ():
163
- surfrec .symlink_to (f'../../../../RawT2/{ sid } .nii.gz' )
164
-
165
- # TODO: T1w -> <subject_id>/RawT1RadiologicalIsotropic/<subjectid>.nii.gz
186
+ surfrec .symlink_to (f'../../../RawT2/{ sid } .nii.gz' )
187
+ # TODO?: T1w -> <subject_id>/RawT1RadiologicalIsotropic/<subjectid>.nii.gz
166
188
return
167
189
168
190
def _run_interface (self , runtime ):
169
191
# if users wish to preserve their runs
170
- mcribs_dir = os .getenv ('MCRIBS_SUBJECTS' )
171
- if mcribs_dir is None or not Path (mcribs_dir ).exists ():
172
- mcribs_dir = runtime .cwd / 'mcribs'
173
- self ._mcribs_dir = mcribs_dir
174
- self ._setup_directory_structure (mcribs_dir )
175
- # runs in CWD
176
- os .chdir (mcribs_dir / self .inputs .subject_id )
192
+ mcribs_dir = self .inputs .outdir or Path (runtime .cwd ) / 'mcribs'
193
+ self ._mcribs_dir = Path (mcribs_dir )
194
+ self ._setup_directory_structure (self ._mcribs_dir )
195
+ # overwrite CWD to be in MCRIB subject's directory
196
+ runtime .cwd = str (self ._mcribs_dir / self .inputs .subject_id )
177
197
return super ()._run_interface (runtime )
178
198
179
199
def _list_outputs (self ):
180
200
outputs = self ._outputs ().get ()
181
- outputs ['mcribs_dir' ] = self ._mcribs_dir
201
+ outputs ['mcribs_dir' ] = str (self ._mcribs_dir )
202
+
203
+ # Copy freesurfer directory into FS subjects dir
204
+ sid = self .inputs .subject_id
205
+ mcribs_fs = self ._mcribs_dir / sid / 'freesurfer' / sid
206
+ if mcribs_fs .exists ():
207
+ dst = Path (self .inputs .subjects_dir ) / self .inputs .subject_id
208
+ if not dst .exists ():
209
+ shutil .copytree (mcribs_fs , dst )
182
210
183
- # TODO: Copy freesurfer directory into FS subjects dir
184
- # fs_outputs = self._mcribs_dir / self.inputs.subject_id / 'freesurfer'
185
- # if fs_outputs.exists():
186
- # pass
187
211
return outputs
0 commit comments