/* * Read the SCF format sequence from FILE *fp into a 'scf' structure. * A NULL result indicates failure. */ Scf *fread_scf(FILE *fp) { Scf *scf; Header h; int err; float scf_version; int sections = read_sections(0); /* Read header */ if (read_scf_header(fp, &h) == -1) { return NULL; } /* Allocate memory */ if (NULL == (scf = scf_allocate(h.samples, h.sample_size, h.bases, h.comments_size, h.private_size))) return NULL; /* fake things for older style SCF -- SD */ if (h.sample_size != 1 && h.sample_size != 2) h.sample_size = 1; scf_version = scf_version_str2float(h.version); memcpy(&scf->header, &h, sizeof(Header)); if (sections & READ_SAMPLES) { /* Read samples */ if (fseek(fp, (off_t)h.samples_offset, 0 /* SEEK_SET */) != 0) { scf_deallocate(scf); return NULL; } if ( 2.9 > scf_version ) { if (h.sample_size == 1) { err= read_scf_samples1(fp, scf->samples.samples1, h.samples); } else { err= read_scf_samples2(fp, scf->samples.samples2, h.samples); } } else { if (h.sample_size == 1) { err= read_scf_samples31(fp, scf->samples.samples1, h.samples); } else { err= read_scf_samples32(fp, scf->samples.samples2, h.samples); } } if (-1 == err) { scf_deallocate(scf); return NULL; } } if (sections & READ_BASES) { /* Read bases */ if (fseek(fp, (off_t)h.bases_offset, 0 /* SEEK_SET */) != 0) { scf_deallocate(scf); return NULL; } if ( 2.9 > scf_version ) { if (-1 == read_scf_bases(fp, scf->bases, h.bases)) { scf_deallocate(scf); return NULL; } } else { if (-1 == read_scf_bases3(fp, scf->bases, h.bases)) { scf_deallocate(scf); return NULL; } } } if (sections & READ_COMMENTS) { /* Read comments */ if (scf->comments) { if (fseek(fp,(off_t)(h.comments_offset), 0) != 0 || -1 == read_scf_comment(fp, scf->comments, h.comments_size)) { /* * Was: "scf_deallocate(scf); return NULL;". * We now simply clear the comments and gracefully continue. */ fprintf(stderr, "Warning: SCF file had invalid comment field\n"); xfree(scf->comments); scf->comments = NULL; } else { scf->comments[h.comments_size] = '\0'; } } } /* Read private data */ if (h.private_size) { if (-1 == fseek(fp, (off_t)(h.private_offset), 0) || h.private_size != fread(scf->private_data, 1, h.private_size, fp)){ scf_deallocate(scf); return NULL; } } return scf; }
/* * Write a sequence to a FILE *fp of format "format". If "format" is 0, * we choose our favourite - SCF. * * Returns: * 0 for success * -1 for failure */ int mfwrite_reading(mFILE *fp, Read *read, int format) { int r = -1; int no_compress = 0; #ifdef _WIN32 /* * jkb 09/06/00 comment below * * On windows "prog > file.scf" will work wrongly (compared to * "prog file.scf") because windows is rather stupid. It treats ascii * and binary streams differently, it considers stdout to be ascii unless * told otherwise, and it can only be told otherwise by using non-ansi * windows-specific function calls. */ if (format != TT_EXP && format != TT_PLN && fp->fp) _setmode(_fileno(fp->fp), _O_BINARY); #endif switch (format) { default: /* Defaults to ZTR type */ #ifdef IOLIB_ZTR case TT_ZTR: case TT_ZTR2: { ztr_t *ztr; ztr = read2ztr(read); compress_ztr(ztr, 2); r = mfwrite_ztr(fp, ztr); delete_ztr(ztr); no_compress = 1; break; } case TT_ZTR1: { ztr_t *ztr; ztr = read2ztr(read); compress_ztr(ztr, 1); r = mfwrite_ztr(fp, ztr); delete_ztr(ztr); break; } case TT_ZTR3: { ztr_t *ztr; ztr = read2ztr(read); compress_ztr(ztr, 3); r = mfwrite_ztr(fp, ztr); delete_ztr(ztr); no_compress = 1; break; } #endif #ifdef IOLIB_SCF case TT_SCF: { Scf *scf; scf = read2scf(read); r = mfwrite_scf(scf, fp); scf_deallocate(scf); break; } #endif #ifdef IOLIB_CTF case TT_CTF: r = mfwrite_ctf(fp, read); break; #endif #ifdef IOLIB_ABI case TT_ABI: /*return mfwrite_abi(fp, read); */ break; #endif #ifdef IOLIB_ALF case TT_ALF: /* return mfwrite_alf(fp, read); */ break; #endif #ifdef IOLIB_EXP case TT_EXP: { Exp_info *e = read2exp(read, read->ident ? read->ident : "unknown"); if (NULL == e) { fprintf(stderr, "Failed to create experiment file.\n"); r = -1; } else { exp_print_mfile(fp, e); exp_destroy_info(e); r = 0; } break; } #endif #ifdef IOLIB_PLN case TT_PLN: r = mfwrite_pln(fp, read); break; #endif } mftruncate(fp, -1); if (r == 0 && !no_compress) { fcompress_file(fp); } mfflush(fp); return r; }
/* * Translates a Read structure into a Scf structure. * The Read structure is left unchanged. * * Returns: * A pointer to an allocated Scf structure upon success. * NULL upon failure. */ Scf *read2scf(Read *read) { Scf *scf; register int i, i_end; int sample_size; /* allocate */ sample_size = read->maxTraceVal >= 0x100 ? 2 : 1; scf = scf_allocate(read->NPoints, sample_size, read->NBases, 0, 0); if (NULL == scf) return NULL; /* copy the samples */ i_end = read->NPoints; scf->header.samples = i_end; if (sample_size == 1) { scf->header.sample_size = 1; for (i = 0; i < i_end; i++) { scf->samples.samples1[i].sample_A = (uint_1)read->traceA[i]; scf->samples.samples1[i].sample_C = (uint_1)read->traceC[i]; scf->samples.samples1[i].sample_G = (uint_1)read->traceG[i]; scf->samples.samples1[i].sample_T = (uint_1)read->traceT[i]; } } else { scf->header.sample_size = 2; for (i = 0; i < i_end; i++) { scf->samples.samples2[i].sample_A = read->traceA[i]; scf->samples.samples2[i].sample_C = read->traceC[i]; scf->samples.samples2[i].sample_G = read->traceG[i]; scf->samples.samples2[i].sample_T = read->traceT[i]; } } /* copy the bases */ i_end = read->NBases; scf->header.bases = i_end; for (i = 0; i < i_end; i++) { scf->bases[i].peak_index = read->basePos ? read->basePos[i] : i; scf->bases[i].prob_A = read->prob_A ? read->prob_A[i] : 0; scf->bases[i].prob_C = read->prob_A ? read->prob_C[i] : 0; scf->bases[i].prob_G = read->prob_A ? read->prob_G[i] : 0; scf->bases[i].prob_T = read->prob_A ? read->prob_T[i] : 0; scf->bases[i].base = read->base ? read->base[i] : '-'; } /* allocate and copy the comments */ if (read->info) { scf->header.comments_size = strlen(read->info) + 1; scf->comments = (char *)xmalloc(scf->header.comments_size); if (NULL == scf->comments) { scf_deallocate(scf); return NULL; } memcpy(scf->comments, read->info, scf->header.comments_size - 1); /* just to make sure */ scf->comments[scf->header.comments_size-1] = '\0'; } /* other bits and pieces */ scf->header.bases_left_clip = read->leftCutoff; scf->header.bases_right_clip = read->NBases - read->rightCutoff + 1; scf->header.code_set = CSET_DEFAULT; memcpy(scf->header.version, scf_version_float2str(SCF_VERSION), 4); return scf; }
/* * Read a sequence from a FILE *fp of format "format". If "format" is 0 * (ANY_FORMAT), we automatically determine the correct format. * We still pass a filename 'fn' although this isn't used other than for * filling in the read->trace_name field. * * NB this function should NOT be used when Biolims support is required * (as biolims readings are not stored in a file) * * Returns: * Read * for success * NULLRead for failure */ Read *mfread_reading(mFILE *fp, char *fn, int format) { Read *read; mFILE *newfp; if (!fn) fn = "(unknown)"; newfp = freopen_compressed(fp, NULL); if (newfp != fp) { fp = newfp; } else { newfp = NULL; } #ifdef _WIN32 /* * jkb 16/05/00 comment below * * On windows "prog < file.abi" will work wrongly (compared to * "prog file.abi") because windows is rather stupid. It treats ascii * and binary streams differently, it considers stdin to be ascii unless * told otherwise, and it can only be told otherwise by using non-ansi * windows-specific function calls. */ if (format != TT_EXP && format != TT_PLN && fp->fp) _setmode(_fileno(fp->fp), _O_BINARY); #endif if (format == TT_ANY) { format = fdetermine_trace_type(fp); mrewind(fp); } switch (format) { case TT_UNK: case TT_ERR: errout("File '%s' has unknown trace type\n", fn); read = NULLRead; break; #ifdef IOLIB_SCF case TT_SCF: { Scf *scf; scf = mfread_scf(fp); if (scf) { read = scf2read(scf); scf_deallocate(scf); } else read = NULLRead; break; } #endif #ifdef IOLIB_CTF case TT_CTF: read = mfread_ctf(fp); break; #endif #ifdef IOLIB_ZTR case TT_ZTR: case TT_ZTR1: case TT_ZTR2: case TT_ZTR3: { ztr_t *ztr; if ((ztr = mfread_ztr(fp))) { uncompress_ztr(ztr); read = ztr2read(ztr); delete_ztr(ztr); } else { read = NULLRead; } break; } #endif #ifdef IOLIB_ABI case TT_ABI: read = mfread_abi(fp); break; #endif #ifdef IOLIB_ALF case TT_ALF: read = mfread_alf(fp); break; #endif #ifdef IOLIB_EXP case TT_EXP: { /* FIXME: we shouldn't redirect like this */ Exp_info *e = exp_mfread_info(fp); read = e ? exp2read(e,fn) : NULLRead; break; } #endif #ifdef IOLIB_PLN case TT_PLN: read = mfread_pln(fp); break; #endif default: errout("Unknown format %d specified to read_reading()\n", format); read = NULLRead; } if (read != NULLRead && (read->trace_name = (char *)xmalloc(strlen(fn)+1))) strcpy(read->trace_name, fn); if (newfp) mfclose(newfp); return read; }