1 /*  sam_internal.h -- internal functions; not part of the public API.
2 
3     Copyright (C) 2019-2020 Genome Research Ltd.
4 
5 Permission is hereby granted, free of charge, to any person obtaining a copy
6 of this software and associated documentation files (the "Software"), to deal
7 in the Software without restriction, including without limitation the rights
8 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9 copies of the Software, and to permit persons to whom the Software is
10 furnished to do so, subject to the following conditions:
11 
12 The above copyright notice and this permission notice shall be included in
13 all copies or substantial portions of the Software.
14 
15 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18 THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20 FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
21 DEALINGS IN THE SOFTWARE.  */
22 
23 #ifndef HTSLIB_SAM_INTERNAL_H
24 #define HTSLIB_SAM_INTERNAL_H
25 
26 #include <errno.h>
27 #include <stdint.h>
28 #include "htslib/sam.h"
29 
30 #ifdef __cplusplus
31 extern "C" {
32 #endif
33 
34 // Used internally in the SAM format multi-threading.
35 int sam_state_destroy(samFile *fp);
36 int sam_set_thread_pool(htsFile *fp, htsThreadPool *p);
37 int sam_set_threads(htsFile *fp, int nthreads);
38 
39 // Fastq state
40 int fastq_state_set(samFile *fp, enum hts_fmt_option opt, ...);
41 void fastq_state_destroy(samFile *fp);
42 
43 // bam1_t data (re)allocation
44 int sam_realloc_bam_data(bam1_t *b, size_t desired);
45 
realloc_bam_data(bam1_t * b,size_t desired)46 static inline int realloc_bam_data(bam1_t *b, size_t desired)
47 {
48     if (desired <= b->m_data) return 0;
49     return sam_realloc_bam_data(b, desired);
50 }
51 
possibly_expand_bam_data(bam1_t * b,size_t bytes)52 static inline int possibly_expand_bam_data(bam1_t *b, size_t bytes) {
53     size_t new_len = (size_t) b->l_data + bytes;
54 
55     if (new_len > INT32_MAX || new_len < bytes) { // Too big or overflow
56         errno = ENOMEM;
57         return -1;
58     }
59     if (new_len <= b->m_data) return 0;
60     return sam_realloc_bam_data(b, new_len);
61 }
62 
63 /*
64  * Convert a nibble encoded BAM sequence to a string of bases.
65  *
66  * We do this 2 bp at a time for speed. Equiv to:
67  *
68  * for (i = 0; i < len; i++)
69  *    seq[i] = seq_nt16_str[bam_seqi(nib, i)];
70  */
nibble2base(uint8_t * nib,char * seq,int len)71 static inline void nibble2base(uint8_t *nib, char *seq, int len) {
72     static const char code2base[512] =
73         "===A=C=M=G=R=S=V=T=W=Y=H=K=D=B=N"
74         "A=AAACAMAGARASAVATAWAYAHAKADABAN"
75         "C=CACCCMCGCRCSCVCTCWCYCHCKCDCBCN"
76         "M=MAMCMMMGMRMSMVMTMWMYMHMKMDMBMN"
77         "G=GAGCGMGGGRGSGVGTGWGYGHGKGDGBGN"
78         "R=RARCRMRGRRRSRVRTRWRYRHRKRDRBRN"
79         "S=SASCSMSGSRSSSVSTSWSYSHSKSDSBSN"
80         "V=VAVCVMVGVRVSVVVTVWVYVHVKVDVBVN"
81         "T=TATCTMTGTRTSTVTTTWTYTHTKTDTBTN"
82         "W=WAWCWMWGWRWSWVWTWWWYWHWKWDWBWN"
83         "Y=YAYCYMYGYRYSYVYTYWYYYHYKYDYBYN"
84         "H=HAHCHMHGHRHSHVHTHWHYHHHKHDHBHN"
85         "K=KAKCKMKGKRKSKVKTKWKYKHKKKDKBKN"
86         "D=DADCDMDGDRDSDVDTDWDYDHDKDDDBDN"
87         "B=BABCBMBGBRBSBVBTBWBYBHBKBDBBBN"
88         "N=NANCNMNGNRNSNVNTNWNYNHNKNDNBNN";
89 
90     int i, len2 = len/2;
91     seq[0] = 0;
92 
93     for (i = 0; i < len2; i++)
94         // Note size_t cast helps gcc optimiser.
95         memcpy(&seq[i*2], &code2base[(size_t)nib[i]*2], 2);
96 
97     if ((i *= 2) < len)
98         seq[i] = seq_nt16_str[bam_seqi(nib, i)];
99 }
100 
101 #ifdef __cplusplus
102 }
103 #endif
104 
105 #endif
106