如何从 vcf table 中提取数据

how to pull data from a vcf table

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

 19687

 36075

 n...

modi_VCF - 包含每个 SNP 信息的 vcf table。

  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 解决方案:

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[]++; next} {if (seen[]) print}' SCR_location modi_VCF > file

[编辑] 为了过滤掉未匹配的行,只需将逻辑取反即可:

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

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

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

希望这对您有所帮助。