From 8237c44937590fd06030945a35aa1de24e29463b Mon Sep 17 00:00:00 2001 From: zsloan Date: Wed, 29 May 2019 12:43:21 -0500 Subject: Added option to get traits from dataset to API --- wqflask/wqflask/api/router.py | 116 ++++++++++++++++++++++++++++++++++++++---- 1 file changed, 105 insertions(+), 11 deletions(-) (limited to 'wqflask') diff --git a/wqflask/wqflask/api/router.py b/wqflask/wqflask/api/router.py index 845873a0..4ecd6699 100644 --- a/wqflask/wqflask/api/router.py +++ b/wqflask/wqflask/api/router.py @@ -290,6 +290,106 @@ def get_dataset_info(dataset_name, group_name = None, file_format="json"): else: return return_error(code=204, source=request.url_rule.rule, title="No Results", details="") +@app.route("/api/v_{}/traits/".format(version), methods=('GET',)) +@app.route("/api/v_{}/traits/.".format(version), methods=('GET',)) +def fetch_traits(dataset_name, file_format = "csv"): + trait_ids, _trait_names, data_type, dataset_id = get_dataset_trait_ids(dataset_name) + if ('ids_only' in request.args) and (len(trait_ids) > 0): + if file_format == "json": + filename = dataset_name + "_trait_ids.json" + return flask.jsonify(trait_ids) + else: + filename = dataset_name + "_trait_ids.csv" + + si = StringIO.StringIO() + csv_writer = csv.writer(si) + csv_writer.writerows([[trait_id] for trait_id in trait_ids]) + output = make_response(si.getvalue()) + output.headers["Content-Disposition"] = "attachment; filename=" + filename + output.headers["Content-type"] = "text/csv" + return output + else: + if len(trait_ids) > 0: + if data_type == "ProbeSet": + query = """ + SELECT + ProbeSet.Id, ProbeSet.Name, ProbeSet.Symbol, ProbeSet.description, ProbeSet.Chr, ProbeSet.Mb, ProbeSet.alias, + ProbeSetXRef.mean, ProbeSetXRef.se, ProbeSetXRef.Locus, ProbeSetXRef.LRS, ProbeSetXRef.pValue, ProbeSetXRef.additive, ProbeSetXRef.h2 + FROM + ProbeSet, ProbeSetXRef + WHERE + ProbeSetXRef.ProbeSetFreezeId = '{0}' AND + ProbeSetXRef.ProbeSetId = ProbeSet.Id + ORDER BY + ProbeSet.Id + """ + + field_list = ["Id", "Name", "Symbol", "Description", "Chr", "Mb", "Aliases", "Mean", "SE", "Locus", "LRS", "P-Value", "Additive", "h2"] + elif data_type == "Geno": + query = """ + SELECT + Geno.Id, Geno.Name, Geno.Marker_Name, Geno.Chr, Geno.Mb, Geno.Sequence, Geno.Source + FROM + Geno, GenoXRef + WHERE + GenoXRef.GenoFreezeId = '{0}' AND + GenoXRef.GenoId = Geno.Id + ORDER BY + Geno.Id + """ + + field_list = ["Id", "Name", "Marker_Name", "Chr", "Mb", "Sequence", "Source"] + else: + query = """ + SELECT + PublishXRef.Id, PublishXRef.PhenotypeId, PublishXRef.PublicationId, PublishXRef.Locus, PublishXRef.LRS, PublishXRef.additive, PublishXRef.Sequence + FROM + PublishXRef + WHERE + PublishXRef.InbredSetId = {0} + ORDER BY + PublishXRef.Id + """ + + field_list = ["Id", "PhenotypeId", "PublicationId", "Locus", "LRS", "Additive", "Sequence"] + + if file_format == "json": + filename = dataset_name + "_traits.json" + + final_query = query.format(dataset_id) + + result_list = [] + for result in g.db.execute(final_query).fetchall(): + trait_dict = {} + for i, field in enumerate(field_list): + if result[i]: + trait_dict[field] = result[i] + result_list.append(trait_dict) + + return flask.jsonify(result_list) + elif file_format == "csv": + filename = dataset_name + "_traits.csv" + + results_list = [] + header_list = [] + header_list += field_list + results_list.append(header_list) + + final_query = query.format(dataset_id) + for result in g.db.execute(final_query).fetchall(): + results_list.append(result) + + si = StringIO.StringIO() + csv_writer = csv.writer(si) + csv_writer.writerows(results_list) + output = make_response(si.getvalue()) + output.headers["Content-Disposition"] = "attachment; filename=" + filename + output.headers["Content-type"] = "text/csv" + return output + else: + return return_error(code=400, source=request.url_rule.rule, title="Invalid Output Format", details="Current formats available are JSON and CSV, with CSV as default") + else: + return return_error(code=204, source=request.url_rule.rule, title="No Results", details="") @app.route("/api/v_{}/sample_data/".format(version)) @app.route("/api/v_{}/sample_data/.".format(version)) @@ -528,8 +628,6 @@ def get_trait_info(dataset_name, trait_name, file_format = "json"): PublishXRef.Id = '{0}' AND PublishXRef.InbredSetId = '{1}' """.format(trait_name, group_id) - - logger.debug("QUERY:", pheno_query) pheno_results = g.db.execute(pheno_query) @@ -612,13 +710,6 @@ def get_genotypes(group_name, file_format="csv"): return output -@app.route("/api/v_{}/traits/".format(version), methods=('GET',)) -@app.route("/api/v_{}/traits/.".format(version), methods=('GET',)) -def get_traits(dataset_name, file_format = "json"): - #ZS: Need to check about the "start" and "stop" stuff since it seems to just limit the number of results to stop - start + 1 in Pjotr's elixir code - - NotImplemented - def return_error(code, source, title, details): json_ob = {"errors": [ { @@ -652,7 +743,7 @@ def get_dataset_trait_ids(dataset_name): dataset_id = results[0][2] return trait_ids, trait_names, data_type, dataset_id - elif "Publish" in dataset_name: + elif "Publish" in dataset_name or get_group_id(dataset_name): data_type = "Publish" dataset_name = dataset_name.replace("Publish", "") dataset_id = get_group_id(dataset_name) @@ -743,7 +834,10 @@ def get_group_id_from_dataset(dataset_name): result = g.db.execute(query).fetchone() - return result[0] + if len(result) > 0: + return result[0] + else: + return None def get_group_id(group_name): query = """ -- cgit v1.2.3 From 84d165a30aaff2fcb4dd6b3bb7106ff0ceb495e9 Mon Sep 17 00:00:00 2001 From: zsloan Date: Fri, 31 May 2019 12:58:26 -0500 Subject: Added option to get just trait names from traits query, and also fixed sample_data query to avoid duplicates --- wqflask/wqflask/api/router.py | 20 +++++++++++++++++--- 1 file changed, 17 insertions(+), 3 deletions(-) (limited to 'wqflask') diff --git a/wqflask/wqflask/api/router.py b/wqflask/wqflask/api/router.py index 4ecd6699..707e4e65 100644 --- a/wqflask/wqflask/api/router.py +++ b/wqflask/wqflask/api/router.py @@ -292,8 +292,8 @@ def get_dataset_info(dataset_name, group_name = None, file_format="json"): @app.route("/api/v_{}/traits/".format(version), methods=('GET',)) @app.route("/api/v_{}/traits/.".format(version), methods=('GET',)) -def fetch_traits(dataset_name, file_format = "csv"): - trait_ids, _trait_names, data_type, dataset_id = get_dataset_trait_ids(dataset_name) +def fetch_traits(dataset_name, file_format = "json"): + trait_ids, trait_names, data_type, dataset_id = get_dataset_trait_ids(dataset_name) if ('ids_only' in request.args) and (len(trait_ids) > 0): if file_format == "json": filename = dataset_name + "_trait_ids.json" @@ -308,6 +308,20 @@ def fetch_traits(dataset_name, file_format = "csv"): output.headers["Content-Disposition"] = "attachment; filename=" + filename output.headers["Content-type"] = "text/csv" return output + elif ('names_only' in request.args) and (len(trait_ids) > 0): + if file_format == "json": + filename = dataset_name + "_trait_names.json" + return flask.jsonify(trait_names) + else: + filename = dataset_name + "_trait_names.csv" + + si = StringIO.StringIO() + csv_writer = csv.writer(si) + csv_writer.writerows([[trait_name] for trait_name in trait_names]) + output = make_response(si.getvalue()) + output.headers["Content-Disposition"] = "attachment; filename=" + filename + output.headers["Content-type"] = "text/csv" + return output else: if len(trait_ids) > 0: if data_type == "ProbeSet": @@ -534,7 +548,7 @@ def trait_sample_data(dataset_name, trait_name, file_format = "json"): dataset_or_group = dataset_name pheno_query = """ - SELECT + SELECT DISTINCT Strain.Name, Strain.Name2, PublishData.value, PublishData.Id, PublishSE.error, NStrain.count FROM (PublishData, Strain, PublishXRef, PublishFreeze) -- cgit v1.2.3 From 34d962fe1e0dd5e30f067e7a30fcbd8931f2aacb Mon Sep 17 00:00:00 2001 From: zsloan Date: Tue, 4 Jun 2019 11:20:28 -0500 Subject: Added GeneMANIA link Fixed issue that caused error when creating UCSC RefSeq link Fixed issue that sometimes caused an error for phenotype searches Changed some text on collections page --- wqflask/base/webqtlConfig.py | 2 +- wqflask/wqflask/show_trait/show_trait.py | 11 ++++++++--- wqflask/wqflask/templates/collections/view.html | 4 ++-- wqflask/wqflask/templates/search_result_page.html | 6 +++++- wqflask/wqflask/templates/show_trait_details.html | 6 ++++++ 5 files changed, 22 insertions(+), 7 deletions(-) (limited to 'wqflask') diff --git a/wqflask/base/webqtlConfig.py b/wqflask/base/webqtlConfig.py index b14cc4b0..a08acb0a 100644 --- a/wqflask/base/webqtlConfig.py +++ b/wqflask/base/webqtlConfig.py @@ -43,6 +43,7 @@ HOMOLOGENE_ID = "http://www.ncbi.nlm.nih.gov/homologene/?term=%s" GENOTATION_URL = "http://www.genotation.org/Getd2g.pl?gene_list=%s" GTEX_URL = "https://www.gtexportal.org/home/gene/%s" GENEBRIDGE_URL = "https://www.systems-genetics.org/modules_by_gene/%s?organism=%s" +GENEMANIA_URL = "https://genemania.org/search/%s/%s" UCSC_REFSEQ = "http://genome.cse.ucsc.edu/cgi-bin/hgTracks?db=%s&hgg_gene=%s&hgg_chrom=chr%s&hgg_start=%s&hgg_end=%s" BIOGPS_URL = "http://biogps.org/?org=%s#goto=genereport&id=%s" STRING_URL = "http://string-db.org/newstring_cgi/show_network_section.pl?identifier=%s" @@ -87,4 +88,3 @@ if not valid_path(JSON_GENODIR): PORTADDR = "http://50.16.251.170" INFOPAGEHREF = '/dbdoc/%s.html' CGIDIR = '/webqtl/' #XZ: The variable name 'CGIDIR' should be changed to 'PYTHONDIR' -SCRIPTFILE = 'main.py' diff --git a/wqflask/wqflask/show_trait/show_trait.py b/wqflask/wqflask/show_trait/show_trait.py index 66d3a448..e10b31c0 100644 --- a/wqflask/wqflask/show_trait/show_trait.py +++ b/wqflask/wqflask/show_trait/show_trait.py @@ -257,7 +257,7 @@ class ShowTrait(object): self.genbank_link = webqtlConfig.GENBANK_ID % genbank_id self.genotation_link = self.gtex_link = self.genebridge_link = self.ucsc_blat_link = self.biogps_link = None - self.string_link = self.panther_link = self.aba_link = self.ebi_gwas_link = self.wiki_pi_link = None + self.string_link = self.panther_link = self.aba_link = self.ebi_gwas_link = self.wiki_pi_link = self.genemania_link = None if self.this_trait.symbol: self.genotation_link = webqtlConfig.GENOTATION_URL % self.this_trait.symbol self.gtex_link = webqtlConfig.GTEX_URL % self.this_trait.symbol @@ -266,7 +266,10 @@ class ShowTrait(object): self.ebi_gwas_link = webqtlConfig.EBIGWAS_URL % self.this_trait.symbol if self.dataset.group.species == "mouse" or self.dataset.group.species == "human": - self.genebridge_link = webqtlConfig.GENEBRIDGE_URL % (self.this_trait.symbol, self.dataset.group.species) + if self.dataset.group.species == "mouse": + self.genemania_link = webqtlConfig.GENEMANIA_URL % ("mus-musculus", self.this_trait.symbol) + else: + self.genemania_link = webqtlConfig.GENEMANIA_URL % ("homo-sapiens", self.this_trait.symbol) if self.dataset.group.species == "mouse": self.aba_link = webqtlConfig.ABA_URL % self.this_trait.symbol @@ -282,8 +285,10 @@ class ShowTrait(object): self.ucsc_blat_link = webqtlConfig.UCSC_REFSEQ % ('mm10', self.this_trait.refseq_transcriptid, chr, transcript_start, transcript_end) if self.dataset.group.species == "rat": + self.genemania_link = webqtlConfig.GENEMANIA_URL % ("rattus-norvegicus", self.this_trait.symbol) + query = """SELECT kgID, chromosome, txStart, txEnd - FROM GeneLink_rn33 + FROM GeneList_rn33 WHERE geneSymbol = '{}'""".format(self.this_trait.symbol) kgId, chr, transcript_start, transcript_end = g.db.execute(query).fetchall()[0] if len(g.db.execute(query).fetchall()) > 0 else None diff --git a/wqflask/wqflask/templates/collections/view.html b/wqflask/wqflask/templates/collections/view.html index 8d22b2a8..7e1001fc 100644 --- a/wqflask/wqflask/templates/collections/view.html +++ b/wqflask/wqflask/templates/collections/view.html @@ -84,8 +84,8 @@ - - + +

diff --git a/wqflask/wqflask/templates/search_result_page.html b/wqflask/wqflask/templates/search_result_page.html index e20ea2d3..bf434452 100644 --- a/wqflask/wqflask/templates/search_result_page.html +++ b/wqflask/wqflask/templates/search_result_page.html @@ -324,7 +324,11 @@ 'width': "500px", 'data': null, 'render': function(data, type, row, meta) { - return decodeURIComponent(escape(data.description)) + try { + return decodeURIComponent(escape(data.description)) + } catch { + return escape(data.description) + } } }, { diff --git a/wqflask/wqflask/templates/show_trait_details.html b/wqflask/wqflask/templates/show_trait_details.html index 09f36021..43efa314 100644 --- a/wqflask/wqflask/templates/show_trait_details.html +++ b/wqflask/wqflask/templates/show_trait_details.html @@ -88,6 +88,12 @@    {% endif %} + {% if genemania_link %} + + GeneMANIA + +    + {% endif %} {% if unigene_link %} UniGene -- cgit v1.2.3