I have two files s3.txt :1   10  201   5   202   20  302   25  301   10  502   20  601   14  17s4.txt:1   10  202   20  30I 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_rangeOutput 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  202   20  302   25  301   14  17Not 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  201   10  20  1   5   201   10  20  1   10  501   14  17  1   14  172   20  30  2   20  302   25  30  2   25  302   20  30  2   20  60>>> print(s4.intersect(s3, wa=True, wb=True, F=1))1   10  20  1   10  201   10  20  1   14  172   20  30  2   20  302   20  30  2   25  30using bedops bin$ less answer.bed 1       5       201       10      201       10      501       14      172       20      302       20      602       25      30using @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
Range overlap python error with genomic regions
bed;genomics;python;homework
Regarding your python codeIf you want the experimental ranges that are entirely contained in one of the reference ranges, you need to have the coordinates in the following order:cst <= tst < ten <= cenIf what you want are the experimental ranges that overlap one of the reference ranges, you need to have either the start or the end of the experimental range fall within the reference range:(cst <= tst < cen) or (cst < ten <= cen)Your code is equivalent to neither possibilities:cst >= tst and cen <= tenThis is equivalent to:tst <= cst and cen <= ten(Or tst <= cst < cen <= ten, since we know that cst < cen, by definition of a bed interval).With this rewriting, we can more easily see that you are actually selecting the experimental ranges that contain a reference range.Here is some (python3) code that gives you the results in the other two situations:#!/usr/bin/env python3ref_intervals = []with open(s4.txt, r) as f:    for line in f:        (chr, start, end) = line.strip().split(\t)        ref_intervals.append((chr, int(start), int(end)))exp_intervals = []with open(s3.txt, r) as f:    for line in f:        (chr, start, end) = line.strip().split(\t)        exp_intervals.append((chr, int(start), int(end)))contained = []overlapping = []for (r_chr, r_start, r_end) in ref_intervals:    for (e_chr, e_start, e_end) in exp_intervals:        if e_chr == r_chr:            if r_start <= e_start < r_end or r_start < e_end <= r_end:                overlapping.append((e_chr, e_start, e_end))            if r_start <= e_start < e_end <= r_end:                contained.append((e_chr, e_start, e_end))print(overlapping)for (chr, start, end) in overlapping:    print(chr, start, end, sep=\t)print(contained)for (chr, start, end) in contained:    print(chr, start, end, sep=\t)If I run it, I obtain the following results:$ ./overlap.py overlapping1   10  201   5   201   10  501   14  172   20  302   25  302   20  60contained1   10  201   14  172   20  302   25  30It is probably an good exercise to program this, but as the other answers point out, there are efficient tools that would be a better solution in a professional setting.