-
Notifications
You must be signed in to change notification settings - Fork 6
/
processbam.c
521 lines (437 loc) · 14.5 KB
/
processbam.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
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
#include <ctype.h>
#include <inttypes.h>
/* tardis headers */
#include "common.h"
#include "processbam.h"
#include "processfq.h"
#include "external.h"
bool isRGAvailable = true;
void load_bam( parameters *params, configuration *cfg, bam_info* in_bam, char* path, int alternative, int bam_cnt, char* ref_genome_path)
{
/* Variables */
htsFile* bam_file;
bam_hdr_t* bam_header;
bam1_core_t bam_alignment_core;
bam1_t* bam_alignment;
int** fragment_size;
int** second_pass_fragments;
int* fragments_sampled;
int* second_test_pass;
int* fragment_size_total;
float* variance;
char* library_name = NULL;
int i, j, k, library_index, diff, return_value, temp;
char fai_file[MAX_SEQ];
double count;
fprintf( stderr, "Processing BAM file %s.\n", path);
/* Open the BAM file for reading. htslib automatically detects the format
of the file, so appending "b" after "r" in mode is redundant. */
bam_file = safe_hts_open( path, "r");
/* Read in BAM header information */
//bam_header = bam_hdr_read( ( bam_file->fp).bgzf);
bam_header = sam_hdr_read(bam_file);
/* Extract the Sample Name from the header text */
get_sample_name( in_bam, bam_header->text);
/* Extract the number of libraries within the BAM file */
get_library_count( in_bam, bam_header->text);
if ( in_bam->num_libraries != 0)
{
fprintf( stderr, "Total %d %s found in %s. Sample name is %s.\n", in_bam->num_libraries, in_bam->num_libraries > 1 ? "libraries":"library", path, in_bam->sample_name);
fprintf( logFile, "\nSample name = %s, total library count = %d\n", in_bam->sample_name, in_bam->num_libraries);
}
else
{
fprintf( stderr, "[BAM FILE ERROR] No libraries (RG) found in the BAM file. Setting default library as Lib1.\n");
in_bam->num_libraries = 1;
isRGAvailable = false;
sprintf(in_bam->sample_name, "Sample%d", bam_cnt);
/*
fprintf( stderr, "[BAM FILE ERROR] No libraries found in the BAM file. Do you have read groups set in the file? Use picard-tools (AddOrRemoveReadGroups) to fix your BAM file.\nExiting.\n");
exit (EXIT_READGROUP);
*/
}
/* Initialize the structures for library properties */
in_bam->libraries = ( struct library_properties**) getMem( (in_bam->num_libraries + 1) * sizeof( struct library_properties*));
for( i = 0; i < in_bam->num_libraries; i++)
{
( in_bam->libraries)[i] = ( struct library_properties*) getMem( sizeof( struct library_properties));
( in_bam->libraries)[i]->libname = NULL;
( in_bam->libraries)[i]->fastq1 = NULL;
( in_bam->libraries)[i]->fastq2 = NULL;
( in_bam->libraries)[i]->divet = NULL;
( in_bam->libraries)[i]->is_valid = 0;
( in_bam->libraries)[i]->mappings_discordant = ( discordantMapping **) getMem( (NHASH + 1) * sizeof( discordantMapping *));
for( j = 0; j < NHASH; j++)
( in_bam->libraries)[i]->mappings_discordant[j] = NULL;
if( alternative != 0)
{
( in_bam->libraries)[i]->mappings_alternative = ( alternativeMapping **) getMem( (NHASH + 1) * sizeof( alternativeMapping *));
for( j = 0; j < NHASH; j++)
( in_bam->libraries)[i]->mappings_alternative[j] = NULL;
}
( in_bam->libraries)[i]->listMEI_Mapping = NULL;
( in_bam->libraries)[i]->listNUMT_Mapping = NULL;
( in_bam->libraries)[i]->listSoftClip = NULL;
}
/* Extract the ids/names for the libraries. A single Sample with multiple
possible libraries are assumed for each BAM file */
if( isRGAvailable == true)
get_library_names( in_bam, bam_header->text);
else
set_str( &(in_bam->libraries[0]->libname), "Lib1");
/* For SAMPLEFRAG number of alignments, store the template length field.
Performed for each different library */
fragment_size = ( int**) getMem( in_bam->num_libraries * sizeof( int*));
for( i = 0; i < in_bam->num_libraries; i++)
{
fragment_size[i] = NULL;
fragment_size[i] = ( int*) getMem( SAMPLEFRAG * sizeof( int));
}
sprintf( fai_file,"%s.fai", ref_genome_path);
hts_set_fai_filename( bam_file, fai_file);
/* Initial read */
bam_alignment = bam_init1();
//return_value = bam_read1( ( bam_file->fp).bgzf, bam_alignment);
return_value = sam_read1( bam_file, bam_header, bam_alignment);
/* The remaining reads */
fragments_sampled = ( int*) getMem( in_bam->num_libraries * sizeof( int));
for( i = 0; i < in_bam->num_libraries; i++)
{
fragments_sampled[i] = 0;
}
fprintf( stderr, "Sampling reads from libraries to infer fragment sizes.\n");
while( return_value != -1 && !sufficient_fragments_sampled( fragments_sampled, in_bam->num_libraries))
{
bam_alignment_core = bam_alignment->core;
if( bam_alignment_core.isize > 0 && !bam_is_rev( bam_alignment) && bam_is_mrev( bam_alignment))
{
if( isRGAvailable == true)
{
set_str( &library_name, bam_aux_get( bam_alignment, "RG"));
/* get rid of the leading 'Z' in front of the library_name */
library_index = find_library_index( in_bam, library_name + 1);
}
else
library_index = 0;
/* Sample SAMPLEFRAG number of alignments for each library at most */
if( library_index != -1 && fragments_sampled[library_index] < SAMPLEFRAG)
{
fragment_size[library_index][fragments_sampled[library_index]] = bam_alignment_core.isize;
fragments_sampled[library_index] = fragments_sampled[library_index] + 1;
}
}
/* Read next alignment */
//return_value = bam_read1( ( bam_file->fp).bgzf, bam_alignment);
return_value = sam_read1( bam_file, bam_header, bam_alignment);
}
fprintf( stderr, "Sampling finished. Now calculating library statistics.\n");
/* Now we have SAMPLEFRAG number of fragment sizes which are positive and pass the flag conditions */
for( i = 0; i < in_bam->num_libraries; i++)
{
/* Sort the fragment sizes */
qsort( fragment_size[i], SAMPLEFRAG, sizeof( int), compare_size_int);
/* When the library does not have sufficient number of reads, we discard the leading 0s when calculating the median */
count = 0;
if( fragments_sampled[i] < SAMPLEFRAG)
{
for( k = 0; k < SAMPLEFRAG; k++)
{
if( fragment_size[i][k] == 0)
count++;
else
break;
}
}
/* Get the medians */
temp = ( fragments_sampled[i] / 2) - 1 + (int) count;
( in_bam->libraries)[i]->frag_med = fragment_size[i][temp];
//fprintf(stderr,"There are %d 0s, %d fragments, index %d and median is %d\n", (int) count, fragments_sampled[i], temp, ( in_bam->libraries)[i]->frag_med);
}
/* Find the fragment sizes which pass the second test, and will contribute to the avg and std */
second_pass_fragments = ( int**) getMem( in_bam->num_libraries * sizeof( int*));
for( i = 0; i < in_bam->num_libraries; i++)
{
second_pass_fragments[i] = ( int*) getMem( SAMPLEFRAG * sizeof( int));
}
second_test_pass = ( int*) getMem( in_bam->num_libraries * sizeof( int));
fragment_size_total = ( int*) getMem( in_bam->num_libraries * sizeof( int));
for( i = 0; i < in_bam->num_libraries; i++)
{
second_test_pass[i] = 0;
fragment_size_total[i] = 0;
}
for( i = 0; i < in_bam->num_libraries; i++)
{
for( j = 0; j < SAMPLEFRAG; j++)
{
if( fragment_size[i][j] <= 2 * ( in_bam->libraries)[i]->frag_med)
{
fragment_size_total[i] = fragment_size_total[i] + fragment_size[i][j];
second_pass_fragments[i][j] = fragment_size[i][j];
second_test_pass[i] = second_test_pass[i] + 1;
}
}
}
for( i = 0; i < in_bam->num_libraries; i++)
{
/* Compute the averages */
( in_bam->libraries)[i]->frag_avg = ( float) fragment_size_total[i] / ( float) second_test_pass[i];
/* Set the read length for the current library */
( in_bam->libraries)[i]->read_length = bam_alignment_core.l_qseq;
}
/* Compute the variance and std */
variance = ( float*) getMem( in_bam->num_libraries * sizeof( float));
for( i = 0; i < in_bam->num_libraries; i++)
{
variance[i] = 0;
}
for( i = 0; i < in_bam->num_libraries; i++)
{
for( j = 0; j < second_test_pass[i]; j++)
{
diff = ( second_pass_fragments[i][j] - ( in_bam->libraries)[i]->frag_avg);
variance[i] = variance[i] + diff * diff;
}
}
for( i = 0; i < in_bam->num_libraries; i++)
{
variance[i] = ( float) variance[i] / ( float) second_test_pass[i];
( in_bam->libraries)[i]->frag_std = sqrt( variance[i]);
fprintf( stderr, "\nLibrary %s\n\tMean: %f\n\tStdev: %f\n\n", ( in_bam->libraries)[i]->libname, ( in_bam->libraries)[i]->frag_avg, ( in_bam->libraries)[i]->frag_std);
fprintf( logFile, "\nLibrary %s\n\tMean: %f\n\tStdev: %f\n\tRead Length: %d\n\n", ( in_bam->libraries)[i]->libname, ( in_bam->libraries)[i]->frag_avg, ( in_bam->libraries)[i]->frag_std, ( in_bam->libraries)[i]->read_length);
set_library_min_max( ( in_bam->libraries)[i]);
is_library_valid(( in_bam->libraries)[i]);
plot_histogram( params, cfg, in_bam->sample_name, in_bam->libraries[i]->libname, second_test_pass[i], second_pass_fragments[i], in_bam->libraries[i]->frag_avg, in_bam->libraries[i]->frag_std);
}
/* Close the BAM file */
return_value = hts_close( bam_file);
if( return_value != 0)
{
fprintf( stderr, "Error closing BAM file\n");
exit( EXIT_BAM_CLOSE);
}
/* Free Memory */
for( i = 0; i < in_bam->num_libraries; i++)
{
free( fragment_size[i]);
free( second_pass_fragments[i]);
}
free( fragment_size);
free( second_pass_fragments);
free( fragments_sampled);
free( second_test_pass);
free( fragment_size_total);
free( variance);
free( library_name);
}
void get_sample_name(bam_info* in_bam, char* header_text)
{
/* Delimit the BAM header text with tabs and newlines */
char *tmp_header = NULL;
set_str( &( tmp_header), header_text);
char* p = strtok( tmp_header, "\t\n");
char sample_name_buffer[1024];
while( p != NULL)
{
/* If the current token has "SM" as the first two characters,
we have found our Sample Name */
if( p[0] == 'S' && p[1] == 'M')
{
/* Get the Sample Name */
strncpy( sample_name_buffer, p + 3, strlen( p) - 3);
/* Add the NULL terminator */
sample_name_buffer[strlen( p) - 3] = '\0';
/* Exit loop */
break;
}
p = strtok( NULL, "\t\n");
}
set_str( &( in_bam->sample_name), sample_name_buffer);
free( tmp_header);
}
void get_library_count( bam_info* in_bam, char* header_text)
{
int number_of_libraries = 0;
/* Delimit the BAM header text with newlines */
char *tmp_header = NULL;
set_str( &( tmp_header), header_text);
char* p = strtok( tmp_header, "\n");
while( p != NULL)
{
/* If the current token (which is a line) has "RG" as the second and third characters,
we have found a new library */
if( p[1] == 'R' && p[2] == 'G')
{
number_of_libraries = number_of_libraries + 1;
}
p = strtok( NULL, "\n");
}
in_bam->num_libraries = number_of_libraries;
free( tmp_header);
}
void get_library_names( bam_info* in_bam, char* header_text)
{
char line_buffer[1024];
char library_name_buffer[1024];
int i;
char *saveptr_p, *saveptr_q;
/* Delimit the BAM header text with newlines */
i = 0;
char *tmp_header = NULL;
set_str( &( tmp_header), header_text);
char* p = strtok_r( tmp_header, "\n", &saveptr_p);
while( p != NULL)
{
/* If the current token (which is a line) has "RG" as the second and third characters,
we copy the current line to a new char* and tokenize it to get the id/name */
if( p[1] == 'R' && p[2] == 'G')
{
/* Copy the current line to the line buffer */
strncpy( line_buffer, p, strlen( p));
line_buffer[strlen( p)] = '\0';
/* Tokenize the line buffer by tabs */
char* q = strtok_r( line_buffer, "\t", &saveptr_q);
while( q != NULL)
{
if( q[0] == 'I' && q[1] == 'D')
{
/* Get the Library Name */
strncpy( library_name_buffer, q + 3, strlen( q) - 3);
/* Add the NULL terminator */
library_name_buffer[strlen( q) - 3] = '\0';
/* Exit loop */
break;
}
q = strtok_r( NULL, "\t", &saveptr_q);
}
set_str( &( ( in_bam->libraries)[i]->libname), library_name_buffer);
i = i + 1;
}
p = strtok_r( NULL, "\n", &saveptr_p);
}
free( tmp_header);
}
int find_library_index( bam_info* in_bam, char* library_name)
{
int library_index;
int i;
for( i = 0; i < in_bam->num_libraries; i++)
{
if( strcmp( ( in_bam->libraries)[i]->libname, library_name) == 0)
{
return i;
}
}
return -1;
}
int sufficient_fragments_sampled( int* fragments_sampled, int num_libraries)
{
int i;
for( i = 0; i < num_libraries; i++)
{
if( fragments_sampled[i] != SAMPLEFRAG)
{
return 0;
}
}
return 1;
}
void create_fastq( bam_info* in_bam, char* bam_path, parameters* params)
{
int i;
for( i = 0; i < in_bam->num_libraries; i++)
{
fprintf( stderr, "Creating FASTQ files for the library: %s.\n", ( in_bam->libraries)[i]->libname);
create_fastq_library( ( in_bam->libraries)[i], in_bam->sample_name, bam_path, params);
}
}
void is_library_valid( struct library_properties* in_lib)
{
if( in_lib->frag_avg >=0 && in_lib->frag_std >=0 && in_lib->read_length >= 0 && in_lib->conc_min>=0 && in_lib->conc_max >=0)
in_lib->is_valid = true;
else
in_lib->is_valid = false;
}
void set_library_min_max( struct library_properties* in_lib)
{
in_lib->conc_min = in_lib->frag_avg - ( 4 * in_lib->frag_std);
if ( in_lib->conc_min < 0)
{
in_lib->conc_min = 0;
}
in_lib->conc_max = in_lib->frag_avg + ( 4 * in_lib->frag_std);
}
int is_concordant_bamonly( int pos_left, int pos_right, uint16_t flag, int32_t isize, int min, int max)
{
if( ( flag & BAM_FPAIRED) == 0)
{
/* Read is single-end. Skip this by calling it concordant */
return RPCONC;
}
/*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( ( flag & BAM_FREVERSE) != 0 && ( flag & BAM_FMREVERSE) != 0)
{
/* -- orientation = inversion */
return RPINV;
}
if( ( flag & BAM_FREVERSE) == 0 && ( flag & BAM_FMREVERSE) == 0)
{
/* ++ orientation = inversion */
return RPINV;
}
/*
if( chrID_left != chrID_right)
{
/* On different chromosomes
return RPINTERCHR;
}
*/
if( pos_left <= pos_right) // c.a.
{
/* Read is placed BEFORE its mate */
if( ( flag & BAM_FREVERSE) != 0 && ( flag & BAM_FMREVERSE) == 0)
{
/* -+ orientation = tandem duplication */
return RPTDUP; //was 0 before
}
}
else
{
/* Read is placed AFTER its mate */
if( ( flag & BAM_FREVERSE) == 0 && ( flag & BAM_FMREVERSE) != 0)
{
/* +- orientation = tandem duplication */
return RPTDUP; //was 0 before
}
}
/* Passed all of the above. proper pair, both mapped, in +- orientation. Now check the isize */
if( abs( isize) < min) // c.a.
{
/* Deletion or Insertion */
return RPINS;
}
else if( abs( isize) > max)
{
return RPDEL;
}
/* All passed. Read is concordant */
return RPCONC;
}