PQ * enlarge_pqueue(PQ **pq, puint32 nbucks) { PQ *q1, *q=*pq; puint32 i; int first_i; puint32 bucket; q1=new_pqueue(nbucks, q->l.nelems, q->l.cost); if (!q1) { animal_err_register ("enlarge_pqueue", ANIMAL_ERROR_FAILURE,""); return NULL; } q1->c.mincost = q->c.mincost; q1->c.maxcost = q->c.maxcost; q1->c.tiebreak = q->c.tiebreak; for (i=0; i < q->c.nbuckets+1; i++) if (q->c.first[i] != NIL) { first_i = q->c.first[i]; bucket = q->l.cost[first_i] % q->c.nbuckets; q1->c.first[bucket] = first_i; q1->c.last[bucket] = q->c.last[i]; } for (i=0; i < q->l.nelems; i++) q1->l.elem[i] = q->l.elem[i]; free_pqueue(pq); return q1; }
void prediction_stat (char *AB, pst_type T, const int n, const char *sfile, const char *ifile, const char *s0file, const char *i0file, const int s1, const int s2, int s3, double *non1p, double *non2p, double *non12p, double *iso1p, double *iso2p, double *iso12p) { int e1, e2, e3, i, cbuflen, dum1, dum2; char cbuf[STRINGLEN_MAX+1], f1, f2, f12; double a, amin, iso3; pQ_type Q; FILE *fp, *ifp; amin = -DBL_MAX; Q = make_empty_pqueue(); e1 = 0; /* train */ e2 = 0; /* test */ e3 = 0; /* others */ if ((fp = fopen(s0file, "r")) == NULL) { perror("failed to open others strings file for read"); exit(1); } if ((ifp = fopen(i0file, "r")) == NULL) { perror("failed to open others names file for read"); exit(1); } dum1 = 0; while ((dum1<s3) && (fscanf(fp,"%s",cbuf) != EOF)) { if ((cbuflen = strlen(cbuf)) > STRINGLEN_MAX) { fprintf(stderr, "single string size exceeds STRINGLEN_MAX = %d (may update and re-compile)\n", STRINGLEN_MAX); exit(1); } if (strspn(cbuf,AB)!=cbuflen) { fprintf(stderr,"found a rejected string\n"); exit(1); } if ((fscanf(ifp,"%*s %*s %*d %d",&i)) == EOF) /* id acc len cln */ { fprintf(stderr,"names file is shorter then others string file\n"); exit(1); } if (i != n) { dum1++; a = log10like_on_pst(AB, T, cbuf)/cbuflen; if (a>amin) amin = a; enqueue_pqueue(&Q, NULL, a, 3, 0,0); } } fclose(fp); fclose(ifp); /* we disable so that PFAM could be run... if (dum1 < s3) { fprintf(stderr,"not enough strings in 0.strings\n"); exit(1); } */ if ((fp = fopen(sfile, "r")) == NULL) { perror("failed to open cluster <strings_file> for read"); exit(1); } if ((ifp = fopen(ifile, "r")) == NULL) { perror("failed to open cluster <index_file> for read"); exit(1); } while (fscanf(fp,"%s",cbuf) != EOF) { if ((cbuflen = strlen(cbuf)) > STRINGLEN_MAX) { fprintf(stderr, "single string size exceeds STRINGLEN_MAX = %d (may update and re-compile)\n", STRINGLEN_MAX); exit(1); } if (strspn(cbuf,AB)!=cbuflen) { fprintf(stderr,"found a rejected string\n"); exit(1); } if ((fscanf(ifp,"%d",&i)) == EOF) /* 0=train and 1=test */ { fprintf(stderr,"<index_file> is shorter then <string_file>\n"); exit(1); } a = log10like_on_pst(AB, T, cbuf)/cbuflen; if (a<=amin) { enqueue_pqueue(&Q, NULL, a, i+1, 0,0); ((i == 0) ? e1++ : e2++); } } fclose(fp); fclose(ifp); *non1p = 1.0*e1; /* /s1; */ *non2p = 1.0*e2; /* /s2; */ *non12p = 1.0*(e1+e2); /* /(s1+s2); */ *iso1p = *non1p; *iso2p = *non2p; *iso12p = *non12p; iso3 = 0.0; f1 = (*iso1p > iso3); f2 = (*iso2p > iso3); f12 = (*iso12p > iso3); while (f1+f2+f12 > 0) { dequeue_pqueue(&Q, NULL, &a, &i, &dum1, &dum2); switch(i) { case 1: e1--; if (f1) *iso1p = 1.0*e1; /* /s1; */ if (f12) *iso12p = 1.0*(e1+e2); /* /(s1+s2); */ break; case 2: e2--; if (f2) *iso2p = 1.0*e2; /* /s2; */ if (f12) *iso12p = 1.0*(e1+e2); /* /(s1+s2); */ break; case 3: e3++; iso3 = 1.0*e3; /* /s3; */ break; } f1 = (*iso1p > iso3); f2 = (*iso2p > iso3); f12 = (*iso12p > iso3); } free_pqueue(Q); free(cbuf); return; }
void pred_stat_save (char *AB, pst_type T, const int n, const char *sfile, const char *ifile, const char *s0file, const char *i0file, int s1, int s2, int s3, double *non1p, double *non2p, double *non12p, double *iso1p, double *iso2p, double *iso12p, const char *wfile, const char *w0file, const int tsize) { int e1, e2, e3, i, cbuflen, dum1, dum2; char cbuf[STRINGLEN_MAX+1], f1, f2, f12, nam[20], acc[20]; /* PFAM disabled comb[40]; */ double a, amin, iso3, y; pQ_type Q; /* PFAM disabled Qh; */ FILE *fp, *ifp, *wfp; amin = -DBL_MAX; Q = make_empty_pqueue(); e1 = 0; /* train */ e2 = 0; /* test */ e3 = 0; /* others */ if ((wfp = fopen(w0file, "w")) == NULL) { perror("failed to open others output file for write"); exit(1); } if ((fp = fopen(s0file, "r")) == NULL) { perror("failed to open others strings file for read"); exit(1); } if ((ifp = fopen(i0file, "r")) == NULL) { perror("failed to open others names file for read"); exit(1); } dum1 = 0; while (fscanf(fp,"%s",cbuf) != EOF) { if ((cbuflen = strlen(cbuf)) > STRINGLEN_MAX) { fprintf(stderr, "single string size exceeds STRINGLEN_MAX = %d (may update and re-compile)\n", STRINGLEN_MAX); exit(1); } if (strspn(cbuf,AB)!=cbuflen) { fprintf(stderr,"found a rejected string\n"); exit(1); } if ((fscanf(ifp,"%s %s %d %d",nam, acc, &dum2, &i)) == EOF) { fprintf(stderr,"names file is shorter then others string file\n"); exit(1); } if ((i != n) && (dum1<s3)) { dum1++; y = log10like_on_pst(AB, T, cbuf); fprintf(wfp, "%f\n", y); /* a = y/cbuflen; if (a>amin) amin = a; sprintf(comb, "%15s %10s", nam, acc); enqueue_pqueue(&Q, comb, a, 3, i, dum2); */ } /* id_acc kind cluster len */ else fprintf(wfp, "%f\n", 1.0); } fclose(fp); fclose(ifp); fclose(wfp); /* printf("\nthe %d most related others strings:\n \t slope\t len\tcluster\tname\n", tsize/2); Qh = Q; for (i=1; (i<=tsize/2) && (Qh!=NULL); i++) { printf("(%d)\t%5.3f\t%5d\t%5d\t%s\n", i, -Qh->p_S, Qh->chi_sufSx, Qh->chi_xS,Qh->S); Qh = Qh->nextp; } if (dum1 < s3) { fprintf(stderr,"not enough strings in 0.strings\n"); exit(1); } */ if ((wfp = fopen(wfile, "w")) == NULL) { perror("failed to open cluster output file for write"); exit(1); } if ((fp = fopen(sfile, "r")) == NULL) { perror("failed to open cluster <strings_file> for read"); exit(1); } if ((ifp = fopen(ifile, "r")) == NULL) { perror("failed to open cluster <index_file> for read"); exit(1); } while (fscanf(fp,"%s",cbuf) != EOF) { if ((cbuflen = strlen(cbuf)) > STRINGLEN_MAX) { fprintf(stderr, "single string size exceeds STRINGLEN_MAX = %d (may update and re-compile)\n", STRINGLEN_MAX); exit(1); } if (strspn(cbuf,AB)!=cbuflen) { fprintf(stderr,"found a rejected string\n"); exit(1); } if ((fscanf(ifp,"%d",&i)) == EOF) /* 0=train and 1=test */ { fprintf(stderr,"<index_file> is shorter then <string_file>\n"); exit(1); } y = log10like_on_pst(AB, T, cbuf); fprintf (wfp, "%f\n", y); /* a = y/cbuflen; if (a<=amin) { enqueue_pqueue(&Q, NULL, a, i+1, 0,0); ((i == 0) ? e1++ : e2++); } */ } fclose(fp); fclose(ifp); fclose(wfp); return; /* PFAM!!! */ *non1p = 1.0*e1; /* /s1; */ *non2p = 1.0*e2; /* /s2; */ *non12p = 1.0*(e1+e2); /* /(s1+s2); */ *iso1p = *non1p; *iso2p = *non2p; *iso12p = *non12p; iso3 = 0.0; f1 = (*iso1p > iso3); f2 = (*iso2p > iso3); f12 = (*iso12p > iso3); while (f1+f2+f12 > 0) { dequeue_pqueue(&Q, NULL, &a, &i, &dum1, &dum2); switch(i) { case 1: e1--; if (f1) *iso1p = 1.0*e1; /* /s1; */ if (f12) *iso12p = 1.0*(e1+e2); /* /(s1+s2); */ break; case 2: e2--; if (f2) *iso2p = 1.0*e2; /* /s2; */ if (f12) *iso12p = 1.0*(e1+e2); /* /(s1+s2); */ break; case 3: e3++; iso3 = 1.0*e3; /* /s3; */ break; } f1 = (*iso1p > iso3); f2 = (*iso2p > iso3); f12 = (*iso12p > iso3); } free_pqueue(Q); free(cbuf); return; }
/* * Single source shortest path algorithm (Dijkstra's algorithm). */ static int shortest_path(double *nodeArray, int num_nodes, double *edgeArray, int num_edges, int start_index, double dimdist[3], double radius, double *lastNodeList, int geoflag) { int i, cnt, num_nbhrs, curNodeIdx, curNhbrIdx, firstNodeOffset; double *node, *new_node, *nbhrs, *nbhrdists, new_dist; PQueue *PQ; /* Initialize distance to HUGE_VAL. */ for (i=0, node=&nodeArray[DIST]; i<num_nodes; i++, node+=NUM_ATTRS) *node = HUGE_VAL; /* Allocate priority queue and insert start_index as first node. */ PQ = make_pqueue(num_nodes); node = NODEN(nodeArray,start_index-1); node[DIST] = 0.0; if (lastNodeList != NULL) { lastNodeList[start_index-1] = 0; } pqueue_insert(PQ, (PQueueNode)node); firstNodeOffset = ((int)NODEN(nodeArray,0)); /* Dijkstra's algorithm. */ cnt = 0; while (!pqueue_empty_p(PQ)) { node = (double *) pqueue_extract_min(PQ); curNodeIdx = (int)(((int)(node) - firstNodeOffset) / (8*NUM_ATTRS)) + 1; node[DIST] = -node[DIST]; /* computed */ cnt++; /* Relax all the neighboring nodes. */ num_nbhrs = (int) node[NUM_NBHRS]; /* Get the geodesic distances to the neighbors. */ if (geoflag == 1) { nbhrs = EDGEN(edgeArray,(((int)node[NBHRS])-1)); nbhrdists = EDGEN(edgeArray,(((int)node[NBHRS])-1)) + 1; } else { nbhrs = &edgeArray[((int)node[NBHRS])-1]; } for (i=0; i<num_nbhrs; i++) { if (geoflag == 1) { curNhbrIdx = ((int)nbhrs[2*i]); new_node = NODEN(nodeArray,((int)nbhrs[2*i])-1); } else { curNhbrIdx = (((int)nbhrs[i])); new_node = NODEN(nodeArray,((int)nbhrs[i])-1); } if (new_node[DIST]>=0) { /* not computed yet */ /* node[DIST] has been negated a couple of lines above. * so, we need to negate it again to get the actual * (positive) distance. * -node[DIST] is the distance from our working node * to the start point. node_dist() is the distance * between our working node and its ith neighbor. */ /* 08.05.98 SJC * If geoflag == 1, use the geodesic edge distance. */ if (geoflag == 1) { new_dist = -node[DIST] + nbhrdists[2*i]; } else { new_dist = -node[DIST] + node_dist(node, new_node, dimdist); } if (new_dist<radius) { /* If this is the first time that we've gotten here, we * use this new distance and add the node into the * priority queue. */ if (new_node[DIST]==HUGE_VAL) { new_node[DIST] = new_dist; if (lastNodeList != NULL) { lastNodeList[curNhbrIdx - 1] = curNodeIdx; } pqueue_insert(PQ, (PQueueNode)new_node); } /* Otherwise, if the new distance is less than * the previously computed distance, then we pick * the new (shorter) distance and adjust its * position in the priority queue. */ else { if (new_dist<new_node[DIST]) { new_node[DIST] = new_dist; if (lastNodeList != NULL) { lastNodeList[curNhbrIdx - 1] = curNodeIdx; } pqueue_deckey(PQ, (PQueueNode)new_node); } } } } } } free_pqueue(PQ); return(cnt); }