view test/test_export_to_metexp_tabular.py @ 62:9bd2597c8851 default tip

r
author pieter.lukasse@wur.nl
date Fri, 06 Feb 2015 15:49:26 +0100
parents 19d8fd10248e
children
line wrap: on
line source

'''Integration tests for the GCMS project'''

from pkg_resources import resource_filename  # @UnresolvedImport # pylint: disable=E0611
from GCMS import export_to_metexp_tabular
import os.path
import sys
import unittest


class IntegrationTest(unittest.TestCase):


    def test_MM_calculations(self):
        '''
        test the implemented method for MM calculations for 
        given chemical formulas
        '''
        export_to_metexp_tabular.init_elements_and_masses_map()
        
        formula = "C8H18O3"
        # should be = 12.01*8 + 1.01*18 + 16*3 = 162.26
        result = export_to_metexp_tabular.get_molecular_mass(formula)
        self.assertEqual(162.26, result)
        
        formula = "CH2O3Fe2Ni"
        # should be = 12.01*1 + 1.01*2 + 16*3 + 55.85*2 + 58.71 = 232.44
        result = export_to_metexp_tabular.get_molecular_mass(formula)
        self.assertAlmostEqual(232.44, result, 2)
        
        
        
        

    def test_combine_output_simple(self):
        '''
        comment me
        '''
        # Create out folder
        outdir = "output/metexp/"
        if not os.path.exists(outdir):
            os.makedirs(outdir)

        #Build up arguments and run
        
        rankfilter_and_caslookup_combined_file = resource_filename(__name__, "data/dummy1_produced_combine_output_single.txt")
        msclust_quantification_and_spectra_file = resource_filename(__name__, "data/dummy1_sim.txt")
        output_csv = resource_filename(__name__, outdir + "metexp_tabular.txt")
    
        sys.argv = ['test',
                    rankfilter_and_caslookup_combined_file,
                    msclust_quantification_and_spectra_file,
                    output_csv, 
                    'tomato',
                    'leafs',
                    'test experiment',
                    'pieter',
                    'DB5 column']
        
        # Execute main function with arguments provided through sys.argv
        export_to_metexp_tabular.main()

        '''
        # Asserts are based on reading in with process_data and comparing values of 
        # certain columns
        
        # Check 3: library_lookup RI column, centrotype column, ri_svr column are correct:
        caslookup_items = combine_output._process_data(input_caslookup)
        rankfilter_items = combine_output._process_data(input_rankfilter)
        
        # check that the caslookup RI column is correctly maintained in its original order in
        # the combined file:
        ri_caslookup = caslookup_items['RI']
        ri_combine_single = combine_result_single_items['RI']
        self.assertListEqual(ri_caslookup, ri_combine_single) 
        
        # check the centrotype column's integrity:
        centrotype_caslookup = caslookup_items['Centrotype']
        centrotype_combine_single = combine_result_single_items['Centrotype']
        centrotype_rankfilter = _get_centrotype_rankfilter(rankfilter_items['ID'])
        self.assertListEqual(centrotype_caslookup, centrotype_combine_single)
        self.assertListEqual(centrotype_caslookup, centrotype_rankfilter)
                
        # integration and integrity checks:
        file_NIST = resource_filename(__name__, "data/integration/NIST_identification_results_tabular.txt")
        file_NIST_items = combine_output._process_data(file_NIST)
        # check that rank filter output has exactly the same ID items as the original NIST input file:
        self.assertListEqual(file_NIST_items['ID'], rankfilter_items['ID']) 
        # check the same for the CAS column:
        self.assertListEqual(_get_strippedcas(file_NIST_items['CAS']), rankfilter_items['CAS'])
        # now check the NIST CAS column against the cas lookup results:  
        cas_NIST = _get_processedcas(file_NIST_items['CAS'])
        self.assertListEqual(cas_NIST, caslookup_items['CAS'])
        # now check the CAS of the combined result. If all checks are OK, it means the CAS column's order
        # and values remained stable throughout all steps: 
        self.assertListEqual(rankfilter_items['CAS'], combine_result_single_items['CAS']) 
        
        # check that the rankfilter RIsvr column is correctly maintained in its original order in
        # the combined file:
        risvr_rankfilter = rankfilter_items['RIsvr']
        risvr_combine_single = combine_result_single_items['RIsvr']
        self.assertListEqual(risvr_rankfilter, risvr_combine_single) 
        '''
        
   

def _read_file(filename):
    '''
    Helper method to quickly read a file
    @param filename:
    '''
    with open(filename) as handle:
        return handle.read()