Skip to content

Commit

Permalink
wip: starting from_tsv implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
proycon committed Mar 27, 2023
1 parent c25899d commit 2071936
Show file tree
Hide file tree
Showing 2 changed files with 70 additions and 5 deletions.
4 changes: 2 additions & 2 deletions src/bin/stam/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,14 @@ mod annotate;
mod info;
mod tag;
mod to_text;
mod to_tsv;
mod tsv;
mod validate;

use crate::annotate::*;
use crate::info::*;
use crate::tag::*;
use crate::to_text::*;
use crate::to_tsv::*;
use crate::tsv::*;
use crate::validate::*;

const VERSION: &'static str = env!("CARGO_PKG_VERSION");
Expand Down
71 changes: 68 additions & 3 deletions src/bin/stam/to_tsv.rs → src/bin/stam/tsv.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ use stam::{
Storable, TextResource, TextResourceHandle, TextSelection,
};
use std::fmt;
use std::fs::File;
use std::io::{BufRead, BufReader};
use std::process::exit;

pub fn tsv_arguments<'a>() -> Vec<clap::Arg<'a>> {
Expand Down Expand Up @@ -500,9 +502,9 @@ impl Column {
}
}

struct ColumnConfig(Vec<Column>);
struct Columns(Vec<Column>);

impl ColumnConfig {
impl Columns {
fn printrow(
&self,
store: &AnnotationStore,
Expand Down Expand Up @@ -540,7 +542,7 @@ pub fn to_tsv(
null: &str,
header: bool,
) {
let columns = ColumnConfig(
let columns = Columns(
columnconfig
.iter()
.map(|col| {
Expand Down Expand Up @@ -689,3 +691,66 @@ pub fn to_tsv(
}
}
}

pub fn from_tsv(
store: &mut AnnotationStore,
filename: &str,
columns: Vec<&str>,
tp: Option<Type>,
flattened: bool,
delimiter: &str,
null: &str,
has_header: bool,
) {
let f = File::open(filename).unwrap_or_else(|e| {
eprintln!("Error opening rules {}: {}", filename, e);
exit(1)
});
let reader = BufReader::new(f);

let mut columns = Columns(
columns
.iter()
.map(|col| {
Column::try_from(*col)
.map_err(|err| {
eprintln!("{}", err);
exit(1);
})
.unwrap()
})
.collect(),
);

for (i, line) in reader.lines().enumerate() {
if let Ok(line) = line {
if line.is_empty() {
continue;
} else if i == 0 && (has_header || line.starts_with('#')) {
//assume a first line with a comment is a header
let headerline = if line.starts_with('#') {
line[1..].to_string()
} else {
line
};
columns = Columns(
headerline
.split("\t")
.map(|col| {
Column::try_from(col)
.map_err(|err| {
eprintln!("{}", err);
exit(1);
})
.unwrap()
})
.collect(),
);
continue;
} else if line.starts_with('#') {
continue;
}
//TODO
}
}
}

0 comments on commit 2071936

Please sign in to comment.