17
17
from qiita_db .exceptions import QiitaDBUnknownIDError
18
18
19
19
from json import dumps
20
+ from collections import Counter
20
21
21
22
22
23
class AdminProcessingJobBaseClass (BaseHandler ):
@@ -109,21 +110,43 @@ def post(self):
109
110
# Get user-inputted qiita id and sample names
110
111
qid = self .get_argument ("qid" )
111
112
snames = self .get_argument ("snames" ).split ()
112
- error , matching , missing , extra , blank = [None ]* 5
113
+ error , matching , missing , extra , blank , duplicates = [None ]* 6
113
114
114
115
# Stripping leading qiita id from sample names
115
116
# Example: 1.SKB1.640202 -> SKB1.640202
116
117
try :
117
- qsnames = list (Study (qid ).sample_template )
118
+ sample_info = Study (qid ).sample_template
119
+ qsnames = list (sample_info )
118
120
except TypeError :
119
121
error = f'Study { qid } seems to have no sample template'
120
122
except QiitaDBUnknownIDError :
121
123
error = f'Study { qid } does not exist'
122
124
123
125
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 sample_info .categories :
129
+ for k , v in sample_info .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
+
124
137
for i , qsname in enumerate (qsnames ):
125
138
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 ]
127
150
128
151
# Remove blank samples from sample names
129
152
blank = [x for x in snames if x .lower ().startswith ('blank' )]
@@ -137,4 +160,5 @@ def post(self):
137
160
extra = snames .difference (qsnames )
138
161
139
162
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 )
0 commit comments