1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
use crate::{
utils::{
expression::CLIexpression,
variable_data::{GOAT_ASSEMBLY_VARIABLE_DATA, GOAT_TAXON_VARIABLE_DATA},
variables::Variables,
},
IndexType,
};
use anyhow::Result;
fn format_rank(r: &str) -> String {
let ranks = vec![
"subspecies",
"species",
"genus",
"family",
"order",
"class",
"phylum",
"kingdom",
"superkingdom",
];
let position_selected = ranks.iter().position(|e| e == &r);
let updated_ranks = match position_selected {
Some(p) => &ranks[p..],
None => return "".to_string(),
};
let mut rank_string = String::new();
rank_string += "&ranks=";
let ranks_to_add = updated_ranks.join("%2C");
rank_string += &ranks_to_add;
rank_string
}
fn format_names(flag: bool) -> String {
match flag {
true => "&names=synonym%2Ctol_id%2Ccommon_name".to_string(),
false => "".to_string(),
}
}
pub fn format_expression(exp: &str, index_type: IndexType) -> Result<String> {
let mut new_exp = CLIexpression::new(exp);
let parsed_string = match index_type {
IndexType::Taxon => new_exp.parse(&*GOAT_TAXON_VARIABLE_DATA)?,
IndexType::Assembly => new_exp.parse(&*GOAT_ASSEMBLY_VARIABLE_DATA)?,
};
Ok(parsed_string)
}
#[derive(Copy, Clone)]
pub struct FieldBuilder {
pub taxon_assembly: bool,
pub taxon_bioproject: bool,
pub taxon_busco: bool,
pub taxon_country_list: bool,
pub taxon_cvalues: bool,
pub taxon_date: bool,
pub taxon_gc_percent: bool,
pub taxon_gene_count: bool,
pub taxon_gs: bool,
pub taxon_karyotype: bool,
pub taxon_legislation: bool,
pub taxon_mitochondrion: bool,
pub taxon_n50: bool,
pub taxon_names: bool,
pub taxon_plastid: bool,
pub taxon_ploidy: bool,
pub taxon_sex_determination: bool,
pub taxon_status: bool,
pub taxon_target_lists: bool,
pub taxon_tidy: bool,
pub assembly_assembly: bool,
pub assembly_karyotype: bool,
pub assembly_contig: bool,
pub assembly_scaffold: bool,
pub assembly_gc: bool,
pub assembly_gene: bool,
pub assembly_busco: bool,
pub assembly_btk: bool,
}
impl FieldBuilder {
fn to_vec_tuples(&self) -> Vec<(bool, Vec<&str>)> {
vec![
(self.taxon_assembly, vec!["assembly_level", "assembly_span"]),
(self.taxon_bioproject, vec!["bioproject", "biosample"]),
(
self.taxon_busco,
vec![
"busco_completeness",
"odb10_lineage",
"busco_lineage",
"busco_string",
],
),
(self.taxon_country_list, vec!["country_list"]),
(self.taxon_cvalues, vec!["c_value"]),
(self.taxon_date, vec!["assembly_date", "ebp_metric_date"]),
(self.taxon_gc_percent, vec!["gc_percent"]),
(self.taxon_gene_count, vec!["gene_count"]),
(
self.taxon_gs,
vec!["genome_size", "genome_size_kmer", "genome_size_draft"],
),
(
self.taxon_karyotype,
vec!["chromosome_number", "haploid_number"],
),
(
self.taxon_legislation,
vec![
"isb_wildlife_act_1976",
"HabReg_2017",
"MarHabReg-2017",
"waca_1981",
"Protection_of_Badgers_Act_1992",
"ECHabs92",
],
),
(
self.taxon_mitochondrion,
vec!["mitochondrion_assembly_span", "mitochondrion_gc_percent"],
),
(self.taxon_n50, vec!["scaffold_n50", "contig_n50"]),
(
self.taxon_plastid,
vec!["plastid_assembly_span", "plastid_gc_percent"],
),
(self.taxon_ploidy, vec!["ploidy"]),
(self.taxon_sex_determination, vec!["sex_determination"]),
(
self.taxon_status,
vec![
"sequencing_status",
"sample_collected",
"sample_acquired",
"in_progress",
"insdc_submitted",
"insdc_open",
"published",
"sample_collected_by",
],
),
(
self.taxon_target_lists,
vec!["long_list", "other_priority", "family_representative"],
),
(
self.assembly_assembly,
vec!["assembly_level", "assembly_span"],
),
(self.assembly_btk, vec!["nohit", "target"]),
(
self.assembly_busco,
vec!["busco_completeness", "busco_lineage", "busco_string"],
),
(
self.assembly_contig,
vec!["contig_count", "contig_l50", "contig_n50"],
),
(self.assembly_gc, vec!["gc_percent"]),
(
self.assembly_gene,
vec!["gene_count", "noncoding_gene_count"],
),
(self.assembly_karyotype, vec!["chromosome_count"]),
(
self.assembly_scaffold,
vec!["scaffold_count", "scaffold_l50", "scaffold_n50"],
),
]
}
pub fn build_fields_string(&self) -> String {
const BASE: &str = "&fields=";
const DELIMITER: &str = "%2C";
let data = self.to_vec_tuples();
let mut field_string = String::new();
field_string += BASE;
for (field_present, field_vec) in data.iter() {
match field_present {
true => {
field_string += &field_vec.join(DELIMITER);
field_string += DELIMITER;
}
false => continue,
}
}
field_string.drain(field_string.len() - 3..);
let any_true = data.iter().map(|e| e.0).any(|e| e);
if !any_true {
field_string.drain(..);
}
field_string
}
fn generate_exculde_flags(&self) -> String {
const ANCESTRAL: &str = "&excludeAncestral";
const MISSING: &str = "&excludeMissing";
const OPEN_ANGLE_BRACE: &str = "%5B";
const CLOSE_ANGLE_BRACE: &str = "%5D";
let data = self.to_vec_tuples();
let mut exclusion_string = String::new();
let mut exclude_index: i32 = 0;
for (field_present, field_vec) in data.iter() {
match field_present {
true => {
for field in field_vec {
exclusion_string += ANCESTRAL;
exclusion_string += OPEN_ANGLE_BRACE;
exclusion_string += &exclude_index.to_string();
exclusion_string += CLOSE_ANGLE_BRACE;
exclusion_string += &format!("={field}");
exclusion_string += MISSING;
exclusion_string += OPEN_ANGLE_BRACE;
exclusion_string += &exclude_index.to_string();
exclusion_string += CLOSE_ANGLE_BRACE;
exclusion_string += &format!("={field}");
exclude_index += 1;
}
}
false => continue,
}
}
exclusion_string
}
}
pub fn make_goat_urls(
api: &str,
taxids: &[String],
goat_url: &str,
tax_tree: &str,
include_estimates: bool,
include_raw_values: bool,
exclude: bool,
summarise_values_by: &str,
result: &str,
taxonomy: &str,
size: u64,
ranks: &str,
fields: FieldBuilder,
variables: Option<&str>,
expression: &str,
tax_rank: &str,
unique_ids: Vec<String>,
index_type: IndexType,
) -> Result<Vec<String>> {
let mut res = Vec::new();
let rank_string = format_rank(ranks);
let fields_string = match variables {
Some(v) => match index_type {
IndexType::Taxon => Variables::new(v).parse(&*GOAT_TAXON_VARIABLE_DATA)?,
IndexType::Assembly => Variables::new(v).parse(&*GOAT_ASSEMBLY_VARIABLE_DATA)?,
},
None => fields.build_fields_string(),
};
let exclude_missing_or_ancestral = if exclude {
match variables {
Some(v) => match index_type {
IndexType::Taxon => Variables::new(v).parse_exclude(&*GOAT_TAXON_VARIABLE_DATA)?,
IndexType::Assembly => Variables::new(v).parse_exclude(&*GOAT_ASSEMBLY_VARIABLE_DATA)?,
},
None => fields.generate_exculde_flags(),
}
} else {
"".into()
};
let names = format_names(fields.taxon_names);
let tidy_data: &str = match fields.taxon_tidy {
true => "&tidyData=true",
false => "",
};
for (taxon, chars) in taxids.iter().zip(unique_ids.iter()) {
let query_id = format!("&queryId=goat_cli_{}", chars);
let url = format!(
"{goat_url}{api}?query=tax_{tax_tree}%28{taxon}%29{tax_rank}{expression}&includeEstimates={include_estimates}&includeRawValues={include_raw_values}&summaryValues={summarise_values_by}&result={result}&taxonomy={taxonomy}&size={size}{rank_string}{fields_string}{tidy_data}{names}{query_id}{exclude_missing_or_ancestral}"
);
res.push(url);
}
Ok(res)
}