Esempio n. 1
0
int
compress_filter( void *opaque, int control,
		 IOBUF a, byte *buf, size_t *ret_len)
{
    size_t size = *ret_len;
    compress_filter_context_t *zfx = opaque;
    z_stream *zs = zfx->opaque;
    int rc=0;

    if( control == IOBUFCTRL_UNDERFLOW ) {
	if( !zfx->status ) {
	    zs = zfx->opaque = m_alloc_clear( sizeof *zs );
	    init_uncompress( zfx, zs );
	    zfx->status = 1;
	}

#ifndef __riscos__
	zs->next_out = buf;
#else /* __riscos__ */
	zs->next_out = (Bytef *) buf;
#endif /* __riscos__ */
	zs->avail_out = size;
	zfx->outbufsize = size; /* needed only for calculation */
	rc = do_uncompress( zfx, zs, a, ret_len );
    }
    else if( control == IOBUFCTRL_FLUSH ) {
	if( !zfx->status ) {
	    PACKET pkt;
	    PKT_compressed cd;

	    if( !zfx->algo )
	        zfx->algo = DEFAULT_COMPRESS_ALGO;
	    if( zfx->algo != 1 && zfx->algo != 2 )
	      BUG();
	    memset( &cd, 0, sizeof cd );
	    cd.len = 0;
	    cd.algorithm = zfx->algo;
	    init_packet( &pkt );
	    pkt.pkttype = PKT_COMPRESSED;
	    pkt.pkt.compressed = &cd;
	    if( build_packet( a, &pkt ))
		log_bug("build_packet(PKT_COMPRESSED) failed\n");
	    zs = zfx->opaque = m_alloc_clear( sizeof *zs );
	    init_compress( zfx, zs );
	    zfx->status = 2;
	}

#ifndef __riscos__
	zs->next_in = buf;
#else /* __riscos__ */
	zs->next_in = (Bytef *) buf;
#endif /* __riscos__ */
	zs->avail_in = size;
	rc = do_compress( zfx, zs, Z_NO_FLUSH, a );
    }
    else if( control == IOBUFCTRL_FREE ) {
	if( zfx->status == 1 ) {
	    inflateEnd(zs);
	    m_free(zs);
	    zfx->opaque = NULL;
	    m_free(zfx->outbuf); zfx->outbuf = NULL;
	}
	else if( zfx->status == 2 ) {
#ifndef __riscos__
	    zs->next_in = buf;
#else /* __riscos__ */
	    zs->next_in = (Bytef *) buf;
#endif /* __riscos__ */
	    zs->avail_in = 0;
	    do_compress( zfx, zs, Z_FINISH, a );
	    deflateEnd(zs);
	    m_free(zs);
	    zfx->opaque = NULL;
	    m_free(zfx->outbuf); zfx->outbuf = NULL;
	}
        if (zfx->release)
          zfx->release (zfx);
    }
    else if( control == IOBUFCTRL_DESC )
	*(char**)buf = "compress_filter";
    return rc;
}
Esempio 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;
}
Esempio n. 3
0
int
compress_filter_bz2( void *opaque, int control,
		     IOBUF a, byte *buf, size_t *ret_len)
{
  size_t size = *ret_len;
  compress_filter_context_t *zfx = opaque;
  bz_stream *bzs = zfx->opaque;
  int rc=0;

  if( control == IOBUFCTRL_UNDERFLOW )
    {
      if( !zfx->status )
	{
	  bzs = zfx->opaque = xmalloc_clear( sizeof *bzs );
	  init_uncompress( zfx, bzs );
	  zfx->status = 1;
	}

      bzs->next_out = buf;
      bzs->avail_out = size;
      zfx->outbufsize = size; /* needed only for calculation */
      rc = do_uncompress( zfx, bzs, a, ret_len );
    }
  else if( control == IOBUFCTRL_FLUSH )
    {
      if( !zfx->status )
	{
	  PACKET pkt;
	  PKT_compressed cd;

	  if( zfx->algo != COMPRESS_ALGO_BZIP2 )
	    BUG();
	  memset( &cd, 0, sizeof cd );
	  cd.len = 0;
	  cd.algorithm = zfx->algo;
	  init_packet( &pkt );
	  pkt.pkttype = PKT_COMPRESSED;
	  pkt.pkt.compressed = &cd;
/*	  if( build_packet( a, &pkt ))
	    log_bug("build_packet(PKT_COMPRESSED) failed\n"); */ //FIXME
	  bzs = zfx->opaque = xmalloc_clear( sizeof *bzs );
	  init_compress( zfx, bzs );
	  zfx->status = 2;
	}

      bzs->next_in = buf;
      bzs->avail_in = size;
      rc = do_compress( zfx, bzs, BZ_RUN, a );
    }
  else if( control == IOBUFCTRL_FREE )
    {
      if( zfx->status == 1 )
	{
	  BZ2_bzDecompressEnd(bzs);
	  xfree(bzs);
	  zfx->opaque = NULL;
	  xfree(zfx->outbuf); zfx->outbuf = NULL;
	}
      else if( zfx->status == 2 )
	{
	  bzs->next_in = buf;
	  bzs->avail_in = 0;
	  do_compress( zfx, bzs, BZ_FINISH, a );
	  BZ2_bzCompressEnd(bzs);
	  xfree(bzs);
	  zfx->opaque = NULL;
	  xfree(zfx->outbuf); zfx->outbuf = NULL;
	}
      if (zfx->release)
	zfx->release (zfx);
    }
  else if( control == IOBUFCTRL_DESC )
    *(char**)buf = "compress_filter";
  return rc;
}
Esempio n. 4
0
int main(int argc, char **argv)
{
	char buffer[256];
	int s, addrlen = sizeof(struct sockaddr_in);
	struct sockaddr_in addr;
	struct hostent *hp;
	struct servent *sp;
	int verbose = 1;

	paclen_in = 1024;
	paclen_out = 1024;

	while ((s = getopt(argc, argv, "ci:o:q")) != -1) {
		switch (s) {
		case 'c':
			init_compress();
			compression = 1;
			break;
		case 'i':
			paclen_in = atoi(optarg);
			break;
		case 'o':
			paclen_out = atoi(optarg);
			break;
		case 'q':
			verbose = 0;
			break;
		case ':':
		case '?':
			err("ERROR: invalid option usage\n");
			return 1;
		}
	}

	if (paclen_in < 1 || paclen_out < 1) {
		err("ERROR: invalid paclen\n");
		return 1;
	}

	/*
	 * Arguments should be "tcp_call remaddr remport"
	 */
	if ((argc - optind) != 2) {
		strcpy(buffer, "ERROR: invalid number of parameters\n");
		err(buffer);
	}

	/*
	 * Open the socket into the kernel.
	 */
	if ((s = socket(AF_INET, SOCK_STREAM, 0)) < 0) {
		sprintf(buffer, "ERROR: can't open socket: %s\n", strerror(errno));
		err(buffer);
	}

	/*
	 * Resolve the hostname.
	 */
	hp = gethostbyname(argv[optind]);
	if (hp == NULL) {
		err("ERROR: Unknown host\n");
	}
	addr.sin_family = AF_INET;
	addr.sin_addr.s_addr = ((struct in_addr *)(hp->h_addr))->s_addr;

	/*
	 * And the service name.
	 */
	if ((sp = getservbyname(argv[optind+1], "tcp")) != NULL)
		addr.sin_port = sp->s_port;
	else
		addr.sin_port = htons(atoi(argv[optind+1]));

	if (addr.sin_port == 0) {
		err("ERROR: Unknown service\n");
	}

	if (verbose) {
		sprintf(buffer, "*** Connecting to %s ...\n", hp->h_name);
		user_write(STDOUT_FILENO, buffer, strlen(buffer));
	}

	/*
	 * If no response in 30 seconds, go away.
	 */
	alarm(30);

	signal(SIGALRM, alarm_handler);

	/*
	 * Lets try and connect to the far end.
	 */
	if (connect(s, (struct sockaddr *)&addr, addrlen) != 0) {
		sprintf(buffer, "ERROR: can't connect: %s\n", strerror(errno));
		err(buffer);
	}

	/*
	 * We got there.
	 */
	alarm(0);

	if (verbose) {
		strcpy(buffer, "*** Connected\n");
		user_write(STDOUT_FILENO, buffer, strlen(buffer));
	}

	select_loop(s);

	if (verbose) {
		strcpy(buffer, "\n*** Disconnected\n");
		user_write(STDOUT_FILENO, buffer, strlen(buffer));
	}

	end_compress();

	return 0;
}