-
Notifications
You must be signed in to change notification settings - Fork 2
/
common.c
490 lines (420 loc) · 9.92 KB
/
common.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
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
#include <stdlib.h>
#include <stdarg.h>
#include <string.h>
#include <time.h>
/* htslib headers */
#include <htslib/sam.h>
#include <htslib/hts.h>
#include "common.h"
// Track memory usage
long long memUsage = 0;
const char *sv_type_name(sv_type type){
switch(type){
case SV_INVERSION:
return "inversion";
case SV_DIRECT_DUPLICATION:
return "duplication";
case SV_INVERTED_DUPLICATION:
return "inverted-duplication";
case SV_TANDEM_DUPLICATION:
return "tandem-duplication";
case SV_DELETION:
return "deletion";
case SV_TRANSLOCATION:
return "translocation";
case SV_INVERTED_TRANSLOCATION:
return "inverted-translocation";
case SV_RECIPROCAL:
return "reciprocal";
case SV_INVERTED_RECIPROCAL:
return "inverted-reciprocal";
default:
return "unknown";
}
}
void free_params(void /*parameters*/ *vp){
parameters *p = vp;
free(p->sonic_file);
free(p->logfile);
free(p->outprefix);
free(p->bam_file);
free(p);
}
parameters *get_params(void){
static parameters *params = NULL;
if(params == NULL){ params = malloc(sizeof(parameters));}
return params;
}
parameters *init_params(void){
/* initialize parameters */
parameters *params = get_params();
params->sonic_file = NULL;
params->logfile = NULL;
params->svs_to_find = 0;
params->low_mem = 0;
params->chromosome_count = 24;
params->ploidy = 2;
(params)->outprefix = NULL;
(params)->bam_file = NULL;
(params)->threads = 1;
params->quasi_clique_lambda = QCLIQUE_LAMBDA;
params->quasi_clique_gamma = QCLIQUE_GAMMA;
params->min_qual = MIN_QUAL;
params->barcode_len = BARCODE_LEN;
params->sample_size = READ_SAMPLE_SIZE;
params->max_support = MAX_SUPPORT;
params->max_frag_size = MAX_FRAG_SIZE;
params->filter_gap = VALOR_FILTER_GAP; //true
params->filter_satellite = VALOR_FILTER_SAT; //true
return params;
}
void print_params( parameters* params)
{
printf("\n");
printf( "%-30s%s\n","BAM input:",params->bam_file);
fprintf( logFile,"%-30s%s\n","BAM input:",params->bam_file);
}
void print_error( char* msg)
{
/* print error message and exit */
fprintf( stderr, "\n%s\n", msg);
fprintf( stderr, "Invoke parameter -h for help.\n");
exit( EXIT_COMMON);
}
FILE* safe_fopen( char* path, char* mode)
{
/* Safe file open. Try to open a file; exit if file does not exist */
FILE* file;
char err[500];
file = fopen( path, mode);
if( !file)
{
sprintf( err, "[VALOR INPUT ERROR] Unable to open file %s in %s mode.", path, mode[0]=='w' ? "write" : "read");
print_error( err);
}
return file;
}
htsFile* safe_hts_open( char* path, char* mode)
{
htsFile* bam_file;
char err[500];
bam_file = hts_open( path, mode);
if( !bam_file)
{
sprintf( err, "[VALOR INPUT ERROR] Unable to open file %s in %s mode.", path, mode[0]=='w' ? "write" : "read");
print_error( err);
}
return bam_file;
}
int is_proper( int flag)
{
if ( (flag & BAM_FPAIRED) != 0 && (flag & BAM_FSECONDARY) == 0 && (flag & BAM_FSUPPLEMENTARY) == 0 && (flag & BAM_FDUP) == 0 && (flag & BAM_FQCFAIL) == 0)
return 1;
return 0;
}
int is_alt_concordant( int p1, int p2, int flag, char strand1, char strand2, int min, int max){
if( ( flag & BAM_FPAIRED) == 0)
{
/* Read is single-end. Skip this by calling it concordant */
return RPSEND;
}
/*
if( ( flag & BAM_FPROPER_PAIR) == 0)
{
//Not proper pair
return RPUNMAPPED;
}*/
if( ( flag & BAM_FUNMAP) != 0) // c.a.
{
/* Read unmapped; Orphan or OEA */
return RPUNMAPPED;
}
if( ( flag & BAM_FMUNMAP) != 0) // c.a.
{
/* Mate unmapped; Orphan or OEA */
return RPUNMAPPED;
}
if( strand1 != 0 && strand2 != 0)
{
/* -- orientation = inversion */
return RPMM;
}
if( strand1 == 0 && strand2 == 0)
{
/* ++ orientation = inversion */
return RPPP;
}
if( abs(p1-p2) > DUP_MIN_DIST){
// if( bam_alignment_core.pos <= bam_alignment_core.mpos) // c.a.
{
/* Read is placed BEFORE its mate */
if( strand1 != 0 && strand2 == 0)
{
/* -+ orientation = tandem duplication */
return RPTDUPPM; //was 0 before
}
}
// else
{
/* Read is placed AFTER its mate */
if( strand1 == 0 && strand2 != 0)
{
/* +- orientation = tandem duplication */
return RPTDUPMP; //was 0 before
}
}
}
/* Passed all of the above. proper pair, both mapped, in +- orientation. Now check the isize */
if( abs(p1-p2) < min) // c.a.
{
/* Deletion or Insertion */
return RPINS;
}
else if(abs(p1-p2) > max)
{
return RPDEL;
}
/* All passed. Read is concordant */
return RPCONC;
}
int identify_read_alignment( bam1_core_t bam_alignment_core, int min, int max)
{
int flag = bam_alignment_core.flag;
if( ( flag & BAM_FPAIRED) == 0)
{
/* Read is single-end. Skip this by calling it concordant */
return RPSEND;
}
/*
if( ( flag & BAM_FPROPER_PAIR) == 0)
{
//Not proper pair
return RPUNMAPPED;
}*/
if( ( flag & BAM_FUNMAP) != 0) // c.a.
{
/* Read unmapped; Orphan or OEA */
return RPUNMAPPED;
}
if( ( flag & BAM_FMUNMAP) != 0) // c.a.
{
/* Mate unmapped; Orphan or OEA */
return RPUNMAPPED;
}
if(bam_alignment_core.tid != bam_alignment_core.mtid){
return RPINTER;
}
if( ( flag & BAM_FREVERSE) != 0 && ( flag & BAM_FMREVERSE) != 0)
{
/* -- orientation = inversion */
return RPMM;
}
if( ( flag & BAM_FREVERSE) == 0 && ( flag & BAM_FMREVERSE) == 0)
{
/* ++ orientation = inversion */
return RPPP;
}
if( bam_alignment_core.tid != bam_alignment_core.mtid)
{
/* On different chromosomes */
return RPUNMAPPED;
}
if( abs(bam_alignment_core.pos-bam_alignment_core.mpos) > DUP_MIN_DIST){
// if( bam_alignment_core.pos <= bam_alignment_core.mpos) // c.a.
{
/* Read is placed BEFORE its mate */
if( ( flag & BAM_FREVERSE) != 0 && ( flag & BAM_FMREVERSE) == 0)
{
/* -+ orientation = tandem duplication */
return RPTDUPPM; //was 0 before
}
}
// else
{
/* Read is placed AFTER its mate */
if( ( flag & BAM_FREVERSE) == 0 && ( flag & BAM_FMREVERSE) != 0)
{
/* +- orientation = tandem duplication */
return RPTDUPMP; //was 0 before
}
}
}
/* Passed all of the above. proper pair, both mapped, in +- orientation. Now check the isize */
if( abs(bam_alignment_core.isize) < min) // c.a.
{
/* Deletion or Insertion */
return RPINS;
}
else if(abs(bam_alignment_core.isize) > max)
{
return RPDEL;
}
/* All passed. Read is concordant */
return RPCONC;
}
/* Decode 4-bit encoded bases to their corresponding characters */
char base_as_char( int base_as_int)
{
if( base_as_int == 1)
{
return 'A';
}
else if( base_as_int == 2)
{
return 'C';
}
else if( base_as_int == 4)
{
return 'G';
}
else if( base_as_int == 8)
{
return 'T';
}
else if( base_as_int == 15)
{
return 'N';
}
return 0;
}
/* Return the complement of a base */
char complement_char( char base)
{
switch( base)
{
case 'A':
return 'T';
break;
case 'C':
return 'G';
break;
case 'G':
return 'C';
break;
case 'T':
return 'A';
break;
default:
return 'N';
break;
}
return 'X';
}
/* Add 33 to the interger value of the qual characters to convert them to ASCII */
void qual_to_ascii( char* qual)
{
int i;
for( i = 0; i < strlen( qual); i++)
{
qual[i] = qual[i] + 33;
}
}
void set_str( char** target, char* source)
{
if( *target != NULL)
{
free( ( *target));
}
if (source != NULL)
{
( *target) = ( char*) getMem( sizeof( char) * ( strlen( source) + 1));
strncpy( ( *target), source, ( strlen( source) + 1));
}
else
{
( *target) = NULL;
}
}
/* Reverse a given string */
void reverse_string( char* str)
{
int i;
char swap;
int len = strlen( str);
for( i = 0; i < len / 2; i++)
{
swap = str[i];
str[i] = str[len - i - 1];
str[len - i - 1] = swap;
}
}
void* getMem( size_t size)
{
void* ret;
ret = malloc( size);
if( ret == NULL)
{
fprintf( stderr, "Cannot allocate memory. Currently addressed memory = %0.2f MB, requested memory = %0.2f MB.\nCheck the available main memory.\n", getMemUsage(), ( float) ( size / 1048576.0));
exit( 0);
}
memUsage = memUsage + size;
return ret;
}
void resizeMem( void **ptr, size_t old_size, size_t new_size){
void *ret;
ret = realloc(*ptr,new_size);
if(ret == NULL && new_size != 0){
fprintf( stderr, "Cannot allocate memory. Currently addressed memory = %0.2f MB, requested memory = %0.2f MB.\nCheck the available main memory.\n", getMemUsage(), ( float) ( (new_size-old_size) / 1048576.0));
exit( 0);
}
*ptr = ret;
memUsage = memUsage + new_size - old_size;
}
void freeMem( void* ptr, size_t size)
{
memUsage = memUsage - size;
free( ptr);
}
double getMemUsage()
{
return memUsage / 1048576.0;
}
int chr_atoi(char *chromosome){
if(chromosome[3] > 58){//Some magic number which is between chromosome numbers and letters
return chromosome[3]-66;
}
return atoi(&chromosome[3])-1;
}
int what_is_min_cluster_size(sv_type type,int ploidy){
double dploidy = ploidy;
switch(type){
case SV_DELETION:
return DELETION_MIN_CLUSTER_SIZE / dploidy;
case SV_INVERSION:
return INVERSION_MIN_CLUSTER_SIZE / dploidy;
case SV_DIRECT_DUPLICATION:
case SV_INVERTED_DUPLICATION:
return DUPLICATION_MIN_CLUSTER_SIZE / dploidy;
case SV_TRANSLOCATION:
case SV_INVERTED_TRANSLOCATION:
case SV_RECIPROCAL:
case SV_INVERTED_RECIPROCAL:
return TRANSLOCATION_MIN_CLUSTER_SIZE / dploidy;
case SV_TANDEM_DUPLICATION:
return TANDEM_DUPLICATION_MIN_CLUSTER_SIZE / dploidy;
default:
return -1;
}
}
// DUP,IDUP,DEL,TRA,INV
sv_type atosv(char *str){
if(strcmp(str,"ALL")==0){
return SV_INVERSION | SV_DIRECT_DUPLICATION | SV_INVERTED_DUPLICATION | SV_DELETION | SV_TRANSLOCATION | SV_INVERTED_TRANSLOCATION;
}
if(strcmp(str,"INV")==0){
return SV_INVERSION;
}
if(strcmp(str,"DUP")==0){
return SV_DIRECT_DUPLICATION | SV_INVERTED_DUPLICATION;
}
if(strcmp(str,"TDUP")==0){
return SV_TANDEM_DUPLICATION;
}
if(strcmp(str,"DEL")==0){
return SV_DELETION;
}
if(strcmp(str,"TRA")==0){
return SV_TRANSLOCATION | SV_INVERTED_TRANSLOCATION;
}
return 0;
}