Ejemplo n.º 1
0
    def test_histtab(self):

        data = "aaaacbb"
        hist = tablelib.histtab(data)
        expected = tablelib.Table([
            {
                'item': 'a',
                'count': 4,
                'percent': 4 / 7.
            },
            {
                'item': 'b',
                'count': 2,
                'percent': 2 / 7.
            },
            {
                'item': 'c',
                'count': 1,
                'percent': 1 / 7.
            },
        ])
        self.assertEqual(hist, expected)

        data = tablelib.Table([['first', 'second', 'third'], ['a', 'b', 1],
                               ['a', 'b', 1], ['a', 'c', 2], ['c', 'a', 1],
                               ['a', 'b', 2]])
        hist = tablelib.histtab(data, cols=['first', 'second'])
        expected = tablelib.Table([
            {
                'count': 3,
                'second': 'b',
                'percent': 0.6,
                'first': 'a'
            },
            {
                'count': 1,
                'second': 'a',
                'percent': 0.2,
                'first': 'c'
            },
            {
                'count': 1,
                'second': 'c',
                'percent': 0.2,
                'first': 'a'
            },
        ])
        self.assertEqual(hist, expected)
Ejemplo n.º 2
0
    def test_remove_col(self):

        # parse list of dicts
        data = [{
            'a': 1,
            'b': 2,
            'c': 3
        }, {
            'a': 4,
            'b': 5,
            'c': 6
        }, {
            'a': 7,
            'b': 8,
            'c': 9
        }]
        expected = [
            {
                'a': 1,
                'b': 2
            },
            {
                'a': 4,
                'b': 5
            },
            {
                'a': 7,
                'b': 8
            },
        ]
        tab = tablelib.Table(data)
        tab.remove_col('c')
        self.assertEqual(tab, expected)

        expected = [
            {
                'a': 1
            },
            {
                'a': 4
            },
            {
                'a': 7
            },
        ]
        tab = tablelib.Table(data)
        tab.remove_col('b', 'c')
        self.assertEqual(tab, expected)
Ejemplo n.º 3
0
    def test_write(self):
        text = """\
##types:string	int	float	bool
name	num	real	truth
matt	-123	10.0	True
alex	456	2.5	False
mike	789	-30.0	False
"""

        text_no_comments = """\
name	num	real	truth
matt	-123	10.0	True
alex	456	2.5	False
mike	789	-30.0	False
"""

        # Write
        tab = tablelib.read_table(StringIO(text))
        out = StringIO()
        tab.write(out, comments=True)
        self.assertEqual(out.getvalue(), text)

        # Write no comments
        tab = tablelib.read_table(StringIO(text))
        out = StringIO()
        tab.write(out)
        self.assertEqual(out.getvalue(), text_no_comments)

        # Write string
        tab = tablelib.read_table(StringIO(text))
        out = StringIO()
        out.write(str(tab))
        self.assertEqual(out.getvalue(), text_no_comments)

        expected_repr = ("""\
name  num   real      truth  \n\
matt  -123   10.0000   True  \n\
alex   456    2.5000  False  \n\
mike   789  -30.0000  False  \n\
""")
        # Write repr
        tab = tablelib.read_table(StringIO(text))
        out = StringIO()
        out.write(repr(tab))
        self.assertEqual(out.getvalue(), expected_repr)

        # read with no header and write with header
        expected = ("""\
a	b	c
1	2	3
4	5	6
""")
        data = [
            [1, 2, 3],
            [4, 5, 6],
        ]
        tab = tablelib.Table(data, nheaders=0, headers=['a', 'b', 'c'])
        out = StringIO()
        tab.write(out, nheaders=1)
        self.assertEqual(out.getvalue(), expected)
Ejemplo n.º 4
0
def makeFamtab(parts, famid=0):
    """Make Family Table"""

    famtab = tablelib.Table(headers=["famid", "genes"])

    for i, part in enumerate(parts):
        famtab.add(famid=str(famid + i), genes=",".join(part))
    return famtab
Ejemplo n.º 5
0
def enrichItems(in_items, out_items, M=None, N=None, useq=True, extra=False):
    """Calculates enrichment for items within an in-set vs and out-set.
       Returns a sorted table.
    """
    # DEPRECATED
    # TODO: remove this function

    # count items
    counts = util.Dict(default=[0, 0])
    for item in in_items:
        counts[item][0] += 1
    for item in out_items:
        counts[item][1] += 1

    if N is None:
        N = len(in_items) + len(out_items)
    if M is None:
        M = len(in_items)

    tab = tablelib.Table(
        headers=["item", "in_count", "out_count", "pval", "pval_under"])

    # do hypergeometric
    for item, (a, b) in counts.iteritems():
        tab.add(item=item,
                in_count=a,
                out_count=b,
                pval=rhyper(a, a + b, M, N),
                pval_under=rhyper(a, a + b, M, N, 1))

    # add qvalues
    if useq:
        qval = qvalues(tab.cget("pval"))
        qval_under = qvalues(tab.cget("pval_under"))

        tab.add_col("qval", data=qval)
        tab.add_col("qval_under", data=qval_under)

    if extra:
        tab.add_col("in_size", data=[M] * len(tab))
        tab.add_col("out_size", data=[N - M] * len(tab))
        tab.add_col("item_ratio",
                    data=[
                        row["in_count"] /
                        float(row["in_count"] + row["out_count"])
                        for row in tab
                    ])
        tab.add_col("size_ratio", data=[M / float(N) for row in tab])
        tab.add_col(
            "fold",
            data=[row["item_ratio"] / row["size_ratio"] for row in tab])

    tab.sort(col='pval')
    return tab
Ejemplo n.º 6
0
    def getBebTable(self, lines=None):
        """Returns a table of BEB data"""

        return tablelib.Table(
            list(self.getBeb(lines)),
            headers=["pos", "aa", "prob", "omega", "omega_stdev"],
            types={
                "pos": int,
                "aa": str,
                "prob": float,
                "omega": float,
                "omega_stdev": float
            })
Ejemplo n.º 7
0
    def __init__(self, filename=None, datadir=None, olddatadir=None):
        if filename != None:
            self.famtab = tablelib.read_table(filename)
        else:
            self.famtab = tablelib.Table(headers=["famid", "genes"])

        self.famlookup = self.famtab.lookup("famid")
        self.genelookup = {}

        for fam in self:
            for gene in self.getGenes(fam["famid"]):
                self.genelookup[gene] = fam

        self.filename = filename
        self.datadir = datadir
        self.olddatadir = olddatadir
Ejemplo n.º 8
0
    def test_add_col(self):

        # parse list of dicts
        data = [{
            'a': 1,
            'b': 2,
            'c': 3
        }, {
            'a': 4,
            'b': 5,
            'c': 6
        }, {
            'a': 7,
            'b': 8,
            'c': 9
        }]
        expected = [
            {
                'a': 1,
                'c': 3,
                'b': 2,
                'd': True
            },
            {
                'a': 4,
                'c': 6,
                'b': 5,
                'd': False
            },
            {
                'a': 7,
                'c': 9,
                'b': 8,
                'd': False
            },
        ]
        tab = tablelib.Table(data)
        tab.add_col('d', bool, data=[True, False, False])
        self.assertEqual(tab, expected)
Ejemplo n.º 9
0
    def test_join(self):

        tab1 = tablelib.Table([{
            'a': 1,
            'b': 2,
            'c': 3
        }, {
            'a': 4,
            'b': 5,
            'c': 6
        }, {
            'a': 7,
            'b': 8,
            'c': 9
        }])
        tab2 = tablelib.Table([{
            'a': 1,
            'd': 2,
            'e': 3
        }, {
            'a': 4,
            'd': 5,
            'e': 6
        }, {
            'a': 7,
            'd': 18,
            'e': 19
        }])
        tab3 = tablelib.Table([
            {
                'a': 1,
                'b': 2,
                'c': 3,
                'd': 2,
                'e': 3
            },
            {
                'a': 4,
                'b': 5,
                'c': 6,
                'd': 5,
                'e': 6
            },
            {
                'a': 7,
                'b': 8,
                'c': 9,
                'd': 18,
                'e': 19
            },
        ])
        tab4 = tablelib.Table([
            {
                'a': 1,
                'b': 2,
                'c': 3,
                'd': 2,
                'e': 3
            },
            {
                'a': 4,
                'b': 5,
                'c': 6,
                'd': 5,
                'e': 6
            },
        ])

        # join by column name
        join = tablelib.join_tables((tab1, 'a', ['a', 'b', 'c']),
                                    (tab2, 'a', ['d', 'e']))
        self.assertEqual(join, tab3)

        # join by key function
        join = tablelib.join_tables((tab1, lambda x: x['a'], ['a', 'b', 'c']),
                                    (tab2, lambda x: x['a'], ['d', 'e']))
        self.assertEqual(join, tab3)

        # join by key function
        join = tablelib.join_tables(
            (tab1, lambda x: (x['a'], x['b']), ['a', 'b', 'c']),
            (tab2, lambda x: (x['a'], x['d']), ['d', 'e']))
        self.assertEqual(join, tab4)
Ejemplo n.º 10
0
    def test_add(self):

        # parse list of dicts
        data = [{
            'a': 1,
            'b': 2,
            'c': 3
        }, {
            'a': 4,
            'b': 5,
            'c': 6
        }, {
            'a': 7,
            'b': 8,
            'c': 9
        }]
        tab = tablelib.Table(data)
        tab.add(a=10, b=11, c=12)
        self.assertEqual(tab[-1], {'a': 10, 'b': 11, 'c': 12})

        tab.append({'a': 13, 'b': 14, 'c': 15})
        self.assertEqual(tab[-1], {'a': 13, 'b': 14, 'c': 15})

        expected = [
            {
                'a': 1,
                'c': 3,
                'b': 2
            },
            {
                'a': 4,
                'c': 6,
                'b': 5
            },
            {
                'a': 7,
                'c': 9,
                'b': 8
            },
            {
                'a': 10,
                'c': 12,
                'b': 11
            },
            {
                'a': 13,
                'c': 15,
                'b': 14
            },
            {
                'a': 1,
                'c': 3,
                'b': 2
            },
            {
                'a': 4,
                'c': 6,
                'b': 5
            },
        ]
        tab.extend([
            {
                'a': 1,
                'b': 2,
                'c': 3
            },
            {
                'a': 4,
                'b': 5,
                'c': 6
            },
        ])
        self.assertEqual(tab, expected)
Ejemplo n.º 11
0
    def test_table_data(self):

        expected_tab = [
            {
                'a': 1,
                'b': 2,
                'c': 3
            },
            {
                'a': 4,
                'b': 5,
                'c': 6
            },
        ]

        expected_tab2 = [
            {
                0: 1,
                1: 2,
                2: 3
            },
            {
                0: 4,
                1: 5,
                2: 6
            },
        ]

        # parse list of dicts
        data = [
            {
                'a': 1,
                'b': 2,
                'c': 3
            },
            {
                'a': 4,
                'b': 5,
                'c': 6
            },
        ]
        tab = tablelib.Table(data)
        self.assertEqual(tab, expected_tab)

        # parse list of lists with header
        data = [
            ['a', 'b', 'c'],
            [1, 2, 3],
            [4, 5, 6],
        ]
        tab = tablelib.Table(data)
        self.assertEqual(tab, expected_tab)

        # parse list of lists with separate header
        data = [
            [1, 2, 3],
            [4, 5, 6],
        ]
        tab = tablelib.Table(data, nheaders=0, headers=['a', 'b', 'c'])
        self.assertEqual(tab, expected_tab)

        # parse list of lists with no header
        data = [
            [1, 2, 3],
            [4, 5, 6],
        ]
        tab = tablelib.Table(data, nheaders=0)
        self.assertEqual(tab, expected_tab2)

        # parse list of lists with header but no data
        data = [
            ['a', 'b', 'c'],
        ]
        tab = tablelib.Table(data)
        self.assertEqual(tab, [])