| 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 |
#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; |
| 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; |
| 63 |
|
| 64 |
int i; |
| 65 |
if ( *a ) |
| 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 |
|
| 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; |
| 95 |
if ( !vcmp->ndref ) |
| 96 |
{ |
| 97 |
if ( !*a && !*b ) break; |
| 98 |
continue; |
| 99 |
} |
| 100 |
|
| 101 |
|
| 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; |
| 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; |
| 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 |
|