Skip to content

Commit 1dbd1b7

Browse files
committed
Fix commands and CLI
1 parent 41070c2 commit 1dbd1b7

File tree

3 files changed

+32
-50
lines changed

3 files changed

+32
-50
lines changed

qiita_db/commands.py

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -153,27 +153,24 @@ def load_sample_template_from_cmd(sample_temp_path, study_id):
153153
return SampleTemplate.create(sample_temp, Study(study_id))
154154

155155

156-
def load_prep_template_from_cmd(prep_temp_path, raw_data_id, study_id,
156+
def load_prep_template_from_cmd(prep_temp_path, study_id,
157157
data_type):
158158
r"""Adds a prep template to the database
159159
160160
Parameters
161161
----------
162162
prep_temp_path : str
163163
Path to the prep template file
164-
raw_data_id : int
165-
The raw data id to which the prep template belongs
166164
study_id : int
167165
The study id to which the prep template belongs
168166
data_type : str
169167
The data type of the prep template
170168
"""
171169
prep_temp = load_template_to_dataframe(prep_temp_path)
172-
return PrepTemplate.create(prep_temp, RawData(raw_data_id),
173-
Study(study_id), data_type)
170+
return PrepTemplate.create(prep_temp, Study(study_id), data_type)
174171

175172

176-
def load_raw_data_cmd(filepaths, filepath_types, filetype, study_ids):
173+
def load_raw_data_cmd(filepaths, filepath_types, filetype, prep_template_ids):
177174
"""Add new raw data by populating the relevant tables
178175
179176
Parameters
@@ -184,8 +181,8 @@ def load_raw_data_cmd(filepaths, filepath_types, filetype, study_ids):
184181
Describes the contents of the files.
185182
filetype : str
186183
The type of file being loaded
187-
study_ids : iterable of int
188-
The IDs of the studies with which to associate this raw data
184+
prep_template_ids : iterable of int
185+
The IDs of the prep templates with which to associate this raw data
189186
190187
Returns
191188
-------
@@ -202,9 +199,9 @@ def load_raw_data_cmd(filepaths, filepath_types, filetype, study_ids):
202199
filepath_types_dict = get_filepath_types()
203200
filepath_types = [filepath_types_dict[x] for x in filepath_types]
204201

205-
studies = [Study(x) for x in study_ids]
202+
prep_templates = [PrepTemplate(x) for x in prep_template_ids]
206203

207-
return RawData.create(filetype_id, studies,
204+
return RawData.create(filetype_id, prep_templates,
208205
filepaths=list(zip(filepaths, filepath_types)))
209206

210207

qiita_db/test/test_commands.py

Lines changed: 17 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@
1515
from future import standard_library
1616
from functools import partial
1717

18+
import pandas as pd
19+
1820
from qiita_db.commands import (load_study_from_cmd, load_raw_data_cmd,
1921
load_sample_template_from_cmd,
2022
load_prep_template_from_cmd,
@@ -28,6 +30,7 @@
2830
from qiita_db.data import RawData, PreprocessedData
2931
from qiita_db.util import (get_count, check_count, get_db_files_base_dir,
3032
get_mountpoint)
33+
from qiita_db.metadata_template import PrepTemplate
3134
from qiita_core.util import qiita_test_checker
3235
from qiita_ware.processing_pipeline import generate_demux_file
3336

@@ -154,36 +157,12 @@ def test_load_sample_template_from_cmd(self):
154157
@qiita_test_checker()
155158
class TestLoadPrepTemplateFromCmd(TestCase):
156159
def setUp(self):
157-
# Create a sample template file
158-
fd, seqs_fp = mkstemp(suffix='_seqs.fastq')
159-
close(fd)
160-
fd, barcodes_fp = mkstemp(suffix='_barcodes.fastq')
161-
close(fd)
162-
163-
with open(seqs_fp, "w") as f:
164-
f.write("\n")
165-
with open(barcodes_fp, "w") as f:
166-
f.write("\n")
167-
168160
self.pt_contents = PREP_TEMPLATE
169161

170-
self.raw_data = RawData.create(
171-
2, [Study(1)], filepaths=[(seqs_fp, 1), (barcodes_fp, 2)])
172-
173-
join_f = partial(join, join(get_db_files_base_dir(), 'raw_data'))
174-
self.files_to_remove = [
175-
join_f("%s_%s" % (self.raw_data.id, basename(seqs_fp))),
176-
join_f("%s_%s" % (self.raw_data.id, basename(barcodes_fp)))]
177-
178-
def tearDown(self):
179-
for fp in self.files_to_remove:
180-
if exists(fp):
181-
remove(fp)
182-
183162
def test_load_prep_template_from_cmd(self):
184163
"""Correctly adds a prep template to the DB"""
185164
fh = StringIO(self.pt_contents)
186-
st = load_prep_template_from_cmd(fh, self.raw_data.id, 1, '18S')
165+
st = load_prep_template_from_cmd(fh, 1, '18S')
187166
self.assertEqual(st.id, 2)
188167

189168

@@ -222,14 +201,24 @@ def test_load_data_from_cmd(self):
222201
'raw_barcodes']
223202

224203
filetype = 'FASTQ'
225-
study_ids = [1]
204+
metadata_dict = {
205+
'SKB8.640193': {'center_name': 'ANL',
206+
'primer': 'GTGCCAGCMGCCGCGGTAA',
207+
'barcode': 'GTCCGCAAGTTA',
208+
'run_prefix': "s_G1_L001_sequences",
209+
'platform': 'ILLUMINA',
210+
'library_construction_protocol': 'AAAA',
211+
'experiment_design_description': 'BBBB'}}
212+
metadata = pd.DataFrame.from_dict(metadata_dict, orient='index')
213+
pt1 = PrepTemplate.create(metadata, Study(1), "16S")
214+
prep_templates = [pt1.id]
226215

227216
initial_raw_count = get_count('qiita.raw_data')
228217
initial_fp_count = get_count('qiita.filepath')
229218
initial_raw_fp_count = get_count('qiita.raw_filepath')
230219

231220
new = load_raw_data_cmd(filepaths, filepath_types, filetype,
232-
study_ids)
221+
prep_templates)
233222
raw_data_id = new.id
234223
self.files_to_remove.append(
235224
join(self.db_test_raw_dir,
@@ -246,14 +235,12 @@ def test_load_data_from_cmd(self):
246235
initial_fp_count + 3))
247236
self.assertTrue(check_count('qiita.raw_filepath',
248237
initial_raw_fp_count + 3))
249-
self.assertTrue(check_count('qiita.study_raw_data',
250-
initial_raw_count + 1))
251238

252239
# Ensure that the ValueError is raised when a filepath_type is not
253240
# provided for each and every filepath
254241
with self.assertRaises(ValueError):
255242
load_raw_data_cmd(filepaths, filepath_types[:-1], filetype,
256-
study_ids)
243+
prep_templates)
257244

258245

259246
@qiita_test_checker()

scripts/qiita

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -109,13 +109,13 @@ def load_study(owner, title, info):
109109
@click.option('--filetype', required=True,
110110
type=click.Choice(get_filetypes().keys()),
111111
help='The type of data')
112-
@click.option('--study', multiple=True, help='Associate the data with this '
113-
'study. This option can be used multiple times if the data '
114-
'should be associated with multiple studies', type=int,
115-
required=True)
116-
def load_raw(fp, fp_type, filetype, study):
112+
@click.option('--prep_template', multiple=True, help='Associate the data with '
113+
'this prep template. This option can be used multiple times if '
114+
'the data should be associated with multiple prep templates',
115+
type=int, required=True)
116+
def load_raw(fp, fp_type, filetype, prep_template):
117117
"""Loads a raw data to the database"""
118-
raw_data = load_raw_data_cmd(fp, fp_type, filetype, study)
118+
raw_data = load_raw_data_cmd(fp, fp_type, filetype, prep_template)
119119
click.echo("Raw data successfully added to the database with id %s"
120120
% raw_data.id)
121121

@@ -202,16 +202,14 @@ def load_sample_template(fp, study):
202202
@db.command()
203203
@click.argument('fp', required=True,
204204
type=click.Path(resolve_path=True, readable=True, exists=True))
205-
@click.option('--raw_data', required=True, type=int,
206-
help='Associate the prep template with this raw data')
207205
@click.option('--study', required=True, type=int,
208206
help='Associate the prep template with this study')
209207
@click.option('--data_type', required=True,
210208
type=click.Choice(get_data_types()),
211209
help="The data type of data")
212-
def load_prep_template(fp, raw_data, study, data_type):
210+
def load_prep_template(fp, study, data_type):
213211
"""Loads a sample template to the database"""
214-
prep_template = load_prep_template_from_cmd(fp, raw_data, study, data_type)
212+
prep_template = load_prep_template_from_cmd(fp, study, data_type)
215213
click.echo("Prep template successfully added to the database with id %s"
216214
% prep_template.id)
217215

0 commit comments

Comments
 (0)