【问题标题】:change charters in a string based on vcf table data根据 vcf 表数据更改字符串中的章程
【发布时间】:2019-11-22 21:14:03
【问题描述】:

我有一个长字符串文件 (string.txt) (abcdefghijklmnop) 和一个像这样的 vcf 表 (file.vcf)

position 2 4 6 10 n...
name1 a b c d
name2 x y z a
namen...

该表还包含"mis""het",在这种情况下不应替换字符

我想更改特定位置的字符并将所有字符串存储在一个看起来像这样的新文件中

>name1
aacbecghidklmnop
>name2
axcyezghiaklmnop

有没有办法在 bash 循环中做到这一点?

【问题讨论】:

  • 不清楚你用什么替换什么? “aacbec”和“axcyez”是从哪里来的?
  • 抱歉不清楚。我要操作的字符串在文件 string.txt 中。根据 vcf 表中的数据,我想在 vcf 表中列出的特定位置创建新字符串并进行修改。希望现在更清楚了
  • 不,还不清楚。生成这些修改字符串的规范在哪里?
  • 哦,抱歉,刚刚看到它被编辑了。我已经修复了问题中的表格
  • 您可能应该提供具体示例,因为您提供的玩具示例有些令人困惑。此外,这将很难在bash 中严格执行。您的意思是使用诸如awktr 等Linux 命令吗?

标签: bash bioinformatics fasta genome vcf-variant-call-format


【解决方案1】:

请您尝试以下方法:

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"

字符串.txt:

abcdefghijklmnop

文件.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

我尝试在上面的脚本中嵌入解释为 cmets,但是 如果您仍有疑问,请随时提问。

希望这会有所帮助。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-03-30
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多