Debian Codegraph @

bcftools/vcmp.c.pysam.c

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