use std::collections::HashMap;
use std::io;
use serde_json::json;
use crate::errors;
use crate::io::fasta;
use crate::taxon::TaxonId;
#[derive(Debug, StructOpt)]
#[structopt(verbatim_doc_comment)]
pub struct TaxaToTree {
#[structopt(short = "u", long = "url")]
pub url: bool,
}
pub fn taxa2tree(args: TaxaToTree) -> errors::Result<()> {
let mut taxa = HashMap::new();
for record in fasta::Reader::new(io::stdin(), false).records() {
let record = record?;
let taxon = record.sequence[0].parse::<TaxonId>()?;
*taxa.entry(taxon).or_insert(0) += 1;
}
let json = json!({
"counts": taxa,
"link": args.url.to_string(),
});
let res = attohttpc::post("http://api.unipept.ugent.be/api/v1/taxa2tree")
.json(&json)
.map_err(|err| err.to_string())?
.send()
.map_err(|err| err.to_string())?;
if args.url {
let jsonres: serde_json::Value = res.json().map_err(|err| err.to_string())?;
let gist = jsonres
.get("gist")
.expect("Incompatible server API")
.as_str()
.expect("Incompatible server API");
println!(
"{}",
gist.replace("https://gist.github.com/", "https://bl.ocks.org/")
)
} else {
print!("{}", res.text().map_err(|err| err.to_string())?);
}
Ok(())
}