1 /* SuperRead pipeline
2 * Copyright (C) 2012 Genome group at University of Maryland.
3 *
4 * This program is free software: you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License as
6 * published by the Free Software Foundation, either version 3 of the
7 * License, or (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful, but
10 * WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program. If not, see <http://www.gnu.org/licenses/>.
16 */
17
18
19 #include <stdio.h>
20 #include <stdlib.h>
21
22 struct goodPairInfoStruct {
23 unsigned int pairNumber;
24 int read1;
25 int read2;
26 short offset;
27 unsigned short overlap;
28 };
29
30 /* We discuss the field extraElimCode in the following structure here.
31 0x01: kill to begin of lower-numbered read of pairNumber1
32 0x02: kill to end of lower-numbered read of pairNumber1
33 0x04: kill to begin of higher-numbered read of pairNumber1
34 0x08: kill to end of higher-numbered read of pairNumber1
35 0x10: kill to begin of lower-numbered read of pairNumber2
36 0x20: kill to end of lower-numbered read of pairNumber2
37 0x40: kill to begin of higher-numbered read of pairNumber2
38 0x80: kill to end of higher-numbered read of pairNumber2
39 */
40 struct pairsToCheckStruct
41 {
42 unsigned int pairNumber1;
43 unsigned int pairNumber2;
44 int read1;
45 int read2;
46 short offset;
47 unsigned char pairGroup;
48 /* The following keeps track of if we need to kill to begin or
49 end of any read. See above */
50 unsigned char extraElimCode;
51 };
52
53 struct readIntervalStruct {
54 int readNo;
55 unsigned short begin;
56 unsigned short end;
57 };
58
59 struct failingPairStruct {
60 unsigned int pairNumber;
61 unsigned char extraKillCode;
62 };
63
Fopen(const char * fn,const char * mode)64 FILE *Fopen (const char *fn, const char *mode)
65 {
66 FILE *result;
67 result = fopen (fn, mode);
68 if (result == NULL)
69 {
70 fprintf (stderr, "Couldn't open file '%s' for ", fn);
71 switch (mode[0]) {
72 case 'r': fprintf (stderr, "reading"); break;
73 case 'w': fprintf (stderr, "writing"); break;
74 case 'a': fprintf (stderr, "appending"); break;
75 default: fprintf (stderr, "unknown operation code '%c'", mode[0]);
76 break;
77 }
78 fprintf (stderr, ". Bye!\n");
79 exit (-1);
80 }
81
82 return (result);
83 }
84
Popen(const char * fn,const char * mode)85 FILE *Popen (const char *fn, const char *mode)
86 {
87 FILE *result;
88 result = popen (fn, mode);
89 if (result == NULL)
90 {
91 fprintf (stderr, "Couldn't open file '%s' for ", fn);
92 switch (mode[0]) {
93 case 'r': fprintf (stderr, "reading"); break;
94 case 'w': fprintf (stderr, "writing"); break;
95 case 'a': fprintf (stderr, "appending"); break;
96 default: fprintf (stderr, "unknown operation code '%c'", mode[0]);
97 break;
98 }
99 fprintf (stderr, ". Bye!\n");
100 exit (-1);
101 }
102
103 return (result);
104 }
105
Fwrite(const void * ptr,int sizeOfElement,int numElements,FILE * fptr)106 int Fwrite (const void *ptr, int sizeOfElement, int numElements, FILE *fptr)
107 {
108 int retcode;
109 retcode = fwrite (ptr, sizeOfElement, numElements, fptr);
110 if (retcode != numElements)
111 {
112 fprintf (stderr, "Write error! Presume disk is full. Bye!\n");
113 exit (2);
114 }
115
116 return (retcode);
117 }
118
getInt(char * fname)119 int getInt (char *fname)
120 {
121 FILE *infile;
122 int tval;
123
124 infile = Fopen (fname, "r");
125 int fields_read = fscanf (infile, "%d\n", &tval);
126 if(fields_read != 1) {
127 fprintf(stderr, "Failed to read one int. Bye!\n");
128 exit(2);
129 }
130 fclose (infile);
131 return (tval);
132 }
133
134 #define mallocOrDie(name, num, type) name = (type *) calloc (num, sizeof ( type )); \
135 if (name == NULL) { fprintf (stderr, "Couldn't allocate space for '%s'\nBye!\n", #name ); exit (-1); }
136
137 #define setTwoDimensionalMatrix(name, tempArrayName, numFirstIndex, numSecondIndex, type) mallocOrDie(tempArrayName, (numFirstIndex * numSecondIndex), type); \
138 mallocOrDie(name, numFirstIndex, type *); \
139 { int ijkl; for (ijkl=0; ijkl<numFirstIndex; ijkl++) { name[ijkl] = &(tempArrayName[ijkl*(numSecondIndex)]); } }
140
141