/* interval.c Compile with: gcc -g interval.c -lm -o interval Example run: interval hs2_DEVIATION.X.l6 13 Given a "DEVIATION" file (agree false positives & false negatives data) and the number of characters in the denomination of the runs preceding the length indicator, it outputs the intervals [start,end] of invariance for the percent identity, with the following format: start end #fp #fn #fn+#fp Example: The denomination 'agree.hs2.X.l4.p37.list' in the DEVIATION file specifies an agree run for the hs2 region with the parameter values 4 for minimum length of reported region, and 37% for the required percent identity, respectively. Gaps are not allowed (X). The distance for the file is then 13 (strlen("agree.hs2.X.l")). */ #include #include #include #include #include #include #define EPSILON 1 #define Usage "%s " void fatal(char *message); void fatalf(char *msg, char *val); FILE *ckopen(char *name, char *mode); int get_percent(char *); int chars_to_length; int main(int argc, char *argv[]) { char buffer[200], key[50], *s; int percent, ppercent; int i, min, fp, fn, pfp, pfn, count; int From[1000], To[1000]; int FP[1000], FN[1000]; FILE *file; if (argc!=3) fatalf(Usage, argv[0]); chars_to_length = atoi(argv[2]); file = ckopen(argv[1],"r"); min = 100000; pfp = pfn = -1; count = -1; ppercent = -1; while (fgets(buffer,200,file)!=NULL) { if (buffer[0]=='#') continue; s = buffer; i = 0; while (!isspace(*s)) { key[i] = *s; s++; i++; } key[i] = '\0'; percent = get_percent(key); while (isspace(*s)) s++; /* go past blanks */ while (!isspace(*s)) s++; /* go past fp: label */ while (isspace(*s)) s++; /* go past blanks */ fp = 0; while (isdigit(*s)) { /* go past fp values */ fp = fp*10 + *s-'0'; s++; } while (isspace(*s)) s++; /* go past blanks */ while (!isspace(*s)) s++; /* go past fn: label */ while (isspace(*s)) s++; /* go past blanks */ fn = 0; while (isdigit(*s)) { /* go past fn values */ fn = fn*10 + *s-'0'; s++; } if ((fn!=pfn) || (fp!=pfp)) { /* new interval */ if (count>=0) To[count] = percent-EPSILON; From[++count] = percent; To[count] = percent; FP[count] = fp; FN[count] = fn; pfp = fp; pfn = fn; } else { /* same interval */ To[count] = percent; } } fclose(file); /* print out the values */ for (i= count; i>=0; i--) printf("%3d %3d %3d %3d %3d\n", From[count-i], To[count-i], FP[count-i], FN[count-i], FP[count-i]+FN[count-i]); return 0; } void fatal(char *msg) { (void)fprintf(stderr, "%s\n", msg); exit(1); } void fatalf(char *msg, char *val) { (void)fprintf(stderr, msg, val); (void)putc('\n', stderr); exit(1); } FILE *ckopen(char *name, char *mode) { FILE *fp; if ((fp = fopen(name, mode)) == NULL) fatalf("Cannot open %s.", name); return fp; } /* prune key */ int get_percent(char *key) { int value, decs, sign = 1; char *s; s = key; s = s+chars_to_length; while (isdigit(*s)) s++; /* go past length field */ s++; s++; /* go past '.' and the 'p' */ if (*s=='-') { sign = -1; s++; } value = 0; while (isdigit(*s)) { value = value*10+*s-'0'; s++; } s++; /* replace '.' and skip it */ /* decs = 0; while (isdigit(*s)) { value = value*10+*s-'0'; s++; decs++; } rval = value; while (--decs>=0) rval = rval/10; */ return sign*value; }