get best line when coordinates overlap
<p>I have a dataset like this:</p>

<pre><code>   FBti0018875  2031    2045    -   TTCCAGAAACTGTTG hsf 62  0.9763443383736672
    FBti0018875 2038    2052    +   TTCTGGAATGGCAAC hsf 61  0.96581136371138
    FBti0018925 2628    2642    -   GACTGGAACTTTTCA hsf 60  0.9532992561656318
    FBti0018925 2828    2842    +   AGCTGGAAGCTTCTT hsf 63  0.9657036377575696
    FBti0018949 184 198 -   TTCGAGAAACTTAAC hsf 61  0.965931072979605
    FBti0018986 2036    2050    +   TTCTAGCATATTCTT hsf 63  0.9943559469645551
    FBti0018993 1207    1221    -   TTCTAGCATATTCTT hsf 63  0.9943559469645575
    FBti0018996 2039    2053    +   TTCTAGCATATTCTT hsf 63  0.9943559469645575
    FBti0019092 2985    2999    -   TTCTAGCATATTCTT hsf 63  0.9943559469645409
    FBti0019118 1257    1271    +   TTCCAGAATCTTGGA hsf 60  0.9523907773798134
</code></pre>

<p>The first column is an identifier, and the second and third are coordinates. I only want to keep one line for each range of coordinates. Meaning that I want to keep the best identifier if there is an overlap for it (the best is defined based on the last column, higher value = better).</p>

<p>For example for identifier FBti0018875 I would keep the first one because a) there is overlap with the second line and b) its last column value is higher (0.97>0.96).</p>

<p>If there was not an overlap between the first and second line I would keep both. Sometimes I can have 5 or 6 lines for each identifier, so it's not as simple as comparing the current one with the previous.</p>

<p>So far I have this code that doesn't work.</p>

<pre><code>def test(lista, listb): #Compare lists of coordinates
    a = 0
    b = 0
    found = False
    while a&lt;len(lista) and b&lt;len(listb):
        result = check( lista[a] , listb[b] )
        if result &lt; 0:
            a += 1
            continue
        if result &gt; 0:
            b += 1
            continue
        # we found overlapping intervals
        found = True
        return (found, a, lista[a], b, listb[b] )
    return found

def check( (astart, aend) , (bstart, bend) ):
    if aend &lt; bstart:
        return -1
    if bend &lt; astart:
        return 1
    return 0

refine = open("tffm/tffm_te_hits95.txt", "r")
refined = open("tffm/tffm_te_unique_hits95.txt", "w")
current_TE=[]
for hit in refine:
    info=hit.rstrip().split('\t')
    if len(current_TE)==0 or info[0]==current_TE[0][0]:
        current_TE.append(info)
    elif info[0]!=current_TE[0][0]:
        to_keep=[]
        i=0
        if len(current_TE)==1:
            to_keep.append(0)
        else:
            for i in range(len(current_TE)-1):
                if [current_TE[i][1], current_TE[i][2]] == [current_TE[i+1][1], current_TE[i+1][2]]:
                    if current_TE[i][7]&lt;current_TE[i+1][7]:
                        to_keep.append(i+1)
                elif test([(current_TE[i][1], current_TE[i][2])], [(current_TE[i+1][1], current_TE[i+1][2])])!='False':
                    if current_TE[i][7]&lt;current_TE[i+1][7]:
                        to_keep.append(i+1)
                        try:
                            to_keep.remove(i)
                        except:
                            pass
                    else:
                        to_keep.append(i)
            else:
                to_keep.append(i)
                if i==len(current_TE)-1:
                    to_keep.append(i+1)
    for item in set(to_keep):
        print current_TE[item]
        current_TE=[]
</code></pre>

<p>The expected outcome in this case would be (only losing one FBti0018875)</p>

<pre><code>FBti0018875  2031    2045    -   TTCCAGAAACTGTTG hsf 62  0.9763443383736672
FBti0018925 2628    2642    -   GACTGGAACTTTTCA hsf 60  0.9532992561656318
FBti0018925 2828    2842    +   AGCTGGAAGCTTCTT hsf 63  0.9657036377575696
FBti0018949 184 198 -   TTCGAGAAACTTAAC hsf 61  0.965931072979605
FBti0018986 2036    2050    +   TTCTAGCATATTCTT hsf 63  0.9943559469645551
FBti0018993 1207    1221    -   TTCTAGCATATTCTT hsf 63  0.9943559469645575
FBti0018996 2039    2053    +   TTCTAGCATATTCTT hsf 63  0.9943559469645575
FBti0019092 2985    2999    -   TTCTAGCATATTCTT hsf 63  0.9943559469645409
FBti0019118 1257    1271    +   TTCCAGAATCTTGGA hsf 60  0.9523907773798134
</code></pre>

<p>I have tried (with the code) to generate a list containing several lines with the same identifier and then parse it for the ones with overlapping coordinates and if that was the case select one according to the last column. It succeeds in checking the overlap but I only retrieve a handful of lines in some versions of it or:</p>

<pre><code>Traceback (most recent call last):
  File "&lt;stdin&gt;", line 29, in &lt;module&gt;
IndexError: list index out of range
</code></pre>

<p>Finally I solved. There was a silly mistake with 'False' instead of False.</p>

<p>Here is the solution:</p>

<pre><code>def test(lista, listb):
    a = 0
    b = 0
    found = False
    while a&lt;len(lista) and b&lt;len(listb):
        result = check( lista[a] , listb[b] )
        if result &lt; 0:
            a += 1
            continue
        if result &gt; 0:
            b += 1
            continue
        # we found overlapping intervals
        found = True
        return (found, a, lista[a], b, listb[b] )
    return found

def check( (astart, aend) , (bstart, bend) ):
    if aend &lt; bstart:
        return -1
    if bend &lt; astart:
        return 1
    return 0

def get_unique_sre(current_TE):
    to_keep = range(0,len(current_TE))
    for i in range(len(current_TE)-1):
        if [current_TE[i][1], current_TE[i][2]] == [current_TE[i+1][1], current_TE[i+1][2]]:
            if current_TE[i][7]&lt;current_TE[i+1][7]:
                try:
                    to_keep.remove(i)
                except:
                    pass
        elif test([(current_TE[i][1], current_TE[i][2])], [(current_TE[i+1][1], current_TE[i+1][2])])!=False:
            if current_TE[i][7]&lt;current_TE[i+1][7]:
                try:
                    to_keep.remove(i)
                except:
                    pass
            else:
                to_keep.remove(i+1)
    final_TE=[]
    for i in to_keep:
        final_TE.append(current_TE[i])
    return final_TE


refine = open("tffm/tffm_te_hits95.txt", "r")
refined = open("tffm/tffm_te_unique_hits95.txt", "w")
current_TE=[]
for hit in refine:
    info=hit.rstrip().split('\t')
    if len(current_TE)==0 or info[0]==current_TE[0][0]:
        current_TE.append(info)
    else:
        if len(current_TE)==1:
            print&gt;&gt;refined, current_TE[0]
            current_TE=[]
        else:
            final_TE = get_unique_sre(current_TE)
            for item in final_TE:
                print&gt;&gt;refined, item
                current_TE=[]
refined.close()                
</code></pre>

python coordinates