Bash - 在 qvalue 列上对 bed 文件进行排序然后提取具有最高 q 值的前 20% 的行

Bash - one liner to sort a bed file on qvalue column then extract top 20% of rows with highest q value

我有以下格式的床文件:

chr start   end q-value   name
chr1    10004  10467    310.43    peak_1
chr2    15410  15704    19.61 peak_2
chr3    21207  21354    4.04  peak_3
chr4    26073  26165    25.32 peak_4
chr5   63044057  63044425   39.65  peak_5

如果可能,我需要一个 bash 单行代码在 q 值列(第 4 列)上对该文件进行排序,然后我需要提取具有最高 q- 的前 20% 的行价值。

排序后如下所示:

chr start   end q-value   name
chr1    10004  10467    310.43    peak_1
chr5   63044057  63044425   39.65  peak_5
chr4   26073  26165    25.32 peak_4
chr2    15410  15704    19.61 peak_2
chr3    21207  21354    4.04  peak_3

百分比后看起来像:

chr1    10004  10467    310.43    peak_1

我需要 运行 对 40 多个文件进行此操作。

我也熟悉 R,所以如果这在 bash 中不可能,但在 R 中可行,R 代码也很有用(但 Bash 更可取)。

非常感谢。


编辑评论:

使代码更易于测试。

回复:我自己的尝试

当我第一次尝试 运行 sort -k4 file.txt 时。我得到的不是我想要的:

chr2    15410  15704    19.61 peak_2
chr4    26073  26165    25.32 peak_4
chr1    10004  10467    310.43    peak_1
chr5   63044057  63044425   39.65  peak_5
chr3    21207  21354    4.04  peak_3

这让我很困惑,我认为小数是导致问题的原因,并且不确定如何绕过第一部分。

这是你正在看的吗?

#!/bin/sh
sort -r -g -k 4,4 < inputFile.file > tempfile_sorted.out
lncnt=$(wc -l < tempfile_sorted.out)
percent_linecount_infloat=$(echo "$lncnt*.2" | bc)
float2Int=$(printf %.0f "$percent_linecount_infloat")
head_20_percent=$(head -"$float2Int" tempfile_sorted.out)
new_fn=$(printf "%s_20" tempfile_sorted.out) # new file with top 20% of sorted output
printf "$head_20_percent" > $new_fn