Skip to content

Instantly share code, notes, and snippets.

@dridk
Created December 7, 2022 12:47
Show Gist options
  • Save dridk/65c9126dccebeb0de0be76623f0596bf to your computer and use it in GitHub Desktop.
Save dridk/65c9126dccebeb0de0be76623f0596bf to your computer and use it in GitHub Desktop.
use vcf::{VCFReader,VCFRecord};
use std::fs::File;
use std::fs::remove_file;
use std::io::BufReader;
use std::str::from_utf8;
use duckdb::{params, Connection};
#[derive(Debug)]
struct Variant {
chrom: String,
pos: u64,
reference: String,
alternative: String
}
fn main() {
let file = File::open("test.snpeff.vcf").expect("cannot read file");
let mut reader = VCFReader::new(BufReader::new(file)).expect("cannot read vcf");
remove_file("/tmp/test.db").expect("File don't exists");
let conn = Connection::open("/tmp/test.db").expect("cannot open database");
conn.execute_batch("
CREATE TABLE variant (
chrom TEXT,
pos INTEGER,
reference TEXT,
alternative TEXT
);
").expect("cannot create table");
let mut record: VCFRecord = reader.empty_record();
loop {
let success = reader.next_record(&mut record).unwrap_or(false);
let v = Variant {
chrom :from_utf8(&record.chromosome).unwrap_or("N").to_string(),
pos :record.position,
reference :from_utf8(&record.reference).unwrap_or("N").to_string(),
alternative :from_utf8(&record.alternative[0]).unwrap_or("N").to_string(),
};
conn.execute(
"INSERT INTO variant (chrom, pos, reference, alternative) VALUES (?, ?, ?, ?)",
params![v.chrom, v.pos, v.reference, v.alternative],
).expect("cannot insert");
if !success {break};
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment