def new_voxels_filename(filename, current_vorder, desired_vorder):

    dirname, filename, ext = splitpath(output_path)
    filename = filename + "." + ext
    if output_path is None:
        output_name = filename.replace('.trk', '_' + desired_vorder + '.trk')
        output_path = os.path.join(dirname, output_name)
    if not output_path.find('.'):
        mkcdir(output_path)
        output_name = filename.replace('.nii', '_' + desired_vorder + '.nii')
        output_path = os.path.join(output_path, output_name)
if symmetric:
    symmetric_str = '_symmetric'
else:
    symmetric_str = '_non_symmetric'

trkpaths = glob.glob(os.path.join(TRK_folder, '*trk'))
pickle_folder = os.path.join(
    mainpath, f'Pickle_MDT{inclusive_str}{symmetric_str}{folder_ratio_str}')
centroid_folder = os.path.join(
    mainpath, f'Centroids_MDT{inclusive_str}{symmetric_str}{folder_ratio_str}')
stats_folder = os.path.join(
    mainpath,
    f'Statistics_MDT{inclusive_str}{symmetric_str}{folder_ratio_str}')
excel_folder = os.path.join(
    mainpath, f'Excels_MDT{inclusive_str}{symmetric_str}{folder_ratio_str}')
mkcdir([pickle_folder, centroid_folder, stats_folder, excel_folder])
if not os.path.exists(TRK_folder):
    raise Exception(f'cannot find TRK folder at {TRK_folder}')

#Initializing dictionaries to be filled
stream_point = {}
stream = {}
groupstreamlines = {}
groupstreamlines_orig = {}
groupLines = {}
groupPoints = {}
group_qb = {}
group_clusters = {}
groups_subjects = {}

if project == 'AD_Decode':
import os, glob
from file_tools import mkcdir
from transform_handler import recenter_nii_save_test, affine_superpose, get_affine_transform_test

mainpath = '/Volumes/Data/Badea/Lab/human/AMD/'
ref_subj_folder = os.path.join(mainpath, 'ref_subj')
ref_MDT_folder = os.path.join(mainpath, 'ref_MDT')
#DWI_folder = os.path.join(mainpath,'MDT_to_subj_testzone')
path_transforms = os.path.join(mainpath, 'Transforms')
MDT_to_subj = os.path.join(mainpath, 'MDT_to_subj_testzone')
subj_to_MDT = os.path.join(mainpath, 'subj_to_MDT_testzone')
mkcdir([MDT_to_subj, subj_to_MDT])
ext = '.nii.gz'

files = glob.glob(os.path.join(ref_subj_folder, '*'))
files = [
    '/Volumes/Data/Badea/Lab/human/AMD/ref_subj/H26578_subjspace_fa.nii.gz'
]
recenter = True
overwrite = True

for ref_file in files:
    subj = os.path.basename(ref_file)[:6]
    ref = os.path.basename(ref_file)[17:19]
    SAMBA_init = ref_file
    print(ref_file)
    preprocess = os.path.join(subj_to_MDT, f'{subj}_prepro.nii.gz')
    if recenter and (not os.path.exists(preprocess) or overwrite):
        recenter_nii_save_test(SAMBA_init, preprocess)
        SAMBA_init = preprocess
Example #4
0
bval_path = '/Users/alex/jacques/APOE/bvec_testing/N58794_bvals_fix.txt'
bvec_path = '/Users/alex/jacques/APOE/bvec_testing/N58794_bvecs_fix.txt'
bvals, bvecs = read_bval_bvec(bval_path, bvec_path)

bvec_orient1 = (np.array(list(itertools.permutations([1, 2, 3]))))
bvec_orient2 = [elm * [-1, 1, 1] for elm in bvec_orient1]
bvec_orient3 = [elm * [1, -1, 1] for elm in bvec_orient1]
bvec_orient4 = [elm * [1, 1, -1] for elm in bvec_orient1]
bvec_orient_list = np.concatenate(
    (bvec_orient1, bvec_orient2, bvec_orient3, bvec_orient4))

dtis = '#! /bin/bash \n'
for bvec_orient in bvec_orient_list:
    strproperty = orient_to_str(bvec_orient)
    results_folder = os.path.join(mainpath, strproperty.replace('_', ''))
    mkcdir(results_folder)
    bvecs_reoriented = reorient_bvecs(bvecs, bvec_orient)
    new_bvec_file = os.path.join(results_folder,
                                 strproperty.replace('_', '') + '_bvecs.txt')
    writebvec(bvecs_reoriented,
              new_bvec_file,
              '',
              writeformat="dsi",
              overwrite=False)
    output_path = os.path.join(results_folder, f'dtifit{strproperty}.nii.gz')
    dti_cmd = f'dtifit -k {input_diifile} -o {output_path} -m {mask_path} -r {new_bvec_file} -b {bval_path}\n'
    dtis = dtis + dti_cmd

bash_file_path = os.path.join(mainpath, 'dti_bash.sh')
with open(bash_file_path, 'w') as rsh:
    rsh.write(dtis)
Example #5
0
def launch_preprocessing(subj, raw_nii, outpath, cleanup=False, nominal_bval=4000, bonusshortcutfolder=None,
                         gunniespath="~/gunnies/", processes=1, masking="bet", ref=None, transpose=None,
                         overwrite=False, denoise='None', recenter=0, verbose=False):

    proc_name ="diffusion_prep_" # Not gonna call it diffusion_calc so we don't assume it does the same thing as the civm pipeline
    work_dir=os.path.join(outpath,proc_name+subj)

    if verbose:
        print(f"Processing diffusion data with runno/subj: {subj}")
        print(f"Work directory is {work_dir}")
    mkcdir(work_dir)

    sbatch_folder =os.path.join(work_dir,"sbatch")
    mkcdir(sbatch_folder)

    nii_name=os.path.split(raw_nii)[1]
    niifolder = os.path.dirname(raw_nii)
    ext = ".nii.gz"
    nii_ext=getext(nii_name)
    bxheader = nii_name.replace(nii_ext,".bxh")
    bxheader = os.path.join(niifolder, bxheader)
    bvecs = os.path.join(work_dir, subj+"_bvecs.txt")
    bvals =bvecs.replace("bvecs","bvals")

    if verbose:
        print(f"Original nifti is at {nii_name}\nbvecs are at {bvecs}\nbvals are at {bvals}\n")
    if not os.path.exists(bvecs):
        if verbose:
            print("Extracting diff directions")
        #print("Bvals and bvecs not found, using extractdiffdirs, however it it NOT RELIABLE, beware!")
        bvec_cmd = (f"extractdiffdirs --colvectors --writebvals --fieldsep='\t' --space=RAI {bxheader} {bvecs} {bvals}")
        os.system(bvec_cmd)

    # Make dwi for mask generation purposes.
    orient_string = os.path.join(work_dir,'relative_orientation.txt')
    tmp_mask = os.path.join(work_dir,f"{subj}_tmp_dwi_mask{ext}")
    raw_dwi = os.path.join(work_dir,f"{subj}_raw_dwi.nii.gz")
    orient_string = os.path.join(work_dir,"relative_orientation.txt")

    if bonusshortcutfolder is not None:
        raw_nii_link = os.path.join(bonusshortcutfolder, f"{subj}_rawnii{ext}")
        if not os.path.exists(raw_nii_link) or overwrite:
            buildlink(raw_nii, raw_nii_link)
        bvecs_new = os.path.join(bonusshortcutfolder,subj+"_bvecs.txt")
        bvals_new = os.path.join(bonusshortcutfolder,subj+"_bvals.txt")
        if not os.path.exists(bvecs_new) or not os.path.exists(bvals_new) or overwrite:
            shutil.copyfile(bvecs,bvecs_new)
            shutil.copyfile(bvals,bvals_new)
    final_mask = os.path.join(work_dir, f'{subj}_dwi_mask{ext}')

    if (not os.path.exists(final_mask) and not os.path.exists(tmp_mask)) or overwrite:
        if not os.path.exists(raw_dwi) or overwrite:
            select_cmd = f"select_dwi_vols {raw_nii} {bvals} {raw_dwi} {nominal_bval} -m"
            os.system(select_cmd)
        if not os.path.exists(tmp_mask) or overwrite:
            if masking=="median":
                tmp = tmp_mask.replace("_mask", "")
                median_mask_make(raw_dwi, tmp, outpathmask=tmp_mask)
            elif masking=="bet":
                tmp=tmp_mask.replace("_mask", "")
                bet_cmd = f"bet {raw_dwi} {tmp} -m -n -R"
                os.system(bet_cmd)
            else:
                raise Exception("Unrecognized masking type")

    mask_subjspace =
    if bonusshortcutfolder is not None:
        mask_subj_link = os.path.join(bonusshortcutfolder,f'{subj}_mask_subjspace{ext}')
        if not os.path.exists(mask_subj_link) or overwrite:
            buildlink(tmp_mask, mask_subj_link)

    #if cleanup and (os.path.exists(tmp_mask) and os.path.exists(raw_dwi)):
    #    os.remove(raw_dwi)
    #overwrite=False
    # Run Local PCA Denoising algorithm on 4D nifti:
    masked_nii = os.path.join(work_dir, nii_name)
    if not "nii.gz" in masked_nii:
        masked_nii = masked_nii.replace(".nii", ".nii.gz")
    masked_nii = masked_nii.replace(ext, "_masked" + ext)

    if denoise.lower()=='lpca':
        D_subj=f'LPCA_{subj}';
    elif denoise.lower()=='mpca':
        D_subj=f'MPCA_{subj}';
    elif denoise=="None" or denoise is None:
        D_subj = f'{subj}'

    if denoise=="None" or denoise is None:
        denoised_nii = masked_nii
        if not os.path.exists(masked_nii) or overwrite:
            fsl_cmd = f"fslmaths {raw_nii} -mas {tmp_mask} {masked_nii} -odt 'input'";
            os.system(fsl_cmd)
    else:
        denoised_nii = os.path.join(work_dir,f"{D_subj}_nii4D.nii.gz")
        if not os.path.exists(denoised_nii) or overwrite:
            if not os.path.exists(masked_nii) or overwrite:
                fsl_cmd = f"fslmaths {raw_nii} -mas {tmp_mask} {masked_nii} -odt 'input'";
                os.system(fsl_cmd)
            basic_LPCA_denoise_func(subj,masked_nii,bvecs,denoised_nii, processes=processes,
                                    denoise=denoise, verbose=False) #to improve and make multiprocessing

    #if cleanup and os.path.exists(denoised_nii) and os.path.exists(masked_nii) and denoised_nii!=masked_nii:
    #    os.remove(masked_nii)

    # Run coregistration/eddy current correction:

    coreg_nii_old = f'{outpath}/co_reg_{D_subj}_m00-results/Reg_{D_subj}_nii4D{ext}';
    coreg_nii = os.path.join(work_dir,f'Reg_{D_subj}_nii4D{ext}')
    if not cleanup:
        coreg_nii=coreg_nii_old
    if not os.path.exists(coreg_nii) or overwrite:
        if not os.path.exists(coreg_nii_old) or overwrite:
            temp_cmd = os.path.join(gunniespath,'co_reg_4d_stack_tmpnew.bash')+f' {denoised_nii} {D_subj} 0 {outpath} 0';
            os.system(temp_cmd)
        if cleanup:
            shutil.move(coreg_nii_old,coreg_nii)
    if bonusshortcutfolder is not None:
        coreg_link = os.path.join(bonusshortcutfolder,f'{subj}_coreg{ext}')
        if not os.path.exists(coreg_link) or overwrite:
            buildlink(coreg_nii, coreg_link)

    toeddy=False
    if toeddy:
        #fsl_cmd = f"fslmaths {raw_nii} -mas {tmp_mask} {masked_nii} -odt 'input'";
        #os.system(fsl_cmd)
        eddy_cmd = f"eddy --imain={coreg_nii} --mask={tmp_mask} --acqp=acq_params.txt --index={os.path.join(work_dir,'index.txt')} --bvecs={bvecs} --bvals={bvals} --topup=topup_results --repol --out = {os.path.join(work_dir,f'Reg_{D_subj}_nii4D_eddy{ext}')}"
        os.system(eddy_cmd)

    coreg_inputs=os.path.join(outpath,f'co_reg_{D_subj}_m00-inputs')
    coreg_work=coreg_inputs.replace('-inputs','-work')
    coreg_results=coreg_inputs.replace('-inputs','-results')
    if cleanup and os.path.exists(coreg_nii) and os.path.isdir(coreg_inputs):
        shutil.rmtree(coreg_inputs)
    if cleanup and os.path.exists(coreg_nii) and os.path.isdir(coreg_work):
        shutil.rmtree(coreg_work)
    if cleanup and os.path.exists(coreg_nii) and os.path.isdir(coreg_results):
        shutil.rmtree(coreg_results)

    # Generate tmp DWI:

    tmp_dwi_out=os.path.join(work_dir, f'{subj}_tmp_dwi{ext}')
    dwi_out=os.path.join(work_dir,f'{subj}_dwi{ext}')

    if not os.path.exists(dwi_out) or overwrite:
        if not os.path.exists(tmp_dwi_out) or overwrite:
            cmd=f'select_dwi_vols {coreg_nii} {bvals} {tmp_dwi_out} {nominal_bval}  -m'
            os.system(cmd)

    if bonusshortcutfolder is not None:
        tmp_dwi_out_link = os.path.join(bonusshortcutfolder,f'{subj}_mask_subjspace{ext}')
        if not os.path.exists(tmp_dwi_out_link) or overwrite:
            buildlink(tmp_dwi_out, tmp_dwi_out_link)

    # Generate tmp B0:
    tmp_b0_out=os.path.join(work_dir,f'{subj}_tmp_b0{ext}')
    b0_out = os.path.join(work_dir, f'{subj}_b0{ext}')
    if (not os.path.exists(b0_out) and not os.path.exists(tmp_b0_out)) or overwrite:
        cmd=f'select_dwi_vols {coreg_nii} {bvals} {tmp_b0_out} 0  -m;'
        os.system(cmd)
    #overwrite=False
    #elif cleanup and os.path.exists(tmp_b0_out):
    #    os.remove(tmp_b0_out)

    # Generate DTI contrasts and perform some tracking QA:
    if cleanup:
        c_string=' --cleanup '
    else:
        c_string=''

    #Important note: this is what first creates the fa, md, etc
    overwrite = True
    if len(glob.glob(os.path.join(work_dir,f'*.fib.gz.md{ext}'))) == 0 or overwrite:
        if overwrite:
            os.remove(os.path.join(work_dir, f'*.fib.gz.*{ext}'))
        cmd = os.path.join(gunniespath,'dti_qa_with_dsi_studio.bash')+f' {coreg_nii} {bvecs} {tmp_mask} {work_dir} {c_string}';
        os.system(cmd)
    overwrite = False

    #Save the subject space dti results

    #Generate tmp MD:
    for contrast in ['md']:
        real_file=largerfile(os.path.join(work_dir,f'*.fib.gz.{contrast}{ext}'))  #Catch the 'real file' for each contrast
        tmp_file = f'{work_dir}/{subj}_tmp_{contrast}{ext}';
        #contrast=contrast.replace('0','')   #some little nonsense mostly to deal with the 'fa0' name to become 'fa', probably sohould change it
        if not os.path.exists(tmp_file):
            shutil.copy(real_file,tmp_file)

    tmp_md = f'{work_dir}/{subj}_tmp_md{ext}';

    if ref=="md" or ref is None:
        reference=tmp_md
    elif ref=="coreg":
        reference=coreg_nii
    elif os.path.exists(ref):
        reference=ref

    reference_file = os.path.join(work_dir, f'{subj}_reference{ext}')
    if not os.path.exists(reference_file):
        shutil.copy(reference, reference_file)
    if bonusshortcutfolder is not None:
        bonus_ref_link = os.path.join(bonusshortcutfolder, f'{subj}_reference{ext}')
        if not os.path.exists(bonus_ref_link) or overwrite:
            buildlink(reference_file,bonus_ref_link)

    """
    for contrast in ['md']:
        real_file=largerfile(os.path.join(work_dir,f'*.fib.gz.{contrast}{ext}'))  #Catch the 'real file' for each contrast
        tmp_file = f'{work_dir}/{subj}_tmp_{contrast}{ext}';
        #contrast=contrast.replace('0','')   #some little nonsense mclassifierostly to deal with the 'fa0' name to become 'fa', probably sohould change it
        if not os.path.exists(tmp_md):
            shutil.copy(real_file,tmp_file)
    """

    #give real header to the temp files using md as reference

    overwrite=True
    for contrast in ['dwi', 'b0', 'dwi_mask']:
        tmp_file=os.path.join(work_dir,f'{subj}_tmp_{contrast}{ext}')
        tmp2_file=os.path.join(work_dir,f'{subj}_tmp2_{contrast}{ext}')
        final_file=os.path.join(work_dir,f'{subj}_{contrast}{ext}')
        if ((not os.path.exists(tmp2_file) and not os.path.exists(final_file)) or overwrite):
            if not os.path.exists(tmp_file):
                raise Exception("Tmp file was not created, need to rerun previous processes")
            else:
                cmd = os.path.join(gunniespath, 'nifti_header_splicer.bash') + f' {reference_file} {tmp_file} {tmp2_file}'
                os.system(cmd)
                tmp_test_file = os.path.join(work_dir,f'{subj}_tmp2_test_{contrast}{ext}')
                header_superpose(reference, tmp_file, outpath=tmp_test_file)

    #write the relative orientation file here
    if not os.path.isfile(orient_string) or overwrite:
        if os.path.isfile(orient_string):
            os.remove(orient_string)
        file = os.path.join(work_dir,subj+'_tmp_dwi_mask'+ext);
        cmd = 'bash ' + os.path.join(gunniespath,'find_relative_orientation_by_CoM.bash') + f' {reference_file} {file}'
        orient_relative = subprocess.getoutput(cmd)

        with open(orient_string, 'w') as f:
            f.write(orient_relative)
    else:
        orient_relative = open(orient_string, mode='r').read()

    #check extracted values from relative orientation vals
    orientation_out = orient_relative.split(',')[0]
    orientation_out = orientation_out.split(':')[1]
    orientation_in = orient_relative.split(',')[1]
    orientation_in = orientation_in.split(':')[1]
    if verbose:
        print(f'flexible orientation: {orientation_in}');
        print(f'reference orientation: {orientation_out}');

    #apply the orientation modification to specified contrasts
    for contrast in ['dwi', 'b0', 'dwi_mask']:
        img_in=os.path.join(work_dir,f'{subj}_tmp2_{contrast}{ext}')
        img_out=os.path.join(work_dir,f'{subj}_{contrast}{ext}')
        if not os.path.isfile(img_out) or overwrite:
            if orientation_out != orientation_in:
                print('TRYING TO REORIENT...b0 and dwi and mask')
                if os.path.exists(img_in) and (not os.path.exists(img_out) or overwrite):
                    img_transform_exec(img_in, orientation_in, orientation_out, img_out, recenter=recenter)
                    if os.path.exists(img_out):
                        os.remove(img_in)
                elif os.path.exists(img_out) and cleanup:
                    os.remove(img_in)
            else:
                shutil.move(img_in,img_out)

        if bonusshortcutfolder is not None:
            bonus_link = os.path.join(bonusshortcutfolder, f'{subj}_{contrast}{ext}')
            if not os.path.exists(bonus_link) or overwrite:
                buildlink(img_out, bonus_link)


    mask = os.path.join(work_dir,f'{subj}_mask{ext}')
    b0 = os.path.join(work_dir,f'{subj}_b0{ext}')

    #if cleanup and os.path.exists(dwi_out) and os.path.exists(tmp_dwi_out):
    #    os.remove(tmp_dwi_out)

    #refine mask (to check!!!!)

    bonusmask=False
    if bonusmask:
        mask_new = mask.replace('_mask', '_new')
        bet_cmd = f'bet {b0} {mask_new} -m -f 0.3'
        os.system(bet_cmd)
        mask_new = mask_new.replace('_new','_new_mask')

        outpath=dwi_out.replace(".nii","_newmask.nii")
        applymask_samespace(dwi_out, mask_new, outpath) #(add outpath if you want to save to different location rather than overwrite)
        dwi_out=outpath

        outpath=b0_out.replace(".nii","_newmask.nii")
        applymask_samespace(b0_out, mask_new,outpath=outpath)

    for contrast in ['fa0', 'rd', 'ad', 'md']:
        real_file=largerfile(os.path.join(work_dir,f'*.fib.gz.{contrast}{ext}'))  # It will be fun times if we ever have more than one match to this pattern...
        if bonusmask:
            real_file_new=real_file.replace("D.nii.gz","D_newmask.nii.gz")
            applymask_samespace(real_file, mask_new,outpath=real_file_new)
            real_file=real_file_new
        contrast=contrast.replace('0','')
        #linked_file=os.path.join(shortcut_dir,f'{subj}_{contrast}{ext}')
        linked_file_w=os.path.join(work_dir,f'{subj}_{contrast}{ext}')
        header_fix=True
        if header_fix:
            affine_superpose(dwi_out, real_file, transpose=transpose)

        if not os.path.isfile(linked_file_w) or overwrite:
            buildlink(real_file, linked_file_w)
        if bonusshortcutfolder is not None:
            blinked_file = os.path.join(bonusshortcutfolder, f'{subj}_{contrast}{ext}')
            if not os.path.exists(blinked_file) or overwrite:
                buildlink(real_file, blinked_file)
elif 'blade' in computer_name:
    mainpath = '/mnt/munin6/Badea/Lab/human/'
    atlas_legends = "/mnt/munin6/Badea/Lab/atlases/IITmean_RPI/IITmean_RPI_index.xlsx"
else:
    raise Exception('No other computer name yet')

project = "AMD"

if project == 'AD_Decode':
    mainpath = os.path.join(mainpath, 'AD_Decode', 'Analysis')
else:
    mainpath = os.path.join(mainpath, project)

diff_preprocessed = os.path.join(mainpath, "DWI")

mkcdir([mainpath, diff_preprocessed])

subjects = [
    "H29056", "H26578", "H29060", "H26637", "H29264", "H26765", "H29225",
    "H26660", "H29304", "H26890", "H29556", "H26862", "H29410", "H26966",
    "H29403", "H26841", "H21593", "H27126", "H29618", "H27111", "H29627",
    "H27164", "H29502", "H27100", "H27381", "H21836", "H27391", "H21850",
    "H27495", "H21729", "H27488", "H21915", "H27682", "H21956", "H27686",
    "H22331", "H28208", "H21990", "H28955", "H29878", "H27719", "H22102",
    "H27841", "H22101", "H27842", "H22228", "H28029", "H22140", "H27852",
    "H22276", "H27999", "H22369", "H28115", "H22644", "H28308", "H22574",
    "H28377", "H22368", "H28325", "H22320", "H28182", "H22898", "H28748",
    "H22683", "H28373", "H22536", "H28433", "H22825", "H28662", "H22864",
    "H28698", "H23143", "H28861", "H23157", "H28820", "H23028", "H29002",
    "H23210", "H29020", "H23309", "H29161", "H26841", "H26862", "H26949",
    "H26966", "H27100", "H27126", "H27163", "H27246", "H27488", "H27682",
from file_tools import mkcdir, largerfile
import shutil
from argument_tools import parse_arguments
from bvec_handler import orient_to_str

gunniespath = "~/gunnies/"
mainpath = "/Volumes/Data/Badea/Lab/"
diffpath = "/Volumes/Data/Badea/Lab/RaulChavezValdez/"

#outpath = "/Users/alex/jacques/APOE_temp"
outpath = "/mnt/munin6/Badea/Lab/mouse/Chavez_series/diffusion_prep_locale/"
SAMBA_inputs_folder = "/mnt/munin6/Badea/Lab/Chavez_prep/"
shortcuts_all_folder = "/mnt/munin6/Badea/Lab/mouse/Chavez_symlink_pool_allfiles/"

if SAMBA_inputs_folder is not None:
    mkcdir(SAMBA_inputs_folder)
if shortcuts_all_folder is not None:
    mkcdir(shortcuts_all_folder)
mkcdir(outpath)
subjects = [
    'C_20220124_001', 'C_20220124_002', 'C_20220124_003', 'C_20220124_004',
    'C_20220124_005', 'C_20220124_006', 'C_20220124_007'
]
"""
subjects_folders = glob.glob(os.path.join(diffpath,'diffusion*/'))
subjects = []
for subject_folder in subjects_folders:
    subjects.append(subject_folder.split('diffusion')[1][:6])
"""
removed_list = []
for remove in removed_list:
import shutil
from diffusion_preprocessing import launch_preprocessing
from file_tools import mkcdir, largerfile
from transform_handler import get_transpose
import shutil
from bvec_handler import orient_to_str

gunniespath = "~/gunnies/"
mainpath = "/mnt/munin6/Badea/ADdecode.01/"

#outpath = "/Users/alex/jacques/APOE_temp"
outpath = "/mnt/munin6/Badea/Lab/human/AD_Decode/diffusion_prep_locale/"
SAMBA_inputs_folder = "/mnt/munin6/Badea/Lab/mouse/ADDeccode_symlink_pool/"
shortcuts_all_folder = "/mnt/munin6/Badea/Lab/human/ADDeccode_symlink_pool_allfiles/"
diffpath = mainpath + "Data/Anat"
mkcdir([SAMBA_inputs_folder, shortcuts_all_folder])

subjects = [
    '01912', '02110', '02224', '02227', '02230', '02231', '02266', '02289',
    '02320', '02361', '02363', '02373', '02386', '02390', '02402', '02410',
    '02421', '02424', '02446', '02451', '02469', '02473', '02485', '02491',
    '02490', '02506', '02523', '02524', '02535', '02654', '02666', '02670',
    '02686', '02690', '02695', '02715', '02720', '02737', '02745', '02753',
    '02765', '02771', '02781', '02802', '02804', '02813', '02812', '02817',
    '02840', '02842', '02871', '02877', '02898', '02926', '02938', '02939',
    '02954', '02967', '02987', '03010', '03017', '03028', '03033', '03034',
    '03045', '03048', '03069', '03225', '03265', '03293', '03308', '03321',
    '03343', '03350', '03378', '03391', '03394'
]

subjects = [
Example #9
0
from bvec_handler import orient_to_str

#gunniespath = "/mnt/clustertmp/common/rja20_dev/gunnies/"
#gunniespath = "/Users/alex/bass/gitfolder/wuconnectomes/gunnies/"
#diffpath = "/Volumes/dusom_civm-atlas/20.abb.15/research/"
#outpath = "/Volumes/Data/Badea/Lab/mouse/APOE_series/diffusion_prep_locale/"

gunniespath = ""
outpath = "/mnt/munin6/Badea/Lab/mouse/APOE_series/diffusion_prep_locale/"
#bonusshortcutfolder = "/Volumes/Data/Badea/Lab/jacques/APOE_series/19abb14/"
#bonusshortcutfolder = "/mnt/munin6/Badea/Lab/APOE_symlink_pool/"
#bonusshortcutfolder = None
SAMBA_inputs_folder = "/mnt/munin6/Badea/Lab/19abb14/"
shortcuts_all_folder = "/mnt/munin6/Badea/Lab/mouse/APOE_symlink_pool_allfiles/"

mkcdir([SAMBA_inputs_folder, shortcuts_all_folder])

subjects = [
    "N58214", "N58215", "N58216", "N58217", "N58218", "N58219", "N58221",
    "N58222", "N58223", "N58224", "N58225", "N58226", "N58228", "N58229",
    "N58230", "N58231", "N58232", "N58633", "N58634", "N58635", "N58636",
    "N58649", "N58650", "N58651", "N58653", "N58654", 'N58408', 'N58398',
    'N58714', 'N58740', 'N58477', 'N58734', 'N58309', 'N58792', 'N58302',
    'N58784', 'N58706', 'N58361', 'N58355', 'N58712', 'N58790', 'N58606',
    'N58350', 'N58608', 'N58779', 'N58500', 'N58604', 'N58749', 'N58510',
    'N58394', 'N58346', 'N58344', 'N58788', 'N58305', 'N58514', 'N58794',
    'N58733', 'N58655', 'N58735', 'N58310', 'N58400', 'N58708', 'N58780',
    'N58512', 'N58747', 'N58303', 'N58404', 'N58751', 'N58611', 'N58745',
    'N58406', 'N58359', 'N58742', 'N58396', 'N58613', 'N58732', 'N58516',
    'N58813', 'N58402'
]
if project == 'AD_Decode':
    outpath = os.path.join(outpath, project, 'Analysis')
    inpath = os.path.join(inpath, project, 'Analysis')
else:
    outpath = os.path.join(outpath, project)
    inpath = os.path.join(inpath, project)

TRK_folder = os.path.join(inpath, f'TRK_MPCA_MDT{fixed_str}{folder_ratio_str}')
TRK_folder = os.path.join(inpath, f'TRK_MDT{fixed_str}{folder_ratio_str}')
label_folder = os.path.join(outpath, 'DWI')
#trkpaths = glob.glob(os.path.join(TRK_folder, '*trk'))
excel_folder = os.path.join(
    outpath, f'Excels_MDT{inclusive_str}{symmetric_str}{folder_ratio_str}')

print(excel_folder)
mkcdir(excel_folder)

if not remote and os.path.exists(TRK_folder):
    raise Exception(f'cannot find TRK folder at {TRK_folder}')

# Initializing dictionaries to be filled
stream_point = {}
stream = {}
groupstreamlines = {}
groupLines = {}
groupPoints = {}
group_qb = {}
group_clusters = {}
groups_subjects = {}

if project == 'AD_Decode':
Example #11
0
ratio = 100
ratio_str = ratio_to_str(ratio)
if ratio_str == 'all':
    ratio_str = ''
else:
    ratio_str = ratio_str.replace('_ratio', '')

TRK_folder = os.path.join(mainpath, 'TRK_MPCA_MDT_fixed' + ratio_str)
label_folder = os.path.join(mainpath, 'DWI')
trkpaths = glob.glob(os.path.join(TRK_folder, '*trk'))
figures_folder = os.path.join(mainpath, 'Figures_MDT' + ratio_str)
pickle_folder = os.path.join(mainpath, 'Pickle_MDT' + ratio_str)
centroid_folder = os.path.join(mainpath, 'Centroids_MDT' + ratio_str)
excel_folder = os.path.join(mainpath, 'Excels_MDT' + ratio_str)
mkcdir([figures_folder, pickle_folder, centroid_folder, excel_folder])
if not os.path.exists(TRK_folder):
    raise Exception(f'cannot find TRK folder at {TRK_folder}')

#reference_img refers to statistical values that we want to compare to the streamlines, say fa, rd, etc
references = ['fa', 'md', 'rd', 'ad', 'b0']
references = []

verbose = True

#Initializing dictionaries to be filled
stream_point = {}
stream = {}
groupstreamlines = {}
groupLines = {}
groupPoints = {}
import os
import shutil
from tract_handler import trk_fixer
from file_tools import mkcdir
from tract_handler import reducetractnumber, ratio_to_str, get_ratio

TRK_folder = '/Users/alex/jacques/AMD_TRK_testing/TRK_MDT'
TRK_output = '/Users/alex/jacques/AMD_TRK_testing/TRK_MDT_fixed'

TRK_folder = '/Volumes/Data/Badea/Lab/human/AMD/TRK_MDT'
TRK_output = '/Volumes/Data/Badea/Lab/human/AMD/TRK_MDT_fixed'

TRK_folder = '/mnt/paros_MRI/jacques/AD_Decode/Analysis/TRK_MPCA_neworient/'
TRK_output = '/mnt/paros_MRI/jacques/AD_Decode/Analysis/TRK_MPCA_neworient_fixed/'

mkcdir(TRK_output)
for trk_name in os.listdir(TRK_folder):
    if trk_name.endswith('.trk'):
        trk_path = os.path.join(TRK_folder,trk_name)
        trk_newpath = os.path.join(TRK_output, trk_name)
        if not os.path.exists(trk_newpath):
            print(f'Beginning the fix of {trk_path}')
            trk_fixer(trk_path, trk_newpath, verbose=True)
        else:
            print(f'Already fixed {trk_name}')

trk_folder = TRK_output
new_trk_folder = '/mnt/paros_MRI/jacques/AD_Decode/Analysis/TRK_MPCA_neworient_fixed_100/'
mkcdir(new_trk_folder)
ratio = 100
filelist = os.listdir(trk_folder)
project = "AD_Decode"

if project == "AD_Decode":
    path_TRK = os.path.join(main_path, 'AD_Decode', 'Analysis', 'TRK_MPCA')
    path_TRK_output = os.path.join(main_path, 'AD_Decode', 'Analysis',
                                   'TRK_MPCA_MDT')
    path_DWI = os.path.join(main_path, 'AD_Decode', 'Analysis', 'DWI')
    path_DWI_MDT = os.path.join(main_path, 'AD_Decode', 'Analysis', 'DWI_MDT')
    path_transforms = os.path.join(main_path, 'AD_Decode', 'Analysis',
                                   'Transforms')
    ref = "md"
    path_trk_tempdir = os.path.join(main_path, 'AD_Decode', 'Analysis',
                                    'TRK_save_alt2')
    DWI_save = os.path.join(main_path, 'AD_Decode', 'Analysis',
                            'NII_tempsave_alt2')
    mkcdir([path_trk_tempdir, path_TRK_output, DWI_save])
    # Get the values from DTC_launcher_ADDecode. Should probalby create a single parameter file for each project one day
    stepsize = 2
    ratio = 1
    trkroi = ["wholebrain"]
    str_identifier = get_str_identifier(stepsize, ratio, trkroi)

subjects_all = glob.glob(os.path.join(path_DWI, '*subjspace_dwi*.nii.gz'))
subjects = []
for subject in subjects_all:
    subject_name = os.path.basename(subject)
    subjects.append(subject_name[:6])

#removed_list = ['S02804', 'S02817', 'S02898', 'S02871', 'S02877','S03045', 'S02939', 'S02840']

subjects = [
    mainpath = os.path.join(mainpath, project, 'Analysis')
    anat_path = '/Volumes/Data/Badea/Lab/mouse/VBM_21ADDecode03_IITmean_RPI_fullrun-work/dwi/SyN_0p5_3_0p5_fa/faMDT_NoNameYet_n37_i6/median_images/MDT_b0.nii.gz'

# figures_path = '/Volumes/Data/Badea/Lab/human/AMD/Figures_MDT_non_inclusive/'
# centroid_folder = '/Volumes/Data/Badea/Lab/human/AMD/Centroids_MDT_non_inclusive/'
figures_path = os.path.join(
    mainpath, f'Figures_MDT{inclusive_str}{symmetric_str}{folder_ratio_str}')
centroid_folder = os.path.join(
    mainpath, f'Centroids_MDT{inclusive_str}{symmetric_str}{folder_ratio_str}')
trk_folder = os.path.join(
    mainpath, f'Centroids_MDT{inclusive_str}{symmetric_str}{folder_ratio_str}')
stats_folder = os.path.join(
    mainpath,
    f'Statistics_MDT{inclusive_str}{symmetric_str}{folder_ratio_str}')

mkcdir([figures_path, centroid_folder, stats_folder])

# groups = ['Initial AMD', 'Paired 2-YR AMD', 'Initial Control', 'Paired 2-YR Control', 'Paired Initial Control',
#          'Paired Initial AMD']

# anat_path = '/Volumes/Data/Badea/Lab/mouse/VBM_19BrainChAMD01_IITmean_RPI_with_2yr-work/dwi/SyN_0p5_3_0p5_dwi/dwiMDT_Control_n72_i6/median_images/MDT_dwi.nii.gz'

# superior frontal right to cerebellum right

#set parameter
num_points1 = 50
distance1 = 1
feature1 = ResampleFeature(nb_points=num_points1)
metric1 = AveragePointwiseEuclideanMetric(feature=feature1)

#group cluster parameter
def convert_images_templatespace(subject, mainpath, project_name, subject_img_path, orient_string, preppath = None, gunniespath="/Users/alex/bass/gitfolder/wuconnectomes/gunnies/", recenter=0, verbose=True):

    #mainpath = "/Volumes/Data/Badea/Lab/mouse/"
    #gunniespath = "/Users/alex/bass/gitfolder/wuconnectomes/gunnies/"
    #project_name = "VBM_21ADDecode03_IITmean_RPI_fullrun"
    #atlas_labels = "/Volumes/Data/Badea/Lab/atlas/IITmean_RPI/IITmean_RPI_labels.nii.gz"

    out_dir = os.path.join(mainpath, f"{project_name}-results","connectomics",subject)
    work_dir = os.path.join(mainpath, f"{project_name}-work")
    dirty_dir = os.path.join(mainpath,"burn_after_reading")
    mkcdir(dirty_dir)

    template_type_prefix = os.path.basename(os.path.dirname(glob.glob(os.path.join(work_dir,"dwi","SyN*/"))[0]))
    template_runs = glob.glob((os.path.join(work_dir,"dwi",template_type_prefix,"*/")))
    mymax=-1
    for template_run in template_runs:
        if "NoNameYet" in template_run and template_run[-4:-2]=="_i":
            if int(template_run[-2])>mymax:
                mymax=int(template_run[-2])
                final_template_run=template_run
    if mymax==-1:
        raise Exception(f"Could not find template runs in {os.path.join(mainpath, f'{project_name}-work','dwi',template_type_prefix)}")
    #template_type_prefix = "faMDT_NoNameYet_n37_i6"
    #final_template_run = "SyN_0p5_3_0p5_fa"

    #final_ref = f"/mnt/munin6/Badea/Lab/human/AD_Decode/diffusion_prep_locale/diffusion_prep_{subject}/Reg_LPCA_{subject}_nii4D.nii.gz";
    if os.path.exists(orient_string):
        orient_relative = open(orient_string, mode='r').read()
        orientation_out = orient_relative.split(',')[0]
        orientation_out = orientation_out.split(':')[1]
        orientation_in = orient_relative.split(',')[1]
        orientation_in = orientation_in.split(':')[1]
    else:
        orientation_out = "*"
        warnings.warn("Could not find orientation file, may cause errors later")
        orientation_in = "LPS"
        orientation_out = "RAS"

    trans = os.path.join(work_dir,"preprocess","base_images","translation_xforms",f"{subject}_0DerivedInitialMovingTranslation.mat")
    rigid =os.path.join(work_dir,"dwi",f"{subject}_rigid.mat")
    affine = os.path.join(work_dir,"dwi",f"{subject}_affine.mat")
    #MDT_to_subject = os.path.join(final_template_run,"reg_diffeo",f"MDT_to_{subject}_warp.nii.gz")
    #MDT_to_atlas_affine = os.path.join(final_template_run,"stats_by_region","labels","transforms",f"MDT_*_to_{label_name}_affine.mat")
    #atlas_to_MDT = os.path.join(final_template_run,"stats_by_region","labels","transforms",f"{label_name}_to_MDT_warp.nii.gz")

    [trans, rigid, affine], _ = check_files([trans,rigid,affine])


    preprocess_ref = os.path.join(work_dir,"preprocess",f"{subject}_fa_masked.nii.gz")
    preprocess_labels = os.path.join(dirty_dir,f"{subject}_preprocess_labels.nii.gz")
    fixed_preprocess_labels = os.path.join(dirty_dir,f"{subject}_fixed_preprocess_labels.nii.gz")
    coreg_labels = os.path.join(dirty_dir,f"{subject}_{orientation_in}_labels.nii.gz")

    # final_ref="/mnt/munin6/Badea/Lab/mouse/co_reg_LPCA_${subject:1:5}_m00-results/Reg_LPCA_${subject:1:5}_nii4D.nii.gz";
    if preppath is None:
        inputsfolder = os.path.join(mainpath, f"{project_name}-inputs")
        final_ref = os.path.join(inputsfolder, f"{subject}_coreg.nii.gz")
        if os.path.exists(final_ref):
            if Path(final_ref).is_symlink():
                final_ref=str(Path(final_ref).resolve())
        else:
            raise Exception("Could not find final registered subject file")

    mytype="fa"
    symbolic_ref = os.path.join(out_dir,f"{subject}_Reg_LPCA_nii4D.nii.gz")
    final_image_test = os.path.join(out_dir,f"{subject}_{mytype}_to_MDT_test.nii.gz")

    overwrite = False
    if not os.path.exists(final_image_test) or overwrite:
        if verbose:
            print(f"Porting subject to MDT for subject: {subject} to {final_image_test}")

        img_transposed_test = os.path.join(dirty_dir,f"{subject}_{type}_transposed_test.nii.gz")
        img_transposed_test_2 = os.path.join(dirty_dir,f"{subject}_{type}_transposed_test_2.nii.gz")

        if os.path.exists(subject_img_path) and preprocess_ref:
            header_superpose(preprocess_ref, subject_img_path, outpath=img_transposed_test)
            cmd = f"antsApplyTransforms -v 1 -d 3 -i {subject_img_path} -o {img_transposed_test_2} -r {preprocess_ref} -n MultiLabel -t [{trans}] [{rigid},1] [{affine},1]"
            if verbose:
                print(f"Runnings the Ants apply transforms to {subject_img_path}")
                print(cmd)
            os.system(cmd)

        if not os.path.exists(preprocess_labels) or overwrite:
            #Note, it used to be MultiLabel[1.0x1.0x1.0,2] but it seems like the default parameters tend to be based on the image themselves and work fine,
            #so be careful but I removed it for now so it would work on different image sets
            cmd = f"antsApplyTransforms -v 1 -d 3 -i {atlas_labels} -o {preprocess_labels} -r {preprocess_ref} -n MultiLabel -t [{trans},1] [{rigid},1] [{affine},1] {MDT_to_subject} [{MDT_to_atlas_affine},1] {atlas_to_MDT}"
            if verbose:
                print(f"Runnings the Ants apply transforms to {atlas_labels}")
                print(cmd)
            os.system(cmd)

        if os.path.exists(preprocess_labels) and not ((os.path.exists(fixed_preprocess_labels)) or overwrite):
            header_superpose(final_ref, preprocess_labels, outpath=fixed_preprocess_labels)

        if os.path.exists(fixed_preprocess_labels) and not (os.path.exists(coreg_labels) or overwrite):

            if orientation_out != orientation_in:
                if verbose:
                    print(f"Reorientation from {orientation_out} back to {orientation_in}")
                img_transform_exec(fixed_preprocess_labels, orientation_out, orientation_in, coreg_labels, recenter=recenter)
            else:
                if verbose:
                    print(f"Orientations are the same, skipping reorientation")
                shutil.copy(fixed_preprocess_labels, coreg_labels)

        if os.path.exists(coreg_labels):
            header_superpose(final_ref, coreg_labels, outpath=final_labels)
            #cmd = f"{os.path.join(gunniespath, 'nifti_header_splicer.bash')} {final_ref} {coreg_labels} {final_labels}"
            #os.system(cmd)

        if os.path.exists(final_labels):
            if verbose:
                print(f"Applying fsl maths to {final_labels}")
            cmd = f"fslmaths {final_labels} -add 0 {final_labels} -odt short"
            os.system(cmd)
    else:
        print(f"Already calculated the label file for subject {subject}")

    skip_making_data_package_for_tractography = True

    if not skip_making_data_package_for_tractography:

        print('if you see this, make the bval fixes')
        if not os.path.exists9(symbolic_ref):
            cmd=f"ln - s {final_ref} {symbolic_ref}"

        bvals = os.path.join(mainpath, f"diffusion_prep_{subject}", f"{subject}_bvals.txt")
        bvecs = os.path.join(mainpath, f"diffusion_prep_{subject}", f"{subject}_bvecs.txt")

        bval_copy = os.path.join(out_dir, f"{subject}_bvals.txt")
        bvec_copy = os.path.join(out_dir, f"{subject}_bvecs.txt")

        if not os.path.exists(bval_copy):
            shutil.copy(bvals, bval_copy)

        if not os.path.exists(bvec_copy):
            shutil.copy(bvecs, bvec_copy)
Example #16
0
munin=False
if munin:
    gunniespath = "~/wuconnectomes/gunnies"
    mainpath = "/mnt/munin/Whitson/BrainChAMD.01/"
    outpath = "/mnt/munin6/Badea/Lab/human/AMD/diffusion_prep_locale/"
    bonusshortcutfolder = "/mnt/munin6/Badea/Lab/mouse/whiston_symlink_pool_test/"
else:
    gunniespath = "/Users/alex/bass/gitfolder/wuconnectomes/gunnies/"
    mainpath="/Volumes/Data/Whitson/BrainChAMD.01/"
    outpath = "/Volumes/Data/Badea/Lab/human/AMD/diffusion_prep_locale/"
    bonusshortcutfolder = "/Volumes/Data/Badea/Lab/mouse/whiston_symlink_pool_test/"
    bonusshortcutfolder = None

diffpath = os.path.join(mainpath, "Data","Anat")

mkcdir(outpath)
subjects = ["29056", "26578", "29060", "26637", "29264", "26765", "29225", "26660", "29304", "26890", "29556", "26862", "29410", "26966", "29403", "26841", "21593", "27126", "29618", "27111", "29627", "27164", "29502", "27100", "27381", "21836", "27391", "21850", "27495", "21729", "27488", "21915", "27682", "21956", "27686", "22331", "28208", "21990", "28955", "29878", "27719", "22102", "27841", "22101", "27842", "22228", "28029", "22140", "27852", "22276", "27999", "22369", "28115", "22644", "28308", "22574", "28377", "22368", "28325", "22320", "28182", "22898", "28748", "22683", "28373", "22536", "28433", "22825", "28662", "22864", "28698", "23143", "28861", "23157", "28820", "23028", "29002", "23210", "29020", "23309", "29161", "26841", "26862", "26949", "26966", "27100", "27126", "27163", "27246", "27488", "27682", "27686", "27719", "27841", "27842", "27852", "27869", "27999", "28029", "28068", "28208", "28262", "28325", "28820", "28856", "28869", "28955", "29002", "29044", "29089", "29127", "29161", "29242", "29254", "26578", "26637", "26660", "26745", "26765", "26850", "26880", "26890", "26958", "26974", "27017", "27111", "27164", "27381", "27391", "27495", "27610", "27640", "27680", "27778", "27982", "28115", "28308", "28338", "28373", "28377", "28433", "28437", "28463", "28532", "28662", "28698", "28748", "28809", "28857", "28861", "29013", "29020", "29025"]
subjects = ["27999"]

subject_processes, function_processes = parse_arguments(sys.argv,subjects)

proc_subjn="H"
proc_name ="diffusion_prep_"+proc_subjn
denoise = "mpca"
masking = "bet"
overwrite=True
cleanup = True
atlas = None
gettranspose=False
verbose=True
nominal_bval=1000
def create_backport_labels(subject, mainpath, project_name, prep_folder, atlas_labels, headfile=None, overwrite=False, verbose=True):

    #mainpath = "/Volumes/Data/Badea/Lab/mouse/"
    #gunniespath = "/Users/alex/bass/gitfolder/wuconnectomes/gunnies/"
    #project_name = "VBM_21ADDecode03_IITmean_RPI_fullrun"
    #atlas_labels = "/Volumes/Data/Badea/Lab/atlas/IITmean_RPI/IITmean_RPI_labels.nii.gz"

    out_dir = os.path.join(mainpath, f"{project_name}-results","connectomics",subject)
    mkcdir(out_dir)
    work_dir = os.path.join(mainpath, f"{project_name}-work")
    dirty_dir = os.path.join(mainpath,"burn_after_reading")
    mkcdir(dirty_dir)

    template_type_prefix = os.path.basename(os.path.dirname(glob.glob(os.path.join(work_dir,"dwi","SyN*/"))[0]))
    template_runs = glob.glob((os.path.join(work_dir,"dwi",template_type_prefix,"*/")))

    mymax=-1
    if mymax==-1:
        for template_run in template_runs:
            if "NoNameYet" in template_run and template_run[-4:-2] == "_i":
                if int(template_run[-2]) > mymax:
                    mymax = int(template_run[-2])
                    final_template_run = template_run
    if mymax==-1:
        for template_run in template_runs:
            if "dwiMDT_Control_n72" in template_run and template_run[-4:-2]=="_i":
                if int(template_run[-2])>mymax:
                    mymax=int(template_run[-2])
                    final_template_run=template_run
    if mymax == -1:
        raise Exception(f"Could not find template runs in {os.path.join(mainpath, f'{project_name}-work','dwi',template_type_prefix)}")
    #template_type_prefix = "faMDT_NoNameYet_n37_i6"
    #final_template_run = "SyN_0p5_3_0p5_fa"

    #final_ref = f"/mnt/munin6/Badea/Lab/human/AD_Decode/diffusion_prep_locale/diffusion_prep_{subject}/Reg_LPCA_{subject}_nii4D.nii.gz";
    orient_string = os.path.join(prep_folder, f'{subject}_relative_orientation.txt')
    if not os.path.exists(orient_string):
        orient_strings = glob.glob(os.path.join(prep_folder, f'*_relative_orientation.txt'))
        if np.size(orient_strings)>0:
            orient_string = orient_strings[0]
    if os.path.exists(orient_string):
        orient_relative = open(orient_string, mode='r').read()
        orientation_out = orient_relative.split(',')[0]
        orientation_out = orientation_out.split(':')[1]
        orientation_in = orient_relative.split(',')[1]
        orientation_in = orientation_in.split(':')[1]
    else:
        orientation_out = "*"
        warnings.warn("Could not find orientation file, may cause errors later")
        orientation_in = "RAS"
        orientation_out = "RAS"

    if headfile is not None:
        SAMBA_orientation_in, SAMBA_orientation_out = get_info_SAMBA_headfile(headfile)
    else:
        SAMBA_orientation_in, SAMBA_orientation_out = 'RAS', 'RAS'


    trans = os.path.join(work_dir,"preprocess","base_images","translation_xforms",f"{subject}_0DerivedInitialMovingTranslation.mat")
    rigid =os.path.join(work_dir,"dwi",f"{subject}_rigid.mat")
    affine = os.path.join(work_dir,"dwi",f"{subject}_affine.mat")
    MDT_to_subject = os.path.join(final_template_run,"reg_diffeo",f"MDT_to_{subject}_warp.nii.gz")
    label_name = os.path.basename(atlas_labels)
    label_name = label_name.split("_labels")[0]
    MDT_to_atlas_affine = os.path.join(final_template_run,"stats_by_region","labels","transforms",f"MDT_*_to_{label_name}_affine.mat")
    atlas_to_MDT = os.path.join(final_template_run,"stats_by_region","labels","transforms",f"{label_name}_to_MDT_warp.nii.gz")

    [trans, rigid, affine, MDT_to_subject, MDT_to_atlas_affine, atlas_to_MDT], exists = check_files([trans,rigid,affine,MDT_to_subject,MDT_to_atlas_affine,atlas_to_MDT])


    preprocess_ref = os.path.join(work_dir,"preprocess",f"{subject}_fa_masked.nii.gz")
    preprocess_labels = os.path.join(dirty_dir,f"{subject}_preprocess_labels.nii.gz")
    fixed_preprocess_labels = os.path.join(dirty_dir,f"{subject}_fixed_preprocess_labels.nii.gz")
    coreg_labels = os.path.join(dirty_dir,f"{subject}_{orientation_out}_labels.nii.gz")
    coreg_reorient_labels = os.path.join(dirty_dir,f"{subject}_{SAMBA_orientation_in}_labels.nii.gz")

    # final_ref=f"/mnt/munin6/Badea/Lab/mouse/co_reg_LPCA_${subject:1:5}_m00-results/Reg_LPCA_${subject:1:5}_nii4D.nii.gz";

    subjspace_coreg = os.path.join(prep_folder, f"{subject}_subjspace_coreg.nii.gz")
    final_refs = [subjspace_coreg]
    abb20s = glob.glob(f'/Volumes/dusom_civm-atlas/20.abb.15/research/diffusion{subject}*/*.nii')
    if np.size(abb20s)>0:
        final_refs.append(abb20s[0])
    abb19s = glob.glob(f'/Volumes/dusom_civm-atlas/19.abb.14/research/diffusion{subject}*/*.nii')
    if np.size(abb19s)>0:
        final_refs.append(abb19s[0])
    abb18s = glob.glob(f'/Volumes/dusom_civm-atlas/18.abb.11/research/diffusion{subject}*/*.nii')
    if np.size(abb18s) > 0:
        final_refs.append(abb18s[0])
    final_ref = None
    for pos_final_ref in final_refs:
        if isinstance(pos_final_ref, str) and os.path.exists(pos_final_ref):
            if Path(pos_final_ref).is_symlink():
                final_ref=str(Path(pos_final_ref).resolve())
            else:
                final_ref = pos_final_ref
    if final_ref is None:
        txt = f"Could not find final registered subject file for subject {subject}"
        warnings.warn(txt)
        return


    symbolic_ref = os.path.join(out_dir,f"{subject}_Reg_LPCA_nii4D.nii.gz")
    final_labels = os.path.join(out_dir,f"{subject}_IITmean_preprocess_labels.nii.gz")
    final_labels_backup = os.path.join(dirty_dir,f"{subject}_IITmean_preprocess_labels.nii.gz")
    superpose=True
    if not os.path.exists(final_labels) or overwrite:
        if verbose:
            print(f"Backporting labels to raw space for subject: {subject} to {final_labels}")
        if not os.path.exists(preprocess_labels) or overwrite:
            #Note, it used to be MultiLabel[1.0x1.0x1.0,2] but it seems like the default parameters tend to be based on the image themselves and work fine,
            #so be careful but I removed it for now so it would work on different image sets
            cmd = f"antsApplyTransforms -v 1 -d 3 -i {atlas_labels} -o {preprocess_labels} -r {preprocess_ref} -n MultiLabel -t [{trans},1] [{rigid},1] [{affine},1] {MDT_to_subject} [{MDT_to_atlas_affine},1] {atlas_to_MDT}"
            if verbose:
                print(f"Runnings the Ants apply transforms to {atlas_labels}")
                print(cmd)
            os.system(cmd)
        if os.path.exists(preprocess_labels) and ((not os.path.exists(fixed_preprocess_labels)) or overwrite):
            header_superpose(final_ref, preprocess_labels, outpath=fixed_preprocess_labels)

        if os.path.exists(fixed_preprocess_labels) and (not os.path.exists(coreg_labels) or overwrite):

            if orientation_out != orientation_in:
                if verbose:
                    print(f"Reorientation from {orientation_out} back to {orientation_in}")
                img_transform_exec(fixed_preprocess_labels, orientation_out, orientation_in, coreg_labels)
            else:
                if verbose:
                    print(f"Orientations are the same, skipping reorientation")
                shutil.copy(fixed_preprocess_labels, coreg_labels)

        if os.path.exists(coreg_labels) and (not os.path.exists(coreg_reorient_labels) or overwrite):

            if SAMBA_orientation_out != SAMBA_orientation_in:
                if verbose:
                    print(f"Reorientation from {SAMBA_orientation_out} back to {SAMBA_orientation_in}")
                img_transform_exec(coreg_labels, SAMBA_orientation_out, SAMBA_orientation_in,
                                   output_path=coreg_reorient_labels)
            else:
                if verbose:
                    print(f"Orientations are the same, skipping reorientation")
                if coreg_labels != coreg_reorient_labels:
                    warnings.warn('There is a discrepancy between the stated input orientation in headfile and the '
                                  'one found by find_relative_orientation_by_CoM.bash, beware')
                    coreg_reorient_labels = coreg_labels

        if os.path.exists(coreg_reorient_labels):
            header_superpose(final_ref, coreg_reorient_labels, outpath=final_labels)
            #cmd = f"{os.path.join(gunniespath, 'nifti_header_splicer.bash')} {final_ref} {coreg_labels} {final_labels}"
            #os.system(cmd)

        if os.path.exists(final_labels):
            if verbose:
                print(f"Applying fsl maths to {final_labels}")
            cmd = f"fslmaths {final_labels} -add 0 {final_labels} -odt short"
            os.system(cmd)
            shutil.copy(final_labels, final_labels_backup)
    else:
        print(f"Already calculated the label file for subject {subject}")

    skip_making_data_package_for_tractography = True

    if not skip_making_data_package_for_tractography:

        print('if you see this, make the bval fixes')
        if not os.path.exists(symbolic_ref):
            cmd=f"ln - s {final_ref} {symbolic_ref}"

        bvals = os.path.join(mainpath, f"diffusion_prep_{subject}", f"{subject}_bvals.txt")
        bvecs = os.path.join(mainpath, f"diffusion_prep_{subject}", f"{subject}_bvecs.txt")

        bval_copy = os.path.join(out_dir, f"{subject}_bvals.txt")
        bvec_copy = os.path.join(out_dir, f"{subject}_bvecs.txt")

        if not os.path.exists(bval_copy):
            shutil.copy(bvals, bval_copy)

        if not os.path.exists(bvec_copy):
            shutil.copy(bvecs, bvec_copy)
trk_folder = '/Volumes/Data/Badea/Lab/jacques/ADDecode_test_connectomes/TRK_MPCA_fixed'
new_trk_folder = '/Volumes/Data/Badea/Lab/jacques/ADDecode_test_connectomes/TRK_MPCA_fixed_100'

trk_folder = '/Users/alex/jacques/AD_decode_comparison_temp/'
new_trk_folder = '/Users/alex/jacques/AD_decode_comparison_100_temp/'

trk_folder = '/Volumes/Data/Badea/Lab/human/AD_Decode/Analysis/TRK_MPCA'
new_trk_folder = '/Volumes/Data/Badea/Lab/human/AD_Decode/Analysis/TRK_MPCA_100'

trk_folder = '/Volumes/Data/Badea/Lab/human/AD_Decode/Analysis/TRK_MPCA_MDT_fixed'
new_trk_folder = '/Volumes/Data/Badea/Lab/human/AD_Decode/Analysis/TRK_MPCA_MDT_fixed_100'

trk_folder = '/Volumes/Data/Badea/Lab/human/AD_Decode/Analysis/TRK_MPCA_fixed'
new_trk_folder = '/Volumes/Data/Badea/Lab/human/AD_Decode/Analysis/TRK_MPCA_fixed_100'

mkcdir(new_trk_folder)
ratio = 100
filelist = os.listdir(trk_folder)
filelist = sorted(filelist)
random.shuffle(filelist)
#filelist.reverse()

for filepath in filelist:
    filename, f_ext = os.path.splitext(filepath)
    if f_ext == '.trk' and f'ratio_{str(ratio)}' not in filename:
        newfilename = filename.replace('_all','_ratio_'+str(ratio))
        newfilepath = os.path.join(new_trk_folder, newfilename +f_ext)
        if not os.path.exists(newfilepath):
            print(f'Downsampling from {os.path.join(trk_folder,filepath)} to {newfilepath} with ratio {str(ratio)}')
            reducetractnumber(os.path.join(trk_folder,filepath), newfilepath, getdata=False, ratio=100, return_affine= False, verbose=False)
            print(f'succesfully created {newfilepath}')
Example #19
0
	main_path = '/Volumes/Data/Badea/Lab/human/'
else:
	raise Exception('No other computer name yet')

project = "AD_Decode"

if project == "AD_Decode":
	path_TRK = os.path.join(main_path, 'AD_Decode', 'Analysis', 'TRK_MPCA_100')
	path_TRK_output = os.path.join(main_path, 'AD_Decode', 'Analysis', 'TRK_MPCA_MDT_100')
	path_DWI = os.path.join(main_path, 'AD_Decode', 'Analysis', 'DWI')
	path_transforms = os.path.join(main_path, 'AD_Decode', 'Analysis','Transforms')
	ref = "md"
	path_trk_tempdir = os.path.join(main_path, 'AD_Decode', 'Analysis', 'TRK_save')
	DWI_save = os.path.join(main_path, 'AD_Decode', 'Analysis', 'NII_tempsave')

	mkcdir([path_trk_tempdir,path_TRK_output, DWI_save])
	#Get the values from DTC_launcher_ADDecode. Should probalby create a single parameter file for each project one day
	stepsize = 2
	ratio = 100
	trkroi = ["wholebrain"]
	str_identifier = get_str_identifier(stepsize, ratio, trkroi)

overwrite = True
cleanup = False
verbose=True
save_temp_files = True
recenter=1
contrast='dwi'
native_ref=''

orient_string = os.path.join(path_DWI, 'relative_orientation.txt')
from pandas import ExcelFile
from BIAC_tools import isempty

import sys, getopt

subjects = ["00393", "00490", "00560", "00680", "00699", "00795","01952","02263"]
weird_subjects = ["02432"]
atlas_legends = "/Users/alex/jacques/connectomes_testing//atlases/CHASSSYMM3AtlasLegends.xlsx"

outpath = "/Volumes/Data/Badea/Lab/human/Sinha_epilepsy/"
datapath = os.path.join(outpath, "DWI")
figspath = os.path.join(outpath, "Figures")
dwi_preprocessed = os.path.join(outpath, "DWI_temp")
trkpath = os.path.join(outpath, "TRK")

mkcdir([outpath, figspath, dwi_preprocessed, trkpath])
masktype = "FA"
masktype = "T1"
masktype = "dwi"


max_processors = 10

if mp.cpu_count() < max_processors:
    max_processors = mp.cpu_count()

subject_processes = np.size(subjects)
subject_processes = 1
if max_processors < subject_processes:
    subject_processes = max_processors
function_processes = np.int(max_processors / subject_processes)
            outpath=outpath,
            writeformat=writeformat,
            overwrite=overwrite)  #extractbvals_research
        #fbvals, fbvecs = rewrite_subject_bvalues(diffpath, subject, outpath=outpath, writeformat=writeformat, overwrite=overwrite)
elif btables == "copy":
    for subject in subjects:
        #outpathsubj = "/Volumes/dusom_dibs_ad_decode/all_staff/APOE_temp/diffusion_prep_58214/"
        outpathsubj = os.path.join(outpath, proc_name + subject)
        outpathbval = os.path.join(outpathsubj,
                                   proc_subjn + subject + "_bvals.txt")
        outpathbvec = os.path.join(outpathsubj,
                                   proc_subjn + subject + "_bvecs.txt")
        outpathrelative = os.path.join(outpath, "relative_orientation.txt")
        newoutpathrelative = os.path.join(outpathsubj,
                                          "relative_orientation.txt")
        mkcdir(outpathsubj)
        shutil.copy(outpathrelative, newoutpathrelative)
        if not os.path.exists(outpathbval) or not os.path.exists(
                outpathbvec) or overwrite:
            mkcdir(outpathsubj)
            writeformat = "tab"
            writeformat = "dsi"
            overwrite = True
            bvals = glob.glob(os.path.join(outpath, "*N58408*bvals*.txt"))
            bvecs = glob.glob(os.path.join(outpath, "*N58408*bvec*.txt"))
            if np.size(bvals) > 0 and np.size(bvecs) > 0:
                shutil.copy(bvals[0], outpathbval)
                shutil.copy(bvecs[0], outpathbvec)

bval_file = "/Volumes/Data/Badea/Lab/jacques/APOE_series/diffusion_prep_locale/diffusion_prep_N58408/N58408_bvals.txt"
bvec_file = "/Volumes/Data/Badea/Lab/jacques/APOE_series/diffusion_prep_locale/diffusion_prep_N58408/N58408_bvecs.txt"
#main_folder = "/Volumes/dusom_dibs_ad_decode/all_staff/APOE_temp/"
#dwipath = main_folder + "/VBM_19BrainChAMD01_IITmean_RPI_with_2yr-results/connectomics/"

main_folder = "/Users/alex/jacques/APOE_temp/"
atlas_legends = "/Volumes/Data/Badea/Lab/atlases/CHASSSYMM3AtlasLegends.xlsx"
samos = False
if samos:
    main_folder = "/mnt/paros_MRI/jacques/APOE/"
    atlas_legends = "/mnt/paros_MRI/jacques/atlases/CHASSSYMM3AtlasLegends.xlsx"

dwipath_preprocessed = os.path.join(main_folder,"diff_whiston_preprocessed")
dwipath = os.path.join(main_folder,'DWI_allsubj')
outtrkpath = os.path.join(main_folder + 'TRK_allsubj')
figspath = os.path.join(main_folder + "Figures_RAS_allsubj_lr")

mkcdir([outtrkpath,figspath])

outpathpickle = figspath

stepsize = 2

# accepted values are "small" for one in ten streamlines, "all or "large" for all streamlines,
# "none" or None variable for neither and "both" for both of them

targetrois = ["Cerebellum"]
ratio = 1
if ratio == 1:
    saved_streamlines = "_all_"
else:
    saved_streamlines = "_ratio_" + str(ratio) + '_'
Example #23
0
    output_folder = '/Users/alex/jacques/APOE_test/DWI_allsubj_RAS/'

if 'samos' in computer_name:
    DWI_folder = '/mnt/paros_MRI/jacques/APOE/DWI_allsubj/'
    labels_folder = '/mnt/paros_MRI/jacques/APOE/DWI_allsubj/'
    output_folder = '/mnt/paros_MRI/jacques/APOE/DWI_allsubj_RAS/'

subjects_all = glob.glob(os.path.join(DWI_folder, '*_subjspace_coreg.nii.gz'))
subjects_list = []
for subject in subjects_all:
    subject_name = os.path.basename(subject)
    subjects_list.append(subject_name[:6])
subjects_list.sort()
subjects_list = subjects_list[:]
print(subjects_list)
mkcdir(output_folder)

for subject in subjects_list:
    coreg_file = os.path.join(DWI_folder, f'{subject}_subjspace_coreg.nii.gz')
    labels_file = os.path.join(labels_folder, f'{subject}_labels.nii.gz')
    labels_lr_file = os.path.join(labels_folder,
                                  f'{subject}_labels_lr_ordered.nii.gz')
    mask_file = os.path.join(DWI_folder, f'{subject}_subjspace_mask.nii.gz')
    coreg_RAS_file = os.path.join(output_folder, f'{subject}_coreg_RAS.nii.gz')
    labels_RAS_file = os.path.join(output_folder,
                                   f'{subject}_labels_RAS.nii.gz')
    labels_RAS_lr_file = os.path.join(
        output_folder, f'{subject}_labels_lr_ordered_RAS.nii.gz')
    mask_RAS_file = os.path.join(output_folder, f'{subject}_RAS_mask.nii.gz')
    transferred = 0
    if not os.path.exists(coreg_RAS_file):
Example #24
0
def launch_preprocessing(subj, raw_nii, outpath, cleanup=False, nominal_bval=4000, SAMBA_inputs_folder=None,
                         shortcuts_all_folder = None, gunniespath="~/gunnies/", processes=1, masking="bet", ref=None,
                         transpose=None, overwrite=False, denoise='None', recenter=0, verbose=False):

    proc_name ="diffusion_prep_" # Not gonna call it diffusion_calc so we don't assume it does the same thing as the civm pipeline
    work_dir=os.path.join(outpath,proc_name+subj)
    """
    for filePath in glob.glob(os.path.join(work_dir,'*')):
        modTimesinceEpoc = os.path.getmtime(filePath)
        modificationTime = time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(modTimesinceEpoc))
        if modificationTime[5:7]=='09' and int(modificationTime[8:10])>9:
            os.remove(filePath)
    """
    if verbose:
        print(f"Processing diffusion data with runno/subj: {subj}")
        print(f"Work directory is {work_dir}")
    mkcdir(work_dir)

    sbatch_folder =os.path.join(work_dir,"sbatch")
    mkcdir(sbatch_folder)
    #nii_path = os.path.join(work_dir,'nii4D_'+subj + '.nii.gz')
    #if not os.path.exists(nii_path):
    #    shutil.copy(raw_nii, nii_path)
    nii_name=os.path.split(raw_nii)[1]
    niifolder = os.path.dirname(raw_nii)
    ext = ".nii.gz"
    nii_ext=getext(nii_name)
    bxheader = nii_name.replace(nii_ext,".bxh")
    bxheader = os.path.join(niifolder, bxheader)
    bvecs = os.path.join(work_dir, subj+"_bvecs.txt")
    bvals =bvecs.replace("bvecs","bvals")

    if verbose:
        print(f"Original nifti is at {nii_name}\nbvecs are at {bvecs}\nbvals are at {bvals}\n")
    if not os.path.exists(bvecs):
        if verbose:
            print("Extracting diff directions")
        #print("Bvals and bvecs not found, using extractdiffdirs, however it it NOT RELIABLE, beware!")
        bvec_cmd = (f"extractdiffdirs --colvectors --writebvals --fieldsep='\t' --space=RAI {bxheader} {bvecs} {bvals}")
        os.system(bvec_cmd)

    # Make dwi for mask generation purposes.
    tmp_mask = os.path.join(work_dir,f"{subj}_tmp_mask{ext}")
    raw_dwi = os.path.join(work_dir,f"{subj}_raw_dwi.nii.gz")
    orient_string = os.path.join(work_dir,"relative_orientation.txt")

    if shortcuts_all_folder is not None:
        #nii_path_link = os.path.join(shortcuts_all_folder, f"{subj}_rawnii{ext}")
        #if not os.path.exists(nii_path_link) or overwrite:
        #    buildlink(nii_path, nii_path_link)
        bvecs_new = os.path.join(shortcuts_all_folder, subj + "_bvecs.txt")
        bvals_new = os.path.join(shortcuts_all_folder, subj + "_bvals.txt")
        if not os.path.exists(bvecs_new) or not os.path.exists(bvals_new) or overwrite:
            shutil.copyfile(bvecs, bvecs_new)
            shutil.copyfile(bvals, bvals_new)

    final_mask = os.path.join(work_dir, f'{subj}_mask{ext}')

    #if (not os.path.exists(final_mask) and not os.path.exists(tmp_mask)) or overwrite:
    if not os.path.exists(tmp_mask) or overwrite:
        if not os.path.exists(raw_dwi) or overwrite:
            select_cmd = f"select_dwi_vols {raw_nii} {bvals} {raw_dwi} {nominal_bval} -m"
            os.system(select_cmd)
        if not os.path.exists(tmp_mask) or overwrite:
            if 'median' in masking:
                tmp = tmp_mask.replace("_mask", "")
                if np.size(masking.split('_'))>1:
                    median_radius = int(masking.split('_')[1])
                else:
                    median_radius = 4
                median_mask_make(raw_dwi, tmp, outpathmask=tmp_mask, median_radius = median_radius, numpass=median_radius)
            elif masking=="bet":
                tmp=tmp_mask.replace("_mask", "")
                bet_cmd = f"bet {raw_dwi} {tmp} -m -n -R"
                os.system(bet_cmd)
            else:
                raise Exception("Unrecognized masking type")

    # I think this part is done later more properly:     if create_subj_space_files: for contrast in ['dwi', 'b0', 'mask']:
    #if SAMBA_inputs_folder is not None:
    #    mask_subj_link = os.path.join(SAMBA_inputs_folder,f'{subj}_subjspace_mask{ext}')
    #    if not os.path.exists(mask_subj_link) or overwrite:
    #        shutil.copy(tmp_mask, mask_subj_link)

    #if cleanup and (os.path.exists(tmp_mask) and os.path.exists(raw_dwi)):
    #    os.remove(raw_dwi)
    #overwrite=False
    # Run Local PCA Denoising algorithm on 4D nifti:
    masked_nii = os.path.join(work_dir, nii_name)
    if not "nii.gz" in masked_nii:
        masked_nii = masked_nii.replace(".nii", ".nii.gz")
    masked_nii = masked_nii.replace(ext, "_masked" + ext)

    if denoise.lower()=='lpca':
        D_subj=f'LPCA_{subj}';
    elif denoise.lower()=='mpca':
        D_subj=f'MPCA_{subj}';
    elif denoise=="None" or denoise is None:
        D_subj = f'{subj}'

    if denoise=="None" or denoise is None:
        denoised_nii = masked_nii
        if not os.path.exists(masked_nii) or overwrite:
            fsl_cmd = f"fslmaths {raw_nii} -mas {tmp_mask} {masked_nii} -odt 'input'";
            os.system(fsl_cmd)
    else:
        denoised_nii = os.path.join(work_dir,f"{D_subj}_nii4D.nii.gz")
        if not os.path.exists(denoised_nii) or overwrite:
            if not os.path.exists(masked_nii) or overwrite:
                fsl_cmd = f"fslmaths {raw_nii} -mas {tmp_mask} {masked_nii} -odt 'input'";
                os.system(fsl_cmd)
            basic_LPCA_denoise_func(subj,masked_nii,bvecs,denoised_nii, processes=processes,
                                    denoise=denoise, verbose=False) #to improve and make multiprocessing

    #if cleanup and os.path.exists(denoised_nii) and os.path.exists(masked_nii) and denoised_nii!=masked_nii:
    #    os.remove(masked_nii)

    # Run coregistration/eddy current correction:

    coreg_nii_old = f'{outpath}/co_reg_{D_subj}_m00-results/Reg_{D_subj}_nii4D{ext}';
    coreg_nii = os.path.join(work_dir,f'Reg_{D_subj}_nii4D{ext}')
    if not cleanup:
        coreg_nii=coreg_nii_old
    if not os.path.exists(coreg_nii) or overwrite:
        if not os.path.exists(coreg_nii_old) or overwrite:
            temp_cmd = os.path.join(gunniespath,'co_reg_4d_stack_tmpnew.bash')+f' {denoised_nii} {D_subj} 0 {outpath} 0';
            os.system(temp_cmd)
        if cleanup:
            shutil.move(coreg_nii_old,coreg_nii)

    if shortcuts_all_folder is not None:
        coreg_link = os.path.join(shortcuts_all_folder,f'{subj}_subjspace_coreg{ext}')
        if not os.path.exists(coreg_link) or overwrite:
            buildlink(coreg_nii, coreg_link)

    toeddy=False
    if toeddy:
        #fsl_cmd = f"fslmaths {raw_nii} -mas {tmp_mask} {masked_nii} -odt 'input'";
        #os.system(fsl_cmd)
        eddy_cmd = f"eddy --imain={coreg_nii} --mask={tmp_mask} --acqp=acq_params.txt --index={os.path.join(work_dir,'index.txt')} --bvecs={bvecs} --bvals={bvals} --topup=topup_results --repol --out = {os.path.join(work_dir,f'Reg_{D_subj}_nii4D_eddy{ext}')}"
        os.system(eddy_cmd)

    coreg_inputs=os.path.join(outpath,f'co_reg_{D_subj}_m00-inputs')
    coreg_work=coreg_inputs.replace('-inputs','-work')
    coreg_results=coreg_inputs.replace('-inputs','-results')
    if cleanup and os.path.exists(coreg_nii) and os.path.isdir(coreg_inputs):
        shutil.rmtree(coreg_inputs)
    if cleanup and os.path.exists(coreg_nii) and os.path.isdir(coreg_work):
        shutil.rmtree(coreg_work)
    if cleanup and os.path.exists(coreg_nii) and os.path.isdir(coreg_results):
        shutil.rmtree(coreg_results)

    # Generate tmp DWI:

    tmp_dwi_out=os.path.join(work_dir, f'{subj}_tmp_dwi{ext}')
    dwi_out=os.path.join(work_dir,f'{subj}_dwi{ext}')

    if not os.path.exists(tmp_dwi_out) or overwrite:
        cmd=f'select_dwi_vols {coreg_nii} {bvals} {tmp_dwi_out} {nominal_bval}  -m'
        os.system(cmd)

    # Generate tmp B0:
    tmp_b0_out=os.path.join(work_dir,f'{subj}_tmp_b0{ext}')
    b0_out = os.path.join(work_dir, f'{subj}_b0{ext}')
    if (not os.path.exists(b0_out) and not os.path.exists(tmp_b0_out)) or overwrite:
        cmd=f'select_dwi_vols {coreg_nii} {bvals} {tmp_b0_out} 0  -m;'
        os.system(cmd)
    #overwrite=False
    #elif cleanup and os.path.exists(tmp_b0_out):
    #    os.remove(tmp_b0_out)

    # Generate DTI contrasts and perform some tracking QA:
    if cleanup:
        c_string=' --cleanup '
    else:
        c_string=''

    #Important note: this is what first creates the fa, md, etc
    if len(glob.glob(os.path.join(work_dir,f'*.fib.gz.md{ext}'))) == 0 or overwrite:
        if overwrite:
            oldfiles = glob.glob(os.path.join(work_dir, f'*.fib.gz*'))
            for oldfile in oldfiles:
                os.remove(oldfile)
        cmd = 'bash ' + os.path.join(gunniespath,'dti_qa_with_dsi_studio_weirdcall.bash')+f' {coreg_nii} {bvecs} {tmp_mask} {work_dir} {c_string}';
        os.system(cmd)

    #Save the subject space dti results

    #Generate tmp MD:
    for contrast in ['md']:
        real_file=largerfile(os.path.join(work_dir,f'*.fib.gz.{contrast}{ext}'))  #Catch the 'real file' for each contrast
        tmp_file = f'{work_dir}/{subj}_tmp_{contrast}{ext}';
        if not os.path.exists(tmp_file):
            shutil.copy(real_file,tmp_file)

    tmp_md = f'{work_dir}/{subj}_tmp_md{ext}';

    if ref=="md" or ref is None:
        reference=tmp_md
    elif ref=="coreg":
        reference=coreg_nii
    elif os.path.exists(ref):
        reference=ref

    reference_file = os.path.join(work_dir, f'{subj}_reference{ext}')
    if not os.path.exists(reference_file):
        shutil.copy(reference, reference_file)

    if shortcuts_all_folder is not None:
        bonus_ref_link = os.path.join(shortcuts_all_folder, f'{subj}_reference{ext}')
        if not os.path.exists(bonus_ref_link) or overwrite:
            buildlink(reference_file,bonus_ref_link)

    #give new header to the non-dti files using md as reference


    for contrast in ['dwi', 'b0', 'mask']:
        tmp_file=os.path.join(work_dir,f'{subj}_tmp_{contrast}{ext}')
        tmp2_file=os.path.join(work_dir,f'{subj}_tmp2_{contrast}{ext}')
        final_file=os.path.join(work_dir,f'{subj}_{contrast}{ext}')
        if ((not os.path.exists(tmp2_file) and not os.path.exists(final_file)) or overwrite):
            if not os.path.exists(tmp_file):
                raise Exception("Tmp file was not created, need to rerun previous processes")
            else:
                header_superpose(reference, tmp_file, outpath=tmp2_file)

    create_subj_space_files = True
    if create_subj_space_files:
        for contrast in ['dwi', 'b0', 'mask']:
            tmp_file = os.path.join(work_dir, f'{subj}_tmp_{contrast}{ext}')
            subj_file = os.path.join(work_dir, f'{subj}_subjspace_{contrast}{ext}')
            if not os.path.exists(subj_file) or overwrite:
                if not os.path.exists(tmp_file):
                    raise Exception("Tmp file was not created, need to rerun previous processes")
                else:
                    header_superpose(raw_dwi, tmp_file, outpath=subj_file)
            if shortcuts_all_folder is not None:
                subj_link = os.path.join(shortcuts_all_folder, f'{subj}_subjspace_{contrast}{ext}')
                if not os.path.exists(subj_link) or overwrite:
                    buildlink(subj_file, subj_link)

    #write the relative orientation file here
    if not os.path.isfile(orient_string) or overwrite:
        if os.path.isfile(orient_string):
            os.remove(orient_string)
        file = os.path.join(work_dir,subj+'_tmp_mask'+ext);
        cmd = 'bash ' + os.path.join(gunniespath,'find_relative_orientation_by_CoM.bash') + f' {reference_file} {file}'
        orient_relative = subprocess.getoutput(cmd)

        with open(orient_string, 'w') as f:
            f.write(orient_relative)
    else:
        orient_relative = open(orient_string, mode='r').read()

    if SAMBA_inputs_folder is not None:
        subj_orient_string = os.path.join(SAMBA_inputs_folder, f'{subj}_relative_orientation.txt')
        shutil.copy(orient_string, subj_orient_string)

    if shortcuts_all_folder is not None:
        subj_orient_string = os.path.join(shortcuts_all_folder, f'{subj}_relative_orientation.txt')
        shutil.copy(orient_string, subj_orient_string)

    #check extracted values from relative orientation vals
    orientation_out = orient_relative.split(',')[0]
    orientation_out = orientation_out.split(':')[1]
    orientation_in = orient_relative.split(',')[1]
    orientation_in = orientation_in.split(':')[1]
    if verbose:
        print(f'flexible orientation: {orientation_in}');
        print(f'reference orientation: {orientation_out}');

    #apply the orientation modification to specified contrasts
    for contrast in ['dwi', 'b0', 'mask']:
        img_in=os.path.join(work_dir,f'{subj}_tmp2_{contrast}{ext}')
        img_out=os.path.join(work_dir,f'{subj}_{contrast}{ext}')
        if not os.path.isfile(img_out) or overwrite:
            if orientation_out != orientation_in:
                print('TRYING TO REORIENT...b0 and dwi and mask')
                if os.path.exists(img_in) and (not os.path.exists(img_out) or overwrite):
                    img_transform_exec(img_in, orientation_in, orientation_out, img_out)
                    if os.path.exists(img_out):
                        os.remove(img_in)
                elif os.path.exists(img_out) and cleanup:
                    os.remove(img_in)
            else:
                shutil.move(img_in,img_out)

        if SAMBA_inputs_folder is not None:
            inputs_space_link = os.path.join(SAMBA_inputs_folder, f'{subj}_{contrast}{ext}')
            if not os.path.exists(inputs_space_link) or overwrite:
                buildlink(img_out, inputs_space_link)

        if shortcuts_all_folder is not None:
            inputs_space_link = os.path.join(shortcuts_all_folder, f'{subj}_{contrast}{ext}')
            if not os.path.exists(inputs_space_link) or overwrite:
                buildlink(img_out, inputs_space_link)


    mask = os.path.join(work_dir,f'{subj}_mask{ext}')
    b0 = os.path.join(work_dir,f'{subj}_b0{ext}')

    #if cleanup and os.path.exists(dwi_out) and os.path.exists(tmp_dwi_out):
    #    os.remove(tmp_dwi_out)

    for contrast in ['fa0', 'rd', 'ad', 'md']:
        real_file=largerfile(os.path.join(work_dir,f'*.fib.gz.{contrast}{ext}'))  # It will be fun times if we ever have more than one match to this pattern...
        #inputspace = real_file
        inputspace = os.path.join(work_dir, f'{subj}_inputspace_{contrast}{ext}')

        contrast=contrast.replace('0','')
        #linked_file=os.path.join(shortcut_dir,f'{subj}_{contrast}{ext}')
        linked_file_w=os.path.join(work_dir,f'{subj}_{contrast}{ext}')

        made_newfile = affine_superpose(dwi_out, real_file, outpath = inputspace, transpose=transpose)
        if not made_newfile:
            inputspace = real_file
        if not os.path.isfile(linked_file_w) or overwrite:
            buildlink(inputspace, linked_file_w)
        if SAMBA_inputs_folder is not None:
            #warnings.warn('should reach this!')
            blinked_file = os.path.join(SAMBA_inputs_folder, f'{subj}_{contrast}{ext}')
            if not os.path.exists(blinked_file) or overwrite:
                buildlink(inputspace, blinked_file)
                print(f'build link from {inputspace} to {blinked_file}')

        if shortcuts_all_folder is not None:
            #warnings.warn('should reach this!')
            blinked_file = os.path.join(shortcuts_all_folder, f'{subj}_{contrast}{ext}')
            if not os.path.exists(blinked_file) or overwrite:
                buildlink(inputspace, blinked_file)
                print(f'build link from {inputspace} to {blinked_file}')




    if create_subj_space_files:
        for contrast in ['fa0', 'rd', 'ad', 'md']:

            real_file = largerfile(os.path.join(work_dir,
                                                f'*.fib.gz.{contrast}{ext}'))  # It will be fun times if we ever have more than one match to this pattern...
            contrast = contrast.replace('0', '')
            subj_file_tmp = os.path.join(work_dir, f'{subj}_subjspace_tmp_{contrast}{ext}')
            subj_file = os.path.join(work_dir, f'{subj}_subjspace_{contrast}{ext}')
            if not os.path.exists(subj_file) or overwrite:
                if not os.path.exists(subj_file_tmp):
                    if orientation_out != orientation_in:
                        print('TRYING TO REORIENT...b0 and dwi and mask')
                        if os.path.exists(real_file) and (not os.path.exists(subj_file) or overwrite):
                            img_transform_exec(real_file, orientation_out, orientation_in, subj_file_tmp)
                    else:
                        shutil.copy(real_file,subj_file_tmp)
                header_superpose(raw_dwi, subj_file_tmp, outpath=subj_file)

            if shortcuts_all_folder is not None:
                subj_link = os.path.join(shortcuts_all_folder, f'{subj}_subjspace_{contrast}{ext}')
                buildlink(subj_file, subj_link)

    #if cleanup:
    #    tmp_files = glob.glob(os.path.join(work_dir, '*tmp*'))
    #    for file in tmp_files:
    #        os.remove(file)

    nii_path = os.path.join(work_dir,'nii4D_'+subj + ext)
    if os.path.exists(nii_path):
        os.remove(nii_path)

    if cleanup:
        reg_src = os.path.join(work_dir,'Reg_' + subj + f'_nii4D{ext}.src.gz')
        if os.path.exists(reg_src):
            os.remove(reg_src)
        reg_src_fib = os.path.join(work_dir,'Reg_' + subj + f'_nii4D{ext}.src.gz.dti.fib.gz')
        if os.path.exists(reg_src):
            os.remove(reg_src)
def img_transform_exec(img,
                       current_vorder,
                       desired_vorder,
                       output_path=None,
                       write_transform=0,
                       verbose=False):

    is_RGB = 0
    is_vector = 0
    is_tensor = 0

    if not os.path.exists(img):
        raise ('Nifti img file does not exists')

    for char in current_vorder:
        if char.islower():
            warnings.warn("Use uppercase for current order")
            current_vorder = current_vorder.upper()
    for char in desired_vorder:
        if char.islower():
            warnings.warn("Use uppercase for desired order")
            current_vorder = desired_vorder.upper()

    ordervals = 'RLAPSI'
    if not ordervals.find(current_vorder[0]) and not ordervals.find(
            current_vorder[1]) and not ordervals.find(current_vorder[2]):
        raise TypeError(
            'Please use only R L A P S or I for current voxel order')

    if not ordervals.find(desired_vorder[0]) and not ordervals.find(
            desired_vorder[1]) and not ordervals.find(desired_vorder[2]):
        raise TypeError(
            'Please use only R L A P S or I for desired voxel order')

    dirname, filename, ext = splitpath(img)
    filename = filename + "." + ext
    if output_path is None:
        output_name = filename.replace('.nii', '_' + desired_vorder + '.nii')
        output_path = os.path.join(dirname, output_name)
    if not output_path.find('.'):
        mkcdir(output_path)
        output_name = filename.replace('.nii', '_' + desired_vorder + '.nii')
        output_path = os.path.join(output_path, output_name)

    out_dir, _, _ = splitpath(output_path)
    affine_out = os.path.join(
        out_dir, current_vorder + '_to_' + desired_vorder + '_affine.pickle')

    overwrite = True
    if os.path.isfile(output_path) and not overwrite and (
            not write_transform or
        (write_transform and os.path.exists(affine_out))):
        warnings.warn('Existing output:%s, not regenerating', output_path)

    orig_string = 'RLAPSI'
    flip_string = 'LRPAIS'
    orig_current_vorder = current_vorder

    try:
        nii = nib.load(img)
    except:
        raise ('Could not load img at ' + img)
    nii_data = nii.get_data()
    hdr = nii.header

    dims = nii.shape
    if np.size(dims) > 6:
        raise ('Image has > 5 dimensions')
    elif np.size(dims) < 3:
        raise ('Image has < 3 dimensions')

    new_data = nii_data
    affine = nii._affine

    if desired_vorder != orig_current_vorder:
        if ((np.size(dims) > 4) and (dims(5) == 3)):
            is_vector = 1
        elif ((np.size(dims) > 5) and (dims(5) == 6)):
            is_tensor = 1

        #x_row = [1, 0, 0];
        #y_row = [0, 1, 0];
        #z_row = [0, 0, 1];
        x_row = affine[0, :]
        y_row = affine[1, :]
        z_row = affine[2, :]

        xpos = desired_vorder.find(current_vorder[0])
        if xpos == -1:
            if verbose:
                print('Flipping first dimension')
            val = 0
            new_data = np.flip(new_data, 0)
            orig_ind = orig_string.find(current_vorder[0])
            current_vorder = current_vorder[0:val] + flip_string[
                orig_ind] + current_vorder[val + 1:]
            if is_vector:
                new_data[:, :, :, 0, 1] = -new_data[:, :, :, 0, 1]
            x_row = [-1 * val for val in x_row]

        ypos = desired_vorder.find(current_vorder[1])
        if ypos == -1:
            if verbose:
                print('Flipping second dimension')
            val = 1
            new_data = np.flip(new_data, 1)
            orig_ind = orig_string.find(current_vorder[1])
            current_vorder = current_vorder[0:val] + flip_string[
                orig_ind] + current_vorder[val + 1:]
            if is_vector:
                new_data[:, :, :, 0, 2] = -new_data[:, :, :, 0, 2]
            y_row = [-1 * val for val in y_row]

        zpos = desired_vorder.find(current_vorder[2])
        if zpos == -1:
            if verbose:
                print('Flipping third dimension')
            val = 2
            new_data = np.flip(new_data, 2)
            orig_ind = orig_string.find(current_vorder[2])
            current_vorder = current_vorder[0:val] + flip_string[
                orig_ind] + current_vorder[val + 1:]
            if is_vector:
                new_data[:, :, :, 0, 2] = -new_data[:, :, :, 0, 2]
            z_row = [-1 * val for val in z_row]

        xpos = current_vorder.find(desired_vorder[0])
        ypos = current_vorder.find(desired_vorder[1])
        zpos = current_vorder.find(desired_vorder[2])

        if verbose:
            print([
                'Dimension order is:' + str(xpos) + ' ' + str(ypos) + ' ' +
                str(zpos)
            ])
        if not os.path.isfile(output_path) or overwrite:
            if np.size(dims) == 5:
                if is_tensor:
                    new_data = new_data.tranpose(xpos, ypos, zpos, 3, 4)
                else:
                    if is_vector:  # =>> honestly looking at the original code, this doesnt really make sense to me, so deactivated for now. Will raise warning in case it happens
                        warnings.warn('is vector not properly implemented')
                        #    new[:,:,:,1,:] = new[:,:,:,1].transpose(xpos, ypos, zpos)
                        #new=new(:,:,:,[xpos, ypos, zpos]);
                    new_data.transpose(xpos, ypos, zpos, 3, 4)
            elif np.size(dims) == 4:
                if is_RGB:
                    ('is rgb not properly implemented')
                    #new=new(:,:,:,[xpos, ypos, zpos]);
                new_data = new_data.transpose(xpos, ypos, zpos, 3)
            elif np.size(dims) == 3:
                new_data = new_data.transpose(xpos, ypos, zpos)

        if not os.path.isfile(affine_out) and write_transform:
            intermediate_affine_matrix = [x_row, y_row, z_row]
            iam = intermediate_affine_matrix
            affine_matrix_for_points = [
                iam[xpos, :], iam[ypos, :], iam[zpos, :]
            ]
            affine_matrix_for_images = np.inv(affine_matrix_for_points)
            am4i = affine_matrix_for_images
            affine_matrix_string = [
                am4i[1, :] + am4i[2, :] + am4i[3, :] + '0 0 0'
            ]
            affine_fixed_string = [
                '0',
                '0',
                '0',
            ]
            try:
                #write_affine_xform_for_ants(affine_out,affine_matrix_string,affine_fixed_string);
                #needs to be implemented if this is a desired functionality
                print("nope, not implemented")
            except:
                print("nope, not implemented")
    """
    if test is not None:
        newaffine = np.array([[0.045,      0., 0., 0.42525001],[0., 0.045, 0., 0.25920002],
        [0., 0.,0.045, 0.25920002], [0., 0., 0., 1.]])
        new_nii = nib.Nifti1Image(new_data, newaffine, hdr)
        test = str(test)
        nib.save(new_nii, test)
    """

    origin = affine[0:3, 3]
    if desired_vorder != orig_current_vorder:
        trueorigin = origin * [x_row[0], y_row[1], z_row[2]]
        trueorigin[2] = trueorigin[2] * (-1)
    else:
        trueorigin = origin

    newaffine = np.zeros([4, 4])

    newaffine[0:3, 0:3] = affine[0:3, 0:3]
    newaffine[3, :] = [0, 0, 0, 1]
    newaffine[:3, 3] = trueorigin
    #newaffine[0,:]=x_row
    #newaffine[1,:]=y_row
    #newaffine[2,:]=z_row
    #newhdr.srow_x=[newaffine[0,0:3]]
    #newhdr.srow_y=[newaffine[1,0:3]]
    #newhdr.srow_z=[newaffine[2,0:3]]
    #newhdr.pixdim=hdr.pixdim

    new_nii = nib.Nifti1Image(new_data, newaffine, hdr)
    output_path = str(output_path)
    if verbose:
        print(f'Saving nifti file to {output_path}')
    nib.save(new_nii, output_path)
    if verbose:
        print(f'Saved')
    """