Debian Codegraph @

bcftools/gff.h

raw symbols SCIP
1 /* The MIT License
2
3 Copyright (c) 2023-2024 Genome Research Ltd.
4
5 Author: Petr Danecek <[email protected]>
6
7 Permission is hereby granted, free of charge, to any person obtaining a copy
8 of this software and associated documentation files (the "Software"), to deal
9 in the Software without restriction, including without limitation the rights
10 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11 copies of the Software, and to permit persons to whom the Software is
12 furnished to do so, subject to the following conditions:
13
14 The above copyright notice and this permission notice shall be included in
15 all copies or substantial portions of the Software.
16
17 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
23 THE SOFTWARE.
24
25 */
26 /*
27 GFF parsing code refactored from csq.c
28
29 Things that would be nice to have
30 - dynamic N_REF_PAD
31 - for stop-lost events (also in frameshifts) report the number of truncated aa's
32 - memory could be greatly reduced by indexing gff (but it is quite compact already)
33 - deletions that go beyond transcript boundaries are not checked at sequence level
34 - alloc tscript->ref in hap_finalize, introduce fa_off_beg:16,fa_off_end:16
35 - see test/csq/ENST00000573314/insertion-overlap.vcf #1476288882
36
37 Read about transcript types here
38 http://vega.sanger.ac.uk/info/about/gene_and_transcript_types.html
39 https://www.ensembl.org/info/genome/variation/prediction/predicted_data.html
40 https://www.gencodegenes.org/pages/biotypes.html
41
42 List of supported biotypes
43 antisense
44 IG_C_gene
45 IG_D_gene
46 IG_J_gene
47 IG_LV_gene
48 IG_V_gene
49 lincRNA
50 lncRNA .. generic term for 3prime_overlapping_ncRNA, antisense, bidirectional_promoter_lncRNA, lincRNA, macro_lncRNA, non_coding, processed_transcript, sense_intronic, sense_overlapping
51 macro_lncRNA
52 miRNA
53 misc_RNA
54 Mt_rRNA
55 Mt_tRNA
56 polymorphic_pseudogene
57 processed_transcript
58 protein_coding, mRNA
59 ribozyme
60 rRNA
61 sRNA
62 scRNA
63 scaRNA
64 sense_intronic
65 sense_overlapping
66 snRNA
67 snoRNA
68 TR_C_gene
69 TR_D_gene
70 TR_J_gene
71 TR_V_gene
72
73 The gff parsing logic
74 We collect features such by combining gff lines A,B,C as follows:
75 A .. gene line with a supported biotype
76 A.ID=~/^gene:/
77
78 B .. transcript line referencing A with supported biotype
79 B.ID=~/^transcript:/ && B.Parent=~/^gene:A.ID/
80
81 C .. corresponding CDS, exon, and UTR lines:
82 C[3] in {"CDS","exon","three_prime_UTR","five_prime_UTR"} && C.Parent=~/^transcript:B.ID/
83
84 For coding biotypes ("protein_coding" or "polymorphic_pseudogene") the
85 complete chain link C -> B -> A is required. For the rest, link B -> A suffices.
86
87
88 The supported consequence types, sorted by impact:
89 splice_acceptor_variant .. end region of an intron changed (2bp at the 3' end of an intron)
90 splice_donor_variant .. start region of an intron changed (2bp at the 5' end of an intron)
91 stop_gained .. DNA sequence variant resulting in a stop codon
92 frameshift_variant .. number of inserted/deleted bases not a multiple of three, disrupted translational frame
93 stop_lost .. elongated transcript, stop codon changed
94 start_lost .. the first codon changed
95 inframe_altering .. combination of indels leading to unchanged reading frame and length
96 inframe_insertion .. inserted coding sequence, unchanged reading frame
97 inframe_deletion .. deleted coding sequence, unchanged reading frame
98 missense_variant .. amino acid (aa) change, unchanged length
99 splice_region_variant .. change within 1-3 bases of the exon or 3-8 bases of the intron
100 synonymous_variant .. DNA sequence variant resulting in no amino acid change
101 stop_retained_variant .. different stop codon
102 start_retained_variant .. start codon retained by indel realignment
103 non_coding_variant .. variant in non-coding sequence, such as RNA gene
104 5_prime_UTR_variant
105 3_prime_UTR_variant
106 intron_variant .. reported only if none of the above
107 intergenic_variant .. reported only if none of the above
108
109
110 The annotation algorithm.
111 The algorithm checks if the variant falls in a region of a supported type. The
112 search is performed in the following order, until a match is found:
113 1. idx_cds(gf_cds_t) - lookup CDS by position, create haplotypes, call consequences
114 2. idx_utr(gf_utr_t) - check UTR hits
115 3. idx_exon(gf_exon_t) - check for splice variants
116 4. idx_tscript(tscript_t) - check for intronic variants, RNAs, etc.
117
118 These regidx indexes are created by parsing a gff3 file as follows:
119 1. create the array "ftr" of all UTR, CDS, exons. This will be
120 processed later and pruned based on transcript types we want to keep.
121 In the same go, create the hash "id2tr" of transcripts to keep
122 (based on biotype) which maps from transcript_id to a transcript. At
123 the same time also build the hash "gid2gene" which maps from gene_id to
124 gf_gene_t pointer.
125
126 2. build "idx_cds", "idx_tscript", "idx_utr" and "idx_exon" indexes.
127 Use only features from "ftr" which are present in "id2tr".
128
129 3. clean data that won't be needed anymore: ftr, id2tr, gid2gene.
130
131 Data structures.
132 idx_cds, idx_utr, idx_exon, idx_tscript:
133 as described above, regidx structures for fast lookup of exons/transcripts
134 overlapping a region, the payload is a pointer to tscript.cds
135 */
136
137 #ifndef GFF_H__
138 #define GFF_H__
139
140 #include <stdint.h>
141
142 #ifndef __FUNCTION__
143 # define __FUNCTION__ __func__
144 #endif
145
146 // Definition of splice_region, splice_acceptor and splice_donor
147 #define N_SPLICE_DONOR 2
148 #define N_SPLICE_REGION_EXON 3
149 #define N_SPLICE_REGION_INTRON 8
150
151 #define STRAND_REV 0
152 #define STRAND_FWD 1
153 #define STRAND_UNK 2
154
155 #define TRIM_NONE 0
156 #define TRIM_5PRIME 1
157 #define TRIM_3PRIME 2
158
159
160 // GFF line types
161 #define GFF_UNKN_LINE 0
162 #define GFF_TSCRIPT_LINE 1
163 #define GFF_GENE_LINE 2
164
165
166 /*
167 Genomic features, for fast lookup by position to overlapping features
168 */
169 #define GF_coding_bit 6
170 #define GF_is_coding(x) ((x) & (1<<GF_coding_bit))
171 #define GF_MT_rRNA 1 // non-coding: 1, 2, ...
172 #define GF_MT_tRNA 2
173 #define GF_lincRNA 3
174 #define GF_miRNA 4
175 #define GF_MISC_RNA 5
176 #define GF_rRNA 6
177 #define GF_snRNA 7
178 #define GF_snoRNA 8
179 #define GF_PROCESSED_TRANSCRIPT 9
180 #define GF_ANTISENSE 10
181 #define GF_macro_lncRNA 11
182 #define GF_ribozyme 12
183 #define GF_sRNA 13
184 #define GF_scRNA 14
185 #define GF_scaRNA 15
186 #define GF_SENSE_INTRONIC 16
187 #define GF_SENSE_OVERLAPPING 17
188 #define GF_PSEUDOGENE 18
189 #define GF_PROCESSED_PSEUDOGENE 19
190 #define GF_ARTIFACT 20
191 #define GF_IG_PSEUDOGENE 21
192 #define GF_IG_C_PSEUDOGENE 22
193 #define GF_IG_J_PSEUDOGENE 23
194 #define GF_IG_V_PSEUDOGENE 24
195 #define GF_TR_V_PSEUDOGENE 25
196 #define GF_TR_J_PSEUDOGENE 26
197 #define GF_MT_tRNA_PSEUDOGENE 27
198 #define GF_misc_RNA_PSEUDOGENE 28
199 #define GF_miRNA_PSEUDOGENE 29
200 #define GF_RIBOZYME 30
201 #define GF_RETAINED_INTRON 31
202 #define GF_RETROTRANSPOSED 32
203 #define GF_tRNA_PSEUDOGENE 33
204 #define GF_TRANSCRIBED_PROCESSED_PSEUDOGENE 34
205 #define GF_TRANSCRIBED_UNPROCESSED_PSEUDOGENE 35
206 #define GF_TRANSCRIBED_UNITARY_PSEUDOGENE 36
207 #define GF_TRANSLATED_UNPROCESSED_PSEUDOGENE 37
208 #define GF_TRANSLATED_PROCESSED_PSEUDOGENE 38
209 #define GF_KNOWN_NCRNA 39
210 #define GF_UNITARY_PSEUDOGENE 40
211 #define GF_UNPROCESSED_PSEUDOGENE 41
212 #define GF_LRG_GENE 42
213 #define GF_3PRIME_OVERLAPPING_ncRNA 43
214 #define GF_DISRUPTED_DOMAIN 44
215 #define GF_vaultRNA 45
216 #define GF_BIDIRECTIONAL_PROMOTER_lncRNA 46
217 #define GF_AMBIGUOUS_ORF 47
218 #define GF_lncRNA 48
219 #define GF_PROTEIN_CODING (1|(1<<GF_coding_bit)) // coding: 65, 66, ...
220 #define GF_POLYMORPHIC_PSEUDOGENE (2|(1<<GF_coding_bit))
221 #define GF_IG_C (3|(1<<GF_coding_bit))
222 #define GF_IG_D (4|(1<<GF_coding_bit))
223 #define GF_IG_J (5|(1<<GF_coding_bit))
224 #define GF_IG_LV (6|(1<<GF_coding_bit))
225 #define GF_IG_V (7|(1<<GF_coding_bit))
226 #define GF_TR_C (8|(1<<GF_coding_bit))
227 #define GF_TR_D (9|(1<<GF_coding_bit))
228 #define GF_TR_J (10|(1<<GF_coding_bit))
229 #define GF_TR_V (11|(1<<GF_coding_bit))
230 #define GF_NMD (12|(1<<GF_coding_bit))
231 #define GF_NON_STOP_DECAY (13|(1<<GF_coding_bit))
232 #define GF_CDS ((1<<(GF_coding_bit+1))+1) // special types: 129, 130, ...
233 #define GF_EXON ((1<<(GF_coding_bit+1))+2)
234 #define GF_UTR3 ((1<<(GF_coding_bit+1))+3)
235 #define GF_UTR5 ((1<<(GF_coding_bit+1))+4)
236 // GF_MAX = (1<<30)-1, see hap_node_t
237
238 #define CDS_PHASE_UNKN 3
239 typedef struct gf_tscript_t_ gf_tscript_t;
240 typedef struct
241 {
242 gf_tscript_t *tr; // transcript
243 uint32_t beg; // the start coordinate of the CDS (on the reference strand, 0-based)
244 uint32_t pos; // 0-based index of the first exon base within the transcript (only to
245 // update hap_node_t.sbeg in hap_init, could be calculated on the fly)
246 uint32_t len; // exon length
247 uint32_t icds:30, // exon index within the transcript
248 phase:2; // offset of the CDS: 0,1,2 or 3 for unknown
249 }
250 gf_cds_t;
251 typedef struct
252 {
253 char *name; // human readable name, e.g. ORF45
254 uint32_t iseq;
255 uint32_t id,beg,end,strand:31, // used only by --dump-gff
256 used:1; // does it have any exons, CDS, UTR?
257 }
258 gf_gene_t;
259 typedef struct
260 {
261 uint32_t beg,end;
262 gf_tscript_t *tr;
263 }
264 gf_exon_t;
265 typedef enum { prime3, prime5 } utr_t;
266 typedef struct
267 {
268 utr_t which;
269 uint32_t beg,end;
270 gf_tscript_t *tr;
271 }
272 gf_utr_t;
273 struct gf_tscript_t_
274 {
275 uint32_t id; // transcript id
276 uint32_t beg,end; // transcript's beg and end coordinate (ref strand, 0-based, inclusive)
277 uint32_t strand:2, // STRAND_REV,FWD,UNK
278 used:1, // does it have any exons, UTRs, CDS?
279 ncds:29, // number of exons
280 mcds;
281 gf_cds_t **cds; // ordered list of exons
282 uint32_t trim:2, // complete, 5' or 3' trimmed, see TRIM_* types
283 type:30; // one of GF_* types
284 gf_gene_t *gene;
285 void *aux; // auxiliary user data
286 };
287
288 typedef enum
289 {
290 // write options
291 verbosity, // int, 0-2
292 strip_chr_names, // int, 0 to leave as is, 1 to strip 'chr' prefix
293 force_out_of_phase, // int, 1 to proceed even CDS exon out of expected phase
294 dump_fname, // const char*, dump the parsed GFF into this file, for debugging purposes
295
296 // read options
297 idx_cds,
298 idx_utr,
299 idx_exon,
300 idx_tscript,
301 }
302 gff_opt_t;
303
304 typedef enum { transcript } id_type_t; // for gff_id2str
305
306 typedef struct gff_t_ gff_t;
307
308 gff_t *gff_init(const char *fname);
309 int gff_parse(gff_t *gff);
310 void gff_destroy(gff_t *gff);
311
312 int gff_set(gff_t *gff, gff_opt_t key, ...); // returns 0 on success
313 void *gff_get(gff_t *gff, gff_opt_t key);
314 const char *gff_id2string(gff_t *gff, id_type_t type, int id);
315 const char *gf_type2gff_string(int type);
316
317 #endif