wells_mask_t * wells_mask_read(FILE *fp) { int32_t i; wells_mask_t *m; m = ion_calloc(1, sizeof(wells_mask_t), __func__, "m"); if(fread(&m->num_rows, sizeof(int32_t), 1, fp) != 1 || fread(&m->num_cols, sizeof(int32_t), 1, fp) != 1) { free(m); return NULL; } m->masks = ion_malloc(m->num_rows * sizeof(uint16_t*), __func__, "m->mask"); for(i=0;i<m->num_rows;i++) { m->masks[i] = ion_malloc(m->num_cols * sizeof(uint16_t), __func__, "m->masks[i]"); if(fread(m->masks[i], sizeof(uint16_t), m->num_cols, fp) != m->num_cols) { // free while(0 <= i) { free(m->masks[i]); i--; } free(m); return NULL; } } return m; }
sff_read_t* sff_read_init() { sff_read_t *r = NULL; r = ion_calloc(1, sizeof(sff_read_t), __func__, "r"); return r; }
sff_read_header_t * sff_read_header_init() { sff_read_header_t *rh = NULL; rh = ion_calloc(1, sizeof(sff_read_header_t), __func__, "rh"); return rh; }
sff_t * sff_init1() { sff_t *sff = NULL; sff = ion_calloc(1, sizeof(sff_t), __func__, "sff"); sff->gheader = NULL; sff->rheader = sff_read_header_init(); sff->read = sff_read_init(); return sff; }
static sff_read_header_t * sff_read_header_clone(sff_read_header_t *rh) { sff_read_header_t *ret = NULL; ret = ion_calloc(1, sizeof(sff_read_header_t), __func__, "rh"); (*ret) = (*rh); ret->name = ion_string_clone(rh->name); return ret; }
static sff_index_t * sff_index_init() { sff_index_t *idx; idx = ion_calloc(1, sizeof(sff_index_t), __func__, "idx"); idx->index_version = SFF_INDEX_VERSION; idx->index_magic_number = SFF_INDEX_MAGIC; return idx; }
wells_header_t * wells_header_read(FILE *fp) { wells_header_t *h; h = ion_calloc(1, sizeof(wells_header_t), __func__, "h"); if(fread(&h->num_wells, sizeof(uint32_t), 1, fp) != 1 || fread(&h->num_flows, sizeof(uint16_t), 1, fp) != 1) { free(h); return NULL; } h->flow_order = ion_malloc(sizeof(char)*(h->num_flows+1), __func__, "h"); if(fread(h->flow_order, sizeof(char), h->num_flows, fp) != h->num_flows) { free(h); return NULL; } h->flow_order[h->num_flows]='\0'; return h; }
static sff_read_t * sff_read_clone(sff_read_t *r, sff_header_t *gh, sff_read_header_t *rh) { sff_read_t *ret = NULL; int32_t i; ret = ion_calloc(1, sizeof(sff_read_t), __func__, "r"); ret->flowgram = ion_malloc(sizeof(uint16_t)*gh->flow_length, __func__, "ret->flowgram"); for(i=0;i<gh->flow_length;i++) { ret->flowgram[i] = r->flowgram[i]; } ret->flow_index = ion_malloc(sizeof(uint8_t)*rh->n_bases, __func__, "ret->flow_index"); for(i=0;i<rh->n_bases;i++) { ret->flow_index[i] = r->flow_index[i]; } ret->bases = ion_string_clone(r->bases); ret->quality = ion_string_clone(r->quality); return ret; }
dat_frame_t * dat_frame_read1(FILE *fp, dat_frame_t *cur, dat_frame_t *prev, dat_header_t *header) { int32_t i, mode; uint32_t ctr, compressed; int8_t *tmp_data8=NULL; //FILE *fp_debug = stdout; // for debuggin // read the timestamp and compression flag for this frame if(fread_big_endian_uint32_t(fp, &cur->timestamp) != 1 || fread_big_endian_uint32_t(fp, &compressed) != 1) { return NULL; } // the first frame is always uncompressed... if(NULL == prev || DAT_HEADER_UNINTERLACED == header->interlace_type) { if(0 != compressed) { ion_error(__func__, "compressed", Exit, OutOfRange); } // read in the data if(fread(cur->data, sizeof(uint16_t), header->rows*header->cols, fp) != header->rows*header->cols) { // image data return NULL; } // manually convert from big-endian for(i=0;i<header->rows*header->cols;i++) { cur->data[i] = ntohs(cur->data[i]) & DAT_FRAME_DATA_MASK; // unmask data } } else { uint32_t len, transitions, total, sentinel; uint32_t observed_transitions = 0; // read in the length of the frame, the # of transitions, the // total sum of hte pixel values, and hte sentinel if(fread_big_endian_uint32_t(fp, &len) != 1 || fread_big_endian_uint32_t(fp, &transitions) != 1 || fread_big_endian_uint32_t(fp, &total) != 1 || fread_big_endian_uint32_t(fp, &sentinel) != 1) { return NULL; } // check the sentinel if(sentinel != DAT_HEADER_SIGNATURE) { ion_error(__func__, "cur->sentinel", Exit, OutOfRange); } // subtract len, transitions, total, sentinel len -= sizeof(uint32_t)*4; assert(0 < len); // initialize memory cur->data = ion_calloc(header->rows*header->cols, sizeof(uint16_t), __func__, "cur->data"); tmp_data8 = ion_malloc(len*sizeof(int8_t), __func__, "tmp_data8"); // read in the whole frame // NOTE: could read in byte-by-byte and process to reduce memory overhead if(len != fread(tmp_data8, sizeof(int8_t), len, fp)) { free(tmp_data8); return NULL; } // de-interlace the data i=mode=ctr=0; while(ctr < header->rows*header->cols) { if(len <= i) { // not enough bytes read ion_error(__func__, "len <= i", Exit, OutOfRange); } // switch to 8-bit mode, or 16-bit mode where appropriate if(i < len-1 && DAT_FRAME_KEY_0 == (uint8_t)tmp_data8[i]) { if(DAT_FRAME_KEY_8_1 == (uint8_t)tmp_data8[i+1]) { // 16-bit to 8-bit observed_transitions++; /* fprintf(stderr, "[%d-%d] from %d-bit mode to 8-bit mode #%d/%d ctr=%d\n", i, i+1, mode, observed_transitions, transitions, ctr); */ mode = 8; i+=2; } else if(DAT_FRAME_KEY_16_1 == (uint8_t)tmp_data8[i+1]) { // 8-bit to 16-bit observed_transitions++; /* fprintf(stderr, "[%d-%d] from %d-bit mode to 16-bit mode #%d/%d ctr=%d\n", i, i+1, mode, observed_transitions, transitions, ctr); */ mode = 16; i+=2; } } // Note: assumes we must have data read between mode switches // read in data switch(mode) { case 8: // 8-bit mode cur->data[ctr] = tmp_data8[i] + prev->data[ctr]; ctr++; i++; break; case 16: // 16-bit mode cur->data[ctr] = (ntohs((int16_t)((tmp_data8[i] << 8) | tmp_data8[i+1])) & DAT_FRAME_DATA_MASK) + prev->data[ctr]; ctr++; i+=2; break; default: // mode? ion_error(__func__, "mode", Exit, OutOfRange); break; } } if(((i+3) & ~0x3) != len) { // check that the data was quad-word aligned ion_error(__func__, "quad word alignment", Exit, OutOfRange); } // free tmp_data8 free(tmp_data8); // check that the observed # of transitions equals the state # of // transitions if(transitions != observed_transitions) { ion_error(__func__, "transitions != observed_transitions", Exit, OutOfRange); } } return cur; }
int wells_combine_main(int argc, char *argv[]) { FILE **fps_in = NULL; wells_chip_t **chips = NULL; int c; int32_t i, j, k, l, n; int32_t nonzero, min_row, max_row, min_col, max_col; min_row = max_row = min_col = max_col = -1; nonzero=0; while((c = getopt(argc, argv, "r:c:zh")) >= 0) { switch(c) { case 'r': if(ion_parse_range(optarg, &min_row, &max_row) < 0) { ion_error(__func__, "-r : format not recognized", Exit, OutOfRange); } break; case 'c': if(ion_parse_range(optarg, &min_col, &max_col) < 0) { ion_error(__func__, "-c : format not recognized", Exit, OutOfRange); } break; case 'z': nonzero = 1; break; case 'h': default: return usage(); } } if(argc - optind < 2) { return usage(); } else { n = argc - optind; // open the files fps_in = ion_calloc(n, sizeof(FILE*), __func__, "fps_in"); for(i=optind;i<argc;i++) { if(!(fps_in[i-optind] = fopen(argv[i], "rb"))) { fprintf(stderr, "** Could not open %s for reading. **\n", argv[i]); ion_error(__func__, argv[i], Exit, OpenFileError); } } // read in the headers chips = ion_calloc(n, sizeof(wells_chip_t*), __func__, "chips"); for(i=0;i<n;i++) { // NB: reads in both wells files if(NULL == (chips[i] = wells_chip_read1(fps_in[i]))) { ion_error(__func__, argv[i+optind], Exit, ReadFileError); } } // check we can combine the data for(i=1;i<n;i++) { if(chips[i-1]->header->num_wells != chips[i]->header->num_wells) { ion_error(__func__, "# of wells did not match", Exit, OutOfRange); } else if(chips[i-1]->header->num_flows != chips[i]->header->num_flows) { ion_error(__func__, "# of flows did not match", Exit, OutOfRange); } else if(0 != strcmp(chips[i-1]->header->flow_order, chips[i]->header->flow_order)) { ion_error(__func__, "flow order did not match", Exit, OutOfRange); } else if(chips[i-1]->num_rows != chips[i]->num_rows) { ion_error(__func__, "# of rows did not match", Exit, OutOfRange); } else if(chips[i-1]->num_cols != chips[i]->num_cols) { ion_error(__func__, "# of columns did not match", Exit, OutOfRange); } } // write the header if(0 == wells_header_write(stdout, chips[0]->header)) { ion_error(__func__, "Could not write to the output file", Exit, WriteFileError); } // combine the data // NB: modifies the wells for the first chip for(k=0;k<chips[0]->num_cols;k++) { // cols for(j=0;j<chips[0]->num_rows;j++) { // rows wells_data_t data_out; // make room for the new flow values data_out.flow_values = ion_calloc(chips[0]->header->num_flows, sizeof(float), __func__, "data_out->flow_values"); for(i=0;i<n;i++) { // chip wells_data_t *data_in = NULL; // read data in if(NULL == (data_in = wells_data_read(fps_in[i], chips[i]->header))) { ion_error(__func__, argv[i+optind], Exit, ReadFileError); } // copy over relevant data if(0 == i) { data_out.x = data_in->x; data_out.y = data_in->y; data_out.rank = data_in->rank; } for(l=0;l<chips[i]->header->num_flows;l++) { // flows // update sum data_out.flow_values[l] += data_in->flow_values[l]; } // destroy wells_data_destroy(data_in); } // update for(l=0;l<chips[0]->header->num_flows;l++) { // flows data_out.flow_values[l] /= n; } // write if(0 == wells_data_write(stdout, chips[0]->header, &data_out)) { ion_error(__func__, "Could not write to the output file", Exit, WriteFileError); } // destroy free(data_out.flow_values); } } // close the files for(i=0;i<n;i++) { fclose(fps_in[i]); } // free free(fps_in); free(chips); } return 0; }