aboutsummaryrefslogtreecommitdiff
path: root/gn3/db
diff options
context:
space:
mode:
authorFrederick Muriuki Muriithi2022-01-12 11:14:31 +0300
committerFrederick Muriuki Muriithi2022-01-12 11:14:31 +0300
commit394a785da393ad659d198526f7c27a2191e90e9e (patch)
tree9137df072cdbfffd6e8443ec14c996710070e0d4 /gn3/db
parenta7f5d93bec04b9142c8a14fb17a5059f069c6326 (diff)
downloadgenenetwork3-394a785da393ad659d198526f7c27a2191e90e9e.tar.gz
Deduplicate query to fetch data for 'Publish' traits
The queries run in the `get_trait_csv_sample_data` and `retrieve_publish_trait_data` functions in the `gn3.db.traits` module were mostly similar. This commit changes that, by making the `get_trait_csv_sample_data` function make use of the results from calling the `retrieve_publish_trait_data` function.
Diffstat (limited to 'gn3/db')
-rw-r--r--gn3/db/traits.py29
1 files changed, 9 insertions, 20 deletions
diff --git a/gn3/db/traits.py b/gn3/db/traits.py
index 338b320..9554be2 100644
--- a/gn3/db/traits.py
+++ b/gn3/db/traits.py
@@ -86,26 +86,15 @@ def get_trait_csv_sample_data(conn: Any,
if str(num_str)[-2:] == ".0":
return str(int(num_str))
return str(num_str)
- sql = ("SELECT DISTINCT Strain.Name, PublishData.value, "
- "PublishSE.error, NStrain.count FROM "
- "(PublishData, Strain, PublishXRef, PublishFreeze) "
- "LEFT JOIN PublishSE ON "
- "(PublishSE.DataId = PublishData.Id AND "
- "PublishSE.StrainId = PublishData.StrainId) "
- "LEFT JOIN NStrain ON (NStrain.DataId = PublishData.Id AND "
- "NStrain.StrainId = PublishData.StrainId) WHERE "
- "PublishXRef.InbredSetId = PublishFreeze.InbredSetId AND "
- "PublishData.Id = PublishXRef.DataId AND "
- "PublishXRef.Id = %s AND PublishXRef.PhenotypeId = %s "
- "AND PublishData.StrainId = Strain.Id Order BY Strain.Name")
- csv_data = ["Strain Name,Value,SE,Count"]
- with conn.cursor() as cursor:
- cursor.execute(sql, (trait_name, phenotype_id,))
- for record in cursor.fetchall():
- (strain_name, value, error, count) = record
- csv_data.append(
- ",".join([__float_strip(val) if val else "x"
- for val in (strain_name, value, error, count)]))
+ def __process_for_csv__(record):
+ return ",".join([
+ __float_strip(record[key]) if record[key] else "x"
+ for key in ("sample_name", "value", "se_error", "nstrain")])
+ csv_data = ["Strain Name,Value,SE,Count"] + [
+ __process_for_csv__(record) for record in
+ retrieve_publish_trait_data(
+ {"trait_name": trait_name, "db": {"dataset_id": phenotype_id}},
+ conn)]
return "\n".join(csv_data)