Skip to content

Commit 160955e

Browse files
Merge pull request #3295 from antgonza/fix-3275
fix-3275
2 parents fc17977 + 1fa83a9 commit 160955e

File tree

3 files changed

+54
-5
lines changed

3 files changed

+54
-5
lines changed

qiita_pet/handlers/admin_processing_job.py

Lines changed: 28 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
from qiita_db.exceptions import QiitaDBUnknownIDError
1818

1919
from json import dumps
20+
from collections import Counter
2021

2122

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

114115
# Stripping leading qiita id from sample names
115116
# Example: 1.SKB1.640202 -> SKB1.640202
116117
try:
117-
qsnames = list(Study(qid).sample_template)
118+
st = Study(qid).sample_template
119+
qsnames = list(st)
118120
except TypeError:
119121
error = f'Study {qid} seems to have no sample template'
120122
except QiitaDBUnknownIDError:
121123
error = f'Study {qid} does not exist'
122124

123125
if error is None:
126+
# if tube_id is present then this should take precedence in qsnames
127+
tube_ids = dict()
128+
if "tube_id" in st.categories:
129+
for k, v in st.get_category("tube_id").items():
130+
# ignoring empty values
131+
if v in (None, 'None', ''):
132+
continue
133+
if k.startswith(qid):
134+
k = k.replace(f'{qid}.', "", 1)
135+
tube_ids[k] = v
136+
124137
for i, qsname in enumerate(qsnames):
125138
if qsname.startswith(qid):
126-
qsnames[i] = qsname.replace(f'{qid}.', "", 1)
139+
qsname = qsname.replace(f'{qid}.', "", 1)
140+
if qsname in tube_ids:
141+
nname = f'{qsname}, tube_id: {tube_ids[qsname]}'
142+
snames = [s if s != tube_ids[qsname] else nname
143+
for s in snames]
144+
qsname = nname
145+
qsnames[i] = qsname
146+
147+
# Finds duplicates in the samples
148+
seen = Counter(snames)
149+
duplicates = [f'{s} \u00D7 {seen[s]}' for s in seen if seen[s] > 1]
127150

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

139162
self.render("sample_validation.html", input=False, matching=matching,
140-
missing=missing, extra=extra, blank=blank, error=error)
163+
missing=missing, extra=extra, blank=blank,
164+
duplicates=duplicates, error=error)

qiita_pet/templates/sample_validation.html

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
<style>
44
.column {
55
float: left;
6-
width: 25%;
6+
width: 20%;
77
}
88

99
.row:after {
@@ -53,6 +53,14 @@ <h2>Blank</h2>
5353
{% end %}
5454
</ul>
5555
</div>
56+
<div class="column">
57+
<h2>Duplicates</h2>
58+
<ul>
59+
{% for sample in duplicates %}
60+
<li>{{ sample }}</li>
61+
{% end %}
62+
</ul>
63+
</div>
5664
<div class="column">
5765
<h2>Extra</h2>
5866
<ul>

qiita_pet/test/test_admin_processing_job_handlers.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,10 @@
1010
from json import loads
1111

1212
from mock import Mock
13+
import pandas as pd
1314

1415
from qiita_db.user import User
16+
from qiita_db.metadata_template.sample_template import SampleTemplate as ST
1517
from qiita_pet.handlers.base_handlers import BaseHandler
1618
from qiita_pet.test.tornado_test_base import TestHandlerBase
1719

@@ -63,6 +65,21 @@ def test_post(self):
6365
for name in snames:
6466
self.assertIn(name, body)
6567

68+
# Check succes with tube_id
69+
md_dict = {'SKB1.640202': {'tube_id': '12345'}}
70+
md_ext = pd.DataFrame.from_dict(md_dict, orient='index', dtype=str)
71+
ST(1).extend(md_ext)
72+
post_args = {
73+
'qid': 1,
74+
'snames': '12345 SKB2.640194 BLANK.1A BLANK.1B'
75+
}
76+
response = self.post('/admin/sample_validation/', post_args)
77+
self.assertEqual(response.code, 200)
78+
snames = ['SKB2.640194', 'SKB1.640202, tube_id: 12345']
79+
body = response.body.decode('ascii')
80+
for name in snames:
81+
self.assertIn(name, body)
82+
6683
# Check failure: invalid qiita id
6784
post_args = {
6885
'qid': 2,

0 commit comments

Comments
 (0)