+ fixed a bug in the sscanf
authorfabio <fabio@e1793c9e-67f9-0310-80fc-b846ff1f7b36>
Thu, 15 May 2008 11:40:10 +0000 (11:40 +0000)
committerfabio <fabio@e1793c9e-67f9-0310-80fc-b846ff1f7b36>
Thu, 15 May 2008 11:40:10 +0000 (11:40 +0000)
git-svn-id: http://svn.tuebingen.mpg.de/ag-raetsch/projects/QPalma@9023 e1793c9e-67f9-0310-80fc-b846ff1f7b36

cparser/qparser.c

index 8ef9a84..07333a3 100644 (file)
@@ -4,7 +4,6 @@
 #include <sys/mman.h>
 #include <sys/stat.h>
 
-
 // the line format is defined as follows
 // id chr strand seq splitpos size q1 q2 q3 geneId p1 p2 p3 p4 true_cut
 const char* line_format = "%lu\t%d\t%c\t%s\t%d\t%d\t%s\t%s\t%s\t%s\t%d\t%d\t%d\t%d\t%d\n";
@@ -27,23 +26,26 @@ char* chastity = 0;
 
 char* geneId   = 0;
 
-static int set_item_from_line(PyObject *result_dict, const char* current_line) {
 
-   int status;
+static int set_item_from_line(PyObject *result_dict, const char* current_line) {
 
-   status = sscanf(current_line,line_format,&id,&chr,&strand,seq,&splitpos,&size,prb,cal_prb,chastity,geneId,p_start,exon_stop,exon_start,p_stop,&true_cut);
+   //printf("current line is %s\n",current_line);
+   int entries_found = sscanf(current_line,line_format,&id,&chr,&strand,seq,&splitpos,&size,prb,cal_prb,chastity,geneId,&p_start,&exon_stop,&exon_start,&p_stop,&true_cut);
 
-   if (status != 15) {
-      return -1;
+   if (entries_found != 15) {
+      return entries_found;
    }
 
+   //printf("after sscanf\n");
+   int status;
+
    // create dictionary representing one line
    PyObject* entry_dict = PyDict_New();
    PyObject *id_py = PyInt_FromLong(id);
    
    // add entries of that line
-   status = PyDict_SetItem(result_dict, PyString_FromString("id"),         id_py );
-   status = PyDict_SetItem(result_dict, PyString_FromString("chr"),        PyInt_FromLong(chr) );
+   status = PyDict_SetItem(entry_dict, PyString_FromString("id"),         id_py );
+   status = PyDict_SetItem(entry_dict, PyString_FromString("chr"),        PyInt_FromLong(chr) );
 
    PyObject *strand_py = PyString_FromString("--");
 
@@ -54,10 +56,10 @@ static int set_item_from_line(PyObject *result_dict, const char* current_line) {
    if ( strand == 'P' )
       strand_py = PyString_FromString("-");
 
-   status = status || PyDict_SetItem(result_dict, PyString_FromString("strand"),     strand_py );
-   status = PyDict_SetItem(result_dict, PyString_FromString("seq"),        PyString_FromString(seq) );
-   status = PyDict_SetItem(result_dict, PyString_FromString("splitpos"),   PyInt_FromLong(splitpos) );
-   status = PyDict_SetItem(result_dict, PyString_FromString("size"),       PyInt_FromLong(size) );
+   status = status || PyDict_SetItem(entry_dict, PyString_FromString("strand"),     strand_py );
+   status = PyDict_SetItem(entry_dict, PyString_FromString("seq"),        PyString_FromString(seq) );
+   status = PyDict_SetItem(entry_dict, PyString_FromString("splitpos"),   PyInt_FromLong(splitpos) );
+   status = PyDict_SetItem(entry_dict, PyString_FromString("size"),       PyInt_FromLong(size) );
 
    PyObject* prb_list = PyList_New(size);
    PyObject* cal_prb_list = PyList_New(size);
@@ -70,14 +72,14 @@ static int set_item_from_line(PyObject *result_dict, const char* current_line) {
       status = PyList_SetItem( chastity_list, idx, PyInt_FromLong(chastity[idx]+10) );
    }
 
-   status = PyDict_SetItem(result_dict, PyString_FromString("prb"),        prb_list );
-   status = PyDict_SetItem(result_dict, PyString_FromString("cal_prb"),    cal_prb_list );
-   status = PyDict_SetItem(result_dict, PyString_FromString("chastity"),   chastity_list );
-   //status = PyDict_SetItem(result_dict, FromString("gene_id"), id_py );
-   status = PyDict_SetItem(result_dict, PyString_FromString("p_start"),    PyInt_FromLong(p_start) );
-   status = PyDict_SetItem(result_dict, PyString_FromString("exon_stop"),  PyInt_FromLong(exon_stop) );
-   status = PyDict_SetItem(result_dict, PyString_FromString("exon_start"), PyInt_FromLong(exon_start) );
-   status = PyDict_SetItem(result_dict, PyString_FromString("p_stop"),     PyInt_FromLong(p_stop) );
+   status = PyDict_SetItem(entry_dict, PyString_FromString("prb"),        prb_list );
+   status = PyDict_SetItem(entry_dict, PyString_FromString("cal_prb"),    cal_prb_list );
+   status = PyDict_SetItem(entry_dict, PyString_FromString("chastity"),   chastity_list );
+   //status = PyDict_SetItem(entry_dict, FromString("gene_id"), id_py );
+   status = PyDict_SetItem(entry_dict, PyString_FromString("p_start"),    PyInt_FromLong(p_start) );
+   status = PyDict_SetItem(entry_dict, PyString_FromString("exon_stop"),  PyInt_FromLong(exon_stop) );
+   status = PyDict_SetItem(entry_dict, PyString_FromString("exon_start"), PyInt_FromLong(exon_start) );
+   status = PyDict_SetItem(entry_dict, PyString_FromString("p_stop"),     PyInt_FromLong(p_stop) );
 
    // now save the dictionary representing one line in the dictionary
    // representing the whole file
@@ -138,7 +140,7 @@ static PyObject * Py_parse_reads(PyObject *obj, PyObject *args) {
    char* end_of_mapped_area = ((char*) reads_area) + reads_filesize;
 
    while (*lineEndPtr != '\n' && lineEndPtr != end_of_mapped_area) lineEndPtr++;
-      lineEndPtr++;
+   lineEndPtr++;
          
    char* current_line = malloc(sizeof(char)*512);
    memset(current_line,0,512);
@@ -159,21 +161,29 @@ static PyObject * Py_parse_reads(PyObject *obj, PyObject *args) {
       if (strcmp(current_line,"") == 0) 
          break;
 
-      //printf("current line is %s",current_line);
-
       status = set_item_from_line(result_dict,current_line);
       if (status != 0 )
-         perror("Error while parsing line.");
+         printf("Error while parsing line (status=%d).",status);
 
       lineBeginPtr = lineEndPtr;                                                                                                                                                                                         
       while (*(char*)lineEndPtr != '\n' && lineEndPtr != end_of_mapped_area) lineEndPtr++;
-         lineEndPtr++;
+      lineEndPtr++;
 
       readCtr += 1;
       current_line = strncpy(current_line,lineBeginPtr,lineEndPtr-lineBeginPtr);
       current_line[lineEndPtr-lineBeginPtr] = '\0';
    }
 
+   // clean up
+   status = munmap(reads_area,reads_filesize);                                                                                                                                                                                 
+   if(status != 0)
+      perror("munmap");
+
+   free(seq);
+   free(prb);
+   free(cal_prb);
+   free(chastity);
+   free(geneId);
 
    //Py_ssize_t PyDict_Size( PyObject *p)
 
@@ -192,6 +202,7 @@ PyMODINIT_FUNC initqparser(void) {
    prb      = malloc(sizeof(char)*buffer_size);
    cal_prb  = malloc(sizeof(char)*buffer_size);
    chastity = malloc(sizeof(char)*buffer_size);
+   geneId   = malloc(sizeof(char)*buffer_size);
 }
 
 int main(int argc, char *argv[]) {