trinity-plugins/bwa-0.5.7-patched_multi_map/bwtsw2_aux.c
static void print_hits(const bntseq_t *bns, const bsw2opt_t *opt, bsw2seq1_t *ks, bwtsw2_t *b)
{
	int i, k;
	kstring_t str;
	memset(&str, 0, sizeof(kstring_t));
	for (i = 0; i < b->n; ++i) {
		bsw2hit_t *p = b->hits + i;
		int32_t seqid = -1, coor = -1;
		int j, qual, nn = 0;
		int beg, end;
		if (p->l == 0) {
			b->n_cigar[i] = fix_cigar(ks->name, bns, p, b->n_cigar[i], b->cigar[i]);
			nn = bns_coor_pac2real(bns, p->k, p->len, &seqid);
			coor = p->k - bns->anns[seqid].offset;
		}
		ksprintf(&str, "%s\t%d", ks->name, p->flag&0x10);
		ksprintf(&str, "\t%s\t%d", seqid>=0? bns->anns[seqid].name : "*", coor + 1);
		if (p->l == 0) {
			{ // estimate mapping quality
				float c = 1.0;	
				int subo = p->G2 > opt->t? p->G2 : opt->t;
				if (p->flag>>16 == 1 || p->flag>>16 == 2) c *= .5;
				if (p->n_seeds < 2) c *= .2;
				qual = (int)(c * (p->G - subo) * (250.0 / p->G + 0.03 / opt->a) + .499);
				if (qual > 250) qual = 250;
				if (p->flag&1) qual = 0;
			}
			ksprintf(&str, "\t%d\t", qual);
			for (k = 0; k < b->n_cigar[i]; ++k)
				ksprintf(&str, "%d%c", b->cigar[i][k]>>4, (opt->hard_clip? "MIDNHHP" : "MIDNSHP")[b->cigar[i][k]&0xf]);
		} else ksprintf(&str, "\t0\t*");
		ksprintf(&str, "\t*\t0\t0\t");
		beg = 0; end = ks->l;
		if (opt->hard_clip) {
			if ((b->cigar[i][0]&0xf) == 4) beg += b->cigar[i][0]>>4;
			if ((b->cigar[i][b->n_cigar[i]-1]&0xf) == 4) end -= b->cigar[i][b->n_cigar[i]-1]>>4;
		}
		for (j = beg; j < end; ++j) {
			if (p->flag&0x10) kputc(nt_comp_table[(int)ks->seq[ks->l - 1 - j]], &str);
			else kputc(ks->seq[j], &str);
		}
		if (ks->qual) {
			kputc('\t', &str);
			for (j = beg; j < end; ++j) {
				if (p->flag&0x10) kputc(ks->qual[ks->l - 1 - j], &str);
				else kputc(ks->qual[j], &str);
			}
		} else ksprintf(&str, "\t*");
		ksprintf(&str, "\tAS:i:%d\tXS:i:%d\tXF:i:%d\tXE:i:%d\tXN:i:%d", p->G, p->G2, p->flag>>16, p->n_seeds, nn);
		if (p->l) ksprintf(&str, "\tXI:i:%d", p->l - p->k + 1);
		kputc('\n', &str);
	}
	ks->sam = str.s;
	free(ks->seq); ks->seq = 0;
	free(ks->qual); ks->qual = 0;
	free(ks->name); ks->name = 0;
}
/* generate SAM lines for a sequence in ks with alignment stored in
 * b. ks->name and ks->seq will be freed and set to NULL in the end. */
generate SAM lines for a sequence in ks with alignment stored in
b. ks->name and ks->seq will be freed and set to NULL in the end.
generate SAM lines for a sequence in ks with alignment stored in b . ks - > name and ks - > seq will be freed and set to NULL in the end .
static void print_hits(const bntseq_t *bns, const bsw2opt_t *opt, bsw2seq1_t *ks, bwtsw2_t *b)
{
	int i, k;
	kstring_t str;
	memset(&str, 0, sizeof(kstring_t));
	for (i = 0; i < b->n; ++i) {
		bsw2hit_t *p = b->hits + i;
		int32_t seqid = -1, coor = -1;
		int j, qual, nn = 0;
		int beg, end;
		if (p->l == 0) {
			b->n_cigar[i] = fix_cigar(ks->name, bns, p, b->n_cigar[i], b->cigar[i]);
			nn = bns_coor_pac2real(bns, p->k, p->len, &seqid);
			coor = p->k - bns->anns[seqid].offset;
		}
		ksprintf(&str, "%s\t%d", ks->name, p->flag&0x10);
		ksprintf(&str, "\t%s\t%d", seqid>=0? bns->anns[seqid].name : "*", coor + 1);
		if (p->l == 0) {
			{ 
				float c = 1.0;	
				int subo = p->G2 > opt->t? p->G2 : opt->t;
				if (p->flag>>16 == 1 || p->flag>>16 == 2) c *= .5;
				if (p->n_seeds < 2) c *= .2;
				qual = (int)(c * (p->G - subo) * (250.0 / p->G + 0.03 / opt->a) + .499);
				if (qual > 250) qual = 250;
				if (p->flag&1) qual = 0;
			}
			ksprintf(&str, "\t%d\t", qual);
			for (k = 0; k < b->n_cigar[i]; ++k)
				ksprintf(&str, "%d%c", b->cigar[i][k]>>4, (opt->hard_clip? "MIDNHHP" : "MIDNSHP")[b->cigar[i][k]&0xf]);
		} else ksprintf(&str, "\t0\t*");
		ksprintf(&str, "\t*\t0\t0\t");
		beg = 0; end = ks->l;
		if (opt->hard_clip) {
			if ((b->cigar[i][0]&0xf) == 4) beg += b->cigar[i][0]>>4;
			if ((b->cigar[i][b->n_cigar[i]-1]&0xf) == 4) end -= b->cigar[i][b->n_cigar[i]-1]>>4;
		}
		for (j = beg; j < end; ++j) {
			if (p->flag&0x10) kputc(nt_comp_table[(int)ks->seq[ks->l - 1 - j]], &str);
			else kputc(ks->seq[j], &str);
		}
		if (ks->qual) {
			kputc('\t', &str);
			for (j = beg; j < end; ++j) {
				if (p->flag&0x10) kputc(ks->qual[ks->l - 1 - j], &str);
				else kputc(ks->qual[j], &str);
			}
		} else ksprintf(&str, "\t*");
		ksprintf(&str, "\tAS:i:%d\tXS:i:%d\tXF:i:%d\tXE:i:%d\tXN:i:%d", p->G, p->G2, p->flag>>16, p->n_seeds, nn);
		if (p->l) ksprintf(&str, "\tXI:i:%d", p->l - p->k + 1);
		kputc('\n', &str);
	}
	ks->sam = str.s;
	free(ks->seq); ks->seq = 0;
	free(ks->qual); ks->qual = 0;
	free(ks->name); ks->name = 0;
}
static void print_hits ( const bntseq_t * bns , const bsw2opt_t * opt , bsw2seq1_t * ks , bwtsw2_t * b ) { int i , k ; kstring_t str ; memset ( & str , 0 , sizeof ( kstring_t ) ) ; for ( i = 0 ; i < b -> n ; ++ i ) { bsw2hit_t * p = b -> hits + i ; int32_t seqid = -1 , coor = -1 ; int j , qual , nn = 0 ; int beg , end ; if ( p -> l == 0 ) { b -> n_cigar [ i ] = fix_cigar ( ks -> name , bns , p , b -> n_cigar [ i ] , b -> cigar [ i ] ) ; nn = bns_coor_pac2real ( bns , p -> k , p -> len , & seqid ) ; coor = p -> k - bns -> anns [ seqid ] . offset ; } ksprintf ( & str , " \t " , ks -> name , p -> flag & 0x10 ) ; ksprintf ( & str , " \t \t " , seqid >= 0 ? bns -> anns [ seqid ] . name : " " , coor + 1 ) ; if ( p -> l == 0 ) { { float c = 1.0 ; int subo = p -> G2 > opt -> t ? p -> G2 : opt -> t ; if ( p -> flag >> 16 == 1 || p -> flag >> 16 == 2 ) c *= .5 ; if ( p -> n_seeds < 2 ) c *= .2 ; qual = ( int ) ( c * ( p -> G - subo ) * ( 250.0 / p -> G + 0.03 / opt -> a ) + .499 ) ; if ( qual > 250 ) qual = 250 ; if ( p -> flag & 1 ) qual = 0 ; } ksprintf ( & str , " \t \t " , qual ) ; for ( k = 0 ; k < b -> n_cigar [ i ] ; ++ k ) ksprintf ( & str , " " , b -> cigar [ i ] [ k ] >> 4 , ( opt -> hard_clip ? " " : " " ) [ b -> cigar [ i ] [ k ] & 0xf ] ) ; } else ksprintf ( & str , " \t \t " ) ; ksprintf ( & str , " \t \t \t \t " ) ; beg = 0 ; end = ks -> l ; if ( opt -> hard_clip ) { if ( ( b -> cigar [ i ] [ 0 ] & 0xf ) == 4 ) beg += b -> cigar [ i ] [ 0 ] >> 4 ; if ( ( b -> cigar [ i ] [ b -> n_cigar [ i ] - 1 ] & 0xf ) == 4 ) end -= b -> cigar [ i ] [ b -> n_cigar [ i ] - 1 ] >> 4 ; } for ( j = beg ; j < end ; ++ j ) { if ( p -> flag & 0x10 ) kputc ( nt_comp_table [ ( int ) ks -> seq [ ks -> l - 1 - j ] ] , & str ) ; else kputc ( ks -> seq [ j ] , & str ) ; } if ( ks -> qual ) { kputc ( ' \t ' , & str ) ; for ( j = beg ; j < end ; ++ j ) { if ( p -> flag & 0x10 ) kputc ( ks -> qual [ ks -> l - 1 - j ] , & str ) ; else kputc ( ks -> qual [ j ] , & str ) ; } } else ksprintf ( & str , " \t " ) ; ksprintf ( & str , " \t \t \t \t \t " , p -> G , p -> G2 , p -> flag >> 16 , p -> n_seeds , nn ) ; if ( p -> l ) ksprintf ( & str , " \t " , p -> l - p -> k + 1 ) ; kputc ( ' \n ' , & str ) ; } ks -> sam = str . s ; free ( ks -> seq ) ; ks -> seq = 0 ; free ( ks -> qual ) ; ks -> qual = 0 ; free ( ks -> name ) ; ks -> name = 0 ; }
generate SAM lines for a sequence in ks with alignment stored in
 b. ks->name and ks->seq will be freed and set to NULL in the end.
generate SAM lines for a sequence in ks with alignment stored in b . ks - > name and ks - > seq will be freed and set to NULL in the end .
// estimate mapping quality