-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbit_segmentation.sh
executable file
·680 lines (512 loc) · 23.5 KB
/
bit_segmentation.sh
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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
#!/bin/bash
# bit_segmentation.sh
#
# Quick script for running segmentation analyses separate from tract_van.sh
# Atlases include:
# Desikan-Killiany (individual segmentation, from FreeSurfer): cluster based segmentation, thalamus only
# Yeo7: hard segmentation & kmeans, thalamus & pallidum
# Can also run with custom atlas too
#
# Created by Michael Hart on 05/11/2020.
#
#################
#Set up
#################
#remove me from tract_van.sh
tempdir=`pwd`
echo ${tempdir}
codedir=${HOME}/code/github/tractography
FSLOUTPUTTYPE=NIFTI_GZ #occassionally not set as standard
#make log
test -f bit_segmentation_log.txt && rm bit_segmentation_log.txt
touch ${tempdir}/bit_segmentation_log.txt
log=${tempdir}/bit_segmentation_log.txt
echo $(date) >> ${log}
echo "starting bit_segmentation" >> ${log}
echo "workingdir is ${tempdir}" >> ${log}
echo "codedir is ${codedir}" >> ${log}
#########
#Template
#########
#define atlas: fill in & uncomment as required
#atlas=
#atlas=${tempdir}/dkn_volume_MNI_seq.nii.gz
#atlas=${codedir}/templates/Yeo7.nii.gz
#echo ${atlas}
#########
#1. Other
#########
if [ $(imtest ${atlas}) == 1 ] ;
then
echo "${atlas} dataset for segmentation ok"
echo "Doing separate ${atlas} segmentation analysis (nb: code to be added)"
fi
######
#2. DK
######
if [ $(imtest dkn_volume_MNI_seq) == 1 ] ;
then
echo "DKN_volume_MNI_seq template already made"
echo "DKN_volume_MNI_seq template already made" >> ${log}
atlas=${tempdir}/dkn_volume_MNI_seq.nii.gz
else
echo "making DK_volume_MNI_seq"
echo "making DK_volume_MNI_seq" >> ${log}
echo $(date) >> ${log}
#Set up DK: work in 'diffusion' directory
SUBJECTS_DIR=`pwd`
mri_aparc2aseg --s FS --annot aparc
mri_convert ./FS/mri/aparc+aseg.mgz dkn_volume.nii.gz
fslreorient2std dkn_volume.nii.gz dkn_volume.nii.gz
#Registration
flirt -in dkn_volume.nii.gz -ref ${codedir}/templates/500.sym_4mm.nii.gz -o dkn_volume_check -dof 12 -interp nearestneighbour
#Renumber sequentially
file_matlab=temp_DK_renumDesikan
echo "Matlab file is: ${file_matlab}.m"
echo "Matlab file variable is: ${file_matlab}"
echo "renumDesikan_sub('dkn_volume_MNI.nii.gz', 0);exit" > ${file_matlab}.m
matlab -nodisplay -r "${file_matlab}"
#end DK setup
echo "dkn_volume_MNI_seq now made"
echo "dkn_volume_MNI_seq now made" >> ${log}
echo $(date) >> ${log}
atlas=${tempdir}/dkn_volume_MNI_seq.nii.gz
fi
#Define atlas
if [ $(imtest $atlas) == 1 ] ;
then
echo "${atlas} dataset ok"
fi
#Not for DK! (mask atlas by hemisphere)
if [[ ! -d ${tempdir}/segmentation ]] ;
then
mkdir -p ${tempdir}/segmentation
fi
cd segmentation
cp ${atlas} .
outname=`basename ${atlas} .nii.gz` #for parsing outputs
#fslmaths ${atlas} -mas ${codedir}/templates/right_brain.nii.gz ${outname}_right
#fslmaths ${atlas} -mas ${codedir}/templates/left_brain.nii.gz ${outname}_left
#Split atlas into individual ROI files
echo "running deparcellator.sh ${outname}"
echo "running deparcellator.sh ${outname}" >> ${log}
deparcellator.sh ${outname}
#Add dentate to list
echo "${codedir}/templates/dentate_left.nii.gz" >> ${outname}_seeds/seeds_targets_list.txt
echo "${codedir}/templates/dentate_right.nii.gz" >> ${outname}_seeds/seeds_targets_list.txt
cd ../
#Move individual nucleus (seed) to be segmentated to standard space (i.e. same as target parcellation / atlas)
applywarp --in=${tempdir}/first_segmentation/first-R_Thal_first.nii.gz --ref=${FSLDIR}/data/standard/MNI152_T1_2mm_brain --warp=${tempdir}/diffusion.bedpostX/xfms/str2standard_warp --out=${tempdir}/segmentation/thalamus_right_MNI.nii.gz
fslmaths ${tempdir}/segmentation/thalamus_right_MNI.nii.gz -bin ${tempdir}/segmentation/thalamus_right_MNI.nii.gz
#Move individual nucleus (seed) to be segmentated to standard space (i.e. same as target parcellation / atlas)
applywarp --in=${tempdir}/first_segmentation/first-L_Thal_first.nii.gz --ref=${FSLDIR}/data/standard/MNI152_T1_2mm_brain --warp=${tempdir}/diffusion.bedpostX/xfms/str2standard_warp --out=${tempdir}/segmentation/thalamus_left_MNI.nii.gz
fslmaths ${tempdir}/segmentation/thalamus_left_MNI.nii.gz -bin ${tempdir}/segmentation/thalamus_left_MNI.nii.gz
#Thalamus: DK, R+L, cluster
#Right thalamus
#If DK segmentation run pull out, otherwise make call
if [[ -d ${tempdir}/thalamus2cortex_right_cluster ]] ;
then
echo "DK cluster segmentation already run on right"
echo "DK cluster segmentation already run on right" >> ${log}
else
echo "Running DK cluster segmentation on right"
echo "Running DK cluster segmentation on right" >> ${log}
#Seed to target
touch ${tempdir}/batch_thalamus2cortex_right_cluster.sh
echo "#!/bin/bash" >> ${tempdir}/batch_thalamus2cortex_right_cluster.sh
echo "probtrackx2 --samples=${tempdir}/diffusion.bedpostX/merged \
--mask=${tempdir}/diffusion.bedpostX/nodif_brain_mask \
--xfm=${tempdir}/diffusion.bedpostX/xfms/standard2diff \
--invxfm=${tempdir}/diffusion.bedpostX/xfms/diff2standard \
--seedref=${FSLDIR}/data/standard/MNI152_T1_2mm_brain.nii.gz \
--seed=${tempdir}/segmentation/thalamus_right_MNI.nii.gz \
--targetmasks=${tempdir}/segmentation/${outname}_seeds/seeds_targets_list.txt \
--dir=thalamus2cortex_right_cluster \
--loopcheck \
--onewaycondition \
--forcedir \
--opd \
--os2t \
--nsamples=5000" >> ${tempdir}/batch_thalamus2cortex_right_cluster.sh
chmod 777 ${tempdir}/batch_thalamus2cortex_right_cluster.sh
sbatch --time=3:00:00 ${tempdir}/batch_thalamus2cortex_right_cluster.sh
fi
#Left thalamus
#If DK segmentation run pull out, otherwise make call
if [[ -d ${tempdir}/thalamus2cortex_left_cluster ]] ;
then
echo "DK cluster segmentation already run on left"
echo "DK cluster segmentation already run on left" >> ${log}
else
echo "Running DK cluster segmentation on left"
echo "Running DK cluster segmentation on left" >> ${log}
#Seed to target
touch ${tempdir}/batch_thalamus2cortex_left_cluster.sh
echo "#!/bin/bash" >> ${tempdir}/batch_thalamus2cortex_left_cluster.sh
echo "probtrackx2 --samples=${tempdir}/diffusion.bedpostX/merged \
--mask=${tempdir}/diffusion.bedpostX/nodif_brain_mask \
--xfm=${tempdir}/diffusion.bedpostX/xfms/standard2diff \
--invxfm=${tempdir}/diffusion.bedpostX/xfms/diff2standard \
--seedref=${FSLDIR}/data/standard/MNI152_T1_2mm_brain.nii.gz \
--seed=${tempdir}/segmentation/thalamus_left_MNI.nii.gz \
--targetmasks=${tempdir}/segmentation/${outname}_seeds/seeds_targets_list.txt \
--dir=thalamus2cortex_left_cluster \
--loopcheck \
--onewaycondition \
--forcedir \
--opd \
--os2t \
--nsamples=5000" >> ${tempdir}/batch_thalamus2cortex_left_cluster.sh
chmod 777 ${tempdir}/batch_thalamus2cortex_left_cluster.sh
sbatch --time=3:00:00 ${tempdir}/batch_thalamus2cortex_left_cluster.sh
fi
#######
#3. Yeo
#######
atlas="${codedir}/templates/Yeo7.nii.gz"
#Mask atlas by hemisphere (faster & avoids conflict with connectome)
if [[ ! -d ${tempdir}/segmentation ]] ;
then
echo "making new segmentation directory"
mkdir -p ${tempdir}/segmentation
fi
cd segmentation
cp ${atlas} .
outname=`basename ${atlas} .nii.gz` #for parsing outputs
fslmaths ${atlas} -mas ${codedir}/templates/right_brain.nii.gz ${outname}_right
fslmaths ${atlas} -mas ${codedir}/templates/left_brain.nii.gz ${outname}_left
#Split atlas into individual ROI files
echo "running deparcellator ${outname}"
echo "running deparcellator ${outname}" >> ${log}
deparcellator.sh ${outname}_right
deparcellator.sh ${outname}_left
cd ../
#Start segmentation: thalamus then pallidum, right then left
#NB: nsamples set to 5000 at start
#Hard segmentation
#Thalamus
#Right thalamus: hard segmentation
if [[ -d ${tempdir}/thalamus2cortex_right/ ]] && [[ -d ${tempdir}/segmentation/${outname}_right_seeds/ ]] ;
then
echo "Hard segmentation of right thalamus already run with: ${outname}"
echo "Hard segmentation of right thalamus already run with: ${outname}" >> ${log}
else
echo "Running hard segmentation of right thalamus with: ${outname}"
echo "Running hard segmentation of right thalamus with: ${outname}" >> ${log}
#Seed to target
touch ${tempdir}/batch_thalamus2cortex_right.sh
echo "#!/bin/bash" >> ${tempdir}/batch_thalamus2cortex_right.sh
echo "probtrackx2 --samples=${tempdir}/diffusion.bedpostX/merged \
--mask=${tempdir}/diffusion.bedpostX/nodif_brain_mask \
--xfm=${tempdir}/diffusion.bedpostX/xfms/standard2diff \
--invxfm=${tempdir}/diffusion.bedpostX/xfms/diff2standard \
--seedref=${FSLDIR}/data/standard/MNI152_T1_2mm_brain.nii.gz \
--seed=${tempdir}/segmentation/thalamus_right_MNI.nii.gz \
--targetmasks=${tempdir}/segmentation/${outname}_right_seeds/seeds_targets_list.txt \
--dir=thalamus2cortex_right \
--loopcheck \
--onewaycondition \
--forcedir \
--opd \
--os2t \
--nsamples=5000" >> ${tempdir}/batch_thalamus2cortex_right.sh
chmod 777 ${tempdir}/batch_thalamus2cortex_right.sh
sbatch --time=3:00:00 ${tempdir}/batch_thalamus2cortex_right.sh
#Run find_the_biggest later
fi
#Left thalamus: hard segmentation
if [[ -d ${tempdir}/thalamus2cortex_left/ ]] && [[ -d ${tempdir}/segmentation/${outname}_left_seeds/ ]] ;
then
echo "Hard segmentation of left thalamus already run with: ${outname}"
echo "Hard segmentation of left thalamus already run with: ${outname}" >> ${log}
else
echo "Running hard segmentation left thalamus with: ${outname}"
echo "Running hard segmentation left thalamus with: ${outname}" >> ${log}
#Seed to target
touch ${tempdir}/batch_thalamus2cortex_left.sh
echo "#!/bin/bash" >> ${tempdir}/batch_thalamus2cortex_left.sh
echo "probtrackx2 --samples=${tempdir}/diffusion.bedpostX/merged \
--mask=${tempdir}/diffusion.bedpostX/nodif_brain_mask \
--xfm=${tempdir}/diffusion.bedpostX/xfms/standard2diff \
--invxfm=${tempdir}/diffusion.bedpostX/xfms/diff2standard \
--seedref=${FSLDIR}/data/standard/MNI152_T1_2mm_brain.nii.gz \
--seed=${tempdir}/segmentation/thalamus_left_MNI.nii.gz \
--targetmasks=${tempdir}/segmentation/${outname}_left_seeds/seeds_targets_list.txt \
--dir=thalamus2cortex_left \
--loopcheck \
--onewaycondition \
--forcedir \
--opd \
--os2t \
--nsamples=5000" >> ${tempdir}/batch_thalamus2cortex_left.sh
chmod 777 ${tempdir}/batch_thalamus2cortex_left.sh
sbatch --time=3:00:00 ${tempdir}/batch_thalamus2cortex_left.sh
#Run find_the_biggest later
fi
#Pallidum
#Right pallidum: hard segmentation
if [[ -d ${tempdir}/pallidum2cortex_right/ ]] && [[ -d ${tempdir}/segmentation/${outname}_right_seeds/ ]] ;
then
echo "Hard segmentation of right pallidum already run with: ${outname}"
echo "Hard segmentation of right pallidum already run with: ${outname}" >> ${log}
else
echo "Running hard segmentation right pallidum with: ${outname}"
echo "Running hard segmentation right pallidum with: ${outname}" >> ${log}
#Move individual nucleus (seed) to be segmentated to standard space (i.e. same as target parcellation / atlas)
applywarp --in=${tempdir}/first_segmentation/first-R_Pall_first.nii.gz --ref=${FSLDIR}/data/standard/MNI152_T1_2mm_brain --warp=${tempdir}/diffusion.bedpostX/xfms/str2standard_warp --out=${tempdir}/segmentation/pallidum_right_MNI.nii.gz
fslmaths ${tempdir}/segmentation/pallidum_right_MNI.nii.gz -bin ${tempdir}/segmentation/pallidum_right_MNI.nii.gz
#Seed to target
touch ${tempdir}/batch_pallidum2cortex_right.sh
echo "#!/bin/bash" >> ${tempdir}/batch_pallidum2cortex_right.sh
echo "probtrackx2 --samples=${tempdir}/diffusion.bedpostX/merged \
--mask=${tempdir}/diffusion.bedpostX/nodif_brain_mask \
--xfm=${tempdir}/diffusion.bedpostX/xfms/standard2diff \
--invxfm=${tempdir}/diffusion.bedpostX/xfms/diff2standard \
--seedref=${FSLDIR}/data/standard/MNI152_T1_2mm_brain.nii.gz \
--seed=${tempdir}/segmentation/pallidum_right_MNI.nii.gz \
--targetmasks=${tempdir}/segmentation/${outname}_right_seeds/seeds_targets_list.txt \
--dir=pallidum2cortex_right \
--loopcheck \
--onewaycondition \
--forcedir \
--opd \
--os2t \
--nsamples=5000" >> ${tempdir}/batch_pallidum2cortex_right.sh
chmod 777 ${tempdir}/batch_pallidum2cortex_right.sh
sbatch --time=3:00:00 ${tempdir}/batch_pallidum2cortex_right.sh
#run find_the_biggest at end
fi
#Left pallidum: hard segmentation
if [[ -d ${tempdir}/pallidum2cortex_left/ ]] && [[ -d ${tempdir}/segmentation/${outname}_left_seeds/ ]] ;
then
echo "Hard segmentation of left pallidum already run with: ${outname}"
echo "Hard segmentation of left pallidum already run with: ${outname}" >> ${log}
else
echo "Running hard segmentation left pallidum with: ${outname}"
echo "Running hard segmentation left pallidum with: ${outname}" >> ${log}
#Move individual nucleus (seed) to be segmentated to standard space (i.e. same as target parcellation / atlas)
applywarp --in=${tempdir}/first_segmentation/first-L_Pall_first.nii.gz --ref=${FSLDIR}/data/standard/MNI152_T1_2mm_brain --warp=${tempdir}/diffusion.bedpostX/xfms/str2standard_warp --out=${tempdir}/segmentation/pallidum_left_MNI.nii.gz
fslmaths ${tempdir}/segmentation/pallidum_left_MNI.nii.gz -bin ${tempdir}/segmentation/pallidum_left_MNI.nii.gz
#Seed to target
touch ${tempdir}/batch_pallidum2cortex_left.sh
echo "#!/bin/bash" >> ${tempdir}/batch_pallidum2cortex_left.sh
echo "probtrackx2 --samples=${tempdir}/diffusion.bedpostX/merged \
--mask=${tempdir}/diffusion.bedpostX/nodif_brain_mask \
--xfm=${tempdir}/diffusion.bedpostX/xfms/standard2diff \
--invxfm=${tempdir}/diffusion.bedpostX/xfms/diff2standard \
--seedref=${FSLDIR}/data/standard/MNI152_T1_2mm_brain.nii.gz \
--seed=${tempdir}/segmentation/pallidum_left_MNI.nii.gz \
--targetmasks=${tempdir}/segmentation/${outname}_left_seeds/seeds_targets_list.txt \
--dir=pallidum2cortex_left \
--loopcheck \
--onewaycondition \
--forcedir \
--opd \
--os2t \
--nsamples=5000" >> ${tempdir}/batch_pallidum2cortex_left.sh
chmod 777 ${tempdir}/batch_pallidum2cortex_left.sh
sbatch --time=3:00:00 ${tempdir}/batch_pallidum2cortex_left.sh
#run find_the_biggest at end
fi
#do actual segmentation at the end: pause to allow completion of probtrackx2 call
echo "Sleep after sbatch calls"
echo "Sleep after sbatch calls" >> ${log}
echo $(date) >> ${log}
sleep 10h
echo "Resuming after sleep"
echo "Resuming after sleep" >> ${log}
echo $(date) >> ${log}
##########
#4. kmeans
##########
#Alternative segmentation method (hypothesis free): requires subsequent Matlab run for kmeans segmentation
#Make grey matter mask in MNI space
#Use ANTS preferentially
if [[ -f $tempdir}/ACT/CorticalThicknessNormalizedToTemplate.nii.gz ]] ;
then
fslmaths ${tempdir}/ACT/CorticalThicknessNormalizedToTemplate.nii.gz -bin ${tempdir}/segmentation/GM_mask_MNI.nii.gz
else
#Otherwise use FSL
applywarp --in=${tempdir}/structural.anat/T1_fast_pve_1.nii.gz --ref=${FSLDIR}/data/standard/MNI152_T1_2mm_brain --warp=${tempdir}/diffusion.bedpostX/xfms/str2standard_warp --out=${tempdir}/segmentation/GM_mask_MNI
#binarise
fslmaths ${tempdir}/segmentation/GM_mask_MNI -thr 0.5 ${tempdir}/segmentation/GM_mask_MNI #generous threshold
fi
#Right thalamus: kmeans
if [[ -d ${tempdir}/thalamus2cortex_right_omatrix2/ ]] && [[ -d ${tempdir}/segmentation/${outname}_right_seeds/ ]] ;
then
echo "kmeans of right thalamus already run with: ${outname}"
echo "kmeans of right thalamus already run with: ${outname}" >> ${log}
else
echo "Running kmeans segmentation of right thalamus with: ${outname}"
echo "Running kmeans segmentation of right thalamus with: ${outname}" >> ${log}
#omatrix2
touch ${tempdir}/batch_thalamus2cortex_right_omatrix2.sh
echo "#!/bin/bash" >> ${tempdir}/batch_thalamus2cortex_right_omatrix2.sh
echo "probtrackx2 --omatrix2 \
--samples=${tempdir}/diffusion.bedpostX/merged \
--mask=${tempdir}/diffusion.bedpostX/nodif_brain_mask \
--xfm=${tempdir}/diffusion.bedpostX/xfms/standard2diff \
--invxfm=${tempdir}/diffusion.bedpostX/xfms/diff2standard \
--seed=${tempdir}/segmentation/thalamus_right_MNI.nii.gz \
--target2=${tempdir}/segmentation/GM_mask_MNI.nii.gz \
--dir=thalamus2cortex_right_omatrix2 \
--loopcheck \
--onewaycondition \
--forcedir \
--opd \
--nsamples=5000" >> ${tempdir}/batch_thalamus2cortex_right_omatrix2.sh
chmod 777 ${tempdir}/batch_thalamus2cortex_right_omatrix2.sh
sbatch --time=3:00:00 ${tempdir}/batch_thalamus2cortex_right_omatrix2.sh
fi
#Left thalamus: kmeans
if [[ -d ${tempdir}/thalamus2cortex_left_omatrix2/ ]] && [[ -d ${tempdir}/segmentation/${outname}_left_seeds/ ]] ;
then
echo "kmeans of left thalamus already run with: ${outname}"
echo "kmeans of left thalamus already run with: ${outname}" >> ${log}
else
echo "Running kmeans segmentation of left thalamus with: ${outname}"
echo "Running kmeans segmentation of left thalamus with: ${outname}" >> ${log}
#omatrix2
touch ${tempdir}/batch_thalamus2cortex_left_omatrix2.sh
echo "#!/bin/bash" >> ${tempdir}/batch_thalamus2cortex_left_omatrix2.sh
echo "probtrackx2 --omatrix2 \
--samples=${tempdir}/diffusion.bedpostX/merged \
--mask=${tempdir}/diffusion.bedpostX/nodif_brain_mask \
--xfm=${tempdir}/diffusion.bedpostX/xfms/standard2diff \
--invxfm=${tempdir}/diffusion.bedpostX/xfms/diff2standard \
--seed=${tempdir}/segmentation/thalamus_left_MNI.nii.gz \
--target2=${tempdir}/segmentation/GM_mask_MNI.nii.gz \
--dir=thalamus2cortex_left_omatrix2 \
--loopcheck \
--onewaycondition \
--forcedir \
--opd \
--nsamples=5000" >> ${tempdir}/batch_thalamus2cortex_left_omatrix2.sh
chmod 777 ${tempdir}/batch_thalamus2cortex_left_omatrix2.sh
sbatch --time=3:00:00 ${tempdir}/batch_thalamus2cortex_left_omatrix2.sh
fi
#Right pallidum: kmeans
if [[ -d ${tempdir}/pallidum2cortex_right_omatrix2/ ]] && [[ -d ${tempdir}/segmentation/${outname}_right_seeds/ ]] ;
then
echo "kmeans of right pallidum already run with: ${outname}"
echo "kmeans of right pallidum already run with: ${outname}" >> ${log}
else
echo "Running kmeans segmentation of right pallidum with: ${outname}"
echo "Running kmeans segmentation of right pallidum with: ${outname}" >> ${log}
#Alternative segmentation method (hypothesis free): requires subsequent Matlab run for kmeans segmentation
#omatrix2
touch ${tempdir}/batch_pallidum2cortex_right_omatrix2.sh
echo "#!/bin/bash" >> ${tempdir}/batch_pallidum2cortex_right_omatrix2.sh
echo "probtrackx2 --omatrix2 \
--samples=${tempdir}/diffusion.bedpostX/merged \
--mask=${tempdir}/diffusion.bedpostX/nodif_brain_mask \
--xfm=${tempdir}/diffusion.bedpostX/xfms/standard2diff \
--invxfm=${tempdir}/diffusion.bedpostX/xfms/diff2standard \
--seed=${tempdir}/segmentation/pallidum_right_MNI.nii.gz \
--target2=${tempdir}/segmentation/GM_mask_MNI.nii.gz \
--dir=pallidum2cortex_right_omatrix2 \
--loopcheck \
--onewaycondition \
--forcedir \
--opd \
--nsamples=5000" >> ${tempdir}/batch_pallidum2cortex_right_omatrix2.sh
chmod 777 ${tempdir}/batch_pallidum2cortex_right_omatrix2.sh
sbatch --time=3:00:00 ${tempdir}/batch_pallidum2cortex_right_omatrix2.sh
fi
#Left pallidum: kmeans
if [[ -d ${tempdir}/pallidum2cortex_left_omatrix2/ ]] && [[ -d ${tempdir}/segmentation/${outname}_left_seeds/ ]] ;
then
echo "kmeans of left pallidum already run with: ${outname}"
echo "kmeans of left pallidum already run with: ${outname}" >> ${log}
else
echo "Running kmeans segmentation of left pallidum with: ${outname}"
echo "Running kmeans segmentation of left pallidum with: ${outname}" >> ${log}
#omatrix2
touch ${tempdir}/batch_pallidum2cortex_left_omatrix2.sh
echo "#!/bin/bash" >> ${tempdir}/batch_pallidum2cortex_left_omatrix2.sh
echo "probtrackx2 --omatrix2 \
--samples=${tempdir}/diffusion.bedpostX/merged \
--mask=${tempdir}/diffusion.bedpostX/nodif_brain_mask \
--xfm=${tempdir}/diffusion.bedpostX/xfms/standard2diff \
--invxfm=${tempdir}/diffusion.bedpostX/xfms/diff2standard \
--seed=${tempdir}/segmentation/pallidum_left_MNI.nii.gz \
--target2=${tempdir}/segmentation/GM_mask_MNI.nii.gz \
--dir=pallidum2cortex_left_omatrix2 \
--loopcheck \
--onewaycondition \
--forcedir \
--opd \
--nsamples=5000" >> ${tempdir}/batch_pallidum2cortex_left_omatrix2.sh
chmod 777 ${tempdir}/batch_pallidum2cortex_left_omatrix2.sh
sbatch --time=3:00:00 ${tempdir}/batch_pallidum2cortex_left_omatrix2.sh
fi
################################
## 5. Post tractography analysis
################################
#Cluster
echo "Running clustering"
echo "Running clustering" >> ${log}
#right thalamus
#68 parcels (no subcortical)
#61, 57, 55 [-7 -11 -13] SMA-PMC/M1/S1 right
cluster --in=thalamus2cortex_right_cluster/seeds_to_Seg0061.nii.gz --thresh=100 --othresh=thalamus2cortex_right_cluster/cluster_61
cluster --in=thalamus2cortex_right_cluster/seeds_to_Seg0057.nii.gz --thresh=100 --othresh=thalamus2cortex_right_cluster/cluster_57
cluster --in=thalamus2cortex_right_cluster/seeds_to_Seg0055.nii.gz --thresh=100 --othresh=thalamus2cortex_right_cluster/cluster_55
cluster --in=thalamus2cortex_right_cluster/seeds_to_dentate_left.nii.gz --thresh=100 --othresh=thalamus2cortex_right_cluster/cluster_dentate_left
#left thalamus
#68 parcels (no subcortical)
#27, 23, 21 [-7 -11 -13] SMA-PMC/M1/S1 left
cluster --in=thalamus2cortex_left_cluster/seeds_to_Seg0027.nii.gz --thresh=100 --othresh=thalamus2cortex_left_cluster/cluster_27
cluster --in=thalamus2cortex_left_cluster/seeds_to_Seg0023.nii.gz --thresh=100 --othresh=thalamus2cortex_left_cluster/cluster_23
cluster --in=thalamus2cortex_left_cluster/seeds_to_Seg0021.nii.gz --thresh=100 --othresh=thalamus2cortex_left_cluster/cluster_21
cluster --in=thalamus2cortex_left_cluster/seeds_to_dentate_right.nii.gz --thresh=100 --othresh=thalamus2cortex_left_cluster/cluster_dentate_right
#Hard segmentation
echo "Running hard segmentation"
echo "Running hard segmentation" >> ${log}
#right thalamus
find_the_biggest thalamus2cortex_right/seeds_to_* thalamus2cortex_right/biggest_segmentation
#left thalamus
find_the_biggest thalamus2cortex_left/seeds_to_* thalamus2cortex_left/biggest_segmentation
#right pallidum
find_the_biggest pallidum2cortex_right/seeds_to_* pallidum2cortex_right/biggest_segmentation
#left pallidum
find_the_biggest pallidum2cortex_left/seeds_to_* pallidum2cortex_left/biggest_segmentation
#kmeans
echo "Running kmeans"
echo "Running kmeans" >> ${log}
#right thalamus
cd thalamus2cortex_right_omatrix2
file_matlab=temp_Segmentation
echo "segmentation_clustering;exit" > ${file_matlab}.m
matlab -nodisplay -r "${file_matlab}"
fslcpgeom fdt_paths clusters
cd ..
#left thalamus
cd thalamus2cortex_left_omatrix2
file_matlab=temp_Segmentation
echo "segmentation_clustering;exit" > ${file_matlab}.m
matlab -nodisplay -r "${file_matlab}"
fslcpgeom fdt_paths clusters
cd ..
#right pallidum
cd pallidum2cortex_right_omatrix2
file_matlab=temp_Segmentation
echo "segmentation_clustering;exit" > ${file_matlab}.m
matlab -nodisplay -r "${file_matlab}"
fslcpgeom fdt_paths clusters
cd ..
#left pallidum
cd pallidum2cortex_left_omatrix2
file_matlab=temp_Segmentation
echo "segmentation_clustering;exit" > ${file_matlab}.m
matlab -nodisplay -r "${file_matlab}"
fslcpgeom fdt_paths clusters
cd ..
echo "Cleanup of slurm* files"
echo "Cleanup of slurm* files" >> ${log}
rm slurm*
rm batch*
echo $(date) >> ${log}
echo "all done with bit_segmentation"
echo "all done with bit_segmentation" >> ${log}
#fin