1 /*  hts_internal.h -- internal functions; not part of the public API.
2 
3     Copyright (C) 2015-2016, 2018-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_HTS_INTERNAL_H
24 #define HTSLIB_HTS_INTERNAL_H
25 
26 #include <stddef.h>
27 #include <ctype.h>
28 
29 #include "htslib/hts.h"
30 #include "textutils_internal.h"
31 
32 #define HTS_MAX_EXT_LEN 8
33 
34 #ifdef __cplusplus
35 extern "C" {
36 #endif
37 
38 struct hFILE;
39 
40 struct hts_json_token {
41     char type;    ///< Token type
42     char *str;    ///< Value as a C string (filled in for all token types)
43     // TODO Add other fields to fill in for particular data types, e.g.
44     // int inum;
45     // float fnum;
46 };
47 
48 struct cram_fd;
49 
50 /*
51  * Check the existence of a local index file using part of the alignment file name.
52  * The order is alignment.bam.csi, alignment.csi, alignment.bam.bai, alignment.bai
53  * @param fn    - pointer to the file name
54  * @param fnidx - pointer to the index file name placeholder
55  * @return        1 for success, 0 for failure
56  */
57 int hts_idx_check_local(const char *fn, int fmt, char **fnidx);
58 
59 // Retrieve the name of the index file and also download it, if it is remote
60 char *hts_idx_getfn(const char *fn, const char *ext);
61 
62 // Retrieve the name of the index file, but do not download it, if it is remote
63 char *hts_idx_locatefn(const char *fn, const char *ext);
64 
65 // Used for on-the-fly indexing.  See the comments in hts.c.
66 void hts_idx_amend_last(hts_idx_t *idx, uint64_t offset);
67 
68 int hts_idx_fmt(hts_idx_t *idx);
69 
70 // Construct a unique filename based on fname and open it.
71 struct hFILE *hts_open_tmpfile(const char *fname, const char *mode, kstring_t *tmpname);
72 
73 // Check that index is capable of storing items in range beg..end
74 int hts_idx_check_range(hts_idx_t *idx, int tid, hts_pos_t beg, hts_pos_t end);
75 
76 // The CRAM implementation stores the loaded index within the cram_fd rather
77 // than separately as is done elsewhere in htslib.  So if p is a pointer to
78 // an hts_idx_t with p->fmt == HTS_FMT_CRAI, then it actually points to an
79 // hts_cram_idx_t and should be cast accordingly.
80 typedef struct hts_cram_idx_t {
81     int fmt;
82     struct cram_fd *cram;
83 } hts_cram_idx_t;
84 
85 
86 // Entry point to hFILE_multipart backend.
87 struct hFILE *hopen_htsget_redirect(struct hFILE *hfile, const char *mode);
88 
89 struct hts_path_itr {
90     kstring_t path, entry;
91     void *dirv;  // DIR * privately
92     const char *pathdir, *prefix, *suffix;
93     size_t prefix_len, suffix_len, entry_dir_l;
94 };
95 
96 void hts_path_itr_setup(struct hts_path_itr *itr, const char *path,
97     const char *builtin_path, const char *prefix, size_t prefix_len,
98     const char *suffix, size_t suffix_len);
99 
100 const char *hts_path_itr_next(struct hts_path_itr *itr);
101 
102 typedef void plugin_void_func(void);
103 plugin_void_func *load_plugin(void **pluginp, const char *filename, const char *symbol);
104 void *plugin_sym(void *plugin, const char *name, const char **errmsg);
105 plugin_void_func *plugin_func(void *plugin, const char *name, const char **errmsg);
106 void close_plugin(void *plugin);
107 
108 /*
109  * Buffers up arguments to hts_idx_push for later use, once we've written all bar
110  * this block.  This is necessary when multiple blocks are in flight (threading).
111  *
112  * Returns 0 on success,
113  *        -1 on failure
114  */
115 int bgzf_idx_push(BGZF *fp, hts_idx_t *hidx, int tid, hts_pos_t beg, hts_pos_t end, uint64_t offset, int is_mapped);
116 
117 /*
118  * bgzf analogue to hts_idx_amend_last.
119  *
120  * This is needed when multi-threading and writing indices on the fly.
121  * At the point of writing a record we know the virtual offset for start
122  * and end, but that end virtual offset may be the end of the current
123  * block.  In standard indexing our end virtual offset becomes the start
124  * of the next block.  Thus to ensure bit for bit compatibility we
125  * detect this boundary case and fix it up here.
126  */
127 void bgzf_idx_amend_last(BGZF *fp, hts_idx_t *hidx, uint64_t offset);
128 
find_file_extension(const char * fn,char ext_out[static HTS_MAX_EXT_LEN])129 static inline int find_file_extension(const char *fn, char ext_out[static HTS_MAX_EXT_LEN])
130 {
131     const char *delim = fn ? strstr(fn, HTS_IDX_DELIM) : NULL, *ext;
132     if (!fn) return -1;
133     if (!delim) delim = fn + strlen(fn);
134     for (ext = delim; ext > fn && *ext != '.' && *ext != '/'; --ext) {}
135     if (*ext == '.' &&
136         ((delim - ext == 3 && ext[1] == 'g' && ext[2] == 'z') || // permit .sam.gz as a valid file extension
137         (delim - ext == 4 && ext[1] == 'b' && ext[2] == 'g' && ext[3] == 'z'))) // permit .vcf.bgz as a valid file extension
138     {
139         for (ext--; ext > fn && *ext != '.' && *ext != '/'; --ext) {}
140     }
141     if (*ext != '.' || delim - ext > HTS_MAX_EXT_LEN || delim - ext < 4) return -1;
142     memcpy(ext_out, ext + 1, delim - ext - 1);
143     ext_out[delim - ext - 1] = '\0';
144     return 0;
145 }
146 
147 #ifdef __cplusplus
148 }
149 #endif
150 
151 #endif
152