| 1 | /* |
| 2 | Copyright (C) 2014-2015 Genome Research Ltd. |
| 3 | |
| 4 | Author: Petr Danecek <[email protected]> |
| 5 | |
| 6 | Permission is hereby granted, free of charge, to any person obtaining a copy |
| 7 | of this software and associated documentation files (the "Software"), to deal |
| 8 | in the Software without restriction, including without limitation the rights |
| 9 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 10 | copies of the Software, and to permit persons to whom the Software is |
| 11 | furnished to do so, subject to the following conditions: |
| 12 | |
| 13 | The above copyright notice and this permission notice shall be included in |
| 14 | all copies or substantial portions of the Software. |
| 15 | |
| 16 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 17 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 18 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 19 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 20 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 21 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
| 22 | THE SOFTWARE. |
| 23 | */ |
| 24 | |
| 25 | /* |
| 26 | Lookup from region and sex to ploidy. |
| 27 | |
| 28 | Example of usage: |
| 29 | |
| 30 | int default_ploidy = 2; |
| 31 | ploidy_t *pld = ploidy_init(fname, default_ploidy); |
| 32 | |
| 33 | int nsex = ploidy_nsex(pld); |
| 34 | int *sex2ploidy = malloc(sizeof(int)*nsex); |
| 35 | |
| 36 | ploidy_query(pld, "X",60000, sex2ploidy, NULL, NULL); |
| 37 | for (i=0; i<nsex; i++) |
| 38 | printf("ploidy of %s is %d\n", ploidy_id2sex(pld,i), sex2ploidy[i]); |
| 39 | |
| 40 | ploidy_destroy(pld); |
| 41 | |
| 42 | An example of ploidy file format follows. The coordinates are 1-based and |
| 43 | inclusive. The "*" records define the default ploidy for each sex. If not |
| 44 | present, the default_ploidy passed to ploidy_init is used instead: |
| 45 | X 1 60000 M 1 |
| 46 | X 2699521 154931043 M 1 |
| 47 | Y 1 59373566 M 1 |
| 48 | Y 1 59373566 F 0 |
| 49 | MT 1 16569 M 1 |
| 50 | MT 1 16569 F 1 |
| 51 | * * * M 2 |
| 52 | * * * F 2 |
| 53 | */ |
| 54 | |
| 55 | #ifndef __PLOIDY_H__ |
| 56 | #define __PLOIDY_H__ |
| 57 | |
| 58 | #include "regidx.h" |
| 59 | |
| 60 | typedef struct _ploidy_t ploidy_t; |
| 61 | |
| 62 | /* |
| 63 | * ploidy_init() |
| 64 | * @param fname: input file name or NULL if default ploidy from example above should be used |
| 65 | * @param dflt: default ploidy to use for unlisted regions (the '* * *' records have precedence). |
| 66 | * |
| 67 | * Returns new structure on success or NULL on error. |
| 68 | */ |
| 69 | ploidy_t *ploidy_init(const char *fname, int dflt); |
| 70 | |
| 71 | /* Same as ploidy_init() but the whole file is passed as a single string */ |
| 72 | ploidy_t *ploidy_init_string(const char *str, int dflt); |
| 73 | |
| 74 | /* |
| 75 | * ploidy_destroy() - free memory allocated by ploidy_init |
| 76 | */ |
| 77 | void ploidy_destroy(ploidy_t *ploidy); |
| 78 | |
| 79 | /* |
| 80 | * ploidy_query() - query ploidy at a position for all genders at once |
| 81 | * @param seq: chromosome name |
| 82 | * @param pos: 0-based position |
| 83 | * @param sex2ploidy: if not NULL, array will be filled with mapping from sex id to ploidy |
| 84 | * @param min: if not NULL, minimum encountered encountered will be set |
| 85 | * @param max: if not NULL, maximum encountered encountered will be set |
| 86 | * |
| 87 | * Returns 1 if the position is listed in the regions or 0 otherwise. |
| 88 | */ |
| 89 | int ploidy_query(ploidy_t *ploidy, char *seq, int pos, int *sex2ploidy, int *min, int *max); |
| 90 | |
| 91 | /* |
| 92 | * ploidy_nsex() - return number of recognised genders |
| 93 | */ |
| 94 | int ploidy_nsex(ploidy_t *ploidy); |
| 95 | |
| 96 | /* |
| 97 | * ploidy_id2sex() - mapping between numeric gender id and name |
| 98 | * |
| 99 | * Returns gender name (e.g. "M" or "F" in the example above) |
| 100 | * or NULL if there is no such id. |
| 101 | */ |
| 102 | char *ploidy_id2sex(ploidy_t *ploidy, int id); |
| 103 | |
| 104 | /* |
| 105 | * ploidy_sex2id() - mapping between gender name and its numeric id |
| 106 | * |
| 107 | * Returns numeric id or -1 if not present. |
| 108 | */ |
| 109 | int ploidy_sex2id(ploidy_t *ploidy, char *sex); |
| 110 | |
| 111 | /* |
| 112 | * ploidy_add_sex() - register new gender name. This function is |
| 113 | * useful when gender has the default ploidy for all regions |
| 114 | * and is not listed in the file passed to ploidy_init() |
| 115 | * |
| 116 | * Returns numeric id of the added sex, regardless of whether the string was |
| 117 | * newly added or was already present in the dictionary. |
| 118 | */ |
| 119 | int ploidy_add_sex(ploidy_t *ploidy, const char *sex); |
| 120 | |
| 121 | /** Returns region index for raw access */ |
| 122 | regidx_t *ploidy_regions(ploidy_t *ploidy); |
| 123 | |
| 124 | /** Return the minimum / maximum recognised ploidy */ |
| 125 | int ploidy_max(ploidy_t *ploidy); |
| 126 | int ploidy_min(ploidy_t *ploidy); |
| 127 | |
| 128 | #endif |
| 129 |