Skip to content

Commit

Permalink
Merge pull request #484 from AndersenLab/fix/phenotype-db
Browse files Browse the repository at this point in the history
Updating Phenotype DB with updated SQL tables
  • Loading branch information
r-vieira authored May 31, 2024
2 parents 81699d0 + 2b06b05 commit e39ec56
Show file tree
Hide file tree
Showing 10 changed files with 56 additions and 11 deletions.
2 changes: 1 addition & 1 deletion src/modules/db_operations/module.env
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
MODULE_NAME=caendr-db-operations
MODULE_VERSION=v1.0.41
MODULE_VERSION=v1.0.42
1 change: 1 addition & 0 deletions src/modules/db_operations/requirements.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
backoff==2.2.1
bleach==4.1.0
Flask==1.1.2
Flask_SQLAlchemy==2.5.1
SQLAlchemy==1.3.18
Expand Down
6 changes: 6 additions & 0 deletions src/modules/site-v2/base/static/js/plot.js
Original file line number Diff line number Diff line change
Expand Up @@ -411,6 +411,12 @@ function render_ranked_barplot(container_selector, data, config={}) {
// Read values from config, filling in default values when not supplied
const fill_color = config['fill_color'] || 'black';

// Create a template function for the tooltip
// By default, show label & value for each axis
const tooltip_id = config['tooltip_id'] || null;
const tooltip_template = config['tooltip_template'] || ((d) => `
<p class="tooltip-body">${d[0]}: ${d[1]}</p>
`);

// Sort data
data.sort(function(b, a) {
Expand Down
2 changes: 1 addition & 1 deletion src/modules/site-v2/module.env
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
MODULE_NAME=caendr-site-v2
MODULE_VERSION=v2.0.95
MODULE_VERSION=v2.0.96

PORT=8080
FLASK_APP=main:app
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -191,7 +191,7 @@
$('#{{ offcanvas_id }} .btn-close').click()
flashErrorResponse(error.responseJSON, 'There was a problem retrieving this trait. Please try again later.')
})
})
})


// Filter table by tag links
Expand Down
2 changes: 1 addition & 1 deletion src/modules/site-v2/templates/_scripts/trait_utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ function queryTraitByName(trait_id, csrf_token=null) {
// Construct the data object, using the optional CSRF token if provided
let data = {trait_id};
if (csrf_token !== null) data['csrf_token'] = csrf_token;

// Return the AJAX request as a Promise object
return $.ajax({
type: "POST",
Expand Down
16 changes: 16 additions & 0 deletions src/modules/site-v2/templates/tools/phenotype_database/report.html
Original file line number Diff line number Diff line change
Expand Up @@ -228,6 +228,11 @@ <h4 class="h5">Description:</h4>
fill_color: '#0719BC',
bins_per_tick: 2,
x_label: trait_label,
tooltip_id: 'tooltip-single-histogram',
tooltip_template: (d, labels) => `
<p class="tooltip-head"><b>Strain: ${d[0]}</b></p>
<p class="tooltip-body">${labels[0]}: ${d[1]}</p>
`,
});
} catch (err) {
console.error(`Could not construct histogram. ${err}`)
Expand All @@ -238,6 +243,11 @@ <h4 class="h5">Description:</h4>
width: 1200,
fill_color: '#0719BC',
y_label: trait_label,
tooltip_id: 'tooltip-single-barplot',
tooltip_template: (d) => `
<p class="tooltip-head"><b>${d[0]}</b></p>
<p class="tooltip-body">Value = ${d[1].toFixed(4)}</p>
`,
});
} catch (err) {
console.error(`Could not construct ranked bar plot. ${err}`)
Expand All @@ -262,6 +272,12 @@ <h4 class="h5">Description:</h4>
y_label: `${trait_label_2}`,
opacity: 0.5,
opacity_hover: 1,
tooltip_id: 'tooltip-double-scatterplot',
tooltip_template: (d, labels) => `
<p class="tooltip-head"><b>${d[0]}</b></p>
<p class="tooltip-body">x = ${d[1].toFixed(4)}</p>
<p class="tooltip-body">y = ${d[2].toFixed(4)}</p>
`,
});
} catch (err) {
console.error(`Could not construct scatterplot. ${err}`)
Expand Down
2 changes: 1 addition & 1 deletion src/pkg/caendr/caendr/models/datastore/phenotype_report.py
Original file line number Diff line number Diff line change
Expand Up @@ -286,7 +286,7 @@ def compute_display_name(cls, trait: Union[Trait, TraitFile], trait_name: Option
raise ValueError()

# Use the Trait object to compute the display name
return list(trait.display_name)
return [trait.name,] if trait.dataset == 'zhang' else list(trait.display_name)


@classmethod
Expand Down
30 changes: 26 additions & 4 deletions src/pkg/caendr/caendr/models/trait.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@

from caendr.models.datastore import TraitFile
from caendr.models.sql import PhenotypeMetadata, PhenotypeDatabase
from caendr.models.error import NotFoundError
from caendr.utils.data import dataframe_cols_to_dict
from caendr.api.phenotype import get_trait

from caendr.services.cloud.postgresql import db

Expand Down Expand Up @@ -57,9 +59,11 @@ def __init__(self, dataset = None, trait_name = None, trait_file_id = None, trai
raise ValueError('Could not identify a unique trait from the given information')

# Store the dataset value of the trait file
if dataset and self.file['dataset'] and dataset != self.file['dataset']:
raise ValueError('Mismatched dataset values')
self.dataset = self.file['dataset']
# if dataset and self.file['dataset'].value and dataset != self.file['dataset'].value:
# raise ValueError('Mismatched dataset values')
self.dataset = self.file['dataset'].value

self.sql_row = PhenotypeMetadata.query.filter_by(trait_name_caendr = self.name, dataset = self.dataset).one()


#
Expand Down Expand Up @@ -104,6 +108,24 @@ def from_sql(sql_row: PhenotypeMetadata) -> 'Trait':
dataset = sql_row.dataset,
trait_name = sql_row.trait_name_caendr,
)

@classmethod
def from_id(cls, trait_id: str) -> 'Trait':
'''
Instantiate a `Trait` object from a unique trait ID.
The given ID must exist in the PhenotypeMetadata SQL table, otherwise a `ValueError` will be raised.
'''

# Get the SQL row with the given trait ID
sql_row = PhenotypeMetadata.query.get(trait_id)
if sql_row is None:
raise NotFoundError(PhenotypeMetadata, {'id': trait_id})

# Construct a Trait object using the data in the SQL row
return cls(
trait_name = sql_row.trait_name_caendr,
dataset = sql_row.dataset,
)


#
Expand Down Expand Up @@ -145,4 +167,4 @@ def display_name(self):
'''

# For bulk files, store the single trait name, otherwise convert the display_name fields to a list
return (self.name,) if self.file['is_bulk_file'] else self.file.display_name
return (self.sql_row.trait_name_caendr,) if self.file['is_bulk_file'] else self.file.display_name
4 changes: 2 additions & 2 deletions src/pkg/caendr/caendr/services/sql/etl/phenotype_metadata.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ def parse_phenotype_metadata(species: Species, **files: LocalDatastoreFile):
'capture_date': md['capture_date'],
'created_on': md.created_on,
'modified_on': md.modified_on,
'dataset': md['dataset'],
'dataset': md['dataset'].value,
'is_bulk_file': md['is_bulk_file'],
}
else:
Expand All @@ -83,6 +83,6 @@ def parse_phenotype_metadata(species: Species, **files: LocalDatastoreFile):
'capture_date': md['capture_date'],
'created_on': md.created_on,
'modified_on': md.modified_on,
'dataset': md['dataset'],
'dataset': md['dataset'].value,
'is_bulk_file': md['is_bulk_file'],
}

0 comments on commit e39ec56

Please sign in to comment.