annotate test/test_combine_output.py @ 4:0d1557b3d540

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