matching headers in fasta files with python
Question: I have two files: the first is a fasta file with a header and sequence and the
second is composed of only headers.

File_1:

    
    
    >DF94KKQ1|265|D0M1LACXX|3|2103|4637|10742|1|N|0|TGACCA
    TTCCAAAGAAACATGGAAGACCCAGGACTTGGAGGCACCAGGCACCAGCACACAGGGGTA
    GGCACATGGCATGGTGTTGGTTGAAGTCTACTTTTCCCACC
    >DF94KKQ1|265|D0M1LACXX|3|2103|4565|10742|1|N|0|TGACCA
    TTAATTTTTTCAGGCAAGTTTTGTGGATTTCAGTGTGTAAGTCTTTCACCTCTTTGGTTA
    AATTTATTCCTATGTATTTTATTCCTTTAGATGCTATTATG
    >DF94KKQ1|265|D0M1LACXX|3|2103|4565|10742|2|N|0|TGACCA
    TTAATTTTTTCAGGCAAGTTTTGTGGATTTCAGTGTGTAAGTCTTTCACCTCTTTGGTTA
    AATTTATTCCTATGTATTTTATTCCTTTAGATGCTATTATG
    

File_2:

    
    
    >DF94KKQ1|265|D0M1LACXX|3|2103|4565|10742|2|N|0|TGACCA
    >DF94KKQ1|265|D0M1LACXX|3|2103|4668|10746|1|N|0|TGACCA
    >DF94KKQ1|265|D0M1LACXX|3|2103|4668|10746|2|N|0|TGACCA
    >DF94KKQ1|265|D0M1LACXX|1|2207|10852|3331|2|N|0|TGACCA
    

I want to match the header in File_2 with anything in File_1 that has the same
exact characters up until the 7th '|'.

I split the items in File_1 (each section of the header is indexed into a
list). Anyline that begins with a '>' is placed into a variable:

    
    
    #!/usr/bin/env python
    import sys
    from Bio import SeqIO
    
    #Function, split header line into a list
    def getHeaderInfo(blastLine):
       myFields = blastLine.strip("\n").split("|") 
       HeaderInfo = myFields[:6]
       return HeaderInfo
    
    input_file = sys.argv[1]
    
    #Get input file from the command line
    inFileName = sys.argv[1]
    
    #open the input file
    inFileHandle = open(inFileName)
    
    #loop over the input file line by line
    for thisLine in inFileHandle.readlines():
        if thisLine [0] == '>': 
           print getHeaderInfo(thisLine)
           HeaderInfo = getHeaderInfo(thisLine)
    

I have been trying to find a method in which I can compare these same indexes
in File_2 to return the following output:

    
    
    >DF94KKQ1|265|D0M1LACXX|3|2103|4565|10742|1|N|0|TGACCA
    TTAATTTTTTCAGGCAAGTTTTGTGGATTTCAGTGTGTAAGTCTTTCACCTCTTTGGTTA
    AATTTATTCCTATGTATTTTATTCCTTTAGATGCTATTATG
    >DF94KKQ1|265|D0M1LACXX|3|2103|4565|10742|2|N|0|TGACCA
    TTAATTTTTTCAGGCAAGTTTTGTGGATTTCAGTGTGTAAGTCTTTCACCTCTTTGGTTA
    AATTTATTCCTATGTATTTTATTCCTTTAGATGCTATTATG
    

Several of the methods I have tried use an index, however, my key is not
unique. How can I take the first six elements and make them my key or is there
a better method than the current one I am attempting? Thank you.


Answer: Does this do what you want?

    
    
    def make_key(line):
        return "|".join(line.split("|", 7)[ : 7]) + "|"
    
    header_set = set()
    with open("file_2.txt") as in_f:
        for line in in_f:
            header_set.add(make_key(line))
    
    with open("file_1.txt") as in_f, open("file_3.txt", "w") as out_f:
        accept = False
        for line in in_f:
            if line.startswith(">"):
                key = make_key(line)
                accept = key in header_set
    
            if accept:
                out_f.write(line)
    

