示例#1
0
    def test_count_tabs(self):
        '''
        test if the amount of SBtabs in one file can be determined
        '''
        for sbtab in self.sbtabs:
            amount = misc.count_tabs(sbtab.to_str())
            self.assertEqual(amount, 1)

        for doc in self.docs:
            amount = misc.count_tabs(doc.to_str())
            self.assertGreaterEqual(amount, 1)
示例#2
0
    def test_count_tabs(self):
        '''
        test if the amount of SBtabs in one file can be determined
        '''
        for sbtab in self.sbtabs:
            amount = misc.count_tabs(sbtab.to_str())
            self.assertEqual(amount, 1)

        for doc in self.docs:
            amount = misc.count_tabs(doc.to_str())
            self.assertGreaterEqual(amount, 1)
示例#3
0
    def add_sbtab_string(self, sbtab_string, filename):
        '''
        add one or multiple SBtab files as string
        '''
        # set filename if not given
        if not filename: filename = 'unnamed_sbtab'

        # see if there are more than one SBtabs in the string
        try: sbtab_amount = misc.count_tabs(sbtab_string)
        except:
            self.warnings.append('The SBtab file could not be read properly.')

        # if there are more than one SBtabs, cut them in single SBtabs
        if sbtab_amount > 1:
            try:
                sbtab_strings = misc.split_sbtabs(sbtab_string)
                for i, sbtab_s in enumerate(sbtab_strings):
                    name_single = filename + '_' + str(i)
                    sbtab_single = SBtab.SBtabTable(sbtab_s, name_single)
                    self.add_sbtab(sbtab_single)
            except:
                self.warnings.append('The SBtab Table object could not be cre'\
                                     'ated properly.')
        else:
            try:
                sbtab = SBtab.SBtabTable(sbtab_string, filename)
                self.add_sbtab(sbtab)
            except:
                self.warnings.append('The SBtab Table object could not be cre'\
                                     'ated properly.')
示例#4
0
    def add_sbtab_string(self, sbtab_string, filename):
        '''
        Adds one or multiple SBtab files as a string.

        Parameters
        ----------
        sbtab_string: str
            One string holding one or more SBtab tables.
        filename: str
            Name of the given SBtab.
        '''
        # set filename if not given
        sbtab_count = len(self.sbtabs)
        filename = 'unnamed_sbtab_%s.tsv'%(str(sbtab_count))

        if not self.filename: self.filename = filename
        if not self.name: self.name = filename

        # see if there are more than one SBtabs in the string
        try: sbtab_amount = misc.count_tabs(sbtab_string)
        except:
            raise SBtabError('The SBtab file could not be read properly.')

        # here, we find a possible doc row
        for row in sbtab_string.split('\n'):
            if row.startswith('!!!'):
                self.doc_row = self._dequote(row)
                break
            elif row.startswith('"!!!'):
                rm1 = row.replace('""', '#')
                rm2 = rm1.replace('"','')
                self.doc_row = self._dequote(rm2.replace('#', '"'))
                break
            else:
                self.doc_row = None
                self.document_format = None
        
        # determine if this is an SBtab or ObjTables Document
        if self.doc_row:
            if '!!!ObjTables' in self.doc_row:
                self.document_format = 'ObjTables'
            elif '!!!SBtab' in self.doc_row:
                self.document_format = 'SBtab'

        # if there are more than one SBtabs, cut them in single SBtabs
        try:
            if sbtab_amount > 1:
                sbtab_strings = misc.split_sbtabs(sbtab_string)
                for i, sbtab_s in enumerate(sbtab_strings):
                    name_single = str(i) + '_' + self.filename
                    sbtab_single = SBtabTable(sbtab_s, name_single)
                    logging.debug('name = %s, type = %s' % (sbtab_single.table_name, sbtab_single.table_type))
                    self.add_sbtab(sbtab_single)
            else:
                sbtab = SBtabTable(sbtab_string, filename)
                self.add_sbtab(sbtab)
        except Exception as e:
            raise SBtabError('The SBtab Table object could not be cre'\
                            'ated properly: ' + str(e))
        return True
示例#5
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.')
示例#6
0
 def test_split_sbtabs(self):
     '''
     test if multiple sbtabs in one input file can be splitted correctly
     '''
     for doc in self.docs:
         amount = misc.count_tabs(doc.to_str())
         sbtabs = misc.split_sbtabs(doc.to_str())
         self.assertEqual(amount, len(sbtabs))
         for sbtab in sbtabs:
             self.assertEqual(sbtab[0:7], '!!SBtab')
示例#7
0
 def test_split_sbtabs(self):
     '''
     test if multiple sbtabs in one input file can be splitted correctly
     '''
     for doc in self.docs:
         amount = misc.count_tabs(doc.to_str())
         sbtabs = misc.split_sbtabs(doc.to_str())
         self.assertEqual(amount, len(sbtabs))
         for sbtab in sbtabs:
             self.assertEqual(sbtab[0:7],'!!SBtab')    
示例#8
0
文件: SBtab.py 项目: derHahn/SBtab
    def add_sbtab_string(self, sbtab_string, filename):
        '''
        Adds one or multiple SBtab files as a string.

        Parameters
        ----------
        sbtab_string: str
            One string holding one or more SBtab tables.
        filename: str
            Name of the given SBtab.
        '''
        # set filename if not given
        if not filename:
            sbtab_count = len(self.sbtabs)
            filename = 'unnamed_sbtab_%s.tsv'%(str(sbtab_count))

        if not self.filename:
            self.filename = filename

        # see if there are more than one SBtabs in the string
        try: sbtab_amount = misc.count_tabs(sbtab_string)
        except:
            raise SBtabError('The SBtab file could not be read properly.')

        # here, we find a possible doc row
        for row in sbtab_string.split('\n'):
            if row.startswith('!!!'):
                self.doc_row = self._dequote(row)
                break
            elif row.startswith('"!!!'):
                rm1 = row.replace('""', '#')
                rm2 = rm1.replace('"','')
                self.doc_row = self._dequote(rm2.replace('#', '"'))
                break
        
        # if there are more than one SBtabs, cut them in single SBtabs
        try:
            if sbtab_amount > 1:
                sbtab_strings = misc.split_sbtabs(sbtab_string)
                for i, sbtab_s in enumerate(sbtab_strings):
                    name_single = str(i) + '_' + self.filename
                    sbtab_single = SBtabTable(sbtab_s, name_single)
                    logging.debug('name = %s, type = %s' % (sbtab_single.table_name, sbtab_single.table_type))
                    self.add_sbtab(sbtab_single)
            else:
                sbtab = SBtabTable(sbtab_string, filename)
                self.add_sbtab(sbtab)
        except Exception as e:
            raise SBtabError('The SBtab Table object could not be cre'\
                            'ated properly: ' + str(e))
        return True
示例#9
0
def sbtab2html_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)

    # count given tabs and prepare file name w/o path
    tab_amount = misc.count_tabs(f)
    if '/' in args.sbtab:
        name_pre = args.sbtab.split('/')[-1:]
    else: name_pre = args.sbtab
    
    if args.multiple:
        try:
            sbtab_doc = SBtab.SBtabDocument('sbtab_doc_prelim', f, args.sbtab)
            for tab in sbtab_doc.sbtabs:
                name = name_pre[0][:4] + '_' + tab.table_id + '.html'
                try:
                    write_html(tab, name)
                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, args.sbtab)
                name = name_pre[0][:4] + '_' + sbtab.name + '.html'    
            else:
                sbtab = SBtab.SBtabTable(f, args.sbtab)
                name = name_pre[0][:4] + '_' + sbtab.table_id + '.html'
            write_html(sbtab, name)
        except:
            raise SBtabError('The HTML file could not be created.')
示例#10
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)
示例#11
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)
示例#12
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)