安排数据和制作字典

时间:2015-03-24 18:41:04

标签: awk gawk

我有一个制表符分隔文件,如下所示:

chr14   106559873       106560782       MA0004.1_Arnt
chr14   106559873       106560782       MA0093.1_USF1
chr14   106559873       106560782       MA0147.1_Myc
chr14   106559873       106560782       RUNX3_DBD_WAACCRCAAWAACCRCAN
chr10   17037867        17038971        MA0080.2_SPI1
chr10   17037867        17038971        MA0152.1_NFATC2
chr17   8610947 8611433 MA0080.2_SPI1
chr17   8610947 8611433 MA0098.1_ETS1

我想这样安排:

Regions   MA0004.1_Arnt  MA0093.1_USF1  MA0147.1_Myc  RUNX3_DBD_WAACCRCAAWAACCRCAN MA0080.2_SPI1 MA0152.1_NFATC2 MA0098.1_ETS1
chr14;106559873;106560782   1 1 1 1 0 0 0
chr10;17037867;17038971     0 0 0 0 1 1 0
chr10;17037867;17038971     0 0 0 0 1 0 1

示例输出仅显示第一行的四行,但这需要应用于整个文件。 1表示字符串的存在。

这是我编写的代码的中间部分,这对我的分析至关重要。我想不再怎么在awk中这样做。

谢谢。

1 个答案:

答案 0 :(得分:2)

这个awk脚本可以帮助你完成大部分工作:

BEGIN {
    print "Regions   MA0004.1_Arnt  MA0093.1_USF1  MA0147.1_Myc  RUNX3_DBD_WAACCRCAAWAACCRCAN MA0080.2_SPI1 MA0152.1_NFATC2 MA0098.1_ETS1"
    a["MA0004.1_Arnt"] = a["MA0093.1_USF1"] = \
    a["MA0147.1_Myc"] = a["RUNX3_DBD_WAACCRCAAWAACCRCAN"] = \
    a["MA0080.2_SPI1"] = a["MA0152.1_NFATC2"] = a["MA0098.1_ETS1"] = 0
}

function print_fields () {
    print p";"s";"e, a["MA0004.1_Arnt"], a["MA0093.1_USF1"],
    a["MA0147.1_Myc"], a["RUNX3_DBD_WAACCRCAAWAACCRCAN"],
    a["MA0080.2_SPI1"], a["MA0152.1_NFATC2"], a["MA0098.1_ETS1"]    
}

NR>1&&$1!=p {
    print_fields()
    for (i in a) a[i] = 0
}

{ p=$1; s=$2; e=$3; a[$4]=1 }

END { print_fields() }

测试出来:

$ awk -f script.awk file
Regions   MA0004.1_Arnt  MA0093.1_USF1  MA0147.1_Myc  RUNX3_DBD_WAACCRCAAWAACCRCAN MA0080.2_SPI1 MA0152.1_NFATC2 MA0098.1_ETS1
chr14;106559873;106560782 1 1 1 1 0 0 0
chr10;17037867;17038971 0 0 0 0 1 1 0
chr17;8610947;8611433 0 0 0 0 1 0 1