qualia_core_db/q42/chebi_parse/
tsv.rs1use super::error::{bounded_preview, QuarantinedRow};
4use super::record::ChebiRecord;
5
6pub const COMPOUNDS_HEADER: &[&str] = &[
8 "ID",
9 "STATUS",
10 "CHEBI_ACCESSION",
11 "SOURCE",
12 "PARENT_ID",
13 "NAME",
14 "DEFINITION",
15];
16
17pub const REQUIRED_COLUMNS: usize = 7;
19
20pub fn split_tsv_fields(line: &str) -> Vec<&str> {
22 line.split('\t').collect()
23}
24
25pub fn header_matches(fields: &[&str]) -> bool {
27 if fields.len() < REQUIRED_COLUMNS {
28 return false;
29 }
30 COMPOUNDS_HEADER
31 .iter()
32 .zip(fields.iter().take(REQUIRED_COLUMNS))
33 .all(|(expected, got)| *expected == *got)
34}
35
36pub fn accession_ok(accession: &str, id: u64) -> bool {
38 let Some(rest) = accession.strip_prefix("CHEBI:") else {
39 return false;
40 };
41 if rest.is_empty() || !rest.bytes().all(|b| b.is_ascii_digit()) {
42 return false;
43 }
44 match rest.parse::<u64>() {
45 Ok(n) => n == id,
46 Err(_) => false,
47 }
48}
49
50pub fn parse_data_row(source_line: u64, line: &str) -> Result<ChebiRecord, QuarantinedRow> {
62 let fields = split_tsv_fields(line);
63 if fields.len() < REQUIRED_COLUMNS {
64 return Err(QuarantinedRow {
65 source_line,
66 reason: "column_count",
67 raw_preview: bounded_preview(line),
68 });
69 }
70
71 let id_raw = fields[0];
72 let status = fields[1];
73 let accession = fields[2];
74 let source = fields[3];
75 let parent_raw = fields[4];
76 let name = fields[5];
77 let definition = fields[6];
78 let id = match id_raw.parse::<u64>() {
81 Ok(n) => n,
82 Err(_) => {
83 return Err(QuarantinedRow {
84 source_line,
85 reason: "malformed_id",
86 raw_preview: bounded_preview(line),
87 });
88 }
89 };
90
91 if status == "D" {
92 return Err(QuarantinedRow {
93 source_line,
94 reason: "deleted_status",
95 raw_preview: bounded_preview(line),
96 });
97 }
98
99 if !accession_ok(accession, id) {
100 return Err(QuarantinedRow {
101 source_line,
102 reason: "malformed_accession",
103 raw_preview: bounded_preview(line),
104 });
105 }
106
107 let parent_id = if parent_raw.is_empty() {
108 None
109 } else {
110 match parent_raw.parse::<u64>() {
111 Ok(n) => Some(n),
112 Err(_) => {
113 return Err(QuarantinedRow {
114 source_line,
115 reason: "malformed_parent_id",
116 raw_preview: bounded_preview(line),
117 });
118 }
119 }
120 };
121
122 if name.trim().is_empty() {
123 return Err(QuarantinedRow {
124 source_line,
125 reason: "empty_name",
126 raw_preview: bounded_preview(line),
127 });
128 }
129
130 Ok(ChebiRecord {
131 id,
132 status: status.to_string(),
133 accession: accession.to_string(),
134 source: source.to_string(),
135 parent_id,
136 name: name.to_string(),
137 definition: definition.to_string(),
138 source_line,
139 })
140}
141
142#[cfg(test)]
143mod tests {
144 use super::*;
145
146 #[test]
147 fn header_accepts_trailing_columns() {
148 let fields = [
149 "ID",
150 "STATUS",
151 "CHEBI_ACCESSION",
152 "SOURCE",
153 "PARENT_ID",
154 "NAME",
155 "DEFINITION",
156 "EXTRA",
157 ];
158 assert!(header_matches(&fields));
159 }
160
161 #[test]
162 fn accession_must_match_id() {
163 assert!(accession_ok("CHEBI:15377", 15377));
164 assert!(!accession_ok("CHEBI:1", 15377));
165 assert!(!accession_ok("chebi:15377", 15377));
166 assert!(!accession_ok("CHEBI:", 0));
167 }
168
169 #[test]
170 fn empty_parent_is_none() {
171 let line = "10\tC\tCHEBI:10\tsrc\t\twater\tdef";
172 let rec = parse_data_row(2, line).unwrap();
173 assert_eq!(rec.parent_id, None);
174 assert_eq!(rec.name, "water");
175 }
176}