Skip to content

Commit

Permalink
generalizing this functionality
Browse files Browse the repository at this point in the history
  • Loading branch information
antgonza committed Jan 28, 2017
1 parent 19a9dda commit 6f0dd71
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 31 deletions.
40 changes: 17 additions & 23 deletions qiita_pet/handlers/api_proxy/studies.py
Original file line number Diff line number Diff line change
Expand Up @@ -248,34 +248,28 @@ def study_files_get_req(user_id, study_id, prep_template_id, artifact_type):
if any(ftypes_if) and 'run_prefix' in pt.columns:
prep_prefixes = tuple(set(pt['run_prefix']))
num_prefixes = len(prep_prefixes)
# special case for per_sample_FASTQ
if artifact_type == 'per_sample_FASTQ':
# sorting prefixes by length to avoid collisions like: 100 1002
# 10003
prep_prefixes = sorted(prep_prefixes, key=len, reverse=True)
# group files by prefix
sfiles = {p: [f for _, f in uploaded if f.startswith(p)]
for p in prep_prefixes}
for k, v in viewitems(sfiles):
len_files = len(v)
if len_files != 1 and len_files != 2:
remaining.extend(v)
else:
v.sort()
selected.append(v)
else:
len_files = 1
for _, filename in uploaded:
if filename.startswith(prep_prefixes):
selected.append(filename)
else:
remaining.append(filename)
# sorting prefixes by length to avoid collisions like: 100 1002
# 10003
prep_prefixes = sorted(prep_prefixes, key=len, reverse=True)
# group files by prefix
sfiles = {p: [f for _, f in uploaded if f.startswith(p)]
for p in prep_prefixes}
inuse = [y for x in sfiles.values() for y in x]
remaining.extend([f for _, f in uploaded if f not in inuse])

for k, v in viewitems(sfiles):
len_files = len(v)
if len_files != 1 and len_files != 2:
remaining.extend(v)
else:
v.sort()
selected.append(v)
else:
num_prefixes = 0
remaining = [f for _, f in uploaded]

# get file_types, format: filetype, required, list of files
file_types = [(t, req, [x[i] for x in selected if i+1 <= len_files])
file_types = [(t, req, [x[i] for x in selected if i+1 <= len(x)])
for i, (t, req) in enumerate(supp_file_types)]

# Create a list of artifacts that the user has access to, in case that
Expand Down
10 changes: 7 additions & 3 deletions qiita_pet/handlers/api_proxy/tests/test_studies.py
Original file line number Diff line number Diff line change
Expand Up @@ -238,6 +238,8 @@ def test_study_prep_get_req(self):
for i in range(4, 0, -1):
qdb.artifact.Artifact(i).visibility = "private"

qdb.metadata_template.prep_template.PrepTemplate.delete(pt.id)

def test_study_prep_get_req_failed_EBI(self):
temp_dir = mkdtemp()
self._clean_up_files.append(temp_dir)
Expand Down Expand Up @@ -337,6 +339,8 @@ def test_study_prep_get_req_failed_EBI(self):
'status': 'success'}
self.assertEqual(obs, exp)

qdb.metadata_template.prep_template.PrepTemplate.delete(pt.id)

def test_study_prep_get_req_no_access(self):
obs = study_prep_get_req(1, 'demo@microbio.me')
exp = {'status': 'error',
Expand Down Expand Up @@ -463,7 +467,7 @@ def test_study_files_get_req_per_sample_FASTQ(self):
'shared@foo.bar', 1, pt.id, 'per_sample_FASTQ')
exp = {
'status': 'success', 'num_prefixes': 2, 'artifacts': [],
'remaining': [], 'message': '',
'remaining': ['uploaded_file.txt'], 'message': '',
'file_types': [
('raw_forward_seqs', True,
['test_2.R1.fastq.gz', 'test_1.R1.fastq.gz']),
Expand All @@ -479,7 +483,7 @@ def test_study_files_get_req_per_sample_FASTQ(self):
obs = study_files_get_req(
'shared@foo.bar', 1, pt.id, 'per_sample_FASTQ')
exp = {'status': 'success', 'num_prefixes': 2, 'artifacts': [],
'remaining': [], 'message': '',
'remaining': ['uploaded_file.txt'], 'message': '',
'file_types': [('raw_forward_seqs', True,
['test_2.R1.fastq.gz', 'test_1.R1.fastq.gz']),
('raw_reverse_seqs', False,
Expand All @@ -496,7 +500,7 @@ def test_study_files_get_req_per_sample_FASTQ(self):
'shared@foo.bar', 1, pt.id, 'per_sample_FASTQ')
exp = {'status': 'success', 'num_prefixes': 2, 'artifacts': [],
'remaining': ['test_1.R1.fastq.gz', 'test_1.R2.fastq.gz',
'test_1.R3.fastq.gz'],
'test_1.R3.fastq.gz', 'uploaded_file.txt'],
'message': '',
'file_types': [('raw_forward_seqs', True,
['test_2.R1.fastq.gz']),
Expand Down
6 changes: 3 additions & 3 deletions qiita_pet/templates/study_ajax/add_artifact.html
Original file line number Diff line number Diff line change
Expand Up @@ -90,9 +90,6 @@ <h4><i>No files attached to this preparation</i></h4>

<form action="{% raw qiita_config.portal_dir %}/study/new_artifact/" method="POST" id="create-artifact-form">
<div class="row">
<div class="col-md-12">
<b>Name:</b> <input type="text" id="name" name="name" maxlength="35" required>
</div>
<div class="col-md-12">
<b>Select type:</b>
<select name="artifact-type" id="artifact-type">
Expand All @@ -102,6 +99,9 @@ <h4><i>No files attached to this preparation</i></h4>
{% end %}
</select>
</div>
<div class="col-md-12">
<b>Add a name for the file:</b> <input type="text" id="name" name="name" maxlength="35" required>
</div>
</div>
<div id="files-selector" hidden>
</div>
Expand Down
9 changes: 7 additions & 2 deletions qiita_pet/templates/study_ajax/artifact_file_selector.html
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,7 @@
</style>
<div class="row">
<div class="col-md-12">
<b>Import files from other studies:</b>
<b>Now, you can import files from other studies</b>
<select id="import-artifact" name="import-artifact">
<option value="">Choose an artfact to import...</option>
{% for a_id, label in artifacts %}
Expand All @@ -195,7 +195,12 @@
</div>
<div id="drag-files-div" class="row">
<div class="col-md-12">
<b>Or click and drag your uploaded files to the correct file type:</b><br/>
<b>or click and drag your uploaded files to the correct file type</b><br/>
<div class="blinking-message">
Note: the system will try to auto select the files based on run_prefix, if that doesn't work, either the type you selected doesn't support
it or the run_prefix is wrong
</div>

</div>
<div class="col-md-3" id="files-div">
<p style="text-align: center;"><i>Available Files</i></p>
Expand Down

0 comments on commit 6f0dd71

Please sign in to comment.