@@ -284,11 +284,11 @@ void bwa_correct_trimmed(bwa_seq_t *s)
284
284
s -> len = s -> full_len ;
285
285
}
286
286
287
- void bwa_refine_gapped (const bntseq_t * bns , int n_seqs , bwa_seq_t * seqs , ubyte_t * _pacseq )
287
+ void bwa_refine_gapped (const bntseq_t * bns , int n_seqs , bwa_seq_t * seqs , ubyte_t * _pacseq , int with_md )
288
288
{
289
289
ubyte_t * pacseq ;
290
290
int i , j , k ;
291
- kstring_t * str ;
291
+ kstring_t * str = ( kstring_t * ) calloc ( 1 , sizeof ( kstring_t )) ;
292
292
293
293
if (!_pacseq ) {
294
294
pacseq = (ubyte_t * )calloc (bns -> l_pac /4 + 1 , 1 );
@@ -305,15 +305,25 @@ void bwa_refine_gapped(const bntseq_t *bns, int n_seqs, bwa_seq_t *seqs, ubyte_t
305
305
q -> cigar = bwa_refine_gapped_core (bns -> l_pac , pacseq , s -> len , q -> strand ? s -> rseq : s -> seq , q -> ref_shift , & q -> pos , & n_cigar );
306
306
q -> n_cigar = n_cigar ;
307
307
if (q -> cigar ) s -> multi [k ++ ] = * q ;
308
- } else s -> multi [k ++ ] = * q ;
308
+ } else {
309
+ s -> multi [k ++ ] = * q ;
310
+ if (with_md ) { // create the cigar, needed for bwa_cal_md1 below
311
+ q -> n_cigar = 1 ;
312
+ q -> cigar = calloc (q -> n_cigar , sizeof (uint32_t ));
313
+ q -> cigar [0 ] = __cigar_create (FROM_M , s -> len );
314
+ }
315
+ }
316
+ if (with_md ) {
317
+ int nm ;
318
+ q -> md = bwa_cal_md1 (q -> n_cigar , q -> cigar , s -> len , q -> pos , q -> strand ? s -> rseq : s -> seq , bns -> l_pac , pacseq , str , & nm );
319
+ }
309
320
}
310
321
s -> n_multi = k ; // this squeezes out gapped alignments which failed the CIGAR generation
311
322
if (s -> type == BWA_TYPE_NO_MATCH || s -> type == BWA_TYPE_MATESW || s -> n_gapo == 0 ) continue ;
312
323
s -> cigar = bwa_refine_gapped_core (bns -> l_pac , pacseq , s -> len , s -> strand ? s -> rseq : s -> seq , s -> ref_shift , & s -> pos , & s -> n_cigar );
313
324
if (s -> cigar == 0 ) s -> type = BWA_TYPE_NO_MATCH ;
314
325
}
315
326
// generate MD tag
316
- str = (kstring_t * )calloc (1 , sizeof (kstring_t ));
317
327
for (i = 0 ; i != n_seqs ; ++ i ) {
318
328
bwa_seq_t * s = seqs + i ;
319
329
if (s -> type != BWA_TYPE_NO_MATCH ) {
@@ -504,7 +514,7 @@ void bwase_initialize()
504
514
for (i = 1 ; i != 256 ; ++ i ) g_log_n [i ] = (int )(4.343 * log (i ) + 0.5 );
505
515
}
506
516
507
- void bwa_sai2sam_se_core (const char * prefix , const char * fn_sa , const char * fn_fa , int n_occ , const char * rg_line )
517
+ void bwa_sai2sam_se_core (const char * prefix , const char * fn_sa , const char * fn_fa , int n_occ , const char * rg_line , int with_md )
508
518
{
509
519
extern bwa_seqio_t * bwa_open_reads (int mode , const char * fn_fa );
510
520
int i , n_seqs , m_aln ;
@@ -557,7 +567,7 @@ void bwa_sai2sam_se_core(const char *prefix, const char *fn_sa, const char *fn_f
557
567
fprintf (stderr , "%.2f sec\n" , (float )(clock () - t ) / CLOCKS_PER_SEC ); t = clock ();
558
568
559
569
fprintf (stderr , "[bwa_aln_core] refine gapped alignments... " );
560
- bwa_refine_gapped (bns , n_seqs , seqs , 0 );
570
+ bwa_refine_gapped (bns , n_seqs , seqs , 0 , with_md );
561
571
fprintf (stderr , "%.2f sec\n" , (float )(clock () - t ) / CLOCKS_PER_SEC ); t = clock ();
562
572
563
573
fprintf (stderr , "[bwa_aln_core] print alignments... " );
@@ -578,11 +588,12 @@ void bwa_sai2sam_se_core(const char *prefix, const char *fn_sa, const char *fn_f
578
588
579
589
int bwa_sai2sam_se (int argc , char * argv [])
580
590
{
581
- int c , n_occ = 3 ;
591
+ int c , n_occ = 3 , with_md = 0 ;
582
592
char * prefix , * rg_line = 0 ;
583
- while ((c = getopt (argc , argv , "hn :f:r:" )) >= 0 ) {
593
+ while ((c = getopt (argc , argv , "hdn :f:r:" )) >= 0 ) {
584
594
switch (c ) {
585
595
case 'h' : break ;
596
+ case 'd' : with_md = 1 ; break ;
586
597
case 'r' :
587
598
if ((rg_line = bwa_set_rg (optarg )) == 0 ) return 1 ;
588
599
break ;
@@ -593,14 +604,14 @@ int bwa_sai2sam_se(int argc, char *argv[])
593
604
}
594
605
595
606
if (optind + 3 > argc ) {
596
- fprintf (stderr , "Usage: bwa samse [-n max_occ] [-f out.sam] [-r RG_line] <prefix> <in.sai> <in.fq>\n" );
607
+ fprintf (stderr , "Usage: bwa samse [-n max_occ] [-d] [- f out.sam] [-r RG_line] <prefix> <in.sai> <in.fq>\n" );
597
608
return 1 ;
598
609
}
599
610
if ((prefix = bwa_idx_infer_prefix (argv [optind ])) == 0 ) {
600
611
fprintf (stderr , "[%s] fail to locate the index\n" , __func__ );
601
612
return 1 ;
602
613
}
603
- bwa_sai2sam_se_core (prefix , argv [optind + 1 ], argv [optind + 2 ], n_occ , rg_line );
614
+ bwa_sai2sam_se_core (prefix , argv [optind + 1 ], argv [optind + 2 ], n_occ , rg_line , with_md );
604
615
free (prefix );
605
616
return 0 ;
606
617
}
0 commit comments