公開識別子の開始と終了の検索

公開識別子の開始と終了の検索

次のファイルがあります

chrom_pos   chrom    position        BF         window_id
"Chr01__69500112" 1 69500112 10.1214802 "window_1391"
"Chr01__69500129" 1 69500129 -7.59047205 "window_1391"
"Chr01__69500156" 1 69500156 8.475337 "window_1391"
"Chr01__69500197" 1 69500197 10.37964237 "window_1391"
"Chr01__69500233" 1 69500233 -8.59724917 "window_1391"
...
...
...
"Chr01__87300054" 1 87300054 -2.1058919 "window_1747"
"Chr01__87300107" 1 87300107 2.39553299 "window_1747"
"Chr01__87300686" 1 87300686 -11.03002514 "window_1747"
"Chr01__87300694" 1 87300694 -5.9689038 "window_1747"
"Chr01__87300709" 1 87300709 -5.52387942 "window_1747"
"Chr01__87300800" 1 87300800 -3.63119004 "window_1747"
...
...
...
"Chr17__200219189" 17 200219189 -2.30332074 "window_59705"
"Chr17__200219216" 17 200219216 15.04053029 "window_59705"
"Chr17__200219284" 17 200219284 -3.74266312 "window_59705"
"Chr17__200220333" 17 200220333 -1.91153668 "window_59705"
"Chr17__200241059" 17 200241059 -11.47407263 "window_59705"

列の各ウィンドウについて窓口ID、開始位置を知りたいです(場所考える窓口IDそれから開始)と終了位置(場所考える窓口IDついに取得します)以下のように出力されます。

window_id      chrom   starting_positon   ending_position
window_1391     1      69500112           69500233 
window_1747     1      87300054           87300800
window_59705    17     200219189          200241059

各ウィンドウの項目は順序付けされ、リンクされています。

答え1

使用awk:

NR == 1 { next } # skip header

$5 != window { # new (or first) window
    if (window != "") # unless this is the first window, print the collected data
        print window, chrom, start, end

    # collect data for next window
    chrom  = $2
    start  = $3
    window = $5
}

{ end = $3 } # always update the end position

# at the end, print the collected data for the last window
END { print window, chrom, start, end }

これを実行してください:

$ awk -f script.awk file
"window_1391" 1 69500112 69500233
"window_1747" 1 87300054 87300800
"window_59705" 17 200219189 200241059

出力区切り文字としてタブ文字を使用します。

$ awk -v OFS='\t' -f script.awk file
"window_1391"   1       69500112        69500233
"window_1747"   1       87300054        87300800
"window_59705"  17      200219189       200241059

出力用のコードを関数として収集する少しクールなバージョンです。この関数はヘッダーを出力し、元のウィンドウIDから二重引用符を削除します。

function output() {
    if (window == "")
        # no previous window, output header
        print "window_id", "chrom", "starting_posititon", "ending_position"
    else {
        # strip the first and last characters from window ID (the quotes)
        # then output
        w = substr(window, 2, length(window) - 2)
        print w, chrom, start, end
    }
}

NR == 1 { next } # skip header

$5 != window { # new (or first) window
    output()

    # collect data for next window
    chrom  = $2
    start  = $3
    window = $5
}

{ end = $3 } # always update the end position

# at the end, print the collected data for the last window
END { output() }

実行してください:

$ awk -v OFS='\t' -f script.awk file
window_id       chrom   starting_posititon      ending_position
window_1391     1       69500112        69500233
window_1747     1       87300054        87300800
window_59705    17      200219189       200241059

関連情報