-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathfix_nbc.sh
executable file
·95 lines (85 loc) · 4.57 KB
/
fix_nbc.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
#!/bin/bash
testing=true
#This script will fix the filenames of any image files generated by the stainer_ui that had a failed barcode reading
#The desired filenames look like [projectName]_[dateAndTimeOfImageCapture]_[indexInSeriesOfAutomaticCaptures]_[sectionID]_section.png, for example, vnc1_1513740272_c36_2243_section.png
#Bad file names will look like vnc1_1513740272_c36_nbc_section.png
#Step 1: Check if projectNames are properly set -- this is important because strings are split by the _ character, and so if some files have projectName 'vnc1' and others have projectName 'no_project_name_set', there will be different numbers of _ characters in different file names, and this will cause problems. This could be coded around, but I provide a simple rename command to fix this
if grep -q no_project_name_set <<< `ls $1`
then
echo Go fix your project names first with a rename command along the lines of:
echo rename \'s/no_project_name_set/yourSamplesName/\' yourSampleFolder/*
exit
fi
#Step 2: Get a list of section.png files, and determine how many _ characters are in the file name of the first image. The script assumes that all filenames will have that many _ characters in them. This should probably be explicitly checked, or changed to be more flexible
sectionList=`ls $1/*section.png`
sectionArr=($sectionList)
#Find which column has the section number
onlyDividers=$(tr -dc '_' <<< ${sectionArr[0]})
columnWithSectNum=${#onlyDividers}
#Code version 2: Slightly less generalizable, definitely easier to see that it's doing the right thing for the vnc1 images
for i in ${!sectionArr[@]}; do
sectNum=$(cut -d'_' -f $columnWithSectNum <<< ${sectionArr[$i]})
echo $i $(($sectNum+$i)) $(($sectNum-$i)) ${sectionArr[$i]}
if [[ $(($sectNum+$i)) -ne 4443 ]]; then
read -rsn1 -p "Press y to change this sectNum to $((4443-$i))" userInput
echo ""
if [[ $userInput != 'y' ]]; then continue; fi
echo Changing
perlSubstitutionExpression=s/${sectNum}_section.png/$((4443-$i))_section.png/
if $testing; then
echo cp ${sectionArr[$i]} ${sectionArr[$i]}.old
echo rename $perlSubstitutionExpression ${sectionArr[$i]}
else
cp ${sectionArr[$i]} ${sectionArr[$i]}.old
rename $perlSubstitutionExpression ${sectionArr[$i]}
fi
fi
done
if [[ 1 -eq 0 ]]; then
#Step 4: Find filenames with 'nbc' in them, and rename them
difference=''
for i in ${!sectionArr[@]}; do
echo $i ${sectionArr[$i]}
if [[ $i -lt 2 ]]; then continue; fi
sectionNumPreviousImage=$(cut -d'_' -f $columnWithSectNum <<< ${sectionArr[$(($i-1))]})
sectionNumTwoImagesBack=$(cut -d'_' -f $columnWithSectNum <<< ${sectionArr[$(($i-2))]})
currentDifference=$(($sectionNumPreviousImage-$sectionNumTwoImagesBack))
if [[ $currentDifference -ne 1 ]] && [[ $currentDifference -ne -1 ]]; then
echo Non-sequential images found at ${sectionArr[$(($i-1))]}, skipping;
continue
fi
if [[ $(cut -d'_' -f $columnWithSectNum <<< ${sectionArr[$i]}) = nbc ]]; then
if [[ -z ${difference:+x} ]]; then
difference=$currentDifference
for fn in ${sectionArr[@]:$(($i-2)):5}; do echo $fn; done
#ls $1/*section.png | head
if [[ $difference -eq 1 ]]; then
read -rsn1 -p "The section IDs seem to be counting up. Press y to confirm or any other key to quit" pressedKey
echo
if [[ $pressedKey != 'y' ]]; then break; fi
fi
if [[ $difference -eq -1 ]]; then
read -rsn1 -p "The section IDs seem to be counting down. Press y to confirm or any other key to quit" pressedKey
echo
if [[ $pressedKey != 'y' ]]; then break; fi
fi
fi
if [[ $currentDifference -ne $difference ]]; then
echo Something seems wrong around files ${sectionArr[$(($i-1))]}. Did the section indexing switch directions for some reason? Please go fix it by hand. Aborting.
break
fi
echo $sectionNumTwoImagesBack $sectionNumPreviousImage $currentDifference $difference
perlSubstitutionExpression=s/nbc_section.png/$(($sectionNumPreviousImage+$difference))_section.png/
if $testing; then
echo cp ${sectionArr[$i]} ${sectionArr[$i]}.old
echo rename $perlSubstitutionExpression ${sectionArr[$i]}
else
cp ${sectionArr[$i]} ${sectionArr[$i]}.old
rename $perlSubstitutionExpression ${sectionArr[$i]}
fi
#Refresh the sectionList and sectionArr, so that the just-renamed file can be used to rename the next file
sectionList=`ls $1/*section.png`
sectionArr=($sectionList)
fi
done
fi