def setUp(self): ''' setup SBtabDocument class with files from test directory ''' #self.table_names = [f for f in os.listdir('tables/') if os.path.isfile(os.path.join('tables/', f))] self.table_names = [ 'teusink_compartment.csv', 'teusink_compound.csv', 'teusink_data.tsv', 'teusink_reaction.tsv' ] self.doc_names = [ f for f in os.listdir('python/tests/docs/') if os.path.isfile(os.path.join('python/tests/docs/', f)) ] self.sbtabs = [] for t in self.table_names: p = open('python/tests/tables/' + t, 'r') p_content = p.read() sbtab = SBtab.SBtabTable(p_content, t) # initialise SBtabDocument with the SBtabTable, but only save the table for further testing sbtab_doc = SBtab.SBtabDocument('test_doc', sbtab_init=sbtab) self.sbtabs.append(sbtab) p.close() self.docs = [] for i, d in enumerate(self.doc_names): if not d.startswith('_'): p = open('python/tests/docs/' + d, 'r') p_content = p.read() sbtab_doc = SBtab.SBtabDocument('test_' + str(i), sbtab_init=p_content, filename=d) self.docs.append(sbtab_doc) p.close()
def setUp(self): ''' setup SBtabTable class with files from test directory ''' self.table_names = [f for f in os.listdir('python/tests/tables/') if os.path.isfile(os.path.join('python/tests/tables/', f))] self.doc_names = [f for f in os.listdir('python/tests/docs/') if os.path.isfile(os.path.join('python/tests/docs/', f))] self.sbtabs = [] self.sbtab_docs = [] for i, t in enumerate(self.table_names): if t.startswith('_'): continue p = open('python/tests/tables/' + t, 'r') p_content = p.read() sbtab = SBtab.SBtabTable(p_content, t) self.sbtabs.append(sbtab) p.close() for i, d in enumerate(self.doc_names): if d.startswith('_'): continue p = open('python/tests/docs/' + d, 'r') p_content = p.read() sbtab_doc = SBtab.SBtabDocument('test_'+str(i),sbtab_init=p_content, filename=d) self.sbtab_docs.append(sbtab_doc) p.close()
def sbtab2html_wrapper(sbtab, multiple, output, template, links, pageheader, definitions_file,show_table_text=True): ''' commandline wrapper for sbtab_to_html function ''' print(pageheader) # open and create SBtab try: f = open(sbtab, 'r').read() except: raise SBtabError('SBtab file %s could not be found.' % sbtab) # count given tabs and prepare file name w/o path tab_amount = misc.count_tabs(f) if '/' in sbtab: name_pre = sbtab.split('/')[-1:] else: name_pre = sbtab # count given tabs and prepare file name w/o path if output is not None: outfile_dir, outfile_file = os.path.split(output) if not os.path.exists(outfile_dir): os.mkdir(outfile_dir) outfile_file=os.path.splitext(outfile_file)[0] name_pre[0] = outfile_file else: outfile_dir = '.' file_basename = os.path.splitext(name_pre[0])[0] if tab_amount > 1: multiple = True if multiple: try: sbtab_doc = SBtab.SBtabDocument('sbtab_doc_prelim', f, sbtab) for tab in sbtab_doc.sbtabs: if len(file_basename): name = outfile_dir + '/' + file_basename + '_' + tab.table_id + '.html' else: name = outfile_dir + '/' + tab.table_id + '.html' try: write_html(tab, name, template, links, pageheader, definitions_file,show_table_text=show_table_text) except: raise SBtabError('The HTML file %s could not be created.' % name) except: raise SBtabError('The multiple HTML files could not be created.') else: try: if tab_amount > 1: sbtab = SBtab.SBtabDocument('sbtab_doc_prelim', f, sbtab) name = outfile_dir + '/' + file_basename + '_' + sbtab.table_id + '.html' else: sbtab = SBtab.SBtabTable(f, sbtab) name = outfile_dir + '/' + file_basename + '.html' write_html(sbtab, name, template, links, pageheader, definitions_file,show_table_text=show_table_text) except: raise SBtabError('The HTML file could not be created.')
def checkTabs(document, filename, separator=None): ''' this function checks, how many SBtab files are given by the user and saves it/them in a list, moreover stores the SBtab types in a dict linking to the SBtabs ''' sbtabs = [] types = [] docs = [] tnames = [] type2sbtab = {} #if there are more than one SBtabs given in single files that might be comprised of several SBtabs: if len(document) > 1: for single_document in document: #check for several SBtabs in one document document_rows = single_document.split('\n') tabs_in_document = getAmountOfTables(document_rows) if tabs_in_document > 1: sbtabs = splitDocumentInTables(document_rows) else: sbtabs = [document_rows] #generate SBtab class instance for every SBtab for sbtab in sbtabs: new_tablib_obj = tablibIO.importSetNew(sbtab, filename, separator) single_tab = SBtab.SBtabTable(new_tablib_obj, filename) type2sbtab[single_tab.table_type] = single_tab types.append(single_tab.table_type) docs.append(single_tab.table_document) try: tnames.append(single_tab.table_name) except: tnames.append('') #elif there is only one document given, possibly consisting of several SBtabs else: #check for several SBtabs in one document document_rows = document[0].split('\n') tabs_in_document = getAmountOfTables(document_rows) if tabs_in_document > 1: sbtabs = splitDocumentInTables(document_rows) else: sbtabs = [document_rows] #generate SBtab class instance for every SBtab for sbtab in sbtabs: as_sbtab = '\n'.join(sbtab) new_tablib_obj = tablibIO.importSetNew(as_sbtab, filename, separator) single_tab = SBtab.SBtabTable(new_tablib_obj, filename) type2sbtab[single_tab.table_type] = single_tab types.append(single_tab.table_type) docs.append(single_tab.table_document) try: tnames.append(single_tab.table_name) except: tnames.append('') return sbtabs, types, docs, tnames
def validator_wrapper(args): ''' commandline wrapper for the SBtab validator ''' # open and create SBtab try: f = open(args.sbtab, 'r').read() except: raise SBtabError('SBtab file %s could not be found.' % args.sbtab) if args.document: try: sbtab_doc = SBtab.SBtabDocument('validation_document', f, args.sbtab) except: raise SBtabError('SBtab Document %s could not be created.' % args.document_name) else: try: sbtab = SBtab.SBtabTable(f, args.sbtab) except: raise SBtabError('SBtab Table %s could not be created.' % args.sbtab) # if definitions file is given create SBtab object if args.sbtab_definitions: try: d = open(args.sbtab_definitions, 'r').read() sbtab_def = SBtab.SBtabTable(d, args.sbtab_definitions) except: raise SBtabError('The definitions file %s could not be found.' % args.sbtab_definitions) else: sbtab_def = None # create validator class and validate SBtab object if args.document: try: validate_doc = validatorSBtab.ValidateDocument( sbtab_doc, sbtab_def) warnings = validate_doc.validate_document() print(warnings) except: raise SBtabError('SBtab Document %s could not be validated.' % args.document_name) else: try: validate_table = validatorSBtab.ValidateTable(sbtab, sbtab_def) warnings = validate_table.return_output() print(warnings) except: raise SBtabError('SBtab Table %s could not be validated.' % args.sbtab)
def setUp(self): ''' setup SBtabTable class with files from test directory ''' self.table_names = [ f for f in os.listdir('python/tests/tables/') if os.path.isfile(os.path.join('python/tests/tables/', f)) ] self.doc_names = [ f for f in os.listdir('python/tests/docs/') if os.path.isfile(os.path.join('python/tests/docs/', f)) ] self.sbml_names = [ f for f in os.listdir('python/tests/sbml/') if os.path.isfile(os.path.join('python/tests/sbml/', f)) ] self.sbtabs = [] for t in self.table_names: if not t.startswith('_'): p = open('python/tests/tables/' + t, 'r') p_content = p.read() sbtab = SBtab.SBtabTable(p_content, t) self.sbtabs.append(sbtab) p.close() self.docs = [] for i, d in enumerate(self.doc_names): if not d.startswith('_'): p = open('python/tests/docs/' + d, 'r') p_content = p.read() sbtab = SBtab.SBtabDocument('test_' + str(i), sbtab_init=p_content, filename=d) self.docs.append(sbtab) p.close() self.sbml_docs = [] self.sbml_strings = [] reader = libsbml.SBMLReader() for i, s in enumerate(self.sbml_names): if s.startswith('_'): continue # save SBML objects doc = reader.readSBML('python/tests/sbml/' + s) self.sbml_docs.append(doc) # also save the strings sbml = open('python/tests/sbml/' + s, 'r') self.sbml_strings.append(sbml.read()) sbml.close()
def open_definitions_file(_path=None): ''' Opens the SBtab definitions file, which can be in several locations. Parameters ---------- _path: str Optional path to the definitions.tsv. Returns: SBtab.SBtabTable SBtab definitions file as SBtabTable object. ''' sbtab_def = False if _path: try_paths = [_path] else: try_paths = [ 'definitions.tsv', os.path.join(os.path.dirname(__file__), '../static/files/default_files/definitions.tsv'), os.path.join(os.path.dirname(__file__), '../definition_table/definitions.tsv'), os.path.join(os.path.dirname(__file__), 'definitions.tsv') ] for path in try_paths: try: def_file = open(path, 'r') sbtab_def = SBtab.SBtabTable(def_file.read(), 'definitions.tsv') def_file.close() break except: pass return sbtab_def
def read_definition(self, def_table): ''' read the required definition file; either it is provided by the user or the default definition file is read in; otherwise program exit ''' # read in provided definition table or open default if def_table: try: self.definitions = def_table.sbtab_list except: print('''Definition file could not be loaded, so the validation could not be started. Please provide definition file as argument''') sys.exit() else: try: d = os.path.dirname(os.path.abspath(__file__)) + '/files/default_'\ 'files/definitions.tsv' def_file = open(d, 'r') def_table = def_file.read() sbtab_def = SBtab.SBtabTable(def_table, d) self.definitions = sbtab_def.sbtab_list except: print('''Definition file could not be loaded, so the validation could not be started. Please provide definition file as argument''') sys.exit()
def makeSBtabs(self): ''' Generates the SBtab files. ''' self.warnings = [] sbtabs = [] #self.testForInconvertibles() for sbtab_type in allowed_sbtabs: try: function_name = 'self.' + sbtab_type.lower() + 'SBtab()' new_sbtab = eval(function_name) if new_sbtab != False: sbtabs.append(new_sbtab) except: pass sbtabs = self.getRidOfNone(sbtabs) sbtabs = self.getRidOfEmptyColumns(sbtabs) sbtab_objects = [] for sbtab in sbtabs: so = SBtab.SBtabTable(sbtab[0], self.filename[:-4] + '_%s.tsv' % sbtab[1]) sbtab_objects.append(so) return (sbtab_objects, self.warnings)
def compartment_sbtab(self): ''' build a compartment SBtab ''' # header row sbtab_compartment = '!!SBtab TableID="compartment" Document="%s" TableT'\ 'ype="Compartment" TableName="Compartment" SBtabVersion="1.0"'\ '\n' % self.filename # columns columns = ['!ID', '!Name', '!Size', '!Unit', '!SBOTerm'] sbtab_compartment += '\t'.join(columns) + '\n' # value rows for compartment in self.model.getListOfCompartments(): value_row = [''] * len(columns) value_row[0] = compartment.getId() if compartment.getName() != '': value_row[1] = compartment.getName() else: value_row[1] = compartment.getId() try: value_row[2] = str(compartment.getSize()) except: pass try: value_row[3] = str(species.getUnits()) except: pass if str(compartment.getSBOTerm()) != '-1': value_row[4] = 'SBO:%.7d' % compartment.getSBOTerm() sbtab_compartment += '\t'.join(value_row) + '\n' # build SBtab Table object from basic information sbtab_compartment = SBtab.SBtabTable( sbtab_compartment, self.filename + '_compartment.tsv') # test and extend for annotations for i, compartment in enumerate(self.model.getListOfCompartments()): try: annotations = self.get_annotations(compartment) for j, annotation in enumerate(annotations): col_name = '!Identifiers:' + annotation[1] if col_name not in columns: new_column = ['' ] * (self.model.getNumCompartments() + 1) new_column[0] = col_name new_column[i + 1] = annotation[0] columns.append(col_name) sbtab_compartment.add_column(new_column) else: sbtab_compartment.change_value_by_name( compartment.getId(), col_name, annotation[0]) except: self.warnings.append('Could not add the annotation %s for'\ 'compartment %s' % annotations[1], compartment.getId()) return sbtab_compartment
def fbc_gene(self): ''' builds a gene SBtab for the content of the fbc plugin ''' fbc_plugin = self.model.getPlugin('fbc') # header row sbtab_fbc_gene = '!!SBtab TableID="gene" Document="%s" TableType='\ '"Gene" TableName="FBC Gene" SBtabVersion="1.0"\n' % self.filename # columns columns = [ '!ID', '!SBML:fbc:ID', '!SBML:fbc:Name', '!SBML:fbc:geneProduct', '!SBML:fbc:label' ] sbtab_fbc_gene += '\t'.join(columns) + '\n' # value rows for gp in fbc_plugin.getListOfGeneProducts(): value_row = [''] * len(columns) value_row[0] = gp.getId() value_row[1] = gp.getId() try: value_row[2] = gp.getName() except: pass value_row[3] = 'True' try: value_row[4] = gp.getLabel() except: pass sbtab_fbc_gene += '\t'.join(value_row) + '\n' sbtab_fbc_gene = SBtab.SBtabTable(sbtab_fbc_gene, self.filename + '_fbc_gene.tsv') # test and extend for annotations for i, gene in enumerate(fbc_plugin.getListOfGeneProducts()): try: annotations = self.get_annotations(gene) for j, annotation in enumerate(annotations): col_name = '!Identifiers:' + annotation[1] if col_name not in columns: new_column = ['' ] * (fbc_plugin.getNumGeneProducts() + 1) new_column[0] = col_name new_column[i + 1] = annotation[0] columns.append(col_name) sbtab_fbc_gene.add_column(new_column) else: sbtab_fbc_gene.change_value_by_name( gene.getId(), col_name, annotation[0]) except: self.warnings.append('Could not add the annotation %s for '\ 'gene %s' % annotations[1], gene.getId()) return sbtab_fbc_gene
def test_build_empty_object(self): ''' test if empty SBtabDocument can be built ''' sbtab_doc = SBtab.SBtabDocument('empty_doc') self.assertEqual(type(sbtab_doc), SBtab.SBtabDocument) self.assertIsNone(sbtab_doc.filename) self.assertEqual(sbtab_doc.sbtabs, [])
def compound_sbtab(self): ''' build a compound SBtab ''' # header row sbtab_compound = '!!SBtab SBtabVersion="1.0" Document="%s" TableType='\ '"Compound" TableName="Compound"\n' % self.filename # columns columns = ['!ID', '!Name', '!Location', '!Charge', '!IsConstant', '!SBOTerm', '!InitialConcentration', '!hasOnlySubstanceUnits'] sbtab_compound += '\t'.join(columns) + '\n' # value rows for species in self.model.getListOfSpecies(): value_row = [''] * len(columns) value_row[0] = species.getId() try: value_row[1] = species.getName() except: pass try: value_row[2] = species.getCompartment() except: pass try: value_row[3] = str(species.getCharge()) except: pass try: value_row[4] = str(species.getConstant()) except: pass if str(species.getSBOTerm()) != '-1': value_row[5] ='SBO:%.7d'%species.getSBOTerm() try: value_row[6] = str(species.getInitialConcentration()) except: pass try: value_row[7] = str(species.getHasOnlySubstanceUnits()) except: pass sbtab_compound += '\t'.join(value_row) + '\n' sbtab_compound = SBtab.SBtabTable(sbtab_compound, self.filename + '_compound.tsv') # test and extend for annotations for i, species in enumerate(self.model.getListOfSpecies()): try: annotations = self.get_annotations(species) for j, annotation in enumerate(annotations): col_name = '!Identifiers:' + annotation[1] if col_name not in columns: new_column = [''] * (self.model.getNumSpecies() + 1) new_column[0] = col_name new_column[i + 1] = annotation[0] columns.append(col_name) sbtab_compound.add_column(new_column) else: sbtab_compound.change_value_by_name(species.getId(), col_name, annotation[0]) except: self.warnings.append('Could not add the annotation %s for'\ 'compound %s' % annotations[1], species.getId()) return sbtab_compound
def test_read_csv(self): ''' test the read wrapper of SBtab files ''' with self.assertRaises(SBtab.SBtabError): SBtab.read_csv('wrong_file_path', 'doc_name') with self.assertRaises(SBtab.SBtabError): SBtab.read_csv('wrong_file_path', 'doc_name', xlsx=True) valid_table_types = misc.extract_supported_table_types() for file_name in self.table_names: if not file_name.startswith('_'): sbtab = SBtab.read_csv('python/tests/tables/' + file_name, 'doc_name') self.assertEqual(type(sbtab), SBtab.SBtabDocument) for sbtab in self.sbtabs: self.assertIsNotNone(sbtab._get_header_row()) self.assertIsNotNone(sbtab.table_type) self.assertIn(sbtab.table_type, valid_table_types) self.assertIsNotNone(sbtab.table_name) self.assertEqual(sbtab.header_row[:7], '!!SBtab') self.assertIsNotNone(sbtab.header_row.find("'")) self.assertEqual(sbtab.header_row.find('"'), -1) sbtab_xlsx = SBtab.read_csv('python/tests/tables/_transition.xlsx', 'doc_name', xlsx=True) self.assertEqual(type(sbtab_xlsx), SBtab.SBtabDocument) with self.assertRaises(SBtab.SBtabError): sbtab_xlsx = SBtab.read_csv('python/tests/tables/_transition.xlsx', 'doc_name')
def test_singular(self): ''' test if the singular function is working (multiple SBtabs are rejected by SBtabTable) ''' p = open('python/tests/docs/teusink.tsv', 'r') p_content = p.read() with self.assertRaises(SBtab.SBtabError): sbtab = SBtab.SBtabTable(p_content, 'teusink.tsv') p.close()
def setUp(self): ''' setup SBtabTable class with files from test directory ''' self.table_names = [ f for f in os.listdir('python/tests/tables/') if os.path.isfile(os.path.join('python/tests/tables/', f)) ] self.doc_names = [ f for f in os.listdir('python/tests/docs/') if os.path.isfile(os.path.join('python/tests/docs/', f)) ] self.sbtab_docs = [] self.convert_document_objects = [] for i, t in enumerate(self.table_names): if t.startswith('_'): continue p = open('python/tests/tables/' + t, 'r') p_content = p.read() sbtab_doc = SBtab.SBtabDocument('test_' + str(i), sbtab_init=p_content, filename=t) if 'Reaction' in sbtab_doc.type_to_sbtab.keys( ) or 'Compound' in sbtab_doc.type_to_sbtab.keys(): conv = sbtab2sbml.SBtabDocument(sbtab_doc) self.convert_document_objects.append(conv) self.sbtab_docs.append(sbtab_doc) p.close() for i, d in enumerate(self.doc_names): if not d.startswith('_'): p = open('python/tests/docs/' + d, 'r') p_content = p.read() sbtab_doc = SBtab.SBtabDocument('test_' + str(i), sbtab_init=p_content, filename=d) if 'Reaction' in sbtab_doc.type_to_sbtab.keys( ) or 'Compound' in sbtab_doc.type_to_sbtab.keys(): conv = sbtab2sbml.SBtabDocument(sbtab_doc) self.convert_document_objects.append(conv) self.sbtab_docs.append(sbtab_doc) p.close()
def converter_objtables2sbtab_wrapper(args): ''' commandline wrapper for the ObjTables to SBtab converter ''' # open ObjTables file try: f = open(args.objtables, 'r').read() except: raise SBtabError('SBtab file %s could not be found.' % args.objtables) try: objtables_doc = SBtab.SBtabDocument('conversion_document', f, args.objtables) except: raise SBtabError('SBtab Document %s could not be created.' % args.objtables) # if definitions file is given create SBtab object if args.definitions_file: try: d = open(args.definitions_file, 'r').read() sbtab_def = SBtab.SBtabTable(d, args.definitions_file) except: raise SBtabError('The definitions file %s could not be found.' % args.definitions_file) else: sbtab_def = None # create converter class try: sbtab_doc = create_sbtab_from_obj_tables_doc(objtables_doc, sbtab_def) if len(args.outfile) > 0: outfile = args.outfile else: outfile = 'sbtab.tsv' p = open(outfile, 'w') p.write(sbtab_doc.doc_row + '\n') for sbtab in sbtab_doc.sbtabs: p.write(sbtab.to_str() + '\n\n') p.close() except: raise SBtabError('SBtab Document %s could not be converted.' % args.sbtab)
def createDataset(header_row, columns, value_rows, filename): ''' Creates an SBtab object by merging strings or list of strings. Takes a header row, main column row, and the value rows as lists of strings and returns an SBtab object. Parameters ---------- header_row : str String of the header row. columns: list List of strings, names of the columns. value_rows : list List of lists containing the different rows of the table. ''' # Initialize variables sbtab_temp = [] sbtab_dataset = tablib.Dataset() header = header_row.split(' ') # Delete spaces in header, main column and data rows header = [x.strip(' ') for x in header] columns = [x.strip(' ') for x in columns] for row in value_rows: try: for entry in row: entry = entry.strip(' ') except: continue # Add header, main column and data rows to temporary list object sbtab_temp.append(header) sbtab_temp.append(columns) for row in value_rows: sbtab_temp.append(row) # Delete all empty entries at the end of the rows for row in sbtab_temp: if len(row) > 1: while not row[-1]: del row[-1] # Make all rows the same length longest = max([len(x) for x in sbtab_temp]) for row in sbtab_temp: if len(row) < longest: for i in range(longest - len(row)): row.append('') sbtab_dataset.append(row) else: sbtab_dataset.append(row) # Create SBtab object from tablib dataset sbtab = SBtab.SBtabTable(sbtab_dataset, filename) return sbtab
def converter_sbtab2sbml_wrapper(args): ''' commandline wrapper for the SBtab to SBML converter ''' # open and create SBtab try: f = open(args.sbtab, 'r').read() except: raise SBtabError('SBtab file %s could not be found.' % args.sbtab) try: sbtab_doc = SBtab.SBtabDocument('conversion_document', f, args.sbtab) except: raise SBtabError('SBtab Document %s could not be created.' % args.sbtab) if len(args.outfile)>0: outfile = args.outfile else: outfile = 'sbml.xml' # create converter class if args.version: if args.version != '31' and args.version != '24': raise SBtabError('SBtab to SBML conversion does currently only support SBML Level and Version 2.4 and 3.1.') try: converter = sbtab2sbml.SBtabDocument(sbtab_doc) (sbml, warnings) = converter.convert_to_sbml(args.version) if len(warnings)>0: print('Warnings:') print(warnings) p = open(outfile,'w') p.write(sbml) p.close() except: raise SBtabError('SBtab Document %s could not be converted to SBML.' % args.sbtab) else: try: converter = sbtab2sbml.SBtabDocument(sbtab_doc) (sbml, warnings) = converter.convert_to_sbml('31') if len(warnings)>0: print('Warnings:') print(warnings) p = open(outfile,'w') p.write(sbml) p.close() except: raise SBtabError('SBtab Document %s could not be converted to SBML.' % args.sbtab)
def openSBtab(filepath): ''' Opens SBtab from file. Parameters ---------- filepath : str Path of the spreadsheet file. ''' if not os.path.isfile(filepath): return None dataset = tablibIO.importSet(filepath) sbtab = SBtab.SBtabTable(dataset, filepath) return sbtab
def setUp(self): ''' setup SBtabTable class with files from test directory ''' self.table_names = [ f for f in os.listdir('python/tests/tables/') if os.path.isfile(os.path.join('python/tests/tables/', f)) ] self.sbtabs = [] for t in self.table_names: if not t.startswith('_'): p = open('python/tests/tables/' + t, 'r') p_content = p.read() sbtab = SBtab.SBtabTable(p_content, t) self.sbtabs.append(sbtab) p.close()
def test_write_sbtab(self): ''' function writes SBtab to hard drive ''' for sbtab in self.sbtabs: self.assertTrue(sbtab.write(sbtab.filename)) get_back = open(sbtab.filename, 'r') get_back_content = get_back.read() get_back_sbtab = SBtab.SBtabTable(get_back_content, sbtab.filename) get_back.close() self.assertEqual(sbtab.header_row.rstrip(), get_back_sbtab.header_row.rstrip()) self.assertEqual(sbtab.columns, get_back_sbtab.columns) self.assertEqual(sbtab.value_rows, get_back_sbtab.value_rows)
def convert_to_sbtab(self): ''' Generates the SBtab files. ''' self.warnings = [] sbtab_doc = SBtab.SBtabDocument(self.filename) for table_type in supported_table_types: try: function_name = 'self.'+table_type.lower()+'_sbtab()' sbtab = eval(function_name) if sbtab != False: sbtab_doc.add_sbtab(sbtab) except: self.warnings.append('Could not generate SBtab %s.' % table_type) return (sbtab_doc, self.warnings)
def fbc_objective(self): ''' builds a SBtab of the TableType FbcObjective ''' fbc_plugin = self.model.getPlugin('fbc') active_obj = fbc_plugin.getActiveObjectiveId() # header row sbtab_fbc = '!!SBtab TableID="fbcobj" Document="%s" TableType='\ '"FbcObjective" TableName="FBC Objective" SBtabVersion="1.0"\n' % self.filename # columns columns = [ '!ID', '!Name', '!SBML:fbc:type', '!SBML:fbc:active', '!SBML:fbc:objective' ] sbtab_fbc += '\t'.join(columns) + '\n' # value rows for obj in fbc_plugin.getListOfObjectives(): value_row = [''] * len(columns) value_row[0] = obj.getId() try: value_row[1] = obj.getName() except: pass try: value_row[2] = obj.getType() except: pass if obj.getId() == active_obj: value_row[3] = 'True' else: value_row[3] = 'False' objective = '' for fo in obj.getListOfFluxObjectives(): objective += '%s * %s +' % (str( fo.getCoefficient()), fo.getReaction()) value_row[4] = objective[:-2] sbtab_fbc += '\t'.join(value_row) + '\n' sbtab_fbc = SBtab.SBtabTable(sbtab_fbc, self.filename + '_fbc_objective.tsv') return sbtab_fbc
def test_xlsx_to_tsv_and_tab_to_xlsx(self): ''' test if xlsx files can be converted to tsv files (ALSO test if SBtab files can be converted to xlsx) ''' for sbtab in self.sbtabs: # first convert SBtab objects to xlsx sbtab_xlsx = misc.tab_to_xlsx(sbtab) self.assertEqual(type(sbtab_xlsx), bytes) # then convert to tsv string sbtab_tsv = misc.xlsx_to_tsv(sbtab_xlsx) self.assertEqual(type(sbtab_tsv), str) self.assertEqual(sbtab_tsv[0:2], '!!') # last: try to convert the string to an SBtab document, # also test if the final sbtab equals the initial one sbtab_doc = SBtab.SBtabDocument(sbtab.filename, sbtab_tsv, sbtab.filename) self.assertEqual(type(sbtab_doc), SBtab.SBtabDocument) self.assertEqual(sbtab.filename, sbtab_doc.filename) self.assertEqual(sbtab.columns, sbtab_doc.sbtabs[0].columns)
def test_write_sbtab_doc(self): ''' function writes SBtabDocument to hard drive ''' for doc in self.docs: self.assertTrue(doc.write()) get_back = open(doc.filename, 'r') get_back_content = get_back.read() get_back_doc = SBtab.SBtabDocument(doc.name, get_back_content, doc.filename) get_back.close() self.assertEqual(doc.doc_row.rstrip(), get_back_doc.doc_row.rstrip()) old_sbtabs = doc.sbtabs new_sbtabs = get_back_doc.sbtabs for i, os in enumerate(old_sbtabs): self.assertEqual(os.header_row, new_sbtabs[i].header_row) self.assertEqual(os.columns, new_sbtabs[i].columns) self.assertEqual(os.value_rows, new_sbtabs[i].value_rows) self.assertEqual(os.table_type, new_sbtabs[i].table_type) self.assertEqual(os.table_name, new_sbtabs[i].table_name)
def __init__(self, table, sbtab_name, def_table, def_name): """ Initialize validator and start check for file and table format. Parameters ---------- table : tablib object Tablib object of the SBtab file sbtab_name : str File path of the SBtab file """ # import definitions from definition table definition_table = tablibIO.importSetNew(def_table, def_name, separator='\t') definition_sbtab = SBtab.SBtabTable(definition_table, def_name) self.definitions = definition_sbtab.sbtab_list # create set of valid table types self.allowed_table_types = list( set([row[2] for row in self.definitions[2:][0]])) # create dict of valid column names per table type self.allowed_columns = {} for table_type in self.allowed_table_types: self.allowed_columns[table_type] = [ row[0] for row in self.definitions[2:][0] if row[2] == table_type ] # initialize warning string self.warnings = [] # define self variables self.table = table self.filename = sbtab_name # check file format and header row self.checkTableFormat() # try creating SBtab instance #try: self.sbtab = SBtab.SBtabTable(self.table, self.filename) self.column2format = {} defs = self.definitions[2] for row in defs: if row[3] == self.sbtab.table_type: self.column2format[row[1]] = row[4] #except: # raise SBtabError('The Parser cannot work with this file!') # remove empty column headers f_columns = [] for element in self.sbtab.columns: if element == '': pass else: f_columns.append(element) self.sbtab.columns = f_columns # determine headers self.determineHeaders() # check SBtab object for validity self.checkTable()
def convert_to_sbtab(self): ''' Generates the SBtab files from the SBML document. Returns: (SBtab.SBtabDocument, list) SBtab document with SBtab tables that contain the content of the SBML model. A list of warnings that may be issued during the conversion process. ''' self.warnings = [] sbtab_doc = SBtab.SBtabDocument(self.filename) doc_row = "!!!SBtab Document='%s' Name='%s' SBtabVersion='1.0'" % ( self.model.getId(), self.model.getId()) sbtab_doc.set_doc_row(doc_row) try: fbc = self.model.getPlugin('fbc') if fbc: self.fbc = True except: pass # the layout test needs to be a bit deeper than fbc, since # a form of the layout plugin also exists in ancient versions # of SBML2.x try: layout = self.model.getPlugin('layout').getLayout(0) layout_id = layout.getId() if layout_id: self.layout = True except: pass for table_type in supported_table_types: try: function_name = 'self.' + table_type.lower() + '_sbtab()' sbtab = eval(function_name) if sbtab != False: sbtab_doc.add_sbtab(sbtab) except: self.warnings.append('Could not generate SBtab %s.' % table_type) if self.fbc: try: sbtab = self.fbc_objective() if sbtab != False: sbtab_doc.add_sbtab(sbtab) except: self.warnings.append( 'Could not generate SBtab FBC Objective Function.') try: sbtab = self.fbc_gene() if sbtab != False: sbtab_doc.add_sbtab(sbtab) except: self.warnings.append('Could not generate SBtab FBC Gene.') if self.layout: try: sbtab = self.layout_sbtab() if sbtab != False: sbtab_doc.add_sbtab(sbtab) except: self.warnings.append('Could not generate SBtab Layout.') return (sbtab_doc, self.warnings)
def reaction_sbtab(self): ''' build a reaction SBtab ''' # header row sbtab_reaction = '!!SBtab TableID="reaction" Document="%s" TableType='\ '"Reaction" TableName="Reaction" SBtabVersion="1.0"\n' % self.filename # columns columns = [ '!ID', '!Name', '!ReactionFormula', '!Location', '!Regulator', '!KineticLaw', '!SBOTerm', '!IsReversible' ] if self.fbc: columns = columns + [ '!SBML:fbc:GeneAssociation', '!SBML:fbc:LowerBound', '!SBML:fbc:UpperBound' ] sbtab_reaction += '\t'.join(columns) + '\n' for reaction in self.model.getListOfReactions(): value_row = [''] * len(columns) value_row[0] = reaction.getId() try: value_row[1] = reaction.getName() except: pass try: value_row[2] = self.make_sum_formula(reaction) except: pass try: value_row[3] = str(reaction.getCompartment()) except: pass try: modifiers = reaction.getListOfModifiers() if len(modifiers) > 1: modifier_list = '' for i, modifier in enumerate(modifiers): if i != len(reaction.getListOfModifiers()) - 1: modifier_list += modifier.getSpecies() + '|' else: modifier_list += modifier.getSpecies() value_row[4] = modifier_list elif len(modifiers) == 1: for modifier in modifiers: value_row[4] = modifier.getSpecies() except: pass try: fm = reaction.getKineticLaw().getFormula() value_row[5] = fm.replace('\n', '') except: pass if str(reaction.getSBOTerm()) != '-1': value_row[6] = 'SBO:%.7d' % reaction.getSBOTerm() try: value_row[7] = str(reaction.getReversible()) except: pass if self.fbc: try: fbc_plugin = reaction.getPlugin('fbc') try: ga = fbc_plugin.getGeneProductAssociation() ass = ga.getAssociation().toInfix() value_row[8] = ass except: pass value_row[9] = str(fbc_plugin.getLowerFluxBound()) value_row[10] = str(fbc_plugin.getUpperFluxBound()) except: self.warnings.append( 'FBC Reaction information could not be read.') sbtab_reaction += '\t'.join(value_row) + '\n' sbtab_reaction = SBtab.SBtabTable(sbtab_reaction, self.filename + '_reaction.tsv') # test and extend for annotations for i, reaction in enumerate(self.model.getListOfReactions()): try: annotations = self.get_annotations(reaction) for j, annotation in enumerate(annotations): col_name = '!Identifiers:' + annotation[1] if col_name not in columns: new_column = [''] * (self.model.getNumReactions() + 1) new_column[0] = col_name new_column[i + 1] = annotation[0] columns.append(col_name) sbtab_reaction.add_column(new_column) else: sbtab_reaction.change_value_by_name( reaction.getId(), col_name, annotation[0]) except: self.warnings.append('Could not add the annotation %s for'\ 'reaction %s' % annotations[1], reaction.getId()) return sbtab_reaction
def quantity_sbtab(self): ''' Builds a Quantity SBtab. ''' # if there are no parameters/quantities in the model, return False parameters = True if len(self.model.getListOfParameters()) == 0: parameters = False for reaction in self.model.getListOfReactions(): kinetic_law = reaction.getKineticLaw() if len(kinetic_law.getListOfParameters()) != 0: parameters = True if not parameters: return False # header row sbtab_quantity = '!!SBtab TableID="quantity" Document="%s" TableType='\ '"Quantity" TableName="Quantity" SBtabVersion="1.0"\n' % self.filename # columns columns = [ '!ID', '!Parameter:SBML:parameter:id', '!Value', '!Unit', '!Type', '!SBOTerm' ] sbtab_quantity += '\t'.join(columns) + '\n' # required for later iteration of parameter annotations local_parameters = [] # value rows for local parameters for reaction in self.model.getListOfReactions(): kinetic_law = reaction.getKineticLaw() if kinetic_law: value_row = [''] * len(columns) for parameter in kinetic_law.getListOfParameters(): value_row[0] = parameter.getId() + '_' + reaction.getId() value_row[1] = parameter.getId() value_row[2] = str(parameter.getValue()) try: value_row[3] = parameter.getUnits() except: pass value_row[4] = 'local parameter' if str(parameter.getSBOTerm()) != '-1': value_row[5] = 'SBO:%.7d' % parameter.getSBOTerm() local_parameters.append(parameter) sbtab_quantity += '\t'.join(value_row) + '\n' sbtab_quantity = SBtab.SBtabTable(sbtab_quantity, self.filename + '_quantity.tsv') # test and extend for annotations of local parameters for i, quantity in enumerate(local_parameters): try: annotations = self.get_annotations(quantity) for j, annotation in enumerate(annotations): col_name = '!Identifiers:' + annotation[1] if col_name not in columns: new_column = [''] * (len(local_parameters) + 1) new_column[0] = col_name new_column[i + 1] = annotation[0] columns.append(col_name) sbtab_quantity.add_column(new_column) else: sbtab_quantity.change_value_by_name( quantity.getId(), col_name, annotation[0]) except: self.warnings.append('Could not add the annotation %s for'\ 'quantity %s' % annotations[1], quantity.getId()) # value_rows for global parameters for parameter in self.model.getListOfParameters(): value_row = [''] * len(sbtab_quantity.columns) value_row[0] = parameter.getId() value_row[1] = parameter.getId() value_row[2] = str(parameter.getValue()) try: value_row[3] += parameter.getUnits() except: pass value_row[4] = 'global parameter' if str(parameter.getSBOTerm()) != '-1': value_row[5] = 'SBO:%.7d' % parameter.getSBOTerm() sbtab_quantity.add_row(value_row) # test and extend for annotations of global parameters for i, parameter in enumerate(self.model.getListOfParameters()): try: annotations = self.get_annotations(parameter) for j, annotation in enumerate(annotations): col_name = '!Identifiers:' + annotation[1] if col_name not in columns: new_column = [''] * (self.model.getNumParameters() + 1) new_column[0] = col_name new_column[i + 1] = annotation[0] columns.append(col_name) sbtab_quantity.add_column(new_column) else: sbtab_quantity.change_value_by_name( parameter.getId(), col_name, annotation[0]) except: self.warnings.append('Could not add the annotation %s for'\ 'quantity %s' % annotations[1], parameter.getId()) if sbtab_quantity.value_rows == []: return False return sbtab_quantity