Skip to content

Commit

Permalink
Merge pull request #831 from Genomicsplc/develop
Browse files Browse the repository at this point in the history
Add VariantKey support
  • Loading branch information
pd3 committed Jul 24, 2018
2 parents 3313b52 + c71d3d6 commit 7afcbc9
Show file tree
Hide file tree
Showing 21 changed files with 2,210 additions and 1 deletion.
23 changes: 23 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
Expand Up @@ -723,3 +723,26 @@ Public License instead of this License. But first, please read

-----------------------------------------------------------------------------

LICENSE FOR VariantKey (https://github.com/Genomicsplc/variantkey)

The MIT License

Copyright (c) 2017-2018 GENOMICS plc

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
5 changes: 4 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ PLUGINS_ENABLED = yes
PLUGIN_EXT = .so

OBJS = main.o vcfindex.o tabix.o \
variantkey.o \
vcfstats.o vcfisec.o vcfmerge.o vcfquery.o vcffilter.o filter.o vcfsom.o \
vcfnorm.o vcfgtcheck.o vcfview.o vcfannotate.o vcfroh.o vcfconcat.o \
vcfcall.o mcall.o vcmp.o gvcf.o reheader.o convert.o vcfconvert.o tsv2vcf.o \
Expand Down Expand Up @@ -176,7 +177,8 @@ plugins: $(PLUGINS)

bcftools_h = bcftools.h $(htslib_hts_defs_h) $(htslib_vcf_h)
call_h = call.h $(htslib_vcf_h) $(htslib_synced_bcf_reader_h) vcmp.h
convert_h = convert.h $(htslib_vcf_h)
variantkey_h = variantkey.h
convert_h = convert.h $(htslib_vcf_h) $(variantkey_h)
tsv2vcf_h = tsv2vcf.h $(htslib_vcf_h)
filter_h = filter.h $(htslib_vcf_h)
gvcf_h = gvcf.h $(bcftools_h)
Expand Down Expand Up @@ -210,6 +212,7 @@ vcfview.o: vcfview.c $(htslib_vcf_h) $(htslib_synced_bcf_reader_h) $(htslib_vcfu
reheader.o: reheader.c $(htslib_vcf_h) $(htslib_bgzf_h) $(htslib_tbx_h) $(htslib_kseq_h) $(htslib_thread_pool_h) $(bcftools_h) $(khash_str2str_h)
tabix.o: tabix.c $(htslib_bgzf_h) $(htslib_tbx_h)
ccall.o: ccall.c $(htslib_kfunc_h) $(call_h) kmin.h $(prob1_h)
variantkey.o: variantkey.c $(variantkey_h)
convert.o: convert.c $(htslib_vcf_h) $(htslib_synced_bcf_reader_h) $(htslib_vcfutils_h) $(bcftools_h) $(convert_h)
tsv2vcf.o: tsv2vcf.c $(tsv2vcf_h)
em.o: em.c $(htslib_vcf_h) kmin.h $(call_h)
Expand Down
30 changes: 30 additions & 0 deletions convert.c
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,9 @@ THE SOFTWARE. */
#include <htslib/vcf.h>
#include <htslib/synced_bcf_reader.h>
#include <htslib/vcfutils.h>
#include <inttypes.h>
#include "bcftools.h"
#include "variantkey.h"
#include "convert.h"

#define T_CHROM 1
Expand Down Expand Up @@ -67,6 +69,8 @@ THE SOFTWARE. */
#define T_END 27
#define T_POS0 28
#define T_END0 29
#define T_RSX 30 // RSID HEX
#define T_VKX 31 // VARIANTKEY HEX

typedef struct _fmt_t
{
Expand Down Expand Up @@ -1020,6 +1024,26 @@ static void process_gt_to_hap2(convert_t *convert, bcf1_t *line, fmt_t *fmt, int
str->s[--str->l] = 0; // delete the last space
}

static void process_rsid_hex(convert_t *convert, bcf1_t *line, fmt_t *fmt, int isample, kstring_t *str)
{
char *ptr = line->d.id;
ptr += 2; // remove 'rs'
ksprintf(str, "%08"PRIx32"", (uint32_t)strtoul(ptr, NULL, 10));
}

static void process_variantkey_hex(convert_t *convert, bcf1_t *line, fmt_t *fmt, int isample, kstring_t *str)
{
uint64_t vk = variantkey(
convert->header->id[BCF_DT_CTG][line->rid].key,
strlen(convert->header->id[BCF_DT_CTG][line->rid].key),
line->pos,
line->d.allele[0],
strlen(line->d.allele[0]),
line->d.allele[1],
strlen(line->d.allele[1]));
ksprintf(str, "%016"PRIx64"", vk);
}

static fmt_t *register_tag(convert_t *convert, int type, char *key, int is_gtf)
{
convert->nfmt++;
Expand Down Expand Up @@ -1054,6 +1078,8 @@ static fmt_t *register_tag(convert_t *convert, int type, char *key, int is_gtf)
else if ( !strcmp("QUAL",key) ) { fmt->type = T_QUAL; }
else if ( !strcmp("FILTER",key) ) { fmt->type = T_FILTER; }
else if ( !strcmp("_CHROM_POS_ID",key) ) { fmt->type = T_CHROM_POS_ID; }
else if ( !strcmp("RSX",key) ) { fmt->type = T_RSX; }
else if ( !strcmp("VKX",key) ) { fmt->type = T_VKX; }
else if ( id>=0 && bcf_hdr_idinfo_exists(convert->header,BCF_HL_INFO,id) )
{
fmt->type = T_INFO;
Expand Down Expand Up @@ -1093,6 +1119,8 @@ static fmt_t *register_tag(convert_t *convert, int type, char *key, int is_gtf)
case T_GT_TO_HAP2: fmt->handler = &process_gt_to_hap2; convert->max_unpack |= BCF_UN_FMT; break;
case T_TBCSQ: fmt->handler = &process_tbcsq; fmt->destroy = &destroy_tbcsq; convert->max_unpack |= BCF_UN_FMT; break;
case T_LINE: fmt->handler = &process_line; convert->max_unpack |= BCF_UN_FMT; break;
case T_RSX: fmt->handler = &process_rsid_hex; break;
case T_VKX: fmt->handler = &process_variantkey_hex; break;
default: error("TODO: handler for type %d\n", fmt->type);
}
if ( key && fmt->type==T_INFO )
Expand Down Expand Up @@ -1187,6 +1215,8 @@ static char *parse_tag(convert_t *convert, char *p, int is_gtf)
else if ( !strcmp(str.s, "_GP_TO_PROB3") ) register_tag(convert, T_GP_TO_PROB3, str.s, is_gtf);
else if ( !strcmp(str.s, "_GT_TO_HAP") ) register_tag(convert, T_GT_TO_HAP, str.s, is_gtf);
else if ( !strcmp(str.s, "_GT_TO_HAP2") ) register_tag(convert, T_GT_TO_HAP2, str.s, is_gtf);
else if ( !strcmp(str.s, "RSX") ) register_tag(convert, T_RSX, str.s, is_gtf);
else if ( !strcmp(str.s, "VKX") ) register_tag(convert, T_VKX, str.s, is_gtf);
else if ( !strcmp(str.s, "INFO") )
{
if ( *q!='/' ) error("Could not parse format string: %s\n", convert->format_str);
Expand Down
15 changes: 15 additions & 0 deletions doc/bcftools.1
Original file line number Diff line number Diff line change
Expand Up @@ -3373,6 +3373,21 @@ convert between similar tags, such as GL and GP
.RS 4
calculate phase switch rate in trio samples, children samples must have phased GTs\&.
.RE
.PP
\fBadd-variantkey\fR
.RS 4
add VariantKey INFO fields VKX and RSX\&.
.RE
.PP
\fBvariantkey-hex\fR
.RS 4
generate unsorted VariantKey-RSid index files in hexadecimal format.\&.
.RE
.PP
\fBallele-length\fR
.RS 4
count the frequency of the length of REF, ALT and REF+ALT\&.
.RE
.RE
.sp
.it 1 an-trap
Expand Down
12 changes: 12 additions & 0 deletions doc/bcftools.html
Original file line number Diff line number Diff line change
Expand Up @@ -2060,6 +2060,18 @@
<span class="strong"><strong>trio-switch-rate</strong></span>
</span></dt><dd>
calculate phase switch rate in trio samples, children samples must have phased GTs.
</dd><dt><span class="term">
<span class="strong"><strong>add-variantkey</strong></span>
</span></dt><dd>
add VariantKey INFO fields VKX and RSX
</dd><dt><span class="term">
<span class="strong"><strong>variantkey-hex</strong></span>
</span></dt><dd>
generate unsorted VariantKey-RSid index files in hexadecimal format
</dd><dt><span class="term">
<span class="strong"><strong>allele-length</strong></span>
</span></dt><dd>
Count the frequency of the length of REF, ALT and REF+ALT
</dd></dl></div></div><div class="refsect3" title="Examples:"><a id="_examples_3"></a><h4>Examples:</h4><pre class="screen"># List options common to all plugins
bcftools plugin

Expand Down
9 changes: 9 additions & 0 deletions doc/bcftools.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2058,6 +2058,15 @@ By default, appropriate system directories are searched for installed plugins.
*trio-switch-rate*::
calculate phase switch rate in trio samples, children samples must have phased GTs.

*add-variantkey*::
add VariantKey INFO fields VKX and RSX

*variantkey-hex*::
generate unsorted VariantKey-RSid index files in hexadecimal format

*allele-length*::
count the frequency of the length of REF, ALT and REF+ALT


==== Examples:

Expand Down
86 changes: 86 additions & 0 deletions plugins/add-variantkey.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
/* plugins/add-variantkey.c -- add VariantKey INFO field.
Copyright (C) 2017-2018 GENOMICS plc.
Author: Nicola Asuni <nicola.asuni@genomicsplc.com>
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
DEALINGS IN THE SOFTWARE. */

#include <stdio.h>
#include <stdlib.h>
#include <htslib/hts.h>
#include <htslib/vcf.h>
#include <htslib/vcfutils.h>
#include <inttypes.h>
#include "../variantkey.h"

bcf_hdr_t *in_hdr, *out_hdr;

const char *about(void)
{
return "Add VariantKey INFO fields VKX and RSX.\n";
}

const char *usage(void)
{
return
"\n"
"About: Add VKX and RSX columns.\n"
"Usage: bcftools +add-variantkey [General Options] \n"
"Options:\n"
" run \"bcftools plugin\" for a list of common options\n"
"\n"
"Example:\n"
" bcftools +add-variantkey in.vcf\n"
"\n";
}

int init(int argc, char **argv, bcf_hdr_t *in, bcf_hdr_t *out)
{
in_hdr = in;
out_hdr = out;
bcf_hdr_append(out_hdr, "##INFO=<ID=VKX,Number=1,Type=String,Description=\"Hexadecimal representation of 64 bit VariantKey\">");
bcf_hdr_append(out_hdr, "##INFO=<ID=RSX,Number=1,Type=String,Description=\"Hexadecimal representation of ID minus the 'rs' prefix (32bit)\">");
return 0;
}

bcf1_t *process(bcf1_t *rec)
{
uint64_t vk = variantkey(
in_hdr->id[BCF_DT_CTG][rec->rid].key,
strlen(in_hdr->id[BCF_DT_CTG][rec->rid].key),
rec->pos,
rec->d.allele[0],
strlen(rec->d.allele[0]),
rec->d.allele[1],
strlen(rec->d.allele[1]));
char vs[17];
variantkey_hex(vk, vs);
bcf_update_info_string(out_hdr, rec, "VKX", vs);
char rsid[9];
char *ptr = rec->d.id;
ptr += 2; // remove 'rs'
sprintf(rsid, "%08" PRIx32, (uint32_t)strtoul(ptr, NULL, 10));
bcf_update_info_string(out_hdr, rec, "RSX", rsid);
return rec;
}

void destroy(void)
{
}
113 changes: 113 additions & 0 deletions plugins/allele-length.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
/* plugins/allele-length.c -- Calculate stats about the length of alleles
Copyright (C) 2017-2018 GENOMICS plc.
Author: Nicola Asuni <nicola.asuni@genomicsplc.com>
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
DEALINGS IN THE SOFTWARE. */

#include <stdio.h>
#include <stdlib.h>
#include <htslib/vcf.h>
#include <inttypes.h>

#define MAXLEN 512

static uint64_t numvar;
static uint64_t numxvar;
static uint64_t reflen[MAXLEN];
static uint64_t altlen[MAXLEN];
static uint64_t refaltlen[MAXLEN];
static uint64_t xrefaltlen[MAXLEN];

const char *about(void)
{
return "Count the frequency of the length of REF, ALT and REF+ALT\n";
}

const char *usage(void)
{
return
"\n"
"About: Count the frequency of the length of alleles.\n"
"Usage: bcftools +allele-length [General Options] \n"
"Options:\n"
" run \"bcftools plugin\" for a list of common options\n"
"\n"
"Example:\n"
" bcftools +allele-length in.vcf\n"
"\n";
}

// return 0 if the string contains characters other than standard ACGT base letters
int contain_non_base(const char *str)
{
int c;
while ((c = *str++))
{
if ((c != 'A') && (c != 'a') && (c != 'C') && (c != 'c') && (c != 'G') && (c != 'g') && (c != 'T') && (c != 't'))
{
return 1;
}
}
return 0;
}

// Called once at startup, allows to initialize local variables.
// Return 1 to suppress VCF/BCF header from printing, 0 otherwise.
int init(int argc, char **argv, bcf_hdr_t *in, bcf_hdr_t *out)
{
numvar = 0;
int i = 0;
for(i = 0; i < MAXLEN; i++) {
reflen[i] = 0;
altlen[i] = 0;
refaltlen[i] = 0;
xrefaltlen[i] = 0;
}
return 1;
}

// Called for each VCF record. Return rec to output the line or NULL to suppress output.
bcf1_t *process(bcf1_t *rec)
{
int rl = strlen(rec->d.allele[0]);
int al = strlen(rec->d.allele[1]);
reflen[rl] += 1;
altlen[al] += 1;
refaltlen[(rl + al)] += 1;
if ((contain_non_base(rec->d.allele[0])) || (contain_non_base(rec->d.allele[1])))
{
xrefaltlen[(rl + al)] += 1;
numxvar++;
}
numvar++;
return NULL;
}

// Print final output
void destroy(void)
{
int i = 0;
printf("LENGTH\tREF\tALT\tREF+ALT\tREF+ALT WITH NON-BASE NUCLEOTIDES\n");
for(i = 0; i < MAXLEN; i++) {
printf("%d\t%"PRIu64"\t%"PRIu64"\t%"PRIu64"\t%"PRIu64"\n", i, reflen[i], altlen[i], refaltlen[i], xrefaltlen[i]);
}
printf("\t\t\t%"PRIu64"\t%"PRIu64"\n", numvar, numxvar);
}
Loading

0 comments on commit 7afcbc9

Please sign in to comment.