I have two files
s3.txt :
1 10 20
1 5 20
2 20 30
2 25 30
1 10 50
2 20 60
1 14 17
s4.txt:
1 10 20
2 20 30
I am trying to match col0 of both the files and get rows that fall between range(inclusive of themselves) 10-20 and 20-30 as seen in s4 file. file s4 has co ordinates which can be used as reference range (chrom start and end) and s3 has list of co ordinates from an experimental condition what I am trying to achieve is to which co-ordinates from my file s3 fall on or between my reference co ordinates in s4.
code so far:
containing_ranges = []
with open('s4.txt', 'r') as f:
for line in f:
fields = line.strip().split('\t')
containing_ranges.append(fields)
tested_ranges = []
with open('s3.txt', 'r') as f:
for line in f:
fields = line.strip().split('\t')
tested_ranges.append(fields)
for c_range in containing_ranges:
for t_range in tested_ranges:
tst = int(t_range[1])
ten = int(t_range[2])
cst = int(c_range[1])
cen = int(c_range[2])
if c_range[0] == t_range[0]:
included = cst >= tst and cen <= ten
if included == True:
print t_range
Output with missing row(1 14 17) :
['1', '10', '20']
['1', '5', '20']
['1', '10', '50']
['2', '20', '30']
['2', '20', '60']
Desired output:
1 10 20
2 20 30
2 25 30
1 14 17
Not sure if my logic is wrong and why does it miss 14-17 as it falls between 10-20
[EDIT] using pybedtools
>>> print(s4.intersect(s3, wb=True))
1 10 20 1 10 20
1 10 20 1 5 20
1 10 20 1 10 50
1 14 17 1 14 17
2 20 30 2 20 30
2 25 30 2 25 30
2 20 30 2 20 60
>>> print(s4.intersect(s3, wa=True, wb=True, F=1))
1 10 20 1 10 20
1 10 20 1 14 17
2 20 30 2 20 30
2 20 30 2 25 30
using bedops
bin$ less answer.bed
1 5 20
1 10 20
1 10 50
1 14 17
2 20 30
2 20 60
2 25 30
using @bli code(on python2.7)
('1', 10, 20)
('1', 14, 17)
('2', 20, 30)
('2', 25, 30)
why can I not see the interval 1 5 20
bedops --element-of
? $\endgroup$