comparison test/test_export_to_metexp_tabular.py @ 0:dffc38727496

initial commit
author pieter.lukasse@wur.nl
date Sat, 07 Feb 2015 22:02:00 +0100
parents
children
comparison
equal deleted inserted replaced
-1:000000000000 0:dffc38727496
1 '''Integration tests for the GCMS project'''
2
3 from pkg_resources import resource_filename # @UnresolvedImport # pylint: disable=E0611
4 from GCMS import export_to_metexp_tabular
5 import os.path
6 import sys
7 import unittest
8
9
10 class IntegrationTest(unittest.TestCase):
11
12
13 def test_MM_calculations(self):
14 '''
15 test the implemented method for MM calculations for
16 given chemical formulas
17 '''
18 export_to_metexp_tabular.init_elements_and_masses_map()
19
20 formula = "C8H18O3"
21 # should be = 12.01*8 + 1.01*18 + 16*3 = 162.26
22 result = export_to_metexp_tabular.get_molecular_mass(formula)
23 self.assertEqual(162.26, result)
24
25 formula = "CH2O3Fe2Ni"
26 # should be = 12.01*1 + 1.01*2 + 16*3 + 55.85*2 + 58.71 = 232.44
27 result = export_to_metexp_tabular.get_molecular_mass(formula)
28 self.assertAlmostEqual(232.44, result, 2)
29
30
31
32
33
34 def test_combine_output_simple(self):
35 '''
36 comment me
37 '''
38 # Create out folder
39 outdir = "output/metexp/"
40 if not os.path.exists(outdir):
41 os.makedirs(outdir)
42
43 #Build up arguments and run
44
45 rankfilter_and_caslookup_combined_file = resource_filename(__name__, "data/dummy1_produced_combine_output_single.txt")
46 msclust_quantification_and_spectra_file = resource_filename(__name__, "data/dummy1_sim.txt")
47 output_csv = resource_filename(__name__, outdir + "metexp_tabular.txt")
48
49 sys.argv = ['test',
50 rankfilter_and_caslookup_combined_file,
51 msclust_quantification_and_spectra_file,
52 output_csv,
53 'tomato',
54 'leafs',
55 'test experiment',
56 'pieter',
57 'DB5 column']
58
59 # Execute main function with arguments provided through sys.argv
60 export_to_metexp_tabular.main()
61
62 '''
63 # Asserts are based on reading in with process_data and comparing values of
64 # certain columns
65
66 # Check 3: library_lookup RI column, centrotype column, ri_svr column are correct:
67 caslookup_items = combine_output._process_data(input_caslookup)
68 rankfilter_items = combine_output._process_data(input_rankfilter)
69
70 # check that the caslookup RI column is correctly maintained in its original order in
71 # the combined file:
72 ri_caslookup = caslookup_items['RI']
73 ri_combine_single = combine_result_single_items['RI']
74 self.assertListEqual(ri_caslookup, ri_combine_single)
75
76 # check the centrotype column's integrity:
77 centrotype_caslookup = caslookup_items['Centrotype']
78 centrotype_combine_single = combine_result_single_items['Centrotype']
79 centrotype_rankfilter = _get_centrotype_rankfilter(rankfilter_items['ID'])
80 self.assertListEqual(centrotype_caslookup, centrotype_combine_single)
81 self.assertListEqual(centrotype_caslookup, centrotype_rankfilter)
82
83 # integration and integrity checks:
84 file_NIST = resource_filename(__name__, "data/integration/NIST_identification_results_tabular.txt")
85 file_NIST_items = combine_output._process_data(file_NIST)
86 # check that rank filter output has exactly the same ID items as the original NIST input file:
87 self.assertListEqual(file_NIST_items['ID'], rankfilter_items['ID'])
88 # check the same for the CAS column:
89 self.assertListEqual(_get_strippedcas(file_NIST_items['CAS']), rankfilter_items['CAS'])
90 # now check the NIST CAS column against the cas lookup results:
91 cas_NIST = _get_processedcas(file_NIST_items['CAS'])
92 self.assertListEqual(cas_NIST, caslookup_items['CAS'])
93 # now check the CAS of the combined result. If all checks are OK, it means the CAS column's order
94 # and values remained stable throughout all steps:
95 self.assertListEqual(rankfilter_items['CAS'], combine_result_single_items['CAS'])
96
97 # check that the rankfilter RIsvr column is correctly maintained in its original order in
98 # the combined file:
99 risvr_rankfilter = rankfilter_items['RIsvr']
100 risvr_combine_single = combine_result_single_items['RIsvr']
101 self.assertListEqual(risvr_rankfilter, risvr_combine_single)
102 '''
103
104
105
106 def _read_file(filename):
107 '''
108 Helper method to quickly read a file
109 @param filename:
110 '''
111 with open(filename) as handle:
112 return handle.read()