Ejemplo n.º 1
0
/* Read all parameters from the files */
void read_param(char *file_name)
{
   FILE *fp;
   char buf[MAXLEN],*c,*word;
   TextLine *line;
   double number;
   int i,n,rinput[NINPUT];
   
   for(i=0;i<NINPUT;i++)
      rinput[i] = FALSE; 
   
   error = 0;

   line = NULL;

   fp = fopen(file_name,"r");
      if(fp == NULL){
         error_dialog_open(file_name);
         return;
      }

   while(fgets(buf,MAXLEN,fp)!=NULL){
      i = 0;
      while(i<NINPUT && (c=strstr(buf,inlines[i]))==NULL)
         i++;
      
      if(i<NINPUT){
         c += strlen(inlines[i]);
         c = trim_space(c);
      }

      switch(i){
         case I_TYPE:
            if(c != NULL && strcmp(c, "")){
               word = get_word(c,&n);  /* we should free word-memory also! */
               if(strcmp(word,"ERD") == 0)
                  sim_type = SIM_ERD;
               else if(strcmp(word,"RBS") == 0)
                  sim_type = SIM_RBS;
 	           else{
 	              sim_type = SIM_ERD;
                  error = 1;
               }
               free(word);
            }
            else{
               sim_type = SIM_ERD;
               error = 1;
            }
            break;
         case  I_ION:
            if(c != NULL){
	      if(strcmp(c, "")){
                 c = get_string(c);
                 beam_ion = c;
	      }
	      else
	         beam_ion = "";
            }
            else{
               beam_ion = "";
               error = 1;
            }
            break;
         case  I_ENERGY:
            if(c != NULL && strcmp(c, "")){
               c = get_number(c,&number);
               beam_energy = number;
            }
            else{
               beam_energy = 0;
               error = 1;
            }
            break;
         case  I_RECOIL:
            if(c != NULL){
               if(strcmp(c, "")){
                  c = get_string(c);
                  recoil_atom = c;
	       }
	       else
	          recoil_atom = "";
            }
            else{
               recoil_atom = "";
               error = 1;
            }
            break;
         case I_TANGLE:
            if(c != NULL && strcmp(c, "")){
               c = get_number(c,&number);
               target_angle = number;
            }
            else{
               target_angle = 0;
               error = 1;
            }
            break;
         case I_SPOTSIZE:
            if(c != NULL && strcmp(c, "")){
               c = get_number(c,&number);
               beam_size_x = number;
            }
            else{
               beam_size_x = 0;
               error = 1;
            }
            if(c != NULL && strcmp(c, "")){
               c = get_number(c,&number);
               beam_size_y = number;
            }
            else{
               beam_size_y = 0;
               error = 1;
            }
            break;
         case I_MINANGLE:
            if(c != NULL && strcmp(c, "")){
               c = get_number(c,&number);
               min_angle = number;
            }
            else{
               min_angle = 0;
               error = 1;
            }
            break;
         case I_MINENERGY:
            if(c != NULL && strcmp(c, "")){
               c = get_number(c,&number);
               min_energy = number;
            }
            else{
               min_energy = 0;
               error = 1;
            }
            break;
         case I_NIONS:
            if(c != NULL && strcmp(c, "")){
               c = get_number(c,&number);
               nions = number;
            }
            else{
               nions = 0;
               error = 1;
            }
            break;
         case I_NPREIONS:
            if(c != NULL && strcmp(c, "")){
               c = get_number(c,&number);
               nions_presim = number;
            }
            else{
               nions_presim = 0;
               error = 1;
            }
            break;
         case I_RECAVE:
            if(c != NULL && strcmp(c, "")){
               c = get_number(c,&number);
               aver_nions = number;
            }
            else{
               aver_nions = 0;
               error = 1;
            }
            break;
         case I_SEED:
            if(c != NULL && strcmp(c, "")){
               c = get_number(c,&number);
               nseed = number;
            }
            else{
               nseed = 0;
               error = 1;
            }
            break;
         case I_RECWIDTH:
            if(c != NULL && strcmp(c, "")){
               word = get_word(c,&n);
               if(strcmp(word,"wide") == 0 || strcmp(word,"WIDE") == 0)
                  angle_width = REC_WIDE;
               else if(strcmp(word,"narrow") == 0 || strcmp(word,"NARROW") == 0)
                  angle_width = REC_NARROW;
               else{
                  angle_width = REC_NARROW;
                  error = 1;
               }
               free(word);
            }
            else{
               angle_width = REC_NARROW;
               error = 1;
            }
            break;
         default:
            break;
      }
   }
   
   read_target_file(file_name, TYPE_TARGET);
   read_target_file(file_name, TYPE_FOIL);
   read_detector_file(file_name);
   read_rdist_file(file_name);
   read_presimu_file(file_name);
   read_espe_params_file(file_name);

  /* If error occurs somewhere */
  if(error)
    error_dialog("Problems with reading the data");
    
  fclose(fp);
  
  /* Clean "memory" */
  format_layers(nlayers);
  format_foils(nfoils);
    
  saved = 1;
}
Ejemplo n.º 2
0
int main(int argc,char *argv[])
{
    int i,j,k,c,err,nthr;
    int block_size,extend,detail,combine,out_cnt,format;
    struct lk_compress *lkc;
    struct contig *contigs,*ctg;
    struct range_blk *r;
    struct tdc_par tp;
    char *ranges_file,*target_file,*output_file,*compare_ref;
    char *filter,*suffix,*tn,*prefix,*pp;
    count *ct,cc,cbuf[OUT_WIDTH];
    u_int64_t *hist,*thist,nn,kk,tnn,tkk,number;
    u_int32_t x;
    pthread_t *read_threads;
    FILE *ofptr,*ctg_fptr;
    static struct option longopts[]= {
        {"ranges_file",required_argument,0,'r'},
        {"target_file",required_argument,0,'t'},
        {"block_size",required_argument,0,'b'},
        {"number",required_argument,0,'n'},
        {"output",required_argument,0,'o'},
        {"extend_regions",required_argument,0,'x'},
        {"detailed_output",no_argument,0,'d'},
        {"combine",no_argument,0,'c'},
        {"compare",required_argument,0,'C'},
        {"eland",no_argument,0,'E'},
        {"gem",no_argument,0,'G'},
        {"prefix",no_argument,0,'p'},
        {0,0,0,0}
    };

    err=0;
    detail=combine=0;
    ranges_file=target_file=output_file=compare_ref=prefix=0;
    block_size=1;
    extend=0;
    contigs=0;
    number=0;
    format=GEM_FMT;
    while((c=getopt_long(argc,argv,"p:r:t:b:o:n:x:C:dcEG",longopts,0))!=-1) {
        switch(c) {
        case 'p':
            set_opt("prefix",&prefix,optarg);
            break;
        case 'r':
            set_opt("ranges_file",&ranges_file,optarg);
            break;
        case 't':
            set_opt("target_file",&target_file,optarg);
            break;
        case 'C':
            set_opt("compare",&compare_ref,optarg);
            break;
        case 'o':
            set_opt("output",&output_file,optarg);
            break;
        case 'b':
            block_size=atoi(optarg);
            break;
        case 'E':
            format=ELAND_FMT;
            break;
        case 'G':
            format=GEM_FMT;
            break;
        case 'x':
            extend=atoi(optarg);
            break;
        case 'n':
            number=strtoul(optarg,&pp,10);
            break;
        case 'd':
            detail=1;
            break;
        case 'c':
            combine=1;
            break;
        }
    }
    if(!ranges_file) {
        fprintf(stderr,"No ranges file specified.  Use -r or --ranges_file option\n");
        exit(-1);
    }
    if(!output_file) output_file=strdup("coverage_hist.txt");
    if(block_size<1) {
        fprintf(stderr,"Invalid block size specified - must be >0\n");
        exit(-1);
    }
    printf("Block size = %d, extend range = %d\n",block_size,extend);
    hist=lk_malloc(sizeof(u_int64_t)*(MAX_COUNT+1));
    if(target_file) thist=lk_malloc(sizeof(u_int64_t)*(MAX_COUNT+1));
    else thist=0;
    lkc=init_compress();
    err=read_ranges_file(ranges_file,&contigs,lkc);
    if(!err && target_file) err=read_target_file(target_file,contigs,extend,lkc);
    nthr=(int)sysconf(_SC_NPROCESSORS_ONLN);
    input_files=argv;
    input_idx=optind;
    n_input_files=argc;
    if(!nthr) nthr=1;
    read_threads=malloc(sizeof(pthread_t)*nthr);
    if(combine) {
        tp.ctg=contigs;
        tp.lkc=lkc;
        for(i=0; i<nthr; i++) {
            if((j=pthread_create(read_threads+i,NULL,read_det_cov,&tp))) abt(__FILE__,__LINE__,"Thread creation %d failed: %d\n",i+1,j);
        }
        for(i=0; i<nthr; i++) pthread_join(read_threads[i],NULL);
    } else {
        for(i=optind; !err && i<argc; i++) {
            err=process_file(argv[i],contigs,block_size,format,&number,lkc);
        }
    }
    printf("Generating histogram\n");
    filter=suffix=0;
    if(lkc->default_compress<COMPRESS_NONE) {
        i=lkc->default_compress;
        filter=lkc->comp_path[i][i==COMPRESS_ZIP?1:0];
        suffix=lkc->compress_suffix[i];
    }
    tn=0;
    if(prefix) {
        asprintf(&tn,"%s_contig_summ.txt",prefix);
        assert(tn);
        ctg_fptr=fopen(tn,"w");
        free(tn);
    } else ctg_fptr=fopen("contig_summ.txt","w");
    if(target_file) {
        for(i=0; i<=MAX_COUNT; i++) hist[i]=thist[i]=0;
        for(ctg=contigs; ctg; ctg=ctg->hh.next) {
            ct=ctg->counts;
            for(i=0; i<(int)ctg->size; i+=block_size) {
                cc=ct[i];
                if(cc<=MAX_COUNT) {
                    hist[cc]++;
                    ctg->tot_count+=cc;
                }
            }
            ct=ctg->tcounts;
            for(i=0; i<(int)ctg->tsize; i+=block_size) {
                cc=ct[i];
                if(cc<=MAX_COUNT) {
                    thist[cc]++;
                    ctg->tot_tcount+=cc;
                }
            }
            if(ctg_fptr) {
                fprintf(ctg_fptr,"%s\t%u\t%"PRIu64"\t%g",ctg->name,ctg->tot_bases,ctg->tot_count,(double)ctg->tot_count/(double)ctg->tot_bases);
                fprintf(ctg_fptr,"\t%u\t%"PRIu64"\t%g\n",ctg->tot_tbases,ctg->tot_tcount,ctg->tot_tbases?(double)ctg->tot_tcount/(double)ctg->tot_tbases:0.0);
            }
        }
        if(ctg_fptr) fclose(ctg_fptr);
        nn=tnn=0;
        for(i=0; i<=MAX_COUNT; i++) {
            nn+=hist[i];
            tnn+=thist[i];
        }
        kk=tkk=0;
        ofptr=fopen(output_file,"w");
        if(!ofptr) ofptr=stdout;
        for(i=0; i<=MAX_COUNT; i++) {
            kk+=hist[i];
            tkk+=thist[i];
            fprintf(ofptr,"%d\t%"PRIu64"\t%g\t%g\t",i,hist[i],(double)hist[i]/(double)nn,(double)kk/(double)nn);
            fprintf(ofptr,"%"PRIu64"\t%g\t%g\n",thist[i],(double)thist[i]/(double)tnn,(double)tkk/(double)tnn);
        }
        if(ofptr!=stdout) fclose(ofptr);
    } else {
        for(i=0; i<=MAX_COUNT; i++) hist[i]=0;
        for(ctg=contigs; ctg; ctg=ctg->hh.next) {
            ct=ctg->counts;
            for(i=0; i<(int)ctg->size; i+=block_size) {
                cc=ct[i];
                if(cc<=MAX_COUNT) {
                    hist[cc]++;
                    ctg->tot_count+=cc;
                }
            }
            if(ctg_fptr) {
                fprintf(ctg_fptr,"%s\t%u\t%"PRIu64"\t%g\n",ctg->name,ctg->tot_bases,ctg->tot_count,(double)ctg->tot_count/(double)ctg->tot_bases);
            }
        }
        if(ctg_fptr) fclose(ctg_fptr);
        nn=0;
        for(i=0; i<=MAX_COUNT; i++) nn+=hist[i];
        kk=0;
        ofptr=fopen(output_file,"w");
        if(!ofptr) ofptr=stdout;
        for(i=0; i<=MAX_COUNT; i++) {
            kk+=hist[i];
            fprintf(ofptr,"%d\t%"PRIu64"\t%g\t%g\n",i,hist[i],(double)hist[i]/(double)nn,(double)kk/(double)nn);
        }
        if(ofptr!=stdout) fclose(ofptr);
    }
    if(detail) {
        tn=0;
        ofptr=0;
        if(filter && suffix) {
            asprintf(&tn,"detailed_coverage.txt.%s",suffix);
            if(tn) {
                i=child_open(WRITE,tn,filter);
                ofptr=fdopen(i,"w");
            }
        }
        if(!ofptr) ofptr=fopen("detailed_coverage.txt","w");
        if(!ofptr) ofptr=stdout;
        printf("Writing detailed coverage information\n");
        for(ctg=contigs; ctg; ctg=ctg->hh.next) {
            k=0;
            ct=ctg->counts;
            /*      ct1=ctg->tcounts;*/
            for(r=ctg->ranges; r; r=r->next) {
                for(i=0; i<r->idx; i++) {
                    if(!k++) fprintf(ofptr,"*%s\t%x\n",ctg->name,r->x1[i]);
                    else fprintf(ofptr,"*\t%x\n",r->x1[i]);
                    out_cnt=0;
                    for(x=r->x1[i]; x<=r->x2[i]; x++) {
                        cbuf[out_cnt++]=ct[x-1];
                        if(out_cnt==OUT_WIDTH) {
                            out_cnt=write_cbuf(cbuf,out_cnt,ofptr);
                        }
                    }
                    while(out_cnt) out_cnt=write_cbuf(cbuf,out_cnt,ofptr);
                }
            }
        }
        if(ofptr!=stdout) {
            fclose(ofptr);
            if(tn) {
                free(tn);
                while(waitpid(-1,&i,WNOHANG)>0);
            }
        }
    }
    return err;
}