Chapter 3 Programming and Utility codes

3.1 Tips for Neuroimage processing

  • before using -mas fucntion in FSL, make sure they have same FOV and resoluiton

  • Remove skull first then do the registration !!!

  • Make a good plan for testing/trials and errors to debug efficiently; e.g. name every file and folder in well-formated and structured way; to see if the pipeline is working or not by check output files

3.2 Useful Regex examples

  • How to find the content inside of Square bracket?

You can use the following regex globally: find content in square bracket

  • \[ : [ is a meta char and needs to be escaped if you want to match it literally.
  • (.*?) : match everything in a non-greedy way and capture it.
  • \] : ] is a meta char and needs to be escaped if you want to match it literally.

[3-7] will match a single digit in the range 3 to 7.

  • How to Match multiple digits
\d\d       will match 2 consecutive digits
\d+        will match 1 or more consecutive digits
\d*        will match 0 or more consecutive digits
\d{3}      will match 3 consecutive digits
\d{3,6}    will match 3 to 6 consecutive digits
\d{3,}     will match 3 or more consecutive digits

NOTICE! The \\d in the above examples can be replaced with a number range:

[3-7][3-7]    will match 2 consecutive digits that are in the range 3 to 7
[3-7]+        will match 1 or more consecutive digits that are in the range 3 to 7
[3-7]*        will match 0 or more consecutive digits that are in the range 3 to 7
[3-7]{3}      will match 3 consecutive digits that are in the range 3 to 7
[3-7]{3,6}    will match 3 to 6 consecutive digits that are in the range 3 to 7
[3-7]{3,}     will match 3 or more consecutive digits that are in the range 3 to 7

3.3 Useful Shell commands and examples

3.3.1 Find files and copy to target folder


cd /project/3015083.01/processed
procfolder=/project/3015083.01/processed
for ID in 036 090 090 140 161 190 200 221 286 296 300 308 363 366 367 404 434 439 444 485 507;
do
find -name sub*ivim-data.nii.gz -exec cp {} /project/3015083.01/mengfei/Munich_IVIM/IVIM \;
cp "${procfolder}"/sub-"${ID}"/ses-mri01/ivim/sub-"${ID}"_ses-mri01_ivim-data.nii.gz /project/3015083.01/mengfei/Munich_IVIM/IVIM/
done

3.3.2 Kill a range of running batch jobs

qdel {18280..18285}

3.3.3 sed

Using sed to change file names

> echo "./pentaray_run2/Trace_220560.dat" |  sed 's/.*Trace_//' | sed 's/.dat//'

> output:  220560.dat --> 220560 

`.*` is very important

3.3.4 tr

The tr command in UNIX is a command line utility for translating or deleting characters. It supports a range of transformations including uppercase to lowercase, squeezing repeating characters, deleting specific characters and basic find and replace. It can be used with UNIX pipes to support more complex translation. tr stands for translate.

Syntax :

$ tr [OPTION] SET1 [SET2] Options

-c : complements the set of characters in string.i.e., operations apply to characters not in the given set -d : delete characters in the first set from the output. -s : replaces repeated characters listed in the set1 with single occurrence -t : truncates set1

Sample Commands

  1. How to convert lower case to upper case

To convert from lower case to upper case the predefined sets in tr can be used.

$cat greekfile Output:

WELCOME TO GeeksforGeeks $cat greekfile | tr “[a-z]” “[A-Z]” Output:

WELCOME TO GEEKSFORGEEKS or

$cat geekfile | tr “[:lower:]” “[:upper:]” Output:

WELCOME TO GEEKSFORGEEKS

  1. How to translate white-space to tabs

The following command will translate all the white-space to tabs

$ echo “Welcome To GeeksforGeeks” | tr [:space:] ’ Output:

Welcome To GeeksforGeeks

  1. How to translate braces into parenthesis You can also translate from and to a file. In this example we will translate braces in a file with parenthesis.

$cat greekfile Output:

{WELCOME TO} GeeksforGeeks $ tr ‘{}’ ‘()’ newfile.txt

Output:

(WELCOME TO) GeeksforGeeks The above command will read each character from “geekfile.txt”, translate if it is a brace, and write the output in “newfile.txt”.

  1. How to use squeeze repetition of characters using -s

To squeeze repeat occurrences of characters specified in a set use the -s option. This removes repeated instances of a character. OR we can say that,you can convert multiple continuous spaces with a single space

$ echo “Welcome To GeeksforGeeks” | tr -s [:space:] ’ ’ Output:

Welcome To GeeksforGeeks

  1. How to delete specified characters using -d option

To delete specific characters use the -d option.This option deletes characters in the first set specified.

$ echo “Welcome To GeeksforGeeks” | tr -d ‘w’ Output:

elcome To GeeksforGeeks

  1. To remove all the digits from the string, use

$ echo “my ID is 73535” | tr -d [:digit:] Output:

my ID is

  1. How to complement the sets using -c option

You can complement the SET1 using -c option. For example, to remove all characters except digits, you can use the following.

$ echo “my ID is 73535” | tr -cd [:digit:] Output:

73535

for i in $(ls);
do
  mv $i ${i:(-3)};
done  # extract the final three characters

3.3.5 Batch job submit

list="/project/3015083.01/mengfei/script/psmd_2006_list.sh"

while IFS= read -r line
do
    #echo "this_is: ${line} "
    #echo "ID_is...."
    ID=$(echo ${line} | awk -F'[ ]' '{print $2}')   ## echo is important here; field separator is [space]
    echo "sub_ID: ${ID}.....calculating PSMD....."
    echo "$line -i" | qsub -N \'PSMD_${ID}\' -l 'nodes=1:ppn=4,mem=6gb,walltime=6:00:00'  # baclslash was used to escape the single quote, so that ${ID} can be contained.
done < $list

3.3.6 Copy files after checking file existence


cd /project/3015083.01/processed
list=$(ls | head -3 | awk -F'-' '{print $2}')   # for sub-001, to extract 011.
##ID=$(echo ${line} | awk -F'[ ]' '{print $2}') ## echo is important here; field separator is [space]
for ID in ${list};
do
    echo "this is ${ID}"
    cd /project/3015083.01/nifti/sub-"${ID}"/ses-mri01/anat/
    if [ -f "*flair.nii.gz" ] ; then
        /project/3015083.01/mengfei/raw_backup/nifti/resliced_to_2006

    else
        echo "FLAIR nifti does not exist."
    fi
done

3.4 ITK_SNAP command line tutorials

~$ itksnap -h

ITK-SnAP Command Line Usage:

 /Applications/ITK-SNAP.app/Contents/MacOS/ITK-SNAP \[options\] \[main\_image\]

Image Options:

 -g FILE              : Load the main image from FILE

 -s FILE              : Load the segmentation image from FILE

 -l FILE              : Load label descriptions from FILE

 -o FILE \[FILE+\]      : Load additional images from FILE

 :   (multiple files may be provided)

 -w FILE              : Load workspace from FILE

 :   (-w cannot be mixed with -g, -s, -l, -o options)

Additional Options:

 -z FACTOR            : Specify initial zoom in screen pixels/mm

 --cwd PATH           : Start with PATH as the initial directory

Debugging/Testing Options:

 --debug-events       : Dump information regarding UI events

 --test list          : List available tests.

 --test TESTID        : Execute a test.

 --testdir DIR        : Set the root directory for tests.

 --testacc factor     : Adjust the interval between test commands by factor (e.g., 0.5).

3.4.1 ITK-SNAP open serial images once for all

module load itk-snap
#cd /project/3015083.01/processed/
dir=/project/3015006.07/Data

opensubject(){
itksnap -g /opt/fsl/6.0.3/data/standard/MNI152_T1_2mm.nii.gz -o /project/flair_brain.nii.gz \

 &> /dev/null
}

while :
do
echo ""
echo "Enter ID (e.g. 005) for viewing or leave blank for rating of next subject. Press Ctrl+C to exit!"
read number
[ -z $number ] && runloop || opensubject
done

3.5 Compress files/folders with gzip and tar

procfolder=/project/3015083.01/processed

cd ${procfolder}  # this is home folder for processed folders
#/project/3015083.01/processed/sub-004/ses-mri01/SWI/
swi_folder="SWI"
for subject in $(ls);

do
    echo ${subject}
    cd "${procfolder}"/"${subject}"/ses-mri01
    if [ -d "${procfolder}"/"${subject}"/ses-mri01/SWI ]; then  ## space after "[" is very important
        cd "${procfolder}"/"${subject}"/ses-mri01/SWI
        echo "found SWI folder and compressing...."
        gzip *.nii
    else
        echo "there is no SWI folder"
        cd ${procfolder}
    fi
done
## examples for zip a folder [lotte]
tar -zcvf lotte.zip lotte/

Often, you’ll create a zip archive of a directory including the content of subdirectories. The -r option allows you to traverse the whole directory structure recursively:

zip -r archivename.zip directory_name

You can also add multiple files and directories in the same archive:

zip -r archivename.zip directory_name1 directory_name2 file1 file1

3.6 Most commonly used codes

#!/bin/bash

# Define the root directory where the search should start
root_directory="/path/to/root/directory"

# Find all files matching the pattern within subdirectories named "ppn", then delete
find . -type d -name "ppn" -exec find {} -type f -name "*_t1.nii.gz" \; -exec rm {} \;
find . -type d -name "ppn" -exec find {} -type f -name "*_t1.mgz" \; -exec rm {} \;
find . -type d -name "ppn" -exec find {} -type f -name "*_t1_brain.mgz" \; -exec rm {} \;

find . -type d -name "Visit-00" -exec find {} -type f -name L_PPN_in_FW.nii.gz \; | wc -l ;

# find file ending wiht .nii and delete

find . -type d -name "*.nii" -exec rm -r {} +

find . -type d -name "*_anat" -exec rm -r {} +


# Find and delete directories matching the pattern 'flair/temp'
# The -type d ensures we're only looking at directories
find . -type d -name temp -path '*/flair/temp' -exec rm -r {} +

find -name L_PPN_in_FW.nii.gz | wc 

## convert rows to columns 

tr '\n' ' ' < /media/neuro/16t/gdph_pH_recon/gdph_PH_t1_recon_ses-1.txt > /media/neuro/16t/gdph_pH_recon/ses-1_IDs.txt
tr '\n' ' ' < /media/neuro/16t/gdph_pH_recon/gdph_PH_t1_recon_ses-2.txt > /media/neuro/16t/gdph_pH_recon/ses-2_IDs.txt


## gdph_PH-ses-1 recon_all output to tables

 export SUBJECTS_DIR=/media/neuro/16t/gdph_pH_recon/ses-1/
## -- tablefile can not be a/b/c.txt
asegstats2table --subjects sub-022 sub-044 sub-045 sub-046 sub-047 sub-048 sub-049 sub-050 sub-051 sub-052 sub-053 sub-054 sub-055 sub-056 sub-057 sub-058 sub-059 sub-060 sub-061 sub-062 sub-063 sub-064 sub-065 sub-066 sub-067 sub-068 sub-069 sub-070 sub-071 sub-073 sub-074 sub-075 sub-076 sub-078 sub-079 sub-080 sub-081 sub-082 sub-083 sub-085 sub-087 sub-088 sub-089 sub-179 sub-217 sub-260 sub-374 sub-397 sub-409 sub-420 sub-421 sub-422 sub-423 sub-424 sub-425 sub-426 sub-427 sub-428 sub-429 sub-430 sub-431 sub-432 sub-433 sub-434 sub-435 sub-436 sub-437 sub-438 sub-439 sub-440 sub-441 sub-442 sub-443 sub-444 sub-445 sub-446 sub-447 sub-448 sub-449 sub-450 sub-451 sub-452 sub-453 sub-454 sub-455 sub-456 sub-457 sub-458 sub-459 sub-460 sub-461 sub-462 sub-463 sub-464 sub-465 sub-466 sub-467 sub-468 sub-469 sub-470 sub-471 sub-473 sub-474 sub-475 sub-476 sub-477 sub-478 sub-479 sub-480 sub-482 sub-483 sub-484 sub-486 sub-487 sub-488 sub-489 sub-490 sub-492 sub-493 sub-494 sub-495 sub-496 sub-497 sub-498 sub-499 sub-500 sub-501 sub-502 sub-503 sub-504 sub-505 sub-506 sub-509 sub-510 sub-511 sub-513 sub-515 sub-517 sub-518 sub-520 sub-521 sub-522 sub-523 sub-524 sub-525 sub-526 sub-527 sub-528 sub-529 sub-530 sub-531 sub-532 sub-533 sub-534 sub-536 sub-537 sub-538 sub-539 sub-540 sub-541 sub-542 sub-543 sub-544 sub-545 sub-546 sub-547 sub-548 sub-549 sub-550 sub-551 sub-552 sub-554 sub-555 sub-556 sub-557 sub-558 sub-559 sub-560 sub-561 sub-562 sub-563 sub-564 sub-565 sub-566 sub-567 sub-568 sub-569 sub-570 sub-571 sub-572 sub-573 sub-574 sub-576 sub-577 sub-578 sub-579 sub-580 sub-581 sub-582 sub-583 sub-584 sub-585 sub-586 sub-588 sub-589 sub-590 sub-591 sub-592 sub-593 sub-594 sub-595 sub-596 sub-597 sub-598 sub-599 sub-601 sub-602 sub-603 sub-604 sub-605 sub-606 sub-607 sub-608 sub-609 sub-610 sub-611 sub-612 sub-613 sub-614 sub-615 sub-616 sub-617 sub-619 sub-620 sub-621 sub-622 sub-623 sub-624 sub-625 sub-626 sub-627 sub-628 sub-629 sub-630 sub-631 sub-632 sub-633 sub-634 sub-635 sub-636 sub-637 sub-638 sub-639 sub-640 sub-641 sub-642 sub-643 sub-644 sub-645 sub-647 sub-648 sub-649 sub-651 sub-652 sub-653 sub-654 sub-655 sub-656 sub-657 sub-658 sub-659 sub-660 sub-662 sub-663 sub-664 sub-665 sub-666 sub-667 sub-668 sub-669 sub-670 sub-671 sub-672 sub-673 sub-674 sub-675 sub-676 sub-677 sub-678 sub-692 sub-698 sub-699 sub-703 sub-704 sub-705 sub-706 sub-707 sub-708 sub-710 sub-711 sub-713 sub-714 sub-715 sub-716 sub-717 sub-718 sub-720 sub-721 sub-723 sub-725 sub-726 sub-727 sub-728 sub-729 sub-730 sub-731 sub-732 sub-733 sub-734 sub-735 sub-736 sub-737 sub-738 sub-739 sub-740 sub-741 sub-742 sub-743 sub-744 sub-745 sub-747 sub-748 sub-750 sub-751 sub-752 sub-754 sub-756 sub-757 sub-758 sub-759 sub-760 sub-761 sub-762 sub-763 sub-764 sub-765 sub-766 sub-767 sub-768 sub-769 sub-770 sub-774 sub-775 sub-777 sub-778 sub-779 sub-780 sub-781 sub-782 sub-783 sub-784 sub-787 sub-788 sub-791 sub-792 sub-793 sub-794 sub-795 sub-796 sub-797 sub-799 sub-800 sub-801 sub-802 sub-803 sub-804 sub-805 sub-806 sub-807 sub-808 sub-809 sub-815 sub-816  \
--meas volume --tablefile gdph_ses-1_aseg_stats.txt  

aparcstats2table --subjects sub-022 sub-044 sub-045 sub-046 sub-047 sub-048 sub-049 sub-050 sub-051 sub-052 sub-053 sub-054 sub-055 sub-056 sub-057 sub-058 sub-059 sub-060 sub-061 sub-062 sub-063 sub-064 sub-065 sub-066 sub-067 sub-068 sub-069 sub-070 sub-071 sub-073 sub-074 sub-075 sub-076 sub-078 sub-079 sub-080 sub-081 sub-082 sub-083 sub-085 sub-087 sub-088 sub-089 sub-179 sub-217 sub-260 sub-374 sub-397 sub-409 sub-420 sub-421 sub-422 sub-423 sub-424 sub-425 sub-426 sub-427 sub-428 sub-429 sub-430 sub-431 sub-432 sub-433 sub-434 sub-435 sub-436 sub-437 sub-438 sub-439 sub-440 sub-441 sub-442 sub-443 sub-444 sub-445 sub-446 sub-447 sub-448 sub-449 sub-450 sub-451 sub-452 sub-453 sub-454 sub-455 sub-456 sub-457 sub-458 sub-459 sub-460 sub-461 sub-462 sub-463 sub-464 sub-465 sub-466 sub-467 sub-468 sub-469 sub-470 sub-471 sub-473 sub-474 sub-475 sub-476 sub-477 sub-478 sub-479 sub-480 sub-482 sub-483 sub-484 sub-486 sub-487 sub-488 sub-489 sub-490 sub-492 sub-493 sub-494 sub-495 sub-496 sub-497 sub-498 sub-499 sub-500 sub-501 sub-502 sub-503 sub-504 sub-505 sub-506 sub-509 sub-510 sub-511 sub-513 sub-515 sub-517 sub-518 sub-520 sub-521 sub-522 sub-523 sub-524 sub-525 sub-526 sub-527 sub-528 sub-529 sub-530 sub-531 sub-532 sub-533 sub-534 sub-536 sub-537 sub-538 sub-539 sub-540 sub-541 sub-542 sub-543 sub-544 sub-545 sub-546 sub-547 sub-548 sub-549 sub-550 sub-551 sub-552 sub-554 sub-555 sub-556 sub-557 sub-558 sub-559 sub-560 sub-561 sub-562 sub-563 sub-564 sub-565 sub-566 sub-567 sub-568 sub-569 sub-570 sub-571 sub-572 sub-573 sub-574 sub-576 sub-577 sub-578 sub-579 sub-580 sub-581 sub-582 sub-583 sub-584 sub-585 sub-586 sub-588 sub-589 sub-590 sub-591 sub-592 sub-593 sub-594 sub-595 sub-596 sub-597 sub-598 sub-599 sub-601 sub-602 sub-603 sub-604 sub-605 sub-606 sub-607 sub-608 sub-609 sub-610 sub-611 sub-612 sub-613 sub-614 sub-615 sub-616 sub-617 sub-619 sub-620 sub-621 sub-622 sub-623 sub-624 sub-625 sub-626 sub-627 sub-628 sub-629 sub-630 sub-631 sub-632 sub-633 sub-634 sub-635 sub-636 sub-637 sub-638 sub-639 sub-640 sub-641 sub-642 sub-643 sub-644 sub-645 sub-647 sub-648 sub-649 sub-651 sub-652 sub-653 sub-654 sub-655 sub-656 sub-657 sub-658 sub-659 sub-660 sub-662 sub-663 sub-664 sub-665 sub-666 sub-667 sub-668 sub-669 sub-670 sub-671 sub-672 sub-673 sub-674 sub-675 sub-676 sub-677 sub-678 sub-692 sub-698 sub-699 sub-703 sub-704 sub-705 sub-706 sub-707 sub-708 sub-710 sub-711 sub-713 sub-714 sub-715 sub-716 sub-717 sub-718 sub-720 sub-721 sub-723 sub-725 sub-726 sub-727 sub-728 sub-729 sub-730 sub-731 sub-732 sub-733 sub-734 sub-735 sub-736 sub-737 sub-738 sub-739 sub-740 sub-741 sub-742 sub-743 sub-744 sub-745 sub-747 sub-748 sub-750 sub-751 sub-752 sub-754 sub-756 sub-757 sub-758 sub-759 sub-760 sub-761 sub-762 sub-763 sub-764 sub-765 sub-766 sub-767 sub-768 sub-769 sub-770 sub-774 sub-775 sub-777 sub-778 sub-779 sub-780 sub-781 sub-782 sub-783 sub-784 sub-787 sub-788 sub-791 sub-792 sub-793 sub-794 sub-795 sub-796 sub-797 sub-799 sub-800 sub-801 sub-802 sub-803 sub-804 sub-805 sub-806 sub-807 sub-808 sub-809 sub-815 sub-816  \
--hemi lh --meas thickness --tablefile gdph_PH_ses-1_lh_aparc_stats.txt

aparcstats2table --subjects sub-022 sub-044 sub-045 sub-046 sub-047 sub-048 sub-049 sub-050 sub-051 sub-052 sub-053 sub-054 sub-055 sub-056 sub-057 sub-058 sub-059 sub-060 sub-061 sub-062 sub-063 sub-064 sub-065 sub-066 sub-067 sub-068 sub-069 sub-070 sub-071 sub-073 sub-074 sub-075 sub-076 sub-078 sub-079 sub-080 sub-081 sub-082 sub-083 sub-085 sub-087 sub-088 sub-089 sub-179 sub-217 sub-260 sub-374 sub-397 sub-409 sub-420 sub-421 sub-422 sub-423 sub-424 sub-425 sub-426 sub-427 sub-428 sub-429 sub-430 sub-431 sub-432 sub-433 sub-434 sub-435 sub-436 sub-437 sub-438 sub-439 sub-440 sub-441 sub-442 sub-443 sub-444 sub-445 sub-446 sub-447 sub-448 sub-449 sub-450 sub-451 sub-452 sub-453 sub-454 sub-455 sub-456 sub-457 sub-458 sub-459 sub-460 sub-461 sub-462 sub-463 sub-464 sub-465 sub-466 sub-467 sub-468 sub-469 sub-470 sub-471 sub-473 sub-474 sub-475 sub-476 sub-477 sub-478 sub-479 sub-480 sub-482 sub-483 sub-484 sub-486 sub-487 sub-488 sub-489 sub-490 sub-492 sub-493 sub-494 sub-495 sub-496 sub-497 sub-498 sub-499 sub-500 sub-501 sub-502 sub-503 sub-504 sub-505 sub-506 sub-509 sub-510 sub-511 sub-513 sub-515 sub-517 sub-518 sub-520 sub-521 sub-522 sub-523 sub-524 sub-525 sub-526 sub-527 sub-528 sub-529 sub-530 sub-531 sub-532 sub-533 sub-534 sub-536 sub-537 sub-538 sub-539 sub-540 sub-541 sub-542 sub-543 sub-544 sub-545 sub-546 sub-547 sub-548 sub-549 sub-550 sub-551 sub-552 sub-554 sub-555 sub-556 sub-557 sub-558 sub-559 sub-560 sub-561 sub-562 sub-563 sub-564 sub-565 sub-566 sub-567 sub-568 sub-569 sub-570 sub-571 sub-572 sub-573 sub-574 sub-576 sub-577 sub-578 sub-579 sub-580 sub-581 sub-582 sub-583 sub-584 sub-585 sub-586 sub-588 sub-589 sub-590 sub-591 sub-592 sub-593 sub-594 sub-595 sub-596 sub-597 sub-598 sub-599 sub-601 sub-602 sub-603 sub-604 sub-605 sub-606 sub-607 sub-608 sub-609 sub-610 sub-611 sub-612 sub-613 sub-614 sub-615 sub-616 sub-617 sub-619 sub-620 sub-621 sub-622 sub-623 sub-624 sub-625 sub-626 sub-627 sub-628 sub-629 sub-630 sub-631 sub-632 sub-633 sub-634 sub-635 sub-636 sub-637 sub-638 sub-639 sub-640 sub-641 sub-642 sub-643 sub-644 sub-645 sub-647 sub-648 sub-649 sub-651 sub-652 sub-653 sub-654 sub-655 sub-656 sub-657 sub-658 sub-659 sub-660 sub-662 sub-663 sub-664 sub-665 sub-666 sub-667 sub-668 sub-669 sub-670 sub-671 sub-672 sub-673 sub-674 sub-675 sub-676 sub-677 sub-678 sub-692 sub-698 sub-699 sub-703 sub-704 sub-705 sub-706 sub-707 sub-708 sub-710 sub-711 sub-713 sub-714 sub-715 sub-716 sub-717 sub-718 sub-720 sub-721 sub-723 sub-725 sub-726 sub-727 sub-728 sub-729 sub-730 sub-731 sub-732 sub-733 sub-734 sub-735 sub-736 sub-737 sub-738 sub-739 sub-740 sub-741 sub-742 sub-743 sub-744 sub-745 sub-747 sub-748 sub-750 sub-751 sub-752 sub-754 sub-756 sub-757 sub-758 sub-759 sub-760 sub-761 sub-762 sub-763 sub-764 sub-765 sub-766 sub-767 sub-768 sub-769 sub-770 sub-774 sub-775 sub-777 sub-778 sub-779 sub-780 sub-781 sub-782 sub-783 sub-784 sub-787 sub-788 sub-791 sub-792 sub-793 sub-794 sub-795 sub-796 sub-797 sub-799 sub-800 sub-801 sub-802 sub-803 sub-804 sub-805 sub-806 sub-807 sub-808 sub-809 sub-815 sub-816  \
--hemi rh --meas thickness --tablefile gdph_PH_ses-1_rh_aparc_stats.txt

## gdph_PH-ses-2 recon to tables

 export SUBJECTS_DIR=/media/neuro/16t/gdph_pH_recon/ses-2/

asegstats2table --subjects sub-004 sub-008 sub-015 sub-017 sub-020 sub-034 sub-038 sub-052 sub-053 sub-056 sub-093 sub-094 sub-123 sub-124 sub-136 sub-174 sub-203 sub-222 sub-232 sub-238 sub-245 sub-262 sub-265 sub-297 sub-321 sub-323 sub-355 sub-360 sub-367 sub-369 sub-376 sub-381 sub-382 sub-383 sub-384 sub-386 sub-387 sub-392 sub-420 sub-444 sub-532 sub-534 sub-565 sub-589 sub-611 \
--meas volume --tablefile gdph_PH_ses-2_aseg_stats.txt  

aparcstats2table --subjects sub-004 sub-008 sub-015 sub-017 sub-020 sub-034 sub-038 sub-052 sub-053 sub-056 sub-093 sub-094 sub-123 sub-124 sub-136 sub-174 sub-203 sub-222 sub-232 sub-238 sub-245 sub-262 sub-265 sub-297 sub-321 sub-323 sub-355 sub-360 sub-367 sub-369 sub-376 sub-381 sub-382 sub-383 sub-384 sub-386 sub-387 sub-392 sub-420 sub-444 sub-532 sub-534 sub-565 sub-589 sub-611 \
--hemi lh --meas thickness --tablefile gdph_PH_ses-2_lh_aparc_stats.txt

aparcstats2table --subjects sub-004 sub-008 sub-015 sub-017 sub-020 sub-034 sub-038 sub-052 
sub-053 sub-056 sub-093 sub-094 sub-123 sub-124 sub-136 sub-174 sub-203 sub-222 sub-232 sub-238 
sub-245 sub-262 sub-265 sub-297 sub-321 sub-323 sub-355 sub-360 sub-367 sub-369 sub-376 sub-381 
sub-382 sub-383 sub-384 sub-386 sub-387 sub-392 sub-420 sub-444 sub-532 sub-534 sub-565 sub-589 sub-611 \
--hemi rh --meas thickness --tablefile gdph_PH_ses-2_rh_aparc_stats.txt




## find , then copy

 find . -type d -name "Visit-00" -exec cp -r {} /home/neuro/MRI_GDPH/ppmi/20220919_ppmi/PPMI_to_HaoLi/HC_prodromal \;


find . -type d -name "Visit-00" -exec cp -r {} /home/neuro/MRI_GDPH/ppmi/20220919_ppmi/PPMI_to_HaoLi/PD \;

find -name R_PPN_in_FW.nii.gz | sort -n > ppmi_R_PPN.txt


## Freeivew Visulization

cd /media/neuro/16t/ppmi_t1_long_all/t1_longitudinal_freesurfer_template_all
freeview -v 3102_BL.long.3102_template/mri/brain.mgz 3102_v06.long.3102_template/mri/brain.mgz

  export SUBJECTS_DIR=/home/neuro/MRI_GDPH/ppmi/3102-demo/recon
  start=`date +%s`

recon-all -s sub-3102 -i /home/neuro/MRI_GDPH/ppmi/3102-demo/3102_Visit-00_t1.nii.gz -all

freeview -v aparc+aseg.mgz:colormap=lut 
freeview -v aseg.mgz:colormap=lut 

freeview -f ./3102_BL.long.3102_template_rh_MDt.mgh:colormap=lut
mri_convert 3102_BL.long.3102_template_rh_FW.mgh 3102_BL.long.3102_template_rh_FW.nii.gz

freeview -v brain.nii.gz brain.sclimbic.mgz:colormap=lut

freeview -v aseg.mgz:colormap=lut:opacity=0.2 

freeview -v \
./mri/T1.mgz \
./mri/wm.mgz \
./mri/brainmask.mgz \
./mri/aseg.mgz:colormap=lut:opacity=0.2 \
-f ./surf/lh.white:edgecolor=blue \
./surf/lh.pial:edgecolor=red \
./surf/rh.white:edgecolor=blue \
./surf/rh.pial:edgecolor=red



tksurfer PD-3102 lh inflated -annotation lh.aparc.annot

freeview -f ./surf/lh.inflated:overlay=/home/neuro/MRI_GDPH/ppmi/20220919_ppmi/ppmi_PD_processed/3105/Visit-00/dwi/freesurfer/PD-3105_lh_FW.mgh \
./surf/lh.pial:annot=aparc.annot:name=pial_aparc_des:visible=0 

freeview -v /media/neuro/16t/ppmi_BL_recon/PD-3105/mri/orig.mgz
freeview /home/neuro/MRI_GDPH/ppmi/20220919_ppmi/ppmi_PD_processed/3105/Visit-00/dwi/freesurfer/lh.white 
freeview /home/neuro/MRI_GDPH/ppmi/20220919_ppmi/ppmi_PD_processed/3105/Visit-00/dwi/freesurfer/PD-3105_lh_FW.mgh

freeview -v /home/neuro/MRI_GDPH/ppmi/20220919_ppmi/ppmi_PD_processed/3105/Visit-00/dwi/freesurfer/PD-3105_lh_FW.mgh

freeview -f  ./surf/lh.pial:annot=aparc.annot:name=pial_aparc:visible=0 \
./surf/lh.pial:annot=aparc.a2009s.annot:name=pial_aparc_des:visible=0 \
./surf/lh.inflated:overlay=lh.thickness:overlay_threshold=0.1,3::name=inflated_thickness:visible=1 \
./surf/lh.inflated:visible=0 \
./surf/lh.white:visible=0 \
./surf/lh.pial \
--viewport 3d

freeview -f ./surf/lh.inflated:overlay=lh.thickness:overlay_threshold=0.1,2::name=inflated_thickness:visible=1 
--viewport 3d

freeview -f  ./surf/lh.pial:annot=aparc.annot:name=pial_aparc:visible=0 \
./surf/lh.pial:annot=aparc.a2009s.annot:name=pial_aparc_des:visible=0 \
./surf/lh.inflated:overlay=lh.thickness:overlay_threshold=0.1,1::name=inflated_thickness:visible=0 \
--viewport 3d


/home/neuro/codebank/GDPH/gdph/gdph_PH_t1_list_20231014.txt

fa_map=/home/neuro/MRI_GDPH/3108_viz_tract/3108_Visit-00_dwi_dtifit_FA.nii.gz
t1=/home/neuro/MRI_GDPH/3108_viz_tract/3108_Visit-00_t1.nii.gz 
ch2=/home/neuro/MRI_GDPH/3108_viz_tract/ch2bet.nii.gz
mcp=/home/neuro/MRI_GDPH/3108_viz_tract/tractseg/bundle_segmentations_merged/all_MCP.nii.gz

icp=/home/neuro/MRI_GDPH/3108_viz_tract/tractseg/bundle_segmentations_merged/all_ICP.nii.gz 
scp=/home/neuro/MRI_GDPH/3108_viz_tract/tractseg/bundle_segmentations_merged/all_SCP.nii.gz



# DSI studio parameter ids for tracts visualization

 A DTI diffusion scheme was used, and a total of 64 diffusion sampling directions were acquired. The b-value was 1000 s/mm². The in-plane resolution was 1.98276 mm. The slice thickness was 2 mm. The accuracy of b-table orientation was examined by comparing fiber orientations with those of a population-averaged template (Yeh et al., Neuroimage 178, 57-68, 2018). The tensor metrics were calculated using DWI with b-value lower than 1750 s/mm². A deterministic fiber tracking algorithm (Yeh et al., PLoS ONE 8(11): e80713, 2013) was used with augmented tracking strategies (Yeh, Neuroimage, 2020 Dec;223:117329) to improve reproducibility. The anisotropy threshold was randomly selected. The angular threshold was randomly selected from 15 degrees to 90 degrees. The step size was randomly selected from 0.5 voxel to 1.5 voxels. Tracks with length shorter than 30 or longer than 200 mm were discarded. A total of 1000000 seeds were placed. Topology-informed pruning (Yeh et al. Neurotherapeutics, 16(1), 52-58, 2019) was applied to the tractography with 2 iteration(s) to remove false connections. 
 parameter_id=c9A99193Fb803FdbF041b484340420Fdc02CDCC4C3Ec 

for lesion_prob in $(cat /home/neuro/caixin/lesion_prob_list.txt  );do
# /home/neuro/caixin/processed/HC-001/ses-1/flair/HC-001_lesion.nii.gz

3.7 PPMI ppmi_tractseg_job_extract_freewater



ppmi_dir=/home/neuro/MRI_GDPH/ppmi/20220919_ppmi
cd  /home/neuro/MRI_GDPH/ppmi/20220919_ppmi

for file in  $(find -name fwc_wls_dti_FA.nii.gz | sort -n );
#file=./ppmi_PD_processed/3826/Visit-04/dwi/freewater/fwc_wls_dti_FA.nii.gz
do
  start=`date +%s`
  echo "==========================
  dwi file is $file"
group_name=$(echo $file | awk -F'/' '{print $2}' ) ## ppmi_HC_prodromal_nifti or ppmi_PD_nifti
# group_name_processed=$(echo $group_name | sed 's/nifti/processed/')   ## ppmi_HC_prodromal_processed or ppmi_PD_processed
subj_id=$(echo $file | awk -F'/' '{print $3}' ) ## 3390
visit_id=$(echo $file | awk -F'/' '{print $4}' )  ## Visit-04

echo "=====================
group_name is $group_name
subj_id is $subj_id
visit_id is  $visit_id
group_name_processed is  $group_name_processed
=================================="
   ## fwc = free water corrected
      fwc_md_file="$ppmi_dir"/"$group_name"/"$subj_id"/"$visit_id"/dwi/freewater/fwc_wls_dti_MD.nii.gz
      fwc_fa_file="$ppmi_dir"/"$group_name"/"$subj_id"/"$visit_id"/dwi/freewater/fwc_wls_dti_FA.nii.gz
      fw_file="$ppmi_dir"/"$group_name"/"$subj_id"/"$visit_id"/dwi/freewater/wls_dti_FW.nii.gz
      roi_dir="$ppmi_dir"/"$group_name"/"$subj_id"/"$visit_id"/dwi/tractseg/bundle_segmentations_merged

      cp -v "$ppmi_dir"/"$group_name"/"$subj_id"/"$visit_id"/dwi/tractseg/bundle_segmentations/CC_*.nii.gz  "$ppmi_dir"/"$group_name"/"$subj_id"/"$visit_id"/dwi/tractseg/bundle_segmentations_merged
      # cp /"$ppmi_dir"/"$group_name_processed"/"$subj_id"/"$visit_id"/dwi/tractseg/bundle_segmentations/CA.nii.gz /"$ppmi_dir"/"$group_name_processed"/"$subj_id"/"$visit_id"/dwi/tractseg/bundle_segmentations_merged/all_CA.nii.gz
      # cp /"$ppmi_dir"/"$group_name_processed"/"$subj_id"/"$visit_id"/dwi/tractseg/bundle_segmentations/CC.nii.gz /"$ppmi_dir"/"$group_name_processed"/"$subj_id"/"$visit_id"/dwi/tractseg/bundle_segmentations_merged/all_CC.nii.gz
      # cp /"$ppmi_dir"/"$group_name_processed"/"$subj_id"/"$visit_id"/dwi/tractseg/bundle_segmentations/MCP.nii.gz /"$ppmi_dir"/"$group_name_processed"/"$subj_id"/"$visit_id"/dwi/tractseg/bundle_segmentations_merged/all_MCP.nii.gz

      for roi_file in ${roi_dir}/*.nii.gz;
      do
      roi_name=$(basename ${roi_file} .nii.gz )
      fwc_md_mean=$(fslstats ${fwc_md_file} -k ${roi_file} -M )
      fwc_fa_mean=$(fslstats ${fwc_fa_file} -k ${roi_file} -M )
      fw_mean=$(fslstats ${fw_file} -k ${roi_file} -M )
      echo "group_name subj_id visit_id roi_name: fw_mean fwc_MD_mean fwc_FA_mean ==  "$group_name" "$subj_id" "$visit_id" "$roi_name" "$fw_mean" "$fwc_md_mean" "$fwc_fa_mean" " >> "$ppmi_dir"/ppmi_FW_tFA_tMD_metrics_20230514.txt
      done


end=`date +%s`
runtime=$((end-start))
echo "runtime is $runtime"
done

##============================================