comparison test/test_combine_output.py @ 0:9d5f4f5f764b

Initial commit to toolshed
author pieter.lukasse@wur.nl
date Thu, 16 Jan 2014 13:10:00 +0100
parents
children
comparison
equal deleted inserted replaced
-1:000000000000 0:9d5f4f5f764b
1 '''
2 Created on Mar 27, 2012
3
4 @author: marcelk
5 '''
6 from GCMS import combine_output
7 from pkg_resources import resource_filename # @UnresolvedImport # pylint: disable=E0611
8 import os
9 import shutil
10 import tempfile
11 import unittest
12
13
14 class Test(unittest.TestCase):
15 '''
16 Tests for the 'combine_output' Galaxy tool
17 '''
18
19 def setUp(self):
20 self.rf_output = resource_filename(__name__, "data/RankFilter.txt")
21 self.cl_output = resource_filename(__name__, "data/CasLookup.txt")
22
23 def test_process_data(self):
24 '''
25 Tests the processing of the RankFilter and CasLookup files into dictionaries
26 '''
27 rfdata = combine_output._process_data(self.rf_output)
28 cldata = combine_output._process_data(self.cl_output)
29 self.assertEqual(set([' 18457-04-0', ' 55133-95-4', ' 58-08-2', ' 112-34-5']), set(rfdata['CAS']))
30 self.assertEqual(set(['C58082', 'C18457040', 'C55133954', 'C112345']), set(cldata['CAS']))
31
32 def test_add_hit(self):
33 '''
34 Tests the combination of two records from both the RankFilter- and CasLookup-tools
35 '''
36 rfdata = combine_output._process_data(self.rf_output)
37 cldata = combine_output._process_data(self.cl_output)
38 index = 0
39 rf_record = dict(zip(rfdata.keys(), [rfdata[key][index] for key in rfdata.keys()]))
40 cl_record = dict(zip(cldata.keys(), [cldata[key][index] for key in cldata.keys()]))
41
42 hit = combine_output._add_hit(rf_record, cl_record)
43 self.assertEqual(len(hit), 27)
44
45 # Pass empty record, should fail combination
46 self.assertRaises(KeyError, combine_output._add_hit, rf_record, {})
47
48 def test_merge_data(self):
49 '''
50 Tests the merging of the RankFilter and CasLookup data
51 '''
52 rfdata = combine_output._process_data(self.rf_output)
53 cldata = combine_output._process_data(self.cl_output)
54 merged, _ = combine_output._merge_data(rfdata, cldata)
55 centrotypes = _get_centrotypes(merged)
56 self.failUnless(all(centrotype in centrotypes for centrotype in ('2716','12723', '3403', '12710')))
57
58 def _get_centrotypes(merged):
59 '''
60 returns centrotype codes found in merged set
61 '''
62 result = []
63 for item_idx in xrange(len(merged)):
64 item = merged[item_idx]
65 centrotype = item[0][0]
66 result.append(centrotype)
67
68 return result
69
70 def test_remove_formula(self):
71 '''
72 Tests the removal of the Formula from the 'Name' field (RankFilter output)
73 '''
74 name = "Caffeine C8H10N4O2"
75 compound_name, compound_formula = combine_output._remove_formula(name)
76 self.assertEqual(compound_name, 'Caffeine')
77 self.assertEqual(compound_formula, 'C8H10N4O2')
78 name = "Ethanol C2H6O"
79 compound_name, compound_formula = combine_output._remove_formula(name)
80 self.assertEqual(compound_name, 'Ethanol')
81 self.assertEqual(compound_formula, 'C2H6O')
82 # No formula to remove
83 name = "Butanoic acid, 4-[(trimethylsilyl)oxy]-, trimethylsilyl ester"
84 compound_name, compound_formula = combine_output._remove_formula(name)
85 self.assertEqual(compound_name, name)
86 self.assertEqual(compound_formula, False)
87
88 def test_save_data(self):
89 '''
90 Tests the creation of the output tabular files (no content testing)
91 '''
92 temp_folder = tempfile.mkdtemp(prefix='gcms_combine_output_')
93 saved_single_data = '{0}/{1}'.format(temp_folder, 'output_single.tsv')
94 saved_multi_data = '{0}/{1}'.format(temp_folder, 'output_multi.tsv')
95 rfdata = combine_output._process_data(self.rf_output)
96 cldata = combine_output._process_data(self.cl_output)
97 merged, nhits = combine_output._merge_data(rfdata, cldata)
98 combine_output._save_data(merged, nhits, saved_single_data, saved_multi_data)
99 self.failUnless(os.path.exists(saved_single_data))
100 self.failUnless(os.path.exists(saved_multi_data))
101 shutil.rmtree(temp_folder)
102
103
104 if __name__ == "__main__":
105 #import sys;sys.argv = ['', 'Test.testName']
106 unittest.main()