Question: Form Groups Of Lines And Select Specific Values (R)
1
4.8 years ago by
viniciushs8850
Germany
viniciushs8850 wrote:

i would like to form group of lines based in interconection (two ways) between "type1" collumn and "type2" collumn. The logic is: if a string in "type1" are in the same line of "type2" collumn they are in the same group. However if "type2" are more than one line all those are in the same group.

Please take a look in the first 3 lines: "gain_765" and "loss_1136" are related. However, "loss_1136" are related with "gain_766" and subsenquently "gain_766" are relate with "loss_765". Then these is my group: 1- "gain_765", 2- "loss_1136", 3-"gain_766", 4-"loss_765".

Inside this group I wanna to make a new line with string in "chrx" on first line of the group; lowest value in "startx" and "starty"; larger value in "endx" and "endy". Follow a example of my data:

`````` type1      chrx     startx  endx   chry    starty   endy    type2
gain_765    chr15   9681969 9685418 chr15   9660912 9712719 loss_1136
gain_766    chr15   9706682 9852347 chr15   9660912 9712719 loss_1136
gain_766    chr15   9706682 9852347 chr15   9765125 9863990 loss_765
gain_780    chr20   9706682 9852347 ch20    9765125 9863990 loss_769
gain_760    chr15   9706682 9852347 chr15   9660912 9712719 loss_1137
gain_760    chr15   9706682 9852347 chr15   9765125 9863990 loss_763
``````

To first group (line 1 to 3) this is the expected output:

``````     chr       start     end
chr15    9660912   9863990
``````

Now, please take a look in line 4: "gain_780" is related just with "loss_769". Is this group (just line 4) the output expected follows:

``````     chr       start     end
chr20     9706682   9863990
``````

Now, lines 5 and 6 the group is formed by "gain_760"; "loss_1137" and "loss_763". In this last case the expected output is:

``````     chr       start     end
chr15     9660912   9863990
``````

But, I have many of this cases in thousands of lines. Therefore, I need all results in a unique output, like that:

``````      chr       start     end
chr15    9660912   9863990
chr20    9706682   9863990
chr15    9660912   9863990
``````

Cheers.

R • 932 views
modified 4.8 years ago by Alex Reynolds26k • written 4.8 years ago by viniciushs8850
1
4.8 years ago by
Alex Reynolds26k
Seattle, WA USA
Alex Reynolds26k wrote:

Here's a way to do this using Python:

<script src="&lt;a href=" alexpreynolds="" 8945214"="">alexpreynolds/8945214"></script>

Here's output from this script:

``````\$ ./consolidate.py | sort-bed -
chr15    9660912    9863990    set(['gain_765', 'loss_1136', 'gain_766', 'loss_765'])
chr15    9660912    9863990    set(['loss_1137', 'loss_763', 'gain_760'])
chr20    9706682    9863990    set(['loss_769', 'gain_780'])
``````

I can't say how well this would perform with thousands of lines, but it should be okay on a modern workstation, I'd think. It will likely use a fair bit of memory, too.

By building unions of smaller sets that overlap by ID, and then throwing away all empty subsets, I think my approach will work well with longer chains of ids.