| 1 |
#include "bcftools.pysam.h" |
| 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 |
#include <stdio.h> |
| 28 |
#include <unistd.h> |
| 29 |
#include <getopt.h> |
| 30 |
#include <assert.h> |
| 31 |
#include <ctype.h> |
| 32 |
#include <string.h> |
| 33 |
#include <errno.h> |
| 34 |
#include <sys/stat.h> |
| 35 |
#include <sys/types.h> |
| 36 |
#include <math.h> |
| 37 |
#include <time.h> |
| 38 |
#include <htslib/vcf.h> |
| 39 |
#include <htslib/synced_bcf_reader.h> |
| 40 |
#include <htslib/vcfutils.h> |
| 41 |
#include <htslib/hts_os.h> |
| 42 |
#include <htslib/hts_defs.h> |
| 43 |
#include <inttypes.h> |
| 44 |
#include "bcftools.h" |
| 45 |
|
| 46 |
#define SOM_TRAIN 1 |
| 47 |
#define SOM_CLASSIFY 2 |
| 48 |
|
| 49 |
typedef struct |
| 50 |
{ |
| 51 |
int ndim; |
| 52 |
int nbin; |
| 53 |
int size; |
| 54 |
int kdim; |
| 55 |
int nt, t; |
| 56 |
double *w, *c; |
| 57 |
double learn; |
| 58 |
double bmu_th; |
| 59 |
int *a_idx, *b_idx; |
| 60 |
double *div; |
| 61 |
} |
| 62 |
som_t; |
| 63 |
|
| 64 |
typedef struct |
| 65 |
{ |
| 66 |
|
| 67 |
double bmu_th, learn; |
| 68 |
int ndim, nbin, ntrain, t; |
| 69 |
int nfold; |
| 70 |
som_t **som; |
| 71 |
|
| 72 |
|
| 73 |
htsFile *file; |
| 74 |
kstring_t str; |
| 75 |
int dclass, mvals; |
| 76 |
double *vals; |
| 77 |
|
| 78 |
|
| 79 |
double *train_dat; |
| 80 |
int *train_class, mtrain_class, mtrain_dat; |
| 81 |
|
| 82 |
int rand_seed, good_class, bad_class; |
| 83 |
char **argv, *fname, *prefix; |
| 84 |
int argc, action, train_bad, merge; |
| 85 |
} |
| 86 |
args_t; |
| 87 |
|
| 88 |
static void usage(void); |
| 89 |
FILE *open_file(char **fname, const char *mode, const char *fmt, ...) HTS_FORMAT(HTS_PRINTF_FMT, 3, 4); |
| 90 |
|
| 91 |
char * HTS_FORMAT(HTS_PRINTF_FMT, 1, 2) msprintf(const char *fmt, ...) |
| 92 |
{ |
| 93 |
va_list ap; |
| 94 |
va_start(ap, fmt); |
| 95 |
int n = vsnprintf(NULL, 0, fmt, ap) + 2; |
| 96 |
va_end(ap); |
| 97 |
|
| 98 |
char *str = (char*)malloc(n); |
| 99 |
va_start(ap, fmt); |
| 100 |
vsnprintf(str, n, fmt, ap); |
| 101 |
va_end(ap); |
| 102 |
|
| 103 |
return str; |
| 104 |
} |
| 105 |
|
| 106 |
|
| 107 |
|
| 108 |
|
| 109 |
|
| 110 |
|
| 111 |
|
| 112 |
|
| 113 |
|
| 114 |
|
| 115 |
static inline char *column_next(char *start, char delim) |
| 116 |
{ |
| 117 |
char *end = start; |
| 118 |
while (*end && *end!=delim) end++; |
| 119 |
return end; |
| 120 |
} |
| 121 |
|
| 122 |
|
| 123 |
|
| 124 |
|
| 125 |
int annots_reader_next(args_t *args) |
| 126 |
{ |
| 127 |
args->str.l = 0; |
| 128 |
if ( hts_getline(args->file,'\n',&args->str)<=0 ) return 0; |
| 129 |
|
| 130 |
char *t, *line = args->str.s; |
| 131 |
|
| 132 |
if ( !args->mvals ) |
| 133 |
{ |
| 134 |
t = line; |
| 135 |
while ( *t ) |
| 136 |
{ |
| 137 |
if ( *t=='\t' ) args->mvals++; |
| 138 |
t++; |
| 139 |
} |
| 140 |
args->vals = (double*) malloc(args->mvals*sizeof(double)); |
| 141 |
} |
| 142 |
|
| 143 |
|
| 144 |
args->dclass = atoi(line); |
| 145 |
t = column_next(line, '\t'); |
| 146 |
|
| 147 |
|
| 148 |
int i; |
| 149 |
for (i=0; i<args->mvals; i++) |
| 150 |
{ |
| 151 |
if ( !*t ) error("Could not parse %d-th data field: is the line truncated?\nThe line was: [%s]\n",i+2,line); |
| 152 |
args->vals[i] = atof(++t); |
| 153 |
t = column_next(t,'\t'); |
| 154 |
} |
| 155 |
return 1; |
| 156 |
} |
| 157 |
void annots_reader_reset(args_t *args) |
| 158 |
{ |
| 159 |
if ( args->file ) hts_close(args->file); |
| 160 |
if ( !args->fname ) error("annots_reader_reset: no fname\n"); |
| 161 |
args->file = hts_open(args->fname, "r"); |
| 162 |
} |
| 163 |
void annots_reader_close(args_t *args) |
| 164 |
{ |
| 165 |
hts_close(args->file); |
| 166 |
} |
| 167 |
|
| 168 |
static void som_write_map(char *prefix, som_t **som, int nsom) |
| 169 |
{ |
| 170 |
FILE *fp = open_file(NULL,"w","%s.som",prefix); |
| 171 |
size_t nw; |
| 172 |
if ( (nw=fwrite("SOMv1",5,1,fp))!=5 ) error("Failed to write 5 bytes\n"); |
| 173 |
if ( (nw=fwrite(&nsom,sizeof(int),1,fp))!=sizeof(int) ) error("Failed to write %zu bytes\n",sizeof(int)); |
| 174 |
int i; |
| 175 |
for (i=0; i<nsom; i++) |
| 176 |
{ |
| 177 |
if ( (nw=fwrite(&som[i]->size,sizeof(int),1,fp))!=sizeof(int) ) error("Failed to write %zu bytes\n",sizeof(int)); |
| 178 |
if ( (nw=fwrite(&som[i]->kdim,sizeof(int),1,fp))!=sizeof(int) ) error("Failed to write %zu bytes\n",sizeof(int)); |
| 179 |
if ( (nw=fwrite(som[i]->w,sizeof(double),som[i]->size*som[i]->kdim,fp))!=sizeof(double)*som[i]->size*som[i]->kdim ) error("Failed to write %zu bytes\n",sizeof(double)*som[i]->size*som[i]->kdim); |
| 180 |
if ( (nw=fwrite(som[i]->c,sizeof(double),som[i]->size,fp))!=sizeof(double)*som[i]->size ) error("Failed to write %zu bytes\n",sizeof(double)*som[i]->size); |
| 181 |
} |
| 182 |
if ( fclose(fp) ) error("%s.som: fclose failed\n",prefix); |
| 183 |
} |
| 184 |
static som_t** som_load_map(char *prefix, int *nsom) |
| 185 |
{ |
| 186 |
FILE *fp = open_file(NULL,"r","%s.som",prefix); |
| 187 |
char buf[5]; |
| 188 |
if ( fread(buf,5,1,fp)!=1 || strncmp(buf,"SOMv1",5) ) error("Could not parse %s.som\n", prefix); |
| 189 |
|
| 190 |
if ( fread(nsom,sizeof(int),1,fp)!=1 ) error("Could not read %s.som\n", prefix); |
| 191 |
som_t **som = (som_t**)malloc(*nsom*sizeof(som_t*)); |
| 192 |
|
| 193 |
int i; |
| 194 |
for (i=0; i<*nsom; i++) |
| 195 |
{ |
| 196 |
som[i] = (som_t*) calloc(1,sizeof(som_t)); |
| 197 |
if ( fread(&som[i]->size,sizeof(int),1,fp) != 1 ) error("Could not read %s.som\n", prefix); |
| 198 |
if ( fread(&som[i]->kdim,sizeof(int),1,fp) != 1 ) error("Could not read %s.som\n", prefix); |
| 199 |
som[i]->w = (double*) malloc(sizeof(double)*som[i]->size*som[i]->kdim); |
| 200 |
som[i]->c = (double*) malloc(sizeof(double)*som[i]->size); |
| 201 |
if ( fread(som[i]->w,sizeof(double),som[i]->size*som[i]->kdim,fp) != som[i]->size*som[i]->kdim ) error("Could not read from %s.som\n", prefix); |
| 202 |
if ( fread(som[i]->c,sizeof(double),som[i]->size,fp) != som[i]->size ) error("Could not read from %s.som\n", prefix); |
| 203 |
} |
| 204 |
if ( fclose(fp) ) error("%s.som: fclose failed\n",prefix); |
| 205 |
return som; |
| 206 |
} |
| 207 |
static void som_create_plot(som_t *som, char *prefix) |
| 208 |
{ |
| 209 |
if ( som->ndim!=2 ) return; |
| 210 |
|
| 211 |
char *fname; |
| 212 |
FILE *fp = open_file(&fname,"w","%s.py",prefix); |
| 213 |
fprintf(fp, |
| 214 |
"import matplotlib as mpl\n" |
| 215 |
"mpl.use('Agg')\n" |
| 216 |
"import matplotlib.pyplot as plt\n" |
| 217 |
"\n" |
| 218 |
"dat = [\n" |
| 219 |
); |
| 220 |
int i,j; |
| 221 |
double *val = som->c; |
| 222 |
for (i=0; i<som->nbin; i++) |
| 223 |
{ |
| 224 |
fprintf(fp,"["); |
| 225 |
for (j=0; j<som->nbin; j++) |
| 226 |
{ |
| 227 |
if ( j>0 ) fprintf(fp,","); |
| 228 |
fprintf(fp,"%e", *val); |
| 229 |
val++; |
| 230 |
} |
| 231 |
fprintf(fp,"],\n"); |
| 232 |
} |
| 233 |
fprintf(fp, |
| 234 |
"]\n" |
| 235 |
"fig = plt.figure()\n" |
| 236 |
"ax1 = plt.subplot(111)\n" |
| 237 |
"im1 = ax1.imshow(dat)\n" |
| 238 |
"fig.colorbar(im1)\n" |
| 239 |
"plt.savefig('%s.png')\n" |
| 240 |
"plt.close()\n" |
| 241 |
"\n", prefix |
| 242 |
); |
| 243 |
fclose(fp); |
| 244 |
free(fname); |
| 245 |
} |
| 246 |
|
| 247 |
static inline int som_find_bmu(som_t *som, double *vec, double *dist) |
| 248 |
{ |
| 249 |
double *ptr = som->w; |
| 250 |
double min_dist = HUGE_VAL; |
| 251 |
int min_idx = 0; |
| 252 |
|
| 253 |
int i, k; |
| 254 |
for (i=0; i<som->size; i++) |
| 255 |
{ |
| 256 |
double dist = 0; |
| 257 |
for (k=0; k<som->kdim; k++) |
| 258 |
dist += (vec[k] - ptr[k]) * (vec[k] - ptr[k]); |
| 259 |
if ( dist < min_dist ) |
| 260 |
{ |
| 261 |
min_dist = dist; |
| 262 |
min_idx = i; |
| 263 |
} |
| 264 |
ptr += som->kdim; |
| 265 |
} |
| 266 |
|
| 267 |
if ( dist ) *dist = min_dist; |
| 268 |
return min_idx; |
| 269 |
} |
| 270 |
static inline double som_get_score(som_t *som, double *vec, double bmu_th) |
| 271 |
{ |
| 272 |
double *ptr = som->w; |
| 273 |
double min_dist = HUGE_VAL; |
| 274 |
|
| 275 |
int i, k; |
| 276 |
for (i=0; i<som->size; i++) |
| 277 |
{ |
| 278 |
if ( som->c[i] >= bmu_th ) |
| 279 |
{ |
| 280 |
double dist = 0; |
| 281 |
for (k=0; k<som->kdim; k++) |
| 282 |
dist += (vec[k] - ptr[k]) * (vec[k] - ptr[k]); |
| 283 |
if ( dist < min_dist ) min_dist = dist; |
| 284 |
} |
| 285 |
ptr += som->kdim; |
| 286 |
} |
| 287 |
return sqrt(min_dist); |
| 288 |
} |
| 289 |
|
| 290 |
static inline void som_idx_to_ndim(som_t *som, int idx, int *ndim) |
| 291 |
{ |
| 292 |
int i; |
| 293 |
double sub = 0; |
| 294 |
|
| 295 |
ndim[0] = idx/som->div[0]; |
| 296 |
for (i=1; i<som->ndim; i++) |
| 297 |
{ |
| 298 |
sub += ndim[i-1] * som->div[i-1]; |
| 299 |
ndim[i] = (idx - sub)/som->div[i]; |
| 300 |
} |
| 301 |
} |
| 302 |
static void som_train_site(som_t *som, double *vec, int update_counts) |
| 303 |
{ |
| 304 |
|
| 305 |
som->t++; |
| 306 |
double dt = exp(-som->t/som->nt); |
| 307 |
double learning_rate = som->learn * dt; |
| 308 |
double radius = som->nbin * dt; radius *= radius; |
| 309 |
|
| 310 |
|
| 311 |
int min_idx = som_find_bmu(som, vec, NULL); |
| 312 |
som_idx_to_ndim(som, min_idx, som->a_idx); |
| 313 |
|
| 314 |
|
| 315 |
|
| 316 |
double *ptr = som->w; |
| 317 |
double *cnt = som->c; |
| 318 |
int i, j, k; |
| 319 |
for (i=0; i<som->size; i++) |
| 320 |
{ |
| 321 |
som_idx_to_ndim(som, i, som->b_idx); |
| 322 |
double dist = 0; |
| 323 |
for (j=0; j<som->ndim; j++) |
| 324 |
dist += (som->a_idx[j] - som->b_idx[j]) * (som->a_idx[j] - som->b_idx[j]); |
| 325 |
if ( dist <= radius ) |
| 326 |
{ |
| 327 |
double influence = exp(-dist*dist*0.5/radius) * learning_rate; |
| 328 |
for (k=0; k<som->kdim; k++) |
| 329 |
ptr[k] += influence * (vec[k] - ptr[k]); |
| 330 |
|
| 331 |
|
| 332 |
|
| 333 |
if ( update_counts ) *cnt += influence; |
| 334 |
} |
| 335 |
ptr += som->kdim; |
| 336 |
cnt++; |
| 337 |
} |
| 338 |
} |
| 339 |
static void som_norm_counts(som_t *som) |
| 340 |
{ |
| 341 |
int i; |
| 342 |
double max = 0; |
| 343 |
for (i=0; i<som->size; i++) |
| 344 |
if ( max < som->c[i] ) max = som->c[i]; |
| 345 |
for (i=0; i<som->size; i++) |
| 346 |
som->c[i] /= max; |
| 347 |
} |
| 348 |
static som_t *som_init(args_t *args) |
| 349 |
{ |
| 350 |
som_t *som = (som_t*) calloc(1,sizeof(som_t)); |
| 351 |
som->ndim = args->ndim; |
| 352 |
som->nbin = args->nbin; |
| 353 |
som->kdim = args->mvals; |
| 354 |
som->nt = args->ntrain; |
| 355 |
som->learn = args->learn; |
| 356 |
som->bmu_th = args->bmu_th; |
| 357 |
som->size = pow(som->nbin,som->ndim); |
| 358 |
som->w = (double*) malloc(sizeof(double)*som->size*som->kdim); |
| 359 |
if ( !som->w ) error("Could not alloc %"PRIu64" bytes [nbin=%d ndim=%d kdim=%d]\n", (uint64_t)(sizeof(double)*som->size*som->kdim),som->nbin,som->ndim,som->kdim); |
| 360 |
som->c = (double*) calloc(som->size,sizeof(double)); |
| 361 |
if ( !som->w ) error("Could not alloc %"PRIu64" bytes [nbin=%d ndim=%d]\n", (uint64_t)(sizeof(double)*som->size),som->nbin,som->ndim); |
| 362 |
int i; |
| 363 |
for (i=0; i<som->size*som->kdim; i++) |
| 364 |
som->w[i] = random(); |
| 365 |
som->a_idx = (int*) malloc(sizeof(int)*som->ndim); |
| 366 |
som->b_idx = (int*) malloc(sizeof(int)*som->ndim); |
| 367 |
som->div = (double*) malloc(sizeof(double)*som->ndim); |
| 368 |
for (i=0; i<som->ndim; i++) |
| 369 |
som->div[i] = pow(som->nbin,som->ndim-i-1); |
| 370 |
return som; |
| 371 |
} |
| 372 |
static void som_destroy(som_t *som) |
| 373 |
{ |
| 374 |
free(som->a_idx); free(som->b_idx); free(som->div); |
| 375 |
free(som->w); free(som->c); |
| 376 |
free(som); |
| 377 |
} |
| 378 |
|
| 379 |
static void init_data(args_t *args) |
| 380 |
{ |
| 381 |
|
| 382 |
annots_reader_reset(args); |
| 383 |
annots_reader_next(args); |
| 384 |
|
| 385 |
if ( args->action==SOM_CLASSIFY ) |
| 386 |
args->som = som_load_map(args->prefix,&args->nfold); |
| 387 |
} |
| 388 |
static void destroy_data(args_t *args) |
| 389 |
{ |
| 390 |
int i; |
| 391 |
if ( args->som ) |
| 392 |
{ |
| 393 |
for (i=0; i<args->nfold; i++) som_destroy(args->som[i]); |
| 394 |
} |
| 395 |
free(args->train_dat); |
| 396 |
free(args->train_class); |
| 397 |
free(args->som); |
| 398 |
free(args->vals); |
| 399 |
free(args->str.s); |
| 400 |
} |
| 401 |
|
| 402 |
#define MERGE_MIN 0 |
| 403 |
#define MERGE_MAX 1 |
| 404 |
#define MERGE_AVG 2 |
| 405 |
static double get_min_score(args_t *args, int iskip) |
| 406 |
{ |
| 407 |
int i; |
| 408 |
double score, min_score = HUGE_VAL; |
| 409 |
for (i=0; i<args->nfold; i++) |
| 410 |
{ |
| 411 |
if ( i==iskip ) continue; |
| 412 |
score = som_get_score(args->som[i], args->vals, args->bmu_th); |
| 413 |
if ( i==0 || score < min_score ) min_score = score; |
| 414 |
} |
| 415 |
return min_score; |
| 416 |
} |
| 417 |
static double get_max_score(args_t *args, int iskip) |
| 418 |
{ |
| 419 |
int i; |
| 420 |
double score, max_score = -HUGE_VAL; |
| 421 |
for (i=0; i<args->nfold; i++) |
| 422 |
{ |
| 423 |
if ( i==iskip ) continue; |
| 424 |
score = som_get_score(args->som[i], args->vals, args->bmu_th); |
| 425 |
if ( i==0 || max_score < score ) max_score = score; |
| 426 |
} |
| 427 |
return max_score; |
| 428 |
} |
| 429 |
static double get_avg_score(args_t *args, int iskip) |
| 430 |
{ |
| 431 |
int i, n = 0; |
| 432 |
double score = 0; |
| 433 |
for (i=0; i<args->nfold; i++) |
| 434 |
{ |
| 435 |
if ( i==iskip ) continue; |
| 436 |
score += som_get_score(args->som[i], args->vals, args->bmu_th); |
| 437 |
n++; |
| 438 |
} |
| 439 |
return score/n; |
| 440 |
} |
| 441 |
static int cmpfloat_desc(const void *a, const void *b) |
| 442 |
{ |
| 443 |
float fa = *((float*)a); |
| 444 |
float fb = *((float*)b); |
| 445 |
if ( fa<fb ) return 1; |
| 446 |
if ( fa>fb ) return -1; |
| 447 |
return 0; |
| 448 |
} |
| 449 |
|
| 450 |
static void create_eval_plot(args_t *args) |
| 451 |
{ |
| 452 |
FILE *fp = open_file(NULL,"w","%s.eval.py", args->prefix); |
| 453 |
fprintf(fp, |
| 454 |
"import matplotlib as mpl\n" |
| 455 |
"mpl.use('Agg')\n" |
| 456 |
"import matplotlib.pyplot as plt\n" |
| 457 |
"\n" |
| 458 |
"import csv\n" |
| 459 |
"csv.register_dialect('tab', delimiter='\\t', quoting=csv.QUOTE_NONE)\n" |
| 460 |
"dat = []\n" |
| 461 |
"with open('%s.eval', 'r') as f:\n" |
| 462 |
"\treader = csv.reader(f, 'tab')\n" |
| 463 |
"\tfor row in reader:\n" |
| 464 |
"\t\tif row[0][0]!='#': dat.append(row)\n" |
| 465 |
"\n" |
| 466 |
"fig = plt.figure()\n" |
| 467 |
"ax1 = plt.subplot(111)\n" |
| 468 |
"ax1.plot([x[0] for x in dat],[x[1] for x in dat],'g',label='Good')\n" |
| 469 |
"ax1.plot([x[0] for x in dat],[x[2] for x in dat],'r',label='Bad')\n" |
| 470 |
"ax1.set_xlabel('SOM score')\n" |
| 471 |
"ax1.set_ylabel('Number of training sites')\n" |
| 472 |
"ax1.legend(loc='best',prop={'size':8},frameon=False)\n" |
| 473 |
"plt.savefig('%s.eval.png')\n" |
| 474 |
"plt.close()\n" |
| 475 |
"\n", args->prefix,args->prefix |
| 476 |
); |
| 477 |
fclose(fp); |
| 478 |
} |
| 479 |
|
| 480 |
static void do_train(args_t *args) |
| 481 |
{ |
| 482 |
|
| 483 |
int i, igood = 0, ibad = 0, ngood = 0, nbad = 0, ntrain = 0; |
| 484 |
annots_reader_reset(args); |
| 485 |
while ( annots_reader_next(args) ) |
| 486 |
{ |
| 487 |
|
| 488 |
int isom = 0; |
| 489 |
if ( args->dclass == args->good_class ) |
| 490 |
{ |
| 491 |
if ( ++igood >= args->nfold ) igood = 0; |
| 492 |
isom = igood; |
| 493 |
ngood++; |
| 494 |
} |
| 495 |
else if ( args->dclass == args->bad_class ) |
| 496 |
{ |
| 497 |
if ( ++ibad >= args->nfold ) ibad = 0; |
| 498 |
isom = ibad; |
| 499 |
nbad++; |
| 500 |
} |
| 501 |
else |
| 502 |
error("Could not determine the class: %d (vs %d and %d)\n", args->dclass,args->good_class,args->bad_class); |
| 503 |
|
| 504 |
|
| 505 |
ntrain++; |
| 506 |
hts_expand(double, ntrain*args->mvals, args->mtrain_dat, args->train_dat); |
| 507 |
hts_expand(int, ntrain, args->mtrain_class, args->train_class); |
| 508 |
memcpy(args->train_dat+(ntrain-1)*args->mvals, args->vals, args->mvals*sizeof(double)); |
| 509 |
args->train_class[ntrain-1] = (args->dclass==args->good_class ? 1 : 0) | isom<<1; |
| 510 |
} |
| 511 |
annots_reader_close(args); |
| 512 |
|
| 513 |
|
| 514 |
if ( !args->ntrain ) args->ntrain = ngood/args->nfold; |
| 515 |
srandom(args->rand_seed); |
| 516 |
args->som = (som_t**) malloc(sizeof(som_t*)*args->nfold); |
| 517 |
for (i=0; i<args->nfold; i++) args->som[i] = som_init(args); |
| 518 |
|
| 519 |
|
| 520 |
for (i=0; i<ntrain; i++) |
| 521 |
{ |
| 522 |
int is_good = args->train_class[i] & 1; |
| 523 |
int isom = args->train_class[i] >> 1; |
| 524 |
if ( is_good || args->train_bad ) |
| 525 |
som_train_site(args->som[isom], args->train_dat+i*args->mvals, is_good); |
| 526 |
} |
| 527 |
|
| 528 |
|
| 529 |
for (i=0; i<args->nfold; i++) |
| 530 |
{ |
| 531 |
som_norm_counts(args->som[i]); |
| 532 |
if ( args->prefix ) |
| 533 |
{ |
| 534 |
char *bname = msprintf("%s.som.%d", args->prefix,i); |
| 535 |
som_create_plot(args->som[i], bname); |
| 536 |
free(bname); |
| 537 |
} |
| 538 |
} |
| 539 |
|
| 540 |
|
| 541 |
float *good = (float*) malloc(sizeof(float)*ngood); assert(good); |
| 542 |
float *bad = (float*) malloc(sizeof(float)*nbad); assert(bad); |
| 543 |
igood = ibad = 0; |
| 544 |
double max_score = sqrt(args->som[0]->kdim); |
| 545 |
for (i=0; i<ntrain; i++) |
| 546 |
{ |
| 547 |
double score = 0; |
| 548 |
int is_good = args->train_class[i] & 1; |
| 549 |
int isom = args->train_class[i] >> 1; |
| 550 |
if ( args->nfold==1 ) isom = -1; |
| 551 |
memcpy(args->vals, args->train_dat+i*args->mvals, args->mvals*sizeof(double)); |
| 552 |
switch (args->merge) |
| 553 |
{ |
| 554 |
case MERGE_MIN: score = get_min_score(args, isom); break; |
| 555 |
case MERGE_MAX: score = get_max_score(args, isom); break; |
| 556 |
case MERGE_AVG: score = get_avg_score(args, isom); break; |
| 557 |
} |
| 558 |
score = 1.0 - score/max_score; |
| 559 |
if ( is_good ) |
| 560 |
good[igood++] = score; |
| 561 |
else |
| 562 |
bad[ibad++] = score; |
| 563 |
} |
| 564 |
qsort(good, ngood, sizeof(float), cmpfloat_desc); |
| 565 |
qsort(bad, nbad, sizeof(float), cmpfloat_desc); |
| 566 |
FILE *fp = NULL; |
| 567 |
if ( args->prefix ) fp = open_file(NULL,"w","%s.eval", args->prefix); |
| 568 |
igood = 0; |
| 569 |
ibad = 0; |
| 570 |
float prev_score = good[0]>bad[0] ? good[0] : bad[0]; |
| 571 |
int printed = 0; |
| 572 |
while ( igood<ngood || ibad<nbad ) |
| 573 |
{ |
| 574 |
if ( igood<ngood && good[igood]==prev_score ) { igood++; continue; } |
| 575 |
if ( ibad<nbad && bad[ibad]==prev_score ) { ibad++; continue; } |
| 576 |
if ( fp ) |
| 577 |
fprintf(fp,"%e\t%f\t%f\n", prev_score, (float)igood/ngood, (float)ibad/nbad); |
| 578 |
if ( !printed && (float)igood/ngood > 0.9 ) |
| 579 |
{ |
| 580 |
fprintf(bcftools_stdout, "%.2f\t%.2f\t%e\t# %% of bad [1] and good [2] sites at a cutoff [3]\n", 100.*ibad/nbad,100.*igood/ngood,prev_score); |
| 581 |
printed = 1; |
| 582 |
} |
| 583 |
|
| 584 |
if ( igood<ngood && ibad<nbad ) prev_score = good[igood]>bad[ibad] ? good[igood] : bad[ibad]; |
| 585 |
else if ( igood<ngood ) prev_score = good[igood]; |
| 586 |
else prev_score = bad[ibad]; |
| 587 |
} |
| 588 |
if ( !printed ) fprintf(bcftools_stdout, "%.2f\t%.2f\t%e\t# %% of bad [1] and good [2] sites at a cutoff [3]\n", 100.*ibad/nbad,100.*igood/ngood,prev_score); |
| 589 |
if ( fp ) |
| 590 |
{ |
| 591 |
if ( fclose(fp) ) error("%s.eval: fclose failed: %s\n",args->prefix,strerror(errno)); |
| 592 |
create_eval_plot(args); |
| 593 |
som_write_map(args->prefix, args->som, args->nfold); |
| 594 |
} |
| 595 |
|
| 596 |
free(good); |
| 597 |
free(bad); |
| 598 |
} |
| 599 |
|
| 600 |
static void do_classify(args_t *args) |
| 601 |
{ |
| 602 |
annots_reader_reset(args); |
| 603 |
double max_score = sqrt(args->som[0]->kdim); |
| 604 |
while ( annots_reader_next(args) ) |
| 605 |
{ |
| 606 |
double score = 0; |
| 607 |
switch (args->merge) |
| 608 |
{ |
| 609 |
case MERGE_MIN: score = get_min_score(args, -1); break; |
| 610 |
case MERGE_MAX: score = get_max_score(args, -1); break; |
| 611 |
case MERGE_AVG: score = get_avg_score(args, -1); break; |
| 612 |
} |
| 613 |
fprintf(bcftools_stdout, "%e\n", 1.0 - score/max_score); |
| 614 |
} |
| 615 |
annots_reader_close(args); |
| 616 |
} |
| 617 |
|
| 618 |
static void usage(void) |
| 619 |
{ |
| 620 |
fprintf(bcftools_stderr, "\n"); |
| 621 |
fprintf(bcftools_stderr, "About: SOM (Self-Organizing Map) filtering.\n"); |
| 622 |
fprintf(bcftools_stderr, "Usage: bcftools som --train [options] <annots.tab.gz>\n"); |
| 623 |
fprintf(bcftools_stderr, " bcftools som --classify [options]\n"); |
| 624 |
fprintf(bcftools_stderr, "\n"); |
| 625 |
fprintf(bcftools_stderr, "Model training options:\n"); |
| 626 |
fprintf(bcftools_stderr, " -f, --nfold <int> n-fold cross-validation (number of maps) [5]\n"); |
| 627 |
fprintf(bcftools_stderr, " -p, --prefix <string> prefix of output files\n"); |
| 628 |
fprintf(bcftools_stderr, " -s, --size <int> map size [20]\n"); |
| 629 |
fprintf(bcftools_stderr, " -t, --train \n"); |
| 630 |
fprintf(bcftools_stderr, "\n"); |
| 631 |
fprintf(bcftools_stderr, "Classifying options:\n"); |
| 632 |
fprintf(bcftools_stderr, " -c, --classify \n"); |
| 633 |
fprintf(bcftools_stderr, "\n"); |
| 634 |
fprintf(bcftools_stderr, "Experimental training options (no reason to change):\n"); |
| 635 |
fprintf(bcftools_stderr, " -b, --bmu-threshold <float> threshold for selection of best-matching unit [0.9]\n"); |
| 636 |
fprintf(bcftools_stderr, " -d, --som-dimension <int> SOM dimension [2]\n"); |
| 637 |
fprintf(bcftools_stderr, " -e, --exclude-bad exclude bad sites from training, use for evaluation only\n"); |
| 638 |
fprintf(bcftools_stderr, " -l, --learning-rate <float> learning rate [1.0]\n"); |
| 639 |
fprintf(bcftools_stderr, " -m, --merge <min|max|avg> -f merge algorithm [avg]\n"); |
| 640 |
fprintf(bcftools_stderr, " -n, --ntrain-sites <int> effective number of training sites [number of good sites]\n"); |
| 641 |
fprintf(bcftools_stderr, " -r, --random-seed <int> random seed, 0 for time() [1]\n"); |
| 642 |
fprintf(bcftools_stderr, "\n"); |
| 643 |
bcftools_exit(1); |
| 644 |
} |
| 645 |
|
| 646 |
int main_vcfsom(int argc, char *argv[]) |
| 647 |
{ |
| 648 |
int c; |
| 649 |
args_t *args = (args_t*) calloc(1,sizeof(args_t)); |
| 650 |
args->argc = argc; args->argv = argv; |
| 651 |
args->nbin = 20; |
| 652 |
args->learn = 1.0; |
| 653 |
args->bmu_th = 0.9; |
| 654 |
args->nfold = 5; |
| 655 |
args->rand_seed = 1; |
| 656 |
args->ndim = 2; |
| 657 |
args->bad_class = 1; |
| 658 |
args->good_class = 2; |
| 659 |
args->merge = MERGE_AVG; |
| 660 |
args->train_bad = 1; |
| 661 |
|
| 662 |
static struct option loptions[] = |
| 663 |
{ |
| 664 |
{"help",0,0,'h'}, |
| 665 |
{"prefix",1,0,'p'}, |
| 666 |
{"ntrain-sites",1,0,'n'}, |
| 667 |
{"random-seed",1,0,'r'}, |
| 668 |
{"bmu-threshold",1,0,'b'}, |
| 669 |
{"exclude-bad",0,0,'e'}, |
| 670 |
{"learning-rate",1,0,'l'}, |
| 671 |
{"size",1,0,'s'}, |
| 672 |
{"som-dimension",1,0,'d'}, |
| 673 |
{"nfold",1,0,'f'}, |
| 674 |
{"merge",1,0,'m'}, |
| 675 |
{"train",0,0,'t'}, |
| 676 |
{"classify",0,0,'c'}, |
| 677 |
{0,0,0,0} |
| 678 |
}; |
| 679 |
while ((c = getopt_long(argc, argv, "htcp:n:r:b:l:s:f:d:m:e",loptions,NULL)) >= 0) { |
| 680 |
switch (c) { |
| 681 |
case 'e': args->train_bad = 0; break; |
| 682 |
case 'm': |
| 683 |
if ( !strcmp(optarg,"min") ) args->merge = MERGE_MIN; |
| 684 |
else if ( !strcmp(optarg,"max") ) args->merge = MERGE_MAX; |
| 685 |
else if ( !strcmp(optarg,"avg") ) args->merge = MERGE_AVG; |
| 686 |
else error("The -m method not recognised: %s\n", optarg); |
| 687 |
break; |
| 688 |
case 'p': args->prefix = optarg; break; |
| 689 |
case 'n': args->ntrain = atoi(optarg); break; |
| 690 |
case 'r': args->rand_seed = atoi(optarg); break; |
| 691 |
case 'b': args->bmu_th = atof(optarg); break; |
| 692 |
case 'l': args->learn = atof(optarg); break; |
| 693 |
case 's': args->nbin = atoi(optarg); break; |
| 694 |
case 'f': args->nfold = atoi(optarg); break; |
| 695 |
case 'd': |
| 696 |
args->ndim = atoi(optarg); |
| 697 |
if ( args->ndim<2 ) error("Expected -d >=2, got %d\n", args->ndim); |
| 698 |
if ( args->ndim>3 ) fprintf(bcftools_stderr,"Warning: This will take a long time and is not going to make the results better: -d %d\n", args->ndim); |
| 699 |
break; |
| 700 |
case 't': args->action = SOM_TRAIN; break; |
| 701 |
case 'c': args->action = SOM_CLASSIFY; break; |
| 702 |
case 'h': |
| 703 |
case '?': usage(); break; |
| 704 |
default: error("Unknown argument: %s\n", optarg); |
| 705 |
} |
| 706 |
} |
| 707 |
|
| 708 |
if ( !args->rand_seed ) args->rand_seed = time(NULL); |
| 709 |
if ( argc!=optind+1 ) usage(); |
| 710 |
args->fname = argv[optind]; |
| 711 |
init_data(args); |
| 712 |
|
| 713 |
if ( args->action == SOM_TRAIN ) do_train(args); |
| 714 |
else if ( args->action == SOM_CLASSIFY ) do_classify(args); |
| 715 |
|
| 716 |
destroy_data(args); |
| 717 |
free(args); |
| 718 |
return 0; |
| 719 |
} |
| 720 |
|