Skip to content

Fix #3275 #3297

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jun 2, 2023
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
32 changes: 28 additions & 4 deletions qiita_pet/handlers/admin_processing_job.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
from qiita_db.exceptions import QiitaDBUnknownIDError

from json import dumps
from collections import Counter


class AdminProcessingJobBaseClass(BaseHandler):
Expand Down Expand Up @@ -109,21 +110,43 @@ def post(self):
# Get user-inputted qiita id and sample names
qid = self.get_argument("qid")
snames = self.get_argument("snames").split()
error, matching, missing, extra, blank = [None]*5
error, matching, missing, extra, blank, duplicates = [None]*6

# Stripping leading qiita id from sample names
# Example: 1.SKB1.640202 -> SKB1.640202
try:
qsnames = list(Study(qid).sample_template)
sample_info = Study(qid).sample_template
qsnames = list(sample_info)
except TypeError:
error = f'Study {qid} seems to have no sample template'
except QiitaDBUnknownIDError:
error = f'Study {qid} does not exist'

if error is None:
# if tube_id is present then this should take precedence in qsnames
tube_ids = dict()
if "tube_id" in sample_info.categories:
for k, v in sample_info.get_category("tube_id").items():
# ignoring empty values
if v in (None, 'None', ''):
continue
if k.startswith(qid):
k = k.replace(f'{qid}.', "", 1)
tube_ids[k] = v

for i, qsname in enumerate(qsnames):
if qsname.startswith(qid):
qsnames[i] = qsname.replace(f'{qid}.', "", 1)
qsname = qsname.replace(f'{qid}.', "", 1)
if qsname in tube_ids:
nname = f'{qsname}, tube_id: {tube_ids[qsname]}'
snames = [s if s != tube_ids[qsname] else nname
for s in snames]
qsname = nname
qsnames[i] = qsname

# Finds duplicates in the samples
seen = Counter(snames)
duplicates = [f'{s} \u00D7 {seen[s]}' for s in seen if seen[s] > 1]

# Remove blank samples from sample names
blank = [x for x in snames if x.lower().startswith('blank')]
Expand All @@ -137,4 +160,5 @@ def post(self):
extra = snames.difference(qsnames)

self.render("sample_validation.html", input=False, matching=matching,
missing=missing, extra=extra, blank=blank, error=error)
missing=missing, extra=extra, blank=blank,
duplicates=duplicates, error=error)
10 changes: 9 additions & 1 deletion qiita_pet/templates/sample_validation.html
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<style>
.column {
float: left;
width: 25%;
width: 20%;
}

.row:after {
Expand Down Expand Up @@ -53,6 +53,14 @@ <h2>Blank</h2>
{% end %}
</ul>
</div>
<div class="column">
<h2>Duplicates</h2>
<ul>
{% for sample in duplicates %}
<li>{{ sample }}</li>
{% end %}
</ul>
</div>
<div class="column">
<h2>Extra</h2>
<ul>
Expand Down
18 changes: 17 additions & 1 deletion qiita_pet/test/test_admin_processing_job_handlers.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,11 @@

from unittest import main
from json import loads

import pandas as pd
from mock import Mock

from qiita_db.user import User
from qiita_db.metadata_template.sample_template import SampleTemplate as ST
from qiita_pet.handlers.base_handlers import BaseHandler
from qiita_pet.test.tornado_test_base import TestHandlerBase

Expand Down Expand Up @@ -63,6 +64,21 @@ def test_post(self):
for name in snames:
self.assertIn(name, body)

# Check success with tube_id
md_dict = {'SKB1.640202': {'tube_id': '12345'}}
md_ext = pd.DataFrame.from_dict(md_dict, orient='index', dtype=str)
ST(1).extend(md_ext)
post_args = {
'qid': 1,
'snames': '12345 SKB2.640194 BLANK.1A BLANK.1B'
}
response = self.post('/admin/sample_validation/', post_args)
self.assertEqual(response.code, 200)
snames = ['SKB2.640194', 'SKB1.640202, tube_id: 12345']
body = response.body.decode('ascii')
for name in snames:
self.assertIn(name, body)

# Check failure: invalid qiita id
post_args = {
'qid': 2,
Expand Down