Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 23 additions & 28 deletions qiita_db/meta_util.py
Original file line number Diff line number Diff line change
Expand Up @@ -301,41 +301,36 @@ def sizeof_fmt(value, position):


def get_lat_longs():
"""Retrieve the latitude and longitude of all the samples in the DB
"""Retrieve the latitude and longitude of all the public samples in the DB

Returns
-------
list of [float, float]
The latitude and longitude for each sample in the database
"""
portal_table_ids = [
s.id for s in qdb.portal.Portal(qiita_config.portal).get_studies()]

with qdb.sql_connection.TRN:
# getting all tables in the portal
sql = """SELECT DISTINCT table_name
FROM information_schema.columns
WHERE table_name SIMILAR TO 'sample_[0-9]+'
AND SPLIT_PART(table_name, '_', 2)::int IN %s
AND table_schema = 'qiita'"""
qdb.sql_connection.TRN.add(sql, [tuple(portal_table_ids)])

# we are going to create multiple union selects to retrieve the
# latigute and longitude of all available studies. Note that UNION in
# PostgreSQL automatically removes duplicates
sql_query = """
SELECT CAST(sample_values->>'latitude' AS FLOAT),
CAST(sample_values->>'longitude' AS FLOAT)
FROM qiita.%s
WHERE isnumeric(sample_values->>'latitude') AND
isnumeric(sample_values->>'longitude')"""
sql = [sql_query % s
for s in qdb.sql_connection.TRN.execute_fetchflatten()]
sql = ' UNION '.join(sql)
qdb.sql_connection.TRN.add(sql)

# note that we are returning set to remove duplicates
return qdb.sql_connection.TRN.execute_fetchindex()
# getting all the public studies
studies = qdb.study.Study.get_by_status('public')

results = []
if studies:
# we are going to create multiple union selects to retrieve the
# latigute and longitude of all available studies. Note that
# UNION in PostgreSQL automatically removes duplicates
sql_query = """
SELECT CAST(sample_values->>'latitude' AS FLOAT),
CAST(sample_values->>'longitude' AS FLOAT)
FROM qiita.sample_%d
WHERE isnumeric(sample_values->>'latitude') AND
isnumeric(sample_values->>'longitude')"""
sql = [sql_query % s.id for s in studies]
sql = ' UNION '.join(sql)
qdb.sql_connection.TRN.add(sql)

# note that we are returning set to remove duplicates
results = qdb.sql_connection.TRN.execute_fetchindex()

return results


def generate_biom_and_metadata_release(study_status='public'):
Expand Down
32 changes: 13 additions & 19 deletions qiita_db/test/test_meta_util.py
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,14 @@ def test_validate_filepath_access_by_user(self):
qdb.study.Study.delete(study.id)

def test_get_lat_longs(self):
# no public studies should return an empty array
obs = qdb.meta_util.get_lat_longs()
self.assertItemsEqual(obs, [])

old_visibility = {}
for pt in qdb.study.Study(1).prep_templates():
old_visibility[pt] = pt.artifact.visibility
pt.artifact.visibility = 'public'
exp = [
[74.0894932572, 65.3283470202],
[57.571893782, 32.5563076447],
Expand All @@ -163,10 +171,12 @@ def test_get_lat_longs(self):
[95.2060749748, 27.3592668624],
[78.3634273709, 74.423907894],
[38.2627021402, 3.48274264219]]

obs = qdb.meta_util.get_lat_longs()
self.assertItemsEqual(obs, exp)

for k, v in old_visibility.iteritems():
k.artifact.visibility = v

def test_get_lat_longs_EMP_portal(self):
info = {
'timeseries_type_id': 1,
Expand Down Expand Up @@ -206,7 +216,7 @@ def test_get_lat_longs_EMP_portal(self):
qiita_config.portal = 'EMP'

obs = qdb.meta_util.get_lat_longs()
exp = [[42.42, 41.41]]
exp = []

self.assertItemsEqual(obs, exp)
qdb.metadata_template.sample_template.SampleTemplate.delete(st.id)
Expand All @@ -222,7 +232,7 @@ def test_update_redis_stats(self):
('number_of_samples', {'sandbox': '0', 'public': '0',
'private': '27'}, r_client.hgetall),
('num_users', '4', r_client.get),
('lat_longs', EXP_LAT_LONG, r_client.get),
('lat_longs', '[]', r_client.get),
('num_studies_ebi', '1', r_client.get),
('num_samples_ebi', '27', r_client.get),
('number_samples_ebi_prep', '54', r_client.get),
Expand Down Expand Up @@ -394,21 +404,5 @@ def test_generate_biom_and_metadata_release(self):
bdr = qdb.sql_connection.TRN.execute()


EXP_LAT_LONG = (
'[[60.1102854322, 74.7123248382], [23.1218032799, 42.838497795],'
' [3.21190859967, 26.8138925876], [74.0894932572, 65.3283470202],'
' [53.5050692395, 31.6056761814], [12.6245524972, 96.0693176066],'
' [43.9614715197, 82.8516734159], [10.6655599093, 70.784770579],'
' [78.3634273709, 74.423907894], [82.8302905615, 86.3615778099],'
' [44.9725384282, 66.1920014699], [4.59216095574, 63.5115213108],'
' [57.571893782, 32.5563076447], [40.8623799474, 6.66444220187],'
' [95.2060749748, 27.3592668624], [38.2627021402, 3.48274264219],'
' [13.089194595, 92.5274472082], [84.0030227585, 66.8954849864],'
' [68.51099627, 2.35063674718], [29.1499460692, 82.1270418227],'
' [35.2374368957, 68.5041623253], [12.7065957714, 84.9722975792],'
' [0.291867635913, 68.5945325743], [85.4121476399, 15.6526750776],'
' [68.0991287718, 34.8360987059]]')


if __name__ == '__main__':
main()