void deconv3(void *g, int gx, int gy, int gz, void *f, int fx, int fy, int fz, void *out) { double *g2 = unpack3(g, gx, gy, gz, gy, gz); double *f2 = unpack3(f, fx, fy, fz, gy, gz); double ff[(gx - fx + 1) * gy * gz]; deconv(g2, gx * gy * gz, f2, fx * gy * gz, ff, gy * gz); pack3(ff, gx - fx + 1, gy, gz, gy - fy + 1, gz - fz + 1, out); free(g2); free(f2); }
PBWT *pbwtMerge(const char **fnames, int nfiles) { pbwt_reader_t *reader = pbwt_reader_init(fnames, nfiles); int nhaps = 0, i; for (i=0; i<nfiles; i++) nhaps += reader->pbwt[i]->M; PBWT *out_pbwt = pbwtCreate(nhaps, 0); PbwtCursor *cursor = pbwtNakedCursorCreate(nhaps, 0); uchar *yseq = myalloc(nhaps, uchar); out_pbwt->yz = arrayCreate (1<<20, uchar) ; out_pbwt->sites = arrayReCreate(out_pbwt->sites, reader->pbwt[0]->N, Site); out_pbwt->chrom = strdup(reader->pbwt[0]->chrom); int pos, j; while ( (pos=pbwt_reader_next(reader, nfiles)) ) { // Merge only records shared by all files for (i=0; i<nfiles; i++) { PBWT *p = reader->pbwt[i]; Site *site = arrp(p->sites, reader->cpos[i], Site); // Both position and alleles must match. This requires that the records are sorted by alleles. if ( site->x!=pos ) break; char *als = dictName(variationDict, site->varD); if ( strcmp(als,reader->mals) ) break; } if ( i!=nfiles ) { // intersection: skip records which are not present in all files for (i=0; i<nfiles; i++) { PBWT *p = reader->pbwt[i]; Site *site = arrp(p->sites, reader->cpos[i], Site); if ( site->x!=pos ) continue; char *als = dictName(variationDict, site->varD); if ( strcmp(als,reader->mals) ) continue; PbwtCursor *c = reader->cursor[i]; reader->unpacked[i] += unpack3(arrp(p->yz,reader->unpacked[i],uchar), p->M, c->y, 0); pbwtCursorForwardsA(c); } continue; } // read and merge int ihap = 0; for (i=0; i<nfiles; i++) { PbwtCursor *c = reader->cursor[i]; PBWT *p = reader->pbwt[i]; Site *site = arrp(p->sites, reader->cpos[i], Site); reader->unpacked[i] += unpack3(arrp(p->yz,reader->unpacked[i],uchar), p->M, c->y, 0); for (j=0; j<p->M; j++) yseq[ihap + c->a[j]] = c->y[j]; pbwtCursorForwardsA(c); ihap += p->M; } // pack merged haplotypes for (j=0; j<nhaps; j++) cursor->y[j] = yseq[cursor->a[j]]; pack3arrayAdd(cursor->y, out_pbwt->M, out_pbwt->yz); pbwtCursorForwardsA(cursor); // insert new site arrayExtend(out_pbwt->sites, out_pbwt->N+1); Site *site = arrayp(out_pbwt->sites, out_pbwt->N, Site); site->x = pos; dictAdd(variationDict, reader->mals, &site->varD); out_pbwt->N++; } pbwtCursorToAFend (cursor, out_pbwt) ; free(yseq); pbwtCursorDestroy(cursor); pbwt_reader_destroy(reader); return out_pbwt; }