Debian Codegraph @

bcftools/vcmp.c

raw symbols SCIP
1 /* vcmp.c -- reference allele utility functions.
2
3 Copyright (C) 2013-2015, 2018 Genome Research Ltd.
4
5 Author: Petr Danecek <[email protected]>
6
7 Permission is hereby granted, free of charge, to any person obtaining a copy
8 of this software and associated documentation files (the "Software"), to deal
9 in the Software without restriction, including without limitation the rights
10 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11 copies of the Software, and to permit persons to whom the Software is
12 furnished to do so, subject to the following conditions:
13
14 The above copyright notice and this permission notice shall be included in
15 all copies or substantial portions of the Software.
16
17 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
23 THE SOFTWARE. */
24
25 #include <stdio.h>
26 #include <string.h>
27 #include <stdlib.h>
28 #include <htslib/hts.h>
29 #include <htslib/vcf.h>
30 #include <ctype.h>
31 #include "vcmp.h"
32
33 struct _vcmp_t
34 {
35 char *dref;
36 int ndref, mdref; // ndref: positive when ref1 longer, negative when ref2 is longer
37 int nmatch;
38 int *map, mmap, nmap;
39 int *map_dip, mmap_dip, nmap_dip;
40 };
41
42 vcmp_t *vcmp_init()
43 {
44 return (vcmp_t*)calloc(1,sizeof(vcmp_t));
45 }
46
47 void vcmp_destroy(vcmp_t *vcmp)
48 {
49 free(vcmp->map_dip);
50 free(vcmp->map);
51 free(vcmp->dref);
52 free(vcmp);
53 }
54
55 int vcmp_set_ref(vcmp_t *vcmp, char *ref1, char *ref2)
56 {
57 vcmp->ndref = 0;
58
59 char *a = ref1, *b = ref2;
60 while ( *a && *b && toupper(*a)==toupper(*b) ) { a++; b++; }
61 if ( !*a && !*b ) return 0;
62 if ( *a && *b ) return -1; // refs not compatible
63
64 int i;
65 if ( *a ) // ref1 is longer
66 {
67 vcmp->nmatch = b-ref2;
68 while ( *a ) a++;
69 vcmp->ndref = (a-ref1) - vcmp->nmatch;
70 hts_expand(char,vcmp->ndref+1,vcmp->mdref,vcmp->dref);
71 for (i=0; i<vcmp->ndref; i++) vcmp->dref[i] = toupper(ref1[vcmp->nmatch+i]);
72 vcmp->dref[vcmp->ndref] = 0;
73 return 0;
74 }
75
76 // ref2 is longer
77 vcmp->nmatch = a-ref1;
78 while ( *b ) b++;
79 vcmp->ndref = (b-ref2) - vcmp->nmatch;
80 hts_expand(char,vcmp->ndref+1,vcmp->mdref,vcmp->dref);
81 for (i=0; i<vcmp->ndref; i++) vcmp->dref[i] = toupper(ref2[vcmp->nmatch+i]);
82 vcmp->dref[vcmp->ndref] = 0;
83 vcmp->ndref *= -1;
84 return 0;
85 }
86
87 int vcmp_find_allele(vcmp_t *vcmp, char **als1, int nals1, char *al2)
88 {
89 int i, j;
90 for (i=0; i<nals1; i++)
91 {
92 char *a = als1[i], *b = al2;
93 while ( *a && *b && toupper(*a)==toupper(*b) ) { a++; b++; }
94 if ( *a && *b ) continue; // mismatch
95 if ( !vcmp->ndref )
96 {
97 if ( !*a && !*b ) break; // found
98 continue;
99 }
100
101 // the prefixes match
102 if ( *a )
103 {
104 if ( vcmp->ndref<0 ) continue;
105 for (j=0; j<vcmp->ndref; j++)
106 if ( !a[j] || toupper(a[j])!=vcmp->dref[j] ) break;
107 if ( j!=vcmp->ndref || a[j] ) continue;
108 break; // found
109 }
110
111 if ( vcmp->ndref>0 ) continue;
112 for (j=0; j<-vcmp->ndref; j++)
113 if ( !b[j] || toupper(b[j])!=vcmp->dref[j] ) break;
114 if ( j!=-vcmp->ndref || b[j] ) continue;
115 break; // found
116 }
117 if (i==nals1) return -1;
118 return i;
119 }
120
121
122 int *vcmp_map_ARvalues(vcmp_t *vcmp, int n, int nals1, char **als1, int nals2, char **als2)
123 {
124 if ( vcmp_set_ref(vcmp,als1[0],als2[0]) < 0 ) return NULL;
125
126 vcmp->nmap = n;
127 hts_expand(int, vcmp->nmap, vcmp->mmap, vcmp->map);
128
129 int i, ifrom = n==nals2 ? 0 : 1;
130 for (i=ifrom; i<nals2; i++)
131 {
132 vcmp->map[i-ifrom] = vcmp_find_allele(vcmp, als1+ifrom, nals1-ifrom, als2[i]);
133 }
134 return vcmp->map;
135 }
136
137 int *vcmp_map_dipGvalues(vcmp_t *vcmp, int *nmap)
138 {
139 vcmp->nmap_dip = vcmp->nmap*(vcmp->nmap+1)/2;
140 hts_expand(int, vcmp->nmap_dip, vcmp->mmap_dip, vcmp->map_dip);
141
142 int i, j, k = 0;
143 for (i=0; i<vcmp->nmap; i++)
144 {
145 for (j=0; j<=i; j++)
146 {
147 vcmp->map_dip[k] = vcmp->map[i]>=0 && vcmp->map[j]>=0 ? bcf_alleles2gt(vcmp->map[i],vcmp->map[j]) : -1;
148 k++;
149 }
150 }
151 *nmap = k;
152 return vcmp->map_dip;
153 }
154
155