def bulk_background_correction(fragment_dict, list_of_replicates, sample_background, isotope_dict, decimals): input_fragments = [] unlabeled_fragment = [] corrected_fragments_dict = {} for key, value in fragment_dict.iteritems(): if value.unlabeled: unlabeled_fragment.append((key, value)) input_fragments.append((key, value)) else: input_fragments.append((key, value)) try: assert len(unlabeled_fragment) == 1 except AssertionError: raise AssertionError( 'The input should contain atleast and only one unlabeled fragment data' 'Please check metadata or raw data files') for input_fragment in input_fragments: replicate_value = background(list_of_replicates, input_fragment[1], unlabeled_fragment[0][1], isotope_dict) #corrected_sample_data = bacground_correction( # replicate_value, sample_background, input_fragment[1].data, decimals) sample_data = input_fragment[1].data corrected_sample_data = {} for key, value in sample_data.iteritems(): background_value = replicate_value[sample_background[key]] new_value = np.around(value - background_value, decimals) corrected_sample_data[key] = new_value corrected_fragments_dict[input_fragment[0]] = Infopacket( input_fragment[1].frag, corrected_sample_data, input_fragment[1].unlabeled, input_fragment[1].name) return corrected_fragments_dict
def na_correction_mimosa_by_fragment(fragments_dict, isotope_dict, decimals): fragment_dict_mass = change_fragment_keys_to_mass(fragments_dict) corrected_dict_mass = {} for key, value in fragment_dict_mass.iteritems(): m_1_n = (key[0] - 1, key[1]) m_1_n_1 = (key[0] - 1, key[1] - 1) parent_frag_m, daughter_frag_n = value.frag isotope = parent_frag_m.isotracer na = helpers.get_isotope_na(isotope, isotope_dict) corrected_data = {} for sample_name, intensity_m_n in value.data.iteritems(): try: intensity_m_1_n = fragment_dict_mass[m_1_n].data[sample_name] except KeyError: intensity_m_1_n = 0 try: intensity_m_1_n_1 = fragment_dict_mass[m_1_n_1].data[ sample_name] except KeyError: intensity_m_1_n_1 = 0 corrected_data[sample_name] = na_correct_mimosa_algo_array( parent_frag_m, daughter_frag_n, intensity_m_n, intensity_m_1_n, intensity_m_1_n_1, isotope, na, decimals) corrected_dict_mass[key] = Infopacket(value.frag, corrected_data, value.unlabeled, value.name) return corrected_dict_mass
def test_fragmentdict_model(): cit_frag = Fragment('Citruline', 'C6H13N3O3', label_dict={'C13': 0}) fragments_dict = { 'Citruline_C13_0_N15_0': Infopacket(frag=cit_frag, data={'sample_1': np.array([0.3624])}, unlabeled=True, name='Acetic') } lab_sam_dict = {(0, 0): {'sample_1': 0.3624}} assert algo.fragmentdict_model(['C13', 'N15'], fragments_dict, lab_sam_dict) == { 'Citruline_C13_0_N15_0': Infopacket(frag=cit_frag, data={'sample_1': 0.3624}, unlabeled=True, name='Acetic') }
def test_unique_samples_for_dict(): A = { 'Acetic_C13_0': Infopacket(frag='H4C2O2', data={'sample_1': 0.3624}, unlabeled=True, name='Acetic'), 'Acetic_C13_1': Infopacket(frag='H4C2O2', data={'sample_1': 0.040349999999999997}, unlabeled=False, name='Acetic'), 'Acetic_C13_2': Infopacket(frag='H4C2O2', data={'sample_1': 0.59724999999999995}, unlabeled=False, name='Acetic') } assert algo.unique_samples_for_dict(A) == ['sample_1']
def test_add_data_fragment(): frag_dict = {('Succinate 121/103_121.0', 'Succinate 121/103_103.0'): ['C4H5O4', 'C4H3O3']} data = {'TA_SCS-ATP BCH_19May16_1June16.wiff (sample 134)': 5187.6000000000004, 'TA_SCS-ATP BCH_19May16_1June16.wiff (sample 122)': 0.0, 'TA_SCS-ATP BCH_19May16_1June16.wiff (sample 190)': 4480.1999999999998} label_info = False name = 'Succinate 117/99' assert iso.add_data_fragment(frag_dict, data, label_info, name) == {('Succinate 121/103_121.0', 'Succinate 121/103_103.0'): Infopacket(frag=['C4H5O4', 'C4H3O3'], data={'TA_SCS-ATP BCH_19May16_1June16.wiff (sample 122)': 0.0, 'TA_SCS-ATP BCH_19May16_1June16.wiff (sample 190)': 4480.2, 'TA_SCS-ATP BCH_19May16_1June16.wiff (sample 134)': 5187.6}, unlabeled=False, name='Succinate 117/99')}
def fragmentdict_model(iso_tracers, fragments_dict, lab_samp_dict): """ This function creates a model of fragments dictionary on which model functions can applied Args: iso_tracers : list of isotopic tracers fragments_dict : dictionary of the form, example : {'Aceticacid_C13_1': [C2H4O2, {'sample_1': array([ 0.0164])}, False, 'Aceticacid'] lab_samp_dict : dictionary of the form {(0, 1): {'sample_1': 0.0619}....} Returns: nacorr_fragment_dict : fragment dictionary model """ nacorr_fragment_dict = {} for frag_name, frag_info in fragments_dict.iteritems(): if len(iso_tracers) == 1: lab_tup_key = frag_info.frag.get_num_labeled_atoms_isotope( iso_tracers[0]) elif len(iso_tracers) > 1: try: lab_tup_key = [] for isotope in iso_tracers: lab_tup_key.append( frag_info.frag.get_num_labeled_atoms_isotope(isotope)) lab_tup_key = tuple(lab_tup_key) except KeyError: raise KeyError( 'Name, Formula or Sample not found in input data file') nacorr_fragment_dict[frag_name] = Infopacket( frag_info.frag, lab_samp_dict[lab_tup_key], frag_info.unlabeled, frag_info.name) return nacorr_fragment_dict
def output_constants(): nest_dict = { out.OutKey(name='L-Methionine', formula='C5H10NO2S'): { 'N15_0_C13_5': { 'sample_1': 3.156529191428407e-08 }, 'N15_0_C13_4': { 'sample_1': -4.6457232333485042e-06 }, 'N15_0_C13_1': { 'sample_1': 0.055358607526132128 }, 'N15_0_C13_0': { 'sample_1': 0.26081351241574013 }, 'N15_0_C13_3': { 'sample_1': 0.00010623115401093528 }, 'N15_0_C13_2': { 'sample_1': 0.0045440397693722904 }, 'N15_1_C13_0': { 'sample_1': 0.064545716018271096 }, 'N15_1_C13_1': { 'sample_1': 0.013283380798059349 }, 'N15_1_C13_2': { 'sample_1': 0.00084379489120955248 }, 'N15_1_C13_3': { 'sample_1': 6.1161350126415117e-05 }, 'N15_1_C13_4': { 'sample_1': -1.8408545189878132e-06 }, 'N15_1_C13_5': { 'sample_1': 0.60114017085422033 } } } acetic_frag = Fragment('Acetic', 'H4C2O2', label_dict={'C13': 1, 'C13': 2}) fragment_dict = { 'Acetic_C13_1': Infopacket(frag='H4C2O2', data={'sample_1': 1}, unlabeled=False, name='Acetic'), 'Acetic_C13_2': Infopacket(frag='H4C2O2', data={'sample_1': 0}, unlabeled=False, name='Acetic') } metabolite_dict = {('Acetic', 'H4C2O2'): fragment_dict} metabolite_dict = { ('L-Methionine', 'C5H10NO2S'): { 'C13_1': { 'sample_1': 3.18407678e-07 }, 'C13_0': { 'sample_1': 0.48557866 } } } df = pd.DataFrame({ 'Sample': { 0: 'sample_1', 1: 'sample_1', 2: 'sample_1', 3: 'sample_1', 4: 'sample_1', 5: 'sample_1', 6: 'sample_1', 7: 'sample_1', 8: 'sample_1', 9: 'sample_1', 10: 'sample_1', 11: 'sample_1' }, 'Formula': { 0: 'C5H10NO2S', 1: 'C5H10NO2S', 2: 'C5H10NO2S', 3: 'C5H10NO2S', 4: 'C5H10NO2S', 5: 'C5H10NO2S', 6: 'C5H10NO2S', 7: 'C5H10NO2S', 8: 'C5H10NO2S', 9: 'C5H10NO2S', 10: 'C5H10NO2S', 11: 'C5H10NO2S' }, 'Intensity': { 0: 3.156529191428407e-08, 1: -4.6457232333485042e-06, 2: 0.055358607526132128, 3: 0.26081351241574013, 4: 0.00010623115401093528, 5: 0.0045440397693722904, 6: 0.064545716018271096, 7: 0.013283380798059349, 8: 0.00084379489120955248, 9: 6.1161350126415117e-05, 10: -1.8408545189878132e-06, 11: 0.60114017085422033 }, 'Name': { 0: 'L-Methionine', 1: 'L-Methionine', 2: 'L-Methionine', 3: 'L-Methionine', 4: 'L-Methionine', 5: 'L-Methionine', 6: 'L-Methionine', 7: 'L-Methionine', 8: 'L-Methionine', 9: 'L-Methionine', 10: 'L-Methionine', 11: 'L-Methionine' }, 'Label': { 0: 'N15_0_C13_5', 1: 'N15_0_C13_4', 2: 'N15_0_C13_1', 3: 'N15_0_C13_0', 4: 'N15_0_C13_3', 5: 'N15_0_C13_2', 6: 'N15_1_C13_0', 7: 'N15_1_C13_1', 8: 'N15_1_C13_2', 9: 'N15_1_C13_3', 10: 'N15_1_C13_4', 11: 'N15_1_C13_5' } }) return nest_dict, df
def algorithms_yale_constants(): data_input = { 'TA_SCS-ATP BCH_19May16_1June16.wiff (sample 65)': 1967.77, 'TA_SCS-ATP BCH_19May16_1June16.wiff (sample 49)': 1446.77, 'TA_SCS-ATP BCH_19May16_1June16.wiff (sample 17)': 747.77, 'TA_SCS-ATP BCH_19May16_1June16.wiff (sample 81)': 1926.77, 'TA_SCS-ATP BCH_19May16_1June16.wiff (sample 1)': 1969.77, 'TA_SCS-ATP BCH_19May16_1June16.wiff (sample 33)': 661.77 } data_unlabel = { 'TA_SCS-ATP BCH_19May16_1June16.wiff (sample 65)': 62610, 'TA_SCS-ATP BCH_19May16_1June16.wiff (sample 49)': 58640, 'TA_SCS-ATP BCH_19May16_1June16.wiff (sample 17)': 59950, 'TA_SCS-ATP BCH_19May16_1June16.wiff (sample 81)': 62520, 'TA_SCS-ATP BCH_19May16_1June16.wiff (sample 1)': 59690, 'TA_SCS-ATP BCH_19May16_1June16.wiff (sample 33)': 57200 } corrected_data_input = { 'TA_SCS-ATP BCH_19May16_1June16.wiff (sample 65)': -55.07, 'TA_SCS-ATP BCH_19May16_1June16.wiff (sample 49)': -456.52, 'TA_SCS-ATP BCH_19May16_1June16.wiff (sample 17)': -1214.13, 'TA_SCS-ATP BCH_19May16_1June16.wiff (sample 81)': -94., 'TA_SCS-ATP BCH_19May16_1June16.wiff (sample 1)': 43.33, 'TA_SCS-ATP BCH_19May16_1June16.wiff (sample 33)': -1211.27 } corrected_data_unlabel = { 'TA_SCS-ATP BCH_19May16_1June16.wiff (sample 65)': 64676.13, 'TA_SCS-ATP BCH_19May16_1June16.wiff (sample 49)': 60575.12, 'TA_SCS-ATP BCH_19May16_1June16.wiff (sample 17)': 61928.35, 'TA_SCS-ATP BCH_19May16_1June16.wiff (sample 81)': 64583.16, 'TA_SCS-ATP BCH_19May16_1June16.wiff (sample 1)': 61659.77, 'TA_SCS-ATP BCH_19May16_1June16.wiff (sample 33)': 59087.6 } unlabeled_fragment = iso.insert_data_to_fragment_mass( Multiquantkey('2PG 185/79', 'O3P', '2PG 185/79', 'C3H6O7P'), 'C13_185.0_79.0', data_unlabel) input_fragment = iso.insert_data_to_fragment_mass( Multiquantkey('2PG 186/79', 'O3P', '2PG 185/79', 'C3H6O7P'), 'C13_186.0_79.0', data_input) parent_frag_input = input_fragment[('2PG 186/79_186.0', '2PG 186/79_79.0')][0][0] daughter_frag_input = input_fragment[('2PG 186/79_186.0', '2PG 186/79_79.0')][0][1] parent_frag_unlabeled = unlabeled_fragment[('2PG 185/79_185.0', '2PG 185/79_79.0')][0][0] daughter_frag_unlabeled = unlabeled_fragment[('2PG 185/79_185.0', '2PG 185/79_79.0')][0][1] fragment_dict = dict(unlabeled_fragment, **input_fragment) corrected_fragment_dict = { (186.0, 79.0): Infopacket( frag=['C3H6O7P', 'O3P'], data={ 'TA_SCS-ATP BCH_19May16_1June16.wiff (sample 65)': -73.459999999999994, 'TA_SCS-ATP BCH_19May16_1June16.wiff (sample 49)': -473.81999999999999, 'TA_SCS-ATP BCH_19May16_1June16.wiff (sample 17)': -1231.96, 'TA_SCS-ATP BCH_19May16_1June16.wiff (sample 81)': -112.37, 'TA_SCS-ATP BCH_19May16_1June16.wiff (sample 1)': 25.82, 'TA_SCS-ATP BCH_19May16_1June16.wiff (sample 33)': -1228.3 }, unlabeled=False, name='2PG 185/79'), (185.0, 79.0): Infopacket(frag=['C3H6O7P', 'O3P'], data={ 'TA_SCS-ATP BCH_19May16_1June16.wiff (sample 65)': 64694.910000000003, 'TA_SCS-ATP BCH_19May16_1June16.wiff (sample 49)': 60592.709999999999, 'TA_SCS-ATP BCH_19May16_1June16.wiff (sample 17)': 61946.339999999997, 'TA_SCS-ATP BCH_19May16_1June16.wiff (sample 81)': 64601.919999999998, 'TA_SCS-ATP BCH_19May16_1June16.wiff (sample 1)': 61677.68, 'TA_SCS-ATP BCH_19May16_1June16.wiff (sample 33)': 59104.760000000002 }, unlabeled=True, name='2PG 185/79') } unlabeled_fragment_dhap = iso.insert_data_to_fragment_mass( Multiquantkey('DHAP 169/97', 'H2O4P', 'DHAP 169/97', 'C3H6O6P'), 'C13_169.0_97.0', { 'TA_SCS-ATP BCH_19May16_1June16.wiff (sample 1)': 51670, 'TA_SCS-ATP BCH_19May16_1June16.wiff (sample 17)': 52360, 'TA_SCS-ATP BCH_19May16_1June16.wiff (sample 33)': 52540, 'TA_SCS-ATP BCH_19May16_1June16.wiff (sample 49)': 56580, 'TA_SCS-ATP BCH_19May16_1June16.wiff (sample 65)': 52580, 'TA_SCS-ATP BCH_19May16_1June16.wiff (sample 81)': 58990 }) input_fragment_dhap = iso.insert_data_to_fragment_mass( Multiquantkey('DHAP 170/97', 'H2O4P', 'DHAP 169/97', 'C3H6O6P'), 'C13_170.0_97.0', { 'TA_SCS-ATP BCH_19May16_1June16.wiff (sample 1)': 1292.67, 'TA_SCS-ATP BCH_19May16_1June16.wiff (sample 17)': 901.67, 'TA_SCS-ATP BCH_19May16_1June16.wiff (sample 33)': 1292.67, 'TA_SCS-ATP BCH_19May16_1June16.wiff (sample 49)': 1468.67, 'TA_SCS-ATP BCH_19May16_1June16.wiff (sample 65)': 1511.67, 'TA_SCS-ATP BCH_19May16_1June16.wiff (sample 81)': 1946.67 }) corrected_data_unlabel_dhap = { 'TA_SCS-ATP BCH_19May16_1June16.wiff (sample 1)': 53390.610000000001, 'TA_SCS-ATP BCH_19May16_1June16.wiff (sample 17)': 54103.589999999997, 'TA_SCS-ATP BCH_19May16_1June16.wiff (sample 33)': 54289.580000000002, 'TA_SCS-ATP BCH_19May16_1June16.wiff (sample 49)': 58464.110000000001, 'TA_SCS-ATP BCH_19May16_1June16.wiff (sample 65)': 54330.910000000003, 'TA_SCS-ATP BCH_19May16_1June16.wiff (sample 81)': 60954.370000000003 } corrected_data_input_dhap = { 'TA_SCS-ATP BCH_19May16_1June16.wiff (sample 1)': -399.24000000000001, 'TA_SCS-ATP BCH_19May16_1June16.wiff (sample 17)': -821.89999999999998, 'TA_SCS-ATP BCH_19May16_1June16.wiff (sample 33)': -428.20999999999998, 'TA_SCS-ATP BCH_19May16_1June16.wiff (sample 49)': -382.83999999999997, 'TA_SCS-ATP BCH_19May16_1June16.wiff (sample 65)': -205.68000000000001, 'TA_SCS-ATP BCH_19May16_1June16.wiff (sample 81)': 25.52 } parent_frag_input_dhap = input_fragment_dhap[('DHAP 170/97_170.0', 'DHAP 170/97_97.0')][0][0] daughter_frag_input_dhap = input_fragment_dhap[('DHAP 170/97_170.0', 'DHAP 170/97_97.0')][0][1] parent_frag_unlabeled_dhap = unlabeled_fragment_dhap[( 'DHAP 169/97_169.0', 'DHAP 169/97_97.0')][0][0] daughter_frag_unlabeled_dhap = unlabeled_fragment_dhap[( 'DHAP 169/97_169.0', 'DHAP 169/97_97.0')][0][1] corrected_fragment_dict_dhap = { (169.0, 97.0): iso.Infopacket( frag=[parent_frag_unlabeled_dhap, daughter_frag_unlabeled_dhap], data=corrected_data_unlabel_dhap, unlabeled=True, name='DHAP 169/97'), (170.0, 97.0): iso.Infopacket(frag=[parent_frag_input_dhap, daughter_frag_input_dhap], data=corrected_data_input_dhap, unlabeled=False, name='DHAP 169/97') } fragment_dict_dhap = dict(unlabeled_fragment_dhap, **input_fragment_dhap) metabolite_frag_dict = {'dhap': fragment_dict_dhap, '2pg': fragment_dict} corrected_metabolite_dict = { 'dhap': corrected_fragment_dict_dhap, '2pg': corrected_fragment_dict } return parent_frag_input, daughter_frag_input, \ fragment_dict, data_input, parent_frag_unlabeled, \ daughter_frag_unlabeled, data_unlabel, corrected_fragment_dict, metabolite_frag_dict, corrected_fragment_dict_dhap, corrected_fragment_dict