/* * 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; }
/* * Ripped out of io_lib's trace_dump program. * It reformats a trace to as printable ASCII. */ void dump_text(ztr_t *z, char *name, char mode, FILE **files) { Read *read; int i; uncompress_ztr(z); read = ztr2read(z); /* Inefficient; can do direct */ if (read == NULL) { fprintf(stderr, "Tracedump was unable to open file %s\n", name ); return; } fprintf(files[0], "[Trace]\n"); fprintf(files[0], "%s\n", name); fprintf(files[0], "\n[Header]\n"); fprintf(files[0], "%d\t\t# format\n", read->format); fprintf(files[0], "%d\t\t# NPoints\n", read->NPoints); fprintf(files[0], "%d\t\t# NBases\n", read->NBases); fprintf(files[0], "%d\t\t# NFlows\n", read->nflows); fprintf(files[0], "%d\t\t# maxTraceVal\n", (int)read->maxTraceVal-read->baseline); fprintf(files[0], "%d\t\t# baseline\n", read->baseline); fprintf(files[0], "%d\t\t# leftCutoff\n", read->leftCutoff); fprintf(files[0], "%d\t\t# rightCutoff\n", read->rightCutoff); fputs("\n[Bases]\n", files[0]); for (i = 0; i < read->NBases; i++) { fprintf(files[0], "%c %05d %+03d %+03d %+03d %+03d #%3d\n", read->base[i], read->basePos ? read->basePos[i] : 0, (int)read->prob_A[i], (int)read->prob_C[i], (int)read->prob_G[i], (int)read->prob_T[i], i); } if (read->NPoints) { fputs("\n[A_Trace]\n", files[0]); for(i = 0; i < read->NPoints; i++) fprintf(files[0], "%d\t#%5d\n", (int)read->traceA[i] - read->baseline, i); fputs("\n[C_Trace]\n", files[0]); for(i = 0; i < read->NPoints; i++) fprintf(files[0], "%d\t#%5d\n", (int)read->traceC[i] - read->baseline, i); fputs("\n[G_Trace]\n", files[0]); for(i = 0; i < read->NPoints; i++) fprintf(files[0], "%d\t#%5d\n", (int)read->traceG[i] - read->baseline, i); fputs("\n[T_Trace]\n", files[0]); for(i = 0; i < read->NPoints; i++) fprintf(files[0], "%d\t#%5d\n", (int)read->traceT[i] - read->baseline, i); } if (read->flow_order) { fputs("\n[Flows]\n", files[0]); for (i = 0; i < read->nflows; i++) { fprintf(files[0], "%c %5.2f %u\t#%5d\n", read->flow_order[i], read->flow ? read->flow[i] : 0, read->flow_raw ? read->flow_raw[i] : 0, i); } } if (read->info) { fputs("\n[Info]\n", files[0]); fprintf(files[0], "%s\n", read->info); } read_deallocate(read); }
/* * As per partial_decode_ztr in srf.c, but without uncompress_ztr. */ ztr_t *partial_decode_ztr2(srf_t *srf, mFILE *mf, ztr_t *z) { ztr_t *ztr; ztr_chunk_t *chunk; long pos = 0; if (z) { /* Use existing ZTR object => already loaded header */ ztr = z; } else { /* Allocate or use existing ztr */ if (NULL == (ztr = new_ztr())) return NULL; /* Read the header */ if (-1 == ztr_read_header(mf, &ztr->header)) { if (!z) delete_ztr(ztr); mrewind(mf); return NULL; } /* Check magic number and version */ if (memcmp(ztr->header.magic, ZTR_MAGIC, 8) != 0) { if (!z) delete_ztr(ztr); mrewind(mf); return NULL; } if (ztr->header.version_major != ZTR_VERSION_MAJOR) { if (!z) delete_ztr(ztr); mrewind(mf); return NULL; } } /* Load chunks */ pos = mftell(mf); while ((chunk = ztr_read_chunk_hdr(mf))) { chunk->data = (char *)xmalloc(chunk->dlength); if (chunk->dlength != mfread(chunk->data, 1, chunk->dlength, mf)) break; ztr->nchunks++; ztr->chunk = (ztr_chunk_t *)xrealloc(ztr->chunk, ztr->nchunks * sizeof(ztr_chunk_t)); memcpy(&ztr->chunk[ztr->nchunks-1], chunk, sizeof(*chunk)); xfree(chunk); pos = mftell(mf); } /* * At this stage we're 'pos' into the mFILE mf with any remainder being * a partial block. */ if (0 == ztr->nchunks) { if (!z) delete_ztr(ztr); mrewind(mf); return NULL; } /* Ensure we exit at the start of a ztr CHUNK */ mfseek(mf, pos, SEEK_SET); /* If this is the header part, ensure we uncompress and init. data */ if (!z) { /* Force caching of huffman code_sets */ ztr_find_hcode(ztr, CODE_USER); /* And uncompress the rest */ uncompress_ztr(ztr); } return ztr; }
/* * Prints a read in solexa format. Depending on the given chunk type mode, * only some of the chunks are printed out for every read. The files are * given in the following order: seq, prb, sig2, int, nse. The files must * already be open. */ void dump_solexa(ztr_t *z, char *name, char mode, FILE **files) { int i, nc; ztr_chunk_t **chunks; char *seq; int lane, tile, x, y; parse_name(name, &lane, &tile, &x, &y); uncompress_ztr(z); chunks = ztr_find_chunks(z, ZTR_TYPE_BASE, &nc); if (nc != 1) { fprintf(stderr, "Zero or greater than one BASE chunks found.\n"); return; } seq = chunks[0]->data+1; /* Sequence */ if (mode & SEQ) { fprintf(files[0], "%d\t%d\t%d\t%d\t%.*s\n", lane, tile, x, y, chunks[0]->dlength-1, chunks[0]->data+1); } /* Confidence */ if (mode & PRB) { chunks = ztr_find_chunks(z, ZTR_TYPE_CNF4, &nc); if (nc != 1) { fprintf(stderr, "Zero or greater than one CNF chunks found.\n"); return; } dump_conf4_solexa(files[1], seq, (sc *)chunks[0]->data+1, chunks[0]->dlength-1); } /* Traces */ if (mode & SIG2) { chunks = ztr_find_chunks(z, ZTR_TYPE_SMP4, &nc); for (i = 0; i < nc; i++) { char *key = ztr_lookup_mdata_value(z, chunks[i], "TYPE"); if (!key || 0 == strcmp(key, "PROC")) { key = ztr_lookup_mdata_value(z, chunks[i], "OFFS"); dump_samples4_solexa(files[2], lane, tile, x, y, key ? atoi(key) : 0, (uc *)chunks[i]->data+2, chunks[i]->dlength-2); break; } } } if (mode & INT) { chunks = ztr_find_chunks(z, ZTR_TYPE_SMP4, &nc); for (i = 0; i < nc; i++) { char *key = ztr_lookup_mdata_value(z, chunks[i], "TYPE"); if (key && 0 == strcmp(key, "SLXI")) { key = ztr_lookup_mdata_value(z, chunks[i], "OFFS"); dump_samples4_solexa(files[3], lane, tile, x, y, key ? atoi(key) : 0, (uc *)chunks[i]->data+2, chunks[i]->dlength-2); break; } } } if (mode & NSE) { chunks = ztr_find_chunks(z, ZTR_TYPE_SMP4, &nc); for (i = 0; i < nc; i++) { char *key = ztr_lookup_mdata_value(z, chunks[i], "TYPE"); if (key && 0 == strcmp(key, "SLXN")) { key = ztr_lookup_mdata_value(z, chunks[i], "OFFS"); dump_samples4_solexa(files[4], lane, tile, x, y, key ? atoi(key) : 0, (uc *)chunks[i]->data+2, chunks[i]->dlength-2); break; } } } }