Skip to content

Commit 1d4bb5d

Browse files
Merge pull request #566 from colts661/ywang_sample_summary_project
Fix sample summary project output
2 parents 031e066 + fcf78b9 commit 1d4bb5d

File tree

3 files changed

+64
-8
lines changed

3 files changed

+64
-8
lines changed

microsetta_private_api/admin/admin_impl.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -491,6 +491,7 @@ def query_project_barcode_stats(body, token_info, strip_sampleid):
491491
def query_barcode_stats(body, token_info, strip_sampleid):
492492
validate_admin_access(token_info)
493493
if 'sample_barcodes' in body:
494+
project_id = None
494495
barcodes = body["sample_barcodes"]
495496
elif 'project_id' in body:
496497
project_id = body["project_id"]
@@ -502,7 +503,7 @@ def query_barcode_stats(body, token_info, strip_sampleid):
502503
unprocessed_barcodes = barcodes[1000:]
503504
barcodes = barcodes[0:1000]
504505

505-
results = {'samples': per_sample(None, barcodes, strip_sampleid)}
506+
results = {'samples': per_sample(project_id, barcodes, strip_sampleid)}
506507

507508
if unprocessed_barcodes:
508509
results['partial_result'] = True

microsetta_private_api/admin/sample_summary.py

+11-7
Original file line numberDiff line numberDiff line change
@@ -23,13 +23,12 @@ def per_sample(project, barcodes, strip_sampleid):
2323
template_repo = SurveyTemplateRepo(t)
2424
vs_repo = VioscreenSessionRepo(t)
2525

26-
if project is not None:
27-
project_barcodes = admin_repo.get_project_barcodes(project)
28-
else:
29-
project = 'Unspecified'
30-
26+
# all associated projects returned for each barcode,
27+
# so no universal project needed
3128
if barcodes is None:
32-
barcodes = project_barcodes
29+
if project is None:
30+
return summaries
31+
barcodes = admin_repo.get_project_barcodes(project)
3332

3433
for barcode in barcodes:
3534
diag = admin_repo.retrieve_diagnostics_by_barcode(barcode)
@@ -44,6 +43,11 @@ def per_sample(project, barcodes, strip_sampleid):
4443
source_type = None if source is None else source.source_type
4544
vio_id = None
4645

46+
# find all projects for barcode
47+
projects_info = diag['projects_info']
48+
all_projects = [proj_obj['project'] for proj_obj in projects_info]
49+
barcode_project = '; '.join(sorted(all_projects))
50+
4751
if source is not None and source_type == Source.SOURCE_TYPE_HUMAN:
4852

4953
vio_id = template_repo.get_vioscreen_id_if_exists(account.id,
@@ -82,7 +86,7 @@ def per_sample(project, barcodes, strip_sampleid):
8286

8387
summary = {
8488
"sampleid": None if strip_sampleid else barcode,
85-
"project": project,
89+
"project": barcode_project,
8690
"source-type": source_type,
8791
"site-sampled": sample_site,
8892
"sample-date": sample_date,

microsetta_private_api/admin/tests/test_admin_api.py

+51
Original file line numberDiff line numberDiff line change
@@ -1252,6 +1252,57 @@ def test_query_barcode_stats_project_barcodes_with_strip(self):
12521252
self.assertEqual([v['sample-status'] for v in response_obj],
12531253
exp_status)
12541254

1255+
def test_query_barcode_stats_multiple_projects(self):
1256+
barcodes = ['000005059', '000005078', '000005103']
1257+
input_json = json.dumps({'sample_barcodes': barcodes})
1258+
1259+
response = self.client.post(
1260+
"api/admin/account_barcode_summary?strip_sampleid=False",
1261+
content_type='application/json',
1262+
data=input_json,
1263+
headers=MOCK_HEADERS
1264+
)
1265+
# an empty string project should be unknown
1266+
self.assertEqual(200, response.status_code)
1267+
1268+
response_obj = json.loads(response.data)
1269+
self.assertIn('samples', response_obj)
1270+
response_obj = response_obj['samples']
1271+
self.assertEqual(len(response_obj), 3)
1272+
1273+
self.assertEqual([v['sampleid'] for v in response_obj], barcodes)
1274+
response_projects = [v['project'].split('; ') for v in response_obj]
1275+
self.assertEqual(len(response_projects[0]), 2)
1276+
self.assertEqual(len(response_projects[1]), 3)
1277+
self.assertEqual(len(response_projects[2]), 3)
1278+
self.assertTrue(all(
1279+
projs[0] == 'American Gut Project' for projs in response_projects
1280+
))
1281+
# content: should have exactly the same projects
1282+
self.assertEqual(response_projects[1], response_projects[2])
1283+
1284+
def test_query_barcode_stats_by_project(self):
1285+
input_json = json.dumps({'project_id': 19}) # expect 5 barcodes
1286+
exp_barcodes = [
1287+
'000035369', '000035370', '000035371',
1288+
'000035372', '000035373'
1289+
]
1290+
1291+
response = self.client.post(
1292+
"api/admin/account_barcode_summary?strip_sampleid=False",
1293+
content_type='application/json',
1294+
data=input_json,
1295+
headers=MOCK_HEADERS
1296+
)
1297+
# an empty string project should be unknown
1298+
self.assertEqual(200, response.status_code)
1299+
1300+
response_obj = json.loads(response.data)
1301+
self.assertIn('samples', response_obj)
1302+
response_obj = response_obj['samples']
1303+
self.assertEqual(len(response_obj), 5)
1304+
self.assertEqual([v['sampleid'] for v in response_obj], exp_barcodes)
1305+
12551306
def test_send_email(self):
12561307
def mock_func(*args, **kwargs):
12571308
pass

0 commit comments

Comments
 (0)