Skip to content

Commit

Permalink
fix: allow for case insenstive gene search in server (#354)
Browse files Browse the repository at this point in the history
  • Loading branch information
holtgrewe committed Dec 18, 2023
1 parent e132304 commit 3a79fa0
Showing 1 changed file with 35 additions and 13 deletions.
48 changes: 35 additions & 13 deletions src/server/actix_server/genes_search.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,8 @@ struct Request {
/// The fields to search in.
#[serde_as(as = "Option<StringWithSeparator::<CommaSeparator, Fields>>")]
pub fields: Option<Vec<Fields>>,
/// Enable case sensitive search.
pub case_sensitive: Option<bool>,
}

/// A scored result.
Expand Down Expand Up @@ -97,7 +99,27 @@ async fn handle(

let max_items = 100;

let q = &query.q;
let case_sensitive: bool = query.case_sensitive.unwrap_or(false);

Check warning on line 102 in src/server/actix_server/genes_search.rs

View check run for this annotation

Codecov / codecov/patch

src/server/actix_server/genes_search.rs#L102

Added line #L102 was not covered by tests

let q = if case_sensitive {
query.q.clone()

Check warning on line 105 in src/server/actix_server/genes_search.rs

View check run for this annotation

Codecov / codecov/patch

src/server/actix_server/genes_search.rs#L104-L105

Added lines #L104 - L105 were not covered by tests
} else {
query.q.to_lowercase()

Check warning on line 107 in src/server/actix_server/genes_search.rs

View check run for this annotation

Codecov / codecov/patch

src/server/actix_server/genes_search.rs#L107

Added line #L107 was not covered by tests
};
let equals_q = |val: &str| {
if case_sensitive {
val == q

Check warning on line 111 in src/server/actix_server/genes_search.rs

View check run for this annotation

Codecov / codecov/patch

src/server/actix_server/genes_search.rs#L109-L111

Added lines #L109 - L111 were not covered by tests
} else {
val.to_lowercase() == q.to_lowercase()

Check warning on line 113 in src/server/actix_server/genes_search.rs

View check run for this annotation

Codecov / codecov/patch

src/server/actix_server/genes_search.rs#L113

Added line #L113 was not covered by tests
}
};
let contains_q = |val: &str| {
if case_sensitive {
val.contains(&q)

Check warning on line 118 in src/server/actix_server/genes_search.rs

View check run for this annotation

Codecov / codecov/patch

src/server/actix_server/genes_search.rs#L116-L118

Added lines #L116 - L118 were not covered by tests
} else {
val.to_lowercase().contains(&q.to_lowercase())

Check warning on line 120 in src/server/actix_server/genes_search.rs

View check run for this annotation

Codecov / codecov/patch

src/server/actix_server/genes_search.rs#L120

Added line #L120 was not covered by tests
}
};
let fields: Vec<Fields> = if let Some(fields) = query.fields.as_ref() {
fields.clone()
} else {
Expand All @@ -111,27 +133,27 @@ async fn handle(
.gene_names
.iter()
.map(|gn| -> Scored<GeneNames> {
let score = if (fields_contains(&Fields::HgncId) && &gn.hgnc_id == q)
|| (fields_contains(&Fields::Symbol) && &gn.symbol == q)
|| (fields_contains(&Fields::Symbol) && &gn.symbol == q)
|| (fields_contains(&Fields::Name) && &gn.name == q)
let score = if (fields_contains(&Fields::HgncId) && equals_q(&gn.hgnc_id))
|| (fields_contains(&Fields::Symbol) && equals_q(&gn.symbol))
|| (fields_contains(&Fields::Symbol) && equals_q(&gn.symbol))
|| (fields_contains(&Fields::Name) && equals_q(&gn.name))

Check warning on line 139 in src/server/actix_server/genes_search.rs

View check run for this annotation

Codecov / codecov/patch

src/server/actix_server/genes_search.rs#L136-L139

Added lines #L136 - L139 were not covered by tests
|| (fields_contains(&Fields::EnsemblGeneId)
&& gn.ensembl_gene_id.iter().any(|s| s == q))
&& gn.ensembl_gene_id.iter().any(|s| equals_q(s)))

Check warning on line 141 in src/server/actix_server/genes_search.rs

View check run for this annotation

Codecov / codecov/patch

src/server/actix_server/genes_search.rs#L141

Added line #L141 was not covered by tests
|| (fields_contains(&Fields::NcbiGeneId)
&& gn.ensembl_gene_id.iter().any(|s| s == q))
&& gn.ensembl_gene_id.iter().any(|s| equals_q(s)))

Check warning on line 143 in src/server/actix_server/genes_search.rs

View check run for this annotation

Codecov / codecov/patch

src/server/actix_server/genes_search.rs#L143

Added line #L143 was not covered by tests
{
1f32
} else if fields_contains(&Fields::Symbol) && gn.symbol.contains(q) {
} else if fields_contains(&Fields::Symbol) && contains_q(&gn.symbol) {

Check warning on line 146 in src/server/actix_server/genes_search.rs

View check run for this annotation

Codecov / codecov/patch

src/server/actix_server/genes_search.rs#L146

Added line #L146 was not covered by tests
q.len() as f32 / gn.symbol.len() as f32
} else if fields_contains(&Fields::Name) && gn.name.contains(q) {
} else if fields_contains(&Fields::Name) && contains_q(&gn.name) {

Check warning on line 148 in src/server/actix_server/genes_search.rs

View check run for this annotation

Codecov / codecov/patch

src/server/actix_server/genes_search.rs#L148

Added line #L148 was not covered by tests
q.len() as f32 / gn.name.len() as f32
} else if fields_contains(&Fields::AliasSymbol)
&& gn.alias_symbol.iter().any(|s| s.contains(q))
&& gn.alias_symbol.iter().any(|s| contains_q(s))

Check warning on line 151 in src/server/actix_server/genes_search.rs

View check run for this annotation

Codecov / codecov/patch

src/server/actix_server/genes_search.rs#L151

Added line #L151 was not covered by tests
{
gn.alias_symbol
.iter()
.map(|s| {
if s.contains(q) {
if contains_q(s) {

Check warning on line 156 in src/server/actix_server/genes_search.rs

View check run for this annotation

Codecov / codecov/patch

src/server/actix_server/genes_search.rs#L156

Added line #L156 was not covered by tests
q.len() as f32 / s.len() as f32
} else {
0f32
Expand All @@ -140,12 +162,12 @@ async fn handle(
.max_by(|a, b| a.partial_cmp(b).unwrap_or(std::cmp::Ordering::Equal))
.unwrap_or(0f32)
} else if fields_contains(&Fields::AliasName)
&& gn.alias_name.iter().any(|s| s.contains(q))
&& gn.alias_name.iter().any(|s| contains_q(s))

Check warning on line 165 in src/server/actix_server/genes_search.rs

View check run for this annotation

Codecov / codecov/patch

src/server/actix_server/genes_search.rs#L165

Added line #L165 was not covered by tests
{
gn.alias_name
.iter()
.map(|s| {
if s.contains(q) {
if contains_q(s) {

Check warning on line 170 in src/server/actix_server/genes_search.rs

View check run for this annotation

Codecov / codecov/patch

src/server/actix_server/genes_search.rs#L170

Added line #L170 was not covered by tests
q.len() as f32 / s.len() as f32
} else {
0f32
Expand Down

0 comments on commit 3a79fa0

Please sign in to comment.