| 1 | /* The MIT License |
| 2 | |
| 3 | Copyright (c) 2016 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 | /* |
| 28 | Simple hierarchical clustering |
| 29 | */ |
| 30 | |
| 31 | #ifndef __HCLUST_H__ |
| 32 | #define __HCLUST_H__ |
| 33 | |
| 34 | #include <stdio.h> |
| 35 | |
| 36 | typedef struct _hclust_t hclust_t; |
| 37 | |
| 38 | typedef struct |
| 39 | { |
| 40 | float dist; |
| 41 | int nmemb, *memb; |
| 42 | } |
| 43 | cluster_t; |
| 44 | |
| 45 | #define PDIST(mat,a,b) (mat)[((a)>(b)?((a)*((a)-1)/2+(b)):((b)*((b)-1)/2+(a)))] |
| 46 | |
| 47 | /* |
| 48 | * hclust_init() - init and run clustering |
| 49 | * @n: number of elements |
| 50 | * @pdist: pairwise distances. The array will be modified by hclust and |
| 51 | * must exist until hclust_destroy() is called |
| 52 | */ |
| 53 | hclust_t *hclust_init(int n, float *pdist); |
| 54 | void hclust_destroy(hclust_t *clust); |
| 55 | |
| 56 | /* |
| 57 | * hclust_create_list() - returns a list of clusters |
| 58 | * @min_inter_dist: minimum inter-cluster distance. If smaller, elements are considered |
| 59 | * homogenous, belonging to the same cluster. |
| 60 | * @max_intra_dist: maximum intra-cluster distance allowed. If smaller than 0, |
| 61 | * the threshold can be heuristically lowered, otherwise considered |
| 62 | * a fixed cutoff. The pointer will be filled to the cutoff actually used. |
| 63 | */ |
| 64 | cluster_t *hclust_create_list(hclust_t *clust, float min_inter_dist, float *max_intra_dist, int *nclust); |
| 65 | void hclust_destroy_list(cluster_t *clust, int nclust); |
| 66 | |
| 67 | /* |
| 68 | * Access debugging data used in the decision making process. Note that this |
| 69 | * must be called immediately after hclust_create_list because other calls, |
| 70 | * such as hclust_create_dot(), invalidate the temporary data structures. |
| 71 | */ |
| 72 | char **hclust_explain(hclust_t *clust, int *nlines); |
| 73 | |
| 74 | char *hclust_create_dot(hclust_t *clust, char **labels, float th); |
| 75 | |
| 76 | #endif |
| 77 |