static int convert(char *in, mFILE *ofp, char *out, int format, int prec, int comp, int normalise) { Read *r; if (NULL == (r = read_reading(in, format))) { fprintf(stderr, "%s: failed to read\n", in); return 1; } if (normalise) { subtract_background(r); reset_max_called_height(r); rescale_heights(r); } add_comments(r, in, format); if (prec == 1) scale_trace8(r); if (comp != -1) set_compression_method(comp); if (0 != (mfwrite_reading(ofp, r, TT_SCF))) { fprintf(stderr, "%s: failed to write\n", out); read_deallocate(r); return 1; } read_deallocate(r); return 0; }
int main(int argc, char **argv) { Read *r = NULL; char *directory = NULL; char *ident, *value; int ident_len, value_len; int i, j, found; int *found_args = NULL; char **FileList = NULL; char trace_filename[FILENAME_MAX]=""; int num_traces, trace_iter, files_read = 0; char *str; if(argc != 2) usage(); directory = argv[1]; // Get a list of all chromatogram files in the directory num_traces = GetFileList(&FileList, directory); if(num_traces == 0){ fprintf(stderr,"* Path %d yielded 0 files...exiting\n", num_traces); exit(2); } /* step through all the sequences */ for (trace_iter = 0; trace_iter < num_traces; trace_iter++){ if(r){ read_deallocate(r); } //Get the file name from the iterator. sprintf(trace_filename, "%s/%s",directory, FileList[trace_iter]); /* Read the file */ read_sections(READ_COMMENTS); if (NULL == (r = read_reading(trace_filename, TT_ANY))) { continue; // don't worry about it } files_read++; if (!r->info) return 1; for(str = strtok(r->info,"\n"); str != NULL; str = strtok(NULL,"\n")){ char *name; char *value; int items = 0; char *start,*end; name = str; value = strchr(name,'='); *value = '\0'; // skip over the '=' value++; if(!strcmp(name,"RUND")){ start = value; end = strstr(value," - "); *end = '\0'; // terminate the start string end += 3; // skip over the " - " fprintf(stdout,"%s.RUND=start=%s,end=%s\n", FileList[trace_iter], start,end); }else if(! strcmp(name,"DATE")){ start = value; end = strstr(value," to "); *end = '\0'; // terminate the start string end += 4; // skip over the " to " fprintf(stdout,"%s.DATE=start=%s,end=%s\n", FileList[trace_iter],start,end); }else{ fprintf(stdout,"%s.%s=%s\n", FileList[trace_iter],name, value); } } } return (files_read == 0); // is zero, unless we read nothing. }
int convert(char *file, int format, mFILE *ofp, char *name, int output_conf) { Read *r; Exp_info *e; char buf[50]; double aq; if (format == TT_BIO) { if (NULL == (r = read_reading(file, format))) { fprintf(stderr, "%s: failed to read\n", file); return 1; } } else { FILE *infp; if (NULL == (infp = open_trace_file(file, NULL))) { perror(file); return 1; } if (NULL == (r = fread_reading(infp, file, format))) { fprintf(stderr, "%s: failed to read\n", file); return 1; } fclose(infp); } e = read2exp(r, name); if (NULL == e) { fprintf(stderr, "Failed to create experiment file.\n"); read_deallocate(r); return 1; } sprintf(buf, "%f", aq = avg_qual(r)); exp_set_entry(e, EFLT_AQ, buf); exp_print_mfile(ofp, e); if (output_conf && aq != 0) { char *cstr; int1 *conf; int i; conf = xmalloc(r->NBases * sizeof(*conf)); cstr = xmalloc(5 * r->NBases+2); for (i = 0; i < r->NBases; i++) { switch (r->base[i]) { case 'a': case 'A': conf[i] = r->prob_A[i]; break; case 'c': case 'C': conf[i] = r->prob_C[i]; break; case 'g': case 'G': conf[i] = r->prob_G[i]; break; case 't': case 'T': conf[i] = r->prob_T[i]; break; default: conf[i] = (r->prob_A[i] + r->prob_C[i] + r->prob_G[i] + r->prob_T[i]) / 4; break; } } conf2str(conf, r->NBases, cstr); exp_set_entry(e, EFLT_AV, cstr); xfree(cstr); xfree(conf); } read_deallocate(r); exp_destroy_info(e); mfflush(ofp); return 0; }
/* * Produce a consensus trace from a specific region of this contig. */ Read *cons_trace(EdStruct *xx, int start, int end, int strand, int match, int exception) { int *seqList, i, j, count, next; Read *r; int max_points = 10000; char *con = NULL; diff_cons_seq *rlist = NULL; char fileName[256]; char t_type[5]; int form; int offset = 0, w; /* Get the consensus sequence */ if (NULL == (con = (char *)xmalloc(end - start + 2))) goto error; DBcalcConsensus(xx, start, end - start + 1, con, NULL, BOTH_STRANDS); /* Allocate a list of read pointers and positions */ if (NULL == (rlist = (diff_cons_seq *)xcalloc(DBI_gelCount(xx), sizeof(*rlist)))) goto error; /* Allocate a read structure */ if (NULL == (r = read_allocate(max_points, end - start + 1))) goto error; /* Derive the initial list of sequences covering the start point */ count = 0; seqList = DBI_list(xx); for (i = 1; i <= DBI_gelCount(xx) && DB_RelPos(xx, DBI_order(xx)[i]) <= start; i++) { int seq = DBI_order(xx)[i]; DBgetSeq(DBI(xx), seq); if (DB_RelPos(xx, seq) + DB_Length(xx, seq) > start && strand_matches(xx, seq, strand) && seq != exception) { if (get_trace_path(xx, seq, fileName, t_type) == 0) { form = trace_type_str2int(t_type); rlist[count].r = read_reading(fileName, form); if (rlist[count].r) { rlist[count].seq = DBgetSeq(DBI(xx), seq); rlist[count].opos = get_trace_pos(rlist[count].r, xx, seq, 0, DB_Start(xx, seq), DB_Start(xx, seq) + DB_Length(xx, seq), DB_Seq(xx, seq), 0); seqList[count++] = seq; } } } } if (i <= DBI_gelCount(xx)) next = i; else next = 0; /* * Loop along the sequence updating seqList as we go. * At each point we know how many sequences there are so we can * produce the consensus from these sequences. */ for (i = start; i <= end; i++) { w = do_cons_base(xx, con, i, start, count, seqList, rlist, r, offset, match, &max_points); if (w == -1) goto error; offset += w; /* Update seqList for the next position */ if (i < end) { /* Remove sequences */ for (j = 0; j < count; j++) { int seq = seqList[j]; if (DB_RelPos(xx, seq) + DB_Length(xx, seq) - 1 <= i) { read_deallocate(rlist[j].r); xfree(rlist[j].opos); memmove(&seqList[j], &seqList[j+1], (count-1-j) * sizeof(*seqList)); memmove(&rlist[j], &rlist[j+1], (count-1-j) * sizeof(*rlist)); count--; j--; } } /* Add sequences */ while (next && DB_RelPos(xx, next) <= i+1) { /* printf("next=%d %d %d\n", next, DB_RelPos(xx, next), i+1); */ DBgetSeq(DBI(xx), next); if (strand_matches(xx, next, strand) && get_trace_path(xx, next, fileName, t_type) == 0) { form = trace_type_str2int(t_type); rlist[count].r = read_reading(fileName, form); if (rlist[count].r) { rlist[count].seq = DBgetSeq(DBI(xx), next); rlist[count].opos = get_trace_pos(rlist[count].r, xx, next, 0, DB_Start(xx, next), DB_Start(xx,next)+DB_Length(xx,next), DB_Seq(xx, next), 0); seqList[count++] = next; } } if (++next > DBI_gelCount(xx)) next = 0; } } } for (i = 0; i < count; i++) { read_deallocate(rlist[i].r); xfree(rlist[i].opos); } tidy_up(r, end-start + 1, offset); xfree(con); xfree(rlist); return r; error: if (con) xfree(con); if (rlist) xfree(rlist); return NULL; }
/* * --------------------------------------------------------------------------- * Loads confidence values from the trace file and averages them. * 'opos' is optional - if not known then set to NULL. * * Returns 0 for success * -1 for failure */ int get_read_conf(Exp_info *e, int length, int2 *opos, int1 *conf) { int ttype, i; FILE *fp; uint_1 *prob_A, *prob_C, *prob_G, *prob_T; char *seq; float scf_version; int nbases = 0; /* Sanity check */ if (!(exp_Nentries(e,EFLT_LT) && exp_Nentries(e,EFLT_LN))) return -1; /* Find and load trace file */ ttype = trace_type_str2int(exp_get_entry(e, EFLT_LT)); if (ttype != TT_SCF && ttype != TT_ZTR) return -1; /* * We only support direct reading accuracy values from SCF files. * Otherwise we have to take a slower approach. */ if (ttype != TT_SCF) { Read *r; int sec = read_sections(0); read_sections(READ_BASES); if (NULL == (r = read_reading(exp_get_entry(e,EFLT_LN), TT_ANYTR))) { read_sections(sec); return -1; } prob_A = (int1 *)xmalloc(r->NBases); prob_C = (int1 *)xmalloc(r->NBases); prob_G = (int1 *)xmalloc(r->NBases); prob_T = (int1 *)xmalloc(r->NBases); seq = (char *)xmalloc(r->NBases); memcpy(prob_A, r->prob_A, r->NBases); memcpy(prob_C, r->prob_C, r->NBases); memcpy(prob_G, r->prob_G, r->NBases); memcpy(prob_T, r->prob_T, r->NBases); memcpy(seq, r->base, r->NBases); nbases = r->NBases; read_deallocate(r); read_sections(sec); } else { Header h; /* For SCF files we read directly - the above code would also do. */ if (NULL == (fp = open_trace_file(exp_get_entry(e,EFLT_LN), NULL))) return -1; /* Read the SCF header */ if (-1 == read_scf_header(fp, &h)) return -1; scf_version = scf_version_str2float(h.version); nbases = h.bases; /* Alloc memory */ prob_A = (uint_1 *)xmalloc(h.bases * sizeof(*prob_A)); prob_C = (uint_1 *)xmalloc(h.bases * sizeof(*prob_A)); prob_G = (uint_1 *)xmalloc(h.bases * sizeof(*prob_A)); prob_T = (uint_1 *)xmalloc(h.bases * sizeof(*prob_A)); seq = (char *)xmalloc(h.bases * sizeof(*seq)); if (NULL == prob_A || NULL == prob_C || NULL == prob_G || NULL == prob_T || NULL == seq) return -1; /* Load base scores */ if (scf_version >= 3.0) { /* * Version 3 base format: * num_bases * 4byte peak index * num_bases * prob_A * num_bases * prob_C * num_bases * prob_G * num_bases * prob_T * num_bases * base * num_bases * spare (x3) */ fseek(fp, (off_t)h.bases_offset + 4 * h.bases, SEEK_SET); if (h.bases != fread(prob_A, 1, h.bases, fp)) return -1; if (h.bases != fread(prob_C, 1, h.bases, fp)) return -1; if (h.bases != fread(prob_G, 1, h.bases, fp)) return -1; if (h.bases != fread(prob_T, 1, h.bases, fp)) return -1; if (h.bases != fread(seq, 1, h.bases, fp)) return -1; } else { int i; uint_1 buf[12]; /* * Version 2 base format * num_bases * base_struct, where base_struct is 12 bytes: * 0-3 peak_index * 4-7 prob_A/C/G/T * 8 base * 9- spare */ fseek(fp, (off_t)h.bases_offset, SEEK_SET); for (i = 0; (unsigned)i < h.bases; i++) { if (1 != fread(buf, 12, 1, fp)) return -1; prob_A[i] = buf[4]; prob_C[i] = buf[5]; prob_G[i] = buf[6]; prob_T[i] = buf[7]; seq[i] = buf[8]; } } fclose(fp); } /* Determine confidence values */ if (opos) { for (i=0; i<length; i++) { if (opos[i] == 0) { /* Inserted base, change to 0% */ conf[i] = 0; } else { switch(seq[opos[i]-1]) { case 'a': case 'A': conf[i] = prob_A[opos[i]-1]; break; case 'c': case 'C': conf[i] = prob_C[opos[i]-1]; break; case 'g': case 'G': conf[i] = prob_G[opos[i]-1]; break; case 't': case 'T': conf[i] = prob_T[opos[i]-1]; break; default: conf[i] = 2; } } } } else { int mlength = MIN(length, nbases); for (i=0; i < mlength; i++) { switch(seq[i]) { case 'a': case 'A': conf[i] = prob_A[i]; break; case 'c': case 'C': conf[i] = prob_C[i]; break; case 'g': case 'G': conf[i] = prob_G[i]; break; case 't': case 'T': conf[i] = prob_T[i]; break; case 'n': case 'N': case '-': conf[i] = (prob_A[i] + prob_C[i] + prob_G[i] + prob_T[i]) / 4; break; default: conf[i] = 2; } } for (; i < length; i++) conf[i] = 2; } xfree(prob_A); xfree(prob_C); xfree(prob_G); xfree(prob_T); xfree(seq); return 0; }
int main(int argc, char **argv) { Read* read; int i; if (argc != 2) { fprintf(stderr, "Usage: trace_dump <trace file>\n"); return 1; } read = read_reading( argv[1], TT_ANY ); if (read == NULL) { fprintf(stderr, "Tracedump was unable to open file %s\n", argv[1] ); return 1; } printf("[Trace]\n"); printf("%s\n", read->trace_name ); printf("\n[Header]\n"); printf("%d\t\t# format\n", read->format); printf("%d\t\t# NPoints\n", read->NPoints); printf("%d\t\t# NBases\n", read->NBases); printf("%d\t\t# NFlows\n", read->nflows); printf("%d\t\t# maxTraceVal\n", (int)read->maxTraceVal); printf("%d\t\t# baseline\n", read->baseline); printf("%d\t\t# leftCutoff\n", read->leftCutoff); printf("%d\t\t# rightCutoff\n", read->rightCutoff); puts("\n[Bases]"); for (i = 0; i < read->NBases; i++) { printf("%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) { puts("\n[A_Trace]"); for(i = 0; i < read->NPoints; i++) printf("%d\t#%5d\n", (int)read->traceA[i], i); puts("\n[C_Trace]"); for(i = 0; i < read->NPoints; i++) printf("%d\t#%5d\n", (int)read->traceC[i], i); puts("\n[G_Trace]"); for(i = 0; i < read->NPoints; i++) printf("%d\t#%5d\n", (int)read->traceG[i], i); puts("\n[T_Trace]"); for(i = 0; i < read->NPoints; i++) printf("%d\t#%5d\n", (int)read->traceT[i], i); } if (read->flow_order) { puts("\n[Flows]"); for (i = 0; i < read->nflows; i++) { printf("%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) { puts("\n[Info]"); printf("%s\n", read->info); } read_deallocate(read); return 0; }