def get_population_flags(list_only=False, dictionary=False, table=False): """Return all population flags available in the PopFlags class. :list_only: Return a simple text list instead of a list of Phenotype objects. :dictionary: Return a dictionary of population=>ID :table: Return a pretty table for printing. """ flags = _ref.PopFlag.__dict__['__members__'] if list_only: return [i for i in flags.values()] elif dictionary: return {k: v for k, v in flags.items()} elif table: return _tb( [['FLAG', 'Label']] + [[int(v), k] for k, v in flags.items()], headers='firstrow', tablefmt='grid' ) else: return flags
def get_populations(list_only=False, dictionary=False, table=False): """Return all populatons from the Population table. :list_only: Return a simple text list instead of a list of Phenotype objects. :dictionary: Return a dictionary of population=>ID :table: Return a pretty table for printing. """ s, _ = _db.get_session() q = s.query(t.Population).all() if list_only: return [i.population for i in q] elif dictionary: return {i.population: i.id for i in q} elif table: ids = [i.id for i in q] pops = [i.population for i in q] return _tb({'ID': ids, 'Population': pops}, headers='keys', tablefmt='grid') else: return q
def get_phenotypes(list_only=False, dictionary=False, table=False): """Return all phenotypes from the Phenotype table. :list_only: Return a simple text list instead of a list of Phenotype objects. :dictionary: Return a dictionary of phenotype=>ID :table: Return a pretty table for printing. """ s, _ = _db.get_session() q = s.query(t.Phenotype).order_by('phenotype').all() if list_only: return [i.phenotype for i in q] elif dictionary: return {i.phenotype: i.id for i in q} elif table: ids = [i.id for i in q] phenos = [i.phenotype for i in q] return _tb({'ID': ids, 'Phenotype': phenos}, headers='keys', tablefmt='grid') else: return q
def get_phenotype_categories(list_only=False, dictionary=False, table=False): """Return all phenotype categories from the PhenoCats table. :list_only: Return a simple text list instead of a list of Phenotype objects. :dictionary: Return a dictionary of phenotype=>ID :table: Return a pretty table for printing. """ s, _ = _db.get_session() q = s.query(t.PhenoCats).order_by('category').all() if list_only: return [i.category for i in q] elif dictionary: return {i.category: i.id for i in q} elif table: ids = [i.id for i in q] cats = [i.category for i in q] aliases = [i.alias for i in q] return _tb({'ID': ids, 'Category': cats, 'Alias': aliases}, headers='keys', tablefmt='grid') else: return q
def display_columns(self, display_as='table', write=False): """Return all columns in the table nicely formatted. Display choices: table: A formatted grid-like table tab: A tab delimited non-formatted version of table list: A string list of column names Args: display_as: {table,tab,list} write: If true, print output to console, otherwise return string. Returns: A formatted string or None """ cols = self.columns if display_as == 'table': out = _tb( [['Column', 'Description', 'Type']] +\ [[k, v[1], v[0]] for k, v in cols.items()], headers='firstrow', tablefmt='grid' ) elif display_as == 'tab': out = '\n'.join( ['\t'.join(['Column', 'Description', 'Type'])] +\ ['\t'.join([k, v[1], v[0]]) for k, v in cols.items()], ) elif display_as == 'list': out = '\n'.join([i[1] for i in cols.values()]) else: raise Exception("'display_as' must be one of {table,tab,list}") if write: print(out) else: return out