以下用 bash 编写的函数结合 AWK 代码对多列数据执行数学运算,最终将结果保存在所有处理过的 CSV 的输出文件中。
home="$PWD"
# folder with the outputs
rescore="${home}"/rescore
# folder with the folders to analyse
storage="${home}"/results_bench
cd "${storage}"
# pattern of the csv file located inside each of sub-directory of "${storage}"
str='*str1.csv'
rescore_data3 () {
str_name=$(basename "${str}" .csv)
mkdir -p "${rescore}"/"${str_name}"
# loop all directories contained target csv file
while read -r d; do
awk -F', *' -v OFS=', ' '
FNR==1 {
if (suffix) # suppress the empty line
printf "%s %.3f (%d)\n", suffix, dGmin, dGminid
# report the results for dGmin
dGmin = "" # initialize the min value
path=FILENAME
sub(/\/[^/]+$/,"",path)
prefix=suffix=FILENAME
sub(/_.*/, "", prefix)
sub(/\/[^\/]+$/, "", suffix); sub(/^.*_/, "", suffix)
if (FNR==NR)
print "lig(CNE)" " " "dG(" prefix ")" " " "ClusterID" # print the header line
next
}
{
dG = sqrt((($3+10)/10)^2+(($2-100)/100)^2)
if (dGmin == "" || dG < dGmin) {
dGmin = dG # update the min dG value
dGminid = $1 # update the ID with the min dG
}
}
END {
printf "%s %.3f (%d)\n", suffix, dGmin, dGminid # report results for dGmin
}
' "${d}_"*/${str} > "${rescore}/"${str_name}"/"${d%%_*}".csv"
done < <(find . -maxdepth 1 -type d -name '*_*_*' | awk -F '[_/]' '!seen[$2]++ {print $2}')
}
基本上每个处理的 CSV 都包含 3 列:
#input_str1.csv located in the folder 10V1_cne_lig12
ID, POP, dG
1, 142, -5.6500 # the line with ID=1, has lowest value in dG
2, 10, -5.5000
3, 2, -4.9500
4, 150, -4.1200
对 5 个 CSV 文件应用 rescore_data3() 会产生以下输出(每行包含有关单个 csv 的信息):
# 10V1.csv
lig dG(10V1) ID
lig12 0.947 (1)
lig40 0.595 (1)
lig199 1.060 (1)
lig211 0.756 (2)
lig278 0.818 (1)
我需要修改 AWK 代码数学方程中的常数(10 和 100),以便将它们替换为针对所有已处理的 csv 文件计算的灵活变量:应将 10 替换为 dG 的最低值(每个 input.csv 的第 3 列),将 100 替换为 POP 的最高值(每个 input.csv 的第 2 列)。最终,AWK 脚本中修改后的数学方程仍应包含 $2 和 $3 变量(针对特定 csv 获取的信息)以及 ${the_lowest_dG} 和 ${the_highest_POP}(在开始时仅对所有 CSV 计算一次):
dG = sqrt((($3-{the_lowest_dG})/{the_lowest_dG})^2+(($2-{the_highest_POP})/{the_highest_POP})^2)
編輯: 以下是基于 AWK 代码集成到我的函数中的可能解决方案,由 glenn jackman 提出。为了计算 lowest_dG 和 highest_POP对于所有输入 CSV ,我在 AWK 函数之前使用了这个 awk 代码(它也已更新为接受这两个变量并在数学方程中进一步使用它):
rescore_data4 () {
# name of the target CSV file to be rescored
str_name=$(basename "${str}" .csv)
#make dir for output
mkdir -p "${rescore}"/"${str_name}"
**# 1 - calculate max POP and dGmin for ALL rescored CSVs at once**
read highestPOP lowestDG < <(
awk -F ', ' '
FNR == 1 {next}
NR == 2 || $2 > pop {pop = $2}
NR == 2 || $3 < dg {dg = $3}
END {print pop, dg}
' "${storage}"/*_*_*/${str} ## < applied on all *.csv files in each of the subdirectory matching *_*_* pattern
)
printf >&2 'DEBUG INFO: this is topPOP= %d and dGmin= %.1f computed for %s... ' "${highestPOP}" "${lowestDG}" "${str_name}"; sleep 0.1
#
# 2- Apply the following AWK code for rescoring and final data collecting
while read -r d; do
# run rescoring routine using the min/max values
awk -F', *' -v OFS=', ' -v highest_POP="$highest_POP" -v lowest_dG="${lowestDG}" '
FNR==1 {
if (suffix) # suppress the empty line
#print suffix " " dGmin " (" dGminid ")"
printf "%s %.3f (%d)\n", suffix, dGmin, dGminid
#printf "%s %.3f (%d) %.3f (%d)\n", suffix, dGmin, dGminid, dGmax, dGmaxid
# report the results
dGmin = "" # initialize the min value
path=FILENAME
sub(/\/[^/]+$/,"",path)
prefix=suffix=FILENAME
sub(/_.*/, "", prefix)
sub(/\/[^\/]+$/, "", suffix); sub(/^.*_/, "", suffix)
if (FNR==NR)
print "lig(CNE)" " " "dG(" prefix ")" " " "ClusterID" # print the header line
#print "lig(CNE)" " " "dGmin(" prefix ")" " " "ID(dGmin)" " " "dGmax(" prefix ")" " " "ID(dGmax)" # print the header line
next
}
{
dG = sqrt((($3-lowest_dG)/lowest_dG)^2+(($2-240)/240)^2)
if (dGmin == "" || dG < dGmin) {
dGmin = dG # update the min dG value
dGminid = $1 # update the ID with the min dG
}
}
END {
#print suffix " " dGmin " (" dGminid ")" # report the results
printf "%s %.3f (%d)\n", suffix, dGmin, dGminid
#printf "%s %.3f (%d) %.3f (%d)\n", suffix, dGmin, dGminid, dGmax, dGmaxid
}
' "${d}_"*/${str} > "${rescore}/"${str_name}"/"${d%%_*}".csv"
done < <(find . -maxdepth 1 -type d -name '*_*_*' | awk -F '[_/]' '!seen[$2]++ {print $2}')
}
虽然这通常很有效,但新引入的 awk 部分存在一个错误:有时在输入大量 CSV 的情况下无法计算最低 DG 的值包含超过 10 行。
答案1
在 awk 中,将 视为$
用于获取给定字段编号的值的运算符。Awk 变量与 C 变量一样,不需要使用 取消引用$
。
awk -F', *'\
-v OFS=', ' \
-v the_highest_POP="the_highest_POP" \
-v the_lowest_dG="$the_lowest_dG" \
'
# ...
dG = sqrt((($3 - the_lowest_dG) / the_lowest_dG)^2 + (($2 - the_highest_POP) / the_highest_POP)^2)
我是否应该使用 bash(使用 while 一次处理所有 CSV)计算 ${the_highest_POP} 和 ${the_lowest_dG} ,将它们存储在外部变量中并提供给 awk,[...] 或者是否可以直接在 AWK 代码中执行所有步骤
这完全取决于some_method_to_cumpute_highest_POP_for_all_csvs_in_d
啊,我明白了。我没有仔细阅读问题。您将必须处理文件两次,一次用于查找最小/最大值,另一次用于进行 dG 计算。为了便于阅读,我将使用 2 个单独的 find/awk 调用来执行此操作:
# get the files you want to operate on.
# I'm assuming there are not hundreds of them.
mapfile -t cvsFiles < <(
find . -maxdepth 1 -type d -name '*_*_*' |
awk -F '[_/]' '!seen[$2]++ {print $2}')
)
# get the min/max values
read highestPOP lowestDG < <(
awk -F ', ' '
FNR == 1 {next}
NR == 2 || $2 > pop {pop = $2}
NR == 2 || $3 < dg {dg = $3}
END {print pop, dg}
' "${csvFiles[@]}"
)
# do the calculations over all the files
awk -F', *'\
-v OFS=', ' \
-v the_highest_POP="$highestPOP" \
-v the_lowest_dG="$lowestDG" \
'...' "${files[@]}"