generating midpoints on a bedfile
1
0
Entering edit mode
9.0 years ago
a.rex ▴ 350

I have the following befile:

name           start    end                              id                                                            length
scaffold1       1       160     NB501801:1:HN3WLBGXY:4:11601:9830:10032          159
scaffold1       1       164     NB501801:1:HN3WLBGXY:3:13412:18598:13811        163
scaffold1       1       167     NB501801:1:HN3WLBGXY:3:22404:14294:20264        166
scaffold1      15      189     NB501801:1:HN3WLBGXY:2:23204:7996:11409           174
scaffold1       1       201     NB501801:1:HN3WLBGXY:3:21609:2881:5564             200
scaffold1       46      230     NB501801:1:HN3WLBGXY:4:23510:23247:4818          184

I want to generate an extra column in this file with the midpoint. Is there a quick method for doing this using bedtools? How can I do this with python?

bed bedtools • 2.4k views
ADD COMMENT
2
Entering edit mode
9.0 years ago

There probably is a quick awk way to do this, but since you ask specifically for python, try something like the following untested code bit:

import sys

with open(sys.argv[1]) as inbed:
    for line in inbed:
        if line.startswith('name'):
           print(line.strip() + "\tmidpoint")
        else:
           print(line.strip() + "\t" + str((int(line.split('\t')[1]) + int(line.split('\t')[2]))/2))

Save as addMidpoint.py, execute as python addMidpoint.py yourinput.bed > youroutput.be

ADD COMMENT
0
Entering edit mode

Thanks for your help. However, when I run this code an error appears. It seems that there is a concatenation of string and integer in the last line. I can't seem to rectify this. Sorry - any further help would be appreciated.

ADD REPLY
1
Entering edit mode

Wow, had some ridiculous blunders in my code. Can you check again?

ADD REPLY
0
Entering edit mode

Works perfectly now! Thank you.

ADD REPLY

Login before adding your answer.

Traffic: 4300 users visited in the last hour
Help About
FAQ
Access RSS
API
Stats

Use of this site constitutes acceptance of our User Agreement and Privacy Policy.

Powered by the version 2.3.6