Пример #1
0
def checkTabs(document, filename, separator=None):
    '''
    this function checks, how many SBtab files are given by the user and saves it/them
    in a list, moreover stores the SBtab types in a dict linking to the SBtabs
    '''
    sbtabs = []
    types = []
    docs = []
    tnames = []
    type2sbtab = {}

    #if there are more than one SBtabs given in single files that might be comprised of several SBtabs:
    if len(document) > 1:
        for single_document in document:
            #check for several SBtabs in one document
            document_rows = single_document.split('\n')
            tabs_in_document = getAmountOfTables(document_rows)
            if tabs_in_document > 1:
                sbtabs = splitDocumentInTables(document_rows)
            else:
                sbtabs = [document_rows]
            #generate SBtab class instance for every SBtab
            for sbtab in sbtabs:
                new_tablib_obj = tablibIO.importSetNew(sbtab, filename,
                                                       separator)
                single_tab = SBtab.SBtabTable(new_tablib_obj, filename)
                type2sbtab[single_tab.table_type] = single_tab
                types.append(single_tab.table_type)
                docs.append(single_tab.table_document)
                try:
                    tnames.append(single_tab.table_name)
                except:
                    tnames.append('')
    #elif there is only one document given, possibly consisting of several SBtabs
    else:
        #check for several SBtabs in one document
        document_rows = document[0].split('\n')
        tabs_in_document = getAmountOfTables(document_rows)
        if tabs_in_document > 1: sbtabs = splitDocumentInTables(document_rows)
        else: sbtabs = [document_rows]
        #generate SBtab class instance for every SBtab
        for sbtab in sbtabs:
            as_sbtab = '\n'.join(sbtab)
            new_tablib_obj = tablibIO.importSetNew(as_sbtab, filename,
                                                   separator)
            single_tab = SBtab.SBtabTable(new_tablib_obj, filename)
            type2sbtab[single_tab.table_type] = single_tab
            types.append(single_tab.table_type)
            docs.append(single_tab.table_document)
            try:
                tnames.append(single_tab.table_name)
            except:
                tnames.append('')

    return sbtabs, types, docs, tnames
Пример #2
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)
Пример #3
0
def open_definitions_file(_path=None):
    '''
    Opens the SBtab definitions file, which can be in several locations.

    Parameters
    ----------
    _path: str
        Optional path to the definitions.tsv.

    Returns: SBtab.SBtabTable
        SBtab definitions file as SBtabTable object.        
    '''
    sbtab_def = False

    if _path: try_paths = [_path]
    else:
        try_paths = [
            'definitions.tsv',
            os.path.join(os.path.dirname(__file__),
                         '../static/files/default_files/definitions.tsv'),
            os.path.join(os.path.dirname(__file__),
                         '../definition_table/definitions.tsv'),
            os.path.join(os.path.dirname(__file__), 'definitions.tsv')
        ]

    for path in try_paths:
        try:
            def_file = open(path, 'r')
            sbtab_def = SBtab.SBtabTable(def_file.read(), 'definitions.tsv')
            def_file.close()
            break
        except:
            pass

    return sbtab_def
Пример #4
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()
Пример #5
0
    def compartment_sbtab(self):
        '''
        build a compartment SBtab
        '''
        # header row
        sbtab_compartment  = '!!SBtab TableID="compartment" Document="%s" TableT'\
                             'ype="Compartment" TableName="Compartment" SBtabVersion="1.0"'\
                             '\n' % self.filename
        # columns
        columns = ['!ID', '!Name', '!Size', '!Unit', '!SBOTerm']

        sbtab_compartment += '\t'.join(columns) + '\n'

        # value rows
        for compartment in self.model.getListOfCompartments():
            value_row = [''] * len(columns)
            value_row[0] = compartment.getId()
            if compartment.getName() != '':
                value_row[1] = compartment.getName()
            else:
                value_row[1] = compartment.getId()
            try:
                value_row[2] = str(compartment.getSize())
            except:
                pass
            try:
                value_row[3] = str(species.getUnits())
            except:
                pass
            if str(compartment.getSBOTerm()) != '-1':
                value_row[4] = 'SBO:%.7d' % compartment.getSBOTerm()

            sbtab_compartment += '\t'.join(value_row) + '\n'

        # build SBtab Table object from basic information
        sbtab_compartment = SBtab.SBtabTable(
            sbtab_compartment, self.filename + '_compartment.tsv')

        # test and extend for annotations
        for i, compartment in enumerate(self.model.getListOfCompartments()):
            try:
                annotations = self.get_annotations(compartment)
                for j, annotation in enumerate(annotations):
                    col_name = '!Identifiers:' + annotation[1]
                    if col_name not in columns:
                        new_column = [''
                                      ] * (self.model.getNumCompartments() + 1)
                        new_column[0] = col_name
                        new_column[i + 1] = annotation[0]
                        columns.append(col_name)
                        sbtab_compartment.add_column(new_column)
                    else:
                        sbtab_compartment.change_value_by_name(
                            compartment.getId(), col_name, annotation[0])
            except:
                self.warnings.append('Could not add the annotation %s for'\
                                     'compartment %s' % annotations[1],
                                     compartment.getId())

        return sbtab_compartment
Пример #6
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()
Пример #7
0
 def read_definition(self, def_table):
     '''
     read the required definition file; either it is provided by the user
     or the default definition file is read in; otherwise program exit
     '''
     # read in provided definition table or open default
     if def_table:
         try: self.definitions = def_table.sbtab_list
         except:
             print('''Definition file could not be loaded, so the validation
             could not be started. Please provide definition file
             as argument''')
             sys.exit() 
     else:
         try:
             d = os.path.dirname(os.path.abspath(__file__)) + '/files/default_'\
                 'files/definitions.tsv'
             def_file = open(d, 'r')
             def_table = def_file.read()
             sbtab_def = SBtab.SBtabTable(def_table, d)
             self.definitions = sbtab_def.sbtab_list
         except:
             print('''Definition file could not be loaded, so the validation
             could not be started. Please provide definition file
             as argument''')
             sys.exit()
Пример #8
0
    def makeSBtabs(self):
        '''
        Generates the SBtab files.
        '''
        self.warnings = []
        sbtabs = []

        #self.testForInconvertibles()

        for sbtab_type in allowed_sbtabs:
            try:
                function_name = 'self.' + sbtab_type.lower() + 'SBtab()'
                new_sbtab = eval(function_name)
                if new_sbtab != False: sbtabs.append(new_sbtab)
            except:
                pass

        sbtabs = self.getRidOfNone(sbtabs)
        sbtabs = self.getRidOfEmptyColumns(sbtabs)

        sbtab_objects = []

        for sbtab in sbtabs:
            so = SBtab.SBtabTable(sbtab[0],
                                  self.filename[:-4] + '_%s.tsv' % sbtab[1])
            sbtab_objects.append(so)

        return (sbtab_objects, self.warnings)
Пример #9
0
    def fbc_gene(self):
        '''
        builds a gene SBtab for the content of the fbc plugin
        '''
        fbc_plugin = self.model.getPlugin('fbc')

        # header row
        sbtab_fbc_gene = '!!SBtab TableID="gene" Document="%s" TableType='\
                         '"Gene" TableName="FBC Gene" SBtabVersion="1.0"\n' % self.filename

        # columns
        columns = [
            '!ID', '!SBML:fbc:ID', '!SBML:fbc:Name', '!SBML:fbc:geneProduct',
            '!SBML:fbc:label'
        ]

        sbtab_fbc_gene += '\t'.join(columns) + '\n'

        # value rows
        for gp in fbc_plugin.getListOfGeneProducts():
            value_row = [''] * len(columns)
            value_row[0] = gp.getId()
            value_row[1] = gp.getId()
            try:
                value_row[2] = gp.getName()
            except:
                pass
            value_row[3] = 'True'
            try:
                value_row[4] = gp.getLabel()
            except:
                pass
            sbtab_fbc_gene += '\t'.join(value_row) + '\n'

        sbtab_fbc_gene = SBtab.SBtabTable(sbtab_fbc_gene,
                                          self.filename + '_fbc_gene.tsv')

        # test and extend for annotations
        for i, gene in enumerate(fbc_plugin.getListOfGeneProducts()):
            try:
                annotations = self.get_annotations(gene)
                for j, annotation in enumerate(annotations):
                    col_name = '!Identifiers:' + annotation[1]
                    if col_name not in columns:
                        new_column = [''
                                      ] * (fbc_plugin.getNumGeneProducts() + 1)
                        new_column[0] = col_name
                        new_column[i + 1] = annotation[0]
                        columns.append(col_name)
                        sbtab_fbc_gene.add_column(new_column)
                    else:
                        sbtab_fbc_gene.change_value_by_name(
                            gene.getId(), col_name, annotation[0])
            except:
                self.warnings.append('Could not add the annotation %s for '\
                                     'gene %s' % annotations[1],
                                     gene.getId())

        return sbtab_fbc_gene
Пример #10
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.')
Пример #11
0
    def compound_sbtab(self):
        '''
        build a compound SBtab
        '''
        # header row
        sbtab_compound = '!!SBtab SBtabVersion="1.0" Document="%s" TableType='\
                         '"Compound" TableName="Compound"\n' % self.filename

        # columns
        columns = ['!ID', '!Name', '!Location', '!Charge', '!IsConstant',
                   '!SBOTerm', '!InitialConcentration', '!hasOnlySubstanceUnits']
        sbtab_compound += '\t'.join(columns) + '\n'

        # value rows
        for species in self.model.getListOfSpecies():
            value_row = [''] * len(columns)
            value_row[0] = species.getId()
            try: value_row[1] = species.getName()
            except: pass
            try: value_row[2] = species.getCompartment()
            except: pass
            try: value_row[3] = str(species.getCharge())
            except: pass
            try: value_row[4] = str(species.getConstant())
            except: pass
            if str(species.getSBOTerm()) != '-1':
                value_row[5] ='SBO:%.7d'%species.getSBOTerm()
            try: value_row[6] = str(species.getInitialConcentration())
            except: pass
            try: value_row[7] = str(species.getHasOnlySubstanceUnits())
            except: pass
            sbtab_compound += '\t'.join(value_row) + '\n'

        sbtab_compound = SBtab.SBtabTable(sbtab_compound,
                                          self.filename + '_compound.tsv')
            
        # test and extend for annotations
        for i, species in enumerate(self.model.getListOfSpecies()):
            try:
                annotations = self.get_annotations(species)
                for j, annotation in enumerate(annotations):
                    col_name = '!Identifiers:' + annotation[1]
                    if col_name not in columns:
                        new_column = [''] * (self.model.getNumSpecies() + 1)
                        new_column[0] = col_name
                        new_column[i + 1] = annotation[0]
                        columns.append(col_name)
                        sbtab_compound.add_column(new_column)
                    else:
                        sbtab_compound.change_value_by_name(species.getId(),
                                                            col_name,
                                                            annotation[0])
            except:
                self.warnings.append('Could not add the annotation %s for'\
                                     'compound %s' % annotations[1],
                                     species.getId())

        return sbtab_compound
Пример #12
0
 def test_singular(self):
     '''
     test if the singular function is working (multiple SBtabs are rejected by SBtabTable)
     '''
     p = open('python/tests/docs/teusink.tsv', 'r')
     p_content = p.read()
     with self.assertRaises(SBtab.SBtabError):
         sbtab = SBtab.SBtabTable(p_content, 'teusink.tsv')
     p.close()
Пример #13
0
def createDataset(header_row, columns, value_rows, filename):
    '''
    Creates an SBtab object by merging strings or list of strings.
    Takes a header row, main column row, and the value rows as lists of strings and returns an SBtab object.

    Parameters
    ----------
    header_row : str
        String of the header row.
    columns: list
        List of strings, names of the columns.
    value_rows : list
        List of lists containing the different rows of the table.
    '''
    # Initialize variables
    sbtab_temp = []
    sbtab_dataset = tablib.Dataset()
    header = header_row.split(' ')

    # Delete spaces in header, main column and data rows
    header = [x.strip(' ') for x in header]
    columns = [x.strip(' ') for x in columns]
    for row in value_rows:
        try:
            for entry in row:
                entry = entry.strip(' ')
        except:
            continue

    # Add header, main column and data rows to temporary list object
    sbtab_temp.append(header)
    sbtab_temp.append(columns)
    for row in value_rows:
        sbtab_temp.append(row)

    # Delete all empty entries at the end of the rows
    for row in sbtab_temp:
        if len(row) > 1:
            while not row[-1]:
                del row[-1]

    # Make all rows the same length
    longest = max([len(x) for x in sbtab_temp])
    for row in sbtab_temp:
        if len(row) < longest:
            for i in range(longest - len(row)):
                row.append('')
            sbtab_dataset.append(row)
        else:
            sbtab_dataset.append(row)

    # Create SBtab object from tablib dataset
    sbtab = SBtab.SBtabTable(sbtab_dataset, filename)
    return sbtab
Пример #14
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()
Пример #15
0
def openSBtab(filepath):
    '''
    Opens SBtab from file. 

    Parameters
    ----------
    filepath : str
        Path of the spreadsheet file.
    '''
    if not os.path.isfile(filepath):
        return None

    dataset = tablibIO.importSet(filepath)
    sbtab = SBtab.SBtabTable(dataset, filepath)

    return sbtab
Пример #16
0
    def test_write_sbtab(self):
        '''
        function writes SBtab to hard drive
        '''
        for sbtab in self.sbtabs:
            self.assertTrue(sbtab.write(sbtab.filename))

            get_back = open(sbtab.filename, 'r')
            get_back_content = get_back.read()
            get_back_sbtab = SBtab.SBtabTable(get_back_content, sbtab.filename)
            get_back.close()

            self.assertEqual(sbtab.header_row.rstrip(),
                             get_back_sbtab.header_row.rstrip())
            self.assertEqual(sbtab.columns, get_back_sbtab.columns)
            self.assertEqual(sbtab.value_rows, get_back_sbtab.value_rows)
Пример #17
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.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()
Пример #18
0
    def fbc_objective(self):
        '''
        builds a SBtab of the TableType FbcObjective
        '''
        fbc_plugin = self.model.getPlugin('fbc')
        active_obj = fbc_plugin.getActiveObjectiveId()

        # header row
        sbtab_fbc = '!!SBtab TableID="fbcobj" Document="%s" TableType='\
                    '"FbcObjective" TableName="FBC Objective" SBtabVersion="1.0"\n' % self.filename

        # columns
        columns = [
            '!ID', '!Name', '!SBML:fbc:type', '!SBML:fbc:active',
            '!SBML:fbc:objective'
        ]

        sbtab_fbc += '\t'.join(columns) + '\n'

        # value rows
        for obj in fbc_plugin.getListOfObjectives():
            value_row = [''] * len(columns)
            value_row[0] = obj.getId()
            try:
                value_row[1] = obj.getName()
            except:
                pass
            try:
                value_row[2] = obj.getType()
            except:
                pass
            if obj.getId() == active_obj: value_row[3] = 'True'
            else: value_row[3] = 'False'
            objective = ''
            for fo in obj.getListOfFluxObjectives():
                objective += '%s * %s +' % (str(
                    fo.getCoefficient()), fo.getReaction())
            value_row[4] = objective[:-2]

            sbtab_fbc += '\t'.join(value_row) + '\n'

        sbtab_fbc = SBtab.SBtabTable(sbtab_fbc,
                                     self.filename + '_fbc_objective.tsv')

        return sbtab_fbc
Пример #19
0
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)
Пример #20
0
def def_files():
    '''
    upload your own definition SBtab
    '''
    response.title = T('SBtab - Standardised data tables for Systems Biology')
    response.subtitle = T('Upload your own definition files')

    dform = SQLFORM.factory(
        Field('File',
              'upload',
              uploadfolder="/tmp",
              label='Upload new definition file (.csv, .tsv, .xlsx)',
              requires=IS_LENGTH(10485760,
                                 1,
                                 error_message='Max upload size: 10MB')))

    session.warnings_def = []
    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_def = copy.deepcopy(sbtab_def)
            session.definition_file_name = sbtab_def.filename
            session.definition_file_name_def = copy.deepcopy(
                sbtab_def.filename)
            session.new_def = False
        except:
            session.warnings_def.append(
                'There was an error reading the definition file.')

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

        try:
            sbtab_def_file = request.vars.File.value.decode('utf-8', 'ignore')
        except:
            session.warnings_def.append('The file is not a valid SBtab file.')
            redirect(URL(''))
        filename = request.vars.File.filename
        try:
            sbtab_def = SBtab.SBtabTable(sbtab_def_file, filename)
        except:
            session.warnings_def.append(
                'The file could not be used as a valid SBtab Table.')
            redirect(URL(''))

        session.definition_file_def = copy.deepcopy(session.definition_file)
        session.definition_file_name_def = copy.deepcopy(
            session.definition_file_name)

        session.definition_file = sbtab_def
        session.definition_file_name = filename
        session.new_def = True
    elif dform.errors:
        response.flash = 'form has errors'

    #pushed erase button
    if request.vars.erase_def_button:
        # set def file back to default
        session.definition_file = session.definition_file_def
        session.definition_file_name = session.definition_file_name_def
        session.new_def = False
        redirect(URL(''))

    return dict(UPL_FORM=dform,
                DEF_FILE=session.definition_file,
                DEF_NAME=session.definition_file_name,
                NEW=session.new_def,
                WARNINGS_DEF=session.warnings_def)
Пример #21
0
def def_files():
    '''
    upload your own definition SBtab
    '''
    response.title = T('SBtab - Standardised data tables for Systems Biology')
    response.subtitle = T('Upload your own definition files')

    dform = SQLFORM.factory(
        Field('File',
              'upload',
              uploadfolder="/tmp",
              label='Upload new definition file (.csv, .tsv, .xls)',
              requires=IS_LENGTH(10485760,
                                 1,
                                 error_message='Max upload size: 10MB')))
    session.new_def = False

    #update session lists
    if dform.process().accepted:
        response.flash = 'form accepted'
        session.warnings_def = []
        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.')

        try:
            sbtab_def_file = request.vars.File.value.decode('utf-8', 'ignore')
        except:
            session.warnings_def.append('The file is not a valid SBtab file.')
            redirect(URL(''))
        filename = request.vars.File.filename
        try:
            sbtab_def = SBtab.SBtabTable(sbtab_def_file, filename)
        except:
            session.warnings_def.append(
                'The file could not be used as a valid SBtab Table.')
            redirect(URL(''))
        session.definition_file = sbtab_def
        session.definition_file_name = filename
        session.new_def = True
    elif dform.errors:
        response.flash = 'form has errors'

    #pushed erase button
    if request.vars.erase_def_button:
        del session.definition_file[int(request.vars.erase_def_button)]
        del session.definition_file_name[int(request.vars.erase_def_button)]
        session.new_def = False
        redirect(URL(''))

    return dict(UPL_FORM=dform,
                DEF_FILE=session.definition_file,
                DEF_NAME=session.definition_file_name,
                NEW=session.new_def)
Пример #22
0
    def quantity_sbtab(self):
        '''
        Builds a Quantity SBtab.
        '''
        # if there are no parameters/quantities in the model, return False
        parameters = True
        if len(self.model.getListOfParameters()) == 0:
            parameters = False
            for reaction in self.model.getListOfReactions():
                kinetic_law = reaction.getKineticLaw()
                if len(kinetic_law.getListOfParameters()) != 0:
                    parameters = True
        if not parameters: return False

        # header row
        sbtab_quantity = '!!SBtab TableID="quantity" Document="%s" TableType='\
                         '"Quantity" TableName="Quantity" SBtabVersion="1.0"\n' % self.filename
        # columns
        columns = [
            '!ID', '!Parameter:SBML:parameter:id', '!Value', '!Unit', '!Type',
            '!SBOTerm'
        ]
        sbtab_quantity += '\t'.join(columns) + '\n'

        # required for later iteration of parameter annotations
        local_parameters = []

        # value rows for local parameters
        for reaction in self.model.getListOfReactions():
            kinetic_law = reaction.getKineticLaw()
            if kinetic_law:
                value_row = [''] * len(columns)
                for parameter in kinetic_law.getListOfParameters():
                    value_row[0] = parameter.getId() + '_' + reaction.getId()
                    value_row[1] = parameter.getId()
                    value_row[2] = str(parameter.getValue())
                    try:
                        value_row[3] = parameter.getUnits()
                    except:
                        pass
                    value_row[4] = 'local parameter'
                    if str(parameter.getSBOTerm()) != '-1':
                        value_row[5] = 'SBO:%.7d' % parameter.getSBOTerm()
                    local_parameters.append(parameter)
                    sbtab_quantity += '\t'.join(value_row) + '\n'

        sbtab_quantity = SBtab.SBtabTable(sbtab_quantity,
                                          self.filename + '_quantity.tsv')

        # test and extend for annotations of local parameters
        for i, quantity in enumerate(local_parameters):
            try:
                annotations = self.get_annotations(quantity)
                for j, annotation in enumerate(annotations):
                    col_name = '!Identifiers:' + annotation[1]
                    if col_name not in columns:
                        new_column = [''] * (len(local_parameters) + 1)
                        new_column[0] = col_name
                        new_column[i + 1] = annotation[0]
                        columns.append(col_name)
                        sbtab_quantity.add_column(new_column)
                    else:
                        sbtab_quantity.change_value_by_name(
                            quantity.getId(), col_name, annotation[0])
            except:
                self.warnings.append('Could not add the annotation %s for'\
                                     'quantity %s' % annotations[1],
                                     quantity.getId())

        # value_rows for global parameters
        for parameter in self.model.getListOfParameters():
            value_row = [''] * len(sbtab_quantity.columns)
            value_row[0] = parameter.getId()
            value_row[1] = parameter.getId()
            value_row[2] = str(parameter.getValue())
            try:
                value_row[3] += parameter.getUnits()
            except:
                pass
            value_row[4] = 'global parameter'
            if str(parameter.getSBOTerm()) != '-1':
                value_row[5] = 'SBO:%.7d' % parameter.getSBOTerm()
            sbtab_quantity.add_row(value_row)

        # test and extend for annotations of global parameters
        for i, parameter in enumerate(self.model.getListOfParameters()):
            try:
                annotations = self.get_annotations(parameter)
                for j, annotation in enumerate(annotations):
                    col_name = '!Identifiers:' + annotation[1]
                    if col_name not in columns:
                        new_column = [''] * (self.model.getNumParameters() + 1)
                        new_column[0] = col_name
                        new_column[i + 1] = annotation[0]
                        columns.append(col_name)
                        sbtab_quantity.add_column(new_column)
                    else:
                        sbtab_quantity.change_value_by_name(
                            parameter.getId(), col_name, annotation[0])
            except:
                self.warnings.append('Could not add the annotation %s for'\
                                     'quantity %s' % annotations[1],
                                     parameter.getId())

        if sbtab_quantity.value_rows == []:
            return False

        return sbtab_quantity
Пример #23
0
    def reaction_sbtab(self):
        '''
        build a reaction SBtab
        '''
        # header row
        sbtab_reaction = '!!SBtab TableID="reaction" Document="%s" TableType='\
                         '"Reaction" TableName="Reaction" SBtabVersion="1.0"\n' % self.filename

        # columns
        columns = [
            '!ID', '!Name', '!ReactionFormula', '!Location', '!Regulator',
            '!KineticLaw', '!SBOTerm', '!IsReversible'
        ]
        if self.fbc:
            columns = columns + [
                '!SBML:fbc:GeneAssociation', '!SBML:fbc:LowerBound',
                '!SBML:fbc:UpperBound'
            ]

        sbtab_reaction += '\t'.join(columns) + '\n'

        for reaction in self.model.getListOfReactions():
            value_row = [''] * len(columns)
            value_row[0] = reaction.getId()
            try:
                value_row[1] = reaction.getName()
            except:
                pass
            try:
                value_row[2] = self.make_sum_formula(reaction)
            except:
                pass
            try:
                value_row[3] = str(reaction.getCompartment())
            except:
                pass
            try:
                modifiers = reaction.getListOfModifiers()
                if len(modifiers) > 1:
                    modifier_list = ''
                    for i, modifier in enumerate(modifiers):
                        if i != len(reaction.getListOfModifiers()) - 1:
                            modifier_list += modifier.getSpecies() + '|'
                        else:
                            modifier_list += modifier.getSpecies()
                    value_row[4] = modifier_list
                elif len(modifiers) == 1:
                    for modifier in modifiers:
                        value_row[4] = modifier.getSpecies()
            except:
                pass
            try:
                fm = reaction.getKineticLaw().getFormula()
                value_row[5] = fm.replace('\n', '')
            except:
                pass
            if str(reaction.getSBOTerm()) != '-1':
                value_row[6] = 'SBO:%.7d' % reaction.getSBOTerm()
            try:
                value_row[7] = str(reaction.getReversible())
            except:
                pass

            if self.fbc:
                try:
                    fbc_plugin = reaction.getPlugin('fbc')
                    try:
                        ga = fbc_plugin.getGeneProductAssociation()
                        ass = ga.getAssociation().toInfix()
                        value_row[8] = ass
                    except:
                        pass
                    value_row[9] = str(fbc_plugin.getLowerFluxBound())
                    value_row[10] = str(fbc_plugin.getUpperFluxBound())
                except:
                    self.warnings.append(
                        'FBC Reaction information could not be read.')

            sbtab_reaction += '\t'.join(value_row) + '\n'

        sbtab_reaction = SBtab.SBtabTable(sbtab_reaction,
                                          self.filename + '_reaction.tsv')

        # test and extend for annotations
        for i, reaction in enumerate(self.model.getListOfReactions()):
            try:
                annotations = self.get_annotations(reaction)
                for j, annotation in enumerate(annotations):
                    col_name = '!Identifiers:' + annotation[1]
                    if col_name not in columns:
                        new_column = [''] * (self.model.getNumReactions() + 1)
                        new_column[0] = col_name
                        new_column[i + 1] = annotation[0]
                        columns.append(col_name)
                        sbtab_reaction.add_column(new_column)
                    else:
                        sbtab_reaction.change_value_by_name(
                            reaction.getId(), col_name, annotation[0])
            except:
                self.warnings.append('Could not add the annotation %s for'\
                                     'reaction %s' % annotations[1],
                                     reaction.getId())

        return sbtab_reaction
Пример #24
0
def parseReactionTable(sbtab_file,file_name,export=False):
    '''
    parses a Reaction SBtab to a stoichiometric table of the reaction formula.
    if the export parameter is set to True, the file will be written to the hard disk automatically
    '''
    import tablibIO
    import SBtab

    if sbtab_file.table_type != 'Reaction':
        print('The given TableType \"%s\" cannot be parsed. The TableType \"Reaction\" is required.'%sbtab_file.table_type)
        return False

    if not '!ReactionFormula' in sbtab_file.columns:
        print('The given provided SBtab file misses the column \"ReactionFormula\".')
        return False
    else:
        for i,c in enumerate(sbtab_file.columns):
            if c == '!ReactionFormula': rf = i
            elif c == '!ID': r = i

    react_stoich_sub  = []
    react_stoich_prod = []
    
    for row in sbtab_file.value_rows:
        reaction = row[r]
        formula  = row[rf]
        left     = formula.split('<=>')[0].lstrip().rstrip()
        right    = formula.split('<=>')[1].lstrip().rstrip()
        if '+' in left:
            subs = left.split('+')
            for sub in subs:
                sub = sub.lstrip().rstrip()
                try:
                    float(sub[0])
                    (stoich,sub) = sub.split(' ')
                    st = reaction+'\t'+stoich+'\t'+sub+'\t\t\t\n'
                    react_stoich_sub.append(st)
                except:
                    st = reaction+'\t1\t'+sub+'\t\t\t\n'
                    react_stoich_sub.append(st)
        else:
            try:
                float(left[0])
                (stoich,left) = left.split(' ')
                st = reaction+'\t'+stoich+'\t'+left+'\t\t\t\n'
                react_stoich_sub.append(st)
            except:
                st = reaction+'\t1\t'+left+'\t\t\t\n'
                react_stoich_sub.append(st)
        if '+' in right:
            prods = right.split('+')
            for prod in prods:
                prod = prod.lstrip().rstrip()
                try:
                    float(prod[0])
                    (stoich,prod) = prod.split(' ')
                    st = reaction+'\t'+stoich+'\t\t'+prod+'\t\t\n'
                    react_stoich_prod.append(st)
                except:
                    st = reaction+'\t1\t\t'+prod+'\t\n'
                    react_stoich_prod.append(st)
        else:
            try:
                float(right[0])
                (stoich,right) = right.split(' ')
                st = reaction+'\t'+stoich+'\t\t'+right+'\t\t\n'
                react_stoich_prod.append(st)
            except:
                st = reaction+'\t1\t\t'+right+'\t\t\n'
                react_stoich_prod.append(st)
        
    new_SBtab = '!!SBtab SBtabVersion="1.0" TableType="StoichiometricMatrix" TableName="%s" UniqueKey="False"\n!ReactionID\t!Stoichiometry\t!Substrate\t!Product!Location\n'%file_name[:-4]
    for sub in react_stoich_sub:
        new_SBtab += sub
    for prod in react_stoich_prod:
        new_SBtab += prod

    if export:
        parseTable = open('parseTable.tsv','w')
        parseTable.write(new_SBtab)
        parseTable.close()

    return_tab   = tablibIO.importSetNew(new_SBtab,'bla.tsv',separator='\t')
    return_SBtab = SBtab.SBtabTable(return_tab,'parseTableReaction.tsv')

    return return_SBtab
Пример #25
0
    def layout_sbtab(self):
        '''
        builds a SBtab of the TableType Layout
        '''
        layout = self.model.getPlugin('layout').getLayout(0)

        # header row
        sbtab_layout = '!!SBtab TableID="layout" Document="%s" TableType='\
                       '"Layout" TableName="SBML Layout" SBtabVersion="1.0"\n' % self.filename

        # columns
        columns = [
            '!ID', '!Name', '!SBML:layout:modelEntity',
            '!SBML:layout:compartment:id', '!SBML:layout:reaction:id',
            '!SBML:layout:species:id', '!SBML:layout:curveSegment',
            '!SBML:layout:X', '!SBML:layout:Y', '!SBML:layout:width',
            '!SBML:layout:height'
        ]
        sbtab_layout += '\t'.join(columns) + '\n'

        # value rows
        # layout canvas
        value_row = [''] * len(columns)
        value_row[0] = layout.getId()
        try:
            value_row[1] = layout.getName()
        except:
            pass
        value_row[2] = 'LayoutCanvas'
        try:
            dim = layout.getDimensions()
            value_row[9] = str(dim.getWidth())
            value_row[10] = str(dim.getHeight())
        except:
            pass
        sbtab_layout += '\t'.join(value_row) + '\n'

        # compartment glyphs
        for compartment in self.model.getListOfCompartments():
            cg = False
            for cg_i in layout.getListOfCompartmentGlyphs():
                if cg_i.getCompartmentId() == compartment.getId():
                    cg = cg_i
            if not cg: continue
            value_row = [''] * len(columns)
            value_row[0] = cg.getId()
            try:
                value_row[1] = cg.getName()
            except:
                pass
            value_row[2] = 'Compartment'
            value_row[3] = cg.getCompartmentId()
            try:
                bb = cg.getBoundingBox()
                value_row[7] = str(bb.getX())
                value_row[8] = str(bb.getY())
                value_row[9] = str(bb.getWidth())
                value_row[10] = str(bb.getHeight())
            except:
                self.warnings.append(
                    'Compartment layout information could not be read.')
            sbtab_layout += '\t'.join(value_row) + '\n'

        # species glyphs
        for species in self.model.getListOfSpecies():
            sg = False
            for sg_i in layout.getListOfSpeciesGlyphs():
                if sg_i.getSpeciesId() == species.getId():
                    sg = sg_i
            if not sg: continue
            value_row = [''] * len(columns)
            value_row[0] = sg.getId()
            value_row[2] = 'Species'
            value_row[5] = sg.getSpeciesId()
            try:
                bb = sg.getBoundingBox()
                value_row[7] = str(bb.getX())
                value_row[8] = str(bb.getY())
                value_row[9] = str(bb.getWidth())
                value_row[10] = str(bb.getHeight())
            except:
                self.warnings.append(
                    'Species layout information could not be read.')
            sbtab_layout += '\t'.join(value_row) + '\n'

            # construct corresponding text glyph for the species glyph (new row in SBtab)
            value_row = [''] * len(columns)
            value_row[2] = 'SpeciesText'
            value_row[5] = sg.getSpeciesId()

            tg = False
            for tg_i in layout.getListOfTextGlyphs():
                if tg_i.getGraphicalObjectId() == sg.getId():
                    tg = tg_i
            if not tg: continue
            value_row[0] = tg.getId()
            try:
                bb = tg.getBoundingBox()
                value_row[7] = str(bb.getX())
                value_row[8] = str(bb.getY())
                value_row[9] = str(bb.getWidth())
                value_row[10] = str(bb.getHeight())
            except:
                self.warnings.append(
                    'Species layout text information could not be read.')
            sbtab_layout += '\t'.join(value_row) + '\n'

        # reaction glyphs (made up of reaction glyph curve and species reference glyph curve/s)
        for reaction in self.model.getListOfReactions():
            rg = False
            for rg_i in layout.getListOfReactionGlyphs():
                if rg_i.getReactionId() == reaction.getId():
                    rg = rg_i
            if not rg: continue
            try:
                curve = rg.getCurve()
                curve_segment = curve.getListOfCurveSegments()[0]
            except:
                self.warnings.append('Reaction Glyph Curve cannot be read.')
                continue

            # reaction glyph curve segment
            for point in ['Start', 'End']:
                value_row = [''] * len(columns)
                value_row[0] = rg.getId()
                value_row[2] = 'ReactionCurve'
                try:
                    value_row[4] = rg.getReactionId()
                    value_row[6] = point
                    value_row[7] = str(
                        eval('curve_segment.get%s().getXOffset()' % point))
                    value_row[8] = str(
                        eval('curve_segment.get%s().getYOffset()' % point))
                except:
                    self.warnings.append(
                        'Reaction Glyph Curve Segment cannot be read.')

                sbtab_layout += '\t'.join(value_row) + '\n'

            # reaction glyph speciesreference glyph curve segment
            for species_reference_glyph in rg.getListOfSpeciesReferenceGlyphs(
            ):
                try:
                    curve = species_reference_glyph.getCurve()
                    curve_segment = curve.getListOfCurveSegments()[0]
                except:
                    self.warnings.append(
                        'Reaction Species Reference Glyph Curve cannot be read.'
                    )
                    continue
                for point in ['Start', 'End', 'BasePoint1', 'BasePoint2']:
                    value_row = [''] * len(columns)
                    value_row[0] = species_reference_glyph.getId()
                    value_row[2] = 'SpeciesReferenceCurve'
                    try:
                        value_row[4] = rg.getReactionId()
                        value_row[
                            5] = species_reference_glyph.getSpeciesGlyphId()
                        value_row[6] = point
                        value_row[7] = str(
                            eval('curve_segment.get%s().getXOffset()' % point))
                        value_row[8] = str(
                            eval('curve_segment.get%s().getYOffset()' % point))
                    except:
                        self.warnings.append(
                            'Reaction Glyph Curve Segment cannot be read.')
                    sbtab_layout += '\t'.join(value_row) + '\n'

        sbtab_layout = SBtab.SBtabTable(sbtab_layout,
                                        self.filename + '_layout.tsv')

        return sbtab_layout
Пример #26
0
def parameter_balancing_wrapper(sbml, sbtab_data_name=None, sbtab_prior_name=None, sbtab_options_name=None, verbose=False, no_pseudo_values=False, output_name=None, pb_log=False, concat=False):
    '''
    wrapper for parameter balancing.

    Parameters
    ==========
    sbml: string (path to sbml file)
    sbtab_data_name: string (path to sbtab data file)
    sbtab_prior_name: string (path to sbtab prior file)
    sbtab_options_name: string (path to sbtab options file)
    verbose: Boolean (enable messages on commandline)
    no_pseudo_values: Boolean (disable usage of pseudo values)
    output_name: string (name for the output files)
    pb_log: Boolean (enable writing of a log file)
    concat: Boolean (enable writing of concatenation input/output file)
    '''
    model_name = sbml
    parameter_dict = {}
    log_file = 'Parameter balancing log file of model %s\n' % (model_name)
    warn_flag = False

    ###########################
    # 1: open and prepare the files; then check for some rudimentary validity:
    # 1.1: SBML model
    reader = libsbml.SBMLReader()
    try: sbml_file = reader.readSBML(model_name)
    except:
        print('The SBML file %s could not be found.' % (model_name))
        sys.exit()
    try: sbml_model = sbml_file.getModel()
    except:
        print('The SBML file %s is corrupt. I quit.' % (model_name))
        sys.exit()
    valid_extension = misc.validate_file_extension(model_name, 'sbml')
    if not valid_extension:
        print('The SBML file %s has not the correct xml extension'\
              '. I quit.''' % (model_name))
        sys.exit()
        
    if sbml_model.getNumReactions() > 250:
        print('The given model has more than 250 reactions and we '\
              'do not recommend employing models that large for '\
              'parameter balancing.')
        sys.exit()

    pb = balancer.ParameterBalancing(sbml_model)

    ###########################
    # 1.2: open and prepare the optional SBtab data file
    if sbtab_data_name:
        valid_extension = misc.validate_file_extension(sbtab_data_name,
                                                       'sbtab')
        if not valid_extension:
            print('The SBtab data file %s has not the correct file '\
                  'extension.' % (sbtab_data_name))

        try:
            f = open(sbtab_data_name, 'r')
            f_content = f.read()
        except:
            print('The SBtab data file %s cannot be found or'\
                  'read.' % sbtab_data_name)

        try: sbtab_delimiter = misc.check_delimiter(f_content)
        except: sbtab_delimiter = '\t'

        sbtab_data = SBtab.SBtabTable(f_content, sbtab_data_name)
        sbtab_data_validate = validatorSBtab.ValidateTable(sbtab_data)

        warnings = sbtab_data_validate.return_output()
        if warnings != []:
            warn_flag = True
            log_file += 'Log warnings for SBtab data file: '\
                        '%s\n' % sbtab_data_name
            for warning in warnings:
                log_file += warning + '\n'

        #print(sbtab_data.value_rows)
    
    ###########################
    # 1.3: open and prepare an optional SBtab prior file;
    #      if this is not provided, open the default prior file
    if sbtab_prior_name:
        # try to open and read the file
        valid_extension = misc.validate_file_extension(sbtab_prior_name,
                                                       'sbtab')
        if not valid_extension:
            print('The SBtab prior file %s has not the correct file'\
                  'extension.' % (sbtab_prior_name))
        try:
            f = open(sbtab_prior_name, 'r')
            f_content = f.read()
        except:
            print('The SBtab prior file %s cannot be found or'\
                  'read.' % sbtab_prior_name)

        # initialise an SBtab object with the content and check its validity
        sbtab_prior = SBtab.SBtabTable(f_content, sbtab_prior_name)
        pb.get_parameter_information(sbtab_prior)
        sbtab_prior_validate = validatorSBtab.ValidateTable(sbtab_prior)

        # register warnings
        warnings = sbtab_prior_validate.return_output()
        if warnings != []:
            warn_flag = True
            log_file += 'Log warnings for SBtab prior file: '\
                        '%s\n\n' % sbtab_prior_name
            for warning in warnings:
                log_file += warning + '\n'

        valid_prior = misc.valid_prior(sbtab_prior)
        if valid_prior != []:
            warn_flag = True
            log_file += 'Log warnings for SBtab prior file: '\
                        '%s\n\n' % sbtab_prior_name
            for element in valid_prior:
                log_file += str(element) + '\n'

        # extract crucial information from prior
        (pseudos, priors, pmin, pmax) = misc.extract_pseudos_priors(sbtab_prior)
    else:
        # open default prior file
        p = os.path.dirname(os.path.abspath(__file__)) + '/files/default_'\
            'files/pb_prior.tsv'
        try:
            prior_file = open(p, 'r')
            prior = prior_file.read()
        except:
            print('The prior file (/files/default_files/pb_prior.tsv) coul'\
                  'd not be found. I quit.')
            sys.exit()

        sbtab_prior = SBtab.SBtabTable(prior, 'pb_prior.tsv')
        sbtab_prior_validate = validatorSBtab.ValidateTable(sbtab_prior)

        # register warnings
        warnings = sbtab_prior_validate.return_output()
        if warnings != []:
            warn_flag = True
            log_file += 'Log warnings for SBtab prior file: '\
                        '%s\n\n' % sbtab_prior_name
            for warning in warnings:
                log_file += warning + '\n'

        valid_prior = misc.valid_prior(sbtab_prior)
        if valid_prior != []:
            warn_flag = True
            log_file += 'Log warnings for SBtab prior file: '\
                        '%s\n\n' % sbtab_prior_name
            for element in valid_prior:
                log_file += str(element) + '\n'

        # extract crucial information from prior
        (pseudos, priors, pmin, pmax) = misc.extract_pseudos_priors(sbtab_prior)

    ###########################
    # 1.4: open and prepare an optional SBtab options file;
    #      if this is not provided, open the default options file
    if sbtab_options_name:
        valid_extension = misc.validate_file_extension(sbtab_options_name, 'sbtab')
        if not valid_extension:
            print('The SBtab options file %s has not the correct file'\
                  ' extension.' % (sbtab_options_name))
        try:
            f = open(sbtab_options_name, 'r')
            f_content = f.read()
        except:
            print('The SBtab options file %s cannot be found or'\
                  'read.' % sbtab_options_name)

        sbtab_options = SBtab.SBtabTable(f_content, sbtab_options_name)
        sbtab_options_validate = validatorSBtab.ValidateTable(sbtab_options)

        # register warnings
        warnings = sbtab_options_validate.return_output()
        if warnings != []:
            warn_flag = True
            log_file += 'Log warnings for SBtab options file: '\
                        '%s\n\n' % sbtab_options_name
            for warning in warnings:
                log_file += warning + '\n'

        (parameter_dict, log) = misc.readout_config(sbtab_options)
        if log != []:
            warn_flag = True
            log_file += 'Log warnings for SBtab options file: '\
                        '%s\n\n' % sbtab_options_name
            for element in log:
                log_file += str(element) + '\n'
    else:
        o = os.path.dirname(os.path.abspath(__file__)) + '/files/default_'\
            'files/pb_options.tsv'
        try:
            options_file = open(o, 'r')
            f_content = options_file.read()
        except:
            print('The options file (/files/default_files/pb_options.tsv) coul'\
                  'd not be found. I quit.')
            sys.exit()

        sbtab_options = SBtab.SBtabTable(f_content, 'pb_options.tsv')
        sbtab_options_validate = validatorSBtab.ValidateTable(sbtab_options)

        # register warnings
        warnings = sbtab_options_validate.return_output()
        if warnings != []:
            warn_flag = True
            log_file += 'Log warnings for SBtab options file: '\
                        'pb_options.tsv\n\n'
            for warning in warnings:
                log_file += warning + '\n'

        (parameter_dict, log) = misc.readout_config(sbtab_options)

        if log != []:
            warn_flag = True
            log_file += 'Log warnings for SBtab options file: '\
                        '%s\n\n' % sbtab_options_name
            for element in log:
                log_file += str(element) + '\n'

    # Make empty SBtab if required
    if sbtab_data_name:
        sbtab = pb.make_sbtab(sbtab_data, sbtab_data_name, 'All organisms', 43,
                              pmin, pmax, parameter_dict)
        sbtabid2sbmlid = misc.id_checker(sbtab, sbml_model)
        if sbtabid2sbmlid != []:
            warn_flag = True
            log_file += 'Log warnings for SBtab data file: '\
                        '%s\n\n' % sbtab_data_name
            for element in sbtabid2sbmlid:
                log_file += element + '\n'
    else:
        sbtab = pb.make_empty_sbtab(pmin, pmax, parameter_dict)

    # end of file read in and processing;
    # now verify that all required keys are given for the parameter_dict;
    # if not, add them
    if 'temperature' not in parameter_dict.keys():
        parameter_dict['temperature'] = 300
    if 'ph' not in parameter_dict.keys(): parameter_dict['ph'] = 7
    if 'standard chemical potential' not in parameter_dict.keys():
        parameter_dict['standard chemical potential'] = True
    if 'catalytic rate constant geometric mean' not in parameter_dict.keys():
        parameter_dict['catalytic rate constant geometric mean'] = True
    if 'Michaelis constant' not in parameter_dict.keys():
        parameter_dict['Michaelis constant'] = True
    if 'activation constant' not in parameter_dict.keys():
        parameter_dict['activation constant'] = True
    if 'inhibitory constant' not in parameter_dict.keys():
        parameter_dict['inhibitory constant'] = True
    if 'concentration' not in parameter_dict.keys():
        parameter_dict['concentration'] = True
    if 'concentration of enzyme' not in parameter_dict.keys():
        parameter_dict['concentration of enzyme'] = True
    if 'equilibrium constant' not in parameter_dict.keys():
        parameter_dict['equilibrium constant'] = True
    if 'substrate catalytic rate constant' not in parameter_dict.keys():
        parameter_dict['substrate catalytic rate constant'] = True
    if 'product catalytic rate constant' not in parameter_dict.keys():
        parameter_dict['product catalytic rate constant'] = True
    if 'forward maximal velocity' not in parameter_dict.keys():
        parameter_dict['forward maximal velocity'] = True
    if 'reverse maximal velocity' not in parameter_dict.keys():
        parameter_dict['reverse maximal velocity'] = True
    if 'chemical potential' not in parameter_dict.keys():
        parameter_dict['chemical potential'] = True
    if 'reaction affinity' not in parameter_dict.keys():
        parameter_dict['reaction affinity'] = True
    if 'use_pseudo_values' not in parameter_dict.keys():
        parameter_dict['use_pseudo_values'] = False

    if verbose:
        print('\nFiles successfully read. Start balancing.')

    # 2: Parameter balancing
    if parameter_dict['use_pseudo_values'] == 'True' and not no_pseudo_values:
        sbtab_old = copy.deepcopy(sbtab)
        sbtab_new = pb.fill_sbtab(sbtab_old, pseudos, priors)
        pseudo_flag = 'pseudos'
        if verbose:
            print('Parameter balancing is using pseudo values.')

    else:
        sbtab_new = pb.fill_sbtab(sbtab)
        pseudo_flag = 'no_pseudos'
        if verbose:
            print('Parameter balancing is not using pseudo values.')
            
    (sbtab_final, mean_vector, mean_vector_inc, c_post, c_post_inc,
     r_matrix, shannon, log, concat_file) = pb.make_balancing(sbtab_new,
                                                 sbtab, pmin,
                                                 pmax,
                                                 parameter_dict)

    #for row in sbtab_final.value_rows:
    #    print(row)
    log_file += '\n' + log + '\n'

    # 3: inserting parameters and kinetics into SBML model
    transfer_mode = {'standard chemical potential': 'weg',
                     'equilibrium constant': 'hal',
                     'catalytic rate constant': 'cat'}

    try:
        clear_mode = parameter_dict['parametrisation']
        mode = transfer_mode[clear_mode]
    except: mode = 'hal'
    try: enzyme_prefac = parameter_dict['prefac']
    except: enzyme_prefac = True
    try: def_inh = parameter_dict['default_inh']
    except: def_inh = 'complete_inh'
    try: def_act = parameter_dict['default_act']
    except: def_act = 'complete_act'
    try: overwrite = parameter_dict['overwrite']
    except: overwrite = True
    kineticizer_cs = kineticizer.KineticizerCS(sbml_model, sbtab_final, mode,
                                               enzyme_prefac, def_inh,
                                               def_act, True)

    if output_name:
        output_name = output_name
    else:
        try: rm = re.match('.*/(.*)', str(model_name)).group(1)[:-4]
        except: rm = str(model_name)[:-4]
        output_name = rm + '_balanced'

    if verbose:
        print('Done... writing output files.')

    if warn_flag:
        print('The parameter balancing issued warnings. Please generate the '
        'log file with the -l flag and check the warnings.')
        
    # 5: If requested write log file
    if pb_log:
        if log_file.count('\n') == 2:
            log_file += 'No warnings detected. \n'
        log = open(output_name + '_log.txt', 'w')
        log.write(log_file)
        log.close()
        if verbose:
            print('The log file %s has been written.' % (output_name + '_log.txt'))
    
    # 5b: If requested write output file with concatenated input/output files
    if concat:
        c_file = open(output_name + '_concat.tsv', 'w')
        c_file.write(concat_file)
        c_file.close()
        if verbose:
            print('The concat file %s has been written.' % (output_name + '_concat.tsv'))

    # 6: Write SBtab and SBML model
    sbtab_file_new = open(output_name + '.tsv', 'w')
    sbtab_file_new.write(sbtab_final.to_str())
    sbtab_file_new.close()
    if verbose:
        print('The SBtab file %s has been written.' % (output_name + '.tsv'))

    sbml_code = '<?xml version="1.0" encoding="UTF-8"?>\n' + sbml_model.toSBML()
    sbml_model_new = open(output_name + '.xml', 'w')
    sbml_model_new.write(sbml_code)
    sbml_model_new.close()
    if verbose:
        print('The SBML file %s has been written.' % (output_name + '.xml'))
        print('>> Goodbye.')

    return (sbml_model_new, sbtab_final)
Пример #27
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)
Пример #28
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)
Пример #29
0
    def __init__(self, table, sbtab_name, def_table, def_name):
        """
        Initialize validator and start check for file and table format.

        Parameters
        ----------
        table : tablib object
            Tablib object of the SBtab file
        sbtab_name : str
            File path of the SBtab file
        """
        # import definitions from definition table
        definition_table = tablibIO.importSetNew(def_table,
                                                 def_name,
                                                 separator='\t')
        definition_sbtab = SBtab.SBtabTable(definition_table, def_name)
        self.definitions = definition_sbtab.sbtab_list

        # create set of valid table types
        self.allowed_table_types = list(
            set([row[2] for row in self.definitions[2:][0]]))

        # create dict of valid column names per table type
        self.allowed_columns = {}
        for table_type in self.allowed_table_types:
            self.allowed_columns[table_type] = [
                row[0] for row in self.definitions[2:][0]
                if row[2] == table_type
            ]

        # initialize warning string
        self.warnings = []
        # define self variables
        self.table = table
        self.filename = sbtab_name

        # check file format and header row
        self.checkTableFormat()

        # try creating SBtab instance
        #try:
        self.sbtab = SBtab.SBtabTable(self.table, self.filename)
        self.column2format = {}
        defs = self.definitions[2]
        for row in defs:
            if row[3] == self.sbtab.table_type:
                self.column2format[row[1]] = row[4]
        #except:
        #    raise SBtabError('The Parser cannot work with this file!')

        # remove empty column headers
        f_columns = []
        for element in self.sbtab.columns:
            if element == '': pass
            else: f_columns.append(element)
        self.sbtab.columns = f_columns

        # determine headers
        self.determineHeaders()

        # check SBtab object for validity
        self.checkTable()
Пример #30
0
    def __init__(self, table, sbtab_name, def_table=None,def_name=None):
        '''
        Initialises validator and starts check for file and table format.

        Parameters
        ----------
        table : tablib object
            Tablib object of the SBtab file.
        sbtab_name : str
            File path of the SBtab file.
        def_table : str
            SBtab definition table as string representation.
        def_name : str
            SBtab definition table name.
        '''
        delimiter    = misc.getDelimiter(table)
        sbtab_tablib = tablibIO.importSetNew(table,sbtab_name,delimiter)
        
        if not def_name:
            def_name = 'definitions.tsv'
        if not def_table:
            try:
                default_def = open(def_name,'r')
                def_table   = default_def.read()
                default_def.close()
            except:
                print 'Definition file could not be loaded, so the validation could not be started. Please provide definition file as argument or make it is located in the same directory as this script.'
                sys.exit()
        
        # import definitions from definition table
        definition_table = tablibIO.importSetNew(def_table,def_name,separator='\t')
        definition_sbtab = SBtab.SBtabTable(definition_table, def_name)
        self.definitions = definition_sbtab.sbtab_list

        # create set of valid table types
        self.allowed_table_types = list(set([row[3] for row in self.definitions[2:][0]]))

        # create dict of valid column names per table type
        self.allowed_columns = {}
        for table_type in self.allowed_table_types:
            self.allowed_columns[table_type] = [row[1] for row in self.definitions[2:][0] if row[3] == table_type]

        # initialize warning string
        self.warnings = []
        # define self variables
        self.table = sbtab_tablib
        self.filename = sbtab_name

        # check file format and header row
        self.checkTableFormat()

        # try creating SBtab instance
      
        self.sbtab = SBtab.SBtabTable(self.table, self.filename)

        self.column2format = {}
        defs = self.definitions[2]
        for row in defs:
            if row[3] == self.sbtab.table_type:
                self.column2format[row[1]] = row[4]

        # remove empty column headers
        f_columns = []
        for element in self.sbtab.columns:
            if element == '': pass
            else: f_columns.append(element)
        self.sbtab.columns = f_columns

        # determine headers
        self.determineHeaders()

        # check SBtab object for validity
        self.checkTable()

        '''