Debian Codegraph @

bcftools/bin.c

raw symbols SCIP
1 /* The MIT License
2
3 Copyright (c) 2016-2022 Genome Research Ltd.
4
5 Author: Petr Danecek <[email protected]>
6
7 Permission is hereby granted, free of charge, to any person obtaining a copy
8 of this software and associated documentation files (the "Software"), to deal
9 in the Software without restriction, including without limitation the rights
10 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11 copies of the Software, and to permit persons to whom the Software is
12 furnished to do so, subject to the following conditions:
13
14 The above copyright notice and this permission notice shall be included in
15 all copies or substantial portions of the Software.
16
17 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
23 THE SOFTWARE.
24
25 */
26
27 #include <stdio.h>
28 #include <assert.h>
29 #include "bcftools.h"
30 #include "bin.h"
31
32 struct _bin_t
33 {
34 float *bins;
35 int nbins;
36 };
37
38 bin_t *bin_init(const char *list_def, float min, float max)
39 {
40 bin_t *bin = (bin_t*) calloc(1,sizeof(bin_t));
41
42 // a comma indicates a list, otherwise a file
43 int is_file = strchr(list_def,',') ? 0 : 1;
44 int i, nlist;
45 char **list = hts_readlist(list_def, is_file, &nlist);
46 if ( !list ) error("Error: failed to read %s\n",list_def);
47 bin->nbins = nlist;
48 bin->bins = (float*) malloc(sizeof(float)*nlist);
49 for (i=0; i<nlist; i++)
50 {
51 char *tmp;
52 bin->bins[i] = strtod(list[i],&tmp);
53 if ( *tmp ) error("Could not parse %s: %s\n", list_def, list[i]);
54 if ( min!=max && (bin->bins[i]<min || bin->bins[i]>max) )
55 error("Expected values from the interval [%f,%f], found %s\n", min, max, list[i]);
56 free(list[i]);
57 }
58 free(list);
59
60 if ( min!=max )
61 {
62 // make sure we've got both boundaries: min,max.
63 assert( nlist>1 );
64 float max_err = (bin->bins[1] - bin->bins[0])*1e-6;
65 if ( fabs(bin->bins[0] - min) > max_err )
66 {
67 bin->bins = (float*) realloc(bin->bins, (++bin->nbins)*sizeof(float));
68 memmove(bin->bins+1, bin->bins, sizeof(float)*(bin->nbins-1));
69 bin->bins[0] = min;
70 }
71 if ( fabs(bin->bins[bin->nbins-1] - max) > max_err )
72 {
73 bin->bins = (float*) realloc(bin->bins, (++bin->nbins)*sizeof(float));
74 bin->bins[bin->nbins-1] = max;
75 }
76 }
77 return bin;
78 }
79
80 void bin_destroy(bin_t *bin)
81 {
82 free(bin->bins);
83 free(bin);
84 }
85
86 int bin_get_size(bin_t *bin) { return bin->nbins; }
87
88 float bin_get_value(bin_t *bin, int idx) { return bin->bins[idx]; }
89
90 int bin_get_idx(bin_t *bin, float value)
91 {
92 if ( bin->bins[bin->nbins-1] < value ) return bin->nbins-1;
93
94 // Binary search in half-closed,half-open intervals [)
95 int imin = 0, imax = bin->nbins - 2;
96 while ( imin<imax )
97 {
98 int i = (imin+imax)/2;
99 if ( value < bin->bins[i] ) imax = i - 1;
100 else if ( value > bin->bins[i] ) imin = i + 1;
101 else return i;
102 }
103 if ( bin->bins[imax] <= value ) return imax;
104 return imin - 1;
105 }
106