def export_allMetaboliteFragmentMappings_csv(filename_I='ms_method_fragments.csv',mode_I='-',msmethodtype_I='tuning'): '''export all metabolite fragment mappings to .csv INPUT: filename_I = string, filename mode_I = string, MS mode (default=-) msmethodtype_I = string, ms_methodtype (default=tuning) ''' mfautil = MFA_utilities(); #Query met_ids met_ids = []; met_ids = msmethodquery.get_metIDs_msModeAndMsMethodType('-','tuning'); data_O = []; for met in met_ids: # fragments fragment_formulas = []; parent,product=[],[]; parent,product= msmethodquery.get_precursorAndProductFormulas_metID(met,'-','tuning'); parent = list(set(parent)); fragment_formulas.extend(parent); fragment_formulas.extend(product); # fragment carbon mappings frag_cmap = {}; frag_cmap = msmethodquery.get_precursorFormulaAndProductFormulaAndCMapsAndPositions_metID(met,'-','tuning'); for frag in fragment_formulas: if frag_cmap[frag]['fragment'] is None: continue; # combine into a structure positions,elements = [],[]; positions,elements = mfautil.convert_fragmentAndElements2PositionAndElements(frag_cmap[frag]['fragment'],frag_cmap[frag]['fragment_elements']); if positions: tmp = {'met_id':met,'positions':positions,'elements':elements,'formula':frag}; data_O.append(tmp); # export the data if data_O: export = base_exportData(data_O) export.write_dict2csv(filename_I);
def execute_makeMeasuredFragments(self,experiment_id_I, sample_name_abbreviations_I = [], time_points_I = [], scan_types_I = [], met_ids_I = []): '''Collect and format MS data from data_stage01_isotopomer_averagesNormSum for fluxomics simulation''' mfautilities = MFA_utilities(); # get experiment information: met_id_conv_dict = {'Hexose_Pool_fru_glc-D':'glc-D', 'Pool_2pg_3pg':'3pg', '23dpg':'13dpg'}; data_O = []; experiment_stdev = []; # get sample names and sample name abbreviations if sample_name_abbreviations_I: sample_abbreviations = sample_name_abbreviations_I; st = 'Unknown'; sample_types_lst = []; sample_types_lst.extend([st for i in range(len(sample_abbreviations))]); else: sample_abbreviations = []; sample_types = ['Unknown']; sample_types_lst = []; for st in sample_types: sample_abbreviations_tmp = []; sample_abbreviations_tmp = self.get_sampleNameAbbreviations_experimentIDAndSampleType_dataStage01AveragesNormSum(experiment_id_I,st); sample_abbreviations.extend(sample_abbreviations_tmp); sample_types_lst.extend([st for i in range(len(sample_abbreviations_tmp))]); for sna_cnt,sna in enumerate(sample_abbreviations): print('Collecting experimental MS data for sample name abbreviation ' + sna); # get time points if time_points_I: time_points = time_points_I; else: time_points = []; time_points = self.get_timePoint_experimentIDAndSampleNameAbbreviation_dataStage01AveragesNormSum(experiment_id_I,sna); for tp in time_points: print('Collecting experimental MS data for time-point ' + str(tp)); # get the scan_types if scan_types_I: scan_types = []; scan_types_tmp = []; scan_types_tmp = self.get_scanTypes_experimentIDAndTimePointAndSampleAbbreviationsAndSampleType_dataStage01AveragesNormSum(experiment_id_I,tp,sna,sample_types_lst[sna_cnt]); scan_types = [st for st in scan_types_tmp if st in scan_types_I]; else: scan_types = []; scan_types = self.get_scanTypes_experimentIDAndTimePointAndSampleAbbreviationsAndSampleType_dataStage01AveragesNormSum(experiment_id_I,tp,sna,sample_types_lst[sna_cnt]); for scan_type in scan_types: print('Collecting experimental MS data for scan type ' + scan_type) # met_ids if not met_ids_I: met_ids = []; met_ids = self.get_metIDs_experimentIDAndSampleAbbreviationAndTimePointAndSampleTypeAndScanType_dataStage01AveragesNormSum( \ experiment_id_I,sna,tp,sample_types_lst[sna_cnt],scan_type); else: met_ids = met_ids_I; if not(met_ids): continue #no component information was found for met in met_ids: print('Collecting experimental MS data for metabolite ' + met); # format the metabolite if met in list(met_id_conv_dict.keys()): met_formatted = met_id_conv_dict[met]; else: met_formatted = met; met_formatted = re.sub('-','_DASH_',met_formatted) met_formatted = re.sub('[(]','_LPARANTHES_',met_formatted) met_formatted = re.sub('[)]','_RPARANTHES_',met_formatted) # fragments fragment_formulas = []; fragment_formulas = self.get_fragmentFormula_experimentIDAndSampleAbbreviationAndTimePointAndSampleTypeAndScanTypeAndMetID_dataStage01AveragesNormSum( \ experiment_id_I,sna,tp,sample_types_lst[sna_cnt],scan_type,met); # frag c map frag_cmap = {}; frag_cmap = self.get_precursorFormulaAndProductFormulaAndCMapsAndPositions_metID(met,'-','tuning'); for frag in fragment_formulas: # data data_mat = []; data_mat_cv = []; data_mat_n = []; data_mat, data_mat_cv, data_mat_n = self.get_spectrum_experimentIDAndSampleAbbreviationAndTimePointAndSampleTypeAndScanTypeAndMetIDAndFragmentFormula_dataStage01AveragesNormSum( \ experiment_id_I,sna,tp,sample_types_lst[sna_cnt],scan_type,met,frag); # combine into a structure positions,elements = [],[]; positions,elements = mfautilities.convert_fragmentAndElements2PositionAndElements(frag_cmap[frag]['fragment'],frag_cmap[frag]['fragment_elements']); #fragname = met_formatted+'_c'+'_'+ re.sub('[-+]','',frag); fragname = met_formatted+'_c'+'_'+ re.sub('[-+]','',frag)+'_'+scan_type; data_names = []; data_stdev = []; data_stderr = []; for i,d in enumerate(data_mat): stdev = 0.0; stderr = 0.0; if data_mat_cv[i]: if data_mat_n[i]==1: stdev = 0.05; else: stdev = data_mat[i]*data_mat_cv[i]/100; stderr = stdev/sqrt(data_mat_n[i]); data_names.append(fragname+str(i)); data_stdev.append(stdev); data_stderr.append(stderr); experiment_stdev.append(stdev); data_tmp = {'experiment_id':experiment_id_I, 'sample_name_abbreviation':sna, 'sample_type':sample_types_lst[sna_cnt], 'time_point':tp, 'met_id':met_formatted+'_c', 'fragment_id':fragname, 'fragment_formula':frag, 'intensity_normalized_average':data_mat, 'intensity_normalized_cv':data_mat_cv, 'intensity_normalized_stdev':data_stdev, 'intensity_normalized_n':data_mat_n, 'intensity_normalized_units':'normSum', 'met_elements':elements, 'met_atompositions':positions}; data_O.append(data_tmp); #add data to the database row = []; row = data_stage02_isotopomer_measuredFragments( experiment_id_I, sna, tp, met_formatted+'_c', fragname, frag, data_mat, data_mat_cv, data_stdev, 'normSum', scan_type, elements, positions, True, None); self.session.add(row); self.session.commit();