File: clistfnsmodule.c

package info (click to toggle)
python-biopython 1.42-2
  • links: PTS
  • area: main
  • in suites: etch, etch-m68k
  • size: 17,584 kB
  • ctags: 12,272
  • sloc: python: 80,461; xml: 13,834; ansic: 7,902; cpp: 1,855; sql: 1,144; makefile: 203
file content (161 lines) | stat: -rw-r--r-- 4,043 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
/* Copyright 2000 by Jeffrey Chang.  All rights reserved.
 * This code is part of the Biopython distribution and governed by its
 * license.  Please see the LICENSE file that should have been included
 * as part of this package.
 *
 * clistfnsmodule.c
 * Created 3 Jun 2000
 */

#include "Python.h"
#include <math.h>




/************************************** Exported Functions ***********/

static char clistfns_count__doc__[] = 
"count(items) -> dict of counts of each item\n\
\n\
Count the number of times each item appears in a list of data.\n\
\n\
";

static PyObject *clistfns_count(PyObject *self, PyObject *args)
{
    int i;
    PyObject *items, *counts;
    PyObject *item, *count, *newcount;
    long int current;

    if(!PyArg_ParseTuple(args, "O", &items))
	return NULL;
    if(!PySequence_Check(items)) {
	PyErr_SetString(PyExc_TypeError, "expected sequence type");
	return NULL;
    }

    if(!(counts = PyDict_New()))
	return NULL;
    
    /* Go through the loop, counting how often each item appears. */
    i = 0;
    while(1) {
	if(!(item = PySequence_GetItem(items, i)))  {
            PyErr_Clear(); /* clear the exception set by PySequence_GetItem */
	    break;         /* no more numbers */
	}

	if(!(count = PyDict_GetItem(counts, item))) {
	    newcount = PyInt_FromLong(1);  /* New item, set count to 1 */
	}
	else {
	    current = PyInt_AsLong(count);
	    newcount = PyInt_FromLong(current+1);
	}

	PyDict_SetItem(counts, item, newcount);
	Py_DECREF(newcount);
	Py_DECREF(item);
	if(PyErr_Occurred()) 
	    return NULL;

	i++;
    }
    
    return counts;
}


static char clistfns_contents__doc__[] = 
"contents(items) -> dict of item -> percentage\n\
\n\
Summarize the contents of the list in terms of the percentages of each\n\
item.  For example, if an item appears 3 times in a list with 10 items,\n\
it is in 0.3 of the list\n\
\n\
";

static PyObject *clistfns_contents(PyObject *self, PyObject *args)
{
    int i;
    PyObject *items, *counts, *percentages;
    PyObject *countitems, *countitem;
    PyObject *key, *count, *perc;
    long c;
    double total;

    if(!PyArg_ParseTuple(args, "O", &items))
	return NULL;
    if(!PySequence_Check(items)) {
	PyErr_SetString(PyExc_TypeError, "expected mapping type");
	return NULL;
    }
    if((total = PySequence_Length(items)) == -1) {
	PyErr_SetString(PyExc_ValueError, "I couldn't get length of item.");
	return NULL;
    }
    
    counts = clistfns_count(self, args);
    if(!counts || PyErr_Occurred())
	return NULL;

    if(!(percentages = PyDict_New())) {
	Py_DECREF(counts);
	return NULL;
    }

    /* Loop through every element in counts, calculating the probabilities. */
    if(!(countitems = PyMapping_Items(counts))) {
	Py_DECREF(counts);
        Py_DECREF(percentages);
	return NULL;
    }

    /* Go through the loop, counting how often each item appears. */
    i = 0;
    while(1) {
	if(!(countitem = PyList_GetItem(countitems, i))) {
	    PyErr_Clear(); /* clear the exception set by PyList_GetItem */
	    break;         /* no more numbers */
        }
	key = PyTuple_GetItem(countitem, 0);
	count = PyTuple_GetItem(countitem, 1);
	c = PyInt_AsLong(count);
	perc = PyFloat_FromDouble((double)c / total);
	PyDict_SetItem(percentages, key, perc);
	Py_DECREF(perc);
	if(PyErr_Occurred())   /* PyDict_SetItem failed */
	    break;
	i++;
    }
    if(PyErr_Occurred()) {
	Py_DECREF(percentages);
	percentages = NULL;
    }
    Py_DECREF(countitems);
    Py_DECREF(counts);
    
    return percentages;
}


/************************************** Module definition stuff ******/

static PyMethodDef clistfnsMethods[] = {
    {"count", clistfns_count, METH_VARARGS, clistfns_count__doc__},
    {"contents", clistfns_contents, METH_VARARGS, clistfns_contents__doc__},
    {NULL, NULL}
};

static char clistfns__doc__[] =
"This provides helper functions for the listfns module.\n\
You should never import this module on its own.\n\
\n\
";

void initclistfns(void)
{
    (void) Py_InitModule3("clistfns", clistfnsMethods, clistfns__doc__);
}