Copying Bio-python to globplot to satisfy the dependency
[jabaws.git] / binaries / src / globplot / biopython-1.50 / Bio / clistfnsmodule.c
1 /* Copyright 2000 by Jeffrey Chang.  All rights reserved.
2  * This code is part of the Biopython distribution and governed by its
3  * license.  Please see the LICENSE file that should have been included
4  * as part of this package.
5  *
6  * clistfnsmodule.c
7  * Created 3 Jun 2000
8  */
9
10 #include "Python.h"
11 #include <math.h>
12
13
14
15
16 /************************************** Exported Functions ***********/
17
18 static char clistfns_count__doc__[] = 
19 "count(items) -> dict of counts of each item\n\
20 \n\
21 Count the number of times each item appears in a list of data.\n\
22 \n\
23 ";
24
25 static PyObject *clistfns_count(PyObject *self, PyObject *args)
26 {
27     int i;
28     PyObject *items, *counts;
29     PyObject *item, *count, *newcount;
30     long int current;
31
32     if(!PyArg_ParseTuple(args, "O", &items))
33         return NULL;
34     if(!PySequence_Check(items)) {
35         PyErr_SetString(PyExc_TypeError, "expected sequence type");
36         return NULL;
37     }
38
39     if(!(counts = PyDict_New()))
40         return NULL;
41     
42     /* Go through the loop, counting how often each item appears. */
43     i = 0;
44     while(1) {
45         if(!(item = PySequence_GetItem(items, i)))  {
46             PyErr_Clear(); /* clear the exception set by PySequence_GetItem */
47             break;         /* no more numbers */
48         }
49
50         if(!(count = PyDict_GetItem(counts, item))) {
51             newcount = PyInt_FromLong(1);  /* New item, set count to 1 */
52         }
53         else {
54             current = PyInt_AsLong(count);
55             newcount = PyInt_FromLong(current+1);
56         }
57
58         PyDict_SetItem(counts, item, newcount);
59         Py_DECREF(newcount);
60         Py_DECREF(item);
61         if(PyErr_Occurred()) 
62             return NULL;
63
64         i++;
65     }
66     
67     return counts;
68 }
69
70
71 static char clistfns_contents__doc__[] = 
72 "contents(items) -> dict of item -> percentage\n\
73 \n\
74 Summarize the contents of the list in terms of the percentages of each\n\
75 item.  For example, if an item appears 3 times in a list with 10 items,\n\
76 it is in 0.3 of the list\n\
77 \n\
78 ";
79
80 static PyObject *clistfns_contents(PyObject *self, PyObject *args)
81 {
82     int i;
83     PyObject *items, *counts, *percentages;
84     PyObject *countitems, *countitem;
85     PyObject *key, *count, *perc;
86     long c;
87     double total;
88
89     if(!PyArg_ParseTuple(args, "O", &items))
90         return NULL;
91     if(!PySequence_Check(items)) {
92         PyErr_SetString(PyExc_TypeError, "expected mapping type");
93         return NULL;
94     }
95     if((total = PySequence_Length(items)) == -1) {
96         PyErr_SetString(PyExc_ValueError, "I couldn't get length of item.");
97         return NULL;
98     }
99     
100     counts = clistfns_count(self, args);
101     if(!counts || PyErr_Occurred())
102         return NULL;
103
104     if(!(percentages = PyDict_New())) {
105         Py_DECREF(counts);
106         return NULL;
107     }
108
109     /* Loop through every element in counts, calculating the probabilities. */
110     if(!(countitems = PyMapping_Items(counts))) {
111         Py_DECREF(counts);
112         Py_DECREF(percentages);
113         return NULL;
114     }
115
116     /* Go through the loop, counting how often each item appears. */
117     i = 0;
118     while(1) {
119         if(!(countitem = PyList_GetItem(countitems, i))) {
120             PyErr_Clear(); /* clear the exception set by PyList_GetItem */
121             break;         /* no more numbers */
122         }
123         key = PyTuple_GetItem(countitem, 0);
124         count = PyTuple_GetItem(countitem, 1);
125         c = PyInt_AsLong(count);
126         perc = PyFloat_FromDouble((double)c / total);
127         PyDict_SetItem(percentages, key, perc);
128         Py_DECREF(perc);
129         if(PyErr_Occurred())   /* PyDict_SetItem failed */
130             break;
131         i++;
132     }
133     if(PyErr_Occurred()) {
134         Py_DECREF(percentages);
135         percentages = NULL;
136     }
137     Py_DECREF(countitems);
138     Py_DECREF(counts);
139     
140     return percentages;
141 }
142
143
144 /************************************** Module definition stuff ******/
145
146 static PyMethodDef clistfnsMethods[] = {
147     {"count", clistfns_count, METH_VARARGS, clistfns_count__doc__},
148     {"contents", clistfns_contents, METH_VARARGS, clistfns_contents__doc__},
149     {NULL, NULL}
150 };
151
152 static char clistfns__doc__[] =
153 "This provides helper functions for the listfns module.\n\
154 You should never import this module on its own.\n\
155 \n\
156 ";
157
158 void initclistfns(void)
159 {
160     (void) Py_InitModule3("clistfns", clistfnsMethods, clistfns__doc__);
161 }