[973924]: / qiita_ware / test / test_metadata_pipeline.py

Download this file

173 lines (149 with data), 8.1 kB

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
# -----------------------------------------------------------------------------
# Copyright (c) 2014--, The Qiita Development Team.
#
# Distributed under the terms of the BSD 3-clause License.
#
# The full license is in the file LICENSE, distributed with this software.
# -----------------------------------------------------------------------------
from io import StringIO
from unittest import TestCase, main
from os import remove
from os.path import exists
from qiita_core.util import qiita_test_checker
from qiita_ware.exceptions import QiitaWareError
from qiita_db.sql_connection import TRN
from qiita_db.study import Study, StudyPerson
from qiita_db.user import User
from qiita_db.util import get_count
from qiita_db.metadata_template.sample_template import SampleTemplate
from qiita_db.metadata_template.prep_template import PrepTemplate
from qiita_ware.metadata_pipeline import (
create_templates_from_qiime_mapping_file)
@qiita_test_checker()
class TestMetadataPipeline(TestCase):
def setUp(self):
info = {
"timeseries_type_id": 1,
"metadata_complete": True,
"mixs_compliant": True,
"study_alias": "FCM",
"study_description": "Microbiome of people who eat nothing but "
"fried chicken",
"study_abstract": "Exploring how a high fat diet changes the "
"gut microbiome",
"principal_investigator_id": StudyPerson(3),
"lab_person_id": StudyPerson(1)
}
self.new_study = Study.create(
User('test@foo.bar'), "Fried Chicken Microbiome", info)
self._clean_up_files = []
def tearDown(self):
for fp in self._clean_up_files:
if exists(fp):
remove(fp)
study_id = self.new_study.id
for pt in self.new_study.prep_templates():
PrepTemplate.delete(pt.id)
if SampleTemplate.exists(study_id):
SampleTemplate.delete(study_id)
Study.delete(study_id)
def test_create_templates_from_qiime_mapping_file(self):
new_pt_id = get_count('qiita.prep_template') + 1
obs_st, obs_pt = create_templates_from_qiime_mapping_file(
StringIO(QIIME_MAP), self.new_study, "16S")
# Be green: clean the environment
for template in [obs_st, obs_pt]:
for _, fp in template.get_filepaths():
self._clean_up_files.append(fp)
self.assertEqual(obs_st.id, self.new_study.id)
self.assertEqual(obs_pt.id, new_pt_id)
# Check that each template has the correct columns
exp = {"physical_specimen_location", "physical_specimen_remaining",
"dna_extracted", "sample_type", "host_subject_id", "latitude",
"longitude", "taxon_id", "scientific_name",
"collection_timestamp", "description"}
self.assertEqual(set(obs_st.categories), exp)
exp = {"barcode", "primer", "center_name", "run_prefix", "platform",
"library_construction_protocol", "instrument_model",
"experiment_design_description"}
self.assertEqual(set(obs_pt.categories), exp)
def test_create_templates_from_qiime_mapping_file_reverse_linker(self):
with TRN:
TRN.add("SELECT last_value FROM "
"qiita.prep_template_prep_template_id_seq")
curr_id = TRN.execute_fetchflatten()[0]
obs_st, obs_pt = create_templates_from_qiime_mapping_file(
StringIO(QIIME_MAP_WITH_REVERSE_LINKER_PRIMER),
self.new_study, "16S")
# Be green: clean the environment
for template in [obs_st, obs_pt]:
for _, fp in template.get_filepaths():
self._clean_up_files.append(fp)
self.assertEqual(obs_st.id, self.new_study.id)
self.assertEqual(obs_pt.id, curr_id + 1)
# Check that each template has the correct columns
exp = {"physical_specimen_location", "physical_specimen_remaining",
"dna_extracted", "sample_type", "host_subject_id", "latitude",
"longitude", "taxon_id", "scientific_name",
"collection_timestamp", "description"}
self.assertEqual(set(obs_st.categories), exp)
exp = {"barcode", "primer", "center_name", "run_prefix", "platform",
"library_construction_protocol", "instrument_model",
"experiment_design_description", "reverselinkerprimer"}
self.assertEqual(set(obs_pt.categories), exp)
def test_create_templates_from_qiime_mapping_file_error(self):
with self.assertRaises(QiitaWareError):
create_templates_from_qiime_mapping_file(
StringIO(QIIME_MAP_ERROR), self.new_study, "16S")
QIIME_MAP = (
"#SampleID\tBarcodeSequence\tLinkerPrimerSequence\tinstrument_model\t"
"physical_specimen_location\tphysical_specimen_remaining\tdna_extracted\t"
"sample_type\thost_subject_id\tlatitude\tlongitude\ttaxon_id\t"
"scientific_name\tcenter_name\trun_prefix\tplatform\t"
"library_construction_protocol\texperiment_design_description\t"
"collection_timestamp\tDescription\n"
"Sample1\tGTCCGCAAGTTA\tGTGCCAGCMGCCGCGGTAA\tIllumina MiSeq\tUCSD\tTRUE\t"
"TRUE\ttype1\tNotIdentified\t4.1\t4.1\t9606\thomo sapiens\tANL\trp_1\t"
"Illumina\tprotocol_1\tedd_1\t05/28/15 11:00:00\tDescription S1\n"
"Sample2\tCGTAGAGCTCTC\tGTGCCAGCMGCCGCGGTAA\tIllumina MiSeq\tUCSD\tTRUE\t"
"TRUE\ttype2\tNotIdentified\t4.2\t4.2\t9606\thomo sapiens\tANL\trp_1\t"
"Illumina\tprotocol_1\tedd_1\t05/28/15 11:00:00\tDescription S2\n"
"Sample3\tCCTCTGAGAGCT\tGTGCCAGCMGCCGCGGTAA\tIllumina MiSeq\tUCSD\tTRUE\t"
"TRUE\ttype3\tNotIdentified\t4.3\t4.3\t9606\thomo sapiens\tANL\trp_2\t"
"Illumina\tprotocol_1\tedd_1\t05/28/15 11:00:00\tDescription S3\n")
QIIME_MAP_WITH_REVERSE_LINKER_PRIMER = (
"#SampleID\tBarcodeSequence\tLinkerPrimerSequence\tReverseLinkerPrimer\t"
"physical_specimen_location\tphysical_specimen_remaining\tdna_extracted\t"
"sample_type\thost_subject_id\tlatitude\tlongitude\ttaxon_id\t"
"scientific_name\tcenter_name\trun_prefix\tplatform\tinstrument_model\t"
"library_construction_protocol\texperiment_design_description\t"
"collection_timestamp\tDescription\n"
"Sample1\tGTCCGCAAGTTA\tGTGCCAGCMGCCGCGGTAA\tGTGCCAGCMGCCGCGGTAA\tUCSD\t"
"TRUE\tTRUE\ttype1\tNotIdentified\t4.1\t4.1\t9606\thomo sapiens\tANL\t"
"rp_1\tIllumina\tIllumina MiSeq\tprotocol_1\tedd_1\t05/28/15 11:00:00\t"
"Description S1\n"
"Sample2\tCGTAGAGCTCTC\tGTGCCAGCMGCCGCGGTAA\tGTGCCAGCMGCCGCGGTAA\tUCSD\t"
"TRUE\tTRUE\ttype2\tNotIdentified\t4.2\t4.2\t9606\thomo sapiens\tANL\t"
"rp_1\tIllumina\tIllumina MiSeq\tprotocol_1\tedd_1\t05/28/15 11:00:00\t"
"Description S2\n"
"Sample3\tCCTCTGAGAGCT\tGTGCCAGCMGCCGCGGTAA\tGTGCCAGCMGCCGCGGTAA\tUCSD\t"
"TRUE\tTRUE\ttype3\tNotIdentified\t4.3\t4.3\t9606\thomo sapiens\tANL\t"
"rp_2\tIllumina\tIllumina MiSeq\tprotocol_1\tedd_1\t05/28/15 11:00:00\t"
"Description S3\n")
QIIME_MAP_ERROR = (
"#SampleID\tBarcodeSequence\tphysical_specimen_location\t"
"physical_specimen_remaining\tdna_extracted\tsample_type\t"
"host_subject_id\tlatitude\tlongitude\ttaxon_id\tscientific_name\t"
"center_name\trun_prefix\tplatform\tlibrary_construction_protocol\t"
"experiment_design_description\tcollection_timestamp\tDescription\n"
"Sample1\tGTCCGCAAGTTA\tUCSD\tTRUE\tTRUE\ttype1\tNotIdentified\t4.1\t4.1\t"
"9606\thomo sapiens\tANL\trp_1\tIllumina\tprotocol_1\tedd_1\t"
"05/28/15 11:00:00\tDescription S1\n"
"Sample2\tCGTAGAGCTCTC\tUCSD\tTRUE\tTRUE\ttype2\tNotIdentified\t4.2\t4.2\t"
"9606\thomo sapiens\tANL\trp_1\tIllumina\tprotocol_1\tedd_1\t"
"05/28/15 11:00:00\tDescription S2\n"
"Sample3\tCCTCTGAGAGCT\tUCSD\tTRUE\tTRUE\ttype3\tNotIdentified\t4.3\t4.3\t"
"9606\thomo sapiens\tANL\trp_2\tIllumina\tprotocol_1\tedd_1\t"
"05/28/15 11:00:00\tDescription S3\n")
if __name__ == "__main__":
main()