Read multiple VCF files through PyVCF
1
0
Entering edit mode
2.8 years ago
anasjamshed ▴ 140

I am trying this code to read multiple vcf files present in 2 different folders:

import os
import pandas as pd
import vcf

# Folder Path
path1 = "C://Users//USER//Desktop//Anas/VCFs_1/"
path2 = "C://Users//USER//Desktop//Anas/VCFs_2/"
#os.chdir(path1)


def read(f1,f2):
    reader = vcf.Reader(open(f1,f2))
    df = pd.DataFrame([vars(r) for r in reader])
    out = df.merge(pd.DataFrame(df.INFO.tolist()),
                   left_index=True, right_index=True)
    return out
# Read text File


def read_text_file(file_path1,file_path2):
    with open(file_path1, 'r') as f:
        with open(file_path2,'r') as f:
            print(read(path1,path2))


# iterate through all file
for file in os.listdir():
    # Check whether the file is in text format or not
    if file.endswith(".vcf"):
        file_path1 = f"{path1}\{file}"
        file_path2 = f"{path2}\{file}"
        print(file_path1,"\n\n",file_path2)

But it's giving me a permission error? I have also tried to execute code with admin privileges but nothing happened. There is something wrong with the code.

Can anyone fix it?

python • 1.3k views
ADD COMMENT
0
Entering edit mode
2.8 years ago
liorglic ★ 1.4k

There are a few strange things with your code, although I'm not sure if any of them should cause a permission error...

  • os.listdir() takes at least one argument - the directory to be listed.
  • for file in os.listdir... - file is a reserved word in python. Don't use it as a variable name.
  • open(f1,f2) - as far as I know open() can't take two files like that.
  • I don't see the functions being called anywhere...

Is this the actual code you are running?

ADD COMMENT
0
Entering edit mode

It's not actual code.I am trying to modify other code so It could be able to read multiple vcf files present in 2 different folders. Please help to correct it

ADD REPLY
0
Entering edit mode

can anyone help me to correct this code?

I am trying this now:

# Import Module
import os
import vcf
# Folder Path
path1 = "C://Users//USER//Desktop//isiah/VCFs_1/"
path2 = "C://Users//USER//Desktop//isiah/VCFs_2/"

def read(f1,f2):
    reader1 = vcf.Reader(open(f1))
    reader2 = vcf.Reader(open(f2))
    df1 = pd.DataFrame([vars(r) for r in reader1])
    out1 = df1.merge(pd.DataFrame(df1.INFO.tolist()),
                   left_index=True, right_index=True)
    df2 = pd.DataFrame([vars(r) for r in reader2])
    out2 = df2.merge(pd.DataFrame(df2.INFO.tolist()),
                   left_index=True, right_index=True)
    return out1,out2

read(path1,path2)

but still i am getting permission error?

ADD REPLY

Login before adding your answer.

Traffic: 2548 users visited in the last hour
Help About
FAQ
Access RSS
API
Stats

Use of this site constitutes acceptance of our User Agreement and Privacy Policy.

Powered by the version 2.3.6