Skip to content

Instantly share code, notes, and snippets.

@Abusagit
Forked from dceoy/read_vcf.py
Created June 4, 2021 15:46
Show Gist options
  • Save Abusagit/c3fecc625558e3901b96510f4418f45a to your computer and use it in GitHub Desktop.
Save Abusagit/c3fecc625558e3901b96510f4418f45a to your computer and use it in GitHub Desktop.
[Python] Read VCF (variant call format) as pandas.DataFrame
#!/usr/bin/env python
import io
import os
import pandas as pd
def read_vcf(path):
with open(path, 'r') as f:
lines = [l for l in f if not l.startswith('##')]
return pd.read_csv(
io.StringIO(''.join(lines)),
dtype={'#CHROM': str, 'POS': int, 'ID': str, 'REF': str, 'ALT': str,
'QUAL': str, 'FILTER': str, 'INFO': str},
sep='\t'
).rename(columns={'#CHROM': 'CHROM'})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment