Ejemplo n.º 1
0
static double randu(double a, double b)
{
	return a + random_uniform() * (b - a);
}
Ejemplo n.º 2
0
// Returns num of bases printed
size_t sim_reads(seq_file_t *reffile, gzFile out0, gzFile out1,
                 FileList *flist, float err_rate,
                 size_t insert, double insert_stddev, size_t rlen, double depth)
{
  size_t i, chromcap = 16, nchroms, glen = 0, nreads, chr, pos0, pos1, tlen;
  read_t *chroms;

  tlen = rlen + (out1 == NULL ? 0 : insert + rlen);

  chroms = malloc(chromcap * sizeof(read_t));
  nchroms = 0;

  // Load genome
  printf(" Loaded contigs:");
  while(1)
  {
    if(nchroms == chromcap) chroms = realloc(chroms, (chromcap*=2)*sizeof(read_t));
    seq_read_alloc(&chroms[nchroms]);
    if(seq_read(reffile, &chroms[nchroms]) <= 0)
    { seq_read_dealloc(&chroms[nchroms]); break; }
    if(chroms[nchroms].seq.end < tlen) { seq_read_dealloc(&chroms[nchroms]); }
    else {
      seq_read_truncate_name(&chroms[nchroms]);
      printf(" %s[%zu]", chroms[nchroms].name.b, chroms[nchroms].seq.end);
      glen += chroms[nchroms].seq.end;
      nchroms++;
    }
  }
  printf("\n Genome size: %zu\n", glen);

  if(nchroms == 0) {
    die("No sequences long enough in ref genome file [min len: %zu]: %s",
        tlen, reffile->path);
  }

  // Sample
  nreads = (glen * depth) / (out1 == NULL ? rlen : (2 * rlen));
  char read0[rlen+1], read1[rlen+1];
  read0[rlen] = read1[rlen] = '\0';

  printf("Sampling %zu %sreads...\n", nreads,
         out1 == NULL ? "single " : "paired-end ");

  // Sample paired-end if out1 != NULL
  for(i = 0; i < nreads; i++)
  {
    chr = (nchroms == 1) ? 0 : rand_chrom(chroms, nchroms, glen);
    pos0 = random_uniform(chroms[chr].seq.end - (out1 == NULL ? rlen : tlen));
    pos1 = pos0;
    memcpy(read0, chroms[chr].seq.b+pos0, rlen);
    if(out1 != NULL) {
      pos1 = pos0 + rlen + insert + ran_normal()*insert_stddev;
      if(pos1 + rlen > chroms[chr].seq.end) pos1 = chroms[chr].seq.end-rlen;
      memcpy(read1, chroms[chr].seq.b+pos1, rlen);
    }
    if(flist != NULL) {
      add_seq_error_profile(read0, rlen, flist);
      if(out1 != NULL)
        add_seq_error_profile(read1, rlen, flist);
    }
    else if(err_rate >= 0) {
      add_seq_error_rate(read0, rlen, err_rate);
    }
    gzprintf(out0, ">r%zu:%s:%zu:%zu%s\n%.*s\n", i, chroms[chr].name.b,
                   pos0, pos1, (out1 != NULL ? "/1" : ""), (int)rlen, read0);
    if(out1 != NULL) {
      dna_revcmp(read1, rlen);
      gzprintf(out1, ">r%zu:%s:%zu:%zu/2\n%.*s\n", i, chroms[chr].name.b,
                     pos0, pos1, (int)rlen, read1);
    }
  }

  for(i = 0; i < nchroms; i++) seq_read_dealloc(&chroms[i]);
  free(chroms);

  size_t num_bases = nreads * rlen;
  if(out1 != NULL) num_bases *= 2;

  return num_bases;
}
Ejemplo n.º 3
0
/** sanitize

	Sanitizes a gridlabd model by clear names and position from object headers

    @returns 0 on success, -2 on error
 **/
extern "C" int sanitize(int argc, char *argv[])
{
	OBJECT *obj;
	FILE *fp;
	double delta_latitude, delta_longitude;

	// lat/lon change
	if ( strcmp(global_sanitizeoffset,"")==0 )
	{
		delta_latitude = random_uniform(NULL,-5,+5);
		delta_longitude = random_uniform(NULL,-180,+180);
	}
	else if ( global_sanitizeoffset=="destroy" )
		delta_latitude = delta_longitude = QNAN;
	else if ( sscanf(global_sanitizeoffset.get_string(),"%lf%*[,/]%lf",&delta_latitude,&delta_longitude)!=2 )
	{
		output_error("sanitize_offset lat/lon '%s' is not valid", global_sanitizeoffset.get_string());
		return -2;
	}

	// sanitize object names
	for ( obj=object_get_first() ; obj!=NULL ; obj=object_get_next(obj) )
	{
		if ( obj->name!=NULL && (global_sanitizeoptions&SO_NAMES)==SO_NAMES )
			sanitize_name(obj);
		if ( isfinite(obj->latitude) && (global_sanitizeoptions&SO_GEOCOORDS)==SO_GEOCOORDS )
		{
			obj->latitude += delta_latitude;
			if ( obj->latitude<-90 ) obj->latitude = -90;
			if ( obj->latitude>+90 ) obj->latitude = +90;
		}
		if ( isfinite(obj->longitude) && (global_sanitizeoptions&SO_GEOCOORDS)==SO_GEOCOORDS )
			obj->longitude = fmod(obj->longitude+delta_longitude,360);
	}

	// dump object name index
	if ( strcmp(global_sanitizeindex,".xml")==0 )
	{
		strcpy(global_sanitizeindex,global_modelname);
		char *ext = strrchr(global_sanitizeindex,'.');
		if ( ext && strcmp(ext,".glm")==0 ) strcpy(ext,"-index.xml");
		else strcat(global_sanitizeindex,"-index.xml");
	}
	else if ( strcmp(global_sanitizeindex,".txt")==0 )
	{
		strcpy(global_sanitizeindex,global_modelname);
		char *ext = strrchr(global_sanitizeindex,'.');
		if ( ext && strcmp(ext,".glm")==0 ) strcpy(ext,"-index.txt");
		else strcat(global_sanitizeindex,"-index.txt");
	}
	else if ( global_sanitizeindex[0]=='.' )
	{
		output_error("sanitization index file spec '%s' is not recognized", global_sanitizeindex.get_string());
		return -2;
	}
	if ( strcmp(global_sanitizeindex,"")!=0 )
	{
		char *ext = strrchr(global_sanitizeindex,'.');
		bool use_xml = (ext && strcmp(ext,".xml")==0) ;
		fp = fopen(global_sanitizeindex,"w");
		if ( fp )
		{
			SAFENAME *item;
			if ( use_xml )
			{
				fprintf(fp,"<data>\n");
				fprintf(fp,"\t<modelname>%s</modelname>\n",global_modelname);
				fprintf(fp,"\t<geographic_offsets>\n");
				fprintf(fp,"\t\t<latitude>%.6f</latitude>\n",delta_latitude);
				fprintf(fp,"\t\t<longitude>%.6f</longitude>\n",delta_longitude);
				fprintf(fp,"\t</geographic_offsets>\n");
				fprintf(fp,"\t<safename_list>\n");
				for ( item=safename_list ; item!=NULL ; item=item->next )
					fprintf(fp,"\t\t<name>\n\t\t\t<safe>%s</safe>\n\t\t\t<unsafe>%s</unsafe>\n\t\t</name>\n", item->name, item->old);
				fprintf(fp,"\t</safename_list>\n");
				fprintf(fp,"</data>\n");
			}
			else
			{
				fprintf(fp,"modelname\t= %s\n", global_modelname);
				fprintf(fp,"\n[POSITIONS]\n");
				fprintf(fp,"latitude\t= %.6f\n",delta_latitude);
				fprintf(fp,"longitude\t= %.6f\n",delta_longitude);
				fprintf(fp,"\n[NAMES]\n");
				for ( item=safename_list ; item!=NULL ; item=item->next )
					fprintf(fp,"%s\t= %s\n", item->name, item->old);
			}
			fclose(fp);
		}
	}

	return 0;
}
Ejemplo n.º 4
0
int main(int argc, char** argv) {
    KSOM_ERROR e = KSOM_OK;

    ksom_map map;
    ksom_map_options opt;

    double random_number = 0.0;
    double test [3];
    
    opt.fv_size = 3;
    opt.x_dim = 5;
    opt.y_dim = 5;
    opt.init_k_value = 0.1;
    opt.num_iterations = 100;


    //test random number generation functions
    printf("testing random\n");
    printf("initializing random... ");
    e = random_init();
    if (KSOM_FAIL(e)) goto end;
    printf("initialized.\n");

    printf("testing random (i=1000)... ");
    for (int i = 0; i < 1000; ++i) {
        random_number = random_uniform();
        if (random_number >= 1.0 || random_number <= 0.0) {
            printf("(iteration %d)error: number is %f\n", i, random_number);
            e = KSOM_INTERNAL;
            goto end;
        }
    }
    puts("ok.");
    puts("random ok.");

    printf("initializing map... ");
    e = ksom_map_init(&map, &opt);
    if (KSOM_FAIL(e)) goto end;
    puts("done.");

    for (int i = 0; i < map.num_nodes; ++i)
        printf("%10f %10f %10f\n", map.nodes[i].fv[0], map.nodes[i].fv[1], map.nodes[i].fv[2]);

    for (int i = 0; i < 10; ++i) {
        test[0] = random_uniform();
        test[1] = random_uniform();
        test[2] = random_uniform();

        printf("cycle %-3d... ", map.iteration_count);
        e = ksom_map_cycle(&map, test);
        if (KSOM_FAIL(e)) goto end;
        puts("done.");
        
        dump_map_fvdistances(&map);
    }


    

    printf("freeing map... ");
    e = ksom_map_free(&map);
    if (KSOM_FAIL(e)) goto end;
    puts("done.");

    end:
    printf("\n[!] %s\n", ksom_error_str(e));
    return (EXIT_SUCCESS);
}