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

Add private_api/proteins/count and private_api/proteins/filter endpoints #65

Merged
merged 7 commits into from
Mar 18, 2025
Merged
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
1 change: 1 addition & 0 deletions api/src/controllers/private_api/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ pub mod goterms;
pub mod interpros;
pub mod metadata;
pub mod proteins;
pub mod proteins_filter;
pub mod taxa;
pub mod taxa_filter;
pub mod reference_proteomes;
Expand Down
106 changes: 20 additions & 86 deletions api/src/controllers/private_api/proteins.rs
Original file line number Diff line number Diff line change
@@ -1,117 +1,51 @@
use std::collections::HashSet;
use axum::{extract::State, Json};
use database::get_accessions_map;
use serde::{Deserialize, Serialize};

use database::get_accessions;
use crate::{
controllers::generate_handlers,
errors::ApiError,
helpers::{
lca_helper::calculate_lca,
lineage_helper::{get_lineage_array, LineageVersion}
},
AppState
};

#[derive(Deserialize)]
pub struct Parameters {
peptide: String,
equate_il: bool
}

#[derive(Serialize)]
pub struct ProteinInformation {
lca: i32,
common_lineage: Vec<i32>,
proteins: Vec<Protein>
#[serde(default)]
accessions: Vec<String>
}

#[derive(Serialize)]
pub struct Protein {
#[serde(rename = "uniprotAccessionId")]
uniprot_accession_id: String,
name: String,
organism: u32,
#[serde(rename = "ecNumbers")]
ec_numbers: Vec<String>,
#[serde(rename = "goTerms")]
go_terms: Vec<String>,
#[serde(rename = "interproEntries")]
interpro_entries: Vec<String>
}

impl Default for ProteinInformation {
fn default() -> Self {
ProteinInformation { lca: -1, common_lineage: vec![], proteins: vec![] }
}
taxon_id: u32,
db_type: String
}

async fn handler(
State(AppState { index, datastore, database }): State<AppState>,
Parameters { peptide, equate_il }: Parameters
) -> Result<ProteinInformation, ApiError> {
State(AppState { database, .. }): State<AppState>,
Parameters { accessions }: Parameters
) -> Result<Vec<Protein>, ApiError> {
let connection = database.get_conn().await?;

let result = index.analyse(&vec![peptide], equate_il, false, None);

if result.is_empty() {
return Ok(ProteinInformation::default());
}

let accession_numbers: HashSet<String> =
result[0].proteins.iter().map(|protein| protein.uniprot_accession.clone()).collect();

let accessions_map = connection.interact(move |conn| get_accessions_map(conn, &accession_numbers)).await??;

let taxon_store = datastore.taxon_store();
let lineage_store = datastore.lineage_store();

let taxa = result[0].proteins.iter().map(|protein| protein.taxon).collect();
let lca = calculate_lca(taxa, LineageVersion::V2, taxon_store, lineage_store);

let common_lineage = get_lineage_array(lca as u32, LineageVersion::V2, lineage_store)
.iter()
.filter_map(|taxon_id| *taxon_id)
.collect::<Vec<i32>>();

Ok(ProteinInformation {
lca,
common_lineage,
proteins: result[0]
.proteins
.iter()
.filter_map(|protein| {
let uniprot_entry = accessions_map.get(&protein.uniprot_accession)?;

let fa: Vec<&str> = uniprot_entry.fa.split(';').collect();
let ec_numbers =
fa.iter().filter(|key| key.starts_with("EC:")).map(ToString::to_string).collect::<Vec<String>>();
let go_terms =
fa.iter().filter(|key| key.starts_with("GO:")).map(ToString::to_string).collect::<Vec<String>>();
let interpro_entries = fa
.iter()
.filter(|key| key.starts_with("IPR:"))
.map(|k| k[4..].to_string())
.collect::<Vec<String>>();

Some(Protein {
uniprot_accession_id: protein.uniprot_accession.clone(),
name: uniprot_entry.name.clone(),
organism: uniprot_entry.taxon_id,
ec_numbers,
go_terms,
interpro_entries
})
})
.collect()
})
let entries = connection.interact(move |conn| get_accessions(conn, &HashSet::from_iter(accessions.iter().cloned()))).await??;

Ok(entries
.into_iter()
.map(|entry| Protein {
uniprot_accession_id: entry.uniprot_accession_number,
name: entry.name,
taxon_id: entry.taxon_id,
db_type: entry.db_type,
})
.collect())
}

generate_handlers!(
async fn json_handler(
state => State<AppState>,
params => Parameters
) -> Result<Json<ProteinInformation>, ApiError> {
) -> Result<Json<Vec<Protein>>, ApiError> {
Ok(Json(handler(state, params).await?))
}
);
73 changes: 73 additions & 0 deletions api/src/controllers/private_api/proteins_filter.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
use axum::{extract::State, Json};
use serde::{Deserialize, Serialize};
use database::{get_accessions_by_filter, get_accessions_count_by_filter};
use crate::{
controllers::generate_handlers,
AppState
};
use crate::errors::ApiError;

fn default_filter() -> String {
String::from("")
}

fn default_sort_by() -> String {
String::from("uniprot_accession_id")
}

#[derive(Deserialize)]
pub struct ProteinCountParameters {
#[serde(default = "default_filter")]
filter: String
}

#[derive(Deserialize)]
pub struct ProteinFilterParameters {
#[serde(default = "default_filter")]
filter: String,
start: usize,
end: usize,
#[serde(default = "default_sort_by")]
sort_by: String,
#[serde(default)]
sort_descending: bool
}

#[derive(Serialize)]
pub struct ProteinCountResult {
count: u32
}

async fn count_handler(
State(AppState { database, .. }): State<AppState>,
ProteinCountParameters { filter }: ProteinCountParameters
) -> Result<ProteinCountResult, ApiError> {
let connection = database.get_conn().await?;
Ok(ProteinCountResult { count: connection.interact(move |conn| get_accessions_count_by_filter(conn, filter)).await?? })
}

async fn filter_handler(
State(AppState { database, .. }): State<AppState>,
ProteinFilterParameters { filter, start, end, sort_by, sort_descending }: ProteinFilterParameters
) -> Result<Vec<String>, ApiError> {
let connection = database.get_conn().await?;
Ok(connection.interact(move |conn| get_accessions_by_filter(conn, filter, start, end, sort_by, sort_descending)).await??)
}

generate_handlers!(
async fn json_count_handler(
state => State<AppState>,
params => ProteinCountParameters
) -> Result<Json<ProteinCountResult>, ApiError> {
Ok(Json(count_handler(state, params).await?))
}
);

generate_handlers!(
async fn json_filter_handler(
state => State<AppState>,
params => ProteinFilterParameters
) -> Result<Json<Vec<String>>, ApiError> {
Ok(Json(filter_handler(state, params).await?))
}
);
6 changes: 5 additions & 1 deletion api/src/routes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ use crate::{
},
datasets::sampledata,
mpa::{pept2data},
private_api::{ecnumbers, goterms, interpros, metadata, proteins, reference_proteomes, reference_proteomes_filter, taxa, taxa_filter}
private_api::{ecnumbers, goterms, interpros, metadata, proteins, proteins_filter, reference_proteomes, reference_proteomes_filter, taxa, taxa_filter}
},
middleware::{
cors::create_cors_layer,
Expand Down Expand Up @@ -148,6 +148,10 @@ fn create_private_api_routes() -> Router<AppState> {
get(metadata::get_json_handler).post(metadata::post_json_handler),
"/proteins",
get(proteins::get_json_handler).post(proteins::post_json_handler),
"/proteins/count",
get(proteins_filter::get_json_count_handler).post(proteins_filter::post_json_count_handler),
"/proteins/filter",
get(proteins_filter::get_json_filter_handler).post(proteins_filter::post_json_filter_handler),
"/proteomes",
get(reference_proteomes::get_json_handler).post(reference_proteomes::post_json_handler),
"/proteomes/count",
Expand Down
Loading