Searching a file with the contents of another file python
Question: I have a file that has a unique ID number on each line. I am trying to search
a different file for the occurrences of these ID numbers and return the line
where these id numbers are in the second file, in this case into an output
file. I am new to programming and this is what I have so far.

    
    
    outlist = []
    with open('readID.txt', 'r') as readID, \
         open('GOlines.txt', 'w') as output, \
         open('GO.txt', 'r') as GO:  
         x = readID.readlines()
         print x
         for line in GO:
            if x[1:-1] in line:
            outlist.append(line)
            outlist.append('\n')
    
         if x[1:-1] in line:
            outlist.append(line)
            outlist.append('\n')
         print outlist
         output.writelines(outlist)
    

The files look like this: readID.txt

    
    
    00073810.1
    00082422.1
    00018647.1
    00063072.1
    

GO.txt

    
    
    #query  GO  reference DB    reference family    
    HumanDistalGut_READ_00048904.2  GO:0006412  TIGRFAM TIGR00001    
    HumanDistalGut_READ_00043244.3  GO:0022625  TIGRFAM TIGR00001    
    HumanDistalGut_READ_00048644.4  GO:0000315  TIGRFAM TIGR00001   
    HumanDistalGut_READ_00067264.5  GO:0003735  TIGRFAM TIGR00001
    

The read ids match up with some but not all of the ids after _READ_...


Answer: 
    #!/usr/bin/env python
    # encoding: utf-8
    
    import sys
    import re
    
    def extract_id(line):
        """
        input: HumanDistalGut_READ_00048904.2  GO:0006412  TIGRFAM TIGR00001
        returns: 00048904.2
        """
        result = re.search(r'READ_(\d{8}\.\d)', line)
        if result != None:
            return result.group(1)
        else:
            return None
    
    def extract_go_num(line):
        """
        input: HumanDistalGut_READ_00048904.2  GO:0006412  TIGRFAM TIGR00001
        returns: 0006412
        """
        result = re.search(r'GO:(\d{7})', line)
        if result != None:
            return result.group(1)
        else:
            return None
    
    def main(argv = None):
        if argv is None:
            argv = sys.argv
    
        with open('readID.txt', 'r') as f:
            ids = frozenset(f.readlines())
    
        with open('GO.txt', 'r') as haystack, \
            open('GOLines.txt', 'w') as output:
    
            for line in haystack:
                if extract_id(line) in ids:
                    output.write(extract_go_num(line) + '\n')
    
    if __name__ == "__main__":
        sys.exit(main())
    

I'm trading memory overhead for an O(n) solution rather than O(n^2).

I'm using regular expressions to extract the ids and go numbers, but it's
brittle if the number of digits change.

