Coverage Report

Created: 2025-08-29 06:39

/src/htslib/sam_internal.h
Line
Count
Source (jump to first uncovered line)
1
/*  sam_internal.h -- internal functions; not part of the public API.
2
3
    Copyright (C) 2019-2020, 2023-2024 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
29
#include "htslib/sam.h"
30
31
#ifdef __cplusplus
32
extern "C" {
33
#endif
34
35
// Used internally in the SAM format multi-threading.
36
int sam_state_destroy(samFile *fp);
37
int sam_set_thread_pool(htsFile *fp, htsThreadPool *p);
38
int sam_set_threads(htsFile *fp, int nthreads);
39
40
// Fastq state
41
int fastq_state_set(samFile *fp, enum hts_fmt_option opt, ...);
42
void fastq_state_destroy(samFile *fp);
43
44
// bam1_t data (re)allocation
45
int sam_realloc_bam_data(bam1_t *b, size_t desired);
46
47
static inline int realloc_bam_data(bam1_t *b, size_t desired)
48
8.85M
{
49
8.85M
    if (desired <= b->m_data) return 0;
50
585k
    return sam_realloc_bam_data(b, desired);
51
8.85M
}
Unexecuted instantiation: hts.c:realloc_bam_data
sam.c:realloc_bam_data
Line
Count
Source
48
8.85M
{
49
8.85M
    if (desired <= b->m_data) return 0;
50
585k
    return sam_realloc_bam_data(b, desired);
51
8.85M
}
Unexecuted instantiation: simd.c:realloc_bam_data
Unexecuted instantiation: cram_encode.c:realloc_bam_data
52
53
4.52M
static inline int possibly_expand_bam_data(bam1_t *b, size_t bytes) {
54
4.52M
    size_t new_len = (size_t) b->l_data + bytes;
55
56
4.52M
    if (new_len > INT32_MAX || new_len < bytes) { // Too big or overflow
57
0
        errno = ENOMEM;
58
0
        return -1;
59
0
    }
60
4.52M
    if (new_len <= b->m_data) return 0;
61
6.58k
    return sam_realloc_bam_data(b, new_len);
62
4.52M
}
Unexecuted instantiation: hts.c:possibly_expand_bam_data
sam.c:possibly_expand_bam_data
Line
Count
Source
53
4.52M
static inline int possibly_expand_bam_data(bam1_t *b, size_t bytes) {
54
4.52M
    size_t new_len = (size_t) b->l_data + bytes;
55
56
4.52M
    if (new_len > INT32_MAX || new_len < bytes) { // Too big or overflow
57
0
        errno = ENOMEM;
58
0
        return -1;
59
0
    }
60
4.52M
    if (new_len <= b->m_data) return 0;
61
6.58k
    return sam_realloc_bam_data(b, new_len);
62
4.52M
}
Unexecuted instantiation: simd.c:possibly_expand_bam_data
Unexecuted instantiation: cram_encode.c:possibly_expand_bam_data
63
64
/*
65
 * Convert a nibble encoded BAM sequence to a string of bases.
66
 *
67
 * We do this 2 bp at a time for speed. Equiv to:
68
 *
69
 * for (i = 0; i < len; i++)
70
 *    seq[i] = seq_nt16_str[bam_seqi(nib, i)];
71
 */
72
2.33M
static inline void nibble2base_default(uint8_t *nib, char *seq, int len) {
73
2.33M
    static const char code2base[512] =
74
2.33M
        "===A=C=M=G=R=S=V=T=W=Y=H=K=D=B=N"
75
2.33M
        "A=AAACAMAGARASAVATAWAYAHAKADABAN"
76
2.33M
        "C=CACCCMCGCRCSCVCTCWCYCHCKCDCBCN"
77
2.33M
        "M=MAMCMMMGMRMSMVMTMWMYMHMKMDMBMN"
78
2.33M
        "G=GAGCGMGGGRGSGVGTGWGYGHGKGDGBGN"
79
2.33M
        "R=RARCRMRGRRRSRVRTRWRYRHRKRDRBRN"
80
2.33M
        "S=SASCSMSGSRSSSVSTSWSYSHSKSDSBSN"
81
2.33M
        "V=VAVCVMVGVRVSVVVTVWVYVHVKVDVBVN"
82
2.33M
        "T=TATCTMTGTRTSTVTTTWTYTHTKTDTBTN"
83
2.33M
        "W=WAWCWMWGWRWSWVWTWWWYWHWKWDWBWN"
84
2.33M
        "Y=YAYCYMYGYRYSYVYTYWYYYHYKYDYBYN"
85
2.33M
        "H=HAHCHMHGHRHSHVHTHWHYHHHKHDHBHN"
86
2.33M
        "K=KAKCKMKGKRKSKVKTKWKYKHKKKDKBKN"
87
2.33M
        "D=DADCDMDGDRDSDVDTDWDYDHDKDDDBDN"
88
2.33M
        "B=BABCBMBGBRBSBVBTBWBYBHBKBDBBBN"
89
2.33M
        "N=NANCNMNGNRNSNVNTNWNYNHNKNDNBNN";
90
91
2.33M
    int i, len2 = len/2;
92
2.33M
    seq[0] = 0;
93
94
2.96M
    for (i = 0; i < len2; i++)
95
        // Note size_t cast helps gcc optimiser.
96
638k
        memcpy(&seq[i*2], &code2base[(size_t)nib[i]*2], 2);
97
98
2.33M
    if ((i *= 2) < len)
99
103k
        seq[i] = seq_nt16_str[bam_seqi(nib, i)];
100
2.33M
}
Unexecuted instantiation: hts.c:nibble2base_default
Unexecuted instantiation: sam.c:nibble2base_default
simd.c:nibble2base_default
Line
Count
Source
72
2.33M
static inline void nibble2base_default(uint8_t *nib, char *seq, int len) {
73
2.33M
    static const char code2base[512] =
74
2.33M
        "===A=C=M=G=R=S=V=T=W=Y=H=K=D=B=N"
75
2.33M
        "A=AAACAMAGARASAVATAWAYAHAKADABAN"
76
2.33M
        "C=CACCCMCGCRCSCVCTCWCYCHCKCDCBCN"
77
2.33M
        "M=MAMCMMMGMRMSMVMTMWMYMHMKMDMBMN"
78
2.33M
        "G=GAGCGMGGGRGSGVGTGWGYGHGKGDGBGN"
79
2.33M
        "R=RARCRMRGRRRSRVRTRWRYRHRKRDRBRN"
80
2.33M
        "S=SASCSMSGSRSSSVSTSWSYSHSKSDSBSN"
81
2.33M
        "V=VAVCVMVGVRVSVVVTVWVYVHVKVDVBVN"
82
2.33M
        "T=TATCTMTGTRTSTVTTTWTYTHTKTDTBTN"
83
2.33M
        "W=WAWCWMWGWRWSWVWTWWWYWHWKWDWBWN"
84
2.33M
        "Y=YAYCYMYGYRYSYVYTYWYYYHYKYDYBYN"
85
2.33M
        "H=HAHCHMHGHRHSHVHTHWHYHHHKHDHBHN"
86
2.33M
        "K=KAKCKMKGKRKSKVKTKWKYKHKKKDKBKN"
87
2.33M
        "D=DADCDMDGDRDSDVDTDWDYDHDKDDDBDN"
88
2.33M
        "B=BABCBMBGBRBSBVBTBWBYBHBKBDBBBN"
89
2.33M
        "N=NANCNMNGNRNSNVNTNWNYNHNKNDNBNN";
90
91
2.33M
    int i, len2 = len/2;
92
2.33M
    seq[0] = 0;
93
94
2.96M
    for (i = 0; i < len2; i++)
95
        // Note size_t cast helps gcc optimiser.
96
638k
        memcpy(&seq[i*2], &code2base[(size_t)nib[i]*2], 2);
97
98
2.33M
    if ((i *= 2) < len)
99
103k
        seq[i] = seq_nt16_str[bam_seqi(nib, i)];
100
2.33M
}
Unexecuted instantiation: cram_encode.c:nibble2base_default
101
102
#if defined HAVE_ATTRIBUTE_CONSTRUCTOR && \
103
    ((defined __x86_64__ && defined HAVE_ATTRIBUTE_TARGET_SSSE3 && defined HAVE_BUILTIN_CPU_SUPPORT_SSSE3) || \
104
     (defined __ARM_NEON))
105
#define BUILDING_SIMD_NIBBLE2BASE
106
#endif
107
108
2.33M
static inline void nibble2base(uint8_t *nib, char *seq, int len) {
109
2.33M
#ifdef BUILDING_SIMD_NIBBLE2BASE
110
2.33M
    extern void (*htslib_nibble2base)(uint8_t *nib, char *seq, int len);
111
2.33M
    htslib_nibble2base(nib, seq, len);
112
#else
113
    nibble2base_default(nib, seq, len);
114
#endif
115
2.33M
}
Unexecuted instantiation: hts.c:nibble2base
sam.c:nibble2base
Line
Count
Source
108
91.1k
static inline void nibble2base(uint8_t *nib, char *seq, int len) {
109
91.1k
#ifdef BUILDING_SIMD_NIBBLE2BASE
110
91.1k
    extern void (*htslib_nibble2base)(uint8_t *nib, char *seq, int len);
111
91.1k
    htslib_nibble2base(nib, seq, len);
112
#else
113
    nibble2base_default(nib, seq, len);
114
#endif
115
91.1k
}
Unexecuted instantiation: simd.c:nibble2base
cram_encode.c:nibble2base
Line
Count
Source
108
2.23M
static inline void nibble2base(uint8_t *nib, char *seq, int len) {
109
2.23M
#ifdef BUILDING_SIMD_NIBBLE2BASE
110
2.23M
    extern void (*htslib_nibble2base)(uint8_t *nib, char *seq, int len);
111
2.23M
    htslib_nibble2base(nib, seq, len);
112
#else
113
    nibble2base_default(nib, seq, len);
114
#endif
115
2.23M
}
116
117
#ifdef __cplusplus
118
}
119
#endif
120
121
#endif