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

Download this file

281 lines (248 with data), 11.3 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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
# -----------------------------------------------------------------------------
# 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 unittest import TestCase, main, skipIf
from os.path import join, basename, exists
from tempfile import mkdtemp
import pandas as pd
from datetime import datetime
from shutil import rmtree, copyfile
from os import path
from glob import glob
from paramiko.ssh_exception import AuthenticationException
from h5py import File
from qiita_files.demux import to_hdf5
from qiita_ware.exceptions import ComputeError
from qiita_ware.commands import submit_EBI, list_remote, download_remote
from qiita_db.util import get_mountpoint
from qiita_db.study import Study, StudyPerson
from qiita_db.software import DefaultParameters, Parameters
from qiita_db.artifact import Artifact
from qiita_db.metadata_template.prep_template import PrepTemplate
from qiita_db.metadata_template.sample_template import SampleTemplate
from qiita_db.user import User
from qiita_core.util import qiita_test_checker
from qiita_core.qiita_settings import qiita_config
@qiita_test_checker()
class SSHTests(TestCase):
def setUp(self):
self.self_dir_path = path.dirname(path.abspath(__file__))
self.remote_dir_path = join(self.self_dir_path,
'test_data/test_remote_dir/')
self.test_ssh_key = join(self.self_dir_path, 'test_data/test_key')
self.test_wrong_key = join(self.self_dir_path, 'test_data/random_key')
self.temp_local_dir = mkdtemp()
self.exp_files = ['test_0.fastq.gz', 'test_1.txt']
def tearDown(self):
rmtree(self.temp_local_dir)
def _get_valid_files(self, folder):
files = []
for x in qiita_config.valid_upload_extension:
files.extend([basename(f) for f in glob(join(folder, '*.%s' % x))])
return files
def test_list_scp_wrong_key(self):
with self.assertRaises(AuthenticationException):
list_remote('scp://runner@localhost:'+self.remote_dir_path,
self.test_wrong_key)
def test_list_scp_nonexist_key(self):
with self.assertRaises(IOError):
list_remote('scp://runner@localhost:'+self.remote_dir_path,
join(self.self_dir_path, 'nokey'))
def test_list_scp(self):
kpath = join(self.temp_local_dir, 'tmp-key')
copyfile(self.test_ssh_key, kpath)
read_file_list = list_remote(
'scp://runner@localhost:'+self.remote_dir_path, kpath)
self.assertCountEqual(read_file_list, self.exp_files)
def test_download_scp(self):
kpath = join(self.temp_local_dir, 'tmp-key')
copyfile(self.test_ssh_key, kpath)
download_remote('scp://runner@localhost:'+self.remote_dir_path,
kpath, self.temp_local_dir)
local_files = self._get_valid_files(self.temp_local_dir)
self.assertCountEqual(local_files, self.exp_files)
self.assertFalse(exists(kpath))
class CommandsTests(TestCase):
def setUp(self):
self.files_to_remove = []
self.temp_dir = mkdtemp()
self.files_to_remove.append(self.temp_dir)
_, self.base_fp = get_mountpoint("preprocessed_data")[0]
def write_demux_files(self, prep_template, generate_hdf5=True):
"""Writes a demux test file to avoid duplication of code"""
fna_fp = join(self.temp_dir, 'seqs.fna')
demux_fp = join(self.temp_dir, 'demux.seqs')
if generate_hdf5:
with open(fna_fp, 'w') as f:
f.write(FASTA_EXAMPLE)
with File(demux_fp, "w") as f:
to_hdf5(fna_fp, f)
else:
with open(demux_fp, 'w') as f:
f.write('')
if prep_template.artifact is None:
ppd = Artifact.create(
[(demux_fp, 6)], "Demultiplexed", prep_template=prep_template)
else:
params = Parameters.from_default_params(
DefaultParameters(1),
{'input_data': prep_template.artifact.id})
ppd = Artifact.create(
[(demux_fp, 6)], "Demultiplexed",
parents=[prep_template.artifact], processing_parameters=params)
return ppd
def generate_new_study_with_preprocessed_data(self):
"""Creates a new study up to the processed data for testing"""
info = {
"timeseries_type_id": 1,
"metadata_complete": True,
"mixs_compliant": True,
"study_alias": "Test EBI",
"study_description": "Study for testing EBI",
"study_abstract": "Study for testing EBI",
"principal_investigator_id": StudyPerson(3),
"lab_person_id": StudyPerson(1)
}
study = Study.create(User('test@foo.bar'), "Test EBI study", info)
metadata_dict = {
'Sample1': {'collection_timestamp': datetime(2015, 6, 1, 7, 0, 0),
'physical_specimen_location': 'location1',
'taxon_id': 9606,
'scientific_name': 'homo sapiens',
'Description': 'Test Sample 1'},
'Sample2': {'collection_timestamp': datetime(2015, 6, 2, 7, 0, 0),
'physical_specimen_location': 'location1',
'taxon_id': 9606,
'scientific_name': 'homo sapiens',
'Description': 'Test Sample 2'},
'Sample3': {'collection_timestamp': datetime(2015, 6, 3, 7, 0, 0),
'physical_specimen_location': 'location1',
'taxon_id': 9606,
'scientific_name': 'homo sapiens',
'Description': 'Test Sample 3'}
}
metadata = pd.DataFrame.from_dict(metadata_dict, orient='index',
dtype=str)
SampleTemplate.create(metadata, study)
metadata_dict = {
'Sample1': {'primer': 'GTGCCAGCMGCCGCGGTAA',
'barcode': 'CGTAGAGCTCTC',
'center_name': 'KnightLab',
'platform': 'Illumina',
'instrument_model': 'Illumina MiSeq',
'library_construction_protocol': 'Protocol ABC',
'experiment_design_description': "Random value 1"},
'Sample2': {'primer': 'GTGCCAGCMGCCGCGGTAA',
'barcode': 'CGTAGAGCTCTA',
'center_name': 'KnightLab',
'platform': 'Illumina',
'instrument_model': 'Illumina MiSeq',
'library_construction_protocol': 'Protocol ABC',
'experiment_design_description': "Random value 2"},
'Sample3': {'primer': 'GTGCCAGCMGCCGCGGTAA',
'barcode': 'CGTAGAGCTCTT',
'center_name': 'KnightLab',
'platform': 'Illumina',
'instrument_model': 'Illumina MiSeq',
'library_construction_protocol': 'Protocol ABC',
'experiment_design_description': "Random value 3"},
}
metadata = pd.DataFrame.from_dict(metadata_dict, orient='index',
dtype=str)
pt = PrepTemplate.create(metadata, study, "16S", 'Metagenomics')
fna_fp = join(self.temp_dir, 'seqs.fna')
demux_fp = join(self.temp_dir, 'demux.seqs')
with open(fna_fp, 'w') as f:
f.write(FASTA_EXAMPLE_2.format(study.id))
with File(demux_fp, 'w') as f:
to_hdf5(fna_fp, f)
ppd = Artifact.create(
[(demux_fp, 6)], "Demultiplexed", prep_template=pt)
return ppd
def test_submit_EBI_step_2_failure(self):
ppd = self.write_demux_files(PrepTemplate(1), True)
pid = ppd.id
with self.assertRaises(ComputeError):
submit_EBI(pid, 'VALIDATE', True)
rmtree(join(self.base_fp, '%d_ebi_submission' % pid), True)
@skipIf(
qiita_config.ebi_seq_xfer_pass == '', 'skip: ascp not configured')
def test_submit_EBI_parse_EBI_reply_failure(self):
ppd = self.write_demux_files(PrepTemplate(1))
pid = ppd.id
with self.assertRaises(ComputeError) as error:
submit_EBI(pid, 'VALIDATE', True)
error = str(error.exception)
self.assertIn('EBI Submission failed! Log id:', error)
self.assertIn('The EBI submission failed:', error)
rmtree(join(self.base_fp, '%d_ebi_submission' % pid), True)
@skipIf(
qiita_config.ebi_seq_xfer_pass == '', 'skip: ascp not configured')
def test_full_submission(self):
artifact = self.generate_new_study_with_preprocessed_data()
self.assertEqual(
artifact.study.ebi_submission_status, 'not submitted')
aid = artifact.id
submit_EBI(aid, 'VALIDATE', True, test=True)
self.assertEqual(artifact.study.ebi_submission_status, 'submitted')
rmtree(join(self.base_fp, '%d_ebi_submission' % aid), True)
def test_max_ebiena_curl_error(self):
artifact = self.generate_new_study_with_preprocessed_data()
self.assertEqual(
artifact.study.ebi_submission_status, 'not submitted')
aid = artifact.id
with self.assertRaises(ComputeError) as error:
submit_EBI(aid, 'VALIDATE', True, test_size=True)
error = str(error.exception)
self.assertIn('is too large. Before cleaning:', error)
rmtree(join(self.base_fp, '%d_ebi_submission' % aid), True)
FASTA_EXAMPLE = """>1.SKB2.640194_1 X orig_bc=X new_bc=X bc_diffs=0
CCACCCAGTAAC
>1.SKB2.640194_2 X orig_bc=X new_bc=X bc_diffs=0
CCACCCAGTAAC
>1.SKB2.640194_3 X orig_bc=X new_bc=X bc_diffs=0
CCACCCAGTAAC
>1.SKM4.640180_4 X orig_bc=X new_bc=X bc_diffs=0
CCACCCAGTAAC
>1.SKM4.640180_5 X orig_bc=X new_bc=X bc_diffs=0
CCACCCAGTAAC
>1.SKB3.640195_6 X orig_bc=X new_bc=X bc_diffs=0
CCACCCAGTAAC
>1.SKB6.640176_7 X orig_bc=X new_bc=X bc_diffs=0
CCACCCAGTAAC
>1.SKD6.640190_8 X orig_bc=X new_bc=X bc_diffs=0
CCACCCAGTAAC
>1.SKM6.640187_9 X orig_bc=X new_bc=X bc_diffs=0
CCACCCAGTAAC
>1.SKD9.640182_10 X orig_bc=X new_bc=X bc_diffs=0
CCACCCAGTAAC
>1.SKM8.640201_11 X orig_bc=X new_bc=X bc_diffs=0
CCACCCAGTAAC
>1.SKM2.640199_12 X orig_bc=X new_bc=X bc_diffs=0
CCACCCAGTAAC
"""
FASTA_EXAMPLE_2 = """>{0}.Sample1_1 X orig_bc=X new_bc=X bc_diffs=0
CCACCCAGTAAC
>{0}.Sample1_2 X orig_bc=X new_bc=X bc_diffs=0
CCACCCAGTAAC
>{0}.Sample1_3 X orig_bc=X new_bc=X bc_diffs=0
CCACCCAGTAAC
>{0}.Sample2_4 X orig_bc=X new_bc=X bc_diffs=0
CCACCCAGTAAC
>{0}.Sample2_5 X orig_bc=X new_bc=X bc_diffs=0
CCACCCAGTAAC
>{0}.Sample2_6 X orig_bc=X new_bc=X bc_diffs=0
CCACCCAGTAAC
>{0}.Sample3_7 X orig_bc=X new_bc=X bc_diffs=0
CCACCCAGTAAC
>{0}.Sample3_8 X orig_bc=X new_bc=X bc_diffs=0
CCACCCAGTAAC
>{0}.Sample3_9 X orig_bc=X new_bc=X bc_diffs=0
CCACCCAGTAAC
"""
if __name__ == '__main__':
main()