Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/302.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 按三列分组并创建表(最佳awk)_Python_Awk_Count_Grouping - Fatal编程技术网

Python 按三列分组并创建表(最佳awk)

Python 按三列分组并创建表(最佳awk),python,awk,count,grouping,Python,Awk,Count,Grouping,我需要帮助从许多列进行分组和计数 输入:tsv文件 按1、2和第4列排序 标题:字符串、开始、停止、长度、值 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

我需要帮助从许多列进行分组和计数

输入:tsv文件

按1、2和第4列排序

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

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分隔符=“|”
 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
开始{LenStepSize=10;IntStepSize=60000} { #储存标签 标签[$1]++ #调整步长数组大小 如果(IntLastIndex*IntStepSize<$3)IntLastIndex=int($3/IntStepSize)+1 IntIdx=int($3/IntStepSize) #调整长度数组大小 如果(LenLastIndex*LenStepSize<$4)LenLastIndex=int($4/LenStepSize)+1 LenIdx=int($4/LenStepSize) #创建mono“multi”索引引用 Idx=$1“-”IntIdx“-”LenIdx #存储数据元素 As[Idx]++ Bs[Idx]+=$5 #printf(“调试:As[%s]:%s | Bs[%s]:%s(+%s)\n”、Idx、As[Idx]、Idx、Bs[Idx]、$5) } 结束{ #打印标题 printf(“对象”)
对于(Leng=0;Leng请,这样您就可以显示给定输入所需的精确输出。这样,事情会变得更清楚。这样更好吗?很抱歉。跨越间隔边界的范围会发生什么情况?有两个间隔。在Y轴上,输入中有$1和$2以及$3(从0到60000到249480000的间隔)在X轴上,输入的长度$4有一个间隔(0乘以10到200).在此间隔中,我需要计算位于这些间隔中的行数,并将值之和除以位于这些间隔中的行数,如果它交叉,则首选较小的间隔,或者可以在两个间隔中都计算。我尝试了此方法,因为行数效果很好,但值B不起作用:-(但这真的是一项伟大的工作,我很惊讶。)我试图找出,你的脚本中哪里可能有问题,但我没有:-(还有一次我对计算值(“B”)有问题:-(我想,它只显示整数,但我需要浮点(f.e:0.45)添加了一些注释以便更好地理解和输出B的WARE
%d
,因此没有十进制数,我将其更改为“%2.3f”,并在其他一些格式化输出中添加注释,这对我更好地理解脚本。这是一个很好的工作,非常感谢