python - 按三列分组并创建表(最好的 awk)

标签 python awk count grouping

我需要对多列进行分组和计数的帮助。

输入:tsv 文件。

按第 1,2 和第 4 列排序。

header :字符串、开始、停止、长度、值

chr1    56971   57065   94      0.287234
chr1    565460  565601  141     0.411348
chr1    754342  754488  146     0.520548
chr1    783856  784002  146     0.315068
chr1    789652  789768  116     0.310345
chr1    790532  790628  96      0.520833
chr2    1744623 1744774 151     0.509934
chr2    1744623 1744774 151     0.509934
chr2    1744623 1744774 151     0.509934
chr2    1748501 1748635 134     0.440299
chr2    1748501 1748636 135     0.444444

输出:

                    0-10 length ... 90-100 ............140-150... 190-200
chr1:0-60000         A1(0), B1(0)..A2(1),B2(0.287234)..   A,B ... An,Bn
chr1:60000-120000          .             .                 .         . 
.                          .             .                 .         .
.                          .             .                 .         .
chr1:780000-840000       0,0     ..... 1,0.520833 ......1,0.315068..A,B
chr2:0-60000            A1,B1    .....   .        ......   .      .. .

A= 0-60000 区间内的行数(对于输入的第 2n 到第 3 列)

B= 输入中第 5 列的总和除以 A(行数)

首先按第一列分组并按

创建区域
for i in {0..249480000..60000}

对于该区域,计算按长度分组的行数 (0..200..10)

我尝试过:

for z in 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 X Y
do
for i in {0..249480000..60000}
    do
u=$i
let "u +=60000"

“现在我不知道接下来会发生什么”。

我知道按一列分组:

awk -F, 'NR>1{arr[$1]++}END{for (a in arr) print a, arr[a]}'

但这对我来说真的很难。请问你能帮我吗?

最佳答案

 awk -v Separator=' | ' '
    BEGIN{ LenStepSize = 10 ;  IntStepSize = 60000 }
    {
    # Store the labels
    Labels[ $1]++

    # Adapt the Step array size
    if ( IntLastIndex * IntStepSize < $3) IntLastIndex = int( $3 / IntStepSize) + 1
    IntIdx = int( $3 / IntStepSize)

    # Adapt the Length array size
    if( LenLastIndex * LenStepSize < $4) LenLastIndex = int( $4 / LenStepSize) + 1
    LenIdx = int( $4 / LenStepSize)

    # Create the mono "multi" index reference
    Idx = $1 "-" IntIdx "-" LenIdx

    # store the data element
    As[ Idx]++
    Bs[ Idx] += $5
    #printf( "DEBUG: As[%s]: %s | Bs[%s]:%s (+%s)\n", Idx, As[ Idx], Idx, Bs[ Idx], $5)
    }

    END {
       # Print the header
       printf( "Object               ")
       for ( Leng = 0; Leng <= LenLastIndex; Leng++ ) printf( "%s%3d - %3d", Separator, Leng, (Leng + 1) * LenStepSize)
       printf( "\n                     ")
       for ( Leng = 0; Leng <= LenLastIndex; Leng++ ) printf( "%s  length ", Separator)

       # print each element (empty or with value)
       # - lines per label
       for ( Label in Labels) {
          # - per sub section of intervale
          for ( Inter = 0; Inter <= IntLastIndex; Inter++ ) {
             printf( "\n%5s %7d-%7d", Label, Inter * IntStepSize, (Inter + 1) * IntStepSize - 1)

             # column per length section
             for ( Leng = 0; Leng <= LenLastIndex; Leng++ ) {
                Idx = Label "-" Inter "-" Leng
                printf( "%s%d , " ( Bs[ Idx] > 0 ? "%2.3f" : "%-5d") , Separator, As[ Idx], Bs[ Idx] / (As[ Idx] > 0 ? As[ Idx] : 1))
                }
             }
             print ""
          }
       }
    ' tsv.file
  • 使用类似的多维数组(1 个索引但由 3 个索引组成) 元素)
  • 基于元素大小动态(避免在内存中创建一个巨大的几乎为空的数组)
  • 不适合巨大的数据文件(由于内存影响)
  • 输出格式是基本的(没有基于内容的列或行大小调整,...)
    • 在 awk 的开头添加了 Separator 变量以查看(在本例中)列,但允许您将任何模式设置为分隔符(例如空格或“,”,...)以适合您的真正的需要

关于python - 按三列分组并创建表(最好的 awk),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41185303/

相关文章:

c++ - 计算字符串数组 C++ 中的字符串?

python - 使用pygame让子弹移动到你的光标

python - wxPython:线程化 GUI --> 使用自定义事件处理程序

bash - 使用 AWK 组合三个文件的 Shell 脚本

python - 对每对字段执行计算

mysql - SELECT COLUMN 和 COUNT 区分大小写,mysql?

python - 使用类在 python 中使用计数器函数创建顺序搜索

python - Image.fromarray 中的 "None"模式是什么意思?

linux - Bash 脚本没有产生预期的结果

sql - 一次选择返回两个 Count(*) 结果