# BIDS-ing sub-01, ses-02; collected 09/16/2017 # written by Jo Etzel (jetzel@wustl.edu), 9 October 2018 #################################################################################################################################################### rm(list=ls()); # clear R's memory in.path <- "d:/temp/DMCC_box/Raw_Data/999008/999008_test2017/caseforge2_16Sept2017/scans/"; # path to source dicoms ds.path <- "d:/temp/DMCC_box/Raw_Data/999008/999008_test2017/multibandCFtests/sub-01/ses-02/"; # new BIDS dataset dcm.path <- "c:/Program Files/mricrogl/dcm2niix"; # dcm2niix.exe location do.rename <- function(idb.num, new.fname) { # idb.num <- 11; new.fname <- "sub-01_ses-03_T1w" file.rename(from=list.files(out.path, pattern=paste0("_", idb.num, ".json"), full.names=TRUE), to=paste0(out.path, new.fname, ".json")); file.rename(from=list.files(out.path, pattern=paste0("_", idb.num, ".nii.gz"), full.names=TRUE), to=paste0(out.path, new.fname, ".nii.gz")); } #################################################################################################################################################### #################################################################################################################################################### # anat # no anatomy collected during caseforge testing sessions; here's an anatomy of the same person, same scanner and headcoil, collected 17 July 2018 ds.path <- "d:/temp/DMCC_box/Raw_Data/999008/999008_test2017/multibandCFtests/sub-01/ses-03/"; # new BIDS dataset out.path <- paste0(ds.path, "anat/"); if (!dir.exists(out.path)) { dir.create(out.path); } system2(dcm.path, args=paste0("-b y -z y -v y -o ", out.path, ' -f "%t_%p_%s" "', in.path, '12_T1w"'), stdout=TRUE); do.rename(12, "sub-01_ses-03_T1w"); system2(dcm.path, args=paste0("-b y -z y -v y -o ", out.path, ' -f "%t_%p_%s" "', in.path, '14_T2w"'), stdout=TRUE); do.rename(14, "sub-01_ses-03_T2w"); #################################################################################################################################################### #################################################################################################################################################### # func: bold and sbref out.path <- paste0(ds.path, "func/"); if (!dir.exists(out.path)) { dir.create(out.path); } system2(dcm.path, args=paste0("-b y -z y -v y -o ", out.path, ' -f "%t_%p_%s" "', in.path, '17_tfMRI_SBRef"'), stdout=TRUE); do.rename(17, "sub-01_ses-02_task-Stroop_acq-cf2AP_run-01_sbref"); system2(dcm.path, args=paste0("-b y -z y -v y -o ", out.path, ' -f "%t_%p_%s" "', in.path, '18_tfMRI"'), stdout=TRUE); do.rename(18, "sub-01_ses-02_task-Stroop_acq-cf2AP_run-01_bold"); system2(dcm.path, args=paste0("-b y -z y -v y -o ", out.path, ' -f "%t_%p_%s" "', in.path, '20_tfMRI_SBRef"'), stdout=TRUE); do.rename(20, "sub-01_ses-02_task-Stroop_acq-cf2PA_run-02_sbref"); system2(dcm.path, args=paste0("-b y -z y -v y -o ", out.path, ' -f "%t_%p_%s" "', in.path, '21_tfMRI"'), stdout=TRUE); do.rename(21, "sub-01_ses-02_task-Stroop_acq-cf2PA_run-02_bold"); system2(dcm.path, args=paste0("-b y -z y -v y -o ", out.path, ' -f "%t_%p_%s" "', in.path, '32_tfMRI_SBRef"'), stdout=TRUE); do.rename(32, "sub-01_ses-02_task-notStroop_acq-cf2AP_run-03_sbref"); system2(dcm.path, args=paste0("-b y -z y -v y -o ", out.path, ' -f "%t_%p_%s" "', in.path, '33_tfMRI"'), stdout=TRUE); do.rename(33, "sub-01_ses-02_task-notStroop_acq-cf2AP_run-03_bold"); system2(dcm.path, args=paste0("-b y -z y -v y -o ", out.path, ' -f "%t_%p_%s" "', in.path, '35_tfMRI_SBRef"'), stdout=TRUE); do.rename(35, "sub-01_ses-02_task-notStroop_acq-cf2PA_run-04_sbref"); system2(dcm.path, args=paste0("-b y -z y -v y -o ", out.path, ' -f "%t_%p_%s" "', in.path, '36_tfMRI"'), stdout=TRUE); do.rename(36, "sub-01_ses-02_task-notStroop_acq-cf2PA_run-04_bold"); system2(dcm.path, args=paste0("-b y -z y -v y -o ", out.path, ' -f "%t_%p_%s" "', in.path, '48_tfMRI_SBRef"'), stdout=TRUE); do.rename(48, "sub-01_ses-02_task-notStroop_acq-cf0AP_run-05_sbref"); system2(dcm.path, args=paste0("-b y -z y -v y -o ", out.path, ' -f "%t_%p_%s" "', in.path, '49_tfMRI"'), stdout=TRUE); do.rename(49, "sub-01_ses-02_task-notStroop_acq-cf0AP_run-05_bold"); system2(dcm.path, args=paste0("-b y -z y -v y -o ", out.path, ' -f "%t_%p_%s" "', in.path, '51_tfMRI_SBRef"'), stdout=TRUE); do.rename(51, "sub-01_ses-02_task-Stroop_acq-cf0PA_run-06_sbref"); system2(dcm.path, args=paste0("-b y -z y -v y -o ", out.path, ' -f "%t_%p_%s" "', in.path, '52_tfMRI"'), stdout=TRUE); do.rename(52, "sub-01_ses-02_task-Stroop_acq-cf0PA_run-06_bold"); # add the TaskName field to the .json #run.ids <- c("task-Stroop_acq-cf2AP_run-01", "task-Stroop_acq-cf2PA_run-02", "task-Stroop_acq-cf0PA_run-06"); run.ids <- c("task-notStroop_acq-cf2AP_run-03", "task-notStroop_acq-cf2PA_run-04", "task-notStroop_acq-cf0AP_run-05"); for (rid in 1:length(run.ids)) { # rid <- 1; fname.in <- paste0(out.path, "sub-01_ses-02_", run.ids[rid], "_bold.json"); if (file.exists(fname.in)) { #file.copy(from=fname.in, to=paste0(fname.in, "2.txt"), overwrite=FALSE); tmp <- readLines(fname.in); if (length(grep("TaskName", tmp)) == 0) { # field not in, so add it. if (tmp[length(tmp)] != "}") { stop ('tmp[length(tmp)] != "}"'); } move.to.last <- length(tmp) - 1; # which entry will be copied to the (current) last spot, and replaced by the new field tmp <- c(tmp, "}"); # add a new element tmp[move.to.last+1] <- tmp[move.to.last]; #tmp[move.to.last] <- '\t"TaskName": "Stroop",'; tmp[move.to.last] <- '\t"TaskName": "notStroop",'; cat(tmp, file=fname.in, sep="\n"); # OVERWRITE original file } } } #################################################################################################################################################### #################################################################################################################################################### # fmap out.path <- paste0(ds.path, "fmap/"); if (!dir.exists(out.path)) { dir.create(out.path); } system2(dcm.path, args=paste0("-b y -z y -v y -o ", out.path, ' -f "%t_%p_%s" "', in.path, '9_FieldMap_SE_EPI"'), stdout=TRUE); do.rename(9, "sub-01_ses-02_acq-cf2AP_run-01_epi"); system2(dcm.path, args=paste0("-b y -z y -v y -o ", out.path, ' -f "%t_%p_%s" "', in.path, '10_FieldMap_SE_EPI"'), stdout=TRUE); do.rename(10, "sub-01_ses-02_acq-cf2PA_run-01_epi"); system2(dcm.path, args=paste0("-b y -z y -v y -o ", out.path, ' -f "%t_%p_%s" "', in.path, '46_FieldMap_SE_EPI"'), stdout=TRUE); do.rename(46, "sub-01_ses-02_acq-cf0AP_run-05_epi"); system2(dcm.path, args=paste0("-b y -z y -v y -o ", out.path, ' -f "%t_%p_%s" "', in.path, '47_FieldMap_SE_EPI"'), stdout=TRUE); do.rename(47, "sub-01_ses-02_acq-cf0PA_run-05_epi"); # add the IntendedFor field to the .jsons: fieldmap AP goes with task run PA (and the reverse) top.path <- "d:/temp/DMCC_box/Raw_Data/999008/999008_test2017/multibandCFtests/sub-01/"; run.ids <- c("sub-01_ses-02_acq-cf2AP_run-01", "sub-01_ses-02_acq-cf2PA_run-01", "sub-01_ses-02_acq-cf0AP_run-05", "sub-01_ses-02_acq-cf0PA_run-05"); # fieldmap names for.ids <- c("task-Stroop_acq-cf2PA_run-02", "task-Stroop_acq-cf2AP_run-01", "task-Stroop_acq-cf0PA_run-06", "task-notStroop_acq-cf0AP_run-05"); # corresponding task fmri run names for (rid in 1:length(run.ids)) { # rid <- 1; fname.in <- paste0(out.path, run.ids[rid], "_epi.json"); fname.bold <- paste0("ses-02/func/sub-01_ses-02_", for.ids[rid], "_bold.nii.gz"); if (file.exists(fname.in) & file.exists(paste0(top.path, fname.bold))) { file.copy(from=fname.in, to=paste0(fname.in, "2.txt"), overwrite=FALSE); tmp <- readLines(fname.in); if (length(grep("IntendedFor", tmp)) == 0) { # field not in, so add it. if (tmp[length(tmp)] != "}") { stop ('tmp[length(tmp)] != "}"'); } move.to.last <- length(tmp) - 1; # which entry will be copied to the (current) last spot, and replaced by the new field tmp <- c(tmp, "}"); # add a new element tmp[move.to.last+1] <- tmp[move.to.last]; tmp[move.to.last] <- paste0('\t"IntendedFor": "', fname.bold, '",'); cat(tmp, file=fname.in, sep="\n"); # OVERWRITE original file } } } #################################################################################################################################################### #################################################################################################################################################### # func: events files. event times in seconds from run start. in.path <- "d:/temp/DMCC_box/Raw_Data/999008/999008_test2017/caseforge2_16Sept2017/"; out.path <- paste0(ds.path, "func/"); # where to write the tsv # input and output filenames, same order in.fnames <- c("999008_baseline_StroopBas_run1.txt_raw.csv", "999008_baseline_StroopBas_run2.txt_raw.csv", "999008_breath_baseline_StroopBas_run1.txt_raw.csv", "999008_breath_baseline_StroopBas_run2.txt_raw.csv", "999008_nocf_baseline_StroopBas_run1.txt_raw.csv", "999008_nocf_baseline_StroopBas_run2.txt_raw.csv"); out.fnames <- c("sub-01_ses-02_task-Stroop_acq-cf2AP_run-01_events.tsv", "sub-01_ses-02_task-Stroop_acq-cf2PA_run-02_events.tsv", "sub-01_ses-02_task-notStroop_acq-cf2AP_run-03_events.tsv", "sub-01_ses-02_task-notStroop_acq-cf2PA_run-04_events.tsv", "sub-01_ses-02_task-notStroop_acq-cf0AP_run-05_events.tsv", "sub-01_ses-02_task-Stroop_acq-cf0PA_run-06_events.tsv"); for (i in 1:length(in.fnames)) { # i <- 1; e.tbl <- read.csv(paste0(in.path, in.fnames[i]), stringsAsFactors=FALSE); onset <- (e.tbl$Flicker.OnsetTime - e.tbl$scanstart.RTTime[1])/1000; # event onsets in seconds duration <- rep(1, length(onset)); # short events trial_type <- e.tbl$TrialType; # congruent or incongruent out.tbl <- data.frame(onset, duration, trial_type); out.tbl <- out.tbl[which(!is.na(onset)),]; # NA onsets at block boundaries; all events are !is.na write.table(out.tbl, paste0(out.path, out.fnames[i]), sep='\t', row.names=FALSE, quote=FALSE); # write as tsv } #################################################################################################################################################### #################################################################################################################################################### # func: physio in.path <- "d:/temp/DMCC_box/Raw_Data/999008/999008_test2017/caseforge2_16Sept2017/physio/"; out.path <- paste0(ds.path, "func/"); # where to write the tsv # input and output filenames, same order in.fnames <- c("999008_scan52_physio.csv", "999008_scan18_physio.csv", "999008_scan21_physio.csv", "999008_scan33_physio.csv", "999008_scan36_physio.csv", "999008_scan49_physio.csv"); out.fnames <- c("sub-01_ses-02_task-Stroop_acq-cf2AP_run-01", "sub-01_ses-02_task-Stroop_acq-cf2PA_run-02", "sub-01_ses-02_task-notStroop_acq-cf2AP_run-03", "sub-01_ses-02_task-notStroop_acq-cf2PA_run-04", "sub-01_ses-02_task-notStroop_acq-cf0AP_run-05", "sub-01_ses-02_task-Stroop_acq-cf0PA_run-06"); for (i in 1:length(in.fnames)) { # i <- 1; in.tbl <- read.csv(paste0(in.path, in.fnames[i]), stringsAsFactors=FALSE); # save and rename columns for the output tsv out.tbl <- cbind(in.tbl$physio.PULS, in.tbl$physio.RESP, in.tbl$physio.ACQ); write.table(out.tbl, gzfile(paste0(out.path, out.fnames[i], "_physio.tsv.gz")), sep='\t', row.names=FALSE, col.names=FALSE, quote=FALSE); # write as tsv # make the .json fout <- file(paste0(out.path, out.fnames[i], "_physio.json"), 'wt'); cat("{\n", file=fout); cat('"SamplingFrequency": 400.0,', file=fout, sep="\n"); # all sampled at 2.5 msec/tic : 1000/2.5 = 400 HZ (samples/sec) start.tic <- min(which(in.tbl$physio.ACQ == 1)); # first acquisition time, in tics start.time <- start.tic * 2.5 / 1000; # first acquisition time, in seconds cat(paste0('"StartTime": -', start.time, ','), file=fout, sep="\n"); # physio started -start.time before fMRI acquisitions cat('"Columns": ["cardiac", "respiratory", "trigger"]', file=fout, sep="\n"); cat("}\n", file=fout); close(fout); unlink(fout); } #################################################################################################################################################### ####################################################################################################################################################