forked from samtools/bcftools
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathallele-length.c
113 lines (99 loc) · 3.43 KB
/
allele-length.c
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
/* 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);
}