#! /bin/bash


Folder_A="/mnt/hgfs/Ubuntu/Guo_After/data/" #data_path
Folder_B="/mnt/hgfs/Ubuntu/Guo_After/seg_mask_spinal/"  #code_path
SCT_DIR="/home/sct/Downloads/spinalcordtoolbox/" #sct_path
#for folder_aa in ${Folder_A}/*
#do

for file_a in ${Folder_A}/*
do  
    path_=$file_a
    cd $path_
    #label_discs t22t2s
    sct_deepseg_sc -i t2.nii.gz -c t2 -qc ~/qc_singleSubj
    sct_deepseg_sc -i t2s.nii.gz -c t2s  -qc ~/qc_singleSubj
    
    sct_register_multimodal -i t2.nii.gz -iseg t2_seg.nii.gz -d t2s.nii.gz -dseg t2s_seg.nii.gz -param step=1,type=seg,algo=centermass:step=2,type=seg,algo=bsplinesyn,slicewise=1,iter=3:step=3,type=im,algo=syn,slicewise=1,iter=1,metric=CC -owarp t22t2s/warp_t22t2s.nii.gz -owarpinv t22t2s/warp_t2s2t2.nii.gz -ofolder t22t2s/
    #t2s_seg 2 t2
    sct_apply_transfo -i t2s_seg.nii.gz -d t22t2s/t2s_reg.nii.gz -w t22t2s/warp_t2s2t2.nii.gz -o t2s_seg_t2.nii.gz
    #t2andt2s seg -o t2andt2s_seg.nii.gz
    python3 ${Folder_B}/seg_add.py --dir $path_
    # Register T2 to PAM50
    sct_label_vertebrae -i t2.nii.gz -s t2andt2s_seg.nii.gz -c t2 -qc ~/qc_singleSubj
    sct_label_utils -i t2andt2s_seg_labeled.nii.gz -vert-body 4,7 -o t2andt2s_labels_vert.nii.gz
    sct_register_to_template -i t2.nii.gz -s t2andt2s_seg.nii.gz -l t2andt2s_labels_vert.nii.gz -c t2 -qc ~/qc_singleSubj
    # Register PAM50 to T2s
    #sct_apply_transfo -i t2_seg.nii.gz -d t22t2s/t2_reg.nii.gz -w warp_t22t2s.nii.gz -o t2_seg_t2s.nii.gz
    sct_register_multimodal -i ${SCT_DIR}/data/PAM50/template/PAM50_t2s.nii.gz -iseg ${SCT_DIR}/data/PAM50/template/PAM50_cord.nii.gz -d t2s.nii.gz -dseg t2s_seg.nii.gz -param step=1,type=seg,algo=centermass:step=2,type=seg,algo=bsplinesyn,slicewise=1,iter=3:step=3,type=im,algo=syn,slicewise=1,iter=1,metric=CC -initwarp warp_template2anat.nii.gz -initwarpinv warp_anat2template.nii.gz -owarp t2s2temp/warp_template2t2s.nii.gz -owarpinv t2s2temp/warp_t2s2template.nii.gz -ofolder t2s2temp/
    #t2s_spinal 2 temp
    python3 ${Folder_B}/seg.py --dir $path_ --c t2s
    sct_apply_transfo -i t2s_spinal.nii.gz -d t2s2temp/t2s_reg.nii.gz -w t2s2temp/warp_t2s2template.nii.gz -o t2s_spinal_temp.nii.gz
    fslmaths t2andt2s_seg_labeled.nii.gz -thr 3 -uthr 3 -bin t2andt2s_seg_labeled_c3.nii.gz
    fslmaths t2andt2s_seg_labeled.nii.gz -thr 4 -uthr 4 -bin t2andt2s_seg_labeled_c4.nii.gz
 fslmaths t2andt2s_seg_labeled.nii.gz -thr 5 -uthr 5 -bin t2andt2s_seg_labeled_c5.nii.gz
fslmaths t2andt2s_seg_labeled.nii.gz -thr 6 -uthr 6 -bin t2andt2s_seg_labeled_c6.nii.gz
#fslmaths t2andt2s_seg_labeled.nii.gz -thr 7 -uthr 7 -bin t2andt2s_seg_labeled_c7.nii.gz
sct_apply_transfo -i t2andt2s_seg_labeled_c3.nii.gz -d t22t2s/t2_reg.nii.gz -w t22t2s/warp_t22t2s.nii.gz -o t2andt2s_t2s_labeled_c3.nii.gz
sct_apply_transfo -i t2andt2s_seg_labeled_c4.nii.gz -d t22t2s/t2_reg.nii.gz -w t22t2s/warp_t22t2s.nii.gz -o t2andt2s_t2s_labeled_c4.nii.gz
sct_apply_transfo -i t2andt2s_seg_labeled_c5.nii.gz -d t22t2s/t2_reg.nii.gz -w t22t2s/warp_t22t2s.nii.gz -o t2andt2s_t2s_labeled_c5.nii.gz
sct_apply_transfo -i t2andt2s_seg_labeled_c6.nii.gz -d t22t2s/t2_reg.nii.gz -w t22t2s/warp_t22t2s.nii.gz -o t2andt2s_t2s_labeled_c6.nii.gz
#sct_apply_transfo -i t2andt2s_seg_labeled_c7.nii.gz -d t22t2s/t2_reg.nii.gz -w t22t2s/warp_t22t2s.nii.gz -o t2andt2s_t2s_labeled_c7.nii.gz
    python3 ${Folder_B}/c_seg.py --dir $path_
    sct_apply_transfo -i t2s_seg_c3.nii.gz -d t2s2temp/t2s_reg.nii.gz -w t2s2temp/warp_t2s2template.nii.gz -o t2s_temp_c3.nii.gz 
    sct_apply_transfo -i t2s_seg_c4.nii.gz -d t2s2temp/t2s_reg.nii.gz -w t2s2temp/warp_t2s2template.nii.gz -o t2s_temp_c4.nii.gz 
    sct_apply_transfo -i t2s_seg_c5.nii.gz -d t2s2temp/t2s_reg.nii.gz -w t2s2temp/warp_t2s2template.nii.gz -o t2s_temp_c5.nii.gz 
    sct_apply_transfo -i t2s_seg_c6.nii.gz -d t2s2temp/t2s_reg.nii.gz -w t2s2temp/warp_t2s2template.nii.gz -o t2s_temp_c6.nii.gz 
   
    
done
#done
