+ fixed some issues with the splice site scores and ugly code fragments
[qpalma.git] / tools / data_tools / filterReads.c
index 49a0bf0..139c681 100644 (file)
-////////////////////////////////////////////////////////////
+////////////////////////////////////////////////////////////////////////////////
 // The purpose of this program is to read a gff and a    
 // solexa reads file and create a data set used by QPalma. 
 //
+// Notes:
 //
+// - Both the read indices and the gff gene indices are one-based
 //
-////////////////////////////////////////////////////////////
+////////////////////////////////////////////////////////////////////////////////
 
 #include <sys/mman.h>
 #include <sys/stat.h>
 #include <stdio.h>
 #include <stdlib.h>
+#include <assert.h>
 #include <string.h>
 #include <unistd.h>
 #include <math.h>
 
-#include "common.h"
+#include "debug_tools.h"
+#include "join.h"
 #include "datastructures.h"
 
-int parse_gff(char* filename,FILE* fid,struct gene*** allGenes);
+#define _FILE_OFFSET_BITS == 64
+
+const char* line_format = "%d\t%d\t%s\t%lu\t%c\t%d\t%d\t%d\t%d\t%s\t%s\t%s\n";
 
-void process_reads(FILE* reads_fs,struct gene*** allGenes,int numGenes,FILE* out_fs);
+const int read_size = 36;
 
-void combine_info(int exon_stop, int exon_start, void** upstream, int up_size, void** downstream, int down_size, FILE* out_fs);
+const int min_overlap = 1;
+const int max_overlap = 35;
 
-int fitting(char* up_prb, char* up_prb_end, char* down_prb, char* down_prb_end);
+unsigned long read_nr = 1;
+unsigned long unfiltered_read_nr = 1;
 
-void remove_ambiguities(char * old_seq, int old_seq_size, char* new_seq);
+int parse_gff(char* filename,FILE* fid,struct gene*** allGenes);
+void sort_genes(struct gene*** allGenes, int numGenes);
+void process_reads(FILE* reads_fs,struct gene*** allGenes,int numGenes, FILE* out_fs, FILE* unfiltered_out_fs);
+void combine_info(int exon_stop, int exon_start, Read** upstream, int up_size, Read** downstream, int down_size, FILE* out_fs,const char* gene_id, int exon_idx);
+int join_reads(int exon_stop, int exon_start, Read* up_read, Read* down_read, FILE* out_fs,const char* gene_id, int exon_idx);
+int fitting(char* up_prb, char* down_prb, int u_off, int d_off, int u_size, int d_size);
+Tuple join_seq(char* new_seq, char* up_seq,int u_off, int u_size, char* down_seq, int d_off, int d_size, int d_range);
 
 static char *info = "Usage is:\n./filterReads gff reads output";
 
-const int read_size = 36;
+void reverse_complement(char** seq, int len);
 
-int main(int argc, char* argv[]) {
+char current_strand;
 
-   if(argc != 4) {
-      printf("%s\n",info);
-      exit(EXIT_FAILURE);
-   }
+/*
+ * Some constants specifying the exact behavior of the filter
+ *
+ */
 
-   int status;
-   int filenameSize = 256;
-   char* gff_filename = malloc(sizeof(char)*filenameSize);
-   char* reads_filename = malloc(sizeof(char)*filenameSize);
-   char* output_filename = malloc(sizeof(char)*filenameSize);
+//#define _FDEBUG_ 0
+//#define DEBUG_READ 38603
 
-   strncpy(gff_filename,argv[1],filenameSize);
-   strncpy(reads_filename,argv[2],filenameSize);
-   strncpy(output_filename,argv[3],filenameSize);
+/*
+ * TODO:
+ * - Check strand -> done simple (only if equal)
+ * - check for [AC] and similar entries -> done simple (see function
+ */
 
-   FILE *gff_fs = fopen(gff_filename,"r");
-   FILE *reads_fs = fopen(reads_filename,"r");
-   FILE *out_fs = fopen(output_filename,"w");
+#ifdef _FDEBUG_
+   if(read_nr == DEBUG_READ) {
+      printf("read nr %lu",read_nr);
+      printf("pos: %d %d %d %d\n",p_start,exon_stop,exon_start,p_stop);
+      printf("u/d range: %d %d\n",up_range,down_range);
+      printf("u/d size: %d %d\n",u_size,d_size);
+      printf("u/d off: %d %d\n",u_off,d_off);
+      printf("add_pos/down_range-d_size: %d %d\n",additional_pos,(down_range - d_size));
 
-   if(gff_fs == NULL) {
-      printf("Error: Could not open file: %s",gff_filename);
-      exit(EXIT_FAILURE);
-   }
+      printf("******************\n");
 
-   if(reads_fs == NULL) {
-      printf("Error: Could not open file: %s",reads_filename);
-      exit(EXIT_FAILURE);
-   }
+      printf("%s\n",up_read->seq);
+      //printf("%s\n",new_up_seq);
 
-   if(out_fs == NULL) {
-      printf("Error: Could not open file: %s",output_filename);
-      exit(EXIT_FAILURE);
-   }
+      printf("******************\n");
 
-   struct gene** allGenes;
-   int numGenes = parse_gff(gff_filename,gff_fs,&allGenes);
-   status = fclose(gff_fs);
-   free(gff_filename);
-   if(status != 0)
-      printf("closing of gff filestream failed!\n");
+      printf("%s\n",down_read->seq);
+      //printf("%s\n",new_down_seq);
 
-   printf("Successfully parsed gff file! Found %d genes.\n",numGenes);
+      printf("******************\n");
+      printf("%s\n",new_seq);
+      printf("%s\n",new_prb);
+      printf("%s\n",new_cal_prb);
+      printf("%s\n",new_chastity);
+   }
+#endif // _FDEBUG_
 
-   process_reads(reads_fs,&allGenes,numGenes,out_fs);
-   status = fclose(reads_fs);
-   status = fclose(out_fs);
-   if(status != 0)
-      perror("fclose");
-      
-   free(reads_filename);
-   free(output_filename);
-   return 0;
-}
+int combined_reads = 0;
+
+/*
+ *
+ *
+ */
 
-void process_reads(FILE* reads_fs,struct gene*** allGenes,int numGenes, FILE* out_fs) {
+void process_reads(FILE* reads_fs,struct gene*** allGenes,int numGenes, FILE* out_fs, FILE* unfiltered_out_fs) {
    int status;
 
    int buffer_size= 64;
@@ -112,9 +120,9 @@ void process_reads(FILE* reads_fs,struct gene*** allGenes,int numGenes, FILE* ou
    }
    off_t reads_filesize = reads_stat.st_size;
    printf("Reads file is of size %lu bytes\n",(unsigned long) reads_filesize);
-   int numReads = reads_filesize / 178.0;
+   //int numReads = reads_filesize / 178.0;
 
-   void *reads_area = mmap (NULL,reads_filesize,PROT_READ|PROT_WRITE,MAP_PRIVATE,reads_fid,0);
+   void *reads_area = mmap (NULL,reads_filesize,PROT_READ,MAP_PRIVATE,reads_fid,0);
    if (reads_area == MAP_FAILED) {
       perror("mmap");
       exit(EXIT_FAILURE);
@@ -122,17 +130,22 @@ void process_reads(FILE* reads_fs,struct gene*** allGenes,int numGenes, FILE* ou
    close(reads_fid);
    printf("Successfully mapped %lu bytes of reads file into memory\n",(unsigned long)reads_filesize);
 
-   void* linePtr = reads_area;
-   char* current_line = malloc(sizeof(char)*256);
+   char* lineBeginPtr = (char*) reads_area;
+   char* lineEndPtr = (char*) reads_area;
+   char* end_of_mapped_area = ((char*) reads_area) + reads_filesize;
+
+   while (*lineEndPtr != '\n' && lineEndPtr != end_of_mapped_area) lineEndPtr++;
+   lineEndPtr++;
+
+   char* current_line = malloc(sizeof(char)*512);
+   memset(current_line,0,512);
 
-   int SIZE = 500;
+   int SIZE = 5000;
    // initialize boundary arrays
-   void** upstream_end      = malloc(sizeof(void*)*SIZE);
-   void** upstream_overlap  = malloc(sizeof(void*)*SIZE);
-   void** downstream_start  = malloc(sizeof(void*)*SIZE);
-   void** downstream_overlap= malloc(sizeof(void*)*SIZE);
-   int ue,uo,ds,dov;
-   ue = uo = ds = dov = 0;
+   Read** upstream_overlap  = malloc(sizeof(Read*)*SIZE);
+   Read** downstream_overlap= malloc(sizeof(Read*)*SIZE);
+   int uov,dov;
+   uov = dov = 0;
 
    int skippedLinesCounter = 0;
 
@@ -140,370 +153,622 @@ void process_reads(FILE* reads_fs,struct gene*** allGenes,int numGenes, FILE* ou
    int prev_exon_stop = -1;
    int cur_exon_start = -1;
 
-   current_line = strncpy(current_line,linePtr,256);
+   unsigned long line_size = lineEndPtr - lineBeginPtr;
+   //printf("distance is %lu\n",line_size);
+   strncpy(current_line,lineBeginPtr,line_size);
+   current_line[line_size] = '\0';
+   //printf("%s test",current_line);
+
    int gene_idx = 0;
    int exon_idx = 1;
    struct gene* currentGene = (*allGenes)[gene_idx];
+   char* gene_id = currentGene->id;
 
-   char* disamb_seq = malloc(sizeof(char)*read_size);
-
-   int readCtr = 0;
-   // start of the parsing loop 
-   while(1) {
-      if (gene_idx == numGenes || strcmp(current_line,"") == 0)
-         break;
-
-      if (readCtr != 0 && readCtr % 1000000 == 0)
-         printf("Processed %d reads. Processed %d/%d genes and %d/%d reads.\n",readCtr,gene_idx,numGenes,readCtr,numReads);
-
-      //if (gene_idx >= 1833)
-      //   printf("currentGene start/stop: %d/%d. Positions is %d\n",currentGene->start,currentGene->stop,pos);
-      //if (readCtr == 2000000)
-      //   exit(EXIT_SUCCESS);
-
-      status = sscanf(current_line,"%d\t%d\t%s\t%lu\t%c\t%d\t%d\t%d\t%d\t%s\t%s\t%s\n",
-      &chr,&pos,seq,&id,&strand,&mismatch,&occurrence,&size,&cut,prb,cal_prb,chastity);
-      if (status < 12) {
-         skippedLinesCounter++;
-      }
-      //printf("gene start/stop %d,%d, pos: %d\n",currentGene->start,currentGene->stop,pos);
-      // if the read is occurring several times elsewhere then get rid of it 
-      if(occurrence != 1) {
-         while (*(char*)linePtr != '\n') linePtr++;
-         linePtr++;
-         readCtr += 1;
-         current_line = strncpy(current_line,linePtr,256);
-         continue;
-      }
-
-      if (!(currentGene->start <= pos && (pos + read_size-1) <= currentGene->stop)) { // read is not within gene borders
-
-         if ( currentGene->stop < (pos + read_size-1) ) { // go to next gene
-            gene_idx++;
-            exon_idx = 1;
-            currentGene = (*allGenes)[gene_idx];
-            //printf("currentGene->start / currentGene->stop %d/%d pos is %d\n",currentGene->start,currentGene->stop,pos);
-            ue = uo = ds = dov = 0;
-            continue;
-         }
-
-         if ( pos < currentGene->start ) { // go to next read
-            next_read:
-
-            while (*(char*)linePtr != '\n') linePtr++;
-            linePtr++;
-            readCtr += 1;
-            current_line = strncpy(current_line,linePtr,256);
-            continue;
-         }
-
-      } else { // read IS within gene borders
-
-         exon_label:
-
-         if (exon_idx == currentGene->num_exons) {
-            gene_idx++;
-            exon_idx = 1;
-            currentGene = (*allGenes)[gene_idx];
-            continue;
-         }
-
-         prev_exon_start = currentGene->exon_starts[exon_idx-1];
-         prev_exon_stop = currentGene->exon_stops[exon_idx-1];
-         cur_exon_start = currentGene->exon_starts[exon_idx];
-
-         //printf("exon %d %d inton til %d pos %d\n",prev_exon_start,prev_exon_stop,cur_exon_start,pos);
-            
-         if (cur_exon_start - prev_exon_stop < 6 || cur_exon_start < pos ) { // go to next exon
-            exon_idx++;
+   int skippedReadCtr = 0;
+   int uselessReadCtr = 0;
+   int exonicReadCtr = 0;
 
-            if (ue != 0 && dov != 0)
-               combine_info(prev_exon_stop,cur_exon_start,upstream_end,ue,downstream_overlap,dov,out_fs);
+   int currentOverlapCtr = 0;
+   int previousOverlapCtr = 0;
 
-            if (uo != 0 && ds != 0) 
-               combine_info(prev_exon_stop,cur_exon_start,upstream_overlap,uo,downstream_start,ds,out_fs);
+   int multioccurReadCtr = 0;
 
-            ue = uo = ds = dov = 0;
-            goto exon_label;
-         }
+   Read* currentRead;
+   int up_idx, down_idx;
 
-         if ( prev_exon_start < pos && (pos+read_size) < prev_exon_stop ) { // read is inside previous exon
-            remove_ambiguities(seq,strlen(seq),disamb_seq);
-            fprintf(out_fs,"%d\t%c\t%s\t%d\t%s\t%s\t%s\n",chr,strand,disamb_seq,read_size,prb,cal_prb,chastity);
+   int readCtr = 0;
+   int wrong_strand_ctr = 0;
+   int read_within_gene_ctr = 0;
+   int read_outside_gene_ctr = 0;
+   int read_start, read_stop;
+   // start of the parsing loop 
+  
+      while(1) {
+         if (gene_idx == numGenes || strcmp(current_line,"") == 0)
+            break;
+
+         gene_id = currentGene->id;
+
+         //if (readCtr != 0 && readCtr % 1000000 == 0) {
+         //   printf("Processed %d/%d genes and %d/%d reads.\n",gene_idx,numGenes,readCtr,numReads);
+         //   printf("%d reads in total while %d reads where skipped.\n",readCtr,skippedLinesCounter);
+         //   printf("%d were totally intronic. %d were outside of genes and %d were occurring on several positions.\n",uselessReadCtr,skippedReadCtr,multioccurReadCtr);
+         //   printf("%d reads were useless in total\n",uselessReadCtr+skippedReadCtr+multioccurReadCtr);
+         //   printf("\t%d useless reads\n",uselessReadCtr);
+         //   printf("\t%d skipped reads\n",skippedReadCtr);
+         //   printf("\t%d multioccurring\n",multioccurReadCtr);
+         //   printf("\t%d wrong_strand\n",wrong_strand_ctr);
+         //   printf("%d within gene\n",read_within_gene_ctr);
+         //   printf("%d outside gene\n",read_outside_gene_ctr);
+         //   printf("%d reads were totally exonic\n",exonicReadCtr);
+         //   printf("%d reads overlap with prev. exon. %d reads overlap with current exon\n",previousOverlapCtr,currentOverlapCtr);
+         //   printf("%d reads where newly combined from two original reads\n",combined_reads);
+         //   printf("Total used reads: %d (some may not be combined)\n",exonicReadCtr+combined_reads);
+         //}
+
+         // pos of the reads is one-based
+         status = sscanf(current_line,line_format,&chr,&pos,seq,&id,&strand,&mismatch,&occurrence,&size,&cut,prb,cal_prb,chastity);
+         if (status < 12) {
+            skippedLinesCounter++;
             goto next_read;
          }
 
-         if ( pos + (read_size-1) < prev_exon_stop ) // go to next read
-            goto next_read;
-
-         // if this position is reached the read is somehow overlapping or
-         // exactly on the exon boundary. now determine where exactly:
-         if (pos + (read_size-1) == prev_exon_stop) {  // read ends at previous exon end
-            upstream_end[ue] = linePtr;
-            ue++;
+         // if the read is occurring several times elsewhere then get rid of it 
+         if ( occurrence != 1 ) {
+            multioccurReadCtr++;
             goto next_read;
          }
 
-         if ( (prev_exon_stop - pos) >= 6 && (prev_exon_stop - pos) <= 30) { // read overlaps with previous exon boundary
-            upstream_overlap[uo] = linePtr;
-            uo++;
-            goto next_read;
+         //printf("before rc %s\n",seq);
+         if (current_strand == 'P')
+            reverse_complement(&seq,strlen(seq));
+         //printf("after rc %s\n",seq);
+
+         //printf("new read: %s at %d\n",seq,pos);
+   
+         // define read start and stop positions
+         read_start = pos;
+         read_stop  = pos + read_size-1;
+
+         FA(strlen(seq) >= read_size);
+
+         FA(currentGene != 0);
+
+         if ( currentGene->start <= read_start && read_stop <= currentGene->stop) { // read is within gene borders
+            read_within_gene_ctr++;
+
+            exon_label:
+
+            if (exon_idx == currentGene->num_exons) {
+               gene_idx++;
+               exon_idx = 1;
+               currentGene = (*allGenes)[gene_idx];
+               while( currentGene == 0 && gene_idx < numGenes) {
+                  currentGene = (*allGenes)[gene_idx];
+                  gene_idx++;
+               }
+               continue;
+            }
+
+            prev_exon_start = currentGene->exon_starts[exon_idx-1];
+            prev_exon_stop = currentGene->exon_stops[exon_idx-1];
+            cur_exon_start = currentGene->exon_starts[exon_idx];
+
+            //printf("id: %s,exon_idx: %d intron: %d %d read start/stop: %d / %d\n",currentGene->id,exon_idx,prev_exon_stop,cur_exon_start,read_start,read_stop);
+
+            if ( (cur_exon_start - prev_exon_stop - 1) < min_overlap || cur_exon_start < read_start ) { // go to next exon
+
+               if (uov != 0 && dov != 0)
+                  combine_info(prev_exon_stop,cur_exon_start,upstream_overlap,uov,downstream_overlap,dov,out_fs,gene_id,exon_idx);
+
+               for(up_idx=0;up_idx<uov;up_idx++) {
+                  free_read(upstream_overlap[up_idx]);
+                  free(upstream_overlap[up_idx]);
+               }
+
+               for(down_idx=0;down_idx<dov;down_idx++) {
+                  free_read(downstream_overlap[down_idx]);
+                  free(downstream_overlap[down_idx]);
+               }
+
+               uov = dov = 0;
+
+               exon_idx++;
+               goto exon_label;
+            }
+
+            if ( prev_exon_start <= read_start && read_stop <= prev_exon_stop ) { // read is inside previous exon
+
+               // output of unused i.e. unspliced reads
+               fprintf(unfiltered_out_fs,"%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",
+               unfiltered_read_nr,chr,strand,seq,0,read_size,prb,cal_prb,chastity,gene_id,read_start,-1,-1,read_stop,-1);
+               unfiltered_read_nr++;
+               
+               exonicReadCtr++;
+               goto next_read;
+            }
+
+            if ( read_stop < prev_exon_stop ) { // go to next read 
+               //printf("%d\t%d\t%d\n",read_start,prev_exon_start,prev_exon_stop);
+               //if( exon_idx > 1) {
+               //   printf("%d\t%d\n", currentGene->exon_starts[exon_idx-2], currentGene->exon_stops[exon_idx-2]);
+               //   printf("---\n");
+               //}
+
+               uselessReadCtr++;
+               goto next_read;
+            }
+
+            // if this position is reached the read is somehow overlapping or
+            // exactly on the exon boundary.
+            if ( (prev_exon_stop - read_start + 1) >= min_overlap && (prev_exon_stop - read_start + 1) <= max_overlap ) { // read overlaps with previous exon boundary
+               //printf("%s\n",current_line);
+               previousOverlapCtr++;
+               currentRead = create_read(chr,read_start,seq,id,strand,mismatch,occurrence,size,cut,prb,cal_prb,chastity);
+               assert (uov < SIZE);
+               upstream_overlap[uov] = currentRead;
+               uov++;
+               goto next_read;
+            }
+         
+            if ( ( read_stop - cur_exon_start + 1) >= min_overlap && (read_stop - cur_exon_start + 1) <= max_overlap ) { // read overlaps with current exon boundary
+               //printf("%s\n",current_line);
+               currentOverlapCtr++;
+               currentRead = create_read(chr,read_start,seq,id,strand,mismatch,occurrence,size,cut,prb,cal_prb,chastity);
+               assert (dov < SIZE);
+               downstream_overlap[dov] = currentRead;
+               dov++;
+               goto next_read;
+            }
+
+            uselessReadCtr++;
+            goto next_read; // read was not useful i.e. not overlapping/starting at exon boundaries
+
+         } else { // read is not within gene borders
+            read_outside_gene_ctr++;
+
+            if (uov != 0 && dov != 0)
+               combine_info(prev_exon_stop,cur_exon_start,upstream_overlap,uov,downstream_overlap,dov,out_fs,gene_id,exon_idx);
+
+            for(up_idx=0;up_idx<uov;up_idx++) {
+               free_read(upstream_overlap[up_idx]);
+               free(upstream_overlap[up_idx]);
+            }
+
+            for(down_idx=0;down_idx<dov;down_idx++) {
+               free_read(downstream_overlap[down_idx]);
+               free(downstream_overlap[down_idx]);
+            }
+
+            uov = dov = 0;
+
+            if ( currentGene->stop < read_stop ) { // go to next gene
+               gene_idx++;
+               exon_idx = 1;
+               currentGene = (*allGenes)[gene_idx];
+               while( currentGene == 0 && gene_idx < numGenes) {
+                  currentGene = (*allGenes)[gene_idx];
+                  gene_idx++;
+               }
+               //printf("currentGene->start / currentGene->stop %d/%d pos is %d\n",currentGene->start,currentGene->stop,pos);
+               continue;
+            }
+
+            if ( read_start < currentGene->start ) { // go to next read
+               skippedReadCtr++;
+               
+               next_read:
+               
+               lineBeginPtr = lineEndPtr;
+               while (*(char*)lineEndPtr != '\n' && lineEndPtr != end_of_mapped_area) lineEndPtr++;
+               lineEndPtr++;
+               readCtr += 1;
+               current_line = strncpy(current_line,lineBeginPtr,lineEndPtr-lineBeginPtr);
+               current_line[lineEndPtr-lineBeginPtr] = '\0';
+               continue;
+            }
          }
+      }
 
-         if ( pos == cur_exon_start ) { // read starts at current exon start
-            downstream_start[ds] = linePtr;
-            ds++;
-            goto next_read;
-         }
+   //} // end of for all strands
 
-         if ( (cur_exon_start - pos) >= 6 && (cur_exon_start - pos) <= 30 ) { // read overlaps with current exon boundary
-            downstream_overlap[dov] = linePtr;
-            dov++;
-            goto next_read;
-         }
+   if (uov != 0 && dov != 0)
+      combine_info(prev_exon_stop,cur_exon_start,upstream_overlap,uov,downstream_overlap,dov,out_fs,gene_id,exon_idx);
 
-         goto next_read; // read was not useful i.e. not overlapping/starting at exon boundaries
-      }
+   for(up_idx=0;up_idx<uov;up_idx++) {
+      free_read(upstream_overlap[up_idx]);
+      free(upstream_overlap[up_idx]);
+   }
+
+   for(down_idx=0;down_idx<dov;down_idx++) {
+      free_read(downstream_overlap[down_idx]);
+      free(downstream_overlap[down_idx]);
    }
 
-   combine_info(prev_exon_stop,cur_exon_start,upstream_end,ue,downstream_overlap,dov,out_fs);
-   combine_info(prev_exon_stop,cur_exon_start,upstream_overlap,uo,downstream_start,ds,out_fs);
+   uov = dov = 0;
 
-   free(upstream_end);
    free(upstream_overlap);
-   free(downstream_start);
    free(downstream_overlap);
 
    printf("Processed %d reads in total while %d reads where skipped.\n",readCtr,skippedLinesCounter);
+   printf("%d were totally intronic. %d were outside of genes and %d were occurring on several positions.\n",uselessReadCtr,skippedReadCtr,multioccurReadCtr);
+   printf("%d reads were useless in total\n",uselessReadCtr+skippedReadCtr+multioccurReadCtr);
+   printf("\t%d useless reads\n",uselessReadCtr);
+   printf("\t%d skipped reads\n",skippedReadCtr);
+   printf("\t%d multioccurring\n",multioccurReadCtr);
+   printf("\t%d wrong_strand\n",wrong_strand_ctr);
+   printf("%d reads were totally exonic\n",exonicReadCtr);
+   printf("%d reads overlap with prev. exon. %d reads overlap with current exon\n",previousOverlapCtr,currentOverlapCtr);
+   printf("%d reads where newly combined from two original reads\n",combined_reads);
+   printf("Total used reads: %d (some may not be combined)\n",exonicReadCtr+combined_reads);
 
    status = munmap(reads_area,reads_filesize);
    if(status != 0)
       perror("munmap");
 
-   //free(current_line);
+   free(current_line);
    free(seq);
    free(prb);
    free(cal_prb);
    free(chastity);
 }
 
-void combine_info(int exon_stop, int exon_start, void** upstream, int up_size, void** downstream, int down_size,FILE* out_fs) {
-   int up_idx, down_idx, status;
-   char* upstream_line = malloc(sizeof(char)*256);
-   char* downstream_line = malloc(sizeof(char)*256);
 
-   int buffer_size= 64;
+void combine_info(int exon_stop, int exon_start, Read** upstream, int up_size, Read** downstream, int down_size, FILE* out_fs,const char* gene_id, int exon_idx) {
+   int up_idx, down_idx, success;
 
-   int up_chr        = 0;
-   int up_pos        = 0;
-   char* up_seq      = malloc(sizeof(char)*buffer_size);
-   int up_id         = 0;
-   char up_strand    = ' ';
-   int up_mismatch   = 0;
-   int up_occurrence = 0;
-   int up_sz       = 0;
-   int up_cut        = 0;
-   char* up_prb      = malloc(sizeof(char)*buffer_size);
-   char* up_cal_prb  = malloc(sizeof(char)*buffer_size);
-   char* up_chastity = malloc(sizeof(char)*buffer_size);
-
-   int down_chr        = 0;
-   int down_pos        = 0;
-   char* down_seq      = malloc(sizeof(char)*buffer_size);
-   int down_id         = 0;
-   char down_strand    = ' ';
-   int down_mismatch   = 0;
-   int down_occurrence = 0;
-   int down_sz         = 0;
-   int down_cut        = 0;
-   char* down_prb      = malloc(sizeof(char)*buffer_size);
-   char* down_cal_prb  = malloc(sizeof(char)*buffer_size);
-   char* down_chastity = malloc(sizeof(char)*buffer_size);
-
-   int new_chr        = 0;
-   char* new_seq      = malloc(sizeof(char)*buffer_size);
-   char new_strand    = ' ';
-   char* new_prb        = malloc(sizeof(char)*buffer_size);
-   char* new_cal_prb    = malloc(sizeof(char)*buffer_size);
-   char* new_chastity   = malloc(sizeof(char)*buffer_size);
-   char* new_up_seq = malloc(sizeof(char)*read_size);
-   char* new_down_seq = malloc(sizeof(char)*read_size); 
-
-   up_idx=0;
-   down_idx=0;
-   while(1) {
-      if (up_idx == up_size || down_idx == down_size || up_strand != down_strand)
-         break;
+   char* up_used_flag = calloc(up_size,sizeof(char));
+   char* down_used_flag = calloc(down_size,sizeof(char));
 
-      strncpy(upstream_line,upstream[up_idx],256);
-      status = sscanf(upstream_line,"%d\t%d\t%s\t%d\t%c\t%d\t%d\t%d\t%d\t%s\t%s\t%s\n",
-      &up_chr,&up_pos,up_seq,&up_id,&up_strand,&up_mismatch,&up_occurrence,&up_sz,
-      &up_cut,up_prb,up_cal_prb,up_chastity);
-      
-      strncpy(downstream_line,downstream[down_idx],256);
-      status = sscanf(downstream_line,"%d\t%d\t%s\t%d\t%c\t%d\t%d\t%d\t%d\t%s\t%s\t%s\n",
-      &down_chr,&down_pos,down_seq,&down_id,&down_strand,&down_mismatch,&down_occurrence,&down_sz,
-      &down_cut,down_prb,down_cal_prb,down_chastity);
-
-      remove_ambiguities(up_seq,strlen(up_seq),new_up_seq);
-      remove_ambiguities(down_seq,strlen(down_seq),new_down_seq);
-
-      new_seq[0] = '\0';
-      new_prb[0] = '\0';
-      new_cal_prb[0] = '\0';
-      new_chastity[0] = '\0';
-         
-      int fit;
-      int w_size = 6;
-      int overlap = 0;
-      if (up_pos+35 == exon_stop) { // merge with read which is downstream overlapping
-         overlap = exon_start - down_pos;
+   Read* currentUpRead;
+   Read* currentDownRead;
+
+   for(up_idx=0;up_idx<up_size;up_idx++) {
+      if( up_used_flag[up_idx] == 1)
+         continue;
+
+      currentUpRead = upstream[up_idx];
+
+      for(down_idx=0;down_idx<down_size;down_idx++) {
+
+         if( up_used_flag[up_idx] == 1 || down_used_flag[down_idx] == 1)
+            continue;
+
+         currentDownRead = downstream[down_idx];
+
+         if(currentUpRead->strand != currentDownRead->strand)
+            continue;
+
+         success = join_reads(exon_stop,exon_start,currentUpRead,currentDownRead,out_fs,gene_id,exon_idx);
          
-         fit = fitting(up_prb+(36-w_size),up_prb+36,down_prb+overlap,down_prb+overlap+w_size);
-         if (fit != 1)
-            goto end;
+         if(success == 1) {
+            up_used_flag[up_idx] = 1;
+            down_used_flag[down_idx] = 1;
+         }
+      }
+   }
+
+   exit(0);
+   free(up_used_flag);
+   free(down_used_flag);
+}
+
+/*
+ * Now we join the candidate reads wherever possible according to the following
+ * scheme:
+ *
+ * ACGTACGTCA GTXXXXXXXXAG ACGTAGACGT
+ * p1       e1             e2       p2  
+ *
+ * 
+ *
+ *
+ */
+
+int join_reads(int exon_stop, int exon_start, Read* up_read, Read* down_read, FILE* out_fs,const char* gene_id, int exon_idx) {
+   // range of possible sequence length on exon side
+   int up_read_start = up_read->pos;
+   //int up_read_stop  = up_read->pos+read_size-1;
+
+   int down_read_start = down_read->pos;
+   int down_read_stop  = down_read->pos+read_size-1;
+
+   int up_range   = exon_stop - up_read_start + 1;
+   int down_range = down_read_stop - exon_start + 1;
+   int retval;
+
+   int u_size, d_size;
+   u_size = d_size = -1;
+
+   if(up_range+down_range < read_size)
+      return 0;
+
+   if (read_nr % 2 != 0) {
+      d_size = down_range;
+      u_size = read_size - d_size;
+   } else {
+      u_size = up_range;
+      d_size = read_size - u_size;
+   }
+
+   if( u_size > up_range || d_size > down_range)
+      return 0;
+
+   int p_start  = exon_stop  - u_size + 1;
+   int p_stop   = exon_start + d_size - 1;
 
-         new_chr     = up_chr;
-         new_strand  = up_strand;
+   int u_off = p_start - up_read_start;
+   int d_off = exon_start - down_read_start;
 
-         strncat(new_seq,new_up_seq+(36-overlap),overlap);
-         strncat(new_prb,up_prb+(36-overlap),overlap);
-         strncat(new_cal_prb,up_cal_prb+(36-overlap),overlap);
-         strncat(new_chastity,up_chastity+(36-overlap),overlap);
+   FA( u_off >= 0 && d_off >= 0 );
+   FA( exon_stop - p_start + p_stop - exon_start + 2 == read_size);
+   FA( u_size + d_size == read_size );
 
-         strncat(new_seq,new_down_seq+overlap,36-overlap);
-         strncat(new_prb,down_prb+overlap,36-overlap);
-         strncat(new_cal_prb,down_cal_prb+overlap,36-overlap);
-         strncat(new_chastity,down_chastity+overlap,36-overlap);
+   // seems reasonable up to here
 
-         //printf("Between exon stop/start %d/%d : merging pos %d %d with overlap %d\n",exon_stop,exon_start,up_pos+35,down_pos, overlap);
+   int buf_size = 4*read_size;
+   char* new_seq = malloc(sizeof(char)*buf_size);
+   memset(new_seq,'z',sizeof(char)*buf_size);
 
-      } // merge with read which is upstream overlapping
+   if ( current_strand == 'P' ) {
+      printf("flipping read sequences...\n");
+      printf("%s %s\n",up_read->seq,down_read->seq);
+      char *tmp = malloc(sizeof(char)*(strlen(up_read->seq)+1));
+      strncpy(tmp,up_read->seq,strlen(up_read->seq));
+      tmp[strlen(up_read->seq)]='\0';
+
+      realloc(up_read->seq,sizeof(char)*(strlen(down_read->seq)+1));
+      strncpy(up_read->seq,down_read->seq,strlen(down_read->seq));
+      up_read->seq[strlen(down_read->seq)] = '\0';
+
+      if (up_read->seq == NULL)
+         perror("realloc\n");
+
+      realloc(down_read->seq,sizeof(char)*strlen(tmp)+1);
+      strncpy(down_read->seq,tmp,strlen(tmp));
+      down_read->seq[strlen(tmp)] = '\0';
       
-      if (down_pos == exon_start) {
-         overlap = up_pos+36 - exon_stop;
-         //printf("overlap is %d\n",overlap);
-         //printf("pos are: %d %d\n",up_pos,down_pos);
-         fit = fitting(up_prb+36-overlap-w_size,up_prb+36-overlap,down_prb,down_prb+w_size);
-         if (fit != 1)
-            goto end;
-
-         new_chr     = up_chr;
-         new_strand  = up_strand;
-
-         strncat(new_seq,new_up_seq,(36-overlap));
-         strncat(new_prb,up_prb,(36-overlap));
-         strncat(new_cal_prb,up_cal_prb,(36-overlap));
-         strncat(new_chastity,up_chastity,(36-overlap));
-
-         strncat(new_seq,new_down_seq,overlap);
-         strncat(new_prb,down_prb,overlap);
-         strncat(new_cal_prb,down_cal_prb,overlap);
-         strncat(new_chastity,down_chastity,overlap);
-
-         //printf("Between exon stop/start %d/%d : merging pos %d %d with overlap %d\n",exon_stop,exon_start,up_pos,down_pos, overlap);
-      }
+      if (down_read->seq == NULL)
+         perror("realloc\n");
+      
+      free(tmp);
+      printf("flipping done...\n");
+      printf("%s %s\n",up_read->seq,down_read->seq);
+   }
+   
+   printf("start joining...\n");
+   Tuple jinfo = join_seq(new_seq,up_read->seq,u_off,u_size,down_read->seq,d_off,d_size,down_range);
+   printf("end of joining...\n");
 
-      if ( !(up_pos+35 == exon_stop) && !(down_pos == exon_start) )
-         printf("ERROR: Between exon stop/start %d/%d : merging pos %d %d with overlap %d\n",exon_stop,exon_start,up_pos,down_pos, overlap);
+   printf("new_seq is %s\n",new_seq);
 
-      fprintf(out_fs,"%d\t%c\t%s\t%d\t%s\t%s\t%s\n",
-      new_chr,new_strand,new_seq,read_size,new_prb,new_cal_prb,new_chastity);
-    
-      end:
 
-      up_idx++;
-      down_idx++;
+   int cut_pos = jinfo.first;
+   int additional_pos = jinfo.second;
+   printf("jinfo contains %d/%d\n",jinfo.first,jinfo.second);
+
+   buf_size = read_size+1+additional_pos;
+
+   printf("allocating quality arrays (size=%d)...\n",buf_size);
+   char* new_prb        = malloc(sizeof(char)*buf_size);
+   char* new_cal_prb    = malloc(sizeof(char)*buf_size);
+   char* new_chastity   = malloc(sizeof(char)*buf_size);
+
+   if (new_prb == NULL || new_cal_prb == NULL || new_chastity == NULL)
+      perror("malloc\n");
+   
+   if( jinfo.first == -1 || (additional_pos > (down_range - d_size)) ) {
+      retval = 0;
+      goto free;
    }
 
-   free(upstream_line);
-   free(downstream_line);
-  
-   free(new_up_seq);
-   free(new_down_seq); 
+   printf("joining qualities...\n");
+   strncpy(new_prb, up_read->prb+u_off, u_size);
+   strncpy(new_prb+u_size, down_read->prb+d_off, d_size+additional_pos);
+   new_prb[buf_size-1] = '\0';
+
+   strncpy(new_cal_prb, up_read->cal_prb+u_off, u_size);
+   strncpy(new_cal_prb+u_size, down_read->cal_prb+d_off, d_size+additional_pos);
+   new_cal_prb[buf_size-1] = '\0';
+
+   strncpy(new_chastity, up_read->chastity+u_off, u_size);
+   strncpy(new_chastity+u_size, down_read->chastity+d_off, d_size+additional_pos);
+   new_chastity[buf_size-1] = '\0';
+   printf("end of joining qualities...\n");
+
+   //printf("old reads: %s %s (%d %d %d/%d)\n",up_read->seq,down_read->seq,up_read->pos,down_read->pos,u_off,d_off);
+   //printf("new read: %s %d %d\n",new_seq,cut_pos,u_size);
+   //if ( current_strand == 'P' ) {
+   //   int alpha   = read_size - u_off - 1;
+   //   int beta    = alpha - u_size ;
+   //   p_start     = up_read->pos + beta + 1;
+   //   exon_stop   = up_read->pos + alpha;
+   //   alpha   = read_size - d_off - 1;
+   //   beta    = alpha - (read_size - u_size);
+   //   exon_start  = down_read->pos + beta + 1; 
+   //   p_stop      = down_read->pos + alpha;
+   //}
+
+   int status = 1; //fitting(up_read->prb,down_read->prb,u_off,d_off,u_size,d_size);
+
+   if(status != 1) {
+      retval = 0;
+      goto free;
+   }
+
+   retval = status;
 
-   free(up_prb);
-   free(up_cal_prb);
-   free(up_chastity);
+   fprintf(out_fs,"%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",
+      read_nr,up_read->chr,up_read->strand,new_seq,cut_pos,read_size,new_prb,new_cal_prb,new_chastity,gene_id,p_start,exon_stop,exon_start,p_stop,u_size);
 
-   free(down_prb);
-   free(down_cal_prb);
-   free(down_chastity);
+   read_nr++;
+   combined_reads++;
 
+   free:
+   free(new_seq);
    free(new_prb);
    free(new_cal_prb);
    free(new_chastity);
 
+   return retval;
 }
 
-int fitting(char* up_prb, char* up_prb_end, char* down_prb, char* down_prb_end) {
-   double epsilon_mean = 15.0;
-   double epsilon_var = 10.0;
-   double mean_up = 0;
-   double variance_up = 0;
-   double mean_down = 0;
-   double variance_down = 0;
-
-   char *up_ptr = up_prb;
-   char *down_ptr = down_prb;
-
-   int w_size = 0;
-   while(up_ptr != up_prb_end) {
-      mean_up += (*up_ptr)-50;
-      mean_down += (*down_ptr)-50;
-      w_size++;
-      up_ptr++;
-      down_ptr++;
-   }
-   mean_up /= w_size;
-   mean_down /= w_size;
-
-
-   up_ptr = up_prb;
-   down_ptr = down_prb;
-   w_size = 0;
-   while(up_ptr != up_prb_end) {
-      variance_up += pow((*up_prb)-50 - mean_up,2);
-      variance_down += pow((*down_prb)-50 - mean_down,2);
-      w_size++;
-      up_ptr++;
-      down_ptr++;
+static char* translation = "T G   C            A      [ ]";
+
+void reverse_complement(char** seq, int len) {
+   int idx;
+   char *temp = malloc(sizeof(char)*len);
+   for(idx=0;idx<len;idx++)
+      temp[idx] = translation[(*seq)[idx]-65];
+
+   idx=0;
+   int temp_idx=len-1;
+   while(1) {
+      if (temp[temp_idx] == ']') {
+         (*seq)[idx] = '[';
+         (*seq)[idx+1] = temp[temp_idx-2];
+         (*seq)[idx+2] = temp[temp_idx-1];
+         (*seq)[idx+3] = ']';
+         idx      += 4;
+         temp_idx -= 4;
+      } else {
+         (*seq)[idx] = temp[temp_idx];
+         idx++;
+         temp_idx--;
+      }
+
+      if (idx == len || temp_idx == -1)
+         break;
    }
-   variance_up /= (w_size-1);
-   variance_down /= (w_size-1);
+   free(temp);
+}
 
-   //printf("means: %f %f, variances: %f %f\n",mean_up,mean_down,variance_up,variance_down);
 
-   if ( abs(mean_up - mean_down) < epsilon_mean && abs(variance_up - variance_down) < epsilon_var )
-      return 1;
-      
-   return 0;
+void print_read(Read* cRead) {
+   printf(line_format,
+   cRead->chr, cRead->pos, cRead->seq, cRead->id,
+   cRead->strand, cRead->mismatch, cRead->occurrence,
+   cRead->size, cRead->cut, cRead->prb, cRead->cal_prb,
+   cRead->chastity);
 }
 
-void remove_ambiguities(char * old_seq, int old_seq_size, char* new_seq) {
-   //printf("old seq: %s\n",old_seq);
-   //printf("new seq: %s\n",new_seq);
-
-   int idx=0;
-   int new_idx = 0;
-   while(idx<old_seq_size) {
-      if (old_seq[idx] == '[') {
-         new_seq[new_idx] = old_seq[++idx];
-         new_idx++;
-         idx += 3;
-         continue;
-      }
 
-      new_seq[new_idx] = old_seq[idx++];
-      new_idx++;
+void open_file(const char* filename, const char* mode, FILE** fs) {
+   *fs = fopen(filename,mode);
+   if(*fs == NULL) {
+      printf("Error: Could not open file: %s",filename);
+      exit(EXIT_FAILURE);
    }
-   //printf("old seq: %s\n",old_seq);
-   //printf("new seq: %s\n",new_seq);
 }
 
 /*
- * TODO:
- * - Check strand -> done simple (only if equal)
- * - check for [AC] and similar entries -> done simple (see function
- *   remove_ambiguities (exchanges [XY] by the first entry)
+ * The program expects 7 arguments, namely:
+ *
+ * - The strand to be used
+ * - The filename of the gff file
+ * - The filename of the reads file
+ * - The name of the spliced reads ouput file
+ * - The name of the unspliced reads ouput file
+ * - The offset for counting new spliced reads
+ * - The offset for counting new unspliced reads
+ *
  */
+
+int main(int argc, char* argv[]) {
+
+   if(argc != 8) {
+      printf("%s\n",info);
+      exit(EXIT_FAILURE);
+   }
+
+   current_strand = argv[1][0];
+   printf("Current strand is %c\n",current_strand);
+
+   int status;
+   int filenameSize = 256;
+   char* gff_filename = malloc(sizeof(char)*filenameSize);
+
+   strncpy(gff_filename,argv[2],filenameSize);
+
+   FILE *gff_fs;
+   FILE *reads_fs;
+   FILE *out_fs;
+   FILE *unfiltered_out_fs;
+
+   // open file streams for all needed input/output files
+   open_file(argv[2],"r",&gff_fs);
+   open_file(argv[3],"r",&reads_fs);
+   open_file(argv[4],"w",&out_fs);
+   open_file(argv[5],"w",&unfiltered_out_fs);
+
+   read_nr = strtoul(argv[6],NULL,10);
+   read_nr++;
+
+   unfiltered_read_nr = strtoul(argv[7],NULL,10);
+   unfiltered_read_nr++;
+
+   // allocate and load all genes and then close the gff file stream
+   struct gene** allGenes;
+   int numGenes = parse_gff(gff_filename,gff_fs,&allGenes);
+   status = fclose(gff_fs);
+   free(gff_filename);
+   if(status != 0)
+      printf("closing of gff filestream failed!\n");
+
+   printf("Successfully parsed gff file! Found %d genes.\n",numGenes);
+
+   // check if allGenes is sorted. if not throw away those genes that do not
+   // occur in the sorted order
+   int g_idx;
+   struct gene* currentGene = 0;
+   int nulled_genes=0;
+   int old_gene_stop = -1;
+   for(g_idx=0;g_idx<numGenes;g_idx++) {
+      currentGene = allGenes[g_idx];
+
+      if (! (currentGene->start < currentGene->stop))
+         printf("Invalid positions for gene %s!\n",currentGene->id);
+
+      if (! (old_gene_stop <  currentGene->start ) ) {
+         printf("Gene %s is overlapping\n",currentGene->id);
+         old_gene_stop = currentGene->stop;
+         free_gene(allGenes[g_idx]);
+         free(allGenes[g_idx]);
+         allGenes[g_idx] = 0;
+         nulled_genes++;
+         continue;
+      }
+      old_gene_stop = currentGene->stop;
+   }
+
+   printf("Found %d unordered genes.\n",nulled_genes);
+   int gidx, eidx;
+   int exon_cov = 0;
+   for(gidx=0;gidx<numGenes;gidx++) {
+      if (allGenes[gidx] == 0)
+         continue;
+
+      for(eidx=0;eidx<allGenes[gidx]->num_exons;eidx++) {
+         exon_cov += allGenes[gidx]->exon_stops[eidx] - allGenes[gidx]->exon_starts[eidx];
+   }}
+   printf("Exon coverage is %f\n",(double)exon_cov/30432563);
+
+   // now that we loaded all neccessary data we start to process the reads
+   process_reads(reads_fs,&allGenes,numGenes,out_fs,unfiltered_out_fs);
+
+   // free all allocated ressources
+   for(g_idx=0;g_idx<numGenes;g_idx++) {
+      if(allGenes[g_idx] != 0) {
+         free_gene(allGenes[g_idx]);
+         free(allGenes[g_idx]);
+      }
+   }
+   free(allGenes);
+
+   status = fclose(reads_fs);
+   status = fclose(out_fs);
+   if(status != 0)
+      perror("fclose");
+      
+   return 0;
+}