Ejemplo n.º 1
0
int  allocate_shooter(void){
  int mem_points, mem_frames;

  FREEMEMORY(shootpointinfo);
  FREEMEMORY(shoottimeinfo);

  mem_points=max_shooter_points*sizeof(shootpointdata);
  mem_frames=nshooter_frames*sizeof(shoottimedata);

  PRINTF("shooter point memory requirements\n");
  PRINTF("max_shooter_points=%i mem=%i\n",max_shooter_points,mem_points);
  PRINTF("nshooter_frames=%i mem=%i\n",nshooter_frames,mem_frames);

  if(  mem_points<=0||mem_frames<=0||
#ifdef _DEBUG
       mem_points>=2000000000||mem_frames>2000000000||
#endif
    NewMemory((void **)&shootpointinfo,mem_points)==0||
    NewMemory((void **)&shoottimeinfo,mem_frames)==0){
    FREEMEMORY(shootpointinfo);
    FREEMEMORY(shoottimeinfo);
    shooter_active=0;
    PRINTF("shooter point memory allocation failed\n");
    return 1;
  }
  return 0;

}
Ejemplo n.º 2
0
void
freeMatchtype (void *space, void *data)
{
    Matchtype *d = (Matchtype*)data;

    FREEMEMORY(space, d->pos);
    FREEMEMORY(space, data);
}
Ejemplo n.º 3
0
void
getimbissblast(void *space, IntSequence *query, IntSequence **seqs, 
	Uint noofseqs, FAlphabet *alphabet, imbissinfo *imbiss){
  
	double *df, *sf, *scr;
	double avgsum=0, inputscr=0, lambda=0, K=0;
	Uint *sortind, i;
	evdparam *evd;

	/*frequency of query and database*/
	df = dbfreq(space, seqs, noofseqs, alphabet, 1);
	sf = seqfreq(space, query, alphabet);	
	scr  = logoddscr(space, df, sf, alphabet);
	
	for (i=0; i < alphabet->domainsize; i++) avgsum += df[i]*scr[i];
	for (i=0; i < query->length; i++) inputscr += scr[query->sequence[i]]; 
	
	sortind = quickSort(space, scr, alphabet->domainsize, cmp_dbl, NULL);
	
	evd=ALLOCMEMORY (space, NULL, evdparam, 1);
	evd->noofscores = alphabet->domainsize;
	evd->probs =  df;
	evd->scores = scr;

	lambda = uniroot(0, 1, score_evd, 0.0000001, evd); 	
	FREEMEMORY(space, evd);
	
	K = relentropy(space, sortind, scr, alphabet->domainsize, df, lambda);	
	if (K <= 0) K=1;

	imbiss->score = scr;
	imbiss->H = 0;
	imbiss->K = K;
	imbiss->lambda = lambda;

	
	printf("\nBLAST statistics:\n-------------------\n");
	printf("E(score): %f\n", avgsum);
	printf("inputscr: %f\n", inputscr);
	printf("lambda: %19.16e\n", lambda); 
	printf("check: %19.16e\n", 
	checklambda(scr, alphabet->domainsize, df, avgsum, lambda)); 
	printf("K: %19.16e\n\n", K);
	
	FREEMEMORY(space, sortind);
	FREEMEMORY(space, df);
	FREEMEMORY(space, sf);
	
	return;
}
Ejemplo n.º 4
0
void
bl_radixSortKeyFirst(void *space, void *tosrt, 
			 size_t size, size_t nelem, 
			 Uint bits) {
	
	char *p, *b, *src, *toSort;
	Uint *cast;
	
	Uint mask, offset=0, i, key;
	Uint cntsize;
	Uint *cnt;
	
    toSort = (char*) tosrt;
	cntsize = 1 << bits;
	cnt = ALLOCMEMORY(space, NULL, Uint, cntsize);

	memset(cnt, 0, sizeof(Uint)*cntsize);
	b = src = malloc(size*nelem);
	
	mask =~ (UINT_MAX<<bits);
	
	for(; mask; mask <<= bits, offset+=bits) {
		for(p=toSort; p < toSort+(nelem*size); p+=size) {
			cast = (Uint*)p;
			key = (*cast & mask) >> offset;
			++cnt[key];
		}
		
		for(i=1; i < cntsize; ++i) {
			cnt[i]+=cnt[i-1];
		}
		
		for(p=toSort+((nelem-1)*size); p >= toSort; p-=size) {
			cast = (Uint*)p;
			key = (*cast & mask) >> offset;
			memmove(b+((cnt[key]-1)*size), p, size);
			--cnt[key];
		}
		
		p=b; b=toSort; toSort=p;
		memset(cnt, 0, sizeof(Uint)*cntsize);
	}
	
	if(toSort == src) memcpy(b, toSort, size*nelem);
	FREEMEMORY(space, src);
	FREEMEMORY(space, cnt);
	
	return;
}
Ejemplo n.º 5
0
void free_skybox(void){
  int i;
  skyboxdata *skyi;

  for(i=0;i<nskyboxinfo;i++){
    int j;

    skyi = skyboxinfo + i;
    for(j=0;j<6;j++){
      FREEMEMORY(skyi->face[j].file);
    }
  }
  FREEMEMORY(skyboxinfo);
  nskyboxinfo=0;
}
Ejemplo n.º 6
0
void copy_args(int *argc, char **aargv, char ***argv_sv){
#ifdef WIN32
  char *filename=NULL;
  char **argv=NULL;
  int filelength=1024,openfile;
  int i;

  if(NewMemory((void **)&argv,(*argc+1)*sizeof(char **))!=0){
    *argv_sv=argv;
    for(i=0;i<*argc;i++){
      argv[i]=aargv[i];
    }
    if(*argc==1){
      if(NewMemory((void **)&filename,(unsigned int)(filelength+1))!=0){
        openfile=0;
        OpenSMVFile(filename,filelength,&openfile);
        if(openfile==1&&ResizeMemory((void **)&filename,strlen(filename)+1)!=0){
          *argc=2;
          argv[1]=filename;
        }
        else{
          FREEMEMORY(filename);
        }
      }
    }
  }
  else{
    *argc=0;
  }
#else
  *argv_sv=aargv;
#endif
}
Ejemplo n.º 7
0
/*------------------------------ destructSufArr ------------------------------
 *    
 * destruct a suffix array.
 * 
 */
void
destructSufArr (void *space, Suffixarray *arr)
{
    FREEMEMORY(space, arr->suftab);
	if (arr->lcptab != NULL)
	FREEMEMORY(space, arr->lcptab);
	if (arr->inv_suftab != NULL)
	FREEMEMORY(space, arr->inv_suftab);
	if (arr->suffixptr != NULL)
	FREEMEMORY(space, arr->suffixptr);
	if (arr->seq != NULL)
	destructMultiSeq(space, arr->seq);
	FREEMEMORY(space, arr);
	
	return ;
}
Ejemplo n.º 8
0
void loadskytexture(char *filebase, texturedata *texti){
  char *filebuffer=NULL;
  int texwid, texht;
  int errorcode;
  unsigned char *floortex;

  trim(filebase);
  texti->name=0;
  texti->loaded=0;
  if(strcmp(filebase,"NULL")==0)return;
  NewMemory((void **)&filebuffer,strlen(filebase)+1);
  STRCPY(filebuffer,filebase);

  glGenTextures(1,&texti->name);
  glBindTexture(GL_TEXTURE_2D,texti->name);
  floortex=readpicture(filebuffer,&texwid,&texht,0);
  if(floortex==NULL){
    FREEMEMORY(filebuffer);
    return;
  }
  errorcode=gluBuild2DMipmaps(GL_TEXTURE_2D,4, texwid, texht, GL_RGBA, GL_UNSIGNED_BYTE, floortex);
  if(errorcode!=0){
    FREEMEMORY(floortex);
    FREEMEMORY(filebuffer);
    return;
  }
  FREEMEMORY(floortex);
  glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
  glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
  #ifdef pp_GPU
  if(gpuactive==1){
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
  }
  else{
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP);
  }
  #else
  glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP);
  glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP);
  #endif
  texti->file=filebuffer;
  texti->loaded=1;
  return;
}
Ejemplo n.º 9
0
void
bl_radixSortUint(void *space, Uint *toSort, 
					 size_t nelem, 
					 Uint bits) {
	
	Uint *p, *b, *src;

	Uint mask, offset=0, i, key;
	Uint cntsize;
	Uint *cnt;
	
	cntsize = 1 << bits;
	cnt = ALLOCMEMORY(space, NULL, Uint, cntsize);
	
	memset(cnt, 0, sizeof(Uint)*cntsize);
	b = src = malloc(sizeof(Uint)*nelem);
	
	mask =~ (UINT_MAX<<bits);
	
	for(; mask; mask <<= bits, offset+=bits) {
		for(p=toSort; p < toSort+nelem; ++p) {
			key = (*p & mask) >> offset;
			++cnt[key];
		}
		
		for(i=1; i < cntsize; ++i) {
			cnt[i]+=cnt[i-1];
		}
		
		for(p=toSort+((nelem-1)); p >= toSort; --p) {			
			key = (*p & mask) >> offset;
			b[cnt[key]-1] = *p;
			--cnt[key];
		}
		
		p=b; b=toSort; toSort=p;
		memset(cnt, 0, sizeof(Uint)*cntsize);
	}
	
	if(toSort == src) memcpy(b, toSort, sizeof(Uint)*nelem);
	FREEMEMORY(space, src);
	FREEMEMORY(space, cnt);
	
	return;
}
Ejemplo n.º 10
0
void Get_Slice_Bounds(void) {
    int i;

    int endiandata;

    endiandata=getendian();
    if(endianswitch==1)endiandata=1-endiandata;

    PRINTF("Determining slice file bounds\n");
    for(i=0; i<nsliceinfo; i++) {
        slice *slicei;

        slicei = sliceinfo + i;
        slicei->inuse_getbounds=0;
    }
#ifdef pp_THREAD
    mt_update_slice_hist();
#else
    update_slice_hist();
#endif
    for(i=0; i<nsliceinfo; i++) {
        slice *slicei;
        int j;

        slicei = sliceinfo + i;
        if(slicei->dup==1)continue;
        for(j=i+1; j<nsliceinfo; j++) {
            slice *slicej;

            slicej = sliceinfo + j;
            if(strcmp(slicei->label.shortlabel,slicej->label.shortlabel)!=0)continue;
            merge_histogram(slicei->histogram,slicej->histogram);
        }
        slicei->valmax=get_histogram_value(slicei->histogram,0.99);
        slicei->valmin=get_histogram_value(slicei->histogram,0.01);
        slicei->setvalmax=1;
        slicei->setvalmin=1;
        for(j=i+1; j<nsliceinfo; j++) {
            slice *slicej;

            slicej = sliceinfo + j;
            if(strcmp(slicei->label.shortlabel,slicej->label.shortlabel)!=0)continue;
            slicej->valmax=slicei->valmax;
            slicej->valmin=slicei->valmin;
            slicej->setvalmax=1;
            slicej->setvalmin=1;
        }
    }
    for(i=0; i<nsliceinfo; i++) {
        slice *slicei;

        slicei = sliceinfo + i;
        FREEMEMORY(slicei->histogram);
    }

}
Ejemplo n.º 11
0
 int main(int argc, char** argv) {
	char* content;
	Uint contentlen,i,j;
	stringset_t *set, *set2, **csv;
    int space;	


	content = readfile(&space, "casessmall.rtxt", &contentlen);
	printf("read file of length: %d\n", contentlen);
	set = tokensToStringset(&space, "\n", content, contentlen);
	FREEMEMORY(&space, content);
	printf("file contained %d lines\n", set->noofstrings);
	
	for(i=0; i < set->noofstrings; i++) {
		
		set2 = tokensToStringset(&space, ",", set->strings[i].str, set->strings[i].len);
		for(j=0; j < set2->noofstrings; j++) {
			printf("%s,", set2->strings[j].str);
		}
		printf("\n");
		destructStringset(&space, set2);
	}
	destructStringset(&space, set);
		
	printf("reading csv ...\n");
	csv = readcsv(&space, "cases.rtxt", ",", &contentlen);
	
	for (i=0; i < contentlen; i++) {
		for(j=0; j < csv[i]->noofstrings; j++) {
			printf("%s",csv[i]->strings[j].str);
		}
		destructStringset(&space, csv[i]);
		printf("\n");
	}
	
	FREEMEMORY(&space, csv);
	
	return EXIT_SUCCESS;
 }
Ejemplo n.º 12
0
char * 
printSequence (void *space, CharSequence *s, Uint cols)
{
    Uint i, c, k, pos=0, l=0, width=0;
	char *buf, *buf2;
    stringset_t *entries;

	entries=initStringset(space);	
	for (i=0; i < s->length; i++)
	{	    
	  	/*buf = ALLOCMEMORY(space, NULL, char, 32);
	    buf = my_itoa(s->sequence[i], buf, 10);*/	
		addString(space, entries, s->sequence[i], strlen(s->sequence[i]));
		if(SETSTRLEN(entries, i) > width) 
			width = SETSTRLEN(entries, i);		
	} 
	
	/*add spacer*/
	width++;
	c = cols / (width+1);
	l = (s->descrlen+2)+(s->namelen+2)+(s->length*(width+1))+((entries->noofstrings/c)*(5+1+1));
	
	buf = ALLOCMEMORY(space, NULL, char, l);
	memset(&buf[pos++], '>', 1);
	memmove(&buf[pos], s->url, s->urllen);
	pos+=s->urllen;
	memset(&buf[pos++], '\n', 1);
	memset(&buf[pos++], '>', 1);
	memmove(&buf[pos], s->alphabetname, s->namelen);
	pos+=s->namelen;
	memset(&buf[pos++], '\n', 1);
	for (i=0; i < entries->noofstrings; i++) {
		if((i%c) == 0) {
			memset(&buf[pos++], '\n', 1);	
	    	buf2 = ALLOCMEMORY(space, NULL, char, 5);
			buf2 = my_itoa(i, buf2, 10);
			memset(&buf[pos], ' ', 5-strlen(buf2));
			pos += 5-strlen(buf2);
			memmove(&buf[pos], buf2, strlen(buf2));
			pos += strlen(buf2);
			memset(&buf[pos++], '\t', 1);
			FREEMEMORY(space, buf2);
		}
	    k = (width-SETSTRLEN(entries,i));
		memset(&buf[pos], ' ', k);
		pos += k;

	  	memmove(&buf[pos], SETSTR(entries, i), SETSTRLEN(entries, i));
		pos += SETSTRLEN(entries, i);
	}
Ejemplo n.º 13
0
void mt_compress_all(void){
  int i;
  pthread_t *thread_ids;
  int *index;

  NewMemory((void **)&thread_ids,mt_nthreads*sizeof(pthread_t));
  NewMemory((void **)&index,mt_nthreads*sizeof(int));
  NewMemory((void **)&threadinfo,mt_nthreads*sizeof(threaddata));

  for(i=0;i<mt_nthreads;i++){
    index[i]=i;
    pthread_create(&thread_ids[i],NULL,compress_all,&index[i]);
    threadinfo[i].stat=-1;
  }

  for(i=0;i<mt_nthreads;i++){
    pthread_join(thread_ids[i],NULL);
  }

  print_summary();
  FREEMEMORY(thread_ids);
  FREEMEMORY(index);
  FREEMEMORY(threadinfo);
}
Ejemplo n.º 14
0
void mt_update_slice_hist(void) {
    pthread_t *thread_ids;
    int i;

    NewMemory((void **)&thread_ids,mt_nthreads*sizeof(pthread_t));

    for(i=0; i<mt_nthreads; i++) {
        pthread_create(&thread_ids[i],NULL,MT_update_slice_hist,NULL);
    }

    for(i=0; i<mt_nthreads; i++) {
        pthread_join(thread_ids[i],NULL);
    }
    FREEMEMORY(thread_ids);
}
Ejemplo n.º 15
0
void update_slice_hist(void) {
    int i;

    for(i=0; i<nsliceinfo; i++) {
        slice *slicei;
        int unit1;
        FILE_SIZE lenfile;
        int error1;
        float slicetime1, *sliceframe;
        int sliceframesize;
        int is1, is2, js1, js2, ks1, ks2;
        int testslice;

        slicei = sliceinfo + i;

        LOCK_SLICE_BOUND;
        if(slicei->inuse_getbounds==1) {
            UNLOCK_SLICE_BOUND;
            continue;
        }
        slicei->inuse_getbounds=1;
        UNLOCK_SLICE_BOUND;
        PRINTF("  Examining %s\n",slicei->file);

        lenfile=strlen(slicei->file);

        LOCK_COMPRESS;
        FORTget_file_unit(&unit1,&slicei->unit_start);
        FORTopenslice(slicei->file,&unit1,&is1,&is2,&js1,&js2,&ks1,&ks2,&error1,lenfile);
        UNLOCK_COMPRESS;

        sliceframesize=(is2+1-is1)*(js2+1-js1)*(ks2+1-ks1);
        NewMemory((void **)&sliceframe,sliceframesize*sizeof(float));
        init_histogram(slicei->histogram);
        testslice=0;
        while(error1==0) {
            FORTgetsliceframe(&unit1, &is1, &is2, &js1, &js2, &ks1, &ks2, &slicetime1, sliceframe, &testslice,&error1);
            update_histogram(sliceframe,sliceframesize,slicei->histogram);
        }
        FREEMEMORY(sliceframe);

        LOCK_COMPRESS;
        FORTclosefortranfile(&unit1);
        UNLOCK_COMPRESS;
    }
}
Ejemplo n.º 16
0
vector_t *decodeCantor(void *space, Uint code, Uint n) {
	Uint i;
	vector_t *v = NULL;
	vector_t *r = NULL;
	
	v=ALLOCMEMORY(space, v, vector_t, 1);
	INITVECTOR(v);

	for (i=0; i < (n-1); i++) {
		r = decode_2tupel_cantor(space, code);		
		APPENDVEC(space, v, VECTOR(r,0));
		code = VECTOR(r,1);

		FREEMEMORY(space, r);
	}

	APPENDVEC(space, v, VECTOR(r,1));
	return (v);
}
Ejemplo n.º 17
0
double
swconstfilter(void *space, Matchtype *m, IntSequence *a, IntSequence *b,
              Uint *ptr, Uint len, Uint pos, void *info) {

    imbissinfo *imbiss;
    int *swres;
    double t;

    imbiss = (imbissinfo*) info;
    t=scorefilter(space, m, a, b, ptr, len, pos, info);

    if (m->count == imbiss->minseeds) {
        swres = swgapless(space, a->sequence, a->length,
                          b->sequence, b->length,
                          constscr, imbiss->swscores);
        m->swscore= swres[arraymax(swres, (a->length+1)*(b->length+1))];

        FREEMEMORY(space, swres);
    }

    return t;
}
Ejemplo n.º 18
0
int
latexscores (void *space, Matchtype *m, IntSequence **s, Uint len, Uint match, 
		   void *info)
{ 
  int sw;
  double explambda, E; 
  
  /*FILE* fp;*/

  struct salami_info* salami;
  imbissinfo *imbiss;
  stringset_t *query;

  imbiss = (imbissinfo*) info;
 
  if (m->count <= imbiss->minseeds) return 0;
  if (match > imbiss->noofhits) return -1; 
  
  query = tokensToStringset(space, "/.", 
		s[m->id]->url, strlen(s[m->id]->url));
  
  explambda = exp(-imbiss->lambda * m->blast );
  E = imbiss->K*2500000*imbiss->substrlen*explambda;	
  sw = floor(m->swscore);
  
  salami = doWurstAlignment(space, m, s, len, imbiss->query);
  
  printf("%s & %s & %.2f & %d & %.2f & %.2f & %.2f & %.2f\\\\ \n",
	  query->strings[query->noofstrings-1].str, s[m->id]->description, 
	  	m->score, sw, salami->sw_score_tot, salami->id ,log10(E), salami->rmsd);

  /* latexWurstAlignment(space, m, s, len, ((imbissinfo*)info)->query);
  */
  FREEMEMORY(space, salami);
  destructStringset(space, query);
  
  return 1;
}
Ejemplo n.º 19
0
Matchtype*
selectScoreSWconst(void *space, Matchtype *m, Uint k,
                   IntSequence *a, IntSequence **s, void *info) {

    Uint l, i;
    int *swres;
    imbissinfo *imbiss;

    imbiss = (imbissinfo*) info;

    qsort(m, k, sizeof(Matchtype), cmp_score);

    l=0;
    for (i=k; i > 0 && l < 1000; i--) {
        if (m[i-1].count >= imbiss->minseeds) {

            swres = swgapless(space,
                              a->sequence, a->length,
                              s[m[i-1].id]->sequence, s[m[i-1].id]->length,
                              constscr, imbiss->swscores
                              /*subscr, info*/
                             );

            m[i-1].swscore= swres[arraymax(swres,
                                           (a->length+1)*(s[m[i-1].id]->length+1))];

            FREEMEMORY(space, swres);
        } else {
            m[i-1].swscore = 0;
        }
        l++;
    }

    qsort(m, k, sizeof(Matchtype), cmp_swscore);
    return m;
}
Ejemplo n.º 20
0
void
destructinterval(void *space, void *data) {
  FREEMEMORY(space, (PairUint*) data);
}
Ejemplo n.º 21
0
void readhrr(int flag, int *errorcode){
  FILE *HRRFILE;
  int ntimeshrr, nfirst;
  char buffer[LENBUFFER];
  float *hrrtime, *hrrval;
  int display=0;
  int ntimes_saved;

  *errorcode=0;
  if(hrrinfo!=NULL){
    display = hrrinfo->display;
    FREEMEMORY(hrrinfo->times_csv);
    FREEMEMORY(hrrinfo->times);
    FREEMEMORY(hrrinfo->hrrval_csv);
    FREEMEMORY(hrrinfo->hrrval);
    FREEMEMORY(hrrinfo->timeslist);
  }
  FREEMEMORY(hrrinfo);
  if(flag==UNLOAD)return;

  NewMemory((void **)&hrrinfo,sizeof(hrrdata));
  hrrinfo->file=hrr_csv_filename;
  hrrinfo->times_csv=NULL;
  hrrinfo->times=NULL;
  hrrinfo->timeslist=NULL;
  hrrinfo->hrrval_csv=NULL;
  hrrinfo->hrrval=NULL;
  hrrinfo->ntimes_csv=0;
  hrrinfo->loaded=1;
  hrrinfo->display=display;
  hrrinfo->itime=0;

  HRRFILE=fopen(hrrinfo->file,"r");
  if(HRRFILE==NULL){
    readhrr(UNLOAD,errorcode);
    return;
  }


// size data

  ntimeshrr=0;
  nfirst=-1;
  while(!feof(HRRFILE)){
    if(fgets(buffer,LENBUFFER,HRRFILE)==NULL)break;
    if(nfirst==-1&&strstr(buffer,".")!=NULL)nfirst=ntimeshrr;
    ntimeshrr++;
  }
  ntimes_saved=ntimeshrr;
  ntimeshrr-=nfirst;

  rewind(HRRFILE);
  NewMemory((void **)&hrrinfo->times_csv,ntimeshrr*sizeof(float));
  NewMemory((void **)&hrrinfo->hrrval_csv,ntimeshrr*sizeof(float));

// read data
  
  hrrtime=hrrinfo->times_csv;
  hrrval=hrrinfo->hrrval_csv;
  ntimeshrr=0;

// read no more than the number of lines found during first pass

  while(ntimeshrr<ntimes_saved&&!feof(HRRFILE)){
    if(fgets(buffer,LENBUFFER,HRRFILE)==NULL)break;
    if(ntimeshrr<nfirst){
      ntimeshrr++;
      continue;
    }
    stripcommas(buffer);
    sscanf(buffer,"%f %f",hrrtime,hrrval);
    hrrtime++;
    hrrval++;
    ntimeshrr++;
  }
  hrrinfo->ntimes_csv=ntimeshrr-nfirst;
  fclose(HRRFILE);
}
Ejemplo n.º 22
0
int
allscores (void *space, Matchtype *m, IntSequence **s, Uint len, Uint match, 
		   void *info)
{
  char *pic;
  float rmsd = -1;
  double explambda, E;
  FILE* fp;
  imbissinfo *imbiss;
  struct salami_info *salami;
  stringset_t *query;
  
  imbiss = (imbissinfo*) info;
  if (m->count <= imbiss->minseeds) return 0;
  if (match > imbiss->noofhits) return -1; 
  
  /*report score stuff*/
  printf("[%d]: score: %f, count: %d\n", match, m->score, m->count); 
  printf("%d\t%s\t%d\t", m->id, s[m->id]->url, m->count);
  pic= depictSequence(space, len, 20, m->pos, m->count,'*');
  printf("[%s]\n", pic);
  printf("%s\n", s[m->id]->description);

  if (imbiss->wurst) {
  	salami = doWurstAlignment(space, m, s, len, imbiss->query);
  	printf("sequence identity: %f\n", salami->id);	
	printf("scr: %f (%f), scr_tot: %f, cvr: %f (raw: %d)\n", 
		salami->sw_score, salami->sw_smpl_score, salami->sw_score_tot, 
		salami->sw_cvr, salami->sw_raw);
  	printf("frac_dme: %f, z_scr: %f, rmsd: %f, andrew_scr %f\n",
		salami->frac_dme, salami->z_scr, salami->rmsd, salami->andrew_scr);
  	printf("tm_scr %f\n", salami->tmscore);
    FREEMEMORY(space, salami);
  }
   
  printf("gapless sw score: %f\n", m->swscore); 

  if (imbiss->reportfile) {
  	
  	query = tokensToStringset(space, "/.", 
		s[m->id]->url, strlen(s[m->id]->url));

	fp = fopen(imbiss->reportfile,"a+");
  	fprintf(fp, "%s\t%f\t%d\n", query->strings[query->noofstrings-1].str,
	  	rmsd, s[m->id]->length);
  	fclose(fp);	
  	
	destructStringset(space, query);
  }
  
  /*report blast stuff*/
  printf("highest seed score (HSS): %f\n", m->blast);
  /*printf("lambda*S %19.16e\n", m->blast *((imbissinfo*)info)->lambda);*/
  explambda = exp(- imbiss->lambda * m->blast );
  /*printf("exp(-lambda*S): %19.16e\n", explambda);	*/
  E =  ((imbissinfo*)info)->K*2500000*imbiss->substrlen*explambda;	
  /*printf("E=Kmn * exp(-lambda*S): %19.16e\n", E);*/	
  printf("log(HSS): %f\n", log10(E));	
  printf("1-exp(-HSS): %19.16e\n", 1-exp(-E)); 

  FREEMEMORY(space, pic);
  return 1;
}
Ejemplo n.º 23
0
int
main (int argc, char** argv)
{	
 	Sint optindex, c;
	unsigned char depictsw=0;
	unsigned char wurst=1;
	unsigned char gnuplot=0;

	Uint i, j, noofseqs=0, nooffreqs=0, noofqueries=0;
	Uint noofhits=100;
	Uint substrlen = 10;
	Uint minseeds = 5;
	Uint maxmatches = 10000;
	char *seq, *vec, *bin;
	imbissinfo *imbiss;
	void *space = NULL;	
	double *scores = NULL;

	int swscores[2]={3,-2};
	char *pveclistfile=NULL;
	char *alphabetfile=NULL;	
	char *inputfile=NULL;
	char *batchfile = NULL;
	char *subfile=NULL;
	char *reportfile = NULL;

	 int (*handler)
	   (void *, Matchtype *, IntSequence **, Uint, 
		Uint, void *) = allscores;
	 
	 double (*filter) 
	   (void *, Matchtype *, IntSequence *, IntSequence *,
		 Uint *, Uint, Uint, void *) = swconstfilter;

	 Matchtype* (*select)
	   (void *, Matchtype *, Uint k, 
		IntSequence *, IntSequence **, void *) = selectSW;
  

	stringset_t **fn, **freq, *queryurl, **queries=NULL;
	Suffixarray *arr = NULL;	
	IntSequence **sequences = NULL;
	IntSequence *input = NULL;
	FAlphabet *alphabet = NULL;		
    PairSint *matches=NULL;
	Uint percent=0;
	
	time_t startsuf, endsuf; 
	double difsuf, difmatch, difrank;	

#ifdef MEMMAN_H 	
	Spacetable spacetab;
	initmemoryblocks(&spacetab, 100000);
	space = &spacetab;
#endif
	
	while(1) 
	{
		c=getopt_long(argc, argv, "SAghFGBLM:D:r:m:x:n:p:b:s:a:q:l:c:dvw", 
						long_options, &optindex);
		if (c==-1) break;
		
		switch(c) {
		    case 'r':
			  reportfile=optarg;
			  break;
		    case 'v':
			  verbose_flag=1;	
			  break;
		    case 'd':
			  	depictsw = 1;
				break;
		    case 's':
				pveclistfile = optarg;	
				break;
			case 'a':
				alphabetfile = optarg;	
				break;
			case 'q':
				inputfile = optarg;
				noofqueries = 1;
				break;
			case 'l':
				substrlen = atoi(optarg);
				break;
			case 'c':
			  	minseeds = atoi(optarg);
			    break;
			case 'b':
				batchfile = optarg;
				break;
			case 'p':
				percent = atoi(optarg);
				break;
			case 'x':
				subfile = optarg;
				break;
			case 'n':
				noofhits = atoi(optarg);
				break;
			case 'w':
				wurst = 0;
				break;
			case 'B':
				filter = scorefilter;
				select = selectBlastScore;
				break;
			case 'S':
				filter = scorefilter;
				select = selectScore;
				break;
			case 'A':
				filter = swconstfilter;
				select = selectSW;
				break;
			case 'F':
				filter = scorefilter;
				select = selectScoreSWconst;
				break;
			case 'G':
				filter = scorefilter;
				select = selectBlastScoreSWconst;
				break;
			case 'M':
				swscores[0]=atoi(optarg);
				break;
			case 'L':
				handler = latexscores;
				break;
			case 'D':
				swscores[1]=atoi(optarg);
				break;
			case 'g':
				gnuplot = 1;
				break;
			case 'm':
				maxmatches=atoi(optarg);
				break;
			case 'h':
			default:
				usage(argv[0]);
				exit (EXIT_FAILURE);
		}
	}
	if (pveclistfile==NULL || (inputfile == NULL && batchfile==NULL)
		|| alphabetfile == NULL) {
		usage(argv[0]);
		exit (EXIT_FAILURE);
	}
	
	imbiss = ALLOCMEMORY(space, NULL, imbissinfo, 1);
	imbiss->reportfile = reportfile;
	imbiss->swscores = swscores;
	imbiss->noofhits = noofhits;
	imbiss->minseeds = minseeds; 	
	imbiss->wurst = wurst; 
	
	/*read batch file*/
	if (batchfile) {
		queries = readcsv(space, batchfile, "", &noofqueries);	
	}

	/*read substitution matrix*/
	if (subfile) {
		freq=readcsv(space, subfile,",", &nooffreqs);
		scores = ALLOCMEMORY(space, NULL, double, ((nooffreqs-1)*(nooffreqs-1)) );
		for(i=1; i < nooffreqs; i++) {
			for(j=1; j < nooffreqs; j++) {
				if(strcmp(SETSTR(freq[i],j),"inf")==0){
					MATRIX2D(scores, nooffreqs-1, i, j)=0;
				}else{
					MATRIX2D(scores, nooffreqs-1, i, j)=atof(SETSTR(freq[i],j));
				}
			}
		}
	}
	
	/*read alphabet*/	
	if (alphabetfile != NULL) {
		alphabet = loadCSValphabet(space, alphabetfile);
		sortMapdomain(space, alphabet);
    }

	
	/*load sequence database*/
	fn=readcsv(space, pveclistfile, "", &noofseqs);
	sequences = ALLOCMEMORY(space, NULL, IntSequence *, noofseqs);
	for(i=0; i < noofseqs; i++) 
	{		  
		sequences[i] = loadSequence(space, SETSTR(fn[i],0));		
	}

	for (i=0; i < noofseqs; i++) {	
	  	destructStringset(space, fn[i]);
	}
	FREEMEMORY(space, fn);
	
	
	/*construct the suffix array*/
	time (&startsuf);
	arr = constructSufArr(space, sequences, noofseqs, NULL);
        constructLcp(space, arr); 	
   	time (&endsuf);
	difsuf = difftime (endsuf, startsuf);


	/*do search*/
    for (i=0; i < noofqueries; i++) {
	  
	    /*get query form batchfile*/
	  	if (queries) {
			inputfile = SETSTR(queries[i],0);
		}	
		
		/*typically only used with batchfile*/
		if (percent != 0) {
			substrlen = 
			  ((double)((double)input->length/100)*(double) percent);
		}
			
		input = loadSequence(space, inputfile);
		//seq = printSequence (space, input, 60); 
		printf(">IMBISS order delivered\n");	
		//printf("%s\n",seq);
		printf("%s\n", input->url); 
		//FREEMEMORY(space, seq);	
		
		time (&startsuf);
		matches=sufSubstring(space, arr, input->sequence, input->length, substrlen);	 
		time (&endsuf);
		difmatch = difftime (endsuf, startsuf);

		/*get prob vector url for salami/wurst*/
        //printf("%.*s\n", 5, input->url + 58);
        vec = malloc(sizeof(char)*66);
        sprintf(vec, "/smallfiles/public/no_backup/bm/pdb_all_vec_6mer_struct/%5s.vec\0", input->url+56);
        bin = malloc(sizeof(char)*54);
        sprintf(bin, "/smallfiles/public/no_backup/bm/pdb_all_bin/%5s.bin\0", input->url+56);

		queryurl = initStringset(space);
		addString(space, queryurl, bin, strlen(bin));
		addString(space, queryurl, vec, strlen(vec));

		
        getimbissblast(space, input, sequences, noofseqs, alphabet, imbiss);
	

		imbiss->query = queryurl;
		imbiss->substrlen = substrlen;
		imbiss->alphabet = alphabet;
		
		/*if a substition file was given ...*/
		if (subfile) {
			imbiss->sub = createsubmatrix(scores, imbiss->score, nooffreqs-1);
		}
		
		/*match 'n' report*/
		time (&startsuf);
		
		imbiss->consensus = ALLOCMEMORY(space, NULL, Uint, (input->length-substrlen));
		memset(imbiss->consensus, 0, (sizeof(Uint)*(input->length-substrlen)));
	
		rankSufmatch(space, arr, matches, input->length-substrlen,
			maxmatches, substrlen, 
			sequences, noofseqs, filter, select, handler,
			input, imbiss, scores, depictsw);
		
		if (gnuplot) {	
			consensus (space, imbiss->consensus, input->length-substrlen, 
				input, substrlen, imbiss);
		}

		time (&endsuf);
		difrank = difftime (endsuf, startsuf);
	
		printf ("Building  the suffixtree has taken %f seconds.\n", difsuf);
		printf ("Match the suffixtree has taken %f seconds.\n", difmatch);
    		printf ("Rank  the suffixtree has taken %f seconds.\n", difrank);
	
		/*partial cleanup*/
		//destructStringset(space, queryurl);
		destructSequence(space, input);
		if(subfile) {
			FREEMEMORY(space, imbiss->sub);
		}

		FREEMEMORY(space, imbiss->consensus);
		FREEMEMORY(space, imbiss->score);
		FREEMEMORY(space, matches);
        free(bin);
        free(vec);
	}
	
	/*final cleanup*/
	for (i=0; i < noofseqs; i++) {
		destructSequence(space, sequences[i]);
	}
    FREEMEMORY(space, sequences);	
	destructSufArr(space, arr);
	
#ifdef MEMMAN_H
	activeblocks(space);
#endif
	
	printf("Goodbye.\n");	
	return EXIT_SUCCESS;
}
Ejemplo n.º 24
0
void
constructsuflinks(void *space, Suffixarray *s) {

  Uint   i,
         j,
         a,
         b,
         k,
         nooflists,
         lcp,
         pos;
  Stack  istack;
  Stack  jstack;

  List   *children,
         **lists;
  PairUint **data,
           slinkinterval;

  nooflists = maxlcp(s) +1;
  lists = ALLOCMEMORY(space, NULL, List*, nooflists);
  memset(lists, 0, sizeof(List*)*nooflists);

  initStack(space, &istack, 1000);
  initStack(space, &jstack, 1000);

  stackpush(space, &istack, 0);
  stackpush(space, &jstack, s->numofsuffixes-1);

  while(!stackisempty(&istack)) {
    i = stackpop(&istack);
    j = stackpop(&jstack);
    lcp = getlcpval(s, i, j);

    /*printf("adding list %d\n", lcp);*/
    if (lists[lcp] == NULL) {
      lists[lcp] = initList(space, 10);
    }

    addinterval(space, lists[lcp], i, j);

    /*printf("lcp: %d-[%d,%d]\n", lcp, i, j);*/
    children = getChildintervals(space, s, i, j);
    data = (PairUint**) dataList(space, children);

    for(k=children->length; k > 0; k--) {
      a = data[k-1]->a;
      b = data[k-1]->b;

      FREEMEMORY(space, data[k-1]);

      if(a != b) { 
        stackpush(space, &istack, a);
        stackpush(space, &jstack, b);
      }
    }

    FREEMEMORY(space, data);
    wrapList(space, children, NULL);
  }

  destructStack(space, &istack);
  destructStack(space, &jstack);

  s->suflink_l = ALLOCMEMORY(space, NULL, Uint, s->numofsuffixes);
  s->suflink_r = ALLOCMEMORY(space, NULL, Uint, s->numofsuffixes);
  memset(s->suflink_l, 0, sizeof(Uint)*s->numofsuffixes);
  memset(s->suflink_r, 0, sizeof(Uint)*s->numofsuffixes);

  for(i=1; i < nooflists; i++) {
    if(lists[i] !=  NULL && lists[i-1] !=NULL) {
      for(j=0; j < lists[i]->length; j++) {
       /*printf("looking at interval [%d,%d], list %d\n", ((PairUint*)lists[i]->nodes[j].data)->a, ((PairUint*)lists[i]->nodes[j].data)->b, i);*/
        slinkinterval = findslinkinterval(space, s, lists, i, j);
        pos = getfirstlindex(s, ((PairUint*)lists[i]->nodes[j].data)->a, ((PairUint*)lists[i]->nodes[j].data)->b);
       /*printf("store at %d: [%d,%d]\n", pos, slinkinterval.a, slinkinterval.b);*/
        s->suflink_l[pos]=slinkinterval.a;
        s->suflink_r[pos]=slinkinterval.b;
      }
    }
    wrapList(space, lists[i-1], destructinterval);
  }

  FREEMEMORY(space, lists);
  return;
}
Ejemplo n.º 25
0
int convert_slice(slice *slicei, int *thread_index) {

    char slicefile_svz[1024], slicesizefile_svz[1024];
    int fileversion, one, zero;
    char *slice_file;
    int version_local;
    char filetype[1024];
    char *shortlabel, *unit;
    char units[256];
    int ijkbar[6];
    uLong framesize;
    float *sliceframe_data=NULL;
    unsigned char *sliceframe_compressed=NULL, *sliceframe_uncompressed=NULL;
    unsigned char *sliceframe_compressed_rle=NULL, *sliceframe_uncompressed_rle=NULL;
    char cval[256];
    int sizebefore, sizeafter;
    int returncode;
    float minmax[2];
    float time_local;
    LINT data_loc;
    int percent_done;
    int percent_next=10;
    float valmin, valmax, denom;
    int chop_min, chop_max;
    uLongf ncompressed_zlib;
    int ncompressed_save;
#ifndef pp_THREAD
    int count=0;
#endif
    int ncol, nrow, idir;
    float time_max;
    int itime;
    LINT file_loc;

    FILE *SLICEFILE;
    FILE *slicestream,*slicesizestream;

#ifdef pp_THREAD
    if(GLOBcleanfiles==0) {
        int fileindex;

        fileindex = slicei + 1 - sliceinfo;
        sprintf(threadinfo[*thread_index].label,"sf %i",fileindex);
    }
#endif

    slice_file=slicei->file;
    version_local=slicei->version;

    fileversion = 1;
    one = 1;
    zero=0;

    // check if slice file is accessible

    strcpy(filetype,"");
    shortlabel=slicei->label.shortlabel;
    if(strlen(shortlabel)>0)strcat(filetype,shortlabel);
    trim(filetype);

    if(getfileinfo(slice_file,NULL,NULL)!=0) {
        fprintf(stderr,"*** Warning: The file %s does not exist\n",slice_file);
        return 0;
    }

    SLICEFILE=fopen(slice_file,"rb");
    if(SLICEFILE==NULL) {
        fprintf(stderr,"*** Warning: The file %s could not be opened\n",slice_file);
        return 0;
    }

    // set up slice compressed file

    if(GLOBdestdir!=NULL) {
        strcpy(slicefile_svz,GLOBdestdir);
        strcat(slicefile_svz,slicei->filebase);
    }
    else {
        strcpy(slicefile_svz,slicei->file);
    }
    {

        char *ext;
        int lensvz;

        lensvz = strlen(slicefile_svz);

        if(lensvz>4) {
            ext = slicefile_svz + lensvz - 4;
            if(strcmp(ext,".rle")==0) {
                slicefile_svz[lensvz-4]=0;
            }
            strcat(slicefile_svz,".svz");
        }
    }

    if(GLOBdestdir!=NULL) {
        strcpy(slicesizefile_svz,GLOBdestdir);
        strcat(slicesizefile_svz,slicei->filebase);
    }
    else {
        strcpy(slicesizefile_svz,slicei->file);
    }
    {

        char *ext;
        int lensvz;

        lensvz = strlen(slicesizefile_svz);

        if(lensvz>4) {
            ext = slicesizefile_svz + lensvz - 4;
            if(strcmp(ext,".rle")==0) {
                slicesizefile_svz[lensvz-4]=0;
            }
            strcat(slicesizefile_svz,".sz");
        }
    }

    if(GLOBcleanfiles==1) {
        slicestream=fopen(slicefile_svz,"rb");
        if(slicestream!=NULL) {
            fclose(slicestream);
            PRINTF("  Removing %s\n",slicefile_svz);
            UNLINK(slicefile_svz);
            LOCK_COMPRESS;
            GLOBfilesremoved++;
            UNLOCK_COMPRESS;
        }
        slicesizestream=fopen(slicesizefile_svz,"rb");
        if(slicesizestream!=NULL) {
            fclose(slicesizestream);
            PRINTF("  Removing %s\n",slicesizefile_svz);
            UNLINK(slicesizefile_svz);
            LOCK_COMPRESS;
            GLOBfilesremoved++;
            UNLOCK_COMPRESS;
        }
        return 0;
    }

    if(GLOBoverwrite_slice==0) {
        slicestream=fopen(slicefile_svz,"rb");
        if(slicestream!=NULL) {
            fclose(slicestream);
            fprintf(stderr,"*** Warning:  %s exists.\n",slicefile_svz);
            fprintf(stderr,"     Use the -f option to overwrite smokezip compressed files\n");
            return 0;
        }
    }

    slicestream=fopen(slicefile_svz,"wb");
    slicesizestream=fopen(slicesizefile_svz,"w");
    if(slicestream==NULL||slicesizestream==NULL) {
        if(slicestream==NULL) {
            fprintf(stderr,"*** Warning: The file %s could not be opened for writing\n",slicefile_svz);
        }
        if(slicesizestream==NULL) {
            fprintf(stderr,"  %s could not be opened for writing\n",slicesizefile_svz);
        }
        if(slicestream!=NULL)fclose(slicestream);
        if(slicesizestream!=NULL)fclose(slicesizestream);
        fclose(SLICEFILE);
        return 0;
    }

    // read and write slice header

    strcpy(units,"");
    unit=slicei->label.unit;
    if(strlen(unit)>0)strcat(units,unit);
    trim(units);
    sprintf(cval,"%f",slicei->valmin);
    trimzeros(cval);
#ifndef pp_THREAD
    if(GLOBcleanfiles==0) {
        PRINTF("Compressing %s (%s)\n",slice_file,filetype);
        PRINTF("  using min=%s %s",cval,units);
    }
#endif
    sprintf(cval,"%f",slicei->valmax);
    trimzeros(cval);
#ifndef pp_THREAD
    if(GLOBcleanfiles==0) {
        PRINTF(" max=%s %s\n",cval,units);
        PRINTF(" ");
    }
#endif
    valmin=slicei->valmin;
    valmax=slicei->valmax;
    denom = valmax-valmin;
    if(denom==0.0)denom=1.0;

    chop_min=0;
    chop_max=255;
    if(GLOBno_chop==0) {
        if(slicei->setchopvalmax==1) {
            chop_max = 255*(slicei->chopvalmax-valmin)/denom;
            if(chop_max<0)chop_max=0;
            if(chop_max>255)chop_max=255;
        }
        if(slicei->setchopvalmin==1) {
            chop_min = 255*(slicei->chopvalmin-valmin)/denom;
            if(chop_min<0)chop_min=0;
            if(chop_min>255)chop_min=255;
        }
    }


    fwrite(&one,4,1,slicestream);           // write out a 1 to determine "endianness" when file is read in later
    fwrite(&zero,4,1,slicestream);          // write out a zero now, then a one just before file is closed
    fwrite(&fileversion,4,1,slicestream);   // write out compressed fileversion in case file format changes later
    fwrite(&version_local,4,1,slicestream);       // fds slice file version
    sizeafter=16;

    //*** SLICE FILE FORMATS

    //*** FDS FORMAT (FORTRAN - each FORTRAN record has a 4 byte header and a 4 byte trailer surrounding the data)

    // 30 byte long label
    // 30 byte short label
    // 30 byte unit
    // i1,i2,j1,j2,k1,k2

    // for each time step:

    // time, compressed frame size
    // qq(1,nbuffer)              where nbuffer = (i2+1-i1)*(j2+1-j1)*(k2+1-k1)



    //*** ZLIB format (C - no extra bytes surrounding data)

    //*** header
    // endian
    // completion (0/1)
    // fileversion (compressed format)
    // version_local  (slicef version)
    // global min max (used to perform conversion)
    // i1,i2,j1,j2,k1,k2


    //*** frame
    // time, compressed frame size                        for each frame
    // compressed buffer


    //*** RLE format (FORTRAN)

    //*** header
    // endian
    // fileversion, slice version
    // global min max (used to perform conversion)
    // i1,i2,j1,j2,k1,k2


    //*** frame
    // time
    // compressed frame size                        for each frame
    // compressed buffer

    {
        int skip;

        skip = 3*(4+30+4);  // skip over 3 records each containing a 30 byte FORTRAN character string
        returncode=FSEEK(SLICEFILE,skip,SEEK_CUR);
        sizebefore=skip;
    }

    FORTSLICEREAD(ijkbar,6);
    sizebefore+=8+6*4;

    framesize =  (ijkbar[1]+1-ijkbar[0]);
    framesize *= (ijkbar[3]+1-ijkbar[2]);
    framesize *= (ijkbar[5]+1-ijkbar[4]);

    minmax[0]=slicei->valmin;
    minmax[1]=slicei->valmax;
    fwrite(minmax,4,2,slicestream);    // min max vals
    fwrite(ijkbar,4,6,slicestream);
    sizeafter+=(8+24);


    ncompressed_save=1.02*framesize+600;
    if(NewMemory((void **)&sliceframe_data,ncompressed_save*sizeof(float))==0)goto wrapup;
    if(NewMemory((void **)&sliceframe_compressed,ncompressed_save*sizeof(unsigned char))==0)goto wrapup;
    if(NewMemory((void **)&sliceframe_uncompressed,ncompressed_save*sizeof(unsigned char))==0)goto wrapup;

    fprintf(slicesizestream,"%i %i %i %i %i %i\n",ijkbar[0],ijkbar[1],ijkbar[2],ijkbar[3],ijkbar[4],ijkbar[5]);
    fprintf(slicesizestream,"%f %f\n",minmax[0],minmax[1]);

    idir=0;
    if(ijkbar[0]==ijkbar[1]) {
        idir=1;
        ncol = ijkbar[3] + 1 - ijkbar[2];
        nrow = ijkbar[5] + 1 - ijkbar[4];
    }
    else if(ijkbar[2]==ijkbar[3]) {
        idir=2;
        ncol = ijkbar[1] + 1 - ijkbar[0];
        nrow = ijkbar[5] + 1 - ijkbar[4];
    }
    else if(ijkbar[4]==ijkbar[5]) {
        idir=3;
        ncol = ijkbar[1] + 1 - ijkbar[0];
        nrow = ijkbar[3] + 1 - ijkbar[2];
    }
    if(idir==0) {
        idir=1;
        ncol = ijkbar[3] + 1 - ijkbar[2];
        nrow = ijkbar[5] + 1 - ijkbar[4];
    }


    {
        int ni, nj, nk;

        ni = ijkbar[1]+1-ijkbar[0];
        nj = ijkbar[3]+1-ijkbar[2];
        nk = ijkbar[5]+1-ijkbar[4];

        time_max=-1000000.0;
        itime=-1;
        for(;;) {
            int i;

            FORTSLICEREAD(&time_local,1);
            sizebefore+=12;
            if(returncode==0)break;
            FORTSLICEREAD(sliceframe_data,framesize);    //---------------
            if(returncode==0)break;

            sizebefore+=(8+framesize*4);
            if(time_local<time_max)continue;
            time_max=time_local;

#ifndef pp_THREAD
            count++;
#endif

            data_loc=FTELL(SLICEFILE);
            percent_done=100.0*(float)data_loc/(float)slicei->filesize;
#ifdef pp_THREAD
            threadinfo[*thread_index].stat=percent_done;
            if(percent_done>percent_next) {
                LOCK_PRINT;
                print_thread_stats();
                UNLOCK_PRINT;
                percent_next+=10;
            }
#else
            if(percent_done>percent_next) {
                PRINTF(" %i%s",percent_next,GLOBpp);
                FFLUSH();
                percent_next+=10;
            }
#endif
            for(i=0; i<framesize; i++) {
                int ival;
                int icol, jrow, index2;
                int ii,jj,kk;

                // val_in(i,j,k) = i + j*ni + k*ni*nj

                if(framesize<=ncol*nrow) { // only one slice plane

                    // i = jrow*ncol + icol;

                    icol = i%ncol;
                    jrow = i/ncol;

                    index2 = icol*nrow + jrow;
                }
                else {
                    ii = i%ni;
                    jj = (i/ni)%nj;
                    kk = i/(ni*nj);

                    index2 = ii*nj*nk + jj*nk + kk;
                }

                {
                    float val;

                    val = sliceframe_data[i];
                    if(val<valmin) {
                        ival=0;
                    }
                    else if(val>valmax) {
                        ival=255;
                    }
                    else {
                        ival = 1 + 253*(val-valmin)/denom;
                    }
                    if(ival<chop_min)ival=0;
                    if(ival>chop_max)ival=255;
                    sliceframe_uncompressed[index2] = ival;
                }
            }
            itime++;
            if(itime%GLOBslicezipstep!=0)continue;

            //int compress (Bytef *dest,   uLongf *destLen, const Bytef *source, uLong sourceLen);
            ncompressed_zlib=ncompressed_save;
            returncode=compress(sliceframe_compressed,&ncompressed_zlib,sliceframe_uncompressed,framesize);

            file_loc=FTELL(slicestream);
            fwrite(&time_local,4,1,slicestream);
            fwrite(&ncompressed_zlib,4,1,slicestream);
            fwrite(sliceframe_compressed,1,ncompressed_zlib,slicestream);
            sizeafter+=(8+ncompressed_zlib);
            fprintf(slicesizestream,"%f %i, %li\n",time_local,(int)ncompressed_zlib,(long)file_loc);
        }
        if(returncode!=0) {
            fprintf(stderr,"*** Error: compress returncode=%i\n",returncode);
        }
    }

wrapup:
#ifndef pp_THREAD
    PRINTF(" 100%s completed\n",GLOBpp);
#endif
    FREEMEMORY(sliceframe_data);
    FREEMEMORY(sliceframe_compressed);
    FREEMEMORY(sliceframe_uncompressed);
    FREEMEMORY(sliceframe_compressed_rle);
    FREEMEMORY(sliceframe_uncompressed_rle);

    fclose(SLICEFILE);
    FSEEK(slicestream,4,SEEK_SET);
    fwrite(&one,4,1,slicestream);  // write completion code
    fclose(slicestream);
    fclose(slicesizestream);

    {
        char before_label[256],after_label[256];

        getfilesizelabel(sizebefore,before_label);
        getfilesizelabel(sizeafter,after_label);
#ifdef pp_THREAD
        slicei->compressed=1;
        sprintf(slicei->summary,"compressed from %s to %s (%4.1f%s reduction)",before_label,after_label,(float)sizebefore/(float)sizeafter,GLOBx);
        threadinfo[*thread_index].stat=-1;
#else
        PRINTF("  records=%i, ",count);
        PRINTF("Sizes: original=%s, ",before_label);
        PRINTF("compressed=%s (%4.1f%s reduction)\n\n",after_label,(float)sizebefore/(float)sizeafter,GLOBx);
#endif
    }

    return 1;
}
Ejemplo n.º 26
0
int convert_volslice(slice *slicei, int *thread_index) {
    char slicefile_svz[1024];
    char *slice_file;
    char filetype[1024];
    char *shortlabel;
    int ijkbar[6];
    uLong framesize;
    float *sliceframe_data=NULL;
    int sizebefore, sizeafter;
    int returncode;
    LINT data_loc;
    int percent_done;
    int percent_next=10;
#ifndef pp_THREAD
    int count=0;
#endif
    FILE *SLICEFILE;
    FILE *slicestream;

#ifdef pp_THREAD
    if(GLOBcleanfiles==0) {
        int fileindex;

        fileindex = slicei + 1 - sliceinfo;
        sprintf(threadinfo[*thread_index].label,"vsf %i",fileindex);
    }
#endif

    slice_file=slicei->file;

    // check if slice file is accessible

    strcpy(filetype,"");
    shortlabel=slicei->label.shortlabel;
    if(strlen(shortlabel)>0)strcat(filetype,shortlabel);
    trim(filetype);

    if(getfileinfo(slice_file,NULL,NULL)!=0) {
        fprintf(stderr,"*** Warning: The file %s does not exist\n",slice_file);
        return 0;
    }

    SLICEFILE=fopen(slice_file,"rb");
    if(SLICEFILE==NULL) {
        fprintf(stderr,"*** Warning: The file %s could not be opened\n",slice_file);
        return 0;
    }

    // set up slice compressed file

    if(GLOBdestdir!=NULL) {
        strcpy(slicefile_svz,GLOBdestdir);
        strcat(slicefile_svz,slicei->filebase);
    }
    else {
        strcpy(slicefile_svz,slicei->file);
    }

    if(strlen(slicefile_svz)>4)strcat(slicefile_svz,".svv");

    if(GLOBcleanfiles==1) {
        slicestream=fopen(slicefile_svz,"rb");
        if(slicestream!=NULL) {
            fclose(slicestream);
            PRINTF("  Removing %s\n",slicefile_svz);
            UNLINK(slicefile_svz);
            LOCK_COMPRESS;
            GLOBfilesremoved++;
            UNLOCK_COMPRESS;
        }
        return 0;
    }

    if(GLOBoverwrite_slice==0) {
        slicestream=fopen(slicefile_svz,"rb");
        if(slicestream!=NULL) {
            fclose(slicestream);
            fprintf(stderr,"*** Warning: The file %s exists.\n",slicefile_svz);
            fprintf(stderr,"     Use the -f option to overwrite smokezip compressed files\n");
            return 0;
        }
    }

    slicestream=fopen(slicefile_svz,"wb");
    if(slicestream==NULL) {
        fprintf(stderr,"*** Warning: The file %s could not be opened for writing\n",slicefile_svz);
        return 0;
    }

    // read and write slice header

#ifndef pp_THREAD
    if(GLOBcleanfiles==0) {
        PRINTF("Compressing %s (%s)\n",slice_file,filetype);
    }
#endif


    {
        int skip;

        skip = 3*(4+30+4);  // skip over 3 records each containing a 30 byte FORTRAN character string
        returncode=FSEEK(SLICEFILE,skip,SEEK_CUR);
        sizebefore=skip;
    }

    FORTSLICEREAD(ijkbar,6);
    sizebefore+=4+6*4+4;
    sizeafter=0;

    {
        int one=1, version_local=0, completion=0;

        fwrite(&one,4,1,slicestream);
        fwrite(&version_local,4,1,slicestream);
        fwrite(&completion,4,1,slicestream);
    }


    {
        int ni, nj, nk;

        ni = ijkbar[1]+1-ijkbar[0];
        nj = ijkbar[3]+1-ijkbar[2];
        nk = ijkbar[5]+1-ijkbar[4];
        framesize = ni*nj*nk;
        NewMemory((void **)&sliceframe_data,framesize*sizeof(float));

        for(;;) {
            float vmin, vmax;
            float *valmin, *valmax;
            unsigned char *compressed_data_out;
            uLongf ncompressed_data_out;
            float time_local;

            FORTSLICEREAD(&time_local,1);
            if(returncode==0)break;
            CheckMemory;
            sizebefore+=12;

            FORTSLICEREAD(sliceframe_data,framesize);    //---------------
            if(returncode==0)break;
            CheckMemory;
            sizebefore+=(4+framesize*sizeof(float)+4);

            valmin=NULL;
            valmax=NULL;
            if(slicei->voltype==1) {
                vmin=0.0;
                valmin=&vmin;
            }
            else if(slicei->voltype==2) {
                vmin=20.0;
                valmin=&vmin;
                vmax=1400.0;
                valmax=&vmax;
            }
            else {
                ASSERT(0);
            }
            CheckMemory;
            compress_volsliceframe(sliceframe_data, framesize, time_local, valmin, valmax,
                                   &compressed_data_out, &ncompressed_data_out);
            CheckMemory;
            sizeafter+=ncompressed_data_out;
            if(ncompressed_data_out>0) {
                fwrite(compressed_data_out,1,ncompressed_data_out,slicestream);
            }
            CheckMemory;
            FREEMEMORY(compressed_data_out);

#ifndef pp_THREAD
            count++;
#endif

            data_loc=FTELL(SLICEFILE);
            percent_done=100.0*(float)data_loc/(float)slicei->filesize;
#ifdef pp_THREAD
            threadinfo[*thread_index].stat=percent_done;
            if(percent_done>percent_next) {
                LOCK_PRINT;
                print_thread_stats();
                UNLOCK_PRINT;
                percent_next+=10;
            }
#else
            if(percent_done>percent_next) {
                PRINTF(" %i%s",percent_next,GLOBpp);
                FFLUSH();
                percent_next+=10;
            }
#endif

        }
        if(returncode!=0) {
            fprintf(stderr,"*** Error: compress returncode=%i\n",returncode);
        }
        FREEMEMORY(sliceframe_data);
    }

#ifndef pp_THREAD
    PRINTF(" 100%s completed\n",GLOBpp);
#endif

    {
        int completion=1;

        FSEEK(slicestream,4,SEEK_SET);
        fwrite(&completion,4,1,slicestream);
    }
    fclose(SLICEFILE);
    fclose(slicestream);

    {
        char before_label[256],after_label[256];

        getfilesizelabel(sizebefore,before_label);
        getfilesizelabel(sizeafter,after_label);
#ifdef pp_THREAD
        slicei->vol_compressed=1;
        sprintf(slicei->volsummary,"compressed from %s to %s (%4.1f%s reduction)",before_label,after_label,(float)sizebefore/(float)sizeafter,GLOBx);
        threadinfo[*thread_index].stat=-1;
#else
        PRINTF("  records=%i, ",count);
        PRINTF("Sizes: original=%s, ",before_label);
        PRINTF("compressed=%s (%4.1f%s reduction)\n\n",after_label,(float)sizebefore/(float)sizeafter,GLOBx);
#endif
    }

    return 1;

}
Ejemplo n.º 27
0
 int 
 main(int argc, char** argv) 
 {
	Uint noofvecs, i;
	Sint optindex, c;
	vector_t info;
#ifdef MEMMAN_H	
	Spacetable spacetab;
#endif
	void *space = NULL;
	char *url = NULL;
	char *outpath = NULL;
	char *pveclistfile = NULL;
	char *alphabetfile = NULL;
	char *vecext="vec";
	char *seqext="seq";	
	struct prob_vec *p_vec;
	IntSequence *sequence;
	FAlphabet *alphabet;	
	stringset_t *tok;
	stringset_t **fn;
	
#ifdef MEMMAN_H 
	initmemoryblocks(&spacetab, 1000);
	space = &spacetab;
#endif

    while(1) {
		c=getopt_long(argc, argv, "f:a:o:", long_options, &optindex);
		if (c==-1) break;
		
		switch(c) {
			case 'f':
				pveclistfile = optarg;	
				break;
			case 'a':
				alphabetfile = optarg;	
				break;
			case 'o':
				outpath = optarg;
				break;
			default:
				usage(argv[0]);
				exit (EXIT_FAILURE);
		}

	}

	if (pveclistfile==NULL || alphabetfile == NULL) {
		usage(argv[0]);
		exit (EXIT_FAILURE);
	}
	
	fn=readcsv(space, pveclistfile, ".", &noofvecs);
	alphabet = loadCSValphabet(space, alphabetfile);
	sortMapdomain(space, alphabet);
   
	for(i=0; i<noofvecs; i++) 
	{	
	  
		INITVECTOR(&info);	
	  	SETSTR(fn[i],0) = concatdelim(space, SETSTR(fn[i],0), vecext,
							  SETSTRLEN(fn[i],0), 3, '.');
	   
		p_vec = prob_vec_read (SETSTR(fn[i],0));	
			
		if (p_vec->mship == NULL) 
			prob_vec_expand(p_vec);
			
		sequence = encode_prob_vec(space, alphabet, p_vec, 0, 0, 
									cantorchar, &info);
		sequence->info = (Uint*) info.elements;
		sequence->namelen= strlen(alphabetfile);
		
		COPYSTR(space, sequence->alphabetname, alphabetfile, 
							 strlen(alphabetfile));

		/*this is a potential security risk*/
		if (p_vec->compnd_len > 0) {
		  sequence->descrlen = p_vec->compnd_len-1; 
		  COPYSTR(space, sequence->description, p_vec->compnd, 
							 p_vec->compnd_len-1);
		} else {
		  sequence->descrlen = 14;			
		  COPYSTR(space, sequence->description, "descriptor n/a", 14);
		}

		sequence->urllen = SETSTRLEN(fn[i],0);
		COPYSTR(space, sequence->url, SETSTR(fn[i],0), 
						   SETSTRLEN(fn[i],0));
				
		SETSTR(fn[i],0) = concatdelim(space, SETSTR(fn[i],0), seqext,
							SETSTRLEN(fn[i],0), 3, '.');	
		SETSTRLEN(fn[i],0) += 4;
		
		if (outpath) {
			tok = tokensToStringset(space, "/", SETSTR(fn[i],0), 
					SETSTRLEN(fn[i],0));
			
			COPYSTR(space, url, outpath, strlen(outpath));
	
			url = concat(space, url, SETSTR(tok, tok->noofstrings-1), 
				strlen(url), SETSTRLEN(tok, tok->noofstrings-1));
		
			saveSequence(sequence, url);
			
			destructStringset(space, tok);
			FREEMEMORY(space, url);
			url = NULL;
		
		} else {
	
			saveSequence(sequence, SETSTR(fn[i],0));
		}
		
		destructSequence (space, sequence);		
		prob_vec_destroy (p_vec);			 
		destructStringset (space, fn[i]);
		progressBarVT("probability vectors converted", noofvecs-1, i, 25);	
	}
	
	printf("\nexit.\n");
	FREEMEMORY(space, fn);
	destructAlphabet(space, alphabet);
		
	return EXIT_SUCCESS;
 }
Ejemplo n.º 28
0
void
consensus (void *space, Uint *consensus, Uint len, IntSequence *query,
		Uint seedlen, void* info) {

    Uint i,j;	
	char *constr;	
	double sum, norm;
	double *consensus_double;
	double *seedprobability;
	
	imbissinfo *imbiss;	
	IntSequence *consensusSequence;
	gnuplot_ctrl *h;

	imbiss = (imbissinfo*) info;
    

	seedprobability = ALLOCMEMORY(space, NULL, double, len);
	for (i=0; i < len; i++) {
		for (sum=0.0, j=0; j < seedlen; j++) {
			sum += imbiss->score[(Uint) query->sequence[i+j]];
		}
		seedprobability[i] = log10(imbiss->K*2500000*seedlen*
			exp(-(double)imbiss->lambda*sum));
	}
	
	consensusSequence = initSequence(space);
	consensusSequence->sequence = consensus;
	consensusSequence->length = len;
	
	constr = printSequence(space, consensusSequence, 60);
	printf("%s\n", constr);
	FREEMEMORY(space, constr);
	
    consensus_double = ALLOCMEMORY(space, NULL, double, len);
	
	h = gnuplot_init();
	gnuplot_setstyle(h, "lines");
	sum = 0;
	for(i=0; i < len; i++) {
		sum += consensus[i];
	}
	
	for(i=0; i < len; i++) {
	  norm = consensus[i];
	  norm = (norm > 0) ? norm : 1;
	  consensus_double[i] = log10(norm);
	
		/*	log10(imbiss->K*3000000*len*exp(-(double)consensus[i]));
		  if (consensus_double[i] < -400) consensus_double[i]=-400;*/
	}

	gnuplot_cmd(h, "set title 'IMBISS - seed statistics' -28,0 font'Helvetica,15'");	
	gnuplot_cmd(h, "set label '%s' at screen 0.12,0.92 font 'Helvetica,12'", imbiss->query->strings[0].str);
	gnuplot_cmd(h, "set label 'seed length: %d' at graph 0.05,0.95 font 'Helvetica, 12'", seedlen);
	gnuplot_set_xlabel(h, "query sequence residue");
	gnuplot_set_ylabel(h, "log");	
	gnuplot_plot_x(h, consensus_double, len, "log(number of matches)");
	gnuplot_plot_x(h, seedprobability, len,  "log(Kmn*e^{lambda*score(seed)})");
	
	FREEMEMORY(space, seedprobability);
	FREEMEMORY(space, consensus_double);
	FREEMEMORY(space, consensusSequence);
}
Ejemplo n.º 29
0
void adjustpart5bounds(partdata *parti){
  int i,j,k,m;
  part5data *datacopy;
  int alpha05;

  for(i=0;i<npart5prop;i++){
    part5prop *propi;
    int n;
    int *buckets;

    propi = part5propinfo + i;

    if(propi->set_global_bounds==1){
      propi->global_min =  1000000000.0;
      propi->global_max = -1000000000.0;
    }

    NewMemory((void **)&propi->buckets,NBUCKETS*sizeof(int));
    buckets = propi->buckets;
    for (n=0;n<NBUCKETS;n++){
      buckets[n]=0;
    }
  }

  // compute global min and max

  datacopy = parti->data5;
  for(i=0;i<parti->ntimes;i++){
    for(j=0;j<parti->nclasses;j++){
      part5class *partclassi;
      float *rvals;

      partclassi=parti->partclassptr[j];
      rvals = datacopy->rvals;

      for(k=2;k<partclassi->ntypes;k++){
        part5prop *prop_id;
        float *valmin, *valmax;

        prop_id = get_part5prop(partclassi->labels[k].longlabel);
        if(prop_id==NULL)continue;

        valmin = &prop_id->global_min;
        valmax = &prop_id->global_max;

        if(prop_id->set_global_bounds==1&&(valmin!=NULL||valmax!=NULL)){
          for(m=0;m<datacopy->npoints;m++){
            float val;

            val=*rvals++;
            if(valmin!=NULL)*valmin=MIN(*valmin,val);
            if(valmax!=NULL)*valmax=MAX(*valmax,val);
          }
        }
      }
      datacopy++;
    }
  }

  // generate data histogram (buckets) in order to determine percentile min and max

  datacopy = parti->data5;
  for(i=0;i<parti->ntimes;i++){
    for(j=0;j<parti->nclasses;j++){
      part5class *partclassi;
      float *rvals;

      partclassi = parti->partclassptr[j];
      rvals = datacopy->rvals;

      for(k=2;k<partclassi->ntypes;k++){
        part5prop *prop_id;
        float *valmin, *valmax, dg;
        int *buckets;

        prop_id = get_part5prop(partclassi->labels[k].longlabel);
        if(prop_id==NULL)continue;

        valmin = &prop_id->global_min;
        valmax = &prop_id->global_max;
        dg = (*valmax-*valmin)/(float)NBUCKETS;
        if(dg==0.0)dg=1.0;
        buckets=prop_id->buckets;

        for(m=0;m<datacopy->npoints;m++){
          float val;
          int ival;

          val=*rvals++;
          ival = CLAMP((val-*valmin)/dg,0,NBUCKETS-1);
          buckets[ival]++;
        }
      }
      datacopy++;
    }
  }

  // calculate percentile min and max

  for(i=0;i<npart5prop;i++){
    part5prop *propi;
    int total;
    int *buckets;
    int nsmall, nbig;
    int n;
    float gmin, gmax, dg;

    propi = part5propinfo + i;
    buckets = propi->buckets;

    if(propi->set_global_bounds==1){
      total = 0;
      for(n=0;n<NBUCKETS;n++){
        total+=buckets[n];
      }
      alpha05 = (int)(percentile_level*total);

      total = 0;
      nsmall=0;
      nbig = NBUCKETS-1;
      for (n=0;n<NBUCKETS;n++){
        total += buckets[n];
        if(total>alpha05){
          nsmall=n;
          break;
        }
      }
      total = 0;
      for (n=NBUCKETS;n>0;n--){
        total += buckets[n-1];
        if(total>alpha05){
          nbig=n-1;
          break;
        }
      }
      gmin = propi->global_min;
      gmax = propi->global_max;
      dg = (gmax-gmin)/(float)NBUCKETS;
      propi->percentile_min = gmin + nsmall*dg;
      propi->percentile_max = gmin + nbig*dg;
    }
    FREEMEMORY(propi->buckets);
  }
  for(i=0;i<npart5prop;i++){
    part5prop *propi;

    propi = part5propinfo + i;

    switch(propi->setvalmin){
    case PERCENTILE_MIN:
      propi->valmin=propi->percentile_min;
      break;
    case GLOBAL_MIN:
      propi->valmin=propi->global_min;
      break;
    case SET_MIN:
      propi->valmin=propi->user_min;
      break;
    default:
      ASSERT(FFALSE);
      break;
    }
    switch(propi->setvalmax){
    case PERCENTILE_MAX:
      propi->valmax=propi->percentile_max;
      break;
    case GLOBAL_MAX:
      propi->valmax=propi->global_max;
      break;
    case SET_MAX:
      propi->valmax=propi->user_max;
      break;
    default:
      ASSERT(FFALSE);
      break;
    }
  }
  adjustpart5chops(parti);
#ifdef _DEBUG
  print_part5prop();
#endif
}
Ejemplo n.º 30
0
gmatch_t*
alignkdmatches(void *space,
    Suffixarray *s, 
    matchstem_t *M, 
    CharSequence *query,
    char *curseq,
    Uint m,
    Uint t,
    Uint *enctab,
    unsigned char bestonly,
    Uint bedist,
    double lambda,
    double H,
    double K,
    double maxevalue,
    int acc,
    bitvector* D,
    Uint dim,
    Uint* nmatch,
    int* bscr) {

  Uint k,j,l,r,q,noofmatches=0, mat, mis, ins, del; 
  Lint pos, margin, schr, echr, sstart, slen, i ;
  char *sseq;
  unsigned int idx;
  Alignment *al=NULL;
  bitvector *peq;
  PairSint result;
#ifdef ALIGNDBG
  PairSint result2;
  CharSequence *checkseq;
#endif
  double E;
  int scr;
  int maxedist = 0;
  int bestscr = 0;

  gmatch_t *matches=NULL;

  margin = bestscr = maxedist = m-ceil((acc*m)/100);
  peq = getpeq(NULL, curseq, m, s->seq->map, s->seq->mapsize, enctab);

  for(i=0; i < m; i++) {
    for(q=0; q < M[i].noofbranches; q++) {
      l = M[i].branches[q].l; r = M[i].branches[q].r;
      scr =  M[i].branches[q].mat - (M[i].branches[q].mis+M[i].branches[q].ins+M[i].branches[q].del); 
      E = evalue(lambda, K, spacemult(m, s->numofsuffixes, H, K), scr); 

      if(l <= r && E <= maxevalue && (r-l) <= t) {
        for(j=l; j <= r; j++) {
          pos = s->suftab[j];

          /*skip marginal matches*/
          for(k=0; k < noofmatches; k++) 
            if (abs((signed int)matches[k].p-(pos-(i+margin))) <= margin) break;

          if (k == noofmatches) {

            idx = getMultiCharSeqIndex(s->seq, &s->seq->sequences[pos]);
            schr = (idx > 0) ? s->seq->markpos[idx-1]+1 : 0;
            echr = s->seq->markpos[idx];
            assert(echr >=  pos);
            sstart = MAX(schr, pos-(i+margin));
            slen = (echr > sstart+m+2*(margin+1)) ? m+2*(margin+1) : (echr-sstart)+1;  
            sseq = &s->seq->sequences[sstart];
/*            
            if(echr == pos) {
              fprintf(stderr, "\n\n sstart:%lld, slen:%lld, ssend:%lld, echr:%lld\n\n",
                  sstart, slen, sstart+slen-1, echr);
            }
*/
            myersbitmatrix(NULL, curseq, m, sseq, slen, s->seq->map, 
                s->seq->mapsize, enctab, m-bestscr, peq, &result, D, slen);

#ifdef ALIGNDBG
            result2 = myersbitvector(NULL, curseq, m, sseq, slen, 
                s->seq->map, s->seq->mapsize, enctab, m-bestscr, peq);
            assert(result.a == result2.a && result.b == result2.b);
#endif

            if (result.a != -1 && result.b <= maxedist 
                && result.b <= bestscr && result.a < slen) {  
              al = ALLOCMEMORY(space, NULL, Alignment, 1);
              initAlignment(al, curseq, m, 0, sseq, slen, 0);
              bitvectorbacktrack(al, D, slen, m, result.a);
#ifdef ALIGNDBG
              assert(getEdist(al) == result.b);
              checkseq = (CharSequence*) s->seq->ref[idx].ref;
              assert(strncmp(& checkseq->sequence[pos-schr],
                    &s->seq->sequences[pos], slen) == 0);
#endif
              countEops(al, &mat, &mis, &del, &ins);

              /*skip identical matches*/
              for(k=0; k < noofmatches; k++) {
                if (matches[k].p == sstart+al->voff) break;
              }

              if (k == noofmatches) {
                matches=realloc(matches, sizeof(gmatch_t)*(noofmatches+1));
                matches[noofmatches].p = sstart+al->voff;
                matches[noofmatches].q = sstart+result.a-1;
                matches[noofmatches].edist = result.b;
                matches[noofmatches].i = i; 
                matches[noofmatches].j = i+M[i].branches[q].mat+
                  M[i].branches[q].mis+M[i].branches[q].ins-1;
                matches[noofmatches].scr = scr;
                matches[noofmatches].evalue = E;
                matches[noofmatches].mat = mat;
                matches[noofmatches].mis = mis;
                matches[noofmatches].ins = ins;
                matches[noofmatches].del = del;
                matches[noofmatches].subject = idx;
                matches[noofmatches].checklen = matches[noofmatches].j; 
                matches[noofmatches].al = al;
                noofmatches++;
                if(bestonly) {
                  bestscr = MIN(maxedist, (result.b+bedist));
                } 
              } else {
                wrapAlignment(al);
                FREEMEMORY(space, al);
              }
            }
          }
        }
      }
    }
  }


  for(i=0; i < s->seq->mapsize; i++) {
    FREEMEMORY(space, peq[i]);
  }

  FREEMEMORY(space, peq);      
  (*bscr)  = bestscr;
  (*nmatch) = noofmatches;

  return matches;
}