示例#1
0
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)
示例#2
0
    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()
示例#3
0
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)