Skip to content

Commit

Permalink
Merge pull request #46 from unipept/fix/optimise-calls-to-sa
Browse files Browse the repository at this point in the history
Optimise calls to sa
  • Loading branch information
pverscha authored Aug 23, 2024
2 parents 1db0971 + 82257b9 commit e38c854
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 13 deletions.
36 changes: 27 additions & 9 deletions api/src/controllers/api/pept2ec.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use std::collections::HashMap;
use axum::{extract::State, Json};
use serde::{Deserialize, Serialize};

Expand Down Expand Up @@ -36,20 +37,37 @@ async fn handler(
Parameters { input, equate_il, extra }: Parameters
) -> Result<Vec<EcInformation>, ()> {
let input = sanitize_peptides(input);
let result = index.analyse(&input, equate_il, None);

let mut peptide_counts: HashMap<String, usize> = HashMap::new();
for peptide in input.into_iter() {
*peptide_counts.entry(peptide).or_insert(0) += 1;
}

let unique_peptides: Vec<String> = peptide_counts.keys().cloned().collect();
let result = index.analyse(&unique_peptides, equate_il, None);

let ec_store = datastore.ec_store();

Ok(result
.into_iter()
.map(|item| {
// Step 6: Duplicate the results according to the original input
let mut final_results = Vec::new();
for (unique_peptide, item) in unique_peptides.iter().zip(result.into_iter()) {
if let Some(count) = peptide_counts.get(unique_peptide) {
let fa = calculate_fa(&item.proteins);

let total_protein_count = *fa.counts.get("all").unwrap_or(&0);
let ecs = ec_numbers_from_map(&fa.data, ec_store, extra);

EcInformation { peptide: item.sequence, total_protein_count, ec: ecs }
})
.collect())
for _ in 0..*count {
let ecs = ec_numbers_from_map(&fa.data, ec_store, extra);

final_results.push(EcInformation {
peptide: item.sequence.clone(),
total_protein_count,
ec: ecs,
});
}
}
}

Ok(final_results)
}

generate_handlers!(
Expand Down
4 changes: 0 additions & 4 deletions api/src/controllers/request.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,6 @@ where
let form_bytes = form.to_vec();
let decoded_bytes = urlencoding::decode_binary(&form_bytes);

eprintln!("{:?}", decoded_bytes);

Ok(Self(serde_qs::from_bytes(&decoded_bytes).map_err(|_| StatusCode::BAD_REQUEST.into_response())?))
}
}
Expand All @@ -68,8 +66,6 @@ where
querystring.push_str(&format!("{}={}&", name, value));
}

eprintln!("{:?}", querystring);

Ok(Self(serde_qs::from_str(&querystring).map_err(|_| StatusCode::BAD_REQUEST.into_response())?))
}
}
Expand Down

0 comments on commit e38c854

Please sign in to comment.