def converter_sbml2sbtab_wrapper(args): ''' commandline wrapper for the SBML to SBtab converter ''' # open and create SBML try: f = open(args.sbml, 'r').read() except: raise SBtabError('SBML file %s could not be found.' % args.sbml) try: reader = libsbml.SBMLReader() sbml = reader.readSBMLFromString(f) except: raise SBtabError('SBtab Document %s could not be created.' % args.sbml) # create converter class try: converter = sbml2sbtab.SBMLDocument(sbml.getModel(), args.sbml) (sbtab_doc, warnings) = converter.convert_to_sbtab() print(warnings) p = open('sbtab.tsv', 'w') for sbtab in sbtab_doc.sbtabs: p.write(sbtab.to_str() + '\n\n') p.close() except: raise SBtabError('SBML Document %s could not be converted to SBtab.' % args.sbml)
def test_object_creation(self): ''' test if the SBML models can be used for object creation ''' for i, sbml_doc in enumerate(self.sbml_docs): conversion_object = sbml2sbtab.SBMLDocument(sbml_doc.getModel(), self.sbml_names[i]) self.assertIsNotNone(conversion_object)
def converter_sbml2sbtab_wrapper(args): ''' commandline wrapper for the SBML to SBtab converter ''' # open and create SBML try: f = open(args.sbml, 'r').read() except: raise SBtabError('SBML file %s could not be found.' % args.sbml) if len(args.outfile) == 0: args.outfile = "outfile.tsv" try: reader = libsbml.SBMLReader() sbml = reader.readSBMLFromString(f) except: raise SBtabError('SBtab Document %s could not be created.' % args.sbml) # create converter class try: converter = sbml2sbtab.SBMLDocument(sbml.getModel(), args.sbml) (sbtab_doc, objtables_doc, warnings) = converter.convert_to_sbtab() if len(warnings) > 0: print(warnings) outfile = args.outfile if args.objtables: if len(args.outfile) == 0: outfile = 'objtables.tsv' p = open(outfile, 'w') p.write(objtables_doc.doc_row + '\n') for objtable in objtables_doc.sbtabs: p.write(objtable.to_str() + '\n\n') p.close() else: if len(args.outfile) == 0: outfile = 'sbtab.tsv' p = open(args.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('SBML Document %s could not be converted.' % args.sbml)
def test_conversion(self): ''' test if the conversion can be processed ''' for i, sbml_doc in enumerate(self.sbml_docs): # get previous SBML to compare to previous_sbml = self.sbml_docs[i].getModel() sbml_level = previous_sbml.getLevel() # get SBtab document conversion_object = sbml2sbtab.SBMLDocument( sbml_doc.getModel(), self.sbml_names[i]) (sbtab_doc, warnings) = conversion_object.convert_to_sbtab() # check general attributes of sbtab_doc self.assertIsNotNone(sbtab_doc) self.assertNotEqual(len(sbtab_doc.sbtabs), 0) self.assertNotEqual(len(sbtab_doc.name_to_sbtab), 0) self.assertNotEqual(len(sbtab_doc.type_to_sbtab), 0) self.assertNotEqual(len(sbtab_doc.sbtab_filenames), 0) self.assertEqual(len(sbtab_doc.sbtabs), len(sbtab_doc.name_to_sbtab)) self.assertEqual(len(sbtab_doc.name_to_sbtab), len(sbtab_doc.type_to_sbtab)) self.assertEqual(len(sbtab_doc.type_to_sbtab), len(sbtab_doc.sbtab_filenames)) self.assertIsNotNone(sbtab_doc.doc_row) self.assertEqual(sbtab_doc.doc_row[:8], '!!!SBtab') # check single SBtab files if previous_sbml.getNumCompartments() > 0: self.assertIn('Compartment', sbtab_doc.type_to_sbtab) compartment_sbtabs = sbtab_doc.type_to_sbtab['Compartment'] for c_sbtab in compartment_sbtabs: # header row self.assertEqual(c_sbtab.header_row[:7], '!!SBtab') self.assertIn('TableType', c_sbtab.header_row) # columns self.assertEqual(len(c_sbtab.value_rows), previous_sbml.getNumCompartments()) self.assertIn('!ID', c_sbtab.columns) self.assertIn('!ID', c_sbtab.columns_dict) self.assertIn('!Size', c_sbtab.columns) self.assertIn('!Size', c_sbtab.columns_dict) #if sbml_level == 3: # self.assertIn('!Constant', c_sbtab.columns) # self.assertIn('!Constant', c_sbtab.columns_dict) # rows for compartment in c_sbtab.value_rows: self.assertEqual(len(compartment), len(c_sbtab.columns)) self.assertNotEqual( compartment[c_sbtab.columns_dict['!ID']], '') self.assertNotEqual( compartment[c_sbtab.columns_dict['!Name']], '') self.assertNotEqual( compartment[c_sbtab.columns_dict['!Size']], '') #if sbml_level == 3: # self.assertNotEqual(compartment[c_sbtab.columns_dict['!Constant']], '') if previous_sbml.getNumSpecies() > 0: self.assertIn('Compound', sbtab_doc.type_to_sbtab) compound_sbtabs = sbtab_doc.type_to_sbtab['Compound'] for c_sbtab in compound_sbtabs: # header row self.assertEqual(c_sbtab.header_row[:7], '!!SBtab') self.assertIn('TableType', c_sbtab.header_row) # columns self.assertEqual(len(c_sbtab.value_rows), previous_sbml.getNumSpecies()) self.assertIn('!ID', c_sbtab.columns) self.assertIn('!ID', c_sbtab.columns_dict) self.assertIn('!Location', c_sbtab.columns) self.assertIn('!Location', c_sbtab.columns_dict) #if sbml_level == 3: # self.assertIn('!Constant', c_sbtab.columns) # self.assertIn('!Constant', c_sbtab.columns_dict) # self.assertIn('!hasOnlySubstanceUnit', c_sbtab.columns) # self.assertIn('!hasOnlySubstanceUnit', c_sbtab.columns_dict) # self.assertIn('!boundaryCondition', c_sbtab.columns) # self.assertIn('!boundaryCondition', c_sbtab.columns_dict) # self.assertIn('!InitialValue', c_sbtab.columns) # self.assertIn('!InitialValue', c_sbtab.columns_dict) # rows for compound in c_sbtab.value_rows: self.assertEqual(len(compound), len(c_sbtab.columns)) self.assertNotEqual( compound[c_sbtab.columns_dict['!ID']], '') self.assertNotEqual( compound[c_sbtab.columns_dict['!Location']], '') #if sbml_level == 3: # self.assertNotEqual(compound[c_sbtab.columns_dict['!Constant']], '') # self.assertNotEqual(compound[c_sbtab.columns_dict['!hasOnlySubstanceUnit']], '') # self.assertNotEqual(compound[c_sbtab.columns_dict['!boundaryCondition']], '') # self.assertNotEqual(compound[c_sbtab.columns_dict['!InitialValue']], '') if previous_sbml.getNumReactions() > 0: self.assertIn('Reaction', sbtab_doc.type_to_sbtab) reaction_sbtabs = sbtab_doc.type_to_sbtab['Reaction'] for r_sbtab in reaction_sbtabs: # header row self.assertEqual(r_sbtab.header_row[:7], '!!SBtab') self.assertIn('TableType', r_sbtab.header_row) # columns self.assertEqual(len(r_sbtab.value_rows), previous_sbml.getNumReactions()) self.assertIn('!ID', r_sbtab.columns) self.assertIn('!ID', r_sbtab.columns_dict) self.assertIn('!ReactionFormula', r_sbtab.columns) self.assertIn('!ReactionFormula', r_sbtab.columns_dict) #if sbml_level == 3: # self.assertIn('!IsReversible', r_sbtab.columns) # self.assertIn('!IsReversible', r_sbtab.columns_dict) # self.assertIn('!Constant', r_sbtab.columns) # self.assertIn('!Constant', r_sbtab.columns_dict) # rows for reaction in r_sbtab.value_rows: self.assertEqual(len(reaction), len(r_sbtab.columns)) self.assertNotEqual( reaction[r_sbtab.columns_dict['!ID']], '') self.assertNotEqual( reaction[r_sbtab.columns_dict['!ReactionFormula']], '') #if sbml_level == 3: # self.assertNotEqual(reaction[r_sbtab.columns_dict['!Constant']], '') # self.assertNotEqual(reaction[r_sbtab.columns_dict['!IsReversible']], '') if previous_sbml.getNumParameters() > 0: self.assertIn('Quantity', sbtab_doc.type_to_sbtab) quantity_sbtabs = sbtab_doc.type_to_sbtab['Quantity'] for q_sbtab in quantity_sbtabs: # header row self.assertEqual(q_sbtab.header_row[:7], '!!SBtab') self.assertIn('TableType', q_sbtab.header_row) # columns # we have a bug here for the export of local parameters; # uncomment when fixed #print(q_sbtab.value_rows) #self.assertEqual(len(q_sbtab.value_rows), previous_sbml.getNumParameters()) self.assertIn('!ID', q_sbtab.columns) self.assertIn('!ID', q_sbtab.columns_dict) self.assertIn('!Value', q_sbtab.columns) self.assertIn('!Value', q_sbtab.columns_dict) self.assertIn('!Unit', q_sbtab.columns) self.assertIn('!Unit', q_sbtab.columns_dict) #if sbml_level == 3: # self.assertIn('!Constant', q_sbtab.columns) # self.assertIn('!Constant', q_sbtab.columns_dict) # rows for quantity in q_sbtab.value_rows: self.assertEqual(len(quantity), len(q_sbtab.columns)) self.assertNotEqual( quantity[q_sbtab.columns_dict['!ID']], '') self.assertNotEqual( quantity[q_sbtab.columns_dict['!Value']], '')
def converter(): response.title = T('SBtab - Standardised data tables for Systems Biology') response.subtitle = T('SBML / SBtab Conversion') session.sbmlid2label = {'24': '_SBML_L2V4', '31': '_SBML_L3V1'} # initialise required variables and files if not 'warnings_con' in session: session.warnings_con = [] if not session.definition_file: try: sbtab_def = misc.open_definitions_file( os.path.dirname(os.path.abspath(__file__)) + '/../static/files/default_files/definitions.tsv') session.definition_file = sbtab_def session.definition_file_name = sbtab_def.filename except: session.warnings_con.append( 'There was an error reading the definition file.') redirect(URL('')) if 'sbtabs' not in session: session.sbtabs = [] session.sbtab_filenames = [] session.sbtab_docnames = [] session.name2doc = {} session.types = [] # ######################################################################### # form for SBtab files lform = SQLFORM.factory( Field('File', 'upload', uploadfolder="/tmp", label='Upload SBtab file to convert (.csv, .tsv, .xlsx)', requires=IS_LENGTH(10485760, 1, error_message='Max upload size: 10MB'))) if lform.process(formname='form_one').accepted: session.warnings_con = [] response.flash = 'form accepted' # validate file encoding and name try: sbtab_file = request.vars.File.value.decode('utf-8', 'ignore') except: session.warnings_con.append( 'The file does not adhere to spreadsheet standards.') redirect(URL('')) filename = request.vars.File.filename if not filename.endswith('.tsv') and not filename.endswith( '.csv') and not filename.endswith('.xlsx'): session.warnings_con.append( 'The file does not have a correct file format. Please use csv/tsv/xlsx only.' ) redirect(URL('')) # convert from xlsx to csv if required if filename.endswith('.xlsx'): sbtab_file = request.vars.File.value try: sbtab_file = misc.xlsx_to_tsv(sbtab_file) except: session.warnings_con.append( 'The xlsx file could not be converted to SBtab. Please ensure file format validity.' ) redirect(URL('')) # check if there are more than one SBtab files in the file and create SBtabTable or SBtabDocument try: sbtab_amount = misc.count_tabs(sbtab_file) except: session.warnings_con.append( 'The SBtab %s could not be read properly.' % sbtab.filename) redirect(URL('')) if sbtab_amount > 1: try: sbtab_strings = misc.split_sbtabs(sbtab_file) sbtab_doc = SBtab.SBtabDocument(filename) session.sbtab_docnames.append(filename) for i, sbtab_string in enumerate(sbtab_strings): name_single = filename[:-4] + str(i) + filename[-4:] sbtab = SBtab.SBtabTable(sbtab_string, name_single) new_name = filename[:-4] + '_' + sbtab.table_id + filename[ -4:] sbtab.set_filename(new_name) if new_name not in session.sbtab_filenames: sbtab_doc.add_sbtab(sbtab) session.sbtabs.append(sbtab) session.types.append(sbtab.table_type) session.sbtab_filenames.append(new_name) session.name2doc[new_name] = filename else: session.warnings_con.append( 'The SBtab %s is duplicate.' % sbtab.filename) redirect(URL('')) except: session.warnings_con.append( 'The SBtab Document object could not be created properly.') redirect(URL('')) else: try: sbtab = SBtab.SBtabTable(sbtab_file, filename) session.sbtabs.append(sbtab) session.sbtab_filenames.append(sbtab.filename) session.types.append(sbtab.table_type) session.sbtab_docnames.append(sbtab.filename) session.name2doc[sbtab.filename] = sbtab.filename except: session.warnings_con.append( 'The SBtab Table object could not be created properly.') redirect(URL('')) redirect(URL('')) elif lform.errors: response.flash = 'form has errors' # ######################################################################### # form for SBML files rform = SQLFORM.factory( Field('File', 'upload', uploadfolder="/tmp", label='Upload SBML file to convert (.xml)', requires=IS_LENGTH(52428800, 1, error_message='Max upload size: 50MB'))) if rform.process(formname='form_two').accepted: response.flash = 'form accepted' session.warnings_con = [] filename = request.vars.File.filename sbml_file = request.vars.File.value.decode('utf-8', 'ignore') if filename[-3:] != 'xml' and filename[-4:] != 'sbml': session.warnings_con.append( 'The uploaded file has a wrong extension for an SBML file.') redirect(URL('')) if 'sbmls' not in session: session.sbmls = [sbml_file] session.sbml_filenames = [filename] else: if filename not in session.sbml_filenames: session.sbmls.append(sbml_file) session.sbml_filenames.append(filename) else: session.warnings_con.append( 'An SBML file with the name %s is already stored.' % filename) redirect(URL('')) redirect(URL('')) elif rform.errors: response.flash = 'form has errors' # ######################################################################### # buttons # convert (single) sbtab to sbml if request.vars.c2sbml_button24 or request.vars.c2sbml_button31: # determine requested SBML version if request.vars.c2sbml_button24 != None: sbml_version = '24' c2sbml_button = request.vars.c2sbml_button24 else: sbml_version = '31' c2sbml_button = request.vars.c2sbml_button31 session.warnings_con = [] # get SBtab and add to SBtab document try: sbtab = session.sbtabs[int(c2sbml_button)] sbtab_doc = SBtab.SBtabDocument(sbtab.filename, sbtab) except: session.warnings_con = [ 'The SBtab %s could not be added to the document.' % sbtab.filename ] redirect(URL('')) # convert SBtab document to SBML and add details to session try: ConvSBtabClass = sbtab2sbml.SBtabDocument(sbtab_doc) (sbml, session.warnings_con ) = ConvSBtabClass.convert_to_sbml(sbml_version) filename_new = sbtab.filename[:-4] + '.xml' # if the sbml build up crashed: if not sbml: session.warnings_con.append('The SBtab file %s could not be c'\ 'onverted to SBML. Please check file'\ 'validity.' % sbtab.filename) redirect(URL('')) if 'sbmls' not in session: session.sbmls = [sbml] session.sbml_filenames = [filename_new] else: if not filename_new in session.sbml_filenames: session.sbmls.append(sbml) session.sbml_filenames.append(filename_new) else: session.warnings_con.append('A file with the name %s has alre'\ 'ady been uploaded. Please rename'\ ' your SBtab file/s before SBML c'\ 'reation.' % filename_new) except: session.warnings_con.append('The conversion of SBtab %s to SBML was n'\ 'ot successful.' % sbtab.filename) redirect(URL('')) # convert multiple sbtabs to sbml if request.vars.convert_all_button24 or request.vars.convert_all_button31: if request.vars.convert_all_button24 != None: sbml_version = '24' convert_all_button = request.vars.convert_all_button24 else: sbml_version = '31' convert_all_button = request.vars.convert_all_button31 session.warnings_con = [] # get SBtabs and add them to an SBtab document convert_document = session.sbtab_docnames[int(convert_all_button)] sbtabs = [] try: for i, filename in enumerate(session.sbtab_filenames): if session.name2doc[filename] == convert_document: sbtabs.append(session.sbtabs[i]) sbtab_doc = SBtab.SBtabDocument(convert_document) for sbtab in sbtabs: sbtab_doc.add_sbtab(sbtab) except: session.warnings_con = ['The SBtabs could not be added to SBML.'] redirect(URL('')) # convert SBtab document to SBML and add details to session try: ConvSBtabClass = sbtab2sbml.SBtabDocument(sbtab_doc) (sbml, session.warnings_con ) = ConvSBtabClass.convert_to_sbml(sbml_version) filename_new = sbtab_doc.name[:-4] + '.xml' # if the sbml build up crashed: if not sbml: session.warnings_con.append('The SBtab file %s could not be c'\ 'onverted to SBML. Please check file'\ 'validity.' % sbtab_doc.name) redirect(URL('')) if 'sbmls' not in session: session.sbmls = [sbml] session.sbml_filenames = [filename_new] else: if not filename_new in session.sbml_filenames: session.sbmls.append(sbml) session.sbml_filenames.append(filename_new) else: session.warnings_con.append('A file with the name %s has alre'\ 'ady been uploaded. Please rename'\ ' your SBtab file/s before SBML c'\ 'reation.' % filename_new) redirect(URL('')) except: session.warnings_con.append('The conversion of SBtab %s to SBML was n'\ 'ot successful.' % sbtab_doc.filename) redirect(URL('')) # download sbtab if request.vars.dl_sbtab_button: downloader_sbtab() # download sbtab.xlsx if request.vars.dl_xlsx_sbtab_button: downloader_sbtab_xlsx() # download all sbtabs if request.vars.download_all_button: download_document = session.sbtab_docnames[int( request.vars.download_all_button)] sbtab_list = [] for i, filename in enumerate(session.sbtab_filenames): if session.name2doc[filename] == download_document: sbtab_list.append(session.sbtabs[i]) downloader_sbtab_doc(sbtab_list, int(request.vars.download_all_button)) # remove all sbtabs if request.vars.remove_all_button: try: remove_document = session.sbtab_docnames[int( request.vars.remove_all_button)] # gather indices of entries to remove remove_sbtabs = [] remove_filenames = [] for i, s in enumerate(session.sbtabs): if session.name2doc[s.filename] == remove_document: remove_sbtabs.append(i) # delete sbtabs, names, and types remove = sorted(remove_sbtabs, reverse=True) for i in remove: remove_filenames.append(session.sbtab_filenames[i]) del session.sbtabs[i] del session.sbtab_filenames[i] del session.types[i] # delete document name del session.sbtab_docnames[int(request.vars.remove_all_button)] # delete name2doc entries for entry in remove_filenames: del session.name2doc[entry] session.warnings_con = [] redirect(URL('')) except: redirect(URL('')) # erase single SBML if request.vars.erase_sbml_button: del session.sbmls[int(request.vars.erase_sbml_button)] del session.sbml_filenames[int(request.vars.erase_sbml_button)] session.warnings_con = [] redirect(URL('')) # convert sbml to sbtab if request.vars.c2sbtab_button: session.warnings_con = [] try: # initialise variables and parser reader = libsbml.SBMLReader() sbml_model = reader.readSBMLFromString(session.sbmls[int( request.vars.c2sbtab_button)]) filename = session.sbml_filenames[int(request.vars.c2sbtab_button)] # convert SBML to SBtab Document ConvSBMLClass = sbml2sbtab.SBMLDocument(sbml_model.getModel(), filename) (sbtab_doc, objtables_doc, session.warnings_con) = ConvSBMLClass.convert_to_sbtab() if sbtab_doc.sbtabs == []: session.warnings_con = [ 'The SBML file seems to be invalid and could not be converted to SBtab.' ] redirect(URL('')) # append generated SBtabs to session variables for sbtab in sbtab_doc.sbtabs: if 'sbtabs' not in session: session.sbtabs = [sbtab] session.sbtab_filenames = [sbtab.filename] if sbtab_doc.name not in session.sbtab_docnames: session.sbtab_docnames.append(sbtab_doc.name) session.types = [sbtab.table_type] session.name2doc = {} session.name2doc[sbtab.filename] = sbtab_doc.name else: if sbtab.filename not in session.sbtab_filenames: session.sbtabs.append(sbtab) session.sbtab_filenames.append(sbtab.filename) session.name2doc[sbtab.filename] = sbtab_doc.name if sbtab_doc.name not in session.sbtab_docnames: session.sbtab_docnames.append(sbtab_doc.name) session.types.append(sbtab.table_type) except: session.warnings_con = [ 'The SBML file seems to be invalid and could not be converted to SBtab.' ] redirect(URL('')) # erase single SBtab if request.vars.erase_sbtab_button: del session.sbtabs[int(request.vars.erase_sbtab_button)] # this IF is important: you must only delete the document from the list # IF there are no more files attached to it doc_name = session.name2doc[session.sbtab_filenames[int( request.vars.erase_sbtab_button)]] if list(session.name2doc.values()).count(doc_name) == 1: session.sbtab_docnames.remove(doc_name) del session.name2doc[session.sbtab_filenames[int( request.vars.erase_sbtab_button)]] del session.sbtab_filenames[int(request.vars.erase_sbtab_button)] del session.types[int(request.vars.erase_sbtab_button)] session.warnings_con = [] redirect(URL('')) if request.vars.dl_sbml_button: downloader_sbml() return dict(UPL_FORML=lform, UPL_FORMR=rform, SBTAB_LIST=session.sbtabs, SBML_LIST=session.sbmls, NAME_LIST_SBTAB=session.sbtab_filenames, NAME_LIST_SBML=session.sbml_filenames, DOC_NAMES=session.sbtab_docnames, NAME2DOC=session.name2doc, WARNINGS_CON=session.warnings_con, TYPES=session.types)