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
|
// Copyright 2004 by Harry Zuzan. 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.
// This source code parses an Affymetrix cel file.
//
// Documentation to come.
//
#include "Python.h"
#include "Numeric/arrayobject.h"
using namespace std;
#include <string>
#include <deque>
static PyObject * cel_parse(PyObject * self, PyObject * args)
{
char * cel_file;
int len_cel_file;
if (!PyArg_ParseTuple(args, "s#", &cel_file, &len_cel_file)) return 0;
deque<string>lines;
string tmp_str = "";
for (int i=0; i<len_cel_file; i++)
{
char c = cel_file[i];
if ( c == '\n' )
{
lines.push_back(tmp_str);
tmp_str = "";
}
else if ( c != '\r' ) tmp_str = tmp_str + c;
}
string HEADER = "[HEADER]";
while (1)
{
string line = lines.front();
lines.pop_front();
if (line == HEADER) break;
}
size_t token_pos;
token_pos = lines.front().find("=") + 1;
int ncols = atoi(string(lines.front(),token_pos).c_str());
lines.pop_front();
token_pos = lines.front().find("=") + 1;
int nrows = atoi(string(lines.front(),token_pos).c_str());
lines.pop_front();
string INTENSITY = "[INTENSITY]";
while (lines.size())
{
string line = lines.front();
lines.pop_front();
if (line == INTENSITY) break;
}
lines.pop_front();
lines.pop_front();
int dims[2] = {nrows,ncols};
PyArrayObject * mean_py = (PyArrayObject *)
PyArray_FromDims(2, dims, PyArray_DOUBLE);
PyArrayObject * stdev_py = (PyArrayObject *)
PyArray_FromDims(2, dims, PyArray_DOUBLE);
PyArrayObject * npix_py = (PyArrayObject *)
PyArray_FromDims(2, dims, PyArray_LONG);
for (int i=0; i<nrows*ncols; i++)
{
string col_str = "";
string row_str = "";
string mean_str = "";
string stdev_str = "";
string npix_str = "";
string data_str = lines.front();
lines.pop_front();
unsigned int k = 0;
while ( data_str[k] == ' ' || data_str[k] == '\t') k++;
while ( data_str[k] != ' ' && data_str[k] != '\t')
col_str += data_str[k++];
while ( data_str[k] == ' ' || data_str[k] == '\t') k++;
while ( data_str[k] != ' ' && data_str[k] != '\t')
row_str += data_str[k++];
while ( data_str[k] == ' ' || data_str[k] == '\t') k++;
while ( data_str[k] != ' ' && data_str[k] != '\t')
mean_str += data_str[k++]; k++;
while ( data_str[k] == ' ' || data_str[k] == '\t') k++;
while ( data_str[k] != ' ' && data_str[k] != '\t')
stdev_str += data_str[k++];
while ( data_str[k] == ' ' || data_str[k] == '\t') k++;
while ( k < data_str.size()) npix_str += data_str[k++];
int row = atoi(row_str.c_str());
int col = atoi(col_str.c_str());
double mean = atof(mean_str.c_str());
double stdev = atof(stdev_str.c_str());
int npix = atoi(npix_str.c_str());
((double *)mean_py->data)[ncols*row + col] = mean;
((double *)stdev_py->data)[ncols*row + col] = stdev;
((long *)npix_py->data)[ncols*row + col] = npix;
}
PyObject * returnList = PyList_New(0);
PyList_Append( returnList, (PyObject *)mean_py);
PyList_Append( returnList, (PyObject *)stdev_py);
PyList_Append( returnList, (PyObject *)npix_py);
return returnList;
}
extern "C" {
static PyMethodDef celMethods[] = {
{"parse", cel_parse, METH_VARARGS },
{NULL, NULL}
};
void init_cel()
{
Py_InitModule("_cel", celMethods);
import_array(); // need to when using Numeric Python
}
};
|