Skip to content

Commit d777469

Browse files
committed
Addressing comments from @ElDeveloper and @squirrelo
1 parent 0829f95 commit d777469

File tree

3 files changed

+55
-51
lines changed

3 files changed

+55
-51
lines changed

qiita_db/search.py

Lines changed: 11 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -116,8 +116,6 @@ def __repr__(self):
116116

117117

118118
class SearchTerm(object):
119-
# column names from study_sample table
120-
required_cols = set(get_table_cols("study_sample"))
121119
# column names from study table
122120
study_cols = set(get_table_cols("study"))
123121

@@ -140,9 +138,7 @@ def generate_sql(self):
140138
if operator not in allowable_types[argument_type]:
141139
raise QiitaDBIncompatibleDatatypeError(operator, argument_type)
142140

143-
if column_name in self.required_cols:
144-
column_name = "r.%s" % column_name.lower()
145-
elif column_name in self.study_cols:
141+
if column_name in self.study_cols:
146142
column_name = "st.%s" % column_name.lower()
147143
else:
148144
column_name = "sa.%s" % column_name.lower()
@@ -167,8 +163,6 @@ def __repr__(self):
167163
class QiitaStudySearch(object):
168164
"""QiitaStudySearch object to parse and run searches on studies."""
169165

170-
# column names from study_sample table
171-
required_cols = set(get_table_cols("study_sample"))
172166
# column names from study table
173167
study_cols = set(get_table_cols("study"))
174168

@@ -310,9 +304,9 @@ def _parse_study_search_string(self, searchstr,
310304
meta_header_type_lookup[header] = 'varchar'
311305

312306
# create the study finding SQL
313-
# remove metadata headers that are in study_sample table
314-
meta_headers = tuple(meta_headers.difference(
315-
self.required_cols).difference(self.study_cols))
307+
# remove metadata headers that are in study table
308+
meta_headers.discard('sample_id')
309+
meta_headers = tuple(meta_headers.difference(self.study_cols))
316310

317311
# get all study ids that contain all metadata categories searched for
318312
sql = []
@@ -341,17 +335,17 @@ def _parse_study_search_string(self, searchstr,
341335
# build the sql formatted list of metadata headers
342336
header_info = []
343337
for meta in meta_header_type_lookup:
344-
if meta in self.required_cols:
345-
header_info.append("r.%s" % meta)
346-
elif meta in self.study_cols:
338+
if meta in self.study_cols:
347339
header_info.append("st.%s" % meta)
348340
else:
349341
header_info.append("sa.%s" % meta)
350342
# build the SQL query
351-
sample_sql = ("SELECT r.sample_id,%s FROM qiita.study_sample "
352-
"r JOIN qiita.sample_{0} sa ON sa.sample_id = "
353-
"r.sample_id JOIN qiita.study st ON st.study_id = "
354-
"r.study_id WHERE %s" %
343+
344+
sample_sql = ("SELECT ss.sample_id,%s "
345+
"FROM qiita.study_sample ss "
346+
"JOIN qiita.sample_{0} sa USING (sample_id) "
347+
"JOIN qiita.study st USING (study_id) "
348+
"WHERE %s" %
355349
(','.join(header_info), sql_where))
356350
return study_sql, sample_sql, meta_header_type_lookup.keys()
357351

qiita_db/test/test_search.py

Lines changed: 37 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -27,10 +27,11 @@ def test_parse_study_search_string(self):
2727
exp_st_sql = ("SELECT study_id FROM qiita.study_sample_columns WHERE "
2828
"lower(column_name) = lower('altitude') and column_type "
2929
"in ('integer', 'float8')")
30-
exp_samp_sql = ("SELECT r.sample_id,sa.altitude FROM "
31-
"qiita.study_sample r JOIN qiita.sample_{0} sa"
32-
" ON sa.sample_id = r.sample_id JOIN qiita.study st ON"
33-
" st.study_id = r.study_id WHERE sa.altitude > 0")
30+
exp_samp_sql = ("SELECT ss.sample_id,sa.altitude "
31+
"FROM qiita.study_sample ss "
32+
"JOIN qiita.sample_{0} sa USING (sample_id) "
33+
"JOIN qiita.study st USING (study_id) "
34+
"WHERE sa.altitude > 0")
3435
self.assertEqual(st_sql, exp_st_sql)
3536
self.assertEqual(samp_sql, exp_samp_sql)
3637
self.assertEqual(meta, ["altitude"])
@@ -41,11 +42,11 @@ def test_parse_study_search_string(self):
4142
exp_st_sql = ("SELECT study_id FROM qiita.study_sample_columns WHERE "
4243
"lower(column_name) = lower('altitude') and column_type "
4344
"in ('integer', 'float8')")
44-
exp_samp_sql = ("SELECT r.sample_id,sa.altitude FROM "
45-
"qiita.study_sample r JOIN qiita.sample_{0} sa"
46-
" ON sa.sample_id = r.sample_id JOIN qiita.study st ON"
47-
" st.study_id = r.study_id WHERE NOT "
48-
"sa.altitude > 0")
45+
exp_samp_sql = ("SELECT ss.sample_id,sa.altitude "
46+
"FROM qiita.study_sample ss "
47+
"JOIN qiita.sample_{0} sa USING (sample_id) "
48+
"JOIN qiita.study st USING (study_id) "
49+
"WHERE NOT sa.altitude > 0")
4950
self.assertEqual(st_sql, exp_st_sql)
5051
self.assertEqual(samp_sql, exp_samp_sql)
5152
self.assertEqual(meta, ["altitude"])
@@ -56,11 +57,11 @@ def test_parse_study_search_string(self):
5657
exp_st_sql = ("SELECT study_id FROM qiita.study_sample_columns WHERE "
5758
"lower(column_name) = lower('ph') and column_type in "
5859
"('integer', 'float8')")
59-
exp_samp_sql = ("SELECT r.sample_id,sa.ph FROM "
60-
"qiita.study_sample r JOIN qiita.sample_{0} sa"
61-
" ON sa.sample_id = r.sample_id JOIN qiita.study st ON"
62-
" st.study_id = r.study_id WHERE (sa.ph > 7 AND "
63-
"sa.ph < 9)")
60+
exp_samp_sql = ("SELECT ss.sample_id,sa.ph "
61+
"FROM qiita.study_sample ss "
62+
"JOIN qiita.sample_{0} sa USING (sample_id) "
63+
"JOIN qiita.study st USING (study_id) "
64+
"WHERE (sa.ph > 7 AND sa.ph < 9)")
6465
self.assertEqual(st_sql, exp_st_sql)
6566
self.assertEqual(samp_sql, exp_samp_sql)
6667
self.assertEqual(meta, ["ph"])
@@ -71,11 +72,11 @@ def test_parse_study_search_string(self):
7172
exp_st_sql = ("SELECT study_id FROM qiita.study_sample_columns WHERE "
7273
"lower(column_name) = lower('ph') and column_type in "
7374
"('integer', 'float8')")
74-
exp_samp_sql = ("SELECT r.sample_id,sa.ph FROM "
75-
"qiita.study_sample r JOIN qiita.sample_{0} sa"
76-
" ON sa.sample_id = r.sample_id JOIN qiita.study st ON"
77-
" st.study_id = r.study_id WHERE (sa.ph > 7 OR "
78-
"sa.ph < 9)")
75+
exp_samp_sql = ("SELECT ss.sample_id,sa.ph "
76+
"FROM qiita.study_sample ss "
77+
"JOIN qiita.sample_{0} sa USING (sample_id) "
78+
"JOIN qiita.study st USING (study_id) "
79+
"WHERE (sa.ph > 7 OR sa.ph < 9)")
7980
self.assertEqual(st_sql, exp_st_sql)
8081
self.assertEqual(samp_sql, exp_samp_sql)
8182
self.assertEqual(meta, ["ph"])
@@ -87,11 +88,12 @@ def test_parse_study_search_string(self):
8788
exp_st_sql = ("SELECT study_id FROM qiita.study_sample_columns "
8889
"WHERE lower(column_name) = lower('host_subject_id') "
8990
"and column_type in ('varchar')")
90-
exp_samp_sql = ("SELECT r.sample_id,sa.host_subject_id FROM "
91-
"qiita.study_sample r JOIN qiita.sample_{0} sa"
92-
" ON sa.sample_id = r.sample_id JOIN qiita.study st ON"
93-
" st.study_id = r.study_id WHERE "
94-
"LOWER(sa.host_subject_id) LIKE '%chicken little%'")
91+
exp_samp_sql = ("SELECT ss.sample_id,sa.host_subject_id "
92+
"FROM qiita.study_sample ss "
93+
"JOIN qiita.sample_{0} sa USING (sample_id) "
94+
"JOIN qiita.study st USING (study_id) "
95+
"WHERE LOWER(sa.host_subject_id) "
96+
"LIKE '%chicken little%'")
9597
self.assertEqual(st_sql, exp_st_sql)
9698
self.assertEqual(samp_sql, exp_samp_sql)
9799
self.assertEqual(meta, ["host_subject_id"])
@@ -106,11 +108,12 @@ def test_parse_study_search_string(self):
106108
"lower(column_name) = lower('name') and column_type in "
107109
"('varchar')")
108110
exp_samp_sql = (
109-
"SELECT r.sample_id,sa.name FROM qiita.study_sample r JOIN"
110-
" qiita.sample_{0} sa ON sa.sample_id = r.sample_id JOIN "
111-
"qiita.study st ON st.study_id = r.study_id WHERE (sa.name = "
112-
"'Billy Bob' OR sa.name = 'Timmy' OR (sa.name = 'Jimbo' AND "
113-
"sa.name > 25) OR sa.name < 5)")
111+
"SELECT ss.sample_id,sa.name "
112+
"FROM qiita.study_sample ss "
113+
"JOIN qiita.sample_{0} sa USING (sample_id) "
114+
"JOIN qiita.study st USING (study_id) "
115+
"WHERE (sa.name = 'Billy Bob' OR sa.name = 'Timmy' OR "
116+
"(sa.name = 'Jimbo' AND sa.name > 25) OR sa.name < 5)")
114117
self.assertEqual(st_sql, exp_st_sql)
115118
self.assertEqual(samp_sql, exp_samp_sql)
116119
self.assertEqual(meta, ['name'])
@@ -126,11 +129,11 @@ def test_parse_study_search_string(self):
126129
"('integer', 'float8')", "SELECT study_id FROM "
127130
"qiita.study_sample_columns WHERE lower(column_name) = "
128131
"lower('ph') and column_type in ('integer', 'float8')"]
129-
exp_samp_sql = ("SELECT r.sample_id,sa.pH,sa.ph FROM "
130-
"qiita.study_sample r JOIN qiita.sample_{0} sa"
131-
" ON sa.sample_id = r.sample_id JOIN qiita.study st ON"
132-
" st.study_id = r.study_id WHERE (sa.ph > 7 OR "
133-
"sa.ph < 9)")
132+
exp_samp_sql = ("SELECT ss.sample_id,sa.pH,sa.ph "
133+
"FROM qiita.study_sample ss "
134+
"JOIN qiita.sample_{0} sa USING (sample_id) "
135+
"JOIN qiita.study st USING (study_id) "
136+
"WHERE (sa.ph > 7 OR sa.ph < 9)")
134137
# use the split list to make sure the SQL is properly formed
135138
self.assertEqual(len(st_sql), 2)
136139
pos = exp_st_sql.index(st_sql[0])

qiita_db/util.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -951,6 +951,13 @@ def get_processed_params_tables():
951951

952952

953953
def get_lat_longs():
954+
"""Retrieve the latitude and longitude of all the samples in the DB
955+
956+
Returns
957+
-------
958+
list of [float, float]
959+
The latitude and longitude for each sample in the database
960+
"""
954961
conn = SQLConnectionHandler()
955962
sql = """SELECT DISTINCT table_name
956963
FROM information_schema.columns

0 commit comments

Comments
 (0)