-
Notifications
You must be signed in to change notification settings - Fork 9
/
bam_prase.cpp
557 lines (352 loc) · 11.3 KB
/
bam_prase.cpp
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
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
///g++ -O3 bam_prase.cpp -lhts -o bam_prase
#include "Auxiliary.h"
#include"bam_prase.h"
#include "Process_sam_out.h"
#include <pthread.h>
#include <htslib/sam.h>
bam_operations bitmapperBS_bam;
pthread_mutex_t queueMutex_bam;
void get_bam_header(bam_hdr_t** bam_header)
{
samFile* in;
in = sam_open(bitmapperBS_bam.output_name, "r");
///读入sam的首部
if (((*bam_header) = sam_hdr_read(in)) == 0) {
fprintf(stderr, "fail to read %s\n", bitmapperBS_bam.output_name);
return;
}
///一定要关闭,不关的话写不到文件里去
sam_close(in);
}
void init_bam_header_back(char* tmp_sam_file_name, char* bam_file_name)
{
samFile* in;
in = sam_open(tmp_sam_file_name, "r");
bitmapperBS_bam.bam_file = sam_open(bam_file_name, "wb");
bitmapperBS_bam.bam_header = NULL;
bitmapperBS_bam.output_name = tmp_sam_file_name;
///读入sam的首部
if ((bitmapperBS_bam.bam_header = sam_hdr_read(in)) == 0) {
fprintf(stderr, "fail to read %s\n", tmp_sam_file_name);
return;
}
///写入sam的首部
if (sam_hdr_write(bitmapperBS_bam.bam_file, bitmapperBS_bam.bam_header) != 0) {
fprintf(stderr, "fail to write %s\n", bam_file_name);
return;
}
///一定要关闭,不关的话写不到文件里去
sam_close(in);
}
void init_bam_header(char* bam_file_name, _rg_name_l *_ih_refGenName, int refChromeCont, int argc, char *argv[])
{
if (bam_file_name[0] == '\0')
{
bitmapperBS_bam.bam_file = sam_open_format("-", "wb", NULL);
}
else
{
bitmapperBS_bam.bam_file = sam_open_format(bam_file_name, "wb", NULL);
}
bitmapperBS_bam.bam_header = NULL;
bitmapperBS_bam.output_name = bam_file_name;
char** chrome_name = (char**)malloc(sizeof(char*)*refChromeCont);
bitmapper_bs_iter* chrome_length = (bitmapper_bs_iter*)malloc(sizeof(bitmapper_bs_iter)*refChromeCont);
int i = 0;
for (i = 0; i < refChromeCont; i++)
{
chrome_name[i]=_ih_refGenName[i]._rg_chrome_name;
chrome_length[i] = _ih_refGenName[i]._rg_chrome_length;
}
///读入sam的首部
//if ((bitmapperBS_bam.bam_header = sam_hdr_read(in)) == 0) {
if ((bitmapperBS_bam.bam_header = chhy_sam_hdr_read(chrome_name, chrome_length, refChromeCont,
argc, argv, versionN)) == 0) {
fprintf(stderr, "fail to phrase %s\n", bam_file_name);
return;
}
///写入sam的首部
if (sam_hdr_write(bitmapperBS_bam.bam_file, bitmapperBS_bam.bam_header) != 0) {
fprintf(stderr, "fail to write %s\n", bam_file_name);
return;
}
free(chrome_name);
free(chrome_length);
}
///void init_bam_file_from_sam(char* file_name, char* outputFileName)
void init_bam_file_from_sam(char* file_name, char* outputFileName,
_rg_name_l *_ih_refGenName, int refChromeCont, int argc, char *argv[])
{
finalizeOutput();
///init_bam_header(file_name, outputFileName);
///init_bam_header(file_name, outputFileName, _ih_refGenName, refChromeCont, argc, argv);
}
void init_bam_output_cell(bam_output_cell* cell)
{
cell->b = bam_init1(); ///初始化吧,分配内存
}
void init_bam_output_group(bam_output_group* group, long long total_size)
{
group->size = total_size;
group->number = 0;
group->b = (bam1_t **)malloc(sizeof(bam1_t *)*group->size);
int i;
for (i = 0; i < group->size; i++)
{
group->b[i] = bam_init1();
}
}
BGZF* init_buff_bgzf(BGZF* exist_bgzf)
{
BGZF *fp;
fp = (BGZF*)calloc(1, sizeof(BGZF));
if (fp == NULL)
{
return NULL;
}
fp->is_write = 1;
fp->is_compressed = 1;
fp->uncompressed_block = malloc(2 * BGZF_MAX_BLOCK_SIZE);
if (fp->uncompressed_block == NULL)
{
return NULL;
}
fp->compressed_block = (char *)fp->uncompressed_block + BGZF_MAX_BLOCK_SIZE;
fp->compress_level = exist_bgzf->compress_level;
fp->is_be = exist_bgzf->is_be;
fp->fp = exist_bgzf->fp;
fp->block_offset = exist_bgzf->block_offset;
///fprintf(stderr, "exist_bgzf->block_offset: %d\n", exist_bgzf->block_offset);
return fp;
}
void init_bam_buffer(bam_phrase* buf)
{
buf->bgzf = init_buff_bgzf(bitmapperBS_bam.bam_file->fp.bgzf);
buf->b = bam_init1(); ///初始化吧,分配内存
buf->result.uncomp_data = (unsigned char*)malloc(2 * BGZF_MAX_BLOCK_SIZE);
buf->result.comp_data = (unsigned char*)buf->result.uncomp_data + BGZF_MAX_BLOCK_SIZE;
}
void write_alignment_directly(char* alignment, long long alignment_length, bam_output_cell* cell)
{
cell->line.l = alignment_length;
if (cell->line.l == 0)
{
return;
}
cell->line.m = cell->line.l;
cell->line.s = alignment;
free(cell->b->data);
memset(cell->b, 0, sizeof(bam1_t));
int ret = sam_parse1(&cell->line, bitmapperBS_bam.bam_header, cell->b);
sam_write1(bitmapperBS_bam.bam_file, bitmapperBS_bam.bam_header, cell->b);
}
void phrase_alignment_only(char* alignment, long long alignment_length, bam_output_group* bam_groups)
{
bam_groups->line.l = alignment_length;
if (bam_groups->line.l == 0)
{
return;
}
bam_groups->line.m = bam_groups->line.l;
bam_groups->line.s = alignment;
free(bam_groups->b[bam_groups->number]->data);
memset(bam_groups->b[bam_groups->number], 0, sizeof(bam1_t));
int ret = sam_parse1(&bam_groups->line, bam_groups->bam_header, bam_groups->b[bam_groups->number]);
///sam_write1(bitmapperBS_bam.bam_file, bitmapperBS_bam.bam_header, cell->b);
bam_groups->number++;
}
void convert_string_to_bam(char* alignment, long long alignment_length, bam_phrase* bam_groups)
{
bam_groups->line.l = alignment_length;
if (bam_groups->line.l == 0)
{
return;
}
bam_groups->line.m = bam_groups->line.l;
bam_groups->line.s = alignment;
free(bam_groups->b->data);
memset(bam_groups->b, 0, sizeof(bam1_t));
int ret = sam_parse1(&bam_groups->line, bitmapperBS_bam.bam_header, bam_groups->b);
///sam_write1(bitmapperBS_bam.bam_file, bitmapperBS_bam.bam_header, cell->b);
///至少当cigar不超过0xffff时,一行alignment在bam里不会被分开
chhy_bam_write1_pure(bitmapperBS_bam.bam_file->fp.bgzf, bam_groups->bgzf, bam_groups->b, &(bam_groups->result));
}
void flush_bam_buffer(bam_phrase* bam_groups)
{
chhy_lazy_flush_pure(bitmapperBS_bam.bam_file->fp.bgzf, bam_groups->bgzf,&(bam_groups->result));
}
void write_alignment_only(bam_output_group* bam_groups)
{
long long i = 0;
pthread_mutex_lock(&queueMutex_bam);
for (i = 0; i < bam_groups->number; i++)
{
sam_write1(bitmapperBS_bam.bam_file, bitmapperBS_bam.bam_header, bam_groups->b[i]);
}
bam_groups->number = 0;
pthread_mutex_unlock(&queueMutex_bam);
}
void close_bam_file()
{
if (bitmapperBS_bam.bam_header)
bam_hdr_destroy(bitmapperBS_bam.bam_header);
///一定要关闭,不关的话写不到文件里去
sam_close(bitmapperBS_bam.bam_file);
}
void close_bam_file_rename(char* old_name, char* new_name)
{
if (remove(new_name) != 0)
{
fprintf(stderr, "ERROE\n");
exit(0);
}
if (bitmapperBS_bam.bam_header)
bam_hdr_destroy(bitmapperBS_bam.bam_header);
///一定要关闭,不关的话写不到文件里去
sam_close(bitmapperBS_bam.bam_file);
if (rename(old_name, new_name)!=0)
{
fprintf(stderr, "ERROE\n");
exit(0);
}
}
void write_alignment(const char* alignment)
{
kstring_t line;
int r;
line.l = strlen(alignment);
if (line.l == 0)
{
return;
}
line.m = line.l;
line.s = (char*)malloc(line.l + 1);
memcpy(line.s, alignment, line.l + 1);
bam1_t *b = bam_init1(); ///初始化吧,分配内存
int ret = sam_parse1(&line, bitmapperBS_bam.bam_header, b);
line.l = 0;
sam_write1(bitmapperBS_bam.bam_file, bitmapperBS_bam.bam_header, b);
free(line.s);
bam_destroy1(b); ///释放内存
}
///以回车分割
void write_alignment_group(char* alignment, const long long length)
{
char* tab;
long long current_length;
kstring_t line;
bam1_t *b = bam_init1(); ///初始化吧,分配内存
while ((tab = strchr(alignment, '\n')))
{
current_length = tab - alignment;
*tab = '\0';
line.l = current_length;
if (line.l == 0)
{
return;
}
line.m = line.l;
line.s = alignment;
memset(b, 0, sizeof(bam1_t));
int ret = sam_parse1(&line, bitmapperBS_bam.bam_header, b);
sam_write1(bitmapperBS_bam.bam_file, bitmapperBS_bam.bam_header, b);
alignment = tab + 1;
}
bam_destroy1(b); ///释放内存
}
///以回车分割
void write_alignment_group_pre_allocate(char* alignment, const long long length, const long long number)
{
char* tab;
long long current_length;
kstring_t line;
///bam1_t *b = bam_init1(); ///初始化吧,分配内存
bam1_t** b_array = (bam1_t**)malloc(sizeof(bam1_t*)*number);
long long i;
for (i = 0; i < number; i++)
{
b_array[i] = bam_init1();
}
i = 0;
while ((tab = strchr(alignment, '\n')))
{
if (i >= number)
{
fprintf(stderr, "ERROR\n");
exit(0);
}
current_length = tab - alignment;
*tab = '\0';
line.l = current_length;
if (line.l == 0)
{
return;
}
line.m = line.l;
line.s = alignment;
memset(b_array[i], 0, sizeof(bam1_t));
int ret = sam_parse1(&line, bitmapperBS_bam.bam_header, b_array[i]);
alignment = tab + 1;
i++;
}
for (i = 0; i < number; i++)
{
sam_write1(bitmapperBS_bam.bam_file, bitmapperBS_bam.bam_header, b_array[i]);
}
for (i = 0; i < number; i++)
{
bam_destroy1(b_array[i]); ///释放内存
}
}
void write_alignment_muti_thread(const char* alignment)
{
kstring_t line;
int r;
line.l = strlen(alignment);
if (line.l == 0)
{
return;
}
line.m = line.l;
line.s = (char*)malloc(line.l + 1);
memcpy(line.s, alignment, line.l + 1);
bam1_t *b = bam_init1(); ///初始化吧,分配内存
int ret = sam_parse1(&line, bitmapperBS_bam.bam_header, b);
line.l = 0;
pthread_mutex_lock(&queueMutex_bam);
sam_write1(bitmapperBS_bam.bam_file, bitmapperBS_bam.bam_header, b);
pthread_mutex_unlock(&queueMutex_bam);
free(line.s);
bam_destroy1(b); ///释放内存
}
/**
void write_alignment(const char* alignment)
{
kstring_t line;
int r;
///bitmapperBS_bam.bam_file->line.l = strlen(alignment);
line.l = strlen(alignment);
if (bitmapperBS_bam.bam_file->line.l == 0)
{
return;
}
bitmapperBS_bam.bam_file->line.m = bitmapperBS_bam.bam_file->line.l;
bitmapperBS_bam.bam_file->line.s = (char*)malloc(bitmapperBS_bam.bam_file->line.l + 1);
memcpy(bitmapperBS_bam.bam_file->line.s, alignment, bitmapperBS_bam.bam_file->line.l + 1);
bam1_t *b = bam_init1(); ///初始化吧,分配内存
int ret = sam_parse1(&bitmapperBS_bam.bam_file->line, bitmapperBS_bam.bam_header, b);
bitmapperBS_bam.bam_file->line.l = 0;
sam_write1(bitmapperBS_bam.bam_file, bitmapperBS_bam.bam_header, b);
free(bitmapperBS_bam.bam_file->line.s);
bam_destroy1(b); ///释放内存
}
**/
/**
int main(int argc, char **argv)
{
///min_example_string_to_bam();
init_bam_header("output.sam", "output.bam");
write_alignment("SRR1532535.52049\t16\t4\t61322565\t255\t90M\t*\t0\t0\tAACAATCCCCCCACTTTAACTTCCCATAATACTAAAATAACAAACATAAATCACCACTCCTAATCCCCAATATTTCTTTTATATTAATTA\tCDCC><DDFHHGHEHIHIHHGFGJJIIJGGFIJJJJJIJJJJJIIJIJJJJIGIGEJJJJJJJIJJJJJJJJJJJJIHHHHHFFFFFCCC\tNM:i:0\tZS:Z:-+\0");
close_bam_file();
}
**/