| 1 | #include "samtools.pysam.h" |
| 2 | |
| 3 | /* bam_plbuf.c -- plbuf routines (previously in bam_pileup.c). |
| 4 | |
| 5 | Copyright (C) 2008-2010, 2013 Genome Research Ltd. |
| 6 | |
| 7 | Author: Heng Li <[email protected]> |
| 8 | |
| 9 | Permission is hereby granted, free of charge, to any person obtaining a copy |
| 10 | of this software and associated documentation files (the "Software"), to deal |
| 11 | in the Software without restriction, including without limitation the rights |
| 12 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 13 | copies of the Software, and to permit persons to whom the Software is |
| 14 | furnished to do so, subject to the following conditions: |
| 15 | |
| 16 | The above copyright notice and this permission notice shall be included in |
| 17 | all copies or substantial portions of the Software. |
| 18 | |
| 19 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 20 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 21 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL |
| 22 | THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 23 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING |
| 24 | FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER |
| 25 | DEALINGS IN THE SOFTWARE. */ |
| 26 | |
| 27 | #include <config.h> |
| 28 | |
| 29 | #include <stdio.h> |
| 30 | #include <stdlib.h> |
| 31 | #include <ctype.h> |
| 32 | #include <htslib/hts.h> |
| 33 | #include <htslib/sam.h> |
| 34 | #include "bam_plbuf.h" |
| 35 | |
| 36 | /***************** |
| 37 | * callback APIs * |
| 38 | *****************/ |
| 39 | |
| 40 | void bam_plbuf_reset(bam_plbuf_t *buf) |
| 41 | { |
| 42 | bam_plp_reset(buf->iter); |
| 43 | } |
| 44 | |
| 45 | bam_plbuf_t *bam_plbuf_init(bam_pileup_f func, void *data) |
| 46 | { |
| 47 | bam_plbuf_t *buf; |
| 48 | buf = calloc(1, sizeof(bam_plbuf_t)); |
| 49 | buf->iter = bam_plp_init(0, 0); |
| 50 | buf->func = func; |
| 51 | buf->data = data; |
| 52 | return buf; |
| 53 | } |
| 54 | |
| 55 | void bam_plbuf_destroy(bam_plbuf_t *buf) |
| 56 | { |
| 57 | bam_plp_destroy(buf->iter); |
| 58 | free(buf); |
| 59 | } |
| 60 | |
| 61 | int bam_plbuf_push(const bam1_t *b, bam_plbuf_t *buf) |
| 62 | { |
| 63 | int ret, n_plp, tid; |
| 64 | hts_pos_t pos; |
| 65 | const bam_pileup1_t *plp; |
| 66 | ret = bam_plp_push(buf->iter, b); |
| 67 | if (ret < 0) return ret; |
| 68 | while ((plp = bam_plp64_next(buf->iter, &tid, &pos, &n_plp)) != 0) |
| 69 | buf->func(tid, pos, n_plp, plp, buf->data); |
| 70 | return 0; |
| 71 | } |