Skip to content

Commit

Permalink
🚧 add description scraper
Browse files Browse the repository at this point in the history
📝 fix borrows

🐛 fix quote injection
  • Loading branch information
dmiller15 committed Jan 31, 2024
1 parent d4ab3a8 commit 8b8059b
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 4 deletions.
24 changes: 23 additions & 1 deletion src/commands/encoder_cmd.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use bincode::Options;
use echtvar_lib::{echtvar::bstrip_chr, fields, kmer16, var32, zigzag};
use rust_htslib::bcf::header::{TagLength, TagType};
use rust_htslib::bcf::header::{TagLength, TagType, HeaderRecord};
use rust_htslib::bcf::record::{Buffer, Record};
use rust_htslib::bcf::{Read as BCFRead, Reader};
use stream_vbyte::{encode::encode, x86::Sse41};
Expand Down Expand Up @@ -160,6 +160,25 @@ fn is_sorted<T: std::cmp::PartialOrd>(data: &Vec<T>) -> bool {
return true;
}

fn hdr_info_id2description(
mut hrecs: Vec<HeaderRecord>,
id: &String,
default: &std::string::String,
) -> std::string::String {
hrecs.retain(|rec| match rec {
HeaderRecord::Info {key: _, values: v} => &v["ID"] == id,
_ => false}
);
if hrecs.len() != 1 {
panic!("Field {} is either not present in the header or present multiple times!", id);
};
let description = match hrecs.first().unwrap() {
HeaderRecord::Info {key: _, values: v} => if v.contains_key("Description") { &v["Description"] } else { default },
_ => default,
};
return description.to_string();
}

pub fn encoder_main(vpaths: Vec<&str>, opath: &str, jpath: &str) {
let zpath = std::path::Path::new(opath);
let jpath = std::path::Path::new(jpath);
Expand Down Expand Up @@ -228,6 +247,9 @@ pub fn encoder_main(vpaths: Vec<&str>, opath: &str, jpath: &str) {
tl, f.field
),
};
println!("Old description for field {}: {}", f.field, f.description);
f.description = hdr_info_id2description(header.header_records(), &f.field, &f.description);
println!("New description for field {}: {}", f.field, f.description);
}

let zfile = std::fs::File::create(&zpath).unwrap();
Expand Down
6 changes: 3 additions & 3 deletions src/lib/echtvar.rs
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,7 @@ impl EchtVars {
for e in &self.fields {
header.push_record(
format!(
"##INFO=<ID={},Number={},Type={},Description=\"{}\">",
"##INFO=<ID={},Number={},Type={},Description={}>",
e.alias,
if vec!["A", "R", "G"].iter().any(|n| n == &e.number) {
"1"
Expand All @@ -191,8 +191,8 @@ impl EchtVars {
} else {
"Float"
},
if &e.description.to_string() == "added by echtvar" {
format!("added by echtvar from {}", path)
if &e.description.to_string() == "added by echtvar"{
format!("\"added by echtvar from {}\"", path)
} else {
format!("added by echtvar {}", e.description.to_string())
}
Expand Down

0 comments on commit 8b8059b

Please sign in to comment.