extern crate fastobo;
extern crate fastobo_graphs;
extern crate serde_json;
extern crate serde_yaml;
use std::fs::File;
use std::io::Write;
use std::path::PathBuf;
use fastobo::visit::VisitMut;
use fastobo_graphs::model::GraphDocument;
use fastobo_graphs::FromGraph;
fn main() {
for path in std::env::args().skip(1) {
let srcpath = PathBuf::from(&path);
let dstpath = srcpath.with_extension("obo");
let srcfile = File::open(&srcpath).unwrap();
let doc = match srcpath.extension() {
Some(s) if s == "json" => {
match serde_yaml::from_reader::<File, GraphDocument>(srcfile) {
Ok(doc) => doc,
Err(e) => panic!("{} could not be parsed:\n{}", path, e),
}
}
Some(s) if s == "yaml" => {
match serde_yaml::from_reader::<File, GraphDocument>(srcfile) {
Ok(doc) => doc,
Err(e) => panic!("{} could not be parsed:\n{}", path, e),
}
}
Some(other) => panic!("unknown file extension: {:?}", other),
None => panic!("can't determine input file type from extension"),
};
if doc.graphs.len() > 1 {
panic!("input file contains more than one graph");
}
let graph = doc.graphs.into_iter().next().unwrap();
let mut obodoc =
fastobo::ast::OboDoc::from_graph(graph).expect("could not convert from graph");
fastobo::visit::IdCompactor::new().visit_doc(&mut obodoc);
File::create(&dstpath)
.and_then(|mut f| write!(f, "{}", obodoc))
.expect("could not write output file");
}
}