Nested loops for comparing 2 files
Question: I am writing a program to compare two files. For each line in file 1, I want
to compare it to all lines in file 2, then continue with the next line in file
1. The program is not continuing in file 1 after the first hit. Any
suggestions?

Code: Select all

    
    
    #! /usr/bin/env python
    
    import sys
    import fileinput
    
    # Open the two files
    f1 = open(sys.argv[1], "r")
    f2 = open(sys.argv[2], "r")
    
    for line in f1:
        chrR,chrStart,chrEnd,name,score,strand1,codingStart,codingEnd,itemRbg,blockCount,blockSize,BlockStart = line.strip().split()
        chr = range(int(chrStart), int(chrEnd))
        lncRNA = set(chr)
        for line in f2:
            chrC,clustStart,clustEnd,annote,score,strand = line.strip().split()
            clust = range(int(clustStart), int(clustEnd))
            cluster = set(clust)
            if strand1 == '-':
                if chrR == chrC:
                    if strand1 == strand:
                        if cluster & lncRNA:
                             print name,annote,'transcript'
                             continue
                         else:
                             continue
                     continue
            break
    


Answer: After the first line in `f1` you've already read all lines from `f2` file
therefore `for line2 in f2` has zero iteration for the second and following
lines in `f1` file unless `f2` file grows on disk.

    
    
    #!/usr/bin/env python
    import sys
    
    def intersect(r1, r2):
        return r2[0] < (r1[-1]+1) and r1[0] < (r2[-1]+1)
    
    with open(sys.argv[2]) as f2:
         chrC_set, strand_set, clusters = set(), set(), []
         for i, line in enumerate(f2):
             parts = line.split()
             if len(parts) != 6:
                print >>sys.stderr, "%d line has %d parts: %s" % (i, len(parts), line),
                continue
             chrC, clustStart, clustEnd, annote, _, strand = parts
             chrC_set.add(chrC)
             strand_set.add(strand)
             clusters.append((xrange(int(clustStart), int(clustEnd)), annote))
    
    with open(sys.argv[1]) as f1:
         for i, line in enumerate(f1):
             parts = line.split()
             if len(parts) < 6:
                print >>sys.stderr, "%d line has %d parts: %s" % (i, len(parts), line),
                continue
             chrR, chrStart, chrEnd, name, _, strand1 = parts[:6]
             if strand1 == '-' and chrR in chrC_set and strand1 in strand_set:
                lncRNA = xrange(int(chrStart), int(chrEnd))
                for cluster, annote in clusters:
                    if intersect(cluster, lncRNA):
                       print name, annote, 'transcript'
    

