根据 vcf table 数据更改字符串中的章程
change charters in a string based on vcf table data
我有一个长字符串文件(string.txt
) (abcdefghijklmnop
)
和一个看起来像
的 vcf table (file.vcf
)
position 2 4 6 10 n...
name1 a b c d
name2 x y z a
namen...
table 还包含 "mis"
和 "het"
并且在这种情况下不应替换字符
我想更改特定位置的字符并将所有字符串存储在一个如下所示的新文件中
>name1
aacbecghidklmnop
>name2
axcyezghiaklmnop
有没有办法在 bash 循环中做到这一点?
请您尝试以下操作:
mapfile -t string < <(fold -w1 "string.txt")
# set string to an array of single characters: ("a" "b" "c" "d" ..)
while read -ra ary; do
if [[ ${ary[0]} = "position" ]]; then
# 1st line of file.vcf
declare -a pos=("${ary[@]:1}")
# now the array pos holds: (2 4 6 10 ..)
else
# 2nd line of file.vcf and after
declare -a new=("${string[@]}")
# make a copy of string to modify
for ((i=0; i<${#pos[@]}; i++ )); do
repl="${ary[$i+1]}" # replacement
if [[ $repl != "mis" && $repl != "het" ]]; then
new[${pos[$i]}-1]="$repl"
# modify the position with the replacement
fi
done
echo ">${ary[0]}"
(IFS=""; echo "${new[*]}")
# print the modified array as a concatenated string
fi
done < "file.vcf"
string.txt:
abcdefghijklmnop
file.vcf:
position 2 4 6 10
name1 a b c d
name2 x y z a
name3 i mis k l
输出:
>name1
aacbecghidklmnop
>name2
axcyezghiaklmnop
>name3
aicdekghilklmnop
我尝试在上面的脚本中嵌入解释作为注释,但是
如果您还有问题,请随时提问。
希望这对您有所帮助。
我有一个长字符串文件(string.txt
) (abcdefghijklmnop
)
和一个看起来像
file.vcf
)
position 2 4 6 10 n...
name1 a b c d
name2 x y z a
namen...
table 还包含 "mis"
和 "het"
并且在这种情况下不应替换字符
我想更改特定位置的字符并将所有字符串存储在一个如下所示的新文件中
>name1
aacbecghidklmnop
>name2
axcyezghiaklmnop
有没有办法在 bash 循环中做到这一点?
请您尝试以下操作:
mapfile -t string < <(fold -w1 "string.txt")
# set string to an array of single characters: ("a" "b" "c" "d" ..)
while read -ra ary; do
if [[ ${ary[0]} = "position" ]]; then
# 1st line of file.vcf
declare -a pos=("${ary[@]:1}")
# now the array pos holds: (2 4 6 10 ..)
else
# 2nd line of file.vcf and after
declare -a new=("${string[@]}")
# make a copy of string to modify
for ((i=0; i<${#pos[@]}; i++ )); do
repl="${ary[$i+1]}" # replacement
if [[ $repl != "mis" && $repl != "het" ]]; then
new[${pos[$i]}-1]="$repl"
# modify the position with the replacement
fi
done
echo ">${ary[0]}"
(IFS=""; echo "${new[*]}")
# print the modified array as a concatenated string
fi
done < "file.vcf"
string.txt:
abcdefghijklmnop
file.vcf:
position 2 4 6 10
name1 a b c d
name2 x y z a
name3 i mis k l
输出:
>name1
aacbecghidklmnop
>name2
axcyezghiaklmnop
>name3
aicdekghilklmnop
我尝试在上面的脚本中嵌入解释作为注释,但是 如果您还有问题,请随时提问。
希望这对您有所帮助。