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

Copy header from one VCF to another #194

Open
multimeric opened this issue Mar 8, 2021 · 1 comment
Open

Copy header from one VCF to another #194

multimeric opened this issue Mar 8, 2021 · 1 comment

Comments

@multimeric
Copy link

My use case is that I'm writing a VCF merge tool. For that reason, I want the output VCF to have the union of all input VCF headers (INFO and FORMAT, mostly). Unfortunately this is tricky to do in cyvcf2. We have add_info_to_header and co, as well as add_to_header. However, add_info_to_header takes a dictionary and not a HREC object we get from header_iter(), nor is there a way to convert an HREC to a dict. On the other hand, add_to_header(str(some_hrec)) seems like it should work, but it just gives me the error Exception: couldn't add '%s' to header. My current example code is:

in_vcf = cyvcf2.VCF(some_path)
out_vcf = cyvcf2.Writer(some_path, tmpl=some_other_vcf)
for field in in_vcf.header_iter():
    if field.type in ('FORMAT', 'INFO') and not out_vcf.contains(field['ID']):
        out_vcf.add_to_header(str(field))

The error I get here is:

  File "/some/path/vcf_transform/vcf_transform.py", line 83, in merge_vcfs
    out.add_to_header(str(field))
  File "cyvcf2/cyvcf2.pyx", line 297, in cyvcf2.cyvcf2.VCF.add_to_header
Exception: couldn't add '%s' to header
python-BaseException

Is there a working way to copy headers from one VCF to the output?

@CholoTook
Copy link

Do you need to call str(field)? Shouldn't add_to_header expect a header line object?

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

2 participants