Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

taxa2lca can accept a vector of ints or a vector of strings as input #57

Open
wants to merge 1 commit into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions api/src/controllers/api/mod.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
use serde::Deserialize;

pub mod pept2ec;
pub mod pept2funct;
pub mod pept2go;
Expand All @@ -11,6 +13,19 @@ pub mod taxa2lca;
pub mod taxa2tree;
pub mod taxonomy;

#[derive(Deserialize)]
#[serde(untagged)]
pub enum EitherVec<T, U> {
Left(Vec<T>),
Right(Vec<U>)
}

impl<T, U> Default for EitherVec<T, U> {
fn default() -> Self {
EitherVec::Left(Vec::new())
}
}

pub fn default_equate_il() -> bool {
true
}
Expand Down
10 changes: 8 additions & 2 deletions api/src/controllers/api/taxa2lca.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,12 @@ use crate::{
},
AppState
};
use crate::controllers::api::EitherVec;

#[derive(Deserialize)]
pub struct Parameters {
#[serde(default)]
input: Vec<u32>,
input: EitherVec<u32, String>,
#[serde(default = "default_extra")]
extra: bool,
#[serde(default = "default_names")]
Expand Down Expand Up @@ -49,8 +50,13 @@ async fn handler(
let taxon_store = datastore.taxon_store();
let lineage_store = datastore.lineage_store();

let casted_input: Vec<u32> = match input {
EitherVec::Left(numbers) => numbers,
EitherVec::Right(strings) => strings.iter().map(|s| s.parse().unwrap_or_default()).collect()
};

// Calculate the LCA of all taxa
let lca: i32 = calculate_lca(input, version, taxon_store, lineage_store);
let lca: i32 = calculate_lca(casted_input, version, taxon_store, lineage_store);

if let Some((taxon_name, taxon_rank, _)) = taxon_store.get(lca as u32) {
// Calculate the lineage of the LCA
Expand Down