diff options
Diffstat (limited to 'wqflask/base')
-rw-r--r-- | wqflask/base/data_set.py | 110 | ||||
-rw-r--r-- | wqflask/base/mrna_assay_tissue_data.py | 2 | ||||
-rw-r--r-- | wqflask/base/species.py | 6 | ||||
-rw-r--r-- | wqflask/base/trait.py | 70 |
4 files changed, 58 insertions, 130 deletions
diff --git a/wqflask/base/data_set.py b/wqflask/base/data_set.py index 44325d5b..3183363b 100644 --- a/wqflask/base/data_set.py +++ b/wqflask/base/data_set.py @@ -17,7 +17,10 @@ # at rwilliams@uthsc.edu and xzhou15@uthsc.edu # # This module is used by GeneNetwork project (www.genenetwork.org) - +from dataclasses import dataclass +from dataclasses import field +from dataclasses import InitVar +from typing import Optional, Dict from db.call import fetchall, fetchone, fetch1 from utility.logger import getLogger from utility.tools import USE_GN_SERVER, USE_REDIS, flat_files, flat_file_exists, GN2_BASE_URL @@ -59,7 +62,8 @@ logger = getLogger(__name__) DS_NAME_MAP = {} -def create_dataset(dataset_name, dataset_type=None, get_samplelist=True, group_name=None): +def create_dataset(dataset_name, dataset_type=None, + get_samplelist=True, group_name=None): if dataset_name == "Temp": dataset_type = "Temp" @@ -74,11 +78,10 @@ def create_dataset(dataset_name, dataset_type=None, get_samplelist=True, group_n return dataset_class(dataset_name, get_samplelist) +@dataclass class DatasetType: - - def __init__(self, redis_instance): - """Create a dictionary of samples where the value is set to Geno, -Publish or ProbeSet. E.g. + """Create a dictionary of samples where the value is set to Geno, + Publish or ProbeSet. E.g. {'AD-cases-controls-MyersGeno': 'Geno', 'AD-cases-controls-MyersPublish': 'Publish', @@ -89,21 +92,28 @@ Publish or ProbeSet. E.g. 'All Phenotypes': 'Publish', 'B139_K_1206_M': 'ProbeSet', 'B139_K_1206_R': 'ProbeSet' ... - + } """ + redis_instance: InitVar[Redis] + datasets: Optional[Dict] = field(init=False, default_factory=dict) + data: Optional[Dict] = field(init=False) + + def __post_init__(self, redis_instance): self.redis_instance = redis_instance - self.datasets = {} - data = self.redis_instance.get("dataset_structure") + data = redis_instance.get("dataset_structure") if data: self.datasets = json.loads(data) - else: # ZS: I don't think this should ever run unless Redis is emptied + else: + # ZS: I don't think this should ever run unless Redis is + # emptied try: data = json.loads(requests.get( - GN2_BASE_URL + "/api/v_pre1/gen_dropdown", timeout=5).content) - for species in data['datasets']: - for group in data['datasets'][species]: - for dataset_type in data['datasets'][species][group]: - for dataset in data['datasets'][species][group][dataset_type]: + GN2_BASE_URL + "/api/v_pre1/gen_dropdown", + timeout=5).content) + for _species in data['datasets']: + for group in data['datasets'][_species]: + for dataset_type in data['datasets'][_species][group]: + for dataset in data['datasets'][_species][group][dataset_type]: short_dataset_name = dataset[1] if dataset_type == "Phenotypes": new_type = "Publish" @@ -112,15 +122,15 @@ Publish or ProbeSet. E.g. else: new_type = "ProbeSet" self.datasets[short_dataset_name] = new_type - except: + except Exception: # Do nothing pass - - self.redis_instance.set("dataset_structure", json.dumps(self.datasets)) + self.redis_instance.set("dataset_structure", + json.dumps(self.datasets)) + self.data = data def set_dataset_key(self, t, name): - """If name is not in the object's dataset dictionary, set it, and update - dataset_structure in Redis - + """If name is not in the object's dataset dictionary, set it, and + update dataset_structure in Redis args: t: Type of dataset structure which can be: 'mrna_expr', 'pheno', 'other_pheno', 'geno' @@ -128,19 +138,20 @@ Publish or ProbeSet. E.g. """ sql_query_mapping = { - 'mrna_expr': ("""SELECT ProbeSetFreeze.Id FROM """ + - """ProbeSetFreeze WHERE ProbeSetFreeze.Name = "{}" """), - 'pheno': ("""SELECT InfoFiles.GN_AccesionId """ + - """FROM InfoFiles, PublishFreeze, InbredSet """ + - """WHERE InbredSet.Name = '{}' AND """ + - """PublishFreeze.InbredSetId = InbredSet.Id AND """ + - """InfoFiles.InfoPageName = PublishFreeze.Name"""), - 'other_pheno': ("""SELECT PublishFreeze.Name """ + - """FROM PublishFreeze, InbredSet """ + - """WHERE InbredSet.Name = '{}' AND """ + - """PublishFreeze.InbredSetId = InbredSet.Id"""), - 'geno': ("""SELECT GenoFreeze.Id FROM GenoFreeze WHERE """ + - """GenoFreeze.Name = "{}" """) + 'mrna_expr': ("SELECT ProbeSetFreeze.Id FROM " + "ProbeSetFreeze WHERE " + "ProbeSetFreeze.Name = \"%s\" "), + 'pheno': ("SELECT InfoFiles.GN_AccesionId " + "FROM InfoFiles, PublishFreeze, InbredSet " + "WHERE InbredSet.Name = '%s' AND " + "PublishFreeze.InbredSetId = InbredSet.Id AND " + "InfoFiles.InfoPageName = PublishFreeze.Name"), + 'other_pheno': ("SELECT PublishFreeze.Name " + "FROM PublishFreeze, InbredSet " + "WHERE InbredSet.Name = '%s' AND " + "PublishFreeze.InbredSetId = InbredSet.Id"), + 'geno': ("SELECT GenoFreeze.Id FROM GenoFreeze WHERE " + "GenoFreeze.Name = \"%s\" ") } dataset_name_mapping = { @@ -154,22 +165,22 @@ Publish or ProbeSet. E.g. if t in ['pheno', 'other_pheno']: group_name = name.replace("Publish", "") - results = g.db.execute(sql_query_mapping[t].format(group_name)).fetchone() + results = g.db.execute(sql_query_mapping[t] % group_name).fetchone() if results: self.datasets[name] = dataset_name_mapping[t] self.redis_instance.set("dataset_structure", json.dumps(self.datasets)) return True - return None def __call__(self, name): - if name not in self.datasets: for t in ["mrna_expr", "pheno", "other_pheno", "geno"]: - # This has side-effects, with the end result being a truth-y value + # This has side-effects, with the end result being a + # truth-y value if(self.set_dataset_key(t, name)): break - return self.datasets.get(name, None) # Return None if name has not been set + # Return None if name has not been set + return self.datasets.get(name, None) # Do the intensive work at startup one time only @@ -209,7 +220,7 @@ def create_datasets_list(): return datasets -class Markers(object): +class Markers: """Todo: Build in cacheing so it saves us reading the same file more than once""" def __init__(self, name): @@ -262,8 +273,6 @@ class Markers(object): elif isinstance(p_values, dict): filtered_markers = [] for marker in self.markers: - #logger.debug("marker[name]", marker['name']) - #logger.debug("p_values:", p_values) if marker['name'] in p_values: #logger.debug("marker {} IS in p_values".format(i)) marker['p_value'] = p_values[marker['name']] @@ -276,10 +285,6 @@ class Markers(object): marker['lrs_value'] = - \ math.log10(marker['p_value']) * 4.61 filtered_markers.append(marker) - # else: - #logger.debug("marker {} NOT in p_values".format(i)) - # self.markers.remove(marker) - #del self.markers[i] self.markers = filtered_markers @@ -306,13 +311,12 @@ class HumanMarkers(Markers): marker['Mb'] = float(splat[3]) / 1000000 self.markers.append(marker) - #logger.debug("markers is: ", pf(self.markers)) def add_pvalues(self, p_values): super(HumanMarkers, self).add_pvalues(p_values) -class DatasetGroup(object): +class DatasetGroup: """ Each group has multiple datasets; each species has multiple groups. @@ -520,7 +524,6 @@ def datasets(group_name, this_group=None): break if tissue_already_exists: - #logger.debug("dataset_menu:", dataset_menu[i]['datasets']) dataset_menu[i]['datasets'].append((dataset, dataset_short)) else: dataset_menu.append(dict(tissue=tissue_name, @@ -537,7 +540,7 @@ def datasets(group_name, this_group=None): return dataset_menu -class DataSet(object): +class DataSet: """ DataSet class defines a dataset in webqtl, can be either Microarray, Published phenotype, genotype, or user input dataset(temp) @@ -569,10 +572,6 @@ class DataSet(object): self.group.get_samplelist() self.species = species.TheSpecies(self) - def get_desc(self): - """Gets overridden later, at least for Temp...used by trait's get_given_name""" - return None - def get_accession_id(self): if self.type == "Publish": results = g.db.execute("""select InfoFiles.GN_AccesionId from InfoFiles, PublishFreeze, InbredSet where @@ -730,9 +729,6 @@ class PhenotypeDataSet(DataSet): DS_NAME_MAP['Publish'] = 'PhenotypeDataSet' def setup(self): - - #logger.debug("IS A PHENOTYPEDATASET") - # Fields in the database table self.search_fields = ['Phenotype.Post_publication_description', 'Phenotype.Pre_publication_description', diff --git a/wqflask/base/mrna_assay_tissue_data.py b/wqflask/base/mrna_assay_tissue_data.py index f1929518..1f8224af 100644 --- a/wqflask/base/mrna_assay_tissue_data.py +++ b/wqflask/base/mrna_assay_tissue_data.py @@ -11,7 +11,7 @@ from utility.db_tools import escape from utility.logger import getLogger logger = getLogger(__name__ ) -class MrnaAssayTissueData(object): +class MrnaAssayTissueData: def __init__(self, gene_symbols=None): self.gene_symbols = gene_symbols diff --git a/wqflask/base/species.py b/wqflask/base/species.py index 2771d116..eae3325a 100644 --- a/wqflask/base/species.py +++ b/wqflask/base/species.py @@ -6,7 +6,7 @@ from flask import Flask, g from utility.logger import getLogger logger = getLogger(__name__ ) -class TheSpecies(object): +class TheSpecies: def __init__(self, dataset=None, species_name=None): if species_name != None: self.name = species_name @@ -15,7 +15,7 @@ class TheSpecies(object): self.dataset = dataset self.chromosomes = Chromosomes(dataset=self.dataset) -class IndChromosome(object): +class IndChromosome: def __init__(self, name, length): self.name = name self.length = length @@ -25,7 +25,7 @@ class IndChromosome(object): """Chromosome length in megabases""" return self.length / 1000000 -class Chromosomes(object): +class Chromosomes: def __init__(self, dataset=None, species=None): self.chromosomes = collections.OrderedDict() if species != None: diff --git a/wqflask/base/trait.py b/wqflask/base/trait.py index b4b4452a..5574128d 100644 --- a/wqflask/base/trait.py +++ b/wqflask/base/trait.py @@ -51,7 +51,7 @@ def create_trait(**kw): return None -class GeneralTrait(object): +class GeneralTrait: """ Trait class defines a trait in webqtl, can be either Microarray, Published phenotype, genotype, or user input trait @@ -345,74 +345,6 @@ def jsonable(trait): return dict() -def jsonable_table_row(trait, dataset_name, index): - """Return a list suitable for json and intended to be displayed in a table - - Actual turning into json doesn't happen here though""" - - dataset = create_dataset(dataset_name) - - if dataset.type == "ProbeSet": - if trait.mean == "": - mean = "N/A" - else: - mean = "%.3f" % round(float(trait.mean), 2) - if trait.additive == "": - additive = "N/A" - else: - additive = "%.3f" % round(float(trait.additive), 2) - return ['<input type="checkbox" name="searchResult" class="checkbox trait_checkbox" value="' + hmac.data_hmac('{}:{}'.format(str(trait.name), dataset.name)) + '">', - index, - '<a href="/show_trait?trait_id=' + - str(trait.name)+'&dataset='+dataset.name + - '">'+str(trait.name)+'</a>', - trait.symbol, - trait.description_display, - trait.location_repr, - mean, - trait.LRS_score_repr, - trait.LRS_location_repr, - additive] - elif dataset.type == "Publish": - if trait.additive == "": - additive = "N/A" - else: - additive = "%.2f" % round(float(trait.additive), 2) - if trait.pubmed_id: - return ['<input type="checkbox" name="searchResult" class="checkbox trait_checkbox" value="' + hmac.data_hmac('{}:{}'.format(str(trait.name), dataset.name)) + '">', - index, - '<a href="/show_trait?trait_id=' + - str(trait.name)+'&dataset='+dataset.name + - '">'+str(trait.name)+'</a>', - trait.description_display, - trait.authors, - '<a href="' + trait.pubmed_link + '">' + trait.pubmed_text + '</href>', - trait.LRS_score_repr, - trait.LRS_location_repr, - additive] - else: - return ['<input type="checkbox" name="searchResult" class="checkbox trait_checkbox" value="' + hmac.data_hmac('{}:{}'.format(str(trait.name), dataset.name)) + '">', - index, - '<a href="/show_trait?trait_id=' + - str(trait.name)+'&dataset='+dataset.name + - '">'+str(trait.name)+'</a>', - trait.description_display, - trait.authors, - trait.pubmed_text, - trait.LRS_score_repr, - trait.LRS_location_repr, - additive] - elif dataset.type == "Geno": - return ['<input type="checkbox" name="searchResult" class="checkbox trait_checkbox" value="' + hmac.data_hmac('{}:{}'.format(str(trait.name), dataset.name)) + '">', - index, - '<a href="/show_trait?trait_id=' + - str(trait.name)+'&dataset='+dataset.name + - '">'+str(trait.name)+'</a>', - trait.location_repr] - else: - return dict() - - def retrieve_trait_info(trait, dataset, get_qtl_info=False): assert dataset, "Dataset doesn't exist" |