Debian Codegraph @

bcftools/bin.c.pysam.c

raw symbols SCIP
1 #include "bcftools.pysam.h"
2
3 /* The MIT License
4
5 Copyright (c) 2016-2022 Genome Research Ltd.
6
7 Author: Petr Danecek <[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 THE
22 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
23 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
24 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
25 THE SOFTWARE.
26
27 */
28
29 #include <stdio.h>
30 #include <assert.h>
31 #include "bcftools.h"
32 #include "bin.h"
33
34 struct _bin_t
35 {
36 float *bins;
37 int nbins;
38 };
39
40 bin_t *bin_init(const char *list_def, float min, float max)
41 {
42 bin_t *bin = (bin_t*) calloc(1,sizeof(bin_t));
43
44 // a comma indicates a list, otherwise a file
45 int is_file = strchr(list_def,',') ? 0 : 1;
46 int i, nlist;
47 char **list = hts_readlist(list_def, is_file, &nlist);
48 if ( !list ) error("Error: failed to read %s\n",list_def);
49 bin->nbins = nlist;
50 bin->bins = (float*) malloc(sizeof(float)*nlist);
51 for (i=0; i<nlist; i++)
52 {
53 char *tmp;
54 bin->bins[i] = strtod(list[i],&tmp);
55 if ( *tmp ) error("Could not parse %s: %s\n", list_def, list[i]);
56 if ( min!=max && (bin->bins[i]<min || bin->bins[i]>max) )
57 error("Expected values from the interval [%f,%f], found %s\n", min, max, list[i]);
58 free(list[i]);
59 }
60 free(list);
61
62 if ( min!=max )
63 {
64 // make sure we've got both boundaries: min,max.
65 assert( nlist>1 );
66 float max_err = (bin->bins[1] - bin->bins[0])*1e-6;
67 if ( fabs(bin->bins[0] - min) > max_err )
68 {
69 bin->bins = (float*) realloc(bin->bins, (++bin->nbins)*sizeof(float));
70 memmove(bin->bins+1, bin->bins, sizeof(float)*(bin->nbins-1));
71 bin->bins[0] = min;
72 }
73 if ( fabs(bin->bins[bin->nbins-1] - max) > max_err )
74 {
75 bin->bins = (float*) realloc(bin->bins, (++bin->nbins)*sizeof(float));
76 bin->bins[bin->nbins-1] = max;
77 }
78 }
79 return bin;
80 }
81
82 void bin_destroy(bin_t *bin)
83 {
84 free(bin->bins);
85 free(bin);
86 }
87
88 int bin_get_size(bin_t *bin) { return bin->nbins; }
89
90 float bin_get_value(bin_t *bin, int idx) { return bin->bins[idx]; }
91
92 int bin_get_idx(bin_t *bin, float value)
93 {
94 if ( bin->bins[bin->nbins-1] < value ) return bin->nbins-1;
95
96 // Binary search in half-closed,half-open intervals [)
97 int imin = 0, imax = bin->nbins - 2;
98 while ( imin<imax )
99 {
100 int i = (imin+imax)/2;
101 if ( value < bin->bins[i] ) imax = i - 1;
102 else if ( value > bin->bins[i] ) imin = i + 1;
103 else return i;
104 }
105 if ( bin->bins[imax] <= value ) return imax;
106 return imin - 1;
107 }
108