-
Notifications
You must be signed in to change notification settings - Fork 16
/
gene_core.c
381 lines (329 loc) · 9.7 KB
/
gene_core.c
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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <ctype.h>
#include <unistd.h>
#include <dirent.h>
#include <zlib.h>
#include "gene_core.h"
/*******************************************************************************************
*
* GENERAL UTILITIES
*
********************************************************************************************/
char *Prog_Name;
void *Malloc(int64 size, char *mesg)
{ void *p;
if ((p = malloc(size)) == NULL)
{ if (mesg == NULL)
fprintf(stderr,"%s: Out of memory\n",Prog_Name);
else
fprintf(stderr,"%s: Out of memory (%s)\n",Prog_Name,mesg);
}
return (p);
}
void *Realloc(void *p, int64 size, char *mesg)
{ if (size <= 0)
size = 1;
if ((p = realloc(p,size)) == NULL)
{ if (mesg == NULL)
fprintf(stderr,"%s: Out of memory\n",Prog_Name);
else
fprintf(stderr,"%s: Out of memory (%s)\n",Prog_Name,mesg);
}
return (p);
}
char *Strdup(char *name, char *mesg)
{ char *s;
if (name == NULL)
return (NULL);
if ((s = strdup(name)) == NULL)
{ if (mesg == NULL)
fprintf(stderr,"%s: Out of memory\n",Prog_Name);
else
fprintf(stderr,"%s: Out of memory (%s)\n",Prog_Name,mesg);
}
return (s);
}
char *Strndup(char *name, int len, char *mesg)
{ char *s;
if (name == NULL)
return (NULL);
if ((s = strndup(name,len)) == NULL)
{ if (mesg == NULL)
fprintf(stderr,"%s: Out of memory\n",Prog_Name);
else
fprintf(stderr,"%s: Out of memory (%s)\n",Prog_Name,mesg);
}
return (s);
}
char *PathTo(char *name)
{ char *path, *find;
if (name == NULL)
return (NULL);
if ((find = rindex(name,'/')) != NULL)
path = Strndup(name,find-name,"Extracting path from");
else
path = Strdup(".","Allocating default path");
return (path);
}
char *Root(char *name, char *suffix)
{ char *path, *find, *dot;
int epos;
if (name == NULL)
return (NULL);
find = rindex(name,'/');
if (find == NULL)
find = name;
else
find += 1;
if (suffix == NULL)
{ dot = strrchr(find,'.');
path = Strndup(find,dot-find,"Extracting root from");
}
else
{ epos = strlen(find);
epos -= strlen(suffix);
if (epos > 0 && strcasecmp(find+epos,suffix) == 0)
path = Strndup(find,epos,"Extracting root from");
else
path = Strdup(find,"Allocating root");
}
return (path);
}
char *Catenate(char *path, char *sep, char *root, char *suffix)
{ static char *cat = NULL;
static int max = -1;
int len;
if (path == NULL || root == NULL || sep == NULL || suffix == NULL)
{ free(cat);
max = -1;
return (NULL);
}
len = strlen(path);
len += strlen(sep);
len += strlen(root);
len += strlen(suffix);
if (len > max)
{ max = ((int) (1.2*len)) + 100;
if ((cat = (char *) realloc(cat,max+1)) == NULL)
{ fprintf(stderr,"%s: Out of memory (Making path name for %s)\n",Prog_Name,root);
return (NULL);
}
}
sprintf(cat,"%s%s%s%s",path,sep,root,suffix);
return (cat);
}
char *Numbered_Suffix(char *left, int num, char *right)
{ static char *suffix = NULL;
static int max = -1;
int len;
if (left == NULL || right == NULL)
{ free(suffix);
max = -1;
return (NULL);
}
len = strlen(left);
len += strlen(right) + 40;
if (len > max)
{ max = ((int) (1.2*len)) + 100;
if ((suffix = (char *) realloc(suffix,max+1)) == NULL)
{ fprintf(stderr,"%s: Out of memory (Making number suffix for %d)\n",Prog_Name,num);
return (NULL);
}
}
sprintf(suffix,"%s%d%s",left,num,right);
return (suffix);
}
#define COMMA ','
// Print big integers with commas/periods for better readability
void Print_Number(int64 num, int width, FILE *out)
{ if (width == 0)
{ if (num < 1000ll)
fprintf(out,"%lld",num);
else if (num < 1000000ll)
fprintf(out,"%lld%c%03lld",num/1000ll,COMMA,num%1000ll);
else if (num < 1000000000ll)
fprintf(out,"%lld%c%03lld%c%03lld",num/1000000ll,
COMMA,(num%1000000ll)/1000ll,COMMA,num%1000ll);
else
fprintf(out,"%lld%c%03lld%c%03lld%c%03lld",num/1000000000ll,
COMMA,(num%1000000000ll)/1000000ll,
COMMA,(num%1000000ll)/1000ll,COMMA,num%1000ll);
}
else
{ if (num < 1000ll)
fprintf(out,"%*lld",width,num);
else if (num < 1000000ll)
{ if (width <= 4)
fprintf(out,"%lld%c%03lld",num/1000ll,COMMA,num%1000ll);
else
fprintf(out,"%*lld%c%03lld",width-4,num/1000ll,COMMA,num%1000ll);
}
else if (num < 1000000000ll)
{ if (width <= 8)
fprintf(out,"%lld%c%03lld%c%03lld",num/1000000ll,COMMA,(num%1000000ll)/1000ll,
COMMA,num%1000ll);
else
fprintf(out,"%*lld%c%03lld%c%03lld",width-8,num/1000000ll,COMMA,(num%1000000ll)/1000ll,
COMMA,num%1000ll);
}
else
{ if (width <= 12)
fprintf(out,"%lld%c%03lld%c%03lld%c%03lld",num/1000000000ll,COMMA,
(num%1000000000ll)/1000000ll,COMMA,
(num%1000000ll)/1000ll,COMMA,num%1000ll);
else
fprintf(out,"%*lld%c%03lld%c%03lld%c%03lld",width-12,num/1000000000ll,COMMA,
(num%1000000000ll)/1000000ll,COMMA,
(num%1000000ll)/1000ll,COMMA,num%1000ll);
}
}
}
// Return the number of symbols to print num, base 10 (without commas as above)
int Number_Digits(int64 num)
{ int digit;
if (num == 0)
return (1);
if (num < 0)
{ num = -num;
digit = 1;
}
else
digit = 0;
while (num >= 1)
{ num /= 10;
digit += 1;
}
return (digit);
}
/*******************************************************************************************
*
* READ AND ARROW COMPRESSION/DECOMPRESSION UTILITIES
*
********************************************************************************************/
// Compress read into 2-bits per base (from [0-3] per byte representation
void Compress_Read(int len, char *s)
{ int i;
char c, d;
char *s0, *s1, *s2, *s3;
s0 = s;
s1 = s0+1;
s2 = s1+1;
s3 = s2+1;
c = s1[len];
d = s2[len];
s0[len] = s1[len] = s2[len] = 0;
for (i = 0; i < len; i += 4)
*s++ = (char ) ((s0[i] << 6) | (s1[i] << 4) | (s2[i] << 2) | s3[i]);
s1[len] = c;
s2[len] = d;
}
// Uncompress read form 2-bits per base into [0-3] per byte representation
void Uncompress_Read(int len, char *s)
{ int i, tlen, byte;
char *s0, *s1, *s2, *s3;
char *t;
s0 = s;
s1 = s0+1;
s2 = s1+1;
s3 = s2+1;
tlen = (len-1)/4;
t = s+tlen;
for (i = tlen*4; i >= 0; i -= 4)
{ byte = *t--;
s0[i] = (char) ((byte >> 6) & 0x3);
s1[i] = (char) ((byte >> 4) & 0x3);
s2[i] = (char) ((byte >> 2) & 0x3);
s3[i] = (char) (byte & 0x3);
}
s[len] = 4;
}
// Convert read in [0-3] representation to ascii representation (end with '\n')
void Lower_Read(char *s)
{ static char letter[4] = { 'a', 'c', 'g', 't' };
for ( ; *s != 4; s++)
*s = letter[(int) *s];
*s = '\0';
}
void Upper_Read(char *s)
{ static char letter[4] = { 'A', 'C', 'G', 'T' };
for ( ; *s != 4; s++)
*s = letter[(int) *s];
*s = '\0';
}
void Letter_Arrow(char *s)
{ static char letter[4] = { '1', '2', '3', '4' };
for ( ; *s != 4; s++)
*s = letter[(int) *s];
*s = '\0';
}
// Convert read in ascii representation to [0-3] representation (end with 4)
void Number_Read(char *s)
{ static char number[128] =
{ 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 1, 0, 0, 0, 2,
0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 3, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 1, 0, 0, 0, 2,
0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 3, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0,
};
for ( ; *s != '\0'; s++)
*s = number[(int) *s];
*s = 4;
}
void Number_Arrow(char *s)
{ static char arrow[128] =
{ 3, 3, 3, 3, 3, 3, 3, 3,
3, 3, 3, 3, 3, 3, 3, 3,
3, 3, 3, 3, 3, 3, 3, 3,
3, 3, 3, 3, 3, 3, 3, 3,
3, 3, 3, 3, 3, 3, 3, 3,
3, 3, 3, 3, 3, 3, 3, 3,
3, 0, 1, 2, 3, 3, 3, 3,
3, 3, 3, 3, 3, 3, 3, 3,
3, 3, 3, 3, 3, 3, 3, 2,
3, 3, 3, 3, 3, 3, 3, 3,
3, 3, 3, 3, 3, 3, 3, 3,
3, 3, 3, 3, 3, 3, 3, 3,
3, 3, 3, 3, 3, 3, 3, 3,
3, 3, 3, 3, 3, 3, 3, 3,
3, 3, 3, 3, 3, 3, 3, 3,
3, 3, 3, 3, 3, 3, 3, 3,
};
for ( ; *s != '\0'; s++)
*s = arrow[(int) *s];
*s = 4;
}
void Change_Read(char *s)
{ static char change[128] =
{ 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0,
0, 'a', 0, 'c', 0, 0, 0, 'g',
0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 't', 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0,
0, 'A', 0, 'C', 0, 0, 0, 'G',
0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 'T', 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0,
};
for ( ; *s != '\0'; s++)
*s = change[(int) *s];
}