This repository has been archived by the owner on Nov 7, 2018. It is now read-only.
/
chrom.c
287 lines (233 loc) · 6.44 KB
/
chrom.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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
static char rcsid[] = "$Id: chrom.c 44154 2011-08-02 20:52:17Z twu $";
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif
#include "chrom.h"
#include <stdio.h>
#include <stdlib.h> /* For atoi */
#include <string.h>
#include "mem.h"
#include "interval.h"
#ifdef DEBUG
#define debug(x) x
#else
#define debug(x)
#endif
/* This is the order for chrom sort */
typedef enum {PURE_NUMERIC, SEX_CHROMOSOME, MITOCHONDRIAL, NUMERIC_ALPHA, PURE_ALPHA} Chromtype_T;
#ifdef DEBUG
static char *
Chromtype_string (Chromtype_T chromtype) {
switch (chromtype) {
case PURE_NUMERIC: return "numeric";
case SEX_CHROMOSOME: return "sex";
case MITOCHONDRIAL: return "mitochondrial";
case NUMERIC_ALPHA: return "numeric_alpha";
case PURE_ALPHA: return "alpha";
default: abort();
}
}
#endif
#define T Chrom_T
struct T {
unsigned int order; /* order used for sorting (can be universal coordinate) */
bool numericp;
char *string; /* The original string */
unsigned int num; /* The initial numeric part; valid only if numericp == true */
char *alpha; /* The alphabetic part, possibly after the number; valid only if numericp == true */
Chromtype_T chromtype;
};
void
Chrom_free (T *old) {
if ((*old)->numericp == true) {
FREE((*old)->alpha);
}
FREE((*old)->string);
FREE(*old);
return;
}
char *
Chrom_string (T this) {
return this->string;
}
/* Largest number for an unsigned int is 4294967295, which is 10
digits. However, the organism with the most chromosomes is the
Indian fern, with 1260. Hence, more than 4 digits would suggest
a non-chromosomal string. Also, if first digit is '0', treat as
a string. */
T
Chrom_from_string (char *string, char *mitochondrial_string, unsigned int order) {
T new = (T) MALLOC(sizeof(*new));
int ndigits = 0;
char *p;
bool mitochondrial_p = false, sex_p = false;
debug(printf("string is %s\n",string));
new->order = order;
new->string = (char *) CALLOC(strlen(string)+1,sizeof(char));
strcpy(new->string,string);
if (mitochondrial_string != NULL && !strcmp(string,mitochondrial_string)) {
mitochondrial_p = true;
}
if (!strncmp(string,"chr",3)) {
/* Ignore leading chr for sorting purposes */
string += 3;
debug(printf(" => chop chr to yield %s\n",string));
}
if (!strcmp(string,"X")) {
sex_p = true;
} else if (!strcmp(string,"Y")) {
sex_p = true;
} else if (!strcmp(string,"M")) {
mitochondrial_p = true;
} else if (!strcmp(string,"MT")) {
mitochondrial_p = true;
} else if (mitochondrial_string != NULL && !strcmp(string,mitochondrial_string)) {
mitochondrial_p = true;
}
p = string;
while (p != '\0' && *p >= '0' && *p <= '9') {
ndigits++;
p++;
}
debug(printf(" => saw %d digits\n",ndigits));
if (ndigits > 0 && ndigits <= 4 && string[0] != '0') {
new->numericp = true;
new->num = atoi(string);
new->alpha = (char *) CALLOC(strlen(p)+1,sizeof(char));
strcpy(new->alpha,p);
if (mitochondrial_p == true) {
new->chromtype = MITOCHONDRIAL;
} else if (strlen(new->alpha) == 0) {
new->chromtype = PURE_NUMERIC;
} else {
new->chromtype = NUMERIC_ALPHA;
}
debug(printf(" => numeric with value %d and then alpha %s, type %s\n",
new->num,new->alpha,Chromtype_string(new->chromtype)));
} else {
new->numericp = false;
new->num = 0;
new->alpha = (char *) NULL;
if (mitochondrial_p == true) {
new->chromtype = MITOCHONDRIAL;
} else if (sex_p == true) {
new->chromtype = SEX_CHROMOSOME;
} else {
new->chromtype = PURE_ALPHA;
}
debug(printf(" => alphabetical %s, type %s\n",
new->string,Chromtype_string(new->chromtype)));
}
return new;
}
int
Chrom_cmp_alpha (T a, T b) {
return strcmp(a->string,b->string);
}
int
Chrom_cmp_numeric_alpha (T a, T b) {
if (a->numericp == true && b->numericp == false) {
/* 1 and X */
return -1;
} else if (a->numericp == false && b->numericp == true) {
/* X and 1 */
return +1;
} else if (a->numericp == true && b->numericp == true) {
if (a->num < b->num) {
/* 1 and 2U */
return -1;
} else if (a->num > b->num) {
/* 2U and 1 */
return +1;
} else {
return strcmp(a->alpha,b->alpha);
}
} else {
return strcmp(a->string,b->string);
}
}
int
Chrom_cmp_chrom (T a, T b) {
debug(printf("Comparing %s and %s => ",a->string,b->string));
if (a->chromtype < b->chromtype) {
debug(printf("chromtype %d < %d => -1\n",a->chromtype,b->chromtype));
return -1;
} else if (b->chromtype < a->chromtype) {
debug(printf("chromtype %d > %d => +1\n",a->chromtype,b->chromtype));
return +1;
} else if (a->numericp == true && b->numericp == true) {
if (a->num < b->num) {
/* 1 and 2U */
debug(printf("numeric %d < %d => -1\n",a->num,b->num));
return -1;
} else if (a->num > b->num) {
/* 2U and 1 */
debug(printf("numeric %d > %d => +1\n",a->num,b->num));
return +1;
} else {
debug(printf("numeric_alpha %s cmp %s => %d\n",
a->alpha,b->alpha,strcmp(a->alpha,b->alpha)));
return strcmp(a->alpha,b->alpha);
}
} else {
debug(printf("alpha %s cmp %s => %d\n",
a->string,b->string,strcmp(a->string,b->string)));
return strcmp(a->string,b->string);
}
}
/* For use by qsorting an array */
int
Chrom_compare_order (const void *x, const void *y) {
T a = * (T *) x;
T b = * (T *) y;
if (a->order < b->order) {
return -1;
} else if (b->order < a->order) {
return +1;
} else {
return 0;
}
}
/* For use by qsorting an array */
int
Chrom_compare_alpha (const void *x, const void *y) {
T a = * (T *) x;
T b = * (T *) y;
return strcmp(a->string,b->string);
}
/* For use by qsorting an array */
int
Chrom_compare_numeric_alpha (const void *x, const void *y) {
T a = * (T *) x;
T b = * (T *) y;
return Chrom_cmp_numeric_alpha(a,b);
}
/* For use by qsorting an array */
int
Chrom_compare_chrom (const void *x, const void *y) {
T a = * (T *) x;
T b = * (T *) y;
return Chrom_cmp_chrom(a,b);
}
/* For use in table comparisons */
int
Chrom_compare_table (const void *x, const void *y) {
T a = (T) x;
T b = (T) y;
return Chrom_cmp_chrom(a,b);
}
/* This is the X31 hash function */
static unsigned int
string_hash (char *a) {
unsigned int h = 0U;
char *p;
for (p = a; *p != '\0'; p++) {
h = (h << 5) - h + *p;
}
return h;
}
unsigned int
Chrom_hash_table (const void *key) {
T this = (T) key;
return string_hash(this->string);
}