0

我正在运行以下代码来操作 vcf 表中的数字数据。

 cat inputfile | while read row; do
                echo $row > tmp
                originalProb= `awk '{print $1}' tmp`
                probabilityHom1=`awk '{print $2}' tmp`
                probabilityHom2=`awk '{print $4}' tmp`
                numCols=`awk '{print NF}' tmp`

                if [ $numCols -gt 4 ]; then
                        echo "${originalProb}" >> currentRowGenotypes
                elif [ "$probabilityHom1" -gt "$probabilityHom2" ]; then
                        echo "1/1" >> currentRowGenotypes
                elif [ "$probabilityHom1" -lt "$probabilityHom2" ]; then
                        echo "0/0" >> currentRowGenotypes
                elif [ "$probabilityHom1" -eq "$probabilityHom2" ] && [ "$probabilityHom1" -eq 0 ]; then
                        echo "${originalProb}" >> currentRowGenotypes
                else                    
                        echo "het" >> currentRowGenotypes
                fi

        done

        cat tmpHeaders currentRowGenotypes > currentFullCol

输入文件看起来像这样

1/1     255     231     0
0/1     255     0       152
0/1     255     0       82
0/1     255     0       151
0/1     239     0       31
0/1     255     0       255

由于某种原因,awk 命令无法识别第一列。有什么建议么 ?

4

2 回答 2

2

创建一个临时文件只是为了awk 将行拆分为列并不是一个好主意,因为:

  • 它会导致逐行创建临时文件的开销。
  • 它多次产生子进程来调用awk
  • bash和之间的语法差异awk可能是导致错误的原因。

您可以使用awk. 请尝试以下方法:

while read -ra row; do
    originalProb="${row[0]}"
    probabilityHom1="${row[1]}"
    probabilityHom2="${row[3]}"
    numCols="${#row}"

    if (( numCols > 4 )); then
        echo "$originalProb" >> currentRowGenotypes
    elif (( probabilityHom1 > probabilityHom2 )); then
        echo "1/1" >> currentRowGenotypes
    elif (( probabilityHom1 < probabilityHom2 )); then
        echo "0/0" >> currentRowGenotypes
    elif (( probabilityHom1 == probabilityHom2 &&  probabilityHom1 == 0 )); then
        echo "$originalProb" >> currentRowGenotypes
    else
        echo "het" >> currentRowGenotypes
    fi
done < inputfile

cat tmpHeaders currentRowGenotypes > currentFullCol

正如其他人一再建议的那样,更好的方法是编写awk

awk '{
    originalProb = $1
    probabilityHom1 = $2
    probabilityHom2 = $4
    numCols = NF

    if ( numCols > 4 )
        print originalProb >> "currentRowGenotypes"
    else if ( probabilityHom1 > probabilityHom2 )
        print "1/1" >> "currentRowGenotypes"
    else if ( probabilityHom1 < probabilityHom2 )
        print "0/0" >> "currentRowGenotypes"
    else if ( probabilityHom1 == probabilityHom2 && probabilityHom1 == 0 )
        print originalProb >> "currentRowGenotypes"
    else
        print "het" >> "currentRowGenotypes"
}' inputfile

cat tmpHeaders currentRowGenotypes > currentFullCol

希望这可以帮助。

于 2019-12-04T08:02:14.783 回答
1

为什么不使用Pysam?它非常适合解析 BCF/VCF。

于 2020-01-20T01:50:16.040 回答