Useful Bash Commands - neuropsytox/Documentation GitHub Wiki

Useful commands for myself (Eduardo)

Mass Rename

rename

#To add prefix (with example):

rename -v 's/0/' *

#from '02_t1.nii.gz' to '002_t1.nii.gz'

Remove extension in loop

Use %.*, works like a charm.

for f in .mnc; do mnc2nii ${f} ${f%.}.nii; done

$ for f in .nii; do nii2mnc ${f} ${f%.}.mnc; echo ${f%.*}.mnc; done

For removing any charater from the rear just use % then a wildcard. for example:

for f in ?????_sc+orig.; do 3dvolreg -prefix ${f%????????.}_scmc #Removes also the extension.

For removing any caracter from the beginning, use # instead:

AD_022.txt to 022.txt

mv $f ${f#???}

For removing folder path and also change the name:

basename $file

for file in preproc/*n4correct.beastmask.mnc; do echo cp ${file} ct2/input/addimex_basename ${file%%???????.*}_mask.mnc; done

cp preproc/HC_095_t1_iso1mm.anlm_denoise.n4correct.beastmask.mnc ct2/input/addimex_HC_095_t1_mask.mnc

Truco para usar una lista TXT de archivos para copiar

cat templates.txt| while read i; do cp -v preproc_data/${i%%???????.}.clean_and_center.n4correct.cutneckapplyautocrop.mnc subcort/hippocampus-amygdala/input/templates/brains/${i%%???????.}.clean_and_center.n4correct.cutneckapplyautocrop.mnc; done

i = cada linea.

Add "0" in R for subject names

formatC(NAME, width = 2, format = "d", flag = "0")

For example: 0001, 0035

db$id <- formatC(db$id, width = 4, format = "d", flag = "0")

Change resolution of ROI or brain in FSL from 2 mm to 3 mm

flirt -in brain_2mm.nii.gz -ref brain_3mm.nii.gz -out brain_3mm_new.nii.gz -nosearch -applyisoxfm 3

Resample ROIs

flirt -interp nearestneighbour -in shen_2mm_268_parcellation.nii.gz -ref MNI152_T1_3mm_brain.nii.gz -out shen_3mm_268_parcellation.nii.gz -nosearch -applyisoxfm 3

Get correlation matrix

3dNetCorr -prefix HC_153_cm -inset ppBoldv2_woGSR_MNI3mm_HC_153_fmri.nii.gz -mask ../mask.nii.gz -in_rois shen_2mm_268_parcellation.nii.gz -fish_z -ts_wb_corr

#Resample 4D file to MNI 2 mm applywarp --ref=${FSLDIR}/data/standard/MNI152_T1_2mm.nii.gz --in=stats/res4d_woGSR --warp=reg/highres2standard_warp.nii.gz --out=ppBold/ppBoldv2_woGSR_MNI2mm_HC_153_fmri.nii.gz --premat=reg/meanfunc2highres.mat --interp=trilinear

#Bandpass 0.01 0.08 using FSLmaths (sigma or volume)

fslmaths ppBoldv2_woGSR_MNI2mm_HC_153_fmri.nii.gz -bptf 21.276595 2.659574 ppBoldv2_woGSR_MNI2mm_bp_HC_153_fmri.nii.gz

Easy mask fMRI

fslmaths ppBoldv2_woGSR_MNI2mm_bp_HC_153_fmri_0.nii.gz -sqr -bin mask_test

See many images

eog /reg/check.jpg

En un texto, extraer la segunda columna de la segunda fila.

awk 'FNR == 2 {print $2}' HC_147_fd.txt

#Create Civet Image

./create_civet_image.sh lh_average.obj stats/macacc_leftseed_lh_nm.txt 1 10 rh_average.obj stats/macacc_leftseed_rh_nm.txt 1 10 heschl_lh_nm.png

Print out column as row in a txt file.

cat file.txt | paste -s -d","

Create Seed sphere ROI

fslmaths MNI152_T1_2mm_brain.nii.gz -mul 0 -add 1 -roi 45 1 74 1 15 1 0 1 ACCpoint -odt float fslmaths ACCpoint -kernel sphere 5 -fmean ACCsphere -odt float

Use QC 2 in Maget

for file in majorityvote/; do i=$(basename $file);echo ./MAGeT-QC.sh ../../input/subject/${i%%.}.mnc ${file} QC-2/${i%.*}.jpg; done > qc2.txt

Force Maget Morph to 32 bit processing. In qbatch, qbatch_morph and qbatch_vote change this line:

qsub_options = ['-l nodes=1:m32g:ppn=%i,walltime=%s' % (options.processes, walltime)

File with full path list.

ls -1 $PWD//fdc > fdc_input_files.txt

#Boxplots many

lapply(morpho_dataset_1 [,c ("brain_vol","L_striatum","L_gp", "L_thalamus", "R_striatum","R_gp","R_thalamus", "l_nacc_vs","l_pre_put", "l_pre_cau", "l_pos_cau","l_pos_put","r_nacc_vs","r_pre_put", "r_pre_cau", "r_pos_cau", "r_pos_put","l_LGN", "l_MGN","l_AN","l_CN","l_LD","l_LP","l_MD","l_Pul","l_VAN","l_VLN","l_VPN","r_LGN","r_MGN","r_AN","r_CN","r_LD", "r_LP", "r_MD" ,"r_Pul", "r_VAN", "r_VLN", "r_VPN","R_CA1", "R_subiculum", "R_CA4DG", "R_CA2CA3", "R_stratum", "R_fimbria","L_mam_body","R_mam_body", "L_fornix", "R_fornix", "L_fimbria", "L_CA1" , "L_subiculum", "L_CA4DG","L_CA2CA3","L_stratum", "R_alveus", "L_alveus", "L_amygdala", "R_amygdala", "L_I_II", "L_III", "L_IV", "L_V", "L_VI","L_Crus_I", "L_Crus_II", "L_VIIB", "L_VIIIA", "L_VIIIB", "L_IX", "L_X", "L_CM", "R_I_II", "R_III","R_IV" , "R_V", "R_VI", "R_Crus_I", "R_Crus_II", "R_VIIB","R_VIIIA", "R_VIIIB", "R_IX", "R_X", "R_CM", "R_hipp", "L_hipp")], function(x) boxplot(x ~ morpho_dataset_1$group))

Remove file less than 17 Kb

find . -name "*.obj" -size -17K -delete

Download only subfolders with Rsync

rsync -avzh -m --include '??_t1_pp/' --include '??_t1_pp/stats-volumes/**' --exclude '' -e ssh --progress --ignore-existing [email protected]://home/m/mchakrav/jgermann/scratch/mbm_run_17052018/whateveryoulike_processed/ .

Find missing files

for i in _t1.mnc; do file=${i%???.}; file2=${file#?????}; if [ ! -f ../output/${file2}/verify/${file}_verify.png ]; then echo "${file} not found"; fi; done

Print only 1 column using cat, separated by a comma.

cat dicom_tms.csv | sed 's/,/ /' | awk '{print $1}'

Do many ANOVAs in R and summarize

anovathalamus <- paste(names(ct_data_sub[68:89]), " ~ group + brain_vol + age + sex + education", sep = "") anovathalamus2 <- lapply(anovathalamus, FUN = formula) doANCOVAandSummarize <- function(eachFormula) {summary(aov(eachFormula,data=ct_data_sub))} lapply(anovathalamus2, doANCOVAandSummarize)

Touch all Niagara

find $SCRATCH -exec touch {} ;

Sumar una columna en un texto en bash

Ejemplo:

$ paste -sd+ AD_002_t1_vorn_SA.txt |bc

#Loop

for subject in AD_002 AD_003 AD_006 AD_008 AD_009 AD_010 AD_011 AD_012 AD_013 AD_014 AD_015 AD_016 AD_017 AD_018 AD_019 AD_020 AD_023 AD_024 AD_025 AD_026 AD_027 AD_030 AD_034 AD_035 AD_040 AD_043 AD_045 AD_046 AD_047 AD_048 AD_052 AD_053 AD_056 AD_057 AD_058 AD_060 AD_061 AD_064 AD_066 AD_067 AD_069 AD_070 AD_071 AD_072 AD_073 AD_074 AD_076 AD_080 AD_081 AD_083 AD_091 AD_092 AD_103 HC_001 HC_004 HC_005 HC_007 HC_021 HC_022 HC_028 HC_029 HC_031 HC_032 HC_033 HC_036 HC_037 HC_038 HC_041 HC_042 HC_044 HC_049 HC_050 HC_051 HC_054 HC_055 HC_059 HC_062 HC_065 HC_068 HC_075 HC_077 HC_078 HC_082 HC_084 HC_085 HC_086 HC_087 HC_088 HC_093 HC_094 HC_095 HC_097 HC_098 HC_099 HC_100 HC_101 HC_102; do paste -sd+ ${subject}_t1_vorn_SA.txt |bc; done

MINC TIPS

Modify Labels, use mincmath.

#Binarize label

mincmath -const2 101 106 -segment hippo/AD_002_t1_labels_hippo.mnc AD_002_t1_labels_lh.mnc

#Give value to binarized label

mincmath -const 200 -mult AD_002_t1_labels_rh.mnc AD_002_t1_labels_rh200.mnc