2

I have a large text file, as shown below, where column1-colum3 define a region and fourth column is unique identifier. The column 6 is assigned value of the region.

chr6    26204834    26204839    region1 +   119.862 chr6    26204834    26204835    +   0.982
chr6    26204834    26204839    region1 +   119.862 chr6    26204835    26204836    +   8.487
chr6    26204834    26204839    region1 +   119.862 chr6    26204836    26204837    +   2.664
chr6    26204834    26204839    region1 +   119.862 chr6    26204837    26204838    +   105.065
chr6    26204834    26204839    region1 +   119.862 chr6    26204838    26204839    +   2.664
chr18   72218300    72218307    region2 +   113.879 chr18   72218300    72218301    +   0.982
chr18   72218300    72218307    region2 +   113.879 chr18   72218303    72218304    +   1.357
chr18   72218300    72218307    region2 +   113.879 chr18   72218304    72218305    +   4.887
chr18   72218300    72218307    region2 +   113.879 chr18   72218305    72218306    +   1.706
chr18   72218300    72218307    region2 +   113.879 chr18   72218306    72218307    +   104.947

The column7-column9 are sub-region, where column11 is the assigned value of sub region.

Output: I want to assign cumulative sum for each sub region in each identifier.

cat input | grep region1 | awk '{ sum+=$11 } { print $0"\t"sum/$6 }'

this gives correct expected output for one identifier

chr6    26204834    26204839    region1 +   119.862 chr6    26204834    26204835    +   0.982   0.00819276
chr6    26204834    26204839    region1 +   119.862 chr6    26204835    26204836    +   8.487   0.0789992
chr6    26204834    26204839    region1 +   119.862 chr6    26204836    26204837    +   2.664   0.101225
chr6    26204834    26204839    region1 +   119.862 chr6    26204837    26204838    +   105.065 0.977774
chr6    26204834    26204839    region1 +   119.862 chr6    26204838    26204839    +   2.664   1

Similarly for another identifier

cat input | grep region2 | awk '{ sum+=$11 } { print $0"\t"sum/$6 }' 


chr18   72218300    72218307    region2 +   113.879 chr18   72218300    72218301    +   0.982   0.00862319
chr18   72218300    72218307    region2 +   113.879 chr18   72218303    72218304    +   1.357   0.0205393
chr18   72218300    72218307    region2 +   113.879 chr18   72218304    72218305    +   4.887   0.0634533
chr18   72218300    72218307    region2 +   113.879 chr18   72218305    72218306    +   1.706   0.0784341
chr18   72218300    72218307    region2 +   113.879 chr18   72218306    72218307    +   104.947 1

How do i automate this, by defining all identifier in an array and running through text file.

1
  • 1
    This question is perfectly on topic and welcome here, but based on the data you are showing, you might also be interested in our sister site: Bioinformatics.
    – terdon
    Commented Sep 20, 2021 at 20:52

1 Answer 1

2

Here's one way:

$ awk -vOFS='\t' '{ a[$4]+=$11/$6; print $0,a[$4]}' file 
chr6    26204834    26204839    region1 +   119.862 chr6    26204834    26204835    +   0.982   0.00819276
chr6    26204834    26204839    region1 +   119.862 chr6    26204835    26204836    +   8.487   0.0789992
chr6    26204834    26204839    region1 +   119.862 chr6    26204836    26204837    +   2.664   0.101225
chr6    26204834    26204839    region1 +   119.862 chr6    26204837    26204838    +   105.065 0.977774
chr6    26204834    26204839    region1 +   119.862 chr6    26204838    26204839    +   2.664   1
chr18   72218300    72218307    region2 +   113.879 chr18   72218300    72218301    +   0.982   0.00862319
chr18   72218300    72218307    region2 +   113.879 chr18   72218303    72218304    +   1.357   0.0205393
chr18   72218300    72218307    region2 +   113.879 chr18   72218304    72218305    +   4.887   0.0634533
chr18   72218300    72218307    region2 +   113.879 chr18   72218305    72218306    +   1.706   0.0784341
chr18   72218300    72218307    region2 +   113.879 chr18   72218306    72218307    +   104.947 1

The trick is to use the region identifier as the key for an associative array (a, in this case). You can then increment the value associated with that key as you read the file.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .