Skip to content

Commit 7390538

Browse files
committed
Merge pull request #1107 from josenavas/fix-qiita-ware-tests
Fix qiita all tests
2 parents b604ae1 + 6c83ba4 commit 7390538

File tree

8 files changed

+196
-65
lines changed

8 files changed

+196
-65
lines changed

qiita_db/metadata_template/base_metadata_template.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1041,7 +1041,7 @@ def add_filepath(self, filepath, conn_handler=None, fp_id=None):
10411041
fp_id = self._fp_id if fp_id is None else fp_id
10421042

10431043
try:
1044-
fpp_id = insert_filepaths([(filepath, self._fp_id)], None,
1044+
fpp_id = insert_filepaths([(filepath, fp_id)], None,
10451045
"templates", "filepath", conn_handler,
10461046
move_files=False)[0]
10471047
values = (self._id, fpp_id)

qiita_db/metadata_template/prep_template.py

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -421,6 +421,13 @@ def create_qiime_mapping_file(self):
421421
'description': 'Description',
422422
}
423423

424+
if 'reverselinkerprimer' in self.categories():
425+
rename_cols['reverselinkerprimer'] = 'ReverseLinkerPrimer'
426+
new_cols = ['BarcodeSequence', 'LinkerPrimerSequence',
427+
'ReverseLinkerPrimer']
428+
else:
429+
new_cols = ['BarcodeSequence', 'LinkerPrimerSequence']
430+
424431
# getting the latest sample template
425432
conn_handler = SQLConnectionHandler()
426433
sql = """SELECT filepath_id, filepath
@@ -474,7 +481,6 @@ def create_qiime_mapping_file(self):
474481
cols.remove('BarcodeSequence')
475482
cols.remove('LinkerPrimerSequence')
476483
cols.remove('Description')
477-
new_cols = ['BarcodeSequence', 'LinkerPrimerSequence']
478484
new_cols.extend(cols)
479485
new_cols.append('Description')
480486
mapping = mapping[new_cols]
@@ -524,3 +530,25 @@ def status(self):
524530
pd_statuses = conn_handler.execute_fetchall(sql, (self._id,))
525531

526532
return infer_status(pd_statuses)
533+
534+
@property
535+
def qiime_map_fp(self):
536+
"""The QIIME mapping filepath attached to the prep template
537+
538+
Returns
539+
-------
540+
str
541+
The filepath of the QIIME mapping file
542+
"""
543+
conn_handler = SQLConnectionHandler()
544+
545+
sql = """SELECT filepath_id, filepath
546+
FROM qiita.filepath
547+
JOIN qiita.{0} USING (filepath_id)
548+
JOIN qiita.filepath_type USING (filepath_type_id)
549+
WHERE {1} = %s AND filepath_type = 'qiime_map'
550+
ORDER BY filepath_id DESC""".format(self._filepath_table,
551+
self._id_column)
552+
fn = conn_handler.execute_fetchall(sql, (self._id,))[0][1]
553+
base_dir = get_mountpoint('templates')[0][1]
554+
return join(base_dir, fn)

qiita_db/metadata_template/test/test_prep_template.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1293,6 +1293,12 @@ def test_update_category(self):
12931293
self.assertEqual(self.tester['1.SKB8.640193']['center_name'], 'FOO')
12941294
self.assertEqual(self.tester['1.SKD8.640184']['center_name'], 'BAR')
12951295

1296+
def test_qiime_map_fp(self):
1297+
pt = PrepTemplate(1)
1298+
exp = join(get_mountpoint('templates')[0][1],
1299+
'1_prep_1_qiime_19700101-000000.txt')
1300+
self.assertEqual(pt.qiime_map_fp, exp)
1301+
12961302

12971303
EXP_PREP_TEMPLATE = (
12981304
'sample_name\tbarcode\tcenter_name\tcenter_project_name\t'

qiita_db/test/test_analysis.py

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -182,9 +182,9 @@ def test_retrieve_dropped_samples(self):
182182
"lab_person_id": StudyPerson(1)
183183
}
184184
metadata_dict = {
185-
'SKB8.640193': {'physical_location': 'location1',
186-
'has_physical_specimen': True,
187-
'has_extracted_data': True,
185+
'SKB8.640193': {'physical_specimen_location': 'location1',
186+
'physical_specimen_remaining': True,
187+
'dna_extracted': True,
188188
'sample_type': 'type1',
189189
'required_sample_info_status': 'received',
190190
'collection_timestamp':
@@ -194,9 +194,9 @@ def test_retrieve_dropped_samples(self):
194194
'str_column': 'Value for sample 1',
195195
'latitude': 42.42,
196196
'longitude': 41.41},
197-
'SKD8.640184': {'physical_location': 'location1',
198-
'has_physical_specimen': True,
199-
'has_extracted_data': True,
197+
'SKD8.640184': {'physical_specimen_location': 'location1',
198+
'physical_specimen_remaining': True,
199+
'dna_extracted': True,
200200
'sample_type': 'type1',
201201
'required_sample_info_status': 'received',
202202
'collection_timestamp':
@@ -206,9 +206,9 @@ def test_retrieve_dropped_samples(self):
206206
'str_column': 'Value for sample 2',
207207
'latitude': 4.2,
208208
'longitude': 1.1},
209-
'SKB7.640196': {'physical_location': 'location1',
210-
'has_physical_specimen': True,
211-
'has_extracted_data': True,
209+
'SKB7.640196': {'physical_specimen_location': 'location1',
210+
'physical_specimen_remaining': True,
211+
'dna_extracted': True,
212212
'sample_type': 'type1',
213213
'required_sample_info_status': 'received',
214214
'collection_timestamp':

qiita_db/test/test_commands.py

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -723,8 +723,9 @@ def test_update_preprocessed_data_from_cmd_ppd(self):
723723

724724
SAMPLE_TEMPLATE = (
725725
"sample_name\trequired_sample_info_status\tcollection_timestamp\t"
726-
"sample_type\thas_physical_specimen\tphysical_location\thas_extracted_data"
727-
"\thost_subject_id\tTreatment\tDOB\tlatitude\tlongitude\tDescription\n"
726+
"sample_type\tphysical_specimen_remaining\tphysical_specimen_location\t"
727+
"dna_extracted\thost_subject_id\tTreatment\tDOB\tlatitude\tlongitude"
728+
"\tDescription\n"
728729
"PC.354\treceived\t2014-06-18 16:44\ttype_1\tTrue\tLocation_1\tTrue\t"
729730
"HS_ID_PC.354\tControl\t20061218\t1.88401499993\t56.0003871552\t"
730731
"Control_mouse_I.D._354\n"
@@ -739,8 +740,8 @@ def test_update_preprocessed_data_from_cmd_ppd(self):
739740
"Fasting_mouse_I.D._636")
740741

741742
PREP_TEMPLATE = (
742-
'sample_name\tbarcodesequence\tcenter_name\tcenter_project_name\t'
743-
'description\tebi_submission_accession\temp_status\tlinkerprimersequence\t'
743+
'sample_name\tbarcode\tcenter_name\tcenter_project_name\t'
744+
'description\tebi_submission_accession\temp_status\tprimer\t'
744745
'run_prefix\tstr_column\tplatform\tlibrary_construction_protocol\t'
745746
'experiment_design_description\n'
746747
'SKB7.640196\tCCTCTGAGAGCT\tANL\tTest Project\tskb7\tNone\tEMP\t'

qiita_ware/processing_pipeline.py

Lines changed: 30 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -40,33 +40,44 @@ def _get_qiime_minimal_mapping(prep_template, out_dir):
4040
The paths to the qiime minimal mapping files
4141
"""
4242
from functools import partial
43+
from collections import defaultdict
4344
from os.path import join
4445
import pandas as pd
4546

46-
# Get the data in a pandas DataFrame, so it is easier to manage
47-
pt = prep_template.to_dataframe()
47+
# The prep templates has a QIIME mapping file, get it
48+
qiime_map = pd.read_csv(prep_template.qiime_map_fp, sep='\t',
49+
keep_default_na=False, na_values=['unknown'],
50+
index_col=False,
51+
converters=defaultdict(lambda: str))
52+
qiime_map.set_index('#SampleID', inplace=True, drop=True)
4853

49-
# We now need to rename some columns to be QIIME compliant.
50-
# Hopefully, this conversion won't be needed if QIIME relaxes its
51-
# constraints
52-
pt.rename(columns={'barcodesequence': 'BarcodeSequence',
53-
'linkerprimersequence': 'LinkerPrimerSequence'},
54-
inplace=True)
55-
pt['Description'] = pd.Series(['Qiita MMF'] * len(pt.index),
56-
index=pt.index)
54+
# We use our own description to avoid potential processing problems
55+
qiime_map['Description'] = pd.Series(['Qiita MMF'] * len(qiime_map.index),
56+
index=qiime_map.index)
5757

5858
# We ensure the order of the columns as QIIME is expecting
59-
cols = ['BarcodeSequence', 'LinkerPrimerSequence', 'Description']
59+
if 'ReverseLinkerPrimer' in qiime_map:
60+
cols = ['BarcodeSequence', 'LinkerPrimerSequence',
61+
'ReverseLinkerPrimer', 'Description']
62+
else:
63+
cols = ['BarcodeSequence', 'LinkerPrimerSequence', 'Description']
6064

61-
# If the study has more than 1 lane, we should generate a qiita MMF for
62-
# each of the lanes. We know how to split the prep template based on
63-
# the run_prefix column
64-
output_fps = []
6565
path_builder = partial(join, out_dir)
66-
for prefix, df in pt.groupby('run_prefix'):
67-
df = df[cols]
68-
out_fp = path_builder("%s_MMF.txt" % prefix)
69-
output_fps.append(out_fp)
66+
if 'run_prefix' in qiime_map:
67+
# The study potentially has more than 1 lane, so we should generate a
68+
# qiime MMF for each of the lanes. We know how to split the prep
69+
# template based on the run_prefix column
70+
output_fps = []
71+
for prefix, df in qiime_map.groupby('run_prefix'):
72+
df = df[cols]
73+
out_fp = path_builder("%s_MMF.txt" % prefix)
74+
output_fps.append(out_fp)
75+
df.to_csv(out_fp, index_label="#SampleID", sep='\t')
76+
else:
77+
# The study only has one lane, just write the MMF
78+
df = qiime_map[cols]
79+
out_fp = path_builder("prep_%d_MMF.txt" % prep_template.id)
80+
output_fps = [out_fp]
7081
df.to_csv(out_fp, index_label="#SampleID", sep='\t')
7182

7283
return output_fps

0 commit comments

Comments
 (0)