Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

in _serialize_record row = [record.CHROM, record.POS] AttributeError: 'str' object has no attribute 'CHROM' #153

Open
ShadiKhoury opened this issue Jan 18, 2023 · 0 comments

Comments

@ShadiKhoury
Copy link

  • vcfpy version: 0.13.6
  • Python version:3.7.6
  • Operating System:Linux

Description

I'm trying to use vcfpy yo convert a CSV file into VCF the script : import pandas as pd
import vcfpy
import sys

def csv_to_vcf(csv_file, vcf_file):

# read CSV file using pandas
df = pd.read_csv(csv_file)
# read vcf file
reader = vcfpy.Reader.from_path(vcf_file)
# create a new VCF file
vcf_writer = vcfpy.Writer.from_path(vcf_file,reader.header)

# add the header information to the VCF file


# loop through rows in the CSV file
for i, row in df.iterrows():
    # create a new record for the VCF file
    record = vcfpy.Record(
        CHROM=row['#CHROM'],
        POS=row['POS'],
        ID=".",
        REF=row['REF'],
        ALT=".",
        QUAL=".",
        FILTER=".",
        INFO=".",
        FORMAT=[])
    
    chr = row['#CHROM']
    pos = int(row['POS'])
    ref = row['REF']
    alt = row['ALT']

    chr='chr'+str(chr)
    if chr == 'chrMT':
        chr = 'chrM'
    try:
        fetched = reader.fetch(record.CHROM,pos-5,pos+5)
    
    except:
        fetched = '.|.'
    for fet in fetched:
    # add the record to the VCF file
        vcf_writer.write_record(fet)
            



# close the VCF file
vcf_writer.close()

if name == "main":
csv_file = sys.argv[1]
vcf_file = sys.argv[2]
csv_to_vcf(csv_file,vcf_file)

What I Did

python csv_to_vcf.py Cmpd_Hetro_Bathc1_morethan10test.csv results_table.DVD.vcf

where the Cmpd is the CSV file and the VCF file already has a header.

If there was a crash, please include the traceback here.

Error : 
/vcfpy/writer.py", line 135, in _serialize_record
    row = [record.CHROM, record.POS]
AttributeError: 'str' object has no attribute 'CHROM'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant