int putsPivot( int * tab, int begin, int end ) { int i; int p = begin; for( i = p+1; i < end; ++i ) if( tab[i] < tab[p] ) { swapi( &tab[i], &tab[p] ); swapi( &tab[i], &tab[p+1] ); ++p; } return p; }
void bubble_sort( int *t, int size ) { int i,j; for(i=0; i<size-1; ++i) for(j=size-1; j>i; j--) if(t[j]<t[j-1]) swapi(&t[j],&t[j-1]); }
void swim(int a[], int k) { while (k > 1 && lessi(a, k / 2, k)) { swapi(a, k, k / 2); k /= 2; } }
int nbt_write_int(nbt_file *nbt, int32_t *val) { int32_t temp = *val; if (get_endianness() == L_ENDIAN) swapi((uint32_t *)&temp); return gzwrite(nbt->fp, &temp, sizeof(int32_t)); }
int nbt_read_byte_array(nbt_file *nbt, unsigned char **out) { int32_t len; gzread(nbt->fp, &len, sizeof(len)); if (get_endianness() == L_ENDIAN) swapi((uint32_t *)&len); *out = malloc(len); gzread(nbt->fp, *out, len); return len; }
int nbt_read_int(nbt_file *nbt, int32_t **out) { int32_t t; gzread(nbt->fp, &t, sizeof(t)); if (get_endianness() == L_ENDIAN) swapi((uint32_t *)&t); *out = malloc(sizeof(int32_t)); memcpy(*out, &t, sizeof(int32_t)); return 0; }
int del_max(int a[]) { // get the max int max = a[1]; // swap first (root) with last elem swapi(a, 1, N); a[N--] = DEFAULT_INT; // sink the new root to the correct position sink(a, 1); return max; }
int partition(int array[], int lo, int hi) { int i = lo; int j = hi + 1; while (1) { while (lessi(array, ++i, lo)) if (i == hi) break; while (lessi(array, lo, --j)) if (j == lo) break; if (i >= j) break; swapi(array, i, j); } swapi(array, lo, j); return j; }
void sink(int a[], int k) { while (2 * k <= N) { int j = 2 * k; // children of k are 2k and 2k + 1 if (j < N && lessi(a, j, j + 1)) ++j; if (lessi(a, j, k)) break; swapi(a, k, j); k = j; } }
static void switchtonextlevel(void) { struct initrec** inittab = cfg->inittab; struct initrec** pp; struct initrec* p; /* One we're here, reset pid for r-type entries, to run them when entering another runlevel with shouldberunning(p) true. */ for(pp = inittab; (p = *pp); pp++) if(!shouldberunning(p) && once(p) && (p->pid < 0)) p->pid = 0; if(slippery(nextlevel) && !slippery(currlevel)) { /* nextlevel is slippery, turn back to currlevel */ swapi(&currlevel, &nextlevel); /* We've got to make sure pollfds will return immediately. */ timetowait = 0; } else { currlevel = nextlevel; } }
int32_t nbt_read_list(nbt_file *nbt, char *type_out, void ***target) { char type; int32_t len; int i; gzread(nbt->fp, &type, 1); *type_out = type; gzread(nbt->fp, &len, sizeof(len)); if (get_endianness() == L_ENDIAN) swapi((uint32_t *)&len); *target = malloc(len * sizeof(void *)); for (i = 0; i < len; ++i) nbt_read(nbt, type, &((*target)[i])); return len; }
int main(int argc, char* argv[]) { char cpsfilename[MAXBUFFLENGTH]; char chainfilename[MAXBUFFLENGTH]; char outfilename[MAXBUFFLENGTH]=""; int marginlength = 0; int MAXREC; char c; char *pc; int x; char buff[MAXBUFFLENGTH+1]; char aux[MAXBUFFLENGTH+1]; long score; int start1,end1,len1,start2,end2,len2; char strand1, strand2, chr1[MAXBUFFLENGTH], chr2[MAXBUFFLENGTH]; int *size, *dq, *dt; int a,b,k,i,j,s,m; int *position; char *strand; int *ids; int *idg; char *type; int chridx[MAXCHR+1]; int chroff[MAXCHR+1]; char resstr; int rescrd; if(argc==1) { fprintf(stderr,"Finds matches of the given set of sites (CPS file) in the BLASTZ chain alignment (CHAIN file)\n"); fprintf(stderr,"Last update by (dp) on Sep 21, 2011\n"); fprintf(stderr,"Keys:\n -i CPS file (remember to sort by position in ascending order)\n -d CHAIN alignment file\n -o output file\n"); fprintf(stderr," -m margin length [0]\n -v suppress verbose output [NO]\n"); exit(1); } timestamp_set(); for(i=1;i<argc;i++) { pc = argv[i]; if(*pc != '-') continue; if(*(pc+1) == 'i') { sscanf(argv[++i], "%s", &cpsfilename[0]); } if(*(pc+1) == 'd') { sscanf(argv[++i], "%s", &chainfilename[0]); } if(*(pc+1) == 'o') { sscanf(argv[++i], "%s", &outfilename[0]); } if(*(pc+1) == 'm') { sscanf(argv[++i], "%i", &marginlength); } if(*(pc+1) == 'v') { verbose=0; } } if(outfilename[0]==0) { fprintf(stderr,"No output file privided, exiting\n"); exit(1); } outfile = fopen(outfilename,"w"); if(outfile == NULL) { fprintf(stderr,"Can't open output file, exiting\n"); exit(1); } for(i=0;i<MAXCHR;i++) { chridx[i]=chroff[i]=0; } MAXREC = 0; cpsfile= fopen(cpsfilename,"r"); if(cpsfile==NULL) { fprintf(stderr,"Can't access CPS file. Exiting\n"); exit(1); } if(verbose) fprintf(stderr,"Reading CPS input pass 1"); while(!feof(cpsfile)) { buff[0]=0; fgets(buff,MAXBUFFLENGTH,cpsfile); if(strlen(buff)<2) break; sscanf(buff,"%s" , aux); chridx[assign_code(aux)]++; MAXREC++; } fclose(cpsfile); for(s=i=0;i<MAXCHR;i++) { x = chridx[i]; chridx[i] =s; s+=x; } chridx[i] = s; position = (int*) malloc(sizeof(int)*(s+4)); strand = (char*) malloc(sizeof(char)*(s+4)); type = (char*) malloc(sizeof(char)*(s+4)); ids = (int*) malloc(sizeof(int)*(s+4)); idg = (int*) malloc(sizeof(int)*(s+4)); if(position==NULL || strand==NULL || type==NULL || ids==NULL || idg==NULL) { fprintf(stderr,"Not enough memory. Terminated\n"); exit(1); } cpsfile= fopen(cpsfilename,"r"); if(verbose) fprintf(stderr,", records = %i\nReading CPS input pass 2",MAXREC); while(!feof(cpsfile)) { buff[0]=0; fgets(buff,MAXBUFFLENGTH,cpsfile); if(strlen(buff)<2) break; sscanf(buff,"%s" , aux); i = assign_code(aux); m = chridx[i]+chroff[i]; sscanf(buff,"%*s %i %c %i %i %c" , position+m, strand+m, idg+m, ids+m, type+m); chroff[i]++; } fclose(cpsfile); if(verbose) fprintf(stderr,"\nSorting segments"); /* for(i=0;i<MAXCHR;i++) { quickSort_ic(position,strand,chridx[i],chridx[i+1]-1); } */ for(i=0;i<MAXCHR;i++) { k=1; while(k) { k=0; for(j=chridx[i];j<chridx[i+1]-1;j++) { if(position[j]>position[j+1]) { k=1; swapi(position+j,position+j+1); swapc(strand+j,strand+j+1); swapc(type+j,type+j+1); swapi(ids+j,ids+j+1); swapi(idg+j,idg+j+1); } } } } if(verbose) fprintf(stderr," done\nProcessing chains"); /**********************************************************************************************/ size = (int*) malloc(sizeof(int)*MAXALN); dq = (int*) malloc(sizeof(int)*MAXALN); dt = (int*) malloc(sizeof(int)*MAXALN); if(size ==0 || dq ==0 || dt==0) { fprintf(stderr,"Not enough memory for such long chains. Terminated\n"); exit(1); } /**********************************************************************************************/ chainfile = fopen(chainfilename,"r"); while(!feof(chainfile)) { buff[0]=0; fgets(buff,MAXBUFFLENGTH,chainfile); if(strlen(buff)<2) break; buff[5]=0; if(strcmp(buff,"chain")==0) { sscanf(buff+6,"%li %s %i %c %i %i %s %i %c %i %i",&score, &chr1[0], &len1, &strand1, &start1, &end1, &chr2[0], &len2, &strand2, &start2, &end2); k=0; while(!feof(chainfile)) { buff[0]=0; fgets(buff,MAXBUFFLENGTH,chainfile); if(strlen(buff)<2) break; sscanf(buff,"%i %i %i",&size[k],&dt[k],&dq[k]); k++; if(k>MAXALN) { fprintf(stderr,"Chain length exceeded. Terminating"); exit(1); } } x = get_chr_code(chr1); if(x<0) continue; a=start1;b=start2; j=0; for(i=chridx[x];i<chridx[x+1] && position[i]<start1;i++); for(;i<chridx[x+1]&& position[i]<end1;i++) { while(position[i]>a+size[j]+dt[j] && j<k){ a+=size[j]+dt[j]; b+=size[j]+dq[j]; j++; } if(j>=k) break; if(position[i]-a > marginlength && a+size[j]-position[i] >= marginlength) { if(strand1==strand2) { resstr = strand[i]; rescrd = position[i] - a + b; } else { resstr = (strand[i]=='+') ? '-' : '+'; rescrd = len2 - (position[i] - a + b - 1) ; } fprintf(outfile,"%s\t%i\t%c\t%s\t%i\t%c\t%i\t%i\t%c\t%li\n",chr1, position[i], strand[i],chr2,rescrd, resstr, idg[i], ids[i],type[i],score); } } } } if(verbose) fprintf(stderr," done\n"); fclose(chainfile); fclose(outfile); timestamp_report(); exit(0); }
double minver(double a[], int l, int m, double eps) { int i, iw, j, k, *p, r, s, t, u, v, *work; double api, pivot, *q, *q1, w, w1, wmax; if(m < 2 || l < m || eps <= 0.) { fprintf(stderr, "Error : Illegal parameter in minver()\n"); return 0.; } work = (int *)malloc(m * sizeof(int)); if(work == NULL) { fprintf(stderr, "Error : Out of memory in minver()\n"); return 0.; } w1 = 1.; for(i = 0, p = work; i < m; i++) *p++ = i; for(k = 0, u = 0; k < m; k++, u += l) { wmax = 0.; for(i = k; i < m; i++) { w = fabs(*(a + i * l + k)); if(w > wmax) { wmax = w; r = i; } } api = fabs(pivot = *(a + r * l + k)); if(api < eps) { fprintf(stderr, "Error : api < eps in minver()\n"); free((char *)work); return w1; } w1 *= pivot; v = r * l; if(r != k) { w1 = -w1; swapi(work + k, work + r); for(j = 0, q = a + u, q1 = a + v; j < m; j++) swapd(q++, q1++); } for(i = 0, q = a + u; i < m; i++) *q++ /= pivot; for(i = 0, v = 0; i < m; i++, v += l) { if(i != k) { s = v + k; w = *(a + s); if(w != 0.) { for(j = 0, q = a + u, q1 = a + v; j < m; j++, q++, q1++) if (j != k) *q1 -= w * *q; *(a + s) = - w / pivot; } } } *(a + u + k) = 1. / pivot; } for(i = 0; i < m; i++) { for(;;) { k = *(work + i); if(k == i) break; swapi(work + k, work + i); for(j = 0, u = 0; j < m; j++, u += l) swapd(a + u + i, a + u + k); } } free((char *)work); return w1; }
int main(int argc, char* argv[]) { char cps_file_name[MAXBUFFLENGTH]; char chain_file_name[MAXBUFFLENGTH]; char out_file_name[MAXBUFFLENGTH]=""; int marginlength = 0; char buff[MAXBUFFLENGTH+1]; char aux[MAXBUFFLENGTH+1]; long score; int start1,end1,len1,start2,end2,len2; char strand1, strand2, chr1[MAXBUFFLENGTH], chr2[MAXBUFFLENGTH]; int *size, *dq, *dt; int a,b,k,i,j,s,m,x; int *position; int *strand; int *idg; int *ids; char *type; char c; int chridx[MAXCHR+1]; int chroff[MAXCHR+1]; char resstr; int rescrd; long chain_id; if(argc==1) { fprintf(stderr,"This utility does liftOver of coordinates (cps) by using chain alignment\n"); fprintf(stderr,"Gene information is included in the output\n"); fprintf(stderr,"Last update by Dmitri Pervouchine ([email protected]) on Mar 22, 2013\n"); fprintf(stderr,"Usage: %s -in <cps_file> -chain <chain_alignment_file> [-margin <length>] [-quiet]\n", argv[0]); fprintf(stderr," -in cps6, i.e. chr1/position1/strand1/gene/site/type tab-delimited file, strand is +/-\n"); fprintf(stderr," -chain UCSC chain alignment file, species1=>2\n"); fprintf(stderr," -out <output_file> [default=stdout]\n"); fprintf(stderr," -margin margin length [default=0]\n -quiet suppress verbose output [default=NO]\n"); fprintf(stderr,"NOTE: Input has to be sorted by position!\n"); fprintf(stderr,"Output format cps3+cps6: chr1/position1/strand1/chr2/position2/strand2/gene/site/type/score\n"); exit(1); } timestamp_set(); for(i=1;i<argc;i++) { if(strcmp(argv[i],"-in")==0) { sscanf(argv[++i], "%s", &cps_file_name[0]); } if(strcmp(argv[i],"-chain")==0) { sscanf(argv[++i], "%s", &chain_file_name[0]); } if(strcmp(argv[i],"-out")==0) { sscanf(argv[++i], "%s", &out_file_name[0]); } if(strcmp(argv[i],"-margin")==0) { sscanf(argv[++i], "%i", &marginlength); } if(strcmp(argv[i],"-quiet")==0) { verbose=0; } } if(out_file_name[0]==0) { fprintf(stderr,"[WARNING: output file not specified, redirect to stdout]\n"); out_file = stdout; } else { out_file = fopen(out_file_name,"w"); if(out_file == NULL) { fprintf(stderr,"[ERROR: output file %s cannot be opened for writing, exiting]\n", out_file_name); exit(1); } if(verbose) fprintf(stderr,"[>%s]\n",out_file_name); } cps_file= fopen(cps_file_name,"r"); if(cps_file==NULL) { fprintf(stderr,"[ERROR: cannot access %s, exiting]\n", cps_file_name); exit(1); } for(i=0;i<MAXCHR;i++) chridx[i] = chroff[i] = 0; if(verbose) fprintf(stderr,"[<%s, pass 1",cps_file_name); while(fgets(buff,MAXBUFFLENGTH,cps_file)) { if(strlen(buff)<2) break; sscanf(buff,"%s" , aux); chridx[assign_code(aux)]++; } if(verbose) fprintf(stderr,"]\n"); for(s=i=0;i<MAXCHR;i++) { x = chridx[i]; chridx[i] =s; s+=x; } chridx[i] = s; position = (int*) malloc(sizeof(int)*(s + ARRAY_MARGIN)); strand = (int*) malloc(sizeof(int)*(s + ARRAY_MARGIN)); ids = (int*) malloc(sizeof(int)*(s + ARRAY_MARGIN)); idg = (int*) malloc(sizeof(int)*(s + ARRAY_MARGIN)); type = (char*) malloc(sizeof(char)*(s + ARRAY_MARGIN)); if(position==NULL || strand==NULL || type==NULL || ids==NULL || idg==NULL) { fprintf(stderr,"[ERROR: failed to create index tables, exiting]\n"); exit(1); } fseek (cps_file, 0, SEEK_SET); if(verbose) fprintf(stderr,"[<%s, pass 2", cps_file_name); while(fgets(buff,MAXBUFFLENGTH,cps_file)) { if(strlen(buff)<2) break; sscanf(buff,"%s" , aux); i = assign_code(aux); m = chridx[i]+chroff[i]; sscanf(buff,"%*s %i %c %i %i %c" , &position[m], &c,&idg[m],&ids[m],&type[m]); strand[m] = strand_c2i(c); chroff[i]++; } fclose(cps_file); if(verbose) fprintf(stderr,"]\n"); if(verbose) fprintf(stderr,"[Sort by position (if not done before)"); for(i=0;i<MAXCHR;i++) { k=1; while(k) { k=0; for(j=chridx[i];j<chridx[i+1]-1;j++) { if(position[j]>position[j+1]) { k=1; swapi(position+j,position+j+1); swapi(strand+j,strand+j+1); swapi(idg+j,idg+j+1); swapi(ids+j,ids+j+1); swapc(type+j,type+j+1); } } } } if(verbose) fprintf(stderr,"]\n"); /**********************************************************************************************/ size = (int*) malloc(sizeof(int)*(MAXALN + ARRAY_MARGIN)); dq = (int*) malloc(sizeof(int)*(MAXALN + ARRAY_MARGIN)); dt = (int*) malloc(sizeof(int)*(MAXALN + ARRAY_MARGIN)); if(size ==0 || dq ==0 || dt==0) { fprintf(stderr,"[ERROR: not enough memory for chains, exiting]\n"); exit(1); } /**********************************************************************************************/ chain_file = fopen(chain_file_name,"r"); if(chain_file==NULL) { fprintf(stderr,"[ERROR: cannot access %s, exiting]\n", chain_file_name); exit(1); } fseek(chain_file, 0, SEEK_END); unsigned int last_pos = ftell(chain_file); fseek(chain_file, 0, SEEK_SET); while(fgets(buff,MAXBUFFLENGTH,chain_file)) { if(strlen(buff)<2) break; buff[5]=0; if(strcmp(buff,"chain")==0) { sscanf(buff+6,"%li %s %i %c %i %i %s %i %c %i %i %li",&score, &chr1[0], &len1, &strand1, &start1, &end1, &chr2[0], &len2, &strand2, &start2, &end2, &chain_id); k=0; while(fgets(buff,MAXBUFFLENGTH,chain_file)) { if(strlen(buff)<2) break; progressbar(ftell(chain_file), last_pos-1, (char*)"Processing ", verbose); sscanf(buff,"%i %i %i",size + k, dt + k, dq + k); k++; if(k>=MAXALN) { fprintf(stderr,"[ERROR: chain too long, exiting]\n"); exit(1); } } x = get_chr_code(chr1); if(x<0) continue; a=start1;b=start2; j=0; for(i=chridx[x];i<chridx[x+1] && position[i]<start1;i++); for(;i<chridx[x+1]&& position[i]<end1;i++) { while(position[i]>a+size[j]+dt[j] && j<k){ a+=size[j]+dt[j]; b+=size[j]+dq[j]; j++; } if(j>=k) break; if(position[i]-a > marginlength && a+size[j]-position[i] >= marginlength) { if(strand1==strand2) { resstr = strand[i]; rescrd = position[i] - a + b; } else { resstr = -strand[i]; rescrd = len2 - (position[i] - a + b - 1) ; } fprintf(out_file,"%s\t%i\t%c\t%s\t%i\t%c\t%i\t%i\t%c\t%li\n",chr1, position[i], strand_i2c(strand[i]), chr2, rescrd, strand_i2c(resstr), idg[i], ids[i],type[i],score); } } } } fclose(chain_file); fclose(out_file); timestamp_report(); free(size); free(dq); free(dt); free(position); free(strand); exit(0); }