2dba63a8f84d28519bc078b4fff2967239377bec
[qpalma.git] / tools / data_tools / filterReads.c
1 ////////////////////////////////////////////////////////////////////////////////
2 // The purpose of this program is to read a gff and a
3 // solexa reads file and create a data set used by QPalma.
4 //
5 //
6 //
7 // Notes:
8 //
9 // - Both the read indices and the gff gene indices are one-based
10 //
11 //
12 ////////////////////////////////////////////////////////////////////////////////
13
14 #include <sys/mman.h>
15 #include <sys/stat.h>
16 #include <stdio.h>
17 #include <stdlib.h>
18 #include <assert.h>
19 #include <string.h>
20 #include <unistd.h>
21 #include <math.h>
22
23 #include "debug_tools.h"
24 #include "join.h"
25 #include "datastructures.h"
26
27 #define _FILE_OFFSET_BITS == 64
28
29 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";
30
31 const int read_size = 36;
32
33 const int min_overlap = 1;
34 const int max_overlap = 35;
35
36 unsigned long read_nr = 1;
37 unsigned long unfiltered_read_nr = 1;
38
39 int parse_gff(char* filename,FILE* fid,struct gene*** allGenes);
40 void sort_genes(struct gene*** allGenes, int numGenes);
41 void process_reads(FILE* reads_fs,struct gene*** allGenes,int numGenes, FILE* out_fs, FILE* unfiltered_out_fs);
42 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);
43 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);
44 int fitting(char* up_prb, char* down_prb, int u_off, int d_off, int u_size, int d_size);
45 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);
46
47 static char *info = "Usage is:\n./filterReads gff reads output";
48
49 void reverse_complement(char** seq, int len);
50
51 char current_strand;
52
53 /*
54 * Some constants specifying the exact behavior of the filter
55 *
56 */
57
58 //#define _FDEBUG_ 0
59 //#define DEBUG_READ 38603
60
61
62 //
63 /*
64 * TODO:
65 * - Check strand -> done simple (only if equal)
66 * - check for [AC] and similar entries -> done simple (see function
67 */
68
69 #ifdef _FDEBUG_
70 if(read_nr == DEBUG_READ) {
71 printf("read nr %lu",read_nr);
72 printf("pos: %d %d %d %d\n",p_start,exon_stop,exon_start,p_stop);
73 printf("u/d range: %d %d\n",up_range,down_range);
74 printf("u/d size: %d %d\n",u_size,d_size);
75 printf("u/d off: %d %d\n",u_off,d_off);
76 printf("add_pos/down_range-d_size: %d %d\n",additional_pos,(down_range - d_size));
77
78 printf("******************\n");
79
80 printf("%s\n",up_read->seq);
81 //printf("%s\n",new_up_seq);
82
83 printf("******************\n");
84
85 printf("%s\n",down_read->seq);
86 //printf("%s\n",new_down_seq);
87
88 printf("******************\n");
89 printf("%s\n",new_seq);
90 printf("%s\n",new_prb);
91 printf("%s\n",new_cal_prb);
92 printf("%s\n",new_chastity);
93 }
94 #endif // _FDEBUG_
95
96 int combined_reads = 0;
97
98 /*
99 *
100 *
101 */
102
103 void process_reads(FILE* reads_fs,struct gene*** allGenes,int numGenes, FILE* out_fs, FILE* unfiltered_out_fs) {
104 int status;
105
106 int buffer_size= 64;
107 int chr = 0;
108 int pos = 0;
109 char* seq = malloc(sizeof(char)*buffer_size);
110 unsigned long id = 0;
111 char strand = ' ';
112 int mismatch = 0;
113 int occurrence = 0;
114 int size = 0;
115 int cut = 0;
116 char* prb = malloc(sizeof(char)*buffer_size);
117 char* cal_prb = malloc(sizeof(char)*buffer_size);
118 char* chastity = malloc(sizeof(char)*buffer_size);
119
120 int reads_fid = fileno(reads_fs);
121 struct stat reads_stat;
122 if ( fstat(reads_fid,&reads_stat) == -1) {
123 perror("fstat");
124 exit(EXIT_FAILURE);
125 }
126 off_t reads_filesize = reads_stat.st_size;
127 printf("Reads file is of size %lu bytes\n",(unsigned long) reads_filesize);
128 //int numReads = reads_filesize / 178.0;
129
130 void *reads_area = mmap (NULL,reads_filesize,PROT_READ,MAP_PRIVATE,reads_fid,0);
131 if (reads_area == MAP_FAILED) {
132 perror("mmap");
133 exit(EXIT_FAILURE);
134 }
135 close(reads_fid);
136 printf("Successfully mapped %lu bytes of reads file into memory\n",(unsigned long)reads_filesize);
137
138 char* lineBeginPtr = (char*) reads_area;
139 char* lineEndPtr = (char*) reads_area;
140 char* end_of_mapped_area = ((char*) reads_area) + reads_filesize;
141
142 while (*lineEndPtr != '\n' && lineEndPtr != end_of_mapped_area) lineEndPtr++;
143 lineEndPtr++;
144
145 char* current_line = malloc(sizeof(char)*512);
146 memset(current_line,0,512);
147
148 int SIZE = 5000;
149 // initialize boundary arrays
150 Read** upstream_overlap = malloc(sizeof(Read*)*SIZE);
151 Read** downstream_overlap= malloc(sizeof(Read*)*SIZE);
152 int uov,dov;
153 uov = dov = 0;
154
155 int skippedLinesCounter = 0;
156
157 int prev_exon_start = -1;
158 int prev_exon_stop = -1;
159 int cur_exon_start = -1;
160
161 unsigned long line_size = lineEndPtr - lineBeginPtr;
162 //printf("distance is %lu\n",line_size);
163 strncpy(current_line,lineBeginPtr,line_size);
164 current_line[line_size] = '\0';
165 //printf("%s test",current_line);
166
167 int gene_idx = 0;
168 int exon_idx = 1;
169 struct gene* currentGene = (*allGenes)[gene_idx];
170 char* gene_id = currentGene->id;
171
172 int skippedReadCtr = 0;
173 int uselessReadCtr = 0;
174 int exonicReadCtr = 0;
175
176 int currentOverlapCtr = 0;
177 int previousOverlapCtr = 0;
178
179 int multioccurReadCtr = 0;
180
181 Read* currentRead;
182 int up_idx, down_idx;
183
184 int readCtr = 0;
185 int wrong_strand_ctr = 0;
186 int read_within_gene_ctr = 0;
187 int read_outside_gene_ctr = 0;
188 int read_start, read_stop;
189 // start of the parsing loop
190
191 while(1) {
192 if (gene_idx == numGenes || strcmp(current_line,"") == 0)
193 break;
194
195 gene_id = currentGene->id;
196
197 //if (readCtr != 0 && readCtr % 1000000 == 0) {
198 // printf("Processed %d/%d genes and %d/%d reads.\n",gene_idx,numGenes,readCtr,numReads);
199 // printf("%d reads in total while %d reads where skipped.\n",readCtr,skippedLinesCounter);
200 // printf("%d were totally intronic. %d were outside of genes and %d were occurring on several positions.\n",uselessReadCtr,skippedReadCtr,multioccurReadCtr);
201 // printf("%d reads were useless in total\n",uselessReadCtr+skippedReadCtr+multioccurReadCtr);
202 // printf("\t%d useless reads\n",uselessReadCtr);
203 // printf("\t%d skipped reads\n",skippedReadCtr);
204 // printf("\t%d multioccurring\n",multioccurReadCtr);
205 // printf("\t%d wrong_strand\n",wrong_strand_ctr);
206 // printf("%d within gene\n",read_within_gene_ctr);
207 // printf("%d outside gene\n",read_outside_gene_ctr);
208 // printf("%d reads were totally exonic\n",exonicReadCtr);
209 // printf("%d reads overlap with prev. exon. %d reads overlap with current exon\n",previousOverlapCtr,currentOverlapCtr);
210 // printf("%d reads where newly combined from two original reads\n",combined_reads);
211 // printf("Total used reads: %d (some may not be combined)\n",exonicReadCtr+combined_reads);
212 //}
213
214 // pos of the reads is one-based
215 status = sscanf(current_line,line_format,&chr,&pos,seq,&id,&strand,&mismatch,&occurrence,&size,&cut,prb,cal_prb,chastity);
216 if (status < 12) {
217 skippedLinesCounter++;
218 goto next_read;
219 }
220
221 // if the read is occurring several times elsewhere then get rid of it
222 if ( occurrence != 1 ) {
223 multioccurReadCtr++;
224 goto next_read;
225 }
226
227 //printf("before rc %s\n",seq);
228 if (current_strand == 'P')
229 reverse_complement(&seq,strlen(seq));
230 //printf("after rc %s\n",seq);
231
232 //printf("new read: %s at %d\n",seq,pos);
233
234 // define read start and stop positions
235 read_start = pos;
236 read_stop = pos + read_size-1;
237
238 FA(strlen(seq) >= read_size);
239
240 FA(currentGene != 0);
241
242 if ( currentGene->start <= read_start && read_stop <= currentGene->stop) { // read is within gene borders
243 read_within_gene_ctr++;
244
245 exon_label:
246
247 if (exon_idx == currentGene->num_exons) {
248 gene_idx++;
249 exon_idx = 1;
250 currentGene = (*allGenes)[gene_idx];
251 while( currentGene == 0 && gene_idx < numGenes) {
252 currentGene = (*allGenes)[gene_idx];
253 gene_idx++;
254 }
255 continue;
256 }
257
258 prev_exon_start = currentGene->exon_starts[exon_idx-1];
259 prev_exon_stop = currentGene->exon_stops[exon_idx-1];
260 cur_exon_start = currentGene->exon_starts[exon_idx];
261
262 //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);
263
264 if ( (cur_exon_start - prev_exon_stop - 1) < min_overlap || cur_exon_start < read_start ) { // go to next exon
265
266 if (uov != 0 && dov != 0)
267 combine_info(prev_exon_stop,cur_exon_start,upstream_overlap,uov,downstream_overlap,dov,out_fs,gene_id,exon_idx);
268
269 for(up_idx=0;up_idx<uov;up_idx++) {
270 free_read(upstream_overlap[up_idx]);
271 free(upstream_overlap[up_idx]);
272 }
273
274 for(down_idx=0;down_idx<dov;down_idx++) {
275 free_read(downstream_overlap[down_idx]);
276 free(downstream_overlap[down_idx]);
277 }
278
279 uov = dov = 0;
280
281 exon_idx++;
282 goto exon_label;
283 }
284
285 if ( prev_exon_start <= read_start && read_stop <= prev_exon_stop ) { // read is inside previous exon
286
287 // output of unused i.e. unspliced reads
288 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",
289 unfiltered_read_nr,chr,strand,seq,0,read_size,prb,cal_prb,chastity,gene_id,read_start,-1,-1,read_stop,-1);
290 unfiltered_read_nr++;
291
292 exonicReadCtr++;
293 goto next_read;
294 }
295
296 if ( read_stop < prev_exon_stop ) { // go to next read
297 //printf("%d\t%d\t%d\n",read_start,prev_exon_start,prev_exon_stop);
298 //if( exon_idx > 1) {
299 // printf("%d\t%d\n", currentGene->exon_starts[exon_idx-2], currentGene->exon_stops[exon_idx-2]);
300 // printf("---\n");
301 //}
302
303 uselessReadCtr++;
304 goto next_read;
305 }
306
307 // if this position is reached the read is somehow overlapping or
308 // exactly on the exon boundary.
309 if ( (prev_exon_stop - read_start + 1) >= min_overlap && (prev_exon_stop - read_start + 1) <= max_overlap ) { // read overlaps with previous exon boundary
310 //printf("%s\n",current_line);
311 previousOverlapCtr++;
312 currentRead = create_read(chr,read_start,seq,id,strand,mismatch,occurrence,size,cut,prb,cal_prb,chastity);
313 assert (uov < SIZE);
314 upstream_overlap[uov] = currentRead;
315 uov++;
316 goto next_read;
317 }
318
319 if ( ( read_stop - cur_exon_start + 1) >= min_overlap && (read_stop - cur_exon_start + 1) <= max_overlap ) { // read overlaps with current exon boundary
320 //printf("%s\n",current_line);
321 currentOverlapCtr++;
322 currentRead = create_read(chr,read_start,seq,id,strand,mismatch,occurrence,size,cut,prb,cal_prb,chastity);
323 assert (dov < SIZE);
324 downstream_overlap[dov] = currentRead;
325 dov++;
326 goto next_read;
327 }
328
329 uselessReadCtr++;
330 goto next_read; // read was not useful i.e. not overlapping/starting at exon boundaries
331
332 } else { // read is not within gene borders
333 read_outside_gene_ctr++;
334
335 if (uov != 0 && dov != 0)
336 combine_info(prev_exon_stop,cur_exon_start,upstream_overlap,uov,downstream_overlap,dov,out_fs,gene_id,exon_idx);
337
338 for(up_idx=0;up_idx<uov;up_idx++) {
339 free_read(upstream_overlap[up_idx]);
340 free(upstream_overlap[up_idx]);
341 }
342
343 for(down_idx=0;down_idx<dov;down_idx++) {
344 free_read(downstream_overlap[down_idx]);
345 free(downstream_overlap[down_idx]);
346 }
347
348 uov = dov = 0;
349
350 if ( currentGene->stop < read_stop ) { // go to next gene
351 gene_idx++;
352 exon_idx = 1;
353 currentGene = (*allGenes)[gene_idx];
354 while( currentGene == 0 && gene_idx < numGenes) {
355 currentGene = (*allGenes)[gene_idx];
356 gene_idx++;
357 }
358 //printf("currentGene->start / currentGene->stop %d/%d pos is %d\n",currentGene->start,currentGene->stop,pos);
359 continue;
360 }
361
362 if ( read_start < currentGene->start ) { // go to next read
363 skippedReadCtr++;
364
365 next_read:
366
367 lineBeginPtr = lineEndPtr;
368 while (*(char*)lineEndPtr != '\n' && lineEndPtr != end_of_mapped_area) lineEndPtr++;
369 lineEndPtr++;
370 readCtr += 1;
371 current_line = strncpy(current_line,lineBeginPtr,lineEndPtr-lineBeginPtr);
372 current_line[lineEndPtr-lineBeginPtr] = '\0';
373 continue;
374 }
375 }
376 }
377
378 //} // end of for all strands
379
380 if (uov != 0 && dov != 0)
381 combine_info(prev_exon_stop,cur_exon_start,upstream_overlap,uov,downstream_overlap,dov,out_fs,gene_id,exon_idx);
382
383 for(up_idx=0;up_idx<uov;up_idx++) {
384 free_read(upstream_overlap[up_idx]);
385 free(upstream_overlap[up_idx]);
386 }
387
388 for(down_idx=0;down_idx<dov;down_idx++) {
389 free_read(downstream_overlap[down_idx]);
390 free(downstream_overlap[down_idx]);
391 }
392
393 uov = dov = 0;
394
395 free(upstream_overlap);
396 free(downstream_overlap);
397
398 printf("Processed %d reads in total while %d reads where skipped.\n",readCtr,skippedLinesCounter);
399 printf("%d were totally intronic. %d were outside of genes and %d were occurring on several positions.\n",uselessReadCtr,skippedReadCtr,multioccurReadCtr);
400 printf("%d reads were useless in total\n",uselessReadCtr+skippedReadCtr+multioccurReadCtr);
401 printf("\t%d useless reads\n",uselessReadCtr);
402 printf("\t%d skipped reads\n",skippedReadCtr);
403 printf("\t%d multioccurring\n",multioccurReadCtr);
404 printf("\t%d wrong_strand\n",wrong_strand_ctr);
405 printf("%d reads were totally exonic\n",exonicReadCtr);
406 printf("%d reads overlap with prev. exon. %d reads overlap with current exon\n",previousOverlapCtr,currentOverlapCtr);
407 printf("%d reads where newly combined from two original reads\n",combined_reads);
408 printf("Total used reads: %d (some may not be combined)\n",exonicReadCtr+combined_reads);
409
410 status = munmap(reads_area,reads_filesize);
411 if(status != 0)
412 perror("munmap");
413
414 free(current_line);
415 free(seq);
416 free(prb);
417 free(cal_prb);
418 free(chastity);
419 }
420
421
422 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) {
423 int up_idx, down_idx, success;
424
425 char* up_used_flag = calloc(up_size,sizeof(char));
426 char* down_used_flag = calloc(down_size,sizeof(char));
427
428 Read* currentUpRead;
429 Read* currentDownRead;
430
431 for(up_idx=0;up_idx<up_size;up_idx++) {
432 if( up_used_flag[up_idx] == 1)
433 continue;
434
435 currentUpRead = upstream[up_idx];
436
437 for(down_idx=0;down_idx<down_size;down_idx++) {
438
439 if( up_used_flag[up_idx] == 1 || down_used_flag[down_idx] == 1)
440 continue;
441
442 currentDownRead = downstream[down_idx];
443
444 if(currentUpRead->strand != currentDownRead->strand)
445 continue;
446
447 success = join_reads(exon_stop,exon_start,currentUpRead,currentDownRead,out_fs,gene_id,exon_idx);
448
449 if(success == 1) {
450 up_used_flag[up_idx] = 1;
451 down_used_flag[down_idx] = 1;
452 }
453 }
454 }
455
456 free(up_used_flag);
457 free(down_used_flag);
458 }
459
460 /*
461 * Now we join the candidate reads wherever possible according to the following
462 * scheme:
463 *
464 * ACGTACGTCA GTXXXXXXXXAG ACGTAGACGT
465 * p1 e1 e2 p2
466 *
467 *
468 *
469 *
470 */
471
472 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) {
473 // range of possible sequence length on exon side
474 int up_read_start = up_read->pos;
475 //int up_read_stop = up_read->pos+read_size-1;
476
477 int down_read_start = down_read->pos;
478 int down_read_stop = down_read->pos+read_size-1;
479
480 int up_range = exon_stop - up_read_start + 1;
481 int down_range = down_read_stop - exon_start + 1;
482 int retval;
483
484 int u_size, d_size;
485 u_size = d_size = -1;
486
487 if(up_range+down_range < read_size)
488 return 0;
489
490 if (read_nr % 2 != 0) {
491 d_size = down_range;
492 u_size = read_size - d_size;
493 } else {
494 u_size = up_range;
495 d_size = read_size - u_size;
496 }
497
498 if( u_size > up_range || d_size > down_range)
499 return 0;
500
501 int p_start = exon_stop - u_size + 1;
502 int p_stop = exon_start + d_size - 1;
503
504 int u_off = p_start - up_read_start;
505 int d_off = exon_start - down_read_start;
506
507 FA(u_off >= 0 && d_off >= 0);
508 FA( exon_stop - p_start + p_stop - exon_start + 2 == read_size);
509 FA( u_size + d_size == read_size );
510
511 // seems reasonable up to here
512
513 int buf_size = 4*read_size;
514 char* new_seq = malloc(sizeof(char)*buf_size);
515 memset(new_seq,'z',sizeof(char)*buf_size);
516
517 if ( current_strand == 'P' ) {
518 printf("flipping read sequences...\n");
519 printf("%s %s\n",up_read->seq,down_read->seq);
520
521 char *tmp = malloc(sizeof(char)*strlen(up_read->seq+1));
522 strncpy(tmp,up_read->seq,strlen(up_read->seq));
523 tmp[strlen(up_read->seq)]='\0';
524 realloc(up_read->seq,sizeof(char)*strlen(down_read->seq+1));
525 strncpy(up_read->seq,down_read->seq,strlen(down_read->seq));
526 up_read->seq[strlen(down_read->seq)] = '\0';
527 realloc(down_read->seq,sizeof(char)*strlen(tmp));
528 strncpy(down_read->seq,tmp,strlen(tmp));
529 down_read->seq[strlen(tmp)] = '\0';
530
531 free(tmp);
532
533 printf("flipping done...\n");
534 printf("%s %s\n",up_read->seq,down_read->seq);
535 }
536
537 printf("start joining...\n");
538 Tuple jinfo = join_seq(new_seq,up_read->seq,u_off,u_size,down_read->seq,d_off,d_size,down_range);
539 printf("end of joining...\n");
540
541 int cut_pos = jinfo.first;
542 int additional_pos = jinfo.second;
543 printf("jinfo contains %d/%d\n",jinfo.first,jinfo.second);
544
545 buf_size = read_size+1+additional_pos;
546
547 printf("allocating quality arrays (size=%d)...\n",buf_size);
548 char* new_prb = malloc(sizeof(char)*buf_size);
549 char* new_cal_prb = malloc(sizeof(char)*buf_size);
550 char* new_chastity = malloc(sizeof(char)*buf_size);
551 if (new_prb == NULL || new_cal_prb == NULL || new_chastity == NULL)
552 perror("malloc\n");
553
554 if( jinfo.first == -1 ) {
555 retval = 0;
556 goto free;
557 }
558
559 if( additional_pos > (down_range - d_size) ) {
560 retval = 0;
561 goto free;
562 }
563
564 printf("joining qualities...\n");
565 strncpy(new_prb, up_read->prb+u_off, u_size);
566 strncpy(new_prb+u_size, down_read->prb+d_off, d_size+additional_pos);
567 new_prb[buf_size] = '\0';
568
569 strncpy(new_cal_prb, up_read->cal_prb+u_off, u_size);
570 strncpy(new_cal_prb+u_size, down_read->cal_prb+d_off, d_size+additional_pos);
571 new_cal_prb[buf_size] = '\0';
572
573 strncpy(new_chastity, up_read->chastity+u_off, u_size);
574 strncpy(new_chastity+u_size, down_read->chastity+d_off, d_size+additional_pos);
575 new_chastity[buf_size] = '\0';
576 printf("end of joining qualities...\n");
577
578 //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);
579 //printf("new read: %s %d %d\n",new_seq,cut_pos,u_size);
580
581 //if ( current_strand == 'P' ) {
582 // int alpha = read_size - u_off - 1;
583 // int beta = alpha - u_size ;
584 // p_start = up_read->pos + beta + 1;
585 // exon_stop = up_read->pos + alpha;
586
587 // alpha = read_size - d_off - 1;
588 // beta = alpha - (read_size - u_size);
589 // exon_start = down_read->pos + beta + 1;
590 // p_stop = down_read->pos + alpha;
591 //}
592
593 int status = 1; //fitting(up_read->prb,down_read->prb,u_off,d_off,u_size,d_size);
594
595 if(status != 1) {
596 retval = 0;
597 goto free;
598 }
599
600 retval = status;
601
602 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",
603 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);
604
605 read_nr++;
606 combined_reads++;
607
608 free:
609 free(new_seq);
610 free(new_prb);
611 free(new_cal_prb);
612 free(new_chastity);
613
614 return retval;
615 }
616
617 static char* translation = "T G C A [ ]";
618
619 void reverse_complement(char** seq, int len) {
620 int idx;
621 char *temp = malloc(sizeof(char)*len);
622 for(idx=0;idx<len;idx++)
623 temp[idx] = translation[(*seq)[idx]-65];
624
625 idx=0;
626 int temp_idx=len-1;
627 while(1) {
628 if (temp[temp_idx] == ']') {
629 (*seq)[idx] = '[';
630 (*seq)[idx+1] = temp[temp_idx-2];
631 (*seq)[idx+2] = temp[temp_idx-1];
632 (*seq)[idx+3] = ']';
633 idx += 4;
634 temp_idx -= 4;
635 } else {
636 (*seq)[idx] = temp[temp_idx];
637 idx++;
638 temp_idx--;
639 }
640
641 if (idx == len || temp_idx == -1)
642 break;
643 }
644 }
645
646
647 void print_read(Read* cRead) {
648 printf(line_format,
649 cRead->chr, cRead->pos, cRead->seq, cRead->id,
650 cRead->strand, cRead->mismatch, cRead->occurrence,
651 cRead->size, cRead->cut, cRead->prb, cRead->cal_prb,
652 cRead->chastity);
653 }
654
655
656 void open_file(const char* filename, const char* mode, FILE** fs) {
657 *fs = fopen(filename,mode);
658 if(*fs == NULL) {
659 printf("Error: Could not open file: %s",filename);
660 exit(EXIT_FAILURE);
661 }
662 }
663
664 /*
665 * The program expects 7 arguments, namely:
666 *
667 * - The strand to be used
668 * - The filename of the gff file
669 * - The filename of the reads file
670 * - The name of the spliced reads ouput file
671 * - The name of the unspliced reads ouput file
672 * - The offset for counting new spliced reads
673 * - The offset for counting new unspliced reads
674 *
675 */
676
677
678 int main(int argc, char* argv[]) {
679
680 if(argc != 8) {
681 printf("%s\n",info);
682 exit(EXIT_FAILURE);
683 }
684
685 current_strand = argv[1][0];
686 printf("Current strand is %c\n",current_strand);
687
688 int status;
689 int filenameSize = 256;
690 char* gff_filename = malloc(sizeof(char)*filenameSize);
691
692 strncpy(gff_filename,argv[2],filenameSize);
693
694 FILE *gff_fs;
695 FILE *reads_fs;
696 FILE *out_fs;
697 FILE *unfiltered_out_fs;
698
699 // open file streams for all needed input/output files
700 open_file(argv[2],"r",&gff_fs);
701 open_file(argv[3],"r",&reads_fs);
702 open_file(argv[4],"w",&out_fs);
703 open_file(argv[5],"w",&unfiltered_out_fs);
704
705 read_nr = strtoul(argv[6],NULL,10);
706 read_nr++;
707
708 unfiltered_read_nr = strtoul(argv[7],NULL,10);
709 unfiltered_read_nr++;
710
711 // allocate and load all genes and then close the gff file stream
712 struct gene** allGenes;
713 int numGenes = parse_gff(gff_filename,gff_fs,&allGenes);
714 status = fclose(gff_fs);
715 free(gff_filename);
716 if(status != 0)
717 printf("closing of gff filestream failed!\n");
718
719 printf("Successfully parsed gff file! Found %d genes.\n",numGenes);
720
721 // check if allGenes is sorted. if not throw away those genes that do not
722 // occur in the sorted order
723 int g_idx;
724 struct gene* currentGene = 0;
725 int nulled_genes=0;
726 int old_gene_stop = -1;
727 for(g_idx=0;g_idx<numGenes;g_idx++) {
728 currentGene = allGenes[g_idx];
729
730 if (! (currentGene->start < currentGene->stop))
731 printf("Invalid positions for gene %s!\n",currentGene->id);
732
733 if (! (old_gene_stop < currentGene->start ) ) {
734 printf("Gene %s is overlapping\n",currentGene->id);
735 old_gene_stop = currentGene->stop;
736 allGenes[g_idx] = 0;
737 nulled_genes++;
738 continue;
739 }
740 old_gene_stop = currentGene->stop;
741 }
742
743 printf("Found %d unordered genes.\n",nulled_genes);
744 int gidx, eidx;
745 int exon_cov = 0;
746 for(gidx=0;gidx<numGenes;gidx++) {
747 if (allGenes[gidx] == 0)
748 continue;
749
750 for(eidx=0;eidx<allGenes[gidx]->num_exons;eidx++) {
751 exon_cov += allGenes[gidx]->exon_stops[eidx] - allGenes[gidx]->exon_starts[eidx];
752 }}
753 printf("Exon coverage is %f\n",(double)exon_cov/30432563);
754
755 // now that we loaded all neccessary data we start to process the reads
756 process_reads(reads_fs,&allGenes,numGenes,out_fs,unfiltered_out_fs);
757
758 // free all allocated ressources
759 for(g_idx=0;g_idx<numGenes;g_idx++) {
760 if(allGenes[g_idx] != 0) {
761 free_gene(allGenes[g_idx]);
762 free(allGenes[g_idx]);
763 }
764 }
765 free(allGenes);
766
767 status = fclose(reads_fs);
768 status = fclose(out_fs);
769 if(status != 0)
770 perror("fclose");
771
772 return 0;
773 }