Debian Codegraph @

bcftools/gff.c.pysam.c

raw symbols SCIP
1 #include "bcftools.pysam.h"
2
3 /* The MIT License
4
5 Copyright (c) 2023 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 #include <stdarg.h>
29 #include <stdio.h>
30 #include <stdlib.h>
31 #include <assert.h>
32 #include <inttypes.h>
33 #include <string.h>
34 #include <strings.h>
35 #include <htslib/hts.h>
36 #include <htslib/khash.h>
37 #include <htslib/khash_str2int.h>
38 #include <htslib/kseq.h>
39 #include <htslib/bgzf.h>
40 #include <errno.h>
41 #include "bcftools.h"
42 #include "regidx.h"
43 #include "gff.h"
44
45 /*
46 Helper structures, only for initialization
47
48 ftr_t
49 temporary list of all exons, CDS, UTRs
50 */
51 KHASH_MAP_INIT_INT(int2tscript, gf_tscript_t*)
52 KHASH_MAP_INIT_INT(int2gene, gf_gene_t*)
53 typedef struct
54 {
55 int type; // GF_CDS, GF_EXON, GF_5UTR, GF_3UTR
56 uint32_t beg;
57 uint32_t end;
58 uint32_t trid;
59 uint32_t strand:2; // STRAND_{REV,FWD,UNK}
60 uint32_t phase:2; // 0, 1, 2, or 3 for unknown
61 uint32_t iseq:28;
62 }
63 ftr_t;
64
65 /*
66 Mapping from GFF ID string (such as ENST00000450305 or Zm00001d027230_P001)
67 to integer id. To keep the memory requirements low, the original version
68 relied on IDs in the form of a string prefix and a numerical id. However,
69 it turns out that this assumption is not valid for some ensembl GFFs, see
70 for example Zea_mays.AGPv4.36.gff3.gz
71 */
72 typedef struct
73 {
74 void *str2id; // khash_str2int
75 int nstr, mstr;
76 char **str; // numeric id to string
77 }
78 id_tbl_t;
79
80 typedef struct
81 {
82 // all exons, CDS, UTRs
83 ftr_t *ftr;
84 int nftr, mftr;
85
86 // mapping from gene id to gf_gene_t
87 kh_int2gene_t *gid2gene;
88
89 // mapping from transcript id to tscript, for quick CDS anchoring
90 kh_int2tscript_t *id2tr;
91
92 // sequences
93 void *seq2int; // str2int hash
94 char **seq;
95 int nseq, mseq;
96
97 // ignored biotypes
98 void *ignored_biotypes;
99
100 id_tbl_t gene_ids; // temporary table for mapping between gene id (eg. Zm00001d027245) and a numeric idx
101
102 // pointers to the current partially processed line
103 char *id, *id_end, *parent, *parent_end, *biotype, *biotype_end,
104 *chr, *chr_end, *name, *name_end, *type, *type_end;
105 }
106 aux_t;
107
108 struct gff_t_
109 {
110 const char *fname, *dump_fname;
111
112 // the main regidx lookups, from chr:beg-end to overlapping features and
113 // index iterator
114 regidx_t *idx_cds, *idx_utr, *idx_exon, *idx_tscript;
115
116 // temporary structures, deleted after initializtion
117 aux_t init;
118
119 // mapping between transcript id (eg. Zm00001d027245_T001) and a numeric idx
120 id_tbl_t tscript_ids;
121
122 int strip_chr_names, verbosity;
123 int force; // force run under various conditions. Currently only to skip out-of-phase transcripts
124
125 struct {
126 int unknown_chr,unknown_tscript_biotype,unknown_strand,unknown_phase,duplicate_id;
127 int unknown_cds_phase,incomplete_cds,wrong_phase,overlapping_cds;
128 } warned;
129 };
130
131 static const char *gf_strings_noncoding[] =
132 {
133 "MT_rRNA", "MT_tRNA", "lincRNA", "miRNA", "misc_RNA", "rRNA", "snRNA", "snoRNA", "processed_transcript",
134 "antisense", "macro_lncRNA", "ribozyme", "sRNA", "scRNA", "scaRNA", "sense_intronic", "sense_overlapping",
135 "pseudogene", "processed_pseudogene", "artifact", "IG_pseudogene", "IG_C_pseudogene", "IG_J_pseudogene",
136 "IG_V_pseudogene", "TR_V_pseudogene", "TR_J_pseudogene", "MT_tRNA_pseudogene", "misc_RNA_pseudogene",
137 "miRNA_pseudogene", "ribozyme", "retained_intron", "retrotransposed", "Trna_pseudogene", "transcribed_processed_pseudogene",
138 "transcribed_unprocessed_pseudogene", "transcribed_unitary_pseudogene", "translated_unprocessed_pseudogene",
139 "translated_processed_pseudogene", "known_ncRNA", "unitary_pseudogene", "unprocessed_pseudogene",
140 "LRG_gene", "3_prime_overlapping_ncRNA", "disrupted_domain", "vaultRNA", "bidirectional_promoter_lncRNA", "ambiguous_orf",
141 "lncRNA"
142 };
143 static const char *gf_strings_coding[] = { "protein_coding", "polymorphic_pseudogene", "IG_C", "IG_D", "IG_J", "IG_LV", "IG_V", "TR_C", "TR_D", "TR_J", "TR_V", "NMD", "non_stop_decay"};
144 static const char *gf_strings_special[] = { "CDS", "exon", "3_prime_UTR", "5_prime_UTR" };
145
146 int gff_set(gff_t *gff, gff_opt_t key, ...)
147 {
148 va_list args;
149 switch (key)
150 {
151 case dump_fname:
152 va_start(args, key);
153 gff->dump_fname = va_arg(args,char*);
154 va_end(args);
155 return 0;
156
157 case force_out_of_phase:
158 va_start(args, key);
159 gff->force = va_arg(args,int);
160 va_end(args);
161 return 0;
162
163 case strip_chr_names:
164 va_start(args, key);
165 gff->strip_chr_names = va_arg(args,int);
166 va_end(args);
167 return 0;
168
169 case verbosity:
170 va_start(args, key);
171 gff->verbosity = va_arg(args,int);
172 va_end(args);
173 return 0;
174
175 default:
176 error("The key %d is not supported with gff_set\n",key);
177 }
178 return 0;
179 }
180
181 void *gff_get(gff_t *gff, gff_opt_t key)
182 {
183 switch (key)
184 {
185 case idx_cds: return gff->idx_cds;
186 case idx_utr: return gff->idx_utr;
187 case idx_exon: return gff->idx_exon;
188 case idx_tscript: return gff->idx_tscript;
189 default:
190 error("The key %d is not supported with gff_get\n",key);
191 }
192 return NULL;
193 }
194
195 const char *gff_id2string(gff_t *gff, id_type_t type, int id) // currently only transcript ids
196 {
197 return gff->tscript_ids.str[id];
198 }
199
200 const char *gf_type2gff_string(int type)
201 {
202 if ( !GF_is_coding(type) )
203 {
204 if ( type < (1<<GF_coding_bit) ) return gf_strings_noncoding[type-1];
205 type &= (1<<(GF_coding_bit+1)) - 1;
206 return gf_strings_special[type - 1];
207 }
208 type &= (1<<GF_coding_bit) - 1;
209 return gf_strings_coding[type - 1];
210 }
211
212 /*
213 gff parsing functions
214 */
215 static inline int feature_set_seq(gff_t *gff, char *chr_beg, char *chr_end)
216 {
217 aux_t *aux = &gff->init;
218 char tmp = chr_end[1];
219 chr_end[1] = 0;
220 int iseq;
221 if ( khash_str2int_get(aux->seq2int, chr_beg, &iseq)!=0 )
222 {
223 char *new_chr = strdup(chr_beg);
224 hts_expand(char*, aux->nseq+1, aux->mseq, aux->seq);
225 aux->seq[aux->nseq] = new_chr;
226 iseq = khash_str2int_inc(aux->seq2int, aux->seq[aux->nseq]);
227 aux->nseq++;
228 assert( aux->nseq < 1<<29 ); // see gf_gene_t.iseq and ftr_t.iseq
229 }
230 chr_end[1] = tmp;
231 return iseq;
232 }
233 static inline char *gff_skip(const char *line, char *ss)
234 {
235 while ( *ss && *ss!='\t' ) ss++;
236 if ( !*ss ) error("[%s:%d %s] Could not parse the line: %s\n",__FILE__,__LINE__,__FUNCTION__,line);
237 return ss+1;
238 }
239 static inline void gff_parse_chr(gff_t *gff, const char *line, char **chr_beg, char **chr_end)
240 {
241 char *se = (char*) line;
242 while ( *se && *se!='\t' ) se++;
243 if ( !*se ) error("[%s:%d %s] Could not parse the line: %s\n",__FILE__,__LINE__,__FUNCTION__,line);
244 if ( gff->strip_chr_names && !strncasecmp("chr",line,3) ) line += 3;
245 *chr_beg = (char*) line;
246 *chr_end = se-1;
247 }
248 static inline char *gff_parse_beg_end(const char *line, char *ss, uint32_t *beg, uint32_t *end)
249 {
250 char *se = ss;
251 *beg = strtol(ss, &se, 10) - 1;
252 if ( ss==se ) error("[%s:%d %s] Could not parse the line:\n\t%s\n\t%s\n",__FILE__,__LINE__,__FUNCTION__,line,ss);
253 ss = se+1;
254 *end = strtol(ss, &se, 10) - 1;
255 if ( ss==se ) error("[%s:%d %s] Could not parse the line: %s\n",__FILE__,__LINE__,__FUNCTION__,line);
256 return se+1;
257 }
258 static void gff_id_init(id_tbl_t *tbl)
259 {
260 memset(tbl, 0, sizeof(*tbl));
261 tbl->str2id = khash_str2int_init();
262 }
263 static void gff_id_destroy(id_tbl_t *tbl)
264 {
265 khash_str2int_destroy_free(tbl->str2id);
266 free(tbl->str);
267 }
268 static inline int gff_id_register(id_tbl_t *tbl, char *beg, char *end, uint32_t *id_ptr)
269 {
270 char tmp = end[1];
271 end[1] = 0;
272 int id;
273 if ( khash_str2int_get(tbl->str2id, beg, &id) < 0 )
274 {
275 id = tbl->nstr++;
276 hts_expand(char*, tbl->nstr, tbl->mstr, tbl->str);
277 tbl->str[id] = strdup(beg);
278 khash_str2int_set(tbl->str2id, tbl->str[id], id);
279 }
280 end[1] = tmp;
281 *id_ptr = id;
282 return 0;
283 }
284 static inline int gff_parse_biotype(char *line)
285 {
286 if ( !line ) return -1;
287 switch (*line)
288 {
289 case 'p':
290 if ( !strncmp(line,"protein_coding",14) ) return GF_PROTEIN_CODING;
291 else if ( !strncmp(line,"pseudogene",10) ) return GF_PSEUDOGENE;
292 else if ( !strncmp(line,"processed_transcript",20) ) return GF_PROCESSED_TRANSCRIPT;
293 else if ( !strncmp(line,"processed_pseudogene",20) ) return GF_PROCESSED_PSEUDOGENE;
294 else if ( !strncmp(line,"polymorphic_pseudogene",22) ) return GF_POLYMORPHIC_PSEUDOGENE;
295 break;
296 case 'a':
297 if ( !strncmp(line,"artifact",8) ) return GF_ARTIFACT;
298 else if ( !strncmp(line,"antisense",9) ) return GF_ANTISENSE;
299 else if ( !strncmp(line,"ambiguous_orf",13) ) return GF_AMBIGUOUS_ORF;
300 break;
301 case 'I':
302 if ( !strncmp(line,"IG_pseudogene",13) ) return GF_IG_PSEUDOGENE;
303 else if ( !strncmp(line,"IG_C_pseudogene",15) ) return GF_IG_C_PSEUDOGENE;
304 else if ( !strncmp(line,"IG_J_pseudogene",15) ) return GF_IG_J_PSEUDOGENE;
305 else if ( !strncmp(line,"IG_V_pseudogene",15) ) return GF_IG_V_PSEUDOGENE;
306 else if ( !strncmp(line,"IG_C",4) ) return GF_IG_C;
307 else if ( !strncmp(line,"IG_D",4) ) return GF_IG_D;
308 else if ( !strncmp(line,"IG_J",4) ) return GF_IG_J;
309 else if ( !strncmp(line,"IG_V",4) ) return GF_IG_V;
310 else if ( !strncmp(line,"IG_LV",5) ) return GF_IG_LV;
311 break;
312 case 'T':
313 if ( !strncmp(line,"TR_V_pseudogene",15) ) return GF_TR_V_PSEUDOGENE;
314 else if ( !strncmp(line,"TR_J_pseudogene",15) ) return GF_TR_J_PSEUDOGENE;
315 else if ( !strncmp(line,"TR_C",4) ) return GF_TR_C;
316 else if ( !strncmp(line,"TR_D",4) ) return GF_TR_D;
317 else if ( !strncmp(line,"TR_J",4) ) return GF_TR_J;
318 else if ( !strncmp(line,"TR_V",4) ) return GF_TR_V;
319 break;
320 case 'M':
321 if ( !strncmp(line,"Mt_tRNA_pseudogene",18) ) return GF_MT_tRNA_PSEUDOGENE;
322 else if ( !strncasecmp(line,"Mt_tRNA",7) ) return GF_MT_tRNA;
323 else if ( !strncasecmp(line,"Mt_rRNA",7) ) return GF_MT_tRNA;
324 else if ( !strncasecmp(line,"MRNA",4) ) return GF_PROTEIN_CODING;
325 break;
326 case 'l':
327 if ( !strncmp(line,"lincRNA",7) ) return GF_lincRNA;
328 if ( !strncmp(line,"lncRNA",7) ) return GF_lncRNA;
329 break;
330 case 'm':
331 if ( !strncmp(line,"macro_lncRNA",12) ) return GF_macro_lncRNA;
332 else if ( !strncmp(line,"misc_RNA_pseudogene",19) ) return GF_misc_RNA_PSEUDOGENE;
333 else if ( !strncmp(line,"miRNA_pseudogene",16) ) return GF_miRNA_PSEUDOGENE;
334 else if ( !strncmp(line,"miRNA",5) ) return GF_miRNA;
335 else if ( !strncmp(line,"misc_RNA",8) ) return GF_MISC_RNA;
336 else if ( !strncasecmp(line,"mRNA",4) ) return GF_PROTEIN_CODING;
337 break;
338 case 'r':
339 if ( !strncmp(line,"rRNA",4) ) return GF_rRNA;
340 else if ( !strncmp(line,"ribozyme",8) ) return GF_RIBOZYME;
341 else if ( !strncmp(line,"retained_intron",15) ) return GF_RETAINED_INTRON;
342 else if ( !strncmp(line,"retrotransposed",15) ) return GF_RETROTRANSPOSED;
343 break;
344 case 's':
345 if ( !strncmp(line,"snRNA",5) ) return GF_snRNA;
346 else if ( !strncmp(line,"sRNA",4) ) return GF_sRNA;
347 else if ( !strncmp(line,"scRNA",5) ) return GF_scRNA;
348 else if ( !strncmp(line,"scaRNA",6) ) return GF_scaRNA;
349 else if ( !strncmp(line,"snoRNA",6) ) return GF_snoRNA;
350 else if ( !strncmp(line,"sense_intronic",14) ) return GF_SENSE_INTRONIC;
351 else if ( !strncmp(line,"sense_overlapping",17) ) return GF_SENSE_OVERLAPPING;
352 break;
353 case 't':
354 if ( !strncmp(line,"tRNA_pseudogene",15) ) return GF_tRNA_PSEUDOGENE;
355 else if ( !strncmp(line,"transcribed_processed_pseudogene",32) ) return GF_TRANSCRIBED_PROCESSED_PSEUDOGENE;
356 else if ( !strncmp(line,"transcribed_unprocessed_pseudogene",34) ) return GF_TRANSCRIBED_UNPROCESSED_PSEUDOGENE;
357 else if ( !strncmp(line,"transcribed_unitary_pseudogene",30) ) return GF_TRANSCRIBED_UNITARY_PSEUDOGENE;
358 else if ( !strncmp(line,"translated_unprocessed_pseudogene",33) ) return GF_TRANSLATED_UNPROCESSED_PSEUDOGENE;
359 else if ( !strncmp(line,"translated_processed_pseudogene",31) ) return GF_TRANSLATED_PROCESSED_PSEUDOGENE;
360 break;
361 case 'n':
362 if ( !strncmp(line,"nonsense_mediated_decay",23) ) return GF_NMD;
363 else if ( !strncmp(line,"non_stop_decay",14) ) return GF_NON_STOP_DECAY;
364 break;
365 case 'N':
366 if ( !strncmp(line,"NMD",3) ) return GF_NMD;
367 break;
368 case 'k':
369 if ( !strncmp(line,"known_ncrna",11) ) return GF_KNOWN_NCRNA;
370 break;
371 case 'u':
372 if ( !strncmp(line,"unitary_pseudogene",18) ) return GF_UNITARY_PSEUDOGENE;
373 else if ( !strncmp(line,"unprocessed_pseudogene",22) ) return GF_UNPROCESSED_PSEUDOGENE;
374 break;
375 case 'L':
376 if ( !strncmp(line,"LRG_gene",8) ) return GF_LRG_GENE;
377 break;
378 case '3':
379 if ( !strncasecmp(line,"3prime_overlapping_ncRNA",24) ) return GF_3PRIME_OVERLAPPING_ncRNA;
380 else if ( !strncasecmp(line,"3_prime_overlapping_ncRNA",25) ) return GF_3PRIME_OVERLAPPING_ncRNA;
381 break;
382 case 'd':
383 if ( !strncmp(line,"disrupted_domain",16) ) return GF_DISRUPTED_DOMAIN;
384 break;
385 case 'v':
386 if ( !strncmp(line,"vaultRNA",8) ) return GF_vaultRNA;
387 break;
388 case 'b':
389 if ( !strncmp(line,"bidirectional_promoter_lncRNA",29) ) return GF_BIDIRECTIONAL_PROMOTER_lncRNA;
390 break;
391 }
392 return 0;
393 }
394 static inline int gff_ignored_biotype(gff_t *gff, char *ss, char *se)
395 {
396 if ( !ss ) return 0;
397
398 char tmp = se[1];
399 se[1] = 0;
400
401 char *key = ss;
402 int n = 0;
403 if ( khash_str2int_get(gff->init.ignored_biotypes, ss, &n)!=0 ) key = strdup(ss);
404 khash_str2int_set(gff->init.ignored_biotypes, key, n+1);
405
406 se[1] = tmp;
407 return 1;
408 }
409 static gf_gene_t *gene_init(aux_t *aux, uint32_t gene_id)
410 {
411 khint_t k = kh_get(int2gene, aux->gid2gene, (int)gene_id);
412 gf_gene_t *gene = (k == kh_end(aux->gid2gene)) ? NULL : kh_val(aux->gid2gene, k);
413 if ( !gene )
414 {
415 gene = (gf_gene_t*) calloc(1,sizeof(gf_gene_t));
416 int ret;
417 k = kh_put(int2gene, aux->gid2gene, (int)gene_id, &ret);
418 kh_val(aux->gid2gene,k) = gene;
419 }
420 return gene;
421 }
422 static void gff_parse_transcript(gff_t *gff, const char *line, ftr_t *ftr)
423 {
424 aux_t *aux = &gff->init;
425
426 ftr->type = gff_parse_biotype(aux->biotype);
427 if ( ftr->type <= 0 )
428 {
429 char tmp = aux->type_end[1];
430 aux->type_end[1] = 0;
431 ftr->type = gff_parse_biotype(aux->type);
432 aux->type_end[1] = tmp;
433 }
434 if ( ftr->type <= 0 )
435 {
436 if ( !gff_ignored_biotype(gff,aux->biotype,aux->biotype_end) )
437 {
438 if ( gff->verbosity > 0 )
439 {
440 if ( !gff->warned.unknown_tscript_biotype || gff->verbosity > 1 )
441 fprintf(bcftools_stderr,"Warning: Ignoring transcript with unknown biotype .. %s\n", line);
442 gff->warned.unknown_tscript_biotype++;
443 }
444 }
445 return;
446 }
447
448 if ( !aux->id )
449 error("[%s:%d %s] Could not parse the line, neither \"ID=transcript:\" nor \"ID=\" substring is present: %s\n",__FILE__,__LINE__,__FUNCTION__,line);
450 if ( !aux->parent )
451 error("[%s:%d %s] Could not parse the line, neither \"Parent=gene:\" nor \"Parent=\" substring is present: %s\n",__FILE__,__LINE__,__FUNCTION__,line);
452
453 uint32_t trid,gene_id;
454 gff_id_register(&gff->tscript_ids, aux->id, aux->id_end, &trid);
455 gff_id_register(&aux->gene_ids, aux->parent, aux->parent_end, &gene_id);
456
457 gf_tscript_t *tr = (gf_tscript_t*) calloc(1,sizeof(gf_tscript_t));
458 tr->id = trid;
459 tr->strand = ftr->strand;
460 tr->gene = gene_init(aux, gene_id);
461 tr->type = ftr->type;
462 tr->beg = ftr->beg;
463 tr->end = ftr->end;
464
465 khint_t k;
466 int ret;
467 k = kh_put(int2tscript, aux->id2tr, (int)trid, &ret);
468 kh_val(aux->id2tr,k) = tr;
469 }
470 // register exon, CDS, UTR
471 static void gff_parse_exon(gff_t *gff, const char *line, ftr_t *ftr)
472 {
473 aux_t *aux = &gff->init;
474 if ( !aux->parent )
475 error("[%s:%d %s] Could not parse the line, neither \"Parent=transcript:\" nor \"Parent=\" substring found: %s\n",__FILE__,__LINE__,__FUNCTION__,line);
476
477 // associate with transcript id
478 gff_id_register(&gff->tscript_ids, aux->parent, aux->parent_end, &ftr->trid);
479
480 if ( ftr->strand==STRAND_UNK && gff->verbosity > 0 )
481 {
482 if ( !gff->warned.unknown_strand || gff->verbosity > 1 )
483 fprintf(bcftools_stderr,"Warning: Ignoring GFF feature with unknown strand .. %s\n",line);
484 gff->warned.unknown_strand++;
485 }
486 if ( ftr->phase==CDS_PHASE_UNKN && gff->verbosity > 0 )
487 {
488 if ( !gff->warned.unknown_phase|| gff->verbosity > 1 )
489 fprintf(bcftools_stderr,"Warning: Ignoring GFF feature with unknown phase .. %s\n",line);
490 gff->warned.unknown_phase++;
491 }
492 ftr->iseq = feature_set_seq(gff, aux->chr,aux->chr_end);
493 }
494 static void gff_parse_gene(gff_t *gff, const char *line, ftr_t *ftr)
495 {
496 aux_t *aux = &gff->init;
497 if ( !aux->id ) return;
498
499 uint32_t gene_id;
500 gff_id_register(&aux->gene_ids, aux->id, aux->id_end, &gene_id);
501
502 gf_gene_t *gene = gene_init(aux, gene_id);
503 if ( gene->name )
504 {
505 if ( !gff->warned.duplicate_id || gff->verbosity > 1 )
506 fprintf(bcftools_stderr,"Warning: The GFF contains features with duplicate id .. %s\n",line);
507 gff->warned.duplicate_id++;
508 return;
509 }
510
511 gene->iseq = feature_set_seq(gff, aux->chr,aux->chr_end);
512 gene->beg = ftr->beg;
513 gene->end = ftr->end;
514 gene->strand = ftr->strand;
515 gene->id = gene_id;
516
517 if ( aux->name )
518 {
519 gene->name = (char*) malloc(aux->name_end - aux->name + 2);
520 memcpy(gene->name,aux->name,aux->name_end - aux->name + 1);
521 gene->name[aux->name_end - aux->name + 1] = 0;
522 }
523 else
524 gene->name = strdup(aux->gene_ids.str[gene_id]); // Name=<GeneName> field is not present, use the gene ID instead
525 }
526
527 // Returns 0 for exons,CDS,UTRs to indicate these need to be pruned later and regidx built on them,
528 // or -1 to indicate the structure needs not be saved (either because of an error or because saved
529 // as transcript or gene.)
530 static int gff_parse_line(gff_t *gff, char *line, ftr_t *ftr)
531 {
532 // - skip empty lines and commented lines
533 // - columns
534 // 1. chr
535 // 2. <skip>
536 // 3. CDS, transcript, gene, ...
537 // 4-5. beg,end
538 // 6. <skip>
539 // 7. strand
540 // 8. phase
541 // 9. Parent=transcript:ENST(\d+);ID=...;biotype=... etc
542
543 char *ss = line;
544 if ( !*ss ) return -1; // skip blank lines
545 if ( *ss=='#' ) return -1; // skip comments
546
547 aux_t *aux = &gff->init;
548 gff_parse_chr(gff, line, &aux->chr, &aux->chr_end);
549 ss = gff_skip(line, aux->chr_end + 2);
550
551 // 3rd column: is this a CDS, transcript, gene, etc.. The parsing order by frequency in Homo_sapiens.GRCh37.87.gff3
552 int is_gene_line = 0;
553 ftr->type = 0;
554 aux->type = ss;
555 if ( !strncmp("exon\t",ss,5) ) { ftr->type = GF_EXON; ss += 5; }
556 else if ( !strncmp("CDS\t",ss,4) ) { ftr->type = GF_CDS; ss += 4; }
557 else if ( !strncmp("three_prime_UTR\t",ss,16) ) { ftr->type = GF_UTR3; ss += 16; }
558 else if ( !strncmp("five_prime_UTR\t",ss,15) ) { ftr->type = GF_UTR5; ss += 15; }
559 else if ( !strncmp("biological_region\t",ss,18) ) { return -1; } // skip
560 else if ( !strncmp("gene\t",ss,5) ) { is_gene_line = 1; ss += 5; }
561 else ss = gff_skip(line, ss);
562 aux->type_end = ss - 1;
563
564 // 4-5th columns: beg,end
565 ss = gff_parse_beg_end(line, ss, &ftr->beg,&ftr->end);
566
567 // 6th column: skip
568 ss = gff_skip(line, ss);
569
570 // 7th column: strand
571 ftr->strand = -1;
572 if ( *ss == '+' ) ftr->strand = STRAND_FWD;
573 else if ( *ss == '-' ) ftr->strand = STRAND_REV;
574 else ftr->strand = STRAND_UNK;
575 ss += 2;
576
577 // 8th column: phase (codon offset)
578 ftr->phase = CDS_PHASE_UNKN;
579 if ( *ss == '0' ) ftr->phase = 0;
580 else if ( *ss == '1' ) ftr->phase = 1;
581 else if ( *ss == '2' ) ftr->phase = 2;
582 else if ( *ss == '.' ) ftr->phase = CDS_PHASE_UNKN; // exons and even CDS in some GFFs do not have phase
583 ss += 2;
584
585 // 9th column: id, parent, name, biotype
586 aux->name = NULL, aux->id = NULL, aux->parent = NULL, aux->biotype = NULL;
587 while ( *ss )
588 {
589 char *es = ss;
590 while ( *es && *es!=';' ) es++;
591 if ( !strncmp(ss,"ID=",3) )
592 {
593 ss += 3;
594 aux->id_end = es - 1;
595 aux->id = ss;
596 if ( !strncmp(ss,"gene:",5) ) { aux->id += 5; is_gene_line = 1; }
597 else if ( !strncmp(ss,"transcript:",11) ) aux->id += 11;
598 }
599 else if ( !strncmp(ss,"Name=",5) ) { aux->name = ss + 5; aux->name_end = es - 1; }
600 else if ( !strncmp(ss,"Parent=",7) )
601 {
602 ss += 7;
603 aux->parent_end = es - 1;
604 aux->parent = ss;
605 if ( !strncmp(ss,"gene:",5) ) aux->parent += 5;
606 else if ( !strncmp(ss,"transcript:",11) ) aux->parent += 11;
607 }
608 else if ( !strncmp(ss,"biotype=",8) ) { aux->biotype = ss + 8; aux->biotype_end = es - 1; }
609 else if ( !strncmp(ss,"gene_biotype=",13) ) { aux->biotype = ss + 13; aux->biotype_end = es - 1; }
610 if ( !*es ) break;
611 ss = es + 1;
612 }
613
614 if ( is_gene_line || !aux->parent )
615 {
616 gff_parse_gene(gff, line, ftr);
617 return -1;
618 }
619
620 if ( ftr->type )
621 {
622 gff_parse_exon(gff, line, ftr);
623 return 0;
624 }
625
626 gff_parse_transcript(gff, line, ftr);
627 return -1;
628 }
629
630 static int cmp_cds_ptr(const void *a, const void *b)
631 {
632 // comparison function for qsort of transcripts's CDS
633 if ( (*((gf_cds_t**)a))->beg < (*((gf_cds_t**)b))->beg ) return -1;
634 if ( (*((gf_cds_t**)a))->beg > (*((gf_cds_t**)b))->beg ) return 1;
635 return 0;
636 }
637
638 static inline void chr_beg_end(aux_t *aux, int iseq, char **chr_beg, char **chr_end)
639 {
640 *chr_beg = *chr_end = aux->seq[iseq];
641 while ( (*chr_end)[1] ) (*chr_end)++;
642 }
643 static gf_tscript_t *tscript_init(aux_t *aux, uint32_t trid)
644 {
645 khint_t k = kh_get(int2tscript, aux->id2tr, (int)trid);
646 gf_tscript_t *tr = (k == kh_end(aux->id2tr)) ? NULL : kh_val(aux->id2tr, k);
647 assert( tr );
648 return tr;
649 }
650 static void register_cds(gff_t *gff, ftr_t *ftr)
651 {
652 // Make the CDS searchable via idx_cds. Note we do not malloc tr->cds just yet.
653 // ftr is the result of parsing a gff CDS line
654 aux_t *aux = &gff->init;
655
656 gf_tscript_t *tr = tscript_init(aux, ftr->trid);
657 if ( tr->strand != ftr->strand ) error("Conflicting strand in transcript %"PRIu32" .. %d vs %d\n",ftr->trid,tr->strand,ftr->strand);
658
659 gf_cds_t *cds = (gf_cds_t*) malloc(sizeof(gf_cds_t));
660 cds->tr = tr;
661 cds->beg = ftr->beg;
662 cds->len = ftr->end - ftr->beg + 1;
663 cds->icds = 0; // to keep valgrind on mac happy
664 cds->phase = ftr->phase;
665
666 hts_expand(gf_cds_t*,tr->ncds+1,tr->mcds,tr->cds);
667 tr->cds[tr->ncds++] = cds;
668 }
669 static void register_utr(gff_t *gff, ftr_t *ftr)
670 {
671 aux_t *aux = &gff->init;
672 gf_utr_t *utr = (gf_utr_t*) malloc(sizeof(gf_utr_t));
673 utr->which = ftr->type==GF_UTR3 ? prime3 : prime5;
674 utr->beg = ftr->beg;
675 utr->end = ftr->end;
676 utr->tr = tscript_init(aux, ftr->trid);
677
678 char *chr_beg, *chr_end;
679 chr_beg_end(&gff->init, utr->tr->gene->iseq, &chr_beg, &chr_end);
680 regidx_push(gff->idx_utr, chr_beg,chr_end, utr->beg,utr->end, &utr);
681 }
682 static void register_exon(gff_t *gff, ftr_t *ftr)
683 {
684 aux_t *aux = &gff->init;
685 gf_exon_t *exon = (gf_exon_t*) malloc(sizeof(gf_exon_t));
686 exon->beg = ftr->beg;
687 exon->end = ftr->end;
688 exon->tr = tscript_init(aux, ftr->trid);
689
690 char *chr_beg, *chr_end;
691 chr_beg_end(&gff->init, exon->tr->gene->iseq, &chr_beg, &chr_end);
692 regidx_push(gff->idx_exon, chr_beg,chr_end, exon->beg - N_SPLICE_REGION_INTRON, exon->end + N_SPLICE_REGION_INTRON, &exon);
693 }
694
695 static void tscript_init_cds(gff_t *gff)
696 {
697 aux_t *aux = &gff->init;
698
699 // Sort CDS in all transcripts, set offsets, check their phase, length, create index (idx_cds)
700 khint_t k;
701 for (k=0; k<kh_end(aux->id2tr); k++)
702 {
703 if ( !kh_exist(aux->id2tr, k) ) continue;
704 gf_tscript_t *tr = (gf_tscript_t*) kh_val(aux->id2tr, k);
705
706 // position-to-tscript lookup
707 char *chr_beg, *chr_end;
708 chr_beg_end(aux, tr->gene->iseq, &chr_beg, &chr_end);
709 regidx_push(gff->idx_tscript, chr_beg, chr_end, tr->beg, tr->end, &tr);
710
711 if ( !tr->ncds ) continue; // transcript with no CDS
712
713 // sort CDs
714 qsort(tr->cds, tr->ncds, sizeof(gf_cds_t*), cmp_cds_ptr);
715
716 // trim non-coding start
717 int i, len = 0;
718 if ( tr->strand==STRAND_FWD )
719 {
720 if ( tr->cds[0]->phase != CDS_PHASE_UNKN )
721 {
722 if ( tr->cds[0]->phase ) tr->trim |= TRIM_5PRIME;
723 tr->cds[0]->beg += tr->cds[0]->phase;
724 tr->cds[0]->len -= tr->cds[0]->phase;
725 tr->cds[0]->phase = 0;
726 }
727
728 // sanity check phase; the phase number in gff tells us how many bases to skip in this
729 // feature to reach the first base of the next codon
730 int tscript_ok = 1;
731 for (i=0; i<tr->ncds; i++)
732 {
733 if ( tr->cds[i]->phase == CDS_PHASE_UNKN )
734 {
735 if ( gff->verbosity > 0 )
736 {
737 if ( !gff->warned.unknown_cds_phase || gff->verbosity > 1 )
738 fprintf(bcftools_stderr,"Warning: CDS with unknown phase, could not verify reading frame in transcript %s\n",gff->tscript_ids.str[tr->id]);
739 gff->warned.unknown_cds_phase++;
740 }
741 len += tr->cds[i]->len;
742 continue;
743 }
744 int phase = tr->cds[i]->phase ? 3 - tr->cds[i]->phase : 0;
745 if ( phase!=len%3 )
746 {
747 if ( !gff->force )
748 error("Error: GFF3 assumption failed for transcript %s, CDS=%"PRIu32": phase!=len%%3 (phase=%d, len=%d). Use the --force option to proceed anyway (at your own risk).\n",
749 gff->tscript_ids.str[tr->id],tr->cds[i]->beg+1,phase,len);
750 if ( gff->verbosity > 0 )
751 {
752 if ( !gff->warned.wrong_phase || gff->verbosity > 1 )
753 fprintf(bcftools_stderr,"Warning: The GFF has inconsistent phase column in transcript %s, skipping. CDS pos=%"PRIu32": phase!=len%%3 (phase=%d, len=%d)\n",
754 gff->tscript_ids.str[tr->id],tr->cds[i]->beg+1,phase,len);
755 gff->warned.wrong_phase++;
756 }
757 tscript_ok = 0;
758 break;
759 }
760 len += tr->cds[i]->len;
761 }
762 if ( !tscript_ok ) continue; // skip this transcript
763 }
764 else if ( tr->strand==STRAND_REV )
765 {
766 if ( tr->cds[tr->ncds-1]->phase != CDS_PHASE_UNKN )
767 {
768 // Check that the phase is not bigger than CDS length. Curiously, this can really happen,
769 // see Mus_musculus.GRCm38.85.gff3.gz, transcript:ENSMUST00000163141.
770 // This also fixes phase of 5' incomplete CDS, see test/csq/ENST00000520868/ENST00000520868.gff
771 // todo: the same for the fwd strand
772 i = tr->ncds - 1;
773 int phase = tr->cds[i]->phase;
774 if ( phase ) tr->trim |= TRIM_5PRIME;
775 while ( i>=0 && phase > tr->cds[i]->len )
776 {
777 phase -= tr->cds[i]->len;
778 tr->cds[i]->phase = 0;
779 tr->cds[i]->len = 0;
780 i--;
781 }
782 if ( gff->verbosity > 0 && tr->cds[i]->phase )
783 {
784 if ( !gff->warned.incomplete_cds || gff->verbosity > 1 )
785 fprintf(bcftools_stderr,"Note: truncated transcript %s with incomplete CDS (this is very common)\n",gff->tscript_ids.str[tr->id]);
786 gff->warned.incomplete_cds++;
787 }
788 tr->cds[i]->len -= tr->cds[i]->phase;
789 tr->cds[i]->phase = 0;
790 }
791
792 // sanity check phase
793 int tscript_ok = 1;
794 for (i=tr->ncds-1; i>=0; i--)
795 {
796 if ( tr->cds[i]->phase == CDS_PHASE_UNKN )
797 {
798 if ( gff->verbosity > 0 )
799 {
800 if ( !gff->warned.unknown_cds_phase || gff->verbosity > 1 )
801 fprintf(bcftools_stderr,"Warning: CDS with unknown phase, could not verify reading frame in transcript %s\n",gff->tscript_ids.str[tr->id]);
802 gff->warned.unknown_cds_phase++;
803 }
804 len += tr->cds[i]->len;
805 continue;
806 }
807 int phase = tr->cds[i]->phase ? 3 - tr->cds[i]->phase : 0;
808 if ( phase!=len%3 )
809 {
810 if ( !gff->force )
811 error("Error: GFF3 assumption failed for transcript %s, CDS=%"PRIu32": phase!=len%%3 (phase=%d, len=%d). Use the --force option to proceed anyway (at your own risk).\n",
812 gff->tscript_ids.str[tr->id],tr->cds[i]->beg+1,phase,len);
813 if ( gff->verbosity > 0 )
814 {
815 if ( !gff->warned.wrong_phase || gff->verbosity > 1 )
816 fprintf(bcftools_stderr,"Warning: The GFF has inconsistent phase column in transcript %s, skipping. CDS pos=%"PRIu32": phase!=len%%3 (phase=%d, len=%d)\n",
817 gff->tscript_ids.str[tr->id],tr->cds[i]->beg+1,phase,len);
818 gff->warned.wrong_phase++;
819 }
820 tscript_ok = 0;
821 break;
822 }
823 len += tr->cds[i]->len;
824 }
825 if ( !tscript_ok ) continue; // skip this transcript
826 }
827 else
828 continue; // unknown strand
829
830 // set len. At the same check that CDS within a transcript do not overlap
831 len = 0;
832 for (i=0; i<tr->ncds; i++)
833 {
834 tr->cds[i]->icds = i;
835 len += tr->cds[i]->len;
836 if ( !i ) continue;
837
838 gf_cds_t *a = tr->cds[i-1];
839 gf_cds_t *b = tr->cds[i];
840 if ( a->beg + a->len - 1 >= b->beg )
841 {
842 if ( gff->verbosity > 0 )
843 {
844 if ( !gff->warned.overlapping_cds || gff->verbosity > 1 )
845 fprintf(bcftools_stderr,"Warning: GFF contains overlapping CDS %s, %"PRIu32"-%"PRIu32" and %"PRIu32"-%"PRIu32" (ribosomal slippage?)\n",
846 gff->tscript_ids.str[tr->id], a->beg+1,a->beg+a->len, b->beg+1,b->beg+b->len);
847 gff->warned.overlapping_cds++;
848 }
849 }
850 }
851
852 if ( len%3 != 0 )
853 {
854 // There are 13k transcripts with incomplete 3' CDS. See for example ENST00000524289
855 // http://sep2015.archive.ensembl.org/Homo_sapiens/Transcript/Sequence_cDNA?db=core;g=ENSG00000155868;r=5:157138846-157159019;t=ENST00000524289
856 // Also, the incomplete CDS can be too short (1 or 2bp), so it is not enough to trim the last one.
857
858 if ( gff->verbosity > 0 )
859 {
860 if ( !gff->warned.incomplete_cds || gff->verbosity > 1 )
861 fprintf(bcftools_stderr,"Note: truncated transcript %s with incomplete CDS (this is very common)\n",gff->tscript_ids.str[tr->id]);
862 gff->warned.incomplete_cds++;
863 }
864
865 tr->trim |= TRIM_3PRIME;
866 if ( tr->strand==STRAND_FWD )
867 {
868 i = tr->ncds - 1;
869 while ( i>=0 && len%3 )
870 {
871 int dlen = tr->cds[i]->len >= len%3 ? len%3 : tr->cds[i]->len;
872 tr->cds[i]->len -= dlen;
873 len -= dlen;
874 i--;
875 }
876 }
877 else if ( tr->strand==STRAND_REV )
878 {
879 i = 0;
880 while ( i<tr->ncds && len%3 )
881 {
882 int dlen = tr->cds[i]->len >= len%3 ? len%3 : tr->cds[i]->len;
883 tr->cds[i]->len -= dlen;
884 tr->cds[i]->beg += dlen;
885 len -= dlen;
886 i++;
887 }
888 }
889 }
890
891 // set CDS offsets and insert into regidx
892 len=0;
893 for (i=0; i<tr->ncds; i++)
894 {
895 tr->cds[i]->pos = len;
896 len += tr->cds[i]->len;
897 regidx_push(gff->idx_cds, chr_beg,chr_end, tr->cds[i]->beg,tr->cds[i]->beg+tr->cds[i]->len-1, &tr->cds[i]);
898 }
899 }
900 }
901
902 static void regidx_free_gf(void *payload) { free(*((gf_cds_t**)payload)); }
903 static void regidx_free_tscript(void *payload) { gf_tscript_t *tr = *((gf_tscript_t**)payload); free(tr->cds); free(tr); }
904
905 static int gff_dump(gff_t *gff, const char *fname)
906 {
907 BGZF *out = bgzf_open(fname,"wg");
908 if ( !out ) error("Failed to open %s: %s\n", fname, strerror(errno));
909
910 kstring_t str = {0,0,0};
911
912 khint_t k;
913 for (k=0; k<kh_end(gff->init.gid2gene); k++)
914 {
915 if ( !kh_exist(gff->init.gid2gene, k) ) continue;
916 gf_gene_t *gene = (gf_gene_t*) kh_val(gff->init.gid2gene, k);
917 char *gene_id = gff->init.gene_ids.str[gene->id];
918 str.l = 0;
919 ksprintf(&str,"%s\t.\tgene\t%"PRIu32"\t%"PRIu32"\t.\t%c\t.\tID=%s;Name=%s;used=%d\n",gff->init.seq[gene->iseq],gene->beg+1,gene->end+1,gene->strand==STRAND_FWD?'+':(gene->strand==STRAND_REV?'-':'.'),gene_id,gene->name,gene->used);
920 if ( bgzf_write(out, str.s, str.l) != str.l ) error("Error writing %s: %s\n", fname, strerror(errno));
921 }
922
923 regitr_t *itr = regitr_init(gff->idx_tscript);
924 while ( regitr_loop(itr) )
925 {
926 gf_tscript_t *tr = regitr_payload(itr, gf_tscript_t*);
927 char *gene_id = gff->init.gene_ids.str[tr->gene->id];
928 const char *type = tr->type==GF_PROTEIN_CODING ? "mRNA" : gf_type2gff_string(tr->type);
929 str.l = 0;
930 ksprintf(&str,"%s\t.\t%s\t%"PRIu32"\t%"PRIu32"\t.\t%c\t.\tID=%s;Parent=%s;biotype=%s;used=%d\n",itr->seq,type,itr->beg+1,itr->end+1,tr->strand==STRAND_FWD?'+':(tr->strand==STRAND_REV?'-':'.'),gff->tscript_ids.str[tr->id],gene_id,gf_type2gff_string(tr->type),tr->used);
931 if ( bgzf_write(out, str.s, str.l) != str.l ) error("Error writing %s: %s\n", fname, strerror(errno));
932 }
933 regitr_destroy(itr);
934
935 itr = regitr_init(gff->idx_cds);
936 while ( regitr_loop(itr) )
937 {
938 gf_cds_t *cds = regitr_payload(itr,gf_cds_t*);
939 gf_tscript_t *tr = cds->tr;
940 str.l = 0;
941 ksprintf(&str,"%s\t.\tCDS\t%"PRIu32"\t%"PRIu32"\t.\t%c\t%c\tParent=%s\n",itr->seq,cds->beg+1,cds->beg+cds->len,tr->strand==STRAND_FWD?'+':(tr->strand==STRAND_REV?'-':'.'),cds->phase==3?'.':cds->phase+(int)'0',gff->tscript_ids.str[tr->id]);
942 if ( bgzf_write(out, str.s, str.l) != str.l ) error("Error writing %s: %s\n", fname, strerror(errno));
943 }
944 regitr_destroy(itr);
945
946 itr = regitr_init(gff->idx_utr);
947 while ( regitr_loop(itr) )
948 {
949 gf_utr_t *utr = regitr_payload(itr,gf_utr_t*);
950 gf_tscript_t *tr = utr->tr;
951 str.l = 0;
952 ksprintf(&str,"%s\t.\t%s_prime_UTR\t%"PRIu32"\t%"PRIu32"\t.\t%c\t.\tParent=%s\n",itr->seq,utr->which==prime3?"three":"five",utr->beg+1,utr->end+1,tr->strand==STRAND_FWD?'+':(tr->strand==STRAND_REV?'-':'.'),gff->tscript_ids.str[tr->id]);
953 if ( bgzf_write(out, str.s, str.l) != str.l ) error("Error writing %s: %s\n", fname, strerror(errno));
954 }
955 regitr_destroy(itr);
956
957 itr = regitr_init(gff->idx_exon);
958 while ( regitr_loop(itr) )
959 {
960 gf_exon_t *exon = regitr_payload(itr,gf_exon_t*);
961 gf_tscript_t *tr = exon->tr;
962 str.l = 0;
963 ksprintf(&str,"%s\t.\texon\t%"PRIu32"\t%"PRIu32"\t.\t%c\t.\tParent=%s\n",itr->seq,exon->beg+1,exon->end+1,tr->strand==STRAND_FWD?'+':(tr->strand==STRAND_REV?'-':'.'),gff->tscript_ids.str[tr->id]);
964 if ( bgzf_write(out, str.s, str.l) != str.l ) error("Error writing %s: %s\n", fname, strerror(errno));
965 }
966 regitr_destroy(itr);
967
968 if ( bgzf_close(out)!=0 ) error("Error: close failed .. %s\n", fname);
969 free(str.s);
970
971 return 0;
972 }
973
974 int gff_parse(gff_t *gff)
975 {
976 if ( gff->verbosity > 0 ) fprintf(bcftools_stderr,"Parsing %s ...\n", gff->fname);
977
978 aux_t *aux = &gff->init;
979 aux->seq2int = khash_str2int_init(); // chrom's numeric id
980 aux->gid2gene = kh_init(int2gene); // gene id to gf_gene_t, for idx_gene
981 aux->id2tr = kh_init(int2tscript); // transcript id to tscript_t
982 gff->idx_tscript = regidx_init(NULL, NULL, regidx_free_tscript, sizeof(gf_tscript_t*), NULL);
983 aux->ignored_biotypes = khash_str2int_init();
984 gff_id_init(&aux->gene_ids);
985 gff_id_init(&gff->tscript_ids);
986
987 // parse gff
988 kstring_t str = {0,0,0};
989 htsFile *fp = hts_open(gff->fname,"r");
990 if ( !fp ) error("Failed to read %s\n", gff->fname);
991 while ( hts_getline(fp, KS_SEP_LINE, &str) > 0 )
992 {
993 hts_expand(ftr_t, aux->nftr+1, aux->mftr, aux->ftr);
994 int ret = gff_parse_line(gff, str.s, aux->ftr + aux->nftr);
995 if ( !ret ) aux->nftr++;
996 }
997 free(str.s);
998 if ( hts_close(fp)!=0 ) error("Close failed: %s\n", gff->fname);
999
1000
1001 // process gff information: connect CDS and exons to transcripts
1002 gff->idx_cds = regidx_init(NULL, NULL, regidx_free_gf, sizeof(gf_cds_t*), NULL);
1003 gff->idx_utr = regidx_init(NULL, NULL, regidx_free_gf, sizeof(gf_utr_t*), NULL);
1004 gff->idx_exon = regidx_init(NULL, NULL, regidx_free_gf, sizeof(gf_exon_t*), NULL);
1005
1006 int i;
1007 for (i=0; i<aux->nftr; i++)
1008 {
1009 ftr_t *ftr = &aux->ftr[i];
1010
1011 // check whether to keep this feature: is there a mapping trid -> gene_id -> gene?
1012 khint_t k = kh_get(int2tscript, aux->id2tr, (int)ftr->trid);
1013 if ( k==kh_end(aux->id2tr) ) continue; // no corresponding transcript registered, must be an unsupported biotype
1014
1015 gf_tscript_t *tr = kh_val(aux->id2tr,k);
1016 tr->used = 1;
1017 tr->gene->used = 1;
1018
1019 // populate regidx by category:
1020 // ftr->type .. GF_CDS, GF_EXON, GF_UTR3, GF_UTR5
1021 // gene->type .. GF_PROTEIN_CODING, GF_MT_rRNA, GF_IG_C, ...
1022 if ( ftr->type==GF_CDS ) register_cds(gff, ftr);
1023 else if ( ftr->type==GF_EXON ) register_exon(gff, ftr);
1024 else if ( ftr->type==GF_UTR5 ) register_utr(gff, ftr);
1025 else if ( ftr->type==GF_UTR3 ) register_utr(gff, ftr);
1026 else
1027 error("something: %s\t%"PRIu32"\t%"PRIu32"\t%s\t%s\n", aux->seq[ftr->iseq],ftr->beg+1,ftr->end+1,gff->tscript_ids.str[ftr->trid],gf_type2gff_string(ftr->type));
1028 }
1029 tscript_init_cds(gff);
1030
1031 if ( gff->verbosity > 0 )
1032 {
1033 fprintf(bcftools_stderr,"Indexed %d transcripts, %d exons, %d CDSs, %d UTRs\n",
1034 regidx_nregs(gff->idx_tscript),
1035 regidx_nregs(gff->idx_exon),
1036 regidx_nregs(gff->idx_cds),
1037 regidx_nregs(gff->idx_utr));
1038 }
1039
1040 if ( gff->verbosity > 0 && khash_str2int_size(aux->ignored_biotypes) )
1041 {
1042 khash_t(str2int) *ign = (khash_t(str2int)*)aux->ignored_biotypes;
1043 fprintf(bcftools_stderr,"Ignored the following biotypes:\n");
1044 for (i = kh_begin(ign); i < kh_end(ign); i++)
1045 {
1046 if ( !kh_exist(ign,i)) continue;
1047 const char *biotype = kh_key(ign,i);
1048 if ( !strcmp(biotype,"TCE") ) biotype = "TCE (\"To be Experimentally Confirmed\")";
1049 fprintf(bcftools_stderr,"\t%dx\t.. %s\n", kh_value(ign,i), biotype);
1050 }
1051 }
1052 khash_str2int_destroy_free(aux->ignored_biotypes);
1053
1054 // warned about unprinted warnings
1055 if ( gff->verbosity > 0 )
1056 {
1057 int nwarn = 0;
1058 #define INC_NWARN(X) if (gff->warned.X) nwarn += gff->verbosity > 1 ? 0 : gff->warned.X - 1;
1059 INC_NWARN(unknown_chr);
1060 INC_NWARN(unknown_tscript_biotype);
1061 INC_NWARN(unknown_strand);
1062 INC_NWARN(unknown_phase);
1063 INC_NWARN(duplicate_id);
1064 INC_NWARN(unknown_cds_phase);
1065 INC_NWARN(incomplete_cds);
1066 INC_NWARN(wrong_phase);
1067 INC_NWARN(overlapping_cds);
1068 if ( nwarn > 0 )
1069 fprintf(bcftools_stderr,"Warning: %d warnings were suppressed, increase verbosity to see them all\n",nwarn);
1070 }
1071
1072 if ( gff->dump_fname ) gff_dump(gff, gff->dump_fname);
1073
1074 if ( !regidx_nregs(gff->idx_tscript) )
1075 error("Error: No usable transcripts found, likely a failure to parse a non-standard GFF file. Please check if the misc/gff2gff\n"
1076 " or misc/gff2gff.py script can fix the problem (both do different things). See also the man page for the description\n"
1077 " of the expected format http://samtools.github.io/bcftools/bcftools-man.html#csq\n");
1078
1079 free(aux->seq);
1080 free(aux->ftr);
1081 khash_str2int_destroy_free(aux->seq2int);
1082 // keeping only to destroy the genes at the end: kh_destroy(int2gene,aux->gid2gene);
1083 kh_destroy(int2tscript,aux->id2tr);
1084 gff_id_destroy(&aux->gene_ids);
1085
1086 return 0;
1087 }
1088
1089 gff_t *gff_init(const char *fname)
1090 {
1091 gff_t *gff = calloc(sizeof(gff_t),1);
1092 gff->fname = fname;
1093 return gff;
1094 }
1095 void gff_destroy(gff_t *gff)
1096 {
1097 khint_t k;
1098 if ( gff->init.gid2gene )
1099 {
1100 for (k=0; k<kh_end(gff->init.gid2gene); k++)
1101 {
1102 if ( !kh_exist(gff->init.gid2gene, k) ) continue;
1103 gf_gene_t *gene = (gf_gene_t*) kh_val(gff->init.gid2gene, k);
1104 free(gene->name);
1105 free(gene);
1106 }
1107 kh_destroy(int2gene,gff->init.gid2gene);
1108 }
1109
1110 regidx_destroy(gff->idx_cds);
1111 regidx_destroy(gff->idx_utr);
1112 regidx_destroy(gff->idx_exon);
1113 regidx_destroy(gff->idx_tscript);
1114
1115 gff_id_destroy(&gff->tscript_ids);
1116 free(gff);
1117 }
1118