如何从VCF表中提取数据

问题描述 投票:0回答:1

我有两个文件:SCR_location-具有有关SNP位置按升序排列的信息。

 19687

 36075

 n...

modi_VCF-具有有关每个SNP信息的vcf表。

  19687  G A     xxx:255,0,195 xxx:255,0,206

  20398  G C     0/0:0,255,255 0/0:0,208,255

  n...

我只想将具有匹配SNP位置的行保存到新文件中我写了以下脚本,但是不起作用

cat SCR_location |while read SCR_l; do
    cat modi_VCF |while read line; do

            if  [ "$SCR_l" -eq "$line" ] ;
            then echo "$line" >> file
            else :
            fi

    done

done
bash unix
1个回答
0
投票

请您尝试一种bash解决方案:

declare -A seen
while read -r line; do
    seen[$line]=1
done < SCR_location

while read -r line; do
    read -ra ary <<< "$line"
    if [[ ${seen[${ary[0]}]} ]]; then
        echo "$line"
    fi
done < modi_VCF > file
  • 首先迭代SCR_location并将SNP位置存储在关联数组seen中。
  • 接下来,它将扫描modi_VCF,如果在关联数组中找到第一列值,则打印该行。

如果选择awk,您也可以说:

awk 'NR==FNR {seen[$1]++; next} {if (seen[$1]) print}' SCR_location modi_VCF > file

[编辑]为了过滤掉未完成的行,只需将逻辑取反为:]

awk 'NR==FNR {seen[$1]++; next} {if (!seen[$1]) print}' SCR_location modi_VCF > file_unmatched

上面的代码仅输出不匹配的行。如果要一次对匹配的行和不匹配的行进行排序,请尝试:

awk 'NR==FNR {seen[$1]++; next} {if (seen[$1]) {print >> "file_matched"} else {print >> "file_unmatched"} }' SCR_location modi_VCF

希望这会有所帮助。

© www.soinside.com 2019 - 2024. All rights reserved.