| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
|
| 89 |
|
| 90 |
|
| 91 |
|
| 92 |
|
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
|
| 97 |
|
| 98 |
|
| 99 |
|
| 100 |
|
| 101 |
|
| 102 |
|
| 103 |
|
| 104 |
|
| 105 |
|
| 106 |
|
| 107 |
|
| 108 |
|
| 109 |
|
| 110 |
|
| 111 |
|
| 112 |
|
| 113 |
|
| 114 |
|
| 115 |
|
| 116 |
|
| 117 |
|
| 118 |
|
| 119 |
|
| 120 |
|
| 121 |
|
| 122 |
|
| 123 |
|
| 124 |
|
| 125 |
|
| 126 |
|
| 127 |
|
| 128 |
|
| 129 |
|
| 130 |
|
| 131 |
|
| 132 |
|
| 133 |
|
| 134 |
|
| 135 |
#include <stdio.h> |
| 136 |
#include <stdlib.h> |
| 137 |
#include <assert.h> |
| 138 |
#include <getopt.h> |
| 139 |
#include <math.h> |
| 140 |
#include <inttypes.h> |
| 141 |
#include <htslib/hts.h> |
| 142 |
#include <htslib/vcf.h> |
| 143 |
#include <htslib/synced_bcf_reader.h> |
| 144 |
#include <htslib/khash.h> |
| 145 |
#include <htslib/khash_str2int.h> |
| 146 |
#include <htslib/kseq.h> |
| 147 |
#include <htslib/faidx.h> |
| 148 |
#include <htslib/bgzf.h> |
| 149 |
#include <errno.h> |
| 150 |
#include <unistd.h> |
| 151 |
#include <ctype.h> |
| 152 |
#include <strings.h> |
| 153 |
#include "bcftools.h" |
| 154 |
#include "filter.h" |
| 155 |
#include "regidx.h" |
| 156 |
#include "kheap.h" |
| 157 |
#include "smpl_ilist.h" |
| 158 |
#include "rbuf.h" |
| 159 |
#include "gff.h" |
| 160 |
|
| 161 |
#ifndef __FUNCTION__ |
| 162 |
# define __FUNCTION__ __func__ |
| 163 |
#endif |
| 164 |
|
| 165 |
|
| 166 |
#define FLT_INCLUDE 1 |
| 167 |
#define FLT_EXCLUDE 2 |
| 168 |
|
| 169 |
#define N_REF_PAD 10 // number of bases to avoid boundary effects |
| 170 |
|
| 171 |
|
| 172 |
#define PHASE_REQUIRE 0 // --phase r |
| 173 |
#define PHASE_MERGE 1 // --phase m |
| 174 |
#define PHASE_AS_IS 2 // --phase a |
| 175 |
#define PHASE_SKIP 3 // --phase s |
| 176 |
#define PHASE_NON_REF 4 // --phase R |
| 177 |
#define PHASE_DROP_GT 5 // --samples - |
| 178 |
|
| 179 |
|
| 180 |
#define HAP_CDS 0 |
| 181 |
#define HAP_ROOT 1 |
| 182 |
#define HAP_SSS 2 // start/stop/splice |
| 183 |
|
| 184 |
#define CSQ_PRINTED_UPSTREAM (1<<0) |
| 185 |
#define CSQ_SYNONYMOUS_VARIANT (1<<1) |
| 186 |
#define CSQ_MISSENSE_VARIANT (1<<2) |
| 187 |
#define CSQ_STOP_LOST (1<<3) |
| 188 |
#define CSQ_STOP_GAINED (1<<4) |
| 189 |
#define CSQ_INFRAME_DELETION (1<<5) |
| 190 |
#define CSQ_INFRAME_INSERTION (1<<6) |
| 191 |
#define CSQ_FRAMESHIFT_VARIANT (1<<7) |
| 192 |
#define CSQ_SPLICE_ACCEPTOR (1<<8) |
| 193 |
#define CSQ_SPLICE_DONOR (1<<9) |
| 194 |
#define CSQ_START_LOST (1<<10) |
| 195 |
#define CSQ_SPLICE_REGION (1<<11) |
| 196 |
#define CSQ_STOP_RETAINED (1<<12) |
| 197 |
#define CSQ_UTR5 (1<<13) |
| 198 |
#define CSQ_UTR3 (1<<14) |
| 199 |
#define CSQ_NON_CODING (1<<15) |
| 200 |
#define CSQ_INTRON (1<<16) |
| 201 |
|
| 202 |
#define CSQ_INFRAME_ALTERING (1<<18) |
| 203 |
#define CSQ_UPSTREAM_STOP (1<<19) // adds * in front of the csq string |
| 204 |
#define CSQ_INCOMPLETE_CDS (1<<20) // to remove START/STOP in incomplete CDS, see ENSG00000173376/synon.vcf |
| 205 |
#define CSQ_CODING_SEQUENCE (1<<21) // cannot tell exactly what it is, but it does affect the coding sequence |
| 206 |
#define CSQ_ELONGATION (1<<22) // symbolic insertion |
| 207 |
#define CSQ_START_RETAINED (1<<23) |
| 208 |
|
| 209 |
|
| 210 |
#define CSQ_COMPOUND (CSQ_SYNONYMOUS_VARIANT|CSQ_MISSENSE_VARIANT|CSQ_STOP_LOST|CSQ_STOP_GAINED| \ |
| 211 |
CSQ_INFRAME_DELETION|CSQ_INFRAME_INSERTION|CSQ_FRAMESHIFT_VARIANT| \ |
| 212 |
CSQ_START_LOST|CSQ_STOP_RETAINED|CSQ_INFRAME_ALTERING|CSQ_INCOMPLETE_CDS| \ |
| 213 |
CSQ_UPSTREAM_STOP|CSQ_START_RETAINED) |
| 214 |
#define CSQ_START_STOP (CSQ_STOP_LOST|CSQ_STOP_GAINED|CSQ_STOP_RETAINED|CSQ_START_LOST|CSQ_START_RETAINED) |
| 215 |
|
| 216 |
#define CSQ_PRN_STRAND(csq) ((csq)&CSQ_COMPOUND && !((csq)&(CSQ_SPLICE_ACCEPTOR|CSQ_SPLICE_DONOR|CSQ_SPLICE_REGION))) |
| 217 |
#define CSQ_PRN_TSCRIPT (~(CSQ_INTRON|CSQ_NON_CODING)) |
| 218 |
#define CSQ_PRN_NMD (~(CSQ_INTRON|CSQ_NON_CODING)) |
| 219 |
#define CSQ_PRN_BIOTYPE CSQ_NON_CODING |
| 220 |
|
| 221 |
|
| 222 |
const char *csq_strings[] = |
| 223 |
{ |
| 224 |
NULL, |
| 225 |
"synonymous", |
| 226 |
"missense", |
| 227 |
"stop_lost", |
| 228 |
"stop_gained", |
| 229 |
"inframe_deletion", |
| 230 |
"inframe_insertion", |
| 231 |
"frameshift", |
| 232 |
"splice_acceptor", |
| 233 |
"splice_donor", |
| 234 |
"start_lost", |
| 235 |
"splice_region", |
| 236 |
"stop_retained", |
| 237 |
"5_prime_utr", |
| 238 |
"3_prime_utr", |
| 239 |
"non_coding", |
| 240 |
"intron", |
| 241 |
"intergenic", |
| 242 |
"inframe_altering", |
| 243 |
NULL, |
| 244 |
NULL, |
| 245 |
"coding_sequence", |
| 246 |
"feature_elongation", |
| 247 |
"start_retained" |
| 248 |
}; |
| 249 |
|
| 250 |
|
| 251 |
|
| 252 |
|
| 253 |
|
| 254 |
|
| 255 |
|
| 256 |
|
| 257 |
|
| 258 |
|
| 259 |
|
| 260 |
|
| 261 |
|
| 262 |
|
| 263 |
|
| 264 |
|
| 265 |
|
| 266 |
|
| 267 |
|
| 268 |
typedef struct _vbuf_t vbuf_t; |
| 269 |
typedef struct _vcsq_t vcsq_t; |
| 270 |
struct _vcsq_t |
| 271 |
{ |
| 272 |
uint32_t strand:1, |
| 273 |
type:31; |
| 274 |
uint32_t trid; |
| 275 |
uint32_t vcf_ial; |
| 276 |
uint32_t biotype; |
| 277 |
char *gene; |
| 278 |
bcf1_t *ref; |
| 279 |
kstring_t vstr; |
| 280 |
}; |
| 281 |
typedef struct |
| 282 |
{ |
| 283 |
bcf1_t *line; |
| 284 |
uint32_t *fmt_bm; |
| 285 |
uint32_t nfmt:4, |
| 286 |
nvcsq:28, mvcsq; |
| 287 |
vcsq_t *vcsq; |
| 288 |
} |
| 289 |
vrec_t; |
| 290 |
typedef struct |
| 291 |
{ |
| 292 |
uint32_t pos; |
| 293 |
vrec_t *vrec; |
| 294 |
int idx; |
| 295 |
vcsq_t type; |
| 296 |
} |
| 297 |
csq_t; |
| 298 |
struct _vbuf_t |
| 299 |
{ |
| 300 |
vrec_t **vrec; |
| 301 |
int n, m; |
| 302 |
uint32_t keep_until; |
| 303 |
}; |
| 304 |
KHASH_MAP_INIT_INT(pos2vbuf, vbuf_t*) |
| 305 |
|
| 306 |
|
| 307 |
|
| 308 |
|
| 309 |
|
| 310 |
|
| 311 |
|
| 312 |
|
| 313 |
|
| 314 |
|
| 315 |
|
| 316 |
|
| 317 |
|
| 318 |
|
| 319 |
|
| 320 |
|
| 321 |
typedef struct _hap_node_t hap_node_t; |
| 322 |
struct _hap_node_t |
| 323 |
{ |
| 324 |
char *seq; |
| 325 |
char *var; |
| 326 |
uint32_t type:2, |
| 327 |
csq:30; |
| 328 |
int dlen; |
| 329 |
uint32_t rbeg; |
| 330 |
int32_t rlen; |
| 331 |
uint32_t sbeg; |
| 332 |
uint32_t icds; |
| 333 |
hap_node_t **child, *prev; |
| 334 |
int nchild, mchild; |
| 335 |
bcf1_t *cur_rec, *rec; |
| 336 |
int vcf_ial; |
| 337 |
uint32_t nend; |
| 338 |
int *cur_child, mcur_child; |
| 339 |
csq_t *csq_list; |
| 340 |
int ncsq_list, mcsq_list; |
| 341 |
}; |
| 342 |
#define TSCRIPT_AUX(x) ((tscript_t*)(x)->aux) |
| 343 |
typedef struct |
| 344 |
{ |
| 345 |
char *ref; |
| 346 |
char *sref; |
| 347 |
hap_node_t *root; |
| 348 |
hap_node_t **hap; |
| 349 |
int nhap, nsref; |
| 350 |
} |
| 351 |
tscript_t; |
| 352 |
static inline int cmp_tscript(gf_tscript_t **a, gf_tscript_t **b) |
| 353 |
{ |
| 354 |
return ( (*a)->end < (*b)->end ) ? 1 : 0; |
| 355 |
} |
| 356 |
KHEAP_INIT(trhp, gf_tscript_t*, cmp_tscript) |
| 357 |
typedef khp_trhp_t tr_heap_t; |
| 358 |
typedef struct |
| 359 |
{ |
| 360 |
hap_node_t *node; |
| 361 |
int ichild; |
| 362 |
int dlen; |
| 363 |
size_t slen; |
| 364 |
} |
| 365 |
hstack_t; |
| 366 |
typedef struct |
| 367 |
{ |
| 368 |
int mstack; |
| 369 |
hstack_t *stack; |
| 370 |
gf_tscript_t *tr; |
| 371 |
kstring_t sseq; |
| 372 |
kstring_t tseq; |
| 373 |
kstring_t tref; |
| 374 |
uint32_t sbeg; |
| 375 |
int upstream_stop; |
| 376 |
} |
| 377 |
hap_t; |
| 378 |
|
| 379 |
typedef struct _args_t |
| 380 |
{ |
| 381 |
|
| 382 |
|
| 383 |
gff_t *gff; |
| 384 |
regidx_t *idx_cds, *idx_utr, *idx_exon, *idx_tscript; |
| 385 |
regitr_t *itr; |
| 386 |
|
| 387 |
|
| 388 |
FILE *out; |
| 389 |
htsFile *out_fh; |
| 390 |
char *index_fn; |
| 391 |
int write_index; |
| 392 |
char *dump_gff; |
| 393 |
|
| 394 |
|
| 395 |
bcf_srs_t *sr; |
| 396 |
bcf_hdr_t *hdr; |
| 397 |
int hdr_nsmpl; |
| 398 |
|
| 399 |
|
| 400 |
filter_t *filter; |
| 401 |
char *filter_str; |
| 402 |
int filter_logic; |
| 403 |
|
| 404 |
|
| 405 |
int sample_is_file; |
| 406 |
char *sample_list; |
| 407 |
smpl_ilist_t *smpl; |
| 408 |
|
| 409 |
char *outdir, **argv, *fa_fname, *gff_fname, *output_fname; |
| 410 |
char *bcsq_tag; |
| 411 |
int argc, output_type, clevel; |
| 412 |
int phase, verbosity, local_csq, record_cmd_line; |
| 413 |
int ncsq2_max, nfmt_bcsq; |
| 414 |
int ncsq2_small_warned; |
| 415 |
int brief_predictions; |
| 416 |
int unify_chr_names; |
| 417 |
char *chr_name; |
| 418 |
int mchr_name; |
| 419 |
struct { |
| 420 |
int unknown_chr,unknown_tscript_biotype,unknown_strand,unknown_phase,duplicate_id; |
| 421 |
int unknown_cds_phase,incomplete_cds,wrong_phase,overlapping_cds; |
| 422 |
} warned; |
| 423 |
|
| 424 |
int rid; |
| 425 |
tr_heap_t *active_tr; |
| 426 |
hap_t *hap; |
| 427 |
vbuf_t **vcf_buf; |
| 428 |
rbuf_t vcf_rbuf; |
| 429 |
kh_pos2vbuf_t *pos2vbuf; |
| 430 |
gf_tscript_t **rm_tr; |
| 431 |
int nrm_tr, mrm_tr; |
| 432 |
csq_t *csq_buf; |
| 433 |
int ncsq_buf, mcsq_buf; |
| 434 |
int force; |
| 435 |
int n_threads; |
| 436 |
|
| 437 |
faidx_t *fai; |
| 438 |
kstring_t str, str2; |
| 439 |
int32_t *gt_arr, mgt_arr; |
| 440 |
} |
| 441 |
args_t; |
| 442 |
|
| 443 |
|
| 444 |
const char *gencode = "KNKNTTTTRSRSIIMIQHQHPPPPRRRRLLLLEDEDAAAAGGGGVVVV*Y*YSSSS*CWCLFLF"; |
| 445 |
const uint8_t nt4[] = |
| 446 |
{ |
| 447 |
4,4,4,4, 4,4,4,4, 4,4,4,4, 4,4,4,4, |
| 448 |
4,4,4,4, 4,4,4,4, 4,4,4,4, 4,4,4,4, |
| 449 |
4,4,4,4, 4,4,4,4, 4,4,4,4, 4,4,4,4, |
| 450 |
4,4,4,4, 4,4,4,4, 4,4,4,4, 4,4,4,4, |
| 451 |
4,0,4,1, 4,4,4,2, 4,4,4,4, 4,4,4,4, |
| 452 |
4,4,4,4, 3,4,4,4, 4,4,4,4, 4,4,4,4, |
| 453 |
4,0,4,1, 4,4,4,2, 4,4,4,4, 4,4,4,4, |
| 454 |
4,4,4,4, 3 |
| 455 |
}; |
| 456 |
const uint8_t cnt4[] = |
| 457 |
{ |
| 458 |
4,4,4,4, 4,4,4,4, 4,4,4,4, 4,4,4,4, |
| 459 |
4,4,4,4, 4,4,4,4, 4,4,4,4, 4,4,4,4, |
| 460 |
4,4,4,4, 4,4,4,4, 4,4,4,4, 4,4,4,4, |
| 461 |
4,4,4,4, 4,4,4,4, 4,4,4,4, 4,4,4,4, |
| 462 |
4,3,4,2, 4,4,4,1, 4,4,4,4, 4,4,4,4, |
| 463 |
4,4,4,4, 0,4,4,4, 4,4,4,4, 4,4,4,4, |
| 464 |
4,3,4,2, 4,4,4,1, 4,4,4,4, 4,4,4,4, |
| 465 |
4,4,4,4, 0 |
| 466 |
}; |
| 467 |
#define dna2aa(x) gencode[ nt4[(uint8_t)(x)[0]]<<4 | nt4[(uint8_t)(x)[1]]<<2 | nt4[(uint8_t)(x)[2]] ] |
| 468 |
#define cdna2aa(x) gencode[ cnt4[(uint8_t)(x)[2]]<<4 | cnt4[(uint8_t)(x)[1]]<<2 | cnt4[(uint8_t)(x)[0]] ] |
| 469 |
|
| 470 |
static inline int ncsq2_to_nfmt(int ncsq2) |
| 471 |
{ |
| 472 |
return 1 + (ncsq2 - 1) / 30; |
| 473 |
} |
| 474 |
static inline void icsq2_to_bit(int icsq2, int *ival, int *ibit) |
| 475 |
{ |
| 476 |
*ival = icsq2 / 30; |
| 477 |
*ibit = icsq2 % 30; |
| 478 |
} |
| 479 |
|
| 480 |
void init_data(args_t *args) |
| 481 |
{ |
| 482 |
args->nfmt_bcsq = ncsq2_to_nfmt(args->ncsq2_max); |
| 483 |
|
| 484 |
args->fai = fai_load(args->fa_fname); |
| 485 |
if ( !args->fai ) error("Failed to load the fai index: %s\n", args->fa_fname); |
| 486 |
|
| 487 |
args->gff = gff_init(args->gff_fname); |
| 488 |
gff_set(args->gff,verbosity,args->verbosity); |
| 489 |
gff_set(args->gff,strip_chr_names,args->unify_chr_names); |
| 490 |
gff_set(args->gff,force_out_of_phase,args->force); |
| 491 |
gff_set(args->gff,dump_fname,args->dump_gff); |
| 492 |
gff_parse(args->gff); |
| 493 |
args->idx_cds = gff_get(args->gff,idx_cds); |
| 494 |
args->idx_utr = gff_get(args->gff,idx_utr); |
| 495 |
args->idx_exon = gff_get(args->gff,idx_exon); |
| 496 |
args->idx_tscript = gff_get(args->gff,idx_tscript); |
| 497 |
args->itr = regitr_init(NULL); |
| 498 |
|
| 499 |
args->rid = -1; |
| 500 |
|
| 501 |
if ( args->filter_str ) |
| 502 |
args->filter = filter_init(args->hdr, args->filter_str); |
| 503 |
|
| 504 |
args->pos2vbuf = kh_init(pos2vbuf); |
| 505 |
args->active_tr = khp_init(trhp); |
| 506 |
args->hap = (hap_t*) calloc(1,sizeof(hap_t)); |
| 507 |
|
| 508 |
|
| 509 |
if ( !bcf_hdr_nsamples(args->hdr) ) args->phase = PHASE_DROP_GT; |
| 510 |
if ( args->sample_list && !strcmp("-",args->sample_list) ) |
| 511 |
{ |
| 512 |
|
| 513 |
if ( args->output_type==FT_TAB_TEXT ) |
| 514 |
{ |
| 515 |
|
| 516 |
if (bcf_hdr_set_samples(args->hdr,NULL,0) < 0) |
| 517 |
error_errno("[%s] Couldn't build sample filter", __func__); |
| 518 |
} |
| 519 |
args->phase = PHASE_DROP_GT; |
| 520 |
} |
| 521 |
else |
| 522 |
args->smpl = smpl_ilist_init(args->hdr, args->sample_list, args->sample_is_file, SMPL_STRICT); |
| 523 |
args->hdr_nsmpl = args->phase==PHASE_DROP_GT ? 0 : bcf_hdr_nsamples(args->hdr); |
| 524 |
|
| 525 |
if ( args->output_type==FT_TAB_TEXT ) |
| 526 |
{ |
| 527 |
args->out = args->output_fname ? fopen(args->output_fname,"w") : stdout; |
| 528 |
if ( !args->out ) error("Failed to write to %s: %s\n", !strcmp("-",args->output_fname)?"standard output":args->output_fname,strerror(errno)); |
| 529 |
|
| 530 |
fprintf(args->out,"# This file was produced by: bcftools +csq(%s+htslib-%s)\n", bcftools_version(),hts_version()); |
| 531 |
fprintf(args->out,"# The command line was:\tbcftools +%s", args->argv[0]); |
| 532 |
int i; |
| 533 |
for (i=1; i<args->argc; i++) |
| 534 |
fprintf(args->out," %s",args->argv[i]); |
| 535 |
fprintf(args->out,"\n"); |
| 536 |
fprintf(args->out,"# LOG\t[2]Message\n"); |
| 537 |
fprintf(args->out,"# CSQ"); i = 1; |
| 538 |
fprintf(args->out,"\t[%d]Sample", ++i); |
| 539 |
fprintf(args->out,"\t[%d]Haplotype", ++i); |
| 540 |
fprintf(args->out,"\t[%d]Chromosome", ++i); |
| 541 |
fprintf(args->out,"\t[%d]Position", ++i); |
| 542 |
fprintf(args->out,"\t[%d]Consequence", ++i); |
| 543 |
fprintf(args->out,"\n"); |
| 544 |
} |
| 545 |
else |
| 546 |
{ |
| 547 |
char wmode[8]; |
| 548 |
set_wmode(wmode,args->output_type,args->output_fname,args->clevel); |
| 549 |
args->out_fh = hts_open(args->output_fname ? args->output_fname : "-", wmode); |
| 550 |
if ( args->out_fh == NULL ) error("[%s] Error: cannot write to %s: %s\n", __func__,args->output_fname? args->output_fname : "standard output", strerror(errno)); |
| 551 |
if ( args->n_threads > 0) |
| 552 |
hts_set_opt(args->out_fh, HTS_OPT_THREAD_POOL, args->sr->p); |
| 553 |
if ( args->record_cmd_line ) bcf_hdr_append_version(args->hdr,args->argc,args->argv,"bcftools/csq"); |
| 554 |
bcf_hdr_printf(args->hdr,"##INFO=<ID=%s,Number=.,Type=String,Description=\"%s consequence annotation from BCFtools/csq, see http://samtools.github.io/bcftools/howtos/csq-calling.html for details. Format: Consequence|gene|transcript|biotype|strand|amino_acid_change|dna_change\">",args->bcsq_tag, args->local_csq ? "Local" : "Haplotype-aware"); |
| 555 |
if ( args->hdr_nsmpl ) |
| 556 |
bcf_hdr_printf(args->hdr,"##FORMAT=<ID=%s,Number=.,Type=Integer,Description=\"Bitmask of indexes to INFO/BCSQ, with interleaved first/second haplotype. Use \\\"bcftools query -f'[%%CHROM\\t%%POS\\t%%SAMPLE\\t%%TBCSQ\\n]'\\\" to translate.\">",args->bcsq_tag); |
| 557 |
if ( bcf_hdr_write(args->out_fh, args->hdr)!=0 ) error("[%s] Error: cannot write the header to %s\n", __func__,args->output_fname?args->output_fname:"standard output"); |
| 558 |
if ( init_index2(args->out_fh,args->hdr,args->output_fname, |
| 559 |
&args->index_fn, args->write_index) < 0 ) |
| 560 |
error("Error: failed to initialise index for %s\n",args->output_fname); |
| 561 |
} |
| 562 |
if ( args->verbosity > 0 ) fprintf(stderr,"Calling...\n"); |
| 563 |
} |
| 564 |
|
| 565 |
void destroy_data(args_t *args) |
| 566 |
{ |
| 567 |
if ( args->ncsq2_small_warned ) |
| 568 |
fprintf(stderr, |
| 569 |
"Note: Some samples had too many consequences to be represented in %d bytes. If you need to record them all,\n" |
| 570 |
" the limit can be increased by running with `--ncsq %d`.\n",ncsq2_to_nfmt(args->ncsq2_max)/8,1+args->ncsq2_small_warned/2); |
| 571 |
|
| 572 |
regitr_destroy(args->itr); |
| 573 |
gff_destroy(args->gff); |
| 574 |
|
| 575 |
if ( args->filter ) |
| 576 |
filter_destroy(args->filter); |
| 577 |
|
| 578 |
khp_destroy(trhp,args->active_tr); |
| 579 |
kh_destroy(pos2vbuf,args->pos2vbuf); |
| 580 |
if ( args->smpl ) smpl_ilist_destroy(args->smpl); |
| 581 |
int i,j,ret; |
| 582 |
if ( args->out_fh ) |
| 583 |
{ |
| 584 |
if ( args->write_index ) |
| 585 |
{ |
| 586 |
if ( bcf_idx_save(args->out_fh)<0 ) |
| 587 |
{ |
| 588 |
if ( hts_close(args->out_fh)!=0 ) error("Error: close failed .. %s\n", args->output_fname?args->output_fname:"stdout"); |
| 589 |
error("Error: cannot write to index %s\n", args->index_fn); |
| 590 |
} |
| 591 |
free(args->index_fn); |
| 592 |
} |
| 593 |
ret = hts_close(args->out_fh); |
| 594 |
} |
| 595 |
else |
| 596 |
ret = fclose(args->out); |
| 597 |
if ( ret ) error("Error: close failed .. %s\n", args->output_fname?args->output_fname:"stdout"); |
| 598 |
for (i=0; i<args->vcf_rbuf.m; i++) |
| 599 |
{ |
| 600 |
vbuf_t *vbuf = args->vcf_buf[i]; |
| 601 |
if ( !vbuf ) continue; |
| 602 |
for (j=0; j<vbuf->m; j++) |
| 603 |
{ |
| 604 |
if ( !vbuf->vrec[j] ) continue; |
| 605 |
if ( vbuf->vrec[j]->line ) bcf_destroy(vbuf->vrec[j]->line); |
| 606 |
free(vbuf->vrec[j]->fmt_bm); |
| 607 |
free(vbuf->vrec[j]->vcsq); |
| 608 |
free(vbuf->vrec[j]); |
| 609 |
} |
| 610 |
free(vbuf->vrec); |
| 611 |
free(vbuf); |
| 612 |
} |
| 613 |
free(args->vcf_buf); |
| 614 |
free(args->rm_tr); |
| 615 |
free(args->csq_buf); |
| 616 |
free(args->hap->stack); |
| 617 |
free(args->hap->sseq.s); |
| 618 |
free(args->hap->tseq.s); |
| 619 |
free(args->hap->tref.s); |
| 620 |
free(args->hap); |
| 621 |
fai_destroy(args->fai); |
| 622 |
free(args->gt_arr); |
| 623 |
free(args->str.s); |
| 624 |
free(args->str2.s); |
| 625 |
free(args->chr_name); |
| 626 |
} |
| 627 |
|
| 628 |
|
| 629 |
|
| 630 |
|
| 631 |
#define SPLICE_VAR_REF 0 // ref: ACGT>ACGT, csq not applicable, skip completely |
| 632 |
#define SPLICE_OUTSIDE 1 // splice acceptor or similar; csq set and is done, does not overlap the region |
| 633 |
#define SPLICE_INSIDE 2 // overlaps coding region; csq can be set but coding prediction is needed |
| 634 |
#define SPLICE_OVERLAP 3 // indel overlaps region boundary, csq set but could not determine csq |
| 635 |
typedef struct |
| 636 |
{ |
| 637 |
gf_tscript_t *tr; |
| 638 |
struct { |
| 639 |
int32_t pos, rlen, alen, ial; |
| 640 |
char *ref, *alt; |
| 641 |
bcf1_t *rec; |
| 642 |
} vcf; |
| 643 |
uint16_t check_acceptor:1, |
| 644 |
check_start:1, |
| 645 |
check_stop:1, |
| 646 |
check_donor:1, |
| 647 |
check_region_beg:1, |
| 648 |
check_region_end:1, |
| 649 |
check_utr:1, |
| 650 |
set_refalt:1; |
| 651 |
uint32_t csq; |
| 652 |
int tbeg, tend; |
| 653 |
uint32_t ref_beg, |
| 654 |
ref_end; |
| 655 |
kstring_t kref, kalt; |
| 656 |
} |
| 657 |
splice_t; |
| 658 |
void splice_init(splice_t *splice, bcf1_t *rec) |
| 659 |
{ |
| 660 |
memset(splice,0,sizeof(*splice)); |
| 661 |
splice->vcf.rec = rec; |
| 662 |
splice->vcf.pos = rec->pos; |
| 663 |
splice->vcf.rlen = rec->rlen; |
| 664 |
splice->vcf.ref = rec->d.allele[0]; |
| 665 |
splice->csq = 0; |
| 666 |
} |
| 667 |
static inline void splice_build_hap(splice_t *splice, uint32_t beg, int len) |
| 668 |
{ |
| 669 |
|
| 670 |
|
| 671 |
|
| 672 |
int rlen, alen, rbeg, abeg; |
| 673 |
if ( len<0 ) |
| 674 |
{ |
| 675 |
rlen = alen = -len; |
| 676 |
rbeg = beg - rlen + 1; |
| 677 |
int dlen = splice->vcf.alen - splice->vcf.rlen; |
| 678 |
if ( dlen<0 && beg < splice->ref_end ) |
| 679 |
dlen += splice->ref_end - beg; |
| 680 |
abeg = rbeg + dlen; |
| 681 |
} |
| 682 |
else |
| 683 |
{ |
| 684 |
rbeg = abeg = beg; |
| 685 |
rlen = alen = len; |
| 686 |
|
| 687 |
} |
| 688 |
|
| 689 |
#define XDBG 0 |
| 690 |
#if XDBG |
| 691 |
fprintf(stderr,"build_hap: rbeg=%d + %d abeg=%d \n",rbeg,rlen,abeg); |
| 692 |
#endif |
| 693 |
splice->kref.l = 0; |
| 694 |
splice->kalt.l = 0; |
| 695 |
|
| 696 |
|
| 697 |
int roff; |
| 698 |
if ( rbeg < splice->vcf.pos ) |
| 699 |
{ |
| 700 |
assert( splice->tr->beg <= rbeg ); |
| 701 |
kputsn(TSCRIPT_AUX(splice->tr)->ref + N_REF_PAD + rbeg - splice->tr->beg, splice->vcf.pos - rbeg, &splice->kref); |
| 702 |
roff = 0; |
| 703 |
} |
| 704 |
else |
| 705 |
roff = rbeg - splice->vcf.pos; |
| 706 |
#if XDBG |
| 707 |
fprintf(stderr,"r1: %s roff=%d\n",splice->kref.s,roff); |
| 708 |
#endif |
| 709 |
|
| 710 |
if ( roff < splice->vcf.rlen && splice->kref.l < rlen ) |
| 711 |
{ |
| 712 |
int len = splice->vcf.rlen - roff; |
| 713 |
if ( len > rlen - splice->kref.l ) len = rlen - splice->kref.l; |
| 714 |
kputsn(splice->vcf.ref + roff, len, &splice->kref); |
| 715 |
} |
| 716 |
#if XDBG |
| 717 |
fprintf(stderr,"r2: %s\n",splice->kref.s); |
| 718 |
#endif |
| 719 |
|
| 720 |
uint32_t end = splice->vcf.pos + splice->vcf.rlen; |
| 721 |
if ( splice->kref.l < rlen ) |
| 722 |
{ |
| 723 |
if ( end + rlen - splice->kref.l - 1 > splice->tr->end ) |
| 724 |
rlen -= end + rlen - splice->kref.l - 1 - splice->tr->end; |
| 725 |
if ( splice->kref.l < rlen ) |
| 726 |
kputsn(TSCRIPT_AUX(splice->tr)->ref + N_REF_PAD + end - splice->tr->beg, rlen - splice->kref.l, &splice->kref); |
| 727 |
} |
| 728 |
#if XDBG |
| 729 |
fprintf(stderr,"r3: %s\n",splice->kref.s); |
| 730 |
#endif |
| 731 |
|
| 732 |
|
| 733 |
int aoff; |
| 734 |
if ( abeg < splice->vcf.pos ) |
| 735 |
{ |
| 736 |
assert( splice->tr->beg <= abeg ); |
| 737 |
kputsn(TSCRIPT_AUX(splice->tr)->ref + N_REF_PAD + abeg - splice->tr->beg, splice->vcf.pos - abeg, &splice->kalt); |
| 738 |
aoff = 0; |
| 739 |
} |
| 740 |
else |
| 741 |
aoff = abeg - splice->vcf.pos; |
| 742 |
#if XDBG |
| 743 |
fprintf(stderr,"a1: %s aoff=%d\n",splice->kalt.s,aoff); |
| 744 |
#endif |
| 745 |
|
| 746 |
if ( aoff < splice->vcf.alen && splice->kalt.l < alen ) |
| 747 |
{ |
| 748 |
int len = splice->vcf.alen - aoff; |
| 749 |
if ( len > alen - splice->kalt.l ) len = alen - splice->kalt.l; |
| 750 |
kputsn(splice->vcf.alt + aoff, len, &splice->kalt); |
| 751 |
aoff -= len; |
| 752 |
} |
| 753 |
if ( aoff < 0 ) aoff = 0; |
| 754 |
else aoff--; |
| 755 |
#if XDBG |
| 756 |
fprintf(stderr,"a2: %s aoff=%d\n",splice->kalt.s,aoff); |
| 757 |
#endif |
| 758 |
|
| 759 |
end = splice->vcf.pos + splice->vcf.rlen; |
| 760 |
if ( splice->kalt.l < alen ) |
| 761 |
{ |
| 762 |
if ( end + alen + aoff - splice->kalt.l - 1 > splice->tr->end ) |
| 763 |
alen -= end + alen + aoff - splice->kalt.l - 1 - splice->tr->end; |
| 764 |
if ( alen > 0 && alen > splice->kalt.l ) |
| 765 |
kputsn(TSCRIPT_AUX(splice->tr)->ref + aoff + N_REF_PAD + end - splice->tr->beg, alen - splice->kalt.l, &splice->kalt); |
| 766 |
} |
| 767 |
#if XDBG |
| 768 |
fprintf(stderr,"a3: %s\n",splice->kalt.s); |
| 769 |
fprintf(stderr," [%s]\n [%s]\n\n",splice->kref.s,splice->kalt.s); |
| 770 |
#endif |
| 771 |
} |
| 772 |
void csq_stage(args_t *args, csq_t *csq, bcf1_t *rec); |
| 773 |
static inline int csq_stage_utr(args_t *args, regitr_t *itr, bcf1_t *rec, uint32_t trid, uint32_t type, int ial) |
| 774 |
{ |
| 775 |
while ( regitr_overlap(itr) ) |
| 776 |
{ |
| 777 |
gf_utr_t *utr = regitr_payload(itr, gf_utr_t*); |
| 778 |
gf_tscript_t *tr = utr->tr; |
| 779 |
if ( tr->id != trid ) continue; |
| 780 |
csq_t csq; |
| 781 |
memset(&csq, 0, sizeof(csq_t)); |
| 782 |
csq.pos = rec->pos; |
| 783 |
csq.type.type = (utr->which==prime5 ? CSQ_UTR5 : CSQ_UTR3) | type; |
| 784 |
csq.type.biotype = tr->type; |
| 785 |
csq.type.strand = tr->strand; |
| 786 |
csq.type.trid = tr->id; |
| 787 |
csq.type.vcf_ial = ial; |
| 788 |
csq.type.gene = tr->gene->name; |
| 789 |
csq_stage(args, &csq, rec); |
| 790 |
return csq.type.type; |
| 791 |
} |
| 792 |
return 0; |
| 793 |
} |
| 794 |
static inline void csq_stage_splice(args_t *args, bcf1_t *rec, gf_tscript_t *tr, uint32_t type, int ial) |
| 795 |
{ |
| 796 |
#if XDBG |
| 797 |
fprintf(stderr,"csq_stage_splice %d: type=%d\n",(int)rec->pos+1,type); |
| 798 |
#endif |
| 799 |
if ( !type ) return; |
| 800 |
csq_t csq; |
| 801 |
memset(&csq, 0, sizeof(csq_t)); |
| 802 |
csq.pos = rec->pos; |
| 803 |
csq.type.type = type; |
| 804 |
csq.type.biotype = tr->type; |
| 805 |
csq.type.strand = tr->strand; |
| 806 |
csq.type.trid = tr->id; |
| 807 |
csq.type.vcf_ial = ial; |
| 808 |
csq.type.gene = tr->gene->name; |
| 809 |
csq_stage(args, &csq, rec); |
| 810 |
} |
| 811 |
static inline const char *drop_chr_prefix(args_t *args, const char *chr) |
| 812 |
{ |
| 813 |
if ( !args->unify_chr_names ) return chr; |
| 814 |
if ( !strncasecmp("chr",chr,3) ) return chr+3; |
| 815 |
return chr; |
| 816 |
} |
| 817 |
static inline const char *add_chr_prefix(args_t *args, const char *chr) |
| 818 |
{ |
| 819 |
if ( !args->unify_chr_names ) return chr; |
| 820 |
int len = strlen(chr); |
| 821 |
hts_expand(char,len+4,args->mchr_name,args->chr_name); |
| 822 |
memcpy(args->chr_name,"chr",3); |
| 823 |
memcpy(args->chr_name+3,chr,len+1); |
| 824 |
return args->chr_name; |
| 825 |
} |
| 826 |
static inline int splice_csq_ins(args_t *args, splice_t *splice, uint32_t ex_beg, uint32_t ex_end) |
| 827 |
{ |
| 828 |
|
| 829 |
|
| 830 |
if ( splice->tbeg || splice->vcf.ref[0]!=splice->vcf.alt[0] ) |
| 831 |
{ |
| 832 |
splice->ref_beg = splice->vcf.pos + splice->tbeg - 1; |
| 833 |
splice->ref_end = splice->vcf.pos + splice->vcf.rlen - splice->tend; |
| 834 |
} |
| 835 |
else |
| 836 |
{ |
| 837 |
if ( splice->tend ) splice->tend--; |
| 838 |
splice->ref_beg = splice->vcf.pos; |
| 839 |
splice->ref_end = splice->vcf.pos + splice->vcf.rlen - splice->tend; |
| 840 |
} |
| 841 |
#if XDBG |
| 842 |
fprintf(stderr,"ins: %s>%s .. ex=%d,%d beg,end=%d,%d tbeg,tend=%d,%d check_utr=%d start,stop,beg,end=%d,%d,%d,%d\n", splice->vcf.ref,splice->vcf.alt,ex_beg,ex_end,splice->ref_beg,splice->ref_end,splice->tbeg,splice->tend,splice->check_utr,splice->check_start,splice->check_stop,splice->check_region_beg,splice->check_region_end); |
| 843 |
#endif |
| 844 |
|
| 845 |
int ret; |
| 846 |
if ( splice->ref_beg >= ex_end ) |
| 847 |
{ |
| 848 |
if ( splice->check_utr ) |
| 849 |
{ |
| 850 |
regitr_t *itr = regitr_init(NULL); |
| 851 |
const char *chr = drop_chr_prefix(args, bcf_seqname(args->hdr,splice->vcf.rec)); |
| 852 |
if ( regidx_overlap(args->idx_utr,chr,splice->ref_beg+1,splice->ref_beg+1, itr) ) |
| 853 |
{ |
| 854 |
ret = csq_stage_utr(args, itr, splice->vcf.rec, splice->tr->id, splice->csq, splice->vcf.ial); |
| 855 |
if ( ret!=0 ) |
| 856 |
{ |
| 857 |
regitr_destroy(itr); |
| 858 |
return SPLICE_OUTSIDE; |
| 859 |
} |
| 860 |
} |
| 861 |
regitr_destroy(itr); |
| 862 |
} |
| 863 |
if ( !splice->check_region_end ) return SPLICE_OUTSIDE; |
| 864 |
char *ref = NULL, *alt = NULL; |
| 865 |
if ( splice->set_refalt ) |
| 866 |
{ |
| 867 |
splice_build_hap(splice, ex_end+1, N_SPLICE_REGION_INTRON); |
| 868 |
ref = splice->kref.s, alt = splice->kalt.s; |
| 869 |
} |
| 870 |
if ( splice->ref_beg < ex_end + N_SPLICE_REGION_INTRON && splice->ref_end > ex_end + N_SPLICE_DONOR ) |
| 871 |
{ |
| 872 |
splice->csq |= CSQ_SPLICE_REGION; |
| 873 |
if ( ref && !strncmp(ref,alt,N_SPLICE_REGION_INTRON) ) splice->csq |= CSQ_SYNONYMOUS_VARIANT; |
| 874 |
} |
| 875 |
if ( splice->ref_beg < ex_end + N_SPLICE_DONOR ) |
| 876 |
{ |
| 877 |
if ( splice->check_donor && splice->tr->strand==STRAND_FWD ) splice->csq |= CSQ_SPLICE_DONOR; |
| 878 |
if ( splice->check_acceptor && splice->tr->strand==STRAND_REV ) splice->csq |= CSQ_SPLICE_ACCEPTOR; |
| 879 |
if ( ref && !strncmp(ref,alt,N_SPLICE_DONOR) ) splice->csq |= CSQ_SYNONYMOUS_VARIANT; |
| 880 |
} |
| 881 |
csq_stage_splice(args, splice->vcf.rec, splice->tr, splice->csq, splice->vcf.ial); |
| 882 |
return SPLICE_OUTSIDE; |
| 883 |
} |
| 884 |
if ( splice->ref_end < ex_beg || (splice->ref_end == ex_beg && !splice->check_region_beg) ) |
| 885 |
{ |
| 886 |
if ( splice->check_utr ) |
| 887 |
{ |
| 888 |
regitr_t *itr = regitr_init(NULL); |
| 889 |
const char *chr = drop_chr_prefix(args, bcf_seqname(args->hdr,splice->vcf.rec)); |
| 890 |
if ( regidx_overlap(args->idx_utr,chr,splice->ref_end-1,splice->ref_end-1, itr) ) |
| 891 |
{ |
| 892 |
ret = csq_stage_utr(args, itr, splice->vcf.rec, splice->tr->id, splice->csq, splice->vcf.ial); |
| 893 |
if ( ret!=0 ) |
| 894 |
{ |
| 895 |
regitr_destroy(itr); |
| 896 |
return SPLICE_OUTSIDE; |
| 897 |
} |
| 898 |
} |
| 899 |
regitr_destroy(itr); |
| 900 |
} |
| 901 |
if ( !splice->check_region_beg ) return SPLICE_OUTSIDE; |
| 902 |
char *ref = NULL, *alt = NULL; |
| 903 |
if ( splice->set_refalt ) |
| 904 |
{ |
| 905 |
splice_build_hap(splice, ex_beg - N_SPLICE_REGION_INTRON, N_SPLICE_REGION_INTRON); |
| 906 |
ref = splice->kref.s, alt = splice->kalt.s; |
| 907 |
} |
| 908 |
if ( splice->ref_end > ex_beg - N_SPLICE_REGION_INTRON && splice->ref_beg < ex_beg - N_SPLICE_DONOR ) |
| 909 |
{ |
| 910 |
splice->csq |= CSQ_SPLICE_REGION; |
| 911 |
if ( ref && !strncmp(ref,alt,N_SPLICE_REGION_INTRON) ) splice->csq |= CSQ_SYNONYMOUS_VARIANT; |
| 912 |
} |
| 913 |
if ( splice->ref_end > ex_beg - N_SPLICE_DONOR ) |
| 914 |
{ |
| 915 |
if ( splice->check_donor && splice->tr->strand==STRAND_REV ) splice->csq |= CSQ_SPLICE_DONOR; |
| 916 |
if ( splice->check_acceptor && splice->tr->strand==STRAND_FWD ) splice->csq |= CSQ_SPLICE_ACCEPTOR; |
| 917 |
if ( ref && !strncmp(ref+N_SPLICE_REGION_INTRON-N_SPLICE_DONOR,alt+N_SPLICE_REGION_INTRON-N_SPLICE_DONOR,N_SPLICE_DONOR) ) splice->csq |= CSQ_SYNONYMOUS_VARIANT; |
| 918 |
} |
| 919 |
csq_stage_splice(args, splice->vcf.rec, splice->tr, splice->csq, splice->vcf.ial); |
| 920 |
return SPLICE_OUTSIDE; |
| 921 |
} |
| 922 |
|
| 923 |
|
| 924 |
if ( splice->ref_beg <= ex_beg + 2 ) |
| 925 |
{ |
| 926 |
if ( splice->check_region_beg ) splice->csq |= CSQ_SPLICE_REGION; |
| 927 |
if ( splice->tr->strand==STRAND_FWD ) { if ( splice->check_start ) splice->csq |= CSQ_START_LOST; } |
| 928 |
else { if ( splice->check_stop ) splice->csq |= CSQ_STOP_LOST; } |
| 929 |
} |
| 930 |
if ( splice->ref_end > ex_end - 2 ) |
| 931 |
{ |
| 932 |
if ( splice->check_region_end ) splice->csq |= CSQ_SPLICE_REGION; |
| 933 |
if ( splice->tr->strand==STRAND_REV ) { if ( splice->check_start ) splice->csq |= CSQ_START_LOST; } |
| 934 |
else { if ( splice->check_stop ) splice->csq |= CSQ_STOP_LOST; } |
| 935 |
} |
| 936 |
if ( splice->set_refalt ) |
| 937 |
{ |
| 938 |
|
| 939 |
|
| 940 |
|
| 941 |
|
| 942 |
if ( splice->ref_beg < splice->vcf.pos ) |
| 943 |
{ |
| 944 |
int dlen = splice->vcf.pos - splice->ref_beg; |
| 945 |
assert( dlen==1 ); |
| 946 |
splice->tbeg += dlen; |
| 947 |
if ( splice->tbeg + splice->tend == splice->vcf.rlen ) splice->tend -= dlen; |
| 948 |
splice->ref_beg = splice->vcf.pos; |
| 949 |
} |
| 950 |
if ( splice->ref_end==ex_beg ) splice->tend--; |
| 951 |
splice_build_hap(splice, splice->ref_beg, splice->vcf.alen - splice->tend - splice->tbeg + 1); |
| 952 |
splice->vcf.rlen -= splice->tbeg + splice->tend - 1; |
| 953 |
if ( splice->kref.l > splice->vcf.rlen ) { splice->kref.l = splice->vcf.rlen; splice->kref.s[splice->kref.l] = 0; } |
| 954 |
} |
| 955 |
csq_stage_splice(args, splice->vcf.rec, splice->tr, splice->csq, splice->vcf.ial); |
| 956 |
return SPLICE_INSIDE; |
| 957 |
} |
| 958 |
|
| 959 |
int shifted_del_synonymous(args_t *args, splice_t *splice, uint32_t ex_beg, uint32_t ex_end) |
| 960 |
{ |
| 961 |
static int small_ref_padding_warned = 0; |
| 962 |
gf_tscript_t *tr = splice->tr; |
| 963 |
|
| 964 |
|
| 965 |
if ( tr->strand==STRAND_REV && splice->vcf.pos + splice->vcf.rlen + 2 <= ex_end ) return 0; |
| 966 |
if ( tr->strand==STRAND_FWD && splice->vcf.pos >= ex_beg + 3 ) return 0; |
| 967 |
|
| 968 |
#if XDBG |
| 969 |
fprintf(stderr,"shifted_del_synonymous: %d-%d %s\n",ex_beg,ex_end, tr->strand==STRAND_FWD?"fwd":(tr->strand==STRAND_REV?"rev":"unk")); |
| 970 |
fprintf(stderr," %d .. %s > %s\n",splice->vcf.pos+1,splice->vcf.ref,splice->vcf.alt); |
| 971 |
#endif |
| 972 |
|
| 973 |
|
| 974 |
int ref_len = strlen(splice->vcf.ref); |
| 975 |
int alt_len = strlen(splice->vcf.alt); |
| 976 |
assert( ref_len > alt_len ); |
| 977 |
int ndel = ref_len - alt_len; |
| 978 |
|
| 979 |
if ( tr->strand==STRAND_REV ) |
| 980 |
{ |
| 981 |
int32_t vcf_ref_end = splice->vcf.pos + ref_len - 1; |
| 982 |
int32_t tr_ref_end = splice->tr->end + N_REF_PAD; |
| 983 |
if ( vcf_ref_end + ndel > tr_ref_end ) |
| 984 |
{ |
| 985 |
if ( !small_ref_padding_warned ) |
| 986 |
{ |
| 987 |
fprintf(stderr,"Warning: Could not verify synonymous start/stop at %s:%d due to small N_REF_PAD. (Improve me?)\n",bcf_seqname(args->hdr,splice->vcf.rec),splice->vcf.pos+1); |
| 988 |
small_ref_padding_warned = 1; |
| 989 |
} |
| 990 |
return 0; |
| 991 |
} |
| 992 |
|
| 993 |
char *ptr_vcf = splice->vcf.ref + alt_len; |
| 994 |
char *ptr_ref = TSCRIPT_AUX(splice->tr)->ref + N_REF_PAD + (vcf_ref_end + 1 - splice->tr->beg); |
| 995 |
#if XDBG |
| 996 |
fprintf(stderr,"vcf: %s\nref: %s\n",ptr_vcf,ptr_ref); |
| 997 |
#endif |
| 998 |
int i = 0; |
| 999 |
while ( ptr_vcf[i] && ptr_vcf[i]==ptr_ref[i] ) i++; |
| 1000 |
if ( ptr_vcf[i] ) return 0; |
| 1001 |
} |
| 1002 |
else if ( tr->strand==STRAND_FWD ) |
| 1003 |
{ |
| 1004 |
|
| 1005 |
int32_t vcf_block_beg = splice->vcf.pos + ref_len - 2*ndel; |
| 1006 |
if ( vcf_block_beg < 0 ) return 0; |
| 1007 |
|
| 1008 |
#if XDBG |
| 1009 |
fprintf(stderr,"vcf_block_beg: %d\n",vcf_block_beg+1); |
| 1010 |
#endif |
| 1011 |
|
| 1012 |
if ( N_REF_PAD + vcf_block_beg < ex_beg ) |
| 1013 |
{ |
| 1014 |
if ( !small_ref_padding_warned ) |
| 1015 |
{ |
| 1016 |
fprintf(stderr,"Warning: Could not verify synonymous start/stop at %s:%d due to small N_REF_PAD. (Improve me?)\n",bcf_seqname(args->hdr,splice->vcf.rec),splice->vcf.pos+1); |
| 1017 |
small_ref_padding_warned = 1; |
| 1018 |
} |
| 1019 |
return 0; |
| 1020 |
} |
| 1021 |
|
| 1022 |
char *ptr_vcf = splice->vcf.ref + alt_len; |
| 1023 |
char *ptr_ref = TSCRIPT_AUX(splice->tr)->ref + N_REF_PAD + vcf_block_beg - splice->tr->beg; |
| 1024 |
#if XDBG |
| 1025 |
fprintf(stderr,"vcf: %s\nref: %s\n",ptr_vcf,ptr_ref); |
| 1026 |
#endif |
| 1027 |
|
| 1028 |
int i = 0; |
| 1029 |
while ( ptr_vcf[i] && ptr_vcf[i]==ptr_ref[i] ) i++; |
| 1030 |
if ( ptr_vcf[i] ) return 0; |
| 1031 |
} |
| 1032 |
|
| 1033 |
return 1; |
| 1034 |
} |
| 1035 |
|
| 1036 |
static inline int splice_csq_del(args_t *args, splice_t *splice, uint32_t ex_beg, uint32_t ex_end) |
| 1037 |
{ |
| 1038 |
if ( splice->check_start ) |
| 1039 |
{ |
| 1040 |
|
| 1041 |
|
| 1042 |
|
| 1043 |
|
| 1044 |
int is_synonymous = shifted_del_synonymous(args, splice, ex_beg, ex_end); |
| 1045 |
if ( is_synonymous ) |
| 1046 |
{ |
| 1047 |
splice->csq |= CSQ_START_RETAINED; |
| 1048 |
return SPLICE_OVERLAP; |
| 1049 |
} |
| 1050 |
} |
| 1051 |
|
| 1052 |
|
| 1053 |
splice->ref_beg = splice->vcf.pos + splice->tbeg - 1; |
| 1054 |
splice->ref_end = splice->vcf.pos + splice->vcf.rlen - splice->tend - 1; |
| 1055 |
|
| 1056 |
#if XDBG |
| 1057 |
fprintf(stderr,"splice_csq_del: %s>%s .. ex=%d,%d beg,end=%d,%d tbeg,tend=%d,%d check_utr=%d start,stop,beg,end=%d,%d,%d,%d\n", splice->vcf.ref,splice->vcf.alt,ex_beg,ex_end,splice->ref_beg,splice->ref_end,splice->tbeg,splice->tend,splice->check_utr,splice->check_start,splice->check_stop,splice->check_region_beg,splice->check_region_end); |
| 1058 |
#endif |
| 1059 |
|
| 1060 |
if ( splice->ref_beg + 1 < ex_beg ) |
| 1061 |
{ |
| 1062 |
if ( splice->check_region_beg ) |
| 1063 |
{ |
| 1064 |
int csq = 0; |
| 1065 |
if ( splice->check_utr ) |
| 1066 |
{ |
| 1067 |
regitr_t *itr = regitr_init(NULL); |
| 1068 |
const char *chr = drop_chr_prefix(args, bcf_seqname(args->hdr,splice->vcf.rec)); |
| 1069 |
if ( regidx_overlap(args->idx_utr,chr,splice->ref_beg,ex_beg-1, itr) ) |
| 1070 |
csq = csq_stage_utr(args, itr, splice->vcf.rec, splice->tr->id, splice->csq, splice->vcf.ial); |
| 1071 |
regitr_destroy(itr); |
| 1072 |
} |
| 1073 |
if ( !csq ) |
| 1074 |
{ |
| 1075 |
char *ref = NULL, *alt = NULL; |
| 1076 |
if ( splice->set_refalt ) |
| 1077 |
{ |
| 1078 |
|
| 1079 |
|
| 1080 |
|
| 1081 |
|
| 1082 |
|
| 1083 |
|
| 1084 |
splice_build_hap(splice, ex_beg - N_SPLICE_REGION_INTRON, N_SPLICE_REGION_INTRON); |
| 1085 |
ref = splice->kref.s, alt = splice->kalt.s; |
| 1086 |
} |
| 1087 |
if ( splice->ref_end >= ex_beg - N_SPLICE_REGION_INTRON && splice->ref_beg < ex_beg - N_SPLICE_DONOR ) |
| 1088 |
{ |
| 1089 |
splice->csq |= CSQ_SPLICE_REGION; |
| 1090 |
if ( ref && alt && !strncmp(ref,alt,N_SPLICE_REGION_INTRON) ) splice->csq |= CSQ_SYNONYMOUS_VARIANT; |
| 1091 |
} |
| 1092 |
if ( splice->ref_end >= ex_beg - N_SPLICE_DONOR ) |
| 1093 |
{ |
| 1094 |
if ( splice->check_donor && splice->tr->strand==STRAND_REV ) splice->csq |= CSQ_SPLICE_DONOR; |
| 1095 |
if ( splice->check_acceptor && splice->tr->strand==STRAND_FWD ) splice->csq |= CSQ_SPLICE_ACCEPTOR; |
| 1096 |
if ( ref && alt && !strncmp(ref+N_SPLICE_REGION_INTRON-N_SPLICE_DONOR,alt+N_SPLICE_REGION_INTRON-N_SPLICE_DONOR,N_SPLICE_DONOR) ) splice->csq |= CSQ_SYNONYMOUS_VARIANT; |
| 1097 |
} |
| 1098 |
} |
| 1099 |
} |
| 1100 |
if ( splice->ref_end >= ex_beg ) |
| 1101 |
{ |
| 1102 |
splice->tbeg = splice->ref_beg - splice->vcf.pos + 1; |
| 1103 |
splice->ref_beg = ex_beg - 1; |
| 1104 |
if ( splice->tbeg + splice->tend == splice->vcf.alen ) |
| 1105 |
{ |
| 1106 |
|
| 1107 |
if ( !splice->tend ) |
| 1108 |
{ |
| 1109 |
splice->csq |= CSQ_CODING_SEQUENCE; |
| 1110 |
return SPLICE_OVERLAP; |
| 1111 |
} |
| 1112 |
splice->tend--; |
| 1113 |
} |
| 1114 |
} |
| 1115 |
} |
| 1116 |
if ( ex_end < splice->ref_end ) |
| 1117 |
{ |
| 1118 |
if ( splice->check_region_end ) |
| 1119 |
{ |
| 1120 |
int csq = 0; |
| 1121 |
if ( splice->check_utr ) |
| 1122 |
{ |
| 1123 |
regitr_t *itr = regitr_init(NULL); |
| 1124 |
const char *chr = drop_chr_prefix(args, bcf_seqname(args->hdr,splice->vcf.rec)); |
| 1125 |
if ( regidx_overlap(args->idx_utr,chr,ex_end+1,splice->ref_end, itr) ) |
| 1126 |
csq = csq_stage_utr(args, itr, splice->vcf.rec, splice->tr->id, splice->csq, splice->vcf.ial); |
| 1127 |
regitr_destroy(itr); |
| 1128 |
} |
| 1129 |
if ( !csq ) |
| 1130 |
{ |
| 1131 |
char *ref = NULL, *alt = NULL; |
| 1132 |
if ( splice->set_refalt ) |
| 1133 |
{ |
| 1134 |
splice_build_hap(splice, ex_end+1, N_SPLICE_REGION_INTRON); |
| 1135 |
ref = splice->kref.s, alt = splice->kalt.s; |
| 1136 |
} |
| 1137 |
if ( splice->ref_beg < ex_end + N_SPLICE_REGION_INTRON && splice->ref_end > ex_end + N_SPLICE_DONOR ) |
| 1138 |
{ |
| 1139 |
splice->csq |= CSQ_SPLICE_REGION; |
| 1140 |
if ( ref && alt && !strncmp(ref,alt,N_SPLICE_REGION_INTRON) ) splice->csq |= CSQ_SYNONYMOUS_VARIANT; |
| 1141 |
} |
| 1142 |
if ( splice->ref_beg < ex_end + N_SPLICE_DONOR ) |
| 1143 |
{ |
| 1144 |
if ( splice->check_donor && splice->tr->strand==STRAND_FWD ) splice->csq |= CSQ_SPLICE_DONOR; |
| 1145 |
if ( splice->check_acceptor && splice->tr->strand==STRAND_REV ) splice->csq |= CSQ_SPLICE_ACCEPTOR; |
| 1146 |
if ( ref && alt && !strncmp(ref+N_SPLICE_REGION_INTRON-N_SPLICE_DONOR,alt+N_SPLICE_REGION_INTRON-N_SPLICE_DONOR,N_SPLICE_DONOR) ) splice->csq |= CSQ_SYNONYMOUS_VARIANT; |
| 1147 |
} |
| 1148 |
} |
| 1149 |
} |
| 1150 |
if ( splice->ref_beg < ex_end ) |
| 1151 |
{ |
| 1152 |
splice->tend = splice->vcf.rlen - (splice->ref_end - splice->vcf.pos + 1); |
| 1153 |
splice->ref_end = ex_end; |
| 1154 |
} |
| 1155 |
} |
| 1156 |
if ( splice->ref_end < ex_beg || splice->ref_beg >= ex_end ) |
| 1157 |
{ |
| 1158 |
csq_stage_splice(args, splice->vcf.rec, splice->tr, splice->csq, splice->vcf.ial); |
| 1159 |
return SPLICE_OUTSIDE; |
| 1160 |
} |
| 1161 |
if ( splice->ref_beg < ex_beg + 2 ) |
| 1162 |
{ |
| 1163 |
if ( splice->check_region_beg ) splice->csq |= CSQ_SPLICE_REGION; |
| 1164 |
if ( splice->tr->strand==STRAND_FWD ) { if ( splice->check_start ) splice->csq |= CSQ_START_LOST; } |
| 1165 |
else { if ( splice->check_stop ) splice->csq |= CSQ_STOP_LOST; } |
| 1166 |
} |
| 1167 |
if ( splice->ref_end > ex_end - 3 ) |
| 1168 |
{ |
| 1169 |
if ( splice->check_region_end ) splice->csq |= CSQ_SPLICE_REGION; |
| 1170 |
if ( splice->tr->strand==STRAND_REV ) { if ( splice->check_start ) splice->csq |= CSQ_START_LOST; } |
| 1171 |
else { if ( splice->check_stop ) splice->csq |= CSQ_STOP_LOST; } |
| 1172 |
} |
| 1173 |
if ( splice->set_refalt ) |
| 1174 |
{ |
| 1175 |
if ( splice->tbeg>0 ) splice->tbeg--; |
| 1176 |
if ( splice->vcf.rlen > splice->tbeg + splice->tend && splice->vcf.alen > splice->tbeg + splice->tend ) |
| 1177 |
{ |
| 1178 |
splice->vcf.rlen -= splice->tbeg + splice->tend; |
| 1179 |
splice->vcf.alen -= splice->tbeg + splice->tend; |
| 1180 |
} |
| 1181 |
splice->kref.l = 0; kputsn(splice->vcf.ref + splice->tbeg, splice->vcf.rlen, &splice->kref); |
| 1182 |
splice->kalt.l = 0; kputsn(splice->vcf.alt + splice->tbeg, splice->vcf.alen, &splice->kalt); |
| 1183 |
if ( (splice->ref_beg+1 < ex_beg && splice->ref_end >= ex_beg) || (splice->ref_beg+1 < ex_end && splice->ref_end >= ex_end) ) |
| 1184 |
{ |
| 1185 |
int ref_beg = splice->ref_beg + splice->kalt.l - 1; |
| 1186 |
if ( ref_beg < splice->ref_end ) |
| 1187 |
splice->csq |= (splice->ref_end - ref_beg)%3 ? CSQ_FRAMESHIFT_VARIANT : CSQ_INFRAME_DELETION; |
| 1188 |
return SPLICE_OVERLAP; |
| 1189 |
} |
| 1190 |
} |
| 1191 |
csq_stage_splice(args, splice->vcf.rec, splice->tr, splice->csq, splice->vcf.ial); |
| 1192 |
return SPLICE_INSIDE; |
| 1193 |
} |
| 1194 |
|
| 1195 |
static inline int splice_csq_mnp(args_t *args, splice_t *splice, uint32_t ex_beg, uint32_t ex_end) |
| 1196 |
{ |
| 1197 |
|
| 1198 |
if ( splice->tbeg + splice->tend == splice->vcf.rlen ) return SPLICE_VAR_REF; |
| 1199 |
|
| 1200 |
splice->ref_beg = splice->vcf.pos + splice->tbeg; |
| 1201 |
splice->ref_end = splice->vcf.pos + splice->vcf.rlen - splice->tend - 1; |
| 1202 |
|
| 1203 |
#if XDBG |
| 1204 |
fprintf(stderr,"mnp: %s>%s .. ex=%d,%d beg,end=%d,%d tbeg,tend=%d,%d check_utr=%d start,stop,beg,end=%d,%d,%d,%d\n", splice->vcf.ref,splice->vcf.alt,ex_beg,ex_end,splice->ref_beg,splice->ref_end,splice->tbeg,splice->tend,splice->check_utr,splice->check_start,splice->check_stop,splice->check_region_beg,splice->check_region_end); |
| 1205 |
#endif |
| 1206 |
|
| 1207 |
if ( splice->ref_beg < ex_beg ) |
| 1208 |
{ |
| 1209 |
if ( splice->check_region_beg ) |
| 1210 |
{ |
| 1211 |
int csq = 0; |
| 1212 |
if ( splice->check_utr ) |
| 1213 |
{ |
| 1214 |
regitr_t *itr = regitr_init(NULL); |
| 1215 |
const char *chr = drop_chr_prefix(args, bcf_seqname(args->hdr,splice->vcf.rec)); |
| 1216 |
if ( regidx_overlap(args->idx_utr,chr,splice->ref_beg,ex_beg-1, itr) ) |
| 1217 |
csq = csq_stage_utr(args, itr, splice->vcf.rec, splice->tr->id, splice->csq, splice->vcf.ial); |
| 1218 |
regitr_destroy(itr); |
| 1219 |
} |
| 1220 |
if ( !csq ) |
| 1221 |
{ |
| 1222 |
if ( splice->ref_end >= ex_beg - N_SPLICE_REGION_INTRON && splice->ref_beg < ex_beg - N_SPLICE_DONOR ) |
| 1223 |
splice->csq |= CSQ_SPLICE_REGION; |
| 1224 |
if ( splice->ref_end >= ex_beg - N_SPLICE_DONOR ) |
| 1225 |
{ |
| 1226 |
if ( splice->check_donor && splice->tr->strand==STRAND_REV ) splice->csq |= CSQ_SPLICE_DONOR; |
| 1227 |
if ( splice->check_acceptor && splice->tr->strand==STRAND_FWD ) splice->csq |= CSQ_SPLICE_ACCEPTOR; |
| 1228 |
} |
| 1229 |
} |
| 1230 |
} |
| 1231 |
if ( splice->ref_end >= ex_beg ) |
| 1232 |
{ |
| 1233 |
splice->tbeg = splice->ref_beg - splice->vcf.pos; |
| 1234 |
splice->ref_beg = ex_beg; |
| 1235 |
} |
| 1236 |
} |
| 1237 |
if ( ex_end < splice->ref_end ) |
| 1238 |
{ |
| 1239 |
if ( splice->check_region_end ) |
| 1240 |
{ |
| 1241 |
int csq = 0; |
| 1242 |
if ( splice->check_utr ) |
| 1243 |
{ |
| 1244 |
regitr_t *itr = regitr_init(NULL); |
| 1245 |
const char *chr = drop_chr_prefix(args, bcf_seqname(args->hdr,splice->vcf.rec)); |
| 1246 |
if ( regidx_overlap(args->idx_utr,chr,ex_end+1,splice->ref_end, itr) ) |
| 1247 |
csq = csq_stage_utr(args, itr, splice->vcf.rec, splice->tr->id, splice->csq, splice->vcf.ial); |
| 1248 |
regitr_destroy(itr); |
| 1249 |
} |
| 1250 |
if ( !csq ) |
| 1251 |
{ |
| 1252 |
if ( splice->ref_beg <= ex_end + N_SPLICE_REGION_INTRON && splice->ref_end > ex_end + N_SPLICE_DONOR ) |
| 1253 |
splice->csq |= CSQ_SPLICE_REGION; |
| 1254 |
if ( splice->ref_beg <= ex_end + N_SPLICE_DONOR ) |
| 1255 |
{ |
| 1256 |
if ( splice->check_donor && splice->tr->strand==STRAND_FWD ) splice->csq |= CSQ_SPLICE_DONOR; |
| 1257 |
if ( splice->check_acceptor && splice->tr->strand==STRAND_REV ) splice->csq |= CSQ_SPLICE_ACCEPTOR; |
| 1258 |
} |
| 1259 |
} |
| 1260 |
} |
| 1261 |
if ( splice->ref_beg <= ex_end ) |
| 1262 |
{ |
| 1263 |
splice->tend = splice->vcf.rlen - (splice->ref_end - splice->vcf.pos + 1); |
| 1264 |
splice->ref_end = ex_end; |
| 1265 |
} |
| 1266 |
} |
| 1267 |
if ( splice->ref_end < ex_beg || splice->ref_beg > ex_end ) |
| 1268 |
{ |
| 1269 |
csq_stage_splice(args, splice->vcf.rec, splice->tr, splice->csq, splice->vcf.ial); |
| 1270 |
return SPLICE_OUTSIDE; |
| 1271 |
} |
| 1272 |
|
| 1273 |
if ( splice->ref_beg < ex_beg + 3 ) |
| 1274 |
{ |
| 1275 |
if ( splice->check_region_beg ) splice->csq |= CSQ_SPLICE_REGION; |
| 1276 |
if ( splice->tr->strand==STRAND_FWD ) { if ( splice->check_start ) splice->csq |= CSQ_START_LOST; } |
| 1277 |
else if ( splice->tr->strand==STRAND_REV ) { if ( splice->check_stop ) splice->csq |= CSQ_STOP_LOST; } |
| 1278 |
} |
| 1279 |
if ( splice->ref_end > ex_end - 3 ) |
| 1280 |
{ |
| 1281 |
if ( splice->check_region_end ) splice->csq |= CSQ_SPLICE_REGION; |
| 1282 |
if ( splice->tr->strand==STRAND_REV ) { if ( splice->check_start ) splice->csq |= CSQ_START_LOST; } |
| 1283 |
else if ( splice->tr->strand==STRAND_FWD ) { if ( splice->check_stop ) splice->csq |= CSQ_STOP_LOST; } |
| 1284 |
} |
| 1285 |
if ( splice->set_refalt ) |
| 1286 |
{ |
| 1287 |
splice->vcf.rlen -= splice->tbeg + splice->tend; |
| 1288 |
splice->kref.l = 0; kputsn(splice->vcf.ref + splice->tbeg, splice->vcf.rlen, &splice->kref); |
| 1289 |
splice->kalt.l = 0; kputsn(splice->vcf.alt + splice->tbeg, splice->vcf.rlen, &splice->kalt); |
| 1290 |
} |
| 1291 |
csq_stage_splice(args, splice->vcf.rec, splice->tr, splice->csq, splice->vcf.ial); |
| 1292 |
return SPLICE_INSIDE; |
| 1293 |
} |
| 1294 |
static inline int splice_csq(args_t *args, splice_t *splice, uint32_t ex_beg, uint32_t ex_end) |
| 1295 |
{ |
| 1296 |
splice->vcf.alen = strlen(splice->vcf.alt); |
| 1297 |
|
| 1298 |
int rlen1 = splice->vcf.rlen - 1, alen1 = splice->vcf.alen - 1, i = 0; |
| 1299 |
splice->tbeg = 0, splice->tend = 0; |
| 1300 |
|
| 1301 |
|
| 1302 |
while ( i<=rlen1 && i<=alen1 ) |
| 1303 |
{ |
| 1304 |
if ( splice->vcf.ref[rlen1-i] != splice->vcf.alt[alen1-i] ) break; |
| 1305 |
i++; |
| 1306 |
} |
| 1307 |
splice->tend = i; |
| 1308 |
rlen1 -= i, alen1 -= i, i = 0; |
| 1309 |
while ( i<=rlen1 && i<=alen1 ) |
| 1310 |
{ |
| 1311 |
if ( splice->vcf.ref[i] != splice->vcf.alt[i] ) break; |
| 1312 |
i++; |
| 1313 |
} |
| 1314 |
splice->tbeg = i; |
| 1315 |
|
| 1316 |
|
| 1317 |
|
| 1318 |
if ( splice->vcf.rlen==splice->vcf.alen ) return splice_csq_mnp(args, splice, ex_beg, ex_end); |
| 1319 |
if ( splice->vcf.rlen < splice->vcf.alen ) return splice_csq_ins(args, splice, ex_beg, ex_end); |
| 1320 |
if ( splice->vcf.rlen > splice->vcf.alen ) return splice_csq_del(args, splice, ex_beg, ex_end); |
| 1321 |
|
| 1322 |
return 0; |
| 1323 |
} |
| 1324 |
|
| 1325 |
|
| 1326 |
|
| 1327 |
int hap_init(args_t *args, hap_node_t *parent, hap_node_t *child, gf_cds_t *cds, bcf1_t *rec, int ial) |
| 1328 |
{ |
| 1329 |
int i; |
| 1330 |
kstring_t str = {0,0,0}; |
| 1331 |
gf_tscript_t *tr = cds->tr; |
| 1332 |
child->icds = cds->icds; |
| 1333 |
child->vcf_ial = ial; |
| 1334 |
|
| 1335 |
splice_t splice; |
| 1336 |
splice_init(&splice, rec); |
| 1337 |
splice.tr = tr; |
| 1338 |
splice.vcf.ial = ial; |
| 1339 |
splice.vcf.alt = rec->d.allele[ial]; |
| 1340 |
splice.check_acceptor = splice.check_donor = splice.set_refalt = splice.check_utr = 1; |
| 1341 |
if ( !(tr->trim & TRIM_5PRIME) ) |
| 1342 |
{ |
| 1343 |
if ( tr->strand==STRAND_FWD ) { if ( child->icds==0 ) splice.check_start = 1; } |
| 1344 |
else if ( tr->strand==STRAND_REV ) { if ( child->icds==tr->ncds-1 ) splice.check_start = 1; } |
| 1345 |
} |
| 1346 |
if ( !(tr->trim & TRIM_3PRIME) ) |
| 1347 |
{ |
| 1348 |
if ( tr->strand==STRAND_FWD ) { if ( child->icds==tr->ncds-1 ) splice.check_stop = 1; } |
| 1349 |
else if ( tr->strand==STRAND_REV ) { if ( child->icds==0 ) splice.check_stop = 1; } |
| 1350 |
} |
| 1351 |
if ( splice.check_start ) |
| 1352 |
{ |
| 1353 |
if ( tr->strand==STRAND_FWD ) { if ( dna2aa(TSCRIPT_AUX(tr)->ref+N_REF_PAD+cds->beg-tr->beg) != 'M' ) splice.check_start = 0; } |
| 1354 |
else if ( tr->strand==STRAND_REV ) { if ( cdna2aa(TSCRIPT_AUX(tr)->ref+N_REF_PAD+cds->beg-tr->beg+cds->len-3) != 'M' ) splice.check_start = 0; } |
| 1355 |
} |
| 1356 |
if ( child->icds!=0 ) splice.check_region_beg = 1; |
| 1357 |
if ( child->icds!=tr->ncds-1 ) splice.check_region_end = 1; |
| 1358 |
|
| 1359 |
#if XDBG |
| 1360 |
fprintf(stderr,"\nhap_init: %d [%s][%s] check start:%d,stop:%d\n",splice.vcf.pos+1,splice.vcf.ref,splice.vcf.alt,splice.check_start,splice.check_stop); |
| 1361 |
#endif |
| 1362 |
int ret = splice_csq(args, &splice, cds->beg, cds->beg + cds->len - 1); |
| 1363 |
#if XDBG |
| 1364 |
fprintf(stderr,"cds splice_csq: %d [%s][%s] .. beg,end=%d %d, ret=%d, csq=%d\n\n",splice.vcf.pos+1,splice.kref.s,splice.kalt.s,splice.ref_beg+1,splice.ref_end+1,ret,splice.csq); |
| 1365 |
#endif |
| 1366 |
|
| 1367 |
if ( ret==SPLICE_VAR_REF ) return 2; |
| 1368 |
if ( ret==SPLICE_OUTSIDE || ret==SPLICE_OVERLAP || splice.csq==CSQ_START_LOST ) |
| 1369 |
{ |
| 1370 |
free(splice.kref.s); |
| 1371 |
free(splice.kalt.s); |
| 1372 |
|
| 1373 |
if ( !splice.csq ) return 2; |
| 1374 |
|
| 1375 |
|
| 1376 |
child->seq = NULL; |
| 1377 |
child->sbeg = 0; |
| 1378 |
child->rbeg = rec->pos; |
| 1379 |
child->rlen = 0; |
| 1380 |
child->dlen = 0; |
| 1381 |
kputs(rec->d.allele[0],&str); |
| 1382 |
kputc('>',&str); |
| 1383 |
kputs(rec->d.allele[ial],&str); |
| 1384 |
child->var = str.s; |
| 1385 |
child->type = HAP_SSS; |
| 1386 |
child->csq = splice.csq; |
| 1387 |
child->rec = rec; |
| 1388 |
return 0; |
| 1389 |
} |
| 1390 |
if ( splice.csq & CSQ_SYNONYMOUS_VARIANT ) splice.csq &= ~CSQ_SYNONYMOUS_VARIANT; |
| 1391 |
|
| 1392 |
int dbeg = 0; |
| 1393 |
if ( splice.ref_beg < cds->beg ) |
| 1394 |
{ |
| 1395 |
|
| 1396 |
|
| 1397 |
|
| 1398 |
dbeg = cds->beg - splice.ref_beg; |
| 1399 |
splice.kref.l -= dbeg; |
| 1400 |
splice.ref_beg = cds->beg; |
| 1401 |
assert( dbeg <= splice.kalt.l ); |
| 1402 |
} |
| 1403 |
|
| 1404 |
assert( parent->type!=HAP_SSS ); |
| 1405 |
if ( parent->type==HAP_CDS ) |
| 1406 |
{ |
| 1407 |
i = parent->icds; |
| 1408 |
if ( i!=cds->icds ) |
| 1409 |
{ |
| 1410 |
|
| 1411 |
int len = tr->cds[i]->len - parent->rbeg - parent->rlen + tr->cds[i]->beg; |
| 1412 |
if ( len > 0 ) |
| 1413 |
kputsn_(TSCRIPT_AUX(tr)->ref + N_REF_PAD + parent->rbeg + parent->rlen - tr->beg, len, &str); |
| 1414 |
} |
| 1415 |
|
| 1416 |
|
| 1417 |
while ( ++i < cds->icds ) |
| 1418 |
kputsn_(TSCRIPT_AUX(tr)->ref + N_REF_PAD + tr->cds[i]->beg - tr->beg, tr->cds[i]->len, &str); |
| 1419 |
|
| 1420 |
if ( parent->icds==child->icds ) |
| 1421 |
{ |
| 1422 |
int len = splice.ref_beg - parent->rbeg - parent->rlen; |
| 1423 |
if ( len < 0 ) |
| 1424 |
{ |
| 1425 |
free(str.s); |
| 1426 |
free(splice.kref.s); |
| 1427 |
free(splice.kalt.s); |
| 1428 |
return 1; |
| 1429 |
} |
| 1430 |
kputsn_(TSCRIPT_AUX(tr)->ref + N_REF_PAD + parent->rbeg + parent->rlen - tr->beg, len, &str); |
| 1431 |
} |
| 1432 |
else |
| 1433 |
kputsn_(TSCRIPT_AUX(tr)->ref + N_REF_PAD + cds->beg - tr->beg, splice.ref_beg - cds->beg, &str); |
| 1434 |
} |
| 1435 |
kputs(splice.kalt.s + dbeg, &str); |
| 1436 |
|
| 1437 |
child->seq = str.s; |
| 1438 |
child->sbeg = cds->pos + (splice.ref_beg - cds->beg); |
| 1439 |
child->rbeg = splice.ref_beg; |
| 1440 |
child->rlen = splice.kref.l; |
| 1441 |
child->type = HAP_CDS; |
| 1442 |
child->prev = parent; |
| 1443 |
child->rec = rec; |
| 1444 |
child->csq = splice.csq; |
| 1445 |
|
| 1446 |
|
| 1447 |
{ |
| 1448 |
int rlen = strlen(rec->d.allele[0]); |
| 1449 |
int alen = strlen(rec->d.allele[ial]); |
| 1450 |
child->dlen = alen - rlen; |
| 1451 |
child->var = (char*) malloc(rlen+alen+2); |
| 1452 |
memcpy(child->var,rec->d.allele[0],rlen); |
| 1453 |
child->var[rlen] = '>'; |
| 1454 |
memcpy(child->var+rlen+1,rec->d.allele[ial],alen); |
| 1455 |
child->var[rlen+alen+1] = 0; |
| 1456 |
} |
| 1457 |
|
| 1458 |
|
| 1459 |
if ( child->rbeg + child->rlen > cds->beg + cds->len ) |
| 1460 |
{ |
| 1461 |
child->type = HAP_SSS; |
| 1462 |
if ( !child->csq ) child->csq |= CSQ_CODING_SEQUENCE; |
| 1463 |
} |
| 1464 |
|
| 1465 |
|
| 1466 |
free(splice.kref.s); |
| 1467 |
free(splice.kalt.s); |
| 1468 |
return 0; |
| 1469 |
} |
| 1470 |
void hap_destroy(hap_node_t *hap) |
| 1471 |
{ |
| 1472 |
int i; |
| 1473 |
for (i=0; i<hap->nchild; i++) |
| 1474 |
if ( hap->child[i] ) hap_destroy(hap->child[i]); |
| 1475 |
for (i=0; i<hap->mcsq_list; i++) free(hap->csq_list[i].type.vstr.s); |
| 1476 |
free(hap->csq_list); |
| 1477 |
free(hap->child); |
| 1478 |
free(hap->cur_child); |
| 1479 |
free(hap->seq); |
| 1480 |
free(hap->var); |
| 1481 |
free(hap); |
| 1482 |
} |
| 1483 |
|
| 1484 |
|
| 1485 |
|
| 1486 |
|
| 1487 |
|
| 1488 |
|
| 1489 |
|
| 1490 |
|
| 1491 |
|
| 1492 |
|
| 1493 |
|
| 1494 |
|
| 1495 |
|
| 1496 |
void cds_translate(kstring_t *_ref, kstring_t *_seq, uint32_t sbeg, uint32_t rbeg, uint32_t rend, int strand, kstring_t *tseq, int fill) |
| 1497 |
{ |
| 1498 |
#if XDBG |
| 1499 |
fprintf(stderr,"\ntranslate: %d %d %d fill=%d seq.l=%d\n",sbeg,rbeg,rend,fill,(int)_seq->l); |
| 1500 |
#endif |
| 1501 |
char tmp[3], *codon, *end; |
| 1502 |
int i, len, npad; |
| 1503 |
|
| 1504 |
kstring_t ref = *_ref; |
| 1505 |
kstring_t seq = *_seq; |
| 1506 |
|
| 1507 |
tseq->l = 0; |
| 1508 |
if ( !seq.l ) |
| 1509 |
{ |
| 1510 |
kputc('?', tseq); |
| 1511 |
return; |
| 1512 |
} |
| 1513 |
|
| 1514 |
#define DBG 0 |
| 1515 |
#if DBG |
| 1516 |
fprintf(stderr,"translate: sbeg,rbeg,rend=%d %d %d fill=%d seq.l=%d\n",sbeg,rbeg,rend,fill,(int)_seq->l); |
| 1517 |
fprintf(stderr," ref: l=%d %s\n", (int)ref.l,ref.s); |
| 1518 |
fprintf(stderr," seq: l=%d m=%d ", (int)seq.l,(int)seq.m); |
| 1519 |
for (i=0; i<seq.l; i++) fprintf(stderr,"%c",seq.s[i]); fprintf(stderr,"\n"); |
| 1520 |
fprintf(stderr," sbeg,rbeg,rend: %d,%d,%d\n", sbeg,rbeg,rend); |
| 1521 |
fprintf(stderr," strand,fill: %d,%d\n", strand,fill); |
| 1522 |
#endif |
| 1523 |
|
| 1524 |
if ( strand==STRAND_FWD ) |
| 1525 |
{ |
| 1526 |
|
| 1527 |
npad = sbeg % 3; |
| 1528 |
#if DBG>1 |
| 1529 |
fprintf(stderr," npad: %d\n",npad); |
| 1530 |
#endif |
| 1531 |
assert( npad<=rbeg ); |
| 1532 |
|
| 1533 |
for (i=0; i<npad; i++) |
| 1534 |
tmp[i] = ref.s[rbeg+i-npad+N_REF_PAD]; |
| 1535 |
for (; i<3 && i-npad<seq.l; i++) |
| 1536 |
tmp[i] = seq.s[i-npad]; |
| 1537 |
len = seq.l - i + npad; |
| 1538 |
#if DBG>1 |
| 1539 |
fprintf(stderr,"\t i=%d\n", i); |
| 1540 |
#endif |
| 1541 |
if ( i==3 ) |
| 1542 |
{ |
| 1543 |
kputc_(dna2aa(tmp), tseq); |
| 1544 |
#if DBG>1 |
| 1545 |
fprintf(stderr,"[1]%c%c%c\n",tmp[0],tmp[1],tmp[2]); |
| 1546 |
#endif |
| 1547 |
codon = seq.s + 3 - npad; |
| 1548 |
end = codon + len - 1 - (len % 3); |
| 1549 |
while ( codon < end ) |
| 1550 |
{ |
| 1551 |
kputc_(dna2aa(codon), tseq); |
| 1552 |
#if DBG>1 |
| 1553 |
fprintf(stderr,"[2]%c%c%c\n",codon[0],codon[1],codon[2]); |
| 1554 |
#endif |
| 1555 |
codon += 3; |
| 1556 |
} |
| 1557 |
end = seq.s + seq.l - 1; |
| 1558 |
for (i=0; codon+i<=end; i++) tmp[i] = codon[i]; |
| 1559 |
} |
| 1560 |
|
| 1561 |
|
| 1562 |
codon = ref.s + rend + N_REF_PAD; |
| 1563 |
if ( i>0 ) |
| 1564 |
{ |
| 1565 |
#if DBG>1 |
| 1566 |
if(i==1)fprintf(stderr,"[3]%c\n",tmp[0]); |
| 1567 |
if(i==2)fprintf(stderr,"[3]%c%c\n",tmp[0],tmp[1]); |
| 1568 |
#endif |
| 1569 |
for (; i<3; i++) |
| 1570 |
{ |
| 1571 |
tmp[i] = *codon; |
| 1572 |
codon++; |
| 1573 |
} |
| 1574 |
kputc_(dna2aa(tmp), tseq); |
| 1575 |
#if DBG>1 |
| 1576 |
fprintf(stderr,"[4]%c%c%c\n",tmp[0],tmp[1],tmp[2]); |
| 1577 |
#endif |
| 1578 |
} |
| 1579 |
if ( fill!=0 ) |
| 1580 |
{ |
| 1581 |
end = ref.s + ref.l - N_REF_PAD; |
| 1582 |
while ( codon+3 <= end ) |
| 1583 |
{ |
| 1584 |
kputc_(dna2aa(codon), tseq); |
| 1585 |
#if DBG>1 |
| 1586 |
fprintf(stderr,"[5]%c%c%c\t%c\n",codon[0],codon[1],codon[2],dna2aa(codon)); |
| 1587 |
#endif |
| 1588 |
codon += 3; |
| 1589 |
} |
| 1590 |
} |
| 1591 |
} |
| 1592 |
else if ( strand==STRAND_REV ) |
| 1593 |
{ |
| 1594 |
|
| 1595 |
npad = (seq.m - (sbeg + seq.l)) % 3; |
| 1596 |
#if DBG>1 |
| 1597 |
fprintf(stderr," npad: %d\n",npad); |
| 1598 |
#endif |
| 1599 |
if ( !(npad>=0 && sbeg+seq.l+npad<=seq.m) ) fprintf(stderr,"sbeg=%d seq.l=%d seq.m=%d npad=%d\n",sbeg,(int)seq.l,(int)seq.m,npad); |
| 1600 |
assert( npad>=0 && sbeg+seq.l+npad<=seq.m ); |
| 1601 |
|
| 1602 |
if ( npad==2 ) |
| 1603 |
{ |
| 1604 |
tmp[1] = ref.s[rend+N_REF_PAD]; |
| 1605 |
tmp[2] = ref.s[rend+N_REF_PAD+1]; |
| 1606 |
i = 0; |
| 1607 |
} |
| 1608 |
else if ( npad==1 ) |
| 1609 |
{ |
| 1610 |
tmp[2] = ref.s[rend+N_REF_PAD]; |
| 1611 |
i = 1; |
| 1612 |
} |
| 1613 |
else |
| 1614 |
i = 2; |
| 1615 |
|
| 1616 |
end = seq.s + seq.l; |
| 1617 |
for (; i>=0 && end>seq.s; i--) tmp[i] = *(--end); |
| 1618 |
#if DBG>1 |
| 1619 |
fprintf(stderr,"\t i=%d\n", i); |
| 1620 |
if(i==1)fprintf(stderr,"[0] %c\n",tmp[2]); |
| 1621 |
if(i==0)fprintf(stderr,"[0] %c%c\n",tmp[1],tmp[2]); |
| 1622 |
#endif |
| 1623 |
if ( i==-1 ) |
| 1624 |
{ |
| 1625 |
#if DBG>1 |
| 1626 |
fprintf(stderr,"[1]%c%c%c\t%c\n",tmp[0],tmp[1],tmp[2], cdna2aa(tmp)); |
| 1627 |
#endif |
| 1628 |
kputc_(cdna2aa(tmp), tseq); |
| 1629 |
codon = end - 3; |
| 1630 |
while ( codon >= seq.s ) |
| 1631 |
{ |
| 1632 |
kputc_(cdna2aa(codon), tseq); |
| 1633 |
#if DBG>1 |
| 1634 |
fprintf(stderr,"[2]%c%c%c\t%c\n",codon[0],codon[1],codon[2], cdna2aa(codon)); |
| 1635 |
#endif |
| 1636 |
codon -= 3; |
| 1637 |
} |
| 1638 |
if ( seq.s-codon==2 ) |
| 1639 |
{ |
| 1640 |
tmp[2] = seq.s[0]; |
| 1641 |
i = 1; |
| 1642 |
} |
| 1643 |
else if ( seq.s-codon==1 ) |
| 1644 |
{ |
| 1645 |
tmp[1] = seq.s[0]; |
| 1646 |
tmp[2] = seq.s[1]; |
| 1647 |
i = 0; |
| 1648 |
} |
| 1649 |
else |
| 1650 |
i = -1; |
| 1651 |
#if DBG>1 |
| 1652 |
if(i==1)fprintf(stderr,"[3] %c\n",tmp[2]); |
| 1653 |
if(i==0)fprintf(stderr,"[3] %c%c\n",tmp[1],tmp[2]); |
| 1654 |
#endif |
| 1655 |
} |
| 1656 |
|
| 1657 |
end = ref.s + N_REF_PAD + rbeg; |
| 1658 |
if ( i>=0 ) |
| 1659 |
{ |
| 1660 |
for (; i>=0 && end>=ref.s; i--) tmp[i] = *(--end); |
| 1661 |
kputc_(cdna2aa(tmp), tseq); |
| 1662 |
#if DBG>1 |
| 1663 |
fprintf(stderr,"[4]%c%c%c\t%c\n",tmp[0],tmp[1],tmp[2],cdna2aa(tmp)); |
| 1664 |
#endif |
| 1665 |
} |
| 1666 |
if ( fill!=0 ) |
| 1667 |
{ |
| 1668 |
codon = end - 3; |
| 1669 |
while ( codon >= ref.s + N_REF_PAD ) |
| 1670 |
{ |
| 1671 |
kputc_(cdna2aa(codon), tseq); |
| 1672 |
#if DBG>1 |
| 1673 |
fprintf(stderr,"[5]%c%c%c\t%c\n",codon[0],codon[1],codon[2],cdna2aa(codon)); |
| 1674 |
#endif |
| 1675 |
codon -= 3; |
| 1676 |
} |
| 1677 |
} |
| 1678 |
} |
| 1679 |
else error("Should not happen: %d\n", strand); |
| 1680 |
kputc_(0,tseq); tseq->l--; |
| 1681 |
#if DBG |
| 1682 |
fprintf(stderr," tseq: %s\n", tseq->s); |
| 1683 |
#endif |
| 1684 |
} |
| 1685 |
|
| 1686 |
void tscript_splice_ref(gf_tscript_t *tr) |
| 1687 |
{ |
| 1688 |
int i, len = 0; |
| 1689 |
for (i=0; i<tr->ncds; i++) |
| 1690 |
len += tr->cds[i]->len; |
| 1691 |
|
| 1692 |
TSCRIPT_AUX(tr)->nsref = len + 2*N_REF_PAD; |
| 1693 |
TSCRIPT_AUX(tr)->sref = (char*) malloc(len + 1 + 2*N_REF_PAD); |
| 1694 |
len = 0; |
| 1695 |
|
| 1696 |
memcpy(TSCRIPT_AUX(tr)->sref, TSCRIPT_AUX(tr)->ref + tr->cds[0]->beg - tr->beg, N_REF_PAD); |
| 1697 |
len += N_REF_PAD; |
| 1698 |
|
| 1699 |
for (i=0; i<tr->ncds; i++) |
| 1700 |
{ |
| 1701 |
memcpy(TSCRIPT_AUX(tr)->sref + len, TSCRIPT_AUX(tr)->ref + N_REF_PAD + tr->cds[i]->beg - tr->beg, tr->cds[i]->len); |
| 1702 |
len += tr->cds[i]->len; |
| 1703 |
} |
| 1704 |
memcpy(TSCRIPT_AUX(tr)->sref + len, TSCRIPT_AUX(tr)->ref + N_REF_PAD + tr->cds[tr->ncds-1]->beg - tr->beg, N_REF_PAD); |
| 1705 |
len += N_REF_PAD; |
| 1706 |
|
| 1707 |
TSCRIPT_AUX(tr)->sref[len] = 0; |
| 1708 |
} |
| 1709 |
|
| 1710 |
|
| 1711 |
int csq_push(args_t *args, csq_t *csq, bcf1_t *rec) |
| 1712 |
{ |
| 1713 |
#if XDBG |
| 1714 |
fprintf(stderr,"csq_push: %d .. %d\n",(int)rec->pos+1,csq->type.type); |
| 1715 |
#endif |
| 1716 |
khint_t k = kh_get(pos2vbuf, args->pos2vbuf, (int)csq->pos); |
| 1717 |
vbuf_t *vbuf = (k == kh_end(args->pos2vbuf)) ? NULL : kh_val(args->pos2vbuf, k); |
| 1718 |
if ( !vbuf ) error("This should not happen. %s:%d %s\n",bcf_seqname(args->hdr,rec),csq->pos+1,csq->type.vstr.s); |
| 1719 |
|
| 1720 |
int i; |
| 1721 |
for (i=0; i<vbuf->n; i++) |
| 1722 |
if ( vbuf->vrec[i]->line==rec ) break; |
| 1723 |
if ( i==vbuf->n ) error("This should not happen.. %s:%d %s\n", bcf_seqname(args->hdr,rec),csq->pos+1,csq->type.vstr.s); |
| 1724 |
vrec_t *vrec = vbuf->vrec[i]; |
| 1725 |
|
| 1726 |
|
| 1727 |
if ( csq->type.type & CSQ_SPLICE_REGION && csq->type.type & (CSQ_SPLICE_DONOR|CSQ_SPLICE_ACCEPTOR) ) |
| 1728 |
csq->type.type &= ~CSQ_SPLICE_REGION; |
| 1729 |
|
| 1730 |
if ( csq->type.type & CSQ_PRINTED_UPSTREAM ) |
| 1731 |
{ |
| 1732 |
for (i=0; i<vrec->nvcsq; i++) |
| 1733 |
{ |
| 1734 |
|
| 1735 |
|
| 1736 |
|
| 1737 |
if ( csq->type.type&CSQ_START_STOP && vrec->vcsq[i].type&CSQ_START_STOP ) |
| 1738 |
{ |
| 1739 |
vrec->vcsq[i] = csq->type; |
| 1740 |
goto exit_duplicate; |
| 1741 |
} |
| 1742 |
if ( !(vrec->vcsq[i].type & CSQ_PRINTED_UPSTREAM) ) continue; |
| 1743 |
if ( csq->type.ref != vrec->vcsq[i].ref ) continue; |
| 1744 |
goto exit_duplicate; |
| 1745 |
} |
| 1746 |
} |
| 1747 |
else if ( csq->type.type & CSQ_COMPOUND ) |
| 1748 |
{ |
| 1749 |
for (i=0; i<vrec->nvcsq; i++) |
| 1750 |
{ |
| 1751 |
if ( csq->type.trid != vrec->vcsq[i].trid && (csq->type.type|vrec->vcsq[i].type)&CSQ_PRN_TSCRIPT ) continue; |
| 1752 |
if ( csq->type.biotype != vrec->vcsq[i].biotype ) continue; |
| 1753 |
if ( csq->type.gene != vrec->vcsq[i].gene ) continue; |
| 1754 |
if ( csq->type.vcf_ial != vrec->vcsq[i].vcf_ial ) continue; |
| 1755 |
if ( (csq->type.type&CSQ_UPSTREAM_STOP)^(vrec->vcsq[i].type&CSQ_UPSTREAM_STOP) ) continue; |
| 1756 |
if ( csq->type.vstr.s || vrec->vcsq[i].vstr.s ) |
| 1757 |
{ |
| 1758 |
|
| 1759 |
|
| 1760 |
|
| 1761 |
|
| 1762 |
|
| 1763 |
|
| 1764 |
if ( !csq->type.vstr.s || !vrec->vcsq[i].vstr.s ) |
| 1765 |
{ |
| 1766 |
if ( csq->type.type&CSQ_START_STOP && vrec->vcsq[i].type&CSQ_START_STOP ) |
| 1767 |
{ |
| 1768 |
vrec->vcsq[i].type |= csq->type.type; |
| 1769 |
|
| 1770 |
|
| 1771 |
if ( vrec->vcsq[i].type&CSQ_STOP_RETAINED ) |
| 1772 |
vrec->vcsq[i].type &= ~(CSQ_STOP_LOST|CSQ_SYNONYMOUS_VARIANT); |
| 1773 |
|
| 1774 |
if ( !vrec->vcsq[i].vstr.s ) vrec->vcsq[i].vstr = csq->type.vstr; |
| 1775 |
goto exit_duplicate; |
| 1776 |
} |
| 1777 |
continue; |
| 1778 |
} |
| 1779 |
if ( strcmp(csq->type.vstr.s,vrec->vcsq[i].vstr.s) ) continue; |
| 1780 |
} |
| 1781 |
vrec->vcsq[i].type |= csq->type.type; |
| 1782 |
goto exit_duplicate; |
| 1783 |
} |
| 1784 |
} |
| 1785 |
else |
| 1786 |
{ |
| 1787 |
for (i=0; i<vrec->nvcsq; i++) |
| 1788 |
{ |
| 1789 |
if ( csq->type.trid != vrec->vcsq[i].trid && (csq->type.type|vrec->vcsq[i].type)&CSQ_PRN_TSCRIPT) continue; |
| 1790 |
if ( csq->type.biotype != vrec->vcsq[i].biotype ) continue; |
| 1791 |
if ( !(vrec->vcsq[i].type & CSQ_COMPOUND) ) |
| 1792 |
{ |
| 1793 |
vrec->vcsq[i].type |= csq->type.type; |
| 1794 |
goto exit_duplicate; |
| 1795 |
} |
| 1796 |
if ( vrec->vcsq[i].type==(vrec->vcsq[i].type|csq->type.type) ) goto exit_duplicate; |
| 1797 |
} |
| 1798 |
} |
| 1799 |
|
| 1800 |
csq->vrec = vrec; |
| 1801 |
csq->idx = vrec->nvcsq++; |
| 1802 |
hts_expand0(vcsq_t, vrec->nvcsq, vrec->mvcsq, vrec->vcsq); |
| 1803 |
vrec->vcsq[i] = csq->type; |
| 1804 |
return 0; |
| 1805 |
|
| 1806 |
exit_duplicate: |
| 1807 |
csq->vrec = vrec; |
| 1808 |
csq->idx = i; |
| 1809 |
return 1; |
| 1810 |
} |
| 1811 |
|
| 1812 |
|
| 1813 |
|
| 1814 |
|
| 1815 |
|
| 1816 |
#define node2soff(i) (hap->stack[i].slen - (hap->stack[i].node->rlen + hap->stack[i].node->dlen)) |
| 1817 |
#define node2sbeg(i) (hap->sbeg + node2soff(i)) |
| 1818 |
#define node2send(i) (hap->sbeg + hap->stack[i].slen) |
| 1819 |
#define node2rbeg(i) (hap->stack[i].node->sbeg) |
| 1820 |
#define node2rend(i) (hap->stack[i].node->sbeg + hap->stack[i].node->rlen) |
| 1821 |
#define node2rpos(i) (hap->stack[i].node->rec->pos) |
| 1822 |
|
| 1823 |
void kput_vcsq(args_t *args, vcsq_t *csq, kstring_t *str) |
| 1824 |
{ |
| 1825 |
|
| 1826 |
|
| 1827 |
if ( csq->type & CSQ_INCOMPLETE_CDS && (csq->type & ~(CSQ_START_STOP|CSQ_INCOMPLETE_CDS|CSQ_UPSTREAM_STOP)) ) csq->type &= ~(CSQ_START_STOP|CSQ_INCOMPLETE_CDS); |
| 1828 |
|
| 1829 |
|
| 1830 |
if ( csq->type & CSQ_START_STOP && csq->type & CSQ_MISSENSE_VARIANT ) csq->type &= ~CSQ_MISSENSE_VARIANT; |
| 1831 |
|
| 1832 |
if ( csq->type & CSQ_PRINTED_UPSTREAM && csq->ref ) |
| 1833 |
{ |
| 1834 |
kputc_('@',str); |
| 1835 |
kputw(csq->ref->pos+1, str); |
| 1836 |
return; |
| 1837 |
} |
| 1838 |
if ( csq->type & CSQ_UPSTREAM_STOP ) |
| 1839 |
kputc_('*',str); |
| 1840 |
|
| 1841 |
int has_csq = 0, i, n = sizeof(csq_strings)/sizeof(char*); |
| 1842 |
for (i=1; i<n; i++) |
| 1843 |
if ( csq_strings[i] && csq->type&(1<<i) ) { has_csq = 1; kputs(csq_strings[i],str); break; } |
| 1844 |
i++; |
| 1845 |
for (; i<n; i++) |
| 1846 |
if ( csq_strings[i] && csq->type&(1<<i) ) { has_csq = 1; kputc_('&',str); kputs(csq_strings[i],str); } |
| 1847 |
|
| 1848 |
if ( (csq->biotype==GF_NMD) && (csq->type & CSQ_PRN_NMD) ) |
| 1849 |
{ |
| 1850 |
if ( has_csq ) kputc_('&',str); |
| 1851 |
kputs("NMD_transcript",str); |
| 1852 |
} |
| 1853 |
|
| 1854 |
kputc_('|', str); |
| 1855 |
if ( csq->gene ) kputs(csq->gene , str); |
| 1856 |
|
| 1857 |
kputc_('|', str); |
| 1858 |
|
| 1859 |
if ( csq->type & CSQ_PRN_TSCRIPT ) kputs(gff_id2string(args->gff,transcript,csq->trid), str); |
| 1860 |
|
| 1861 |
kputc_('|', str); |
| 1862 |
kputs(gf_type2gff_string(csq->biotype), str); |
| 1863 |
|
| 1864 |
if ( CSQ_PRN_STRAND(csq->type) || csq->vstr.l ) |
| 1865 |
kputs(csq->strand==STRAND_FWD ? "|+" : (csq->strand==STRAND_REV ? "|-" : "|."), str); |
| 1866 |
|
| 1867 |
if ( csq->vstr.l ) |
| 1868 |
kputs(csq->vstr.s, str); |
| 1869 |
} |
| 1870 |
|
| 1871 |
void kprint_aa_prediction(args_t *args, int beg, kstring_t *aa, kstring_t *str) |
| 1872 |
{ |
| 1873 |
if ( !args->brief_predictions || (int)aa->l - args->brief_predictions < 3 ) |
| 1874 |
kputs(aa->s, str); |
| 1875 |
else |
| 1876 |
{ |
| 1877 |
int i, len = aa->l; |
| 1878 |
if ( aa->s[len-1]=='*' ) len--; |
| 1879 |
for (i=0; i<len && i<args->brief_predictions; i++) kputc(aa->s[i], str); |
| 1880 |
kputs("..", str); |
| 1881 |
kputw(beg+len, str); |
| 1882 |
} |
| 1883 |
} |
| 1884 |
|
| 1885 |
void hap_add_csq(args_t *args, hap_t *hap, hap_node_t *node, int tlen, int ibeg, int iend, int dlen, int indel) |
| 1886 |
{ |
| 1887 |
int i; |
| 1888 |
gf_tscript_t *tr = hap->tr; |
| 1889 |
assert( tr->strand==STRAND_FWD || tr->strand==STRAND_REV ); |
| 1890 |
int ref_node = tr->strand==STRAND_FWD ? ibeg : iend; |
| 1891 |
int icsq = node->ncsq_list++; |
| 1892 |
hts_expand0(csq_t,node->ncsq_list,node->mcsq_list,node->csq_list); |
| 1893 |
csq_t *csq = &node->csq_list[icsq]; |
| 1894 |
csq->pos = hap->stack[ref_node].node->rec->pos; |
| 1895 |
csq->type.trid = tr->id; |
| 1896 |
csq->type.vcf_ial = node->vcf_ial; |
| 1897 |
csq->type.gene = tr->gene->name; |
| 1898 |
csq->type.strand = tr->strand; |
| 1899 |
csq->type.biotype = tr->type; |
| 1900 |
|
| 1901 |
|
| 1902 |
int rm_csq = 0; |
| 1903 |
csq->type.type = 0; |
| 1904 |
for (i=ibeg; i<=iend; i++) |
| 1905 |
csq->type.type |= hap->stack[i].node->csq & CSQ_COMPOUND; |
| 1906 |
if ( dlen==0 && indel ) csq->type.type |= CSQ_INFRAME_ALTERING; |
| 1907 |
|
| 1908 |
int has_upstream_stop = hap->upstream_stop; |
| 1909 |
if ( hap->stack[ibeg].node->type != HAP_SSS ) |
| 1910 |
{ |
| 1911 |
|
| 1912 |
for (i=0; i<hap->tref.l; i++) |
| 1913 |
if ( hap->tref.s[i]=='*' ) break; |
| 1914 |
if ( i!=hap->tref.l ) |
| 1915 |
{ |
| 1916 |
hap->tref.l = i+1; |
| 1917 |
hap->tref.s[i+1] = 0; |
| 1918 |
} |
| 1919 |
for (i=0; i<hap->tseq.l; i++) |
| 1920 |
if ( hap->tseq.s[i]=='*' ) break; |
| 1921 |
if ( i!=hap->tseq.l ) |
| 1922 |
{ |
| 1923 |
hap->tseq.l = i+1; |
| 1924 |
hap->tseq.s[i+1] = 0; |
| 1925 |
hap->upstream_stop = 1; |
| 1926 |
} |
| 1927 |
if ( csq->type.type & CSQ_STOP_LOST ) |
| 1928 |
{ |
| 1929 |
if ( hap->tref.s[hap->tref.l-1]=='*' && hap->tref.s[hap->tref.l-1] == hap->tseq.s[hap->tseq.l-1] ) |
| 1930 |
{ |
| 1931 |
rm_csq |= CSQ_STOP_LOST; |
| 1932 |
csq->type.type |= CSQ_STOP_RETAINED; |
| 1933 |
} |
| 1934 |
else if ( hap->tref.s[hap->tref.l-1]!='*' ) |
| 1935 |
{ |
| 1936 |
|
| 1937 |
|
| 1938 |
if ( hap->tseq.s[hap->tseq.l-1] == '*' ) |
| 1939 |
{ |
| 1940 |
rm_csq |= CSQ_STOP_GAINED; |
| 1941 |
csq->type.type |= CSQ_STOP_RETAINED; |
| 1942 |
} |
| 1943 |
else |
| 1944 |
csq->type.type |= CSQ_INCOMPLETE_CDS; |
| 1945 |
} |
| 1946 |
} |
| 1947 |
if ( csq->type.type & CSQ_START_LOST && hap->tref.s[0]!='M' ) |
| 1948 |
{ |
| 1949 |
rm_csq |= CSQ_START_LOST; |
| 1950 |
csq->type.type &= ~CSQ_START_LOST; |
| 1951 |
} |
| 1952 |
if ( dlen!=0 ) |
| 1953 |
{ |
| 1954 |
if ( dlen%3 ) |
| 1955 |
csq->type.type |= CSQ_FRAMESHIFT_VARIANT; |
| 1956 |
else if ( dlen<0 ) |
| 1957 |
csq->type.type |= CSQ_INFRAME_DELETION; |
| 1958 |
else |
| 1959 |
csq->type.type |= CSQ_INFRAME_INSERTION; |
| 1960 |
if ( hap->tref.s[hap->tref.l-1]!='*' && hap->tseq.s[hap->tseq.l-1]=='*' ) |
| 1961 |
csq->type.type |= CSQ_STOP_GAINED; |
| 1962 |
} |
| 1963 |
else |
| 1964 |
{ |
| 1965 |
int aa_change = 0; |
| 1966 |
for (i=0; i<hap->tref.l; i++) |
| 1967 |
{ |
| 1968 |
if ( hap->tref.s[i] == hap->tseq.s[i] ) continue; |
| 1969 |
aa_change = 1; |
| 1970 |
if ( hap->tref.s[i] == '*' ) |
| 1971 |
csq->type.type |= CSQ_STOP_LOST; |
| 1972 |
else if ( hap->tseq.s[i] == '*' ) |
| 1973 |
csq->type.type |= CSQ_STOP_GAINED; |
| 1974 |
else |
| 1975 |
csq->type.type |= CSQ_MISSENSE_VARIANT; |
| 1976 |
} |
| 1977 |
if ( !aa_change ) |
| 1978 |
csq->type.type |= CSQ_SYNONYMOUS_VARIANT; |
| 1979 |
} |
| 1980 |
} |
| 1981 |
|
| 1982 |
if ( ibeg!=iend && (csq->type.type & (CSQ_INFRAME_DELETION|CSQ_INFRAME_INSERTION|CSQ_INFRAME_ALTERING)) && hap->tseq.s[hap->tseq.l-1]=='*' ) |
| 1983 |
{ |
| 1984 |
rm_csq |= CSQ_INFRAME_DELETION | CSQ_INFRAME_INSERTION | CSQ_INFRAME_ALTERING; |
| 1985 |
csq->type.type |= CSQ_FRAMESHIFT_VARIANT | CSQ_STOP_GAINED; |
| 1986 |
} |
| 1987 |
if ( has_upstream_stop ) csq->type.type |= CSQ_UPSTREAM_STOP; |
| 1988 |
csq->type.type &= ~rm_csq; |
| 1989 |
|
| 1990 |
if ( hap->stack[ibeg].node->type == HAP_SSS ) |
| 1991 |
{ |
| 1992 |
node->csq_list[icsq].type.type |= hap->stack[ibeg].node->csq & ~rm_csq; |
| 1993 |
node->csq_list[icsq].type.ref = hap->stack[ibeg].node->rec; |
| 1994 |
node->csq_list[icsq].type.biotype = tr->type; |
| 1995 |
csq_push(args, node->csq_list+icsq, hap->stack[ibeg].node->rec); |
| 1996 |
return; |
| 1997 |
} |
| 1998 |
|
| 1999 |
kstring_t str = node->csq_list[icsq].type.vstr; |
| 2000 |
str.l = 0; |
| 2001 |
|
| 2002 |
|
| 2003 |
int aa_rbeg = tr->strand==STRAND_FWD ? node2rbeg(ibeg)/3+1 : (TSCRIPT_AUX(hap->tr)->nsref - 2*N_REF_PAD - node2rend(iend))/3+1; |
| 2004 |
int aa_sbeg = tr->strand==STRAND_FWD ? node2sbeg(ibeg)/3+1 : (tlen - node2send(iend))/3+1; |
| 2005 |
kputc_('|', &str); |
| 2006 |
kputw(aa_rbeg, &str); |
| 2007 |
kprint_aa_prediction(args,aa_rbeg,&hap->tref,&str); |
| 2008 |
if ( !(csq->type.type & CSQ_SYNONYMOUS_VARIANT) ) |
| 2009 |
{ |
| 2010 |
kputc_('>', &str); |
| 2011 |
kputw(aa_sbeg, &str); |
| 2012 |
kprint_aa_prediction(args,aa_sbeg,&hap->tseq,&str); |
| 2013 |
} |
| 2014 |
kputc_('|', &str); |
| 2015 |
|
| 2016 |
|
| 2017 |
|
| 2018 |
for (i=ibeg; i<=iend; i++) |
| 2019 |
{ |
| 2020 |
if ( i>ibeg ) kputc_('+', &str); |
| 2021 |
kputw(node2rpos(i)+1, &str); |
| 2022 |
kputs(hap->stack[i].node->var, &str); |
| 2023 |
} |
| 2024 |
node->csq_list[icsq].type.vstr = str; |
| 2025 |
csq_push(args, node->csq_list+icsq, hap->stack[ref_node].node->rec); |
| 2026 |
|
| 2027 |
for (i=ibeg; i<=iend; i++) |
| 2028 |
{ |
| 2029 |
|
| 2030 |
|
| 2031 |
if ( hap->stack[i].node->csq & ~CSQ_COMPOUND ) |
| 2032 |
{ |
| 2033 |
node->ncsq_list++; |
| 2034 |
hts_expand0(csq_t,node->ncsq_list,node->mcsq_list,node->csq_list); |
| 2035 |
csq_t *tmp_csq = &node->csq_list[node->ncsq_list - 1]; |
| 2036 |
tmp_csq->pos = hap->stack[i].node->rec->pos; |
| 2037 |
tmp_csq->type.trid = tr->id; |
| 2038 |
|
| 2039 |
tmp_csq->type.gene = tr->gene->name; |
| 2040 |
tmp_csq->type.strand = tr->strand; |
| 2041 |
tmp_csq->type.type = hap->stack[i].node->csq & ~CSQ_COMPOUND & ~rm_csq; |
| 2042 |
tmp_csq->type.biotype = tr->type; |
| 2043 |
tmp_csq->type.vstr.l = 0; |
| 2044 |
kputs(str.s,&tmp_csq->type.vstr); |
| 2045 |
csq_push(args, tmp_csq, hap->stack[i].node->rec); |
| 2046 |
} |
| 2047 |
if ( i!=ref_node && (node->csq_list[icsq].type.type & CSQ_COMPOUND || !(hap->stack[i].node->csq & ~CSQ_COMPOUND)) ) |
| 2048 |
{ |
| 2049 |
node->ncsq_list++; |
| 2050 |
hts_expand0(csq_t,node->ncsq_list,node->mcsq_list,node->csq_list); |
| 2051 |
csq_t *tmp_csq = &node->csq_list[node->ncsq_list - 1]; |
| 2052 |
tmp_csq->pos = hap->stack[i].node->rec->pos; |
| 2053 |
tmp_csq->type.trid = tr->id; |
| 2054 |
|
| 2055 |
tmp_csq->type.gene = tr->gene->name; |
| 2056 |
tmp_csq->type.strand = tr->strand; |
| 2057 |
tmp_csq->type.type = CSQ_PRINTED_UPSTREAM | hap->stack[i].node->csq; |
| 2058 |
tmp_csq->type.biotype = tr->type; |
| 2059 |
tmp_csq->type.ref = hap->stack[ref_node].node->rec; |
| 2060 |
tmp_csq->type.vstr.l = 0; |
| 2061 |
csq_push(args, tmp_csq, hap->stack[i].node->rec); |
| 2062 |
} |
| 2063 |
} |
| 2064 |
} |
| 2065 |
|
| 2066 |
|
| 2067 |
void hap_finalize(args_t *args, hap_t *hap) |
| 2068 |
{ |
| 2069 |
gf_tscript_t *tr = hap->tr; |
| 2070 |
if ( !TSCRIPT_AUX(tr)->sref ) |
| 2071 |
tscript_splice_ref(tr); |
| 2072 |
|
| 2073 |
kstring_t sref; |
| 2074 |
sref.s = TSCRIPT_AUX(tr)->sref; |
| 2075 |
sref.l = TSCRIPT_AUX(tr)->nsref; |
| 2076 |
sref.m = sref.l; |
| 2077 |
|
| 2078 |
int istack = 0; |
| 2079 |
hts_expand(hstack_t,1,hap->mstack,hap->stack); |
| 2080 |
|
| 2081 |
hap->sseq.l = 0; |
| 2082 |
hap->tseq.l = 0; |
| 2083 |
hap->stack[0].node = TSCRIPT_AUX(tr)->root; |
| 2084 |
hap->stack[0].ichild = -1; |
| 2085 |
hap->stack[0].slen = 0; |
| 2086 |
hap->stack[0].dlen = 0; |
| 2087 |
|
| 2088 |
while ( istack>=0 ) |
| 2089 |
{ |
| 2090 |
hstack_t *stack = &hap->stack[istack]; |
| 2091 |
hap_node_t *node = hap->stack[istack].node; |
| 2092 |
while ( ++hap->stack[istack].ichild < node->nchild ) |
| 2093 |
{ |
| 2094 |
if ( node->child[stack->ichild] ) break; |
| 2095 |
} |
| 2096 |
if ( stack->ichild == node->nchild ) { istack--; continue; } |
| 2097 |
|
| 2098 |
node = node->child[stack->ichild]; |
| 2099 |
|
| 2100 |
istack++; |
| 2101 |
hts_expand(hstack_t,istack+1,hap->mstack,hap->stack); |
| 2102 |
stack = &hap->stack[istack-1]; |
| 2103 |
|
| 2104 |
hap->stack[istack].node = node; |
| 2105 |
hap->stack[istack].ichild = -1; |
| 2106 |
|
| 2107 |
hap->sseq.l = stack->slen; |
| 2108 |
if ( node->type==HAP_CDS ) kputs(node->seq, &hap->sseq); |
| 2109 |
hap->stack[istack].slen = hap->sseq.l; |
| 2110 |
hap->stack[istack].dlen = hap->stack[istack-1].dlen + node->dlen; |
| 2111 |
|
| 2112 |
if ( !node->nend ) continue; |
| 2113 |
|
| 2114 |
|
| 2115 |
|
| 2116 |
|
| 2117 |
kstring_t sseq; |
| 2118 |
sseq.m = sref.m - 2*N_REF_PAD + hap->stack[istack].dlen; |
| 2119 |
hap->upstream_stop = 0; |
| 2120 |
|
| 2121 |
int i = 1, dlen = 0, ibeg, indel = 0; |
| 2122 |
hap->sbeg = hap->stack[i].node->sbeg; |
| 2123 |
assert( hap->stack[istack].node->type != HAP_SSS ); |
| 2124 |
if ( tr->strand==STRAND_FWD ) |
| 2125 |
{ |
| 2126 |
i = 0, ibeg = -1; |
| 2127 |
while ( ++i <= istack ) |
| 2128 |
{ |
| 2129 |
assert( hap->stack[i].node->type != HAP_SSS ); |
| 2130 |
|
| 2131 |
dlen += hap->stack[i].node->dlen; |
| 2132 |
if ( hap->stack[i].node->dlen ) indel = 1; |
| 2133 |
|
| 2134 |
|
| 2135 |
if ( i<istack ) |
| 2136 |
{ |
| 2137 |
if ( dlen%3 ) |
| 2138 |
{ |
| 2139 |
if ( ibeg==-1 ) ibeg = i; |
| 2140 |
continue; |
| 2141 |
} |
| 2142 |
|
| 2143 |
|
| 2144 |
int icur = node2sbeg(i); |
| 2145 |
int inext = node2sbeg(i+1); |
| 2146 |
if ( hap->stack[i].node->dlen > 0 ) icur += hap->stack[i].node->dlen; |
| 2147 |
else if ( hap->stack[i].node->dlen < 0 ) icur++; |
| 2148 |
if ( icur/3 == inext/3 ) |
| 2149 |
{ |
| 2150 |
if ( ibeg==-1 ) ibeg = i; |
| 2151 |
continue; |
| 2152 |
} |
| 2153 |
} |
| 2154 |
if ( ibeg<0 ) ibeg = i; |
| 2155 |
|
| 2156 |
int ioff = node2soff(ibeg); |
| 2157 |
int icur = node2sbeg(ibeg); |
| 2158 |
int rbeg = node2rbeg(ibeg); |
| 2159 |
int rend = node2rend(i); |
| 2160 |
int fill = dlen%3; |
| 2161 |
|
| 2162 |
|
| 2163 |
if ( hap->sseq.l ) |
| 2164 |
{ |
| 2165 |
sseq.l = hap->stack[i].slen - ioff; |
| 2166 |
sseq.s = hap->sseq.s + ioff; |
| 2167 |
} |
| 2168 |
else |
| 2169 |
sseq.l = fill = 0; |
| 2170 |
cds_translate(&sref, &sseq, icur,rbeg,rend, tr->strand, &hap->tseq, fill); |
| 2171 |
|
| 2172 |
|
| 2173 |
sseq.l = node2rend(i) - rbeg; |
| 2174 |
sseq.s = sref.s + N_REF_PAD + rbeg; |
| 2175 |
sseq.m = sref.m - 2*N_REF_PAD; |
| 2176 |
cds_translate(&sref, &sseq, rbeg,rbeg,rend, tr->strand, &hap->tref, fill); |
| 2177 |
sseq.m = sref.m - 2*N_REF_PAD + hap->stack[istack].dlen; |
| 2178 |
|
| 2179 |
hap_add_csq(args,hap,node,0, ibeg,i,dlen,indel); |
| 2180 |
ibeg = -1; |
| 2181 |
dlen = 0; |
| 2182 |
indel = 0; |
| 2183 |
} |
| 2184 |
} |
| 2185 |
else if ( tr->strand==STRAND_REV ) |
| 2186 |
{ |
| 2187 |
i = istack + 1, ibeg = -1; |
| 2188 |
while ( --i > 0 ) |
| 2189 |
{ |
| 2190 |
assert ( hap->stack[i].node->type != HAP_SSS ); |
| 2191 |
dlen += hap->stack[i].node->dlen; |
| 2192 |
if ( hap->stack[i].node->dlen ) indel = 1; |
| 2193 |
if ( i>1 ) |
| 2194 |
{ |
| 2195 |
if ( dlen%3 ) |
| 2196 |
{ |
| 2197 |
if ( ibeg==-1 ) ibeg = i; |
| 2198 |
continue; |
| 2199 |
} |
| 2200 |
|
| 2201 |
|
| 2202 |
int icur = sseq.m - 1 - node2sbeg(i); |
| 2203 |
int inext = sseq.m - 1 - node2sbeg(i-1); |
| 2204 |
if ( hap->stack[i].node->dlen > 0 ) icur += hap->stack[i].node->dlen - 1; |
| 2205 |
else if ( hap->stack[i].node->dlen < 0 ) icur -= hap->stack[i].node->dlen; |
| 2206 |
if ( hap->stack[i-1].node->dlen > 0 ) inext -= hap->stack[i-1].node->dlen; |
| 2207 |
if ( icur/3 == inext/3 ) |
| 2208 |
{ |
| 2209 |
if ( ibeg==-1 ) ibeg = i; |
| 2210 |
continue; |
| 2211 |
} |
| 2212 |
} |
| 2213 |
if ( ibeg<0 ) ibeg = i; |
| 2214 |
int ioff = node2soff(i); |
| 2215 |
int icur = node2sbeg(i); |
| 2216 |
int rbeg = node2rbeg(i); |
| 2217 |
int rend = node2rend(ibeg); |
| 2218 |
int fill = dlen%3; |
| 2219 |
|
| 2220 |
|
| 2221 |
if ( hap->sseq.l ) |
| 2222 |
{ |
| 2223 |
sseq.l = hap->stack[ibeg].slen - ioff; |
| 2224 |
sseq.s = hap->sseq.s + ioff; |
| 2225 |
} |
| 2226 |
else |
| 2227 |
sseq.l = fill = 0; |
| 2228 |
cds_translate(&sref, &sseq, icur,rbeg,rend, tr->strand, &hap->tseq, fill); |
| 2229 |
|
| 2230 |
|
| 2231 |
sseq.l = node2rend(ibeg) - rbeg; |
| 2232 |
sseq.s = sref.s + N_REF_PAD + rbeg; |
| 2233 |
sseq.m = sref.m - 2*N_REF_PAD; |
| 2234 |
cds_translate(&sref, &sseq, rbeg,rbeg,rend, tr->strand, &hap->tref, fill); |
| 2235 |
sseq.m = sref.m - 2*N_REF_PAD + hap->stack[istack].dlen; |
| 2236 |
|
| 2237 |
hap_add_csq(args,hap,node,sseq.m, i,ibeg,dlen,indel); |
| 2238 |
ibeg = -1; |
| 2239 |
dlen = 0; |
| 2240 |
indel = 0; |
| 2241 |
} |
| 2242 |
} |
| 2243 |
} |
| 2244 |
} |
| 2245 |
|
| 2246 |
static inline void csq_print_text(args_t *args, csq_t *csq, int ismpl, int ihap) |
| 2247 |
{ |
| 2248 |
if ( csq->type.type & CSQ_PRINTED_UPSTREAM ) return; |
| 2249 |
|
| 2250 |
char *smpl = ismpl >= 0 ? args->hdr->samples[ismpl] : "-"; |
| 2251 |
const char *chr = bcf_hdr_id2name(args->hdr,args->rid); |
| 2252 |
|
| 2253 |
fprintf(args->out,"CSQ\t%s\t", smpl); |
| 2254 |
if ( ihap>0 ) |
| 2255 |
fprintf(args->out,"%d", ihap); |
| 2256 |
else |
| 2257 |
fprintf(args->out,"-"); |
| 2258 |
|
| 2259 |
args->str.l = 0; |
| 2260 |
kput_vcsq(args, &csq->type, &args->str); |
| 2261 |
fprintf(args->out,"\t%s\t%d\t%s\n",chr,csq->pos+1,args->str.s); |
| 2262 |
} |
| 2263 |
static inline void hap_print_text(args_t *args, gf_tscript_t *tr, int ismpl, int ihap, hap_node_t *node) |
| 2264 |
{ |
| 2265 |
if ( !node || !node->ncsq_list ) return; |
| 2266 |
|
| 2267 |
char *smpl = ismpl >= 0 ? args->hdr->samples[ismpl] : "-"; |
| 2268 |
const char *chr = bcf_hdr_id2name(args->hdr,args->rid); |
| 2269 |
|
| 2270 |
int i; |
| 2271 |
for (i=0; i<node->ncsq_list; i++) |
| 2272 |
{ |
| 2273 |
csq_t *csq = node->csq_list + i; |
| 2274 |
if ( csq->type.type & CSQ_PRINTED_UPSTREAM ) continue; |
| 2275 |
assert( csq->type.vstr.l ); |
| 2276 |
|
| 2277 |
fprintf(args->out,"CSQ\t%s\t", smpl); |
| 2278 |
if ( ihap>0 ) |
| 2279 |
fprintf(args->out,"%d", ihap); |
| 2280 |
else |
| 2281 |
fprintf(args->out,"-"); |
| 2282 |
|
| 2283 |
args->str.l = 0; |
| 2284 |
kput_vcsq(args, &csq->type, &args->str); |
| 2285 |
fprintf(args->out,"\t%s\t%d\t%s\n",chr,csq->pos+1,args->str.s); |
| 2286 |
} |
| 2287 |
} |
| 2288 |
|
| 2289 |
static inline void hap_stage_vcf(args_t *args, gf_tscript_t *tr, int ismpl, int ihap, hap_node_t *node) |
| 2290 |
{ |
| 2291 |
if ( !node || !node->ncsq_list || ismpl<0 ) return; |
| 2292 |
|
| 2293 |
int i; |
| 2294 |
for (i=0; i<node->ncsq_list; i++) |
| 2295 |
{ |
| 2296 |
csq_t *csq = node->csq_list + i; |
| 2297 |
vrec_t *vrec = csq->vrec; |
| 2298 |
int icsq2 = 2*csq->idx + ihap; |
| 2299 |
if ( icsq2 >= args->ncsq2_max ) |
| 2300 |
{ |
| 2301 |
if ( args->verbosity && (!args->ncsq2_small_warned || args->verbosity > 1) ) |
| 2302 |
{ |
| 2303 |
fprintf(stderr, |
| 2304 |
"Warning: Too many consequences for sample %s at %s:%"PRId64", keeping the first %d and skipping the rest.\n", |
| 2305 |
args->hdr->samples[ismpl],bcf_hdr_id2name(args->hdr,args->rid),(int64_t) vrec->line->pos+1,csq->idx); |
| 2306 |
if ( !args->ncsq2_small_warned ) |
| 2307 |
fprintf(stderr," The limit can be increased by setting the --ncsq parameter. This warning is printed only once.\n"); |
| 2308 |
} |
| 2309 |
if ( args->ncsq2_small_warned < icsq2 ) args->ncsq2_small_warned = icsq2; |
| 2310 |
break; |
| 2311 |
} |
| 2312 |
int ival, ibit; |
| 2313 |
icsq2_to_bit(icsq2, &ival,&ibit); |
| 2314 |
if ( vrec->nfmt < 1 + ival ) vrec->nfmt = 1 + ival; |
| 2315 |
vrec->fmt_bm[ismpl*args->nfmt_bcsq + ival] |= 1 << ibit; |
| 2316 |
} |
| 2317 |
} |
| 2318 |
|
| 2319 |
void hap_flush(args_t *args, uint32_t pos) |
| 2320 |
{ |
| 2321 |
int i,j; |
| 2322 |
tr_heap_t *heap = args->active_tr; |
| 2323 |
while ( heap->ndat && heap->dat[0]->end<=pos ) |
| 2324 |
{ |
| 2325 |
gf_tscript_t *tr = heap->dat[0]; |
| 2326 |
khp_delete(trhp, heap); |
| 2327 |
args->hap->tr = tr; |
| 2328 |
if ( TSCRIPT_AUX(tr)->root && TSCRIPT_AUX(tr)->root->nchild ) |
| 2329 |
{ |
| 2330 |
hap_finalize(args, args->hap); |
| 2331 |
|
| 2332 |
if ( args->output_type==FT_TAB_TEXT ) |
| 2333 |
{ |
| 2334 |
if ( args->phase==PHASE_DROP_GT ) |
| 2335 |
hap_print_text(args, tr, -1,0, TSCRIPT_AUX(tr)->hap[0]); |
| 2336 |
else |
| 2337 |
{ |
| 2338 |
for (i=0; i<args->smpl->n; i++) |
| 2339 |
{ |
| 2340 |
for (j=0; j<2; j++) |
| 2341 |
hap_print_text(args, tr, args->smpl->idx[i],j+1, TSCRIPT_AUX(tr)->hap[i*2+j]); |
| 2342 |
} |
| 2343 |
} |
| 2344 |
} |
| 2345 |
else if ( args->phase!=PHASE_DROP_GT ) |
| 2346 |
{ |
| 2347 |
for (i=0; i<args->smpl->n; i++) |
| 2348 |
{ |
| 2349 |
for (j=0; j<2; j++) |
| 2350 |
hap_stage_vcf(args, tr, args->smpl->idx[i],j, TSCRIPT_AUX(tr)->hap[i*2+j]); |
| 2351 |
} |
| 2352 |
} |
| 2353 |
} |
| 2354 |
|
| 2355 |
|
| 2356 |
|
| 2357 |
args->nrm_tr++; |
| 2358 |
hts_expand(gf_tscript_t*,args->nrm_tr,args->mrm_tr,args->rm_tr); |
| 2359 |
args->rm_tr[args->nrm_tr-1] = tr; |
| 2360 |
} |
| 2361 |
} |
| 2362 |
|
| 2363 |
#define SWAP(type_t, a, b) { type_t t = a; a = b; b = t; } |
| 2364 |
|
| 2365 |
vbuf_t *vbuf_push(args_t *args, bcf1_t **rec_ptr) |
| 2366 |
{ |
| 2367 |
int i; |
| 2368 |
|
| 2369 |
assert(rec_ptr); |
| 2370 |
bcf1_t *rec = *rec_ptr; |
| 2371 |
|
| 2372 |
|
| 2373 |
i = args->vcf_rbuf.n ? rbuf_last(&args->vcf_rbuf) : -1; |
| 2374 |
if ( i<0 || args->vcf_buf[i]->vrec[0]->line->pos!=rec->pos ) |
| 2375 |
{ |
| 2376 |
|
| 2377 |
rbuf_expand0(&args->vcf_rbuf, vbuf_t*, args->vcf_rbuf.n+1, args->vcf_buf); |
| 2378 |
i = rbuf_append(&args->vcf_rbuf); |
| 2379 |
if ( !args->vcf_buf[i] ) args->vcf_buf[i] = (vbuf_t*) calloc(1,sizeof(vbuf_t)); |
| 2380 |
args->vcf_buf[i]->n = 0; |
| 2381 |
args->vcf_buf[i]->keep_until = 0; |
| 2382 |
} |
| 2383 |
vbuf_t *vbuf = args->vcf_buf[i]; |
| 2384 |
vbuf->n++; |
| 2385 |
hts_expand0(vrec_t*, vbuf->n, vbuf->m, vbuf->vrec); |
| 2386 |
if ( !vbuf->vrec[vbuf->n - 1] ) |
| 2387 |
vbuf->vrec[vbuf->n - 1] = (vrec_t*) calloc(1,sizeof(vrec_t)); |
| 2388 |
|
| 2389 |
vrec_t *vrec = vbuf->vrec[vbuf->n - 1]; |
| 2390 |
if ( args->phase!=PHASE_DROP_GT && args->smpl->n ) |
| 2391 |
{ |
| 2392 |
if ( !vrec->fmt_bm ) vrec->fmt_bm = (uint32_t*) calloc(args->hdr_nsmpl,sizeof(*vrec->fmt_bm) * args->nfmt_bcsq); |
| 2393 |
else memset(vrec->fmt_bm,0,args->hdr_nsmpl*sizeof(*vrec->fmt_bm) * args->nfmt_bcsq); |
| 2394 |
} |
| 2395 |
if ( !vrec->line ) vrec->line = bcf_init1(); |
| 2396 |
SWAP(bcf1_t*, (*rec_ptr), vrec->line); |
| 2397 |
|
| 2398 |
int ret; |
| 2399 |
khint_t k = kh_put(pos2vbuf, args->pos2vbuf, (int)rec->pos, &ret); |
| 2400 |
kh_val(args->pos2vbuf,k) = vbuf; |
| 2401 |
|
| 2402 |
return vbuf; |
| 2403 |
} |
| 2404 |
|
| 2405 |
void vbuf_flush(args_t *args, uint32_t pos) |
| 2406 |
{ |
| 2407 |
int i,j; |
| 2408 |
while ( args->vcf_rbuf.n ) |
| 2409 |
{ |
| 2410 |
vbuf_t *vbuf; |
| 2411 |
if ( !args->local_csq && args->active_tr->ndat ) |
| 2412 |
{ |
| 2413 |
|
| 2414 |
|
| 2415 |
vbuf = args->vcf_buf[ args->vcf_rbuf.f ]; |
| 2416 |
if ( vbuf->keep_until > pos ) break; |
| 2417 |
assert( vbuf->n ); |
| 2418 |
} |
| 2419 |
|
| 2420 |
i = rbuf_shift(&args->vcf_rbuf); |
| 2421 |
assert( i>=0 ); |
| 2422 |
vbuf = args->vcf_buf[i]; |
| 2423 |
int pos = vbuf->n ? vbuf->vrec[0]->line->pos : -1; |
| 2424 |
for (i=0; i<vbuf->n; i++) |
| 2425 |
{ |
| 2426 |
vrec_t *vrec = vbuf->vrec[i]; |
| 2427 |
if ( !args->out_fh ) |
| 2428 |
{ |
| 2429 |
vrec->nvcsq = 0; |
| 2430 |
continue; |
| 2431 |
} |
| 2432 |
if ( !vrec->nvcsq ) |
| 2433 |
{ |
| 2434 |
if ( bcf_write(args->out_fh, args->hdr, vrec->line)!=0 ) error("[%s] Error: cannot write to %s\n", __func__,args->output_fname?args->output_fname:"standard output"); |
| 2435 |
int save_pos = vrec->line->pos; |
| 2436 |
bcf_empty(vrec->line); |
| 2437 |
vrec->line->pos = save_pos; |
| 2438 |
continue; |
| 2439 |
} |
| 2440 |
|
| 2441 |
args->str.l = 0; |
| 2442 |
kput_vcsq(args, &vrec->vcsq[0], &args->str); |
| 2443 |
for (j=1; j<vrec->nvcsq; j++) |
| 2444 |
{ |
| 2445 |
kputc_(',', &args->str); |
| 2446 |
kput_vcsq(args, &vrec->vcsq[j], &args->str); |
| 2447 |
} |
| 2448 |
bcf_update_info_string(args->hdr, vrec->line, args->bcsq_tag, args->str.s); |
| 2449 |
if ( args->hdr_nsmpl ) |
| 2450 |
{ |
| 2451 |
if ( vrec->nfmt < args->nfmt_bcsq ) |
| 2452 |
for (j=1; j<args->hdr_nsmpl; j++) |
| 2453 |
memmove(&vrec->fmt_bm[j*vrec->nfmt], &vrec->fmt_bm[j*args->nfmt_bcsq], vrec->nfmt*sizeof(*vrec->fmt_bm)); |
| 2454 |
bcf_update_format_int32(args->hdr, vrec->line, args->bcsq_tag, vrec->fmt_bm, args->hdr_nsmpl*vrec->nfmt); |
| 2455 |
} |
| 2456 |
vrec->nvcsq = 0; |
| 2457 |
if ( bcf_write(args->out_fh, args->hdr, vrec->line)!=0 ) error("[%s] Error: cannot write to %s\n", __func__,args->output_fname?args->output_fname:"standard output"); |
| 2458 |
int save_pos = vrec->line->pos; |
| 2459 |
bcf_empty(vrec->line); |
| 2460 |
vrec->line->pos = save_pos; |
| 2461 |
} |
| 2462 |
if ( pos!=-1 ) |
| 2463 |
{ |
| 2464 |
khint_t k = kh_get(pos2vbuf, args->pos2vbuf, pos); |
| 2465 |
if ( k != kh_end(args->pos2vbuf) ) kh_del(pos2vbuf, args->pos2vbuf, k); |
| 2466 |
} |
| 2467 |
vbuf->n = 0; |
| 2468 |
} |
| 2469 |
if ( args->active_tr->ndat ) return; |
| 2470 |
|
| 2471 |
for (i=0; i<args->nrm_tr; i++) |
| 2472 |
{ |
| 2473 |
gf_tscript_t *tr = args->rm_tr[i]; |
| 2474 |
tscript_t *aux = TSCRIPT_AUX(tr); |
| 2475 |
if ( aux->root ) hap_destroy(aux->root); |
| 2476 |
aux->root = NULL; |
| 2477 |
free(aux->hap); |
| 2478 |
free(aux->ref); |
| 2479 |
free(aux->sref); |
| 2480 |
free(aux); |
| 2481 |
tr->aux = NULL; |
| 2482 |
} |
| 2483 |
args->nrm_tr = 0; |
| 2484 |
args->ncsq_buf = 0; |
| 2485 |
} |
| 2486 |
|
| 2487 |
void tscript_init_ref(args_t *args, gf_tscript_t *tr, const char *chr) |
| 2488 |
{ |
| 2489 |
int i, len; |
| 2490 |
int pad_beg = tr->beg >= N_REF_PAD ? N_REF_PAD : tr->beg; |
| 2491 |
|
| 2492 |
const char *tmp_chr = chr; |
| 2493 |
if ( !faidx_has_seq(args->fai,tmp_chr) ) |
| 2494 |
{ |
| 2495 |
tmp_chr = drop_chr_prefix(args,chr); |
| 2496 |
if ( !faidx_has_seq(args->fai,tmp_chr) ) tmp_chr = add_chr_prefix(args,chr); |
| 2497 |
} |
| 2498 |
TSCRIPT_AUX(tr)->ref = faidx_fetch_seq(args->fai, tmp_chr, tr->beg - pad_beg, tr->end + N_REF_PAD, &len); |
| 2499 |
if ( !TSCRIPT_AUX(tr)->ref ) |
| 2500 |
error("faidx_fetch_seq failed %s:%d-%d\n", chr,tr->beg+1,tr->end+1); |
| 2501 |
|
| 2502 |
int pad_end = len - (tr->end - tr->beg + 1 + pad_beg); |
| 2503 |
if ( pad_beg + pad_end != 2*N_REF_PAD ) |
| 2504 |
{ |
| 2505 |
char *ref = (char*) malloc(tr->end - tr->beg + 1 + 2*N_REF_PAD + 1); |
| 2506 |
for (i=0; i < N_REF_PAD - pad_beg; i++) ref[i] = 'N'; |
| 2507 |
memcpy(ref+i, TSCRIPT_AUX(tr)->ref, len); |
| 2508 |
len += i; |
| 2509 |
for (i=0; i < N_REF_PAD - pad_end; i++) ref[i+len] = 'N'; |
| 2510 |
ref[i+len] = 0; |
| 2511 |
free(TSCRIPT_AUX(tr)->ref); |
| 2512 |
TSCRIPT_AUX(tr)->ref = ref; |
| 2513 |
} |
| 2514 |
} |
| 2515 |
|
| 2516 |
static void sanity_check_ref(args_t *args, gf_tscript_t *tr, bcf1_t *rec) |
| 2517 |
{ |
| 2518 |
int vbeg = 0; |
| 2519 |
int rbeg = rec->pos - tr->beg + N_REF_PAD; |
| 2520 |
if ( rbeg < 0 ) { vbeg += abs(rbeg); rbeg = 0; } |
| 2521 |
char *ref = TSCRIPT_AUX(tr)->ref + rbeg; |
| 2522 |
char *vcf = rec->d.allele[0] + vbeg; |
| 2523 |
assert( vcf - rec->d.allele[0] < strlen(rec->d.allele[0]) && ref - TSCRIPT_AUX(tr)->ref < tr->end - tr->beg + 2*N_REF_PAD ); |
| 2524 |
int i = 0; |
| 2525 |
while ( ref[i] && vcf[i] ) |
| 2526 |
{ |
| 2527 |
if ( ref[i]!=vcf[i] && toupper(ref[i])!=toupper(vcf[i]) ) |
| 2528 |
error("Error: the fasta reference does not match the VCF REF allele at %s:%"PRId64" .. fasta=%c vcf=%c\n", |
| 2529 |
bcf_seqname(args->hdr,rec),(int64_t) rec->pos+vbeg+1,ref[i],vcf[i]); |
| 2530 |
i++; |
| 2531 |
} |
| 2532 |
} |
| 2533 |
|
| 2534 |
int test_cds_local(args_t *args, bcf1_t *rec) |
| 2535 |
{ |
| 2536 |
int i,j, ret = 0; |
| 2537 |
const char *chr = drop_chr_prefix(args, bcf_seqname(args->hdr,rec)); |
| 2538 |
|
| 2539 |
if ( !regidx_overlap(args->idx_cds,chr,rec->pos,rec->pos+rec->rlen, args->itr) ) return 0; |
| 2540 |
|
| 2541 |
|
| 2542 |
hap_node_t root, node; |
| 2543 |
root.type = HAP_ROOT; |
| 2544 |
kstring_t *tref = &args->hap->tref, *tseq = &args->hap->tseq; |
| 2545 |
|
| 2546 |
while ( regitr_overlap(args->itr) ) |
| 2547 |
{ |
| 2548 |
gf_cds_t *cds = regitr_payload(args->itr,gf_cds_t*); |
| 2549 |
gf_tscript_t *tr = cds->tr; |
| 2550 |
if ( !GF_is_coding(tr->type) ) continue; |
| 2551 |
ret = 1; |
| 2552 |
|
| 2553 |
if ( !TSCRIPT_AUX(tr) ) |
| 2554 |
{ |
| 2555 |
tr->aux = calloc(sizeof(tscript_t),1); |
| 2556 |
tscript_init_ref(args, tr, chr); |
| 2557 |
tscript_splice_ref(tr); |
| 2558 |
khp_insert(trhp, args->active_tr, &tr); |
| 2559 |
} |
| 2560 |
|
| 2561 |
sanity_check_ref(args, tr, rec); |
| 2562 |
|
| 2563 |
kstring_t sref; |
| 2564 |
sref.s = TSCRIPT_AUX(tr)->sref; |
| 2565 |
sref.l = TSCRIPT_AUX(tr)->nsref; |
| 2566 |
sref.m = sref.l; |
| 2567 |
|
| 2568 |
for (i=1; i<rec->n_allele; i++) |
| 2569 |
{ |
| 2570 |
if ( rec->d.allele[i][0]=='<' || rec->d.allele[i][0]=='*' ) { continue; } |
| 2571 |
if ( hap_init(args, &root, &node, cds, rec, i)!=0 ) continue; |
| 2572 |
|
| 2573 |
csq_t csq; |
| 2574 |
memset(&csq, 0, sizeof(csq_t)); |
| 2575 |
csq.pos = rec->pos; |
| 2576 |
csq.type.biotype = tr->type; |
| 2577 |
csq.type.strand = tr->strand; |
| 2578 |
csq.type.trid = tr->id; |
| 2579 |
csq.type.vcf_ial = i; |
| 2580 |
csq.type.gene = tr->gene->name; |
| 2581 |
|
| 2582 |
int csq_type = node.csq; |
| 2583 |
|
| 2584 |
|
| 2585 |
if ( node.type == HAP_SSS ) |
| 2586 |
{ |
| 2587 |
csq.type.type = csq_type; |
| 2588 |
csq_stage(args, &csq, rec); |
| 2589 |
} |
| 2590 |
else |
| 2591 |
{ |
| 2592 |
kstring_t sseq; |
| 2593 |
sseq.m = sref.m - 2*N_REF_PAD + node.dlen; |
| 2594 |
sseq.s = node.seq; |
| 2595 |
int alen = sseq.l = strlen(sseq.s); |
| 2596 |
int fill = node.dlen%3 && alen ? 1 : 0; |
| 2597 |
cds_translate(&sref, &sseq, node.sbeg,node.sbeg,node.sbeg+node.rlen, tr->strand, tseq, fill); |
| 2598 |
|
| 2599 |
sseq.m = sref.m - 2*N_REF_PAD; |
| 2600 |
sseq.s = sref.s + N_REF_PAD + node.sbeg; |
| 2601 |
sseq.l = node.rlen; |
| 2602 |
cds_translate(&sref, &sseq, node.sbeg,node.sbeg,node.sbeg+node.rlen, tr->strand, tref, fill); |
| 2603 |
|
| 2604 |
|
| 2605 |
for (j=0; j<tref->l; j++) |
| 2606 |
if ( tref->s[j]=='*' ) break; |
| 2607 |
if ( j!=tref->l ) |
| 2608 |
{ |
| 2609 |
tref->l = j+1; |
| 2610 |
tref->s[j+1] = 0; |
| 2611 |
} |
| 2612 |
for (j=0; j<tseq->l; j++) |
| 2613 |
if ( tseq->s[j]=='*' ) break; |
| 2614 |
if ( j!=tseq->l ) |
| 2615 |
{ |
| 2616 |
tseq->l = j+1; |
| 2617 |
tseq->s[j+1] = 0; |
| 2618 |
} |
| 2619 |
if ( csq_type & CSQ_STOP_LOST ) |
| 2620 |
{ |
| 2621 |
if ( tref->s[tref->l-1]=='*' && tref->s[tref->l-1] == tseq->s[tseq->l-1] ) |
| 2622 |
{ |
| 2623 |
csq_type &= ~CSQ_STOP_LOST; |
| 2624 |
csq_type |= CSQ_STOP_RETAINED; |
| 2625 |
} |
| 2626 |
else if (tref->s[tref->l-1]!='*' ) |
| 2627 |
{ |
| 2628 |
|
| 2629 |
|
| 2630 |
if ( tseq->s[tseq->l-1] == '*' ) |
| 2631 |
{ |
| 2632 |
csq_type &= ~CSQ_STOP_GAINED; |
| 2633 |
csq_type |= CSQ_STOP_RETAINED; |
| 2634 |
} |
| 2635 |
else |
| 2636 |
csq_type |= CSQ_INCOMPLETE_CDS; |
| 2637 |
} |
| 2638 |
} |
| 2639 |
if ( csq_type & CSQ_START_LOST && tref->s[0]!='M' ) |
| 2640 |
csq_type &= ~CSQ_START_LOST; |
| 2641 |
if ( node.dlen!=0 ) |
| 2642 |
{ |
| 2643 |
if ( node.dlen%3 ) |
| 2644 |
csq_type |= CSQ_FRAMESHIFT_VARIANT; |
| 2645 |
else if ( node.dlen<0 ) |
| 2646 |
csq_type |= CSQ_INFRAME_DELETION; |
| 2647 |
else |
| 2648 |
csq_type |= CSQ_INFRAME_INSERTION; |
| 2649 |
if ( tref->s[tref->l-1]!='*' && tseq->s[tseq->l-1]=='*' ) |
| 2650 |
csq_type |= CSQ_STOP_GAINED; |
| 2651 |
} |
| 2652 |
else |
| 2653 |
{ |
| 2654 |
int aa_change = 0; |
| 2655 |
for (j=0; j<tref->l; j++) |
| 2656 |
{ |
| 2657 |
if ( tref->s[j] == tseq->s[j] ) continue; |
| 2658 |
aa_change = 1; |
| 2659 |
if ( tref->s[j] == '*' ) |
| 2660 |
csq_type |= CSQ_STOP_LOST; |
| 2661 |
else if ( tseq->s[j] == '*' ) |
| 2662 |
csq_type |= CSQ_STOP_GAINED; |
| 2663 |
else |
| 2664 |
csq_type |= CSQ_MISSENSE_VARIANT; |
| 2665 |
} |
| 2666 |
if ( !aa_change ) |
| 2667 |
csq_type |= CSQ_SYNONYMOUS_VARIANT; |
| 2668 |
} |
| 2669 |
if ( csq_type & CSQ_COMPOUND ) |
| 2670 |
{ |
| 2671 |
|
| 2672 |
kstring_t str = {0,0,0}; |
| 2673 |
int aa_rbeg = tr->strand==STRAND_FWD ? node.sbeg/3+1 : (TSCRIPT_AUX(tr)->nsref - 2*N_REF_PAD - node.sbeg - node.rlen)/3+1; |
| 2674 |
int aa_sbeg = tr->strand==STRAND_FWD ? node.sbeg/3+1 : (TSCRIPT_AUX(tr)->nsref - 2*N_REF_PAD + node.dlen - node.sbeg - alen)/3+1; |
| 2675 |
kputc_('|', &str); |
| 2676 |
kputw(aa_rbeg, &str); |
| 2677 |
kprint_aa_prediction(args,aa_rbeg,tref,&str); |
| 2678 |
if ( !(csq_type & CSQ_SYNONYMOUS_VARIANT) ) |
| 2679 |
{ |
| 2680 |
kputc_('>', &str); |
| 2681 |
kputw(aa_sbeg, &str); |
| 2682 |
kprint_aa_prediction(args,aa_sbeg,tseq,&str); |
| 2683 |
} |
| 2684 |
kputc_('|', &str); |
| 2685 |
kputw(rec->pos+1, &str); |
| 2686 |
kputs(node.var, &str); |
| 2687 |
csq.type.vstr = str; |
| 2688 |
csq.type.type = csq_type & CSQ_COMPOUND; |
| 2689 |
csq_stage(args, &csq, rec); |
| 2690 |
|
| 2691 |
|
| 2692 |
if ( !TSCRIPT_AUX(tr)->root ) |
| 2693 |
TSCRIPT_AUX(tr)->root = (hap_node_t*) calloc(1,sizeof(hap_node_t)); |
| 2694 |
TSCRIPT_AUX(tr)->root->ncsq_list++; |
| 2695 |
hts_expand0(csq_t,TSCRIPT_AUX(tr)->root->ncsq_list,TSCRIPT_AUX(tr)->root->mcsq_list,TSCRIPT_AUX(tr)->root->csq_list); |
| 2696 |
csq_t *rm_csq = TSCRIPT_AUX(tr)->root->csq_list + TSCRIPT_AUX(tr)->root->ncsq_list - 1; |
| 2697 |
rm_csq->type.vstr = str; |
| 2698 |
} |
| 2699 |
if ( csq_type & ~CSQ_COMPOUND ) |
| 2700 |
{ |
| 2701 |
csq.type.type = csq_type & ~CSQ_COMPOUND; |
| 2702 |
csq.type.vstr.l = 0; |
| 2703 |
csq_stage(args, &csq, rec); |
| 2704 |
} |
| 2705 |
} |
| 2706 |
free(node.seq); |
| 2707 |
free(node.var); |
| 2708 |
} |
| 2709 |
} |
| 2710 |
return ret; |
| 2711 |
} |
| 2712 |
|
| 2713 |
int test_cds(args_t *args, bcf1_t *rec, vbuf_t *vbuf) |
| 2714 |
{ |
| 2715 |
static int overlaps_warned = 0, multiploid_warned = 0; |
| 2716 |
|
| 2717 |
int i, ret = 0, hap_ret; |
| 2718 |
const char *chr = drop_chr_prefix(args, bcf_seqname(args->hdr,rec)); |
| 2719 |
|
| 2720 |
if ( !regidx_overlap(args->idx_cds,chr,rec->pos,rec->pos+rec->rlen, args->itr) ) return 0; |
| 2721 |
while ( regitr_overlap(args->itr) ) |
| 2722 |
{ |
| 2723 |
gf_cds_t *cds = regitr_payload(args->itr,gf_cds_t*); |
| 2724 |
gf_tscript_t *tr = cds->tr; |
| 2725 |
if ( !GF_is_coding(tr->type) ) continue; |
| 2726 |
if ( vbuf->keep_until < tr->end ) vbuf->keep_until = tr->end; |
| 2727 |
ret = 1; |
| 2728 |
if ( !TSCRIPT_AUX(tr) ) |
| 2729 |
{ |
| 2730 |
|
| 2731 |
tr->aux = calloc(sizeof(tscript_t),1); |
| 2732 |
tscript_init_ref(args, tr, chr); |
| 2733 |
|
| 2734 |
TSCRIPT_AUX(tr)->root = (hap_node_t*) calloc(1,sizeof(hap_node_t)); |
| 2735 |
TSCRIPT_AUX(tr)->nhap = args->phase==PHASE_DROP_GT ? 1 : 2*args->smpl->n; |
| 2736 |
TSCRIPT_AUX(tr)->hap = (hap_node_t**) malloc(TSCRIPT_AUX(tr)->nhap*sizeof(hap_node_t*)); |
| 2737 |
for (i=0; i<TSCRIPT_AUX(tr)->nhap; i++) TSCRIPT_AUX(tr)->hap[i] = NULL; |
| 2738 |
TSCRIPT_AUX(tr)->root->nend = TSCRIPT_AUX(tr)->nhap; |
| 2739 |
TSCRIPT_AUX(tr)->root->type = HAP_ROOT; |
| 2740 |
|
| 2741 |
khp_insert(trhp, args->active_tr, &tr); |
| 2742 |
} |
| 2743 |
|
| 2744 |
sanity_check_ref(args, tr, rec); |
| 2745 |
|
| 2746 |
if ( args->phase==PHASE_DROP_GT ) |
| 2747 |
{ |
| 2748 |
if ( rec->d.allele[1][0]=='<' || rec->d.allele[1][0]=='*' ) { continue; } |
| 2749 |
hap_node_t *parent = TSCRIPT_AUX(tr)->hap[0] ? TSCRIPT_AUX(tr)->hap[0] : TSCRIPT_AUX(tr)->root; |
| 2750 |
hap_node_t *child = (hap_node_t*)calloc(1,sizeof(hap_node_t)); |
| 2751 |
hap_ret = hap_init(args, parent, child, cds, rec, 1); |
| 2752 |
if ( hap_ret!=0 ) |
| 2753 |
{ |
| 2754 |
|
| 2755 |
if ( hap_ret==1 ) |
| 2756 |
{ |
| 2757 |
if ( args->verbosity && (!overlaps_warned || args->verbosity > 1) ) |
| 2758 |
{ |
| 2759 |
fprintf(stderr, |
| 2760 |
"Warning: Skipping overlapping variants at %s:%"PRId64"\t%s>%s.\n", |
| 2761 |
chr,(int64_t) rec->pos+1,rec->d.allele[0],rec->d.allele[1]); |
| 2762 |
if ( !overlaps_warned ) |
| 2763 |
fprintf(stderr," This message is printed only once, the verbosity can be increased with `--verbose 2`\n"); |
| 2764 |
overlaps_warned = 1; |
| 2765 |
} |
| 2766 |
if ( args->out ) |
| 2767 |
fprintf(args->out,"LOG\tWarning: Skipping overlapping variants at %s:%"PRId64"\t%s>%s\n", chr,(int64_t) rec->pos+1,rec->d.allele[0],rec->d.allele[1]); |
| 2768 |
} |
| 2769 |
else ret = 1; |
| 2770 |
hap_destroy(child); |
| 2771 |
continue; |
| 2772 |
} |
| 2773 |
if ( child->type==HAP_SSS ) |
| 2774 |
{ |
| 2775 |
csq_t csq; |
| 2776 |
memset(&csq, 0, sizeof(csq_t)); |
| 2777 |
csq.pos = rec->pos; |
| 2778 |
csq.type.biotype = tr->type; |
| 2779 |
csq.type.strand = tr->strand; |
| 2780 |
csq.type.trid = tr->id; |
| 2781 |
csq.type.vcf_ial = 1; |
| 2782 |
csq.type.gene = tr->gene->name; |
| 2783 |
csq.type.type = child->csq; |
| 2784 |
csq_stage(args, &csq, rec); |
| 2785 |
hap_destroy(child); |
| 2786 |
ret = 1; |
| 2787 |
continue; |
| 2788 |
} |
| 2789 |
parent->nend--; |
| 2790 |
parent->nchild = 1; |
| 2791 |
parent->mchild = 1; |
| 2792 |
parent->child = (hap_node_t**) malloc(sizeof(hap_node_t*)); |
| 2793 |
parent->child[0] = child; |
| 2794 |
TSCRIPT_AUX(tr)->hap[0] = child; |
| 2795 |
TSCRIPT_AUX(tr)->hap[0]->nend = 1; |
| 2796 |
continue; |
| 2797 |
} |
| 2798 |
|
| 2799 |
|
| 2800 |
int j, ismpl, ihap, ngts = bcf_get_genotypes(args->hdr, rec, &args->gt_arr, &args->mgt_arr); |
| 2801 |
ngts /= bcf_hdr_nsamples(args->hdr); |
| 2802 |
if ( ngts!=1 && ngts!=2 ) |
| 2803 |
{ |
| 2804 |
if ( args->verbosity && (!multiploid_warned || args->verbosity > 1) ) |
| 2805 |
{ |
| 2806 |
fprintf(stderr, |
| 2807 |
"Warning: Skipping site with non-diploid/non-haploid genotypes at %s:%"PRId64"\t%s>%s.\n", |
| 2808 |
chr,(int64_t) rec->pos+1,rec->d.allele[0],rec->d.allele[1]); |
| 2809 |
if ( !multiploid_warned ) |
| 2810 |
fprintf(stderr," This message is printed only once, the verbosity can be increased with `--verbose 2`\n"); |
| 2811 |
multiploid_warned = 1; |
| 2812 |
} |
| 2813 |
if ( args->out ) |
| 2814 |
fprintf(args->out,"LOG\tWarning: Skipping site with non-diploid/non-haploid genotypes at %s:%"PRId64"\t%s>%s\n", chr,(int64_t) rec->pos+1,rec->d.allele[0],rec->d.allele[1]); |
| 2815 |
continue; |
| 2816 |
} |
| 2817 |
for (ismpl=0; ismpl<args->smpl->n; ismpl++) |
| 2818 |
{ |
| 2819 |
int32_t *gt = args->gt_arr + args->smpl->idx[ismpl]*ngts; |
| 2820 |
if ( gt[0]==bcf_gt_missing ) continue; |
| 2821 |
|
| 2822 |
if ( ngts>1 && gt[1]!=bcf_gt_missing && gt[1]!=bcf_int32_vector_end && bcf_gt_allele(gt[0])!=bcf_gt_allele(gt[1]) ) |
| 2823 |
{ |
| 2824 |
if ( args->phase==PHASE_MERGE ) |
| 2825 |
{ |
| 2826 |
if ( !bcf_gt_allele(gt[0]) ) gt[0] = gt[1]; |
| 2827 |
} |
| 2828 |
if ( !bcf_gt_is_phased(gt[0]) && !bcf_gt_is_phased(gt[1]) ) |
| 2829 |
{ |
| 2830 |
if ( args->phase==PHASE_REQUIRE ) |
| 2831 |
error("Unphased heterozygous genotype at %s:%"PRId64", sample %s. See the --phase option.\n", chr,(int64_t) rec->pos+1,args->hdr->samples[args->smpl->idx[ismpl]]); |
| 2832 |
if ( args->phase==PHASE_SKIP ) |
| 2833 |
continue; |
| 2834 |
if ( args->phase==PHASE_NON_REF ) |
| 2835 |
{ |
| 2836 |
if ( !bcf_gt_allele(gt[0]) ) gt[0] = gt[1]; |
| 2837 |
else if ( !bcf_gt_allele(gt[1]) ) gt[1] = gt[0]; |
| 2838 |
} |
| 2839 |
} |
| 2840 |
} |
| 2841 |
|
| 2842 |
for (ihap=0; ihap<ngts; ihap++) |
| 2843 |
{ |
| 2844 |
if ( gt[ihap]==bcf_gt_missing || gt[ihap]==bcf_int32_vector_end ) continue; |
| 2845 |
|
| 2846 |
i = 2*ismpl + ihap; |
| 2847 |
|
| 2848 |
int ial = bcf_gt_allele(gt[ihap]); |
| 2849 |
if ( !ial ) continue; |
| 2850 |
assert( ial < rec->n_allele ); |
| 2851 |
if ( rec->d.allele[ial][0]=='<' || rec->d.allele[ial][0]=='*' ) { continue; } |
| 2852 |
|
| 2853 |
hap_node_t *parent = TSCRIPT_AUX(tr)->hap[i] ? TSCRIPT_AUX(tr)->hap[i] : TSCRIPT_AUX(tr)->root; |
| 2854 |
if ( parent->cur_rec==rec && parent->cur_child[ial]>=0 ) |
| 2855 |
{ |
| 2856 |
|
| 2857 |
TSCRIPT_AUX(tr)->hap[i] = parent->child[ parent->cur_child[ial] ]; |
| 2858 |
TSCRIPT_AUX(tr)->hap[i]->nend++; |
| 2859 |
parent->nend--; |
| 2860 |
continue; |
| 2861 |
} |
| 2862 |
|
| 2863 |
hap_node_t *child = (hap_node_t*)calloc(1,sizeof(hap_node_t)); |
| 2864 |
hap_ret = hap_init(args, parent, child, cds, rec, ial); |
| 2865 |
if ( hap_ret!=0 ) |
| 2866 |
{ |
| 2867 |
|
| 2868 |
if ( hap_ret==1 ) |
| 2869 |
{ |
| 2870 |
if ( args->verbosity && (!overlaps_warned || args->verbosity > 1) ) |
| 2871 |
{ |
| 2872 |
fprintf(stderr, |
| 2873 |
"Warning: Skipping overlapping variants at %s:%"PRId64", sample %s\t%s>%s.\n", |
| 2874 |
chr,(int64_t) rec->pos+1,args->hdr->samples[args->smpl->idx[ismpl]],rec->d.allele[0],rec->d.allele[ial]); |
| 2875 |
if ( !overlaps_warned ) |
| 2876 |
fprintf(stderr," This message is printed only once, the verbosity can be increased with `--verbose 2`\n"); |
| 2877 |
overlaps_warned = 1; |
| 2878 |
} |
| 2879 |
if ( args->out ) |
| 2880 |
fprintf(args->out,"LOG\tWarning: Skipping overlapping variants at %s:%"PRId64", sample %s\t%s>%s\n", |
| 2881 |
chr,(int64_t) rec->pos+1,args->hdr->samples[args->smpl->idx[ismpl]],rec->d.allele[0],rec->d.allele[ial]); |
| 2882 |
} |
| 2883 |
hap_destroy(child); |
| 2884 |
continue; |
| 2885 |
} |
| 2886 |
if ( child->type==HAP_SSS ) |
| 2887 |
{ |
| 2888 |
csq_t csq; |
| 2889 |
memset(&csq, 0, sizeof(csq_t)); |
| 2890 |
csq.pos = rec->pos; |
| 2891 |
csq.type.biotype = tr->type; |
| 2892 |
csq.type.strand = tr->strand; |
| 2893 |
csq.type.trid = tr->id; |
| 2894 |
csq.type.vcf_ial = ial; |
| 2895 |
csq.type.gene = tr->gene->name; |
| 2896 |
csq.type.type = child->csq; |
| 2897 |
csq_stage(args, &csq, rec); |
| 2898 |
hap_destroy(child); |
| 2899 |
continue; |
| 2900 |
} |
| 2901 |
if ( parent->cur_rec!=rec ) |
| 2902 |
{ |
| 2903 |
hts_expand(int,rec->n_allele,parent->mcur_child,parent->cur_child); |
| 2904 |
for (j=0; j<rec->n_allele; j++) parent->cur_child[j] = -1; |
| 2905 |
parent->cur_rec = rec; |
| 2906 |
} |
| 2907 |
|
| 2908 |
j = parent->nchild++; |
| 2909 |
hts_expand0(hap_node_t*,parent->nchild,parent->mchild,parent->child); |
| 2910 |
parent->cur_child[ial] = j; |
| 2911 |
parent->child[j] = child; |
| 2912 |
TSCRIPT_AUX(tr)->hap[i] = child; |
| 2913 |
TSCRIPT_AUX(tr)->hap[i]->nend++; |
| 2914 |
parent->nend--; |
| 2915 |
} |
| 2916 |
} |
| 2917 |
} |
| 2918 |
return ret; |
| 2919 |
} |
| 2920 |
|
| 2921 |
void csq_stage(args_t *args, csq_t *csq, bcf1_t *rec) |
| 2922 |
{ |
| 2923 |
|
| 2924 |
|
| 2925 |
|
| 2926 |
if ( csq_push(args, csq, rec)!=0 && args->phase==PHASE_DROP_GT ) return; |
| 2927 |
|
| 2928 |
int i,j,ngt = 0; |
| 2929 |
if ( args->phase!=PHASE_DROP_GT ) |
| 2930 |
{ |
| 2931 |
ngt = bcf_get_genotypes(args->hdr, rec, &args->gt_arr, &args->mgt_arr); |
| 2932 |
if ( ngt>0 ) ngt /= bcf_hdr_nsamples(args->hdr); |
| 2933 |
} |
| 2934 |
if ( ngt<=0 ) |
| 2935 |
{ |
| 2936 |
if ( args->output_type==FT_TAB_TEXT ) |
| 2937 |
csq_print_text(args, csq, -1,0); |
| 2938 |
return; |
| 2939 |
} |
| 2940 |
assert( ngt<=2 ); |
| 2941 |
|
| 2942 |
if ( args->output_type==FT_TAB_TEXT ) |
| 2943 |
{ |
| 2944 |
for (i=0; i<args->smpl->n; i++) |
| 2945 |
{ |
| 2946 |
int32_t *gt = args->gt_arr + args->smpl->idx[i]*ngt; |
| 2947 |
for (j=0; j<ngt; j++) |
| 2948 |
{ |
| 2949 |
if ( gt[j]==bcf_gt_missing || gt[j]==bcf_int32_vector_end ) continue; |
| 2950 |
int ial = bcf_gt_allele(gt[j]); |
| 2951 |
if ( !ial || ial!=csq->type.vcf_ial ) continue; |
| 2952 |
csq_print_text(args, csq, args->smpl->idx[i],j+1); |
| 2953 |
} |
| 2954 |
} |
| 2955 |
return; |
| 2956 |
} |
| 2957 |
|
| 2958 |
vrec_t *vrec = csq->vrec; |
| 2959 |
for (i=0; i<args->smpl->n; i++) |
| 2960 |
{ |
| 2961 |
int32_t *gt = args->gt_arr + args->smpl->idx[i]*ngt; |
| 2962 |
for (j=0; j<ngt; j++) |
| 2963 |
{ |
| 2964 |
if ( gt[j]==bcf_gt_missing || gt[j]==bcf_int32_vector_end ) continue; |
| 2965 |
int ial = bcf_gt_allele(gt[j]); |
| 2966 |
if ( !ial || ial!=csq->type.vcf_ial ) continue; |
| 2967 |
|
| 2968 |
int icsq2 = 2*csq->idx + j; |
| 2969 |
if ( icsq2 >= args->ncsq2_max ) |
| 2970 |
{ |
| 2971 |
int ismpl = args->smpl->idx[i]; |
| 2972 |
if ( args->verbosity && (!args->ncsq2_small_warned || args->verbosity > 1) ) |
| 2973 |
{ |
| 2974 |
fprintf(stderr, |
| 2975 |
"Warning: Too many consequences for sample %s at %s:%"PRId64", keeping the first %d and skipping the rest.\n", |
| 2976 |
args->hdr->samples[ismpl],bcf_hdr_id2name(args->hdr,args->rid),(int64_t) vrec->line->pos+1,icsq2+1); |
| 2977 |
if ( !args->ncsq2_small_warned ) |
| 2978 |
fprintf(stderr," The limit can be increased by setting the --ncsq parameter. This warning is printed only once.\n"); |
| 2979 |
args->ncsq2_small_warned = 1; |
| 2980 |
} |
| 2981 |
if ( args->ncsq2_small_warned < icsq2 ) args->ncsq2_small_warned = icsq2; |
| 2982 |
break; |
| 2983 |
} |
| 2984 |
int ival, ibit; |
| 2985 |
icsq2_to_bit(icsq2, &ival,&ibit); |
| 2986 |
if ( vrec->nfmt < 1 + ival ) vrec->nfmt = 1 + ival; |
| 2987 |
vrec->fmt_bm[i*args->nfmt_bcsq + ival] |= 1 << ibit; |
| 2988 |
} |
| 2989 |
} |
| 2990 |
} |
| 2991 |
int test_utr(args_t *args, bcf1_t *rec) |
| 2992 |
{ |
| 2993 |
const char *chr = drop_chr_prefix(args, bcf_seqname(args->hdr,rec)); |
| 2994 |
|
| 2995 |
if ( !regidx_overlap(args->idx_utr,chr,rec->pos,rec->pos+rec->rlen, args->itr) ) return 0; |
| 2996 |
|
| 2997 |
splice_t splice; |
| 2998 |
splice_init(&splice, rec); |
| 2999 |
|
| 3000 |
int i, ret = 0; |
| 3001 |
while ( regitr_overlap(args->itr) ) |
| 3002 |
{ |
| 3003 |
gf_utr_t *utr = regitr_payload(args->itr, gf_utr_t*); |
| 3004 |
gf_tscript_t *tr = splice.tr = utr->tr; |
| 3005 |
for (i=1; i<rec->n_allele; i++) |
| 3006 |
{ |
| 3007 |
if ( rec->d.allele[i][0]=='<' || rec->d.allele[i][0]=='*' ) { continue; } |
| 3008 |
splice.vcf.alt = rec->d.allele[i]; |
| 3009 |
splice.csq = 0; |
| 3010 |
int splice_ret = splice_csq(args, &splice, utr->beg, utr->end); |
| 3011 |
if ( splice_ret!=SPLICE_INSIDE && splice_ret!=SPLICE_OVERLAP ) continue; |
| 3012 |
csq_t csq; |
| 3013 |
memset(&csq, 0, sizeof(csq_t)); |
| 3014 |
csq.pos = rec->pos; |
| 3015 |
csq.type.type = utr->which==prime5 ? CSQ_UTR5 : CSQ_UTR3; |
| 3016 |
csq.type.biotype = tr->type; |
| 3017 |
csq.type.strand = tr->strand; |
| 3018 |
csq.type.trid = tr->id; |
| 3019 |
csq.type.vcf_ial = i; |
| 3020 |
csq.type.gene = tr->gene->name; |
| 3021 |
csq_stage(args, &csq, rec); |
| 3022 |
ret = 1; |
| 3023 |
} |
| 3024 |
} |
| 3025 |
assert(!splice.kref.s); |
| 3026 |
assert(!splice.kalt.s); |
| 3027 |
return ret; |
| 3028 |
} |
| 3029 |
int test_splice(args_t *args, bcf1_t *rec) |
| 3030 |
{ |
| 3031 |
const char *chr = drop_chr_prefix(args, bcf_seqname(args->hdr,rec)); |
| 3032 |
if ( !regidx_overlap(args->idx_exon,chr,rec->pos,rec->pos + rec->rlen, args->itr) ) return 0; |
| 3033 |
|
| 3034 |
splice_t splice; |
| 3035 |
splice_init(&splice, rec); |
| 3036 |
splice.check_acceptor = splice.check_donor = 1; |
| 3037 |
|
| 3038 |
int i, ret = 0; |
| 3039 |
while ( regitr_overlap(args->itr) ) |
| 3040 |
{ |
| 3041 |
gf_exon_t *exon = regitr_payload(args->itr, gf_exon_t*); |
| 3042 |
splice.tr = exon->tr; |
| 3043 |
if ( !splice.tr->ncds ) continue; |
| 3044 |
|
| 3045 |
splice.check_region_beg = splice.tr->beg==exon->beg ? 0 : 1; |
| 3046 |
splice.check_region_end = splice.tr->end==exon->end ? 0 : 1; |
| 3047 |
|
| 3048 |
for (i=1; i<rec->n_allele; i++) |
| 3049 |
{ |
| 3050 |
if ( rec->d.allele[1][0]=='<' || rec->d.allele[1][0]=='*' ) { continue; } |
| 3051 |
splice.vcf.alt = rec->d.allele[i]; |
| 3052 |
splice.csq = 0; |
| 3053 |
splice_csq(args, &splice, exon->beg, exon->end); |
| 3054 |
if ( splice.csq ) ret = 1; |
| 3055 |
} |
| 3056 |
} |
| 3057 |
free(splice.kref.s); |
| 3058 |
free(splice.kalt.s); |
| 3059 |
return ret; |
| 3060 |
} |
| 3061 |
int test_tscript(args_t *args, bcf1_t *rec) |
| 3062 |
{ |
| 3063 |
const char *chr = drop_chr_prefix(args, bcf_seqname(args->hdr,rec)); |
| 3064 |
if ( !regidx_overlap(args->idx_tscript,chr,rec->pos,rec->pos+rec->rlen, args->itr) ) return 0; |
| 3065 |
|
| 3066 |
splice_t splice; |
| 3067 |
splice_init(&splice, rec); |
| 3068 |
|
| 3069 |
int i, ret = 0; |
| 3070 |
while ( regitr_overlap(args->itr) ) |
| 3071 |
{ |
| 3072 |
gf_tscript_t *tr = splice.tr = regitr_payload(args->itr, gf_tscript_t*); |
| 3073 |
for (i=1; i<rec->n_allele; i++) |
| 3074 |
{ |
| 3075 |
if ( rec->d.allele[i][0]=='<' || rec->d.allele[i][0]=='*' ) { continue; } |
| 3076 |
splice.vcf.alt = rec->d.allele[i]; |
| 3077 |
splice.csq = 0; |
| 3078 |
int splice_ret = splice_csq(args, &splice, tr->beg, tr->end); |
| 3079 |
if ( splice_ret!=SPLICE_INSIDE && splice_ret!=SPLICE_OVERLAP ) continue; |
| 3080 |
csq_t csq; |
| 3081 |
memset(&csq, 0, sizeof(csq_t)); |
| 3082 |
csq.pos = rec->pos; |
| 3083 |
csq.type.type = GF_is_coding(tr->type) ? CSQ_INTRON : CSQ_NON_CODING; |
| 3084 |
csq.type.biotype = tr->type; |
| 3085 |
csq.type.strand = tr->strand; |
| 3086 |
csq.type.trid = tr->id; |
| 3087 |
csq.type.gene = tr->gene->name; |
| 3088 |
csq_stage(args, &csq, rec); |
| 3089 |
ret = 1; |
| 3090 |
} |
| 3091 |
} |
| 3092 |
assert(!splice.kref.s); |
| 3093 |
assert(!splice.kalt.s); |
| 3094 |
return ret; |
| 3095 |
} |
| 3096 |
|
| 3097 |
void test_symbolic_alt(args_t *args, bcf1_t *rec) |
| 3098 |
{ |
| 3099 |
static int warned = 0; |
| 3100 |
if ( args->verbosity && (!warned && args->verbosity > 0) ) |
| 3101 |
{ |
| 3102 |
fprintf(stderr,"Warning: The support for symbolic ALT insertions is experimental.\n"); |
| 3103 |
warned = 1; |
| 3104 |
} |
| 3105 |
|
| 3106 |
const char *chr = drop_chr_prefix(args, bcf_seqname(args->hdr,rec)); |
| 3107 |
|
| 3108 |
|
| 3109 |
int beg = rec->pos + 1; |
| 3110 |
int end = beg; |
| 3111 |
int csq_class = CSQ_ELONGATION; |
| 3112 |
|
| 3113 |
int hit = 0; |
| 3114 |
if ( regidx_overlap(args->idx_cds,chr,beg,end, args->itr) ) |
| 3115 |
{ |
| 3116 |
while ( regitr_overlap(args->itr) ) |
| 3117 |
{ |
| 3118 |
csq_t csq; |
| 3119 |
memset(&csq, 0, sizeof(csq_t)); |
| 3120 |
gf_cds_t *cds = regitr_payload(args->itr,gf_cds_t*); |
| 3121 |
gf_tscript_t *tr = cds->tr; |
| 3122 |
csq.type.type = (GF_is_coding(tr->type) ? CSQ_CODING_SEQUENCE : CSQ_NON_CODING) | csq_class; |
| 3123 |
csq.pos = rec->pos; |
| 3124 |
csq.type.biotype = tr->type; |
| 3125 |
csq.type.strand = tr->strand; |
| 3126 |
csq.type.trid = tr->id; |
| 3127 |
csq.type.gene = tr->gene->name; |
| 3128 |
csq_stage(args, &csq, rec); |
| 3129 |
hit = 1; |
| 3130 |
} |
| 3131 |
} |
| 3132 |
if ( regidx_overlap(args->idx_utr,chr,beg,end, args->itr) ) |
| 3133 |
{ |
| 3134 |
while ( regitr_overlap(args->itr) ) |
| 3135 |
{ |
| 3136 |
csq_t csq; |
| 3137 |
memset(&csq, 0, sizeof(csq_t)); |
| 3138 |
gf_utr_t *utr = regitr_payload(args->itr, gf_utr_t*); |
| 3139 |
gf_tscript_t *tr = utr->tr; |
| 3140 |
csq.type.type = (utr->which==prime5 ? CSQ_UTR5 : CSQ_UTR3) | csq_class; |
| 3141 |
csq.pos = rec->pos; |
| 3142 |
csq.type.biotype = tr->type; |
| 3143 |
csq.type.strand = tr->strand; |
| 3144 |
csq.type.trid = tr->id; |
| 3145 |
csq.type.gene = tr->gene->name; |
| 3146 |
csq_stage(args, &csq, rec); |
| 3147 |
hit = 1; |
| 3148 |
} |
| 3149 |
} |
| 3150 |
if ( regidx_overlap(args->idx_exon,chr,beg,end, args->itr) ) |
| 3151 |
{ |
| 3152 |
splice_t splice; |
| 3153 |
splice_init(&splice, rec); |
| 3154 |
splice.check_acceptor = splice.check_donor = 1; |
| 3155 |
|
| 3156 |
while ( regitr_overlap(args->itr) ) |
| 3157 |
{ |
| 3158 |
gf_exon_t *exon = regitr_payload(args->itr, gf_exon_t*); |
| 3159 |
splice.tr = exon->tr; |
| 3160 |
if ( !splice.tr->ncds ) continue; |
| 3161 |
splice.check_region_beg = splice.tr->beg==exon->beg ? 0 : 1; |
| 3162 |
splice.check_region_end = splice.tr->end==exon->end ? 0 : 1; |
| 3163 |
splice.vcf.alt = rec->d.allele[1]; |
| 3164 |
splice.csq = csq_class; |
| 3165 |
splice_csq(args, &splice, exon->beg, exon->end); |
| 3166 |
if ( splice.csq ) hit = 1; |
| 3167 |
} |
| 3168 |
} |
| 3169 |
if ( !hit && regidx_overlap(args->idx_tscript,chr,beg,end, args->itr) ) |
| 3170 |
{ |
| 3171 |
splice_t splice; |
| 3172 |
splice_init(&splice, rec); |
| 3173 |
|
| 3174 |
while ( regitr_overlap(args->itr) ) |
| 3175 |
{ |
| 3176 |
csq_t csq; |
| 3177 |
memset(&csq, 0, sizeof(csq_t)); |
| 3178 |
gf_tscript_t *tr = splice.tr = regitr_payload(args->itr, gf_tscript_t*); |
| 3179 |
splice.vcf.alt = rec->d.allele[1]; |
| 3180 |
splice.csq = csq_class; |
| 3181 |
int splice_ret = splice_csq(args, &splice, tr->beg, tr->end); |
| 3182 |
if ( splice_ret!=SPLICE_INSIDE && splice_ret!=SPLICE_OVERLAP ) continue; |
| 3183 |
csq.type.type = (GF_is_coding(tr->type) ? CSQ_INTRON : CSQ_NON_CODING) | csq_class; |
| 3184 |
csq.pos = rec->pos; |
| 3185 |
csq.type.biotype = tr->type; |
| 3186 |
csq.type.strand = tr->strand; |
| 3187 |
csq.type.trid = tr->id; |
| 3188 |
csq.type.gene = tr->gene->name; |
| 3189 |
csq_stage(args, &csq, rec); |
| 3190 |
} |
| 3191 |
} |
| 3192 |
} |
| 3193 |
|
| 3194 |
void debug_print_buffers(args_t *args, int pos) |
| 3195 |
{ |
| 3196 |
int i,j; |
| 3197 |
fprintf(stderr,"debug_print_buffers at %d\n", pos); |
| 3198 |
fprintf(stderr,"vbufs:\n"); |
| 3199 |
for (i=0; i<args->vcf_rbuf.n; i++) |
| 3200 |
{ |
| 3201 |
int k = rbuf_kth(&args->vcf_rbuf, i); |
| 3202 |
vbuf_t *vbuf = args->vcf_buf[k]; |
| 3203 |
|
| 3204 |
fprintf(stderr,"\tvbuf %d:\n", i); |
| 3205 |
for (j=0; j<vbuf->n; j++) |
| 3206 |
{ |
| 3207 |
vrec_t *vrec = vbuf->vrec[j]; |
| 3208 |
fprintf(stderr,"\t\t%"PRId64" .. nvcsq=%d\n", (int64_t) vrec->line->pos+1, vrec->nvcsq); |
| 3209 |
} |
| 3210 |
} |
| 3211 |
fprintf(stderr,"pos2vbuf:"); |
| 3212 |
khint_t k; |
| 3213 |
for (k = 0; k < kh_end(args->pos2vbuf); ++k) |
| 3214 |
if (kh_exist(args->pos2vbuf, k)) fprintf(stderr," %d",1+(int)kh_key(args->pos2vbuf, k)); |
| 3215 |
fprintf(stderr,"\n"); |
| 3216 |
fprintf(stderr,"active_tr: %d\n", args->active_tr->ndat); |
| 3217 |
} |
| 3218 |
|
| 3219 |
static void process(args_t *args, bcf1_t **rec_ptr) |
| 3220 |
{ |
| 3221 |
if ( !rec_ptr ) |
| 3222 |
{ |
| 3223 |
hap_flush(args, REGIDX_MAX); |
| 3224 |
vbuf_flush(args, REGIDX_MAX); |
| 3225 |
return; |
| 3226 |
} |
| 3227 |
|
| 3228 |
bcf1_t *rec = *rec_ptr; |
| 3229 |
static int32_t prev_rid = -1, prev_pos = -1; |
| 3230 |
if ( prev_rid!=rec->rid ) |
| 3231 |
{ |
| 3232 |
prev_rid = rec->rid; |
| 3233 |
prev_pos = rec->pos; |
| 3234 |
|
| 3235 |
|
| 3236 |
|
| 3237 |
|
| 3238 |
if ( !faidx_has_seq(args->fai,bcf_seqname(args->hdr,rec)) ) |
| 3239 |
{ |
| 3240 |
if ( !faidx_has_seq(args->fai,drop_chr_prefix(args,bcf_seqname(args->hdr,rec))) && !faidx_has_seq(args->fai,add_chr_prefix(args,bcf_seqname(args->hdr,rec))) ) |
| 3241 |
error("Error: the chromosome \"%s\" is not present in %s\n",bcf_seqname(args->hdr,rec),args->fa_fname); |
| 3242 |
} |
| 3243 |
} |
| 3244 |
if ( prev_pos > rec->pos ) |
| 3245 |
error("Error: The file is not sorted, %s:%d comes before %s:%"PRId64"\n",bcf_seqname(args->hdr,rec),prev_pos+1,bcf_seqname(args->hdr,rec),(int64_t) rec->pos+1); |
| 3246 |
|
| 3247 |
int call_csq = 1; |
| 3248 |
if ( rec->n_allele < 2 ) call_csq = 0; |
| 3249 |
else if ( rec->n_allele==2 && (rec->d.allele[1][0]=='*' || rec->d.allele[1][1]=='*') ) call_csq = 0; |
| 3250 |
else if ( rec->d.allele[1][0]=='<' ) |
| 3251 |
{ |
| 3252 |
if ( strncmp("<INS",rec->d.allele[1], 4) ) call_csq = 0; |
| 3253 |
} |
| 3254 |
if ( call_csq && args->filter ) |
| 3255 |
{ |
| 3256 |
call_csq = filter_test(args->filter, rec, NULL); |
| 3257 |
if ( args->filter_logic==FLT_EXCLUDE ) call_csq = call_csq ? 0 : 1; |
| 3258 |
} |
| 3259 |
if ( !call_csq ) |
| 3260 |
{ |
| 3261 |
if ( !args->out_fh ) return; |
| 3262 |
vbuf_push(args, rec_ptr); |
| 3263 |
hap_flush(args, rec->pos-1); |
| 3264 |
vbuf_flush(args, rec->pos-1); |
| 3265 |
return; |
| 3266 |
} |
| 3267 |
|
| 3268 |
if ( args->rid != rec->rid ) |
| 3269 |
{ |
| 3270 |
hap_flush(args, REGIDX_MAX); |
| 3271 |
vbuf_flush(args, REGIDX_MAX); |
| 3272 |
} |
| 3273 |
args->rid = rec->rid; |
| 3274 |
vbuf_t *vbuf = vbuf_push(args, rec_ptr); |
| 3275 |
|
| 3276 |
if ( rec->d.allele[1][0]!='<' ) |
| 3277 |
{ |
| 3278 |
int hit = args->local_csq ? test_cds_local(args, rec) : test_cds(args, rec, vbuf); |
| 3279 |
hit += test_utr(args, rec); |
| 3280 |
hit += test_splice(args, rec); |
| 3281 |
if ( !hit ) test_tscript(args, rec); |
| 3282 |
} |
| 3283 |
else |
| 3284 |
test_symbolic_alt(args, rec); |
| 3285 |
|
| 3286 |
if ( rec->pos > 0 ) |
| 3287 |
{ |
| 3288 |
hap_flush(args, rec->pos-1); |
| 3289 |
vbuf_flush(args, rec->pos-1); |
| 3290 |
} |
| 3291 |
|
| 3292 |
return; |
| 3293 |
} |
| 3294 |
|
| 3295 |
static const char *usage(void) |
| 3296 |
{ |
| 3297 |
return |
| 3298 |
"\n" |
| 3299 |
"About: Haplotype-aware consequence caller.\n" |
| 3300 |
"Usage: bcftools csq [OPTIONS] in.vcf\n" |
| 3301 |
"\n" |
| 3302 |
"Required options:\n" |
| 3303 |
" -f, --fasta-ref FILE Reference file in fasta format\n" |
| 3304 |
" -g, --gff-annot FILE GFF3 annotation file\n" |
| 3305 |
"\n" |
| 3306 |
"CSQ options:\n" |
| 3307 |
" -B, --trim-protein-seq INT Abbreviate protein-changing predictions to max INT aminoacids\n" |
| 3308 |
" -c, --custom-tag STRING Use this tag instead of the default BCSQ\n" |
| 3309 |
" -l, --local-csq Localized predictions, consider only one VCF record at a time\n" |
| 3310 |
" -n, --ncsq INT Maximum number of per-haplotype consequences to consider for each site [15]\n" |
| 3311 |
" -p, --phase a|m|r|R|s How to handle unphased heterozygous genotypes: [r]\n" |
| 3312 |
" a: take GTs as is, create haplotypes regardless of phase (0/1 -> 0|1)\n" |
| 3313 |
" m: merge *all* GTs into a single haplotype (0/1 -> 1, 1/2 -> 1)\n" |
| 3314 |
" r: require phased GTs, throw an error on unphased het GTs\n" |
| 3315 |
" R: create non-reference haplotypes if possible (0/1 -> 1|1, 1/2 -> 1|2)\n" |
| 3316 |
" s: skip unphased hets\n" |
| 3317 |
"GFF options:\n" |
| 3318 |
" --dump-gff FILE.gz Dump the parsed GFF file (for debugging purposes)\n" |
| 3319 |
" --force Run even if some sanity checks fail\n" |
| 3320 |
" --unify-chr-names 1|0 Automatically unify chromosome naming (e.g. chrX vs X) in GFF, fasta, and VCF [1]\n" |
| 3321 |
"General options:\n" |
| 3322 |
" -e, --exclude EXPR Exclude sites for which the expression is true\n" |
| 3323 |
" -i, --include EXPR Select sites for which the expression is true\n" |
| 3324 |
" --no-version Do not append version and command line to the header\n" |
| 3325 |
" -o, --output FILE Write output to a file [standard output]\n" |
| 3326 |
" -O, --output-type b|u|z|v|t[0-9] b: compressed BCF, u: uncompressed BCF, z: compressed VCF\n" |
| 3327 |
" v: uncompressed VCF, t: plain tab-delimited text output, 0-9: compression level [v]\n" |
| 3328 |
" -r, --regions REGION Restrict to comma-separated list of regions\n" |
| 3329 |
" -R, --regions-file FILE Restrict to regions listed in a file\n" |
| 3330 |
" --regions-overlap 0|1|2 Include if POS in the region (0), record overlaps (1), variant overlaps (2) [1]\n" |
| 3331 |
" -s, --samples -|LIST Samples to include or \"-\" to apply all variants and ignore samples\n" |
| 3332 |
" -S, --samples-file FILE Samples to include\n" |
| 3333 |
" -t, --targets REGION Similar to -r but streams rather than index-jumps\n" |
| 3334 |
" -T, --targets-file FILE Similar to -R but streams rather than index-jumps\n" |
| 3335 |
" --targets-overlap 0|1|2 Include if POS in the region (0), record overlaps (1), variant overlaps (2) [0]\n" |
| 3336 |
" --threads INT Use multithreading with <int> worker threads [0]\n" |
| 3337 |
" -v, --verbose INT Verbosity level 0-2 [1]\n" |
| 3338 |
" -W, --write-index[=FMT] Automatically index the output files [off]\n" |
| 3339 |
"\n" |
| 3340 |
"Example:\n" |
| 3341 |
" bcftools csq -f hs37d5.fa -g Homo_sapiens.GRCh37.87.gff3.gz in.vcf\n" |
| 3342 |
"\n" |
| 3343 |
" # GFF3 annotation files can be downloaded from Ensembl. e.g. for human:\n" |
| 3344 |
" http://ftp.ensembl.org/pub/current_gff3/homo_sapiens/\n" |
| 3345 |
" http://ftp.ensembl.org/pub/grch37/current/gff3/homo_sapiens/\n" |
| 3346 |
"\n"; |
| 3347 |
} |
| 3348 |
|
| 3349 |
int main_csq(int argc, char *argv[]) |
| 3350 |
{ |
| 3351 |
args_t *args = (args_t*) calloc(1,sizeof(args_t)); |
| 3352 |
args->argc = argc; args->argv = argv; |
| 3353 |
args->output_type = FT_VCF; |
| 3354 |
args->bcsq_tag = "BCSQ"; |
| 3355 |
args->ncsq2_max = 2*(16-1); |
| 3356 |
args->verbosity = 1; |
| 3357 |
args->record_cmd_line = 1; |
| 3358 |
args->clevel = -1; |
| 3359 |
args->unify_chr_names = 1; |
| 3360 |
|
| 3361 |
static struct option loptions[] = |
| 3362 |
{ |
| 3363 |
{"force",0,0,1}, |
| 3364 |
{"threads",required_argument,NULL,2}, |
| 3365 |
{"help",0,0,'h'}, |
| 3366 |
{"ncsq",1,0,'n'}, |
| 3367 |
{"brief-predictions",no_argument,0,'b'}, |
| 3368 |
{"trim-protein-seq",required_argument,0,'B'}, |
| 3369 |
{"custom-tag",1,0,'c'}, |
| 3370 |
{"local-csq",0,0,'l'}, |
| 3371 |
{"gff-annot",1,0,'g'}, |
| 3372 |
{"fasta-ref",1,0,'f'}, |
| 3373 |
{"include",1,0,'i'}, |
| 3374 |
{"exclude",1,0,'e'}, |
| 3375 |
{"output",1,0,'o'}, |
| 3376 |
{"output-type",1,NULL,'O'}, |
| 3377 |
{"phase",1,0,'p'}, |
| 3378 |
{"quiet",0,0,'q'}, |
| 3379 |
{"verbose",1,0,'v'}, |
| 3380 |
{"regions",1,0,'r'}, |
| 3381 |
{"regions-file",1,0,'R'}, |
| 3382 |
{"regions-overlap",required_argument,NULL,4}, |
| 3383 |
{"samples",1,0,'s'}, |
| 3384 |
{"samples-file",1,0,'S'}, |
| 3385 |
{"targets",1,0,'t'}, |
| 3386 |
{"targets-file",1,0,'T'}, |
| 3387 |
{"targets-overlap",required_argument,NULL,5}, |
| 3388 |
{"no-version",no_argument,NULL,3}, |
| 3389 |
{"write-index",optional_argument,NULL,'W'}, |
| 3390 |
{"dump-gff",required_argument,NULL,7}, |
| 3391 |
{"unify-chr-names",required_argument,NULL,8}, |
| 3392 |
{0,0,0,0} |
| 3393 |
}; |
| 3394 |
int c, targets_is_file = 0, regions_is_file = 0; |
| 3395 |
int regions_overlap = 1; |
| 3396 |
int targets_overlap = 0; |
| 3397 |
char *targets_list = NULL, *regions_list = NULL, *tmp; |
| 3398 |
while ((c = getopt_long(argc, argv, "?hr:R:t:T:i:e:f:o:O:g:s:S:p:qc:ln:bB:v:W::",loptions,NULL)) >= 0) |
| 3399 |
{ |
| 3400 |
switch (c) |
| 3401 |
{ |
| 3402 |
case 1 : args->force = 1; break; |
| 3403 |
case 2 : |
| 3404 |
args->n_threads = strtol(optarg,&tmp,10); |
| 3405 |
if ( *tmp ) error("Could not parse argument: --threads %s\n", optarg); |
| 3406 |
break; |
| 3407 |
case 3 : args->record_cmd_line = 0; break; |
| 3408 |
case 'b': |
| 3409 |
args->brief_predictions = 1; |
| 3410 |
fprintf(stderr,"Warning: The -b option will be removed in future versions. Please use -B 1 instead.\n"); |
| 3411 |
break; |
| 3412 |
case 'B': |
| 3413 |
args->brief_predictions = strtol(optarg,&tmp,10); |
| 3414 |
if ( *tmp || args->brief_predictions<1 ) error("Could not parse argument: --trim-protein-seq %s\n", optarg); |
| 3415 |
break; |
| 3416 |
case 'l': args->local_csq = 1; break; |
| 3417 |
case 'c': args->bcsq_tag = optarg; break; |
| 3418 |
case 'q': error("Error: the -q option has been deprecated, use -v, --verbose instead.\n"); break; |
| 3419 |
case 'v': |
| 3420 |
args->verbosity = atoi(optarg); |
| 3421 |
if ( args->verbosity<0 || args->verbosity>2 ) error("Error: expected integer 0-2 with -v, --verbose\n"); |
| 3422 |
break; |
| 3423 |
case 'p': |
| 3424 |
switch (optarg[0]) |
| 3425 |
{ |
| 3426 |
case 'a': args->phase = PHASE_AS_IS; break; |
| 3427 |
case 'm': args->phase = PHASE_MERGE; break; |
| 3428 |
case 'r': args->phase = PHASE_REQUIRE; break; |
| 3429 |
case 'R': args->phase = PHASE_NON_REF; break; |
| 3430 |
case 's': args->phase = PHASE_SKIP; break; |
| 3431 |
default: error("The -p code \"%s\" not recognised\n", optarg); |
| 3432 |
} |
| 3433 |
break; |
| 3434 |
case 'f': args->fa_fname = optarg; break; |
| 3435 |
case 'g': args->gff_fname = optarg; break; |
| 3436 |
case 'n': |
| 3437 |
args->ncsq2_max = 2 * atoi(optarg); |
| 3438 |
if ( args->ncsq2_max <= 0 ) error("Expected positive integer with -n, got %s\n", optarg); |
| 3439 |
break; |
| 3440 |
case 'o': args->output_fname = optarg; break; |
| 3441 |
case 'O': |
| 3442 |
switch (optarg[0]) { |
| 3443 |
case 't': args->output_type = FT_TAB_TEXT; break; |
| 3444 |
case 'b': args->output_type = FT_BCF_GZ; break; |
| 3445 |
case 'u': args->output_type = FT_BCF; break; |
| 3446 |
case 'z': args->output_type = FT_VCF_GZ; break; |
| 3447 |
case 'v': args->output_type = FT_VCF; break; |
| 3448 |
default: |
| 3449 |
{ |
| 3450 |
args->clevel = strtol(optarg,&tmp,10); |
| 3451 |
if ( *tmp || args->clevel<0 || args->clevel>9 ) error("The output type \"%s\" not recognised\n", optarg); |
| 3452 |
} |
| 3453 |
} |
| 3454 |
if ( optarg[1] ) |
| 3455 |
{ |
| 3456 |
args->clevel = strtol(optarg+1,&tmp,10); |
| 3457 |
if ( *tmp || args->clevel<0 || args->clevel>9 ) error("Could not parse argument: --output-type %s\n", optarg+1); |
| 3458 |
} |
| 3459 |
break; |
| 3460 |
case 'e': |
| 3461 |
if ( args->filter_str ) error("Error: only one -i or -e expression can be given, and they cannot be combined\n"); |
| 3462 |
args->filter_str = optarg; args->filter_logic |= FLT_EXCLUDE; break; |
| 3463 |
case 'i': |
| 3464 |
if ( args->filter_str ) error("Error: only one -i or -e expression can be given, and they cannot be combined\n"); |
| 3465 |
args->filter_str = optarg; args->filter_logic |= FLT_INCLUDE; break; |
| 3466 |
case 'r': regions_list = optarg; break; |
| 3467 |
case 'R': regions_list = optarg; regions_is_file = 1; break; |
| 3468 |
case 's': args->sample_list = optarg; break; |
| 3469 |
case 'S': args->sample_list = optarg; args->sample_is_file = 1; break; |
| 3470 |
case 't': targets_list = optarg; break; |
| 3471 |
case 'T': targets_list = optarg; targets_is_file = 1; break; |
| 3472 |
case 4 : |
| 3473 |
regions_overlap = parse_overlap_option(optarg); |
| 3474 |
if ( regions_overlap < 0 ) error("Could not parse: --regions-overlap %s\n",optarg); |
| 3475 |
break; |
| 3476 |
case 5 : |
| 3477 |
targets_overlap = parse_overlap_option(optarg); |
| 3478 |
if ( targets_overlap < 0 ) error("Could not parse: --targets-overlap %s\n",optarg); |
| 3479 |
break; |
| 3480 |
case 'W': |
| 3481 |
if (!(args->write_index = write_index_parse(optarg))) |
| 3482 |
error("Unsupported index format '%s'\n", optarg); |
| 3483 |
break; |
| 3484 |
case 7 : args->dump_gff = optarg; break; |
| 3485 |
case 8 : |
| 3486 |
if ( !strcmp(optarg,"0") ) args->unify_chr_names = 0; |
| 3487 |
else if ( !strcmp(optarg,"1") ) args->unify_chr_names = 1; |
| 3488 |
else error("Could not parse: --unify-chr-names %s\n",optarg); |
| 3489 |
break; |
| 3490 |
case 'h': |
| 3491 |
case '?': error("%s",usage()); |
| 3492 |
default: error("The option not recognised: %s\n\n", optarg); break; |
| 3493 |
} |
| 3494 |
} |
| 3495 |
char *fname = NULL; |
| 3496 |
if ( optind==argc ) |
| 3497 |
{ |
| 3498 |
if ( !isatty(fileno((FILE *)stdin)) ) fname = "-"; |
| 3499 |
else error("%s", usage()); |
| 3500 |
} |
| 3501 |
else fname = argv[optind]; |
| 3502 |
if ( argc - optind>1 ) error("%s", usage()); |
| 3503 |
if ( !args->fa_fname ) error("Missing the --fasta-ref option\n"); |
| 3504 |
if ( !args->gff_fname ) error("Missing the --gff option\n"); |
| 3505 |
args->sr = bcf_sr_init(); |
| 3506 |
if ( targets_list ) |
| 3507 |
{ |
| 3508 |
bcf_sr_set_opt(args->sr,BCF_SR_TARGETS_OVERLAP,targets_overlap); |
| 3509 |
if ( bcf_sr_set_targets(args->sr, targets_list, targets_is_file, 0)<0 ) |
| 3510 |
error("Failed to read the targets: %s\n", targets_list); |
| 3511 |
} |
| 3512 |
if ( regions_list ) |
| 3513 |
{ |
| 3514 |
bcf_sr_set_opt(args->sr,BCF_SR_REGIONS_OVERLAP,regions_overlap); |
| 3515 |
if ( bcf_sr_set_regions(args->sr, regions_list, regions_is_file)<0 ) |
| 3516 |
error("Failed to read the regions: %s\n", regions_list); |
| 3517 |
} |
| 3518 |
if ( bcf_sr_set_threads(args->sr, args->n_threads)<0 ) error("Failed to create %d extra threads\n", args->n_threads); |
| 3519 |
if ( !bcf_sr_add_reader(args->sr, fname) ) |
| 3520 |
error("Failed to read from %s: %s\n", !strcmp("-",fname)?"standard input":fname,bcf_sr_strerror(args->sr->errnum)); |
| 3521 |
args->hdr = bcf_sr_get_header(args->sr,0); |
| 3522 |
|
| 3523 |
init_data(args); |
| 3524 |
while ( bcf_sr_next_line(args->sr) ) |
| 3525 |
{ |
| 3526 |
process(args, &args->sr->readers[0].buffer[0]); |
| 3527 |
} |
| 3528 |
process(args,NULL); |
| 3529 |
|
| 3530 |
destroy_data(args); |
| 3531 |
bcf_sr_destroy(args->sr); |
| 3532 |
free(args); |
| 3533 |
return 0; |
| 3534 |
} |
| 3535 |
|