Using awk to add up values and create new files

Viewed 55

I have a file that has a format like the following with two columns. The first being a name and the second column being the length (of something else) associated with that name:

scaffold1 8
scaffold2 15
scaffold3 4

My desired output is to add up the values of the second column and create an interval based on a set value, for this dummy set I'll say I want to add up to length of 10, output to a new file (we'll use the naming convention Interval.###.list), and then start over. With the above example, the total sums to 27, so my desired output would be the following three files (Interval.001.list to Interval.003.list), where Interval.001.list and Interval.002.list both sum to 10 and Interval.003.list has the remaining values (summing to 7). For example, Interval.001.list sums to 10 because scaffold1 is length 8 and then from scaffold2 there's length 2 being added. In the same manner, Interval.003.list sums to 7 because scaffold2 adds length 3 (13-15) and the entire scaffold3 is length 4.

> cat Interval.001.list
scaffold1
scaffold2:1-2

> cat Interval.002.list
scaffold2:3-12

> cat Interval.003.list
scaffold2:13-15
scaffold3 

Alternatively, it would be fine if it looked like:

> cat Interval.001.list
scaffold1:1-8
scaffold2:1-2

> cat Interval.002.list
scaffold2:3-12

> cat Interval.003.list
scaffold2:13-15
scaffold3:1-4

I currently have awk code to break up a single line in the original file if it is more than a set value, but I haven't figured out how to adjust it to total individual smaller quantities to the desired value. For what I currently have, if I have the following example in a file called scaffold-lengths.txt:

scaffold1 5275358
scaffold2 3399639

I then run and get (splitting up by 2000000):

> cat scaffold-lengths.txt | awk '{tot=0; while(tot<$2) {start=tot+1; tot+=2e6; if(tot>$2) tot=$2; printf("%d\t%s:%d-%d\n",++n, $1,start,tot);}}'
1   scaffold1:1-2000000
2   scaffold1:2000001-4000000
3   scaffold1:4000001-5275358
4   scaffold2:1-2000000
5   scaffold2:2000001-3399639

Then I would need to add up the length of the values next to the scaffold name to produce the Interval.list files with exactly the length I want. I am not adept at awk but I it seemed like awk would be the best way to go about this.

Any help would be appreciated, thanks!

0 Answers
Related