Search code examples
pythonlistfunctionvcf-variant-call-format

All outputs from function are the same


I have a function to read VCF files

def readVCF(VCF):
    VCF = VCF.readlines()
    header = [] 
    data = []
    for line in VCF:
        if line.startswith('#'):
            header.append(line) 
        else:
            data.append(line)
    VCF_contents =[]  
    for line in data:
        VCF_contents.extend(line.strip().split('\t'))
    for i in range(0, len(VCF_contents), 10): 
        lines.append(VCF_contents[i : i+10]) 

The function works as I want it to, but problems start when I use it more than once.

I want to use this function multiple times, on different files, and the use the outputs from all these different files simultaneously. I have been using the function like this:

VCF = open("frogs.txt", 'r')
lines = []
readVCF(VCF)
frogs = lines
VCF.close

VCF = open("rabbits.txt.", 'r')
readVCF(VCF)
rabbits = lines
VCF.close

VCF = open("lizards.txt", 'r')
readVCF(VCF)
lizards = lines
VCF.close

The problem is that, all my outputs are the same, and all have the output that comes from frogs.txt, the first file I used the function for. How can reuse this function without getting the same values?


Solution

  • Possibly, you want to add:

    lines = []
    

    inside your function at the beginning and:

    return lines
    

    at the end of your function, and call it like e.g.:

    VCF = open("frogs.txt", 'r')
    frogs = readVCF(VCF)
    VCF.close()
    

    or alternatively (probably better):

    with open("frogs.txt", 'r') as VCF:
        frogs = readVCF(VCF)