Skip to content

Commit

Permalink
DEV: find dups
Browse files Browse the repository at this point in the history
  • Loading branch information
jungheejung committed May 14, 2024
1 parent e3c3604 commit f4eb902
Show file tree
Hide file tree
Showing 4 changed files with 71 additions and 6 deletions.
3 changes: 1 addition & 2 deletions spacetop_prep/datalad/remove_dups_anatfmap.sh
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,11 @@ SUMMARYLOG_FILE="fmap_summary_log.txt"
if [ -f "$SUMMARYLOG_FILE" ]; then
rm "$SUMMARYLOG_FILE"
fi
# mapfile -t dup_files < <(find . -type f -name '*__dup-*')

dup_files=()
while IFS= read -r -d $'\n' file; do
dup_files+=("$file")
done < <(find . -type f -path "*/fmap/*__dup-*.json")
done < <(find . -path "*/fmap/*__dup-*.json")

# Loop through each file found.
for DUPJSON in "${dup_files[@]}"; do
Expand Down
33 changes: 33 additions & 0 deletions spacetop_prep/datalad/remove_dups_fmap_sub-0057.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
#!/bin/bash

# Function to handle renaming for a given direction
rename_fmapdup() {
local DIRECTION="$1"
local RUN_NUM_ORIG="$2"
local RUN_NUM_NEW="$3"

# Construct original and new JSON filenames based on direction and run numbers
local DUPJSON="./sub-0057/ses-01/fmap/sub-0057_ses-01_acq-mb8_dir-${DIRECTION}_run-${RUN_NUM_ORIG}_epi__dup-01.json"
local RENAMEJSON="./sub-0057/ses-01/fmap/sub-0057_ses-01_acq-mb8_dir-${DIRECTION}_run-${RUN_NUM_NEW}_epi.json"

# Rename the JSON file
datalad run ../../../code/rename_file "${DUPJSON}" "${RENAMEJSON}"

# Construct the corresponding .nii.gz filenames
local ORIGINAL_NII_GZ="${DUPJSON%.json}.nii.gz"
local NEW_NII_GZ="${RENAMEJSON%.json}.nii.gz"

# Check if the original .nii.gz file exists before attempting to rename
if [ -f "$ORIGINAL_NII_GZ" ]; then
# Rename the .nii.gz file
git mv "$ORIGINAL_NII_GZ" "$NEW_NII_GZ"
echo "Renamed $ORIGINAL_NII_GZ to $NEW_NII_GZ"
else
echo "File $ORIGINAL_NII_GZ does not exist, skipping..."
fi
}

# Run the renaming for both directions
# Adjust RUN_NUM_ORIG and RUN_NUM_NEW as needed for each case
rename_fmapdup "ap" "02" "03"
rename_fmapdup "pa" "02" "03" # Update RUN_NUM_ORIG and RUN_NUM_NEW as per your requirements for the pa direction
6 changes: 2 additions & 4 deletions spacetop_prep/datalad/remove_dups_func.sh
Original file line number Diff line number Diff line change
Expand Up @@ -34,13 +34,11 @@ done
# Define a file to log errors
error_log="error_log.txt"

# mapfile -t dup_files < <(find . -type f -name '*__dup-*')

dup_files=()
while IFS= read -r -d $'\n' file; do
dup_files+=("$file")
done < <(find . -type f -name '*bold__dup-*')
# done < <(find . -type f -path '*/func/*__dup-*')
done < <(find . -name '*bold__dup-*')
# done < <(find . -path '*/func/*__dup-*')
# Loop through each file found.
for DUPJSON in "${dup_files[@]}"; do
# Use jq to extract TR values.
Expand Down
35 changes: 35 additions & 0 deletions spacetop_prep/datalad/remove_fmap_sub-0122.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
#NEED TO REMOVE PRIMARY and rename DUP as PRIMARY
# PRIMARYJSON="sub-0122_ses-03_acq-mb8_dir-ap_run-01_epi.json"
DUPJSON="./sub-0122/ses-03/fmapsub-0122_ses-03_acq-mb8_dir-ap_run-01_epi__dup-01.json"
DUPJSON_TR=$(jq '.AcquisitionTime' "${DUPJSON}")
PRIMARYJSON=$(echo "${DUPJSON}" | sed 's/__dup-[0-9]*//')
PRIMARYJSON_TR=$(jq '.AcquisitionTime' "${PRIMARYJSON}")
if [[ "$PRIMARYJSON_TR" == "null" || "$PRIMARYJSON_TR" =~ "error" ]]; then
echo "$PRIMARYJSON: Error extracting TR value." >> "$error_log"
continue
fi
PRIMARYJSON_NODEC=${PRIMARYJSON_TR%%.*}
DUPJSON_NODEC=${DUPJSON_TR%%.*}
PRIMARYJSON_SEC=$(echo $PRIMARYJSON_NODEC | tr -d '"'| awk -F: '{ print ($1 * 3600) + ($2 * 60) + $3 }')
DUPJSON_SEC=$(echo $DUPJSON_NODEC | tr -d '"' | awk -F: '{ print ($1 * 3600) + ($2 * 60) + $3 }')

if [ "$PRIMARYJSON_SEC" -lt "$DUPJSON_SEC" ]; then
echo -e "\n${DUPJSON}\nPRIMARYJSON acquisition time is earlier." >> $LOG_FILE
# echo -e "\n>>>>> ERROR: PRIMARYJSON ${PRIMARYJSON_NODEC} ${PRIMARYJSON_SEC} has an earlier time than DUPJSON ${DUPJSON_NODEC} ${DUPJSON_SEC}\n\n" >> $LOG_FILE

# SWAP DUP AND PRIMARY, THEN DELETED
PRIMARYGZ=${PRIMARYJSON}
DUPGZ=${DUPJSON} #TODO remove file extension
# RENAME JSON primary is crap, keep dup
$(dirname "$0")/rename_file "${PRIMARYJSON}" CRAPJSON;
$(dirname "$0")/rename_file "${DUPJSON}" "${PRIMARYJSON}";
$(dirname "$0")/rename_file CRAPJSON "${DUPJSON}"
# # RENAME fmap
$(dirname "$0")/rename_file "${PRIMARYGZ}" CRAPNII;
$(dirname "$0")/rename_file "${DUPGZ}" "${PRIMARYGZ}";
$(dirname "$0")/rename_file CRAPNII "${DUPGZ}"
# DELETE files
git rm "${DUPGZ}"
git rm "${DUPJSON}"

fi

0 comments on commit f4eb902

Please sign in to comment.