Exemple #1
0
    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()
Exemple #2
0
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.')
Exemple #3
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.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()
Exemple #4
0
 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, [])
Exemple #5
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()
Exemple #6
0
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)
Exemple #7
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.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 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)
Exemple #9
0
    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 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)
Exemple #11
0
    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)
Exemple #12
0
    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)
Exemple #13
0
    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)
Exemple #14
0
def validator():
    """
    example action using the internationalization operator T and flash
    rendered by views/default/index.html or views/generic.html

    if you need a simple wiki simple replace the two lines below with:
    return auth.wiki()
    """
    response.title = T('SBtab - Standardised data tables for Systems Biology')
    response.subtitle = T('Online Validator')

    lform = SQLFORM.factory(
        Field('File',
              'upload',
              uploadfolder="/tmp",
              label='Upload SBtab file (.csv, .tsv, .xlsx)',
              requires=IS_LENGTH(10485760,
                                 1,
                                 error_message='Max upload size: 10MB')))
    filename = None
    output = []

    # load the definition file which is required for validation
    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_val.append(
                'There was an error reading the definition file.')

    #update session lists
    if lform.process().accepted:
        response.flash = 'form accepted'

        # initialise session variables
        session.warnings_val = []
        if 'sbtabs' not in session:
            session.sbtabs = []
            session.sbtab_filenames = []
            session.sbtab_docnames = []
            session.types = []
            session.name2doc = {}

        # validate file name
        try:
            sbtab_file = request.vars.File.value.decode('utf-8', 'ignore')
        except:
            session.warnings_val.append(
                'The file has a faulty encryption. Please use UTF-8 instead.')
            redirect(URL(''))

        filename = request.vars.File.filename
        if not filename.endswith('.tsv') and not filename.endswith(
                '.csv') and not filename.endswith('.xlsx'):
            session.warnings_val.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_val.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_val.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)
                if sbtab_doc.document_format == 'ObjTables':
                    session.warnings_val.append(
                        'This SBtab validator cannot be used for ObjTables files.'
                    )
                    redirect(URL(''))
                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_val.append(
                            'The SBtab %s is duplicate. The document cannot be validated. All TableIDs in a document need to be unique.'
                            % sbtab.filename)
                        redirect(URL(''))
            except:
                session.warnings_val.append(
                    'The SBtab Document object could not be created properly.')
                redirect(URL(''))
        else:
            try:
                sbtab = SBtab.SBtabTable(sbtab_file, filename)
                if sbtab.table_format == 'ObjTables':
                    session.warnings_val.append(
                        'This SBtab validator cannot be used for ObjTables files.'
                    )
                    redirect(URL(''))
                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_val.append(
                    'The SBtab Table object could not be created properly.')
                redirect(URL(''))
    elif lform.errors:
        response.flash = 'form has errors'

    # buttons
    # validate
    if request.vars.validate_button:
        try:
            filename = session.sbtab_filenames[int(
                request.vars.validate_button)]
            TableValidClass = validatorSBtab.ValidateTable(
                session.sbtabs[int(request.vars.validate_button)],
                session.definition_file)
            output = TableValidClass.return_output()
        except:
            session.warnings_val.append(
                'The file could not be validated. It seems to be broken.')
            redirect(URL(''))

    # erase
    if request.vars.erase_button:
        flname = session.sbtab_filenames[int(request.vars.erase_button)]
        del session.sbtabs[int(request.vars.erase_button)]
        del session.sbtab_filenames[int(request.vars.erase_button)]
        del session.types[int(request.vars.erase_button)]
        doc_name = session.name2doc[flname]
        # this IF is important: you must only delete the document from the list
        # IF there are no more files attached to it
        if list(session.name2doc.values()).count(doc_name) == 1:
            session.sbtab_docnames.remove(doc_name)
        del session.name2doc[flname]
        session.warnings_val = []
        redirect(URL(''))

    # erase all
    if request.vars.remove_all_button_val:
        try:
            remove_document = session.sbtab_docnames[int(
                request.vars.remove_all_button_val)]

            # 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 rs in remove:
                remove_filenames.append(session.sbtab_filenames[rs])
                del session.sbtabs[rs]
                del session.sbtab_filenames[rs]
                del session.types[rs]

            # delete document name
            del session.sbtab_docnames[int(request.vars.remove_all_button_val)]

            # delete name2doc entries
            for entry in remove_filenames:
                del session.name2doc[entry]

            session.warnings_val = []
            redirect(URL(''))
        except:
            redirect(URL(''))

    return dict(UPL_FORM=lform,
                DEF_FILE_NAME=session.definition_file_name,
                SBTAB_LIST=session.sbtabs,
                NAME_LIST=session.sbtab_filenames,
                SBTAB_VAL=filename,
                DOC_NAMES=session.sbtab_docnames,
                NAME2DOC=session.name2doc,
                OUTPUT=output,
                WARNINGS=session.warnings_val)
Exemple #15
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)
Exemple #16
0
def validator():
    """
    example action using the internationalization operator T and flash
    rendered by views/default/index.html or views/generic.html

    if you need a simple wiki simple replace the two lines below with:
    return auth.wiki()
    """
    response.title = T('SBtab - Standardised data tables for Systems Biology')
    response.subtitle = T('Online Validator')

    lform = SQLFORM.factory(
        Field('File',
              'upload',
              uploadfolder="/tmp",
              label='Upload SBtab file (.csv, .tsv, .xls)',
              requires=IS_LENGTH(10485760,
                                 1,
                                 error_message='Max upload size: 10MB')))
    sbtab_val = None
    output = []

    #update session lists
    if lform.process().accepted:
        response.flash = 'form accepted'

        # initialise session variables
        session.warnings_val = []
        if 'sbtabs' not in session:
            session.sbtabs = []
            session.sbtab_filenames = []
            session.sbtab_docnames = []
            session.types = []
            session.name2doc = {}

        # load the definition file which is required for validation
        if not session.definition_file:
            try:
                def_file_open = open(
                    './applications/sbtab_web/static/files/default_files/definitions.tsv'
                )
                def_file = def_file_open.read()
                definition_name = 'definitions.tsv'
                sbtab_def = SBtab.SBtabTable(def_file, definition_name)
                session.definition_file = sbtab_def
                session.definition_file_name = sbtab_def.filename
            except:
                session.warnings_val.append(
                    'There was an error reading the definition file.')

        # validate file name
        try:
            sbtab_file = request.vars.File.value.decode('utf-8', 'ignore')
        except:
            session.warnings_val.append(
                'The file has a faulty encryption. Please use UTF-8 instead.')
            redirect(URL(''))

        filename = request.vars.File.filename
        if not filename.endswith('.tsv') and not filename.endswith(
                '.csv') and not filename.endswith('.xls'):
            session.warnings_val.append(
                'The file does not have a correct file format. Please use csv/tsv/xls only.'
            )
            redirect(URL(''))

        # convert from xls to csv if required
        if filename.endswith('.xls'):
            try:
                sbtab_file = misc.xls2csv(sbtab_file, filename)
            except:
                session.warnings_val.append(
                    'The xls 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_val.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:]
                    if name_single not in session.sbtab_filenames:
                        sbtab = SBtab.SBtabTable(sbtab_string, name_single)
                        sbtab_doc.add_sbtab(sbtab)
                        session.sbtabs.append(sbtab)
                        session.types.append(sbtab.table_type)
                        session.sbtab_filenames.append(name_single)
                        session.name2doc[name_single] = filename
                    else:
                        session.warnings_val.append(
                            'The SBtab %s is duplicate.' % sbtab.filename)
                        redirect(URL(''))
            except:
                session.warnings_val.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_val.append(
                    'The SBtab Table object could not be created properly.')
                redirect(URL(''))
    elif lform.errors:
        response.flash = 'form has errors'

    # buttons
    # validate
    if request.vars.validate_button:
        sbtab_val = session.sbtab_filenames[int(request.vars.validate_button)]
        TableValidClass = validatorSBtab.ValidateTable(
            session.sbtabs[int(request.vars.validate_button)],
            session.definition_file)
        output = TableValidClass.return_output()

    # erase
    if request.vars.erase_button:
        flname = session.sbtab_filenames[int(request.vars.erase_button)]
        del session.sbtabs[int(request.vars.erase_button)]
        del session.sbtab_filenames[int(request.vars.erase_button)]
        del session.types[int(request.vars.erase_button)]
        del session.sbtab_docnames[int(request.vars.erase_button)]
        del session.name2doc[flname]
        session.warnings_val = []
        redirect(URL(''))

    # erase all
    if request.vars.remove_all_button_val:
        try:
            remove_document = session.sbtab_docnames[int(
                request.vars.remove_all_button_val)]
            remove_sbtabs = []
            for i, docname in enumerate(session.sbtab_docnames):
                if docname == remove_document:
                    remove_sbtabs.append(i)
            remove = sorted(remove_sbtabs, reverse=True)
            for i in remove:
                del session.sbtabs[i]
                del session.name2doc[session.sbtab_filenames[i]]
                del session.sbtab_filenames[i]
                del session.types[i]
                del session.sbtab_docnames[i]
                session.warnings_val = []
        except:
            session.warnings_val.append(
                'The document could not be removed. Please reload session.')
            redirect(URL(''))

    return dict(UPL_FORM=lform,
                DEF_FILE_NAME=session.definition_file_name,
                SBTAB_LIST=session.sbtabs,
                NAME_LIST=session.sbtab_filenames,
                SBTAB_VAL=sbtab_val,
                DOC_NAMES=session.sbtab_docnames,
                NAME2DOC=session.name2doc,
                OUTPUT=output,
                WARNINGS=session.warnings_val)