1414from os import close , remove , path
1515from os .path import join
1616from tempfile import mkstemp , gettempdir
17- from shutil import rmtree
1817from unittest import TestCase , main
1918from xml .dom import minidom
2019from xml .etree import ElementTree as ET
20+ from functools import partial
2121
2222from qiita_ware .ebi import (SampleAlreadyExistsError , NoXMLError ,
2323 EBISubmission )
2626
2727class TestEBISubmission (TestCase ):
2828 def setUp (self ):
29- self .path = path .dirname (path .abspath (__file__ )) + '/test_data'
30- self .temp_dir = gettempdir ()
31- self .demux_output_dir = join (self .temp_dir , 'demux_output' )
29+ self .path = join (path .dirname (path .abspath (__file__ )), 'test_data' ,
30+ 'test_ebi' )
31+
32+ ebi_test_file = partial (join , self .path )
3233
33- def tearDown ( self ):
34- try :
35- rmtree ( self . demux_output_dir )
36- except :
37- pass
34+ self . sample1_fp = ebi_test_file ( 'sample1.fastq.gz' )
35+ self . sample2_fp = ebi_test_file ( 'sample2.fastq.gz' )
36+ self . sample3_fp = ebi_test_file ( 'sample3.fastq.gz' )
37+
38+ self . temp_dir = gettempdir ()
3839
3940 def test_init (self ):
4041 e = EBISubmission ('2' , 'Study Title' , 'Study Abstract' ,
@@ -199,15 +200,18 @@ def test_add_sample_prep(self):
199200 new_investigation_type = 'metagenome' )
200201 submission .add_sample ('test1' )
201202 submission .add_sample ('test2' )
203+
202204 submission .add_sample_prep ('test1' , 'ILLUMINA' , 'fastq' ,
203- self .path , 'experiment description' ,
205+ self .sample1_fp , 'experiment description' ,
204206 'library protocol' )
207+
205208 prep_info = submission .samples ['test1' ]['prep' ]
206209 self .assertEqual (prep_info ['platform' ], 'ILLUMINA' )
207- self .assertEqual (prep_info ['file_path' ], self .path )
210+ self .assertEqual (prep_info ['file_path' ], self .sample1_fp )
208211 with self .assertRaises (KeyError ):
209212 submission .add_sample_prep ('test3' , 'ILLUMINA' , 'fastq' ,
210- self .path , 'experiment description' ,
213+ self .sample3_fp ,
214+ 'experiment description' ,
211215 'library protocol' )
212216
213217 def test_add_sample_prep_exception (self ):
@@ -218,11 +222,13 @@ def test_add_sample_prep_exception(self):
218222 submission .add_sample ('test2' )
219223 with self .assertRaises (ValueError ):
220224 submission .add_sample_prep ('test2' , 'DOES-NOT-EXIST' , 'fastq' ,
221- self .path , 'experiment description' ,
225+ self .sample1_fp ,
226+ 'experiment description' ,
222227 'library protocol' )
223228 with self .assertRaises (KeyError ):
224229 submission .add_sample_prep ('test3' , 'DOES-NOT-EXIST' , 'fastq' ,
225- self .path , 'experiment description' ,
230+ self .sample3_fp ,
231+ 'experiment description' ,
226232 'library protocol' )
227233
228234 def test_generate_library_descriptor (self ):
@@ -253,14 +259,17 @@ def test_generate_experiment_xml(self):
253259 new_investigation_type = 'metagenome' )
254260 submission .add_sample ('test1' )
255261 submission .add_sample_prep ('test1' , 'ILLUMINA' , 'fastq' ,
256- 'fakepath' ,
262+ self . sample1_fp ,
257263 'experiment description' ,
258264 'library protocol' )
259265 xmlelement = submission .generate_experiment_xml ()
260266 xml = minidom .parseString (ET .tostring (xmlelement ))
261267 xmlstring = xml .toprettyxml (indent = ' ' , encoding = 'UTF-8' )
262268 obs_stripped = '' .join ([l .strip () for l in xmlstring .splitlines ()])
263- exp_stripped = '' .join ([l .strip () for l in EXPERIMENTXML .splitlines ()])
269+ exp = EXPERIMENTXML % {
270+ 'path' : self .sample1_fp ,
271+ 'organization_prefix' : qiita_config .ebi_organization_prefix }
272+ exp_stripped = '' .join ([l .strip () for l in exp .splitlines ()])
264273 self .assertEqual (obs_stripped , exp_stripped )
265274
266275 def test_generate_run_xml (self ):
@@ -269,7 +278,7 @@ def test_generate_run_xml(self):
269278 new_investigation_type = 'metagenome' )
270279 submission .add_sample ('test1' )
271280 submission .add_sample_prep ('test1' , 'ILLUMINA' , 'fastq' ,
272- join ( self .path , '__init__.py' ) ,
281+ self .sample1_fp ,
273282 'experiment description' ,
274283 'library protocol' )
275284 xmlelement = submission .generate_run_xml ()
@@ -344,13 +353,16 @@ def test_write_experiment_xml(self):
344353 new_investigation_type = 'metagenome' )
345354 submission .add_sample ('test1' )
346355 submission .add_sample_prep ('test1' , 'ILLUMINA' , 'fastq' ,
347- 'fakepath' , 'experiment description' ,
356+ self . sample1_fp , 'experiment description' ,
348357 'library protocol' )
349358 fh , output = mkstemp ()
350359 close (fh )
351360 submission .write_experiment_xml (output )
352361 obs_stripped = '' .join ([l .strip () for l in open (output )])
353- exp_stripped = '' .join ([l .strip () for l in EXPERIMENTXML .splitlines ()])
362+ exp = EXPERIMENTXML % {
363+ 'path' : self .sample1_fp ,
364+ 'organization_prefix' : qiita_config .ebi_organization_prefix }
365+ exp_stripped = '' .join ([l .strip () for l in exp .splitlines ()])
354366 self .assertEqual (obs_stripped , exp_stripped )
355367 remove (output )
356368
@@ -369,7 +381,7 @@ def test_add_samples_from_templates(self):
369381 'ILLUMINA' )
370382 self .assertEqual (
371383 submission .samples ['sample2' ]['prep' ]['file_path' ],
372- self .path + '/sample2.fastq.gz' )
384+ self .sample2_fp )
373385 with self .assertRaises (KeyError ):
374386 submission .samples ['nothere' ]
375387
@@ -394,7 +406,7 @@ def test_from_templates_and_per_sample_fastqs(self):
394406 'ILLUMINA' )
395407 self .assertEqual (
396408 submission .samples ['sample2' ]['prep' ]['file_path' ],
397- self .path + '/sample2.fastq.gz' )
409+ self .sample2_fp )
398410 with self .assertRaises (KeyError ):
399411 submission .samples ['nothere' ]
400412
@@ -559,7 +571,7 @@ def test_generate_curl_command(self):
559571 </EXPERIMENT_ATTRIBUTE>
560572 <EXPERIMENT_ATTRIBUTE>
561573 <TAG>file_path</TAG>
562- <VALUE>fakepath </VALUE>
574+ <VALUE>%(path)s </VALUE>
563575 </EXPERIMENT_ATTRIBUTE>
564576 <EXPERIMENT_ATTRIBUTE>
565577 <TAG>file_type</TAG>
@@ -576,18 +588,19 @@ def test_generate_curl_command(self):
576588 </EXPERIMENT_ATTRIBUTES>
577589 </EXPERIMENT>
578590</EXPERIMENT_SET>
579- """ % { 'organization_prefix' : qiita_config . ebi_organization_prefix }
591+ """
580592
581593RUNXML = """
582594<?xml version="1.0" encoding="UTF-8"?>
583595<RUN_SET xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:no\
584596 NamespaceSchemaLocation="ftp://ftp.sra.ebi.ac.uk/meta/xsd/sra_1_3/SRA.run.xsd">
585- <RUN alias="%(study_alias)s___init__.py_run" center_name="CCME-COLORADO">
597+ <RUN alias="%(study_alias)s_sample1.fastq.gz_run" \
598+ center_name="CCME-COLORADO">
586599 <EXPERIMENT_REF refname="%(organization_prefix)s_ppdid_001:test1"/>
587600 <DATA_BLOCK>
588601 <FILES>
589- <FILE checksum="612cbff13a4f0e236e5e62ac2e00329a " checksum_method=\
590- "MD5" filename="%(ebi_dir)s/__init__.py " filetype="fastq" \
602+ <FILE checksum="506d31c82999a2cbcda138a369955e7d " checksum_method=\
603+ "MD5" filename="%(ebi_dir)s/sample1.fastq.gz " filetype="fastq" \
591604 quality_scoring_system="phred"/>
592605 </FILES>
593606 </DATA_BLOCK>
0 commit comments