Text into sliding window and count

Viewed 63

I have file (More than 1 lack lines)as like this

 20     14370   rs6054257 G      A       29   PASS   NS=3;DP=14;AF=0.5;DB;H2           GT:GQ:DP:HQ 0|0:48:1:51,51 
 20     17330   .         T      A       3    q10    NS=3;DP=11;AF=0.017               GT:GQ:DP:HQ 0|0:49:3:58,50 
 20     1110696 rs6040355 A      G,T     67   PASS   NS=2;DP=10;AF=0.333,0.667;AA=T;DB GT:GQ:DP:HQ 0|0:21:6:23,27
 20     1230237 .         T      .       47   PASS   NS=3;DP=13;AA=T                   GT:GQ:DP:HQ 0|0:54:7:56,60
 20     1234567           GTC    G,GTCT  50   PASS   NS=3;DP=9;AA=G                    GT:GQ:DP    0/1:35:4

I need split as sliding window and count "0/0" positions as like this

 Pos     Count 
 1-10001  0
 2-10002  1
 3-10003  0

For counting each 10000 positions I used this cmd

tail -n +11 file | 
awk -v n=10000 '/0\/0/{c++} NR%n==0{print c; c=0} END {if (NR%n!=0) print c}'
1 Answers

1st solution: Completely based on your shown attempts only, written in GNU awk. Couldn't test much since samples are not having 0/0 values in it, should work through. Taken tail command from OP's attempt itself.

tail -n +11 Input_file | 
awk -v n="10000" '
  NR%n==0{
    ++occur
    print n+occur,count
    count=""
  }
  /0\/0/{
    count++
  }
  END{
    ++occur
    if(count){ print n+occur }
  }
'


2nd solution: In case you have multiple occurrences of 0/0 in your lines and you want to count all in each line then try following slightly different from 1st solution.

tail -n +11 Input_file | 
awk -v n="10000" '
  NR%n==0{
    ++occur
    print n+occur,count
    count=""
  }
  {
    count+=gsub(/0\/0/,"&")
  }
  END{
    ++occur
    if(count){ print n+occur }
  }
'
Related