1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320
|
/* qtask_ordered.c -- showcases the htslib api usage
Copyright (C) 2024 Genome Research Ltd.
Author: Vasudeva Sarma <vasudeva.sarma@sanger.ac.uk>
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
DEALINGS IN THE SOFTWARE
*/
/* The purpose of this code is to demonstrate the library apis and need proper error handling and optimisation */
#include <getopt.h>
#include <unistd.h>
#include <pthread.h>
#include <sys/time.h>
#include <htslib/sam.h>
#include <htslib/thread_pool.h>
struct datacache;
typedef struct basecount {
uint64_t counts[16]; //count of all bases
} basecount;
typedef struct data {
int count; //used up size
int maxsize; //max size per data chunk
bam1_t **bamarray; //bam1_t array for optimal queueing
struct datacache *cache;
basecount *bases; //count of all possible bases
struct data *next; //pointer to next one - to reuse earlier allocations
} data;
typedef struct datacache
{
pthread_mutex_t lock; //synchronizes the access to cache
data *list; //data storage
} datacache;
/// print_usage - print the usage
/** @param fp pointer to the file / terminal to which usage to be dumped
returns nothing
*/
static void print_usage(FILE *fp)
{
fprintf(fp, "Usage: qtask_unordered infile threadcount [chunksize]\n\
Shows the base counts and calculates GC ratio - sum(G,C) / sum(A,T,C,G)\n\
chunksize [4096] sets the number of alignments clubbed together to process.\n");
return;
}
/// getbamstorage - allocates storage for alignments to queue
/** @param chunk number of bam data to allocate
* @param bases storage of result
* @param bamcache cached storage
returns already allocated data storage if one is available, otherwise allocates new
*/
data* getbamstorage(int chunk, basecount *bases, datacache *bamcache)
{
int i = 0;
data *bamdata = NULL;
if (!bamcache || !bases) {
return NULL;
}
//get from cache if there is an already allocated storage
if (pthread_mutex_lock(&bamcache->lock)) {
return NULL;
}
if (bamcache->list) { //available
bamdata = bamcache->list;
bamcache->list = bamdata->next; //remove and set next one as available
bamdata->next = NULL; //remove link
bamdata->count = 0;
bamdata->bases = bases;
bamdata->cache = bamcache;
goto end;
}
//allocate and use
if (!(bamdata = malloc(sizeof(data)))) {
goto end;
}
bamdata->bamarray = malloc(chunk * sizeof(bam1_t*));
if (!bamdata->bamarray) {
free(bamdata);
bamdata = NULL;
goto end;
}
for (i = 0; i < chunk; ++i) {
bamdata->bamarray[i] = bam_init1();
}
bamdata->maxsize = chunk;
bamdata->count = 0;
bamdata->next = NULL;
bamdata->bases = bases;
bamdata->cache = bamcache;
end:
pthread_mutex_unlock(&bamcache->lock);
return bamdata;
}
/// cleanup_bamstorage - frees a bamdata struct plus contents
/** @param arg Pointer to data to free
@p arg has type void * so it can be used as a callback passed
to hts_tpool_dispatch3().
*/
void cleanup_bamstorage(void *arg)
{
data *bamdata = (data *) arg;
if (!bamdata)
return;
if (bamdata->bamarray) {
int i;
for (i = 0; i < bamdata->maxsize; i++) {
bam_destroy1(bamdata->bamarray[i]);
}
free(bamdata->bamarray);
}
free(bamdata);
}
/// thread_unordered_proc - does the processing of task in queue and updates result
/** @param args pointer to set of data to be processed
returns NULL
the processing could be in any order based on the number of threads in use
*/
void *thread_unordered_proc(void *args)
{
int i = 0;
data *bamdata = (data*)args;
uint64_t pos = 0;
uint8_t *data = NULL;
uint64_t counts[16] = {0};
for ( i = 0; i < bamdata->count; ++i) {
data = bam_get_seq(bamdata->bamarray[i]);
for (pos = 0; pos < bamdata->bamarray[i]->core.l_qseq; ++pos) {
/* it is faster to count all bases and select required ones later
compared to select and count here */
counts[bam_seqi(data, pos)]++;
}
}
//update result and add the memory block for reuse
pthread_mutex_lock(&bamdata->cache->lock);
for (i = 0; i < 16; i++) {
bamdata->bases->counts[i] += counts[i];
}
bamdata->next = bamdata->cache->list;
bamdata->cache->list = bamdata;
pthread_mutex_unlock(&bamdata->cache->lock);
return NULL;
}
/// main - start of the demo
/** @param argc - count of arguments
* @param argv - pointer to array of arguments
returns 1 on failure 0 on success
*/
int main(int argc, char *argv[])
{
const char *inname = NULL;
int c = 0, ret = EXIT_FAILURE, cnt = 0, chunk = 0;
samFile *infile = NULL;
sam_hdr_t *in_samhdr = NULL;
hts_tpool *pool = NULL;
hts_tpool_process *queue = NULL;
htsThreadPool tpool = {NULL, 0};
data *bamdata = NULL;
basecount gccount = {{0}};
datacache bamcache = {PTHREAD_MUTEX_INITIALIZER, NULL};
//qtask infile threadcount [chunksize]
if (argc != 3 && argc != 4) {
print_usage(stdout);
goto end;
}
inname = argv[1];
cnt = atoi(argv[2]);
if (argc == 4) {
chunk = atoi(argv[3]);
}
if (cnt < 1) {
cnt = 1;
}
if (chunk < 1) {
chunk = 4096;
}
if (!(pool = hts_tpool_init(cnt))) {
fprintf(stderr, "Failed to create thread pool\n");
goto end;
}
tpool.pool = pool; //to share the pool for file read and write as well
//queue to use with thread pool, for tasks
if (!(queue = hts_tpool_process_init(pool, cnt * 2, 1))) {
fprintf(stderr, "Failed to create queue\n");
goto end;
}
//open input file - r reading
if (!(infile = sam_open(inname, "r"))) {
fprintf(stderr, "Could not open %s\n", inname);
goto end;
}
//share the thread pool with i/o files
if (hts_set_opt(infile, HTS_OPT_THREAD_POOL, &tpool) < 0) {
fprintf(stderr, "Failed to set threads to i/o files\n");
goto end;
}
//read header, required to resolve the target names to proper ids
if (!(in_samhdr = sam_hdr_read(infile))) {
fprintf(stderr, "Failed to read header from file!\n");
goto end;
}
/*tasks are queued, worker threads get them and process in parallel;
all bases are counted instead of counting atcg alone as it is faster*/
c = 0;
while (c >= 0) {
//use cached storage to avoid allocate/deallocate overheads
if (!(bamdata = getbamstorage(chunk, &gccount, &bamcache))) {
fprintf(stderr, "Failed to allocate memory\n");
break;
}
//read alignments, upto max size for this lot
for (cnt = 0; cnt < bamdata->maxsize; ++cnt) {
c = sam_read1(infile, in_samhdr, bamdata->bamarray[cnt]);
if (c < 0) {
break; // EOF or failure
}
}
if (c >= -1 ) {
//max size data or reached EOF
bamdata->count = cnt;
// Queue the data for processing. hts_tpool_dispatch3() is
// used here as it allows in-flight data to be cleaned up
// properly when stopping early due to errors.
if (hts_tpool_dispatch3(pool, queue, thread_unordered_proc, bamdata,
cleanup_bamstorage, cleanup_bamstorage,
0) == -1) {
fprintf(stderr, "Failed to schedule processing\n");
goto end;
}
bamdata = NULL;
} else {
fprintf(stderr, "Error in reading data\n");
break;
}
}
if (-1 == c) {
// EOF read, ensure all are processed, waits for all to finish
if (hts_tpool_process_flush(queue) == -1) {
fprintf(stderr, "Failed to flush queue\n");
} else { //all done
//refer seq_nt16_str to find position of required bases
fprintf(stdout, "GCratio: %f\nBase counts:\n",
(gccount.counts[2] /*C*/ + gccount.counts[4] /*G*/) / (float)
(gccount.counts[1] /*A*/ + gccount.counts[8] /*T*/ +
gccount.counts[2] + gccount.counts[4]));
for (cnt = 0; cnt < 16; ++cnt) {
fprintf(stdout, "%c: %"PRIu64"\n", seq_nt16_str[cnt], gccount.counts[cnt]);
}
ret = EXIT_SUCCESS;
}
}
end:
if (queue) {
hts_tpool_process_destroy(queue);
}
if (in_samhdr) {
sam_hdr_destroy(in_samhdr);
}
if (infile) {
if (sam_close(infile) != 0) {
ret = EXIT_FAILURE;
}
}
pthread_mutex_lock(&bamcache.lock);
if (bamcache.list) {
struct data *tmp = NULL;
while (bamcache.list) {
tmp = bamcache.list;
bamcache.list = bamcache.list->next;
cleanup_bamstorage(tmp);
}
}
pthread_mutex_unlock(&bamcache.lock);
if (pool) {
hts_tpool_destroy(pool);
}
return ret;
}
|