goat_cli/lookup/
mod.rs

1//!
2//! Invoked by calling:
3//! `goat-cli taxon/assembly lookup <args>`
4
5use crate::client::GoatClient;
6use crate::error::Result;
7use crate::cli::LookupArgs;
8use crate::output::Table;
9use crate::IndexType;
10use futures::StreamExt;
11use serde_json::Value;
12
13/// The inner structs used in lookup.
14pub mod lookup;
15use lookup::{AssemblyCollector, Collector, Lookups, TaxonCollector};
16
17pub enum LookupAction {
18    Continue,
19    PrintedAndExit,
20}
21
22/// Main entry point for `goat-cli lookup`.
23pub async fn lookup(
24    args: &LookupArgs,
25    cli: bool,
26    index_type: IndexType,
27) -> Result<LookupAction> {
28    let lookups = Lookups::new(args, index_type)?;
29    let url_vector_api = lookups.make_urls();
30    let size = args.size;
31
32    if args.url {
33        for (index, (url, _)) in url_vector_api.iter().enumerate() {
34            crate::outln!("{}.\tGoaT lookup API URL: {}", index, url)?;
35        }
36        return Ok(LookupAction::PrintedAndExit);
37    }
38
39    let concurrent_requests = url_vector_api.len();
40
41    let client = GoatClient::new();
42    let fetches = futures::stream::iter(
43        url_vector_api
44            .into_iter()
45            .enumerate()
46            .map(|(idx, (path, search_query))| {
47                let client = client.clone();
48                async move {
49                    let v: Value = client.get_json(&path).await?;
50                            // print a warning if number of hits > size specified.
51                            let request_size_op = &v["status"]["hits"].as_u64();
52                            match request_size_op {
53                                Some(s) => {
54                                    if size < *s {
55                                        eprintln!(
56                                            "For search query {}, size specified ({}) was less than the number of results returned, ({}).",
57                                            search_query, size, s
58                                        )
59                                    }
60                                }
61                                None => (),
62                            }
63
64                            // get all the suggestions
65                            let suggestions_text_op = &v["suggestions"].as_array();
66                            // collect into a vec
67                            let mut suggestions_vec = Vec::new();
68                            let suggestions_text = match suggestions_text_op {
69                                Some(suggestions) => {
70                                    for el in *suggestions {
71                                        let sug_str = el["suggestion"]["text"].as_str();
72                                        let sug_string_op = sug_str.map(String::from);
73                                        suggestions_vec.push(sug_string_op);
74                                    }
75                                    Some(suggestions_vec.clone())
76                                }
77                                None => None,
78                            };
79
80                            // we have all the information to process the results
81                            let collector = match index_type {
82                                IndexType::Taxon => Collector::Taxon(process_taxon_results(
83                                    v,
84                                    search_query,
85                                    suggestions_text,
86                                )),
87                                IndexType::Assembly => Collector::Assembly(
88                                    process_assembly_results(v, search_query, suggestions_text),
89                                ),
90                            };
91
92                    Ok((idx, collector))
93                }
94            }),
95    )
96    .buffer_unordered(crate::client::concurrency(concurrent_requests))
97    .collect::<Vec<_>>();
98
99    let awaited_fetches = fetches.await;
100
101    let mut ordered_results = Vec::new();
102    for el in awaited_fetches {
103        match el {
104            Ok(v) => ordered_results.push(v),
105            Err(e) => return Err(e),
106        }
107    }
108
109    ordered_results.sort_by_key(|(idx, _)| *idx);
110
111    if cli {
112        let mut table = Table::new(match index_type {
113            IndexType::Taxon => TaxonCollector::HEADER,
114            IndexType::Assembly => AssemblyCollector::HEADER,
115        });
116        for (_original_idx, collector) in ordered_results {
117            match collector {
118                Collector::Taxon(e) => e.add_rows(&mut table),
119                Collector::Assembly(e) => e.add_rows(&mut table),
120            }
121        }
122        table.print(args.format)?;
123    }
124
125    Ok(LookupAction::Continue)
126}
127
128/// As the taxon and assembly return JSON's are in
129/// different structures, they have to be parsed differently.
130///
131/// Each must return [`Result<Collector, anyhow::Error>`].
132fn process_taxon_results(
133    v: Value,
134    search_query: String,
135    suggestions_text: Option<Vec<Option<String>>>,
136) -> TaxonCollector {
137    // and the taxon ID
138    // we need to iterate over the array of results.
139    // potentially look at the scores, and keep those over a certain amount
140    // or keep everything. Currently I'm missing synonymous genera.
141
142    let mut taxon_id_vec = Vec::new();
143    let mut taxon_rank_vec = Vec::new();
144    let mut taxon_names_array_vec = Vec::new();
145
146    let results_array = v["results"].as_array();
147    // unwrap safely here
148    if let Some(arr) = results_array {
149        for el in arr {
150            let taxon_id = el["result"]["taxon_id"].as_str();
151            let taxon_rank = el["result"]["taxon_rank"].as_str();
152            let taxon_names_array_op = el["result"]["taxon_names"].as_array();
153
154            let taxon_names_array = match taxon_names_array_op {
155                Some(vec) => {
156                    let mut collect_names = Vec::new();
157                    for el in vec.iter() {
158                        let key = el["name"].as_str().unwrap_or("-");
159                        let value = el["class"].as_str().unwrap_or("-");
160                        // let source = el["source"].as_str().unwrap_or("-");
161                        collect_names.push((key.to_string(), value.to_string()));
162                    }
163                    Some(collect_names)
164                }
165                None => None,
166            };
167
168            // gather results into the vecs
169            taxon_id_vec.push(taxon_id);
170            taxon_rank_vec.push(taxon_rank);
171            taxon_names_array_vec.push(taxon_names_array);
172        }
173    }
174
175    // Vec<Option<&str>> -> Vec<Option<String>>
176    let taxon_id = taxon_id_vec.iter().map(|e| e.map(String::from)).collect();
177    let taxon_rank = taxon_rank_vec.iter().map(|e| e.map(String::from)).collect();
178
179    TaxonCollector {
180        search: Some(search_query),
181        suggestions: suggestions_text,
182        taxon_id,
183        taxon_names: taxon_names_array_vec,
184        taxon_rank,
185    }
186}
187
188/// The assembly counterpart to the above function.
189fn process_assembly_results(
190    v: Value,
191    search_query: String,
192    suggestions_text: Option<Vec<Option<String>>>,
193) -> AssemblyCollector {
194    // taxon ID stays the same
195    let mut taxon_id_vec = Vec::new();
196    // there is no taxon rank
197    let mut identifiers_array_vec = Vec::new();
198
199    let results_array = v["results"].as_array();
200    // unwrap safely here
201    if let Some(arr) = results_array {
202        for el in arr {
203            let taxon_id = el["result"]["taxon_id"].as_str();
204            let identifiers_array_op = el["result"]["identifiers"].as_array();
205
206            let identifiers_array = match identifiers_array_op {
207                Some(vec) => {
208                    let mut collect_names = Vec::new();
209                    for el in vec.iter() {
210                        let key = el["identifier"].as_str().unwrap_or("-");
211                        let value = el["class"].as_str().unwrap_or("-");
212                        // let source = el["source"].as_str().unwrap_or("-");
213                        collect_names.push((key.to_string(), value.to_string()));
214                    }
215                    Some(collect_names)
216                }
217                None => None,
218            };
219
220            // gather results into the vecs
221            taxon_id_vec.push(taxon_id);
222            identifiers_array_vec.push(identifiers_array);
223        }
224    }
225
226    // Vec<Option<&str>> -> Vec<Option<String>>
227    let taxon_id = taxon_id_vec.iter().map(|e| e.map(String::from)).collect();
228
229    AssemblyCollector {
230        search: Some(search_query),
231        suggestions: suggestions_text,
232        taxon_id,
233        identifiers: identifiers_array_vec,
234    }
235}