| 1 |
/* |
| 2 |
* jnibwa.c |
| 3 |
*/ |
| 4 |
|
| 5 |
#include <stdio.h> |
| 6 |
#include <string.h> |
| 7 |
#include <stdlib.h> |
| 8 |
#include <unistd.h> |
| 9 |
#include <fcntl.h> |
| 10 |
#include <sys/stat.h> |
| 11 |
#include <sys/mman.h> |
| 12 |
#include <time.h> |
| 13 |
#include <unistd.h> |
| 14 |
#include <zlib.h> |
| 15 |
#include <errno.h> |
| 16 |
|
| 17 |
#include "jnibwa.h" |
| •18 |
#include "bwaComponent/kstring.h" |
| 19 |
#include "bwaComponent/bntseq.h" |
| 20 |
#include "bwaComponent/bwt.h" |
| 21 |
#include "bwaComponent/utils.h" |
| 22 |
#include "bwaComponent/rle.h" |
| 23 |
#include "bwaComponent/rope.h" |
| 24 |
|
| 25 |
|
| 26 |
static inline void kput32( int32_t val, kstring_t* str ) { |
| 27 |
kputsn((char*)&val, sizeof(int32_t), str); |
| 28 |
} |
| 29 |
|
| 30 |
static inline int cigarRefLen( int nCigar, uint32_t const* pCigar ) |
| 31 |
{ |
| 32 |
uint32_t const* pEnd = pCigar + nCigar; |
| 33 |
int len = 0; |
| 34 |
while ( pCigar != pEnd ) { |
| 35 |
uint32_t lenOp = *pCigar++; |
| 36 |
int op = lenOp & 0xf; |
| 37 |
if ( !op || op == 2 ) |
| 38 |
len += lenOp >> 4; |
| 39 |
} |
| 40 |
return len; |
| 41 |
} |
| 42 |
|
| 43 |
static void fmt_BAMish(mem_opt_t const* opt, bntseq_t const* bns, kstring_t *str, bseq1_t *s, int n, mem_aln_t const* list, int which, mem_aln_t const* p, mem_aln_t const* m) { |
| 44 |
if ( !which ) { |
| 45 |
size_t nInts = 12; // for space planning, assume mapped, unpaired reads with 3 cigar ops and an 8 character MD |
| 46 |
if ( p->flag & 0x1 ) nInts += 3; // if paired, add in enough space for mate info |
| 47 |
ks_resize(str, (n*nInts+1)*sizeof(int32_t)); |
| 48 |
kput32(n, str); |
| 49 |
} |
| 50 |
int32_t flag_mapQ = p->flag; |
| 51 |
if ( p->flag & 0x10000 ) flag_mapQ |= 0x100; |
| 52 |
flag_mapQ = (flag_mapQ << 16) | (p->mapq & 0xff); |
| 53 |
kput32(flag_mapQ, str); |
| 54 |
if ( !(p->flag & 0x4) ) { |
| 55 |
kput32(p->rid, str); |
| 56 |
kput32(p->pos, str); |
| 57 |
kput32(p->NM, str); |
| 58 |
kput32(p->score, str); |
| 59 |
kput32(p->sub, str); |
| 60 |
int32_t nCig = p->n_cigar; |
| 61 |
kput32(nCig, str); |
| 62 |
uint32_t* pCig = p->cigar; |
| 63 |
while ( nCig-- ) { |
| 64 |
uint32_t lenOp = *pCig++; |
| 65 |
// op is encoded as MIDSH in a mem_aln_t, but as MIDNSH in a BAM |
| 66 |
if ( (lenOp & 0xf) > 2 ) ++lenOp; |
| 67 |
kput32(lenOp, str); |
| 68 |
} |
| 69 |
int32_t nMD = p->n_cigar ? strlen((char*)pCig) : 0; |
| 70 |
kput32(nMD, str); |
| 71 |
if ( nMD ) |
| 72 |
kputsn((char*)pCig, (nMD+3)&~3, str); |
| 73 |
int32_t nXA = p->XA ? strlen(p->XA) : 0; |
| 74 |
kput32(nXA, str); |
| 75 |
if ( nXA ) { |
| 76 |
kputsn(p->XA, (nXA+3)&~3, str); |
| 77 |
} |
| 78 |
} |
| 79 |
if ( (p->flag & 0x9) == 1 ) { |
| 80 |
kput32(m->rid, str); |
| 81 |
kput32(m->pos, str); |
| 82 |
if ( (p->flag & 0x4) || p->rid != m->rid ) kput32(0, str); |
| 83 |
// the next two lines represent my interpretation of the SAM spec |
| 84 |
// else if ( p->pos < m->pos ) kput32(m->pos+cigarRefLen(m->n_cigar, m->cigar)-p->pos, str); |
| 85 |
// else kput32(m->pos-p->pos-cigarRefLen(p->n_cigar, p->cigar), str); |
| 86 |
// but BWA does something else which is very odd in the case of outies, |
| 87 |
// but is faithfully reproduced below |
| 88 |
else { |
| 89 |
// get 5' end of + reads, and 3' end of - reads |
| 90 |
long p0 = p->pos; |
| 91 |
if ( p->is_rev ) p0 += cigarRefLen(p->n_cigar, p->cigar) - 1; |
| 92 |
long m0 = m->pos; |
| 93 |
if ( m->is_rev ) m0 += cigarRefLen(m->n_cigar, m->cigar) - 1; |
| 94 |
kput32(m0 - p0 + (p0 > m0 ? -1 : p0 < m0 ? 1 : 0), str); |
| 95 |
} |
| 96 |
} |
| 97 |
} |
| 98 |
|
| 99 |
static size_t bufLen( int32_t* pBuf ) { |
| 100 |
size_t totLen = 1; |
| 101 |
size_t nAligns = *pBuf++; |
| 102 |
while ( nAligns-- ) { |
| 103 |
int32_t flag = *pBuf++ >> 16; |
| 104 |
totLen += 1; // for flags |
| 105 |
if ( !(flag & 0x4) ) { |
| 106 |
totLen += 8; // refId, pos, NM, AS, XS, nCigOps, nMDchars, nXAchars |
| 107 |
pBuf += 5; |
| 108 |
int32_t nCig = *pBuf++; |
| 109 |
totLen += nCig; |
| 110 |
pBuf += nCig; |
| 111 |
int32_t nMD = (*pBuf++ + 3) >> 2; |
| 112 |
totLen += nMD; |
| 113 |
pBuf += nMD; |
| 114 |
int32_t nXA = (*pBuf++ + 3) >> 2; |
| 115 |
totLen += nXA; |
| 116 |
pBuf += nXA; |
| 117 |
} |
| 118 |
if ( (flag & 0x9) == 1 ) { |
| 119 |
totLen += 3; // mate rid, mate pos, tlen |
| 120 |
pBuf += 3; |
| 121 |
} |
| 122 |
} |
| 123 |
return totLen; |
| 124 |
} |
| 125 |
|
| 126 |
int jnibwa_createIndexFile( char const* refName, char const* imgName ) { |
| 127 |
bwaidx_t* pIdx = bwa_idx_load(refName, BWA_IDX_ALL); |
| 128 |
bwa_idx2mem(pIdx); |
| 129 |
int fd = open(imgName, O_WRONLY|O_CREAT|O_TRUNC, 0644); |
| 130 |
if ( fd == -1 ) { |
| 131 |
printf("Failed to open %s for writing: %s\n", imgName, strerror(errno)); |
| 132 |
return 2; |
| 133 |
} |
| 134 |
size_t len = pIdx->l_mem; |
| 135 |
uint8_t* buf = pIdx->mem; |
| 136 |
while ( len ) { |
| 137 |
size_t toWrite = len; |
| 138 |
if ( toWrite > (1L<<30) ) toWrite = 1L<<30; |
| 139 |
if ( write(fd,buf,toWrite) != toWrite ) { |
| 140 |
printf("Failed to write %s: %s\n", imgName, strerror(errno)); |
| 141 |
return 2; |
| 142 |
} |
| 143 |
buf += toWrite; |
| 144 |
len -= toWrite; |
| 145 |
} |
| 146 |
if ( close(fd) != 0 ) { |
| 147 |
printf("Failed to close %s: %s\n", imgName, strerror(errno)); |
| 148 |
return 2; |
| 149 |
} |
| 150 |
bwa_idx_destroy(pIdx); |
| 151 |
return 0; |
| 152 |
} |
| 153 |
|
| 154 |
bwaidx_t* jnibwa_openIndex( int fd ) { |
| 155 |
struct stat statBuf; |
| 156 |
if ( fstat(fd, &statBuf) == -1 ) return 0; |
| 157 |
uint8_t* mem = mmap(0, statBuf.st_size, PROT_READ, MAP_SHARED, fd, 0); |
| 158 |
close(fd); |
| 159 |
bwaidx_t* pIdx = calloc(1, sizeof(bwaidx_t)); |
| 160 |
bwa_mem2idx(statBuf.st_size, mem, pIdx); |
| 161 |
pIdx->is_shm = 1; |
| 162 |
mem_fmt_fnc = &fmt_BAMish; |
| 163 |
bwa_verbose = 0; |
| 164 |
return pIdx; |
| 165 |
} |
| 166 |
|
| 167 |
int jnibwa_destroyIndex( bwaidx_t* pIdx ) { |
| 168 |
void* pMem = pIdx->mem; |
| 169 |
size_t memLen = pIdx->l_mem; |
| 170 |
bwa_idx_destroy(pIdx); |
| 171 |
return munmap(pMem, memLen); |
| 172 |
} |
| 173 |
|
| 174 |
void* jnibwa_getRefContigNames( bwaidx_t* pIdx, size_t* pBufSize ) { |
| 175 |
int nRefContigs = pIdx->bns->n_seqs; |
| 176 |
bntann1_t* pAnnoBeg = pIdx->bns->anns; |
| 177 |
bntann1_t* pAnnoEnd = pAnnoBeg + nRefContigs; |
| 178 |
bntann1_t* pAnno; |
| 179 |
int bufSize = 4 + 4*nRefContigs; // for the ints that describe the number of contigs, and the length of each name |
| 180 |
for ( pAnno = pAnnoBeg; pAnno != pAnnoEnd; ++pAnno ) { |
| 181 |
bufSize += strlen(pAnno->name)+1; |
| 182 |
} |
| 183 |
char* bufMem = malloc(bufSize); |
| 184 |
*(int32_t*)bufMem = nRefContigs; |
| 185 |
char* pBuf = bufMem + sizeof(int32_t); |
| 186 |
for ( pAnno = pAnnoBeg; pAnno != pAnnoEnd; ++pAnno ) { |
| 187 |
size_t len = strlen(pAnno->name); |
| 188 |
*(int32_t*)pBuf = len; |
| 189 |
pBuf += sizeof(int32_t); |
| 190 |
memcpy(pBuf, pAnno->name, len); |
| 191 |
pBuf += len; |
| 192 |
} |
| 193 |
*pBufSize = bufSize; |
| 194 |
return bufMem; |
| 195 |
} |
| 196 |
|
| 197 |
void* jnibwa_createAlignments( bwaidx_t* pIdx, mem_opt_t* pOpts, char* pSeq, size_t* pBufSize) { |
| 198 |
char c = 0; |
| 199 |
char* emptyString = &c; |
| 200 |
uint32_t nSeqs = *(uint32_t*)pSeq; |
| 201 |
pSeq += sizeof(uint32_t); |
| 202 |
bseq1_t* pSeq1Beg = calloc(nSeqs, sizeof(bseq1_t)); |
| 203 |
bseq1_t* pSeq1End = pSeq1Beg+nSeqs; |
| 204 |
bseq1_t* pSeq1; |
| 205 |
for ( pSeq1 = pSeq1Beg; pSeq1 != pSeq1End; ++pSeq1 ) { |
| 206 |
size_t seqLen = strlen(pSeq); |
| 207 |
pSeq1->l_seq = seqLen; |
| 208 |
pSeq1->seq = pSeq; |
| 209 |
pSeq1->name = emptyString; |
| 210 |
pSeq1->id = pSeq1-pSeq1Beg; |
| 211 |
pSeq += seqLen + 1; |
| 212 |
} |
| 213 |
|
| 214 |
mem_process_seqs(pOpts, pIdx->bwt, pIdx->bns, pIdx->pac, 0, nSeqs, pSeq1Beg, 0); |
| 215 |
|
| 216 |
size_t nInts = 0; |
| 217 |
for ( pSeq1 = pSeq1Beg; pSeq1 != pSeq1End; ++pSeq1 ) { |
| 218 |
if ( pSeq1->sam ) nInts += bufLen((int32_t*)pSeq1->sam); |
| 219 |
} |
| 220 |
int32_t* resultsBeg = malloc(nInts*sizeof(int32_t)); |
| 221 |
int32_t* pResults = resultsBeg; |
| 222 |
for ( pSeq1 = pSeq1Beg; pSeq1 != pSeq1End; ++pSeq1 ) { |
| 223 |
int32_t* pBuf = (int32_t*)pSeq1->sam; |
| 224 |
if ( pBuf ) { |
| 225 |
size_t len = bufLen(pBuf); |
| 226 |
memcpy(pResults, pBuf, len*sizeof(int32_t)); |
| 227 |
free(pBuf); |
| 228 |
pResults += len; |
| 229 |
} |
| 230 |
} |
| 231 |
free(pSeq1Beg); |
| 232 |
|
| 233 |
*pBufSize = nInts*sizeof(int32_t); |
| 234 |
return resultsBeg; |
| 235 |
} |