예제 #1
0
/* Set clp to desired link, return True if found, False otherwise */
int llcheck(char* lookfor)
{
	if(!ll_length())
		return 0;
	for(;;)
	{
		if((*list->match)(list->clp->item, lookfor))
			return 1;
		else
		{
			if(!llnext())
				return 0;
		}
	}
}
예제 #2
0
/**
   @brief Check if the iterator can be advanced.

   @param iter A pointer to the iterator.
   @returns Whether the iterator can be advanced.
 */
bool ll_iter_has_next(smb_iter *iter)
{
  bool node_clean, index_clean;
  smb_ll_node *node = iter->state.data_ptr;
  const smb_ll *ds = iter->ds;

  // Is there a node to return?
  node_clean = node != NULL;
  // Is the index valid?
  index_clean = iter->index < ll_length(ds);

  // Both should have the same value.  It is impossible for the node to be null
  // when the index is in bounds.  So, assert it.
  assert(node_clean == index_clean);

  return node_clean;
}
예제 #3
0
void lladdhead(char *newitem) /* Add a new head, reset CLP. */
{
	struct LINKTYPE *newlink;
	struct LINKTYPE *llcrlink();

	/* If empty, initialize list */
	if(!ll_length())
	{
		llinit(newitem);
		return;
	}

	/* Create new link */
	newlink = llcrlink();
	moveitem(newitem, newlink->item);
	list->listlength++;
	
	/* Reset pointers */
	newlink->previous = NULL;
	newlink->next = list->head;
	list->head->previous = newlink;
	list->clp = list->head = newlink;
}
예제 #4
0
int main(int argc, char **argv)
{
	int n_tmor,n_nml,ncol,nbins;
	double  *tumor, *normal;
	SRM_binning args; 
	BIN_LIST bins = ll_new();
	
	args = option_assign(argc,argv);
	
	if(args.inbin_file==NULL){ /*input is not binned data*/
		tumor = read_table(args.in_tmor,&n_tmor,&ncol,-1,0);
		if(tumor==NULL) { fprintf(stderr,"Warning: the file %s is empty.\n",args.tumor_file);exit(1);}
		if(ncol!=1) {fprintf(stderr,"Error: tumor file has multiple columns.\n"); exit(1);}
		fprintf(stderr,"%d tumor reads loaded\n",n_tmor);

		normal = read_table(args.in_nml,&n_nml,&ncol,-1,0);
		if(normal==NULL)  { fprintf(stderr,"Warning: the file %s is empty.\n",args.normal_file);exit(1);}
		if(ncol!=1) { fprintf(stderr,"Error: normal file has multiple columns.\n"); exit(1);}
		fprintf(stderr,"%d normal reads loaded\n",n_nml);


		if(args.fdr==1&&args.resampling==1){
                        qsort(tumor,n_tmor,sizeof(double),cmpdouble);
                        fprintf(stderr,"sorted %d case reads\n",n_tmor);
                        qsort(normal,n_nml,sizeof(double),cmpdouble);
                        fprintf(stderr,"sorted %d control reads.\n",n_nml);
                        check_read(tumor,n_tmor);
                        check_read(normal,n_nml);

                        fprintf(stderr,"Start resampling\n");
                        bic_seq_resample(tumor,n_tmor,normal,n_nml,args);
                        free(tumor);tumor=NULL;
                        free(normal);normal = NULL;		
			}

		else{
			fprintf(stderr,"Binning\n");
			bins = sort_rms_binning(tumor,n_tmor,normal,n_nml,args.bin_size,&nbins,args.win_size,args.quantile,args.multple,args.outlier,args.tumor_file,args.normal_file);
			/*sort, remove singular positions and bin*/

			if(args.binout_file!=NULL){
				fprintf(stderr,"Reporting Binned data\n");
				BIN_LIST_print(bins, args.outbin);
				}

			if(args.fdr!=1){
				set_BinList(bins);
				fprintf(stderr,"Merging\n");
				if(args.autoselect_lambda!=1){
					bic_seq(args.paired);
					}else{
					bic_seq_auto(ll_length(bins),args.FP, args.paired);
					}
				bins = get_BinList();
				BIN_LIST_print(bins, args.output);
				ll_dealloc(bins);
				}else{
				SEG_PERMUTE segs = NULL;
				segs = bic_seq_perm(bins, args.tumor_freq, args.FP,args.B ,args.autoselect_lambda);
			        print_SEG_PERMUTE(segs,args.output);
			        SEG_PERMUTE_destroy(segs); segs = NULL;
				}
			}

		}else{
		double N_total=0.0, N_tumor=0.0,freq;
		int start, end, tumor, total;
	        while (fscanf(args.inbin, "%d %d %lf %d %d",
	                &tumor, &total, &freq, &start, &end) != EOF) {
	                ll_append(bins, bin_new(tumor, total, freq, start, end));
	                N_total += total; N_tumor += tumor;
                	}
		 set_totalreadcount(N_tumor,N_total-N_tumor);
		 
		if(args.fdr!=1){
			set_BinList(bins);
			fprintf(stderr,"Merging\n");
			if(args.autoselect_lambda!=1){
				bic_seq(args.paired);
				//bic_seq(0);
				}else{
				bic_seq_auto(ll_length(bins),args.FP, args.paired);
				//bic_seq_auto(ll_length(bins),args.FP, 0);
				}
			bins = get_BinList();
			BIN_LIST_print(bins, args.output);
			ll_dealloc(bins);
			}else{
			SEG_PERMUTE segs = NULL;
			segs = bic_seq_perm(bins, args.tumor_freq, args.FP,args.B ,args.autoselect_lambda);
			print_SEG_PERMUTE(segs,args.output);
			SEG_PERMUTE_destroy(segs); segs = NULL;
			}
		}

	return 0;	

}
예제 #5
0
static void  bic_seq_resample(double *tumor, int n_tumor, double *normal, int n_nml, SRM_binning args)
{	SEG_PERMUTE segs = NULL;
	int *tumor_bin, *normal_bin, nbins;
	int n_tumor_sample, n_normal_sample,i,k, total,start,end, kmin;
	double tmp, freq, N_tumor, N_normal;
        struct timeval tv;
        int seed;

        gettimeofday(&tv, NULL);
        seed = tv.tv_sec * 1000000 + tv.tv_usec;
        seed_set(seed);
	srand48(seed);
	
	segs = SEG_PERMUTE_create(args.B);

	tmp = tumor[n_tumor-1] > normal[n_nml-1] ? tumor[n_tumor-1]:normal[n_nml-1];
	nbins = floor(tmp/args.bin_size)+10;
	nbins = nbins>10?nbins:10;
	tumor_bin = (int *) malloc(sizeof(int)*nbins);
	normal_bin = (int *)malloc(sizeof(int)*nbins);
	if(tumor_bin==NULL||normal_bin==NULL){
		fprintf(stderr,"Error in bic_seq_resample: memory allocation failed\n");
		exit(1);
		}

        tmp = tumor[0] < normal[0] ? tumor[0]:normal[0];
        kmin = (int) floor(tmp/args.bin_size)-1;
        kmin = (kmin>0? kmin:0);

	for(i=0;i<segs->size;i++){
		n_tumor_sample = rbinom(args.tumor_freq,n_tumor+n_nml);
		n_normal_sample = rbinom(1-args.tumor_freq,n_tumor+n_nml);
		random_sample(tumor, n_tumor, normal, n_nml, n_tumor_sample,  args.bin_size ,tumor_bin, nbins, args.paired, args.insert, args.sd);
		random_sample(tumor, n_tumor, normal, n_nml, n_normal_sample, args.bin_size ,normal_bin,nbins, args.paired, args.insert, args.sd);


		N_tumor=0.0; N_normal = 0.0;
		for(k=kmin;k<nbins;k++){
			start = k*args.bin_size+1;
			end = start+args.bin_size;
			total = tumor_bin[k] + normal_bin[k];
			freq = ((double) tumor_bin[k])/((double) total);
			if(total>0) ll_append(segs->bins_perm[i], bin_new(tumor_bin[k], total, freq, start, end));
			N_tumor += tumor_bin[k];
			N_normal += normal_bin[k];
			}
		set_BinList(segs->bins_perm[i]);
		set_totalreadcount(N_tumor,N_normal);

                if(args.autoselect_lambda!=1){
                        bic_seq(args.paired);
			//bic_seq(0);
                        }else{
                        bic_seq_auto(ll_length(segs->bins_perm[i]),args.FP,args.paired);
			//bic_seq_auto(ll_length(segs->bins_perm[i]),args.FP,0);
                        }
		segs->bins_perm[i] = get_BinList();
		}

	print_SEG_PERMUTE(segs,args.output);
	SEG_PERMUTE_destroy(segs); segs = NULL;
	free(tumor_bin); tumor_bin = NULL;
	free(normal_bin);normal_bin = NULL;

	return;
}