void kref(char *name, FILE *fp, Table_T identifiers) { char buf[512], *filename = ""; int linenum; if (name) filename = name; for (linenum = 1; fgets(buf, sizeof buf, fp) != NULL; linenum++) { int i, j; for (i = 0; (j = getword(buf, &i, first, rest)) > 0; i = j) { char *id = Str_sub(buf, j, i); const char *ln = Atom_int(linenum); Seq_T seq; Table_T files; files = Table_get(identifiers, id); if (files == NULL) { files = Table_new(0, (int (*)(const void *, const void *))strcmp, strhash); Table_put(identifiers, id, files); } else FREE(id); seq = Table_get(files, filename); if (seq == NULL) { seq = Seq_new(0); Table_put(files, Str_dup(filename, 1, 0, 1), seq); Seq_addlo(seq, (void *)ln); } else if (Seq_get(seq, 0) != ln) Seq_addlo(seq, (void *)ln); } } }
void cref(char *name, FILE *fp, Table_T identifiers) { char buf[512]; Text_T filename = { 0, "" }; int linenum; if (name) filename = Text_put(name); for (linenum = 1; fgets(buf, sizeof buf, fp) != NULL; linenum++) { Text_T id, line = Text_put(buf); while ((id = getword(&line, first, rest)).len > 0) { Ring_T ring; Table_T files; files = Table_get(identifiers, &id); if (files == NULL) { files = Table_new(0, textcmp, texthash); Table_put(identifiers, copy(id), files); } ring = Table_get(files, &filename); if (ring == NULL) { ring = Ring_new(); Table_put(files, copy(filename), ring); Ring_addlo(ring, Integer_new(linenum)); } else if (Integer_get(Ring_get(ring, 0)) != linenum) Ring_addlo(ring, Integer_new(linenum)); } } }
void xref(const char *name, FILE *fp, Table_T identifiers){ char buf[128]; if (name == NULL) name = ""; name = Atom_string(name); linenum = 1; while (getword(fp, buf, sizeof buf, first, rest)) { Set_T set; Table_T files; const char *id = Atom_string(buf); files = Table_get(identifiers, id); if (files == NULL) { files = Table_new(0, NULL, NULL); Table_put(identifiers, id, files); } set = Table_get(files, name); if (set == NULL) { set = Set_new(0, intcmp, inthash); Table_put(files, name, set); } { int *p = &linenum; if (!Set_member(set, p)) { NEW(p); *p = linenum; Set_put(set, p); } } } }
/* Umsections_new makes a new 'assembler' in the form of a Umsections_T. It * creates a new table and makes the first key value pair with the given * section. It also sets the assembler to emit to that section. */ Umsections_T Umsections_new(const char *section, int (*error)(void *errstate, const char *message), void *errstate) { Umsections_T assembler = malloc(sizeof(*assembler)); assert(assembler); Table_T table = Table_new(100, NULL, NULL); assert(table); Seq_T order = Seq_new(100); assert(order); Seq_addhi(order, (void *)section); Seq_T instructions = Seq_new(100); assert(instructions); Table_put(table, Atom_string(section), instructions); /* intializes the struct */ assembler->table = table; assembler->section = section; assembler->order = order; assembler->err_func = error; assembler->errstate = errstate; return assembler; }
T Umsections_new (const char *section, int (*error)(void *errstate, const char *message), void *errstate) { T Umsections = malloc(sizeof(*Umsections)); Umsections->sections = Table_new(TABLE_HINT, NULL, NULL); Seq_T temp; temp = Seq_new(SEQ_HINT); Seq_T temp2; temp2 = Seq_new(SEQ_HINT); Umsections->error = error; Umsections->errstate = errstate; Umsections->currentSection = temp; Umsections->sectionOrder = temp2; Seq_addlo(Umsections->sectionOrder, temp); Table_put(Umsections->sections, Atom_string(section), temp); /* Table_map((Umsections)->sections,applyFree,NULL); Table_free(&((Umsections)->sections)); Seq_free(&((Umsections)->sectionOrder)); // Seq_free(((Umsectinos)->currentSection)); free(Umsections); exit(1);*/ return Umsections; }
void wf(char *name, FILE *fp) { Table_T table = Table_new(0, NULL, NULL); char buf[128]; while (getword(fp, buf, sizeof buf, first, rest)) { const char *word; int i, *count; for (i = 0; buf[i] != '\0'; i++) buf[i] = tolower(buf[i]); word = Atom_string(buf); count = Table_get(table, word); if (count) (*count)++; else { NEW(count); *count = 1; Table_put(table, word, count); } } if (name) printf("%s:\n", name); { int i; void **array = Table_toArray(table, NULL); qsort(array, Table_length(table), 2*sizeof (*array), compare); for (i = 0; array[i]; i += 2) printf("%d\t%s\n", *(int *)array[i+1], (char *)array[i]); FREE(array); } Table_map(table, vfree, NULL); Table_free(&table); }
int exponent_table_put(exponent_table *E, const exponent expon, int value) /* Puts the element 'expon => value' into the table, and if an element is already there, its value is returned, otherwise 0 is returned. Thus, it is a good idea to not insert zero values into the table */ { table_nvars = E->nvars; return (int) Table_put(E->table, expon, (void *) value); }
END_TEST START_TEST(test_put_replace) { /* New values replace old values with the same key. */ Table_T *table = Table_new(0, cmpatom, hashatom); Table_put(table, (void*)0x12, (void*)0x34); Table_put(table, (void*)0x12, (void*)0x56); fail_unless( Table_length(table) == 1 ); fail_unless( Table_get(table, (void*)0x12) == (void*)0x56); Table_remove(table, (void*)0x12); Table_free(&table); }
//todo: implement some throttle related to outstanding responses static void _save_context(T _this_, oe_id cid, ACContext *pc) { OE_DLOG(0, OEP_CLIENT_SAVE_C_W_CID, cid); assert(_this_); assert(_this_->pending); assert(cid); assert(pc); CID_KEY *key; key = Arena_alloc(pc->context->arena, sizeof *key, __FILE__, __LINE__); key->cid = cid; Table_put(_this_->pending, key, pc); }
void xref(const char *name, FILE *fp, Table_T identifiers){ char buf[128]; if (name == NULL){ name = ""; } name = Atom_string(name); linenum = 1; while (getword(fp, buf, sizeof(buf), first, rest)){ Set_T set; Table_T files; const char *id = Atom_string(buf); // files <- file table in identifiers associated with id files = Table_get(identifiers, id); if (files == NULL){ files = Table_new(0, NULL, NULL); Table_put(identifiers, id, files); } // set <- set in files associated with name set = Table_get(files, name); if (set == NULL){ set = Set_new(0, intcmp, inthash); Table_put(files, name, set); } // add linenum to set, if necessary { int *p = &linenum; if (!Set_member(set, p)){ NEW(p); *p = linenum; Set_put(set, p); } } } }
END_TEST START_TEST(test_put_in_same_bucket) { /* Our hash function always places key/vals in the same bucket - * assert that multiple items can be placed and retreived from * the same bucket */ Table_T *table = Table_new(0, cmpatom, hashatom); Table_put(table, (void*)0x12, (void*)0x34); Table_put(table, (void*)0x56, (void*)0x78); fail_unless( Table_length(table) == 2 ); fail_unless( Table_get(table, (void*)0x12) == (void*)0x34); fail_unless( Table_get(table, (void*)0x56) == (void*)0x78); Table_remove(table, (void*)0x12); Table_remove(table, (void*)0x56); Table_free(&table); }
unsigned test_table() { Table_T tbl = Table_new(10, NULL, NULL); struct Test t1 = { 10, "Luca"}; char* s1 = TestToStr(&t1); const char* a1 = Atom_string(s1); char * result; Table_put(tbl, a1, "Luca"); result = (char*) Table_get(tbl, a1); test_assert(strcmp(result, "Luca") == 0); Table_free(&tbl); FREE(s1); Atom_freeAll(); return TEST_SUCCESS; }
/* symboluid - returns sym's uid, adding the symbol, if necessary */ static int symboluid(const Symbol p) { int uid; sym_symbol_ty sym; if (p == NULL) return 0; sym = Table_get(uidTable, p); if (sym != NULL) return sym->uid; uid = pickle->nuids++; switch (p->sclass) { case ENUM: sym = sym_ENUMCONST(p->name, uid, uname, NULL, 0, 0, p->u.value); sym->type = typeuid(inttype); break; case TYPEDEF: sym = sym_TYPEDEF(p->name, uid, uname, NULL, 0, 0); sym->type = typeuid(p->type); break; default: if (p->sclass == STATIC) { sym = sym_STATIC(p->name, uid, uname, NULL, 0, 0, Seq_length(statics)); Seq_addhi(statics, p); } else if (p->scope == GLOBAL || p->sclass == EXTERN) { sym = sym_GLOBAL(p->name, uid, uname, NULL, 0, 0, Seq_length(statics)); Seq_addhi(statics, p); } else if (p->scope == PARAM) sym = sym_PARAM(p->name, uid, uname, NULL, 0, 0, p->x.offset); else { assert(p->scope >= LOCAL); sym = sym_LOCAL(p->name, uid, uname, NULL, 0, 0, p->x.offset); } sym->type = typeuid(p->type); } Table_put(uidTable, p, sym); Seq_addhi(pickle->items, sym_Symbol(uid, sym)); sym->src = sym_coordinate(p->src.file ? p->src.file : string(""), p->src.x, p->src.y); sym->uplink = symboluid(up(p->up)); return sym->uid; }
/*Inserts the value into the table under the corresponding fingerprint*/ void insert_atom(Table_T* table, char* fingerprint, char* name) { const char *key; const char *value; Set_T old_set; /*Turns fingerprint and name strings into Hanson atoms*/ key = Atom_string(fingerprint); value = Atom_string(name); old_set = Table_get(*table, key); if (old_set == NULL) { Set_T new_set = Set_new(1, NULL, NULL); Set_put(new_set, value); Table_put(*table, key, new_set); } else { Set_put(old_set, value); } }
END_TEST START_TEST(test_get) { /* Table_get() with non-existant key returns NULL */ Table_T *table = Table_new(0, cmpatom, hashatom); Table_put(table, (void*)0x12, (void*)0x34); fail_unless( Table_length(table) == 1 ); fail_unless( Table_get(table, (void*)0x12) == (void*)0x34); /* non-existant key */ fail_unless( Table_get(table, (void*)0x34) == NULL); Table_remove(table, (void*)0x12); Table_free(&table); }
__declspec(dllexport) int curl_shim_install_delegates(void* handle, void* pvThis, void* pvWriteDel, void* pvReadDel, void* pvProgDel, void* pvDebugDel, void* pvHeaderDel, void* pvSSLContextDel, void* pvIoctlDel) { // cast return from GetProcAddress as a CPROC CPROC pcp = (CPROC)GetProcAddress(g_hModCurl, "curl_easy_setopt"); // install all delegates through here when this works unsigned int* pnDelegates = malloc(7 * sizeof(unsigned int)); pnDelegates[0] = (unsigned int)pvWriteDel; pnDelegates[1] = (unsigned int)pvReadDel; pnDelegates[2] = (unsigned int)pvProgDel; pnDelegates[3] = (unsigned int)pvDebugDel; pnDelegates[4] = (unsigned int)pvHeaderDel; pnDelegates[5] = (unsigned int)pvSSLContextDel; pnDelegates[6] = (unsigned int)pvIoctlDel; // add to the table (need to serialize access) EnterCriticalSection(&g_csDelegateTable); Table_put(g_delegateTable, pvThis, pnDelegates); LeaveCriticalSection(&g_csDelegateTable); // setup the callbacks from libcurl pcp(handle, CURLOPT_WRITEFUNCTION, write_callback_impl); pcp(handle, CURLOPT_WRITEDATA, pvThis); pcp(handle, CURLOPT_READFUNCTION, read_callback_impl); pcp(handle, CURLOPT_READDATA, pvThis); pcp(handle, CURLOPT_PROGRESSFUNCTION, progress_callback_impl); pcp(handle, CURLOPT_PROGRESSDATA, pvThis); pcp(handle, CURLOPT_DEBUGFUNCTION, debug_callback_impl); pcp(handle, CURLOPT_DEBUGDATA, pvThis); pcp(handle, CURLOPT_HEADERFUNCTION, header_callback_impl); pcp(handle, CURLOPT_HEADERDATA, pvThis); pcp(handle, CURLOPT_SSL_CTX_FUNCTION, ssl_ctx_callback_impl); pcp(handle, CURLOPT_SSL_CTX_DATA, pvThis); pcp(handle, CURLOPT_IOCTLFUNCTION, ioctl_callback_impl); pcp(handle, CURLOPT_IOCTLDATA, pvThis); return 0; }
void addToTableHelp( Table_T fpTable, fpPair pair ) { List_T names = NULL; const char * fpAtom = Atom_string(pair.fp); // if the key does exist in the table // get the list already stored if( Table_get( fpTable, fpAtom ) != NULL ) { names = Table_get( fpTable, fpAtom ); } // pushes the the list names = List_push( names, pair.name); Table_put( fpTable, fpAtom, names ); free(pair.fp); // List_map( names , applyFree, NULL ); }
terminal *terminalTable(Table_T termtable, subBoard state) { int player; int i; int w; int *canon = canonBoard(state); terminal *terms; if ((terms = Table_get(termtable, canon)) != 0) { free(canon); return terms; } terms = (terminal *) calloc(1, sizeof(terminal)); if ((w = win(state)) != 0) { terms->max = (w == 1) ? 1 : 0; terms->min = (w == -1) ? 1 : 0; } else if (countOpen(state) == 0) { terms->max = 0; terms->min = 0; } else { int childState[9]; memcpy(childState, state, 9*sizeof(int)); for (i = 0; i < 9; ++i) { if (childState[i] == 0) { for (player = 0; player < 2; ++player) { childState[i] = (2*player) - 1; terminal *childterm = terminalTable(termtable, childState); terms->max += childterm->max; terms->min += childterm->min; } childState[i] = 0; } } } Table_put(termtable, canon, terms); return terms; }
//calculate the most unique fld in item against the map of lists of templates //put the tuple in that list associated with the most unique key static int _add_template(T _this_, item_ *item) { Table_T items = _this_->items; //calc most unique key oe_scalar key = NULL; int key_idx = 0; int set_size = 0; Set_T curset = NULL; for ( int i = 0; item->object[i]; i++ ) { oe_scalar tmpkey = item->object[i]; if (strcmp("_", tmpkey) == 0) continue; //don't index by wildcard Set_T s = (Set_T) Table_get( items, tmpkey ); if (s) { int size = Set_length( s ); if ( !key || size < set_size ) { key = tmpkey; key_idx = i; set_size = size; curset = s; } } else { //found an unused key key = tmpkey; key_idx = i; set_size = 0; curset = Set_new( 10, _itemcmp, _itemhash ); Table_put( items, key, curset ); break; } } if (!curset) { OE_ERR(0, "Dispatcher can not add template, make sure it is not all '_' fields\n"); //i18n return -1; } item->items = curset; item->key_idx = key_idx; //for self remove of empty sets Set_put( curset, item ); return 0; }
static void store_accession (Table_T accsegmentpos_table, Tableint_T chrlength_table, char *accession, char *chr_string, Genomicpos_T chrpos1, Genomicpos_T chrpos2, bool revcompp, Genomicpos_T seglength, int contigtype, unsigned int universal_coord) { Chrom_T chrom; Segmentpos_T segmentpos; chrom = Chrom_from_string(chr_string,mitochondrial_string,/*order*/universal_coord); segmentpos = Segmentpos_new(chrom,chrpos1,chrpos2,revcompp,seglength,contigtype); Table_put(accsegmentpos_table,(void *) accession,(void *) segmentpos); /* Update chrlength */ if (chrpos2 > chrpos1 + seglength) { chrlength_update(chrlength_table,chrom,chrpos2); } else { chrlength_update(chrlength_table,chrom,chrpos1+seglength); } return; }
__declspec(dllexport) int curl_shim_install_share_delegates( void* handle, void* pvThis, void* pvLockDel, void* pvUnlockDel) { // cast return from GetProcAddress as a CPROC CPROC pcp = (CPROC)GetProcAddress(g_hModCurl, "curl_share_setopt"); // install delegates unsigned int* pnDelegates = malloc(2 * sizeof(unsigned int)); pnDelegates[0] = (unsigned int)pvLockDel; pnDelegates[1] = (unsigned int)pvUnlockDel; // add to the table, with serialized access EnterCriticalSection(&g_csShareDelegateTable); Table_put(g_shareDelegateTable, pvThis, pnDelegates); LeaveCriticalSection(&g_csShareDelegateTable); // set up the callbacks from libcurl pcp(handle, CURLSHOPT_LOCKFUNC, lock_callback_impl); pcp(handle, CURLSHOPT_UNLOCKFUNC, unlock_callback_impl); pcp(handle, CURLSHOPT_USERDATA, pvThis); return 0; }
void store_name(char* fprint_buffer, char* name_buf, T* table, L* name_list) { const char* fprint = Atom_string(fprint_buffer); const char* name = Atom_string(name_buf); char **name_arr = (char**) List_toArray(*name_list, NULL); for(int z = 0; name_arr[z]; z++) { if(name_arr[z] == name) { fprintf(stderr, "Duplicate name detected: Ignoring input\n"); free (name_arr); return; } } free(name_arr); *name_list = List_push(*name_list, (void*) name); L list = (L) Table_get(*table, fprint); if (list == NULL) { list = List_list((char*) name, NULL); } else { list = List_push(list, (void*) name); } Table_put(*table, fprint, list); }
/* Modifies chrlength_table to store offsets, rather than chrlengths */ static void write_chromosome_file (char *genomesubdir, char *fileroot, Tableint_T chrlength_table) { FILE *textfp, *chrsubsetfp; char *divstring, *textfile, *chrsubsetfile, *iitfile, *chr_string, emptystring[1]; int n, i; Chrom_T *chroms; Genomicpos_T chroffset = 0, chrlength; List_T divlist = NULL; List_T intervallist = NULL, chrtypelist = NULL, labellist = NULL, annotlist = NULL, p; Table_T intervaltable, labeltable, annottable; Interval_T interval; emptystring[0] = '\0'; if (divsort == NO_SORT) { chroms = (Chrom_T *) Tableint_keys_by_timeindex(chrlength_table,0U); n = Tableint_length(chrlength_table); } else { /* Get chromosomes in order */ chroms = (Chrom_T *) Tableint_keys(chrlength_table,0U); n = Tableint_length(chrlength_table); switch (divsort) { case ALPHA_SORT: qsort(chroms,n,sizeof(Chrom_T),Chrom_compare_alpha); break; case NUMERIC_ALPHA_SORT: qsort(chroms,n,sizeof(Chrom_T),Chrom_compare_numeric_alpha); break; case CHROM_SORT: qsort(chroms,n,sizeof(Chrom_T),Chrom_compare_chrom); break; default: abort(); } } /* Write chromosome text file and chrsubset file */ textfile = (char *) CALLOC(strlen(genomesubdir)+strlen("/")+ strlen(fileroot)+strlen(".chromosome")+1,sizeof(char)); sprintf(textfile,"%s/%s.chromosome",genomesubdir,fileroot); /* Use binary, not text, so files are Unix-compatible */ if ((textfp = FOPEN_WRITE_BINARY(textfile)) == NULL) { fprintf(stderr,"Can't write to file %s\n",textfile); exit(9); } FREE(textfile); chrsubsetfile = (char *) CALLOC(strlen(genomesubdir)+strlen("/")+ strlen(fileroot)+strlen(".chrsubset")+1,sizeof(char)); sprintf(chrsubsetfile,"%s/%s.chrsubset",genomesubdir,fileroot); /* Use binary, not text, so files are Unix-compatible */ if ((chrsubsetfp = FOPEN_WRITE_BINARY(chrsubsetfile)) == NULL) { fprintf(stderr,"Can't write to file %s\n",chrsubsetfile); exit(9); } FREE(chrsubsetfile); fprintf(chrsubsetfp,">all\n"); fprintf(chrsubsetfp,"\n"); chrtypelist = List_push(chrtypelist,""); for (i = 0; i < n; i++) { chrlength = (Genomicpos_T) Tableint_get(chrlength_table,chroms[i]); assert(chroffset <= chroffset+chrlength-1); chr_string = Chrom_string(chroms[i]); if (i < 100) { fprintf(stderr,"Chromosome %s has universal coordinates %u..%u\n", chr_string,chroffset+1,chroffset+1+chrlength-1); } else if (i == 100) { fprintf(stderr,"More than 100 contigs. Will stop printing messages\n"); } if (n <= 100) { fprintf(chrsubsetfp,">%s\n",chr_string); fprintf(chrsubsetfp,"+%s\n",chr_string); } fprintf(textfp,"%s\t%u..%u\t%u\n", chr_string,chroffset+1,chroffset+chrlength,chrlength); intervallist = List_push(intervallist,(void *) Interval_new(chroffset,chroffset+chrlength-1U,0)); labellist = List_push(labellist,(void *) chr_string); annotlist = List_push(annotlist,(void *) emptystring); /* No annotations */ Tableint_put(chrlength_table,chroms[i],chroffset); chroffset += chrlength; } FREE(chroms); intervallist = List_reverse(intervallist); labellist = List_reverse(labellist); fclose(chrsubsetfp); fclose(textfp); /* Write chromosome IIT file */ divstring = (char *) CALLOC(1,sizeof(char)); divstring[0] = '\0'; divlist = List_push(NULL,divstring); intervaltable = Table_new(65522,Table_string_compare,Table_string_hash); labeltable = Table_new(65522,Table_string_compare,Table_string_hash); annottable = Table_new(65522,Table_string_compare,Table_string_hash); Table_put(intervaltable,(void *) divstring,intervallist); Table_put(labeltable,(void *) divstring,labellist); Table_put(annottable,(void *) divstring,annotlist); iitfile = (char *) CALLOC(strlen(genomesubdir)+strlen("/")+ strlen(fileroot)+strlen(".chromosome.iit")+1,sizeof(char)); sprintf(iitfile,"%s/%s.chromosome.iit",genomesubdir,fileroot); IIT_write(iitfile,divlist,chrtypelist,/*fieldlist*/(List_T) NULL, intervaltable,labeltable,annottable,divsort, /*version, use 1 for backward compatibility*/1, /*label_pointers_8p*/false,/*annot_pointers_8p*/false); FREE(iitfile); List_free(&divlist); FREE(divstring); Table_free(&annottable); Table_free(&labeltable); Table_free(&intervaltable); List_free(&annotlist); /* Do not free strings in labellist, since they are not allocated */ List_free(&labellist); /* chrtypelist has no dynamically allocated strings */ List_free(&chrtypelist); for (p = intervallist; p != NULL; p = List_next(p)) { interval = (Interval_T) List_head(p); Interval_free(&interval); } List_free(&intervallist); return; }
int main() { FILE *fp = fopen("test.txt", "r"); int c = 0; int i = 0; char s[512]; Table_T our_table = Table_new(10, NULL, NULL); char * name; const char *fingerprint; const char* temp; while((c=fgetc(fp)) != ' ' && c != EOF) //Fingerprint section { s[i]= c; i++; } s[i] = '\0'; //Here we have fingerprint in s fingerprint = Atom_string(s); temp = Atom_string(s); i = 0; while((c = getc(fp)) != '\n' && c != EOF) { s[i] = c; i++; } s[i] = '\0'; //Here we have name in s name = s; Table_put(our_table, fingerprint, name); while( c != EOF) { i = 0; while((c = fgetc(fp)) != ' ' && c != EOF) // Fingerprint section { s[i] = c; i++; } s[i] = '\0'; fingerprint = Atom_string(s); i = 0; while((c = getc(fp)) != '\n' && c != EOF) { s[i] = c; i++; } s[i] = '\0'; name = s; printf("FP: %s\n ", fingerprint); printf("NAME: %s\n", name); char * test = Table_put(our_table, fingerprint, name); printf("idk: %s\n", test); // printf("VALUE STORED IN FP: %s\n\n", (char *)Table_get(our_table, temp)); } // NEED TO DEAL WITH TABLE MAP AND IMPLEMENTING A LIST // Table_map(our_table, vfree, NULL); //printf("%s", fingerprint); // printf("%s", (char *)Table_get(our_table, fingerprint)); printf("%d", Table_length(our_table)); // Table_map(our_table, vfree, NULL); int length = Table_length(our_table); void **table = Table_toArray(our_table, NULL); for(int x = 0; x < 2*length; x++) { printf("%s ", (char *)table[x]); } Table_free(&our_table); return 0; }
static void write_contig_file (char *genomesubdir, char *fileroot, Table_T accsegmentpos_table, Tableint_T chrlength_table, List_T contigtypelist) { FILE *textfp; char *textfile, *iitfile, *annot; int naccessions, i; char **accessions, *divstring; Segmentpos_T segmentpos; Chrom_T chrom; Genomicpos_T chroffset, universalpos1, universalpos2; List_T divlist = NULL, intervallist = NULL, labellist = NULL, annotlist = NULL, p; Table_T intervaltable, labeltable, annottable; Interval_T interval; #if 0 void **keys; int *values, ntypes; #endif if (divsort == NO_SORT) { accessions = (char **) Table_keys_by_timeindex(accsegmentpos_table,NULL); naccessions = Table_length(accsegmentpos_table); } else { /* Get accessions in order */ accessions = (char **) Table_keys(accsegmentpos_table,NULL); naccessions = Table_length(accsegmentpos_table); current_accsegmentpos_table = accsegmentpos_table; qsort(accessions,naccessions,sizeof(char *),bysegmentpos_compare); } #if 0 /* Get types in order */ keys = Tableint_keys(contigtype_table,NULL); values = Tableint_values(contigtype_table,0); ntypes = Tableint_length(contigtype_table); contigtypes = (char **) CALLOC(ntypes+1,sizeof(char *)); /* Add 1 for type 0 */ contigtypes[0] = ""; for (j = 0; j < ntypes; j++) { contigtypes[values[j]] = keys[j]; } FREE(values); FREE(keys); #endif /* Write contig text file */ textfile = (char *) CALLOC(strlen(genomesubdir)+strlen("/")+ strlen(fileroot)+strlen(".contig")+1,sizeof(char)); sprintf(textfile,"%s/%s.contig",genomesubdir,fileroot); /* Use binary, not text, so files are Unix-compatible */ if ((textfp = FOPEN_WRITE_BINARY(textfile)) == NULL) { fprintf(stderr,"Can't write to file %s\n",textfile); exit(9); } FREE(textfile); for (i = 0; i < naccessions; i++) { segmentpos = (Segmentpos_T) Table_get(accsegmentpos_table,(void *) accessions[i]); chrom = Segmentpos_chrom(segmentpos); chroffset = (Genomicpos_T) Tableint_get(chrlength_table,chrom); universalpos1 = chroffset + Segmentpos_chrpos1(segmentpos); universalpos2 = chroffset + Segmentpos_chrpos2(segmentpos); /* Print as 1-based, inclusive [a,b] */ if (Segmentpos_revcompp(segmentpos) == true) { fprintf(textfp,"%s\t%u..%u\t%s:%u..%u\t%u", accessions[i],universalpos2+1U,universalpos1, Chrom_string(chrom),Segmentpos_chrpos2(segmentpos)+1U,Segmentpos_chrpos1(segmentpos), Segmentpos_length(segmentpos)); } else { fprintf(textfp,"%s\t%u..%u\t%s:%u..%u\t%u", accessions[i],universalpos1+1U,universalpos2, Chrom_string(chrom),Segmentpos_chrpos1(segmentpos)+1U,Segmentpos_chrpos2(segmentpos), Segmentpos_length(segmentpos)); } #if 0 if (Segmentpos_type(segmentpos) > 0) { fprintf(textfp,"\t%s",contigtypes[Segmentpos_type(segmentpos)]); } #endif fprintf(textfp,"\n"); /* Store as 0-based, inclusive [a,b] */ labellist = List_push(labellist,(void *) accessions[i]); if (Segmentpos_revcompp(segmentpos) == true) { /* The negative sign in the interval is the indication that the contig was reverse complement */ intervallist = List_push(intervallist, (void *) Interval_new(universalpos2-1U,universalpos1, Segmentpos_type(segmentpos))); } else { intervallist = List_push(intervallist, (void *) Interval_new(universalpos1,universalpos2-1U, Segmentpos_type(segmentpos))); } #if 0 /* IIT version 1 */ sprintf(seglength,"%u",Segmentpos_length(segmentpos)); annot = (char *) CALLOC(strlen(seglength)+1,sizeof(char)); strcpy(annot,seglength); #else /* IIT versions >= 2 */ annot = (char *) CALLOC(1,sizeof(char)); annot[0] = '\0'; #endif annotlist = List_push(annotlist,(void *) annot); } fclose(textfp); #if 0 FREE(contigtypes); #endif FREE(accessions); intervallist = List_reverse(intervallist); /* contigtypelist = List_reverse(contigtypelist); -- Done by caller */ labellist = List_reverse(labellist); annotlist = List_reverse(annotlist); /* Write contig IIT file */ divstring = (char *) CALLOC(1,sizeof(char)); divstring[0] = '\0'; divlist = List_push(NULL,divstring); intervaltable = Table_new(65522,Table_string_compare,Table_string_hash); labeltable = Table_new(65522,Table_string_compare,Table_string_hash); annottable = Table_new(65522,Table_string_compare,Table_string_hash); Table_put(intervaltable,(void *) divstring,intervallist); Table_put(labeltable,(void *) divstring,labellist); Table_put(annottable,(void *) divstring,annotlist); iitfile = (char *) CALLOC(strlen(genomesubdir)+strlen("/")+ strlen(fileroot)+strlen(".contig.iit")+1,sizeof(char)); sprintf(iitfile,"%s/%s.contig.iit",genomesubdir,fileroot); #if 0 debug( for (p = contigtypelist; p != NULL; p = List_next(p)) { printf("Type %s\n",(char *) List_head(p)); } );