示例#1
0
    def get_tables_row_count(self, table_name=None):
        """returns a cogent Table object with the row count for each table
        in the database

        Arguments:
            - table_name: database table name. If none, all database tables
              assessed."""
        if type(table_name) == str:
            table_name = (table_name, )
        elif table_name is None:
            self._meta.reflect()
            table_name = list(self._meta.tables.keys())
        rows = []
        for name in table_name:
            table = self.get_table(name)
            count = table.count().execute().fetchone()[0]
            rows.append([f"{self.db_name}.{name}", count])

        return cogent_table.Table(header=["name", "count"], data=rows)
示例#2
0
文件: distance.py 项目: mr-c/cogent3
    def get_table(self, summary_function="mean", **kwargs):
        """returns a Table instance of the distance matrix.

        Parameters
        ----------
        summary_function
            a string naming the function used for
            estimating param from threeway distances. Valid values are 'mean'
            (default) and 'median'.

        """
        d = self.get_pairwise_distances(summary_function=summary_function,
                                        **kwargs)
        if not d:
            d = {}
            for s1 in self._seqnames:
                for s2 in self._seqnames:
                    if s1 == s2:
                        continue
                    else:
                        d[(s1, s2)] = "Not Done"
        twoD = []
        for s1 in self._seqnames:
            row = [s1]
            for s2 in self._seqnames:
                if s1 == s2:
                    row.append("")
                    continue
                try:
                    row.append(d[(s1, s2)])
                except KeyError:
                    row.append(d[(s2, s1)])
            twoD.append(row)
        T = table.Table(
            [r"Seq1 \ Seq2"] + self._seqnames,
            twoD,
            index_name=r"Seq1 \ Seq2",
            missing_data="*",
        )
        return T
示例#3
0
    def get_statistics(self, with_motif_probs=True, with_titles=True):
        """returns the parameter values as tables/dict

        Parameters
        ----------
        with_motif_probs
            include the motif probability table
        with_titles
            include a title for each table based on it's
            dimension

        """
        result = []
        group = {}
        param_names = self.get_param_names()

        mprob_name = [n for n in param_names if "mprob" in n]
        if mprob_name:
            mprob_name = mprob_name[0]
        else:
            mprob_name = ""

        if not with_motif_probs:
            param_names.remove(mprob_name)

        for param in param_names:
            dims = tuple(self.get_used_dimensions(param))
            if dims not in group:
                group[dims] = []
            group[dims].append(param)
        table_order = list(group.keys())
        table_order.sort()
        for table_dims in table_order:
            raw_table = self.get_param_value_dict(dimensions=table_dims,
                                                  params=group[table_dims])
            param_names = group[table_dims]
            param_names.sort()
            if table_dims == ("edge", ):
                if "length" in param_names:
                    param_names.remove("length")
                    param_names.insert(0, "length")
                raw_table["parent"] = dict([
                    (e.name, e.parent.name)
                    for e in self._tree.get_edge_vector() if not e.isroot()
                ])
                param_names.insert(0, "parent")
            list_table = []
            heading_names = list(table_dims) + param_names
            row_order = self._valuesForDimensions(table_dims)
            for scope in row_order:
                row = {}
                row_used = False
                for param in param_names:
                    d = raw_table[param]
                    try:
                        for part in scope:
                            d = d[part]
                    except KeyError:
                        d = "NA"
                    else:
                        row_used = True
                    row[param] = d
                if row_used:
                    row.update(dict(list(zip(table_dims, scope))))
                    row = [row[k] for k in heading_names]
                    list_table.append(row)
            if table_dims:
                title = ["", "%s params" % " ".join(table_dims)][with_titles]
                row_ids = True
            else:
                row_ids = False
                title = ["", "global params"][with_titles]

            stat_table = table.Table(
                heading_names,
                list_table,
                max_width=80,
                row_ids=row_ids,
                title=title,
                **self._format,
            )
            if group[table_dims] == [mprob_name]:
                # if stat_table.shape
                # if mprobs, we use the motifs as header
                motifs = list(sorted(set(stat_table.tolist("motif"))))
                if stat_table.shape[1] == 2:
                    motif_prob = dict(stat_table.tolist())
                    heading_names = motifs
                    list_table = [motif_prob[m] for m in motifs]
                    list_table = [list_table]
                elif stat_table.shape[1] == 3:
                    rows = []
                    other_col = [
                        c for c in stat_table.header
                        if "motif" not in c and "mprobs" not in c
                    ][0]
                    for val in stat_table.distinct_values(other_col):
                        subtable = stat_table.filtered(lambda x: x == val,
                                                       columns=other_col)
                        motif_prob = dict(
                            subtable.tolist([
                                c for c in stat_table.header if c != other_col
                            ]))
                        rows.append([val] + [motif_prob[m] for m in motifs])
                    heading_names = [other_col] + motifs
                    list_table = rows
                stat_table = table.Table(heading_names,
                                         list_table,
                                         max_width=80,
                                         title=title,
                                         **self._format)

            result.append(stat_table)
        return result