Debian Codegraph @

bcftools/gff.c

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