Skip to content
27 changes: 11 additions & 16 deletions src/aslprep-pre.sh
Original file line number Diff line number Diff line change
Expand Up @@ -8,25 +8,20 @@ export m0scan=NO_M0SCAN
export aslscan=NO_ASLSCAN
export aslsource=NO_ASLSOURCE
export examcard=NO_EXAMCARD
export t1scan=NO_T1SCAN

# Parse options
while [[ $# -gt 0 ]]; do
key="${1}"
case $key in
--indir)
export indir="${2}"; shift; shift ;;
--outdir)
export bidsdir="${2}"; shift; shift ;;
--m0scan)
export m0scan="${2}"; shift; shift ;;
--aslscan)
export aslscan="${2}"; shift; shift ;;
--sourcescan)
export sourcescan="${2}"; shift; shift ;;
--examcard)
export examcard="${2}"; shift; shift ;;
--fs_license)
export fs_license="${2}"; shift; shift ;;
--indir) export indir="${2}"; shift; shift ;;
--outdir) export bidsdir="${2}"; shift; shift ;;
--m0scan) export m0scan="${2}"; shift; shift ;;
--aslscan) export aslscan="${2}"; shift; shift ;;
--sourcescan) export sourcescan="${2}"; shift; shift ;;
--examcard) export examcard="${2}"; shift; shift ;;
--fs_license) export fs_license="${2}"; shift; shift ;;
--t1scan) export t1scan="${2}"; shift; shift ;;
*)
echo Unknown input "${1}"; shift ;;
esac
Expand All @@ -35,10 +30,10 @@ done

# Format BIDS directory and convert to nii
# Save Series Description to json
organize_data.py -i ${indir} -a ${aslscan} -m ${m0scan} -s ${sourcescan}
organize_data.py -i ${indir} -a ${aslscan} -m ${m0scan} -s ${sourcescan} -t ${t1scan}

#Get necessary data form examcard and write to json sidecar
examcard2json.py -i ${indir} -b ${bidsdir} -e ${examcard}

#Create ASL context tsv file
create_context_tsv.py -b ${bidsdir}
create_tsv.py -b ${bidsdir}
21 changes: 21 additions & 0 deletions src/bash_commands.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
#!/bin/bash

export indir=$1
export source=$2
export m0=$3
export t1w=$4
export source_base=$5
export m0_base=$6
export t1w_base=$7

# move scans to BIDS directories
mkdir -p $indir/BIDS/sub-01/ses-01/anat/
mkdir -p $indir/BIDS/sub-01/ses-01/perf/

cp $source $indir/BIDS/sub-01/ses-01/perf/$source_base
cp $m0 $indir/BIDS/sub-01/ses-01/perf/m0$m0_base
cp $t1w $indir/BIDS/sub-01/ses-01/anat/$t1w_base

# run dcm2niix on source and m0 scans
/data/mcr/centos7/dcm2niix/v1.0.20240202/console/dcm2niix -z y -f %b $indir/BIDS/sub-01/ses-01/anat
/data/mcr/centos7/dcm2niix/v1.0.20240202/console/dcm2niix -z y -f %b $indir/BIDS/sub-01/ses-01/perf
63 changes: 0 additions & 63 deletions src/create_context_tsv.py

This file was deleted.

65 changes: 65 additions & 0 deletions src/create_tsv.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
#!/usr/bin/env python

'''
Inputs:
-b: BIDS file structure containing nifti files and json sidecars
Outputs:
updated json sidecar for ASL images in Exam Card
Name: sub-01_ses-01_asl.json OR sub-01_ses-01_m0scan.json
'''

from __future__ import print_function
import os
import csv
import sys, getopt
import glob
import nibabel as nib

def main(argv):
bids = ''
try:
opts, args = getopt.getopt(argv, "hb:",["bids="])
except getopt.GetoptError:
print('create_tsv.py -b <folder>')
sys.exit(2)

for opt, arg in opts:
if opt == '-h':
print('create_tsv.py -b <folder>')
sys.exit()
elif opt in ("-b", "--bids"):
bids = arg

print('BIDS Folder: ', bids)

asl_file = glob.glob(bids+'/sub-*/ses-*/perf/*_asl.nii.gz')

print(asl_file)
print(asl_file[0])

if asl_file:
asl_img = nib.load(asl_file[0])
if len(asl_img.shape) < 4:
print('ASL data not 4-dimensional. Please repeat with correct file.')
sys.exit()
else:
abs_path = os.path.abspath(asl_file[0])
file_struct = abs_path.split('/')
tsv_loc = '/'.join(file_struct[:-1]) + ''
name_struct = file_struct[-1].split('_')
tsv_name = '_'.join(name_struct[:-1]) + '_aslcontext.tsv'

with open(tsv_loc + '/' + tsv_name,'wt') as tsv_file:
csv_writer=csv.writer(tsv_file,delimiter='\t')
csv_writer.writerow(['volume_type'])
for x in range(asl_img.shape[3]):
if (x % 2) == 0:
csv_writer.writerow(['control'])
else:
csv_writer.writerow(['label'])
else:
print('Files not found or data is not in BIDS format. Please repeat with correct file/structure.')
sys.exit()

if __name__ == '__main__':
main(sys.argv[1:])
Loading