Coverage Report

Created: 2025-07-12 06:16

/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
5.84M
{
49
5.84M
    if (desired <= b->m_data) return 0;
50
635k
    return sam_realloc_bam_data(b, desired);
51
5.84M
}
Unexecuted instantiation: hts.c:realloc_bam_data
sam.c:realloc_bam_data
Line
Count
Source
48
5.84M
{
49
5.84M
    if (desired <= b->m_data) return 0;
50
635k
    return sam_realloc_bam_data(b, desired);
51
5.84M
}
Unexecuted instantiation: simd.c:realloc_bam_data
Unexecuted instantiation: cram_encode.c:realloc_bam_data
52
53
6.51M
static inline int possibly_expand_bam_data(bam1_t *b, size_t bytes) {
54
6.51M
    size_t new_len = (size_t) b->l_data + bytes;
55
56
6.51M
    if (new_len > INT32_MAX || new_len < bytes) { // Too big or overflow
57
0
        errno = ENOMEM;
58
0
        return -1;
59
0
    }
60
6.51M
    if (new_len <= b->m_data) return 0;
61
18.0k
    return sam_realloc_bam_data(b, new_len);
62
6.51M
}
Unexecuted instantiation: hts.c:possibly_expand_bam_data
sam.c:possibly_expand_bam_data
Line
Count
Source
53
6.51M
static inline int possibly_expand_bam_data(bam1_t *b, size_t bytes) {
54
6.51M
    size_t new_len = (size_t) b->l_data + bytes;
55
56
6.51M
    if (new_len > INT32_MAX || new_len < bytes) { // Too big or overflow
57
0
        errno = ENOMEM;
58
0
        return -1;
59
0
    }
60
6.51M
    if (new_len <= b->m_data) return 0;
61
18.0k
    return sam_realloc_bam_data(b, new_len);
62
6.51M
}
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
1.70M
static inline void nibble2base_default(uint8_t *nib, char *seq, int len) {
73
1.70M
    static const char code2base[512] =
74
1.70M
        "===A=C=M=G=R=S=V=T=W=Y=H=K=D=B=N"
75
1.70M
        "A=AAACAMAGARASAVATAWAYAHAKADABAN"
76
1.70M
        "C=CACCCMCGCRCSCVCTCWCYCHCKCDCBCN"
77
1.70M
        "M=MAMCMMMGMRMSMVMTMWMYMHMKMDMBMN"
78
1.70M
        "G=GAGCGMGGGRGSGVGTGWGYGHGKGDGBGN"
79
1.70M
        "R=RARCRMRGRRRSRVRTRWRYRHRKRDRBRN"
80
1.70M
        "S=SASCSMSGSRSSSVSTSWSYSHSKSDSBSN"
81
1.70M
        "V=VAVCVMVGVRVSVVVTVWVYVHVKVDVBVN"
82
1.70M
        "T=TATCTMTGTRTSTVTTTWTYTHTKTDTBTN"
83
1.70M
        "W=WAWCWMWGWRWSWVWTWWWYWHWKWDWBWN"
84
1.70M
        "Y=YAYCYMYGYRYSYVYTYWYYYHYKYDYBYN"
85
1.70M
        "H=HAHCHMHGHRHSHVHTHWHYHHHKHDHBHN"
86
1.70M
        "K=KAKCKMKGKRKSKVKTKWKYKHKKKDKBKN"
87
1.70M
        "D=DADCDMDGDRDSDVDTDWDYDHDKDDDBDN"
88
1.70M
        "B=BABCBMBGBRBSBVBTBWBYBHBKBDBBBN"
89
1.70M
        "N=NANCNMNGNRNSNVNTNWNYNHNKNDNBNN";
90
91
1.70M
    int i, len2 = len/2;
92
1.70M
    seq[0] = 0;
93
94
3.37M
    for (i = 0; i < len2; i++)
95
        // Note size_t cast helps gcc optimiser.
96
1.66M
        memcpy(&seq[i*2], &code2base[(size_t)nib[i]*2], 2);
97
98
1.70M
    if ((i *= 2) < len)
99
204k
        seq[i] = seq_nt16_str[bam_seqi(nib, i)];
100
1.70M
}
Unexecuted instantiation: hts.c:nibble2base_default
Unexecuted instantiation: sam.c:nibble2base_default
simd.c:nibble2base_default
Line
Count
Source
72
1.70M
static inline void nibble2base_default(uint8_t *nib, char *seq, int len) {
73
1.70M
    static const char code2base[512] =
74
1.70M
        "===A=C=M=G=R=S=V=T=W=Y=H=K=D=B=N"
75
1.70M
        "A=AAACAMAGARASAVATAWAYAHAKADABAN"
76
1.70M
        "C=CACCCMCGCRCSCVCTCWCYCHCKCDCBCN"
77
1.70M
        "M=MAMCMMMGMRMSMVMTMWMYMHMKMDMBMN"
78
1.70M
        "G=GAGCGMGGGRGSGVGTGWGYGHGKGDGBGN"
79
1.70M
        "R=RARCRMRGRRRSRVRTRWRYRHRKRDRBRN"
80
1.70M
        "S=SASCSMSGSRSSSVSTSWSYSHSKSDSBSN"
81
1.70M
        "V=VAVCVMVGVRVSVVVTVWVYVHVKVDVBVN"
82
1.70M
        "T=TATCTMTGTRTSTVTTTWTYTHTKTDTBTN"
83
1.70M
        "W=WAWCWMWGWRWSWVWTWWWYWHWKWDWBWN"
84
1.70M
        "Y=YAYCYMYGYRYSYVYTYWYYYHYKYDYBYN"
85
1.70M
        "H=HAHCHMHGHRHSHVHTHWHYHHHKHDHBHN"
86
1.70M
        "K=KAKCKMKGKRKSKVKTKWKYKHKKKDKBKN"
87
1.70M
        "D=DADCDMDGDRDSDVDTDWDYDHDKDDDBDN"
88
1.70M
        "B=BABCBMBGBRBSBVBTBWBYBHBKBDBBBN"
89
1.70M
        "N=NANCNMNGNRNSNVNTNWNYNHNKNDNBNN";
90
91
1.70M
    int i, len2 = len/2;
92
1.70M
    seq[0] = 0;
93
94
3.37M
    for (i = 0; i < len2; i++)
95
        // Note size_t cast helps gcc optimiser.
96
1.66M
        memcpy(&seq[i*2], &code2base[(size_t)nib[i]*2], 2);
97
98
1.70M
    if ((i *= 2) < len)
99
204k
        seq[i] = seq_nt16_str[bam_seqi(nib, i)];
100
1.70M
}
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
1.70M
static inline void nibble2base(uint8_t *nib, char *seq, int len) {
109
1.70M
#ifdef BUILDING_SIMD_NIBBLE2BASE
110
1.70M
    extern void (*htslib_nibble2base)(uint8_t *nib, char *seq, int len);
111
1.70M
    htslib_nibble2base(nib, seq, len);
112
#else
113
    nibble2base_default(nib, seq, len);
114
#endif
115
1.70M
}
Unexecuted instantiation: hts.c:nibble2base
sam.c:nibble2base
Line
Count
Source
108
185k
static inline void nibble2base(uint8_t *nib, char *seq, int len) {
109
185k
#ifdef BUILDING_SIMD_NIBBLE2BASE
110
185k
    extern void (*htslib_nibble2base)(uint8_t *nib, char *seq, int len);
111
185k
    htslib_nibble2base(nib, seq, len);
112
#else
113
    nibble2base_default(nib, seq, len);
114
#endif
115
185k
}
Unexecuted instantiation: simd.c:nibble2base
cram_encode.c:nibble2base
Line
Count
Source
108
1.51M
static inline void nibble2base(uint8_t *nib, char *seq, int len) {
109
1.51M
#ifdef BUILDING_SIMD_NIBBLE2BASE
110
1.51M
    extern void (*htslib_nibble2base)(uint8_t *nib, char *seq, int len);
111
1.51M
    htslib_nibble2base(nib, seq, len);
112
#else
113
    nibble2base_default(nib, seq, len);
114
#endif
115
1.51M
}
116
117
#ifdef __cplusplus
118
}
119
#endif
120
121
#endif