Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.
 
 
 
 

201 řádky
7.5 KiB

  1. /*
  2. Copyright (C) 2014-2016, 2018 Genome Research Ltd.
  3. Author: Petr Danecek <pd3@sanger.ac.uk>
  4. Permission is hereby granted, free of charge, to any person obtaining a copy
  5. of this software and associated documentation files (the "Software"), to deal
  6. in the Software without restriction, including without limitation the rights
  7. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  8. copies of the Software, and to permit persons to whom the Software is
  9. furnished to do so, subject to the following conditions:
  10. The above copyright notice and this permission notice shall be included in
  11. all copies or substantial portions of the Software.
  12. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  13. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  14. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  15. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  16. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  17. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  18. THE SOFTWARE.
  19. */
  20. /*
  21. Region indexing with an optional payload.
  22. Example of usage:
  23. // Init the parser and print regions. In this example the payload is a
  24. // pointer to a string. For the description of parse_custom and
  25. // free_custom functions, see regidx_parse_f and regidx_free_f below,
  26. // and for working example see test/test-regidx.c.
  27. regidx_t *idx = regidx_init(in_fname,parse_custom,free_custom,sizeof(char*),NULL);
  28. // Query overlap with chr:beg-end (beg,end are 1-based coordinates)
  29. regitr_t *itr = regitr_init(idx);
  30. if ( regidx_overlap(idx, chr,beg-1,end-1, itr) ) printf("There is an overlap!\n");
  31. while ( regitr_overlap(itr) )
  32. {
  33. printf("[%d,%d] overlaps with [%d,%d], payload=%s\n", beg,end,
  34. itr->beg+1, itr->end+1, regitr_payload(itr,char*));
  35. }
  36. regidx_destroy(idx);
  37. regitr_destroy(itr);
  38. Another example, loop over all regions:
  39. regidx_t *idx = regidx_init(in_fname,NULL,NULL,0,NULL);
  40. regitr_t *itr = regitr_init(idx);
  41. while ( regitr_loop(itr) )
  42. printf("chr=%s beg=%d end=%d\n", itr->seq, itr->beg+1, itr->end+1);
  43. regidx_destroy(idx);
  44. regitr_destroy(itr);
  45. */
  46. #ifndef __REGIDX_H__
  47. #define __REGIDX_H__
  48. #include <stdio.h>
  49. #include <inttypes.h>
  50. #ifdef __cplusplus
  51. extern "C" {
  52. #endif
  53. #define REGIDX_MAX 2147483646 // maximum regidx coordinate (0-based)
  54. typedef struct _regidx_t regidx_t;
  55. typedef struct
  56. {
  57. uint32_t beg,end;
  58. void *payload;
  59. char *seq;
  60. void *itr;
  61. }
  62. regitr_t;
  63. #define regitr_payload(itr,type_t) (*((type_t*)(itr)->payload))
  64. /*
  65. * regidx_parse_f - Function to parse one input line, such as regidx_parse_bed
  66. * or regidx_parse_tab below. The function is expected to set `chr_from` and
  67. * `chr_to` to point to first and last character of chromosome name and set
  68. * coordinates `beg` and `end` (0-based, inclusive). If regidx_init() was
  69. * called with non-zero payload_size, the `payload` points to a memory
  70. * location of the payload_size and `usr` is the data passed to regidx_init().
  71. * Any memory allocated by the function will be freed by regidx_free_f called
  72. * by regidx_destroy().
  73. *
  74. * Return value: 0 on success, -1 to skip a record, -2 on fatal error.
  75. */
  76. typedef int (*regidx_parse_f)(const char *line, char **chr_beg, char **chr_end, uint32_t *beg, uint32_t *end, void *payload, void *usr);
  77. typedef void (*regidx_free_f)(void *payload);
  78. /*
  79. * A note about the parsers:
  80. * - leading spaces are ignored
  81. * - lines starting with "#" are ignored
  82. */
  83. int regidx_parse_bed(const char*,char**,char**,uint32_t*,uint32_t*,void*,void*); // CHROM or whitespace-sepatated CHROM,FROM,TO (0-based,right-open)
  84. int regidx_parse_tab(const char*,char**,char**,uint32_t*,uint32_t*,void*,void*); // CHROM or whitespace-separated CHROM,POS (1-based, inclusive)
  85. int regidx_parse_reg(const char*,char**,char**,uint32_t*,uint32_t*,void*,void*); // CHROM, CHROM:POS, CHROM:FROM-TO, CHROM:FROM- (1-based, inclusive)
  86. int regidx_parse_vcf(const char*,char**,char**,uint32_t*,uint32_t*,void*,void*);
  87. /*
  88. * regidx_init() - creates new index
  89. * regidx_init_string() - creates new index, from a string rather than from a file
  90. *
  91. * @param fname: input file name or NULL if regions will be added one-by-one via regidx_insert()
  92. * @param parsef: regidx_parse_bed, regidx_parse_tab or see description of regidx_parse_f. If NULL,
  93. * the format will be autodected, currently either regidx_parse_tab (the default) or
  94. * regidx_parse_bed (file must be named 'bed' or 'bed.gz') will be used. Note that
  95. * the exact autodetection algorithm will change.
  96. * @param freef: NULL or see description of regidx_parse_f
  97. * @param payload_size: 0 with regidx_parse_bed, regidx_parse_tab or see regidx_parse_f
  98. * @param usr: optional user data passed to regidx_parse_f
  99. *
  100. * Returns index on success or NULL on error.
  101. */
  102. regidx_t *regidx_init(const char *fname, regidx_parse_f parsef, regidx_free_f freef, size_t payload_size, void *usr);
  103. regidx_t *regidx_init_string(const char *string, regidx_parse_f parsef, regidx_free_f freef, size_t payload_size, void *usr);
  104. /*
  105. * regidx_destroy() - free memory allocated by regidx_init
  106. */
  107. void regidx_destroy(regidx_t *idx);
  108. /*
  109. * regidx_overlap() - check overlap of the location chr:from-to with regions
  110. * @param beg,end: 0-based start, end coordinate (inclusive)
  111. * @param itr: pointer to iterator, can be NULL if regidx_loop not needed
  112. *
  113. * Returns 0 if there is no overlap or 1 if overlap is found. The overlapping
  114. * regions can be iterated as shown in the example above.
  115. */
  116. int regidx_overlap(regidx_t *idx, const char *chr, uint32_t beg, uint32_t end, regitr_t *itr);
  117. /*
  118. * regidx_insert() - add a new region.
  119. * regidx_insert_list() - add new regions from a list
  120. * regidx_push() - low level insertion of a new region
  121. *
  122. * Returns 0 on success or -1 on error.
  123. */
  124. int regidx_insert(regidx_t *idx, char *line);
  125. int regidx_insert_list(regidx_t *idx, char *line, char delim);
  126. int regidx_push(regidx_t *idx, char *chr_beg, char *chr_end, uint32_t beg, uint32_t end, void *payload);
  127. /*
  128. * regidx_seq_names() - return list of all sequence names
  129. */
  130. char **regidx_seq_names(regidx_t *idx, int *n);
  131. /*
  132. * regidx_seq_nregs() - number of regions
  133. * regidx_nregs() - total number of regions
  134. */
  135. int regidx_seq_nregs(regidx_t *idx, const char *seq);
  136. int regidx_nregs(regidx_t *idx);
  137. /*
  138. * regitr_init() - initialize an iterator. The idx parameter is required only
  139. * with regitr_loop. If only regitr_overlap is called, NULL
  140. * can be given.
  141. *
  142. * regitr_reset() - initialize an iterator for a repeated regitr_loop cycle.
  143. * Not required with regitr_overlap.
  144. */
  145. regitr_t *regitr_init(regidx_t *idx);
  146. void regitr_destroy(regitr_t *itr);
  147. void regitr_reset(regidx_t *idx, regitr_t *itr);
  148. /*
  149. * regitr_overlap() - next overlapping region
  150. * Returns 0 when done or 1 when itr is set to next region
  151. */
  152. int regitr_overlap(regitr_t *itr);
  153. /*
  154. * regitr_loop() - loop over all regions
  155. * Returns 0 when done or 1 when itr is set to next region
  156. */
  157. int regitr_loop(regitr_t *itr);
  158. /*
  159. * regitr_copy() - create a copy of an iterator for a repeated iteration with regitr_loop
  160. */
  161. void regitr_copy(regitr_t *dst, regitr_t *src);
  162. #ifdef __cplusplus
  163. }
  164. #endif
  165. #endif